Merge "Rely on the platform -std default." am: 66f15cc01b am: 5a989a504a
am: 39ad1b92fa

Change-Id: Iec4bca8e96c776d61c5da5540a74acc6773c2bce
diff --git a/Android.mk b/Android.mk
index 6ce745d..491c8be 100644
--- a/Android.mk
+++ b/Android.mk
@@ -75,6 +75,7 @@
 	core/java/android/app/IAppTask.aidl \
 	core/java/android/app/ITaskStackListener.aidl \
 	core/java/android/app/IBackupAgent.aidl \
+	core/java/android/app/IEphemeralResolver.aidl \
 	core/java/android/app/IInstrumentationWatcher.aidl \
 	core/java/android/app/INotificationManager.aidl \
 	core/java/android/app/IProcessObserver.aidl \
@@ -199,6 +200,7 @@
 	core/java/android/net/ICaptivePortal.aidl \
 	core/java/android/net/IConnectivityManager.aidl \
 	core/java/android/net/IConnectivityMetricsLogger.aidl \
+	core/java/android/net/IIpConnectivityMetrics.aidl \
 	core/java/android/net/IEthernetManager.aidl \
 	core/java/android/net/IEthernetServiceListener.aidl \
 	core/java/android/net/INetworkManagementEventObserver.aidl \
@@ -306,9 +308,9 @@
 	core/java/com/android/internal/app/IAppOpsService.aidl \
 	core/java/com/android/internal/app/IAssistScreenshotReceiver.aidl \
 	core/java/com/android/internal/app/IBatteryStats.aidl \
-	core/java/com/android/internal/app/IEphemeralResolver.aidl \
 	core/java/com/android/internal/app/ISoundTriggerService.aidl \
 	core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl \
+	core/java/com/android/internal/app/IVoiceInteractionSessionListener.aidl \
 	core/java/com/android/internal/app/IVoiceInteractionSessionShowCallback.aidl \
 	core/java/com/android/internal/app/IVoiceInteractor.aidl \
 	core/java/com/android/internal/app/IVoiceInteractorCallback.aidl \
@@ -319,6 +321,7 @@
 	core/java/com/android/internal/appwidget/IAppWidgetHost.aidl \
 	core/java/com/android/internal/backup/IBackupTransport.aidl \
 	core/java/com/android/internal/backup/IObbBackupService.aidl \
+	core/java/com/android/internal/inputmethod/IInputContentUriToken.aidl \
 	core/java/com/android/internal/policy/IKeyguardDrawnCallback.aidl \
 	core/java/com/android/internal/policy/IKeyguardExitCallback.aidl \
 	core/java/com/android/internal/policy/IKeyguardService.aidl \
@@ -342,6 +345,7 @@
 	core/java/com/android/internal/view/IInputMethodManager.aidl \
 	core/java/com/android/internal/view/IInputMethodSession.aidl \
 	core/java/com/android/internal/view/IInputSessionCallback.aidl \
+	core/java/com/android/internal/widget/ICheckCredentialProgressCallback.aidl \
 	core/java/com/android/internal/widget/ILockSettings.aidl \
 	core/java/com/android/internal/widget/IRemoteViewsFactory.aidl \
 	core/java/com/android/internal/widget/IRemoteViewsAdapterConnection.aidl \
@@ -468,6 +472,9 @@
 	../../system/update_engine/binder_bindings/android/os/IUpdateEngine.aidl \
 	../../system/update_engine/binder_bindings/android/os/IUpdateEngineCallback.aidl \
 
+LOCAL_SRC_FILES +=  \
+	../../system/netd/server/binder/android/net/INetd.aidl \
+
 LOCAL_AIDL_INCLUDES += system/update_engine/binder_bindings
 
 # FRAMEWORKS_BASE_JAVA_SRC_DIRS comes from build/core/pathmap.mk
@@ -696,6 +703,7 @@
 	frameworks/base/core/java/android/service/quicksettings/Tile.aidl \
 	frameworks/native/aidl/binder/android/os/PersistableBundle.aidl \
 	system/netd/server/binder/android/net/UidRange.aidl \
+	frameworks/base/telephony/java/android/telephony/PcoData.aidl \
 
 gen := $(TARGET_OUT_COMMON_INTERMEDIATES)/framework.aidl
 $(gen): PRIVATE_SRC_FILES := $(aidl_files)
diff --git a/api/current.txt b/api/current.txt
index b391b00..d5b0426 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -397,6 +397,7 @@
     field public static final int colorPressedHighlight = 16843661; // 0x101038d
     field public static final int colorPrimary = 16843827; // 0x1010433
     field public static final int colorPrimaryDark = 16843828; // 0x1010434
+    field public static final int colorSecondary = 16844080; // 0x1010530
     field public static final int columnCount = 16843639; // 0x1010377
     field public static final int columnDelay = 16843215; // 0x10101cf
     field public static final int columnOrderPreserved = 16843640; // 0x1010378
@@ -420,7 +421,9 @@
     field public static final int contentInsetStart = 16843859; // 0x1010453
     field public static final int contentInsetStartWithNavigation = 16844066; // 0x1010522
     field public static final int contextClickable = 16844007; // 0x10104e7
+    field public static final int contextDescription = 16844078; // 0x101052e
     field public static final int contextPopupMenuStyle = 16844033; // 0x1010501
+    field public static final int contextUri = 16844077; // 0x101052d
     field public static final int controlX1 = 16843772; // 0x10103fc
     field public static final int controlX2 = 16843774; // 0x10103fe
     field public static final int controlY1 = 16843773; // 0x10103fd
@@ -1039,6 +1042,7 @@
     field public static final int rotation = 16843558; // 0x1010326
     field public static final int rotationX = 16843559; // 0x1010327
     field public static final int rotationY = 16843560; // 0x1010328
+    field public static final int roundIcon = 16844076; // 0x101052c
     field public static final int rowCount = 16843637; // 0x1010375
     field public static final int rowDelay = 16843216; // 0x10101d0
     field public static final int rowEdgeFlags = 16843329; // 0x1010241
@@ -1106,11 +1110,16 @@
     field public static final int shareInterpolator = 16843195; // 0x10101bb
     field public static final int sharedUserId = 16842763; // 0x101000b
     field public static final int sharedUserLabel = 16843361; // 0x1010261
+    field public static final int shortcutDisabledMessage = 16844075; // 0x101052b
+    field public static final int shortcutId = 16844072; // 0x1010528
+    field public static final int shortcutLongLabel = 16844074; // 0x101052a
+    field public static final int shortcutShortLabel = 16844073; // 0x1010529
     field public static final int shouldDisableView = 16843246; // 0x10101ee
     field public static final int showAsAction = 16843481; // 0x10102d9
     field public static final int showDefault = 16843258; // 0x10101fa
     field public static final int showDividers = 16843561; // 0x1010329
     field public static final int showForAllUsers = 16844015; // 0x10104ef
+    field public static final int showMetadataInPreview = 16844079; // 0x101052f
     field public static final deprecated int showOnLockScreen = 16843721; // 0x10103c9
     field public static final int showSilent = 16843259; // 0x10101fb
     field public static final int showText = 16843949; // 0x10104ad
@@ -3696,6 +3705,7 @@
     method public void moveTaskToFront(int, int);
     method public void moveTaskToFront(int, int, android.os.Bundle);
     method public deprecated void restartPackage(java.lang.String);
+    method public static void setVrThread(int);
     method public void setWatchHeapLimit(long);
     field public static final java.lang.String ACTION_REPORT_HEAP_LIMIT = "android.app.action.REPORT_HEAP_LIMIT";
     field public static final int LOCK_TASK_MODE_LOCKED = 1; // 0x1
@@ -5037,12 +5047,14 @@
     method public android.app.Notification.Action.Builder extend(android.app.Notification.Action.Builder);
     method public java.lang.CharSequence getCancelLabel();
     method public java.lang.CharSequence getConfirmLabel();
+    method public boolean getHintDisplayActionInline();
     method public boolean getHintLaunchesActivity();
     method public java.lang.CharSequence getInProgressLabel();
     method public boolean isAvailableOffline();
     method public android.app.Notification.Action.WearableExtender setAvailableOffline(boolean);
     method public android.app.Notification.Action.WearableExtender setCancelLabel(java.lang.CharSequence);
     method public android.app.Notification.Action.WearableExtender setConfirmLabel(java.lang.CharSequence);
+    method public android.app.Notification.Action.WearableExtender setHintDisplayActionInline(boolean);
     method public android.app.Notification.Action.WearableExtender setHintLaunchesActivity(boolean);
     method public android.app.Notification.Action.WearableExtender setInProgressLabel(java.lang.CharSequence);
   }
@@ -5756,7 +5768,10 @@
     method public android.content.pm.ServiceInfo getServiceInfo();
     method public java.lang.String getServiceName();
     method public java.lang.String getSettingsActivity();
+    method public boolean getShowMetadataInPreview();
     method public java.lang.CharSequence loadAuthor(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
+    method public java.lang.CharSequence loadContextDescription(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
+    method public android.net.Uri loadContextUri(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
     method public java.lang.CharSequence loadDescription(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
     method public android.graphics.drawable.Drawable loadIcon(android.content.pm.PackageManager);
     method public java.lang.CharSequence loadLabel(android.content.pm.PackageManager);
@@ -6484,11 +6499,13 @@
     method public android.content.res.Configuration getConfiguration();
     method public int getEventType();
     method public java.lang.String getPackageName();
+    method public java.lang.String getShortcutId();
     method public long getTimeStamp();
     field public static final int CONFIGURATION_CHANGE = 5; // 0x5
     field public static final int MOVE_TO_BACKGROUND = 2; // 0x2
     field public static final int MOVE_TO_FOREGROUND = 1; // 0x1
     field public static final int NONE = 0; // 0x0
+    field public static final int SHORTCUT_INVOCATION = 8; // 0x8
     field public static final int USER_INTERACTION = 7; // 0x7
   }
 
@@ -8191,6 +8208,7 @@
     field public static final java.lang.String RESTRICTIONS_SERVICE = "restrictions";
     field public static final java.lang.String SEARCH_SERVICE = "search";
     field public static final java.lang.String SENSOR_SERVICE = "sensor";
+    field public static final java.lang.String SHORTCUT_SERVICE = "shortcut";
     field public static final java.lang.String STORAGE_SERVICE = "storage";
     field public static final java.lang.String SYSTEM_HEALTH_SERVICE = "systemhealth";
     field public static final java.lang.String TELECOM_SERVICE = "telecom";
@@ -9500,13 +9518,20 @@
 
   public class LauncherApps {
     method public java.util.List<android.content.pm.LauncherActivityInfo> getActivityList(java.lang.String, android.os.UserHandle);
+    method public android.graphics.drawable.Drawable getShortcutBadgedIconDrawable(android.content.pm.ShortcutInfo, int);
+    method public android.graphics.drawable.Drawable getShortcutIconDrawable(android.content.pm.ShortcutInfo, int);
+    method public java.util.List<android.content.pm.ShortcutInfo> getShortcuts(android.content.pm.LauncherApps.ShortcutQuery, android.os.UserHandle);
+    method public boolean hasShortcutHostPermission();
     method public boolean isActivityEnabled(android.content.ComponentName, android.os.UserHandle);
     method public boolean isPackageEnabled(java.lang.String, android.os.UserHandle);
+    method public void pinShortcuts(java.lang.String, java.util.List<java.lang.String>, 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 android.content.pm.LauncherActivityInfo resolveActivity(android.content.Intent, 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 startShortcut(java.lang.String, java.lang.String, android.graphics.Rect, android.os.Bundle, android.os.UserHandle);
+    method public void startShortcut(android.content.pm.ShortcutInfo, android.graphics.Rect, android.os.Bundle);
     method public void unregisterCallback(android.content.pm.LauncherApps.Callback);
   }
 
@@ -9519,6 +9544,20 @@
     method public void onPackagesSuspended(java.lang.String[], android.os.UserHandle);
     method public abstract void onPackagesUnavailable(java.lang.String[], android.os.UserHandle, boolean);
     method public void onPackagesUnsuspended(java.lang.String[], android.os.UserHandle);
+    method public void onShortcutsChanged(java.lang.String, java.util.List<android.content.pm.ShortcutInfo>, android.os.UserHandle);
+  }
+
+  public static class LauncherApps.ShortcutQuery {
+    ctor public LauncherApps.ShortcutQuery();
+    method public android.content.pm.LauncherApps.ShortcutQuery setActivity(android.content.ComponentName);
+    method public android.content.pm.LauncherApps.ShortcutQuery setChangedSince(long);
+    method public android.content.pm.LauncherApps.ShortcutQuery setPackage(java.lang.String);
+    method public android.content.pm.LauncherApps.ShortcutQuery setQueryFlags(int);
+    method public android.content.pm.LauncherApps.ShortcutQuery setShortcutIds(java.util.List<java.lang.String>);
+    field public static final int FLAG_GET_KEY_FIELDS_ONLY = 4; // 0x4
+    field public static final int FLAG_MATCH_DYNAMIC = 1; // 0x1
+    field public static final int FLAG_MATCH_MANIFEST = 8; // 0x8
+    field public static final int FLAG_MATCH_PINNED = 2; // 0x2
   }
 
   public class PackageInfo implements android.os.Parcelable {
@@ -10019,6 +10058,66 @@
     field public java.lang.String permission;
   }
 
+  public final class ShortcutInfo implements android.os.Parcelable {
+    method public int describeContents();
+    method public android.content.ComponentName getActivity();
+    method public java.util.Set<java.lang.String> getCategories();
+    method public java.lang.CharSequence getDisabledMessage();
+    method public android.os.PersistableBundle getExtras();
+    method public java.lang.String getId();
+    method public android.content.Intent getIntent();
+    method public android.content.Intent[] getIntents();
+    method public long getLastChangedTimestamp();
+    method public java.lang.CharSequence getLongLabel();
+    method public java.lang.String getPackage();
+    method public int getRank();
+    method public java.lang.CharSequence getShortLabel();
+    method public android.os.UserHandle getUserHandle();
+    method public boolean hasKeyFieldsOnly();
+    method public boolean isDeclaredInManifest();
+    method public boolean isDynamic();
+    method public boolean isEnabled();
+    method public boolean isImmutable();
+    method public boolean isPinned();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.content.pm.ShortcutInfo> CREATOR;
+    field public static final java.lang.String SHORTCUT_CATEGORY_CONVERSATION = "android.shortcut.conversation";
+  }
+
+  public static class ShortcutInfo.Builder {
+    ctor public ShortcutInfo.Builder(android.content.Context, java.lang.String);
+    method public android.content.pm.ShortcutInfo build();
+    method public android.content.pm.ShortcutInfo.Builder setActivity(android.content.ComponentName);
+    method public android.content.pm.ShortcutInfo.Builder setCategories(java.util.Set<java.lang.String>);
+    method public android.content.pm.ShortcutInfo.Builder setDisabledMessage(java.lang.CharSequence);
+    method public android.content.pm.ShortcutInfo.Builder setExtras(android.os.PersistableBundle);
+    method public android.content.pm.ShortcutInfo.Builder setIcon(android.graphics.drawable.Icon);
+    method public android.content.pm.ShortcutInfo.Builder setIntent(android.content.Intent);
+    method public android.content.pm.ShortcutInfo.Builder setIntents(android.content.Intent[]);
+    method public android.content.pm.ShortcutInfo.Builder setLongLabel(java.lang.CharSequence);
+    method public android.content.pm.ShortcutInfo.Builder setRank(int);
+    method public android.content.pm.ShortcutInfo.Builder setShortLabel(java.lang.CharSequence);
+  }
+
+  public class ShortcutManager {
+    method public boolean addDynamicShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+    method public void disableShortcuts(java.util.List<java.lang.String>);
+    method public void disableShortcuts(java.util.List<java.lang.String>, java.lang.CharSequence);
+    method public void enableShortcuts(java.util.List<java.lang.String>);
+    method public java.util.List<android.content.pm.ShortcutInfo> getDynamicShortcuts();
+    method public int getIconMaxHeight();
+    method public int getIconMaxWidth();
+    method public java.util.List<android.content.pm.ShortcutInfo> getManifestShortcuts();
+    method public int getMaxShortcutCountPerActivity();
+    method public java.util.List<android.content.pm.ShortcutInfo> getPinnedShortcuts();
+    method public boolean isRateLimitingActive();
+    method public void removeAllDynamicShortcuts();
+    method public void removeDynamicShortcuts(java.util.List<java.lang.String>);
+    method public void reportShortcutUsed(java.lang.String);
+    method public boolean setDynamicShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+    method public boolean updateShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+  }
+
   public class Signature implements android.os.Parcelable {
     ctor public Signature(byte[]);
     ctor public Signature(java.lang.String);
@@ -10225,7 +10324,7 @@
   }
 
   public class Resources {
-    ctor public Resources(android.content.res.AssetManager, android.util.DisplayMetrics, android.content.res.Configuration);
+    ctor public deprecated Resources(android.content.res.AssetManager, android.util.DisplayMetrics, android.content.res.Configuration);
     method public final void finishPreloading();
     method public final void flushLayoutCache();
     method public android.content.res.XmlResourceParser getAnimation(int) throws android.content.res.Resources.NotFoundException;
@@ -10276,7 +10375,7 @@
     method public android.content.res.AssetFileDescriptor openRawResourceFd(int) throws android.content.res.Resources.NotFoundException;
     method public void parseBundleExtra(java.lang.String, android.util.AttributeSet, android.os.Bundle) throws org.xmlpull.v1.XmlPullParserException;
     method public void parseBundleExtras(android.content.res.XmlResourceParser, android.os.Bundle) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
-    method public void updateConfiguration(android.content.res.Configuration, android.util.DisplayMetrics);
+    method public deprecated void updateConfiguration(android.content.res.Configuration, android.util.DisplayMetrics);
   }
 
   public static class Resources.NotFoundException extends java.lang.RuntimeException {
@@ -19556,6 +19655,7 @@
     field public static final android.os.Parcelable.Creator<android.media.AudioFormat> CREATOR;
     field public static final int ENCODING_AC3 = 5; // 0x5
     field public static final int ENCODING_DEFAULT = 1; // 0x1
+    field public static final int ENCODING_DOLBY_TRUEHD = 14; // 0xe
     field public static final int ENCODING_DTS = 7; // 0x7
     field public static final int ENCODING_DTS_HD = 8; // 0x8
     field public static final int ENCODING_E_AC3 = 6; // 0x6
@@ -28268,6 +28368,7 @@
     field public static final int LOLLIPOP_MR1 = 22; // 0x16
     field public static final int M = 23; // 0x17
     field public static final int N = 24; // 0x18
+    field public static final int N_MR1 = 25; // 0x19
   }
 
   public final class Bundle extends android.os.BaseBundle implements java.lang.Cloneable android.os.Parcelable {
@@ -29261,6 +29362,7 @@
     method public android.os.Bundle getUserRestrictions();
     method public android.os.Bundle getUserRestrictions(android.os.UserHandle);
     method public boolean hasUserRestriction(java.lang.String);
+    method public boolean isDemoUser();
     method public boolean isQuietModeEnabled(android.os.UserHandle);
     method public boolean isSystemUser();
     method public boolean isUserAGoat();
@@ -29500,6 +29602,7 @@
     method public boolean isObbMounted(java.lang.String);
     method public boolean mountObb(java.lang.String, java.lang.String, android.os.storage.OnObbStateChangeListener);
     method public boolean unmountObb(java.lang.String, boolean, android.os.storage.OnObbStateChangeListener);
+    field public static final java.lang.String ACTION_MANAGE_STORAGE = "android.os.storage.action.MANAGE_STORAGE";
   }
 
   public final class StorageVolume implements android.os.Parcelable {
@@ -30642,6 +30745,7 @@
   public static class CallLog.Calls implements android.provider.BaseColumns {
     ctor public CallLog.Calls();
     method public static java.lang.String getLastOutgoingCall(android.content.Context);
+    field public static final int ANSWERED_EXTERNALLY_TYPE = 7; // 0x7
     field public static final int BLOCKED_TYPE = 6; // 0x6
     field public static final java.lang.String CACHED_FORMATTED_NUMBER = "formatted_number";
     field public static final java.lang.String CACHED_LOOKUP_URI = "lookup_uri";
@@ -30664,6 +30768,7 @@
     field public static final java.lang.String DURATION = "duration";
     field public static final java.lang.String EXTRA_CALL_TYPE_FILTER = "android.provider.extra.CALL_TYPE_FILTER";
     field public static final java.lang.String FEATURES = "features";
+    field public static final int FEATURES_PULLED_EXTERNALLY = 2; // 0x2
     field public static final int FEATURES_VIDEO = 1; // 0x1
     field public static final java.lang.String GEOCODED_LOCATION = "geocoded_location";
     field public static final int INCOMING_TYPE = 1; // 0x1
@@ -32369,6 +32474,7 @@
     field public static final java.lang.String DATA_ROAMING = "data_roaming";
     field public static final java.lang.String DEBUG_APP = "debug_app";
     field public static final java.lang.String DEVELOPMENT_SETTINGS_ENABLED = "development_settings_enabled";
+    field public static final java.lang.String DEVICE_NAME = "device_name";
     field public static final java.lang.String DEVICE_PROVISIONED = "device_provisioned";
     field public static final java.lang.String HTTP_PROXY = "http_proxy";
     field public static final deprecated java.lang.String INSTALL_NON_MARKET_APPS = "install_non_market_apps";
@@ -32954,6 +33060,7 @@
     field public static final java.lang.String ACTION_NEW_VOICEMAIL = "android.intent.action.NEW_VOICEMAIL";
     field public static final java.lang.String ACTION_SYNC_VOICEMAIL = "android.provider.action.SYNC_VOICEMAIL";
     field public static final java.lang.String AUTHORITY = "com.android.voicemail";
+    field public static final java.lang.String EXTRA_PHONE_ACCOUNT_HANDLE = "android.provider.extra.PHONE_ACCOUNT_HANDLE";
     field public static final java.lang.String EXTRA_SELF_CHANGE = "com.android.voicemail.extra.SELF_CHANGE";
     field public static final java.lang.String PARAM_KEY_SOURCE_PACKAGE = "source_package";
   }
@@ -32962,6 +33069,9 @@
     method public static android.net.Uri buildSourceUri(java.lang.String);
     field public static final java.lang.String CONFIGURATION_STATE = "configuration_state";
     field public static final int CONFIGURATION_STATE_CAN_BE_CONFIGURED = 2; // 0x2
+    field public static final int CONFIGURATION_STATE_CONFIGURING = 3; // 0x3
+    field public static final int CONFIGURATION_STATE_DISABLED = 5; // 0x5
+    field public static final int CONFIGURATION_STATE_FAILED = 4; // 0x4
     field public static final int CONFIGURATION_STATE_NOT_CONFIGURED = 1; // 0x1
     field public static final int CONFIGURATION_STATE_OK = 0; // 0x0
     field public static final android.net.Uri CONTENT_URI;
@@ -32986,6 +33096,7 @@
     field public static final int QUOTA_UNAVAILABLE = -1; // 0xffffffff
     field public static final java.lang.String SETTINGS_URI = "settings_uri";
     field public static final java.lang.String SOURCE_PACKAGE = "source_package";
+    field public static final java.lang.String SOURCE_TYPE = "source_type";
     field public static final java.lang.String VOICEMAIL_ACCESS_URI = "voicemail_access_uri";
   }
 
@@ -35945,9 +36056,14 @@
     method public void phoneAccountSelected(android.telecom.PhoneAccountHandle, boolean);
     method public void playDtmfTone(char);
     method public void postDialContinue(boolean);
+    method public void pullExternalCall();
+    method public final void putExtras(android.os.Bundle);
     method public void registerCallback(android.telecom.Call.Callback);
     method public void registerCallback(android.telecom.Call.Callback, android.os.Handler);
     method public void reject(boolean, java.lang.String);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
+    method public void sendCallEvent(java.lang.String, android.os.Bundle);
     method public void splitFromConference();
     method public void stopDtmfTone();
     method public void swapConference();
@@ -35961,6 +36077,7 @@
     field public static final int STATE_DISCONNECTING = 10; // 0xa
     field public static final int STATE_HOLDING = 3; // 0x3
     field public static final int STATE_NEW = 0; // 0x0
+    field public static final int STATE_PULLING_CALL = 11; // 0xb
     field public static final int STATE_RINGING = 2; // 0x2
     field public static final int STATE_SELECT_PHONE_ACCOUNT = 8; // 0x8
   }
@@ -35971,6 +36088,7 @@
     method public void onCannedTextResponsesLoaded(android.telecom.Call, java.util.List<java.lang.String>);
     method public void onChildrenChanged(android.telecom.Call, java.util.List<android.telecom.Call>);
     method public void onConferenceableCallsChanged(android.telecom.Call, java.util.List<android.telecom.Call>);
+    method public void onConnectionEvent(android.telecom.Call, java.lang.String, android.os.Bundle);
     method public void onDetailsChanged(android.telecom.Call, android.telecom.Call.Details);
     method public void onParentChanged(android.telecom.Call, android.telecom.Call);
     method public void onPostDialWait(android.telecom.Call, java.lang.String);
@@ -36001,6 +36119,7 @@
     method public static java.lang.String propertiesToString(int);
     field public static final int CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO = 4194304; // 0x400000
     field public static final int CAPABILITY_CAN_PAUSE_VIDEO = 1048576; // 0x100000
+    field public static final int CAPABILITY_CAN_PULL_CALL = 8388608; // 0x800000
     field public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 8192; // 0x2000
     field public static final int CAPABILITY_HOLD = 1; // 0x1
     field public static final int CAPABILITY_MANAGE_CONFERENCE = 128; // 0x80
@@ -36020,7 +36139,9 @@
     field public static final int PROPERTY_EMERGENCY_CALLBACK_MODE = 4; // 0x4
     field public static final int PROPERTY_ENTERPRISE_CALL = 32; // 0x20
     field public static final int PROPERTY_GENERIC_CONFERENCE = 2; // 0x2
+    field public static final int PROPERTY_HAS_CDMA_VOICE_PRIVACY = 128; // 0x80
     field public static final int PROPERTY_HIGH_DEF_AUDIO = 16; // 0x10
+    field public static final int PROPERTY_IS_EXTERNAL_CALL = 64; // 0x40
     field public static final int PROPERTY_WIFI = 8; // 0x8
   }
 
@@ -36071,6 +36192,7 @@
     method public final android.telecom.CallAudioState getCallAudioState();
     method public final java.util.List<android.telecom.Connection> getConferenceableConnections();
     method public final int getConnectionCapabilities();
+    method public final int getConnectionProperties();
     method public final long getConnectionTime();
     method public final java.util.List<android.telecom.Connection> getConnections();
     method public final android.telecom.DisconnectCause getDisconnectCause();
@@ -36083,6 +36205,7 @@
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
     method public void onConnectionAdded(android.telecom.Connection);
     method public void onDisconnect();
+    method public void onExtrasChanged(android.os.Bundle);
     method public void onHold();
     method public void onMerge(android.telecom.Connection);
     method public void onMerge();
@@ -36091,10 +36214,14 @@
     method public void onStopDtmfTone();
     method public void onSwap();
     method public void onUnhold();
+    method public final void putExtras(android.os.Bundle);
     method public final void removeConnection(android.telecom.Connection);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
     method public final void setActive();
     method public final void setConferenceableConnections(java.util.List<android.telecom.Connection>);
     method public final void setConnectionCapabilities(int);
+    method public final void setConnectionProperties(int);
     method public final void setConnectionTime(long);
     method public final void setDialing();
     method public final void setDisconnected(android.telecom.DisconnectCause);
@@ -36124,6 +36251,7 @@
     method public final android.telecom.Conference getConference();
     method public final java.util.List<android.telecom.Conferenceable> getConferenceables();
     method public final int getConnectionCapabilities();
+    method public final int getConnectionProperties();
     method public final android.telecom.DisconnectCause getDisconnectCause();
     method public final android.os.Bundle getExtras();
     method public final int getState();
@@ -36134,16 +36262,24 @@
     method public void onAnswer(int);
     method public void onAnswer();
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
+    method public void onCallEvent(java.lang.String, android.os.Bundle);
     method public void onDisconnect();
+    method public void onExtrasChanged(android.os.Bundle);
     method public void onHold();
     method public void onPlayDtmfTone(char);
     method public void onPostDialContinue(boolean);
+    method public void onPullExternalCall();
     method public void onReject();
     method public void onReject(java.lang.String);
     method public void onSeparate();
     method public void onStateChanged(int);
     method public void onStopDtmfTone();
     method public void onUnhold();
+    method public static java.lang.String propertiesToString(int);
+    method public final void putExtras(android.os.Bundle);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
+    method public void sendConnectionEvent(java.lang.String, android.os.Bundle);
     method public final void setActive();
     method public final void setAddress(android.net.Uri, int);
     method public final void setAudioModeIsVoip(boolean);
@@ -36151,6 +36287,7 @@
     method public final void setConferenceableConnections(java.util.List<android.telecom.Connection>);
     method public final void setConferenceables(java.util.List<android.telecom.Conferenceable>);
     method public final void setConnectionCapabilities(int);
+    method public final void setConnectionProperties(int);
     method public final void setDialing();
     method public final void setDisconnected(android.telecom.DisconnectCause);
     method public final void setExtras(android.os.Bundle);
@@ -36159,6 +36296,7 @@
     method public final void setNextPostDialChar(char);
     method public final void setOnHold();
     method public final void setPostDialWait(java.lang.String);
+    method public final void setPulling();
     method public final void setRingbackRequested(boolean);
     method public final void setRinging();
     method public final void setStatusHints(android.telecom.StatusHints);
@@ -36167,6 +36305,7 @@
     method public static java.lang.String stateToString(int);
     field public static final int CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO = 8388608; // 0x800000
     field public static final int CAPABILITY_CAN_PAUSE_VIDEO = 1048576; // 0x100000
+    field public static final int CAPABILITY_CAN_PULL_CALL = 16777216; // 0x1000000
     field public static final int CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION = 4194304; // 0x400000
     field public static final int CAPABILITY_CAN_UPGRADE_TO_VIDEO = 524288; // 0x80000
     field public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 8192; // 0x2000
@@ -36184,15 +36323,21 @@
     field public static final int CAPABILITY_SUPPORTS_VT_REMOTE_TX = 2048; // 0x800
     field public static final int CAPABILITY_SUPPORT_HOLD = 2; // 0x2
     field public static final int CAPABILITY_SWAP_CONFERENCE = 8; // 0x8
+    field public static final java.lang.String EVENT_CALL_MERGE_FAILED = "android.telecom.event.CALL_MERGE_FAILED";
+    field public static final java.lang.String EVENT_CALL_PULL_FAILED = "android.telecom.event.CALL_PULL_FAILED";
+    field public static final java.lang.String EXTRA_ANSWERING_DROPS_FG_CALL = "android.telecom.extra.ANSWERING_DROPS_FG_CALL";
     field public static final java.lang.String EXTRA_CALL_SUBJECT = "android.telecom.extra.CALL_SUBJECT";
     field public static final java.lang.String EXTRA_CHILD_ADDRESS = "android.telecom.extra.CHILD_ADDRESS";
     field public static final java.lang.String EXTRA_LAST_FORWARDED_NUMBER = "android.telecom.extra.LAST_FORWARDED_NUMBER";
+    field public static final int PROPERTY_HAS_CDMA_VOICE_PRIVACY = 32; // 0x20
+    field public static final int PROPERTY_IS_EXTERNAL_CALL = 16; // 0x10
     field public static final int STATE_ACTIVE = 4; // 0x4
     field public static final int STATE_DIALING = 3; // 0x3
     field public static final int STATE_DISCONNECTED = 6; // 0x6
     field public static final int STATE_HOLDING = 5; // 0x5
     field public static final int STATE_INITIALIZING = 0; // 0x0
     field public static final int STATE_NEW = 1; // 0x1
+    field public static final int STATE_PULLING_CALL = 7; // 0x7
     field public static final int STATE_RINGING = 2; // 0x2
   }
 
@@ -36270,7 +36415,9 @@
     method public java.lang.String getReason();
     method public int getTone();
     method public void writeToParcel(android.os.Parcel, int);
+    field public static final int ANSWERED_ELSEWHERE = 11; // 0xb
     field public static final int BUSY = 7; // 0x7
+    field public static final int CALL_PULLED = 12; // 0xc
     field public static final int CANCELED = 4; // 0x4
     field public static final int CONNECTION_MANAGER_NOT_SUPPORTED = 10; // 0xa
     field public static final android.os.Parcelable.Creator<android.telecom.DisconnectCause> CREATOR;
@@ -36306,6 +36453,7 @@
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
     method public void onCallRemoved(android.telecom.Call);
     method public void onCanAddCallChanged(boolean);
+    method public void onConnectionEvent(android.telecom.Call, java.lang.String, android.os.Bundle);
     method public void onSilenceRinger();
     method public final void setAudioRoute(int);
     method public final void setMuted(boolean);
@@ -36428,6 +36576,7 @@
     method public void onConferenceableConnectionsChanged(android.telecom.RemoteConference, java.util.List<android.telecom.RemoteConnection>);
     method public void onConnectionAdded(android.telecom.RemoteConference, android.telecom.RemoteConnection);
     method public void onConnectionCapabilitiesChanged(android.telecom.RemoteConference, int);
+    method public void onConnectionPropertiesChanged(android.telecom.RemoteConference, int);
     method public void onConnectionRemoved(android.telecom.RemoteConference, android.telecom.RemoteConnection);
     method public void onDestroyed(android.telecom.RemoteConference);
     method public void onDisconnected(android.telecom.RemoteConference, android.telecom.DisconnectCause);
@@ -36446,6 +36595,7 @@
     method public android.telecom.RemoteConference getConference();
     method public java.util.List<android.telecom.RemoteConnection> getConferenceableConnections();
     method public int getConnectionCapabilities();
+    method public int getConnectionProperties();
     method public android.telecom.DisconnectCause getDisconnectCause();
     method public final android.os.Bundle getExtras();
     method public int getState();
@@ -36457,6 +36607,7 @@
     method public boolean isVoipAudioMode();
     method public void playDtmfTone(char);
     method public void postDialContinue(boolean);
+    method public void pullExternalCall();
     method public void registerCallback(android.telecom.RemoteConnection.Callback);
     method public void registerCallback(android.telecom.RemoteConnection.Callback, android.os.Handler);
     method public void reject();
@@ -36473,6 +36624,8 @@
     method public void onConferenceChanged(android.telecom.RemoteConnection, android.telecom.RemoteConference);
     method public void onConferenceableConnectionsChanged(android.telecom.RemoteConnection, java.util.List<android.telecom.RemoteConnection>);
     method public void onConnectionCapabilitiesChanged(android.telecom.RemoteConnection, int);
+    method public void onConnectionEvent(android.telecom.RemoteConnection, java.lang.String, android.os.Bundle);
+    method public void onConnectionPropertiesChanged(android.telecom.RemoteConnection, int);
     method public void onDestroyed(android.telecom.RemoteConnection);
     method public void onDisconnected(android.telecom.RemoteConnection, android.telecom.DisconnectCause);
     method public void onExtrasChanged(android.telecom.RemoteConnection, android.os.Bundle);
@@ -36569,6 +36722,7 @@
     field public static final java.lang.String EXTRA_START_CALL_WITH_VIDEO_STATE = "android.telecom.extra.START_CALL_WITH_VIDEO_STATE";
     field public static final java.lang.String GATEWAY_ORIGINAL_ADDRESS = "android.telecom.extra.GATEWAY_ORIGINAL_ADDRESS";
     field public static final java.lang.String GATEWAY_PROVIDER_PACKAGE = "android.telecom.extra.GATEWAY_PROVIDER_PACKAGE";
+    field public static final java.lang.String METADATA_INCLUDE_EXTERNAL_CALLS = "android.telecom.INCLUDE_EXTERNAL_CALLS";
     field public static final java.lang.String METADATA_IN_CALL_SERVICE_RINGING = "android.telecom.IN_CALL_SERVICE_RINGING";
     field public static final java.lang.String METADATA_IN_CALL_SERVICE_UI = "android.telecom.IN_CALL_SERVICE_UI";
     field public static final int PRESENTATION_ALLOWED = 1; // 0x1
@@ -36623,9 +36777,11 @@
     field public static final java.lang.String ACTION_CARRIER_CONFIG_CHANGED = "android.telephony.action.CARRIER_CONFIG_CHANGED";
     field public static final java.lang.String KEY_ADDITIONAL_CALL_SETTING_BOOL = "additional_call_setting_bool";
     field public static final java.lang.String KEY_ALLOW_ADDING_APNS_BOOL = "allow_adding_apns_bool";
+    field public static final java.lang.String KEY_ALLOW_ADD_CALL_DURING_VIDEO_CALL_BOOL = "allow_add_call_during_video_call";
     field public static final java.lang.String KEY_ALLOW_EMERGENCY_NUMBERS_IN_CALL_LOG_BOOL = "allow_emergency_numbers_in_call_log_bool";
     field public static final java.lang.String KEY_ALLOW_EMERGENCY_VIDEO_CALLS_BOOL = "allow_emergency_video_calls_bool";
     field public static final java.lang.String KEY_ALLOW_LOCAL_DTMF_TONES_BOOL = "allow_local_dtmf_tones_bool";
+    field public static final java.lang.String KEY_ALLOW_MERGE_WIFI_CALLS_WHEN_VOWIFI_OFF_BOOL = "allow_merge_wifi_calls_when_vowifi_off_bool";
     field public static final java.lang.String KEY_ALLOW_NON_EMERGENCY_CALLS_IN_ECM_BOOL = "allow_non_emergency_calls_in_ecm_bool";
     field public static final java.lang.String KEY_ALWAYS_SHOW_EMERGENCY_ALERT_ONOFF_BOOL = "always_show_emergency_alert_onoff_bool";
     field public static final java.lang.String KEY_APN_EXPAND_BOOL = "apn_expand_bool";
@@ -36657,6 +36813,7 @@
     field public static final java.lang.String KEY_CSP_ENABLED_BOOL = "csp_enabled_bool";
     field public static final java.lang.String KEY_DEFAULT_SIM_CALL_MANAGER_STRING = "default_sim_call_manager_string";
     field public static final java.lang.String KEY_DISABLE_CDMA_ACTIVATION_CODE_BOOL = "disable_cdma_activation_code_bool";
+    field public static final java.lang.String KEY_DROP_VIDEO_CALL_WHEN_ANSWERING_AUDIO_CALL_BOOL = "drop_video_call_when_answering_audio_call_bool";
     field public static final java.lang.String KEY_DTMF_TYPE_ENABLED_BOOL = "dtmf_type_enabled_bool";
     field public static final java.lang.String KEY_DURATION_BLOCKING_DISABLED_AFTER_EMERGENCY_INT = "duration_blocking_disabled_after_emergency_int";
     field public static final java.lang.String KEY_EDITABLE_ENHANCED_4G_LTE_BOOL = "editable_enhanced_4g_lte_bool";
@@ -36714,6 +36871,7 @@
     field public static final java.lang.String KEY_SUPPORT_CONFERENCE_CALL_BOOL = "support_conference_call_bool";
     field public static final java.lang.String KEY_SUPPORT_PAUSE_IMS_VIDEO_CALLS_BOOL = "support_pause_ims_video_calls_bool";
     field public static final java.lang.String KEY_SUPPORT_SWAP_AFTER_MERGE_BOOL = "support_swap_after_merge_bool";
+    field public static final java.lang.String KEY_TREAT_DOWNGRADED_VIDEO_CALLS_AS_VIDEO_CALLS_BOOL = "treat_downgraded_video_calls_as_video_calls_bool";
     field public static final java.lang.String KEY_USE_HFA_FOR_PROVISIONING_BOOL = "use_hfa_for_provisioning_bool";
     field public static final java.lang.String KEY_USE_OTASP_FOR_PROVISIONING_BOOL = "use_otasp_for_provisioning_bool";
     field public static final java.lang.String KEY_USE_RCS_PRESENCE_BOOL = "use_rcs_presence_bool";
@@ -37298,12 +37456,15 @@
     field public static final int NETWORK_TYPE_EVDO_A = 6; // 0x6
     field public static final int NETWORK_TYPE_EVDO_B = 12; // 0xc
     field public static final int NETWORK_TYPE_GPRS = 1; // 0x1
+    field public static final int NETWORK_TYPE_GSM = 16; // 0x10
     field public static final int NETWORK_TYPE_HSDPA = 8; // 0x8
     field public static final int NETWORK_TYPE_HSPA = 10; // 0xa
     field public static final int NETWORK_TYPE_HSPAP = 15; // 0xf
     field public static final int NETWORK_TYPE_HSUPA = 9; // 0x9
     field public static final int NETWORK_TYPE_IDEN = 11; // 0xb
+    field public static final int NETWORK_TYPE_IWLAN = 18; // 0x12
     field public static final int NETWORK_TYPE_LTE = 13; // 0xd
+    field public static final int NETWORK_TYPE_TD_SCDMA = 17; // 0x11
     field public static final int NETWORK_TYPE_UMTS = 3; // 0x3
     field public static final int NETWORK_TYPE_UNKNOWN = 0; // 0x0
     field public static final int PHONE_TYPE_CDMA = 2; // 0x2
@@ -40104,7 +40265,10 @@
     method public boolean equals(android.util.DisplayMetrics);
     method public void setTo(android.util.DisplayMetrics);
     method public void setToDefaults();
+    field public static final int DENSITY_260 = 260; // 0x104
     field public static final int DENSITY_280 = 280; // 0x118
+    field public static final int DENSITY_300 = 300; // 0x12c
+    field public static final int DENSITY_340 = 340; // 0x154
     field public static final int DENSITY_360 = 360; // 0x168
     field public static final int DENSITY_400 = 400; // 0x190
     field public static final int DENSITY_420 = 420; // 0x1a4
@@ -41434,6 +41598,10 @@
     field public static final int KEYCODE_SWITCH_CHARSET = 95; // 0x5f
     field public static final int KEYCODE_SYM = 63; // 0x3f
     field public static final int KEYCODE_SYSRQ = 120; // 0x78
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_DOWN = 281; // 0x119
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_LEFT = 282; // 0x11a
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_RIGHT = 283; // 0x11b
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_UP = 280; // 0x118
     field public static final int KEYCODE_T = 48; // 0x30
     field public static final int KEYCODE_TAB = 61; // 0x3d
     field public static final int KEYCODE_TV = 170; // 0xaa
@@ -42316,6 +42484,7 @@
     method public float getPivotY();
     method public android.view.PointerIcon getPointerIcon();
     method public android.content.res.Resources getResources();
+    method public final boolean getRevealOnFocusHint();
     method public final int getRight();
     method protected float getRightFadingEdgeStrength();
     method protected int getRightPaddingOffset();
@@ -42603,6 +42772,7 @@
     method public void setPivotY(float);
     method public void setPointerIcon(android.view.PointerIcon);
     method public void setPressed(boolean);
+    method public final void setRevealOnFocusHint(boolean);
     method public final void setRight(int);
     method public void setRotation(float);
     method public void setRotationX(float);
@@ -43752,6 +43922,7 @@
     field public static final int TYPE_APPLICATION_SUB_PANEL = 1002; // 0x3ea
     field public static final int TYPE_BASE_APPLICATION = 1; // 0x1
     field public static final int TYPE_CHANGED = 2; // 0x2
+    field public static final int TYPE_DRAWN_APPLICATION = 4; // 0x4
     field public static final int TYPE_INPUT_METHOD = 2011; // 0x7db
     field public static final int TYPE_INPUT_METHOD_DIALOG = 2012; // 0x7dc
     field public static final int TYPE_KEYGUARD_DIALOG = 2009; // 0x7d9
@@ -44524,6 +44695,7 @@
     method public boolean clearMetaKeyStates(int);
     method public void closeConnection();
     method public boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public boolean commitText(java.lang.CharSequence, int);
     method public boolean deleteSurroundingText(int, int);
@@ -44633,6 +44805,7 @@
     field public static final int IME_NULL = 0; // 0x0
     field public int actionId;
     field public java.lang.CharSequence actionLabel;
+    field public java.lang.String[] contentMimeTypes;
     field public android.os.Bundle extras;
     field public int fieldId;
     field public java.lang.String fieldName;
@@ -44692,6 +44865,7 @@
     method public abstract boolean clearMetaKeyStates(int);
     method public abstract void closeConnection();
     method public abstract boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public abstract boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public abstract boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public abstract boolean commitText(java.lang.CharSequence, int);
     method public abstract boolean deleteSurroundingText(int, int);
@@ -44717,6 +44891,7 @@
     field public static final int CURSOR_UPDATE_MONITOR = 2; // 0x2
     field public static final int GET_EXTRACTED_TEXT_MONITOR = 1; // 0x1
     field public static final int GET_TEXT_WITH_STYLES = 1; // 0x1
+    field public static final int INPUT_CONTENT_GRANT_READ_URI_PERMISSION = 1; // 0x1
   }
 
   public class InputConnectionWrapper implements android.view.inputmethod.InputConnection {
@@ -44725,6 +44900,7 @@
     method public boolean clearMetaKeyStates(int);
     method public void closeConnection();
     method public boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public boolean commitText(java.lang.CharSequence, int);
     method public boolean deleteSurroundingText(int, int);
@@ -44749,6 +44925,19 @@
     method public void setTarget(android.view.inputmethod.InputConnection);
   }
 
+  public final class InputContentInfo implements android.os.Parcelable {
+    ctor public InputContentInfo(android.net.Uri, android.content.ClipDescription);
+    ctor public InputContentInfo(android.net.Uri, android.content.ClipDescription, android.net.Uri);
+    method public int describeContents();
+    method public android.net.Uri getContentUri();
+    method public android.content.ClipDescription getDescription();
+    method public android.net.Uri getLinkUri();
+    method public void releasePermission();
+    method public void requestPermission();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.view.inputmethod.InputContentInfo> CREATOR;
+  }
+
   public abstract interface InputMethod {
     method public abstract void attachToken(android.os.IBinder);
     method public abstract void bindInput(android.view.inputmethod.InputBinding);
diff --git a/api/system-current.txt b/api/system-current.txt
index 68c7b48..db5273b 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -504,6 +504,7 @@
     field public static final int colorPressedHighlight = 16843661; // 0x101038d
     field public static final int colorPrimary = 16843827; // 0x1010433
     field public static final int colorPrimaryDark = 16843828; // 0x1010434
+    field public static final int colorSecondary = 16844080; // 0x1010530
     field public static final int columnCount = 16843639; // 0x1010377
     field public static final int columnDelay = 16843215; // 0x10101cf
     field public static final int columnOrderPreserved = 16843640; // 0x1010378
@@ -527,7 +528,9 @@
     field public static final int contentInsetStart = 16843859; // 0x1010453
     field public static final int contentInsetStartWithNavigation = 16844066; // 0x1010522
     field public static final int contextClickable = 16844007; // 0x10104e7
+    field public static final int contextDescription = 16844078; // 0x101052e
     field public static final int contextPopupMenuStyle = 16844033; // 0x1010501
+    field public static final int contextUri = 16844077; // 0x101052d
     field public static final int controlX1 = 16843772; // 0x10103fc
     field public static final int controlX2 = 16843774; // 0x10103fe
     field public static final int controlY1 = 16843773; // 0x10103fd
@@ -1146,6 +1149,7 @@
     field public static final int rotation = 16843558; // 0x1010326
     field public static final int rotationX = 16843559; // 0x1010327
     field public static final int rotationY = 16843560; // 0x1010328
+    field public static final int roundIcon = 16844076; // 0x101052c
     field public static final int rowCount = 16843637; // 0x1010375
     field public static final int rowDelay = 16843216; // 0x10101d0
     field public static final int rowEdgeFlags = 16843329; // 0x1010241
@@ -1217,11 +1221,16 @@
     field public static final int shareInterpolator = 16843195; // 0x10101bb
     field public static final int sharedUserId = 16842763; // 0x101000b
     field public static final int sharedUserLabel = 16843361; // 0x1010261
+    field public static final int shortcutDisabledMessage = 16844075; // 0x101052b
+    field public static final int shortcutId = 16844072; // 0x1010528
+    field public static final int shortcutLongLabel = 16844074; // 0x101052a
+    field public static final int shortcutShortLabel = 16844073; // 0x1010529
     field public static final int shouldDisableView = 16843246; // 0x10101ee
     field public static final int showAsAction = 16843481; // 0x10102d9
     field public static final int showDefault = 16843258; // 0x10101fa
     field public static final int showDividers = 16843561; // 0x1010329
     field public static final int showForAllUsers = 16844015; // 0x10104ef
+    field public static final int showMetadataInPreview = 16844079; // 0x101052f
     field public static final deprecated int showOnLockScreen = 16843721; // 0x10103c9
     field public static final int showSilent = 16843259; // 0x10101fb
     field public static final int showText = 16843949; // 0x10104ad
@@ -3832,6 +3841,7 @@
     method public void moveTaskToFront(int, int);
     method public void moveTaskToFront(int, int, android.os.Bundle);
     method public deprecated void restartPackage(java.lang.String);
+    method public static void setVrThread(int);
     method public void setWatchHeapLimit(long);
     field public static final java.lang.String ACTION_REPORT_HEAP_LIMIT = "android.app.action.REPORT_HEAP_LIMIT";
     field public static final int LOCK_TASK_MODE_LOCKED = 1; // 0x1
@@ -4515,6 +4525,15 @@
     field public static final int VISIBILITY_VISIBLE_NOTIFY_ONLY_COMPLETION = 3; // 0x3
   }
 
+  public abstract class EphemeralResolverService extends android.app.Service {
+    ctor public EphemeralResolverService();
+    method public final void attachBaseContext(android.content.Context);
+    method public final android.os.IBinder onBind(android.content.Intent);
+    method public abstract java.util.List<android.content.pm.EphemeralResolveInfo> onEphemeralResolveInfoList(int[], int);
+    field public static final java.lang.String EXTRA_RESOLVE_INFO = "android.app.extra.RESOLVE_INFO";
+    field public static final java.lang.String EXTRA_SEQUENCE = "android.app.extra.SEQUENCE";
+  }
+
   public class ExpandableListActivity extends android.app.Activity implements android.widget.ExpandableListView.OnChildClickListener android.widget.ExpandableListView.OnGroupCollapseListener android.widget.ExpandableListView.OnGroupExpandListener android.view.View.OnCreateContextMenuListener {
     ctor public ExpandableListActivity();
     method public android.widget.ExpandableListAdapter getExpandableListAdapter();
@@ -5185,12 +5204,14 @@
     method public android.app.Notification.Action.Builder extend(android.app.Notification.Action.Builder);
     method public java.lang.CharSequence getCancelLabel();
     method public java.lang.CharSequence getConfirmLabel();
+    method public boolean getHintDisplayActionInline();
     method public boolean getHintLaunchesActivity();
     method public java.lang.CharSequence getInProgressLabel();
     method public boolean isAvailableOffline();
     method public android.app.Notification.Action.WearableExtender setAvailableOffline(boolean);
     method public android.app.Notification.Action.WearableExtender setCancelLabel(java.lang.CharSequence);
     method public android.app.Notification.Action.WearableExtender setConfirmLabel(java.lang.CharSequence);
+    method public android.app.Notification.Action.WearableExtender setHintDisplayActionInline(boolean);
     method public android.app.Notification.Action.WearableExtender setHintLaunchesActivity(boolean);
     method public android.app.Notification.Action.WearableExtender setInProgressLabel(java.lang.CharSequence);
   }
@@ -5904,7 +5925,10 @@
     method public android.content.pm.ServiceInfo getServiceInfo();
     method public java.lang.String getServiceName();
     method public java.lang.String getSettingsActivity();
+    method public boolean getShowMetadataInPreview();
     method public java.lang.CharSequence loadAuthor(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
+    method public java.lang.CharSequence loadContextDescription(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
+    method public android.net.Uri loadContextUri(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
     method public java.lang.CharSequence loadDescription(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
     method public android.graphics.drawable.Drawable loadIcon(android.content.pm.PackageManager);
     method public java.lang.CharSequence loadLabel(android.content.pm.PackageManager);
@@ -6767,11 +6791,13 @@
     method public android.content.res.Configuration getConfiguration();
     method public int getEventType();
     method public java.lang.String getPackageName();
+    method public java.lang.String getShortcutId();
     method public long getTimeStamp();
     field public static final int CONFIGURATION_CHANGE = 5; // 0x5
     field public static final int MOVE_TO_BACKGROUND = 2; // 0x2
     field public static final int MOVE_TO_FOREGROUND = 1; // 0x1
     field public static final int NONE = 0; // 0x0
+    field public static final int SHORTCUT_INVOCATION = 8; // 0x8
     field public static final int USER_INTERACTION = 7; // 0x7
   }
 
@@ -8515,6 +8541,7 @@
     field public static final java.lang.String RESTRICTIONS_SERVICE = "restrictions";
     field public static final java.lang.String SEARCH_SERVICE = "search";
     field public static final java.lang.String SENSOR_SERVICE = "sensor";
+    field public static final java.lang.String SHORTCUT_SERVICE = "shortcut";
     field public static final java.lang.String STORAGE_SERVICE = "storage";
     field public static final java.lang.String SYSTEM_HEALTH_SERVICE = "systemhealth";
     field public static final java.lang.String TELECOM_SERVICE = "telecom";
@@ -9855,13 +9882,20 @@
 
   public class LauncherApps {
     method public java.util.List<android.content.pm.LauncherActivityInfo> getActivityList(java.lang.String, android.os.UserHandle);
+    method public android.graphics.drawable.Drawable getShortcutBadgedIconDrawable(android.content.pm.ShortcutInfo, int);
+    method public android.graphics.drawable.Drawable getShortcutIconDrawable(android.content.pm.ShortcutInfo, int);
+    method public java.util.List<android.content.pm.ShortcutInfo> getShortcuts(android.content.pm.LauncherApps.ShortcutQuery, android.os.UserHandle);
+    method public boolean hasShortcutHostPermission();
     method public boolean isActivityEnabled(android.content.ComponentName, android.os.UserHandle);
     method public boolean isPackageEnabled(java.lang.String, android.os.UserHandle);
+    method public void pinShortcuts(java.lang.String, java.util.List<java.lang.String>, 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 android.content.pm.LauncherActivityInfo resolveActivity(android.content.Intent, 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 startShortcut(java.lang.String, java.lang.String, android.graphics.Rect, android.os.Bundle, android.os.UserHandle);
+    method public void startShortcut(android.content.pm.ShortcutInfo, android.graphics.Rect, android.os.Bundle);
     method public void unregisterCallback(android.content.pm.LauncherApps.Callback);
   }
 
@@ -9874,6 +9908,20 @@
     method public void onPackagesSuspended(java.lang.String[], android.os.UserHandle);
     method public abstract void onPackagesUnavailable(java.lang.String[], android.os.UserHandle, boolean);
     method public void onPackagesUnsuspended(java.lang.String[], android.os.UserHandle);
+    method public void onShortcutsChanged(java.lang.String, java.util.List<android.content.pm.ShortcutInfo>, android.os.UserHandle);
+  }
+
+  public static class LauncherApps.ShortcutQuery {
+    ctor public LauncherApps.ShortcutQuery();
+    method public android.content.pm.LauncherApps.ShortcutQuery setActivity(android.content.ComponentName);
+    method public android.content.pm.LauncherApps.ShortcutQuery setChangedSince(long);
+    method public android.content.pm.LauncherApps.ShortcutQuery setPackage(java.lang.String);
+    method public android.content.pm.LauncherApps.ShortcutQuery setQueryFlags(int);
+    method public android.content.pm.LauncherApps.ShortcutQuery setShortcutIds(java.util.List<java.lang.String>);
+    field public static final int FLAG_GET_KEY_FIELDS_ONLY = 4; // 0x4
+    field public static final int FLAG_MATCH_DYNAMIC = 1; // 0x1
+    field public static final int FLAG_MATCH_MANIFEST = 8; // 0x8
+    field public static final int FLAG_MATCH_PINNED = 2; // 0x2
   }
 
   public class PackageInfo implements android.os.Parcelable {
@@ -10444,6 +10492,66 @@
     field public java.lang.String permission;
   }
 
+  public final class ShortcutInfo implements android.os.Parcelable {
+    method public int describeContents();
+    method public android.content.ComponentName getActivity();
+    method public java.util.Set<java.lang.String> getCategories();
+    method public java.lang.CharSequence getDisabledMessage();
+    method public android.os.PersistableBundle getExtras();
+    method public java.lang.String getId();
+    method public android.content.Intent getIntent();
+    method public android.content.Intent[] getIntents();
+    method public long getLastChangedTimestamp();
+    method public java.lang.CharSequence getLongLabel();
+    method public java.lang.String getPackage();
+    method public int getRank();
+    method public java.lang.CharSequence getShortLabel();
+    method public android.os.UserHandle getUserHandle();
+    method public boolean hasKeyFieldsOnly();
+    method public boolean isDeclaredInManifest();
+    method public boolean isDynamic();
+    method public boolean isEnabled();
+    method public boolean isImmutable();
+    method public boolean isPinned();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.content.pm.ShortcutInfo> CREATOR;
+    field public static final java.lang.String SHORTCUT_CATEGORY_CONVERSATION = "android.shortcut.conversation";
+  }
+
+  public static class ShortcutInfo.Builder {
+    ctor public ShortcutInfo.Builder(android.content.Context, java.lang.String);
+    method public android.content.pm.ShortcutInfo build();
+    method public android.content.pm.ShortcutInfo.Builder setActivity(android.content.ComponentName);
+    method public android.content.pm.ShortcutInfo.Builder setCategories(java.util.Set<java.lang.String>);
+    method public android.content.pm.ShortcutInfo.Builder setDisabledMessage(java.lang.CharSequence);
+    method public android.content.pm.ShortcutInfo.Builder setExtras(android.os.PersistableBundle);
+    method public android.content.pm.ShortcutInfo.Builder setIcon(android.graphics.drawable.Icon);
+    method public android.content.pm.ShortcutInfo.Builder setIntent(android.content.Intent);
+    method public android.content.pm.ShortcutInfo.Builder setIntents(android.content.Intent[]);
+    method public android.content.pm.ShortcutInfo.Builder setLongLabel(java.lang.CharSequence);
+    method public android.content.pm.ShortcutInfo.Builder setRank(int);
+    method public android.content.pm.ShortcutInfo.Builder setShortLabel(java.lang.CharSequence);
+  }
+
+  public class ShortcutManager {
+    method public boolean addDynamicShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+    method public void disableShortcuts(java.util.List<java.lang.String>);
+    method public void disableShortcuts(java.util.List<java.lang.String>, java.lang.CharSequence);
+    method public void enableShortcuts(java.util.List<java.lang.String>);
+    method public java.util.List<android.content.pm.ShortcutInfo> getDynamicShortcuts();
+    method public int getIconMaxHeight();
+    method public int getIconMaxWidth();
+    method public java.util.List<android.content.pm.ShortcutInfo> getManifestShortcuts();
+    method public int getMaxShortcutCountPerActivity();
+    method public java.util.List<android.content.pm.ShortcutInfo> getPinnedShortcuts();
+    method public boolean isRateLimitingActive();
+    method public void removeAllDynamicShortcuts();
+    method public void removeDynamicShortcuts(java.util.List<java.lang.String>);
+    method public void reportShortcutUsed(java.lang.String);
+    method public boolean setDynamicShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+    method public boolean updateShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+  }
+
   public class Signature implements android.os.Parcelable {
     ctor public Signature(byte[]);
     ctor public Signature(java.lang.String);
@@ -10664,7 +10772,7 @@
   }
 
   public class Resources {
-    ctor public Resources(android.content.res.AssetManager, android.util.DisplayMetrics, android.content.res.Configuration);
+    ctor public deprecated Resources(android.content.res.AssetManager, android.util.DisplayMetrics, android.content.res.Configuration);
     method public final void finishPreloading();
     method public final void flushLayoutCache();
     method public android.content.res.XmlResourceParser getAnimation(int) throws android.content.res.Resources.NotFoundException;
@@ -10715,7 +10823,7 @@
     method public android.content.res.AssetFileDescriptor openRawResourceFd(int) throws android.content.res.Resources.NotFoundException;
     method public void parseBundleExtra(java.lang.String, android.util.AttributeSet, android.os.Bundle) throws org.xmlpull.v1.XmlPullParserException;
     method public void parseBundleExtras(android.content.res.XmlResourceParser, android.os.Bundle) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
-    method public void updateConfiguration(android.content.res.Configuration, android.util.DisplayMetrics);
+    method public deprecated void updateConfiguration(android.content.res.Configuration, android.util.DisplayMetrics);
   }
 
   public static class Resources.NotFoundException extends java.lang.RuntimeException {
@@ -21064,6 +21172,7 @@
     field public static final android.os.Parcelable.Creator<android.media.AudioFormat> CREATOR;
     field public static final int ENCODING_AC3 = 5; // 0x5
     field public static final int ENCODING_DEFAULT = 1; // 0x1
+    field public static final int ENCODING_DOLBY_TRUEHD = 14; // 0xe
     field public static final int ENCODING_DTS = 7; // 0x7
     field public static final int ENCODING_DTS_HD = 8; // 0x8
     field public static final int ENCODING_E_AC3 = 6; // 0x6
@@ -25916,6 +26025,33 @@
 
 package android.net.metrics {
 
+  public final class ApfProgramEvent 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.net.metrics.ApfProgramEvent> CREATOR;
+    field public static final int FLAG_HAS_IPV4_ADDRESS = 1; // 0x1
+    field public static final int FLAG_MULTICAST_FILTER_ON = 0; // 0x0
+    field public final int currentRas;
+    field public final int filteredRas;
+    field public final int flags;
+    field public final long lifetime;
+    field public final int programLength;
+  }
+
+  public final class ApfStats 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.net.metrics.ApfStats> CREATOR;
+    field public final int droppedRas;
+    field public final long durationMs;
+    field public final int matchingRas;
+    field public final int maxProgramSize;
+    field public final int parseErrors;
+    field public final int programUpdates;
+    field public final int receivedRas;
+    field public final int zeroLifetimeRas;
+  }
+
   public final class DefaultNetworkEvent implements android.os.Parcelable {
     method public int describeContents();
     method public static void logEvent(int, int[], int, boolean, boolean);
@@ -25933,6 +26069,7 @@
     method public static void logStateEvent(java.lang.String, java.lang.String);
     method public void writeToParcel(android.os.Parcel, int);
     field public static final android.os.Parcelable.Creator<android.net.metrics.DhcpClientEvent> CREATOR;
+    field public final int durationMs;
     field public final java.lang.String ifName;
     field public final java.lang.String msg;
   }
@@ -26024,6 +26161,18 @@
     field public final int netId;
   }
 
+  public final class RaEvent 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.net.metrics.RaEvent> CREATOR;
+    field public final long dnsslLifetime;
+    field public final long prefixPreferredLifetime;
+    field public final long prefixValidLifetime;
+    field public final long rdnssLifetime;
+    field public final long routeInfoLifetime;
+    field public final long routerLifetime;
+  }
+
   public final class ValidationProbeEvent implements android.os.Parcelable {
     method public int describeContents();
     method public static void logEvent(int, long, int, int);
@@ -30709,6 +30858,7 @@
     field public static final int LOLLIPOP_MR1 = 22; // 0x16
     field public static final int M = 23; // 0x17
     field public static final int N = 24; // 0x18
+    field public static final int N_MR1 = 25; // 0x19
   }
 
   public final class Bundle extends android.os.BaseBundle implements java.lang.Cloneable android.os.Parcelable {
@@ -31544,6 +31694,7 @@
     method public static void installPackage(android.content.Context, java.io.File, boolean) throws java.io.IOException;
     method public static void processPackage(android.content.Context, java.io.File, android.os.RecoverySystem.ProgressListener, android.os.Handler) throws java.io.IOException;
     method public static void processPackage(android.content.Context, java.io.File, android.os.RecoverySystem.ProgressListener) throws java.io.IOException;
+    method public static void rebootWipeAb(android.content.Context, java.io.File, java.lang.String) throws java.io.IOException;
     method public static void rebootWipeCache(android.content.Context) throws java.io.IOException;
     method public static void rebootWipeUserData(android.content.Context) throws java.io.IOException;
     method public static void scheduleUpdateOnBoot(android.content.Context, java.io.File) throws java.io.IOException;
@@ -31786,6 +31937,7 @@
     method public android.os.Bundle getUserRestrictions();
     method public android.os.Bundle getUserRestrictions(android.os.UserHandle);
     method public boolean hasUserRestriction(java.lang.String);
+    method public boolean isDemoUser();
     method public boolean isManagedProfile();
     method public boolean isManagedProfile(int);
     method public boolean isQuietModeEnabled(android.os.UserHandle);
@@ -32034,6 +32186,7 @@
     method public boolean isObbMounted(java.lang.String);
     method public boolean mountObb(java.lang.String, java.lang.String, android.os.storage.OnObbStateChangeListener);
     method public boolean unmountObb(java.lang.String, boolean, android.os.storage.OnObbStateChangeListener);
+    field public static final java.lang.String ACTION_MANAGE_STORAGE = "android.os.storage.action.MANAGE_STORAGE";
   }
 
   public final class StorageVolume implements android.os.Parcelable {
@@ -33215,6 +33368,7 @@
   public static class CallLog.Calls implements android.provider.BaseColumns {
     ctor public CallLog.Calls();
     method public static java.lang.String getLastOutgoingCall(android.content.Context);
+    field public static final int ANSWERED_EXTERNALLY_TYPE = 7; // 0x7
     field public static final int BLOCKED_TYPE = 6; // 0x6
     field public static final java.lang.String CACHED_FORMATTED_NUMBER = "formatted_number";
     field public static final java.lang.String CACHED_LOOKUP_URI = "lookup_uri";
@@ -33237,6 +33391,7 @@
     field public static final java.lang.String DURATION = "duration";
     field public static final java.lang.String EXTRA_CALL_TYPE_FILTER = "android.provider.extra.CALL_TYPE_FILTER";
     field public static final java.lang.String FEATURES = "features";
+    field public static final int FEATURES_PULLED_EXTERNALLY = 2; // 0x2
     field public static final int FEATURES_VIDEO = 1; // 0x1
     field public static final java.lang.String GEOCODED_LOCATION = "geocoded_location";
     field public static final int INCOMING_TYPE = 1; // 0x1
@@ -35074,6 +35229,7 @@
     field public static final java.lang.String DATA_ROAMING = "data_roaming";
     field public static final java.lang.String DEBUG_APP = "debug_app";
     field public static final java.lang.String DEVELOPMENT_SETTINGS_ENABLED = "development_settings_enabled";
+    field public static final java.lang.String DEVICE_NAME = "device_name";
     field public static final java.lang.String DEVICE_PROVISIONED = "device_provisioned";
     field public static final java.lang.String HTTP_PROXY = "http_proxy";
     field public static final deprecated java.lang.String INSTALL_NON_MARKET_APPS = "install_non_market_apps";
@@ -35662,6 +35818,7 @@
     field public static final java.lang.String ACTION_NEW_VOICEMAIL = "android.intent.action.NEW_VOICEMAIL";
     field public static final java.lang.String ACTION_SYNC_VOICEMAIL = "android.provider.action.SYNC_VOICEMAIL";
     field public static final java.lang.String AUTHORITY = "com.android.voicemail";
+    field public static final java.lang.String EXTRA_PHONE_ACCOUNT_HANDLE = "android.provider.extra.PHONE_ACCOUNT_HANDLE";
     field public static final java.lang.String EXTRA_SELF_CHANGE = "com.android.voicemail.extra.SELF_CHANGE";
     field public static final java.lang.String PARAM_KEY_SOURCE_PACKAGE = "source_package";
   }
@@ -35670,6 +35827,9 @@
     method public static android.net.Uri buildSourceUri(java.lang.String);
     field public static final java.lang.String CONFIGURATION_STATE = "configuration_state";
     field public static final int CONFIGURATION_STATE_CAN_BE_CONFIGURED = 2; // 0x2
+    field public static final int CONFIGURATION_STATE_CONFIGURING = 3; // 0x3
+    field public static final int CONFIGURATION_STATE_DISABLED = 5; // 0x5
+    field public static final int CONFIGURATION_STATE_FAILED = 4; // 0x4
     field public static final int CONFIGURATION_STATE_NOT_CONFIGURED = 1; // 0x1
     field public static final int CONFIGURATION_STATE_OK = 0; // 0x0
     field public static final android.net.Uri CONTENT_URI;
@@ -35694,6 +35854,7 @@
     field public static final int QUOTA_UNAVAILABLE = -1; // 0xffffffff
     field public static final java.lang.String SETTINGS_URI = "settings_uri";
     field public static final java.lang.String SOURCE_PACKAGE = "source_package";
+    field public static final java.lang.String SOURCE_TYPE = "source_type";
     field public static final java.lang.String VOICEMAIL_ACCESS_URI = "voicemail_access_uri";
   }
 
@@ -38781,10 +38942,15 @@
     method public void phoneAccountSelected(android.telecom.PhoneAccountHandle, boolean);
     method public void playDtmfTone(char);
     method public void postDialContinue(boolean);
+    method public void pullExternalCall();
+    method public final void putExtras(android.os.Bundle);
     method public void registerCallback(android.telecom.Call.Callback);
     method public void registerCallback(android.telecom.Call.Callback, android.os.Handler);
     method public void reject(boolean, java.lang.String);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
     method public deprecated void removeListener(android.telecom.Call.Listener);
+    method public void sendCallEvent(java.lang.String, android.os.Bundle);
     method public void splitFromConference();
     method public void stopDtmfTone();
     method public void swapConference();
@@ -38799,6 +38965,7 @@
     field public static final int STATE_HOLDING = 3; // 0x3
     field public static final int STATE_NEW = 0; // 0x0
     field public static final deprecated int STATE_PRE_DIAL_WAIT = 8; // 0x8
+    field public static final int STATE_PULLING_CALL = 11; // 0xb
     field public static final int STATE_RINGING = 2; // 0x2
     field public static final int STATE_SELECT_PHONE_ACCOUNT = 8; // 0x8
   }
@@ -38809,6 +38976,7 @@
     method public void onCannedTextResponsesLoaded(android.telecom.Call, java.util.List<java.lang.String>);
     method public void onChildrenChanged(android.telecom.Call, java.util.List<android.telecom.Call>);
     method public void onConferenceableCallsChanged(android.telecom.Call, java.util.List<android.telecom.Call>);
+    method public void onConnectionEvent(android.telecom.Call, java.lang.String, android.os.Bundle);
     method public void onDetailsChanged(android.telecom.Call, android.telecom.Call.Details);
     method public void onParentChanged(android.telecom.Call, android.telecom.Call);
     method public void onPostDialWait(android.telecom.Call, java.lang.String);
@@ -38839,6 +39007,7 @@
     method public static java.lang.String propertiesToString(int);
     field public static final int CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO = 4194304; // 0x400000
     field public static final int CAPABILITY_CAN_PAUSE_VIDEO = 1048576; // 0x100000
+    field public static final int CAPABILITY_CAN_PULL_CALL = 8388608; // 0x800000
     field public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 8192; // 0x2000
     field public static final int CAPABILITY_HOLD = 1; // 0x1
     field public static final int CAPABILITY_MANAGE_CONFERENCE = 128; // 0x80
@@ -38858,7 +39027,9 @@
     field public static final int PROPERTY_EMERGENCY_CALLBACK_MODE = 4; // 0x4
     field public static final int PROPERTY_ENTERPRISE_CALL = 32; // 0x20
     field public static final int PROPERTY_GENERIC_CONFERENCE = 2; // 0x2
+    field public static final int PROPERTY_HAS_CDMA_VOICE_PRIVACY = 128; // 0x80
     field public static final int PROPERTY_HIGH_DEF_AUDIO = 16; // 0x10
+    field public static final int PROPERTY_IS_EXTERNAL_CALL = 64; // 0x40
     field public static final int PROPERTY_WIFI = 8; // 0x8
   }
 
@@ -38915,6 +39086,7 @@
     method public final java.util.List<android.telecom.Connection> getConferenceableConnections();
     method public final deprecated long getConnectTimeMillis();
     method public final int getConnectionCapabilities();
+    method public final int getConnectionProperties();
     method public final long getConnectionTime();
     method public final java.util.List<android.telecom.Connection> getConnections();
     method public final android.telecom.DisconnectCause getDisconnectCause();
@@ -38929,6 +39101,7 @@
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
     method public void onConnectionAdded(android.telecom.Connection);
     method public void onDisconnect();
+    method public void onExtrasChanged(android.os.Bundle);
     method public void onHold();
     method public void onMerge(android.telecom.Connection);
     method public void onMerge();
@@ -38937,11 +39110,15 @@
     method public void onStopDtmfTone();
     method public void onSwap();
     method public void onUnhold();
+    method public final void putExtras(android.os.Bundle);
     method public final void removeConnection(android.telecom.Connection);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
     method public final void setActive();
     method public final void setConferenceableConnections(java.util.List<android.telecom.Connection>);
     method public final deprecated void setConnectTimeMillis(long);
     method public final void setConnectionCapabilities(int);
+    method public final void setConnectionProperties(int);
     method public final void setConnectionTime(long);
     method public final void setDialing();
     method public final void setDisconnected(android.telecom.DisconnectCause);
@@ -38972,6 +39149,7 @@
     method public final android.telecom.Conference getConference();
     method public final java.util.List<android.telecom.Conferenceable> getConferenceables();
     method public final int getConnectionCapabilities();
+    method public final int getConnectionProperties();
     method public final android.telecom.DisconnectCause getDisconnectCause();
     method public final android.os.Bundle getExtras();
     method public final int getState();
@@ -38983,16 +39161,24 @@
     method public void onAnswer();
     method public deprecated void onAudioStateChanged(android.telecom.AudioState);
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
+    method public void onCallEvent(java.lang.String, android.os.Bundle);
     method public void onDisconnect();
+    method public void onExtrasChanged(android.os.Bundle);
     method public void onHold();
     method public void onPlayDtmfTone(char);
     method public void onPostDialContinue(boolean);
+    method public void onPullExternalCall();
     method public void onReject();
     method public void onReject(java.lang.String);
     method public void onSeparate();
     method public void onStateChanged(int);
     method public void onStopDtmfTone();
     method public void onUnhold();
+    method public static java.lang.String propertiesToString(int);
+    method public final void putExtras(android.os.Bundle);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
+    method public void sendConnectionEvent(java.lang.String, android.os.Bundle);
     method public final void setActive();
     method public final void setAddress(android.net.Uri, int);
     method public final void setAudioModeIsVoip(boolean);
@@ -39000,6 +39186,7 @@
     method public final void setConferenceableConnections(java.util.List<android.telecom.Connection>);
     method public final void setConferenceables(java.util.List<android.telecom.Conferenceable>);
     method public final void setConnectionCapabilities(int);
+    method public final void setConnectionProperties(int);
     method public final void setDialing();
     method public final void setDisconnected(android.telecom.DisconnectCause);
     method public final void setExtras(android.os.Bundle);
@@ -39008,6 +39195,7 @@
     method public final void setNextPostDialChar(char);
     method public final void setOnHold();
     method public final void setPostDialWait(java.lang.String);
+    method public final void setPulling();
     method public final void setRingbackRequested(boolean);
     method public final void setRinging();
     method public final void setStatusHints(android.telecom.StatusHints);
@@ -39016,6 +39204,7 @@
     method public static java.lang.String stateToString(int);
     field public static final int CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO = 8388608; // 0x800000
     field public static final int CAPABILITY_CAN_PAUSE_VIDEO = 1048576; // 0x100000
+    field public static final int CAPABILITY_CAN_PULL_CALL = 16777216; // 0x1000000
     field public static final int CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION = 4194304; // 0x400000
     field public static final int CAPABILITY_CAN_UPGRADE_TO_VIDEO = 524288; // 0x80000
     field public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 8192; // 0x2000
@@ -39033,15 +39222,21 @@
     field public static final int CAPABILITY_SUPPORTS_VT_REMOTE_TX = 2048; // 0x800
     field public static final int CAPABILITY_SUPPORT_HOLD = 2; // 0x2
     field public static final int CAPABILITY_SWAP_CONFERENCE = 8; // 0x8
+    field public static final java.lang.String EVENT_CALL_MERGE_FAILED = "android.telecom.event.CALL_MERGE_FAILED";
+    field public static final java.lang.String EVENT_CALL_PULL_FAILED = "android.telecom.event.CALL_PULL_FAILED";
+    field public static final java.lang.String EXTRA_ANSWERING_DROPS_FG_CALL = "android.telecom.extra.ANSWERING_DROPS_FG_CALL";
     field public static final java.lang.String EXTRA_CALL_SUBJECT = "android.telecom.extra.CALL_SUBJECT";
     field public static final java.lang.String EXTRA_CHILD_ADDRESS = "android.telecom.extra.CHILD_ADDRESS";
     field public static final java.lang.String EXTRA_LAST_FORWARDED_NUMBER = "android.telecom.extra.LAST_FORWARDED_NUMBER";
+    field public static final int PROPERTY_HAS_CDMA_VOICE_PRIVACY = 32; // 0x20
+    field public static final int PROPERTY_IS_EXTERNAL_CALL = 16; // 0x10
     field public static final int STATE_ACTIVE = 4; // 0x4
     field public static final int STATE_DIALING = 3; // 0x3
     field public static final int STATE_DISCONNECTED = 6; // 0x6
     field public static final int STATE_HOLDING = 5; // 0x5
     field public static final int STATE_INITIALIZING = 0; // 0x0
     field public static final int STATE_NEW = 1; // 0x1
+    field public static final int STATE_PULLING_CALL = 7; // 0x7
     field public static final int STATE_RINGING = 2; // 0x2
   }
 
@@ -39119,7 +39314,9 @@
     method public java.lang.String getReason();
     method public int getTone();
     method public void writeToParcel(android.os.Parcel, int);
+    field public static final int ANSWERED_ELSEWHERE = 11; // 0xb
     field public static final int BUSY = 7; // 0x7
+    field public static final int CALL_PULLED = 12; // 0xc
     field public static final int CANCELED = 4; // 0x4
     field public static final int CONNECTION_MANAGER_NOT_SUPPORTED = 10; // 0xa
     field public static final android.os.Parcelable.Creator<android.telecom.DisconnectCause> CREATOR;
@@ -39156,6 +39353,7 @@
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
     method public void onCallRemoved(android.telecom.Call);
     method public void onCanAddCallChanged(boolean);
+    method public void onConnectionEvent(android.telecom.Call, java.lang.String, android.os.Bundle);
     method public deprecated void onPhoneCreated(android.telecom.Phone);
     method public deprecated void onPhoneDestroyed(android.telecom.Phone);
     method public void onSilenceRinger();
@@ -39193,14 +39391,16 @@
   }
 
   public class ParcelableCallAnalytics implements android.os.Parcelable {
-    ctor public ParcelableCallAnalytics(long, long, int, boolean, boolean, int, int, boolean, java.lang.String, boolean);
+    ctor public ParcelableCallAnalytics(long, long, int, boolean, boolean, int, int, boolean, java.lang.String, boolean, java.util.List<android.telecom.ParcelableCallAnalytics.AnalyticsEvent>, java.util.List<android.telecom.ParcelableCallAnalytics.EventTiming>);
     ctor public ParcelableCallAnalytics(android.os.Parcel);
+    method public java.util.List<android.telecom.ParcelableCallAnalytics.AnalyticsEvent> analyticsEvents();
     method public int describeContents();
     method public long getCallDurationMillis();
     method public int getCallTechnologies();
     method public int getCallTerminationCode();
     method public int getCallType();
     method public java.lang.String getConnectionService();
+    method public java.util.List<android.telecom.ParcelableCallAnalytics.EventTiming> getEventTimings();
     method public long getStartTimeMillis();
     method public boolean isAdditionalCall();
     method public boolean isCreatedFromExistingConnection();
@@ -39221,6 +39421,73 @@
     field public static final int THIRD_PARTY_PHONE = 16; // 0x10
   }
 
+  public static final class ParcelableCallAnalytics.AnalyticsEvent implements android.os.Parcelable {
+    ctor public ParcelableCallAnalytics.AnalyticsEvent(int, long);
+    method public int describeContents();
+    method public int getEventName();
+    method public long getTimeSinceLastEvent();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final int AUDIO_ROUTE_BT = 204; // 0xcc
+    field public static final int AUDIO_ROUTE_EARPIECE = 205; // 0xcd
+    field public static final int AUDIO_ROUTE_HEADSET = 206; // 0xce
+    field public static final int AUDIO_ROUTE_SPEAKER = 207; // 0xcf
+    field public static final int BIND_CS = 5; // 0x5
+    field public static final int BLOCK_CHECK_FINISHED = 105; // 0x69
+    field public static final int BLOCK_CHECK_INITIATED = 104; // 0x68
+    field public static final int CONFERENCE_WITH = 300; // 0x12c
+    field public static final android.os.Parcelable.Creator<android.telecom.ParcelableCallAnalytics.AnalyticsEvent> CREATOR;
+    field public static final int CS_BOUND = 6; // 0x6
+    field public static final int DIRECT_TO_VM_FINISHED = 103; // 0x67
+    field public static final int DIRECT_TO_VM_INITIATED = 102; // 0x66
+    field public static final int FILTERING_COMPLETED = 107; // 0x6b
+    field public static final int FILTERING_INITIATED = 106; // 0x6a
+    field public static final int FILTERING_TIMED_OUT = 108; // 0x6c
+    field public static final int MUTE = 202; // 0xca
+    field public static final int REMOTELY_HELD = 402; // 0x192
+    field public static final int REMOTELY_UNHELD = 403; // 0x193
+    field public static final int REQUEST_ACCEPT = 7; // 0x7
+    field public static final int REQUEST_HOLD = 400; // 0x190
+    field public static final int REQUEST_PULL = 500; // 0x1f4
+    field public static final int REQUEST_REJECT = 8; // 0x8
+    field public static final int REQUEST_UNHOLD = 401; // 0x191
+    field public static final int SCREENING_COMPLETED = 101; // 0x65
+    field public static final int SCREENING_SENT = 100; // 0x64
+    field public static final int SET_ACTIVE = 1; // 0x1
+    field public static final int SET_DIALING = 4; // 0x4
+    field public static final int SET_DISCONNECTED = 2; // 0x2
+    field public static final int SET_HOLD = 404; // 0x194
+    field public static final int SET_PARENT = 302; // 0x12e
+    field public static final int SET_SELECT_PHONE_ACCOUNT = 0; // 0x0
+    field public static final int SILENCE = 201; // 0xc9
+    field public static final int SKIP_RINGING = 200; // 0xc8
+    field public static final int SPLIT_CONFERENCE = 301; // 0x12d
+    field public static final int START_CONNECTION = 3; // 0x3
+    field public static final int SWAP = 405; // 0x195
+    field public static final int UNMUTE = 203; // 0xcb
+  }
+
+  public static final class ParcelableCallAnalytics.EventTiming implements android.os.Parcelable {
+    ctor public ParcelableCallAnalytics.EventTiming(int, long);
+    method public int describeContents();
+    method public int getName();
+    method public long getTime();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final int ACCEPT_TIMING = 0; // 0x0
+    field public static final int BIND_CS_TIMING = 6; // 0x6
+    field public static final int BLOCK_CHECK_FINISHED_TIMING = 9; // 0x9
+    field public static final android.os.Parcelable.Creator<android.telecom.ParcelableCallAnalytics.EventTiming> CREATOR;
+    field public static final int DIRECT_TO_VM_FINISHED_TIMING = 8; // 0x8
+    field public static final int DISCONNECT_TIMING = 2; // 0x2
+    field public static final int FILTERING_COMPLETED_TIMING = 10; // 0xa
+    field public static final int FILTERING_TIMED_OUT_TIMING = 11; // 0xb
+    field public static final int HOLD_TIMING = 3; // 0x3
+    field public static final int INVALID = 999999; // 0xf423f
+    field public static final int OUTGOING_TIME_TO_DIALING_TIMING = 5; // 0x5
+    field public static final int REJECT_TIMING = 1; // 0x1
+    field public static final int SCREENING_COMPLETED_TIMING = 7; // 0x7
+    field public static final int UNHOLD_TIMING = 4; // 0x4
+  }
+
   public final deprecated class Phone {
     method public final void addListener(android.telecom.Phone.Listener);
     method public final boolean canAddCall();
@@ -39333,6 +39600,7 @@
     method public void onConferenceableConnectionsChanged(android.telecom.RemoteConference, java.util.List<android.telecom.RemoteConnection>);
     method public void onConnectionAdded(android.telecom.RemoteConference, android.telecom.RemoteConnection);
     method public void onConnectionCapabilitiesChanged(android.telecom.RemoteConference, int);
+    method public void onConnectionPropertiesChanged(android.telecom.RemoteConference, int);
     method public void onConnectionRemoved(android.telecom.RemoteConference, android.telecom.RemoteConnection);
     method public void onDestroyed(android.telecom.RemoteConference);
     method public void onDisconnected(android.telecom.RemoteConference, android.telecom.DisconnectCause);
@@ -39351,6 +39619,7 @@
     method public android.telecom.RemoteConference getConference();
     method public java.util.List<android.telecom.RemoteConnection> getConferenceableConnections();
     method public int getConnectionCapabilities();
+    method public int getConnectionProperties();
     method public android.telecom.DisconnectCause getDisconnectCause();
     method public final android.os.Bundle getExtras();
     method public int getState();
@@ -39362,6 +39631,7 @@
     method public boolean isVoipAudioMode();
     method public void playDtmfTone(char);
     method public void postDialContinue(boolean);
+    method public void pullExternalCall();
     method public void registerCallback(android.telecom.RemoteConnection.Callback);
     method public void registerCallback(android.telecom.RemoteConnection.Callback, android.os.Handler);
     method public void reject();
@@ -39379,6 +39649,8 @@
     method public void onConferenceChanged(android.telecom.RemoteConnection, android.telecom.RemoteConference);
     method public void onConferenceableConnectionsChanged(android.telecom.RemoteConnection, java.util.List<android.telecom.RemoteConnection>);
     method public void onConnectionCapabilitiesChanged(android.telecom.RemoteConnection, int);
+    method public void onConnectionEvent(android.telecom.RemoteConnection, java.lang.String, android.os.Bundle);
+    method public void onConnectionPropertiesChanged(android.telecom.RemoteConnection, int);
     method public void onDestroyed(android.telecom.RemoteConnection);
     method public void onDisconnected(android.telecom.RemoteConnection, android.telecom.DisconnectCause);
     method public void onExtrasChanged(android.telecom.RemoteConnection, android.os.Bundle);
@@ -39432,6 +39704,41 @@
     field public static final android.os.Parcelable.Creator<android.telecom.StatusHints> CREATOR;
   }
 
+  public final class TelecomAnalytics implements android.os.Parcelable {
+    ctor public TelecomAnalytics(java.util.List<android.telecom.TelecomAnalytics.SessionTiming>, java.util.List<android.telecom.ParcelableCallAnalytics>);
+    method public int describeContents();
+    method public java.util.List<android.telecom.ParcelableCallAnalytics> getCallAnalytics();
+    method public java.util.List<android.telecom.TelecomAnalytics.SessionTiming> getSessionTimings();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.telecom.TelecomAnalytics> CREATOR;
+  }
+
+  public static final class TelecomAnalytics.SessionTiming implements android.os.Parcelable {
+    ctor public TelecomAnalytics.SessionTiming(int, long);
+    method public int describeContents();
+    method public java.lang.Integer getKey();
+    method public long getTime();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.telecom.TelecomAnalytics.SessionTiming> CREATOR;
+    field public static final int CSW_ADD_CONFERENCE_CALL = 108; // 0x6c
+    field public static final int CSW_HANDLE_CREATE_CONNECTION_COMPLETE = 100; // 0x64
+    field public static final int CSW_REMOVE_CALL = 106; // 0x6a
+    field public static final int CSW_SET_ACTIVE = 101; // 0x65
+    field public static final int CSW_SET_DIALING = 103; // 0x67
+    field public static final int CSW_SET_DISCONNECTED = 104; // 0x68
+    field public static final int CSW_SET_IS_CONFERENCED = 107; // 0x6b
+    field public static final int CSW_SET_ON_HOLD = 105; // 0x69
+    field public static final int CSW_SET_RINGING = 102; // 0x66
+    field public static final int ICA_ANSWER_CALL = 1; // 0x1
+    field public static final int ICA_CONFERENCE = 8; // 0x8
+    field public static final int ICA_DISCONNECT_CALL = 3; // 0x3
+    field public static final int ICA_HOLD_CALL = 4; // 0x4
+    field public static final int ICA_MUTE = 6; // 0x6
+    field public static final int ICA_REJECT_CALL = 2; // 0x2
+    field public static final int ICA_SET_AUDIO_ROUTE = 7; // 0x7
+    field public static final int ICA_UNHOLD_CALL = 5; // 0x5
+  }
+
   public class TelecomManager {
     method public void acceptRingingCall();
     method public void acceptRingingCall(int);
@@ -39441,7 +39748,7 @@
     method public deprecated void clearAccounts();
     method public void clearPhoneAccounts();
     method public android.content.Intent createManageBlockedNumbersIntent();
-    method public java.util.List<android.telecom.ParcelableCallAnalytics> dumpAnalytics();
+    method public android.telecom.TelecomAnalytics dumpAnalytics();
     method public void enablePhoneAccount(android.telecom.PhoneAccountHandle, boolean);
     method public boolean endCall();
     method public android.net.Uri getAdnUriForPhoneAccount(android.telecom.PhoneAccountHandle);
@@ -39502,6 +39809,7 @@
     field public static final java.lang.String EXTRA_START_CALL_WITH_VIDEO_STATE = "android.telecom.extra.START_CALL_WITH_VIDEO_STATE";
     field public static final java.lang.String GATEWAY_ORIGINAL_ADDRESS = "android.telecom.extra.GATEWAY_ORIGINAL_ADDRESS";
     field public static final java.lang.String GATEWAY_PROVIDER_PACKAGE = "android.telecom.extra.GATEWAY_PROVIDER_PACKAGE";
+    field public static final java.lang.String METADATA_INCLUDE_EXTERNAL_CALLS = "android.telecom.INCLUDE_EXTERNAL_CALLS";
     field public static final java.lang.String METADATA_IN_CALL_SERVICE_RINGING = "android.telecom.IN_CALL_SERVICE_RINGING";
     field public static final java.lang.String METADATA_IN_CALL_SERVICE_UI = "android.telecom.IN_CALL_SERVICE_UI";
     field public static final int PRESENTATION_ALLOWED = 1; // 0x1
@@ -39558,9 +39866,11 @@
     field public static final java.lang.String ACTION_CARRIER_CONFIG_CHANGED = "android.telephony.action.CARRIER_CONFIG_CHANGED";
     field public static final java.lang.String KEY_ADDITIONAL_CALL_SETTING_BOOL = "additional_call_setting_bool";
     field public static final java.lang.String KEY_ALLOW_ADDING_APNS_BOOL = "allow_adding_apns_bool";
+    field public static final java.lang.String KEY_ALLOW_ADD_CALL_DURING_VIDEO_CALL_BOOL = "allow_add_call_during_video_call";
     field public static final java.lang.String KEY_ALLOW_EMERGENCY_NUMBERS_IN_CALL_LOG_BOOL = "allow_emergency_numbers_in_call_log_bool";
     field public static final java.lang.String KEY_ALLOW_EMERGENCY_VIDEO_CALLS_BOOL = "allow_emergency_video_calls_bool";
     field public static final java.lang.String KEY_ALLOW_LOCAL_DTMF_TONES_BOOL = "allow_local_dtmf_tones_bool";
+    field public static final java.lang.String KEY_ALLOW_MERGE_WIFI_CALLS_WHEN_VOWIFI_OFF_BOOL = "allow_merge_wifi_calls_when_vowifi_off_bool";
     field public static final java.lang.String KEY_ALLOW_NON_EMERGENCY_CALLS_IN_ECM_BOOL = "allow_non_emergency_calls_in_ecm_bool";
     field public static final java.lang.String KEY_ALWAYS_SHOW_EMERGENCY_ALERT_ONOFF_BOOL = "always_show_emergency_alert_onoff_bool";
     field public static final java.lang.String KEY_APN_EXPAND_BOOL = "apn_expand_bool";
@@ -39592,6 +39902,7 @@
     field public static final java.lang.String KEY_CSP_ENABLED_BOOL = "csp_enabled_bool";
     field public static final java.lang.String KEY_DEFAULT_SIM_CALL_MANAGER_STRING = "default_sim_call_manager_string";
     field public static final java.lang.String KEY_DISABLE_CDMA_ACTIVATION_CODE_BOOL = "disable_cdma_activation_code_bool";
+    field public static final java.lang.String KEY_DROP_VIDEO_CALL_WHEN_ANSWERING_AUDIO_CALL_BOOL = "drop_video_call_when_answering_audio_call_bool";
     field public static final java.lang.String KEY_DTMF_TYPE_ENABLED_BOOL = "dtmf_type_enabled_bool";
     field public static final java.lang.String KEY_DURATION_BLOCKING_DISABLED_AFTER_EMERGENCY_INT = "duration_blocking_disabled_after_emergency_int";
     field public static final java.lang.String KEY_EDITABLE_ENHANCED_4G_LTE_BOOL = "editable_enhanced_4g_lte_bool";
@@ -39649,6 +39960,7 @@
     field public static final java.lang.String KEY_SUPPORT_CONFERENCE_CALL_BOOL = "support_conference_call_bool";
     field public static final java.lang.String KEY_SUPPORT_PAUSE_IMS_VIDEO_CALLS_BOOL = "support_pause_ims_video_calls_bool";
     field public static final java.lang.String KEY_SUPPORT_SWAP_AFTER_MERGE_BOOL = "support_swap_after_merge_bool";
+    field public static final java.lang.String KEY_TREAT_DOWNGRADED_VIDEO_CALLS_AS_VIDEO_CALLS_BOOL = "treat_downgraded_video_calls_as_video_calls_bool";
     field public static final java.lang.String KEY_USE_HFA_FOR_PROVISIONING_BOOL = "use_hfa_for_provisioning_bool";
     field public static final java.lang.String KEY_USE_OTASP_FOR_PROVISIONING_BOOL = "use_otasp_for_provisioning_bool";
     field public static final java.lang.String KEY_USE_RCS_PRESENCE_BOOL = "use_rcs_presence_bool";
@@ -40144,6 +40456,26 @@
     method public void onSubscriptionsChanged();
   }
 
+  public final class TelephonyHistogram implements android.os.Parcelable {
+    ctor public TelephonyHistogram(int, int, int);
+    ctor public TelephonyHistogram(android.telephony.TelephonyHistogram);
+    ctor public TelephonyHistogram(android.os.Parcel);
+    method public void addTimeTaken(int);
+    method public int describeContents();
+    method public int getAverageTime();
+    method public int getBucketCount();
+    method public int[] getBucketCounters();
+    method public int[] getBucketEndPoints();
+    method public int getCategory();
+    method public int getId();
+    method public int getMaxTime();
+    method public int getMinTime();
+    method public int getSampleCount();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.telephony.TelephonyHistogram> CREATOR;
+    field public static final int TELEPHONY_CATEGORY_RIL = 1; // 0x1
+  }
+
   public class TelephonyManager {
     method public void answerRingingCall();
     method public void call(java.lang.String, java.lang.String);
@@ -40193,6 +40525,7 @@
     method public java.lang.String getSimSerialNumber();
     method public int getSimState();
     method public java.lang.String getSubscriberId();
+    method public java.util.List<android.telephony.TelephonyHistogram> getTelephonyHistograms();
     method public java.lang.String getVoiceMailAlphaTag();
     method public java.lang.String getVoiceMailNumber();
     method public int getVoiceNetworkType();
@@ -40216,6 +40549,7 @@
     method public boolean isSmsCapable();
     method public boolean isTtyModeSupported();
     method public boolean isVideoCallingEnabled();
+    method public boolean isVisualVoicemailEnabled(android.telecom.PhoneAccountHandle);
     method public boolean isVoiceCapable();
     method public boolean isVoicemailVibrationEnabled(android.telecom.PhoneAccountHandle);
     method public boolean isWorldPhone();
@@ -40229,6 +40563,7 @@
     method public boolean setPreferredNetworkTypeToGlobal();
     method public boolean setRadio(boolean);
     method public boolean setRadioPower(boolean);
+    method public void setVisualVoicemailEnabled(android.telecom.PhoneAccountHandle, boolean);
     method public boolean setVoiceMailNumber(java.lang.String, java.lang.String);
     method public void silenceRinger();
     method public boolean supplyPin(java.lang.String);
@@ -40281,12 +40616,15 @@
     field public static final int NETWORK_TYPE_EVDO_A = 6; // 0x6
     field public static final int NETWORK_TYPE_EVDO_B = 12; // 0xc
     field public static final int NETWORK_TYPE_GPRS = 1; // 0x1
+    field public static final int NETWORK_TYPE_GSM = 16; // 0x10
     field public static final int NETWORK_TYPE_HSDPA = 8; // 0x8
     field public static final int NETWORK_TYPE_HSPA = 10; // 0xa
     field public static final int NETWORK_TYPE_HSPAP = 15; // 0xf
     field public static final int NETWORK_TYPE_HSUPA = 9; // 0x9
     field public static final int NETWORK_TYPE_IDEN = 11; // 0xb
+    field public static final int NETWORK_TYPE_IWLAN = 18; // 0x12
     field public static final int NETWORK_TYPE_LTE = 13; // 0xd
+    field public static final int NETWORK_TYPE_TD_SCDMA = 17; // 0x11
     field public static final int NETWORK_TYPE_UMTS = 3; // 0x3
     field public static final int NETWORK_TYPE_UNKNOWN = 0; // 0x0
     field public static final int PHONE_TYPE_CDMA = 2; // 0x2
@@ -43104,7 +43442,10 @@
     method public boolean equals(android.util.DisplayMetrics);
     method public void setTo(android.util.DisplayMetrics);
     method public void setToDefaults();
+    field public static final int DENSITY_260 = 260; // 0x104
     field public static final int DENSITY_280 = 280; // 0x118
+    field public static final int DENSITY_300 = 300; // 0x12c
+    field public static final int DENSITY_340 = 340; // 0x154
     field public static final int DENSITY_360 = 360; // 0x168
     field public static final int DENSITY_400 = 400; // 0x190
     field public static final int DENSITY_420 = 420; // 0x1a4
@@ -44434,6 +44775,10 @@
     field public static final int KEYCODE_SWITCH_CHARSET = 95; // 0x5f
     field public static final int KEYCODE_SYM = 63; // 0x3f
     field public static final int KEYCODE_SYSRQ = 120; // 0x78
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_DOWN = 281; // 0x119
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_LEFT = 282; // 0x11a
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_RIGHT = 283; // 0x11b
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_UP = 280; // 0x118
     field public static final int KEYCODE_T = 48; // 0x30
     field public static final int KEYCODE_TAB = 61; // 0x3d
     field public static final int KEYCODE_TV = 170; // 0xaa
@@ -45316,6 +45661,7 @@
     method public float getPivotY();
     method public android.view.PointerIcon getPointerIcon();
     method public android.content.res.Resources getResources();
+    method public final boolean getRevealOnFocusHint();
     method public final int getRight();
     method protected float getRightFadingEdgeStrength();
     method protected int getRightPaddingOffset();
@@ -45603,6 +45949,7 @@
     method public void setPivotY(float);
     method public void setPointerIcon(android.view.PointerIcon);
     method public void setPressed(boolean);
+    method public final void setRevealOnFocusHint(boolean);
     method public final void setRight(int);
     method public void setRotation(float);
     method public void setRotationX(float);
@@ -46755,6 +47102,7 @@
     field public static final int TYPE_APPLICATION_SUB_PANEL = 1002; // 0x3ea
     field public static final int TYPE_BASE_APPLICATION = 1; // 0x1
     field public static final int TYPE_CHANGED = 2; // 0x2
+    field public static final int TYPE_DRAWN_APPLICATION = 4; // 0x4
     field public static final int TYPE_INPUT_METHOD = 2011; // 0x7db
     field public static final int TYPE_INPUT_METHOD_DIALOG = 2012; // 0x7dc
     field public static final int TYPE_KEYGUARD_DIALOG = 2009; // 0x7d9
@@ -47527,6 +47875,7 @@
     method public boolean clearMetaKeyStates(int);
     method public void closeConnection();
     method public boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public boolean commitText(java.lang.CharSequence, int);
     method public boolean deleteSurroundingText(int, int);
@@ -47636,6 +47985,7 @@
     field public static final int IME_NULL = 0; // 0x0
     field public int actionId;
     field public java.lang.CharSequence actionLabel;
+    field public java.lang.String[] contentMimeTypes;
     field public android.os.Bundle extras;
     field public int fieldId;
     field public java.lang.String fieldName;
@@ -47695,6 +48045,7 @@
     method public abstract boolean clearMetaKeyStates(int);
     method public abstract void closeConnection();
     method public abstract boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public abstract boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public abstract boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public abstract boolean commitText(java.lang.CharSequence, int);
     method public abstract boolean deleteSurroundingText(int, int);
@@ -47720,6 +48071,7 @@
     field public static final int CURSOR_UPDATE_MONITOR = 2; // 0x2
     field public static final int GET_EXTRACTED_TEXT_MONITOR = 1; // 0x1
     field public static final int GET_TEXT_WITH_STYLES = 1; // 0x1
+    field public static final int INPUT_CONTENT_GRANT_READ_URI_PERMISSION = 1; // 0x1
   }
 
   public class InputConnectionWrapper implements android.view.inputmethod.InputConnection {
@@ -47728,6 +48080,7 @@
     method public boolean clearMetaKeyStates(int);
     method public void closeConnection();
     method public boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public boolean commitText(java.lang.CharSequence, int);
     method public boolean deleteSurroundingText(int, int);
@@ -47752,6 +48105,19 @@
     method public void setTarget(android.view.inputmethod.InputConnection);
   }
 
+  public final class InputContentInfo implements android.os.Parcelable {
+    ctor public InputContentInfo(android.net.Uri, android.content.ClipDescription);
+    ctor public InputContentInfo(android.net.Uri, android.content.ClipDescription, android.net.Uri);
+    method public int describeContents();
+    method public android.net.Uri getContentUri();
+    method public android.content.ClipDescription getDescription();
+    method public android.net.Uri getLinkUri();
+    method public void releasePermission();
+    method public void requestPermission();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.view.inputmethod.InputContentInfo> CREATOR;
+  }
+
   public abstract interface InputMethod {
     method public abstract void attachToken(android.os.IBinder);
     method public abstract void bindInput(android.view.inputmethod.InputBinding);
diff --git a/api/test-current.txt b/api/test-current.txt
index 2aa6cfe..9b51a1c 100644
--- a/api/test-current.txt
+++ b/api/test-current.txt
@@ -397,6 +397,7 @@
     field public static final int colorPressedHighlight = 16843661; // 0x101038d
     field public static final int colorPrimary = 16843827; // 0x1010433
     field public static final int colorPrimaryDark = 16843828; // 0x1010434
+    field public static final int colorSecondary = 16844080; // 0x1010530
     field public static final int columnCount = 16843639; // 0x1010377
     field public static final int columnDelay = 16843215; // 0x10101cf
     field public static final int columnOrderPreserved = 16843640; // 0x1010378
@@ -420,7 +421,9 @@
     field public static final int contentInsetStart = 16843859; // 0x1010453
     field public static final int contentInsetStartWithNavigation = 16844066; // 0x1010522
     field public static final int contextClickable = 16844007; // 0x10104e7
+    field public static final int contextDescription = 16844078; // 0x101052e
     field public static final int contextPopupMenuStyle = 16844033; // 0x1010501
+    field public static final int contextUri = 16844077; // 0x101052d
     field public static final int controlX1 = 16843772; // 0x10103fc
     field public static final int controlX2 = 16843774; // 0x10103fe
     field public static final int controlY1 = 16843773; // 0x10103fd
@@ -1039,6 +1042,7 @@
     field public static final int rotation = 16843558; // 0x1010326
     field public static final int rotationX = 16843559; // 0x1010327
     field public static final int rotationY = 16843560; // 0x1010328
+    field public static final int roundIcon = 16844076; // 0x101052c
     field public static final int rowCount = 16843637; // 0x1010375
     field public static final int rowDelay = 16843216; // 0x10101d0
     field public static final int rowEdgeFlags = 16843329; // 0x1010241
@@ -1106,11 +1110,16 @@
     field public static final int shareInterpolator = 16843195; // 0x10101bb
     field public static final int sharedUserId = 16842763; // 0x101000b
     field public static final int sharedUserLabel = 16843361; // 0x1010261
+    field public static final int shortcutDisabledMessage = 16844075; // 0x101052b
+    field public static final int shortcutId = 16844072; // 0x1010528
+    field public static final int shortcutLongLabel = 16844074; // 0x101052a
+    field public static final int shortcutShortLabel = 16844073; // 0x1010529
     field public static final int shouldDisableView = 16843246; // 0x10101ee
     field public static final int showAsAction = 16843481; // 0x10102d9
     field public static final int showDefault = 16843258; // 0x10101fa
     field public static final int showDividers = 16843561; // 0x1010329
     field public static final int showForAllUsers = 16844015; // 0x10104ef
+    field public static final int showMetadataInPreview = 16844079; // 0x101052f
     field public static final deprecated int showOnLockScreen = 16843721; // 0x10103c9
     field public static final int showSilent = 16843259; // 0x10101fb
     field public static final int showText = 16843949; // 0x10104ad
@@ -3246,6 +3255,7 @@
     method public java.lang.Object getAnimatedValue(java.lang.String);
     method public long getCurrentPlayTime();
     method public long getDuration();
+    method public static float getDurationScale();
     method public static long getFrameDelay();
     method public int getRepeatCount();
     method public int getRepeatMode();
@@ -3263,6 +3273,7 @@
     method public void setCurrentFraction(float);
     method public void setCurrentPlayTime(long);
     method public android.animation.ValueAnimator setDuration(long);
+    method public static void setDurationScale(float);
     method public void setEvaluator(android.animation.TypeEvaluator);
     method public void setFloatValues(float...);
     method public static void setFrameDelay(long);
@@ -3696,6 +3707,7 @@
     method public void moveTaskToFront(int, int);
     method public void moveTaskToFront(int, int, android.os.Bundle);
     method public deprecated void restartPackage(java.lang.String);
+    method public static void setVrThread(int);
     method public void setWatchHeapLimit(long);
     field public static final java.lang.String ACTION_REPORT_HEAP_LIMIT = "android.app.action.REPORT_HEAP_LIMIT";
     field public static final int LOCK_TASK_MODE_LOCKED = 1; // 0x1
@@ -5038,12 +5050,14 @@
     method public android.app.Notification.Action.Builder extend(android.app.Notification.Action.Builder);
     method public java.lang.CharSequence getCancelLabel();
     method public java.lang.CharSequence getConfirmLabel();
+    method public boolean getHintDisplayActionInline();
     method public boolean getHintLaunchesActivity();
     method public java.lang.CharSequence getInProgressLabel();
     method public boolean isAvailableOffline();
     method public android.app.Notification.Action.WearableExtender setAvailableOffline(boolean);
     method public android.app.Notification.Action.WearableExtender setCancelLabel(java.lang.CharSequence);
     method public android.app.Notification.Action.WearableExtender setConfirmLabel(java.lang.CharSequence);
+    method public android.app.Notification.Action.WearableExtender setHintDisplayActionInline(boolean);
     method public android.app.Notification.Action.WearableExtender setHintLaunchesActivity(boolean);
     method public android.app.Notification.Action.WearableExtender setInProgressLabel(java.lang.CharSequence);
   }
@@ -5617,6 +5631,7 @@
   public class TimePickerDialog extends android.app.AlertDialog implements android.content.DialogInterface.OnClickListener android.widget.TimePicker.OnTimeChangedListener {
     ctor public TimePickerDialog(android.content.Context, android.app.TimePickerDialog.OnTimeSetListener, int, int, boolean);
     ctor public TimePickerDialog(android.content.Context, int, android.app.TimePickerDialog.OnTimeSetListener, int, int, boolean);
+    method public android.widget.TimePicker getTimePicker();
     method public void onClick(android.content.DialogInterface, int);
     method public void onTimeChanged(android.widget.TimePicker, int, int);
     method public void updateTime(int, int);
@@ -5762,7 +5777,10 @@
     method public android.content.pm.ServiceInfo getServiceInfo();
     method public java.lang.String getServiceName();
     method public java.lang.String getSettingsActivity();
+    method public boolean getShowMetadataInPreview();
     method public java.lang.CharSequence loadAuthor(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
+    method public java.lang.CharSequence loadContextDescription(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
+    method public android.net.Uri loadContextUri(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
     method public java.lang.CharSequence loadDescription(android.content.pm.PackageManager) throws android.content.res.Resources.NotFoundException;
     method public android.graphics.drawable.Drawable loadIcon(android.content.pm.PackageManager);
     method public java.lang.CharSequence loadLabel(android.content.pm.PackageManager);
@@ -6490,11 +6508,13 @@
     method public android.content.res.Configuration getConfiguration();
     method public int getEventType();
     method public java.lang.String getPackageName();
+    method public java.lang.String getShortcutId();
     method public long getTimeStamp();
     field public static final int CONFIGURATION_CHANGE = 5; // 0x5
     field public static final int MOVE_TO_BACKGROUND = 2; // 0x2
     field public static final int MOVE_TO_FOREGROUND = 1; // 0x1
     field public static final int NONE = 0; // 0x0
+    field public static final int SHORTCUT_INVOCATION = 8; // 0x8
     field public static final int USER_INTERACTION = 7; // 0x7
   }
 
@@ -8199,6 +8219,7 @@
     field public static final java.lang.String RESTRICTIONS_SERVICE = "restrictions";
     field public static final java.lang.String SEARCH_SERVICE = "search";
     field public static final java.lang.String SENSOR_SERVICE = "sensor";
+    field public static final java.lang.String SHORTCUT_SERVICE = "shortcut";
     field public static final java.lang.String STORAGE_SERVICE = "storage";
     field public static final java.lang.String SYSTEM_HEALTH_SERVICE = "systemhealth";
     field public static final java.lang.String TELECOM_SERVICE = "telecom";
@@ -9512,13 +9533,20 @@
   public class LauncherApps {
     ctor public LauncherApps(android.content.Context);
     method public java.util.List<android.content.pm.LauncherActivityInfo> getActivityList(java.lang.String, android.os.UserHandle);
+    method public android.graphics.drawable.Drawable getShortcutBadgedIconDrawable(android.content.pm.ShortcutInfo, int);
+    method public android.graphics.drawable.Drawable getShortcutIconDrawable(android.content.pm.ShortcutInfo, int);
+    method public java.util.List<android.content.pm.ShortcutInfo> getShortcuts(android.content.pm.LauncherApps.ShortcutQuery, android.os.UserHandle);
+    method public boolean hasShortcutHostPermission();
     method public boolean isActivityEnabled(android.content.ComponentName, android.os.UserHandle);
     method public boolean isPackageEnabled(java.lang.String, android.os.UserHandle);
+    method public void pinShortcuts(java.lang.String, java.util.List<java.lang.String>, 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 android.content.pm.LauncherActivityInfo resolveActivity(android.content.Intent, 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 startShortcut(java.lang.String, java.lang.String, android.graphics.Rect, android.os.Bundle, android.os.UserHandle);
+    method public void startShortcut(android.content.pm.ShortcutInfo, android.graphics.Rect, android.os.Bundle);
     method public void unregisterCallback(android.content.pm.LauncherApps.Callback);
   }
 
@@ -9531,6 +9559,20 @@
     method public void onPackagesSuspended(java.lang.String[], android.os.UserHandle);
     method public abstract void onPackagesUnavailable(java.lang.String[], android.os.UserHandle, boolean);
     method public void onPackagesUnsuspended(java.lang.String[], android.os.UserHandle);
+    method public void onShortcutsChanged(java.lang.String, java.util.List<android.content.pm.ShortcutInfo>, android.os.UserHandle);
+  }
+
+  public static class LauncherApps.ShortcutQuery {
+    ctor public LauncherApps.ShortcutQuery();
+    method public android.content.pm.LauncherApps.ShortcutQuery setActivity(android.content.ComponentName);
+    method public android.content.pm.LauncherApps.ShortcutQuery setChangedSince(long);
+    method public android.content.pm.LauncherApps.ShortcutQuery setPackage(java.lang.String);
+    method public android.content.pm.LauncherApps.ShortcutQuery setQueryFlags(int);
+    method public android.content.pm.LauncherApps.ShortcutQuery setShortcutIds(java.util.List<java.lang.String>);
+    field public static final int FLAG_GET_KEY_FIELDS_ONLY = 4; // 0x4
+    field public static final int FLAG_MATCH_DYNAMIC = 1; // 0x1
+    field public static final int FLAG_MATCH_MANIFEST = 8; // 0x8
+    field public static final int FLAG_MATCH_PINNED = 2; // 0x2
   }
 
   public class PackageInfo implements android.os.Parcelable {
@@ -10032,6 +10074,67 @@
     field public java.lang.String permission;
   }
 
+  public final class ShortcutInfo implements android.os.Parcelable {
+    method public int describeContents();
+    method public android.content.ComponentName getActivity();
+    method public java.util.Set<java.lang.String> getCategories();
+    method public java.lang.CharSequence getDisabledMessage();
+    method public android.os.PersistableBundle getExtras();
+    method public java.lang.String getId();
+    method public android.content.Intent getIntent();
+    method public android.content.Intent[] getIntents();
+    method public long getLastChangedTimestamp();
+    method public java.lang.CharSequence getLongLabel();
+    method public java.lang.String getPackage();
+    method public int getRank();
+    method public java.lang.CharSequence getShortLabel();
+    method public android.os.UserHandle getUserHandle();
+    method public boolean hasKeyFieldsOnly();
+    method public boolean isDeclaredInManifest();
+    method public boolean isDynamic();
+    method public boolean isEnabled();
+    method public boolean isImmutable();
+    method public boolean isPinned();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.content.pm.ShortcutInfo> CREATOR;
+    field public static final java.lang.String SHORTCUT_CATEGORY_CONVERSATION = "android.shortcut.conversation";
+  }
+
+  public static class ShortcutInfo.Builder {
+    ctor public ShortcutInfo.Builder(android.content.Context, java.lang.String);
+    method public android.content.pm.ShortcutInfo build();
+    method public android.content.pm.ShortcutInfo.Builder setActivity(android.content.ComponentName);
+    method public android.content.pm.ShortcutInfo.Builder setCategories(java.util.Set<java.lang.String>);
+    method public android.content.pm.ShortcutInfo.Builder setDisabledMessage(java.lang.CharSequence);
+    method public android.content.pm.ShortcutInfo.Builder setExtras(android.os.PersistableBundle);
+    method public android.content.pm.ShortcutInfo.Builder setIcon(android.graphics.drawable.Icon);
+    method public android.content.pm.ShortcutInfo.Builder setIntent(android.content.Intent);
+    method public android.content.pm.ShortcutInfo.Builder setIntents(android.content.Intent[]);
+    method public android.content.pm.ShortcutInfo.Builder setLongLabel(java.lang.CharSequence);
+    method public android.content.pm.ShortcutInfo.Builder setRank(int);
+    method public android.content.pm.ShortcutInfo.Builder setShortLabel(java.lang.CharSequence);
+  }
+
+  public class ShortcutManager {
+    ctor public ShortcutManager(android.content.Context);
+    method public boolean addDynamicShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+    method public void disableShortcuts(java.util.List<java.lang.String>);
+    method public void disableShortcuts(java.util.List<java.lang.String>, java.lang.CharSequence);
+    method public void enableShortcuts(java.util.List<java.lang.String>);
+    method public java.util.List<android.content.pm.ShortcutInfo> getDynamicShortcuts();
+    method public int getIconMaxHeight();
+    method public int getIconMaxWidth();
+    method public java.util.List<android.content.pm.ShortcutInfo> getManifestShortcuts();
+    method public int getMaxShortcutCountPerActivity();
+    method public java.util.List<android.content.pm.ShortcutInfo> getPinnedShortcuts();
+    method public boolean isRateLimitingActive();
+    method public void removeAllDynamicShortcuts();
+    method public void removeDynamicShortcuts(java.util.List<java.lang.String>);
+    method public void reportShortcutUsed(java.lang.String);
+    method public boolean setDynamicShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+    method public boolean updateShortcuts(java.util.List<android.content.pm.ShortcutInfo>);
+  }
+
   public class Signature implements android.os.Parcelable {
     ctor public Signature(byte[]);
     ctor public Signature(java.lang.String);
@@ -10238,7 +10341,7 @@
   }
 
   public class Resources {
-    ctor public Resources(android.content.res.AssetManager, android.util.DisplayMetrics, android.content.res.Configuration);
+    ctor public deprecated Resources(android.content.res.AssetManager, android.util.DisplayMetrics, android.content.res.Configuration);
     method public final void finishPreloading();
     method public final void flushLayoutCache();
     method public android.content.res.XmlResourceParser getAnimation(int) throws android.content.res.Resources.NotFoundException;
@@ -10289,7 +10392,7 @@
     method public android.content.res.AssetFileDescriptor openRawResourceFd(int) throws android.content.res.Resources.NotFoundException;
     method public void parseBundleExtra(java.lang.String, android.util.AttributeSet, android.os.Bundle) throws org.xmlpull.v1.XmlPullParserException;
     method public void parseBundleExtras(android.content.res.XmlResourceParser, android.os.Bundle) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException;
-    method public void updateConfiguration(android.content.res.Configuration, android.util.DisplayMetrics);
+    method public deprecated void updateConfiguration(android.content.res.Configuration, android.util.DisplayMetrics);
   }
 
   public static class Resources.NotFoundException extends java.lang.RuntimeException {
@@ -19625,6 +19728,7 @@
     field public static final android.os.Parcelable.Creator<android.media.AudioFormat> CREATOR;
     field public static final int ENCODING_AC3 = 5; // 0x5
     field public static final int ENCODING_DEFAULT = 1; // 0x1
+    field public static final int ENCODING_DOLBY_TRUEHD = 14; // 0xe
     field public static final int ENCODING_DTS = 7; // 0x7
     field public static final int ENCODING_DTS_HD = 8; // 0x8
     field public static final int ENCODING_E_AC3 = 6; // 0x6
@@ -28337,6 +28441,7 @@
     field public static final int LOLLIPOP_MR1 = 22; // 0x16
     field public static final int M = 23; // 0x17
     field public static final int N = 24; // 0x18
+    field public static final int N_MR1 = 25; // 0x19
   }
 
   public final class Bundle extends android.os.BaseBundle implements java.lang.Cloneable android.os.Parcelable {
@@ -29126,6 +29231,7 @@
     method public static final long getStartElapsedRealtime();
     method public static final long getStartUptimeMillis();
     method public static final int getThreadPriority(int) throws java.lang.IllegalArgumentException;
+    method public static final int getThreadScheduler(int) throws java.lang.IllegalArgumentException;
     method public static final int getUidForName(java.lang.String);
     method public static final boolean is64Bit();
     method public static boolean isApplicationUid(int);
@@ -29331,6 +29437,7 @@
     method public android.os.Bundle getUserRestrictions();
     method public android.os.Bundle getUserRestrictions(android.os.UserHandle);
     method public boolean hasUserRestriction(java.lang.String);
+    method public boolean isDemoUser();
     method public boolean isQuietModeEnabled(android.os.UserHandle);
     method public boolean isSystemUser();
     method public boolean isUserAGoat();
@@ -29570,6 +29677,7 @@
     method public boolean isObbMounted(java.lang.String);
     method public boolean mountObb(java.lang.String, java.lang.String, android.os.storage.OnObbStateChangeListener);
     method public boolean unmountObb(java.lang.String, boolean, android.os.storage.OnObbStateChangeListener);
+    field public static final java.lang.String ACTION_MANAGE_STORAGE = "android.os.storage.action.MANAGE_STORAGE";
   }
 
   public final class StorageVolume implements android.os.Parcelable {
@@ -30715,6 +30823,7 @@
   public static class CallLog.Calls implements android.provider.BaseColumns {
     ctor public CallLog.Calls();
     method public static java.lang.String getLastOutgoingCall(android.content.Context);
+    field public static final int ANSWERED_EXTERNALLY_TYPE = 7; // 0x7
     field public static final int BLOCKED_TYPE = 6; // 0x6
     field public static final java.lang.String CACHED_FORMATTED_NUMBER = "formatted_number";
     field public static final java.lang.String CACHED_LOOKUP_URI = "lookup_uri";
@@ -30737,6 +30846,7 @@
     field public static final java.lang.String DURATION = "duration";
     field public static final java.lang.String EXTRA_CALL_TYPE_FILTER = "android.provider.extra.CALL_TYPE_FILTER";
     field public static final java.lang.String FEATURES = "features";
+    field public static final int FEATURES_PULLED_EXTERNALLY = 2; // 0x2
     field public static final int FEATURES_VIDEO = 1; // 0x1
     field public static final java.lang.String GEOCODED_LOCATION = "geocoded_location";
     field public static final int INCOMING_TYPE = 1; // 0x1
@@ -32442,6 +32552,7 @@
     field public static final java.lang.String DATA_ROAMING = "data_roaming";
     field public static final java.lang.String DEBUG_APP = "debug_app";
     field public static final java.lang.String DEVELOPMENT_SETTINGS_ENABLED = "development_settings_enabled";
+    field public static final java.lang.String DEVICE_NAME = "device_name";
     field public static final java.lang.String DEVICE_PROVISIONED = "device_provisioned";
     field public static final java.lang.String HTTP_PROXY = "http_proxy";
     field public static final deprecated java.lang.String INSTALL_NON_MARKET_APPS = "install_non_market_apps";
@@ -33030,6 +33141,7 @@
     field public static final java.lang.String ACTION_NEW_VOICEMAIL = "android.intent.action.NEW_VOICEMAIL";
     field public static final java.lang.String ACTION_SYNC_VOICEMAIL = "android.provider.action.SYNC_VOICEMAIL";
     field public static final java.lang.String AUTHORITY = "com.android.voicemail";
+    field public static final java.lang.String EXTRA_PHONE_ACCOUNT_HANDLE = "android.provider.extra.PHONE_ACCOUNT_HANDLE";
     field public static final java.lang.String EXTRA_SELF_CHANGE = "com.android.voicemail.extra.SELF_CHANGE";
     field public static final java.lang.String PARAM_KEY_SOURCE_PACKAGE = "source_package";
   }
@@ -33038,6 +33150,9 @@
     method public static android.net.Uri buildSourceUri(java.lang.String);
     field public static final java.lang.String CONFIGURATION_STATE = "configuration_state";
     field public static final int CONFIGURATION_STATE_CAN_BE_CONFIGURED = 2; // 0x2
+    field public static final int CONFIGURATION_STATE_CONFIGURING = 3; // 0x3
+    field public static final int CONFIGURATION_STATE_DISABLED = 5; // 0x5
+    field public static final int CONFIGURATION_STATE_FAILED = 4; // 0x4
     field public static final int CONFIGURATION_STATE_NOT_CONFIGURED = 1; // 0x1
     field public static final int CONFIGURATION_STATE_OK = 0; // 0x0
     field public static final android.net.Uri CONTENT_URI;
@@ -33062,6 +33177,7 @@
     field public static final int QUOTA_UNAVAILABLE = -1; // 0xffffffff
     field public static final java.lang.String SETTINGS_URI = "settings_uri";
     field public static final java.lang.String SOURCE_PACKAGE = "source_package";
+    field public static final java.lang.String SOURCE_TYPE = "source_type";
     field public static final java.lang.String VOICEMAIL_ACCESS_URI = "voicemail_access_uri";
   }
 
@@ -36022,9 +36138,14 @@
     method public void phoneAccountSelected(android.telecom.PhoneAccountHandle, boolean);
     method public void playDtmfTone(char);
     method public void postDialContinue(boolean);
+    method public void pullExternalCall();
+    method public final void putExtras(android.os.Bundle);
     method public void registerCallback(android.telecom.Call.Callback);
     method public void registerCallback(android.telecom.Call.Callback, android.os.Handler);
     method public void reject(boolean, java.lang.String);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
+    method public void sendCallEvent(java.lang.String, android.os.Bundle);
     method public void splitFromConference();
     method public void stopDtmfTone();
     method public void swapConference();
@@ -36038,6 +36159,7 @@
     field public static final int STATE_DISCONNECTING = 10; // 0xa
     field public static final int STATE_HOLDING = 3; // 0x3
     field public static final int STATE_NEW = 0; // 0x0
+    field public static final int STATE_PULLING_CALL = 11; // 0xb
     field public static final int STATE_RINGING = 2; // 0x2
     field public static final int STATE_SELECT_PHONE_ACCOUNT = 8; // 0x8
   }
@@ -36048,6 +36170,7 @@
     method public void onCannedTextResponsesLoaded(android.telecom.Call, java.util.List<java.lang.String>);
     method public void onChildrenChanged(android.telecom.Call, java.util.List<android.telecom.Call>);
     method public void onConferenceableCallsChanged(android.telecom.Call, java.util.List<android.telecom.Call>);
+    method public void onConnectionEvent(android.telecom.Call, java.lang.String, android.os.Bundle);
     method public void onDetailsChanged(android.telecom.Call, android.telecom.Call.Details);
     method public void onParentChanged(android.telecom.Call, android.telecom.Call);
     method public void onPostDialWait(android.telecom.Call, java.lang.String);
@@ -36078,6 +36201,7 @@
     method public static java.lang.String propertiesToString(int);
     field public static final int CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO = 4194304; // 0x400000
     field public static final int CAPABILITY_CAN_PAUSE_VIDEO = 1048576; // 0x100000
+    field public static final int CAPABILITY_CAN_PULL_CALL = 8388608; // 0x800000
     field public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 8192; // 0x2000
     field public static final int CAPABILITY_HOLD = 1; // 0x1
     field public static final int CAPABILITY_MANAGE_CONFERENCE = 128; // 0x80
@@ -36097,7 +36221,9 @@
     field public static final int PROPERTY_EMERGENCY_CALLBACK_MODE = 4; // 0x4
     field public static final int PROPERTY_ENTERPRISE_CALL = 32; // 0x20
     field public static final int PROPERTY_GENERIC_CONFERENCE = 2; // 0x2
+    field public static final int PROPERTY_HAS_CDMA_VOICE_PRIVACY = 128; // 0x80
     field public static final int PROPERTY_HIGH_DEF_AUDIO = 16; // 0x10
+    field public static final int PROPERTY_IS_EXTERNAL_CALL = 64; // 0x40
     field public static final int PROPERTY_WIFI = 8; // 0x8
   }
 
@@ -36148,6 +36274,7 @@
     method public final android.telecom.CallAudioState getCallAudioState();
     method public final java.util.List<android.telecom.Connection> getConferenceableConnections();
     method public final int getConnectionCapabilities();
+    method public final int getConnectionProperties();
     method public final long getConnectionTime();
     method public final java.util.List<android.telecom.Connection> getConnections();
     method public final android.telecom.DisconnectCause getDisconnectCause();
@@ -36160,6 +36287,7 @@
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
     method public void onConnectionAdded(android.telecom.Connection);
     method public void onDisconnect();
+    method public void onExtrasChanged(android.os.Bundle);
     method public void onHold();
     method public void onMerge(android.telecom.Connection);
     method public void onMerge();
@@ -36168,10 +36296,14 @@
     method public void onStopDtmfTone();
     method public void onSwap();
     method public void onUnhold();
+    method public final void putExtras(android.os.Bundle);
     method public final void removeConnection(android.telecom.Connection);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
     method public final void setActive();
     method public final void setConferenceableConnections(java.util.List<android.telecom.Connection>);
     method public final void setConnectionCapabilities(int);
+    method public final void setConnectionProperties(int);
     method public final void setConnectionTime(long);
     method public final void setDialing();
     method public final void setDisconnected(android.telecom.DisconnectCause);
@@ -36201,6 +36333,7 @@
     method public final android.telecom.Conference getConference();
     method public final java.util.List<android.telecom.Conferenceable> getConferenceables();
     method public final int getConnectionCapabilities();
+    method public final int getConnectionProperties();
     method public final android.telecom.DisconnectCause getDisconnectCause();
     method public final android.os.Bundle getExtras();
     method public final int getState();
@@ -36211,16 +36344,24 @@
     method public void onAnswer(int);
     method public void onAnswer();
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
+    method public void onCallEvent(java.lang.String, android.os.Bundle);
     method public void onDisconnect();
+    method public void onExtrasChanged(android.os.Bundle);
     method public void onHold();
     method public void onPlayDtmfTone(char);
     method public void onPostDialContinue(boolean);
+    method public void onPullExternalCall();
     method public void onReject();
     method public void onReject(java.lang.String);
     method public void onSeparate();
     method public void onStateChanged(int);
     method public void onStopDtmfTone();
     method public void onUnhold();
+    method public static java.lang.String propertiesToString(int);
+    method public final void putExtras(android.os.Bundle);
+    method public final void removeExtras(java.util.List<java.lang.String>);
+    method public final void removeExtras(java.lang.String...);
+    method public void sendConnectionEvent(java.lang.String, android.os.Bundle);
     method public final void setActive();
     method public final void setAddress(android.net.Uri, int);
     method public final void setAudioModeIsVoip(boolean);
@@ -36228,6 +36369,7 @@
     method public final void setConferenceableConnections(java.util.List<android.telecom.Connection>);
     method public final void setConferenceables(java.util.List<android.telecom.Conferenceable>);
     method public final void setConnectionCapabilities(int);
+    method public final void setConnectionProperties(int);
     method public final void setDialing();
     method public final void setDisconnected(android.telecom.DisconnectCause);
     method public final void setExtras(android.os.Bundle);
@@ -36236,6 +36378,7 @@
     method public final void setNextPostDialChar(char);
     method public final void setOnHold();
     method public final void setPostDialWait(java.lang.String);
+    method public final void setPulling();
     method public final void setRingbackRequested(boolean);
     method public final void setRinging();
     method public final void setStatusHints(android.telecom.StatusHints);
@@ -36244,6 +36387,7 @@
     method public static java.lang.String stateToString(int);
     field public static final int CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO = 8388608; // 0x800000
     field public static final int CAPABILITY_CAN_PAUSE_VIDEO = 1048576; // 0x100000
+    field public static final int CAPABILITY_CAN_PULL_CALL = 16777216; // 0x1000000
     field public static final int CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION = 4194304; // 0x400000
     field public static final int CAPABILITY_CAN_UPGRADE_TO_VIDEO = 524288; // 0x80000
     field public static final int CAPABILITY_DISCONNECT_FROM_CONFERENCE = 8192; // 0x2000
@@ -36261,15 +36405,21 @@
     field public static final int CAPABILITY_SUPPORTS_VT_REMOTE_TX = 2048; // 0x800
     field public static final int CAPABILITY_SUPPORT_HOLD = 2; // 0x2
     field public static final int CAPABILITY_SWAP_CONFERENCE = 8; // 0x8
+    field public static final java.lang.String EVENT_CALL_MERGE_FAILED = "android.telecom.event.CALL_MERGE_FAILED";
+    field public static final java.lang.String EVENT_CALL_PULL_FAILED = "android.telecom.event.CALL_PULL_FAILED";
+    field public static final java.lang.String EXTRA_ANSWERING_DROPS_FG_CALL = "android.telecom.extra.ANSWERING_DROPS_FG_CALL";
     field public static final java.lang.String EXTRA_CALL_SUBJECT = "android.telecom.extra.CALL_SUBJECT";
     field public static final java.lang.String EXTRA_CHILD_ADDRESS = "android.telecom.extra.CHILD_ADDRESS";
     field public static final java.lang.String EXTRA_LAST_FORWARDED_NUMBER = "android.telecom.extra.LAST_FORWARDED_NUMBER";
+    field public static final int PROPERTY_HAS_CDMA_VOICE_PRIVACY = 32; // 0x20
+    field public static final int PROPERTY_IS_EXTERNAL_CALL = 16; // 0x10
     field public static final int STATE_ACTIVE = 4; // 0x4
     field public static final int STATE_DIALING = 3; // 0x3
     field public static final int STATE_DISCONNECTED = 6; // 0x6
     field public static final int STATE_HOLDING = 5; // 0x5
     field public static final int STATE_INITIALIZING = 0; // 0x0
     field public static final int STATE_NEW = 1; // 0x1
+    field public static final int STATE_PULLING_CALL = 7; // 0x7
     field public static final int STATE_RINGING = 2; // 0x2
   }
 
@@ -36347,7 +36497,9 @@
     method public java.lang.String getReason();
     method public int getTone();
     method public void writeToParcel(android.os.Parcel, int);
+    field public static final int ANSWERED_ELSEWHERE = 11; // 0xb
     field public static final int BUSY = 7; // 0x7
+    field public static final int CALL_PULLED = 12; // 0xc
     field public static final int CANCELED = 4; // 0x4
     field public static final int CONNECTION_MANAGER_NOT_SUPPORTED = 10; // 0xa
     field public static final android.os.Parcelable.Creator<android.telecom.DisconnectCause> CREATOR;
@@ -36383,6 +36535,7 @@
     method public void onCallAudioStateChanged(android.telecom.CallAudioState);
     method public void onCallRemoved(android.telecom.Call);
     method public void onCanAddCallChanged(boolean);
+    method public void onConnectionEvent(android.telecom.Call, java.lang.String, android.os.Bundle);
     method public void onSilenceRinger();
     method public final void setAudioRoute(int);
     method public final void setMuted(boolean);
@@ -36505,6 +36658,7 @@
     method public void onConferenceableConnectionsChanged(android.telecom.RemoteConference, java.util.List<android.telecom.RemoteConnection>);
     method public void onConnectionAdded(android.telecom.RemoteConference, android.telecom.RemoteConnection);
     method public void onConnectionCapabilitiesChanged(android.telecom.RemoteConference, int);
+    method public void onConnectionPropertiesChanged(android.telecom.RemoteConference, int);
     method public void onConnectionRemoved(android.telecom.RemoteConference, android.telecom.RemoteConnection);
     method public void onDestroyed(android.telecom.RemoteConference);
     method public void onDisconnected(android.telecom.RemoteConference, android.telecom.DisconnectCause);
@@ -36523,6 +36677,7 @@
     method public android.telecom.RemoteConference getConference();
     method public java.util.List<android.telecom.RemoteConnection> getConferenceableConnections();
     method public int getConnectionCapabilities();
+    method public int getConnectionProperties();
     method public android.telecom.DisconnectCause getDisconnectCause();
     method public final android.os.Bundle getExtras();
     method public int getState();
@@ -36534,6 +36689,7 @@
     method public boolean isVoipAudioMode();
     method public void playDtmfTone(char);
     method public void postDialContinue(boolean);
+    method public void pullExternalCall();
     method public void registerCallback(android.telecom.RemoteConnection.Callback);
     method public void registerCallback(android.telecom.RemoteConnection.Callback, android.os.Handler);
     method public void reject();
@@ -36550,6 +36706,8 @@
     method public void onConferenceChanged(android.telecom.RemoteConnection, android.telecom.RemoteConference);
     method public void onConferenceableConnectionsChanged(android.telecom.RemoteConnection, java.util.List<android.telecom.RemoteConnection>);
     method public void onConnectionCapabilitiesChanged(android.telecom.RemoteConnection, int);
+    method public void onConnectionEvent(android.telecom.RemoteConnection, java.lang.String, android.os.Bundle);
+    method public void onConnectionPropertiesChanged(android.telecom.RemoteConnection, int);
     method public void onDestroyed(android.telecom.RemoteConnection);
     method public void onDisconnected(android.telecom.RemoteConnection, android.telecom.DisconnectCause);
     method public void onExtrasChanged(android.telecom.RemoteConnection, android.os.Bundle);
@@ -36646,6 +36804,7 @@
     field public static final java.lang.String EXTRA_START_CALL_WITH_VIDEO_STATE = "android.telecom.extra.START_CALL_WITH_VIDEO_STATE";
     field public static final java.lang.String GATEWAY_ORIGINAL_ADDRESS = "android.telecom.extra.GATEWAY_ORIGINAL_ADDRESS";
     field public static final java.lang.String GATEWAY_PROVIDER_PACKAGE = "android.telecom.extra.GATEWAY_PROVIDER_PACKAGE";
+    field public static final java.lang.String METADATA_INCLUDE_EXTERNAL_CALLS = "android.telecom.INCLUDE_EXTERNAL_CALLS";
     field public static final java.lang.String METADATA_IN_CALL_SERVICE_RINGING = "android.telecom.IN_CALL_SERVICE_RINGING";
     field public static final java.lang.String METADATA_IN_CALL_SERVICE_UI = "android.telecom.IN_CALL_SERVICE_UI";
     field public static final int PRESENTATION_ALLOWED = 1; // 0x1
@@ -36700,9 +36859,11 @@
     field public static final java.lang.String ACTION_CARRIER_CONFIG_CHANGED = "android.telephony.action.CARRIER_CONFIG_CHANGED";
     field public static final java.lang.String KEY_ADDITIONAL_CALL_SETTING_BOOL = "additional_call_setting_bool";
     field public static final java.lang.String KEY_ALLOW_ADDING_APNS_BOOL = "allow_adding_apns_bool";
+    field public static final java.lang.String KEY_ALLOW_ADD_CALL_DURING_VIDEO_CALL_BOOL = "allow_add_call_during_video_call";
     field public static final java.lang.String KEY_ALLOW_EMERGENCY_NUMBERS_IN_CALL_LOG_BOOL = "allow_emergency_numbers_in_call_log_bool";
     field public static final java.lang.String KEY_ALLOW_EMERGENCY_VIDEO_CALLS_BOOL = "allow_emergency_video_calls_bool";
     field public static final java.lang.String KEY_ALLOW_LOCAL_DTMF_TONES_BOOL = "allow_local_dtmf_tones_bool";
+    field public static final java.lang.String KEY_ALLOW_MERGE_WIFI_CALLS_WHEN_VOWIFI_OFF_BOOL = "allow_merge_wifi_calls_when_vowifi_off_bool";
     field public static final java.lang.String KEY_ALLOW_NON_EMERGENCY_CALLS_IN_ECM_BOOL = "allow_non_emergency_calls_in_ecm_bool";
     field public static final java.lang.String KEY_ALWAYS_SHOW_EMERGENCY_ALERT_ONOFF_BOOL = "always_show_emergency_alert_onoff_bool";
     field public static final java.lang.String KEY_APN_EXPAND_BOOL = "apn_expand_bool";
@@ -36734,6 +36895,7 @@
     field public static final java.lang.String KEY_CSP_ENABLED_BOOL = "csp_enabled_bool";
     field public static final java.lang.String KEY_DEFAULT_SIM_CALL_MANAGER_STRING = "default_sim_call_manager_string";
     field public static final java.lang.String KEY_DISABLE_CDMA_ACTIVATION_CODE_BOOL = "disable_cdma_activation_code_bool";
+    field public static final java.lang.String KEY_DROP_VIDEO_CALL_WHEN_ANSWERING_AUDIO_CALL_BOOL = "drop_video_call_when_answering_audio_call_bool";
     field public static final java.lang.String KEY_DTMF_TYPE_ENABLED_BOOL = "dtmf_type_enabled_bool";
     field public static final java.lang.String KEY_DURATION_BLOCKING_DISABLED_AFTER_EMERGENCY_INT = "duration_blocking_disabled_after_emergency_int";
     field public static final java.lang.String KEY_EDITABLE_ENHANCED_4G_LTE_BOOL = "editable_enhanced_4g_lte_bool";
@@ -36791,6 +36953,7 @@
     field public static final java.lang.String KEY_SUPPORT_CONFERENCE_CALL_BOOL = "support_conference_call_bool";
     field public static final java.lang.String KEY_SUPPORT_PAUSE_IMS_VIDEO_CALLS_BOOL = "support_pause_ims_video_calls_bool";
     field public static final java.lang.String KEY_SUPPORT_SWAP_AFTER_MERGE_BOOL = "support_swap_after_merge_bool";
+    field public static final java.lang.String KEY_TREAT_DOWNGRADED_VIDEO_CALLS_AS_VIDEO_CALLS_BOOL = "treat_downgraded_video_calls_as_video_calls_bool";
     field public static final java.lang.String KEY_USE_HFA_FOR_PROVISIONING_BOOL = "use_hfa_for_provisioning_bool";
     field public static final java.lang.String KEY_USE_OTASP_FOR_PROVISIONING_BOOL = "use_otasp_for_provisioning_bool";
     field public static final java.lang.String KEY_USE_RCS_PRESENCE_BOOL = "use_rcs_presence_bool";
@@ -37375,12 +37538,15 @@
     field public static final int NETWORK_TYPE_EVDO_A = 6; // 0x6
     field public static final int NETWORK_TYPE_EVDO_B = 12; // 0xc
     field public static final int NETWORK_TYPE_GPRS = 1; // 0x1
+    field public static final int NETWORK_TYPE_GSM = 16; // 0x10
     field public static final int NETWORK_TYPE_HSDPA = 8; // 0x8
     field public static final int NETWORK_TYPE_HSPA = 10; // 0xa
     field public static final int NETWORK_TYPE_HSPAP = 15; // 0xf
     field public static final int NETWORK_TYPE_HSUPA = 9; // 0x9
     field public static final int NETWORK_TYPE_IDEN = 11; // 0xb
+    field public static final int NETWORK_TYPE_IWLAN = 18; // 0x12
     field public static final int NETWORK_TYPE_LTE = 13; // 0xd
+    field public static final int NETWORK_TYPE_TD_SCDMA = 17; // 0x11
     field public static final int NETWORK_TYPE_UMTS = 3; // 0x3
     field public static final int NETWORK_TYPE_UNKNOWN = 0; // 0x0
     field public static final int PHONE_TYPE_CDMA = 2; // 0x2
@@ -40183,7 +40349,10 @@
     method public boolean equals(android.util.DisplayMetrics);
     method public void setTo(android.util.DisplayMetrics);
     method public void setToDefaults();
+    field public static final int DENSITY_260 = 260; // 0x104
     field public static final int DENSITY_280 = 280; // 0x118
+    field public static final int DENSITY_300 = 300; // 0x12c
+    field public static final int DENSITY_340 = 340; // 0x154
     field public static final int DENSITY_360 = 360; // 0x168
     field public static final int DENSITY_400 = 400; // 0x190
     field public static final int DENSITY_420 = 420; // 0x1a4
@@ -41513,6 +41682,10 @@
     field public static final int KEYCODE_SWITCH_CHARSET = 95; // 0x5f
     field public static final int KEYCODE_SYM = 63; // 0x3f
     field public static final int KEYCODE_SYSRQ = 120; // 0x78
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_DOWN = 281; // 0x119
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_LEFT = 282; // 0x11a
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_RIGHT = 283; // 0x11b
+    field public static final int KEYCODE_SYSTEM_NAVIGATION_UP = 280; // 0x118
     field public static final int KEYCODE_T = 48; // 0x30
     field public static final int KEYCODE_TAB = 61; // 0x3d
     field public static final int KEYCODE_TV = 170; // 0xaa
@@ -42395,6 +42568,7 @@
     method public float getPivotY();
     method public android.view.PointerIcon getPointerIcon();
     method public android.content.res.Resources getResources();
+    method public final boolean getRevealOnFocusHint();
     method public final int getRight();
     method protected float getRightFadingEdgeStrength();
     method protected int getRightPaddingOffset();
@@ -42682,6 +42856,7 @@
     method public void setPivotY(float);
     method public void setPointerIcon(android.view.PointerIcon);
     method public void setPressed(boolean);
+    method public final void setRevealOnFocusHint(boolean);
     method public final void setRight(int);
     method public void setRotation(float);
     method public void setRotationX(float);
@@ -43831,6 +44006,7 @@
     field public static final int TYPE_APPLICATION_SUB_PANEL = 1002; // 0x3ea
     field public static final int TYPE_BASE_APPLICATION = 1; // 0x1
     field public static final int TYPE_CHANGED = 2; // 0x2
+    field public static final int TYPE_DRAWN_APPLICATION = 4; // 0x4
     field public static final int TYPE_INPUT_METHOD = 2011; // 0x7db
     field public static final int TYPE_INPUT_METHOD_DIALOG = 2012; // 0x7dc
     field public static final int TYPE_KEYGUARD_DIALOG = 2009; // 0x7d9
@@ -44603,6 +44779,7 @@
     method public boolean clearMetaKeyStates(int);
     method public void closeConnection();
     method public boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public boolean commitText(java.lang.CharSequence, int);
     method public boolean deleteSurroundingText(int, int);
@@ -44712,6 +44889,7 @@
     field public static final int IME_NULL = 0; // 0x0
     field public int actionId;
     field public java.lang.CharSequence actionLabel;
+    field public java.lang.String[] contentMimeTypes;
     field public android.os.Bundle extras;
     field public int fieldId;
     field public java.lang.String fieldName;
@@ -44771,6 +44949,7 @@
     method public abstract boolean clearMetaKeyStates(int);
     method public abstract void closeConnection();
     method public abstract boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public abstract boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public abstract boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public abstract boolean commitText(java.lang.CharSequence, int);
     method public abstract boolean deleteSurroundingText(int, int);
@@ -44796,6 +44975,7 @@
     field public static final int CURSOR_UPDATE_MONITOR = 2; // 0x2
     field public static final int GET_EXTRACTED_TEXT_MONITOR = 1; // 0x1
     field public static final int GET_TEXT_WITH_STYLES = 1; // 0x1
+    field public static final int INPUT_CONTENT_GRANT_READ_URI_PERMISSION = 1; // 0x1
   }
 
   public class InputConnectionWrapper implements android.view.inputmethod.InputConnection {
@@ -44804,6 +44984,7 @@
     method public boolean clearMetaKeyStates(int);
     method public void closeConnection();
     method public boolean commitCompletion(android.view.inputmethod.CompletionInfo);
+    method public boolean commitContent(android.view.inputmethod.InputContentInfo, int, android.os.Bundle);
     method public boolean commitCorrection(android.view.inputmethod.CorrectionInfo);
     method public boolean commitText(java.lang.CharSequence, int);
     method public boolean deleteSurroundingText(int, int);
@@ -44828,6 +45009,19 @@
     method public void setTarget(android.view.inputmethod.InputConnection);
   }
 
+  public final class InputContentInfo implements android.os.Parcelable {
+    ctor public InputContentInfo(android.net.Uri, android.content.ClipDescription);
+    ctor public InputContentInfo(android.net.Uri, android.content.ClipDescription, android.net.Uri);
+    method public int describeContents();
+    method public android.net.Uri getContentUri();
+    method public android.content.ClipDescription getDescription();
+    method public android.net.Uri getLinkUri();
+    method public void releasePermission();
+    method public void requestPermission();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.view.inputmethod.InputContentInfo> CREATOR;
+  }
+
   public abstract interface InputMethod {
     method public abstract void attachToken(android.os.IBinder);
     method public abstract void bindInput(android.view.inputmethod.InputBinding);
@@ -46421,6 +46615,7 @@
     method public int getFirstDayOfWeek();
     method public long getMaxDate();
     method public long getMinDate();
+    method public int getMode();
     method public int getMonth();
     method public deprecated boolean getSpinnersShown();
     method public int getYear();
@@ -46431,6 +46626,8 @@
     method public void setMinDate(long);
     method public deprecated void setSpinnersShown(boolean);
     method public void updateDate(int, int, int);
+    field public static final int MODE_CALENDAR = 2; // 0x2
+    field public static final int MODE_SPINNER = 1; // 0x1
   }
 
   public static abstract interface DatePicker.OnDateChangedListener {
@@ -48195,6 +48392,7 @@
     method public deprecated java.lang.Integer getCurrentMinute();
     method public int getHour();
     method public int getMinute();
+    method public int getMode();
     method public boolean is24HourView();
     method public deprecated void setCurrentHour(java.lang.Integer);
     method public deprecated void setCurrentMinute(java.lang.Integer);
@@ -48202,6 +48400,8 @@
     method public void setIs24HourView(java.lang.Boolean);
     method public void setMinute(int);
     method public void setOnTimeChangedListener(android.widget.TimePicker.OnTimeChangedListener);
+    field public static final int MODE_CLOCK = 2; // 0x2
+    field public static final int MODE_SPINNER = 1; // 0x1
   }
 
   public static abstract interface TimePicker.OnTimeChangedListener {
diff --git a/cmds/am/src/com/android/commands/am/Am.java b/cmds/am/src/com/android/commands/am/Am.java
index 8ccd5d2e..d6c0058 100644
--- a/cmds/am/src/com/android/commands/am/Am.java
+++ b/cmds/am/src/com/android/commands/am/Am.java
@@ -723,10 +723,10 @@
                 System.out.println("Complete");
             }
             mRepeat--;
-            if (mRepeat > 1) {
+            if (mRepeat > 0) {
                 mAm.unhandledBack();
             }
-        } while (mRepeat > 1);
+        } while (mRepeat > 0);
     }
 
     private void runForceStop() throws Exception {
diff --git a/cmds/bootanimation/Android.mk b/cmds/bootanimation/Android.mk
index 7f3fe22..3a92b9e 100644
--- a/cmds/bootanimation/Android.mk
+++ b/cmds/bootanimation/Android.mk
@@ -3,14 +3,16 @@
 
 LOCAL_SRC_FILES:= \
     bootanimation_main.cpp \
-    AudioPlayer.cpp \
+    audioplay.cpp \
     BootAnimation.cpp
 
 LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES -DEGL_EGLEXT_PROTOTYPES
 
 LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code
 
-LOCAL_C_INCLUDES += external/tinyalsa/include
+LOCAL_C_INCLUDES += \
+    external/tinyalsa/include \
+    frameworks/wilhelm/include
 
 LOCAL_SHARED_LIBRARIES := \
     libcutils \
@@ -23,8 +25,8 @@
     libEGL \
     libGLESv1_CM \
     libgui \
-    libtinyalsa \
-    libmedia
+    libOpenSLES \
+    libtinyalsa
 
 LOCAL_MODULE:= bootanimation
 
diff --git a/cmds/bootanimation/AudioPlayer.cpp b/cmds/bootanimation/AudioPlayer.cpp
deleted file mode 100644
index f5acac4..0000000
--- a/cmds/bootanimation/AudioPlayer.cpp
+++ /dev/null
@@ -1,317 +0,0 @@
-/*
- * Copyright (C) 2014 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.
- */
-
-#define LOG_NDEBUG 0
-#define LOG_TAG "BootAnim_AudioPlayer"
-
-#include "AudioPlayer.h"
-
-#include <androidfw/ZipFileRO.h>
-#include <tinyalsa/asoundlib.h>
-#include <utils/Log.h>
-#include <utils/String8.h>
-#include <media/AudioSystem.h>
-
-#define ID_RIFF 0x46464952
-#define ID_WAVE 0x45564157
-#define ID_FMT  0x20746d66
-#define ID_DATA 0x61746164
-
-// Maximum line length for audio_conf.txt
-// We only accept lines less than this length to avoid overflows using sscanf()
-#define MAX_LINE_LENGTH 1024
-
-struct riff_wave_header {
-    uint32_t riff_id;
-    uint32_t riff_sz;
-    uint32_t wave_id;
-};
-
-struct chunk_header {
-    uint32_t id;
-    uint32_t sz;
-};
-
-struct chunk_fmt {
-    uint16_t audio_format;
-    uint16_t num_channels;
-    uint32_t sample_rate;
-    uint32_t byte_rate;
-    uint16_t block_align;
-    uint16_t bits_per_sample;
-};
-
-
-namespace android {
-
-AudioPlayer::AudioPlayer()
-    :   mCard(-1),
-        mDevice(-1),
-        mPeriodSize(0),
-        mPeriodCount(0),
-        mCurrentFile(NULL)
-{
-}
-
-AudioPlayer::~AudioPlayer() {
-}
-
-static bool setMixerValue(struct mixer* mixer, const char* name, const char* values)
-{
-    if (!mixer) {
-        ALOGE("no mixer in setMixerValue");
-        return false;
-    }
-    struct mixer_ctl *ctl = mixer_get_ctl_by_name(mixer, name);
-    if (!ctl) {
-        ALOGE("mixer_get_ctl_by_name failed for %s", name);
-        return false;
-    }
-
-    enum mixer_ctl_type type = mixer_ctl_get_type(ctl);
-    int numValues = mixer_ctl_get_num_values(ctl);
-    int intValue;
-    char stringValue[MAX_LINE_LENGTH];
-
-    for (int i = 0; i < numValues && values; i++) {
-        // strip leading space
-        while (*values == ' ') values++;
-        if (*values == 0) break;
-
-        switch (type) {
-            case MIXER_CTL_TYPE_BOOL:
-            case MIXER_CTL_TYPE_INT:
-                if (sscanf(values, "%d", &intValue) == 1) {
-                    if (mixer_ctl_set_value(ctl, i, intValue) != 0) {
-                        ALOGE("mixer_ctl_set_value failed for %s %d", name, intValue);
-                    }
-                } else {
-                    ALOGE("Could not parse %s as int for %s", values, name);
-                }
-                break;
-            case MIXER_CTL_TYPE_ENUM:
-                if (sscanf(values, "%s", stringValue) == 1) {
-                    if (mixer_ctl_set_enum_by_string(ctl, stringValue) != 0) {
-                        ALOGE("mixer_ctl_set_enum_by_string failed for %s %s", name, stringValue);
-                    }
-                } else {
-                    ALOGE("Could not parse %s as enum for %s", values, name);
-                }
-                break;
-            default:
-                ALOGE("unsupported mixer type %d for %s", type, name);
-                break;
-        }
-
-        values = strchr(values, ' ');
-    }
-
-    return true;
-}
-
-
-/*
- * Parse the audio configuration file.
- * The file is named audio_conf.txt and must begin with the following header:
- *
- * card=<ALSA card number>
- * device=<ALSA device number>
- * period_size=<period size>
- * period_count=<period count>
- *
- * This header is followed by zero or more mixer settings, each with the format:
- * mixer "<name>" = <value list>
- * Since mixer names can contain spaces, the name must be enclosed in double quotes.
- * The values in the value list can be integers, booleans (represented by 0 or 1)
- * or strings for enum values.
- */
-bool AudioPlayer::init(const char* config)
-{
-    int tempInt;
-    struct mixer* mixer = NULL;
-    char    name[MAX_LINE_LENGTH];
-
-    // Fake call to wait for audio HAL initialization completion
-    AudioSystem::getParameters(String8(""));
-
-    for (;;) {
-        const char* endl = strstr(config, "\n");
-        if (!endl) break;
-        String8 line(config, endl - config);
-        if (line.length() >= MAX_LINE_LENGTH) {
-            ALOGE("Line too long in audio_conf.txt");
-            return false;
-        }
-        const char* l = line.string();
-
-        if (sscanf(l, "card=%d", &tempInt) == 1) {
-            ALOGD("card=%d", tempInt);
-            mCard = tempInt;
-
-            mixer = mixer_open(mCard);
-            if (!mixer) {
-                ALOGE("could not open mixer for card %d", mCard);
-                return false;
-            }
-        } else if (sscanf(l, "device=%d", &tempInt) == 1) {
-            ALOGD("device=%d", tempInt);
-            mDevice = tempInt;
-        } else if (sscanf(l, "period_size=%d", &tempInt) == 1) {
-            ALOGD("period_size=%d", tempInt);
-            mPeriodSize = tempInt;
-        } else if (sscanf(l, "period_count=%d", &tempInt) == 1) {
-            ALOGD("period_count=%d", tempInt);
-            mPeriodCount = tempInt;
-        } else if (sscanf(l, "mixer \"%[0-9a-zA-Z _]s\"", name) == 1) {
-            const char* values = strchr(l, '=');
-            if (values) {
-                values++;   // skip '='
-                ALOGD("name: \"%s\" = %s", name, values);
-                setMixerValue(mixer, name, values);
-            } else {
-                ALOGE("values missing for name: \"%s\"", name);
-            }
-        }
-        config = ++endl;
-    }
-
-    mixer_close(mixer);
-
-    if (mCard >= 0 && mDevice >= 0) {
-        return true;
-    }
-
-    return false;
-}
-
-void AudioPlayer::playFile(FileMap* fileMap) {
-    // stop any currently playing sound
-    requestExitAndWait();
-
-    mCurrentFile = fileMap;
-    run("bootanim audio", PRIORITY_URGENT_AUDIO);
-}
-
-bool AudioPlayer::threadLoop()
-{
-    struct pcm_config config;
-    struct pcm *pcm = NULL;
-    bool moreChunks = true;
-    const struct chunk_fmt* chunkFmt = NULL;
-    int bufferSize;
-    const uint8_t* wavData;
-    size_t wavLength;
-    const struct riff_wave_header* wavHeader;
-
-    if (mCurrentFile == NULL) {
-        ALOGE("mCurrentFile is NULL");
-        return false;
-     }
-
-    wavData = (const uint8_t *)mCurrentFile->getDataPtr();
-    if (!wavData) {
-        ALOGE("Could not access WAV file data");
-        goto exit;
-    }
-    wavLength = mCurrentFile->getDataLength();
-
-    wavHeader = (const struct riff_wave_header *)wavData;
-    if (wavLength < sizeof(*wavHeader) || (wavHeader->riff_id != ID_RIFF) ||
-        (wavHeader->wave_id != ID_WAVE)) {
-        ALOGE("Error: audio file is not a riff/wave file\n");
-        goto exit;
-    }
-    wavData += sizeof(*wavHeader);
-    wavLength -= sizeof(*wavHeader);
-
-    do {
-        const struct chunk_header* chunkHeader = (const struct chunk_header*)wavData;
-        if (wavLength < sizeof(*chunkHeader)) {
-            ALOGE("EOF reading chunk headers");
-            goto exit;
-        }
-
-        wavData += sizeof(*chunkHeader);
-        wavLength -=  sizeof(*chunkHeader);
-
-        switch (chunkHeader->id) {
-            case ID_FMT:
-                chunkFmt = (const struct chunk_fmt *)wavData;
-                wavData += chunkHeader->sz;
-                wavLength -= chunkHeader->sz;
-                break;
-            case ID_DATA:
-                /* Stop looking for chunks */
-                moreChunks = 0;
-                break;
-            default:
-                /* Unknown chunk, skip bytes */
-                wavData += chunkHeader->sz;
-                wavLength -= chunkHeader->sz;
-        }
-    } while (moreChunks);
-
-    if (!chunkFmt) {
-        ALOGE("format not found in WAV file");
-        goto exit;
-    }
-
-
-    memset(&config, 0, sizeof(config));
-    config.channels = chunkFmt->num_channels;
-    config.rate = chunkFmt->sample_rate;
-    config.period_size = mPeriodSize;
-    config.period_count = mPeriodCount;
-    config.start_threshold = mPeriodSize / 4;
-    config.stop_threshold = INT_MAX;
-    config.avail_min = config.start_threshold;
-    if (chunkFmt->bits_per_sample != 16) {
-        ALOGE("only 16 bit WAV files are supported");
-        goto exit;
-    }
-    config.format = PCM_FORMAT_S16_LE;
-
-    pcm = pcm_open(mCard, mDevice, PCM_OUT, &config);
-    if (!pcm || !pcm_is_ready(pcm)) {
-        ALOGE("Unable to open PCM device (%s)\n", pcm_get_error(pcm));
-        goto exit;
-    }
-
-    bufferSize = pcm_frames_to_bytes(pcm, pcm_get_buffer_size(pcm));
-
-    while (wavLength > 0) {
-        if (exitPending()) goto exit;
-        size_t count = bufferSize;
-        if (count > wavLength)
-            count = wavLength;
-
-        if (pcm_write(pcm, wavData, count)) {
-            ALOGE("pcm_write failed (%s)", pcm_get_error(pcm));
-            goto exit;
-        }
-        wavData += count;
-        wavLength -= count;
-    }
-
-exit:
-    if (pcm)
-        pcm_close(pcm);
-    delete mCurrentFile;
-    mCurrentFile = NULL;
-    return false;
-}
-
-} // namespace android
diff --git a/cmds/bootanimation/AudioPlayer.h b/cmds/bootanimation/AudioPlayer.h
deleted file mode 100644
index 1def0ae..0000000
--- a/cmds/bootanimation/AudioPlayer.h
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (C) 2014 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.
- */
-
-#ifndef _BOOTANIMATION_AUDIOPLAYER_H
-#define _BOOTANIMATION_AUDIOPLAYER_H
-
-#include <utils/Thread.h>
-#include <utils/FileMap.h>
-
-namespace android {
-
-class AudioPlayer : public Thread
-{
-public:
-                AudioPlayer();
-    virtual     ~AudioPlayer();
-    bool        init(const char* config);
-
-    void        playFile(FileMap* fileMap);
-
-private:
-    virtual bool        threadLoop();
-
-private:
-    int                 mCard;      // ALSA card to use
-    int                 mDevice;    // ALSA device to use
-    int                 mPeriodSize;
-    int                 mPeriodCount;
-
-    FileMap*            mCurrentFile;
-};
-
-} // namespace android
-
-#endif // _BOOTANIMATION_AUDIOPLAYER_H
diff --git a/cmds/bootanimation/BootAnimation.cpp b/cmds/bootanimation/BootAnimation.cpp
index 25b264e..c9211939 100644
--- a/cmds/bootanimation/BootAnimation.cpp
+++ b/cmds/bootanimation/BootAnimation.cpp
@@ -18,6 +18,9 @@
 #define LOG_TAG "BootAnimation"
 
 #include <stdint.h>
+#include <sys/inotify.h>
+#include <sys/poll.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <math.h>
 #include <fcntl.h>
@@ -55,25 +58,55 @@
 #include <EGL/eglext.h>
 
 #include "BootAnimation.h"
-#include "AudioPlayer.h"
-
-#define OEM_BOOTANIMATION_FILE "/oem/media/bootanimation.zip"
-#define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
-#define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
-#define EXIT_PROP_NAME "service.bootanim.exit"
+#include "audioplay.h"
 
 namespace android {
 
+static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
+static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
+static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
+static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
+static const char SYSTEM_TIME_DIR_NAME[] = "time";
+static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
+static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
+static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
+static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
+static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
+static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
+static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
+// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
+static const long long ACCURATE_TIME_EPOCH = 946684800000;
+static constexpr char FONT_BEGIN_CHAR = ' ';
+static constexpr char FONT_END_CHAR = '~' + 1;
+static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
+static constexpr size_t FONT_NUM_COLS = 16;
+static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
+static const int TEXT_CENTER_VALUE = INT_MAX;
+static const int TEXT_MISSING_VALUE = INT_MIN;
+static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
+static const char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
 static const int ANIM_ENTRY_NAME_MAX = 256;
+static constexpr size_t TEXT_POS_LEN_MAX = 16;
+static const char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
+static const char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
+// bootreasons list in "system/core/bootstat/bootstat.cpp".
+static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
+  "kernel_panic",
+  "Panic",
+  "Watchdog",
+};
 
 // ---------------------------------------------------------------------------
 
-BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true) {
+BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
+        mTimeCheckThread(NULL) {
     mSession = new SurfaceComposerClient();
+
+    // If the system has already booted, the animation is not being used for a boot.
+    mSystemBoot = !property_get_bool(BOOT_COMPLETED_PROP_NAME, 0);
 }
 
-BootAnimation::~BootAnimation() {
-}
+BootAnimation::~BootAnimation() {}
 
 void BootAnimation::onFirstRef() {
     status_t err = mSession->linkToComposerDeath(this);
@@ -97,9 +130,7 @@
     // might be blocked on a condition variable that will never be updated.
     kill( getpid(), SIGKILL );
     requestExit();
-    if (mAudioPlayer != NULL) {
-        mAudioPlayer->requestExit();
-    }
+    audioplay::destroy();
 }
 
 status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
@@ -154,15 +185,14 @@
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+
     return NO_ERROR;
 }
 
-status_t BootAnimation::initTexture(const Animation::Frame& frame)
+status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
 {
-    //StopWatch watch("blah");
-
     SkBitmap bitmap;
-    SkMemoryStream  stream(frame.map->getDataPtr(), frame.map->getDataLength());
+    SkMemoryStream  stream(map->getDataPtr(), map->getDataLength());
     SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
     if (codec != NULL) {
         codec->setDitherImage(false);
@@ -175,7 +205,7 @@
     // FileMap memory is never released until application exit.
     // Release it now as the texture is already loaded and the memory used for
     // the packed resource can be released.
-    delete frame.map;
+    delete map;
 
     // ensure we can call getPixels(). No need to call unlock, since the
     // bitmap will go out of scope when we return from this method.
@@ -193,25 +223,25 @@
 
     switch (bitmap.colorType()) {
         case kN32_SkColorType:
-            if (tw != w || th != h) {
+            if (!mUseNpotTextures && (tw != w || th != h)) {
                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
                         GL_UNSIGNED_BYTE, 0);
                 glTexSubImage2D(GL_TEXTURE_2D, 0,
                         0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
             } else {
-                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
+                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
                         GL_UNSIGNED_BYTE, p);
             }
             break;
 
         case kRGB_565_SkColorType:
-            if (tw != w || th != h) {
+            if (!mUseNpotTextures && (tw != w || th != h)) {
                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
                         GL_UNSIGNED_SHORT_5_6_5, 0);
                 glTexSubImage2D(GL_TEXTURE_2D, 0,
                         0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
             } else {
-                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
+                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
                         GL_UNSIGNED_SHORT_5_6_5, p);
             }
             break;
@@ -221,6 +251,9 @@
 
     glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
 
+    *width = w;
+    *height = h;
+
     return NO_ERROR;
 }
 
@@ -384,7 +417,6 @@
     return false;
 }
 
-
 void BootAnimation::checkExit() {
     // Allow surface flinger to gracefully request shutdown
     char value[PROPERTY_VALUE_MAX];
@@ -392,9 +424,47 @@
     int exitnow = atoi(value);
     if (exitnow) {
         requestExit();
-        if (mAudioPlayer != NULL) {
-            mAudioPlayer->requestExit();
+    }
+}
+
+bool BootAnimation::validClock(const Animation::Part& part) {
+    return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
+}
+
+bool parseTextCoord(const char* str, int* dest) {
+    if (strcmp("c", str) == 0) {
+        *dest = TEXT_CENTER_VALUE;
+        return true;
+    }
+
+    char* end;
+    int val = (int) strtol(str, &end, 0);
+    if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
+        return false;
+    }
+    *dest = val;
+    return true;
+}
+
+// Parse two position coordinates. If only string is non-empty, treat it as the y value.
+void parsePosition(const char* str1, const char* str2, int* x, int* y) {
+    bool success = false;
+    if (strlen(str1) == 0) {  // No values were specified
+        // success = false
+    } else if (strlen(str2) == 0) {  // we have only one value
+        if (parseTextCoord(str1, y)) {
+            *x = TEXT_CENTER_VALUE;
+            success = true;
         }
+    } else {
+        if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
+            success = true;
+        }
+    }
+
+    if (!success) {
+        *x = TEXT_MISSING_VALUE;
+        *y = TEXT_MISSING_VALUE;
     }
 }
 
@@ -444,24 +514,87 @@
     return true;
 }
 
-// The time glyphs are stored in a single image of height 64 pixels. Each digit is 40 pixels wide,
-// and the colon character is half that at 20 pixels. The glyph order is '0123456789:'.
-// We render 24 hour time.
-void BootAnimation::drawTime(const Texture& clockTex, const int yPos) {
-    static constexpr char TIME_FORMAT[] = "%H:%M";
-    static constexpr int TIME_LENGTH = sizeof(TIME_FORMAT);
+// The font image should be a 96x2 array of character images.  The
+// columns are the printable ASCII characters 0x20 - 0x7f.  The
+// top row is regular text; the bottom row is bold.
+status_t BootAnimation::initFont(Font* font, const char* fallback) {
+    status_t status = NO_ERROR;
 
-    static constexpr int DIGIT_HEIGHT = 64;
-    static constexpr int DIGIT_WIDTH = 40;
-    static constexpr int COLON_WIDTH = DIGIT_WIDTH / 2;
-    static constexpr int TIME_WIDTH = (DIGIT_WIDTH * 4) + COLON_WIDTH;
+    if (font->map != nullptr) {
+        glGenTextures(1, &font->texture.name);
+        glBindTexture(GL_TEXTURE_2D, font->texture.name);
 
-    if (clockTex.h < DIGIT_HEIGHT || clockTex.w < (10 * DIGIT_WIDTH + COLON_WIDTH)) {
-        ALOGE("Clock texture is too small; abandoning boot animation clock");
-        mClockEnabled = false;
-        return;
+        status = initTexture(font->map, &font->texture.w, &font->texture.h);
+
+        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+    } else if (fallback != nullptr) {
+        status = initTexture(&font->texture, mAssets, fallback);
+    } else {
+        return NO_INIT;
     }
 
+    if (status == NO_ERROR) {
+        font->char_width = font->texture.w / FONT_NUM_COLS;
+        font->char_height = font->texture.h / FONT_NUM_ROWS / 2;  // There are bold and regular rows
+    }
+
+    return status;
+}
+
+void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
+    glEnable(GL_BLEND);  // Allow us to draw on top of the animation
+    glBindTexture(GL_TEXTURE_2D, font.texture.name);
+
+    const int len = strlen(str);
+    const int strWidth = font.char_width * len;
+
+    if (*x == TEXT_CENTER_VALUE) {
+        *x = (mWidth - strWidth) / 2;
+    } else if (*x < 0) {
+        *x = mWidth + *x - strWidth;
+    }
+    if (*y == TEXT_CENTER_VALUE) {
+        *y = (mHeight - font.char_height) / 2;
+    } else if (*y < 0) {
+        *y = mHeight + *y - font.char_height;
+    }
+
+    int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
+
+    for (int i = 0; i < len; i++) {
+        char c = str[i];
+
+        if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
+            c = '?';
+        }
+
+        // Crop the texture to only the pixels in the current glyph
+        const int charPos = (c - FONT_BEGIN_CHAR);  // Position in the list of valid characters
+        const int row = charPos / FONT_NUM_COLS;
+        const int col = charPos % FONT_NUM_COLS;
+        cropRect[0] = col * font.char_width;  // Left of column
+        cropRect[1] = row * font.char_height * 2; // Top of row
+        // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
+        cropRect[1] += bold ? 2 * font.char_height : font.char_height;
+        glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
+
+        glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
+
+        *x += font.char_width;
+    }
+
+    glDisable(GL_BLEND);  // Return to the animation's default behaviour
+    glBindTexture(GL_TEXTURE_2D, 0);
+}
+
+// We render 24 hour time.
+void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
+    static constexpr char TIME_FORMAT[] = "%H:%M";
+    static constexpr int TIME_LENGTH = 6;
+
     time_t rawtime;
     time(&rawtime);
     struct tm* timeInfo = localtime(&rawtime);
@@ -475,36 +608,9 @@
         return;
     }
 
-    glEnable(GL_BLEND);  // Allow us to draw on top of the animation
-    glBindTexture(GL_TEXTURE_2D, clockTex.name);
-
-    int xPos = (mWidth - TIME_WIDTH) / 2;
-    int cropRect[4] = { 0, DIGIT_HEIGHT, DIGIT_WIDTH, -DIGIT_HEIGHT };
-
-    for (int i = 0; i < TIME_LENGTH - 1; i++) {
-        char c = timeBuff[i];
-        int width = DIGIT_WIDTH;
-        int pos = c - '0';  // Position in the character list
-        if (pos < 0 || pos > 10) {
-            continue;
-        }
-        if (c == ':') {
-            width = COLON_WIDTH;
-        }
-
-        // Crop the texture to only the pixels in the current glyph
-        int left = pos * DIGIT_WIDTH;
-        cropRect[0] = left;
-        cropRect[2] = width;
-        glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
-
-        glDrawTexiOES(xPos, yPos, 0, width, DIGIT_HEIGHT);
-
-        xPos += width;
-    }
-
-    glDisable(GL_BLEND);  // Return to the animation's default behaviour
-    glBindTexture(GL_TEXTURE_2D, 0);
+    int x = xPos;
+    int y = yPos;
+    drawText(timeBuff, font, false, &x, &y);
 }
 
 bool BootAnimation::parseAnimationDesc(Animation& animation)
@@ -516,16 +622,6 @@
     }
     char const* s = desString.string();
 
-    // Create and initialize an AudioPlayer if we have an audio_conf.txt file
-    String8 audioConf;
-    if (readFile(animation.zip, "audio_conf.txt", audioConf)) {
-        mAudioPlayer = new AudioPlayer;
-        if (!mAudioPlayer->init(audioConf.string())) {
-            ALOGE("mAudioPlayer.init failed");
-            mAudioPlayer = NULL;
-        }
-    }
-
     // Parse the description file
     for (;;) {
         const char* endl = strstr(s, "\n");
@@ -537,9 +633,10 @@
         int height = 0;
         int count = 0;
         int pause = 0;
-        int clockPosY = -1;
         char path[ANIM_ENTRY_NAME_MAX];
         char color[7] = "000000"; // default to black if unspecified
+        char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
+        char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
 
         char pathType;
         if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
@@ -547,16 +644,16 @@
             animation.width = width;
             animation.height = height;
             animation.fps = fps;
-        } else if (sscanf(l, " %c %d %d %s #%6s %d",
-                          &pathType, &count, &pause, path, color, &clockPosY) >= 4) {
-            // ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPosY=%d", pathType, count, pause, path, color, clockPosY);
+        } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
+                          &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
+            //ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
+            //    pathType, count, pause, path, color, clockPos1, clockPos2);
             Animation::Part part;
             part.playUntilComplete = pathType == 'c';
             part.count = count;
             part.pause = pause;
             part.path = path;
-            part.clockPosY = clockPosY;
-            part.audioFile = NULL;
+            part.audioData = NULL;
             part.animation = NULL;
             if (!parseColor(color, part.backgroundColor)) {
                 ALOGE("> invalid color '#%s'", color);
@@ -564,6 +661,7 @@
                 part.backgroundColor[1] = 0.0f;
                 part.backgroundColor[2] = 0.0f;
             }
+            parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
             animation.parts.add(part);
         }
         else if (strcmp(l, "$SYSTEM") == 0) {
@@ -572,7 +670,7 @@
             part.playUntilComplete = false;
             part.count = 1;
             part.pause = 0;
-            part.audioFile = NULL;
+            part.audioData = NULL;
             part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
             if (part.animation != NULL)
                 animation.parts.add(part);
@@ -588,15 +686,16 @@
     // read all the data structures
     const size_t pcount = animation.parts.size();
     void *cookie = NULL;
-    ZipFileRO* mZip = animation.zip;
-    if (!mZip->startIteration(&cookie)) {
+    ZipFileRO* zip = animation.zip;
+    if (!zip->startIteration(&cookie)) {
         return false;
     }
 
+    Animation::Part* partWithAudio = NULL;
     ZipEntryRO entry;
     char name[ANIM_ENTRY_NAME_MAX];
-    while ((entry = mZip->nextEntry(cookie)) != NULL) {
-        const int foundEntryName = mZip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
+    while ((entry = zip->nextEntry(cookie)) != NULL) {
+        const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
         if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
             ALOGE("Error fetching entry file name");
             continue;
@@ -606,25 +705,44 @@
         const String8 path(entryName.getPathDir());
         const String8 leaf(entryName.getPathLeaf());
         if (leaf.size() > 0) {
-            for (size_t j=0 ; j<pcount ; j++) {
+            if (entryName == CLOCK_FONT_ZIP_NAME) {
+                FileMap* map = zip->createEntryFileMap(entry);
+                if (map) {
+                    animation.clockFont.map = map;
+                }
+                continue;
+            }
+
+            for (size_t j = 0; j < pcount; j++) {
                 if (path == animation.parts[j].path) {
                     uint16_t method;
                     // supports only stored png files
-                    if (mZip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
+                    if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
                         if (method == ZipFileRO::kCompressStored) {
-                            FileMap* map = mZip->createEntryFileMap(entry);
+                            FileMap* map = zip->createEntryFileMap(entry);
                             if (map) {
                                 Animation::Part& part(animation.parts.editItemAt(j));
                                 if (leaf == "audio.wav") {
                                     // a part may have at most one audio file
-                                    part.audioFile = map;
+                                    part.audioData = (uint8_t *)map->getDataPtr();
+                                    part.audioLength = map->getDataLength();
+                                    partWithAudio = &part;
+                                } else if (leaf == "trim.txt") {
+                                    part.trimData.setTo((char const*)map->getDataPtr(),
+                                                        map->getDataLength());
                                 } else {
                                     Animation::Frame frame;
                                     frame.name = leaf;
                                     frame.map = map;
+                                    frame.trimWidth = animation.width;
+                                    frame.trimHeight = animation.height;
+                                    frame.trimX = 0;
+                                    frame.trimY = 0;
                                     part.frames.add(frame);
                                 }
                             }
+                        } else {
+                            ALOGE("bootanimation.zip is compressed; must be only stored");
                         }
                     }
                 }
@@ -632,18 +750,76 @@
         }
     }
 
-    mZip->endIteration(cookie);
+    // If there is trimData present, override the positioning defaults.
+    for (Animation::Part& part : animation.parts) {
+        const char* trimDataStr = part.trimData.string();
+        for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
+            const char* endl = strstr(trimDataStr, "\n");
+            // No more trimData for this part.
+            if (endl == NULL) {
+                break;
+            }
+            String8 line(trimDataStr, endl - trimDataStr);
+            const char* lineStr = line.string();
+            trimDataStr = ++endl;
+            int width = 0, height = 0, x = 0, y = 0;
+            if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
+                Animation::Frame& frame(part.frames.editItemAt(frameIdx));
+                frame.trimWidth = width;
+                frame.trimHeight = height;
+                frame.trimX = x;
+                frame.trimY = y;
+            } else {
+                ALOGE("Error parsing trim.txt, line: %s", lineStr);
+                break;
+            }
+        }
+    }
+
+    // Create and initialize audioplay if there is a wav file in any of the animations.
+    if (partWithAudio != NULL) {
+        ALOGD("found audio.wav, creating playback engine");
+        if (!audioplay::create(partWithAudio->audioData, partWithAudio->audioLength)) {
+            return false;
+        }
+    }
+
+    zip->endIteration(cookie);
 
     return true;
 }
 
 bool BootAnimation::movie()
 {
-
     Animation* animation = loadAnimation(mZipFileName);
     if (animation == NULL)
         return false;
 
+    bool anyPartHasClock = false;
+    for (size_t i=0; i < animation->parts.size(); i++) {
+        if(validClock(animation->parts[i])) {
+            anyPartHasClock = true;
+            break;
+        }
+    }
+    if (!anyPartHasClock) {
+        mClockEnabled = false;
+    }
+
+    // Check if npot textures are supported
+    mUseNpotTextures = false;
+    String8 gl_extensions;
+    const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
+    if (!exts) {
+        glGetError();
+    } else {
+        gl_extensions.setTo(exts);
+        if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
+            (gl_extensions.find("GL_OES_texture_npot") != -1)) {
+            mUseNpotTextures = true;
+        }
+    }
+
     // Blend required to draw time on top of animation frames.
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     glShadeModel(GL_FLAT);
@@ -659,17 +835,29 @@
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
-    bool clockTextureInitialized = false;
+    bool clockFontInitialized = false;
     if (mClockEnabled) {
-        clockTextureInitialized = (initTexture(&mClock, mAssets, "images/clock64.png") == NO_ERROR);
-        mClockEnabled = clockTextureInitialized;
+        clockFontInitialized =
+            (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
+        mClockEnabled = clockFontInitialized;
+    }
+
+    if (mClockEnabled && !updateIsTimeAccurate()) {
+        mTimeCheckThread = new TimeCheckThread(this);
+        mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
     }
 
     playAnimation(*animation);
+
+    if (mTimeCheckThread != NULL) {
+        mTimeCheckThread->requestExit();
+        mTimeCheckThread = NULL;
+    }
+
     releaseAnimation(animation);
 
-    if (clockTextureInitialized) {
-        glDeleteTextures(1, &mClock.name);
+    if (clockFontInitialized) {
+        glDeleteTextures(1, &animation->clockFont.texture.name);
     }
 
     return false;
@@ -678,12 +866,9 @@
 bool BootAnimation::playAnimation(const Animation& animation)
 {
     const size_t pcount = animation.parts.size();
-    const int xc = (mWidth - animation.width) / 2;
-    const int yc = ((mHeight - animation.height) / 2);
     nsecs_t frameDuration = s2ns(1) / animation.fps;
-
-    Region clearReg(Rect(mWidth, mHeight));
-    clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
+    const int animationX = (mWidth - animation.width) / 2;
+    const int animationY = (mHeight - animation.height) / 2;
 
     for (size_t i=0 ; i<pcount ; i++) {
         const Animation::Part& part(animation.parts[i]);
@@ -704,8 +889,9 @@
                 break;
 
             // only play audio file the first time we animate the part
-            if (r == 0 && mAudioPlayer != NULL && part.audioFile) {
-                mAudioPlayer->playFile(part.audioFile);
+            if (r == 0 && part.audioData && playSoundsAllowed()) {
+                ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
+                audioplay::playClip(part.audioData, part.audioLength);
             }
 
             glClearColor(
@@ -727,27 +913,31 @@
                         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                     }
-                    initTexture(frame);
+                    int w, h;
+                    initTexture(frame.map, &w, &h);
                 }
 
+                const int xc = animationX + frame.trimX;
+                const int yc = animationY + frame.trimY;
+                Region clearReg(Rect(mWidth, mHeight));
+                clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
                 if (!clearReg.isEmpty()) {
                     Region::const_iterator head(clearReg.begin());
                     Region::const_iterator tail(clearReg.end());
                     glEnable(GL_SCISSOR_TEST);
                     while (head != tail) {
                         const Rect& r2(*head++);
-                        glScissor(r2.left, mHeight - r2.bottom,
-                                r2.width(), r2.height());
+                        glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
                         glClear(GL_COLOR_BUFFER_BIT);
                     }
                     glDisable(GL_SCISSOR_TEST);
                 }
-                // specify the y center as ceiling((mHeight - animation.height) / 2)
-                // which is equivalent to mHeight - (yc + animation.height)
-                glDrawTexiOES(xc, mHeight - (yc + animation.height),
-                              0, animation.width, animation.height);
-                if (mClockEnabled && part.clockPosY >= 0) {
-                    drawTime(mClock, part.clockPosY);
+                // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
+                // which is equivalent to mHeight - (yc + frame.trimHeight)
+                glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
+                              0, frame.trimWidth, frame.trimHeight);
+                if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
+                    drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
                 }
 
                 eglSwapBuffers(mDisplay, mSurface);
@@ -777,14 +967,23 @@
                 break;
         }
 
-        // free the textures for this part
+    }
+
+    // Free textures created for looping parts now that the animation is done.
+    for (const Animation::Part& part : animation.parts) {
         if (part.count != 1) {
-            for (size_t j=0 ; j<fcount ; j++) {
+            const size_t fcount = part.frames.size();
+            for (size_t j = 0; j < fcount; j++) {
                 const Animation::Frame& frame(part.frames[j]);
                 glDeleteTextures(1, &frame.tid);
             }
         }
     }
+
+    // we've finally played everything we're going to play
+    audioplay::setPlaying(false);
+    audioplay::destroy();
+
     return true;
 }
 
@@ -817,14 +1016,169 @@
     Animation *animation =  new Animation;
     animation->fileName = fn;
     animation->zip = zip;
+    animation->clockFont.map = nullptr;
     mLoadedFiles.add(animation->fileName);
 
     parseAnimationDesc(*animation);
-    preloadZip(*animation);
+    if (!preloadZip(*animation)) {
+        return NULL;
+    }
+
 
     mLoadedFiles.remove(fn);
     return animation;
 }
+
+bool BootAnimation::playSoundsAllowed() const {
+    // Only play sounds for system boots, not runtime restarts.
+    if (!mSystemBoot) {
+        return false;
+    }
+
+    // Read the system property to see if we should play the sound.
+    // If it's not present, default to allowed.
+    if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
+        return false;
+    }
+
+    // Don't play sounds if this is a reboot due to an error.
+    char bootreason[PROPERTY_VALUE_MAX];
+    if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
+        for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
+            if (strcasecmp(str.c_str(), bootreason) == 0) {
+                return false;
+            }
+        }
+    }
+    return true;
+}
+
+bool BootAnimation::updateIsTimeAccurate() {
+    static constexpr long long MAX_TIME_IN_PAST =   60000LL * 60LL * 24LL * 30LL;  // 30 days
+    static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL;  // 90 minutes
+
+    if (mTimeIsAccurate) {
+        return true;
+    }
+
+    struct stat statResult;
+    if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
+        mTimeIsAccurate = true;
+        return true;
+    }
+
+    FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
+    if (file != NULL) {
+      long long lastChangedTime = 0;
+      fscanf(file, "%lld", &lastChangedTime);
+      fclose(file);
+      if (lastChangedTime > 0) {
+        struct timespec now;
+        clock_gettime(CLOCK_REALTIME, &now);
+        // Match the Java timestamp format
+        long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
+        if (ACCURATE_TIME_EPOCH < rtcNow
+            && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
+            && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
+            mTimeIsAccurate = true;
+        }
+      }
+    }
+
+    return mTimeIsAccurate;
+}
+
+BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
+    mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
+
+BootAnimation::TimeCheckThread::~TimeCheckThread() {
+    // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
+    close(mInotifyFd);
+}
+
+bool BootAnimation::TimeCheckThread::threadLoop() {
+    bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
+        && mBootAnimation->mClockEnabled;
+    if (!shouldLoop) {
+        close(mInotifyFd);
+        mInotifyFd = -1;
+    }
+    return shouldLoop;
+}
+
+bool BootAnimation::TimeCheckThread::doThreadLoop() {
+    static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
+
+    // Poll instead of doing a blocking read so the Thread can exit if requested.
+    struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
+    ssize_t pollResult = poll(&pfd, 1, 1000);
+
+    if (pollResult == 0) {
+        return true;
+    } else if (pollResult < 0) {
+        ALOGE("Could not poll inotify events");
+        return false;
+    }
+
+    char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
+    ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
+    if (length == 0) {
+        return true;
+    } else if (length < 0) {
+        ALOGE("Could not read inotify events");
+        return false;
+    }
+
+    const struct inotify_event *event;
+    for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
+        event = (const struct inotify_event *) ptr;
+        if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
+            addTimeDirWatch();
+        } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
+                || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
+            return !mBootAnimation->updateIsTimeAccurate();
+        }
+    }
+
+    return true;
+}
+
+void BootAnimation::TimeCheckThread::addTimeDirWatch() {
+        mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
+                IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
+        if (mTimeWd > 0) {
+            // No need to watch for the time directory to be created if it already exists
+            inotify_rm_watch(mInotifyFd, mSystemWd);
+            mSystemWd = -1;
+        }
+}
+
+status_t BootAnimation::TimeCheckThread::readyToRun() {
+    mInotifyFd = inotify_init();
+    if (mInotifyFd < 0) {
+        ALOGE("Could not initialize inotify fd");
+        return NO_INIT;
+    }
+
+    mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
+    if (mSystemWd < 0) {
+        close(mInotifyFd);
+        mInotifyFd = -1;
+        ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
+        return NO_INIT;
+    }
+
+    addTimeDirWatch();
+
+    if (mBootAnimation->updateIsTimeAccurate()) {
+        close(mInotifyFd);
+        mInotifyFd = -1;
+        return ALREADY_EXISTS;
+    }
+
+    return NO_ERROR;
+}
+
 // ---------------------------------------------------------------------------
 
 }
diff --git a/cmds/bootanimation/BootAnimation.h b/cmds/bootanimation/BootAnimation.h
index d49e1ec..42759f1 100644
--- a/cmds/bootanimation/BootAnimation.h
+++ b/cmds/bootanimation/BootAnimation.h
@@ -30,7 +30,6 @@
 
 namespace android {
 
-class AudioPlayer;
 class Surface;
 class SurfaceComposerClient;
 class SurfaceControl;
@@ -51,16 +50,45 @@
     virtual void        onFirstRef();
     virtual void        binderDied(const wp<IBinder>& who);
 
+    bool                updateIsTimeAccurate();
+
+    class TimeCheckThread : public Thread {
+    public:
+        TimeCheckThread(BootAnimation* bootAnimation);
+        virtual ~TimeCheckThread();
+    private:
+        virtual status_t    readyToRun();
+        virtual bool        threadLoop();
+        bool                doThreadLoop();
+        void                addTimeDirWatch();
+
+        int mInotifyFd;
+        int mSystemWd;
+        int mTimeWd;
+        BootAnimation* mBootAnimation;
+    };
+
     struct Texture {
         GLint   w;
         GLint   h;
         GLuint  name;
     };
 
+    struct Font {
+        FileMap* map;
+        Texture texture;
+        int char_width;
+        int char_height;
+    };
+
     struct Animation {
         struct Frame {
             String8 name;
             FileMap* map;
+            int trimX;
+            int trimY;
+            int trimWidth;
+            int trimHeight;
             mutable GLuint tid;
             bool operator < (const Frame& rhs) const {
                 return name < rhs.name;
@@ -69,13 +97,19 @@
         struct Part {
             int count;  // The number of times this part should repeat, 0 for infinite
             int pause;  // The number of frames to pause for at the end of this part
-            int clockPosY;  // The y position of the clock, in pixels, from the bottom of the
-                            // display (the clock is centred horizontally). -1 to disable the clock
+            int clockPosX;  // The x position of the clock, in pixels. Positive values offset from
+                            // the left of the screen, negative values offset from the right.
+            int clockPosY;  // The y position of the clock, in pixels. Positive values offset from
+                            // the bottom of the screen, negative values offset from the top.
+                            // If either of the above are INT_MIN the clock is disabled, if INT_MAX
+                            // the clock is centred on that axis.
             String8 path;
+            String8 trimData;
             SortedVector<Frame> frames;
             bool playUntilComplete;
             float backgroundColor[3];
-            FileMap* audioFile;
+            uint8_t* audioData;
+            int audioLength;
             Animation* animation;
         };
         int fps;
@@ -85,36 +119,43 @@
         String8 audioConf;
         String8 fileName;
         ZipFileRO* zip;
+        Font clockFont;
     };
 
     status_t initTexture(Texture* texture, AssetManager& asset, const char* name);
-    status_t initTexture(const Animation::Frame& frame);
+    status_t initTexture(FileMap* map, int* width, int* height);
+    status_t initFont(Font* font, const char* fallback);
     bool android();
     bool movie();
-    void drawTime(const Texture& clockTex, const int yPos);
+    void drawText(const char* str, const Font& font, bool bold, int* x, int* y);
+    void drawClock(const Font& font, const int xPos, const int yPos);
+    bool validClock(const Animation::Part& part);
     Animation* loadAnimation(const String8&);
     bool playAnimation(const Animation&);
     void releaseAnimation(Animation*) const;
     bool parseAnimationDesc(Animation&);
     bool preloadZip(Animation &animation);
+    bool playSoundsAllowed() const;
 
     void checkExit();
 
     sp<SurfaceComposerClient>       mSession;
-    sp<AudioPlayer>                 mAudioPlayer;
     AssetManager mAssets;
     Texture     mAndroid[2];
-    Texture     mClock;
     int         mWidth;
     int         mHeight;
+    bool        mUseNpotTextures = false;
     EGLDisplay  mDisplay;
     EGLDisplay  mContext;
     EGLDisplay  mSurface;
     sp<SurfaceControl> mFlingerSurfaceControl;
     sp<Surface> mFlingerSurface;
     bool        mClockEnabled;
+    bool        mTimeIsAccurate;
+    bool        mSystemBoot;
     String8     mZipFileName;
     SortedVector<String8> mLoadedFiles;
+    sp<TimeCheckThread> mTimeCheckThread;
 };
 
 // ---------------------------------------------------------------------------
diff --git a/cmds/bootanimation/FORMAT.md b/cmds/bootanimation/FORMAT.md
new file mode 100644
index 0000000..9ea6fea
--- /dev/null
+++ b/cmds/bootanimation/FORMAT.md
@@ -0,0 +1,101 @@
+# bootanimation format
+
+## zipfile paths
+
+The system selects a boot animation zipfile from the following locations, in order:
+
+    /system/media/bootanimation-encrypted.zip (if getprop("vold.decrypt") = '1')
+    /system/media/bootanimation.zip
+    /oem/media/bootanimation.zip
+
+## zipfile layout
+
+The `bootanimation.zip` archive file includes:
+
+    desc.txt - a text file
+    part0  \
+    part1   \  directories full of PNG frames
+    ...     /
+    partN  /
+
+## desc.txt format
+
+The first line defines the general parameters of the animation:
+
+    WIDTH HEIGHT FPS
+
+  * **WIDTH:** animation width (pixels)
+  * **HEIGHT:** animation height (pixels)
+  * **FPS:** frames per second, e.g. 60
+
+It is followed by a number of rows of the form:
+
+    TYPE COUNT PAUSE PATH [#RGBHEX CLOCK]
+
+  * **TYPE:** a single char indicating what type of animation segment this is:
+      + `p` -- this part will play unless interrupted by the end of the boot
+      + `c` -- this part will play to completion, no matter what
+  * **COUNT:** how many times to play the animation, or 0 to loop forever until boot is complete
+  * **PAUSE:** number of FRAMES to delay after this part ends
+  * **PATH:** directory in which to find the frames for this part (e.g. `part0`)
+  * **RGBHEX:** _(OPTIONAL)_ a background color, specified as `#RRGGBB`
+  * **CLOCK:** _(OPTIONAL)_ the y-coordinate at which to draw the current time (for watches)
+
+There is also a special TYPE, `$SYSTEM`, that loads `/system/media/bootanimation.zip`
+and plays that.
+
+## loading and playing frames
+
+Each part is scanned and loaded directly from the zip archive. Within a part directory, every file
+(except `trim.txt` and `audio.wav`; see next sections) is expected to be a PNG file that represents
+one frame in that part (at the specified resolution). For this reason it is important that frames be
+named sequentially (e.g. `part000.png`, `part001.png`, ...) and added to the zip archive in that
+order.
+
+## trim.txt
+
+To save on memory, textures may be trimmed by their background color.  trim.txt sequentially lists
+the trim output for each frame in its directory, so the frames may be properly positioned.
+Output should be of the form: `WxH+X+Y`. Example:
+
+    713x165+388+914
+    708x152+388+912
+    707x139+388+911
+    649x92+388+910
+
+If the file is not present, each frame is assumed to be the same size as the animation.
+
+## audio.wav
+
+Each part may optionally play a `wav` sample when it starts. To enable this, add a file
+with the name `audio.wav` in the part directory.
+
+## exiting
+
+The system will end the boot animation (first completing any incomplete or even entirely unplayed
+parts that are of type `c`) when the system is finished booting. (This is accomplished by setting
+the system property `service.bootanim.exit` to a nonzero string.)
+
+## protips
+
+### PNG compression
+
+Use `zopflipng` if you have it, otherwise `pngcrush` will do. e.g.:
+
+    for fn in *.png ; do
+        zopflipng -m ${fn}s ${fn}s.new && mv -f ${fn}s.new ${fn}
+        # or: pngcrush -q ....
+    done
+
+Some animations benefit from being reduced to 256 colors:
+
+    pngquant --force --ext .png *.png
+    # alternatively: mogrify -colors 256 anim-tmp/*/*.png
+
+### creating the ZIP archive
+
+    cd <path-to-pieces>
+    zip -0qry -i \*.txt \*.png \*.wav @ ../bootanimation.zip *.txt part*
+
+Note that the ZIP archive is not actually compressed! The PNG files are already as compressed
+as they can reasonably get, and there is unlikely to be any redundancy between files.
diff --git a/cmds/bootanimation/audioplay.cpp b/cmds/bootanimation/audioplay.cpp
new file mode 100644
index 0000000..c546072
--- /dev/null
+++ b/cmds/bootanimation/audioplay.cpp
@@ -0,0 +1,400 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+// cribbed from samples/native-audio
+
+#include "audioplay.h"
+
+#define CHATTY ALOGD
+#define LOG_TAG "audioplay"
+
+#include <string.h>
+
+#include <utils/Log.h>
+
+// for native audio
+#include <SLES/OpenSLES.h>
+#include <SLES/OpenSLES_Android.h>
+
+namespace audioplay {
+namespace {
+
+// engine interfaces
+static SLObjectItf engineObject = NULL;
+static SLEngineItf engineEngine;
+
+// output mix interfaces
+static SLObjectItf outputMixObject = NULL;
+
+// buffer queue player interfaces
+static SLObjectItf bqPlayerObject = NULL;
+static SLPlayItf bqPlayerPlay;
+static SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
+static SLMuteSoloItf bqPlayerMuteSolo;
+static SLVolumeItf bqPlayerVolume;
+
+// pointer and size of the next player buffer to enqueue, and number of remaining buffers
+static const uint8_t* nextBuffer;
+static unsigned nextSize;
+
+static const uint32_t ID_RIFF = 0x46464952;
+static const uint32_t ID_WAVE = 0x45564157;
+static const uint32_t ID_FMT  = 0x20746d66;
+static const uint32_t ID_DATA = 0x61746164;
+
+struct RiffWaveHeader {
+    uint32_t riff_id;
+    uint32_t riff_sz;
+    uint32_t wave_id;
+};
+
+struct ChunkHeader {
+    uint32_t id;
+    uint32_t sz;
+};
+
+struct ChunkFormat {
+    uint16_t audio_format;
+    uint16_t num_channels;
+    uint32_t sample_rate;
+    uint32_t byte_rate;
+    uint16_t block_align;
+    uint16_t bits_per_sample;
+};
+
+// this callback handler is called every time a buffer finishes playing
+void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context) {
+    (void)bq;
+    (void)context;
+    audioplay::setPlaying(false);
+}
+
+bool hasPlayer() {
+    return (engineObject != NULL && bqPlayerObject != NULL);
+}
+
+// create the engine and output mix objects
+bool createEngine() {
+    SLresult result;
+
+    // create engine
+    result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("slCreateEngine failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // realize the engine
+    result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl engine Realize failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // get the engine interface, which is needed in order to create other objects
+    result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl engine GetInterface failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // create output mix
+    result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 0, NULL, NULL);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl engine CreateOutputMix failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // realize the output mix
+    result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl outputMix Realize failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    return true;
+}
+
+// create buffer queue audio player
+bool createBufferQueueAudioPlayer(const ChunkFormat* chunkFormat) {
+    SLresult result;
+
+    // configure audio source
+    SLDataLocator_AndroidSimpleBufferQueue loc_bufq = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 1};
+
+    // Determine channelMask from num_channels
+    SLuint32 channelMask;
+    switch (chunkFormat->num_channels) {
+        case 1:
+            channelMask = SL_SPEAKER_FRONT_CENTER;
+            break;
+        case 2:
+            channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
+            break;
+        default:
+            // Default of 0 will derive mask from num_channels and log a warning.
+            channelMask = 0;
+    }
+
+    SLDataFormat_PCM format_pcm = {
+        SL_DATAFORMAT_PCM,
+        chunkFormat->num_channels,
+        chunkFormat->sample_rate * 1000,  // convert to milliHz
+        chunkFormat->bits_per_sample,
+        16,
+        channelMask,
+        SL_BYTEORDER_LITTLEENDIAN
+    };
+    SLDataSource audioSrc = {&loc_bufq, &format_pcm};
+
+    // configure audio sink
+    SLDataLocator_OutputMix loc_outmix = {SL_DATALOCATOR_OUTPUTMIX, outputMixObject};
+    SLDataSink audioSnk = {&loc_outmix, NULL};
+
+    // create audio player
+    const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE, SL_IID_VOLUME, SL_IID_ANDROIDCONFIGURATION};
+    const SLboolean req[3] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
+    result = (*engineEngine)->CreateAudioPlayer(engineEngine, &bqPlayerObject, &audioSrc, &audioSnk,
+            3, ids, req);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl CreateAudioPlayer failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // Use the System stream for boot sound playback.
+    SLAndroidConfigurationItf playerConfig;
+    result = (*bqPlayerObject)->GetInterface(bqPlayerObject,
+        SL_IID_ANDROIDCONFIGURATION, &playerConfig);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("config GetInterface failed with result %d", result);
+        return false;
+    }
+    SLint32 streamType = SL_ANDROID_STREAM_SYSTEM;
+    result = (*playerConfig)->SetConfiguration(playerConfig,
+        SL_ANDROID_KEY_STREAM_TYPE, &streamType, sizeof(SLint32));
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("SetConfiguration failed with result %d", result);
+        return false;
+    }
+    // use normal performance mode as low latency is not needed. This is not mandatory so
+    // do not bail if we fail
+    SLuint32 performanceMode = SL_ANDROID_PERFORMANCE_NONE;
+    result = (*playerConfig)->SetConfiguration(
+           playerConfig, SL_ANDROID_KEY_PERFORMANCE_MODE, &performanceMode, sizeof(SLuint32));
+    ALOGW_IF(result != SL_RESULT_SUCCESS,
+            "could not set performance mode on player, error %d", result);
+    (void)result;
+
+    // realize the player
+    result = (*bqPlayerObject)->Realize(bqPlayerObject, SL_BOOLEAN_FALSE);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl player Realize failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // get the play interface
+    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_PLAY, &bqPlayerPlay);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl player GetInterface failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // get the buffer queue interface
+    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_BUFFERQUEUE,
+            &bqPlayerBufferQueue);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl playberBufferQueue GetInterface failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // register callback on the buffer queue
+    result = (*bqPlayerBufferQueue)->RegisterCallback(bqPlayerBufferQueue, bqPlayerCallback, NULL);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl bqPlayerBufferQueue RegisterCallback failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // get the volume interface
+    result = (*bqPlayerObject)->GetInterface(bqPlayerObject, SL_IID_VOLUME, &bqPlayerVolume);
+    if (result != SL_RESULT_SUCCESS) {
+        ALOGE("sl volume GetInterface failed with result %d", result);
+        return false;
+    }
+    (void)result;
+
+    // set the player's state to playing
+    audioplay::setPlaying(true);
+    CHATTY("Created buffer queue player: %p", bqPlayerBufferQueue);
+    return true;
+}
+
+bool parseClipBuf(const uint8_t* clipBuf, int clipBufSize, const ChunkFormat** oChunkFormat,
+                  const uint8_t** oSoundBuf, unsigned* oSoundBufSize) {
+    *oSoundBuf = clipBuf;
+    *oSoundBufSize = clipBufSize;
+    *oChunkFormat = NULL;
+    const RiffWaveHeader* wavHeader = (const RiffWaveHeader*)*oSoundBuf;
+    if (*oSoundBufSize < sizeof(*wavHeader) || (wavHeader->riff_id != ID_RIFF) ||
+        (wavHeader->wave_id != ID_WAVE)) {
+        ALOGE("Error: audio file is not a riff/wave file\n");
+        return false;
+    }
+    *oSoundBuf += sizeof(*wavHeader);
+    *oSoundBufSize -= sizeof(*wavHeader);
+
+    while (true) {
+        const ChunkHeader* chunkHeader = (const ChunkHeader*)*oSoundBuf;
+        if (*oSoundBufSize < sizeof(*chunkHeader)) {
+            ALOGE("EOF reading chunk headers");
+            return false;
+        }
+
+        *oSoundBuf += sizeof(*chunkHeader);
+        *oSoundBufSize -= sizeof(*chunkHeader);
+
+        bool endLoop = false;
+        switch (chunkHeader->id) {
+            case ID_FMT:
+                *oChunkFormat = (const ChunkFormat*)*oSoundBuf;
+                *oSoundBuf += chunkHeader->sz;
+                *oSoundBufSize -= chunkHeader->sz;
+                break;
+            case ID_DATA:
+                /* Stop looking for chunks */
+                *oSoundBufSize = chunkHeader->sz;
+                endLoop = true;
+                break;
+            default:
+                /* Unknown chunk, skip bytes */
+                *oSoundBuf += chunkHeader->sz;
+                *oSoundBufSize -= chunkHeader->sz;
+        }
+        if (endLoop) {
+            break;
+        }
+    }
+
+    if (*oChunkFormat == NULL) {
+        ALOGE("format not found in WAV file");
+        return false;
+    }
+    return true;
+}
+
+} // namespace
+
+bool create(const uint8_t* exampleClipBuf, int exampleClipBufSize) {
+    if (!createEngine()) {
+        return false;
+    }
+
+    // Parse the example clip.
+    const ChunkFormat* chunkFormat;
+    const uint8_t* soundBuf;
+    unsigned soundBufSize;
+    if (!parseClipBuf(exampleClipBuf, exampleClipBufSize, &chunkFormat, &soundBuf, &soundBufSize)) {
+        return false;
+    }
+
+    // Initialize the BufferQueue based on this clip's format.
+    if (!createBufferQueueAudioPlayer(chunkFormat)) {
+        return false;
+    }
+    return true;
+}
+
+bool playClip(const uint8_t* buf, int size) {
+    // Parse the WAV header
+    const ChunkFormat* chunkFormat;
+    if (!parseClipBuf(buf, size, &chunkFormat, &nextBuffer, &nextSize)) {
+        return false;
+    }
+
+    if (!hasPlayer()) {
+        ALOGD("cannot play clip %p without a player", buf);
+        return false;
+    }
+
+    CHATTY("playClip on player %p: buf=%p size=%d nextSize %d",
+           bqPlayerBufferQueue, buf, size, nextSize);
+
+    if (nextSize > 0) {
+        // here we only enqueue one buffer because it is a long clip,
+        // but for streaming playback we would typically enqueue at least 2 buffers to start
+        SLresult result;
+        result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, nextBuffer, nextSize);
+        if (SL_RESULT_SUCCESS != result) {
+            return false;
+        }
+        audioplay::setPlaying(true);
+    }
+
+    return true;
+}
+
+// set the playing state for the buffer queue audio player
+void setPlaying(bool isPlaying) {
+    if (!hasPlayer()) return;
+
+    SLresult result;
+
+    if (NULL != bqPlayerPlay) {
+        // set the player's state
+        result = (*bqPlayerPlay)->SetPlayState(bqPlayerPlay,
+            isPlaying ? SL_PLAYSTATE_PLAYING : SL_PLAYSTATE_STOPPED);
+    }
+
+}
+
+void destroy() {
+    // destroy buffer queue audio player object, and invalidate all associated interfaces
+    if (bqPlayerObject != NULL) {
+        CHATTY("destroying audio player");
+        (*bqPlayerObject)->Destroy(bqPlayerObject);
+        bqPlayerObject = NULL;
+        bqPlayerPlay = NULL;
+        bqPlayerBufferQueue = NULL;
+        bqPlayerMuteSolo = NULL;
+        bqPlayerVolume = NULL;
+    }
+
+    // destroy output mix object, and invalidate all associated interfaces
+    if (outputMixObject != NULL) {
+        (*outputMixObject)->Destroy(outputMixObject);
+        outputMixObject = NULL;
+    }
+
+    // destroy engine object, and invalidate all associated interfaces
+    if (engineObject != NULL) {
+        CHATTY("destroying audio engine");
+        (*engineObject)->Destroy(engineObject);
+        engineObject = NULL;
+        engineEngine = NULL;
+    }
+}
+
+}  // namespace audioplay
diff --git a/cmds/bootanimation/audioplay.h b/cmds/bootanimation/audioplay.h
new file mode 100644
index 0000000..0e5705a
--- /dev/null
+++ b/cmds/bootanimation/audioplay.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef AUDIOPLAY_H_
+#define AUDIOPLAY_H_
+
+#include <string.h>
+
+namespace audioplay {
+
+// Initializes the engine with an example of the type of WAV clip to play.
+// All buffers passed to playClip are assumed to be in the same format.
+bool create(const uint8_t* exampleClipBuf, int exampleClipBufSize);
+
+// Plays a WAV contained in buf.
+// Should not be called while a clip is still playing.
+bool playClip(const uint8_t* buf, int size);
+void setPlaying(bool isPlaying);
+void destroy();
+
+}
+
+#endif // AUDIOPLAY_H_
diff --git a/cmds/bootanimation/bootanim.rc b/cmds/bootanimation/bootanim.rc
index ee0d0b8..7344ba7 100644
--- a/cmds/bootanimation/bootanim.rc
+++ b/cmds/bootanimation/bootanim.rc
@@ -4,3 +4,4 @@
     group graphics audio
     disabled
     oneshot
+    writepid /dev/stune/top-app/tasks
\ No newline at end of file
diff --git a/cmds/pm/src/com/android/commands/pm/Pm.java b/cmds/pm/src/com/android/commands/pm/Pm.java
index c6834f9..32a8088 100644
--- a/cmds/pm/src/com/android/commands/pm/Pm.java
+++ b/cmds/pm/src/com/android/commands/pm/Pm.java
@@ -922,6 +922,8 @@
                 flags |= UserInfo.FLAG_EPHEMERAL;
             } else if ("--guest".equals(opt)) {
                 flags |= UserInfo.FLAG_GUEST;
+            } else if ("--demo".equals(opt)) {
+                flags |= UserInfo.FLAG_DEMO;
             } else {
                 System.err.println("Error: unknown option " + opt);
                 return showUsage();
diff --git a/cmds/wm/src/com/android/commands/wm/Wm.java b/cmds/wm/src/com/android/commands/wm/Wm.java
index f7f7c88..383cd01 100644
--- a/cmds/wm/src/com/android/commands/wm/Wm.java
+++ b/cmds/wm/src/com/android/commands/wm/Wm.java
@@ -23,6 +23,7 @@
 import android.graphics.Rect;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.UserHandle;
 import android.util.AndroidException;
 import android.util.DisplayMetrics;
 import android.view.Display;
@@ -201,9 +202,11 @@
         try {
             if (density > 0) {
                 // TODO(multidisplay): For now Configuration only applies to main screen.
-                mWm.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, density);
+                mWm.setForcedDisplayDensityForUser(Display.DEFAULT_DISPLAY, density,
+                        UserHandle.USER_CURRENT);
             } else {
-                mWm.clearForcedDisplayDensity(Display.DEFAULT_DISPLAY);
+                mWm.clearForcedDisplayDensityForUser(Display.DEFAULT_DISPLAY,
+                        UserHandle.USER_CURRENT);
             }
         } catch (RemoteException e) {
         }
diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java
index c4eaccc..163e7d2 100644
--- a/core/java/android/accessibilityservice/AccessibilityService.java
+++ b/core/java/android/accessibilityservice/AccessibilityService.java
@@ -53,7 +53,7 @@
 import java.util.List;
 
 /**
- * Accessibility services are intended to assist users with disabilities in using
+ * Accessibility services should only be used to assist users with disabilities in using
  * Android devices and apps. They run in the background and receive callbacks by the system
  * when {@link AccessibilityEvent}s are fired. Such events denote some state transition
  * in the user interface, for example, the focus has changed, a button has been clicked,
diff --git a/core/java/android/accounts/Account.java b/core/java/android/accounts/Account.java
index 7b83a30..b6e85f1 100644
--- a/core/java/android/accounts/Account.java
+++ b/core/java/android/accounts/Account.java
@@ -16,9 +16,19 @@
 
 package android.accounts;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.Context;
 import android.os.Parcelable;
 import android.os.Parcel;
+import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.text.TextUtils;
+import android.util.ArraySet;
+import android.util.Log;
+import com.android.internal.annotations.GuardedBy;
+
+import java.util.Set;
 
 /**
  * Value type that represents an Account in the {@link AccountManager}. This object is
@@ -26,8 +36,14 @@
  * suitable for use as the key of a {@link java.util.Map}
  */
 public class Account implements Parcelable {
+    private static final String TAG = "Account";
+
+    @GuardedBy("sAccessedAccounts")
+    private static final Set<Account> sAccessedAccounts = new ArraySet<>();
+
     public final String name;
     public final String type;
+    private final @Nullable String accessId;
 
     public boolean equals(Object o) {
         if (o == this) return true;
@@ -44,6 +60,20 @@
     }
 
     public Account(String name, String type) {
+        this(name, type, null);
+    }
+
+    /**
+     * @hide
+     */
+    public Account(@NonNull Account other, @NonNull String accessId) {
+        this(other.name, other.type, accessId);
+    }
+
+    /**
+     * @hide
+     */
+    public Account(String name, String type, String accessId) {
         if (TextUtils.isEmpty(name)) {
             throw new IllegalArgumentException("the name must not be empty: " + name);
         }
@@ -52,11 +82,31 @@
         }
         this.name = name;
         this.type = type;
+        this.accessId = accessId;
     }
 
     public Account(Parcel in) {
         this.name = in.readString();
         this.type = in.readString();
+        this.accessId = in.readString();
+        if (accessId != null) {
+            synchronized (sAccessedAccounts) {
+                if (sAccessedAccounts.add(this)) {
+                    try {
+                        IAccountManager accountManager = IAccountManager.Stub.asInterface(
+                                ServiceManager.getService(Context.ACCOUNT_SERVICE));
+                        accountManager.onAccountAccessed(accessId);
+                    } catch (RemoteException e) {
+                        Log.e(TAG, "Error noting account access", e);
+                    }
+                }
+            }
+        }
+    }
+
+    /** @hide */
+    public String getAccessId() {
+        return accessId;
     }
 
     public int describeContents() {
@@ -66,6 +116,7 @@
     public void writeToParcel(Parcel dest, int flags) {
         dest.writeString(name);
         dest.writeString(type);
+        dest.writeString(accessId);
     }
 
     public static final Creator<Account> CREATOR = new Creator<Account>() {
diff --git a/core/java/android/accounts/AccountManager.java b/core/java/android/accounts/AccountManager.java
index ab34a24..cabaf85 100644
--- a/core/java/android/accounts/AccountManager.java
+++ b/core/java/android/accounts/AccountManager.java
@@ -28,6 +28,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.IntentSender;
 import android.content.res.Resources;
 import android.database.SQLException;
 import android.os.Build;
@@ -178,6 +179,14 @@
     public static final String KEY_ACCOUNT_TYPE = "accountType";
 
     /**
+     * Bundle key used for the account access id used for noting the
+     * account was accessed when unmarshalled from a parcel.
+     *
+     * @hide
+     */
+    public static final String KEY_ACCOUNT_ACCESS_ID = "accountAccessId";
+
+    /**
      * Bundle key used for the auth token value in results
      * from {@link #getAuthToken} and friends.
      */
@@ -265,6 +274,15 @@
             "android.accounts.AccountAuthenticator";
     public static final String AUTHENTICATOR_ATTRIBUTES_NAME = "account-authenticator";
 
+    /**
+     * Token type for the special case where a UID has access only to an account
+     * but no authenticator specific auth token types.
+     *
+     * @hide
+     */
+    public static final String ACCOUNT_ACCESS_TOKEN_TYPE =
+            "com.android.AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE";
+
     private final Context mContext;
     private final IAccountManager mService;
     private final Handler mMainHandler;
@@ -803,7 +821,8 @@
             public Account bundleToResult(Bundle bundle) throws AuthenticatorException {
                 String name = bundle.getString(KEY_ACCOUNT_NAME);
                 String type = bundle.getString(KEY_ACCOUNT_TYPE);
-                return new Account(name, type);
+                String accessId = bundle.getString(KEY_ACCOUNT_ACCESS_ID);
+                return new Account(name, type, accessId);
             }
         }.start();
     }
@@ -2263,6 +2282,7 @@
                                     result.putString(KEY_ACCOUNT_NAME, null);
                                     result.putString(KEY_ACCOUNT_TYPE, null);
                                     result.putString(KEY_AUTHTOKEN, null);
+                                    result.putBinder(KEY_ACCOUNT_ACCESS_ID, null);
                                     try {
                                         mResponse.onResult(result);
                                     } catch (RemoteException e) {
@@ -2288,9 +2308,11 @@
                                         public void onResult(Bundle value) throws RemoteException {
                                             Account account = new Account(
                                                     value.getString(KEY_ACCOUNT_NAME),
-                                                    value.getString(KEY_ACCOUNT_TYPE));
-                                            mFuture = getAuthToken(account, mAuthTokenType, mLoginOptions,
-                                                    mActivity, mMyCallback, mHandler);
+                                                    value.getString(KEY_ACCOUNT_TYPE),
+                                                    value.getString(KEY_ACCOUNT_ACCESS_ID));
+                                            mFuture = getAuthToken(account, mAuthTokenType,
+                                                    mLoginOptions,  mActivity, mMyCallback,
+                                                    mHandler);
                                         }
 
                                         @Override
@@ -2337,7 +2359,8 @@
                         setException(new AuthenticatorException("account not in result"));
                         return;
                     }
-                    final Account account = new Account(accountName, accountType);
+                    final String accessId = result.getString(KEY_ACCOUNT_ACCESS_ID);
+                    final Account account = new Account(accountName, accountType, accessId);
                     mNumAccounts = 1;
                     getAuthToken(account, mAuthTokenType, null /* options */, mActivity,
                             mMyCallback, mHandler);
@@ -2960,4 +2983,49 @@
             }
         }.start();
     }
+
+    /**
+     * Gets whether a given package under a user has access to an account.
+     * Can be called only from the system UID.
+     *
+     * @param account The account for which to check.
+     * @param packageName The package for which to check.
+     * @param userHandle The user for which to check.
+     * @return True if the package can access the account.
+     *
+     * @hide
+     */
+    public boolean hasAccountAccess(@NonNull Account account, @NonNull String packageName,
+            @NonNull UserHandle userHandle) {
+        try {
+            return mService.hasAccountAccess(account, packageName, userHandle);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Creates an intent to request access to a given account for a UID.
+     * The returned intent should be stated for a result where {@link
+     * Activity#RESULT_OK} result means access was granted whereas {@link
+     * Activity#RESULT_CANCELED} result means access wasn't granted. Can
+     * be called only from the system UID.
+     *
+     * @param account The account for which to request.
+     * @param packageName The package name which to request.
+     * @param userHandle The user for which to request.
+     * @return The intent to request account access or null if the package
+     *     doesn't exist.
+     *
+     * @hide
+     */
+    public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account,
+            @NonNull String packageName, @NonNull UserHandle userHandle) {
+        try {
+            return mService.createRequestAccountAccessIntentSenderAsUser(account, packageName,
+                    userHandle);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
 }
diff --git a/core/java/android/accounts/AccountManagerInternal.java b/core/java/android/accounts/AccountManagerInternal.java
new file mode 100644
index 0000000..68c17c3
--- /dev/null
+++ b/core/java/android/accounts/AccountManagerInternal.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.accounts;
+
+import android.annotation.IntRange;
+import android.annotation.NonNull;
+import android.os.RemoteCallback;
+
+/**
+ * Account manager local system service interface.
+ *
+ * @hide Only for use within the system server.
+ */
+public abstract class AccountManagerInternal {
+
+    /**
+     * Listener for explicit UID account access grant changes.
+     */
+    public interface OnAppPermissionChangeListener {
+
+        /**
+         * Called when the explicit grant state for a given UID to
+         * access an account changes.
+         *
+         * @param account The account
+         * @param uid The UID for which the grant changed
+         */
+        public void onAppPermissionChanged(Account account, int uid);
+    }
+
+    /**
+     * Requests that a given package is given access to an account.
+     * The provided callback will be invoked with a {@link android.os.Bundle}
+     * containing the result which will be a boolean value mapped to the
+     * {@link AccountManager#KEY_BOOLEAN_RESULT} key.
+     *
+     * @param account The account for which to request.
+     * @param packageName The package name for which to request.
+     * @param userId Concrete user id for which to request.
+     * @param callback A callback for receiving the result.
+     */
+    public abstract void requestAccountAccess(@NonNull Account account,
+            @NonNull String packageName, @IntRange(from = 0) int userId,
+            @NonNull RemoteCallback callback);
+
+    /**
+     * Check whether the given UID has access to the account.
+     *
+     * @param account The account
+     * @param uid The UID
+     * @return Whether the UID can access the account
+     */
+    public abstract boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid);
+
+    /**
+     * Adds a listener for explicit UID account access grant changes.
+     *
+     * @param listener The listener
+     */
+    public abstract void addOnAppPermissionChangeListener(
+            @NonNull OnAppPermissionChangeListener listener);
+
+    /**
+     * Backups the account access permissions.
+     * @param userId The user for which to backup.
+     * @return The backup data.
+     */
+    public abstract byte[] backupAccountAccessPermissions(int userId);
+
+    /**
+     * Restores the account access permissions.
+     * @param data The restore data.
+     * @param userId The user for which to restore.
+     */
+    public abstract void restoreAccountAccessPermissions(byte[] data, int userId);
+}
diff --git a/core/java/android/accounts/ChooseTypeAndAccountActivity.java b/core/java/android/accounts/ChooseTypeAndAccountActivity.java
index 133df2b..aed7a36 100644
--- a/core/java/android/accounts/ChooseTypeAndAccountActivity.java
+++ b/core/java/android/accounts/ChooseTypeAndAccountActivity.java
@@ -399,7 +399,7 @@
      * useless.
      */
     private void setNonLabelThemeAndCallSuperCreate(Bundle savedInstanceState) {
-        setTheme(R.style.Theme_Material_Light_Dialog_NoActionBar);
+        setTheme(R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);
         super.onCreate(savedInstanceState);
     }
 
diff --git a/core/java/android/accounts/GrantCredentialsPermissionActivity.java b/core/java/android/accounts/GrantCredentialsPermissionActivity.java
index 12b2b9c..38eab29 100644
--- a/core/java/android/accounts/GrantCredentialsPermissionActivity.java
+++ b/core/java/android/accounts/GrantCredentialsPermissionActivity.java
@@ -35,12 +35,10 @@
  */
 public class GrantCredentialsPermissionActivity extends Activity implements View.OnClickListener {
     public static final String EXTRAS_ACCOUNT = "account";
-    public static final String EXTRAS_AUTH_TOKEN_LABEL = "authTokenLabel";
     public static final String EXTRAS_AUTH_TOKEN_TYPE = "authTokenType";
     public static final String EXTRAS_RESPONSE = "response";
-    public static final String EXTRAS_ACCOUNT_TYPE_LABEL = "accountTypeLabel";
-    public static final String EXTRAS_PACKAGES = "application";
     public static final String EXTRAS_REQUESTING_UID = "uid";
+
     private Account mAccount;
     private String mAuthTokenType;
     private int mUid;
@@ -109,7 +107,11 @@
                 }
             }
         };
-        AccountManager.get(this).getAuthTokenLabel(mAccount.type, mAuthTokenType, callback, null);
+
+        if (!AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(mAuthTokenType)) {
+            AccountManager.get(this).getAuthTokenLabel(mAccount.type,
+                    mAuthTokenType, callback, null);
+        }
 
         findViewById(R.id.allow_button).setOnClickListener(this);
         findViewById(R.id.deny_button).setOnClickListener(this);
diff --git a/core/java/android/accounts/IAccountManager.aidl b/core/java/android/accounts/IAccountManager.aidl
index 7199288..c271e7e 100644
--- a/core/java/android/accounts/IAccountManager.aidl
+++ b/core/java/android/accounts/IAccountManager.aidl
@@ -19,8 +19,10 @@
 import android.accounts.IAccountManagerResponse;
 import android.accounts.Account;
 import android.accounts.AuthenticatorDescription;
+import android.content.IntentSender;
 import android.os.Bundle;
-
+import android.os.RemoteCallback;
+import android.os.UserHandle;
 
 /**
  * Central application service that provides account management.
@@ -102,4 +104,12 @@
     /* Check if credentials update is suggested */
     void isCredentialsUpdateSuggested(in IAccountManagerResponse response, in Account account,
         String statusToken);
+
+    /* Check if the package in a user can access an account */
+    boolean hasAccountAccess(in Account account, String packageName, in UserHandle userHandle);
+    /* Crate an intent to request account access for package and a given user id */
+    IntentSender createRequestAccountAccessIntentSenderAsUser(in Account account,
+        String packageName, in UserHandle userHandle);
+
+    void onAccountAccessed(String token);
 }
diff --git a/core/java/android/animation/AnimatorSet.java b/core/java/android/animation/AnimatorSet.java
index 7841d29..053ba7d 100644
--- a/core/java/android/animation/AnimatorSet.java
+++ b/core/java/android/animation/AnimatorSet.java
@@ -1064,15 +1064,15 @@
     /**
      * @hide
      * TODO: For animatorSet defined in XML, we can use a flag to indicate what the play order
-     * if defined (i.e. sequential or together), then we can use the flag instead of calculate
-     * dynamically.
+     * if defined (i.e. sequential or together), then we can use the flag instead of calculating
+     * dynamically. Note that when AnimatorSet is empty this method returns true.
      * @return whether all the animators in the set are supposed to play together
      */
     public boolean shouldPlayTogether() {
         updateAnimatorsDuration();
         createDependencyGraph();
         // All the child nodes are set out to play right after the delay animation
-        return mRootNode.mChildNodes.size() == mNodes.size() - 1;
+        return mRootNode.mChildNodes == null || mRootNode.mChildNodes.size() == mNodes.size() - 1;
     }
 
     @Override
diff --git a/core/java/android/animation/ObjectAnimator.java b/core/java/android/animation/ObjectAnimator.java
index 5c4b979..0c21c4f 100644
--- a/core/java/android/animation/ObjectAnimator.java
+++ b/core/java/android/animation/ObjectAnimator.java
@@ -978,7 +978,8 @@
     void animateValue(float fraction) {
         final Object target = getTarget();
         if (mTarget != null && target == null) {
-            // We lost the target reference, cancel and clean up.
+            // We lost the target reference, cancel and clean up. Note: we allow null target if the
+            /// target has never been set.
             cancel();
             return;
         }
diff --git a/core/java/android/animation/PropertyValuesHolder.java b/core/java/android/animation/PropertyValuesHolder.java
index 224823e..ba16e67 100644
--- a/core/java/android/animation/PropertyValuesHolder.java
+++ b/core/java/android/animation/PropertyValuesHolder.java
@@ -1095,8 +1095,12 @@
         }
         // TODO: We need a better way to get data out of keyframes.
         if (mKeyframes instanceof PathKeyframes.FloatKeyframesBase
-                || mKeyframes instanceof PathKeyframes.IntKeyframesBase) {
-            // property values will animate based on external data source (e.g. Path)
+                || mKeyframes instanceof PathKeyframes.IntKeyframesBase
+                || (mKeyframes.getKeyframes() != null && mKeyframes.getKeyframes().size() > 2)) {
+            // When a pvh has more than 2 keyframes, that means there are intermediate values in
+            // addition to start/end values defined for animators. Another case where such
+            // intermediate values are defined is when animator has a path to animate along. In
+            // these cases, a data source is needed to capture these intermediate values.
             values.dataSource = new PropertyValues.DataSource() {
                 @Override
                 public Object getValueAtFraction(float fraction) {
@@ -1108,6 +1112,13 @@
         }
     }
 
+    /**
+     * @hide
+     */
+    public Class getValueType() {
+        return mValueType;
+    }
+
     @Override
     public String toString() {
         return mPropertyName + ": " + mKeyframes.toString();
@@ -1178,6 +1189,15 @@
         }
 
         @Override
+        public void setProperty(Property property) {
+            if (property instanceof IntProperty) {
+                mIntProperty = (IntProperty) property;
+            } else {
+                super.setProperty(property);
+            }
+        }
+
+        @Override
         public void setIntValues(int... values) {
             super.setIntValues(values);
             mIntKeyframes = (Keyframes.IntKeyframes) mKeyframes;
@@ -1316,6 +1336,15 @@
         }
 
         @Override
+        public void setProperty(Property property) {
+            if (property instanceof FloatProperty) {
+                mFloatProperty = (FloatProperty) property;
+            } else {
+                super.setProperty(property);
+            }
+        }
+
+        @Override
         public void setFloatValues(float... values) {
             super.setFloatValues(values);
             mFloatKeyframes = (Keyframes.FloatKeyframes) mKeyframes;
@@ -1516,7 +1545,7 @@
                     }
                     propertyMap.put(mPropertyName, mJniSetter);
                 }
-           }
+            }
         }
     }
 
diff --git a/core/java/android/animation/ValueAnimator.java b/core/java/android/animation/ValueAnimator.java
index 0c7ee2c..7e16e3e 100644
--- a/core/java/android/animation/ValueAnimator.java
+++ b/core/java/android/animation/ValueAnimator.java
@@ -18,6 +18,7 @@
 
 import android.annotation.CallSuper;
 import android.annotation.IntDef;
+import android.annotation.TestApi;
 import android.os.Looper;
 import android.os.Trace;
 import android.util.AndroidRuntimeException;
@@ -261,6 +262,7 @@
     /**
      * @hide
      */
+    @TestApi
     public static void setDurationScale(float durationScale) {
         sDurationScale = durationScale;
     }
@@ -268,6 +270,7 @@
     /**
      * @hide
      */
+    @TestApi
     public static float getDurationScale() {
         return sDurationScale;
     }
@@ -982,6 +985,7 @@
         mStarted = true;
         mPaused = false;
         mRunning = false;
+        mAnimationEndRequested = false;
         // Resets mLastFrameTime when start() is called, so that if the animation was running,
         // calling start() would put the animation in the
         // started-but-not-yet-reached-the-first-frame phase.
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index b1a578d..e4880b0 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -4220,6 +4220,7 @@
     public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
             @Nullable Bundle options) {
         if (mParent == null) {
+            options = transferSpringboardActivityOptions(options);
             Instrumentation.ActivityResult ar =
                 mInstrumentation.execStartActivity(
                     this, mMainThread.getApplicationThread(), mToken, this,
@@ -4268,6 +4269,17 @@
         }
     }
 
+    private Bundle transferSpringboardActivityOptions(Bundle options) {
+        if (options == null && (mWindow != null && !mWindow.isActive())) {
+            final ActivityOptions activityOptions = getActivityOptions();
+            if (activityOptions != null &&
+                    activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
+                return activityOptions.toBundle();
+            }
+        }
+        return options;
+    }
+
     /**
      * @hide Implement to provide correct calling token.
      */
@@ -4283,6 +4295,7 @@
         if (mParent != null) {
             throw new RuntimeException("Can't be called from a child");
         }
+        options = transferSpringboardActivityOptions(options);
         Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity(
                 this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode,
                 options, user);
@@ -4318,6 +4331,7 @@
         if (mParent != null) {
             throw new RuntimeException("Can't be called from a child");
         }
+        options = transferSpringboardActivityOptions(options);
         Instrumentation.ActivityResult ar =
                 mInstrumentation.execStartActivity(
                         this, mMainThread.getApplicationThread(), mToken, this,
@@ -4350,6 +4364,7 @@
         if (mParent != null) {
             throw new RuntimeException("Can't be called from a child");
         }
+        options = transferSpringboardActivityOptions(options);
         Instrumentation.ActivityResult ar =
                 mInstrumentation.execStartActivityAsCaller(
                         this, mMainThread.getApplicationThread(), mToken, this,
@@ -4789,6 +4804,7 @@
      */
     public void startActivityFromChild(@NonNull Activity child, @RequiresPermission Intent intent,
             int requestCode, @Nullable Bundle options) {
+        options = transferSpringboardActivityOptions(options);
         Instrumentation.ActivityResult ar =
             mInstrumentation.execStartActivity(
                 this, mMainThread.getApplicationThread(), mToken, child,
@@ -4854,6 +4870,7 @@
         if (referrer != null) {
             intent.putExtra(Intent.EXTRA_REFERRER, referrer);
         }
+        options = transferSpringboardActivityOptions(options);
         Instrumentation.ActivityResult ar =
             mInstrumentation.execStartActivity(
                 this, mMainThread.getApplicationThread(), mToken, who,
diff --git a/core/java/android/app/ActivityManager.java b/core/java/android/app/ActivityManager.java
index c96bd39e..af981f6 100644
--- a/core/java/android/app/ActivityManager.java
+++ b/core/java/android/app/ActivityManager.java
@@ -93,7 +93,8 @@
     @IntDef({
             BUGREPORT_OPTION_FULL,
             BUGREPORT_OPTION_INTERACTIVE,
-            BUGREPORT_OPTION_REMOTE
+            BUGREPORT_OPTION_REMOTE,
+            BUGREPORT_OPTION_WEAR
     })
     public @interface BugreportMode {}
     /**
@@ -114,6 +115,11 @@
      * @hide
      */
     public static final int BUGREPORT_OPTION_REMOTE = 2;
+    /**
+     * Takes a bugreport on a wearable device.
+     * @hide
+     */
+    public static final int BUGREPORT_OPTION_WEAR = 3;
 
     /**
      * <a href="{@docRoot}guide/topics/manifest/meta-data-element.html">{@code
@@ -3625,6 +3631,24 @@
     }
 
     /**
+     * Enable more aggressive scheduling for latency-sensitive low-runtime VR threads. Only one
+     * thread can be a VR thread in a process at a time, and that thread may be subject to
+     * restrictions on the amount of time it can run.
+     *
+     * To reset the VR thread for an application, a tid of 0 can be passed.
+     *
+     * @see android.os.Process#myTid()
+     * @param tid tid of the VR thread
+     */
+    public static void setVrThread(int tid) {
+        try {
+            ActivityManagerNative.getDefault().setVrThread(tid);
+        } catch (RemoteException e) {
+            // pass
+        }
+    }
+
+    /**
      * The AppTask allows you to manage your own application's tasks.
      * See {@link android.app.ActivityManager#getAppTasks()}
      */
diff --git a/core/java/android/app/ActivityManagerInternal.java b/core/java/android/app/ActivityManagerInternal.java
index 3a70a4c..0ba937a 100644
--- a/core/java/android/app/ActivityManagerInternal.java
+++ b/core/java/android/app/ActivityManagerInternal.java
@@ -19,6 +19,9 @@
 import android.annotation.NonNull;
 import android.content.ComponentName;
 import android.content.IIntentSender;
+import android.content.Intent;
+import android.content.res.Configuration;
+import android.os.Bundle;
 import android.os.IBinder;
 import android.service.voice.IVoiceInteractionSession;
 
@@ -151,4 +154,29 @@
      *  such as Power Save mode.
      */
     public abstract void setPendingIntentWhitelistDuration(IIntentSender target, long duration);
+
+    /**
+     * Updates and persists the {@link Configuration} for a given user.
+     *
+     * @param values the configuration to update
+     * @param userId the user to update the configuration for
+     */
+    public abstract void updatePersistentConfigurationForUser(@NonNull Configuration values,
+            int userId);
+
+    /**
+     * Start activity {@code intents} as if {@code packageName} on user {@code userId} did it.
+     *
+     * @return error codes used by {@link IActivityManager#startActivity} and its siblings.
+     */
+    public abstract int startActivitiesAsPackage(String packageName,
+            int userId, Intent[] intents, Bundle bOptions);
+
+    /**
+     * Get the procstate for the UID.  The return value will be between
+     * {@link ActivityManager#MIN_PROCESS_STATE} and {@link ActivityManager#MAX_PROCESS_STATE}.
+     * Note if the UID doesn't exist, it'll return {@link ActivityManager#PROCESS_STATE_NONEXISTENT}
+     * (-1).
+     */
+    public abstract int getUidProcessState(int uid);
 }
diff --git a/core/java/android/app/ActivityManagerNative.java b/core/java/android/app/ActivityManagerNative.java
index dcac633..50479c8 100644
--- a/core/java/android/app/ActivityManagerNative.java
+++ b/core/java/android/app/ActivityManagerNative.java
@@ -2371,7 +2371,8 @@
             data.enforceInterface(IActivityManager.descriptor);
             IUserSwitchObserver observer = IUserSwitchObserver.Stub.asInterface(
                     data.readStrongBinder());
-            registerUserSwitchObserver(observer);
+            String name = data.readString();
+            registerUserSwitchObserver(observer, name);
             reply.writeNoException();
             return true;
         }
@@ -2995,6 +2996,35 @@
             reply.writeInt(result);
             return true;
         }
+        case SET_VR_THREAD_TRANSACTION: {
+            data.enforceInterface(IActivityManager.descriptor);
+            final int tid = data.readInt();
+            setVrThread(tid);
+            reply.writeNoException();
+            return true;
+        }
+        case SET_RENDER_THREAD_TRANSACTION: {
+            data.enforceInterface(IActivityManager.descriptor);
+            final int tid = data.readInt();
+            setRenderThread(tid);
+            reply.writeNoException();
+            return true;
+        }
+        case SET_HAS_TOP_UI: {
+            data.enforceInterface(IActivityManager.descriptor);
+            final boolean hasTopUi = data.readInt() != 0;
+            setHasTopUi(hasTopUi);
+            reply.writeNoException();
+            return true;
+        }
+        case CAN_BYPASS_WORK_CHALLENGE: {
+            data.enforceInterface(IActivityManager.descriptor);
+            final PendingIntent intent = PendingIntent.CREATOR.createFromParcel(data);
+            final boolean result = canBypassWorkChallenge(intent);
+            reply.writeNoException();
+            reply.writeInt(result ? 1 : 0);
+            return true;
+        }
         }
 
         return super.onTransact(code, data, reply, flags);
@@ -6060,11 +6090,13 @@
         return result;
     }
 
-    public void registerUserSwitchObserver(IUserSwitchObserver observer) throws RemoteException {
+    public void registerUserSwitchObserver(IUserSwitchObserver observer,
+            String name) throws RemoteException {
         Parcel data = Parcel.obtain();
         Parcel reply = Parcel.obtain();
         data.writeInterfaceToken(IActivityManager.descriptor);
         data.writeStrongBinder(observer != null ? observer.asBinder() : null);
+        data.writeString(name);
         mRemote.transact(REGISTER_USER_SWITCH_OBSERVER_TRANSACTION, data, reply, 0);
         reply.readException();
         data.recycle();
@@ -7028,5 +7060,59 @@
         return res;
     }
 
+    @Override
+    public void setVrThread(int tid)
+        throws RemoteException {
+        Parcel data = Parcel.obtain();
+        Parcel reply = Parcel.obtain();
+        data.writeInterfaceToken(IActivityManager.descriptor);
+        data.writeInt(tid);
+        mRemote.transact(SET_VR_THREAD_TRANSACTION, data, reply, 0);
+        reply.readException();
+        data.recycle();
+        reply.recycle();
+        return;
+    }
+
+    public void setRenderThread(int tid)
+        throws RemoteException {
+        Parcel data = Parcel.obtain();
+        Parcel reply = Parcel.obtain();
+        data.writeInterfaceToken(IActivityManager.descriptor);
+        data.writeInt(tid);
+        mRemote.transact(SET_RENDER_THREAD_TRANSACTION, data, reply, 0);
+        reply.readException();
+        data.recycle();
+        reply.recycle();
+        return;
+    }
+
+    public void setHasTopUi(boolean hasTopUi)
+            throws RemoteException {
+        Parcel data = Parcel.obtain();
+        Parcel reply = Parcel.obtain();
+        data.writeInterfaceToken(IActivityManager.descriptor);
+        data.writeInt(hasTopUi ? 1 : 0);
+        mRemote.transact(SET_HAS_TOP_UI, data, reply, 0);
+        reply.readException();
+        data.recycle();
+        reply.recycle();
+        return;
+    }
+    @Override
+    public boolean canBypassWorkChallenge(PendingIntent intent)
+            throws RemoteException {
+        Parcel data = Parcel.obtain();
+        Parcel reply = Parcel.obtain();
+        data.writeInterfaceToken(IActivityManager.descriptor);
+        intent.writeToParcel(data, 0);
+        mRemote.transact(CAN_BYPASS_WORK_CHALLENGE, data, reply, 0);
+        reply.readException();
+        final int result = reply.readInt();
+        data.recycle();
+        reply.recycle();
+        return result != 0;
+    }
+
     private IBinder mRemote;
 }
diff --git a/core/java/android/app/ActivityOptions.java b/core/java/android/app/ActivityOptions.java
index 4c8ddc7..d9a4690 100644
--- a/core/java/android/app/ActivityOptions.java
+++ b/core/java/android/app/ActivityOptions.java
@@ -31,10 +31,13 @@
 import android.os.Parcelable;
 import android.os.RemoteException;
 import android.os.ResultReceiver;
+import android.transition.Transition;
+import android.transition.TransitionManager;
 import android.util.Pair;
 import android.util.Slog;
 import android.view.AppTransitionAnimationSpec;
 import android.view.View;
+import android.view.ViewGroup;
 import android.view.Window;
 
 import java.util.ArrayList;
@@ -190,6 +193,7 @@
             = "android:activity.exitCoordinatorIndex";
 
     private static final String KEY_USAGE_TIME_REPORT = "android:activity.usageTimeReport";
+    private static final String KEY_ROTATION_ANIMATION_HINT = "android:activity.rotationAnimationHint";
 
     /** @hide */
     public static final int ANIM_NONE = 0;
@@ -241,6 +245,7 @@
     private int mDockCreateMode = DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT;
     private boolean mTaskOverlay;
     private AppTransitionAnimationSpec mAnimSpecs[];
+    private int mRotationAnimationHint = -1;
 
     /**
      * Create an ActivityOptions specifying a custom animation to run when
@@ -640,10 +645,71 @@
     public static ActivityOptions makeSceneTransitionAnimation(Activity activity,
             Pair<View, String>... sharedElements) {
         ActivityOptions opts = new ActivityOptions();
-        if (!activity.getWindow().hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)) {
-            opts.mAnimationType = ANIM_DEFAULT;
+        makeSceneTransitionAnimation(activity, activity.getWindow(), opts,
+                activity.mExitTransitionListener, sharedElements);
+        return opts;
+    }
+
+    /**
+     * Call this immediately prior to startActivity to begin a shared element transition
+     * from a non-Activity. The window must support Window.FEATURE_ACTIVITY_TRANSITIONS.
+     * The exit transition will start immediately and the shared element transition will
+     * start once the launched Activity's shared element is ready.
+     * <p>
+     * When all transitions have completed and the shared element has been transfered,
+     * the window's decor View will have its visibility set to View.GONE.
+     *
+     * @hide
+     */
+    @SafeVarargs
+    public static ActivityOptions startSharedElementAnimation(Window window,
+            Pair<View, String>... sharedElements) {
+        ActivityOptions opts = new ActivityOptions();
+        final View decorView = window.getDecorView();
+        if (decorView == null) {
             return opts;
         }
+        final ExitTransitionCoordinator exit =
+                makeSceneTransitionAnimation(null, window, opts, null, sharedElements);
+        if (exit != null) {
+            HideWindowListener listener = new HideWindowListener(window, exit);
+            exit.setHideSharedElementsCallback(listener);
+            exit.startExit();
+        }
+        return opts;
+    }
+
+    /**
+     * This method should be called when the {@link #startSharedElementAnimation(Window, Pair[])}
+     * animation must be stopped and the Views reset. This can happen if there was an error
+     * from startActivity or a springboard activity and the animation should stop and reset.
+     *
+     * @hide
+     */
+    public static void stopSharedElementAnimation(Window window) {
+        final View decorView = window.getDecorView();
+        if (decorView == null) {
+            return;
+        }
+        final ExitTransitionCoordinator exit = (ExitTransitionCoordinator)
+                decorView.getTag(com.android.internal.R.id.cross_task_transition);
+        if (exit != null) {
+            exit.cancelPendingTransitions();
+            decorView.setTagInternal(com.android.internal.R.id.cross_task_transition, null);
+            TransitionManager.endTransitions((ViewGroup) decorView);
+            exit.resetViews();
+            exit.clearState();
+            decorView.setVisibility(View.VISIBLE);
+        }
+    }
+
+    static ExitTransitionCoordinator makeSceneTransitionAnimation(Activity activity, Window window,
+            ActivityOptions opts, SharedElementCallback callback,
+            Pair<View, String>[] sharedElements) {
+        if (!window.hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)) {
+            opts.mAnimationType = ANIM_DEFAULT;
+            return null;
+        }
         opts.mAnimationType = ANIM_SCENE_TRANSITION;
 
         ArrayList<String> names = new ArrayList<String>();
@@ -665,18 +731,22 @@
             }
         }
 
-        ExitTransitionCoordinator exit = new ExitTransitionCoordinator(activity, names, names,
-                views, false);
+        ExitTransitionCoordinator exit = new ExitTransitionCoordinator(activity, window,
+                callback, names, names, views, false);
         opts.mTransitionReceiver = exit;
         opts.mSharedElementNames = names;
-        opts.mIsReturning = false;
-        opts.mExitCoordinatorIndex =
-                activity.mActivityTransitionState.addExitTransitionCoordinator(exit);
-        return opts;
+        opts.mIsReturning = (activity == null);
+        if (activity == null) {
+            opts.mExitCoordinatorIndex = -1;
+        } else {
+            opts.mExitCoordinatorIndex =
+                    activity.mActivityTransitionState.addExitTransitionCoordinator(exit);
+        }
+        return exit;
     }
 
     /** @hide */
-    public static ActivityOptions makeSceneTransitionAnimation(Activity activity,
+    static ActivityOptions makeSceneTransitionAnimation(Activity activity,
             ExitTransitionCoordinator exitCoordinator, ArrayList<String> sharedElementNames,
             int resultCode, Intent resultData) {
         ActivityOptions opts = new ActivityOptions();
@@ -795,6 +865,7 @@
             mAnimationFinishedListener = IRemoteCallback.Stub.asInterface(
                     opts.getBinder(KEY_ANIMATION_FINISHED_LISTENER));
         }
+        mRotationAnimationHint = opts.getInt(KEY_ROTATION_ANIMATION_HINT);
     }
 
     /**
@@ -900,6 +971,16 @@
         return mIsReturning;
     }
 
+    /**
+     * Returns whether or not the ActivityOptions was created with
+     * {@link #startSharedElementAnimation(Window, Pair[])}.
+     *
+     * @hide
+     */
+    boolean isCrossTask() {
+        return mExitCoordinatorIndex < 0;
+    }
+
     /** @hide */
     public ArrayList<String> getSharedElementNames() {
         return mSharedElementNames;
@@ -1138,6 +1219,7 @@
         if (mAnimationFinishedListener != null) {
             b.putBinder(KEY_ANIMATION_FINISHED_LISTENER, mAnimationFinishedListener.asBinder());
         }
+        b.putInt(KEY_ROTATION_ANIMATION_HINT, mRotationAnimationHint);
 
         return b;
     }
@@ -1184,6 +1266,27 @@
         return null;
     }
 
+    /**
+     * Returns the rotation animation set by {@link setRotationAnimationHint} or -1
+     * if unspecified.
+     * @hide
+     */
+    public int getRotationAnimationHint() {
+        return mRotationAnimationHint;
+    }
+
+
+    /**
+     * Set a rotation animation to be used if launching the activity
+     * triggers an orientation change, or -1 to clear. See
+     * {@link android.view.WindowManager.LayoutParams} for rotation
+     * animation values.
+     * @hide
+     */
+    public void setRotationAnimationHint(int hint) {
+        mRotationAnimationHint = hint;
+    }
+
     /** @hide */
     @Override
     public String toString() {
@@ -1191,4 +1294,65 @@
                 + ", mAnimationType=" + mAnimationType + ", mStartX=" + mStartX + ", mStartY="
                 + mStartY + ", mWidth=" + mWidth + ", mHeight=" + mHeight;
     }
+
+    private static class HideWindowListener extends Transition.TransitionListenerAdapter
+        implements ExitTransitionCoordinator.HideSharedElementsCallback {
+        private final Window mWindow;
+        private final ExitTransitionCoordinator mExit;
+        private final boolean mWaitingForTransition;
+        private boolean mTransitionEnded;
+        private boolean mSharedElementHidden;
+        private ArrayList<View> mSharedElements;
+
+        public HideWindowListener(Window window, ExitTransitionCoordinator exit) {
+            mWindow = window;
+            mExit = exit;
+            mSharedElements = new ArrayList<>(exit.mSharedElements);
+            Transition transition = mWindow.getExitTransition();
+            if (transition != null) {
+                transition.addListener(this);
+                mWaitingForTransition = true;
+            } else {
+                mWaitingForTransition = false;
+            }
+            View decorView = mWindow.getDecorView();
+            if (decorView != null) {
+                if (decorView.getTag(com.android.internal.R.id.cross_task_transition) != null) {
+                    throw new IllegalStateException(
+                            "Cannot start a transition while one is running");
+                }
+                decorView.setTagInternal(com.android.internal.R.id.cross_task_transition, exit);
+            }
+        }
+
+        @Override
+        public void onTransitionEnd(Transition transition) {
+            mTransitionEnded = true;
+            hideWhenDone();
+            transition.removeListener(this);
+        }
+
+        @Override
+        public void hideSharedElements() {
+            mSharedElementHidden = true;
+            hideWhenDone();
+        }
+
+        private void hideWhenDone() {
+            if (mSharedElementHidden && (!mWaitingForTransition || mTransitionEnded)) {
+                mExit.resetViews();
+                int numSharedElements = mSharedElements.size();
+                for (int i = 0; i < numSharedElements; i++) {
+                    View view = mSharedElements.get(i);
+                    view.requestLayout();
+                }
+                View decorView = mWindow.getDecorView();
+                if (decorView != null) {
+                    decorView.setTagInternal(
+                            com.android.internal.R.id.cross_task_transition, null);
+                    decorView.setVisibility(View.GONE);
+                }
+            }
+        }
+    }
 }
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index 83dab16..6a2b9aa 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -108,6 +108,7 @@
 import android.system.Os;
 import android.system.OsConstants;
 import android.system.ErrnoException;
+import android.webkit.WebView;
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.app.IVoiceInteractor;
@@ -236,6 +237,7 @@
     boolean mSystemThread = false;
     boolean mJitEnabled = false;
     boolean mSomeActivitiesChanged = false;
+    boolean mUpdatingSystemConfig = false;
 
     // These can be accessed by multiple threads; mPackages is the lock.
     // XXX For now we keep around information about all packages we have
@@ -432,8 +434,10 @@
     static final class NewIntentData {
         List<ReferrerIntent> intents;
         IBinder token;
+        boolean andPause;
         public String toString() {
-            return "NewIntentData{intents=" + intents + " token=" + token + "}";
+            return "NewIntentData{intents=" + intents + " token=" + token
+                    + " andPause=" + andPause +"}";
         }
     }
 
@@ -750,10 +754,12 @@
                     configChanges, notResumed, config, overrideConfig, true, preserveWindow);
         }
 
-        public final void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token) {
+        public final void scheduleNewIntent(
+                List<ReferrerIntent> intents, IBinder token, boolean andPause) {
             NewIntentData data = new NewIntentData();
             data.intents = intents;
             data.token = token;
+            data.andPause = andPause;
 
             sendMessage(H.NEW_INTENT, data);
         }
@@ -1036,10 +1042,21 @@
             long dalvikMax = runtime.totalMemory() / 1024;
             long dalvikFree = runtime.freeMemory() / 1024;
             long dalvikAllocated = dalvikMax - dalvikFree;
+
+            Class[] classesToCount = new Class[] {
+                    ContextImpl.class,
+                    Activity.class,
+                    WebView.class,
+                    OpenSSLSocketImpl.class
+            };
+            long[] instanceCounts = VMDebug.countInstancesOfClasses(classesToCount, true);
+            long appContextInstanceCount = instanceCounts[0];
+            long activityInstanceCount = instanceCounts[1];
+            long webviewInstanceCount = instanceCounts[2];
+            long openSslSocketCount = instanceCounts[3];
+
             long viewInstanceCount = ViewDebug.getViewInstanceCount();
             long viewRootInstanceCount = ViewDebug.getViewRootImplCount();
-            long appContextInstanceCount = Debug.countInstancesOfClass(ContextImpl.class);
-            long activityInstanceCount = Debug.countInstancesOfClass(Activity.class);
             int globalAssetCount = AssetManager.getGlobalAssetCount();
             int globalAssetManagerCount = AssetManager.getGlobalAssetManagerCount();
             int binderLocalObjectCount = Debug.getBinderLocalObjectCount();
@@ -1047,7 +1064,6 @@
             int binderDeathObjectCount = Debug.getBinderDeathObjectCount();
             long parcelSize = Parcel.getGlobalAllocSize();
             long parcelCount = Parcel.getGlobalAllocCount();
-            long openSslSocketCount = Debug.countInstancesOfClass(OpenSSLSocketImpl.class);
             SQLiteDebug.PagerStats stats = SQLiteDebug.getDatabaseInfo();
 
             dumpMemInfoTable(pw, memInfo, checkin, dumpFullInfo, dumpDalvik, dumpSummaryOnly,
@@ -1110,6 +1126,7 @@
                     "Parcel count:", parcelCount);
             printRow(pw, TWO_COUNT_COLUMNS, "Death Recipients:", binderDeathObjectCount,
                     "OpenSSL Sockets:", openSslSocketCount);
+            printRow(pw, ONE_COUNT_COLUMN, "WebViews:", webviewInstanceCount);
 
             // SQLite mem info
             pw.println(" ");
@@ -1574,7 +1591,9 @@
                 case CONFIGURATION_CHANGED:
                     Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "configChanged");
                     mCurDefaultDisplayDpi = ((Configuration)msg.obj).densityDpi;
+                    mUpdatingSystemConfig = true;
                     handleConfigurationChanged((Configuration)msg.obj, null);
+                    mUpdatingSystemConfig = false;
                     Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                     break;
                 case CLEAN_UP_CONTEXT:
@@ -2784,24 +2803,34 @@
         }
     }
 
-    public final void performNewIntents(IBinder token, List<ReferrerIntent> intents) {
-        ActivityClientRecord r = mActivities.get(token);
-        if (r != null) {
-            final boolean resumed = !r.paused;
-            if (resumed) {
-                r.activity.mTemporaryPause = true;
-                mInstrumentation.callActivityOnPause(r.activity);
-            }
-            deliverNewIntents(r, intents);
-            if (resumed) {
-                r.activity.performResume();
-                r.activity.mTemporaryPause = false;
-            }
+    void performNewIntents(IBinder token, List<ReferrerIntent> intents, boolean andPause) {
+        final ActivityClientRecord r = mActivities.get(token);
+        if (r == null) {
+            return;
+        }
+
+        final boolean resumed = !r.paused;
+        if (resumed) {
+            r.activity.mTemporaryPause = true;
+            mInstrumentation.callActivityOnPause(r.activity);
+        }
+        deliverNewIntents(r, intents);
+        if (resumed) {
+            r.activity.performResume();
+            r.activity.mTemporaryPause = false;
+        }
+
+        if (r.paused && andPause) {
+            // In this case the activity was in the paused state when we delivered the intent,
+            // to guarantee onResume gets called after onNewIntent we temporarily resume the
+            // activity and pause again as the caller wanted.
+            performResumeActivity(token, false, "performNewIntents");
+            performPauseActivityIfNeeded(r, "performNewIntents");
         }
     }
 
     private void handleNewIntent(NewIntentData data) {
-        performNewIntents(data.token, data.intents);
+        performNewIntents(data.token, data.intents, data.andPause);
     }
 
     public void handleRequestAssistContextExtras(RequestAssistContextExtras cmd) {
@@ -3782,7 +3811,7 @@
      * than our client -- for the server, stop means to save state and give
      * it the result when it is done, but the window may still be visible.
      * For the client, we want to call onStop()/onStart() to indicate when
-     * the activity's UI visibillity changes.
+     * the activity's UI visibility changes.
      */
     private void performStopActivityInner(ActivityClientRecord r,
             StopInfo info, boolean keepShown, boolean saveState, String reason) {
@@ -3956,6 +3985,9 @@
         mSomeActivitiesChanged = true;
     }
 
+    // TODO: This method should be changed to use {@link #performStopActivityInner} to perform to
+    // stop operation on the activity to reduce code duplication and the chance of fixing a bug in
+    // one place and missing the other.
     private void handleSleeping(IBinder token, boolean sleeping) {
         ActivityClientRecord r = mActivities.get(token);
 
@@ -3966,6 +3998,10 @@
 
         if (sleeping) {
             if (!r.stopped && !r.isPreHoneycomb()) {
+                if (!r.activity.mFinished && r.state == null) {
+                    callCallActivityOnSaveInstanceState(r);
+                }
+
                 try {
                     // Now we are idle.
                     r.activity.performStop(false /*preserveWindow*/);
@@ -4624,12 +4660,20 @@
         if ((activity == null) || (activity.mCurrentConfig == null)) {
             shouldChangeConfig = true;
         } else {
-            // If the new config is the same as the config this Activity
-            // is already running with then don't bother calling
-            // onConfigurationChanged
+            // If the new config is the same as the config this Activity is already
+            // running with and the override config also didn't change, then don't
+            // bother calling onConfigurationChanged.
             int diff = activity.mCurrentConfig.diff(newConfig);
-            if (diff != 0) {
-                shouldChangeConfig = true;
+            if (diff != 0 || !mResourcesManager.isSameResourcesOverrideConfig(activityToken,
+                    amOverrideConfig)) {
+                // Always send the task-level config changes. For system-level configuration, if
+                // this activity doesn't handle any of the config changes, then don't bother
+                // calling onConfigurationChanged as we're going to destroy it.
+                if (!mUpdatingSystemConfig
+                        || (~activity.mActivityInfo.getRealConfigChanged() & diff) == 0
+                        || !reportToActivity) {
+                    shouldChangeConfig = true;
+                }
             }
         }
 
diff --git a/core/java/android/app/ActivityTransitionState.java b/core/java/android/app/ActivityTransitionState.java
index 02eb4d3..aef1d0c 100644
--- a/core/java/android/app/ActivityTransitionState.java
+++ b/core/java/android/app/ActivityTransitionState.java
@@ -185,7 +185,12 @@
             activity.getWindow().getDecorView().setVisibility(View.VISIBLE);
         }
         mEnterTransitionCoordinator = new EnterTransitionCoordinator(activity,
-                resultReceiver, sharedElementNames, mEnterActivityOptions.isReturning());
+                resultReceiver, sharedElementNames, mEnterActivityOptions.isReturning(),
+                mEnterActivityOptions.isCrossTask());
+        if (mEnterActivityOptions.isCrossTask()) {
+            mExitingFrom = new ArrayList<>(mEnterActivityOptions.getSharedElementNames());
+            mExitingTo = new ArrayList<>(mEnterActivityOptions.getSharedElementNames());
+        }
 
         if (!mIsEnterPostponed) {
             startEnter();
@@ -275,7 +280,8 @@
     }
 
     private void restoreReenteringViews() {
-        if (mEnterTransitionCoordinator != null && mEnterTransitionCoordinator.isReturning()) {
+        if (mEnterTransitionCoordinator != null && mEnterTransitionCoordinator.isReturning() &&
+                !mEnterTransitionCoordinator.isCrossTask()) {
             mEnterTransitionCoordinator.forceViewsToAppear();
             mExitingFrom = null;
             mExitingTo = null;
@@ -302,8 +308,9 @@
                     }
                 }
 
-                mReturnExitCoordinator =
-                        new ExitTransitionCoordinator(activity, mEnteringNames, null, null, true);
+                mReturnExitCoordinator = new ExitTransitionCoordinator(activity,
+                        activity.getWindow(), activity.mEnterTransitionListener, mEnteringNames,
+                        null, null, true);
                 if (enterViewsTransition != null && decor != null) {
                     enterViewsTransition.resume(decor);
                 }
@@ -330,11 +337,12 @@
     }
 
     public void startExitOutTransition(Activity activity, Bundle options) {
-        if (!activity.getWindow().hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)) {
+        mEnterTransitionCoordinator = null;
+        if (!activity.getWindow().hasFeature(Window.FEATURE_ACTIVITY_TRANSITIONS) ||
+                mExitTransitionCoordinators == null) {
             return;
         }
         ActivityOptions activityOptions = new ActivityOptions(options);
-        mEnterTransitionCoordinator = null;
         if (activityOptions.getAnimationType() == ActivityOptions.ANIM_SCENE_TRANSITION) {
             int key = activityOptions.getExitCoordinatorKey();
             int index = mExitTransitionCoordinators.indexOfKey(key);
diff --git a/core/java/android/app/AlertDialog.java b/core/java/android/app/AlertDialog.java
index 7824072..9928512 100644
--- a/core/java/android/app/AlertDialog.java
+++ b/core/java/android/app/AlertDialog.java
@@ -201,7 +201,7 @@
                 createContextThemeWrapper);
 
         mWindow.alwaysReadCloseOnTouchAttr();
-        mAlert = new AlertController(getContext(), this, getWindow());
+        mAlert = AlertController.create(getContext(), this, getWindow());
     }
 
     static int resolveDialogTheme(Context context, int themeResId) {
diff --git a/core/java/android/app/ApplicationErrorReport.java b/core/java/android/app/ApplicationErrorReport.java
index 8692336..9fa8a5d 100644
--- a/core/java/android/app/ApplicationErrorReport.java
+++ b/core/java/android/app/ApplicationErrorReport.java
@@ -345,7 +345,7 @@
             PrintWriter pw = new FastPrintWriter(sw, false, 256);
             tr.printStackTrace(pw);
             pw.flush();
-            stackTrace = sw.toString();
+            stackTrace = sanitizeString(sw.toString());
             exceptionMessage = tr.getMessage();
 
             // Populate fields with the "root cause" exception
@@ -374,6 +374,29 @@
                 throwMethodName = "unknown";
                 throwLineNumber = 0;
             }
+
+            exceptionMessage = sanitizeString(exceptionMessage);
+        }
+
+        /**
+         * Ensure that the string is of reasonable size, truncating from the middle if needed.
+         */
+        private String sanitizeString(String s) {
+            int prefixLength = 10 * 1024;
+            int suffixLength = 10 * 1024;
+            int acceptableLength = prefixLength + suffixLength;
+
+            if (s != null && s.length() > acceptableLength) {
+                String replacement =
+                        "\n[TRUNCATED " + (s.length() - acceptableLength) + " CHARS]\n";
+
+                StringBuilder sb = new StringBuilder(acceptableLength + replacement.length());
+                sb.append(s.substring(0, prefixLength));
+                sb.append(replacement);
+                sb.append(s.substring(s.length() - suffixLength));
+                return sb.toString();
+            }
+            return s;
         }
 
         /**
diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java
index 8cc1bc4..37faa2e 100644
--- a/core/java/android/app/ApplicationPackageManager.java
+++ b/core/java/android/app/ApplicationPackageManager.java
@@ -1245,18 +1245,16 @@
             return mContext.mMainThread.getSystemContext().getResources();
         }
         final boolean sameUid = (app.uid == Process.myUid());
-        try {
-            return mContext.mMainThread.getTopLevelResources(
+        final Resources r = mContext.mMainThread.getTopLevelResources(
                     sameUid ? app.sourceDir : app.publicSourceDir,
                     sameUid ? app.splitSourceDirs : app.splitPublicSourceDirs,
                     app.resourceDirs, app.sharedLibraryFiles, Display.DEFAULT_DISPLAY,
                     mContext.mPackageInfo);
-        } catch (Resources.NotFoundException cause) {
-            final NameNotFoundException ex =
-                    new NameNotFoundException("Unable to open " + app.publicSourceDir);
-            ex.initCause(cause);
-            throw ex;
+        if (r != null) {
+            return r;
         }
+        throw new NameNotFoundException("Unable to open " + app.publicSourceDir);
+
     }
 
     @Override
diff --git a/core/java/android/app/ApplicationThreadNative.java b/core/java/android/app/ApplicationThreadNative.java
index d6da3f4..05d9d7e 100644
--- a/core/java/android/app/ApplicationThreadNative.java
+++ b/core/java/android/app/ApplicationThreadNative.java
@@ -190,7 +190,8 @@
             data.enforceInterface(IApplicationThread.descriptor);
             List<ReferrerIntent> pi = data.createTypedArrayList(ReferrerIntent.CREATOR);
             IBinder b = data.readStrongBinder();
-            scheduleNewIntent(pi, b);
+            final boolean andPause = data.readInt() == 1;
+            scheduleNewIntent(pi, b, andPause);
             return true;
         }
 
@@ -909,12 +910,13 @@
         data.recycle();
     }
 
-    public void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token)
+    public void scheduleNewIntent(List<ReferrerIntent> intents, IBinder token, boolean andPause)
             throws RemoteException {
         Parcel data = Parcel.obtain();
         data.writeInterfaceToken(IApplicationThread.descriptor);
         data.writeTypedList(intents);
         data.writeStrongBinder(token);
+        data.writeInt(andPause ? 1 : 0);
         mRemote.transact(SCHEDULE_NEW_INTENT_TRANSACTION, data, null,
                 IBinder.FLAG_ONEWAY);
         data.recycle();
diff --git a/core/java/android/app/BackStackRecord.java b/core/java/android/app/BackStackRecord.java
index 1e2cc26..a8d332b 100644
--- a/core/java/android/app/BackStackRecord.java
+++ b/core/java/android/app/BackStackRecord.java
@@ -17,6 +17,7 @@
 package android.app;
 
 import android.graphics.Rect;
+import android.os.Build;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.text.TextUtils;
@@ -852,7 +853,9 @@
              * Ensure that fragments that are entering are at least at the CREATED state
              * so that they may load Transitions using TransitionInflater.
              */
-            if (fragment.mState < Fragment.CREATED && mManager.mCurState >= Fragment.CREATED) {
+            if (fragment.mState < Fragment.CREATED && mManager.mCurState >= Fragment.CREATED &&
+                    mManager.mHost.getContext().getApplicationInfo().targetSdkVersion >=
+                    Build.VERSION_CODES.N) {
                 mManager.makeActive(fragment);
                 mManager.moveToState(fragment, Fragment.CREATED, 0, 0, false);
             }
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java
index e6ca520..8f42467 100644
--- a/core/java/android/app/ContextImpl.java
+++ b/core/java/android/app/ContextImpl.java
@@ -185,15 +185,6 @@
     @GuardedBy("mSync")
     private File mCodeCacheDir;
 
-    @GuardedBy("mSync")
-    private File[] mExternalObbDirs;
-    @GuardedBy("mSync")
-    private File[] mExternalFilesDirs;
-    @GuardedBy("mSync")
-    private File[] mExternalCacheDirs;
-    @GuardedBy("mSync")
-    private File[] mExternalMediaDirs;
-
     // The system service cache for the system services that are cached per-ContextImpl.
     final Object[] mServiceCache = SystemServiceRegistry.createServiceCache();
 
@@ -562,17 +553,10 @@
     @Override
     public File[] getExternalFilesDirs(String type) {
         synchronized (mSync) {
-            if (mExternalFilesDirs == null) {
-                mExternalFilesDirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
-            }
-
-            // Splice in requested type, if any
-            File[] dirs = mExternalFilesDirs;
+            File[] dirs = Environment.buildExternalStorageAppFilesDirs(getPackageName());
             if (type != null) {
                 dirs = Environment.buildPaths(dirs, type);
             }
-
-            // Create dirs if needed
             return ensureExternalDirsExistOrFilter(dirs);
         }
     }
@@ -586,12 +570,8 @@
     @Override
     public File[] getObbDirs() {
         synchronized (mSync) {
-            if (mExternalObbDirs == null) {
-                mExternalObbDirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
-            }
-
-            // Create dirs if needed
-            return ensureExternalDirsExistOrFilter(mExternalObbDirs);
+            File[] dirs = Environment.buildExternalStorageAppObbDirs(getPackageName());
+            return ensureExternalDirsExistOrFilter(dirs);
         }
     }
 
@@ -624,24 +604,16 @@
     @Override
     public File[] getExternalCacheDirs() {
         synchronized (mSync) {
-            if (mExternalCacheDirs == null) {
-                mExternalCacheDirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
-            }
-
-            // Create dirs if needed
-            return ensureExternalDirsExistOrFilter(mExternalCacheDirs);
+            File[] dirs = Environment.buildExternalStorageAppCacheDirs(getPackageName());
+            return ensureExternalDirsExistOrFilter(dirs);
         }
     }
 
     @Override
     public File[] getExternalMediaDirs() {
         synchronized (mSync) {
-            if (mExternalMediaDirs == null) {
-                mExternalMediaDirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
-            }
-
-            // Create dirs if needed
-            return ensureExternalDirsExistOrFilter(mExternalMediaDirs);
+            File[] dirs = Environment.buildExternalStorageAppMediaDirs(getPackageName());
+            return ensureExternalDirsExistOrFilter(dirs);
         }
     }
 
diff --git a/core/java/android/app/Dialog.java b/core/java/android/app/Dialog.java
index 85a0403..6e2c464 100644
--- a/core/java/android/app/Dialog.java
+++ b/core/java/android/app/Dialog.java
@@ -32,6 +32,7 @@
 import android.content.Context;
 import android.content.ContextWrapper;
 import android.content.DialogInterface;
+import android.content.res.Configuration;
 import android.content.pm.ApplicationInfo;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
@@ -288,9 +289,14 @@
         }
 
         mCanceled = false;
-        
+
         if (!mCreated) {
             dispatchOnCreate(null);
+        } else {
+            // Fill the DecorView in on any configuration changes that
+            // may have occured while it was removed from the WindowManager.
+            final Configuration config = mContext.getResources().getConfiguration();
+            mWindow.getDecorView().dispatchConfigurationChanged(config);
         }
 
         onStart();
diff --git a/core/java/android/app/EnterTransitionCoordinator.java b/core/java/android/app/EnterTransitionCoordinator.java
index 8bf1e9a..5d12b0d 100644
--- a/core/java/android/app/EnterTransitionCoordinator.java
+++ b/core/java/android/app/EnterTransitionCoordinator.java
@@ -59,12 +59,14 @@
     private boolean mIsViewsTransitionStarted;
     private Transition mEnterViewsTransition;
     private OnPreDrawListener mViewsReadyListener;
+    private final boolean mIsCrossTask;
 
     public EnterTransitionCoordinator(Activity activity, ResultReceiver resultReceiver,
-            ArrayList<String> sharedElementNames, boolean isReturning) {
+            ArrayList<String> sharedElementNames, boolean isReturning, boolean isCrossTask) {
         super(activity.getWindow(), sharedElementNames,
-                getListener(activity, isReturning), isReturning);
+                getListener(activity, isReturning && !isCrossTask), isReturning);
         mActivity = activity;
+        mIsCrossTask = isCrossTask;
         setResultReceiver(resultReceiver);
         prepareEnter();
         Bundle resultReceiverBundle = new Bundle();
@@ -85,6 +87,10 @@
         }
     }
 
+    boolean isCrossTask() {
+        return mIsCrossTask;
+    }
+
     public void viewInstancesReady(ArrayList<String> accepted, ArrayList<String> localNames,
             ArrayList<View> localViews) {
         boolean remap = false;
@@ -325,7 +331,9 @@
         if (mActivity == null || decorView == null) {
             return;
         }
-        mActivity.overridePendingTransition(0, 0);
+        if (!isCrossTask()) {
+            mActivity.overridePendingTransition(0, 0);
+        }
         if (!mIsReturning) {
             mWasOpaque = mActivity.convertToTranslucent(null, null);
             Drawable background = decorView.getBackground();
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/java/android/app/EphemeralResolveInfo.aidl
similarity index 94%
rename from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
rename to core/java/android/app/EphemeralResolveInfo.aidl
index 529527b..db71d25 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/java/android/app/EphemeralResolveInfo.aidl
@@ -14,6 +14,6 @@
 ** limitations under the License.
 */
 
-package com.android.internal.app;
+package android.app;
 
 parcelable EphemeralResolveInfo;
diff --git a/core/java/com/android/internal/app/EphemeralResolverService.java b/core/java/android/app/EphemeralResolverService.java
similarity index 69%
rename from core/java/com/android/internal/app/EphemeralResolverService.java
rename to core/java/android/app/EphemeralResolverService.java
index 6ba04a9..ba79108 100644
--- a/core/java/com/android/internal/app/EphemeralResolverService.java
+++ b/core/java/android/app/EphemeralResolverService.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.internal.app;
+package android.app;
 
 import android.annotation.SystemApi;
 import android.app.Service;
@@ -37,19 +37,24 @@
  */
 @SystemApi
 public abstract class EphemeralResolverService extends Service {
-    public static final String EXTRA_RESOLVE_INFO = "com.android.internal.app.RESOLVE_INFO";
-    public static final String EXTRA_SEQUENCE = "com.android.internal.app.SEQUENCE";
+    public static final String EXTRA_RESOLVE_INFO = "android.app.extra.RESOLVE_INFO";
+    public static final String EXTRA_SEQUENCE = "android.app.extra.SEQUENCE";
+    private static final String EXTRA_PREFIX = "android.app.PREFIX";
     private Handler mHandler;
 
     /**
      * Called to retrieve resolve info for ephemeral applications.
      *
      * @param digestPrefix The hash prefix of the ephemeral's domain.
+     * @param prefixMask A mask that was applied to each digest prefix. This should
+     *      be used when comparing against the digest prefixes as all bits might
+     *      not be set.
      */
-    protected abstract List<EphemeralResolveInfo> getEphemeralResolveInfoList(int digestPrefix);
+    public abstract List<EphemeralResolveInfo> onEphemeralResolveInfoList(
+            int digestPrefix[], int prefixMask);
 
     @Override
-    protected final void attachBaseContext(Context base) {
+    public final void attachBaseContext(Context base) {
         super.attachBaseContext(base);
         mHandler = new ServiceHandler(base.getMainLooper());
     }
@@ -59,10 +64,13 @@
         return new IEphemeralResolver.Stub() {
             @Override
             public void getEphemeralResolveInfoList(
-                    IRemoteCallback callback, int digestPrefix, int sequence) {
-                mHandler.obtainMessage(ServiceHandler.MSG_GET_EPHEMERAL_RESOLVE_INFO,
-                        digestPrefix, sequence, callback)
-                    .sendToTarget();
+                    IRemoteCallback callback, int digestPrefix[], int prefixMask, int sequence) {
+                final Message msg = mHandler.obtainMessage(
+                        ServiceHandler.MSG_GET_EPHEMERAL_RESOLVE_INFO, prefixMask, sequence, callback);
+                final Bundle data = new Bundle();
+                data.putIntArray(EXTRA_PREFIX, digestPrefix);
+                msg.setData(data);
+                msg.sendToTarget();
             }
         };
     }
@@ -81,8 +89,9 @@
             switch (action) {
                 case MSG_GET_EPHEMERAL_RESOLVE_INFO: {
                     final IRemoteCallback callback = (IRemoteCallback) message.obj;
+                    final int[] digestPrefix = message.getData().getIntArray(EXTRA_PREFIX);
                     final List<EphemeralResolveInfo> resolveInfo =
-                            getEphemeralResolveInfoList(message.arg1);
+                            onEphemeralResolveInfoList(digestPrefix, message.arg1);
                     final Bundle data = new Bundle();
                     data.putInt(EXTRA_SEQUENCE, message.arg2);
                     data.putParcelableList(EXTRA_RESOLVE_INFO, resolveInfo);
diff --git a/core/java/android/app/ExitTransitionCoordinator.java b/core/java/android/app/ExitTransitionCoordinator.java
index 0404288..160c285 100644
--- a/core/java/android/app/ExitTransitionCoordinator.java
+++ b/core/java/android/app/ExitTransitionCoordinator.java
@@ -35,6 +35,7 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
+import android.view.Window;
 
 import java.util.ArrayList;
 
@@ -59,18 +60,20 @@
     private Bundle mExitSharedElementBundle;
     private boolean mIsExitStarted;
     private boolean mSharedElementsHidden;
+    private HideSharedElementsCallback mHideSharedElementsCallback;
 
-    public ExitTransitionCoordinator(Activity activity, ArrayList<String> names,
+    public ExitTransitionCoordinator(Activity activity, Window window,
+            SharedElementCallback listener, ArrayList<String> names,
             ArrayList<String> accepted, ArrayList<View> mapped, boolean isReturning) {
-        super(activity.getWindow(), names, getListener(activity, isReturning), isReturning);
+        super(window, names, listener, isReturning);
         viewsReady(mapSharedElements(accepted, mapped));
         stripOffscreenViews();
         mIsBackgroundReady = !isReturning;
         mActivity = activity;
     }
 
-    private static SharedElementCallback getListener(Activity activity, boolean isReturning) {
-        return isReturning ? activity.mEnterTransitionListener : activity.mExitTransitionListener;
+    void setHideSharedElementsCallback(HideSharedElementsCallback callback) {
+        mHideSharedElementsCallback = callback;
     }
 
     @Override
@@ -188,6 +191,9 @@
 
     private void hideSharedElements() {
         moveSharedElementsFromOverlay();
+        if (mHideSharedElementsCallback != null) {
+            mHideSharedElementsCallback.hideSharedElements();
+        }
         if (!mIsHidden) {
             hideViews(mSharedElements);
         }
@@ -207,7 +213,11 @@
             startTransition(new Runnable() {
                 @Override
                 public void run() {
-                    beginTransitions();
+                    if (mActivity != null) {
+                        beginTransitions();
+                    } else {
+                        startExitTransition();
+                    }
                 }
             });
         }
@@ -508,4 +518,8 @@
             return getWindow().getSharedElementExitTransition();
         }
     }
+
+    interface HideSharedElementsCallback {
+        void hideSharedElements();
+    }
 }
diff --git a/core/java/android/app/FragmentHostCallback.java b/core/java/android/app/FragmentHostCallback.java
index e1d7136..d869168 100644
--- a/core/java/android/app/FragmentHostCallback.java
+++ b/core/java/android/app/FragmentHostCallback.java
@@ -340,6 +340,11 @@
     }
 
     void restoreLoaderNonConfig(ArrayMap<String, LoaderManager> loaderManagers) {
+        if (loaderManagers != null) {
+            for (int i = 0, N = loaderManagers.size(); i < N; i++) {
+                ((LoaderManagerImpl) loaderManagers.valueAt(i)).updateHostController(this);
+            }
+        }
         mAllLoaderManagers = loaderManagers;
     }
 
diff --git a/core/java/android/app/IActivityManager.java b/core/java/android/app/IActivityManager.java
index 80ba3eb..5a4470b 100644
--- a/core/java/android/app/IActivityManager.java
+++ b/core/java/android/app/IActivityManager.java
@@ -512,7 +512,8 @@
     public int getLaunchedFromUid(IBinder activityToken) throws RemoteException;
     public String getLaunchedFromPackage(IBinder activityToken) throws RemoteException;
 
-    public void registerUserSwitchObserver(IUserSwitchObserver observer) throws RemoteException;
+    public void registerUserSwitchObserver(IUserSwitchObserver observer,
+            String name) throws RemoteException;
     public void unregisterUserSwitchObserver(IUserSwitchObserver observer) throws RemoteException;
 
     public void requestBugReport(int bugreportType) throws RemoteException;
@@ -657,6 +658,31 @@
             IIntentReceiver finishedReceiver, String requiredPermission, Bundle options)
             throws RemoteException;
 
+    public void setVrThread(int tid) throws RemoteException;
+    public void setRenderThread(int tid) throws RemoteException;
+
+    /**
+     * Lets activity manager know whether the calling process is currently showing "top-level" UI
+     * that is not an activity, i.e. windows on the screen the user is currently interacting with.
+     *
+     * <p>This flag can only be set for persistent processes.
+     *
+     * @param hasTopUi Whether the calling process has "top-level" UI.
+     */
+    public void setHasTopUi(boolean hasTopUi) throws RemoteException;
+
+    /**
+     * Returns if the target of the PendingIntent can be fired directly, without triggering
+     * a work profile challenge. This can happen if the PendingIntent is to start direct-boot
+     * aware activities, and the target user is in RUNNING_LOCKED state, i.e. we should allow
+     * direct-boot aware activity to bypass work challenge when the user hasn't unlocked yet.
+     * @param intent the {@link  PendingIntent} to be tested.
+     * @return {@code true} if the intent should not trigger a work challenge, {@code false}
+     *     otherwise.
+     * @throws RemoteException
+     */
+    public boolean canBypassWorkChallenge(PendingIntent intent) throws RemoteException;
+
     /*
      * Private non-Binder interfaces
      */
@@ -1043,4 +1069,10 @@
     int START_CONFIRM_DEVICE_CREDENTIAL_INTENT = IBinder.FIRST_CALL_TRANSACTION + 374;
     int SEND_IDLE_JOB_TRIGGER_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 375;
     int SEND_INTENT_SENDER_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 376;
+
+    // Start of N MR1 transactions
+    int SET_VR_THREAD_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 377;
+    int SET_RENDER_THREAD_TRANSACTION = IBinder.FIRST_CALL_TRANSACTION + 378;
+    int SET_HAS_TOP_UI = IBinder.FIRST_CALL_TRANSACTION + 379;
+    int CAN_BYPASS_WORK_CHALLENGE = IBinder.FIRST_CALL_TRANSACTION + 380;
 }
diff --git a/core/java/android/app/IApplicationThread.java b/core/java/android/app/IApplicationThread.java
index 559f69f..3fa88ae 100644
--- a/core/java/android/app/IApplicationThread.java
+++ b/core/java/android/app/IApplicationThread.java
@@ -67,7 +67,8 @@
             List<ReferrerIntent> pendingNewIntents, int configChanges, boolean notResumed,
             Configuration config, Configuration overrideConfig, boolean preserveWindow)
             throws RemoteException;
-    void scheduleNewIntent(List<ReferrerIntent> intent, IBinder token) throws RemoteException;
+    void scheduleNewIntent(
+            List<ReferrerIntent> intent, IBinder token, boolean andPause) throws RemoteException;
     void scheduleDestroyActivity(IBinder token, boolean finished,
             int configChanges) throws RemoteException;
     void scheduleReceiver(Intent intent, ActivityInfo info, CompatibilityInfo compatInfo,
diff --git a/core/java/com/android/internal/app/IEphemeralResolver.aidl b/core/java/android/app/IEphemeralResolver.aidl
similarity index 88%
rename from core/java/com/android/internal/app/IEphemeralResolver.aidl
rename to core/java/android/app/IEphemeralResolver.aidl
index 40429ee..ee869ea 100644
--- a/core/java/com/android/internal/app/IEphemeralResolver.aidl
+++ b/core/java/android/app/IEphemeralResolver.aidl
@@ -14,11 +14,12 @@
  * limitations under the License.
  */
 
-package com.android.internal.app;
+package android.app;
 
-import android.content.Intent;
 import android.os.IRemoteCallback;
 
+/** @hide */
 oneway interface IEphemeralResolver {
-    void getEphemeralResolveInfoList(IRemoteCallback callback, int digestPrefix, int sequence);
+    void getEphemeralResolveInfoList(IRemoteCallback callback, in int[] digestPrefix,
+            int prefixMask, int sequence);
 }
diff --git a/core/java/android/app/ITransientNotification.aidl b/core/java/android/app/ITransientNotification.aidl
index 35b53a4..d5b3ed0 100644
--- a/core/java/android/app/ITransientNotification.aidl
+++ b/core/java/android/app/ITransientNotification.aidl
@@ -19,7 +19,7 @@
 
 /** @hide */
 oneway interface ITransientNotification {
-    void show();
+    void show(IBinder windowToken);
     void hide();
 }
 
diff --git a/core/java/android/app/IWallpaperManager.aidl b/core/java/android/app/IWallpaperManager.aidl
index a0762b9..75a5bf7 100644
--- a/core/java/android/app/IWallpaperManager.aidl
+++ b/core/java/android/app/IWallpaperManager.aidl
@@ -44,12 +44,12 @@
      */
     ParcelFileDescriptor setWallpaper(String name, in String callingPackage,
             in Rect cropHint, boolean allowBackup, out Bundle extras, int which,
-            IWallpaperManagerCallback completion);
+            IWallpaperManagerCallback completion, int userId);
 
     /**
      * Set the live wallpaper. This only affects the system wallpaper.
      */
-    void setWallpaperComponentChecked(in ComponentName name, in String callingPackage);
+    void setWallpaperComponentChecked(in ComponentName name, in String callingPackage, int userId);
 
     /**
      * Set the live wallpaper. This only affects the system wallpaper.
@@ -72,7 +72,7 @@
      * information about that wallpaper.  Otherwise, if it is a static image,
      * simply return null.
      */
-    WallpaperInfo getWallpaperInfo();
+    WallpaperInfo getWallpaperInfo(int userId);
 
     /**
      * Clear the system wallpaper.
@@ -128,7 +128,7 @@
     /*
      * Backup: is the current system wallpaper image eligible for off-device backup?
      */
-    boolean isWallpaperBackupEligible(int userId);
+    boolean isWallpaperBackupEligible(int which, int userId);
 
     /*
      * Keyguard: register a callback for being notified that lock-state relevant
diff --git a/core/java/android/app/LoadedApk.java b/core/java/android/app/LoadedApk.java
index b889c8f..7754244 100644
--- a/core/java/android/app/LoadedApk.java
+++ b/core/java/android/app/LoadedApk.java
@@ -1396,18 +1396,6 @@
         }
 
         public void death(ComponentName name, IBinder service) {
-            ServiceDispatcher.ConnectionInfo old;
-
-            synchronized (this) {
-                old = mActiveConnections.remove(name);
-                if (old == null || old.binder != service) {
-                    // Death for someone different than who we last
-                    // reported...  just ignore it.
-                    return;
-                }
-                old.binder.unlinkToDeath(old.deathMonitor, 0);
-            }
-
             if (mActivityThread != null) {
                 mActivityThread.post(new RunConnection(name, service, 1));
             } else {
@@ -1456,7 +1444,7 @@
                 }
             }
 
-            // If there was an old service, it is not disconnected.
+            // If there was an old service, it is now disconnected.
             if (old != null) {
                 mConnection.onServiceDisconnected(name);
             }
@@ -1467,6 +1455,17 @@
         }
 
         public void doDeath(ComponentName name, IBinder service) {
+            synchronized (this) {
+                ConnectionInfo old = mActiveConnections.get(name);
+                if (old == null || old.binder != service) {
+                    // Death for someone different than who we last
+                    // reported...  just ignore it.
+                    return;
+                }
+                mActiveConnections.remove(name);
+                old.binder.unlinkToDeath(old.deathMonitor, 0);
+            }
+
             mConnection.onServiceDisconnected(name);
         }
 
diff --git a/core/java/android/app/LoaderManager.java b/core/java/android/app/LoaderManager.java
index c14dec9..bedf31a 100644
--- a/core/java/android/app/LoaderManager.java
+++ b/core/java/android/app/LoaderManager.java
@@ -195,6 +195,9 @@
     public static void enableDebugLogging(boolean enabled) {
         LoaderManagerImpl.DEBUG = enabled;
     }
+
+    /** @hide for internal testing only */
+    public FragmentHostCallback getFragmentHostCallback() { return null; }
 }
 
 class LoaderManagerImpl extends LoaderManager {
@@ -542,6 +545,10 @@
     void updateHostController(FragmentHostCallback host) {
         mHost = host;
     }
+
+    public FragmentHostCallback getFragmentHostCallback() {
+        return mHost;
+    }
     
     private LoaderInfo createLoader(int id, Bundle args,
             LoaderManager.LoaderCallbacks<Object> callback) {
diff --git a/core/java/android/app/LocalActivityManager.java b/core/java/android/app/LocalActivityManager.java
index 2a1e3c2..3b273bc 100644
--- a/core/java/android/app/LocalActivityManager.java
+++ b/core/java/android/app/LocalActivityManager.java
@@ -314,7 +314,7 @@
                     ArrayList<ReferrerIntent> intents = new ArrayList<>(1);
                     intents.add(new ReferrerIntent(intent, mParent.getPackageName()));
                     if (localLOGV) Log.v(TAG, r.id + ": new intent");
-                    mActivityThread.performNewIntents(r, intents);
+                    mActivityThread.performNewIntents(r, intents, false /* andPause */);
                     r.intent = intent;
                     moveToState(r, mCurState);
                     if (mSingleMode) {
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index 35c49b3..1103d9e 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -19,6 +19,7 @@
 import android.annotation.ColorInt;
 import android.annotation.DrawableRes;
 import android.annotation.IntDef;
+import android.annotation.NonNull;
 import android.annotation.SdkConstant;
 import android.annotation.SdkConstant.SdkConstantType;
 import android.annotation.SystemApi;
@@ -39,7 +40,6 @@
 import android.media.session.MediaSession;
 import android.net.Uri;
 import android.os.BadParcelableException;
-import android.os.BaseBundle;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Parcel;
@@ -51,7 +51,9 @@
 import android.text.Spanned;
 import android.text.TextUtils;
 import android.text.style.AbsoluteSizeSpan;
+import android.text.style.BackgroundColorSpan;
 import android.text.style.CharacterStyle;
+import android.text.style.ForegroundColorSpan;
 import android.text.style.RelativeSizeSpan;
 import android.text.style.TextAppearanceSpan;
 import android.util.ArraySet;
@@ -73,7 +75,6 @@
 import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
@@ -1229,7 +1230,7 @@
                     getIcon(),
                     title,
                     actionIntent, // safe to alias
-                    new Bundle(mExtras),
+                    mExtras == null ? new Bundle() : new Bundle(mExtras),
                     getRemoteInputs(),
                     getAllowGeneratedReplies());
         }
@@ -1307,6 +1308,7 @@
             // Flags bitwise-ored to mFlags
             private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
             private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
+            private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
 
             // Default value for flags integer
             private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
@@ -1493,6 +1495,29 @@
             public boolean getHintLaunchesActivity() {
                 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
             }
+
+            /**
+             * Set a hint that this Action should be displayed inline.
+             *
+             * @param hintDisplayInline {@code true} if action should be displayed inline, false
+             *        otherwise
+             * @return this object for method chaining
+             */
+            public WearableExtender setHintDisplayActionInline(
+                    boolean hintDisplayInline) {
+                setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
+                return this;
+            }
+
+            /**
+             * Get a hint that this Action should be displayed inline.
+             *
+             * @return {@code true} if the Action should be displayed inline, {@code false} 
+             *         otherwise. The default value is {@code false} if this was never set.
+             */
+            public boolean getHintDisplayActionInline() {
+                return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
+            }
         }
     }
 
@@ -3232,7 +3257,8 @@
          * Resets the notification header to its original state
          */
         private void resetNotificationHeader(RemoteViews contentView) {
-            contentView.setImageViewResource(R.id.icon, 0);
+            // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
+            // re-using the drawable when the notification is updated.
             contentView.setBoolean(R.id.notification_header, "setExpanded", false);
             contentView.setTextViewText(R.id.app_name_text, null);
             contentView.setViewVisibility(R.id.chronometer, View.GONE);
@@ -3450,6 +3476,8 @@
                 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
             }
             contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
+            contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
+                    -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
             processSmallIconColor(mN.mSmallIcon, contentView);
         }
 
@@ -3495,6 +3523,8 @@
             boolean validRemoteInput = false;
 
             int N = mActions.size();
+            boolean emphazisedMode = mN.fullScreenIntent != null;
+            big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
             if (N > 0) {
                 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
                 big.setViewVisibility(R.id.actions, View.VISIBLE);
@@ -3505,7 +3535,8 @@
                     Action action = mActions.get(i);
                     validRemoteInput |= hasValidRemoteInput(action);
 
-                    final RemoteViews button = generateActionButton(action);
+                    final RemoteViews button = generateActionButton(action, emphazisedMode,
+                            i % 2 != 0);
                     big.addView(R.id.actions, button);
                 }
             } else {
@@ -3670,13 +3701,13 @@
 
 
 
-        private RemoteViews generateActionButton(Action action) {
+        private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
+                boolean oddAction) {
             final boolean tombstone = (action.actionIntent == null);
             RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
-                    tombstone ? getActionTombstoneLayoutResource()
-                              : getActionLayoutResource());
-            final Icon ai = action.getIcon();
-            button.setTextViewText(R.id.action0, processLegacyText(action.title));
+                    emphazisedMode ? getEmphasizedActionLayoutResource()
+                            : tombstone ? getActionTombstoneLayoutResource()
+                                    : getActionLayoutResource());
             if (!tombstone) {
                 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
             }
@@ -3684,13 +3715,144 @@
             if (action.mRemoteInputs != null) {
                 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
             }
-            if (mN.color != COLOR_DEFAULT) {
-                button.setTextColor(R.id.action0, resolveContrastColor());
+            if (emphazisedMode) {
+                // change the background bgColor
+                int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
+                        : R.color.notification_action_list_dark);
+                button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
+                        PorterDuff.Mode.SRC_ATOP, -1);
+                CharSequence title = action.title;
+                ColorStateList[] outResultColor = null;
+                if (isLegacy()) {
+                    title = clearColorSpans(title);
+                } else {
+                    outResultColor = new ColorStateList[1];
+                    title = ensureColorSpanContrast(title, bgColor, outResultColor);
+                }
+                button.setTextViewText(R.id.action0, title);
+                if (outResultColor != null && outResultColor[0] != null) {
+                    // We need to set the text color as well since changing a text to uppercase
+                    // clears its spans.
+                    button.setTextColor(R.id.action0, outResultColor[0]);
+                } else if (mN.color != COLOR_DEFAULT) {
+                    button.setTextColor(R.id.action0,resolveContrastColor());
+                }
+            } else {
+                button.setTextViewText(R.id.action0, processLegacyText(action.title));
+                if (mN.color != COLOR_DEFAULT) {
+                    button.setTextColor(R.id.action0, resolveContrastColor());
+                }
             }
             return button;
         }
 
         /**
+         * Clears all color spans of a text
+         * @param charSequence the input text
+         * @return the same text but without color spans
+         */
+        private CharSequence clearColorSpans(CharSequence charSequence) {
+            if (charSequence instanceof Spanned) {
+                Spanned ss = (Spanned) charSequence;
+                Object[] spans = ss.getSpans(0, ss.length(), Object.class);
+                SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
+                for (Object span : spans) {
+                    Object resultSpan = span;
+                    if (resultSpan instanceof CharacterStyle) {
+                        resultSpan = ((CharacterStyle) span).getUnderlying();
+                    }
+                    if (resultSpan instanceof TextAppearanceSpan) {
+                        TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
+                        if (originalSpan.getTextColor() != null) {
+                            resultSpan = new TextAppearanceSpan(
+                                    originalSpan.getFamily(),
+                                    originalSpan.getTextStyle(),
+                                    originalSpan.getTextSize(),
+                                    null,
+                                    originalSpan.getLinkTextColor());
+                        }
+                    } else if (resultSpan instanceof ForegroundColorSpan
+                            || (resultSpan instanceof BackgroundColorSpan)) {
+                        continue;
+                    } else {
+                        resultSpan = span;
+                    }
+                    builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
+                            ss.getSpanFlags(span));
+                }
+                return builder;
+            }
+            return charSequence;
+        }
+
+        /**
+         * Ensures contrast on color spans against a background color. also returns the color of the
+         * text if a span was found that spans over the whole text.
+         *
+         * @param charSequence the charSequence on which the spans are
+         * @param background the background color to ensure the contrast against
+         * @param outResultColor an array in which a color will be returned as the first element if
+         *                    there exists a full length color span.
+         * @return the contrasted charSequence
+         */
+        private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
+                ColorStateList[] outResultColor) {
+            if (charSequence instanceof Spanned) {
+                Spanned ss = (Spanned) charSequence;
+                Object[] spans = ss.getSpans(0, ss.length(), Object.class);
+                SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
+                for (Object span : spans) {
+                    Object resultSpan = span;
+                    int spanStart = ss.getSpanStart(span);
+                    int spanEnd = ss.getSpanEnd(span);
+                    boolean fullLength = (spanEnd - spanStart) == charSequence.length();
+                    if (resultSpan instanceof CharacterStyle) {
+                        resultSpan = ((CharacterStyle) span).getUnderlying();
+                    }
+                    if (resultSpan instanceof TextAppearanceSpan) {
+                        TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
+                        ColorStateList textColor = originalSpan.getTextColor();
+                        if (textColor != null) {
+                            int[] colors = textColor.getColors();
+                            int[] newColors = new int[colors.length];
+                            for (int i = 0; i < newColors.length; i++) {
+                                newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
+                                        colors[i], background);
+                            }
+                            textColor = new ColorStateList(textColor.getStates().clone(),
+                                    newColors);
+                            resultSpan = new TextAppearanceSpan(
+                                    originalSpan.getFamily(),
+                                    originalSpan.getTextStyle(),
+                                    originalSpan.getTextSize(),
+                                    textColor,
+                                    originalSpan.getLinkTextColor());
+                            if (fullLength) {
+                                outResultColor[0] = new ColorStateList(
+                                        textColor.getStates().clone(), newColors);
+                            }
+                        }
+                    } else if (resultSpan instanceof ForegroundColorSpan) {
+                        ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
+                        int foregroundColor = originalSpan.getForegroundColor();
+                        foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
+                                foregroundColor, background);
+                        resultSpan = new ForegroundColorSpan(foregroundColor);
+                        if (fullLength) {
+                            outResultColor[0] = ColorStateList.valueOf(foregroundColor);
+                        }
+                    } else {
+                        resultSpan = span;
+                    }
+
+                    builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
+                }
+                return builder;
+            }
+            return charSequence;
+        }
+
+        /**
          * @return Whether we are currently building a notification from a legacy (an app that
          *         doesn't create material notifications by itself) app.
          */
@@ -3955,6 +4117,10 @@
             return R.layout.notification_material_action;
         }
 
+        private int getEmphasizedActionLayoutResource() {
+            return R.layout.notification_material_action_emphasized;
+        }
+
         private int getActionTombstoneLayoutResource() {
             return R.layout.notification_material_action_tombstone;
         }
@@ -4260,9 +4426,15 @@
             //          mN.mLargeIcon
             //   2. !mBigLargeIconSet -> mN.mLargeIcon applies
             Icon oldLargeIcon = null;
+            Bitmap largeIconLegacy = null;
             if (mBigLargeIconSet) {
                 oldLargeIcon = mBuilder.mN.mLargeIcon;
                 mBuilder.mN.mLargeIcon = mBigLargeIcon;
+                // The legacy largeIcon might not allow us to clear the image, as it's taken in
+                // replacement if the other one is null. Because we're restoring these legacy icons
+                // for old listeners, this is in general non-null.
+                largeIconLegacy = mBuilder.mN.largeIcon;
+                mBuilder.mN.largeIcon = null;
             }
 
             RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
@@ -4274,6 +4446,7 @@
 
             if (mBigLargeIconSet) {
                 mBuilder.mN.mLargeIcon = oldLargeIcon;
+                mBuilder.mN.largeIcon = largeIconLegacy;
             }
 
             contentView.setImageViewBitmap(R.id.big_picture, mPicture);
@@ -4477,12 +4650,12 @@
         }
 
         /**
-         * @param userDisplayName the name to be displayed for any replies sent by the user before the
-         * posting app reposts the notification with those messages after they've been actually
-         * sent and in previous messages sent by the user added in
+         * @param userDisplayName Required - the name to be displayed for any replies sent by the
+         * user before the posting app reposts the notification with those messages after they've
+         * been actually sent and in previous messages sent by the user added in
          * {@link #addMessage(Notification.MessagingStyle.Message)}
          */
-        public MessagingStyle(CharSequence userDisplayName) {
+        public MessagingStyle(@NonNull CharSequence userDisplayName) {
             mUserDisplayName = userDisplayName;
         }
 
diff --git a/core/java/android/app/NotificationManager.java b/core/java/android/app/NotificationManager.java
index 18b72e2..ff514bd 100644
--- a/core/java/android/app/NotificationManager.java
+++ b/core/java/android/app/NotificationManager.java
@@ -312,8 +312,8 @@
         try {
             service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
                     copy, idOut, user.getIdentifier());
-            if (id != idOut[0]) {
-                Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
+            if (localLOGV && id != idOut[0]) {
+                Log.v(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
             }
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
diff --git a/core/java/android/app/ResourcesManager.java b/core/java/android/app/ResourcesManager.java
index c4673a3..d2e0327 100644
--- a/core/java/android/app/ResourcesManager.java
+++ b/core/java/android/app/ResourcesManager.java
@@ -242,7 +242,7 @@
      * @return a new AssetManager.
     */
     @VisibleForTesting
-    protected @NonNull AssetManager createAssetManager(@NonNull final ResourcesKey key) {
+    protected @Nullable AssetManager createAssetManager(@NonNull final ResourcesKey key) {
         AssetManager assets = new AssetManager();
 
         // resDir can be null if the 'android' package is creating a new Resources object.
@@ -250,15 +250,16 @@
         // already.
         if (key.mResDir != null) {
             if (assets.addAssetPath(key.mResDir) == 0) {
-                throw new Resources.NotFoundException("failed to add asset path " + key.mResDir);
+                Log.e(TAG, "failed to add asset path " + key.mResDir);
+                return null;
             }
         }
 
         if (key.mSplitResDirs != null) {
             for (final String splitResDir : key.mSplitResDirs) {
                 if (assets.addAssetPath(splitResDir) == 0) {
-                    throw new Resources.NotFoundException(
-                            "failed to add split asset path " + splitResDir);
+                    Log.e(TAG, "failed to add split asset path " + splitResDir);
+                    return null;
                 }
             }
         }
@@ -303,11 +304,15 @@
         return config;
     }
 
-    private @NonNull ResourcesImpl createResourcesImpl(@NonNull ResourcesKey key) {
+    private @Nullable ResourcesImpl createResourcesImpl(@NonNull ResourcesKey key) {
         final DisplayAdjustments daj = new DisplayAdjustments(key.mOverrideConfiguration);
         daj.setCompatibilityInfo(key.mCompatInfo);
 
         final AssetManager assets = createAssetManager(key);
+        if (assets == null) {
+            return null;
+        }
+
         final DisplayMetrics dm = getDisplayMetrics(key.mDisplayId, daj);
         final Configuration config = generateConfig(key, dm);
         final ResourcesImpl impl = new ResourcesImpl(assets, dm, config, daj);
@@ -323,7 +328,7 @@
      * @param key The key to match.
      * @return a ResourcesImpl if the key matches a cache entry, null otherwise.
      */
-    private ResourcesImpl findResourcesImplForKeyLocked(@NonNull ResourcesKey key) {
+    private @Nullable ResourcesImpl findResourcesImplForKeyLocked(@NonNull ResourcesKey key) {
         WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.get(key);
         ResourcesImpl impl = weakImplRef != null ? weakImplRef.get() : null;
         if (impl != null && impl.getAssets().isUpToDate()) {
@@ -338,12 +343,14 @@
      * @param key The key to match.
      * @return a ResourcesImpl object matching the key.
      */
-    private @NonNull ResourcesImpl findOrCreateResourcesImplForKeyLocked(
+    private @Nullable ResourcesImpl findOrCreateResourcesImplForKeyLocked(
             @NonNull ResourcesKey key) {
         ResourcesImpl impl = findResourcesImplForKeyLocked(key);
         if (impl == null) {
             impl = createResourcesImpl(key);
-            mResourceImpls.put(key, new WeakReference<>(impl));
+            if (impl != null) {
+                mResourceImpls.put(key, new WeakReference<>(impl));
+            }
         }
         return impl;
     }
@@ -352,7 +359,8 @@
      * Find the ResourcesKey that this ResourcesImpl object is associated with.
      * @return the ResourcesKey or null if none was found.
      */
-    private ResourcesKey findKeyForResourceImplLocked(@NonNull ResourcesImpl resourceImpl) {
+    private @Nullable ResourcesKey findKeyForResourceImplLocked(
+            @NonNull ResourcesImpl resourceImpl) {
         final int refCount = mResourceImpls.size();
         for (int i = 0; i < refCount; i++) {
             WeakReference<ResourcesImpl> weakImplRef = mResourceImpls.valueAt(i);
@@ -364,6 +372,26 @@
         return null;
     }
 
+    /**
+     * Check if activity resources have same override config as the provided on.
+     * @param activityToken The Activity that resources should be associated with.
+     * @param overrideConfig The override configuration to be checked for equality with.
+     * @return true if activity resources override config matches the provided one or they are both
+     *         null, false otherwise.
+     */
+    boolean isSameResourcesOverrideConfig(@Nullable IBinder activityToken,
+            @Nullable Configuration overrideConfig) {
+        synchronized (this) {
+            final ActivityResources activityResources
+                    = activityToken != null ? mActivityResourceReferences.get(activityToken) : null;
+            if (activityResources == null) {
+                return overrideConfig == null;
+            } else {
+                return Objects.equals(activityResources.overrideConfig, overrideConfig);
+            }
+        }
+    }
+
     private ActivityResources getOrCreateActivityResourcesStructLocked(
             @NonNull IBinder activityToken) {
         ActivityResources activityResources = mActivityResourceReferences.get(activityToken);
@@ -460,7 +488,7 @@
      *                    {@link ClassLoader#getSystemClassLoader()} is used.
      * @return a Resources object from which to access resources.
      */
-    public @NonNull Resources createBaseActivityResources(@NonNull IBinder activityToken,
+    public @Nullable Resources createBaseActivityResources(@NonNull IBinder activityToken,
             @Nullable String resDir,
             @Nullable String[] splitResDirs,
             @Nullable String[] overlayDirs,
@@ -514,7 +542,7 @@
      *         {@link #applyConfigurationToResourcesLocked(Configuration, CompatibilityInfo)}
      *         is called.
      */
-    private @NonNull Resources getOrCreateResources(@Nullable IBinder activityToken,
+    private @Nullable Resources getOrCreateResources(@Nullable IBinder activityToken,
             @NonNull ResourcesKey key, @NonNull ClassLoader classLoader) {
         synchronized (this) {
             if (DEBUG) {
@@ -569,6 +597,9 @@
 
         // If we're here, we didn't find a suitable ResourcesImpl to use, so create one now.
         ResourcesImpl resourcesImpl = createResourcesImpl(key);
+        if (resourcesImpl == null) {
+            return null;
+        }
 
         synchronized (this) {
             ResourcesImpl existingResourcesImpl = findResourcesImplForKeyLocked(key);
@@ -622,7 +653,7 @@
      * {@link ClassLoader#getSystemClassLoader()} is used.
      * @return a Resources object from which to access resources.
      */
-    public @NonNull Resources getResources(@Nullable IBinder activityToken,
+    public @Nullable Resources getResources(@Nullable IBinder activityToken,
             @Nullable String resDir,
             @Nullable String[] splitResDirs,
             @Nullable String[] overlayDirs,
@@ -745,10 +776,12 @@
                     ResourcesImpl resourcesImpl = findResourcesImplForKeyLocked(newKey);
                     if (resourcesImpl == null) {
                         resourcesImpl = createResourcesImpl(newKey);
-                        mResourceImpls.put(newKey, new WeakReference<>(resourcesImpl));
+                        if (resourcesImpl != null) {
+                            mResourceImpls.put(newKey, new WeakReference<>(resourcesImpl));
+                        }
                     }
 
-                    if (resourcesImpl != resources.getImpl()) {
+                    if (resourcesImpl != null && resourcesImpl != resources.getImpl()) {
                         // Set the ResourcesImpl, updating it for all users of this Resources
                         // object.
                         resources.setImpl(resourcesImpl);
@@ -890,7 +923,11 @@
                 if (r != null) {
                     final ResourcesKey key = updatedResourceKeys.get(r.getImpl());
                     if (key != null) {
-                        r.setImpl(findOrCreateResourcesImplForKeyLocked(key));
+                        final ResourcesImpl impl = findOrCreateResourcesImplForKeyLocked(key);
+                        if (impl == null) {
+                            throw new Resources.NotFoundException("failed to load " + libAsset);
+                        }
+                        r.setImpl(impl);
                     }
                 }
             }
@@ -903,7 +940,11 @@
                     if (r != null) {
                         final ResourcesKey key = updatedResourceKeys.get(r.getImpl());
                         if (key != null) {
-                            r.setImpl(findOrCreateResourcesImplForKeyLocked(key));
+                            final ResourcesImpl impl = findOrCreateResourcesImplForKeyLocked(key);
+                            if (impl == null) {
+                                throw new Resources.NotFoundException("failed to load " + libAsset);
+                            }
+                            r.setImpl(impl);
                         }
                     }
                 }
diff --git a/core/java/android/app/RetailDemoModeServiceInternal.java b/core/java/android/app/RetailDemoModeServiceInternal.java
new file mode 100644
index 0000000..7ca214a
--- /dev/null
+++ b/core/java/android/app/RetailDemoModeServiceInternal.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.app;
+
+/**
+ * Retail Demo Mode Service interface to be used locally inside system server
+ *
+ * @hide Only for use inside system server
+ */
+public interface RetailDemoModeServiceInternal {
+    /**
+     * Used to notify RetailDemoModeService of any user activity.
+     */
+    public void onUserActivity();
+}
\ No newline at end of file
diff --git a/core/java/android/app/TabActivity.java b/core/java/android/app/TabActivity.java
index 882e55a..637c8c1 100644
--- a/core/java/android/app/TabActivity.java
+++ b/core/java/android/app/TabActivity.java
@@ -28,23 +28,6 @@
  * {@link ActionBar#newTab() ActionBar.newTab()} and
  * related APIs for placing tabs within their action bar area.</p>
  *
- * <p>A replacement for TabActivity can also be implemented by directly using
- * TabHost.  You will need to define a layout that correctly uses a TabHost
- * with a TabWidget as well as an area in which to display your tab content.
- * A typical example would be:</p>
- *
- * {@sample development/samples/Support4Demos/res/layout/fragment_tabs.xml complete}
- *
- * <p>The implementation needs to take over responsibility for switching
- * the shown content when the user switches between tabs.
- *
- * {@sample development/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabs.java
- *      complete}
- *
- * <p>Also see the <a href="{@docRoot}resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager.html">
- * Fragment Tabs Pager</a> sample for an example of using the support library's ViewPager to
- * allow the user to swipe the content to switch between tabs.</p>
- *
  * @deprecated New applications should use Fragments instead of this class;
  * to continue to run on older devices, you can use the v4 support library
  * which provides a version of the Fragment API that is compatible down to
diff --git a/core/java/android/app/TimePickerDialog.java b/core/java/android/app/TimePickerDialog.java
index aca0763..3f467a0 100644
--- a/core/java/android/app/TimePickerDialog.java
+++ b/core/java/android/app/TimePickerDialog.java
@@ -16,6 +16,7 @@
 
 package android.app;
 
+import android.annotation.TestApi;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnClickListener;
@@ -91,6 +92,12 @@
 
     /**
      * Creates a new time picker dialog with the specified theme.
+     * <p>
+     * The theme is overlaid on top of the theme of the parent {@code context}.
+     * If {@code themeResId} is 0, the dialog will be inflated using the theme
+     * specified by the
+     * {@link android.R.attr#timePickerDialogTheme android:timePickerDialogTheme}
+     * attribute on the parent {@code context}'s theme.
      *
      * @param context the parent context
      * @param themeResId the resource ID of the theme to apply to this dialog
@@ -109,11 +116,6 @@
         mIs24HourView = is24HourView;
 
         final Context themeContext = getContext();
-
-
-        final TypedValue outValue = new TypedValue();
-        context.getTheme().resolveAttribute(R.attr.timePickerDialogTheme, outValue, true);
-
         final LayoutInflater inflater = LayoutInflater.from(themeContext);
         final View view = inflater.inflate(R.layout.time_picker_dialog, null);
         setView(view);
@@ -128,6 +130,15 @@
         mTimePicker.setOnTimeChangedListener(this);
     }
 
+    /**
+     * @return the time picker displayed in the dialog
+     * @hide For testing only.
+     */
+    @TestApi
+    public TimePicker getTimePicker() {
+        return mTimePicker;
+    }
+
     @Override
     public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
         /* do nothing */
diff --git a/core/java/android/app/WallpaperInfo.java b/core/java/android/app/WallpaperInfo.java
index 7db9fa8..9d40381f 100644
--- a/core/java/android/app/WallpaperInfo.java
+++ b/core/java/android/app/WallpaperInfo.java
@@ -31,6 +31,7 @@
 import android.content.res.TypedArray;
 import android.content.res.XmlResourceParser;
 import android.graphics.drawable.Drawable;
+import android.net.Uri;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.service.wallpaper.WallpaperService;
@@ -72,6 +73,10 @@
      */
     final int mDescriptionResource;
 
+    final int mContextUriResource;
+    final int mContextDescriptionResource;
+    final boolean mShowMetadataInPreview;
+
     /**
      * Constructor.
      * 
@@ -89,7 +94,10 @@
         int thumbnailRes = -1;
         int authorRes = -1;
         int descriptionRes = -1;
-        
+        int contextUriRes = -1;
+        int contextDescriptionRes = -1;
+        boolean showMetadataInPreview = false;
+
         XmlResourceParser parser = null;
         try {
             parser = si.loadXmlMetaData(pm, WallpaperService.SERVICE_META_DATA);
@@ -127,6 +135,15 @@
             descriptionRes = sa.getResourceId(
                     com.android.internal.R.styleable.Wallpaper_description,
                     -1);
+            contextUriRes = sa.getResourceId(
+                    com.android.internal.R.styleable.Wallpaper_contextUri,
+                    -1);
+            contextDescriptionRes = sa.getResourceId(
+                    com.android.internal.R.styleable.Wallpaper_contextDescription,
+                    -1);
+            showMetadataInPreview = sa.getBoolean(
+                    com.android.internal.R.styleable.Wallpaper_showMetadataInPreview,
+                    false);
 
             sa.recycle();
         } catch (NameNotFoundException e) {
@@ -140,6 +157,9 @@
         mThumbnailResource = thumbnailRes;
         mAuthorResource = authorRes;
         mDescriptionResource = descriptionRes;
+        mContextUriResource = contextUriRes;
+        mContextDescriptionResource = contextDescriptionRes;
+        mShowMetadataInPreview = showMetadataInPreview;
     }
 
     WallpaperInfo(Parcel source) {
@@ -147,6 +167,9 @@
         mThumbnailResource = source.readInt();
         mAuthorResource = source.readInt();
         mDescriptionResource = source.readInt();
+        mContextUriResource = source.readInt();
+        mContextDescriptionResource = source.readInt();
+        mShowMetadataInPreview = source.readInt() != 0;
         mService = ResolveInfo.CREATOR.createFromParcel(source);
     }
     
@@ -248,7 +271,60 @@
         return pm.getText(packageName, mDescriptionResource,
                 mService.serviceInfo.applicationInfo);
     }
-    
+
+    /**
+     * Returns an URI that specifies a link for further context about this wallpaper.
+     *
+     * @param pm An instance of {@link PackageManager} to retrieve the URI.
+     * @return The URI.
+     */
+    public Uri loadContextUri(PackageManager pm) throws NotFoundException {
+        if (mContextUriResource <= 0) throw new NotFoundException();
+        String packageName = mService.resolvePackageName;
+        ApplicationInfo applicationInfo = null;
+        if (packageName == null) {
+            packageName = mService.serviceInfo.packageName;
+            applicationInfo = mService.serviceInfo.applicationInfo;
+        }
+        String contextUriString = pm.getText(
+                packageName, mContextUriResource, applicationInfo).toString();
+        if (contextUriString == null) {
+            return null;
+        }
+        return Uri.parse(contextUriString);
+    }
+
+    /**
+     * Retrieves a title of the URI that specifies a link for further context about this wallpaper.
+     *
+     * @param pm An instance of {@link PackageManager} to retrieve the title.
+     * @return The title.
+     */
+    public CharSequence loadContextDescription(PackageManager pm) throws NotFoundException {
+        if (mContextDescriptionResource <= 0) throw new NotFoundException();
+        String packageName = mService.resolvePackageName;
+        ApplicationInfo applicationInfo = null;
+        if (packageName == null) {
+            packageName = mService.serviceInfo.packageName;
+            applicationInfo = mService.serviceInfo.applicationInfo;
+        }
+        return pm.getText(packageName, mContextDescriptionResource, applicationInfo).toString();
+    }
+
+    /**
+     * Queries whether any metadata should be shown when previewing the wallpaper. If this value is
+     * set to true, any component that shows a preview of this live wallpaper should also show
+     * accompanying information like {@link #loadLabel},
+     * {@link #loadDescription}, {@link #loadAuthor} and
+     * {@link #loadContextDescription(PackageManager)}, so the user gets to know further information
+     * about this wallpaper.
+     *
+     * @return Whether any metadata should be shown when previewing the wallpaper.
+     */
+    public boolean getShowMetadataInPreview() {
+        return mShowMetadataInPreview;
+    }
+
     /**
      * Return the class name of an activity that provides a settings UI for
      * the wallpaper.  You can launch this activity be starting it with
@@ -287,6 +363,9 @@
         dest.writeInt(mThumbnailResource);
         dest.writeInt(mAuthorResource);
         dest.writeInt(mDescriptionResource);
+        dest.writeInt(mContextUriResource);
+        dest.writeInt(mContextDescriptionResource);
+        dest.writeInt(mShowMetadataInPreview ? 1 : 0);
         mService.writeToParcel(dest, flags);
     }
 
diff --git a/core/java/android/app/WallpaperManager.java b/core/java/android/app/WallpaperManager.java
index 84457b4..aa0eaae 100644
--- a/core/java/android/app/WallpaperManager.java
+++ b/core/java/android/app/WallpaperManager.java
@@ -48,9 +48,11 @@
 import android.os.IBinder;
 import android.os.Looper;
 import android.os.ParcelFileDescriptor;
+import android.os.Process;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemProperties;
+import android.os.UserHandle;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.WindowManagerGlobal;
@@ -265,7 +267,7 @@
     }
 
     static class Globals extends IWallpaperManagerCallback.Stub {
-        private IWallpaperManager mService;
+        private final IWallpaperManager mService;
         private Bitmap mCachedWallpaper;
         private int mCachedWallpaperUserId;
         private Bitmap mDefaultWallpaper;
@@ -292,16 +294,16 @@
 
         public Bitmap peekWallpaperBitmap(Context context, boolean returnDefault,
                 @SetWallpaperFlags int which, int userId) {
-            synchronized (this) {
-                if (mService != null) {
-                    try {
-                        if (!mService.isWallpaperSupported(context.getOpPackageName())) {
-                            return null;
-                        }
-                    } catch (RemoteException e) {
-                        throw e.rethrowFromSystemServer();
+            if (mService != null) {
+                try {
+                    if (!mService.isWallpaperSupported(context.getOpPackageName())) {
+                        return null;
                     }
+                } catch (RemoteException e) {
+                    throw e.rethrowFromSystemServer();
                 }
+            }
+            synchronized (this) {
                 if (mCachedWallpaper != null && mCachedWallpaperUserId == userId) {
                     return mCachedWallpaper;
                 }
@@ -316,17 +318,21 @@
                 if (mCachedWallpaper != null) {
                     return mCachedWallpaper;
                 }
-                if (returnDefault) {
-                    if (mDefaultWallpaper == null) {
-                        mDefaultWallpaper = getDefaultWallpaperLocked(context, which);
-                    }
-                    return mDefaultWallpaper;
-                }
-                return null;
             }
+            if (returnDefault) {
+                Bitmap defaultWallpaper = mDefaultWallpaper;
+                if (defaultWallpaper == null) {
+                    defaultWallpaper = getDefaultWallpaper(context, which);
+                    synchronized (this) {
+                        mDefaultWallpaper = defaultWallpaper;
+                    }
+                }
+                return defaultWallpaper;
+            }
+            return null;
         }
 
-        public void forgetLoadedWallpaper() {
+        void forgetLoadedWallpaper() {
             synchronized (this) {
                 mCachedWallpaper = null;
                 mCachedWallpaperUserId = 0;
@@ -361,7 +367,7 @@
             return null;
         }
 
-        private Bitmap getDefaultWallpaperLocked(Context context, @SetWallpaperFlags int which) {
+        private Bitmap getDefaultWallpaper(Context context, @SetWallpaperFlags int which) {
             InputStream is = openDefaultWallpaper(context, which);
             if (is != null) {
                 try {
@@ -412,8 +418,14 @@
      * This is returned as an
      * abstract Drawable that you can install in a View to display whatever
      * wallpaper the user has currently set.
+     * <p>
+     * This method can return null if there is no system wallpaper available, if
+     * wallpapers are not supported in the current user, or if the calling app is not
+     * permitted to access the system wallpaper.
      *
-     * @return Returns a Drawable object that will draw the wallpaper.
+     * @return Returns a Drawable object that will draw the system wallpaper,
+     *     or {@code null} if no system wallpaper exists or if the calling application
+     *     is not able to access the wallpaper.
      */
     public Drawable getDrawable() {
         Bitmap bm = sGlobals.peekWallpaperBitmap(mContext, true, FLAG_SYSTEM);
@@ -783,7 +795,7 @@
                 Log.w(TAG, "WallpaperService not running");
                 throw new RuntimeException(new DeadSystemException());
             } else {
-                return sGlobals.mService.getWallpaperInfo();
+                return sGlobals.mService.getWallpaperInfo(UserHandle.myUserId());
             }
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
@@ -928,7 +940,8 @@
             /* Set the wallpaper to the default values */
             ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(
                     "res:" + resources.getResourceName(resid),
-                    mContext.getOpPackageName(), null, false, result, which, completion);
+                    mContext.getOpPackageName(), null, false, result, which, completion,
+                    UserHandle.myUserId());
             if (fd != null) {
                 FileOutputStream fos = null;
                 boolean ok = false;
@@ -1029,6 +1042,19 @@
     public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
             boolean allowBackup, @SetWallpaperFlags int which)
             throws IOException {
+        return setBitmap(fullImage, visibleCropHint, allowBackup, which,
+                UserHandle.myUserId());
+    }
+
+    /**
+     * Like {@link #setBitmap(Bitmap, Rect, boolean, int)}, but allows to pass in an explicit user
+     * id. If the user id doesn't match the user id the process is running under, calling this
+     * requires permission {@link android.Manifest.permission#INTERACT_ACROSS_USERS_FULL}.
+     * @hide
+     */
+    public int setBitmap(Bitmap fullImage, Rect visibleCropHint,
+            boolean allowBackup, @SetWallpaperFlags int which, int userId)
+            throws IOException {
         validateRect(visibleCropHint);
         if (sGlobals.mService == null) {
             Log.w(TAG, "WallpaperService not running");
@@ -1039,7 +1065,7 @@
         try {
             ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
                     mContext.getOpPackageName(), visibleCropHint, allowBackup,
-                    result, which, completion);
+                    result, which, completion, userId);
             if (fd != null) {
                 FileOutputStream fos = null;
                 try {
@@ -1165,7 +1191,7 @@
         try {
             ParcelFileDescriptor fd = sGlobals.mService.setWallpaper(null,
                     mContext.getOpPackageName(), visibleCropHint, allowBackup,
-                    result, which, completion);
+                    result, which, completion, UserHandle.myUserId());
             if (fd != null) {
                 FileOutputStream fos = null;
                 try {
@@ -1367,7 +1393,8 @@
      */
     @SystemApi
     public void clearWallpaper() {
-        clearWallpaper(FLAG_SYSTEM | FLAG_LOCK, mContext.getUserId());
+        clearWallpaper(FLAG_LOCK, mContext.getUserId());
+        clearWallpaper(FLAG_SYSTEM, mContext.getUserId());
     }
 
     /**
@@ -1399,12 +1426,26 @@
      */
     @SystemApi
     public boolean setWallpaperComponent(ComponentName name) {
+        return setWallpaperComponent(name, UserHandle.myUserId());
+    }
+
+    /**
+     * Set the live wallpaper.
+     *
+     * This can only be called by packages with android.permission.SET_WALLPAPER_COMPONENT
+     * permission. The caller must hold the INTERACT_ACROSS_USERS_FULL permission to change
+     * another user's wallpaper.
+     *
+     * @hide
+     */
+    public boolean setWallpaperComponent(ComponentName name, int userId) {
         if (sGlobals.mService == null) {
             Log.w(TAG, "WallpaperService not running");
             throw new RuntimeException(new DeadSystemException());
         }
         try {
-            sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName());
+            sGlobals.mService.setWallpaperComponentChecked(name, mContext.getOpPackageName(),
+                    userId);
             return true;
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
@@ -1655,13 +1696,13 @@
      * Only the OS itself may use this method.
      * @hide
      */
-    public boolean isWallpaperBackupEligible() {
+    public boolean isWallpaperBackupEligible(int which) {
         if (sGlobals.mService == null) {
             Log.w(TAG, "WallpaperService not running");
             throw new RuntimeException(new DeadSystemException());
         }
         try {
-            return sGlobals.mService.isWallpaperBackupEligible(mContext.getUserId());
+            return sGlobals.mService.isWallpaperBackupEligible(which, mContext.getUserId());
         } catch (RemoteException e) {
             Log.e(TAG, "Exception querying wallpaper backup eligibility: " + e.getMessage());
         }
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index 5305473..afdd434 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -417,6 +417,14 @@
     public static final int NOTIFICATION_BUGREPORT_FINISHED_NOT_ACCEPTED = 3;
 
     /**
+     * Default and maximum timeout in milliseconds after which unlocking with weak auth times out,
+     * i.e. the user has to use a strong authentication method like password, PIN or pattern.
+     *
+     * @hide
+     */
+    public static final long DEFAULT_STRONG_AUTH_TIMEOUT_MS = 72 * 60 * 60 * 1000; // 72h
+
+    /**
      * A {@link android.os.Parcelable} extra of type {@link android.os.PersistableBundle} that
      * allows a mobile device management application or NFC programmer application which starts
      * managed provisioning to pass data to the management application instance after provisioning.
@@ -1276,6 +1284,33 @@
     public static final int PASSWORD_QUALITY_MANAGED = 0x80000;
 
     /**
+     * @hide
+     *
+     * adb shell dpm set-{device,profile}-owner will normally not allow installing an owner to
+     * a user with accounts.  {@link #ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED}
+     * and {@link #ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_DISALLOWED} are the account features
+     * used by authenticator to exempt their accounts from this:
+     *
+     * <ul>
+     *     <li>Non-test-only DO/PO still can't be installed when there are accounts.
+     *     <p>In order to make an apk test-only, add android:testOnly="true" to the
+     *     &lt;application&gt; tag in the manifest.
+     *
+     *     <li>Test-only DO/PO can be installed even when there are accounts, as long as all the
+     *     accounts have the {@link #ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED} feature.
+     *     Some authenticators claim to have any features, so to detect it, we also check
+     *     {@link #ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_DISALLOWED} and disallow installing
+     *     if any of the accounts have it.
+     * </ul>
+     */
+    public static final String ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED =
+            "android.account.DEVICE_OR_PROFILE_OWNER_ALLOWED";
+
+    /** @hide See {@link #ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED} */
+    public static final String ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_DISALLOWED =
+            "android.account.DEVICE_OR_PROFILE_OWNER_DISALLOWED";
+
+    /**
      * Called by an application that is administering the device to set the password restrictions it
      * is imposing. After setting this, the user will not be able to enter a new password that is
      * not at least as restrictive as what has been set. Note that the current password will remain
@@ -1315,7 +1350,7 @@
 
     /**
      * Retrieve the current minimum password quality for a particular admin or all admins that set
-     * retrictions on this user and its participating profiles. Restrictions on profiles that have
+     * restrictions on this user and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account.
      *
      * <p>This method can be called on the {@link DevicePolicyManager} instance
@@ -1379,7 +1414,7 @@
 
     /**
      * Retrieve the current minimum password length for a particular admin or all admins that set
-     * retrictions on this user and its participating profiles. Restrictions on profiles that have
+     * restrictions on this user and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account.
      *
      * <p>This method can be called on the {@link DevicePolicyManager} instance
@@ -1442,7 +1477,7 @@
 
     /**
      * Retrieve the current number of upper case letters required in the password
-     * for a particular admin or all admins that set retrictions on this user and
+     * for a particular admin or all admins that set restrictions on this user and
      * its participating profiles. Restrictions on profiles that have a separate challenge
      * are not taken into account.
      * This is the same value as set by
@@ -1511,7 +1546,7 @@
 
     /**
      * Retrieve the current number of lower case letters required in the password
-     * for a particular admin or all admins that set retrictions on this user
+     * for a particular admin or all admins that set restrictions on this user
      * and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account.
      * This is the same value as set by
@@ -1580,7 +1615,7 @@
 
     /**
      * Retrieve the current number of letters required in the password
-     * for a particular admin or all admins that set retrictions on this user
+     * for a particular admin or all admins that set restrictions on this user
      * and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account.
      * This is the same value as set by
@@ -1648,7 +1683,7 @@
 
     /**
      * Retrieve the current number of numerical digits required in the password
-     * for a particular admin or all admins that set retrictions on this user
+     * for a particular admin or all admins that set restrictions on this user
      * and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account.
      * This is the same value as set by
@@ -1716,7 +1751,7 @@
 
     /**
      * Retrieve the current number of symbols required in the password
-     * for a particular admin or all admins that set retrictions on this user
+     * for a particular admin or all admins that set restrictions on this user
      * and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account. This is the same value as
      * set by {@link #setPasswordMinimumSymbols(ComponentName, int)}
@@ -1783,7 +1818,7 @@
 
     /**
      * Retrieve the current number of non-letter characters required in the password
-     * for a particular admin or all admins that set retrictions on this user
+     * for a particular admin or all admins that set restrictions on this user
      * and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account.
      * This is the same value as set by
@@ -1915,7 +1950,7 @@
 
     /**
      * Get the current password expiration time for a particular admin or all admins that set
-     * retrictions on this user and its participating profiles. Restrictions on profiles that have
+     * restrictions on this user and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account. If admin is {@code null}, then a composite
      * of all expiration times is returned - which will be the minimum of all of them.
      *
@@ -1939,7 +1974,7 @@
 
     /**
      * Retrieve the current password history length for a particular admin or all admins that
-     * set retrictions on this user and its participating profiles. Restrictions on profiles that
+     * set restrictions on this user and its participating profiles. Restrictions on profiles that
      * have a separate challenge are not taken into account.
      *
      * <p>This method can be called on the {@link DevicePolicyManager} instance
@@ -2121,7 +2156,7 @@
 
     /**
      * Retrieve the current maximum number of login attempts that are allowed before the device
-     * or profile is wiped, for a particular admin or all admins that set retrictions on this user
+     * or profile is wiped, for a particular admin or all admins that set restrictions on this user
      * and its participating profiles. Restrictions on profiles that have a separate challenge are
      * not taken into account.
      *
@@ -2219,6 +2254,7 @@
      * @throws SecurityException if the calling application does not own an active administrator
      *             that uses {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD}
      * @throws IllegalStateException if the calling user is locked or has a managed profile.
+     * @throws IllegalArgumentException if the password does not meet system requirements.
      */
     public boolean resetPassword(String password, int flags) {
         throwIfParentInstance("resetPassword");
@@ -2262,7 +2298,7 @@
 
     /**
      * Retrieve the current maximum time to unlock for a particular admin or all admins that set
-     * retrictions on this user and its participating profiles. Restrictions on profiles that have
+     * restrictions on this user and its participating profiles. Restrictions on profiles that have
      * a separate challenge are not taken into account.
      *
      * <p>This method can be called on the {@link DevicePolicyManager} instance
@@ -2308,6 +2344,78 @@
     }
 
     /**
+     * Called by a device/profile owner to set the timeout after which unlocking with secondary, non
+     * strong auth (e.g. fingerprint, trust agents) times out, i.e. the user has to use a strong
+     * authentication method like password, pin or pattern.
+     *
+     * <p>This timeout is used internally to reset the timer to require strong auth again after
+     * specified timeout each time it has been successfully used.
+     *
+     * <p>Fingerprint can also be disabled altogether using {@link #KEYGUARD_DISABLE_FINGERPRINT}.
+     *
+     * <p>Trust agents can also be disabled altogether using {@link #KEYGUARD_DISABLE_TRUST_AGENTS}.
+     *
+     * <p>The calling device admin must be a device or profile owner. If it is not,
+     * a {@link SecurityException} will be thrown.
+     *
+     * <p>This method can be called on the {@link DevicePolicyManager} instance returned by
+     * {@link #getParentProfileInstance(ComponentName)} in order to set restrictions on the parent
+     * profile.
+     *
+     * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
+     * @param timeoutMs The new timeout, after which the user will have to unlock with strong
+     *         authentication method. If the timeout is lower than 1 hour (minimum) or higher than
+     *         72 hours (default and maximum) an {@link IllegalArgumentException} is thrown.
+     *
+     * @throws SecurityException if {@code admin} is not a device or profile owner.
+     * @throws IllegalArgumentException if the timeout is lower than 1 hour (minimum) or higher than
+     *         72 hours (default and maximum)
+     *
+     * @hide
+     */
+    public void setRequiredStrongAuthTimeout(@NonNull ComponentName admin,
+            long timeoutMs) {
+        if (mService != null) {
+            try {
+                mService.setRequiredStrongAuthTimeout(admin, timeoutMs, mParentInstance);
+            } catch (RemoteException e) {
+                throw e.rethrowFromSystemServer();
+            }
+        }
+    }
+
+    /**
+     * Determine for how long the user will be able to use secondary, non strong auth for
+     * authentication, since last strong method authentication (password, pin or pattern) was used.
+     * After the returned timeout the user is required to use strong authentication method.
+     *
+     * <p>This method can be called on the {@link DevicePolicyManager} instance
+     * returned by {@link #getParentProfileInstance(ComponentName)} in order to retrieve
+     * restrictions on the parent profile.
+     *
+     * @param admin The name of the admin component to check, or {@code null} to aggregate
+     *         accross all participating admins.
+     * @return The timeout or default timeout if not configured
+     *
+     * @hide
+     */
+    public long getRequiredStrongAuthTimeout(@Nullable ComponentName admin) {
+        return getRequiredStrongAuthTimeout(admin, myUserId());
+    }
+
+    /** @hide per-user version */
+    public long getRequiredStrongAuthTimeout(@Nullable ComponentName admin, @UserIdInt int userId) {
+        if (mService != null) {
+            try {
+                return mService.getRequiredStrongAuthTimeout(admin, userId, mParentInstance);
+            } catch (RemoteException e) {
+                throw e.rethrowFromSystemServer();
+            }
+        }
+        return DEFAULT_STRONG_AUTH_TIMEOUT_MS;
+    }
+
+    /**
      * Make the device lock immediately, as if the lock screen timeout has expired at the point of
      * this call.
      * <p>
@@ -3348,7 +3456,7 @@
 
     /**
      * Determine whether or not features have been disabled in keyguard either by the calling
-     * admin, if specified, or all admins that set retrictions on this user and its participating
+     * admin, if specified, or all admins that set restrictions on this user and its participating
      * profiles. Restrictions on profiles that have a separate challenge are not taken into account.
      *
      * <p>This method can be called on the {@link DevicePolicyManager} instance
@@ -6423,9 +6531,77 @@
         }
     }
 
+    /**
+     * @hide
+     * @return whether {@link android.provider.Settings.Global#DEVICE_PROVISIONED} has ever been set
+     * to 1.
+     */
+    public boolean isDeviceProvisioned() {
+        try {
+            return mService.isDeviceProvisioned();
+        } catch (RemoteException re) {
+            throw re.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * @hide
+     * Writes that the provisioning configuration has been applied.
+     */
+    public void setDeviceProvisioningConfigApplied() {
+        try {
+            mService.setDeviceProvisioningConfigApplied();
+        } catch (RemoteException re) {
+            throw re.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * @hide
+     * @return whether the provisioning configuration has been applied.
+     */
+    public boolean isDeviceProvisioningConfigApplied() {
+        try {
+            return mService.isDeviceProvisioningConfigApplied();
+        } catch (RemoteException re) {
+            throw re.rethrowFromSystemServer();
+        }
+    }
+
     private void throwIfParentInstance(String functionName) {
         if (mParentInstance) {
             throw new SecurityException(functionName + " cannot be called on the parent instance");
         }
     }
+
+    /**
+     * @hide
+     * Enable backup service.
+     * <p>This includes all backup and restore mechanisms.
+     * Setting this to {@code false} will make backup service no-op or return empty results.
+     *
+     * <p>There must be only one user on the device, managed by the device owner.
+     * Otherwise a {@link SecurityException} will be thrown.
+     *
+     * <p>Backup service is off by default when device owner is present.
+     */
+    public void setBackupServiceEnabled(@NonNull ComponentName admin, boolean enabled) {
+        try {
+            mService.setBackupServiceEnabled(admin, enabled);
+        } catch (RemoteException re) {
+            throw re.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * @hide
+     * @return {@code true} if backup service is enabled, {@code false} otherwise.
+     */
+    public boolean isBackupServiceEnabled(@NonNull ComponentName admin) {
+        try {
+            return mService.isBackupServiceEnabled(admin);
+        } catch (RemoteException re) {
+            throw re.rethrowFromSystemServer();
+        }
+    }
 }
diff --git a/core/java/android/app/admin/IDevicePolicyManager.aidl b/core/java/android/app/admin/IDevicePolicyManager.aidl
index ddec412..f39cb5a 100644
--- a/core/java/android/app/admin/IDevicePolicyManager.aidl
+++ b/core/java/android/app/admin/IDevicePolicyManager.aidl
@@ -82,6 +82,9 @@
     long getMaximumTimeToLock(in ComponentName who, int userHandle, boolean parent);
     long getMaximumTimeToLockForUserAndProfiles(int userHandle);
 
+    void setRequiredStrongAuthTimeout(in ComponentName who, long timeMs, boolean parent);
+    long getRequiredStrongAuthTimeout(in ComponentName who, int userId, boolean parent);
+
     void lockNow(boolean parent);
 
     void wipeData(int flags);
@@ -301,4 +304,11 @@
 
     boolean isUninstallInQueue(String packageName);
     void uninstallPackageWithActiveAdmins(String packageName);
+
+    boolean isDeviceProvisioned();
+    boolean isDeviceProvisioningConfigApplied();
+    void setDeviceProvisioningConfigApplied();
+
+    void setBackupServiceEnabled(in ComponentName admin, boolean enabled);
+    boolean isBackupServiceEnabled(in ComponentName admin);
 }
diff --git a/core/java/android/app/backup/BackupAgent.java b/core/java/android/app/backup/BackupAgent.java
index bc82806..3ba81c8 100644
--- a/core/java/android/app/backup/BackupAgent.java
+++ b/core/java/android/app/backup/BackupAgent.java
@@ -35,6 +35,8 @@
 import android.util.ArraySet;
 import android.util.Log;
 
+import libcore.io.IoUtils;
+
 import org.xmlpull.v1.XmlPullParserException;
 
 import java.io.File;
@@ -921,6 +923,13 @@
                 } catch (RemoteException e) {
                     // we'll time out anyway, so we're safe
                 }
+
+                // Don't close the fd out from under the system service if this was local
+                if (Binder.getCallingPid() != Process.myPid()) {
+                    IoUtils.closeQuietly(oldState);
+                    IoUtils.closeQuietly(data);
+                    IoUtils.closeQuietly(newState);
+                }
             }
         }
 
@@ -951,6 +960,11 @@
                 } catch (RemoteException e) {
                     // we'll time out anyway, so we're safe
                 }
+
+                if (Binder.getCallingPid() != Process.myPid()) {
+                    IoUtils.closeQuietly(data);
+                    IoUtils.closeQuietly(newState);
+                }
             }
         }
 
@@ -994,6 +1008,10 @@
                 } catch (RemoteException e) {
                     // we'll time out anyway, so we're safe
                 }
+
+                if (Binder.getCallingPid() != Process.myPid()) {
+                    IoUtils.closeQuietly(data);
+                }
             }
         }
 
@@ -1041,6 +1059,10 @@
                 } catch (RemoteException e) {
                     // we'll time out anyway, so we're safe
                 }
+
+                if (Binder.getCallingPid() != Process.myPid()) {
+                    IoUtils.closeQuietly(data);
+                }
             }
         }
 
diff --git a/core/java/android/app/backup/BackupManager.java b/core/java/android/app/backup/BackupManager.java
index 7fcca09..80bc136 100644
--- a/core/java/android/app/backup/BackupManager.java
+++ b/core/java/android/app/backup/BackupManager.java
@@ -128,6 +128,14 @@
     @SystemApi
     public static final int ERROR_AGENT_FAILURE = BackupTransport.AGENT_ERROR;
 
+    /**
+     * Intent extra when any subsidiary backup-related UI is launched from Settings:  does
+     * device policy or configuration permit backup operations to run at all?
+     *
+     * @hide
+     */
+    public static final String EXTRA_BACKUP_SERVICES_AVAILABLE = "backup_services_available";
+
     private Context mContext;
     private static IBackupManager sService;
 
diff --git a/core/java/android/app/backup/FullBackup.java b/core/java/android/app/backup/FullBackup.java
index 478024d..76828ee 100644
--- a/core/java/android/app/backup/FullBackup.java
+++ b/core/java/android/app/backup/FullBackup.java
@@ -21,6 +21,8 @@
 import android.content.res.XmlResourceParser;
 import android.os.ParcelFileDescriptor;
 import android.os.Process;
+import android.os.storage.StorageManager;
+import android.os.storage.StorageVolume;
 import android.system.ErrnoException;
 import android.system.Os;
 import android.text.TextUtils;
@@ -223,8 +225,12 @@
 
         final int mFullBackupContent;
         final PackageManager mPackageManager;
+        final StorageManager mStorageManager;
         final String mPackageName;
 
+        // lazy initialized, only when needed
+        private StorageVolume[] mVolumes = null;
+
         /**
          * Parse out the semantic domains into the correct physical location.
          */
@@ -260,16 +266,41 @@
                     } else {
                         return null;
                     }
+                } else if (domainToken.startsWith(FullBackup.SHARED_PREFIX)) {
+                    return sharedDomainToPath(domainToken);
                 }
                 // Not a supported location
                 Log.i(TAG, "Unrecognized domain " + domainToken);
                 return null;
-            } catch (IOException e) {
+            } catch (Exception e) {
                 Log.i(TAG, "Error reading directory for domain: " + domainToken);
                 return null;
             }
 
         }
+
+        private String sharedDomainToPath(String domain) throws IOException {
+            // already known to start with SHARED_PREFIX, so we just look after that
+            final String volume = domain.substring(FullBackup.SHARED_PREFIX.length());
+            final StorageVolume[] volumes = getVolumeList();
+            final int volNum = Integer.parseInt(volume);
+            if (volNum < mVolumes.length) {
+                return volumes[volNum].getPathFile().getCanonicalPath();
+            }
+            return null;
+        }
+
+        private StorageVolume[] getVolumeList() {
+            if (mStorageManager != null) {
+                if (mVolumes == null) {
+                    mVolumes = mStorageManager.getVolumeList();
+                }
+            } else {
+                Log.e(TAG, "Unable to access Storage Manager");
+            }
+            return mVolumes;
+        }
+
         /**
         * A map of domain -> list of canonical file names in that domain that are to be included.
         * We keep track of the domain so that we can go through the file system in order later on.
@@ -283,6 +314,7 @@
 
         BackupScheme(Context context) {
             mFullBackupContent = context.getApplicationInfo().fullBackupContent;
+            mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
             mPackageManager = context.getPackageManager();
             mPackageName = context.getPackageName();
 
diff --git a/core/java/android/app/backup/WallpaperBackupHelper.java b/core/java/android/app/backup/WallpaperBackupHelper.java
index f256a95..f987468 100644
--- a/core/java/android/app/backup/WallpaperBackupHelper.java
+++ b/core/java/android/app/backup/WallpaperBackupHelper.java
@@ -42,7 +42,7 @@
     // If 'true', then apply an acceptable-size heuristic at restore time, dropping back
     // to the factory default wallpaper if the restored one differs "too much" from the
     // device's preferred wallpaper image dimensions.
-    private static final boolean REJECT_OUTSIZED_RESTORE = true;
+    private static final boolean REJECT_OUTSIZED_RESTORE = false;
 
     // When outsized restore rejection is enabled, this is the maximum ratio between the
     // source and target image heights that will be permitted.  The ratio is checked both
@@ -60,6 +60,9 @@
     public static final String WALLPAPER_IMAGE =
             new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
                     "wallpaper").getAbsolutePath();
+    public static final String WALLPAPER_ORIG_IMAGE =
+            new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
+                    "wallpaper_orig").getAbsolutePath();
     public static final String WALLPAPER_INFO =
             new File(Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM),
                     "wallpaper_info.xml").getAbsolutePath();
@@ -199,7 +202,7 @@
             // since it does not exist anywhere other than the private wallpaper
             // file.
             Slog.d(TAG, "Applying restored wallpaper image.");
-            f.renameTo(new File(WALLPAPER_IMAGE));
+            f.renameTo(new File(WALLPAPER_ORIG_IMAGE));
         }
     }
 }
diff --git a/core/java/android/app/job/JobInfo.java b/core/java/android/app/job/JobInfo.java
index 5823abf..734bf69 100644
--- a/core/java/android/app/job/JobInfo.java
+++ b/core/java/android/app/job/JobInfo.java
@@ -165,6 +165,9 @@
      * network restrictions for the requesting app. Note that this flag alone
      * doesn't actually place your {@link JobService} in the foreground; you
      * still need to post the notification yourself.
+     * <p>
+     * To use this flag, the caller must hold the
+     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL} permission.
      *
      * @hide
      */
diff --git a/core/java/android/app/usage/UsageEvents.java b/core/java/android/app/usage/UsageEvents.java
index 24647f38..a0da258 100644
--- a/core/java/android/app/usage/UsageEvents.java
+++ b/core/java/android/app/usage/UsageEvents.java
@@ -79,6 +79,13 @@
         public static final int USER_INTERACTION = 7;
 
         /**
+         * An event type denoting that an action equivalent to a ShortcutInfo is taken by the user.
+         *
+         * @see android.content.pm.ShortcutManager#reportShortcutUsed(String)
+         */
+        public static final int SHORTCUT_INVOCATION = 8;
+
+        /**
          * {@hide}
          */
         public String mPackage;
@@ -105,6 +112,13 @@
         public Configuration mConfiguration;
 
         /**
+         * ID of the shortcut.
+         * Only present for {@link #SHORTCUT_INVOCATION} event types.
+         * {@hide}
+         */
+        public String mShortcutId;
+
+        /**
          * The package name of the source of this event.
          */
         public String getPackageName() {
@@ -145,6 +159,16 @@
         public Configuration getConfiguration() {
             return mConfiguration;
         }
+
+        /**
+         * Returns the ID of a {@link android.content.pm.ShortcutInfo} for this event
+         * if the event is of type {@link #SHORTCUT_INVOCATION}, otherwise it returns null.
+         *
+         * @see android.content.pm.ShortcutManager#reportShortcutUsed(String)
+         */
+        public String getShortcutId() {
+            return mShortcutId;
+        }
     }
 
     // Only used when creating the resulting events. Not used for reading/unparceling.
@@ -276,8 +300,13 @@
         p.writeInt(event.mEventType);
         p.writeLong(event.mTimeStamp);
 
-        if (event.mEventType == Event.CONFIGURATION_CHANGE) {
-            event.mConfiguration.writeToParcel(p, flags);
+        switch (event.mEventType) {
+            case Event.CONFIGURATION_CHANGE:
+                event.mConfiguration.writeToParcel(p, flags);
+                break;
+            case Event.SHORTCUT_INVOCATION:
+                p.writeString(event.mShortcutId);
+                break;
         }
     }
 
@@ -301,11 +330,18 @@
         eventOut.mEventType = p.readInt();
         eventOut.mTimeStamp = p.readLong();
 
-        // Extract the configuration for configuration change events.
-        if (eventOut.mEventType == Event.CONFIGURATION_CHANGE) {
-            eventOut.mConfiguration = Configuration.CREATOR.createFromParcel(p);
-        } else {
-            eventOut.mConfiguration = null;
+        // Fill out the event-dependant fields.
+        eventOut.mConfiguration = null;
+        eventOut.mShortcutId = null;
+
+        switch (eventOut.mEventType) {
+            case Event.CONFIGURATION_CHANGE:
+                // Extract the configuration for configuration change events.
+                eventOut.mConfiguration = Configuration.CREATOR.createFromParcel(p);
+                break;
+            case Event.SHORTCUT_INVOCATION:
+                eventOut.mShortcutId = p.readString();
+                break;
         }
     }
 
diff --git a/core/java/android/app/usage/UsageStatsManagerInternal.java b/core/java/android/app/usage/UsageStatsManagerInternal.java
index b6f1567..a6f91fe 100644
--- a/core/java/android/app/usage/UsageStatsManagerInternal.java
+++ b/core/java/android/app/usage/UsageStatsManagerInternal.java
@@ -56,6 +56,17 @@
     public abstract void reportConfigurationChange(Configuration config, int userId);
 
     /**
+     * Reports that an action equivalent to a ShortcutInfo is taken by the user.
+     *
+     * @param packageName The package name of the shortcut publisher
+     * @param shortcutId The ID of the shortcut in question
+     * @param userId The user in which the content provider was accessed.
+     *
+     * @see android.content.pm.ShortcutManager#reportShortcutUsed(String)
+     */
+    public abstract void reportShortcutUsage(String packageName, String shortcutId, int userId);
+
+    /**
      * Reports that a content provider has been accessed by a foreground app.
      * @param name The authority of the content provider
      * @param pkgName The package name of the content provider
diff --git a/core/java/android/appwidget/AppWidgetHost.java b/core/java/android/appwidget/AppWidgetHost.java
index 2d9f4a7..cd14469 100644
--- a/core/java/android/appwidget/AppWidgetHost.java
+++ b/core/java/android/appwidget/AppWidgetHost.java
@@ -34,7 +34,6 @@
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.ServiceManager;
-import android.os.SystemClock;
 import android.util.DisplayMetrics;
 import android.util.SparseArray;
 import android.util.TypedValue;
@@ -187,19 +186,28 @@
                 idsToUpdate[i] = mViews.keyAt(i);
             }
         }
-        List<RemoteViews> updatedViews;
-        int[] updatedIds = new int[idsToUpdate.length];
+        List<PendingHostUpdate> updates;
         try {
-            updatedViews = sService.startListening(
-                    mCallbacks, mContextOpPackageName, mHostId, idsToUpdate, updatedIds).getList();
+            updates = sService.startListening(
+                    mCallbacks, mContextOpPackageName, mHostId, idsToUpdate).getList();
         }
         catch (RemoteException e) {
             throw new RuntimeException("system server dead?", e);
         }
 
-        int N = updatedViews.size();
+        int N = updates.size();
         for (int i = 0; i < N; i++) {
-            updateAppWidgetView(updatedIds[i], updatedViews.get(i));
+            PendingHostUpdate update = updates.get(i);
+            switch (update.type) {
+                case PendingHostUpdate.TYPE_VIEWS_UPDATE:
+                    updateAppWidgetView(update.appWidgetId, update.views);
+                    break;
+                case PendingHostUpdate.TYPE_PROVIDER_CHANGED:
+                    onProviderChanged(update.appWidgetId, update.widgetInfo);
+                    break;
+                case PendingHostUpdate.TYPE_VIEW_DATA_CHANGED:
+                    viewDataChanged(update.appWidgetId, update.viewId);
+            }
         }
     }
 
diff --git a/core/java/android/appwidget/PendingHostUpdate.java b/core/java/android/appwidget/PendingHostUpdate.java
new file mode 100644
index 0000000..5780319
--- /dev/null
+++ b/core/java/android/appwidget/PendingHostUpdate.java
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.appwidget;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.widget.RemoteViews;
+
+/**
+ * @hide
+ */
+public class PendingHostUpdate implements Parcelable {
+
+    static final int TYPE_VIEWS_UPDATE = 0;
+    static final int TYPE_PROVIDER_CHANGED = 1;
+    static final int TYPE_VIEW_DATA_CHANGED = 2;
+
+    final int appWidgetId;
+    final int type;
+    RemoteViews views;
+    AppWidgetProviderInfo widgetInfo;
+    int viewId;
+
+    public static PendingHostUpdate updateAppWidget(int appWidgetId, RemoteViews views) {
+        PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_VIEWS_UPDATE);
+        update.views = views;
+        return update;
+    }
+
+    public static PendingHostUpdate providerChanged(int appWidgetId, AppWidgetProviderInfo info) {
+        PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_PROVIDER_CHANGED);
+        update.widgetInfo = info;
+        return update;
+    }
+
+    public static PendingHostUpdate viewDataChanged(int appWidgetId, int viewId) {
+        PendingHostUpdate update = new PendingHostUpdate(appWidgetId, TYPE_VIEW_DATA_CHANGED);
+        update.viewId = viewId;
+        return update;
+    }
+
+    private PendingHostUpdate(int appWidgetId, int type) {
+        this.appWidgetId = appWidgetId;
+        this.type = type;
+    }
+
+    private PendingHostUpdate(Parcel in) {
+        appWidgetId = in.readInt();
+        type = in.readInt();
+
+        switch (type) {
+            case TYPE_VIEWS_UPDATE:
+                if (0 != in.readInt()) {
+                    views = new RemoteViews(in);
+                }
+                break;
+            case TYPE_PROVIDER_CHANGED:
+                if (0 != in.readInt()) {
+                    widgetInfo = new AppWidgetProviderInfo(in);
+                }
+                break;
+            case TYPE_VIEW_DATA_CHANGED:
+                viewId = in.readInt();
+        }
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeInt(appWidgetId);
+        dest.writeInt(type);
+        switch (type) {
+            case TYPE_VIEWS_UPDATE:
+                writeNullParcelable(views, dest, flags);
+                break;
+            case TYPE_PROVIDER_CHANGED:
+                writeNullParcelable(widgetInfo, dest, flags);
+                break;
+            case TYPE_VIEW_DATA_CHANGED:
+                dest.writeInt(viewId);
+                break;
+        }
+    }
+
+    private void writeNullParcelable(Parcelable p, Parcel dest, int flags) {
+        if (p != null) {
+            dest.writeInt(1);
+            p.writeToParcel(dest, flags);
+        } else {
+            dest.writeInt(0);
+        }
+    }
+
+    /**
+     * Parcelable.Creator that instantiates PendingHostUpdate objects
+     */
+    public static final Parcelable.Creator<PendingHostUpdate> CREATOR
+            = new Parcelable.Creator<PendingHostUpdate>() {
+        public PendingHostUpdate createFromParcel(Parcel parcel) {
+            return new PendingHostUpdate(parcel);
+        }
+
+        public PendingHostUpdate[] newArray(int size) {
+            return new PendingHostUpdate[size];
+        }
+    };
+}
diff --git a/core/java/android/bluetooth/BluetoothA2dp.java b/core/java/android/bluetooth/BluetoothA2dp.java
index f66b5ff..353c640 100644
--- a/core/java/android/bluetooth/BluetoothA2dp.java
+++ b/core/java/android/bluetooth/BluetoothA2dp.java
@@ -30,8 +30,11 @@
 import android.os.RemoteException;
 import android.util.Log;
 
+import com.android.internal.annotations.GuardedBy;
+
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 
 /**
@@ -114,7 +117,8 @@
 
     private Context mContext;
     private ServiceListener mServiceListener;
-    private IBluetoothA2dp mService;
+    private final ReentrantReadWriteLock mServiceLock = new ReentrantReadWriteLock();
+    @GuardedBy("mServiceLock") private IBluetoothA2dp mService;
     private BluetoothAdapter mAdapter;
 
     final private IBluetoothStateChangeCallback mBluetoothStateChangeCallback =
@@ -122,25 +126,27 @@
                 public void onBluetoothStateChange(boolean up) {
                     if (DBG) Log.d(TAG, "onBluetoothStateChange: up=" + up);
                     if (!up) {
-                        if (VDBG) Log.d(TAG,"Unbinding service...");
-                        synchronized (mConnection) {
-                            try {
-                                mService = null;
-                                mContext.unbindService(mConnection);
-                            } catch (Exception re) {
-                                Log.e(TAG,"",re);
-                            }
+                        if (VDBG) Log.d(TAG, "Unbinding service...");
+                        try {
+                            mServiceLock.writeLock().lock();
+                            mService = null;
+                            mContext.unbindService(mConnection);
+                        } catch (Exception re) {
+                            Log.e(TAG, "", re);
+                        } finally {
+                            mServiceLock.writeLock().unlock();
                         }
                     } else {
-                        synchronized (mConnection) {
-                            try {
-                                if (mService == null) {
-                                    if (VDBG) Log.d(TAG,"Binding service...");
-                                    doBind();
-                                }
-                            } catch (Exception re) {
-                                Log.e(TAG,"",re);
+                        try {
+                            mServiceLock.readLock().lock();
+                            if (mService == null) {
+                                if (VDBG) Log.d(TAG,"Binding service...");
+                                doBind();
                             }
+                        } catch (Exception re) {
+                            Log.e(TAG,"",re);
+                        } finally {
+                            mServiceLock.readLock().unlock();
                         }
                     }
                 }
@@ -189,15 +195,16 @@
             }
         }
 
-        synchronized (mConnection) {
+        try {
+            mServiceLock.writeLock().lock();
             if (mService != null) {
-                try {
-                    mService = null;
-                    mContext.unbindService(mConnection);
-                } catch (Exception re) {
-                    Log.e(TAG,"",re);
-                }
+                mService = null;
+                mContext.unbindService(mConnection);
             }
+        } catch (Exception re) {
+            Log.e(TAG, "", re);
+        } finally {
+            mServiceLock.writeLock().unlock();
         }
     }
 
@@ -229,17 +236,20 @@
      */
     public boolean connect(BluetoothDevice device) {
         if (DBG) log("connect(" + device + ")");
-        if (mService != null && isEnabled() &&
-            isValidDevice(device)) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled() &&
+                isValidDevice(device)) {
                 return mService.connect(device);
-            } catch (RemoteException e) {
-                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
-                return false;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return false;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return false;
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return false;
     }
 
     /**
@@ -270,17 +280,20 @@
      */
     public boolean disconnect(BluetoothDevice device) {
         if (DBG) log("disconnect(" + device + ")");
-        if (mService != null && isEnabled() &&
-            isValidDevice(device)) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled() &&
+                isValidDevice(device)) {
                 return mService.disconnect(device);
-            } catch (RemoteException e) {
-                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
-                return false;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return false;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return false;
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return false;
     }
 
     /**
@@ -288,16 +301,19 @@
      */
     public List<BluetoothDevice> getConnectedDevices() {
         if (VDBG) log("getConnectedDevices()");
-        if (mService != null && isEnabled()) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()) {
                 return mService.getConnectedDevices();
-            } catch (RemoteException e) {
-                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
-                return new ArrayList<BluetoothDevice>();
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return new ArrayList<BluetoothDevice>();
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return new ArrayList<BluetoothDevice>();
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return new ArrayList<BluetoothDevice>();
     }
 
     /**
@@ -305,16 +321,19 @@
      */
     public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
         if (VDBG) log("getDevicesMatchingStates()");
-        if (mService != null && isEnabled()) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()) {
                 return mService.getDevicesMatchingConnectionStates(states);
-            } catch (RemoteException e) {
-                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
-                return new ArrayList<BluetoothDevice>();
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return new ArrayList<BluetoothDevice>();
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return new ArrayList<BluetoothDevice>();
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return new ArrayList<BluetoothDevice>();
     }
 
     /**
@@ -322,17 +341,20 @@
      */
     public int getConnectionState(BluetoothDevice device) {
         if (VDBG) log("getState(" + device + ")");
-        if (mService != null && isEnabled()
-            && isValidDevice(device)) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()
+                && isValidDevice(device)) {
                 return mService.getConnectionState(device);
-            } catch (RemoteException e) {
-                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
-                return BluetoothProfile.STATE_DISCONNECTED;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return BluetoothProfile.STATE_DISCONNECTED;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return BluetoothProfile.STATE_DISCONNECTED;
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return BluetoothProfile.STATE_DISCONNECTED;
     }
 
     /**
@@ -352,21 +374,24 @@
      */
     public boolean setPriority(BluetoothDevice device, int priority) {
         if (DBG) log("setPriority(" + device + ", " + priority + ")");
-        if (mService != null && isEnabled()
-            && isValidDevice(device)) {
-            if (priority != BluetoothProfile.PRIORITY_OFF &&
-                priority != BluetoothProfile.PRIORITY_ON){
-              return false;
-            }
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()
+                && isValidDevice(device)) {
+                if (priority != BluetoothProfile.PRIORITY_OFF &&
+                    priority != BluetoothProfile.PRIORITY_ON) {
+                    return false;
+                }
                 return mService.setPriority(device, priority);
-            } catch (RemoteException e) {
-                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
-                return false;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return false;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return false;
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return false;
     }
 
     /**
@@ -385,17 +410,20 @@
     @RequiresPermission(Manifest.permission.BLUETOOTH)
     public int getPriority(BluetoothDevice device) {
         if (VDBG) log("getPriority(" + device + ")");
-        if (mService != null && isEnabled()
-            && isValidDevice(device)) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()
+                && isValidDevice(device)) {
                 return mService.getPriority(device);
-            } catch (RemoteException e) {
-                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
-                return BluetoothProfile.PRIORITY_OFF;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return BluetoothProfile.PRIORITY_OFF;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return BluetoothProfile.PRIORITY_OFF;
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return BluetoothProfile.PRIORITY_OFF;
     }
 
     /**
@@ -406,16 +434,19 @@
      */
     public boolean isAvrcpAbsoluteVolumeSupported() {
         if (DBG) Log.d(TAG, "isAvrcpAbsoluteVolumeSupported");
-        if (mService != null && isEnabled()) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()) {
                 return mService.isAvrcpAbsoluteVolumeSupported();
-            } catch (RemoteException e) {
-                Log.e(TAG, "Error talking to BT service in isAvrcpAbsoluteVolumeSupported()", e);
-                return false;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return false;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error talking to BT service in isAvrcpAbsoluteVolumeSupported()", e);
+            return false;
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return false;
     }
 
     /**
@@ -433,16 +464,17 @@
      */
     public void adjustAvrcpAbsoluteVolume(int direction) {
         if (DBG) Log.d(TAG, "adjustAvrcpAbsoluteVolume");
-        if (mService != null && isEnabled()) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()) {
                 mService.adjustAvrcpAbsoluteVolume(direction);
-                return;
-            } catch (RemoteException e) {
-                Log.e(TAG, "Error talking to BT service in adjustAvrcpAbsoluteVolume()", e);
-                return;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error talking to BT service in adjustAvrcpAbsoluteVolume()", e);
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
     }
 
     /**
@@ -453,16 +485,17 @@
      */
     public void setAvrcpAbsoluteVolume(int volume) {
         if (DBG) Log.d(TAG, "setAvrcpAbsoluteVolume");
-        if (mService != null && isEnabled()) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()) {
                 mService.setAvrcpAbsoluteVolume(volume);
-                return;
-            } catch (RemoteException e) {
-                Log.e(TAG, "Error talking to BT service in setAvrcpAbsoluteVolume()", e);
-                return;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error talking to BT service in setAvrcpAbsoluteVolume()", e);
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
     }
 
     /**
@@ -473,17 +506,20 @@
      * @param device BluetoothDevice device
      */
     public boolean isA2dpPlaying(BluetoothDevice device) {
-        if (mService != null && isEnabled()
-            && isValidDevice(device)) {
-            try {
+        try {
+            mServiceLock.readLock().lock();
+            if (mService != null && isEnabled()
+                && isValidDevice(device)) {
                 return mService.isA2dpPlaying(device);
-            } catch (RemoteException e) {
-                Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
-                return false;
             }
+            if (mService == null) Log.w(TAG, "Proxy not attached to service");
+            return false;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
+            return false;
+        } finally {
+            mServiceLock.readLock().unlock();
         }
-        if (mService == null) Log.w(TAG, "Proxy not attached to service");
-        return false;
     }
 
     /**
@@ -534,7 +570,12 @@
     private final ServiceConnection mConnection = new ServiceConnection() {
         public void onServiceConnected(ComponentName className, IBinder service) {
             if (DBG) Log.d(TAG, "Proxy object connected");
-            mService = IBluetoothA2dp.Stub.asInterface(service);
+            try {
+                mServiceLock.writeLock().lock();
+                mService = IBluetoothA2dp.Stub.asInterface(service);
+            } finally {
+                mServiceLock.writeLock().unlock();
+            }
 
             if (mServiceListener != null) {
                 mServiceListener.onServiceConnected(BluetoothProfile.A2DP, BluetoothA2dp.this);
@@ -542,7 +583,12 @@
         }
         public void onServiceDisconnected(ComponentName className) {
             if (DBG) Log.d(TAG, "Proxy object disconnected");
-            mService = null;
+            try {
+                mServiceLock.writeLock().lock();
+                mService = null;
+            } finally {
+                mServiceLock.writeLock().unlock();
+            }
             if (mServiceListener != null) {
                 mServiceListener.onServiceDisconnected(BluetoothProfile.A2DP);
             }
diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java
index 8a35b0c..5c9e2ee 100644
--- a/core/java/android/bluetooth/BluetoothDevice.java
+++ b/core/java/android/bluetooth/BluetoothDevice.java
@@ -1604,7 +1604,7 @@
                 // BLE is not supported
                 return null;
             }
-            BluetoothGatt gatt = new BluetoothGatt(context, iGatt, this, transport);
+            BluetoothGatt gatt = new BluetoothGatt(iGatt, this, transport);
             gatt.connect(autoConnect, callback);
             return gatt;
         } catch (RemoteException e) {Log.e(TAG, "", e);}
diff --git a/core/java/android/bluetooth/BluetoothGatt.java b/core/java/android/bluetooth/BluetoothGatt.java
index 9cfe417..2843e35 100644
--- a/core/java/android/bluetooth/BluetoothGatt.java
+++ b/core/java/android/bluetooth/BluetoothGatt.java
@@ -41,7 +41,6 @@
     private static final boolean DBG = true;
     private static final boolean VDBG = false;
 
-    private final Context mContext;
     private IBluetoothGatt mService;
     private BluetoothGattCallback mCallback;
     private int mClientIf;
@@ -492,9 +491,8 @@
             }
         };
 
-    /*package*/ BluetoothGatt(Context context, IBluetoothGatt iGatt, BluetoothDevice device,
+    /*package*/ BluetoothGatt(IBluetoothGatt iGatt, BluetoothDevice device,
                                 int transport) {
-        mContext = context;
         mService = iGatt;
         mDevice = device;
         mTransport = transport;
diff --git a/core/java/android/bluetooth/BluetoothGattServer.java b/core/java/android/bluetooth/BluetoothGattServer.java
index c2bcbb2..5ffceba 100644
--- a/core/java/android/bluetooth/BluetoothGattServer.java
+++ b/core/java/android/bluetooth/BluetoothGattServer.java
@@ -44,7 +44,6 @@
     private static final boolean DBG = true;
     private static final boolean VDBG = false;
 
-    private final Context mContext;
     private BluetoothAdapter mAdapter;
     private IBluetoothGatt mService;
     private BluetoothGattServerCallback mCallback;
@@ -298,8 +297,7 @@
     /**
      * Create a BluetoothGattServer proxy object.
      */
-    /*package*/ BluetoothGattServer(Context context, IBluetoothGatt iGatt, int transport) {
-        mContext = context;
+    /*package*/ BluetoothGattServer(IBluetoothGatt iGatt, int transport) {
         mService = iGatt;
         mAdapter = BluetoothAdapter.getDefaultAdapter();
         mCallback = null;
diff --git a/core/java/android/bluetooth/BluetoothHealthAppConfiguration.java b/core/java/android/bluetooth/BluetoothHealthAppConfiguration.java
index 15a9101..1717a1e 100644
--- a/core/java/android/bluetooth/BluetoothHealthAppConfiguration.java
+++ b/core/java/android/bluetooth/BluetoothHealthAppConfiguration.java
@@ -68,7 +68,9 @@
     public boolean equals(Object o) {
         if (o instanceof BluetoothHealthAppConfiguration) {
             BluetoothHealthAppConfiguration config = (BluetoothHealthAppConfiguration) o;
-            // config.getName() can never be NULL
+
+            if (mName == null) return false;
+
             return mName.equals(config.getName()) &&
                     mDataType == config.getDataType() &&
                     mRole == config.getRole() &&
diff --git a/core/java/android/bluetooth/BluetoothManager.java b/core/java/android/bluetooth/BluetoothManager.java
index 00058a9..29283e7 100644
--- a/core/java/android/bluetooth/BluetoothManager.java
+++ b/core/java/android/bluetooth/BluetoothManager.java
@@ -236,7 +236,7 @@
                 Log.e(TAG, "Fail to get GATT Server connection");
                 return null;
             }
-            BluetoothGattServer mGattServer = new BluetoothGattServer(context, iGatt,transport);
+            BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt,transport);
             Boolean regStatus = mGattServer.registerCallback(callback);
             return regStatus? mGattServer : null;
         } catch (RemoteException e) {
diff --git a/core/java/android/bluetooth/BluetoothSap.java b/core/java/android/bluetooth/BluetoothSap.java
index 014cb22..e70c936 100644
--- a/core/java/android/bluetooth/BluetoothSap.java
+++ b/core/java/android/bluetooth/BluetoothSap.java
@@ -138,7 +138,7 @@
     }
 
     boolean doBind() {
-        Intent intent = new Intent(IBluetoothMap.class.getName());
+        Intent intent = new Intent(IBluetoothSap.class.getName());
         ComponentName comp = intent.resolveSystemService(mContext.getPackageManager(), 0);
         intent.setComponent(comp);
         if (comp == null || !mContext.bindServiceAsUser(intent, mConnection, 0,
diff --git a/core/java/android/bluetooth/BluetoothSocket.java b/core/java/android/bluetooth/BluetoothSocket.java
index 1e99ece..b686938 100644
--- a/core/java/android/bluetooth/BluetoothSocket.java
+++ b/core/java/android/bluetooth/BluetoothSocket.java
@@ -532,22 +532,19 @@
                 if(length <= mMaxTxPacketSize) {
                     mSocketOS.write(b, offset, length);
                 } else {
-                    int tmpOffset = offset;
-                    int tmpLength = mMaxTxPacketSize;
-                    int endIndex = offset + length;
-                    boolean done = false;
                     if(DBG) Log.w(TAG, "WARNING: Write buffer larger than L2CAP packet size!\n"
                             + "Packet will be divided into SDU packets of size "
                             + mMaxTxPacketSize);
-                    do{
+                    int tmpOffset = offset;
+                    int bytesToWrite = length;
+                    while (bytesToWrite > 0) {
+                        int tmpLength = (bytesToWrite > mMaxTxPacketSize)
+                                ? mMaxTxPacketSize
+                                : bytesToWrite;
                         mSocketOS.write(b, tmpOffset, tmpLength);
-                        tmpOffset += mMaxTxPacketSize;
-                        if((tmpOffset + mMaxTxPacketSize) > endIndex) {
-                            tmpLength = endIndex - tmpOffset;
-                            done = true;
-                        }
-                    } while(!done);
-
+                        tmpOffset += tmpLength;
+                        bytesToWrite -= tmpLength;
+                    }
                 }
             } else {
                 mSocketOS.write(b, offset, length);
diff --git a/core/java/android/bluetooth/IBluetoothManager.aidl b/core/java/android/bluetooth/IBluetoothManager.aidl
index 0b81ee8..2b853a3 100644
--- a/core/java/android/bluetooth/IBluetoothManager.aidl
+++ b/core/java/android/bluetooth/IBluetoothManager.aidl
@@ -37,6 +37,7 @@
     boolean enable();
     boolean enableNoAutoConnect();
     boolean disable(boolean persist);
+    int getState();
     IBluetoothGatt getBluetoothGatt();
 
     boolean bindBluetoothProfileService(int profile, IBluetoothProfileServiceConnection proxy);
diff --git a/core/java/android/content/ClipData.java b/core/java/android/content/ClipData.java
index c365e9e..d9816a6 100644
--- a/core/java/android/content/ClipData.java
+++ b/core/java/android/content/ClipData.java
@@ -191,6 +191,14 @@
         final Intent mIntent;
         Uri mUri;
 
+        /** @hide */
+        public Item(Item other) {
+            mText = other.mText;
+            mHtmlText = other.mHtmlText;
+            mIntent = other.mIntent;
+            mUri = other.mUri;
+        }
+
         /**
          * Create an Item consisting of a single block of (possibly styled) text.
          */
@@ -816,6 +824,11 @@
         return mItems.get(index);
     }
 
+    /** @hide */
+    public void setItemAt(int index, Item item) {
+        mItems.set(index, item);
+    }
+
     /**
      * Prepare this {@link ClipData} to leave an app process.
      *
diff --git a/core/java/android/content/ContentResolver.java b/core/java/android/content/ContentResolver.java
index c8d8920..daa1b93 100644
--- a/core/java/android/content/ContentResolver.java
+++ b/core/java/android/content/ContentResolver.java
@@ -34,6 +34,7 @@
 import android.database.Cursor;
 import android.database.IContentObserver;
 import android.graphics.Point;
+import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.CancellationSignal;
@@ -51,6 +52,7 @@
 import android.util.Log;
 
 import com.android.internal.util.ArrayUtils;
+import com.android.internal.util.MimeIconUtils;
 import com.android.internal.util.Preconditions;
 
 import dalvik.system.CloseGuard;
@@ -1875,6 +1877,7 @@
         if (extras != null) {
             String accountName = extras.getString(SYNC_EXTRAS_ACCOUNT);
             if (!TextUtils.isEmpty(accountName)) {
+                // TODO: No references to Google in AOSP
                 account = new Account(accountName, "com.google");
             }
             extras.remove(SYNC_EXTRAS_ACCOUNT);
@@ -2693,4 +2696,9 @@
     public int resolveUserId(Uri uri) {
         return ContentProvider.getUserIdFromUri(uri, mContext.getUserId());
     }
+
+    /** @hide */
+    public Drawable getTypeDrawable(String mimeType) {
+        return MimeIconUtils.loadMimeIcon(mContext, mimeType);
+    }
 }
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java
index 3a0b5ae..ad446c4 100644
--- a/core/java/android/content/Context.java
+++ b/core/java/android/content/Context.java
@@ -2764,8 +2764,10 @@
      *  <dd> A {@link android.net.ConnectivityManager ConnectivityManager} for
      *  handling management of network connections.
      *  <dt> {@link #WIFI_SERVICE} ("wifi")
-     *  <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of
-     * Wi-Fi connectivity.
+     *  <dd> A {@link android.net.wifi.WifiManager WifiManager} for management of Wi-Fi
+     *  connectivity.  On releases before NYC, it should only be obtained from an application
+     *  context, and not from any other derived context to avoid memory leaks within the calling
+     *  process.
      *  <dt> {@link #WIFI_P2P_SERVICE} ("wifip2p")
      *  <dd> A {@link android.net.wifi.p2p.WifiP2pManager WifiP2pManager} for management of
      * Wi-Fi Direct connectivity.
@@ -3618,12 +3620,11 @@
     public static final String HARDWARE_PROPERTIES_SERVICE = "hardware_properties";
 
     /**
-     * TODO Javadoc
+     * Use with {@link #getSystemService} to retrieve a
+     * {@link android.content.pm.ShortcutManager} for accessing the launcher shortcut service.
      *
      * @see #getSystemService
      * @see android.content.pm.ShortcutManager
-     *
-     * @hide
      */
     public static final String SHORTCUT_SERVICE = "shortcut";
 
diff --git a/core/java/android/content/Intent.java b/core/java/android/content/Intent.java
index 2df25d8..cf6880a 100644
--- a/core/java/android/content/Intent.java
+++ b/core/java/android/content/Intent.java
@@ -2219,6 +2219,22 @@
         "android.intent.action.EXTERNAL_APPLICATIONS_UNAVAILABLE";
 
     /**
+     * Broadcast Action: preferred activities have changed *explicitly*.
+     *
+     * <p>Note there are cases where a preferred activity is invalidated *implicitly*, e.g.
+     * when an app is installed or uninstalled, but in such cases this broadcast will *not*
+     * be sent.
+     *
+     * {@link #EXTRA_USER_HANDLE} contains the user ID in question.
+     *
+     * @hide
+     */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
+    public static final String ACTION_PREFERRED_ACTIVITY_CHANGED =
+            "android.intent.action.ACTION_PREFERRED_ACTIVITY_CHANGED";
+
+
+    /**
      * Broadcast Action:  The current system wallpaper has changed.  See
      * {@link android.app.WallpaperManager} for retrieving the new wallpaper.
      * This should <em>only</em> be used to determine when the wallpaper
@@ -3174,6 +3190,14 @@
     public static final String ACTION_MASTER_CLEAR = "android.intent.action.MASTER_CLEAR";
 
     /**
+     * Boolean intent extra to be used with {@link ACTION_MASTER_CLEAR} in order to force a factory
+     * reset even if {@link android.os.UserManager.DISALLOW_FACTORY_RESET} is set.
+     * @hide
+     */
+    public static final String EXTRA_FORCE_MASTER_CLEAR =
+            "android.intent.extra.FORCE_MASTER_CLEAR";
+
+    /**
      * Broadcast action: report that a settings element is being restored from backup.  The intent
      * contains three extras: EXTRA_SETTING_NAME is a string naming the restored setting,
      * EXTRA_SETTING_NEW_VALUE is the value being restored, and EXTRA_SETTING_PREVIOUS_VALUE
@@ -4290,6 +4314,14 @@
     public static final int FLAG_DEBUG_TRIAGED_MISSING = 0x00000100;
 
     /**
+     * Internal flag used to indicate ephemeral applications should not be
+     * considered when resolving the intent.
+     *
+     * @hide
+     */
+    public static final int FLAG_IGNORE_EPHEMERAL = 0x00000200;
+
+    /**
      * If set, the new activity is not kept in the history stack.  As soon as
      * the user navigates away from it, the activity is finished.  This may also
      * be set with the {@link android.R.styleable#AndroidManifestActivity_noHistory
diff --git a/core/java/android/content/IntentFilter.java b/core/java/android/content/IntentFilter.java
index ec76f2d..f5a79c8 100644
--- a/core/java/android/content/IntentFilter.java
+++ b/core/java/android/content/IntentFilter.java
@@ -265,6 +265,7 @@
     public static final String SCHEME_HTTPS = "https";
 
     private int mPriority;
+    private int mOrder;
     private final ArrayList<String> mActions;
     private ArrayList<String> mCategories = null;
     private ArrayList<String> mDataSchemes = null;
@@ -425,6 +426,7 @@
      */
     public IntentFilter(IntentFilter o) {
         mPriority = o.mPriority;
+        mOrder = o.mOrder;
         mActions = new ArrayList<String>(o.mActions);
         if (o.mCategories != null) {
             mCategories = new ArrayList<String>(o.mCategories);
@@ -477,6 +479,16 @@
         return mPriority;
     }
 
+    /** @hide */
+    public final void setOrder(int order) {
+        mOrder = order;
+    }
+
+    /** @hide */
+    public final int getOrder() {
+        return mOrder;
+    }
+
     /**
      * Set whether this filter will needs to be automatically verified against its data URIs or not.
      * The default is false.
diff --git a/core/java/android/content/SyncAdapterType.java b/core/java/android/content/SyncAdapterType.java
index 8a16ac9..6ef7fd2 100644
--- a/core/java/android/content/SyncAdapterType.java
+++ b/core/java/android/content/SyncAdapterType.java
@@ -16,6 +16,7 @@
 
 package android.content;
 
+import android.annotation.Nullable;
 import android.text.TextUtils;
 import android.os.Parcelable;
 import android.os.Parcel;
@@ -33,6 +34,7 @@
     private final boolean isAlwaysSyncable;
     private final boolean allowParallelSyncs;
     private final String settingsActivity;
+    private final String packageName;
 
     public SyncAdapterType(String authority, String accountType, boolean userVisible,
             boolean supportsUploading) {
@@ -50,6 +52,7 @@
         this.allowParallelSyncs = false;
         this.settingsActivity = null;
         this.isKey = false;
+        this.packageName = null;
     }
 
     /** @hide */
@@ -57,7 +60,8 @@
             boolean supportsUploading,
             boolean isAlwaysSyncable,
             boolean allowParallelSyncs,
-            String settingsActivity) {
+            String settingsActivity,
+            String packageName) {
         if (TextUtils.isEmpty(authority)) {
             throw new IllegalArgumentException("the authority must not be empty: " + authority);
         }
@@ -72,6 +76,7 @@
         this.allowParallelSyncs = allowParallelSyncs;
         this.settingsActivity = settingsActivity;
         this.isKey = false;
+        this.packageName = packageName;
     }
 
     private SyncAdapterType(String authority, String accountType) {
@@ -89,6 +94,7 @@
         this.allowParallelSyncs = false;
         this.settingsActivity = null;
         this.isKey = true;
+        this.packageName = null;
     }
 
     public boolean supportsUploading() {
@@ -148,6 +154,16 @@
         return settingsActivity;
     }
 
+    /**
+     * The package hosting the sync adapter.
+     * @return The package name.
+     *
+     * @hide
+     */
+    public @Nullable String getPackageName() {
+        return packageName;
+    }
+
     public static SyncAdapterType newKey(String authority, String accountType) {
         return new SyncAdapterType(authority, accountType);
     }
@@ -181,6 +197,7 @@
                     + ", isAlwaysSyncable=" + isAlwaysSyncable
                     + ", allowParallelSyncs=" + allowParallelSyncs
                     + ", settingsActivity=" + settingsActivity
+                    + ", packageName=" + packageName
                     + "}";
         }
     }
@@ -201,6 +218,7 @@
         dest.writeInt(isAlwaysSyncable ? 1 : 0);
         dest.writeInt(allowParallelSyncs ? 1 : 0);
         dest.writeString(settingsActivity);
+        dest.writeString(packageName);
     }
 
     public SyncAdapterType(Parcel source) {
@@ -211,6 +229,7 @@
                 source.readInt() != 0,
                 source.readInt() != 0,
                 source.readInt() != 0,
+                source.readString(),
                 source.readString());
     }
 
diff --git a/core/java/android/content/SyncAdaptersCache.java b/core/java/android/content/SyncAdaptersCache.java
index 6704b75..ddbdb8a 100644
--- a/core/java/android/content/SyncAdaptersCache.java
+++ b/core/java/android/content/SyncAdaptersCache.java
@@ -81,7 +81,7 @@
                     sa.getString(com.android.internal.R.styleable
                             .SyncAdapter_settingsActivity);
             return new SyncAdapterType(authority, accountType, userVisible, supportsUploading,
-                    isAlwaysSyncable, allowParallelSyncs, settingsActivity);
+                    isAlwaysSyncable, allowParallelSyncs, settingsActivity, packageName);
         } finally {
             sa.recycle();
         }
diff --git a/core/java/android/content/pm/EphemeralResolveInfo.java b/core/java/android/content/pm/EphemeralResolveInfo.java
index afb4c30..fc3b958 100644
--- a/core/java/android/content/pm/EphemeralResolveInfo.java
+++ b/core/java/android/content/pm/EphemeralResolveInfo.java
@@ -27,6 +27,7 @@
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
 
 /**
  * Information about an ephemeral application.
@@ -37,10 +38,7 @@
     /** Algorithm that will be used to generate the domain digest */
     public static final String SHA_ALGORITHM = "SHA-256";
 
-    /** Full digest of the domain hash */
-    private final byte[] mDigestBytes;
-    /** The first 4 bytes of the domain hash */
-    private final int mDigestPrefix;
+    private final EphemeralDigest mDigest;
     private final String mPackageName;
     /** The filters used to match domain */
     private final List<IntentFilter> mFilters = new ArrayList<IntentFilter>();
@@ -55,29 +53,23 @@
             throw new IllegalArgumentException();
         }
 
-        mDigestBytes = generateDigest(uri);
-        mDigestPrefix =
-                mDigestBytes[0] << 24
-                | mDigestBytes[1] << 16
-                | mDigestBytes[2] << 8
-                | mDigestBytes[3] << 0;
+        mDigest = new EphemeralDigest(uri, 0xFFFFFFFF, -1);
         mFilters.addAll(filters);
         mPackageName = packageName;
     }
 
     EphemeralResolveInfo(Parcel in) {
-        mDigestBytes = in.createByteArray();
-        mDigestPrefix = in.readInt();
+        mDigest = in.readParcelable(null /*loader*/);
         mPackageName = in.readString();
         in.readList(mFilters, null /*loader*/);
     }
 
     public byte[] getDigestBytes() {
-        return mDigestBytes;
+        return mDigest.getDigestBytes()[0];
     }
 
     public int getDigestPrefix() {
-        return mDigestPrefix;
+        return mDigest.getDigestPrefix()[0];
     }
 
     public String getPackageName() {
@@ -88,16 +80,6 @@
         return mFilters;
     }
 
-    private static byte[] generateDigest(Uri uri) {
-        try {
-            final MessageDigest digest = MessageDigest.getInstance(SHA_ALGORITHM);
-            final byte[] hostBytes = uri.getHost().getBytes();
-            return digest.digest(hostBytes);
-        } catch (NoSuchAlgorithmException e) {
-            throw new IllegalStateException("could not find digest algorithm");
-        }
-    }
-
     @Override
     public int describeContents() {
         return 0;
@@ -105,8 +87,7 @@
 
     @Override
     public void writeToParcel(Parcel out, int flags) {
-        out.writeByteArray(mDigestBytes);
-        out.writeInt(mDigestPrefix);
+        out.writeParcelable(mDigest, flags);
         out.writeString(mPackageName);
         out.writeList(mFilters);
     }
@@ -136,4 +117,121 @@
             return mResolveInfo;
         }
     }
+
+    /**
+     * Helper class to generate and store each of the digests and prefixes
+     * sent to the Ephemeral Resolver.
+     * <p>
+     * Since intent filters may want to handle multiple hosts within a
+     * domain [eg “*.google.com”], the resolver is presented with multiple
+     * hash prefixes. For example, "a.b.c.d.e" generates digests for
+     * "d.e", "c.d.e", "b.c.d.e" and "a.b.c.d.e".
+     *
+     * @hide
+     */
+    public static final class EphemeralDigest implements Parcelable {
+        /** Full digest of the domain hashes */
+        private final byte[][] mDigestBytes;
+        /** The first 4 bytes of the domain hashes */
+        private final int[] mDigestPrefix;
+
+        public EphemeralDigest(@NonNull Uri uri, int digestMask, int maxDigests) {
+            if (uri == null) {
+                throw new IllegalArgumentException();
+            }
+            mDigestBytes = generateDigest(uri, maxDigests);
+            mDigestPrefix = new int[mDigestBytes.length];
+            for (int i = 0; i < mDigestBytes.length; i++) {
+                mDigestPrefix[i] =
+                        ((mDigestBytes[i][0] & 0xFF) << 24
+                                | (mDigestBytes[i][1] & 0xFF) << 16
+                                | (mDigestBytes[i][2] & 0xFF) << 8
+                                | (mDigestBytes[i][3] & 0xFF) << 0)
+                        & digestMask;
+            }
+        }
+
+        private static byte[][] generateDigest(Uri uri, int maxDigests) {
+            ArrayList<byte[]> digests = new ArrayList<>();
+            try {
+                final String host = uri.getHost().toLowerCase(Locale.ENGLISH);
+                final MessageDigest digest = MessageDigest.getInstance(SHA_ALGORITHM);
+                if (maxDigests <= 0) {
+                    final byte[] hostBytes = host.getBytes();
+                    digests.add(digest.digest(hostBytes));
+                } else {
+                    int prevDot = host.lastIndexOf('.');
+                    prevDot = host.lastIndexOf('.', prevDot - 1);
+                    // shortcut for short URLs
+                    if (prevDot < 0) {
+                        digests.add(digest.digest(host.getBytes()));
+                    } else {
+                        byte[] hostBytes = host.substring(prevDot + 1, host.length()).getBytes();
+                        digests.add(digest.digest(hostBytes));
+                        int digestCount = 1;
+                        while (prevDot >= 0 && digestCount < maxDigests) {
+                            prevDot = host.lastIndexOf('.', prevDot - 1);
+                            hostBytes = host.substring(prevDot + 1, host.length()).getBytes();
+                            digests.add(digest.digest(hostBytes));
+                            digestCount++;
+                        }
+                    }
+                }
+            } catch (NoSuchAlgorithmException e) {
+                throw new IllegalStateException("could not find digest algorithm");
+            }
+            return digests.toArray(new byte[digests.size()][]);
+        }
+
+        EphemeralDigest(Parcel in) {
+            final int digestCount = in.readInt();
+            if (digestCount == -1) {
+                mDigestBytes = null;
+            } else {
+                mDigestBytes = new byte[digestCount][];
+                for (int i = 0; i < digestCount; i++) {
+                    mDigestBytes[i] = in.createByteArray();
+                }
+            }
+            mDigestPrefix = in.createIntArray();
+        }
+
+        public byte[][] getDigestBytes() {
+            return mDigestBytes;
+        }
+
+        public int[] getDigestPrefix() {
+            return mDigestPrefix;
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public void writeToParcel(Parcel out, int flags) {
+            if (mDigestBytes == null) {
+                out.writeInt(-1);
+            } else {
+                out.writeInt(mDigestBytes.length);
+                for (int i = 0; i < mDigestBytes.length; i++) {
+                    out.writeByteArray(mDigestBytes[i]);
+                }
+            }
+            out.writeIntArray(mDigestPrefix);
+        }
+
+        @SuppressWarnings("hiding")
+        public static final Parcelable.Creator<EphemeralDigest> CREATOR =
+                new Parcelable.Creator<EphemeralDigest>() {
+            public EphemeralDigest createFromParcel(Parcel in) {
+                return new EphemeralDigest(in);
+            }
+
+            public EphemeralDigest[] newArray(int size) {
+                return new EphemeralDigest[size];
+            }
+        };
+    }
 }
diff --git a/core/java/android/content/pm/IShortcutService.aidl b/core/java/android/content/pm/IShortcutService.aidl
index 2ba24f6..1bf2ab0 100644
--- a/core/java/android/content/pm/IShortcutService.aidl
+++ b/core/java/android/content/pm/IShortcutService.aidl
@@ -28,6 +28,8 @@
 
     ParceledListSlice getDynamicShortcuts(String packageName, int userId);
 
+    ParceledListSlice getManifestShortcuts(String packageName, int userId);
+
     boolean addDynamicShortcuts(String packageName, in ParceledListSlice shortcutInfoList,
             int userId);
 
@@ -39,7 +41,12 @@
 
     boolean updateShortcuts(String packageName, in ParceledListSlice shortcuts, int userId);
 
-    int getMaxDynamicShortcutCount(String packageName, int userId);
+    void disableShortcuts(String packageName, in List shortcutIds, CharSequence disabledMessage,
+            int disabledMessageResId, int userId);
+
+    void enableShortcuts(String packageName, in List shortcutIds, int userId);
+
+    int getMaxShortcutCountPerActivity(String packageName, int userId);
 
     int getRemainingCallCount(String packageName, int userId);
 
@@ -47,6 +54,8 @@
 
     int getIconMaxDimensions(String packageName, int userId);
 
+    void reportShortcutUsed(String packageName, String shortcutId, int userId);
+
     void resetThrottling(); // system only API for developer opsions
 
     void onApplicationActive(String packageName, int userId); // system only API for sysUI
diff --git a/core/java/android/content/pm/LauncherApps.java b/core/java/android/content/pm/LauncherApps.java
index 8ca27c5..6b23da9 100644
--- a/core/java/android/content/pm/LauncherApps.java
+++ b/core/java/android/content/pm/LauncherApps.java
@@ -20,11 +20,18 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.TestApi;
+import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager.ApplicationInfoFlags;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.graphics.Rect;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Looper;
@@ -34,8 +41,10 @@
 import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.util.DisplayMetrics;
 import android.util.Log;
 
+import java.io.IOException;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
@@ -156,18 +165,19 @@
         }
 
         /**
-         * Indicates that one or more shortcuts (which may be dynamic and/or pinned)
+         * Indicates that one or more shortcuts of any kind (dynamic, pinned, or manifest)
          * have been added, updated or removed.
          *
          * <p>Only the applications that are allowed to access the shortcut information,
          * as defined in {@link #hasShortcutHostPermission()}, will receive it.
          *
          * @param packageName The name of the package that has the shortcuts.
-         * @param shortcuts all shortcuts from the package (dynamic and/or pinned).  Only "key"
-         *    information will be provided, as defined in {@link ShortcutInfo#hasKeyFieldsOnly()}.
+         * @param shortcuts All shortcuts from the package (dynamic, manifest and/or pinned).
+         *    Only "key" information will be provided, as defined in
+         *    {@link ShortcutInfo#hasKeyFieldsOnly()}.
          * @param user The UserHandle of the profile that generated the change.
          *
-         * @hide
+         * @see ShortcutManager
          */
         public void onShortcutsChanged(@NonNull String packageName,
                 @NonNull List<ShortcutInfo> shortcuts, @NonNull UserHandle user) {
@@ -176,31 +186,68 @@
 
     /**
      * Represents a query passed to {@link #getShortcuts(ShortcutQuery, UserHandle)}.
-     *
-     * @hide
      */
     public static class ShortcutQuery {
         /**
          * Include dynamic shortcuts in the result.
          */
-        public static final int FLAG_GET_DYNAMIC = 1 << 0;
+        public static final int FLAG_MATCH_DYNAMIC = 1 << 0;
+
+        /** @hide kept for unit tests */
+        @Deprecated
+        public static final int FLAG_GET_DYNAMIC = FLAG_MATCH_DYNAMIC;
 
         /**
          * Include pinned shortcuts in the result.
          */
-        public static final int FLAG_GET_PINNED = 1 << 1;
+        public static final int FLAG_MATCH_PINNED = 1 << 1;
+
+        /** @hide kept for unit tests */
+        @Deprecated
+        public static final int FLAG_GET_PINNED = FLAG_MATCH_PINNED;
 
         /**
-         * Requests "key" fields only.  See {@link ShortcutInfo#hasKeyFieldsOnly()} for which
-         * fields are available.
+         * Include manifest shortcuts in the result.
+         */
+        public static final int FLAG_MATCH_MANIFEST = 1 << 3;
+
+        /** @hide kept for unit tests */
+        @Deprecated
+        public static final int FLAG_GET_MANIFEST = FLAG_MATCH_MANIFEST;
+
+        /** @hide */
+        public static final int FLAG_MATCH_ALL_KINDS =
+                FLAG_GET_DYNAMIC | FLAG_GET_PINNED | FLAG_GET_MANIFEST;
+
+        /** @hide kept for unit tests */
+        @Deprecated
+        public static final int FLAG_GET_ALL_KINDS = FLAG_MATCH_ALL_KINDS;
+
+        /**
+         * Requests "key" fields only.  See {@link ShortcutInfo#hasKeyFieldsOnly()}'s javadoc to
+         * see which fields fields "key".
+         * This allows quicker access to shortcut information in order to
+         * determine whether the caller's in-memory cache needs to be updated.
+         *
+         * <p>Typically, launcher applications cache all or most shortcut information
+         * in memory in order to show shortcuts without a delay.
+         *
+         * When a given launcher application wants to update its cache, such as when its process
+         * restarts, it can fetch shortcut information with this flag.
+         * The application can then check {@link ShortcutInfo#getLastChangedTimestamp()} for each
+         * shortcut, fetching a shortcut's non-key information only if that shortcut has been
+         * updated.
+         *
+         * @see ShortcutManager
          */
         public static final int FLAG_GET_KEY_FIELDS_ONLY = 1 << 2;
 
         /** @hide */
         @IntDef(flag = true,
                 value = {
-                        FLAG_GET_DYNAMIC,
-                        FLAG_GET_PINNED,
+                        FLAG_MATCH_DYNAMIC,
+                        FLAG_MATCH_PINNED,
+                        FLAG_MATCH_MANIFEST,
                         FLAG_GET_KEY_FIELDS_ONLY,
                 })
         @Retention(RetentionPolicy.SOURCE)
@@ -224,40 +271,56 @@
         }
 
         /**
-         * If non-zero, returns only shortcuts that have been added or updated since the timestamp,
-         * which is a milliseconds since the Epoch.
+         * If non-zero, returns only shortcuts that have been added or updated
+         * since the given timestamp, expressed in milliseconds since the Epoch&mdash;see
+         * {@link System#currentTimeMillis()}.
          */
-        public void setChangedSince(long changedSince) {
+        public ShortcutQuery setChangedSince(long changedSince) {
             mChangedSince = changedSince;
+            return this;
         }
 
         /**
          * If non-null, returns only shortcuts from the package.
          */
-        public void setPackage(@Nullable String packageName) {
+        public ShortcutQuery setPackage(@Nullable String packageName) {
             mPackage = packageName;
+            return this;
         }
 
         /**
          * If non-null, return only the specified shortcuts by ID.  When setting this field,
-         * a packange name must also be set with {@link #setPackage}.
+         * a package name must also be set with {@link #setPackage}.
          */
-        public void setShortcutIds(@Nullable List<String> shortcutIds) {
+        public ShortcutQuery setShortcutIds(@Nullable List<String> shortcutIds) {
             mShortcutIds = shortcutIds;
+            return this;
         }
 
         /**
-         * If non-null, returns only shortcuts associated with the activity.
+         * If non-null, returns only shortcuts associated with the activity; i.e.
+         * {@link ShortcutInfo}s whose {@link ShortcutInfo#getActivity()} are equal
+         * to {@code activity}.
          */
-        public void setActivity(@Nullable ComponentName activity) {
+        public ShortcutQuery setActivity(@Nullable ComponentName activity) {
             mActivity = activity;
+            return this;
         }
 
         /**
-         * Set query options.
+         * Set query options.  At least one of the {@code MATCH} flags should be set.  Otherwise,
+         * no shortcuts will be returned.
+         *
+         * <ul>
+         *     <li>{@link #FLAG_MATCH_DYNAMIC}
+         *     <li>{@link #FLAG_MATCH_PINNED}
+         *     <li>{@link #FLAG_MATCH_MANIFEST}
+         *     <li>{@link #FLAG_GET_KEY_FIELDS_ONLY}
+         * </ul>
          */
-        public void setQueryFlags(@QueryFlags int queryFlags) {
+        public ShortcutQuery setQueryFlags(@QueryFlags int queryFlags) {
             mQueryFlags = queryFlags;
+            return this;
         }
     }
 
@@ -422,12 +485,16 @@
      *
      * <p>Only the default launcher can access the shortcut information.
      *
-     * <p>Note when this method returns {@code false}, that may be a temporary situation because
+     * <p>Note when this method returns {@code false}, it may be a temporary situation because
      * the user is trying a new launcher application.  The user may decide to change the default
-     * launcher to the calling application again, so even if a launcher application loses
+     * launcher back to the calling application again, so even if a launcher application loses
      * this permission, it does <b>not</b> have to purge pinned shortcut information.
+     * If the calling launcher application contains pinned shortcuts, they will still work,
+     * even though the caller no longer has the shortcut host permission.
      *
-     * @hide
+     * @throws IllegalStateException when the user is locked.
+     *
+     * @see ShortcutManager
      */
     public boolean hasShortcutHostPermission() {
         try {
@@ -438,7 +505,7 @@
     }
 
     /**
-     * Returns the IDs of {@link ShortcutInfo}s that match {@code query}.
+     * Returns {@link ShortcutInfo}s that match {@code query}.
      *
      * <p>Callers must be allowed to access the shortcut information, as defined in {@link
      * #hasShortcutHostPermission()}.
@@ -447,8 +514,10 @@
      * @param user The UserHandle of the profile.
      *
      * @return the IDs of {@link ShortcutInfo}s that match the query.
+     * @throws IllegalStateException when the user is locked, or when the {@code user} user
+     * is locked or not running.
      *
-     * @hide
+     * @see ShortcutManager
      */
     @Nullable
     public List<ShortcutInfo> getShortcuts(@NonNull ShortcutQuery query,
@@ -467,12 +536,13 @@
      * @hide // No longer used.  Use getShortcuts() instead.  Kept for unit tests.
      */
     @Nullable
+    @Deprecated
     public List<ShortcutInfo> getShortcutInfo(@NonNull String packageName,
             @NonNull List<String> ids, @NonNull UserHandle user) {
         final ShortcutQuery q = new ShortcutQuery();
         q.setPackage(packageName);
         q.setShortcutIds(ids);
-        q.setQueryFlags(ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED);
+        q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
         return getShortcuts(q, user);
     }
 
@@ -482,14 +552,16 @@
      * <p>This API is <b>NOT</b> cumulative; this will replace all pinned shortcuts for the package.
      * However, different launchers may have different set of pinned shortcuts.
      *
-     * <p>Callers must be allowed to access the shortcut information, as defined in {@link
-     * #hasShortcutHostPermission()}.
+     * <p>The calling launcher application must be allowed to access the shortcut information,
+     * as defined in {@link #hasShortcutHostPermission()}.
      *
      * @param packageName The target package name.
      * @param shortcutIds The IDs of the shortcut to be pinned.
      * @param user The UserHandle of the profile.
+     * @throws IllegalStateException when the user is locked, or when the {@code user} user
+     * is locked or not running.
      *
-     * @hide
+     * @see ShortcutManager
      */
     public void pinShortcuts(@NonNull String packageName, @NonNull List<String> shortcutIds,
             @NonNull UserHandle user) {
@@ -503,6 +575,7 @@
     /**
      * @hide kept for testing.
      */
+    @Deprecated
     public int getShortcutIconResId(@NonNull ShortcutInfo shortcut) {
         return shortcut.getIconResourceId();
     }
@@ -510,46 +583,29 @@
     /**
      * @hide kept for testing.
      */
+    @Deprecated
     public int getShortcutIconResId(@NonNull String packageName, @NonNull String shortcutId,
             @NonNull UserHandle user) {
         final ShortcutQuery q = new ShortcutQuery();
         q.setPackage(packageName);
         q.setShortcutIds(Arrays.asList(shortcutId));
-        q.setQueryFlags(ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED);
+        q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
         final List<ShortcutInfo> shortcuts = getShortcuts(q, user);
 
         return shortcuts.size() > 0 ? shortcuts.get(0).getIconResourceId() : 0;
     }
 
     /**
-     * Return the icon as {@link ParcelFileDescriptor}, when it's stored as a file
-     * (i.e. when {@link ShortcutInfo#hasIconFile()} returns {@code true}).
-     *
-     * <p>Callers must be allowed to access the shortcut information, as defined in {@link
-     * #hasShortcutHostPermission()}.
-     *
-     * @param shortcut The target shortcut.
-     *
-     * @hide
+     * @hide internal/unit tests only
      */
     public ParcelFileDescriptor getShortcutIconFd(
             @NonNull ShortcutInfo shortcut) {
-        return getShortcutIconFd(shortcut.getPackageName(), shortcut.getId(),
+        return getShortcutIconFd(shortcut.getPackage(), shortcut.getId(),
                 shortcut.getUserId());
     }
 
     /**
-     * Return the icon as {@link ParcelFileDescriptor}, when it's stored as a file
-     * (i.e. when {@link ShortcutInfo#hasIconFile()} returns {@code true}).
-     *
-     * <p>Callers must be allowed to access the shortcut information, as defined in {@link
-     * #hasShortcutHostPermission()}.
-     *
-     * @param packageName The target package name.
-     * @param shortcutId The ID of the shortcut to lad rom.
-     * @param user The UserHandle of the profile.
-     *
-     * @hide
+     * @hide internal/unit tests only
      */
     public ParcelFileDescriptor getShortcutIconFd(
             @NonNull String packageName, @NonNull String shortcutId, @NonNull UserHandle user) {
@@ -567,55 +623,133 @@
     }
 
     /**
-     * Launches a shortcut.
+     * Returns the icon for this shortcut, without any badging for the profile.
      *
-     * <p>Callers must be allowed to access the shortcut information, as defined in {@link
-     * #hasShortcutHostPermission()}.
+     * <p>The calling launcher application must be allowed to access the shortcut information,
+     * as defined in {@link #hasShortcutHostPermission()}.
+     *
+     * @param density The preferred density of the icon, zero for default density. Use
+     * density DPI values from {@link DisplayMetrics}.
+     *
+     * @return The drawable associated with the shortcut.
+     * @throws IllegalStateException when the user is locked, or when the {@code user} user
+     * is locked or not running.
+     *
+     * @see ShortcutManager
+     * @see #getShortcutBadgedIconDrawable(ShortcutInfo, int)
+     * @see DisplayMetrics
+     */
+    public Drawable getShortcutIconDrawable(@NonNull ShortcutInfo shortcut, int density) {
+        if (shortcut.hasIconFile()) {
+            final ParcelFileDescriptor pfd = getShortcutIconFd(shortcut);
+            if (pfd == null) {
+                return null;
+            }
+            try {
+                final Bitmap bmp = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
+                return (bmp == null) ? null : new BitmapDrawable(mContext.getResources(), bmp);
+            } finally {
+                try {
+                    pfd.close();
+                } catch (IOException ignore) {
+                }
+            }
+        } else if (shortcut.hasIconResource()) {
+            try {
+                final int resId = shortcut.getIconResourceId();
+                if (resId == 0) {
+                    return null; // Shouldn't happen but just in case.
+                }
+                final ApplicationInfo ai = getApplicationInfo(shortcut.getPackage(),
+                        /* flags =*/ 0, shortcut.getUserHandle());
+                final Resources res = mContext.getPackageManager().getResourcesForApplication(ai);
+                return res.getDrawableForDensity(resId, density);
+            } catch (NameNotFoundException | Resources.NotFoundException e) {
+                return null;
+            }
+        } else {
+            return null; // Has no icon.
+        }
+    }
+
+    /**
+     * Returns the shortcut icon with badging appropriate for the profile.
+     *
+     * <p>The calling launcher application must be allowed to access the shortcut information,
+     * as defined in {@link #hasShortcutHostPermission()}.
+     *
+     * @param density Optional density for the icon, or 0 to use the default density. Use
+     * @return A badged icon for the shortcut.
+     * @throws IllegalStateException when the user is locked, or when the {@code user} user
+     * is locked or not running.
+     *
+     * @see ShortcutManager
+     * @see #getShortcutIconDrawable(ShortcutInfo, int)
+     * @see DisplayMetrics
+     */
+    public Drawable getShortcutBadgedIconDrawable(ShortcutInfo shortcut, int density) {
+        final Drawable originalIcon = getShortcutIconDrawable(shortcut, density);
+
+        return (originalIcon == null) ? null : mContext.getPackageManager().getUserBadgedIcon(
+                originalIcon, shortcut.getUserHandle());
+    }
+
+    /**
+     * Starts a shortcut.
+     *
+     * <p>The calling launcher application must be allowed to access the shortcut information,
+     * as defined in {@link #hasShortcutHostPermission()}.
      *
      * @param packageName The target shortcut package name.
      * @param shortcutId The target shortcut ID.
      * @param sourceBounds The Rect containing the source bounds of the clicked icon.
      * @param startActivityOptions Options to pass to startActivity.
      * @param user The UserHandle of the profile.
-     * @return {@code false} when the shortcut is no longer valid (e.g. the creator application
-     *   has been uninstalled). {@code true} when the shortcut is still valid.
+     * @throws IllegalStateException when the user is locked, or when the {@code user} user
+     * is locked or not running.
      *
-     * @hide
+     * @throws android.content.ActivityNotFoundException failed to start shortcut. (e.g.
+     * the shortcut no longer exists, is disabled, the intent receiver activity doesn't exist, etc)
      */
-    public boolean startShortcut(@NonNull String packageName, @NonNull String shortcutId,
+    public void startShortcut(@NonNull String packageName, @NonNull String shortcutId,
             @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions,
             @NonNull UserHandle user) {
-        return startShortcut(packageName, shortcutId, sourceBounds, startActivityOptions,
+        startShortcut(packageName, shortcutId, sourceBounds, startActivityOptions,
                 user.getIdentifier());
     }
 
     /**
      * Launches a shortcut.
      *
-     * <p>Callers must be allowed to access the shortcut information, as defined in {@link
-     * #hasShortcutHostPermission()}.
+     * <p>The calling launcher application must be allowed to access the shortcut information,
+     * as defined in {@link #hasShortcutHostPermission()}.
      *
      * @param shortcut The target shortcut.
      * @param sourceBounds The Rect containing the source bounds of the clicked icon.
      * @param startActivityOptions Options to pass to startActivity.
-     * @return {@code false} when the shortcut is no longer valid (e.g. the creator application
-     *   has been uninstalled). {@code true} when the shortcut is still valid.
+     * @throws IllegalStateException when the user is locked, or when the {@code user} user
+     * is locked or not running.
      *
-     * @hide
+     * @throws android.content.ActivityNotFoundException failed to start shortcut. (e.g.
+     * the shortcut no longer exists, is disabled, the intent receiver activity doesn't exist, etc)
      */
-    public boolean startShortcut(@NonNull ShortcutInfo shortcut,
+    public void startShortcut(@NonNull ShortcutInfo shortcut,
             @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions) {
-        return startShortcut(shortcut.getPackageName(), shortcut.getId(),
+        startShortcut(shortcut.getPackage(), shortcut.getId(),
                 sourceBounds, startActivityOptions,
                 shortcut.getUserId());
     }
 
-    private boolean startShortcut(@NonNull String packageName, @NonNull String shortcutId,
+    private void startShortcut(@NonNull String packageName, @NonNull String shortcutId,
             @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions,
             int userId) {
         try {
-            return mService.startShortcut(mContext.getPackageName(), packageName, shortcutId,
+            final boolean success =
+                    mService.startShortcut(mContext.getPackageName(), packageName, shortcutId,
                     sourceBounds, startActivityOptions, userId);
+            if (!success) {
+                throw new ActivityNotFoundException("Shortcut could not be started");
+            }
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index 5ce92f1..e86b96f 100644
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -132,6 +132,9 @@
             MATCH_SYSTEM_ONLY,
             MATCH_FACTORY_ONLY,
             MATCH_DEBUG_TRIAGED_MISSING,
+            GET_DISABLED_COMPONENTS,
+            GET_DISABLED_UNTIL_USED_COMPONENTS,
+            GET_UNINSTALLED_PACKAGES,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface PackageInfoFlags {}
@@ -143,6 +146,9 @@
             MATCH_UNINSTALLED_PACKAGES,
             MATCH_SYSTEM_ONLY,
             MATCH_DEBUG_TRIAGED_MISSING,
+            MATCH_DISABLED_UNTIL_USED_COMPONENTS,
+            GET_DISABLED_UNTIL_USED_COMPONENTS,
+            GET_UNINSTALLED_PACKAGES,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ApplicationInfoFlags {}
@@ -160,6 +166,9 @@
             MATCH_DIRECT_BOOT_UNAWARE,
             MATCH_SYSTEM_ONLY,
             MATCH_UNINSTALLED_PACKAGES,
+            GET_DISABLED_COMPONENTS,
+            GET_DISABLED_UNTIL_USED_COMPONENTS,
+            GET_UNINSTALLED_PACKAGES,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ComponentInfoFlags {}
@@ -178,6 +187,9 @@
             MATCH_DIRECT_BOOT_UNAWARE,
             MATCH_SYSTEM_ONLY,
             MATCH_UNINSTALLED_PACKAGES,
+            GET_DISABLED_COMPONENTS,
+            GET_DISABLED_UNTIL_USED_COMPONENTS,
+            GET_UNINSTALLED_PACKAGES,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ResolveInfoFlags {}
@@ -2868,6 +2880,7 @@
      *
      * @see #GET_META_DATA
      * @see #GET_SHARED_LIBRARY_FILES
+     * @see #MATCH_DISABLED_UNTIL_USED_COMPONENTS
      * @see #MATCH_SYSTEM_ONLY
      * @see #MATCH_UNINSTALLED_PACKAGES
      */
@@ -3496,6 +3509,7 @@
      *
      * @see #GET_META_DATA
      * @see #GET_SHARED_LIBRARY_FILES
+     * @see #MATCH_DISABLED_UNTIL_USED_COMPONENTS
      * @see #MATCH_SYSTEM_ONLY
      * @see #MATCH_UNINSTALLED_PACKAGES
      */
diff --git a/core/java/android/content/pm/PackageManagerInternal.java b/core/java/android/content/pm/PackageManagerInternal.java
index 14f7727..f5bcf64 100644
--- a/core/java/android/content/pm/PackageManagerInternal.java
+++ b/core/java/android/content/pm/PackageManagerInternal.java
@@ -157,7 +157,15 @@
             int deviceOwnerUserId, String deviceOwner, SparseArray<String> profileOwners);
 
     /**
-     * Whether a package's data be cleared.
+     * Returns {@code true} if a given package can't be wiped. Otherwise, returns {@code false}.
      */
-    public abstract boolean canPackageBeWiped(int userId, String packageName);
+    public abstract boolean isPackageDataProtected(int userId, String packageName);
+
+    /**
+     * Gets whether the package was ever launched.
+     * @param packageName The package name.
+     * @param userId The user for which to check.
+     * @return Whether was launched.
+     */
+    public abstract boolean wasPackageEverLaunched(String packageName, int userId);
 }
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index 4da77f4..2093124 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -270,6 +270,7 @@
         final int nameRes;
         final int labelRes;
         final int iconRes;
+        final int roundIconRes;
         final int logoRes;
         final int bannerRes;
 
@@ -277,7 +278,8 @@
         TypedArray sa;
 
         ParsePackageItemArgs(Package _owner, String[] _outError,
-                int _nameRes, int _labelRes, int _iconRes, int _logoRes, int _bannerRes) {
+                int _nameRes, int _labelRes, int _iconRes, int _roundIconRes, int _logoRes,
+                int _bannerRes) {
             owner = _owner;
             outError = _outError;
             nameRes = _nameRes;
@@ -285,6 +287,7 @@
             iconRes = _iconRes;
             logoRes = _logoRes;
             bannerRes = _bannerRes;
+            roundIconRes = _roundIconRes;
         }
     }
 
@@ -296,10 +299,12 @@
         int flags;
 
         ParseComponentArgs(Package _owner, String[] _outError,
-                int _nameRes, int _labelRes, int _iconRes, int _logoRes, int _bannerRes,
+                int _nameRes, int _labelRes, int _iconRes, int _roundIconRes, int _logoRes,
+                int _bannerRes,
                 String[] _sepProcesses, int _processRes,
                 int _descriptionRes, int _enabledRes) {
-            super(_owner, _outError, _nameRes, _labelRes, _iconRes, _logoRes, _bannerRes);
+            super(_owner, _outError, _nameRes, _labelRes, _iconRes, _roundIconRes, _logoRes,
+                    _bannerRes);
             sepProcesses = _sepProcesses;
             processRes = _processRes;
             descriptionRes = _descriptionRes;
@@ -2285,11 +2290,7 @@
             b.append(cls);
             return b.toString().intern();
         }
-        if (c >= 'a' && c <= 'z') {
-            return cls.intern();
-        }
-        outError[0] = "Bad class name " + cls + " in package " + pkg;
-        return null;
+        return cls.intern();
     }
 
     private static String buildCompoundName(String pkg,
@@ -2503,12 +2504,12 @@
 
         TypedArray sa = res.obtainAttributes(parser,
                 com.android.internal.R.styleable.AndroidManifestPermissionGroup);
-
         if (!parsePackageItemInfo(owner, perm.info, outError,
-                "<permission-group>", sa,
+                "<permission-group>", sa, true /*nameRequired*/,
                 com.android.internal.R.styleable.AndroidManifestPermissionGroup_name,
                 com.android.internal.R.styleable.AndroidManifestPermissionGroup_label,
                 com.android.internal.R.styleable.AndroidManifestPermissionGroup_icon,
+                com.android.internal.R.styleable.AndroidManifestPermissionGroup_roundIcon,
                 com.android.internal.R.styleable.AndroidManifestPermissionGroup_logo,
                 com.android.internal.R.styleable.AndroidManifestPermissionGroup_banner)) {
             sa.recycle();
@@ -2549,10 +2550,11 @@
                 com.android.internal.R.styleable.AndroidManifestPermission);
 
         if (!parsePackageItemInfo(owner, perm.info, outError,
-                "<permission>", sa,
+                "<permission>", sa, true /*nameRequired*/,
                 com.android.internal.R.styleable.AndroidManifestPermission_name,
                 com.android.internal.R.styleable.AndroidManifestPermission_label,
                 com.android.internal.R.styleable.AndroidManifestPermission_icon,
+                com.android.internal.R.styleable.AndroidManifestPermission_roundIcon,
                 com.android.internal.R.styleable.AndroidManifestPermission_logo,
                 com.android.internal.R.styleable.AndroidManifestPermission_banner)) {
             sa.recycle();
@@ -2618,10 +2620,11 @@
                 com.android.internal.R.styleable.AndroidManifestPermissionTree);
 
         if (!parsePackageItemInfo(owner, perm.info, outError,
-                "<permission-tree>", sa,
+                "<permission-tree>", sa, true /*nameRequired*/,
                 com.android.internal.R.styleable.AndroidManifestPermissionTree_name,
                 com.android.internal.R.styleable.AndroidManifestPermissionTree_label,
                 com.android.internal.R.styleable.AndroidManifestPermissionTree_icon,
+                com.android.internal.R.styleable.AndroidManifestPermissionTree_roundIcon,
                 com.android.internal.R.styleable.AndroidManifestPermissionTree_logo,
                 com.android.internal.R.styleable.AndroidManifestPermissionTree_banner)) {
             sa.recycle();
@@ -2668,6 +2671,7 @@
                     com.android.internal.R.styleable.AndroidManifestInstrumentation_name,
                     com.android.internal.R.styleable.AndroidManifestInstrumentation_label,
                     com.android.internal.R.styleable.AndroidManifestInstrumentation_icon,
+                    com.android.internal.R.styleable.AndroidManifestInstrumentation_roundIcon,
                     com.android.internal.R.styleable.AndroidManifestInstrumentation_logo,
                     com.android.internal.R.styleable.AndroidManifestInstrumentation_banner);
             mParseInstrumentationArgs.tag = "<instrumentation>";
@@ -2733,15 +2737,21 @@
         TypedArray sa = res.obtainAttributes(parser,
                 com.android.internal.R.styleable.AndroidManifestApplication);
 
-        String name = sa.getNonConfigurationString(
-                com.android.internal.R.styleable.AndroidManifestApplication_name, 0);
-        if (name != null) {
-            ai.className = buildClassName(pkgName, name, outError);
-            if (ai.className == null) {
-                sa.recycle();
-                mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
-                return false;
-            }
+        if (!parsePackageItemInfo(owner, ai, outError,
+                "<application>", sa, false /*nameRequired*/,
+                com.android.internal.R.styleable.AndroidManifestApplication_name,
+                com.android.internal.R.styleable.AndroidManifestApplication_label,
+                com.android.internal.R.styleable.AndroidManifestApplication_icon,
+                com.android.internal.R.styleable.AndroidManifestApplication_roundIcon,
+                com.android.internal.R.styleable.AndroidManifestApplication_logo,
+                com.android.internal.R.styleable.AndroidManifestApplication_banner)) {
+            sa.recycle();
+            mParseError = PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED;
+            return false;
+        }
+
+        if (ai.name != null) {
+            ai.className = ai.name;
         }
 
         String manageSpaceActivity = sa.getNonConfigurationString(
@@ -2807,18 +2817,6 @@
             }
         }
 
-        TypedValue v = sa.peekValue(
-                com.android.internal.R.styleable.AndroidManifestApplication_label);
-        if (v != null && (ai.labelRes=v.resourceId) == 0) {
-            ai.nonLocalizedLabel = v.coerceToString();
-        }
-
-        ai.icon = sa.getResourceId(
-                com.android.internal.R.styleable.AndroidManifestApplication_icon, 0);
-        ai.logo = sa.getResourceId(
-                com.android.internal.R.styleable.AndroidManifestApplication_logo, 0);
-        ai.banner = sa.getResourceId(
-                com.android.internal.R.styleable.AndroidManifestApplication_banner, 0);
         ai.theme = sa.getResourceId(
                 com.android.internal.R.styleable.AndroidManifestApplication_theme, 0);
         ai.descriptionRes = sa.getResourceId(
@@ -3332,25 +3330,35 @@
         return true;
     }
 
-    private boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
-            String[] outError, String tag, TypedArray sa,
-            int nameRes, int labelRes, int iconRes, int logoRes, int bannerRes) {
+    private static boolean parsePackageItemInfo(Package owner, PackageItemInfo outInfo,
+            String[] outError, String tag, TypedArray sa, boolean nameRequired,
+            int nameRes, int labelRes, int iconRes, int roundIconRes, int logoRes, int bannerRes) {
         String name = sa.getNonConfigurationString(nameRes, 0);
         if (name == null) {
-            outError[0] = tag + " does not specify android:name";
-            return false;
+            if (nameRequired) {
+                outError[0] = tag + " does not specify android:name";
+                return false;
+            }
+        } else {
+            outInfo.name
+                = buildClassName(owner.applicationInfo.packageName, name, outError);
+            if (outInfo.name == null) {
+                return false;
+            }
         }
 
-        outInfo.name
-            = buildClassName(owner.applicationInfo.packageName, name, outError);
-        if (outInfo.name == null) {
-            return false;
-        }
-
-        int iconVal = sa.getResourceId(iconRes, 0);
-        if (iconVal != 0) {
-            outInfo.icon = iconVal;
+        final boolean useRoundIcon =
+                Resources.getSystem().getBoolean(com.android.internal.R.bool.config_useRoundIcon);
+        int roundIconVal = useRoundIcon ? sa.getResourceId(roundIconRes, 0) : 0;
+        if (roundIconVal != 0) {
+            outInfo.icon = roundIconVal;
             outInfo.nonLocalizedLabel = null;
+        } else {
+            int iconVal = sa.getResourceId(iconRes, 0);
+            if (iconVal != 0) {
+                outInfo.icon = iconVal;
+                outInfo.nonLocalizedLabel = null;
+            }
         }
 
         int logoVal = sa.getResourceId(logoRes, 0);
@@ -3384,6 +3392,7 @@
                     R.styleable.AndroidManifestActivity_name,
                     R.styleable.AndroidManifestActivity_label,
                     R.styleable.AndroidManifestActivity_icon,
+                    R.styleable.AndroidManifestActivity_roundIcon,
                     R.styleable.AndroidManifestActivity_logo,
                     R.styleable.AndroidManifestActivity_banner,
                     mSeparateProcesses,
@@ -3758,6 +3767,7 @@
                     com.android.internal.R.styleable.AndroidManifestActivityAlias_name,
                     com.android.internal.R.styleable.AndroidManifestActivityAlias_label,
                     com.android.internal.R.styleable.AndroidManifestActivityAlias_icon,
+                    com.android.internal.R.styleable.AndroidManifestActivityAlias_roundIcon,
                     com.android.internal.R.styleable.AndroidManifestActivityAlias_logo,
                     com.android.internal.R.styleable.AndroidManifestActivityAlias_banner,
                     mSeparateProcesses,
@@ -3912,6 +3922,7 @@
                     com.android.internal.R.styleable.AndroidManifestProvider_name,
                     com.android.internal.R.styleable.AndroidManifestProvider_label,
                     com.android.internal.R.styleable.AndroidManifestProvider_icon,
+                    com.android.internal.R.styleable.AndroidManifestProvider_roundIcon,
                     com.android.internal.R.styleable.AndroidManifestProvider_logo,
                     com.android.internal.R.styleable.AndroidManifestProvider_banner,
                     mSeparateProcesses,
@@ -4231,6 +4242,7 @@
                     com.android.internal.R.styleable.AndroidManifestService_name,
                     com.android.internal.R.styleable.AndroidManifestService_label,
                     com.android.internal.R.styleable.AndroidManifestService_icon,
+                    com.android.internal.R.styleable.AndroidManifestService_roundIcon,
                     com.android.internal.R.styleable.AndroidManifestService_logo,
                     com.android.internal.R.styleable.AndroidManifestService_banner,
                     mSeparateProcesses,
@@ -4547,8 +4559,16 @@
             outInfo.nonLocalizedLabel = v.coerceToString();
         }
 
-        outInfo.icon = sa.getResourceId(
-                com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
+        final boolean useRoundIcon =
+                Resources.getSystem().getBoolean(com.android.internal.R.bool.config_useRoundIcon);
+        int roundIconVal = useRoundIcon ? sa.getResourceId(
+                com.android.internal.R.styleable.AndroidManifestIntentFilter_roundIcon, 0) : 0;
+        if (roundIconVal != 0) {
+            outInfo.icon = roundIconVal;
+        } else {
+            outInfo.icon = sa.getResourceId(
+                    com.android.internal.R.styleable.AndroidManifestIntentFilter_icon, 0);
+        }
 
         outInfo.logo = sa.getResourceId(
                 com.android.internal.R.styleable.AndroidManifestIntentFilter_logo, 0);
@@ -5177,45 +5197,13 @@
         public Component(final ParsePackageItemArgs args, final PackageItemInfo outInfo) {
             owner = args.owner;
             intents = new ArrayList<II>(0);
-            String name = args.sa.getNonConfigurationString(args.nameRes, 0);
-            if (name == null) {
+            if (parsePackageItemInfo(args.owner, outInfo, args.outError, args.tag, args.sa,
+                    true /*nameRequired*/, args.nameRes, args.labelRes, args.iconRes,
+                    args.roundIconRes, args.logoRes, args.bannerRes)) {
+                className = outInfo.name;
+            } else {
                 className = null;
-                args.outError[0] = args.tag + " does not specify android:name";
-                return;
             }
-
-            outInfo.name
-                = buildClassName(owner.applicationInfo.packageName, name, args.outError);
-            if (outInfo.name == null) {
-                className = null;
-                args.outError[0] = args.tag + " does not have valid android:name";
-                return;
-            }
-
-            className = outInfo.name;
-
-            int iconVal = args.sa.getResourceId(args.iconRes, 0);
-            if (iconVal != 0) {
-                outInfo.icon = iconVal;
-                outInfo.nonLocalizedLabel = null;
-            }
-
-            int logoVal = args.sa.getResourceId(args.logoRes, 0);
-            if (logoVal != 0) {
-                outInfo.logo = logoVal;
-            }
-
-            int bannerVal = args.sa.getResourceId(args.bannerRes, 0);
-            if (bannerVal != 0) {
-                outInfo.banner = bannerVal;
-            }
-
-            TypedValue v = args.sa.peekValue(args.labelRes);
-            if (v != null && (outInfo.labelRes=v.resourceId) == 0) {
-                outInfo.nonLocalizedLabel = v.coerceToString();
-            }
-
-            outInfo.packageName = owner.packageName;
         }
 
         public Component(final ParseComponentArgs args, final ComponentInfo outInfo) {
diff --git a/core/java/android/content/pm/RegisteredServicesCache.java b/core/java/android/content/pm/RegisteredServicesCache.java
index 693228f..aea843a 100644
--- a/core/java/android/content/pm/RegisteredServicesCache.java
+++ b/core/java/android/content/pm/RegisteredServicesCache.java
@@ -30,6 +30,7 @@
 import android.os.UserManager;
 import android.util.AtomicFile;
 import android.util.AttributeSet;
+import android.util.IntArray;
 import android.util.Log;
 import android.util.Slog;
 import android.util.SparseArray;
@@ -54,6 +55,7 @@
 import java.io.PrintWriter;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -344,6 +346,47 @@
         }
     }
 
+    public void updateServices(int userId) {
+        if (DEBUG) {
+            Slog.d(TAG, "updateServices u" + userId);
+        }
+        List<ServiceInfo<V>> allServices;
+        synchronized (mServicesLock) {
+            final UserServices<V> user = findOrCreateUserLocked(userId);
+            // If services haven't been initialized yet - no updates required
+            if (user.services == null) {
+                return;
+            }
+            allServices = new ArrayList<>(user.services.values());
+        }
+        IntArray updatedUids = null;
+        for (ServiceInfo<V> service : allServices) {
+            int versionCode = service.componentInfo.applicationInfo.versionCode;
+            String pkg = service.componentInfo.packageName;
+            ApplicationInfo newAppInfo = null;
+            try {
+                newAppInfo = mContext.getPackageManager().getApplicationInfoAsUser(pkg, 0, userId);
+            } catch (NameNotFoundException e) {
+                // Package uninstalled - treat as null app info
+            }
+            // If package updated or removed
+            if ((newAppInfo == null) || (newAppInfo.versionCode != versionCode)) {
+                if (DEBUG) {
+                    Slog.d(TAG, "Package " + pkg + " uid=" + service.uid
+                            + " updated. New appInfo: " + newAppInfo);
+                }
+                if (updatedUids == null) {
+                    updatedUids = new IntArray();
+                }
+                updatedUids.add(service.uid);
+            }
+        }
+        if (updatedUids != null && updatedUids.size() > 0) {
+            int[] updatedUidsArray = updatedUids.toArray();
+            generateServicesMap(updatedUidsArray, userId);
+        }
+    }
+
     @VisibleForTesting
     protected boolean inSystemImage(int callerUid) {
         String[] packages = mContext.getPackageManager().getPackagesForUid(callerUid);
@@ -381,10 +424,11 @@
      */
     private void generateServicesMap(int[] changedUids, int userId) {
         if (DEBUG) {
-            Slog.d(TAG, "generateServicesMap() for " + userId + ", changed UIDs = " + changedUids);
+            Slog.d(TAG, "generateServicesMap() for " + userId + ", changed UIDs = "
+                    + Arrays.toString(changedUids));
         }
 
-        final ArrayList<ServiceInfo<V>> serviceInfos = new ArrayList<ServiceInfo<V>>();
+        final ArrayList<ServiceInfo<V>> serviceInfos = new ArrayList<>();
         final List<ResolveInfo> resolveInfos = queryIntentServices(userId);
         for (ResolveInfo resolveInfo : resolveInfos) {
             try {
diff --git a/core/java/android/content/pm/ResolveInfo.java b/core/java/android/content/pm/ResolveInfo.java
index c9be6ed..b5df4d7 100644
--- a/core/java/android/content/pm/ResolveInfo.java
+++ b/core/java/android/content/pm/ResolveInfo.java
@@ -68,12 +68,6 @@
     public EphemeralResolveInfo ephemeralResolveInfo;
 
     /**
-     * A ResolveInfo that points at the ephemeral installer.
-     * @hide
-     */
-    public ResolveInfo ephemeralInstaller;
-
-    /**
      * The IntentFilter that was matched for this ResolveInfo.
      */
     public IntentFilter filter;
diff --git a/core/java/android/content/pm/ShortcutInfo.java b/core/java/android/content/pm/ShortcutInfo.java
index 4340d04..ed0ac53 100644
--- a/core/java/android/content/pm/ShortcutInfo.java
+++ b/core/java/android/content/pm/ShortcutInfo.java
@@ -19,57 +19,79 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.UserIdInt;
+import android.app.TaskStackBuilder;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.LauncherApps.ShortcutQuery;
+import android.content.res.Resources;
+import android.content.res.Resources.NotFoundException;
+import android.graphics.Bitmap;
 import android.graphics.drawable.Icon;
 import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.os.PersistableBundle;
 import android.os.UserHandle;
+import android.text.TextUtils;
 import android.util.ArraySet;
+import android.util.Log;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.Preconditions;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
+import java.util.List;
 import java.util.Set;
 
-// TODO Enhance javadoc
 /**
+ * Represents a "launcher shortcut" that can be published via {@link ShortcutManager}.
  *
- * Represents a shortcut from an application.
- *
- * <p>Notes about icons:
- * <ul>
- *     <li>If an {@link Icon} is a resource, the system keeps the package name and the resource ID.
- *     Otherwise, the bitmap is fetched when it's registered to ShortcutManager,
- *     then shrunk if necessary, and persisted.
- *     <li>The system disallows byte[] icons, because they can easily go over the binder size limit.
- * </ul>
- *
- * @see {@link ShortcutManager}.
- *
- * @hide
+ * @see ShortcutManager
  */
 public final class ShortcutInfo implements Parcelable {
-    /* @hide */
+    static final String TAG = "Shortcut";
+
+    private static final String RES_TYPE_STRING = "string";
+
+    private static final String ANDROID_PACKAGE_NAME = "android";
+
+    private static final int IMPLICIT_RANK_MASK = 0x7fffffff;
+
+    private static final int RANK_CHANGED_BIT = ~IMPLICIT_RANK_MASK;
+
+    /** @hide */
+    public static final int RANK_NOT_SET = Integer.MAX_VALUE;
+
+    /** @hide */
     public static final int FLAG_DYNAMIC = 1 << 0;
 
-    /* @hide */
+    /** @hide */
     public static final int FLAG_PINNED = 1 << 1;
 
-    /* @hide */
+    /** @hide */
     public static final int FLAG_HAS_ICON_RES = 1 << 2;
 
-    /* @hide */
+    /** @hide */
     public static final int FLAG_HAS_ICON_FILE = 1 << 3;
 
-    /* @hide */
+    /** @hide */
     public static final int FLAG_KEY_FIELDS_ONLY = 1 << 4;
 
     /** @hide */
+    public static final int FLAG_MANIFEST = 1 << 5;
+
+    /** @hide */
+    public static final int FLAG_DISABLED = 1 << 6;
+
+    /** @hide */
+    public static final int FLAG_STRINGS_RESOLVED = 1 << 7;
+
+    /** @hide */
+    public static final int FLAG_IMMUTABLE = 1 << 8;
+
+    /** @hide */
     @IntDef(flag = true,
             value = {
             FLAG_DYNAMIC,
@@ -77,26 +99,34 @@
             FLAG_HAS_ICON_RES,
             FLAG_HAS_ICON_FILE,
             FLAG_KEY_FIELDS_ONLY,
+            FLAG_MANIFEST,
+            FLAG_DISABLED,
+            FLAG_STRINGS_RESOLVED,
+            FLAG_IMMUTABLE,
     })
     @Retention(RetentionPolicy.SOURCE)
     public @interface ShortcutFlags {}
 
     // Cloning options.
 
-    /* @hide */
+    /** @hide */
     private static final int CLONE_REMOVE_ICON = 1 << 0;
 
-    /* @hide */
+    /** @hide */
     private static final int CLONE_REMOVE_INTENT = 1 << 1;
 
-    /* @hide */
+    /** @hide */
     public static final int CLONE_REMOVE_NON_KEY_INFO = 1 << 2;
 
-    /* @hide */
-    public static final int CLONE_REMOVE_FOR_CREATOR = CLONE_REMOVE_ICON;
+    /** @hide */
+    public static final int CLONE_REMOVE_RES_NAMES = 1 << 3;
 
-    /* @hide */
-    public static final int CLONE_REMOVE_FOR_LAUNCHER = CLONE_REMOVE_ICON | CLONE_REMOVE_INTENT;
+    /** @hide */
+    public static final int CLONE_REMOVE_FOR_CREATOR = CLONE_REMOVE_ICON | CLONE_REMOVE_RES_NAMES;
+
+    /** @hide */
+    public static final int CLONE_REMOVE_FOR_LAUNCHER = CLONE_REMOVE_ICON | CLONE_REMOVE_INTENT
+            | CLONE_REMOVE_RES_NAMES;
 
     /** @hide */
     @IntDef(flag = true,
@@ -104,6 +134,7 @@
                     CLONE_REMOVE_ICON,
                     CLONE_REMOVE_INTENT,
                     CLONE_REMOVE_NON_KEY_INFO,
+                    CLONE_REMOVE_RES_NAMES,
                     CLONE_REMOVE_FOR_CREATOR,
                     CLONE_REMOVE_FOR_LAUNCHER
             })
@@ -111,7 +142,7 @@
     public @interface CloneFlags {}
 
     /**
-     * Shortcut category for
+     * Shortcut category for messaging related actions, such as chat.
      */
     public static final String SHORTCUT_CATEGORY_CONVERSATION = "android.shortcut.conversation";
 
@@ -121,33 +152,57 @@
     private final String mPackageName;
 
     @Nullable
-    private ComponentName mActivityComponent;
+    private ComponentName mActivity;
 
     @Nullable
     private Icon mIcon;
 
-    @NonNull
-    private String mTitle;
+    private int mTitleResId;
+
+    private String mTitleResName;
 
     @Nullable
-    private String mText;
+    private CharSequence mTitle;
 
-    @NonNull
+    private int mTextResId;
+
+    private String mTextResName;
+
+    @Nullable
+    private CharSequence mText;
+
+    private int mDisabledMessageResId;
+
+    private String mDisabledMessageResName;
+
+    @Nullable
+    private CharSequence mDisabledMessage;
+
+    @Nullable
     private ArraySet<String> mCategories;
 
     /**
-     * Intent *with extras removed*.
+     * Intents *with extras removed*.
      */
-    @NonNull
-    private Intent mIntent;
+    @Nullable
+    private Intent[] mIntents;
 
     /**
-     * Extras for the intent.
+     * Extras for the intents.
      */
-    @NonNull
-    private PersistableBundle mIntentPersistableExtras;
+    @Nullable
+    private PersistableBundle[] mIntentPersistableExtrases;
 
-    private int mWeight;
+    private int mRank;
+
+    /**
+     * Internally used for auto-rank-adjustment.
+     *
+     * RANK_CHANGED_BIT is used to denote that the rank of a shortcut is changing.
+     * The rest of the bits are used to denote the order in which shortcuts are passed to
+     * APIs, which is used to preserve the argument order when ranks are tie.
+     */
+    private int mImplicitRank;
 
     @Nullable
     private PersistableBundle mExtras;
@@ -159,7 +214,9 @@
     private int mFlags;
 
     // Internal use only.
-    private int mIconResourceId;
+    private int mIconResId;
+
+    private String mIconResName;
 
     // Internal use only.
     @Nullable
@@ -175,26 +232,81 @@
         // Note we can't do other null checks here because SM.updateShortcuts() takes partial
         // information.
         mPackageName = b.mContext.getPackageName();
-        mActivityComponent = b.mActivityComponent;
+        mActivity = b.mActivity;
         mIcon = b.mIcon;
         mTitle = b.mTitle;
+        mTitleResId = b.mTitleResId;
         mText = b.mText;
-        mCategories = clone(b.mCategories);
-        mIntent = b.mIntent;
-        if (mIntent != null) {
-            final Bundle intentExtras = mIntent.getExtras();
-            if (intentExtras != null) {
-                mIntent.replaceExtras((Bundle) null);
-                mIntentPersistableExtras = new PersistableBundle(intentExtras);
-            }
-        }
-        mWeight = b.mWeight;
+        mTextResId = b.mTextResId;
+        mDisabledMessage = b.mDisabledMessage;
+        mDisabledMessageResId = b.mDisabledMessageResId;
+        mCategories = cloneCategories(b.mCategories);
+        mIntents = cloneIntents(b.mIntents);
+        fixUpIntentExtras();
+        mRank = b.mRank;
         mExtras = b.mExtras;
         updateTimestamp();
     }
 
-    private <T> ArraySet<T> clone(Set<T> source) {
-        return (source == null) ? null : new ArraySet<>(source);
+    /**
+     * Extract extras from {@link #mIntents} and set them to {@link #mIntentPersistableExtrases}
+     * as {@link PersistableBundle}, and remove extras from the original intents.
+     */
+    private void fixUpIntentExtras() {
+        if (mIntents == null) {
+            mIntentPersistableExtrases = null;
+            return;
+        }
+        mIntentPersistableExtrases = new PersistableBundle[mIntents.length];
+        for (int i = 0; i < mIntents.length; i++) {
+            final Intent intent = mIntents[i];
+            final Bundle extras = intent.getExtras();
+            if (extras == null) {
+                mIntentPersistableExtrases[i] = null;
+            } else {
+                mIntentPersistableExtrases[i] = new PersistableBundle(extras);
+                intent.replaceExtras((Bundle) null);
+            }
+        }
+    }
+
+    private static ArraySet<String> cloneCategories(Set<String> source) {
+        if (source == null) {
+            return null;
+        }
+        final ArraySet<String> ret = new ArraySet<>(source.size());
+        for (CharSequence s : source) {
+            if (!TextUtils.isEmpty(s)) {
+                ret.add(s.toString().intern());
+            }
+        }
+        return ret;
+    }
+
+    private static Intent[] cloneIntents(Intent[] intents) {
+        if (intents == null) {
+            return null;
+        }
+        final Intent[] ret = new Intent[intents.length];
+        for (int i = 0; i < ret.length; i++) {
+            if (intents[i] != null) {
+                ret[i] = new Intent(intents[i]);
+            }
+        }
+        return ret;
+    }
+
+    private static PersistableBundle[] clonePersistableBundle(PersistableBundle[] bundle) {
+        if (bundle == null) {
+            return null;
+        }
+        final PersistableBundle[] ret = new PersistableBundle[bundle.length];
+        for (int i = 0; i < ret.length; i++) {
+            if (bundle[i] != null) {
+                ret[i] = new PersistableBundle(bundle[i]);
+            }
+        }
+        return ret;
     }
 
     /**
@@ -204,8 +316,12 @@
      */
     public void enforceMandatoryFields() {
         Preconditions.checkStringNotEmpty(mId, "Shortcut ID must be provided");
-        Preconditions.checkStringNotEmpty(mTitle, "Shortcut title must be provided");
-        Preconditions.checkNotNull(mIntent, "Shortcut Intent must be provided");
+        Preconditions.checkNotNull(mActivity, "Activity must be provided");
+        if (mTitle == null && mTitleResId == 0) {
+            throw new IllegalArgumentException("Short label must be provided");
+        }
+        Preconditions.checkNotNull(mIntents, "Shortcut Intent must be provided");
+        Preconditions.checkArgument(mIntents.length > 0, "Shortcut Intent must be provided");
     }
 
     /**
@@ -215,14 +331,14 @@
         mUserId = source.mUserId;
         mId = source.mId;
         mPackageName = source.mPackageName;
+        mActivity = source.mActivity;
         mFlags = source.mFlags;
         mLastChangedTimestamp = source.mLastChangedTimestamp;
 
         // Just always keep it since it's cheep.
-        mIconResourceId = source.mIconResourceId;
+        mIconResId = source.mIconResId;
 
         if ((cloneFlags & CLONE_REMOVE_NON_KEY_INFO) == 0) {
-            mActivityComponent = source.mActivityComponent;
 
             if ((cloneFlags & CLONE_REMOVE_ICON) == 0) {
                 mIcon = source.mIcon;
@@ -230,14 +346,26 @@
             }
 
             mTitle = source.mTitle;
+            mTitleResId = source.mTitleResId;
             mText = source.mText;
-            mCategories = clone(source.mCategories);
+            mTextResId = source.mTextResId;
+            mDisabledMessage = source.mDisabledMessage;
+            mDisabledMessageResId = source.mDisabledMessageResId;
+            mCategories = cloneCategories(source.mCategories);
             if ((cloneFlags & CLONE_REMOVE_INTENT) == 0) {
-                mIntent = source.mIntent;
-                mIntentPersistableExtras = source.mIntentPersistableExtras;
+                mIntents = cloneIntents(source.mIntents);
+                mIntentPersistableExtrases =
+                        clonePersistableBundle(source.mIntentPersistableExtrases);
             }
-            mWeight = source.mWeight;
+            mRank = source.mRank;
             mExtras = source.mExtras;
+
+            if ((cloneFlags & CLONE_REMOVE_RES_NAMES) == 0) {
+                mTitleResName = source.mTitleResName;
+                mTextResName = source.mTextResName;
+                mDisabledMessageResName = source.mDisabledMessageResName;
+                mIconResName = source.mIconResName;
+            }
         } else {
             // Set this bit.
             mFlags |= FLAG_KEY_FIELDS_ONLY;
@@ -245,6 +373,221 @@
     }
 
     /**
+     * Load a string resource from the publisher app.
+     *
+     * @param resId resource ID
+     * @param defValue default value to be returned when the specified resource isn't found.
+     */
+    private CharSequence getResourceString(Resources res, int resId, CharSequence defValue) {
+        try {
+            return res.getString(resId);
+        } catch (NotFoundException e) {
+            Log.e(TAG, "Resource for ID=" + resId + " not found in package " + mPackageName);
+            return defValue;
+        }
+    }
+
+    /**
+     * Load the string resources for the text fields and set them to the actual value fields.
+     * This will set {@link #FLAG_STRINGS_RESOLVED}.
+     *
+     * @param res {@link Resources} for the publisher.  Must have been loaded with
+     * {@link PackageManager#getResourcesForApplicationAsUser}.
+     *
+     * @hide
+     */
+    public void resolveResourceStrings(@NonNull Resources res) {
+        mFlags |= FLAG_STRINGS_RESOLVED;
+
+        if ((mTitleResId == 0) && (mTextResId == 0) && (mDisabledMessageResId == 0)) {
+            return; // Bail early.
+        }
+
+        if (mTitleResId != 0) {
+            mTitle = getResourceString(res, mTitleResId, mTitle);
+        }
+        if (mTextResId != 0) {
+            mText = getResourceString(res, mTextResId, mText);
+        }
+        if (mDisabledMessageResId != 0) {
+            mDisabledMessage = getResourceString(res, mDisabledMessageResId, mDisabledMessage);
+        }
+    }
+
+    /**
+     * Look up resource name for a given resource ID.
+     *
+     * @return a simple resource name (e.g. "text_1") when {@code withType} is false, or with the
+     * type (e.g. "string/text_1").
+     *
+     * @hide
+     */
+    @VisibleForTesting
+    public static String lookUpResourceName(@NonNull Resources res, int resId, boolean withType,
+            @NonNull String packageName) {
+        if (resId == 0) {
+            return null;
+        }
+        try {
+            final String fullName = res.getResourceName(resId);
+
+            if (ANDROID_PACKAGE_NAME.equals(getResourcePackageName(fullName))) {
+                // If it's a framework resource, the value won't change, so just return the ID
+                // value as a string.
+                return String.valueOf(resId);
+            }
+            return withType ? getResourceTypeAndEntryName(fullName)
+                    : getResourceEntryName(fullName);
+        } catch (NotFoundException e) {
+            Log.e(TAG, "Resource name for ID=" + resId + " not found in package " + packageName
+                    + ". Resource IDs may change when the application is upgraded, and the system"
+                    + " may not be able to find the correct resource.");
+            return null;
+        }
+    }
+
+    /**
+     * Extract the package name from a fully-donated resource name.
+     * e.g. "com.android.app1:drawable/icon1" -> "com.android.app1"
+     * @hide
+     */
+    @VisibleForTesting
+    public static String getResourcePackageName(@NonNull String fullResourceName) {
+        final int p1 = fullResourceName.indexOf(':');
+        if (p1 < 0) {
+            return null;
+        }
+        return fullResourceName.substring(0, p1);
+    }
+
+    /**
+     * Extract the type name from a fully-donated resource name.
+     * e.g. "com.android.app1:drawable/icon1" -> "drawable"
+     * @hide
+     */
+    @VisibleForTesting
+    public static String getResourceTypeName(@NonNull String fullResourceName) {
+        final int p1 = fullResourceName.indexOf(':');
+        if (p1 < 0) {
+            return null;
+        }
+        final int p2 = fullResourceName.indexOf('/', p1 + 1);
+        if (p2 < 0) {
+            return null;
+        }
+        return fullResourceName.substring(p1 + 1, p2);
+    }
+
+    /**
+     * Extract the type name + the entry name from a fully-donated resource name.
+     * e.g. "com.android.app1:drawable/icon1" -> "drawable/icon1"
+     * @hide
+     */
+    @VisibleForTesting
+    public static String getResourceTypeAndEntryName(@NonNull String fullResourceName) {
+        final int p1 = fullResourceName.indexOf(':');
+        if (p1 < 0) {
+            return null;
+        }
+        return fullResourceName.substring(p1 + 1);
+    }
+
+    /**
+     * Extract the entry name from a fully-donated resource name.
+     * e.g. "com.android.app1:drawable/icon1" -> "icon1"
+     * @hide
+     */
+    @VisibleForTesting
+    public static String getResourceEntryName(@NonNull String fullResourceName) {
+        final int p1 = fullResourceName.indexOf('/');
+        if (p1 < 0) {
+            return null;
+        }
+        return fullResourceName.substring(p1 + 1);
+    }
+
+    /**
+     * Return the resource ID for a given resource ID.
+     *
+     * Basically its' a wrapper over {@link Resources#getIdentifier(String, String, String)}, except
+     * if {@code resourceName} is an integer then it'll just return its value.  (Which also the
+     * aforementioned method would do internally, but not documented, so doing here explicitly.)
+     *
+     * @param res {@link Resources} for the publisher.  Must have been loaded with
+     * {@link PackageManager#getResourcesForApplicationAsUser}.
+     *
+     * @hide
+     */
+    @VisibleForTesting
+    public static int lookUpResourceId(@NonNull Resources res, @Nullable String resourceName,
+            @Nullable String resourceType, String packageName) {
+        if (resourceName == null) {
+            return 0;
+        }
+        try {
+            try {
+                // It the name can be parsed as an integer, just use it.
+                return Integer.parseInt(resourceName);
+            } catch (NumberFormatException ignore) {
+            }
+
+            return res.getIdentifier(resourceName, resourceType, packageName);
+        } catch (NotFoundException e) {
+            Log.e(TAG, "Resource ID for name=" + resourceName + " not found in package "
+                    + packageName);
+            return 0;
+        }
+    }
+
+    /**
+     * Look up resource names from the resource IDs for the icon res and the text fields, and fill
+     * in the resource name fields.
+     *
+     * @param res {@link Resources} for the publisher.  Must have been loaded with
+     * {@link PackageManager#getResourcesForApplicationAsUser}.
+     *
+     * @hide
+     */
+    public void lookupAndFillInResourceNames(@NonNull Resources res) {
+        if ((mTitleResId == 0) && (mTextResId == 0) && (mDisabledMessageResId == 0)
+                && (mIconResId == 0)) {
+            return; // Bail early.
+        }
+
+        // We don't need types for strings because their types are always "string".
+        mTitleResName = lookUpResourceName(res, mTitleResId, /*withType=*/ false, mPackageName);
+        mTextResName = lookUpResourceName(res, mTextResId, /*withType=*/ false, mPackageName);
+        mDisabledMessageResName = lookUpResourceName(res, mDisabledMessageResId,
+                /*withType=*/ false, mPackageName);
+
+        // But icons have multiple possible types, so include the type.
+        mIconResName = lookUpResourceName(res, mIconResId, /*withType=*/ true, mPackageName);
+    }
+
+    /**
+     * Look up resource IDs from the resource names for the icon res and the text fields, and fill
+     * in the resource ID fields.
+     *
+     * This is called when an app is updated.
+     *
+     * @hide
+     */
+    public void lookupAndFillInResourceIds(@NonNull Resources res) {
+        if ((mTitleResName == null) && (mTextResName == null) && (mDisabledMessageResName == null)
+                && (mIconResName == null)) {
+            return; // Bail early.
+        }
+
+        mTitleResId = lookUpResourceId(res, mTitleResName, RES_TYPE_STRING, mPackageName);
+        mTextResId = lookUpResourceId(res, mTextResName, RES_TYPE_STRING, mPackageName);
+        mDisabledMessageResId = lookUpResourceId(res, mDisabledMessageResName, RES_TYPE_STRING,
+                mPackageName);
+
+        // mIconResName already contains the type, so the third argument is not needed.
+        mIconResId = lookUpResourceId(res, mIconResName, null, mPackageName);
+    }
+
+    /**
      * Copy a {@link ShortcutInfo}, optionally removing fields.
      * @hide
      */
@@ -253,49 +596,85 @@
     }
 
     /**
+     * @hide
+     */
+    public void ensureUpdatableWith(ShortcutInfo source) {
+        Preconditions.checkState(mUserId == source.mUserId, "Owner User ID must match");
+        Preconditions.checkState(mId.equals(source.mId), "ID must match");
+        Preconditions.checkState(mPackageName.equals(source.mPackageName),
+                "Package name must match");
+        Preconditions.checkState(!isImmutable(), "Target ShortcutInfo is immutable");
+    }
+
+    /**
      * Copy non-null/zero fields from another {@link ShortcutInfo}.  Only "public" information
-     * will be overwritten.  The timestamp will be updated.
+     * will be overwritten.  The timestamp will *not* be updated to be consistent with other
+     * setters (and also the clock is not injectable in this file).
      *
      * - Flags will not change
      * - mBitmapPath will not change
      * - Current time will be set to timestamp
      *
+     * @throws IllegalStateException if source is not compatible.
+     *
      * @hide
      */
     public void copyNonNullFieldsFrom(ShortcutInfo source) {
-        Preconditions.checkState(mUserId == source.mUserId, "Owner User ID must match");
-        Preconditions.checkState(mId.equals(source.mId), "ID must match");
-        Preconditions.checkState(mPackageName.equals(source.mPackageName),
-                "Package name must match");
+        ensureUpdatableWith(source);
 
-        if (source.mActivityComponent != null) {
-            mActivityComponent = source.mActivityComponent;
+        if (source.mActivity != null) {
+            mActivity = source.mActivity;
         }
 
         if (source.mIcon != null) {
             mIcon = source.mIcon;
+
+            mIconResId = 0;
+            mIconResName = null;
+            mBitmapPath = null;
         }
         if (source.mTitle != null) {
             mTitle = source.mTitle;
+            mTitleResId = 0;
+            mTitleResName = null;
+        } else if (source.mTitleResId != 0) {
+            mTitle = null;
+            mTitleResId = source.mTitleResId;
+            mTitleResName = null;
         }
+
         if (source.mText != null) {
             mText = source.mText;
+            mTextResId = 0;
+            mTextResName = null;
+        } else if (source.mTextResId != 0) {
+            mText = null;
+            mTextResId = source.mTextResId;
+            mTextResName = null;
+        }
+        if (source.mDisabledMessage != null) {
+            mDisabledMessage = source.mDisabledMessage;
+            mDisabledMessageResId = 0;
+            mDisabledMessageResName = null;
+        } else if (source.mDisabledMessageResId != 0) {
+            mDisabledMessage = null;
+            mDisabledMessageResId = source.mDisabledMessageResId;
+            mDisabledMessageResName = null;
         }
         if (source.mCategories != null) {
-            mCategories = clone(source.mCategories);
+            mCategories = cloneCategories(source.mCategories);
         }
-        if (source.mIntent != null) {
-            mIntent = source.mIntent;
-            mIntentPersistableExtras = source.mIntentPersistableExtras;
+        if (source.mIntents != null) {
+            mIntents = cloneIntents(source.mIntents);
+            mIntentPersistableExtrases =
+                    clonePersistableBundle(source.mIntentPersistableExtrases);
         }
-        if (source.mWeight != 0) {
-            mWeight = source.mWeight;
+        if (source.mRank != RANK_NOT_SET) {
+            mRank = source.mRank;
         }
         if (source.mExtras != null) {
             mExtras = source.mExtras;
         }
-
-        updateTimestamp();
     }
 
     /**
@@ -310,7 +689,6 @@
                 throw getInvalidIconException();
         }
         if (icon.hasTint()) {
-            // TODO support it
             throw new IllegalArgumentException("Icons with tints are not supported");
         }
 
@@ -320,77 +698,123 @@
     /** @hide */
     public static IllegalArgumentException getInvalidIconException() {
         return new IllegalArgumentException("Unsupported icon type:"
-                +" only bitmap, resource and content URI are supported");
+                +" only the bitmap and resource types are supported");
     }
 
     /**
      * Builder class for {@link ShortcutInfo} objects.
+     *
+     * @see ShortcutManager
      */
     public static class Builder {
         private final Context mContext;
 
         private String mId;
 
-        private ComponentName mActivityComponent;
+        private ComponentName mActivity;
 
         private Icon mIcon;
 
-        private String mTitle;
+        private int mTitleResId;
 
-        private String mText;
+        private CharSequence mTitle;
+
+        private int mTextResId;
+
+        private CharSequence mText;
+
+        private int mDisabledMessageResId;
+
+        private CharSequence mDisabledMessage;
 
         private Set<String> mCategories;
 
-        private Intent mIntent;
+        private Intent[] mIntents;
 
-        private int mWeight;
+        private int mRank = RANK_NOT_SET;
 
         private PersistableBundle mExtras;
 
-        /** Constructor. */
+        /**
+         * Old style constructor.
+         * @hide
+         */
+        @Deprecated
         public Builder(Context context) {
             mContext = context;
         }
 
         /**
-         * Sets the ID of the shortcut.  This is a mandatory field.
+         * Used with the old style constructor, kept for unit tests.
+         * @hide
          */
         @NonNull
+        @Deprecated
         public Builder setId(@NonNull String id) {
-            mId = Preconditions.checkStringNotEmpty(id, "id");
+            mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty");
             return this;
         }
 
         /**
-         * Optionally sets the target activity.  If it's not set, and if the caller application
-         * has multiple launcher icons, this shortcut will be shown on all those icons.
-         * If it's set, this shortcut will be only shown on this activity.
+         * Constructor.
          *
-         * <p>The package name of the target activity must match the package name of the shortcut
-         * publisher.
-         *
-         * <p>This has nothing to do with the activity that this shortcut will launch.  This is
-         * a hint to the launcher app about which launcher icon to associate this shortcut with.
+         * @param context Client context.
+         * @param id ID of the shortcut.
          */
-        @NonNull
-        public Builder setActivityComponent(@NonNull ComponentName activityComponent) {
-            mActivityComponent = Preconditions.checkNotNull(activityComponent, "activityComponent");
-            return this;
+        public Builder(Context context, String id) {
+            mContext = context;
+            mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty");
         }
 
         /**
-         * Optionally sets an icon.
+         * Sets the target activity.  A shortcut will be shown along with this activity's icon
+         * on the launcher.
          *
+         * When selecting a target activity, keep the following in mind:
          * <ul>
-         *     <li>Tints set by {@link Icon#setTint} or {@link Icon#setTintList} are not supported.
-         *     <li>Bitmaps and resources are supported, but "content:" URIs are not supported.
+         * <li>All dynamic shortcuts must have a target activity.  When a shortcut with no target
+         * activity is published using
+         * {@link ShortcutManager#addDynamicShortcuts(List)} or
+         * {@link ShortcutManager#setDynamicShortcuts(List)},
+         * the first main activity defined in the application's <code>AndroidManifest.xml</code>
+         * file is used.
+         *
+         * <li>Only "main" activities&mdash;ones that define the {@link Intent#ACTION_MAIN}
+         * and {@link Intent#CATEGORY_LAUNCHER} intent filters&mdash;can be target
+         * activities.
+         *
+         * <li>By default, the first main activity defined in the application manifest is
+         * the target activity.
+         *
+         * <li>A target activity must belong to the publisher application.
          * </ul>
          *
-         * <p>For performance reasons, icons will <b>NOT</b> be available on instances
-         * returned by {@link ShortcutManager} or {@link LauncherApps}.  Launcher applications
-         * can use {@link ShortcutInfo#getIconResourceId()} if {@link #hasIconResource()} is true.
-         * Otherwise, if {@link #hasIconFile()} is true, use
-         * {@link LauncherApps#getShortcutIconFd} to load the image.
+         * @see ShortcutInfo#getActivity()
+         */
+        @NonNull
+        public Builder setActivity(@NonNull ComponentName activity) {
+            mActivity = Preconditions.checkNotNull(activity, "activity cannot be null");
+            return this;
+        }
+
+        /**
+         * Sets an icon of a shortcut.
+         *
+         * <p>Icons are not available on {@link ShortcutInfo} instances
+         * returned by {@link ShortcutManager} or {@link LauncherApps}.  The default launcher
+         * application can use {@link LauncherApps#getShortcutIconDrawable(ShortcutInfo, int)}
+         * or {@link LauncherApps#getShortcutBadgedIconDrawable(ShortcutInfo, int)} to fetch
+         * shortcut icons.
+         *
+         * <p>Tints set with {@link Icon#setTint} or {@link Icon#setTintList} are not supported
+         * and will be ignored.
+         *
+         * <p>Only icons created with {@link Icon#createWithBitmap(Bitmap)} and
+         * {@link Icon#createWithResource} are supported.
+         * Other types, such as URI-based icons, are not supported.
+         *
+         * @see LauncherApps#getShortcutIconDrawable(ShortcutInfo, int)
+         * @see LauncherApps#getShortcutBadgedIconDrawable(ShortcutInfo, int)
          */
         @NonNull
         public Builder setIcon(Icon icon) {
@@ -399,26 +823,112 @@
         }
 
         /**
-         * Sets the title of a shortcut.  This is a mandatory field.
-         *
-         * <p>This field is intended for a concise description of a shortcut displayed under
-         * an icon.  The recommend max length is 10 characters.
+         * @hide We don't support resource strings for dynamic shortcuts for now.  (But unit tests
+         * use it.)
          */
-        @NonNull
-        public Builder setTitle(@NonNull String title) {
-            mTitle = Preconditions.checkStringNotEmpty(title, "title");
+        @Deprecated
+        public Builder setShortLabelResId(int shortLabelResId) {
+            Preconditions.checkState(mTitle == null, "shortLabel already set");
+            mTitleResId = shortLabelResId;
             return this;
         }
 
         /**
-         * Sets the text of a shortcut.  This is an optional field.
+         * Sets the short title of a shortcut.
          *
-         * <p>This field is intended to be more descriptive than the shortcut title.
-         * The recommend max length is 25 characters.
+         * <p>This is a mandatory field when publishing a new shortcut with
+         * {@link ShortcutManager#addDynamicShortcuts(List)} or
+         * {@link ShortcutManager#setDynamicShortcuts(List)}.
+         *
+         * <p>This field is intended to be a concise description of a shortcut.
+         *
+         * <p>The recommended maximum length is 10 characters.
+         *
+         * @see ShortcutInfo#getShortLabel()
          */
         @NonNull
-        public Builder setText(@NonNull String text) {
-            mText = Preconditions.checkStringNotEmpty(text, "text");
+        public Builder setShortLabel(@NonNull CharSequence shortLabel) {
+            Preconditions.checkState(mTitleResId == 0, "shortLabelResId already set");
+            mTitle = Preconditions.checkStringNotEmpty(shortLabel, "shortLabel cannot be empty");
+            return this;
+        }
+
+        /**
+         * @hide We don't support resource strings for dynamic shortcuts for now.  (But unit tests
+         * use it.)
+         */
+        @Deprecated
+        public Builder setLongLabelResId(int longLabelResId) {
+            Preconditions.checkState(mText == null, "longLabel already set");
+            mTextResId = longLabelResId;
+            return this;
+        }
+
+        /**
+         * Sets the text of a shortcut.
+         *
+         * <p>This field is intended to be more descriptive than the shortcut title.  The launcher
+         * shows this instead of the short title when it has enough space.
+         *
+         * <p>The recommend maximum length is 25 characters.
+         *
+         * @see ShortcutInfo#getLongLabel()
+         */
+        @NonNull
+        public Builder setLongLabel(@NonNull CharSequence longLabel) {
+            Preconditions.checkState(mTextResId == 0, "longLabelResId already set");
+            mText = Preconditions.checkStringNotEmpty(longLabel, "longLabel cannot be empty");
+            return this;
+        }
+
+        /** @hide -- old signature, the internal code still uses it. */
+        @Deprecated
+        public Builder setTitle(@NonNull CharSequence value) {
+            return setShortLabel(value);
+        }
+
+        /** @hide -- old signature, the internal code still uses it. */
+        @Deprecated
+        public Builder setTitleResId(int value) {
+            return setShortLabelResId(value);
+        }
+
+        /** @hide -- old signature, the internal code still uses it. */
+        @Deprecated
+        public Builder setText(@NonNull CharSequence value) {
+            return setLongLabel(value);
+        }
+
+        /** @hide -- old signature, the internal code still uses it. */
+        @Deprecated
+        public Builder setTextResId(int value) {
+            return setLongLabelResId(value);
+        }
+
+        /**
+         * @hide We don't support resource strings for dynamic shortcuts for now.  (But unit tests
+         * use it.)
+         */
+        @Deprecated
+        public Builder setDisabledMessageResId(int disabledMessageResId) {
+            Preconditions.checkState(mDisabledMessage == null, "disabledMessage already set");
+            mDisabledMessageResId = disabledMessageResId;
+            return this;
+        }
+
+        /**
+         * Sets the message that should be shown when the user attempts to start a shortcut that
+         * is disabled.
+         *
+         * @see ShortcutInfo#getDisabledMessage()
+         */
+        @NonNull
+        public Builder setDisabledMessage(@NonNull CharSequence disabledMessage) {
+            Preconditions.checkState(
+                    mDisabledMessageResId == 0, "disabledMessageResId already set");
+            mDisabledMessage =
+                    Preconditions.checkStringNotEmpty(disabledMessage,
+                            "disabledMessage cannot be empty");
             return this;
         }
 
@@ -427,6 +937,7 @@
          * categorise shortcuts.
          *
          * @see #SHORTCUT_CATEGORY_CONVERSATION
+         * @see ShortcutInfo#getCategories()
          */
         @NonNull
         public Builder setCategories(Set<String> categories) {
@@ -435,28 +946,70 @@
         }
 
         /**
-         * Sets the intent of a shortcut.  This is a mandatory field.  The extras must only contain
-         * persistable information.  (See {@link PersistableBundle}).
+         * Sets the intent of a shortcut.  Alternatively, {@link #setIntents(Intent[])} can be used
+         * to launch an activity with other activities in the back stack.
+         *
+         * <p>This is a mandatory field when publishing a new shortcut with
+         * {@link ShortcutManager#addDynamicShortcuts(List)} or
+         * {@link ShortcutManager#setDynamicShortcuts(List)}.
+         *
+         * <p>A shortcut can launch any intent that the publisher application has permission to
+         * launch.  For example, a shortcut can launch an unexported activity within the publisher
+         * application.  A shortcut intent doesn't have to point at the target activity.
+         *
+         * <p>The given {@code intent} can contain extras, but these extras must contain values
+         * of primitive types in order for the system to persist these values.
+         *
+         * @see ShortcutInfo#getIntent()
+         * @see #setIntents(Intent[])
          */
         @NonNull
         public Builder setIntent(@NonNull Intent intent) {
-            mIntent = Preconditions.checkNotNull(intent, "intent");
-            return this;
+            return setIntents(new Intent[]{intent});
         }
 
         /**
-         * Optionally sets the weight of a shortcut, which will be used by the launcher for sorting.
-         * The larger the weight, the more "important" a shortcut is.
+         * Sets multiple intents instead of a single intent, in order to launch an activity with
+         * other activities in back stack.  Use {@link TaskStackBuilder} to build intents.
+         * See the {@link ShortcutManager} javadoc for details.
+         *
+         * @see Builder#setIntent(Intent)
+         * @see ShortcutInfo#getIntents()
+         * @see Context#startActivities(Intent[])
+         * @see TaskStackBuilder
          */
         @NonNull
-        public Builder setWeight(int weight) {
-            mWeight = weight;
+        public Builder setIntents(@NonNull Intent[] intents) {
+            Preconditions.checkNotNull(intents, "intents cannot be null");
+            Preconditions.checkNotNull(intents.length, "intents cannot be empty");
+            for (Intent intent : intents) {
+                Preconditions.checkNotNull(intent, "intents cannot contain null");
+                Preconditions.checkNotNull(intent.getAction(), "intent's action must be set");
+            }
+            // Make sure always clone incoming intents.
+            mIntents = cloneIntents(intents);
             return this;
         }
 
         /**
-         * Optional values that applications can set.  Applications can store any meta-data of
-         * shortcuts in this, and retrieve later from {@link ShortcutInfo#getExtras()}.
+         * "Rank" of a shortcut, which is a non-negative value that's used by the launcher app
+         * to sort shortcuts.
+         *
+         * See {@link ShortcutInfo#getRank()} for details.
+         */
+        @NonNull
+        public Builder setRank(int rank) {
+            Preconditions.checkArgument((0 <= rank),
+                    "Rank cannot be negative or bigger than MAX_RANK");
+            mRank = rank;
+            return this;
+        }
+
+        /**
+         * Extras that application can set for any purpose.
+         *
+         * <p>Applications can store arbitrary shortcut metadata in extras and retrieve the
+         * metadata later using {@link ShortcutInfo#getExtras()}.
          */
         @NonNull
         public Builder setExtras(@NonNull PersistableBundle extras) {
@@ -474,7 +1027,11 @@
     }
 
     /**
-     * Return the ID of the shortcut.
+     * Returns the ID of a shortcut.
+     *
+     * <p>Shortcut IDs are unique within each publisher application and must be stable across
+     * devices so that shortcuts will still be valid when restored on a different device.
+     * See {@link ShortcutManager} for details.
      */
     @NonNull
     public String getId() {
@@ -482,33 +1039,34 @@
     }
 
     /**
-     * Return the package name of the creator application.
+     * Return the package name of the publisher application.
      */
     @NonNull
-    public String getPackageName() {
+    public String getPackage() {
         return mPackageName;
     }
 
     /**
-     * Return the target activity, which may be null, in which case the shortcut is not associated
-     * with a specific activity.
+     * Return the target activity.
      *
-     * <p>This has nothing to do with the activity that this shortcut will launch.  This is
-     * a hint to the launcher app that on which launcher icon this shortcut should be shown.
+     * <p>This has nothing to do with the activity that this shortcut will launch.
+     * Launcher applications should show the launcher icon for the returned activity alongside
+     * this shortcut.
      *
-     * @see Builder#setActivityComponent
+     * @see Builder#setActivity
      */
     @Nullable
-    public ComponentName getActivityComponent() {
-        return mActivityComponent;
+    public ComponentName getActivity() {
+        return mActivity;
+    }
+
+    /** @hide */
+    public void setActivity(ComponentName activity) {
+        mActivity = activity;
     }
 
     /**
-     * Icon.
-     *
-     * For performance reasons, this will <b>NOT</b> be available when an instance is returned
-     * by {@link ShortcutManager} or {@link LauncherApps}.  A launcher application needs to use
-     * other APIs in LauncherApps to fetch the bitmap.
+     * Returns the shortcut icon.
      *
      * @hide
      */
@@ -517,27 +1075,82 @@
         return mIcon;
     }
 
-    /**
-     * Return the shortcut title.
-     *
-     * <p>All shortcuts must have a non-empty title, but this method will return null when
-     * {@link #hasKeyFieldsOnly()} is true.
-     */
+    /** @hide -- old signature, the internal code still uses it. */
     @Nullable
-    public String getTitle() {
+    @Deprecated
+    public CharSequence getTitle() {
         return mTitle;
     }
 
-    /**
-     * Return the shortcut text.
-     */
+    /** @hide -- old signature, the internal code still uses it. */
+    @Deprecated
+    public int getTitleResId() {
+        return mTitleResId;
+    }
+
+    /** @hide -- old signature, the internal code still uses it. */
     @Nullable
-    public String getText() {
+    @Deprecated
+    public CharSequence getText() {
         return mText;
     }
 
+    /** @hide -- old signature, the internal code still uses it. */
+    @Deprecated
+    public int getTextResId() {
+        return mTextResId;
+    }
+
     /**
-     * Return the categories.
+     * Return the shorter description of a shortcut.
+     *
+     * @see Builder#setShortLabel(CharSequence)
+     */
+    @Nullable
+    public CharSequence getShortLabel() {
+        return mTitle;
+    }
+
+    /** @hide */
+    public int getShortLabelResourceId() {
+        return mTitleResId;
+    }
+
+    /**
+     * Return the longer description of a shortcut.
+     *
+     * @see Builder#setLongLabel(CharSequence)
+     */
+    @Nullable
+    public CharSequence getLongLabel() {
+        return mText;
+    }
+
+    /** @hide */
+    public int getLongLabelResourceId() {
+        return mTextResId;
+    }
+
+    /**
+     * Return the message that should be shown when the user attempts to start a shortcut
+     * that is disabled.
+     *
+     * @see Builder#setDisabledMessage(CharSequence)
+     */
+    @Nullable
+    public CharSequence getDisabledMessage() {
+        return mDisabledMessage;
+    }
+
+    /** @hide */
+    public int getDisabledMessageResourceId() {
+        return mDisabledMessageResId;
+    }
+
+    /**
+     * Return the shortcut's categories.
+     *
+     * @see Builder#setCategories(Set)
      */
     @Nullable
     public Set<String> getCategories() {
@@ -545,55 +1158,125 @@
     }
 
     /**
-     * Return the intent.
+     * Returns the intent that is executed when the user selects this shortcut.
+     * If setIntents() was used, then return the last intent in the array.
      *
-     * <p>All shortcuts must have an intent, but this method will return null when
-     * {@link #hasKeyFieldsOnly()} is true.
+     * <p>Launcher applications <b>cannot</b> see the intent.  If a {@link ShortcutInfo} is
+     * obtained via {@link LauncherApps}, then this method will always return null.
+     * Launchers can only start a shortcut intent with {@link LauncherApps#startShortcut}.
      *
-     * <p>Launcher apps <b>cannot</b> see the intent.  If a {@link ShortcutInfo} is obtained via
-     * {@link LauncherApps}, then this method will always return null.  Launcher apps can only
-     * start a shortcut intent with {@link LauncherApps#startShortcut}.
+     * @see Builder#setIntent(Intent)
      */
     @Nullable
     public Intent getIntent() {
-        if (mIntent == null) {
+        if (mIntents == null || mIntents.length == 0) {
             return null;
         }
-        final Intent intent = new Intent(mIntent);
-        intent.replaceExtras(
-                mIntentPersistableExtras != null ? new Bundle(mIntentPersistableExtras) : null);
-        return intent;
+        final int last = mIntents.length - 1;
+        final Intent intent = new Intent(mIntents[last]);
+        return setIntentExtras(intent, mIntentPersistableExtrases[last]);
     }
 
     /**
-     * Return "raw" intent, which is the original intent without the extras.
+     * Return the intent set with {@link Builder#setIntents(Intent[])}.
+     *
+     * <p>Launcher applications <b>cannot</b> see the intents.  If a {@link ShortcutInfo} is
+     * obtained via {@link LauncherApps}, then this method will always return null.
+     * Launchers can only start a shortcut intent with {@link LauncherApps#startShortcut}.
+     *
+     * @see Builder#setIntents(Intent[])
+     */
+    @Nullable
+    public Intent[] getIntents() {
+        final Intent[] ret = new Intent[mIntents.length];
+
+        for (int i = 0; i < ret.length; i++) {
+            ret[i] = new Intent(mIntents[i]);
+            setIntentExtras(ret[i], mIntentPersistableExtrases[i]);
+        }
+
+        return ret;
+    }
+
+    /**
+     * Return "raw" intents, which is the original intents without the extras.
      * @hide
      */
     @Nullable
-    public Intent getIntentNoExtras() {
-        return mIntent;
+    public Intent[] getIntentsNoExtras() {
+        return mIntents;
     }
 
     /**
-     * The extras in the intent.  We convert extras into {@link PersistableBundle} so we can
+     * The extras in the intents.  We convert extras into {@link PersistableBundle} so we can
      * persist them.
      * @hide
      */
     @Nullable
-    public PersistableBundle getIntentPersistableExtras() {
-        return mIntentPersistableExtras;
+    public PersistableBundle[] getIntentPersistableExtrases() {
+        return mIntentPersistableExtrases;
     }
 
     /**
-     * Return the weight of a shortcut, which will be used by Launcher for sorting.
-     * The larger the weight, the more "important" a shortcut is.
+     * "Rank" of a shortcut, which is a non-negative, sequential value that's unique for each
+     * {@link #getActivity} for each of the two kinds, dynamic shortcuts and manifest shortcuts.
+     *
+     * <p>Because manifest shortcuts and dynamic shortcuts have overlapping ranks,
+     * when a launcher application shows shortcuts for an activity, it should first show
+     * the manifest shortcuts followed by the dynamic shortcuts.  Within each of those categories,
+     * shortcuts should be sorted by rank in ascending order.
+     *
+     * <p>"Floating" shortcuts (i.e. shortcuts that are neither dynamic nor manifest) will all
+     * have rank 0, because there's no sorting for them.
+     *
+     * See the {@link ShortcutManager}'s class javadoc for details.
+     *
+     * @see Builder#setRank(int)
      */
-    public int getWeight() {
-        return mWeight;
+    public int getRank() {
+        return mRank;
+    }
+
+    /** @hide */
+    public boolean hasRank() {
+        return mRank != RANK_NOT_SET;
+    }
+
+    /** @hide */
+    public void setRank(int rank) {
+        mRank = rank;
+    }
+
+    /** @hide */
+    public void clearImplicitRankAndRankChangedFlag() {
+        mImplicitRank = 0;
+    }
+
+    /** @hide */
+    public void setImplicitRank(int rank) {
+        // Make sure to keep RANK_CHANGED_BIT.
+        mImplicitRank = (mImplicitRank & RANK_CHANGED_BIT) | (rank & IMPLICIT_RANK_MASK);
+    }
+
+    /** @hide */
+    public int getImplicitRank() {
+        return mImplicitRank & IMPLICIT_RANK_MASK;
+    }
+
+    /** @hide */
+    public void setRankChanged() {
+        mImplicitRank |= RANK_CHANGED_BIT;
+    }
+
+    /** @hide */
+    public boolean isRankChanged() {
+        return (mImplicitRank & RANK_CHANGED_BIT) != 0;
     }
 
     /**
-     * Optional values that application can set.
+     * Extras that application can set to any purposes.
+     *
+     * @see Builder#setExtras(PersistableBundle)
      */
     @Nullable
     public PersistableBundle getExtras() {
@@ -606,7 +1289,7 @@
     }
 
     /**
-     * {@link UserHandle} on which the publisher created shortcuts.
+     * {@link UserHandle} on which the publisher created this shortcut.
      */
     public UserHandle getUserHandle() {
         return UserHandle.of(mUserId);
@@ -656,18 +1339,93 @@
     }
 
     /**
+     * Return whether a shortcut is published from AndroidManifest.xml or not.  If {@code true},
+     * it's also {@link #isImmutable()}.
+     *
+     * <p>When an app is upgraded and a shortcut is no longer published from AndroidManifest.xml,
+     * this will be set to {@code false}.  If the shortcut is not pinned, then it'll just disappear.
+     * However, if it's pinned, it will still be alive, and {@link #isEnabled()} will be
+     * {@code false} and {@link #isImmutable()} will be {@code true}.
+     */
+    public boolean isDeclaredInManifest() {
+        return hasFlags(FLAG_MANIFEST);
+    }
+
+    /** @hide kept for unit tests */
+    @Deprecated
+    public boolean isManifestShortcut() {
+        return isDeclaredInManifest();
+    }
+
+    /**
+     * @return true if pinned but neither dynamic nor manifest.
+     * @hide
+     */
+    public boolean isFloating() {
+        return isPinned() && !(isDynamic() || isManifestShortcut());
+    }
+
+    /** @hide */
+    public boolean isOriginallyFromManifest() {
+        return hasFlags(FLAG_IMMUTABLE);
+    }
+
+    /**
+     * Return if a shortcut is immutable, in which case it cannot be modified with any of
+     * {@link ShortcutManager} APIs.
+     *
+     * <p>All manifest shortcuts are immutable.  When a manifest shortcut is pinned and then
+     * disabled because the app is upgraded and its AndroidManifest.xml no longer publishes it,
+     * {@link #isDeclaredInManifest()} returns {@code false}, but it is still immutable.
+     *
+     * <p>All shortcuts originally published via the {@link ShortcutManager} APIs
+     * are all mutable.
+     */
+    public boolean isImmutable() {
+        return hasFlags(FLAG_IMMUTABLE);
+    }
+
+    /**
+     * Returns {@code false} if a shortcut is disabled with
+     * {@link ShortcutManager#disableShortcuts}.
+     */
+    public boolean isEnabled() {
+        return !hasFlags(FLAG_DISABLED);
+    }
+
+    /** @hide */
+    public boolean isAlive() {
+        return hasFlags(FLAG_PINNED) || hasFlags(FLAG_DYNAMIC) || hasFlags(FLAG_MANIFEST);
+    }
+
+    /** @hide */
+    public boolean usesQuota() {
+        return hasFlags(FLAG_DYNAMIC) || hasFlags(FLAG_MANIFEST);
+    }
+
+    /**
      * Return whether a shortcut's icon is a resource in the owning package.
      *
-     * @see LauncherApps#getShortcutIconResId(ShortcutInfo)
+     * @hide internal/unit tests only
      */
     public boolean hasIconResource() {
         return hasFlags(FLAG_HAS_ICON_RES);
     }
 
+    /** @hide */
+    public boolean hasStringResources() {
+        return (mTitleResId != 0) || (mTextResId != 0) || (mDisabledMessageResId != 0);
+    }
+
+    /** @hide */
+    public boolean hasAnyResources() {
+        return hasIconResource() || hasStringResources();
+    }
+
     /**
      * Return whether a shortcut's icon is stored as a file.
      *
-     * @see LauncherApps#getShortcutIconFd(ShortcutInfo)
+     * @hide internal/unit tests only
      */
     public boolean hasIconFile() {
         return hasFlags(FLAG_HAS_ICON_FILE);
@@ -678,19 +1436,33 @@
      * following fields are available.
      * <ul>
      *     <li>{@link #getId()}
-     *     <li>{@link #getPackageName()}
+     *     <li>{@link #getPackage()}
+     *     <li>{@link #getActivity()}
      *     <li>{@link #getLastChangedTimestamp()}
      *     <li>{@link #isDynamic()}
      *     <li>{@link #isPinned()}
-     *     <li>{@link #hasIconResource()}
-     *     <li>{@link #hasIconFile()}
+     *     <li>{@link #isDeclaredInManifest()}
+     *     <li>{@link #isImmutable()}
+     *     <li>{@link #isEnabled()}
+     *     <li>{@link #getUserHandle()}
      * </ul>
+     *
+     * <p>For performance reasons, shortcuts passed to
+     * {@link LauncherApps.Callback#onShortcutsChanged(String, List, UserHandle)} as well as those
+     * returned from {@link LauncherApps#getShortcuts(ShortcutQuery, UserHandle)}
+     * while using the {@link ShortcutQuery#FLAG_GET_KEY_FIELDS_ONLY} option contain only key
+     * information.
      */
     public boolean hasKeyFieldsOnly() {
         return hasFlags(FLAG_KEY_FIELDS_ONLY);
     }
 
     /** @hide */
+    public boolean hasStringResourcesResolved() {
+        return hasFlags(FLAG_STRINGS_RESOLVED);
+    }
+
+    /** @hide */
     public void updateTimestamp() {
         mLastChangedTimestamp = System.currentTimeMillis();
     }
@@ -708,14 +1480,18 @@
 
     /** @hide */
     public void setIconResourceId(int iconResourceId) {
-        mIconResourceId = iconResourceId;
+        if (mIconResId != iconResourceId) {
+            mIconResName = null;
+        }
+        mIconResId = iconResourceId;
     }
 
     /**
      * Get the resource ID for the icon, valid only when {@link #hasIconResource()} } is true.
+     * @hide internal / tests only.
      */
     public int getIconResourceId() {
-        return mIconResourceId;
+        return mIconResId;
     }
 
     /** @hide */
@@ -728,25 +1504,129 @@
         mBitmapPath = bitmapPath;
     }
 
+    /** @hide */
+    public void setDisabledMessageResId(int disabledMessageResId) {
+        if (mDisabledMessageResId != disabledMessageResId) {
+            mDisabledMessageResName = null;
+        }
+        mDisabledMessageResId = disabledMessageResId;
+        mDisabledMessage = null;
+    }
+
+    /** @hide */
+    public void setDisabledMessage(String disabledMessage) {
+        mDisabledMessage = disabledMessage;
+        mDisabledMessageResId = 0;
+        mDisabledMessageResName = null;
+    }
+
+    /** @hide */
+    public String getTitleResName() {
+        return mTitleResName;
+    }
+
+    /** @hide */
+    public void setTitleResName(String titleResName) {
+        mTitleResName = titleResName;
+    }
+
+    /** @hide */
+    public String getTextResName() {
+        return mTextResName;
+    }
+
+    /** @hide */
+    public void setTextResName(String textResName) {
+        mTextResName = textResName;
+    }
+
+    /** @hide */
+    public String getDisabledMessageResName() {
+        return mDisabledMessageResName;
+    }
+
+    /** @hide */
+    public void setDisabledMessageResName(String disabledMessageResName) {
+        mDisabledMessageResName = disabledMessageResName;
+    }
+
+    /** @hide */
+    public String getIconResName() {
+        return mIconResName;
+    }
+
+    /** @hide */
+    public void setIconResName(String iconResName) {
+        mIconResName = iconResName;
+    }
+
+    /**
+     * Replaces the intent
+     *
+     * @throws IllegalArgumentException when extra is not compatible with {@link PersistableBundle}.
+     *
+     * @hide
+     */
+    public void setIntents(Intent[] intents) throws IllegalArgumentException {
+        Preconditions.checkNotNull(intents);
+        Preconditions.checkArgument(intents.length > 0);
+
+        mIntents = cloneIntents(intents);
+        fixUpIntentExtras();
+    }
+
+    /** @hide */
+    public static Intent setIntentExtras(Intent intent, PersistableBundle extras) {
+        if (extras == null) {
+            intent.replaceExtras((Bundle) null);
+        } else {
+            intent.replaceExtras(new Bundle(extras));
+        }
+        return intent;
+    }
+
+    /**
+     * Replaces the categories.
+     *
+     * @hide
+     */
+    public void setCategories(Set<String> categories) {
+        mCategories = cloneCategories(categories);
+    }
+
     private ShortcutInfo(Parcel source) {
         final ClassLoader cl = getClass().getClassLoader();
 
         mUserId = source.readInt();
         mId = source.readString();
         mPackageName = source.readString();
-        mActivityComponent = source.readParcelable(cl);
-        mIcon = source.readParcelable(cl);
-        mTitle = source.readString();
-        mText = source.readString();
-        mIntent = source.readParcelable(cl);
-        mIntentPersistableExtras = source.readParcelable(cl);
-        mWeight = source.readInt();
-        mExtras = source.readParcelable(cl);
-        mLastChangedTimestamp = source.readLong();
+        mActivity = source.readParcelable(cl);
         mFlags = source.readInt();
-        mIconResourceId = source.readInt();
+        mIconResId = source.readInt();
+        mLastChangedTimestamp = source.readLong();
+
+        if (source.readInt() == 0) {
+            return; // key information only.
+        }
+
+        mIcon = source.readParcelable(cl);
+        mTitle = source.readCharSequence();
+        mTitleResId = source.readInt();
+        mText = source.readCharSequence();
+        mTextResId = source.readInt();
+        mDisabledMessage = source.readCharSequence();
+        mDisabledMessageResId = source.readInt();
+        mIntents = source.readParcelableArray(cl, Intent.class);
+        mIntentPersistableExtrases = source.readParcelableArray(cl, PersistableBundle.class);
+        mRank = source.readInt();
+        mExtras = source.readParcelable(cl);
         mBitmapPath = source.readString();
 
+        mIconResName = source.readString();
+        mTitleResName = source.readString();
+        mTextResName = source.readString();
+        mDisabledMessageResName = source.readString();
+
         int N = source.readInt();
         if (N == 0) {
             mCategories = null;
@@ -763,20 +1643,36 @@
         dest.writeInt(mUserId);
         dest.writeString(mId);
         dest.writeString(mPackageName);
-        dest.writeParcelable(mActivityComponent, flags);
-        dest.writeParcelable(mIcon, flags);
-        dest.writeString(mTitle);
-        dest.writeString(mText);
-
-        dest.writeParcelable(mIntent, flags);
-        dest.writeParcelable(mIntentPersistableExtras, flags);
-        dest.writeInt(mWeight);
-        dest.writeParcelable(mExtras, flags);
-        dest.writeLong(mLastChangedTimestamp);
+        dest.writeParcelable(mActivity, flags);
         dest.writeInt(mFlags);
-        dest.writeInt(mIconResourceId);
+        dest.writeInt(mIconResId);
+        dest.writeLong(mLastChangedTimestamp);
+
+        if (hasKeyFieldsOnly()) {
+            dest.writeInt(0);
+            return;
+        }
+        dest.writeInt(1);
+
+        dest.writeParcelable(mIcon, flags);
+        dest.writeCharSequence(mTitle);
+        dest.writeInt(mTitleResId);
+        dest.writeCharSequence(mText);
+        dest.writeInt(mTextResId);
+        dest.writeCharSequence(mDisabledMessage);
+        dest.writeInt(mDisabledMessageResId);
+
+        dest.writeParcelableArray(mIntents, flags);
+        dest.writeParcelableArray(mIntentPersistableExtrases, flags);
+        dest.writeInt(mRank);
+        dest.writeParcelable(mExtras, flags);
         dest.writeString(mBitmapPath);
 
+        dest.writeString(mIconResName);
+        dest.writeString(mTitleResName);
+        dest.writeString(mTextResName);
+        dest.writeString(mDisabledMessageResName);
+
         if (mCategories != null) {
             final int N = mCategories.size();
             dest.writeInt(N);
@@ -823,24 +1719,67 @@
         sb.append("id=");
         sb.append(secure ? "***" : mId);
 
+        sb.append(", flags=0x");
+        sb.append(Integer.toHexString(mFlags));
+        sb.append(" [");
+        if (!isEnabled()) {
+            sb.append("X");
+        }
+        if (isImmutable()) {
+            sb.append("Im");
+        }
+        if (isManifestShortcut()) {
+            sb.append("M");
+        }
+        if (isDynamic()) {
+            sb.append("D");
+        }
+        if (isPinned()) {
+            sb.append("P");
+        }
+        if (hasIconFile()) {
+            sb.append("If");
+        }
+        if (hasIconResource()) {
+            sb.append("Ir");
+        }
+        if (hasKeyFieldsOnly()) {
+            sb.append("K");
+        }
+        if (hasStringResourcesResolved()) {
+            sb.append("Sr");
+        }
+        sb.append("]");
+
         sb.append(", packageName=");
         sb.append(mPackageName);
 
-        if (isDynamic()) {
-            sb.append(", dynamic");
-        }
-        if (isPinned()) {
-            sb.append(", pinned");
-        }
-
         sb.append(", activity=");
-        sb.append(mActivityComponent);
+        sb.append(mActivity);
 
-        sb.append(", title=");
+        sb.append(", shortLabel=");
         sb.append(secure ? "***" : mTitle);
+        sb.append(", resId=");
+        sb.append(mTitleResId);
+        sb.append("[");
+        sb.append(mTitleResName);
+        sb.append("]");
 
-        sb.append(", text=");
+        sb.append(", longLabel=");
         sb.append(secure ? "***" : mText);
+        sb.append(", resId=");
+        sb.append(mTextResId);
+        sb.append("[");
+        sb.append(mTextResName);
+        sb.append("]");
+
+        sb.append(", disabledMessage=");
+        sb.append(secure ? "***" : mDisabledMessage);
+        sb.append(", resId=");
+        sb.append(mDisabledMessageResId);
+        sb.append("[");
+        sb.append(mDisabledMessageResName);
+        sb.append("]");
 
         sb.append(", categories=");
         sb.append(mCategories);
@@ -848,28 +1787,44 @@
         sb.append(", icon=");
         sb.append(mIcon);
 
-        sb.append(", weight=");
-        sb.append(mWeight);
+        sb.append(", rank=");
+        sb.append(mRank);
 
         sb.append(", timestamp=");
         sb.append(mLastChangedTimestamp);
 
-        sb.append(", intent=");
-        sb.append(mIntent);
-
-        sb.append(", intentExtras=");
-        sb.append(secure ? "***" : mIntentPersistableExtras);
+        sb.append(", intents=");
+        if (mIntents == null) {
+            sb.append("null");
+        } else {
+            if (secure) {
+                sb.append("size:");
+                sb.append(mIntents.length);
+            } else {
+                final int size = mIntents.length;
+                sb.append("[");
+                String sep = "";
+                for (int i = 0; i < size; i++) {
+                    sb.append(sep);
+                    sep = ", ";
+                    sb.append(mIntents[i]);
+                    sb.append("/");
+                    sb.append(mIntentPersistableExtrases[i]);
+                }
+                sb.append("]");
+            }
+        }
 
         sb.append(", extras=");
         sb.append(mExtras);
 
-        sb.append(", flags=");
-        sb.append(mFlags);
-
         if (includeInternalData) {
 
             sb.append(", iconRes=");
-            sb.append(mIconResourceId);
+            sb.append(mIconResId);
+            sb.append("[");
+            sb.append(mIconResName);
+            sb.append("]");
 
             sb.append(", bitmapPath=");
             sb.append(mBitmapPath);
@@ -881,26 +1836,36 @@
 
     /** @hide */
     public ShortcutInfo(
-            @UserIdInt int userId, String id, String packageName, ComponentName activityComponent,
-            Icon icon, String title, String text, Set<String> categories, Intent intent,
-            PersistableBundle intentPersistableExtras,
-            int weight, PersistableBundle extras, long lastChangedTimestamp,
-            int flags, int iconResId, String bitmapPath) {
+            @UserIdInt int userId, String id, String packageName, ComponentName activity,
+            Icon icon, CharSequence title, int titleResId, String titleResName,
+            CharSequence text, int textResId, String textResName,
+            CharSequence disabledMessage, int disabledMessageResId, String disabledMessageResName,
+            Set<String> categories, Intent[] intentsWithExtras, int rank, PersistableBundle extras,
+            long lastChangedTimestamp,
+            int flags, int iconResId, String iconResName, String bitmapPath) {
         mUserId = userId;
         mId = id;
         mPackageName = packageName;
-        mActivityComponent = activityComponent;
+        mActivity = activity;
         mIcon = icon;
         mTitle = title;
+        mTitleResId = titleResId;
+        mTitleResName = titleResName;
         mText = text;
-        mCategories = clone(categories);
-        mIntent = intent;
-        mIntentPersistableExtras = intentPersistableExtras;
-        mWeight = weight;
+        mTextResId = textResId;
+        mTextResName = textResName;
+        mDisabledMessage = disabledMessage;
+        mDisabledMessageResId = disabledMessageResId;
+        mDisabledMessageResName = disabledMessageResName;
+        mCategories = cloneCategories(categories);
+        mIntents = cloneIntents(intentsWithExtras);
+        fixUpIntentExtras();
+        mRank = rank;
         mExtras = extras;
         mLastChangedTimestamp = lastChangedTimestamp;
         mFlags = flags;
-        mIconResourceId = iconResId;
+        mIconResId = iconResId;
+        mIconResName = iconResName;
         mBitmapPath = bitmapPath;
     }
 }
diff --git a/core/java/android/content/pm/ShortcutManager.java b/core/java/android/content/pm/ShortcutManager.java
index 16486e1..cd248ea 100644
--- a/core/java/android/content/pm/ShortcutManager.java
+++ b/core/java/android/content/pm/ShortcutManager.java
@@ -17,8 +17,11 @@
 
 import android.annotation.NonNull;
 import android.annotation.TestApi;
+import android.annotation.UserIdInt;
+import android.app.Activity;
+import android.app.usage.UsageStatsManager;
 import android.content.Context;
-import android.os.IBinder;
+import android.content.Intent;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.UserHandle;
@@ -27,69 +30,416 @@
 
 import java.util.List;
 
-// TODO Enhance javadoc
 /**
- * {@link ShortcutManager} manages shortcuts created by applications.
+ * The ShortcutManager manages "launcher shortcuts" (or simply "shortcuts").  Shortcuts provide
+ * users
+ * with quick access to activities other than an application's main activity in the currently-active
+ * launcher.  For example,
+ * an email application may publish the "compose new email" action, which will directly open the
+ * compose activity.  The {@link ShortcutInfo} class contains information about each of the
+ * shortcuts themselves.
  *
- * <h3>Dynamic shortcuts and pinned shortcuts</h3>
+ * <h3>Dynamic Shortcuts and Manifest Shortcuts</h3>
  *
- * An application can publish shortcuts with {@link #setDynamicShortcuts(List)} and
- * {@link #addDynamicShortcuts(List)}.  There can be at most
- * {@link #getMaxDynamicShortcutCount()} number of dynamic shortcuts at a time from the same
+ * There are two ways to publish shortcuts: manifest shortcuts and dynamic shortcuts.
+ *
+ * <ul>
+ * <li>Manifest shortcuts are declared in a resource
+ * XML, which is referenced in the publisher application's <code>AndroidManifest.xml</code> file.
+ * Manifest shortcuts are published when an application is installed,
+ * and the details of these shortcuts change when an application is upgraded with an updated XML
+ * file.
+ * Manifest shortcuts are immutable, and their
+ * definitions, such as icons and labels, cannot be changed dynamically without upgrading the
+ * publisher application.
+ *
+ * <li>Dynamic shortcuts are published at runtime using the {@link ShortcutManager} APIs.
+ * Applications can publish, update, and remove dynamic shortcuts at runtime.
+ * </ul>
+ *
+ * <p>Only "main" activities&mdash;activities that handle the {@code MAIN} action and the
+ * {@code LAUNCHER} category&mdash;can have shortcuts.
+ * If an application has multiple main activities, these activities will have different sets
+ * of shortcuts.
+ *
+ * <p>Dynamic shortcuts and manifest shortcuts are shown in the currently active launcher when
+ * the user long-presses on an application launcher icon.  The actual gesture may be different
+ * depending on the launcher application.
+ *
+ * <p>Each launcher icon can have at most {@link #getMaxShortcutCountPerActivity()} number of
+ * dynamic and manifest shortcuts combined.
+ *
+ *
+ * <h3>Pinning Shortcuts</h3>
+ *
+ * Launcher applications allow users to "pin" shortcuts so they're easier to access.  Both manifest
+ * and dynamic shortcuts can be pinned.
+ * Pinned shortcuts <b>cannot</b> be removed by publisher
+ * applications; they're removed only when the user removes them,
+ * when the publisher application is uninstalled, or when the
+ * user performs the "clear data" action on the publisher application from the device's Settings
  * application.
- * A dynamic shortcut can be deleted with {@link #removeDynamicShortcuts(List)}, and apps
- * can also use {@link #removeAllDynamicShortcuts()} to delete all dynamic shortcuts.
  *
- * <p>The shortcuts that are currently published by the above APIs are called "dynamic", because
- * they can be removed by the creator application at any time.  The user may "pin" dynamic shortcuts
- * on Launcher to make "pinned" shortcuts.  Pinned shortcuts <b>cannot</b> be removed by the creator
- * app.  An application can obtain all pinned shortcuts from itself with
- * {@link #getPinnedShortcuts()}.  Applications should keep the pinned shortcut information
- * up-to-date using {@link #updateShortcuts(List)}.
+ * <p>However, the publisher application can <em>disable</em> pinned shortcuts so they cannot be
+ * started.  See the following sections for details.
  *
- * <p>The number of pinned shortcuts does not affect the number of dynamic shortcuts that can be
- * published by an application at a time.
- * No matter how many pinned shortcuts that Launcher has for an application, the
- * application can still always publish {@link #getMaxDynamicShortcutCount()} number of dynamic
- * shortcuts.
  *
- * <h3>Shortcut IDs</h3>
+ * <h3>Updating and Disabling Shortcuts</h3>
  *
- * Each shortcut must have an ID, which must be unique within each application.  When a shortcut is
- * published, existing shortcuts with the same ID will be updated.  Note this may include a
- * pinned shortcut.
+ * <p>When a dynamic shortcut is pinned, even when the publisher removes it as a dynamic shortcut,
+ * the pinned shortcut will still be visible and launchable.  This allows an application to have
+ * more than {@link #getMaxShortcutCountPerActivity()} number of shortcuts.
  *
- * <h3>Rate limiting</h3>
+ * <p>For example, suppose {@link #getMaxShortcutCountPerActivity()} is 5:
+ * <ul>
+ *     <li>A chat application publishes 5 dynamic shortcuts for the 5 most recent
+ *     conversations, "c1" - "c5".
  *
- * Calls to {@link #setDynamicShortcuts(List)}, {@link #addDynamicShortcuts(List)},
- * and {@link #updateShortcuts(List)} from <b>background applications</b> will be
- * rate-limited.  An application can call these methods at most
- * {@link #getRemainingCallCount()} times until the rate-limiting counter is reset,
- * which happens at a certain time every day.
+ *     <li>The user pins all 5 of the shortcuts.
  *
- * <p>An application can use {@link #getRateLimitResetTime()} to get the next reset time.
+ *     <li>Later, the user has started 3 additional conversations ("c6", "c7", and "c8"),
+ *     so the publisher application
+ *     re-publishes its dynamic shortcuts.  The new dynamic shortcut list is:
+ *     "c4", "c5", "c6", "c7", and "c8".
+ *     The publisher application has to remove "c1", "c2", and "c3" because it can't have more than
+ *     5 dynamic shortcuts.
  *
- * <p>Foreground applications (i.e. ones with a foreground activity or a foreground services)
- * will not be throttled. Also, when an application comes to foreground,
- * {@link #getRemainingCallCount()} will be reset to the initial value.
+ *     <li>However, even though "c1", "c2" and "c3" are no longer dynamic shortcuts, the pinned
+ *     shortcuts for these conversations are still available and launchable.
  *
- * <p>For testing purposes, use "Developer Options" (found in the Settings menu) to reset the
- * internal rate-limiting counter.  Automated tests can use the following ADB shell command to
- * achieve the same effect:</p>
- * <pre>adb shell cmd shortcut reset-throttling</pre>
+ *     <li>At this point, the user can access a total of 8 shortcuts that link to activities in
+ *     the publisher application, including the 3 pinned
+ *     shortcuts, even though it's allowed to have at most 5 dynamic shortcuts.
+ *
+ *     <li>The application can use {@link #updateShortcuts(List)} to update any of the existing
+ *     8 shortcuts, when, for example, the chat peers' icons have changed.
+ * </ul>
+ * The {@link #addDynamicShortcuts(List)} and {@link #setDynamicShortcuts(List)} methods
+ * can also be used
+ * to update existing shortcuts with the same IDs, but they <b>cannot</b> be used
+ * for updating non-dynamic, pinned shortcuts because these two methods try to convert the given
+ * lists of shortcuts to dynamic shortcuts.
+ *
+ *
+ * <h4>Disabling Manifest Shortcuts</h4>
+ * When an application is upgraded and the new version
+ * no longer uses a manifest shortcut that appeared in the previous version, this deprecated
+ * shortcut will no longer be published as a manifest shortcut.
+ *
+ * <p>If the deprecated shortcut is pinned, then the pinned shortcut will remain on the launcher,
+ * but it will be disabled automatically.
+ * Note that, in this case, the pinned shortcut is no longer a manifest shortcut, but it's
+ * still <b>immutable</b> and cannot be updated using the {@link ShortcutManager} APIs.
+ *
+ *
+ * <h4>Disabling Dynamic Shortcuts</h4>
+ * Sometimes pinned shortcuts become obsolete and may not be usable.  For example, a pinned shortcut
+ * to a group chat will be unusable when the associated group chat is deleted.  In cases like this,
+ * applications should use {@link #disableShortcuts(List)}, which will remove the specified dynamic
+ * shortcuts and also make any specified pinned shortcuts un-launchable.
+ * The {@link #disableShortcuts(List, CharSequence)} method can also be used to disabled shortcuts
+ * and show users a custom error message when they attempt to launch the disabled shortcuts.
+ *
+ *
+ * <h3>Publishing Manifest Shortcuts</h3>
+ *
+ * In order to add manifest shortcuts to your application, first add
+ * {@code <meta-data android:name="android.app.shortcuts" />} to your main activity in
+ * AndroidManifest.xml:
+ * <pre>
+ * &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
+ *   package=&quot;com.example.myapplication&quot;&gt;
+ *   &lt;application . . .&gt;
+ *     &lt;activity android:name=&quot;Main&quot;&gt;
+ *       &lt;intent-filter&gt;
+ *         &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
+ *         &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
+ *       &lt;/intent-filter&gt;
+ *       <b>&lt;meta-data android:name=&quot;android.app.shortcuts&quot; android:resource=&quot;@xml/shortcuts&quot;/&gt;</b>
+ *     &lt;/activity&gt;
+ *   &lt;/application&gt;
+ * &lt;/manifest&gt;
+ * </pre>
+ *
+ * Then, define your application's manifest shortcuts in the <code>res/xml/shortcuts.xml</code>
+ * file:
+ * <pre>
+ * &lt;shortcuts xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; &gt;
+ *   &lt;shortcut
+ *     android:shortcutId=&quot;compose&quot;
+ *     android:enabled=&quot;true&quot;
+ *     android:icon=&quot;@drawable/compose_icon&quot;
+ *     android:shortcutShortLabel=&quot;@string/compose_shortcut_short_label1&quot;
+ *     android:shortcutLongLabel=&quot;@string/compose_shortcut_long_label1&quot;
+ *     android:shortcutDisabledMessage=&quot;@string/compose_disabled_message1&quot;
+ *     &gt;
+ *     &lt;intent
+ *       android:action=&quot;android.intent.action.VIEW&quot;
+ *       android:targetPackage=&quot;com.example.myapplication&quot;
+ *       android:targetClass=&quot;com.example.myapplication.ComposeActivity&quot; /&gt;
+ *     &lt;!-- more intents can go here; see below --&gt;
+ *     &lt;categories android:name=&quot;android.shortcut.conversation&quot; /&gt;
+ *   &lt;/shortcut&gt;
+ *   &lt;!-- more shortcuts can go here --&gt;
+ * &lt;/shortcuts&gt;
+ * </pre>
+ *
+ * The following list includes descriptions for the different attributes within a manifest shortcut:
+ * <dl>
+ *   <dt>android:shortcutId</dt>
+ *   <dd>Mandatory shortcut ID</dd>
+ *
+ *   <dt>android:enabled</dt>
+ *   <dd>Default is {@code true}.  Can be set to {@code false} in order
+ *   to disable a manifest shortcut that was published in a previous version and and set a custom
+ *   disabled message.  If a custom disabled message is not needed, then a manifest shortcut can
+ *   be simply removed from the XML file rather than keeping it with {@code enabled="false"}.</dd>
+ *
+ *   <dt>android:icon</dt>
+ *   <dd>Shortcut icon.</dd>
+ *
+ *   <dt>android:shortcutShortLabel</dt>
+ *   <dd>Mandatory shortcut short label.
+ *   See {@link ShortcutInfo.Builder#setShortLabel(CharSequence)}.</dd>
+ *
+ *   <dt>android:shortcutLongLabel</dt>
+ *   <dd>Shortcut long label.
+ *   See {@link ShortcutInfo.Builder#setLongLabel(CharSequence)}.</dd>
+ *
+ *   <dt>android:shortcutDisabledMessage</dt>
+ *   <dd>When {@code android:enabled} is set to
+ *   {@code false}, this attribute is used to display a custom disabled message.</dd>
+ *
+ *   <dt>intent</dt>
+ *   <dd>Intent to launch when the user selects the shortcut.
+ *   {@code android:action} is mandatory.
+ *   See <a href="{@docRoot}guide/topics/ui/settings.html#Intents">Using intents</a> for the
+ *   other supported tags.
+ *   You can provide multiple intents for a single shortcut so that an activity is launched
+ *   with other activities in the back stack. See {@link android.app.TaskStackBuilder} for details.
+ *   </dd>
+ *   <dt>categories</dt>
+ *   <dd>Specify shortcut categories.  Currently only
+ *   {@link ShortcutInfo#SHORTCUT_CATEGORY_CONVERSATION} is defined in the framework.
+ *   </dd>
+ * </dl>
+ *
+ * <h3>Publishing Dynamic Shortcuts</h3>
+ *
+ * Applications can publish dynamic shortcuts with {@link #setDynamicShortcuts(List)}
+ * or {@link #addDynamicShortcuts(List)}.  The {@link #updateShortcuts(List)} method can also be
+ * used to update existing, mutable shortcuts.
+ * Use {@link #removeDynamicShortcuts(List)} or {@link #removeAllDynamicShortcuts()} to remove
+ * dynamic shortcuts.
+ *
+ * <p>Example:
+ * <pre>
+ * ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
+ *
+ * ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
+ *     .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.mysite.com/")))
+ *     .setShortLabel("Web site")
+ *     .setLongLabel("Open the web site")
+ *     .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
+ *     .build();
+ *
+ * shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
+ * </pre>
+ *
+ *
+ * <h3>Shortcut Intents</h3>
+ * Dynamic shortcuts can be published with any set of {@link Intent#addFlags Intent} flags.
+ * Typically, {@link Intent#FLAG_ACTIVITY_CLEAR_TASK} is specified, possibly along with other
+ * flags; otherwise, if the application is already running, the application is simply brought to
+ * the foreground, and the target activity may not appear.
+ *
+ * <p>The {@link ShortcutInfo.Builder#setIntents(Intent[])} method can be used instead of
+ * {@link ShortcutInfo.Builder#setIntent(Intent)} with {@link android.app.TaskStackBuilder}
+ * in order to launch an activity with other activities in the back stack.
+ * When the user selects a shortcut to load an activity with a back stack,
+ * then presses the back key, a "parent" activity will be shown instead of the user being
+ * navigated back to the launcher.
+ *
+ * <p>Manifest shortcuts can also have multiple intents to achieve the same effect.
+ * In order to associate multiple {@link Intent} objects with a shortcut, simply list multiple
+ * <code>&lt;intent&gt;</code> elements within a single <code>&lt;shortcut&gt;</code> element.
+ * The last intent specifies what the user will see when they launch a shortcut.
+ *
+ * <p>Manifest shortcuts <b>cannot</b> have custom intent flags.
+ * The first intent of a manifest shortcut will always have {@link Intent#FLAG_ACTIVITY_NEW_TASK}
+ * and {@link Intent#FLAG_ACTIVITY_CLEAR_TASK} set.
+ * This means, when the application is already running, all the existing activities will be
+ * destroyed when a manifest shortcut is launched.
+ * If this behavior is not desirable, you can use a <em>trampoline activity</em>,
+ * or an invisible activity that starts another activity in {@link Activity#onCreate},
+ * then calls {@link Activity#finish()}.
+ * The first activity should include an attribute setting
+ * of {@code android:taskAffinity=""} in the application's <code>AndroidManifest.xml</code>
+ * file, and the intent within the manifest shortcut should point at this first activity.
+ *
+ *
+ * <h3>Showing New Information in a Shortcut</h3>
+ * In order to avoid confusion, you should not use {@link #updateShortcuts(List)} to update
+ * a shortcut so that it contains conceptually different information.
+ *
+ * <p>For example, a phone application may publish the most frequently called contact as a dynamic
+ * shortcut.  Over time, this contact may change; when it does, the application should
+ * represent the changed contact with a new shortcut that contains a different ID, using either
+ * {@link #setDynamicShortcuts(List)} or {@link #addDynamicShortcuts(List)}, rather than updating
+ * the existing shortcut with {@link #updateShortcuts(List)}.
+ * This is because when the shortcut is pinned, changing
+ * it to reference a different contact will likely confuse the user.
+ *
+ * <p>On the other hand, when the
+ * contact's information has changed, such as the name or picture, the application should
+ * use {@link #updateShortcuts(List)} so that the pinned shortcut is updated too.
+ *
+ *
+ * <h3>Shortcut Display Order</h3>
+ * When the launcher displays the shortcuts that are associated with a particular launcher icon,
+ * the shortcuts should appear in the following order:
+ * <ul>
+ *   <li>First show manifest shortcuts
+ *   (if {@link ShortcutInfo#isDeclaredInManifest()} is {@code true}),
+ *   and then show dynamic shortcuts (if {@link ShortcutInfo#isDynamic()} is {@code true}).
+ *   <li>Within each category of shortcuts (manifest and dynamic), sort the shortcuts in order
+ *   of increasing rank according to {@link ShortcutInfo#getRank()}.
+ * </ul>
+ * <p>Shortcut ranks are non-negative sequential integers
+ * that determine the order in which shortcuts appear, assuming that the shortcuts are all in
+ * the same category.
+ * Ranks of existing shortcuts can be updated with
+ * {@link #updateShortcuts(List)}; you can use {@link #addDynamicShortcuts(List)} and
+ * {@link #setDynamicShortcuts(List)}, too.
+ *
+ * <p>Ranks are auto-adjusted so that they're unique for each target activity in each category
+ * (dynamic or manifest).  For example, if there are 3 dynamic shortcuts with ranks 0, 1 and 2,
+ * adding another dynamic shortcut with a rank of 1 represents a request to place this shortcut at
+ * the second position.
+ * In response, the third and fourth shortcuts move closer to the bottom of the shortcut list,
+ * with their ranks changing to 2 and 3, respectively.
+ *
+ * <h3>Rate Limiting</h3>
+ *
+ * Calls to {@link #setDynamicShortcuts(List)}, {@link #addDynamicShortcuts(List)}, and
+ * {@link #updateShortcuts(List)} may be rate-limited when called by background applications, or
+ * applications with no foreground activity or service.  When you attempt to call these methods
+ * from a background application after exceeding the rate limit, these APIs return {@code false}.
+ *
+ * <p>Applications with a foreground activity or service are not rate-limited.
+ *
+ * <p>Rate-limiting will be reset upon certain events, so that even background applications
+ * can call these APIs again until they are rate limit is reached again.
+ * These events include the following:
+ * <ul>
+ *   <li>When an application comes to the foreground.
+ *   <li>When the system locale changes.
+ *   <li>When the user performs an "inline reply" action on a notification.
+ * </ul>
+ *
+ * <p>When rate-limiting is active, {@link #isRateLimitingActive()} returns {@code true}.
+ *
+ * <h4>Resetting rate-limiting for testing</h4>
+ *
+ * If your application is rate-limited during development or testing, you can use the
+ * "Reset ShortcutManager rate-limiting" development option or the following adb command to reset
+ * it:
+ * <pre>
+ * adb shell cmd shortcut reset-throttling [ --user USER-ID ]
+ * </pre>
+ *
+ * <h3>Handling System Locale Changes</h3>
+ *
+ * Applications should update dynamic and pinned shortcuts when the system locale changes
+ * using the {@link Intent#ACTION_LOCALE_CHANGED} broadcast.
+ *
+ * <p>When the system locale changes, rate-limiting is reset, so even background applications
+ * can set dynamic shortcuts, add dynamic shortcuts, and update shortcuts until the rate limit
+ * is reached again.
+ *
  *
  * <h3>Backup and Restore</h3>
  *
- * Shortcuts will be backed up and restored across devices.  This means all information, including
- * IDs, must be meaningful on a different device.
+ * When an application has the {@code android:allowBackup="true"} attribute assignment included
+ * in its <code>AndroidManifest.xml</code> file, pinned shortcuts are
+ * backed up automatically and are restored when the user sets up a new device.
  *
- * <h3>APIs for launcher</h3>
+ * <h4>Categories of Shortcuts that are Backed Up</h4>
  *
- * Launcher applications should use {@link LauncherApps} to get shortcuts that are published from
- * applications.  Launcher applications can also pin shortcuts with
- * {@link LauncherApps#pinShortcuts(String, List, UserHandle)}.
+ * <ul>
+ *  <li>Pinned shortcuts are backed up.  Bitmap icons are not backed up by the system,
+ *  but launcher applications should back them up and restore them so that the user still sees icons
+ *  for pinned shortcuts on the launcher.  Applications can always use
+ *  {@link #updateShortcuts(List)} to re-publish icons.
  *
- * @hide
+ *  <li>Manifest shortcuts are not backed up, but when an application is re-installed on a new
+ *  device, they are re-published from the <code>AndroidManifest.xml</code> file, anyway.
+ *
+ *  <li>Dynamic shortcuts are <b>not</b> backed up.
+ * </ul>
+ *
+ * <p>Because dynamic shortcuts are not restored, it is recommended that applications check
+ * currently-published dynamic shortcuts using {@link #getDynamicShortcuts()}
+ * each time they are launched, and they should re-publish
+ * dynamic shortcuts when necessary.
+ *
+ * <pre>
+ * public class MainActivity extends Activity {
+ *     public void onCreate(Bundle savedInstanceState) {
+ *         super.onCreate(savedInstanceState);
+ *
+ *         ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
+ *
+ *         if (shortcutManager.getDynamicShortcuts().size() == 0) {
+ *             // Application restored; re-publish dynamic shortcuts.
+ *
+ *             if (shortcutManager.getPinnedShortcuts().size() > 0) {
+ *                 // Pinned shortcuts have been restored.  Use updateShortcuts() to make sure
+ *                 // they have up-to-date information.
+ *             }
+ *         }
+ *     }
+ *     :
+ *
+ * }
+ * </pre>
+ *
+ *
+ * <h4>Backup/restore and shortcut IDs</h4>
+ *
+ * Because pinned shortcuts are backed up and restored on new devices, shortcut IDs should be
+ * meaningful across devices; that is, IDs should contain either stable, constant strings
+ * or server-side identifiers,
+ * rather than identifiers generated locally that might not make sense on other devices.
+ *
+ *
+ * <h3>Report Shortcut Usage and Prediction</h3>
+ *
+ * Launcher applications may be capable of predicting which shortcuts will most likely be
+ * used at a given time by examining the shortcut usage history data.
+ *
+ * <p>In order to provide launchers with such data, publisher applications should
+ * report the shortcuts that are used with {@link #reportShortcutUsed(String)}
+ * when a shortcut is selected,
+ * <b>or when an action equivalent to a shortcut is taken by the user even if it wasn't started
+ * with the shortcut</b>.
+ *
+ * <p>For example, suppose a GPS navigation application supports "navigate to work" as a shortcut.
+ * It should then report when the user selects this shortcut <b>and</b> when the user chooses
+ * to navigate to work within the application itself.
+ * This helps the launcher application
+ * learn that the user wants to navigate to work at a certain time every
+ * weekday, and it can then show this shortcut in a suggestion list at the right time.
+ *
+ * <h3>Launcher API</h3>
+ *
+ * The {@link LauncherApps} class provides APIs for launcher applications to access shortcuts.
+ *
+ *
+ * <h3>Direct Boot and Shortcuts</h3>
+ *
+ * All shortcut information is stored in credential encrypted storage, so no shortcuts can be
+ * accessed when the user is locked.
  */
 public class ShortcutManager {
     private static final String TAG = "ShortcutManager";
@@ -115,15 +465,18 @@
     }
 
     /**
-     * Publish a list of shortcuts.  All existing dynamic shortcuts from the caller application
-     * will be replaced.
+     * Publish the list of shortcuts.  All existing dynamic shortcuts from the caller application
+     * will be replaced.  If there are already pinned shortcuts with the same IDs,
+     * the mutable pinned shortcuts are updated.
      *
      * <p>This API will be rate-limited.
      *
      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
      *
-     * @throws IllegalArgumentException if {@code shortcutInfoList} contains more than
-     * {@link #getMaxDynamicShortcutCount()} shortcuts.
+     * @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
+     * or when trying to update immutable shortcuts.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
     public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
         try {
@@ -135,8 +488,9 @@
     }
 
     /**
-     * Return all dynamic shortcuts from the caller application.  The number of result items
-     * will not exceed the value returned by {@link #getMaxDynamicShortcutCount()}.
+     * Return all dynamic shortcuts from the caller application.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
     @NonNull
     public List<ShortcutInfo> getDynamicShortcuts() {
@@ -149,15 +503,32 @@
     }
 
     /**
-     * Publish a single dynamic shortcut.  If there's already dynamic or pinned shortcuts with
-     * the same ID, they will all be updated.
+     * Return all manifest shortcuts from the caller application.
+     *
+     * @throws IllegalStateException when the user is locked.
+     */
+    @NonNull
+    public List<ShortcutInfo> getManifestShortcuts() {
+        try {
+            return mService.getManifestShortcuts(mContext.getPackageName(), injectMyUserId())
+                    .getList();
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Publish the list of dynamic shortcuts.  If there are already dynamic or pinned shortcuts with
+     * the same IDs, each mutable shortcut is updated.
      *
      * <p>This API will be rate-limited.
      *
      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
      *
-     * @throws IllegalArgumentException if the caller application has already published the
-     * max number of dynamic shortcuts.
+     * @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
+     * or when trying to update immutable shortcuts.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
     public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
         try {
@@ -169,7 +540,9 @@
     }
 
     /**
-     * Delete a single dynamic shortcut by ID.
+     * Delete dynamic shortcuts by ID.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
     public void removeDynamicShortcuts(@NonNull List<String> shortcutIds) {
         try {
@@ -182,6 +555,8 @@
 
     /**
      * Delete all dynamic shortcuts from the caller application.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
     public void removeAllDynamicShortcuts() {
         try {
@@ -193,6 +568,8 @@
 
     /**
      * Return all pinned shortcuts from the caller application.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
     @NonNull
     public List<ShortcutInfo> getPinnedShortcuts() {
@@ -205,11 +582,16 @@
     }
 
     /**
-     * Update all existing shortcuts with the same IDs.  Shortcuts may be pinned and/or dynamic.
+     * Update all existing shortcuts with the same IDs.  Target shortcuts may be pinned and/or
+     * dynamic, but they must not be immutable.
      *
      * <p>This API will be rate-limited.
      *
      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
+     *
+     * @throws IllegalArgumentException If trying to update immutable shortcuts.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
     public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
         try {
@@ -221,11 +603,94 @@
     }
 
     /**
-     * Return the max number of dynamic shortcuts that each application can have at a time.
+     * Disable pinned shortcuts.  For more details, see the Javadoc for the {@link ShortcutManager}
+     * class.
+     *
+     * @throws IllegalArgumentException If trying to disable immutable shortcuts.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
-    public int getMaxDynamicShortcutCount() {
+    public void disableShortcuts(@NonNull List<String> shortcutIds) {
         try {
-            return mService.getMaxDynamicShortcutCount(mContext.getPackageName(), injectMyUserId());
+            mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
+                    /* disabledMessage =*/ null, /* disabledMessageResId =*/ 0,
+                    injectMyUserId());
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * @hide old signature, kept for unit testing.
+     */
+    public void disableShortcuts(@NonNull List<String> shortcutIds, int disabledMessageResId) {
+        try {
+            mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
+                    /* disabledMessage =*/ null, disabledMessageResId,
+                    injectMyUserId());
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * @hide old signature, kept for unit testing.
+     */
+    public void disableShortcuts(@NonNull List<String> shortcutIds, String disabledMessage) {
+        disableShortcuts(shortcutIds, (CharSequence) disabledMessage);
+    }
+
+    /**
+     * Disable pinned shortcuts, showing the user a custom error message when they try to select
+     * the disabled shortcuts.
+     * For more details, see the Javadoc for the {@link ShortcutManager} class.
+     *
+     * @throws IllegalArgumentException If trying to disable immutable shortcuts.
+     *
+     * @throws IllegalStateException when the user is locked.
+     */
+    public void disableShortcuts(@NonNull List<String> shortcutIds, CharSequence disabledMessage) {
+        try {
+            mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
+                    disabledMessage, /* disabledMessageResId =*/ 0,
+                    injectMyUserId());
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Re-enable pinned shortcuts that were previously disabled.  If the target shortcuts
+     * already enabled, this method does nothing.
+     *
+     * @throws IllegalArgumentException If trying to enable immutable shortcuts.
+     *
+     * @throws IllegalStateException when the user is locked.
+     */
+    public void enableShortcuts(@NonNull List<String> shortcutIds) {
+        try {
+            mService.enableShortcuts(mContext.getPackageName(), shortcutIds, injectMyUserId());
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+
+    /**
+     * @hide old signature, kept for unit testing.
+     */
+    public int getMaxShortcutCountForActivity() {
+        return getMaxShortcutCountPerActivity();
+    }
+
+    /**
+     * Return the maximum number of dynamic and manifest shortcuts that each launcher icon
+     * can have at a time.
+     */
+    public int getMaxShortcutCountPerActivity() {
+        try {
+            return mService.getMaxShortcutCountPerActivity(
+                    mContext.getPackageName(), injectMyUserId());
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
@@ -236,6 +701,8 @@
      * before the rate limit counter is reset.
      *
      * @see #getRateLimitResetTime()
+     *
+     * @hide
      */
     public int getRemainingCallCount() {
         try {
@@ -250,6 +717,8 @@
      *
      * @see #getRemainingCallCount()
      * @see System#currentTimeMillis()
+     *
+     * @hide
      */
     public long getRateLimitResetTime() {
         try {
@@ -260,16 +729,82 @@
     }
 
     /**
-     * Return the max width and height for icons, in pixels.
+     * Return {@code true} when rate-limiting is active for the caller application.
+     *
+     * <p>See the class level javadoc for details.
+     *
+     * @throws IllegalStateException when the user is locked.
      */
-    public int getIconMaxDimensions() {
+    public boolean isRateLimitingActive() {
         try {
+            return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId())
+                    == 0;
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Return the max width for icons, in pixels.
+     */
+    public int getIconMaxWidth() {
+        try {
+            // TODO Implement it properly using xdpi.
             return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId());
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
     }
 
+    /**
+     * Return the max height for icons, in pixels.
+     */
+    public int getIconMaxHeight() {
+        try {
+            // TODO Implement it properly using ydpi.
+            return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId());
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Applications that publish shortcuts should call this method
+     * whenever the user selects the shortcut containing the given ID or when the user completes
+     * an action in the application that is equivalent to selecting the shortcut.
+     * For more details, see the Javadoc for the {@link ShortcutManager} class
+     *
+     * <p>The information is accessible via {@link UsageStatsManager#queryEvents}
+     * Typically, launcher applications use this information to build a prediction model
+     * so that they can promote the shortcuts that are likely to be used at the moment.
+     *
+     * @throws IllegalStateException when the user is locked.
+     */
+    public void reportShortcutUsed(String shortcutId) {
+        try {
+            mService.reportShortcutUsed(mContext.getPackageName(), shortcutId,
+                    injectMyUserId());
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Called internally when an application is considered to have come to foreground
+     * even when technically it's not.  This method resets the throttling for this package.
+     * For example, when the user sends an "inline reply" on an notification, the system UI will
+     * call it.
+     *
+     * @hide
+     */
+    public void onApplicationActive(@NonNull String packageName, @UserIdInt int userId) {
+        try {
+            mService.onApplicationActive(packageName, userId);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
     /** @hide injection point */
     @VisibleForTesting
     protected int injectMyUserId() {
diff --git a/core/java/android/content/pm/ShortcutServiceInternal.java b/core/java/android/content/pm/ShortcutServiceInternal.java
index 3f8bad1..af56105 100644
--- a/core/java/android/content/pm/ShortcutServiceInternal.java
+++ b/core/java/android/content/pm/ShortcutServiceInternal.java
@@ -53,7 +53,8 @@
             @NonNull String callingPackage, @NonNull String packageName,
             @NonNull List<String> shortcutIds, int userId);
 
-    public abstract Intent createShortcutIntent(int launcherUserId, @NonNull String callingPackage,
+    public abstract Intent[] createShortcutIntents(
+            int launcherUserId, @NonNull String callingPackage,
             @NonNull String packageName, @NonNull String shortcutId, int userId);
 
     public abstract void addListener(@NonNull ShortcutChangeListener listener);
@@ -67,10 +68,4 @@
 
     public abstract boolean hasShortcutHostPermission(int launcherUserId,
             @NonNull String callingPackage);
-
-    /**
-     * Called by AM when the system locale changes *within the AM lock*.  ABSOLUTELY do not take
-     * any locks in this method.
-     */
-    public abstract void onSystemLocaleChangedNoLock();
 }
diff --git a/core/java/android/content/pm/UserInfo.java b/core/java/android/content/pm/UserInfo.java
index dd3a36c..6cd8403 100644
--- a/core/java/android/content/pm/UserInfo.java
+++ b/core/java/android/content/pm/UserInfo.java
@@ -28,8 +28,8 @@
  */
 public class UserInfo implements Parcelable {
 
-    /** 8 bits for user type */
-    public static final int FLAG_MASK_USER_TYPE = 0x000000FF;
+    /** 16 bits for user type */
+    public static final int FLAG_MASK_USER_TYPE = 0x0000FFFF;
 
     /**
      * *************************** NOTE ***************************
@@ -87,6 +87,11 @@
      */
     public static final int FLAG_EPHEMERAL = 0x00000100;
 
+    /**
+     * User is for demo purposes only and can be removed at any time.
+     */
+    public static final int FLAG_DEMO = 0x00000200;
+
     public static final int NO_PROFILE_GROUP_ID = UserHandle.USER_NULL;
 
     public int id;
@@ -153,6 +158,10 @@
         return (flags & FLAG_INITIALIZED) == FLAG_INITIALIZED;
     }
 
+    public boolean isDemo() {
+        return (flags & FLAG_DEMO) == FLAG_DEMO;
+    }
+
     /**
      * Returns true if the user is a split system user.
      * <p>If {@link UserManager#isSplitSystemUser split system user mode} is not enabled,
diff --git a/core/java/android/content/res/Configuration.java b/core/java/android/content/res/Configuration.java
index 6f43d99..b2d518c 100644
--- a/core/java/android/content/res/Configuration.java
+++ b/core/java/android/content/res/Configuration.java
@@ -1543,27 +1543,41 @@
      * @hide
      */
     public static String localesToResourceQualifier(LocaleList locs) {
-        StringBuilder sb = new StringBuilder();
+        final StringBuilder sb = new StringBuilder();
         for (int i = 0; i < locs.size(); i++) {
-            Locale loc = locs.get(i);
-            boolean l = (loc.getLanguage().length() != 0);
-            boolean c = (loc.getCountry().length() != 0);
-            boolean s = (loc.getScript().length() != 0);
-            boolean v = (loc.getVariant().length() != 0);
-            // TODO: take script and extensions into account
-            if (l) {
-                if (sb.length() != 0) {
-                    sb.append(",");
-                }
+            final Locale loc = locs.get(i);
+            final int l = loc.getLanguage().length();
+            if (l == 0) {
+                continue;
+            }
+            final int s = loc.getScript().length();
+            final int c = loc.getCountry().length();
+            final int v = loc.getVariant().length();
+            // We ignore locale extensions, since they are not supported by AAPT
+
+            if (sb.length() != 0) {
+                sb.append(",");
+            }
+            if (l == 2 && s == 0 && (c == 0 || c == 2) && v == 0) {
+                // Traditional locale format: xx or xx-rYY
                 sb.append(loc.getLanguage());
-                if (c) {
+                if (c == 2) {
                     sb.append("-r").append(loc.getCountry());
-                    if (s) {
-                        sb.append("-s").append(loc.getScript());
-                        if (v) {
-                            sb.append("-v").append(loc.getVariant());
-                        }
-                    }
+                }
+            } else {
+                sb.append("b+");
+                sb.append(loc.getLanguage());
+                if (s != 0) {
+                    sb.append("+");
+                    sb.append(loc.getScript());
+                }
+                if (c != 0) {
+                    sb.append("+");
+                    sb.append(loc.getCountry());
+                }
+                if (v != 0) {
+                    sb.append("+");
+                    sb.append(loc.getVariant());
                 }
             }
         }
diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java
index 8d3940c..ad11307 100644
--- a/core/java/android/content/res/Resources.java
+++ b/core/java/android/content/res/Resources.java
@@ -201,12 +201,16 @@
      * Create a new Resources object on top of an existing set of assets in an
      * AssetManager.
      *
+     * @deprecated Resources should not be constructed by apps.
+     * See {@link android.content.Context#createConfigurationContext(Configuration)}.
+     *
      * @param assets Previously created AssetManager.
      * @param metrics Current display metrics to consider when
      *                selecting/computing resource values.
      * @param config Desired device configuration to consider when
      *               selecting/computing resource values (optional).
      */
+    @Deprecated
     public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config) {
         this(null);
         mResourcesImpl = new ResourcesImpl(assets, metrics, config, new DisplayAdjustments());
@@ -1762,7 +1766,10 @@
 
     /**
      * Store the newly updated configuration.
+     *
+     * @deprecated See {@link android.content.Context#createConfigurationContext(Configuration)}.
      */
+    @Deprecated
     public void updateConfiguration(Configuration config, DisplayMetrics metrics) {
         updateConfiguration(config, metrics, null);
     }
diff --git a/core/java/android/hardware/camera2/CameraCaptureSession.java b/core/java/android/hardware/camera2/CameraCaptureSession.java
index 38279a4..4b21187 100644
--- a/core/java/android/hardware/camera2/CameraCaptureSession.java
+++ b/core/java/android/hardware/camera2/CameraCaptureSession.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.hardware.camera2.params.OutputConfiguration;
 import android.os.Handler;
 import android.view.Surface;
 
@@ -220,6 +221,53 @@
     public abstract void tearDown(@NonNull Surface surface) throws CameraAccessException;
 
     /**
+     * <p>
+     * Finish the deferred output configurations where the output Surface was not configured before.
+     * </p>
+     * <p>
+     * For camera use cases where a preview and other output configurations need to be configured,
+     * it can take some time for the preview Surface to be ready (e.g., if the preview Surface is
+     * obtained from {@link android.view.SurfaceView}, the SurfaceView is ready after the UI layout
+     * is done, then it takes some time to get the preview Surface).
+     * </p>
+     * <p>
+     * To speed up camera startup time, the application can configure the
+     * {@link CameraCaptureSession} with the desired preview size, and defer the preview output
+     * configuration until the Surface is ready. After the {@link CameraCaptureSession} is created
+     * successfully with this deferred configuration and other normal configurations, the
+     * application can submit requests that don't include deferred output Surfaces. Once the
+     * deferred Surface is ready, the application can set the Surface to the same deferred output
+     * configuration with the {@link OutputConfiguration#setDeferredSurface} method, and then finish
+     * the deferred output configuration via this method, before it can submit requests with this
+     * output target.
+     * </p>
+     * <p>
+     * The output Surfaces included by this list of deferred {@link OutputConfiguration
+     * OutputConfigurations} can be used as {@link CaptureRequest} targets as soon as this call
+     * returns;
+     * </p>
+     * <p>
+     * This method is not supported by Legacy devices.
+     * </p>
+     *
+     * @param deferredOutputConfigs a list of {@link OutputConfiguration OutputConfigurations} that
+     *            have had {@link OutputConfiguration#setDeferredSurface setDeferredSurface} invoked
+     *            with a valid output Surface.
+     * @throws CameraAccessException if the camera device is no longer connected or has encountered
+     *             a fatal error.
+     * @throws IllegalStateException if this session is no longer active, either because the session
+     *             was explicitly closed, a new session has been created or the camera device has
+     *             been closed. Or if this output configuration was already finished with the
+     *             included surface before.
+     * @throws IllegalArgumentException for invalid output configurations, including ones where the
+     *             source of the Surface is no longer valid or the Surface is from a unsupported
+     *             source.
+     * @hide
+     */
+    public abstract void finishDeferredConfiguration(
+            List<OutputConfiguration> deferredOutputConfigs) throws CameraAccessException;
+
+    /**
      * <p>Submit a request for an image to be captured by the camera device.</p>
      *
      * <p>The request defines all the parameters for capturing the single image,
diff --git a/core/java/android/hardware/camera2/CameraManager.java b/core/java/android/hardware/camera2/CameraManager.java
index 3917bfa..145b1d0 100644
--- a/core/java/android/hardware/camera2/CameraManager.java
+++ b/core/java/android/hardware/camera2/CameraManager.java
@@ -267,6 +267,10 @@
      * @param cameraId The unique identifier of the camera device to open
      * @param callback The callback for the camera. Must not be null.
      * @param handler  The handler to invoke the callback on. Must not be null.
+     * @param uid      The UID of the application actually opening the camera.
+     *                 Must be USE_CALLING_UID unless the caller is a service
+     *                 that is trusted to open the device on behalf of an
+     *                 application and to forward the real UID.
      *
      * @throws CameraAccessException if the camera is disabled by device policy,
      * too many camera devices are already open, or the cameraId does not match
@@ -281,7 +285,7 @@
      * @see android.app.admin.DevicePolicyManager#setCameraDisabled
      */
     private CameraDevice openCameraDeviceUserAsync(String cameraId,
-            CameraDevice.StateCallback callback, Handler handler)
+            CameraDevice.StateCallback callback, Handler handler, final int uid)
             throws CameraAccessException {
         CameraCharacteristics characteristics = getCameraCharacteristics(cameraId);
         CameraDevice device = null;
@@ -317,7 +321,7 @@
                             "Camera service is currently unavailable");
                     }
                     cameraUser = cameraService.connectDevice(callbacks, id,
-                            mContext.getOpPackageName(), USE_CALLING_UID);
+                            mContext.getOpPackageName(), uid);
                 } else {
                     // Use legacy camera implementation for HAL1 devices
                     Log.i(TAG, "Using legacy camera HAL.");
@@ -434,6 +438,29 @@
             @NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler)
             throws CameraAccessException {
 
+        openCameraForUid(cameraId, callback, handler, USE_CALLING_UID);
+    }
+
+    /**
+     * Open a connection to a camera with the given ID, on behalf of another application
+     * specified by clientUid.
+     *
+     * <p>The behavior of this method matches that of {@link #openCamera}, except that it allows
+     * the caller to specify the UID to use for permission/etc verification. This can only be
+     * done by services trusted by the camera subsystem to act on behalf of applications and
+     * to forward the real UID.</p>
+     *
+     * @param clientUid
+     *             The UID of the application on whose behalf the camera is being opened.
+     *             Must be USE_CALLING_UID unless the caller is a trusted service.
+     *
+     * @hide
+     */
+    public void openCameraForUid(@NonNull String cameraId,
+            @NonNull final CameraDevice.StateCallback callback, @Nullable Handler handler,
+            int clientUid)
+            throws CameraAccessException {
+
         if (cameraId == null) {
             throw new IllegalArgumentException("cameraId was null");
         } else if (callback == null) {
@@ -447,7 +474,7 @@
             }
         }
 
-        openCameraDeviceUserAsync(cameraId, callback, handler);
+        openCameraDeviceUserAsync(cameraId, callback, handler, clientUid);
     }
 
     /**
diff --git a/core/java/android/hardware/camera2/DngCreator.java b/core/java/android/hardware/camera2/DngCreator.java
index 9478dc0..1a51acd 100644
--- a/core/java/android/hardware/camera2/DngCreator.java
+++ b/core/java/android/hardware/camera2/DngCreator.java
@@ -27,6 +27,7 @@
 import android.media.ExifInterface;
 import android.media.Image;
 import android.os.SystemClock;
+import android.util.Log;
 import android.util.Size;
 
 import java.io.IOException;
@@ -89,21 +90,43 @@
             throw new IllegalArgumentException("Null argument to DngCreator constructor");
         }
 
-        // Find current time
+        // Find current time in milliseconds since 1970
         long currentTime = System.currentTimeMillis();
+        // Assume that sensor timestamp has that timebase to start
+        long timeOffset = 0;
 
-        // Find boot time
-        long bootTimeMillis = currentTime - SystemClock.elapsedRealtime();
+        int timestampSource = characteristics.get(
+                CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE);
+
+        if (timestampSource == CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
+            // This means the same timebase as SystemClock.elapsedRealtime(),
+            // which is CLOCK_BOOTTIME
+            timeOffset = currentTime - SystemClock.elapsedRealtime();
+        } else if (timestampSource == CameraCharacteristics.SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN) {
+            // This means the same timebase as System.currentTimeMillis(),
+            // which is CLOCK_MONOTONIC
+            timeOffset = currentTime - SystemClock.uptimeMillis();
+        } else {
+            // Unexpected time source - treat as CLOCK_MONOTONIC
+            Log.w(TAG, "Sensor timestamp source is unexpected: " + timestampSource);
+            timeOffset = currentTime - SystemClock.uptimeMillis();
+        }
 
         // Find capture time (nanos since boot)
         Long timestamp = metadata.get(CaptureResult.SENSOR_TIMESTAMP);
         long captureTime = currentTime;
         if (timestamp != null) {
-            captureTime = timestamp / 1000000 + bootTimeMillis;
+            captureTime = timestamp / 1000000 + timeOffset;
         }
 
+        // Create this fresh each time since the time zone may change while a long-running application
+        // is active.
+        final DateFormat dateTimeStampFormat =
+            new SimpleDateFormat(TIFF_DATETIME_FORMAT);
+        dateTimeStampFormat.setTimeZone(TimeZone.getDefault());
+
         // Format for metadata
-        String formattedCaptureTime = sDateTimeStampFormat.format(captureTime);
+        String formattedCaptureTime = dateTimeStampFormat.format(captureTime);
 
         nativeInit(characteristics.getNativeCopy(), metadata.getNativeCopy(),
                 formattedCaptureTime);
@@ -450,13 +473,10 @@
     private static final String GPS_DATE_FORMAT_STR = "yyyy:MM:dd";
     private static final String TIFF_DATETIME_FORMAT = "yyyy:MM:dd HH:mm:ss";
     private static final DateFormat sExifGPSDateStamp = new SimpleDateFormat(GPS_DATE_FORMAT_STR);
-    private static final DateFormat sDateTimeStampFormat =
-            new SimpleDateFormat(TIFF_DATETIME_FORMAT);
     private final Calendar mGPSTimeStampCalendar = Calendar
             .getInstance(TimeZone.getTimeZone("UTC"));
 
     static {
-        sDateTimeStampFormat.setTimeZone(TimeZone.getDefault());
         sExifGPSDateStamp.setTimeZone(TimeZone.getTimeZone("UTC"));
     }
 
diff --git a/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java b/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java
index 6736d34..b10c341 100644
--- a/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraCaptureSessionImpl.java
@@ -24,6 +24,7 @@
 import android.hardware.camera2.dispatch.DuckTypingDispatcher;
 import android.hardware.camera2.dispatch.HandlerDispatcher;
 import android.hardware.camera2.dispatch.InvokeDispatcher;
+import android.hardware.camera2.params.OutputConfiguration;
 import android.hardware.camera2.utils.TaskDrainer;
 import android.hardware.camera2.utils.TaskSingleDrainer;
 import android.os.Handler;
@@ -156,6 +157,12 @@
     }
 
     @Override
+    public void finishDeferredConfiguration(
+            List<OutputConfiguration> deferredOutputConfigs) throws CameraAccessException {
+        mDeviceImpl.finishDeferredConfig(deferredOutputConfigs);
+    }
+
+    @Override
     public synchronized int capture(CaptureRequest request, CaptureCallback callback,
             Handler handler) throws CameraAccessException {
         if (request == null) {
@@ -279,23 +286,29 @@
     }
 
     @Override
-    public synchronized void abortCaptures() throws CameraAccessException {
-        checkNotClosed();
+    public void abortCaptures() throws CameraAccessException {
+        synchronized (this) {
+            checkNotClosed();
 
-        if (DEBUG) {
-            Log.v(TAG, mIdString + "abortCaptures");
+            if (DEBUG) {
+                Log.v(TAG, mIdString + "abortCaptures");
+            }
+
+            if (mAborting) {
+                Log.w(TAG, mIdString + "abortCaptures - Session is already aborting; doing nothing");
+                return;
+            }
+
+            mAborting = true;
+            mAbortDrainer.taskStarted();
         }
 
-        if (mAborting) {
-            Log.w(TAG, mIdString + "abortCaptures - Session is already aborting; doing nothing");
-            return;
+        synchronized (mDeviceImpl.mInterfaceLock) {
+            synchronized (this) {
+                mDeviceImpl.flush();
+                // The next BUSY -> IDLE set of transitions will mark the end of the abort.
+            }
         }
-
-        mAborting = true;
-        mAbortDrainer.taskStarted();
-
-        mDeviceImpl.flush();
-        // The next BUSY -> IDLE set of transitions will mark the end of the abort.
     }
 
     @Override
@@ -323,78 +336,86 @@
      * @see CameraCaptureSession#close
      */
     @Override
-    public synchronized void replaceSessionClose() {
-        /*
-         * In order for creating new sessions to be fast, the new session should be created
-         * before the old session is closed.
-         *
-         * Otherwise the old session will always unconfigure if there is no new session to
-         * replace it.
-         *
-         * Unconfiguring could add hundreds of milliseconds of delay. We could race and attempt
-         * to skip unconfigure if a new session is created before the captures are all drained,
-         * but this would introduce nondeterministic behavior.
-         */
+    public void replaceSessionClose() {
+        synchronized (this) {
+            /*
+             * In order for creating new sessions to be fast, the new session should be created
+             * before the old session is closed.
+             *
+             * Otherwise the old session will always unconfigure if there is no new session to
+             * replace it.
+             *
+             * Unconfiguring could add hundreds of milliseconds of delay. We could race and attempt
+             * to skip unconfigure if a new session is created before the captures are all drained,
+             * but this would introduce nondeterministic behavior.
+             */
 
-        if (DEBUG) Log.v(TAG, mIdString + "replaceSessionClose");
+            if (DEBUG) Log.v(TAG, mIdString + "replaceSessionClose");
 
-        // Set up fast shutdown. Possible alternative paths:
-        // - This session is active, so close() below starts the shutdown drain
-        // - This session is mid-shutdown drain, and hasn't yet reached the idle drain listener.
-        // - This session is already closed and has executed the idle drain listener, and
-        //   configureOutputsChecked(null) has already been called.
-        //
-        // Do not call configureOutputsChecked(null) going forward, since it would race with the
-        // configuration for the new session. If it was already called, then we don't care, since it
-        // won't get called again.
-        mSkipUnconfigure = true;
-
+            // Set up fast shutdown. Possible alternative paths:
+            // - This session is active, so close() below starts the shutdown drain
+            // - This session is mid-shutdown drain, and hasn't yet reached the idle drain listener.
+            // - This session is already closed and has executed the idle drain listener, and
+            //   configureOutputsChecked(null) has already been called.
+            //
+            // Do not call configureOutputsChecked(null) going forward, since it would race with the
+            // configuration for the new session. If it was already called, then we don't care,
+            // since it won't get called again.
+            mSkipUnconfigure = true;
+        }
         close();
     }
 
     @Override
-    public synchronized void close() {
+    public void close() {
+        synchronized (this) {
+            if (mClosed) {
+                if (DEBUG) Log.v(TAG, mIdString + "close - reentering");
+                return;
+            }
 
-        if (mClosed) {
-            if (DEBUG) Log.v(TAG, mIdString + "close - reentering");
-            return;
+            if (DEBUG) Log.v(TAG, mIdString + "close - first time");
+
+            mClosed = true;
         }
 
-        if (DEBUG) Log.v(TAG, mIdString + "close - first time");
+        synchronized (mDeviceImpl.mInterfaceLock) {
+            synchronized (this) {
+                /*
+                 * Flush out any repeating request. Since camera is closed, no new requests
+                 * can be queued, and eventually the entire request queue will be drained.
+                 *
+                 * If the camera device was already closed, short circuit and do nothing; since
+                 * no more internal device callbacks will fire anyway.
+                 *
+                 * Otherwise, once stopRepeating is done, wait for camera to idle, then unconfigure
+                 * the camera. Once that's done, fire #onClosed.
+                 */
+                try {
+                    mDeviceImpl.stopRepeating();
+                } catch (IllegalStateException e) {
+                    // OK: Camera device may already be closed, nothing else to do
 
-        mClosed = true;
+                    // TODO: Fire onClosed anytime we get the device onClosed or the ISE?
+                    // or just suppress the ISE only and rely onClosed.
+                    // Also skip any of the draining work if this is already closed.
 
-        /*
-         * Flush out any repeating request. Since camera is closed, no new requests
-         * can be queued, and eventually the entire request queue will be drained.
-         *
-         * If the camera device was already closed, short circuit and do nothing; since
-         * no more internal device callbacks will fire anyway.
-         *
-         * Otherwise, once stopRepeating is done, wait for camera to idle, then unconfigure the
-         * camera. Once that's done, fire #onClosed.
-         */
-        try {
-            mDeviceImpl.stopRepeating();
-        } catch (IllegalStateException e) {
-            // OK: Camera device may already be closed, nothing else to do
+                    // Short-circuit; queue callback immediately and return
+                    mStateCallback.onClosed(this);
+                    return;
+                } catch (CameraAccessException e) {
+                    // OK: close does not throw checked exceptions.
+                    Log.e(TAG, mIdString + "Exception while stopping repeating: ", e);
 
-            // TODO: Fire onClosed anytime we get the device onClosed or the ISE?
-            // or just suppress the ISE only and rely onClosed.
-            // Also skip any of the draining work if this is already closed.
-
-            // Short-circuit; queue callback immediately and return
-            mStateCallback.onClosed(this);
-            return;
-        } catch (CameraAccessException e) {
-            // OK: close does not throw checked exceptions.
-            Log.e(TAG, mIdString + "Exception while stopping repeating: ", e);
-
-            // TODO: call onError instead of onClosed if this happens
+                    // TODO: call onError instead of onClosed if this happens
+                }
+            }
         }
 
-        // If no sequences are pending, fire #onClosed immediately
-        mSequenceDrainer.beginDrain();
+        synchronized (this) {
+            // If no sequences are pending, fire #onClosed immediately
+            mSequenceDrainer.beginDrain();
+        }
     }
 
     /**
diff --git a/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java b/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java
index 8cd1da5..1c8e124 100644
--- a/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraConstrainedHighSpeedCaptureSessionImpl.java
@@ -21,6 +21,7 @@
 import android.hardware.camera2.CameraConstrainedHighSpeedCaptureSession;
 import android.hardware.camera2.CameraDevice;
 import android.hardware.camera2.CaptureRequest;
+import android.hardware.camera2.params.OutputConfiguration;
 import android.hardware.camera2.params.StreamConfigurationMap;
 import android.hardware.camera2.utils.SurfaceUtils;
 import android.os.Handler;
@@ -256,6 +257,12 @@
         return mSessionImpl.isAborting();
     }
 
+    @Override
+    public void finishDeferredConfiguration(List<OutputConfiguration> deferredOutputConfigs)
+            throws CameraAccessException {
+        mSessionImpl.finishDeferredConfiguration(deferredOutputConfigs);
+    }
+
     private class WrapperCallback extends StateCallback {
         private final StateCallback mCallback;
 
@@ -263,26 +270,32 @@
             mCallback = callback;
         }
 
+        @Override
         public void onConfigured(CameraCaptureSession session) {
             mCallback.onConfigured(CameraConstrainedHighSpeedCaptureSessionImpl.this);
         }
 
+        @Override
         public void onConfigureFailed(CameraCaptureSession session) {
             mCallback.onConfigureFailed(CameraConstrainedHighSpeedCaptureSessionImpl.this);
         }
 
+        @Override
         public void onReady(CameraCaptureSession session) {
             mCallback.onReady(CameraConstrainedHighSpeedCaptureSessionImpl.this);
         }
 
+        @Override
         public void onActive(CameraCaptureSession session) {
             mCallback.onActive(CameraConstrainedHighSpeedCaptureSessionImpl.this);
         }
 
+        @Override
         public void onClosed(CameraCaptureSession session) {
             mCallback.onClosed(CameraConstrainedHighSpeedCaptureSessionImpl.this);
         }
 
+        @Override
         public void onSurfacePrepared(CameraCaptureSession session, Surface surface) {
             mCallback.onSurfacePrepared(CameraConstrainedHighSpeedCaptureSessionImpl.this,
                     surface);
diff --git a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
index 0cee114..ee8a6d7 100644
--- a/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
+++ b/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java
@@ -407,7 +407,10 @@
                 int streamId = mConfiguredOutputs.keyAt(i);
                 OutputConfiguration outConfig = mConfiguredOutputs.valueAt(i);
 
-                if (!outputs.contains(outConfig)) {
+                if (!outputs.contains(outConfig) || outConfig.isDeferredConfiguration()) {
+                    // Always delete the deferred output configuration when the session
+                    // is created, as the deferred output configuration doesn't have unique surface
+                    // related identifies.
                     deleteList.add(streamId);
                 } else {
                     addSet.remove(outConfig);  // Don't create a stream previously created
@@ -744,6 +747,37 @@
         }
     }
 
+    public void finishDeferredConfig(List<OutputConfiguration> deferredConfigs)
+            throws CameraAccessException {
+        if (deferredConfigs == null || deferredConfigs.size() == 0) {
+            throw new IllegalArgumentException("deferred config is null or empty");
+        }
+
+        synchronized(mInterfaceLock) {
+            for (OutputConfiguration config : deferredConfigs) {
+                int streamId = -1;
+                for (int i = 0; i < mConfiguredOutputs.size(); i++) {
+                    // Have to use equal here, as createCaptureSessionByOutputConfigurations() and
+                    // createReprocessableCaptureSessionByConfigurations() do a copy of the configs.
+                    if (config.equals(mConfiguredOutputs.valueAt(i))) {
+                        streamId = mConfiguredOutputs.keyAt(i);
+                        break;
+                    }
+                }
+                if (streamId == -1) {
+                    throw new IllegalArgumentException("Deferred config is not part of this "
+                            + "session");
+                }
+
+                if (config.getSurface() == null) {
+                    throw new IllegalArgumentException("The deferred config for stream " + streamId
+                            + " must have a non-null surface");
+                }
+                mRemoteDevice.setDeferredConfiguration(streamId, config);
+            }
+        }
+    }
+
     public int capture(CaptureRequest request, CaptureCallback callback, Handler handler)
             throws CameraAccessException {
         if (DEBUG) {
@@ -2005,6 +2039,7 @@
      *
      * <p> Handle binder death for ICameraDeviceUser. Trigger onError.</p>
      */
+    @Override
     public void binderDied() {
         Log.w(TAG, "CameraDevice " + mCameraId + " died unexpectedly");
 
diff --git a/core/java/android/hardware/camera2/impl/ICameraDeviceUserWrapper.java b/core/java/android/hardware/camera2/impl/ICameraDeviceUserWrapper.java
index ef5f6d7..d77f60b 100644
--- a/core/java/android/hardware/camera2/impl/ICameraDeviceUserWrapper.java
+++ b/core/java/android/hardware/camera2/impl/ICameraDeviceUserWrapper.java
@@ -215,5 +215,14 @@
         }
     }
 
+    public void setDeferredConfiguration(int streamId, OutputConfiguration deferredConfig)
+            throws CameraAccessException {
+        try {
+            mRemoteDevice.setDeferredConfiguration(streamId, deferredConfig);
+        } catch (Throwable t) {
+            CameraManager.throwAsPublicException(t);
+            throw new UnsupportedOperationException("Unexpected exception", t);
+        }
+    }
 
 }
diff --git a/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java b/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
index acbf214..b9e75ee 100644
--- a/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
+++ b/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
@@ -567,6 +567,13 @@
     }
 
     @Override
+    public void setDeferredConfiguration(int steamId, OutputConfiguration config) {
+        String err = "Set deferred configuration is not supported on legacy devices";
+        Log.e(TAG, err);
+        throw new ServiceSpecificException(ICameraService.ERROR_INVALID_OPERATION, err);
+    }
+
+    @Override
     public int createInputStream(int width, int height, int format) {
         String err = "Creating input stream is not supported on legacy devices";
         Log.e(TAG, err);
diff --git a/core/java/android/hardware/camera2/params/OutputConfiguration.java b/core/java/android/hardware/camera2/params/OutputConfiguration.java
index 61b534b..69c00e9 100644
--- a/core/java/android/hardware/camera2/params/OutputConfiguration.java
+++ b/core/java/android/hardware/camera2/params/OutputConfiguration.java
@@ -20,6 +20,8 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.SystemApi;
+import android.graphics.ImageFormat;
+import android.hardware.camera2.CameraCaptureSession;
 import android.hardware.camera2.CameraDevice;
 import android.hardware.camera2.utils.HashCodeHelpers;
 import android.hardware.camera2.utils.SurfaceUtils;
@@ -95,6 +97,21 @@
     }
 
     /**
+     * Unknown surface source type.
+     */
+    private final int SURFACE_TYPE_UNKNOWN = -1;
+
+    /**
+     * The surface is obtained from {@link android.view.SurfaceView}.
+     */
+    private final int SURFACE_TYPE_SURFACE_VIEW = 0;
+
+    /**
+     * The surface is obtained from {@link android.graphics.SurfaceTexture}.
+     */
+    private final int SURFACE_TYPE_SURFACE_TEXTURE = 1;
+
+    /**
      * Create a new {@link OutputConfiguration} instance with a {@link Surface},
      * with a surface group ID.
      *
@@ -179,12 +196,110 @@
         checkNotNull(surface, "Surface must not be null");
         checkArgumentInRange(rotation, ROTATION_0, ROTATION_270, "Rotation constant");
         mSurfaceGroupId = surfaceGroupId;
+        mSurfaceType = SURFACE_TYPE_UNKNOWN;
         mSurface = surface;
         mRotation = rotation;
         mConfiguredSize = SurfaceUtils.getSurfaceSize(surface);
         mConfiguredFormat = SurfaceUtils.getSurfaceFormat(surface);
         mConfiguredDataspace = SurfaceUtils.getSurfaceDataspace(surface);
         mConfiguredGenerationId = surface.getGenerationId();
+        mIsDeferredConfig = false;
+    }
+
+    /**
+     * Create a new {@link OutputConfiguration} instance, with desired Surface size and Surface
+     * source class.
+     * <p>
+     * This constructor takes an argument for desired Surface size and the Surface source class
+     * without providing the actual output Surface. This is used to setup a output configuration
+     * with a deferred Surface. The application can use this output configuration to create a
+     * session.
+     * </p>
+     * <p>
+     * However, the actual output Surface must be set via {@link #setDeferredSurface} and finish the
+     * deferred Surface configuration via {@link CameraCaptureSession#finishDeferredConfiguration}
+     * before submitting a request with this Surface target. The deferred Surface can only be
+     * obtained from either from {@link android.view.SurfaceView} by calling
+     * {@link android.view.SurfaceHolder#getSurface}, or from
+     * {@link android.graphics.SurfaceTexture} via
+     * {@link android.view.Surface#Surface(android.graphics.SurfaceTexture)}).
+     * </p>
+     *
+     * @param surfaceSize Size for the deferred surface.
+     * @param klass a non-{@code null} {@link Class} object reference that indicates the source of
+     *            this surface. Only {@link android.view.SurfaceHolder SurfaceHolder.class} and
+     *            {@link android.graphics.SurfaceTexture SurfaceTexture.class} are supported.
+     * @hide
+     */
+    public <T> OutputConfiguration(@NonNull Size surfaceSize, @NonNull Class<T> klass) {
+        checkNotNull(klass, "surfaceSize must not be null");
+        checkNotNull(klass, "klass must not be null");
+        if (klass == android.view.SurfaceHolder.class) {
+            mSurfaceType = SURFACE_TYPE_SURFACE_VIEW;
+        } else if (klass == android.graphics.SurfaceTexture.class) {
+            mSurfaceType = SURFACE_TYPE_SURFACE_TEXTURE;
+        } else {
+            mSurfaceType = SURFACE_TYPE_UNKNOWN;
+            throw new IllegalArgumentException("Unknow surface source class type");
+        }
+
+        mSurfaceGroupId = SURFACE_GROUP_ID_NONE;
+        mSurface = null;
+        mRotation = ROTATION_0;
+        mConfiguredSize = surfaceSize;
+        mConfiguredFormat = StreamConfigurationMap.imageFormatToInternal(ImageFormat.PRIVATE);
+        mConfiguredDataspace = StreamConfigurationMap.imageFormatToDataspace(ImageFormat.PRIVATE);
+        mConfiguredGenerationId = 0;
+        mIsDeferredConfig = true;
+    }
+
+    /**
+     * Check if this configuration has deferred configuration.
+     *
+     * <p>This will return true if the output configuration was constructed with surface deferred.
+     * It will return true even after the deferred surface is set later.</p>
+     *
+     * @return true if this configuration has deferred surface.
+     * @hide
+     */
+    public boolean isDeferredConfiguration() {
+        return mIsDeferredConfig;
+    }
+
+    /**
+     * Set the deferred surface to this OutputConfiguration.
+     *
+     * <p>
+     * The deferred surface must be obtained from either from {@link android.view.SurfaceView} by
+     * calling {@link android.view.SurfaceHolder#getSurface}, or from
+     * {@link android.graphics.SurfaceTexture} via
+     * {@link android.view.Surface#Surface(android.graphics.SurfaceTexture)}). After the deferred
+     * surface is set, the application must finish the deferred surface configuration via
+     * {@link CameraCaptureSession#finishDeferredConfiguration} before submitting a request with
+     * this surface target.
+     * </p>
+     *
+     * @param surface The deferred surface to be set.
+     * @throws IllegalArgumentException if the Surface is invalid.
+     * @throws IllegalStateException if a Surface was already set to this deferred
+     *         OutputConfiguration.
+     * @hide
+     */
+    public void setDeferredSurface(@NonNull Surface surface) {
+        checkNotNull(surface, "Surface must not be null");
+        if (mSurface != null) {
+            throw new IllegalStateException("Deferred surface is already set!");
+        }
+
+        // This will throw IAE is the surface was abandoned.
+        Size surfaceSize = SurfaceUtils.getSurfaceSize(surface);
+        if (!surfaceSize.equals(mConfiguredSize)) {
+            Log.w(TAG, "Deferred surface size " + surfaceSize +
+                    " is different with pre-configured size " + mConfiguredSize +
+                    ", the pre-configured size will be used.");
+        }
+
+        mSurface = surface;
     }
 
     /**
@@ -203,10 +318,12 @@
         this.mSurface = other.mSurface;
         this.mRotation = other.mRotation;
         this.mSurfaceGroupId = other.mSurfaceGroupId;
+        this.mSurfaceType = other.mSurfaceType;
         this.mConfiguredDataspace = other.mConfiguredDataspace;
         this.mConfiguredFormat = other.mConfiguredFormat;
         this.mConfiguredSize = other.mConfiguredSize;
         this.mConfiguredGenerationId = other.mConfiguredGenerationId;
+        this.mIsDeferredConfig = other.mIsDeferredConfig;
     }
 
     /**
@@ -215,16 +332,30 @@
     private OutputConfiguration(@NonNull Parcel source) {
         int rotation = source.readInt();
         int surfaceSetId = source.readInt();
+        int surfaceType = source.readInt();
+        int width = source.readInt();
+        int height = source.readInt();
         Surface surface = Surface.CREATOR.createFromParcel(source);
-        checkNotNull(surface, "Surface must not be null");
         checkArgumentInRange(rotation, ROTATION_0, ROTATION_270, "Rotation constant");
         mSurfaceGroupId = surfaceSetId;
         mSurface = surface;
         mRotation = rotation;
-        mConfiguredSize = SurfaceUtils.getSurfaceSize(mSurface);
-        mConfiguredFormat = SurfaceUtils.getSurfaceFormat(mSurface);
-        mConfiguredDataspace = SurfaceUtils.getSurfaceDataspace(mSurface);
-        mConfiguredGenerationId = mSurface.getGenerationId();
+        if (surface != null) {
+            mSurfaceType = SURFACE_TYPE_UNKNOWN;
+            mConfiguredSize = SurfaceUtils.getSurfaceSize(mSurface);
+            mConfiguredFormat = SurfaceUtils.getSurfaceFormat(mSurface);
+            mConfiguredDataspace = SurfaceUtils.getSurfaceDataspace(mSurface);
+            mConfiguredGenerationId = mSurface.getGenerationId();
+            mIsDeferredConfig = true;
+        } else {
+            mSurfaceType = surfaceType;
+            mConfiguredSize = new Size(width, height);
+            mConfiguredFormat = StreamConfigurationMap.imageFormatToInternal(ImageFormat.PRIVATE);
+            mConfiguredGenerationId = 0;
+            mConfiguredDataspace =
+                    StreamConfigurationMap.imageFormatToDataspace(ImageFormat.PRIVATE);
+            mIsDeferredConfig = false;
+        }
     }
 
     /**
@@ -291,7 +422,12 @@
         }
         dest.writeInt(mRotation);
         dest.writeInt(mSurfaceGroupId);
-        mSurface.writeToParcel(dest, flags);
+        dest.writeInt(mSurfaceType);
+        dest.writeInt(mConfiguredSize.getWidth());
+        dest.writeInt(mConfiguredSize.getHeight());
+        if (mSurface != null) {
+            mSurface.writeToParcel(dest, flags);
+        }
     }
 
     /**
@@ -311,13 +447,20 @@
             return true;
         } else if (obj instanceof OutputConfiguration) {
             final OutputConfiguration other = (OutputConfiguration) obj;
+            boolean iSSurfaceEqual = mSurface == other.mSurface &&
+                    mConfiguredGenerationId == other.mConfiguredGenerationId ;
+            if (mIsDeferredConfig) {
+                Log.i(TAG, "deferred config has the same surface");
+                iSSurfaceEqual = true;
+            }
             return mRotation == other.mRotation &&
-                   mSurface == other.mSurface &&
-                   mConfiguredGenerationId == other.mConfiguredGenerationId &&
+                   iSSurfaceEqual&&
                    mConfiguredSize.equals(other.mConfiguredSize) &&
                    mConfiguredFormat == other.mConfiguredFormat &&
                    mConfiguredDataspace == other.mConfiguredDataspace &&
-                   mSurfaceGroupId == other.mSurfaceGroupId;
+                   mSurfaceGroupId == other.mSurfaceGroupId &&
+                   mSurfaceType == other.mSurfaceType &&
+                   mIsDeferredConfig == other.mIsDeferredConfig;
         }
         return false;
     }
@@ -327,15 +470,26 @@
      */
     @Override
     public int hashCode() {
+        // Need ensure that the hashcode remains unchanged after set a deferred surface. Otherwise
+        // The deferred output configuration will be lost in the camera streammap after the deferred
+        // surface is set.
+        if (mIsDeferredConfig) {
+            return HashCodeHelpers.hashCode(
+                    mRotation, mConfiguredSize.hashCode(), mConfiguredFormat, mConfiguredDataspace,
+                    mSurfaceGroupId, mSurfaceType);
+        }
+
         return HashCodeHelpers.hashCode(
             mRotation, mSurface.hashCode(), mConfiguredGenerationId,
             mConfiguredSize.hashCode(), mConfiguredFormat, mConfiguredDataspace, mSurfaceGroupId);
     }
 
     private static final String TAG = "OutputConfiguration";
-    private final Surface mSurface;
+    private Surface mSurface;
     private final int mRotation;
-    private int mSurfaceGroupId;
+    private final int mSurfaceGroupId;
+    // Surface source type, this is only used by the deferred surface configuration objects.
+    private final int mSurfaceType;
 
     // The size, format, and dataspace of the surface when OutputConfiguration is created.
     private final Size mConfiguredSize;
@@ -343,4 +497,6 @@
     private final int mConfiguredDataspace;
     // Surface generation ID to distinguish changes to Surface native internals
     private final int mConfiguredGenerationId;
+    // Flag indicating if this config has deferred surface.
+    private final boolean mIsDeferredConfig;
 }
diff --git a/core/java/android/hardware/camera2/utils/TaskDrainer.java b/core/java/android/hardware/camera2/utils/TaskDrainer.java
index 7c46e50..ed30ff3 100644
--- a/core/java/android/hardware/camera2/utils/TaskDrainer.java
+++ b/core/java/android/hardware/camera2/utils/TaskDrainer.java
@@ -29,8 +29,9 @@
  * (and new ones won't begin).
  *
  * <p>The initial state is to allow all tasks to be started and finished. A task may only be started
- * once, after which it must be finished before starting again. Likewise, finishing a task
- * that hasn't been started is also not allowed.</p>
+ * once, after which it must be finished before starting again. Likewise, a task may only be
+ * finished once, after which it must be started before finishing again. It is okay to finish a
+ * task before starting it due to different threads handling starting and finishing.</p>
  *
  * <p>When draining begins, no more new tasks can be started. This guarantees that at some
  * point when all the tasks are finished there will be no more collective new tasks,
@@ -60,6 +61,11 @@
 
     /** Set of tasks which have been started but not yet finished with #taskFinished */
     private final Set<T> mTaskSet = new HashSet<T>();
+    /**
+     * Set of tasks which have been finished but not yet started with #taskStarted. This may happen
+     * if taskStarted and taskFinished are called from two different threads.
+     */
+    private final Set<T> mEarlyFinishedTaskSet = new HashSet<T>();
     private final Object mLock = new Object();
 
     private boolean mDraining = false;
@@ -118,8 +124,12 @@
                 throw new IllegalStateException("Can't start more tasks after draining has begun");
             }
 
-            if (!mTaskSet.add(task)) {
-                throw new IllegalStateException("Task " + task + " was already started");
+            // Try to remove the task from the early finished set.
+            if (!mEarlyFinishedTaskSet.remove(task)) {
+                // The task is not finished early. Add it to the started set.
+                if (!mTaskSet.add(task)) {
+                    throw new IllegalStateException("Task " + task + " was already started");
+                }
             }
         }
     }
@@ -128,8 +138,7 @@
     /**
      * Mark an asynchronous task as having finished.
      *
-     * <p>A task cannot be finished if it hasn't started. Once finished, a task
-     * cannot be finished again (unless it's started again).</p>
+     * <p>A task cannot be finished more than once without first having started.</p>
      *
      * @param task a key to identify a task
      *
@@ -137,7 +146,7 @@
      * @see #beginDrain
      *
      * @throws IllegalStateException
-     *          If attempting to start a task which is already finished (and not re-started),
+     *          If attempting to finish a task which is already finished (and not started),
      */
     public void taskFinished(T task) {
         synchronized (mLock) {
@@ -145,8 +154,12 @@
                 Log.v(TAG + "[" + mName + "]", "taskFinished " + task);
             }
 
+            // Try to remove the task from started set.
             if (!mTaskSet.remove(task)) {
-                throw new IllegalStateException("Task " + task + " was already finished");
+                // Task is not started yet. Add it to the early finished set.
+                if (!mEarlyFinishedTaskSet.add(task)) {
+                    throw new IllegalStateException("Task " + task + " was already finished");
+                }
             }
 
             // If this is the last finished task and draining has already begun, fire #onDrained
diff --git a/core/java/android/hardware/display/DisplayManagerGlobal.java b/core/java/android/hardware/display/DisplayManagerGlobal.java
index 93da3e5..826eb74 100644
--- a/core/java/android/hardware/display/DisplayManagerGlobal.java
+++ b/core/java/android/hardware/display/DisplayManagerGlobal.java
@@ -354,9 +354,9 @@
         }
     }
 
-    public void requestColorTransform(int displayId, int colorTransformId) {
+    public void requestColorMode(int displayId, int colorMode) {
         try {
-            mDm.requestColorTransform(displayId, colorTransformId);
+            mDm.requestColorMode(displayId, colorMode);
         } catch (RemoteException ex) {
             throw ex.rethrowFromSystemServer();
         }
diff --git a/core/java/android/hardware/display/IDisplayManager.aidl b/core/java/android/hardware/display/IDisplayManager.aidl
index 8a1abf1..f696c8d 100644
--- a/core/java/android/hardware/display/IDisplayManager.aidl
+++ b/core/java/android/hardware/display/IDisplayManager.aidl
@@ -59,8 +59,8 @@
     // No permissions required.
     WifiDisplayStatus getWifiDisplayStatus();
 
-    // Requires CONFIGURE_DISPLAY_COLOR_TRANSFORM
-    void requestColorTransform(int displayId, int colorTransformId);
+    // Requires CONFIGURE_DISPLAY_COLOR_MODE
+    void requestColorMode(int displayId, int colorMode);
 
     // Requires CAPTURE_VIDEO_OUTPUT, CAPTURE_SECURE_VIDEO_OUTPUT, or an appropriate
     // MediaProjection token for certain combinations of flags.
diff --git a/core/java/android/hardware/fingerprint/FingerprintManager.java b/core/java/android/hardware/fingerprint/FingerprintManager.java
index 1ff2e8a..f17fd55 100644
--- a/core/java/android/hardware/fingerprint/FingerprintManager.java
+++ b/core/java/android/hardware/fingerprint/FingerprintManager.java
@@ -259,6 +259,7 @@
     public static class AuthenticationResult {
         private Fingerprint mFingerprint;
         private CryptoObject mCryptoObject;
+        private int mUserId;
 
         /**
          * Authentication result
@@ -267,9 +268,10 @@
          * @param fingerprint the recognized fingerprint data, if allowed.
          * @hide
          */
-        public AuthenticationResult(CryptoObject crypto, Fingerprint fingerprint) {
+        public AuthenticationResult(CryptoObject crypto, Fingerprint fingerprint, int userId) {
             mCryptoObject = crypto;
             mFingerprint = fingerprint;
+            mUserId = userId;
         }
 
         /**
@@ -286,6 +288,12 @@
          * @hide
          */
         public Fingerprint getFingerprint() { return mFingerprint; }
+
+        /**
+         * Obtain the userId for which this fingerprint was authenticated.
+         * @hide
+         */
+        public int getUserId() { return mUserId; }
     };
 
     /**
@@ -792,7 +800,7 @@
                     sendAcquiredResult((Long) msg.obj /* deviceId */, msg.arg1 /* acquire info */);
                     break;
                 case MSG_AUTHENTICATION_SUCCEEDED:
-                    sendAuthenticatedSucceeded((Fingerprint) msg.obj);
+                    sendAuthenticatedSucceeded((Fingerprint) msg.obj, msg.arg1 /* userId */);
                     break;
                 case MSG_AUTHENTICATION_FAILED:
                     sendAuthenticatedFailed();
@@ -840,9 +848,10 @@
             }
         }
 
-        private void sendAuthenticatedSucceeded(Fingerprint fp) {
+        private void sendAuthenticatedSucceeded(Fingerprint fp, int userId) {
             if (mAuthenticationCallback != null) {
-                final AuthenticationResult result = new AuthenticationResult(mCryptoObject, fp);
+                final AuthenticationResult result =
+                        new AuthenticationResult(mCryptoObject, fp, userId);
                 mAuthenticationCallback.onAuthenticationSucceeded(result);
             }
         }
@@ -981,8 +990,8 @@
         }
 
         @Override // binder call
-        public void onAuthenticationSucceeded(long deviceId, Fingerprint fp) {
-            mHandler.obtainMessage(MSG_AUTHENTICATION_SUCCEEDED, fp).sendToTarget();
+        public void onAuthenticationSucceeded(long deviceId, Fingerprint fp, int userId) {
+            mHandler.obtainMessage(MSG_AUTHENTICATION_SUCCEEDED, userId, 0, fp).sendToTarget();
         }
 
         @Override // binder call
diff --git a/core/java/android/hardware/fingerprint/IFingerprintServiceReceiver.aidl b/core/java/android/hardware/fingerprint/IFingerprintServiceReceiver.aidl
index 57a429f..b024b29 100644
--- a/core/java/android/hardware/fingerprint/IFingerprintServiceReceiver.aidl
+++ b/core/java/android/hardware/fingerprint/IFingerprintServiceReceiver.aidl
@@ -26,7 +26,7 @@
 oneway interface IFingerprintServiceReceiver {
     void onEnrollResult(long deviceId, int fingerId, int groupId, int remaining);
     void onAcquired(long deviceId, int acquiredInfo);
-    void onAuthenticationSucceeded(long deviceId, in Fingerprint fp);
+    void onAuthenticationSucceeded(long deviceId, in Fingerprint fp, int userId);
     void onAuthenticationFailed(long deviceId);
     void onError(long deviceId, int error);
     void onRemoved(long deviceId, int fingerId, int groupId);
diff --git a/core/java/android/hardware/input/InputManagerInternal.java b/core/java/android/hardware/input/InputManagerInternal.java
index 10fc8e6..01a5404 100644
--- a/core/java/android/hardware/input/InputManagerInternal.java
+++ b/core/java/android/hardware/input/InputManagerInternal.java
@@ -59,4 +59,9 @@
      * @param deviceId The id of input device.
      */
     public abstract void toggleCapsLock(int deviceId);
+
+    /**
+     * Set whether the input stack should deliver pulse gesture events when the device is asleep.
+     */
+    public abstract void setPulseGestureEnabled(boolean enabled);
 }
diff --git a/core/java/android/hardware/location/ContextHubManager.java b/core/java/android/hardware/location/ContextHubManager.java
index 0c3d4b3..d4dcacc 100644
--- a/core/java/android/hardware/location/ContextHubManager.java
+++ b/core/java/android/hardware/location/ContextHubManager.java
@@ -123,10 +123,18 @@
     /**
      * Load a nano app on a specified context hub.
      *
+     * Note that loading is asynchronous.  When we return from this method,
+     * the nano app (probably) hasn't loaded yet.  Assuming a return of 0
+     * from this method, then the final success/failure for the load, along
+     * with the "handle" for the nanoapp, is all delivered in a byte
+     * string via a call to Callback.onMessageReceipt.
+     *
+     * TODO(b/30784270): Provide a better success/failure and "handle" delivery.
+     *
      * @param hubHandle handle of context hub to load the app on.
      * @param app the nanoApp to load on the hub
      *
-     * @return int nanoAppInstance of the loaded nanoApp on success,
+     * @return 0 if the command for loading was sent to the context hub;
      *         -1 otherwise
      *
      * @see NanoApp
@@ -150,9 +158,17 @@
     /**
      * Unload a specified nanoApp
      *
-     * @param nanoAppHandle handle of the nanoApp to load
+     * Note that unloading is asynchronous.  When we return from this method,
+     * the nano app (probably) hasn't unloaded yet.  Assuming a return of 0
+     * from this method, then the final success/failure for the unload is
+     * delivered in a byte string via a call to Callback.onMessageReceipt.
      *
-     * @return int  0 on success, -1 otherwise
+     * TODO(b/30784270): Provide a better success/failure delivery.
+     *
+     * @param nanoAppHandle handle of the nanoApp to unload
+     *
+     * @return 0 if the command for unloading was sent to the context hub;
+     *         -1 otherwise
      */
     public int unloadNanoApp(int nanoAppHandle) {
         int retVal = -1;
@@ -169,6 +185,24 @@
     /**
      * get information about the nano app instance
      *
+     * NOTE: The returned NanoAppInstanceInfo does _not_ contain correct
+     * information for several fields, specifically:
+     * - getName()
+     * - getPublisher()
+     * - getNeededExecMemBytes()
+     * - getNeededReadMemBytes()
+     * - getNeededWriteMemBytes()
+     *
+     * For example, say you call loadNanoApp() with a NanoApp that has
+     * getName() returning "My Name".  Later, if you call getNanoAppInstanceInfo
+     * for that nanoapp, the returned NanoAppInstanceInfo's getName()
+     * method will claim "Preloaded app, unknown", even though you would
+     * have expected "My Name".  For now, as the user, you'll need to
+     * separately track the above fields if they are of interest to you.
+     *
+     * TODO(b/30943489): Have the returned NanoAppInstanceInfo contain the
+     *     correct information.
+     *
      * @param nanoAppHandle handle of the nanoAppInstance
      * @return NanoAppInstanceInfo Information about the nano app instance.
      *
@@ -209,6 +243,14 @@
     /**
      * Send a message to a specific nano app instance on a context hub.
      *
+     * Note that the return value of this method only speaks of success
+     * up to the point of sending this to the Context Hub.  It is not
+     * an assurance that the Context Hub successfully sent this message
+     * on to the nanoapp.  If assurance is desired, a protocol should be
+     * established between your code and the nanoapp, with the nanoapp
+     * sending a confirmation message (which will be reported via
+     * Callback.onMessageReceipt).
+     *
      * @param hubHandle handle of the hub to send the message to
      * @param nanoAppHandle  handle of the nano app to send to
      * @param message Message to be sent
diff --git a/core/java/android/hardware/location/ContextHubService.java b/core/java/android/hardware/location/ContextHubService.java
index 43e596f..06af461 100644
--- a/core/java/android/hardware/location/ContextHubService.java
+++ b/core/java/android/hardware/location/ContextHubService.java
@@ -16,6 +16,13 @@
 
 package android.hardware.location;
 
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.HashMap;
+
 import android.Manifest;
 import android.content.Context;
 import android.content.pm.PackageManager;
@@ -53,10 +60,14 @@
     private static final int PRE_LOADED_APP_MEM_REQ = 0;
 
     private static final int MSG_HEADER_SIZE = 4;
-    private static final int MSG_FIELD_TYPE = 0;
-    private static final int MSG_FIELD_VERSION = 1;
-    private static final int MSG_FIELD_HUB_HANDLE = 2;
-    private static final int MSG_FIELD_APP_INSTANCE = 3;
+    private static final int HEADER_FIELD_MSG_TYPE = 0;
+    private static final int HEADER_FIELD_MSG_VERSION = 1;
+    private static final int HEADER_FIELD_HUB_HANDLE = 2;
+    private static final int HEADER_FIELD_APP_INSTANCE = 3;
+
+    private static final int HEADER_FIELD_LOAD_APP_ID_LO = MSG_HEADER_SIZE;
+    private static final int HEADER_FIELD_LOAD_APP_ID_HI = MSG_HEADER_SIZE + 1;
+    private static final int MSG_LOAD_APP_HEADER_SIZE = MSG_HEADER_SIZE + 2;
 
     private static final int OS_APP_INSTANCE = -1;
 
@@ -137,6 +148,36 @@
         return mContextHubInfo[contextHubHandle];
     }
 
+    // TODO(b/30808791): Remove this when NanoApp's API is correctly treating
+    // app IDs as 64-bits.
+    private static long parseAppId(NanoApp app) {
+        // NOTE: If this shifting seems odd (since it's actually "ONAN"), note
+        //     that it matches how this is defined in context_hub.h.
+        final int HEADER_MAGIC =
+            (((int)'N' <<  0) |
+             ((int)'A' <<  8) |
+             ((int)'N' << 16) |
+             ((int)'O' << 24));
+        final int HEADER_MAGIC_OFFSET = 4;
+        final int HEADER_APP_ID_OFFSET = 8;
+
+        ByteBuffer header = ByteBuffer.wrap(app.getAppBinary())
+            .order(ByteOrder.LITTLE_ENDIAN);
+
+        try {
+            if (header.getInt(HEADER_MAGIC_OFFSET) == HEADER_MAGIC) {
+                // This is a legitimate nanoapp header.  Let's grab the app ID.
+                return header.getLong(HEADER_APP_ID_OFFSET);
+            }
+        } catch (IndexOutOfBoundsException e) {
+            // The header is undersized.  We'll fall through to our code
+            // path below, which handles being unable to parse the header.
+        }
+        // We failed to parse the header.  Even through it's probably wrong,
+        // let's give NanoApp's idea of our ID.  This is at least consistent.
+        return app.getAppId();
+    }
+
     @Override
     public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException {
         checkPermissions();
@@ -146,15 +187,32 @@
             return -1;
         }
 
-        int[] msgHeader = new int[MSG_HEADER_SIZE];
-        msgHeader[MSG_FIELD_HUB_HANDLE] = contextHubHandle;
-        msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE;
-        msgHeader[MSG_FIELD_VERSION] = 0;
-        msgHeader[MSG_FIELD_TYPE] = MSG_LOAD_NANO_APP;
+        int[] msgHeader = new int[MSG_LOAD_APP_HEADER_SIZE];
+        msgHeader[HEADER_FIELD_HUB_HANDLE] = contextHubHandle;
+        msgHeader[HEADER_FIELD_APP_INSTANCE] = OS_APP_INSTANCE;
+        msgHeader[HEADER_FIELD_MSG_VERSION] = 0;
+        msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_LOAD_NANO_APP;
 
-        if (nativeSendMessage(msgHeader, app.getAppBinary()) != 0) {
+        long appId = app.getAppId();
+        // TODO(b/30808791): Remove this hack when the NanoApp API is fixed,
+        //     and getAppId() returns a 'long' instead of an 'int'.
+        if ((appId >> 32) != 0) {
+            // We're unlikely to notice this warning, but at least
+            // we can avoid running our hack logic.
+            Log.w(TAG, "Code has not been updated since API fix.");
+        } else {
+            appId = parseAppId(app);
+        }
+
+        msgHeader[HEADER_FIELD_LOAD_APP_ID_LO] = (int)(appId & 0xFFFFFFFF);
+        msgHeader[HEADER_FIELD_LOAD_APP_ID_HI] = (int)((appId >> 32) & 0xFFFFFFFF);
+
+        int errVal = nativeSendMessage(msgHeader, app.getAppBinary());
+        if (errVal != 0) {
+            Log.e(TAG, "Send Message returns error" + contextHubHandle);
             return -1;
         }
+
         // Do not add an entry to mNanoAppInstance Hash yet. The HAL may reject the app
         return 0;
     }
@@ -169,12 +227,14 @@
 
         // Call Native interface here
         int[] msgHeader = new int[MSG_HEADER_SIZE];
-        msgHeader[MSG_FIELD_HUB_HANDLE] = ANY_HUB;
-        msgHeader[MSG_FIELD_APP_INSTANCE] = OS_APP_INSTANCE;
-        msgHeader[MSG_FIELD_VERSION] = 0;
-        msgHeader[MSG_FIELD_TYPE] = MSG_UNLOAD_NANO_APP;
+        msgHeader[HEADER_FIELD_HUB_HANDLE] = ANY_HUB;
+        msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppInstanceHandle;
+        msgHeader[HEADER_FIELD_MSG_VERSION] = 0;
+        msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_UNLOAD_NANO_APP;
 
-        if (nativeSendMessage(msgHeader, null) != 0) {
+        byte msg[] = new byte[0];
+
+        if (nativeSendMessage(msgHeader, msg) != 0) {
             return -1;
         }
 
@@ -222,10 +282,10 @@
         checkPermissions();
 
         int[] msgHeader = new int[MSG_HEADER_SIZE];
-        msgHeader[MSG_FIELD_HUB_HANDLE] = hubHandle;
-        msgHeader[MSG_FIELD_APP_INSTANCE] = nanoAppHandle;
-        msgHeader[MSG_FIELD_VERSION] = msg.getVersion();
-        msgHeader[MSG_FIELD_TYPE] = msg.getMsgType();
+        msgHeader[HEADER_FIELD_HUB_HANDLE] = hubHandle;
+        msgHeader[HEADER_FIELD_APP_INSTANCE] = nanoAppHandle;
+        msgHeader[HEADER_FIELD_MSG_VERSION] = msg.getVersion();
+        msgHeader[HEADER_FIELD_MSG_TYPE] = msg.getMsgType();
 
         return nativeSendMessage(msgHeader, msg.getData());
     }
@@ -269,15 +329,17 @@
             Log.v(TAG, "No message callbacks registered.");
             return 0;
         }
-        ContextHubMessage message =
-                new ContextHubMessage(header[MSG_FIELD_TYPE], header[MSG_FIELD_VERSION], data);
+
+        ContextHubMessage msg = new ContextHubMessage(header[HEADER_FIELD_MSG_TYPE],
+                                                      header[HEADER_FIELD_MSG_VERSION],
+                                                      data);
         for (int i = 0; i < callbacksCount; ++i) {
             IContextHubCallback callback = mCallbacksList.getBroadcastItem(i);
             try {
                 callback.onMessageReceipt(
-                        header[MSG_FIELD_HUB_HANDLE],
-                        header[MSG_FIELD_APP_INSTANCE],
-                        message);
+                        header[HEADER_FIELD_HUB_HANDLE],
+                        header[HEADER_FIELD_APP_INSTANCE],
+                        msg);
             } catch (RemoteException e) {
                 Log.i(TAG, "Exception (" + e + ") calling remote callback (" + callback + ").");
                 continue;
@@ -301,19 +363,34 @@
         appInfo.setNeededReadMemBytes(PRE_LOADED_APP_MEM_REQ);
         appInfo.setNeededWriteMemBytes(PRE_LOADED_APP_MEM_REQ);
 
+        String action;
+        if (mNanoAppHash.containsKey(appInstanceHandle)) {
+            action = "Updated";
+        } else {
+            action = "Added";
+        }
+
         mNanoAppHash.put(appInstanceHandle, appInfo);
-        Log.d(TAG, "Added app instance " + appInstanceHandle + " with id " + appId
-              + " version " + appVersion);
+        Log.d(TAG, action + " app instance " + appInstanceHandle + " with id "
+              + appId + " version " + appVersion);
+
+        return 0;
+    }
+
+    private int deleteAppInstance(int appInstanceHandle) {
+        if (mNanoAppHash.remove(appInstanceHandle) == null) {
+            return -1;
+        }
 
         return 0;
     }
 
     private void sendVrStateChangeMessageToApp(NanoAppInstanceInfo app, boolean vrModeEnabled) {
         int[] msgHeader = new int[MSG_HEADER_SIZE];
-        msgHeader[MSG_FIELD_TYPE] = 0;
-        msgHeader[MSG_FIELD_VERSION] = 0;
-        msgHeader[MSG_FIELD_HUB_HANDLE] = ANY_HUB;
-        msgHeader[MSG_FIELD_APP_INSTANCE] = app.getHandle();
+        msgHeader[HEADER_FIELD_MSG_TYPE] = 0;
+        msgHeader[HEADER_FIELD_MSG_VERSION] = 0;
+        msgHeader[HEADER_FIELD_HUB_HANDLE] = ANY_HUB;
+        msgHeader[HEADER_FIELD_APP_INSTANCE] = app.getHandle();
 
         byte[] data = new byte[1];
         data[0] = (byte) ((vrModeEnabled) ? 1 : 0);
diff --git a/core/java/android/hardware/location/NanoAppFilter.java b/core/java/android/hardware/location/NanoAppFilter.java
index 8db70e9..bf35a3d 100644
--- a/core/java/android/hardware/location/NanoAppFilter.java
+++ b/core/java/android/hardware/location/NanoAppFilter.java
@@ -43,7 +43,8 @@
     private long mAppIdVendorMask;
 
     // Id of the context hub this instance is expected on
-    private int mContextHubId;
+    // TODO: Provide an API which will let us change this HubId.
+    private int mContextHubId = HUB_ANY;
 
     /**
      * Flag indicating any version. With this flag set, all versions shall match provided version.
diff --git a/core/java/android/hardware/location/NanoAppInstanceInfo.java b/core/java/android/hardware/location/NanoAppInstanceInfo.java
index 71a5a88..ac6d83f 100644
--- a/core/java/android/hardware/location/NanoAppInstanceInfo.java
+++ b/core/java/android/hardware/location/NanoAppInstanceInfo.java
@@ -113,7 +113,12 @@
     }
 
     /**
-     * Set the application version
+     * Get the application version
+     *
+     * NOTE: There is a race condition where shortly after loading, this
+     * may return -1 instead of the correct version.
+     *
+     * TODO(b/30970527): Fix this race condition.
      *
      * @return int - version of the app
      */
diff --git a/core/java/android/hardware/usb/UsbDeviceConnection.java b/core/java/android/hardware/usb/UsbDeviceConnection.java
index c062b3a..893b954 100644
--- a/core/java/android/hardware/usb/UsbDeviceConnection.java
+++ b/core/java/android/hardware/usb/UsbDeviceConnection.java
@@ -16,8 +16,10 @@
 
 package android.hardware.usb;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.Context;
 import android.os.ParcelFileDescriptor;
-
 import java.io.FileDescriptor;
 
 
@@ -31,6 +33,8 @@
 
     private final UsbDevice mDevice;
 
+    private Context mContext;
+
     // used by the JNI code
     private long mNativeContext;
 
@@ -42,11 +46,22 @@
         mDevice = device;
     }
 
-    /* package */ boolean open(String name, ParcelFileDescriptor pfd) {
+    /* package */ boolean open(String name, ParcelFileDescriptor pfd,  @NonNull Context context) {
+        mContext = context.getApplicationContext();
+
         return native_open(name, pfd.getFileDescriptor());
     }
 
     /**
+     * @return The application context the connection was created for.
+     *
+     * @hide
+     */
+    public @Nullable Context getContext() {
+        return mContext;
+    }
+
+    /**
      * Releases all system resources related to the device.
      * Once the object is closed it cannot be used again.
      * The client must call {@link UsbManager#openDevice} again
diff --git a/core/java/android/hardware/usb/UsbManager.java b/core/java/android/hardware/usb/UsbManager.java
index ac968c8..cb2720a 100644
--- a/core/java/android/hardware/usb/UsbManager.java
+++ b/core/java/android/hardware/usb/UsbManager.java
@@ -21,6 +21,7 @@
 
 import android.app.PendingIntent;
 import android.content.Context;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.os.Bundle;
 import android.os.ParcelFileDescriptor;
 import android.os.Process;
@@ -329,7 +330,7 @@
             ParcelFileDescriptor pfd = mService.openDevice(deviceName);
             if (pfd != null) {
                 UsbDeviceConnection connection = new UsbDeviceConnection(device);
-                boolean result = connection.open(deviceName, pfd);
+                boolean result = connection.open(deviceName, pfd, mContext);
                 pfd.close();
                 if (result) {
                     return connection;
@@ -476,6 +477,26 @@
     }
 
     /**
+     * Grants permission to specified package for USB device without showing system dialog.
+     * Only system components can call this function, as it requires the MANAGE_USB permission.
+     * @param device to request permissions for
+     * @param packageName of package to grant permissions
+     *
+     * {@hide}
+     */
+    public void grantPermission(UsbDevice device, String packageName) {
+        try {
+            int uid = mContext.getPackageManager()
+                .getPackageUidAsUser(packageName, mContext.getUserId());
+            mService.grantDevicePermission(device, uid);
+        } catch (NameNotFoundException e) {
+            Log.e(TAG, "Package " + packageName + " not found.", e);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Returns true if the specified USB function is currently enabled when in device mode.
      * <p>
      * USB functions represent interfaces which are published to the host to access
diff --git a/core/java/android/inputmethodservice/AbstractInputMethodService.java b/core/java/android/inputmethodservice/AbstractInputMethodService.java
index 3531926..29177b6 100644
--- a/core/java/android/inputmethodservice/AbstractInputMethodService.java
+++ b/core/java/android/inputmethodservice/AbstractInputMethodService.java
@@ -16,11 +16,14 @@
 
 package android.inputmethodservice;
 
+import android.annotation.NonNull;
 import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
+import android.view.inputmethod.InputConnection;
+import android.view.inputmethod.InputContentInfo;
 import android.view.inputmethod.InputMethod;
 import android.view.inputmethod.InputMethodSession;
 
@@ -208,7 +211,7 @@
      *
      * @param event The motion event being received.
      * @return True if the event was handled in this function, false otherwise.
-     * @see View#onTrackballEvent
+     * @see android.view.View#onTrackballEvent(MotionEvent)
      */
     public boolean onTrackballEvent(MotionEvent event) {
         return false;
@@ -219,9 +222,30 @@
      *
      * @param event The motion event being received.
      * @return True if the event was handled in this function, false otherwise.
-     * @see View#onGenericMotionEvent
+     * @see android.view.View#onGenericMotionEvent(MotionEvent)
      */
     public boolean onGenericMotionEvent(MotionEvent event) {
         return false;
     }
+
+    /**
+     * Allow the receiver of {@link InputContentInfo} to obtain a temporary read-only access
+     * permission to the content.
+     *
+     * <p>Default implementation does nothing.</p>
+     *
+     * @param inputContentInfo Content to be temporarily exposed from the input method to the
+     * application.
+     * This cannot be {@code null}.
+     * @param inputConnection {@link InputConnection} with which
+     * {@link InputConnection#commitContent(InputContentInfo, int, android.os.Bundle)} will be
+     * called.
+     * @return {@code false} if we cannot allow a temporary access permission.
+     * @hide
+     */
+    public void exposeContent(@NonNull InputContentInfo inputContentInfo,
+            @NonNull InputConnection inputConnection) {
+        return;
+    }
+
 }
diff --git a/core/java/android/inputmethodservice/IInputMethodWrapper.java b/core/java/android/inputmethodservice/IInputMethodWrapper.java
index cc71a9c..167d5a0 100644
--- a/core/java/android/inputmethodservice/IInputMethodWrapper.java
+++ b/core/java/android/inputmethodservice/IInputMethodWrapper.java
@@ -168,7 +168,7 @@
                 int missingMethods = msg.arg1;
                 IInputContext inputContext = (IInputContext)args.arg1;
                 InputConnection ic = inputContext != null
-                        ? new InputConnectionWrapper(inputContext, missingMethods) : null;
+                        ? new InputConnectionWrapper(mTarget, inputContext, missingMethods) : null;
                 EditorInfo info = (EditorInfo)args.arg2;
                 info.makeCompatible(mTargetSdkVersion);
                 inputMethod.startInput(ic, info);
@@ -180,7 +180,7 @@
                 int missingMethods = msg.arg1;
                 IInputContext inputContext = (IInputContext)args.arg1;
                 InputConnection ic = inputContext != null
-                        ? new InputConnectionWrapper(inputContext, missingMethods) : null;
+                        ? new InputConnectionWrapper(mTarget, inputContext, missingMethods) : null;
                 EditorInfo info = (EditorInfo)args.arg2;
                 info.makeCompatible(mTargetSdkVersion);
                 inputMethod.restartInput(ic, info);
@@ -251,7 +251,7 @@
     public void bindInput(InputBinding binding) {
         // This IInputContext is guaranteed to implement all the methods.
         final int missingMethodFlags = 0;
-        InputConnection ic = new InputConnectionWrapper(
+        InputConnection ic = new InputConnectionWrapper(mTarget,
                 IInputContext.Stub.asInterface(binding.getConnectionToken()), missingMethodFlags);
         InputBinding nu = new InputBinding(ic, binding);
         mCaller.executeOrSendMessage(mCaller.obtainMessageO(DO_SET_INPUT_CONTEXT, nu));
diff --git a/core/java/android/inputmethodservice/InputMethodService.java b/core/java/android/inputmethodservice/InputMethodService.java
index 4799773..fede77d 100644
--- a/core/java/android/inputmethodservice/InputMethodService.java
+++ b/core/java/android/inputmethodservice/InputMethodService.java
@@ -23,6 +23,7 @@
 import android.annotation.DrawableRes;
 import android.annotation.IntDef;
 import android.annotation.MainThread;
+import android.annotation.NonNull;
 import android.app.ActivityManager;
 import android.app.Dialog;
 import android.content.Context;
@@ -65,6 +66,7 @@
 import android.view.inputmethod.ExtractedTextRequest;
 import android.view.inputmethod.InputBinding;
 import android.view.inputmethod.InputConnection;
+import android.view.inputmethod.InputContentInfo;
 import android.view.inputmethod.InputMethod;
 import android.view.inputmethod.InputMethodManager;
 import android.view.inputmethod.InputMethodSubtype;
@@ -2598,6 +2600,29 @@
     }
 
     /**
+     * Allow the receiver of {@link InputContentInfo} to obtain a temporary read-only access
+     * permission to the content.
+     *
+     * @param inputContentInfo Content to be temporarily exposed from the input method to the
+     * application.
+     * This cannot be {@code null}.
+     * @param inputConnection {@link InputConnection} with which
+     * {@link InputConnection#commitContent(InputContentInfo, Bundle)} will be called.
+     * @hide
+     */
+    @Override
+    public final void exposeContent(@NonNull InputContentInfo inputContentInfo,
+            @NonNull InputConnection inputConnection) {
+        if (inputConnection == null) {
+            return;
+        }
+        if (getCurrentInputConnection() != inputConnection) {
+            return;
+        }
+        mImm.exposeContent(mToken, inputContentInfo, getCurrentInputEditorInfo());
+    }
+
+    /**
      * Performs a dump of the InputMethodService's internal state.  Override
      * to add your own information to the dump.
      */
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index a45e6f5..0afb546 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -343,6 +343,15 @@
     public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
 
     /**
+     * Action used to display a dialog that asks the user whether to avoid a network that is no
+     * longer validated. This intent is used to start the dialog in settings via startActivity.
+     *
+     * @hide
+     */
+    public static final String ACTION_PROMPT_LOST_VALIDATION =
+            "android.net.conn.PROMPT_LOST_VALIDATION";
+
+    /**
      * Invalid tethering type.
      * @see #startTethering(int, OnStartTetheringCallback, boolean)
      * @hide
@@ -1035,6 +1044,26 @@
     }
 
     /**
+     * Request that this callback be invoked at ConnectivityService's earliest
+     * convenience with the current satisfying network's LinkProperties.
+     * If no such network exists no callback invocation is performed.
+     *
+     * The callback must have been registered with #requestNetwork() or
+     * #registerDefaultNetworkCallback(); callbacks registered with
+     * registerNetworkCallback() are not specific to any particular Network so
+     * do not cause any updates.
+     *
+     * @hide
+     */
+    public void requestLinkProperties(NetworkCallback networkCallback) {
+        try {
+            mService.requestLinkProperties(networkCallback.networkRequest);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}.  This
      * will return {@code null} if the network is unknown.
      * <p>This method requires the caller to hold the permission
@@ -1052,6 +1081,26 @@
     }
 
     /**
+     * Request that this callback be invoked at ConnectivityService's earliest
+     * convenience with the current satisfying network's NetworkCapabilities.
+     * If no such network exists no callback invocation is performed.
+     *
+     * The callback must have been registered with #requestNetwork() or
+     * #registerDefaultNetworkCallback(); callbacks registered with
+     * registerNetworkCallback() are not specific to any particular Network so
+     * do not cause any updates.
+     *
+     * @hide
+     */
+    public void requestNetworkCapabilities(NetworkCallback networkCallback) {
+        try {
+            mService.requestNetworkCapabilities(networkCallback.networkRequest);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Gets the URL that should be used for resolving whether a captive portal is present.
      * 1. This URL should respond with a 204 response to a GET request to indicate no captive
      *    portal is present.
@@ -1776,6 +1825,16 @@
         return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     }
 
+    /* TODO: These permissions checks don't belong in client-side code. Move them to
+     * services.jar, possibly in com.android.server.net. */
+
+    /** {@hide} */
+    public static final boolean checkChangePermission(Context context) {
+        int uid = Binder.getCallingUid();
+        return Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
+                .getPackageNameForUid(context, uid), false /* throwException */);
+    }
+
     /** {@hide} */
     public static final void enforceChangePermission(Context context) {
         int uid = Binder.getCallingUid();
@@ -3103,14 +3162,11 @@
             throw new IllegalArgumentException("Invalid NetworkCallback");
         }
         try {
+            // CallbackHandler will release callback when receiving CALLBACK_RELEASED.
             mService.releaseNetworkRequest(networkCallback.networkRequest);
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
-
-        synchronized (sNetworkCallback) {
-            sNetworkCallback.remove(networkCallback.networkRequest);
-        }
     }
 
     /**
@@ -3152,6 +3208,27 @@
     }
 
     /**
+     * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
+     * only meaningful if the system is configured not to penalize such networks, e.g., if the
+     * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
+     * NETWORK_AVOID_BAD_WIFI setting is unset}.
+     *
+     * <p>This method requires the caller to hold the permission
+     * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
+     *
+     * @param network The network to accept.
+     *
+     * @hide
+     */
+    public void setAvoidUnvalidated(Network network) {
+        try {
+            mService.setAvoidUnvalidated(network);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Resets all connectivity manager settings back to factory defaults.
      * @hide
      */
diff --git a/core/java/android/net/ConnectivityMetricsLogger.java b/core/java/android/net/ConnectivityMetricsLogger.java
index d8cdde9..9a2d4e0 100644
--- a/core/java/android/net/ConnectivityMetricsLogger.java
+++ b/core/java/android/net/ConnectivityMetricsLogger.java
@@ -23,6 +23,8 @@
 import android.os.ServiceManager;
 import android.util.Log;
 
+import com.android.internal.annotations.VisibleForTesting;
+
 /** {@hide} */
 @SystemApi
 public class ConnectivityMetricsLogger {
@@ -33,28 +35,58 @@
 
     // Component Tags
     public static final int COMPONENT_TAG_CONNECTIVITY = 0;
-    public static final int COMPONENT_TAG_BLUETOOTH = 1;
-    public static final int COMPONENT_TAG_WIFI = 2;
-    public static final int COMPONENT_TAG_TELECOM = 3;
-    public static final int COMPONENT_TAG_TELEPHONY = 4;
-
-    public static final int NUMBER_OF_COMPONENTS = 5;
+    public static final int COMPONENT_TAG_BLUETOOTH    = 1;
+    public static final int COMPONENT_TAG_WIFI         = 2;
+    public static final int COMPONENT_TAG_TELECOM      = 3;
+    public static final int COMPONENT_TAG_TELEPHONY    = 4;
+    public static final int NUMBER_OF_COMPONENTS       = 5;
 
     // Event Tag
     public static final int TAG_SKIPPED_EVENTS = -1;
 
     public static final String DATA_KEY_EVENTS_COUNT = "count";
 
-    private IConnectivityMetricsLogger mService;
-
-    private long mServiceUnblockedTimestampMillis = 0;
-    private int mNumSkippedEvents = 0;
+    /** {@hide} */ protected IConnectivityMetricsLogger mService;
+    /** {@hide} */ protected volatile long mServiceUnblockedTimestampMillis;
+    private int mNumSkippedEvents;
 
     public ConnectivityMetricsLogger() {
-        mService = IConnectivityMetricsLogger.Stub.asInterface(ServiceManager.getService(
-                CONNECTIVITY_METRICS_LOGGER_SERVICE));
+        // TODO: consider not initializing mService in constructor
+        this(IConnectivityMetricsLogger.Stub.asInterface(
+                ServiceManager.getService(CONNECTIVITY_METRICS_LOGGER_SERVICE)));
     }
 
+    /** {@hide} */
+    @VisibleForTesting
+    public ConnectivityMetricsLogger(IConnectivityMetricsLogger service) {
+        mService = service;
+    }
+
+    /** {@hide} */
+    protected boolean checkLoggerService() {
+        if (mService != null) {
+            return true;
+        }
+        // Two threads racing here will write the same pointer because getService
+        // is idempotent once MetricsLoggerService is initialized.
+        mService = IConnectivityMetricsLogger.Stub.asInterface(
+                ServiceManager.getService(CONNECTIVITY_METRICS_LOGGER_SERVICE));
+        return mService != null;
+    }
+
+    /**
+     * Log a ConnectivityMetricsEvent.
+     *
+     * This method keeps track of skipped events when MetricsLoggerService throttles input events.
+     * It skips logging when MetricsLoggerService is active. When throttling ends, it logs a
+     * meta-event containing the number of events dropped. It is not safe to call this method
+     * concurrently from different threads.
+     *
+     * @param timestamp is the epoch timestamp of the event in ms.
+     * @param componentTag is the COMPONENT_* constant the event belongs to.
+     * @param eventTag is an event type constant whose meaning is specific to the component tag.
+     * @param data is a Parcelable instance representing the event.
+     */
     public void logEvent(long timestamp, int componentTag, int eventTag, Parcelable data) {
         if (mService == null) {
             if (DBG) {
@@ -77,6 +109,12 @@
             // Log number of skipped events
             Bundle b = new Bundle();
             b.putInt(DATA_KEY_EVENTS_COUNT, mNumSkippedEvents);
+
+            // Log the skipped event.
+            // TODO: Note that some of the clients push all states events into the server,
+            // If we lose some states logged here, we might mess up the statistics happened at the
+            // backend. One of the options is to introduce a non-skippable flag for important events
+            // that are logged.
             skippedEventsEvent = new ConnectivityMetricsEvent(mServiceUnblockedTimestampMillis,
                     componentTag, TAG_SKIPPED_EVENTS, b);
 
@@ -104,7 +142,7 @@
                 }
             }
         } catch (RemoteException e) {
-            Log.e(TAG, "Error logging event " + e.getMessage());
+            Log.e(TAG, "Error logging event", e);
         }
     }
 
@@ -121,8 +159,8 @@
     public ConnectivityMetricsEvent[] getEvents(ConnectivityMetricsEvent.Reference reference) {
         try {
             return mService.getEvents(reference);
-        } catch (RemoteException ex) {
-            Log.e(TAG, "IConnectivityMetricsLogger.getEvents: " + ex);
+        } catch (RemoteException e) {
+            Log.e(TAG, "IConnectivityMetricsLogger.getEvents", e);
             return null;
         }
     }
@@ -133,8 +171,8 @@
     public boolean register(PendingIntent newEventsIntent) {
         try {
             return mService.register(newEventsIntent);
-        } catch (RemoteException ex) {
-            Log.e(TAG, "IConnectivityMetricsLogger.register: " + ex);
+        } catch (RemoteException e) {
+            Log.e(TAG, "IConnectivityMetricsLogger.register", e);
             return false;
         }
     }
@@ -142,11 +180,10 @@
     public boolean unregister(PendingIntent newEventsIntent) {
         try {
             mService.unregister(newEventsIntent);
-        } catch (RemoteException ex) {
-            Log.e(TAG, "IConnectivityMetricsLogger.unregister: " + ex);
+            return true;
+        } catch (RemoteException e) {
+            Log.e(TAG, "IConnectivityMetricsLogger.unregister", e);
             return false;
         }
-
-        return true;
     }
 }
diff --git a/core/java/android/net/IConnectivityManager.aidl b/core/java/android/net/IConnectivityManager.aidl
index 0d518cc..4aabda9 100644
--- a/core/java/android/net/IConnectivityManager.aidl
+++ b/core/java/android/net/IConnectivityManager.aidl
@@ -156,9 +156,12 @@
     void pendingListenForNetwork(in NetworkCapabilities networkCapabilities,
             in PendingIntent operation);
 
+    void requestLinkProperties(in NetworkRequest networkRequest);
+    void requestNetworkCapabilities(in NetworkRequest networkRequest);
     void releaseNetworkRequest(in NetworkRequest networkRequest);
 
     void setAcceptUnvalidated(in Network network, boolean accept, boolean always);
+    void setAvoidUnvalidated(in Network network);
 
     int getRestoreDefaultNetworkDelay(int networkType);
 
diff --git a/core/java/com/android/internal/app/IEphemeralResolver.aidl b/core/java/android/net/IIpConnectivityMetrics.aidl
similarity index 62%
copy from core/java/com/android/internal/app/IEphemeralResolver.aidl
copy to core/java/android/net/IIpConnectivityMetrics.aidl
index 40429ee..8f634bb 100644
--- a/core/java/com/android/internal/app/IEphemeralResolver.aidl
+++ b/core/java/android/net/IIpConnectivityMetrics.aidl
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,11 +14,16 @@
  * limitations under the License.
  */
 
-package com.android.internal.app;
+package android.net;
 
-import android.content.Intent;
-import android.os.IRemoteCallback;
+import android.os.Parcelable;
+import android.net.ConnectivityMetricsEvent;
 
-oneway interface IEphemeralResolver {
-    void getEphemeralResolveInfoList(IRemoteCallback callback, int digestPrefix, int sequence);
+/** {@hide} */
+interface IIpConnectivityMetrics {
+
+    /**
+     * @return number of remaining available slots in buffer.
+     */
+    int logEvent(in ConnectivityMetricsEvent event);
 }
diff --git a/core/java/android/net/LinkAddress.java b/core/java/android/net/LinkAddress.java
index 384ab1c..6e74f14 100644
--- a/core/java/android/net/LinkAddress.java
+++ b/core/java/android/net/LinkAddress.java
@@ -103,7 +103,7 @@
     private boolean isIPv6ULA() {
         if (address != null && address instanceof Inet6Address) {
             byte[] bytes = address.getAddress();
-            return ((bytes[0] & (byte)0xfc) == (byte)0xfc);
+            return ((bytes[0] & (byte)0xfe) == (byte)0xfc);
         }
         return false;
     }
diff --git a/core/java/android/net/NetworkCapabilities.java b/core/java/android/net/NetworkCapabilities.java
index ebb9601..6196400 100644
--- a/core/java/android/net/NetworkCapabilities.java
+++ b/core/java/android/net/NetworkCapabilities.java
@@ -182,8 +182,15 @@
      */
     public static final int NET_CAPABILITY_CAPTIVE_PORTAL = 17;
 
+    /**
+     * Indicates that this network is available for use by apps, and not a network that is being
+     * kept up in the background to facilitate fast network switching.
+     * @hide
+     */
+    public static final int NET_CAPABILITY_FOREGROUND = 18;
+
     private static final int MIN_NET_CAPABILITY = NET_CAPABILITY_MMS;
-    private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_CAPTIVE_PORTAL;
+    private static final int MAX_NET_CAPABILITY = NET_CAPABILITY_FOREGROUND;
 
     /**
      * Network capabilities that are expected to be mutable, i.e., can change while a particular
@@ -194,7 +201,8 @@
             // http://b/18206275
             (1 << NET_CAPABILITY_TRUSTED) |
             (1 << NET_CAPABILITY_VALIDATED) |
-            (1 << NET_CAPABILITY_CAPTIVE_PORTAL);
+            (1 << NET_CAPABILITY_CAPTIVE_PORTAL) |
+            (1 << NET_CAPABILITY_FOREGROUND);
 
     /**
      * Network specifier for factories which want to match any network specifier
@@ -217,8 +225,7 @@
      * get immediately torn down because they do not have the requested capability.
      */
     private static final long NON_REQUESTABLE_CAPABILITIES =
-            (1 << NET_CAPABILITY_VALIDATED) |
-            (1 << NET_CAPABILITY_CAPTIVE_PORTAL);
+            MUTABLE_CAPABILITIES & ~(1 << NET_CAPABILITY_TRUSTED);
 
     /**
      * Capabilities that are set by default when the object is constructed.
@@ -325,6 +332,7 @@
     public String describeFirstNonRequestableCapability() {
         if (hasCapability(NET_CAPABILITY_VALIDATED)) return "NET_CAPABILITY_VALIDATED";
         if (hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return "NET_CAPABILITY_CAPTIVE_PORTAL";
+        if (hasCapability(NET_CAPABILITY_FOREGROUND)) return "NET_CAPABILITY_FOREGROUND";
         // This cannot happen unless the preceding checks are incomplete.
         if ((mNetworkCapabilities & NON_REQUESTABLE_CAPABILITIES) != 0) {
             return "unknown non-requestable capabilities " + Long.toHexString(mNetworkCapabilities);
@@ -352,6 +360,11 @@
                 (that.mNetworkCapabilities & ~MUTABLE_CAPABILITIES));
     }
 
+    private boolean equalsNetCapabilitiesRequestable(NetworkCapabilities that) {
+        return ((this.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES) ==
+                (that.mNetworkCapabilities & ~NON_REQUESTABLE_CAPABILITIES));
+    }
+
     /**
      * Removes the NET_CAPABILITY_NOT_RESTRICTED capability if all the capabilities it provides are
      * typically provided by restricted networks.
@@ -756,6 +769,19 @@
                 equalsSpecifier(nc));
     }
 
+    /**
+     * Checks that our requestable capabilities are the same as those of the given
+     * {@code NetworkCapabilities}.
+     *
+     * @hide
+     */
+    public boolean equalRequestableCapabilities(NetworkCapabilities nc) {
+        if (nc == null) return false;
+        return (equalsNetCapabilitiesRequestable(nc) &&
+                equalsTransportTypes(nc) &&
+                equalsSpecifier(nc));
+    }
+
     @Override
     public boolean equals(Object obj) {
         if (obj == null || (obj instanceof NetworkCapabilities == false)) return false;
@@ -840,6 +866,7 @@
                 case NET_CAPABILITY_NOT_VPN:        capabilities += "NOT_VPN"; break;
                 case NET_CAPABILITY_VALIDATED:      capabilities += "VALIDATED"; break;
                 case NET_CAPABILITY_CAPTIVE_PORTAL: capabilities += "CAPTIVE_PORTAL"; break;
+                case NET_CAPABILITY_FOREGROUND:     capabilities += "FOREGROUND"; break;
             }
             if (++i < types.length) capabilities += "&";
         }
diff --git a/core/java/android/net/NetworkIdentity.java b/core/java/android/net/NetworkIdentity.java
index 9cd563e..d570e66 100644
--- a/core/java/android/net/NetworkIdentity.java
+++ b/core/java/android/net/NetworkIdentity.java
@@ -175,7 +175,11 @@
 
         if (isNetworkTypeMobile(type)) {
             if (state.subscriberId == null) {
-                Slog.w(TAG, "Active mobile network without subscriber!");
+                if (state.networkInfo.getState() != NetworkInfo.State.DISCONNECTED &&
+                        state.networkInfo.getState() != NetworkInfo.State.UNKNOWN) {
+                    Slog.w(TAG, "Active mobile network without subscriber! ni = "
+                            + state.networkInfo);
+                }
             }
 
             subscriberId = state.subscriberId;
diff --git a/core/java/android/net/NetworkMisc.java b/core/java/android/net/NetworkMisc.java
index 5511a24..69f50a2 100644
--- a/core/java/android/net/NetworkMisc.java
+++ b/core/java/android/net/NetworkMisc.java
@@ -52,6 +52,15 @@
     public boolean acceptUnvalidated;
 
     /**
+     * Set to avoid surfacing the "Sign in to network" notification.
+     * if carrier receivers/apps are registered to handle the carrier-specific provisioning
+     * procedure, a carrier specific provisioning notification will be placed.
+     * only one notification should be displayed. This field is set based on
+     * which notification should be used for provisioning.
+     */
+    public boolean provisioningNotificationDisabled;
+
+    /**
      * For mobile networks, this is the subscriber ID (such as IMSI).
      */
     public String subscriberId;
@@ -65,6 +74,7 @@
             explicitlySelected = nm.explicitlySelected;
             acceptUnvalidated = nm.acceptUnvalidated;
             subscriberId = nm.subscriberId;
+            provisioningNotificationDisabled = nm.provisioningNotificationDisabled;
         }
     }
 
@@ -79,6 +89,7 @@
         out.writeInt(explicitlySelected ? 1 : 0);
         out.writeInt(acceptUnvalidated ? 1 : 0);
         out.writeString(subscriberId);
+        out.writeInt(provisioningNotificationDisabled ? 1 : 0);
     }
 
     public static final Creator<NetworkMisc> CREATOR = new Creator<NetworkMisc>() {
@@ -89,6 +100,7 @@
             networkMisc.explicitlySelected = in.readInt() != 0;
             networkMisc.acceptUnvalidated = in.readInt() != 0;
             networkMisc.subscriberId = in.readString();
+            networkMisc.provisioningNotificationDisabled = in.readInt() != 0;
             return networkMisc;
         }
 
diff --git a/core/java/android/net/NetworkPolicyManager.java b/core/java/android/net/NetworkPolicyManager.java
index 51c45e0..11b861a 100644
--- a/core/java/android/net/NetworkPolicyManager.java
+++ b/core/java/android/net/NetworkPolicyManager.java
@@ -18,7 +18,6 @@
 
 import static android.content.pm.PackageManager.GET_SIGNATURES;
 import static android.net.NetworkPolicy.CYCLE_NONE;
-import static android.text.format.Time.MONTH_DAY;
 
 import android.content.Context;
 import android.content.Intent;
@@ -27,12 +26,13 @@
 import android.content.pm.Signature;
 import android.os.RemoteException;
 import android.os.UserHandle;
-import android.text.format.Time;
 import android.util.DebugUtils;
 
 import com.google.android.collect.Sets;
 
+import java.util.Calendar;
 import java.util.HashSet;
+import java.util.TimeZone;
 
 /**
  * Manager for creating and modifying network policy rules.
@@ -253,28 +253,18 @@
             throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
         }
 
-        final Time now = new Time(policy.cycleTimezone);
-        now.set(currentTime);
+        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(policy.cycleTimezone));
+        cal.setTimeInMillis(currentTime);
+        snapToCycleDay(cal, policy.cycleDay);
 
-        // first, find cycle boundary for current month
-        final Time cycle = new Time(now);
-        cycle.hour = cycle.minute = cycle.second = 0;
-        snapToCycleDay(cycle, policy.cycleDay);
-
-        if (Time.compare(cycle, now) >= 0) {
-            // cycle boundary is beyond now, use last cycle boundary; start by
-            // pushing ourselves squarely into last month.
-            final Time lastMonth = new Time(now);
-            lastMonth.hour = lastMonth.minute = lastMonth.second = 0;
-            lastMonth.monthDay = 1;
-            lastMonth.month -= 1;
-            lastMonth.normalize(true);
-
-            cycle.set(lastMonth);
-            snapToCycleDay(cycle, policy.cycleDay);
+        if (cal.getTimeInMillis() >= currentTime) {
+            // Cycle boundary is beyond now, use last cycle boundary
+            cal.set(Calendar.DAY_OF_MONTH, 1);
+            cal.add(Calendar.MONTH, -1);
+            snapToCycleDay(cal, policy.cycleDay);
         }
 
-        return cycle.toMillis(true);
+        return cal.getTimeInMillis();
     }
 
     /** {@hide} */
@@ -283,28 +273,18 @@
             throw new IllegalArgumentException("Unable to compute boundary without cycleDay");
         }
 
-        final Time now = new Time(policy.cycleTimezone);
-        now.set(currentTime);
+        final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(policy.cycleTimezone));
+        cal.setTimeInMillis(currentTime);
+        snapToCycleDay(cal, policy.cycleDay);
 
-        // first, find cycle boundary for current month
-        final Time cycle = new Time(now);
-        cycle.hour = cycle.minute = cycle.second = 0;
-        snapToCycleDay(cycle, policy.cycleDay);
-
-        if (Time.compare(cycle, now) <= 0) {
-            // cycle boundary is before now, use next cycle boundary; start by
-            // pushing ourselves squarely into next month.
-            final Time nextMonth = new Time(now);
-            nextMonth.hour = nextMonth.minute = nextMonth.second = 0;
-            nextMonth.monthDay = 1;
-            nextMonth.month += 1;
-            nextMonth.normalize(true);
-
-            cycle.set(nextMonth);
-            snapToCycleDay(cycle, policy.cycleDay);
+        if (cal.getTimeInMillis() <= currentTime) {
+            // Cycle boundary is before now, use next cycle boundary
+            cal.set(Calendar.DAY_OF_MONTH, 1);
+            cal.add(Calendar.MONTH, 1);
+            snapToCycleDay(cal, policy.cycleDay);
         }
 
-        return cycle.toMillis(true);
+        return cal.getTimeInMillis();
     }
 
     /**
@@ -313,16 +293,17 @@
      *
      * @hide
      */
-    public static void snapToCycleDay(Time time, int cycleDay) {
-        if (cycleDay > time.getActualMaximum(MONTH_DAY)) {
-            // cycle day isn't valid this month; snap to last second of month
-            time.month += 1;
-            time.monthDay = 1;
-            time.second = -1;
+    public static void snapToCycleDay(Calendar cal, int cycleDay) {
+        cal.set(Calendar.HOUR_OF_DAY, 0);
+        cal.set(Calendar.MINUTE, 0);
+        cal.set(Calendar.SECOND, 0);
+        if (cycleDay > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
+            cal.set(Calendar.DAY_OF_MONTH, 1);
+            cal.add(Calendar.MONTH, 1);
+            cal.add(Calendar.SECOND, -1);
         } else {
-            time.monthDay = cycleDay;
+            cal.set(Calendar.DAY_OF_MONTH, cycleDay);
         }
-        time.normalize(true);
     }
 
     /**
diff --git a/core/java/android/net/NetworkRequest.java b/core/java/android/net/NetworkRequest.java
index f1edcbe..ae72470 100644
--- a/core/java/android/net/NetworkRequest.java
+++ b/core/java/android/net/NetworkRequest.java
@@ -19,6 +19,8 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 
+import java.util.Objects;
+
 /**
  * Defines a request for a network, made through {@link NetworkRequest.Builder} and used
  * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes
@@ -47,15 +49,69 @@
     public final int legacyType;
 
     /**
+     * A NetworkRequest as used by the system can be one of the following types:
+     *
+     *     - LISTEN, for which the framework will issue callbacks about any
+     *       and all networks that match the specified NetworkCapabilities,
+     *
+     *     - REQUEST, capable of causing a specific network to be created
+     *       first (e.g. a telephony DUN request), the framework will issue
+     *       callbacks about the single, highest scoring current network
+     *       (if any) that matches the specified NetworkCapabilities, or
+     *
+     *     - TRACK_DEFAULT, a hybrid of the two designed such that the
+     *       framework will issue callbacks for the single, highest scoring
+     *       current network (if any) that matches the capabilities of the
+     *       default Internet request (mDefaultRequest), but which cannot cause
+     *       the framework to either create or retain the existence of any
+     *       specific network. Note that from the point of view of the request
+     *       matching code, TRACK_DEFAULT is identical to REQUEST: its special
+     *       behaviour is not due to different semantics, but to the fact that
+     *       the system will only ever create a TRACK_DEFAULT with capabilities
+     *       that are identical to the default request's capabilities, thus
+     *       causing it to share fate in every way with the default request.
+     *
+     *     - BACKGROUND_REQUEST, like REQUEST but does not cause any networks
+     *       to retain the NET_CAPABILITY_FOREGROUND capability. A network with
+     *       no foreground requests is in the background. A network that has
+     *       one or more background requests and loses its last foreground
+     *       request to a higher-scoring network will not go into the
+     *       background immediately, but will linger and go into the background
+     *       after the linger timeout.
+     *
+     *     - The value NONE is used only by applications. When an application
+     *       creates a NetworkRequest, it does not have a type; the type is set
+     *       by the system depending on the method used to file the request
+     *       (requestNetwork, registerNetworkCallback, etc.).
+     *
      * @hide
      */
-    public NetworkRequest(NetworkCapabilities nc, int legacyType, int rId) {
+    public static enum Type {
+        NONE,
+        LISTEN,
+        TRACK_DEFAULT,
+        REQUEST,
+        BACKGROUND_REQUEST,
+    };
+
+    /**
+     * The type of the request. This is only used by the system and is always NONE elsewhere.
+     *
+     * @hide
+     */
+    public final Type type;
+
+    /**
+     * @hide
+     */
+    public NetworkRequest(NetworkCapabilities nc, int legacyType, int rId, Type type) {
         if (nc == null) {
             throw new NullPointerException();
         }
         requestId = rId;
         networkCapabilities = nc;
         this.legacyType = legacyType;
+        this.type = type;
     }
 
     /**
@@ -65,6 +121,7 @@
         networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
         requestId = that.requestId;
         this.legacyType = that.legacyType;
+        this.type = that.type;
     }
 
     /**
@@ -90,14 +147,14 @@
             final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities);
             nc.maybeMarkCapabilitiesRestricted();
             return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE,
-                    ConnectivityManager.REQUEST_ID_UNSET);
+                    ConnectivityManager.REQUEST_ID_UNSET, Type.NONE);
         }
 
         /**
          * Add the given capability requirement to this builder.  These represent
          * the requested network's required capabilities.  Note that when searching
          * for a network to satisfy a request, all capabilities requested must be
-         * satisfied.  See {@link NetworkCapabilities} for {@code NET_CAPABILITIY_*}
+         * satisfied.  See {@link NetworkCapabilities} for {@code NET_CAPABILITY_*}
          * definitions.
          *
          * @param capability The {@code NetworkCapabilities.NET_CAPABILITY_*} to add.
@@ -223,6 +280,7 @@
         dest.writeParcelable(networkCapabilities, flags);
         dest.writeInt(legacyType);
         dest.writeInt(requestId);
+        dest.writeString(type.name());
     }
     public static final Creator<NetworkRequest> CREATOR =
         new Creator<NetworkRequest>() {
@@ -230,7 +288,8 @@
                 NetworkCapabilities nc = (NetworkCapabilities)in.readParcelable(null);
                 int legacyType = in.readInt();
                 int requestId = in.readInt();
-                NetworkRequest result = new NetworkRequest(nc, legacyType, requestId);
+                Type type = Type.valueOf(in.readString());  // IllegalArgumentException if invalid.
+                NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type);
                 return result;
             }
             public NetworkRequest[] newArray(int size) {
@@ -238,8 +297,64 @@
             }
         };
 
+    /**
+     * Returns true iff. this NetworkRequest is of type LISTEN.
+     *
+     * @hide
+     */
+    public boolean isListen() {
+        return type == Type.LISTEN;
+    }
+
+    /**
+     * Returns true iff. the contained NetworkRequest is one that:
+     *
+     *     - should be associated with at most one satisfying network
+     *       at a time;
+     *
+     *     - should cause a network to be kept up, but not necessarily in
+     *       the foreground, if it is the best network which can satisfy the
+     *       NetworkRequest.
+     *
+     * For full detail of how isRequest() is used for pairing Networks with
+     * NetworkRequests read rematchNetworkAndRequests().
+     *
+     * @hide
+     */
+    public boolean isRequest() {
+        return isForegroundRequest() || isBackgroundRequest();
+    }
+
+    /**
+     * Returns true iff. the contained NetworkRequest is one that:
+     *
+     *     - should be associated with at most one satisfying network
+     *       at a time;
+     *
+     *     - should cause a network to be kept up and in the foreground if
+     *       it is the best network which can satisfy the NetworkRequest.
+     *
+     * For full detail of how isRequest() is used for pairing Networks with
+     * NetworkRequests read rematchNetworkAndRequests().
+     *
+     * @hide
+     */
+    public boolean isForegroundRequest() {
+        return type == Type.TRACK_DEFAULT || type == Type.REQUEST;
+    }
+
+    /**
+     * Returns true iff. this NetworkRequest is of type BACKGROUND_REQUEST.
+     *
+     * @hide
+     */
+    public boolean isBackgroundRequest() {
+        return type == Type.BACKGROUND_REQUEST;
+    }
+
     public String toString() {
-        return "NetworkRequest [ id=" + requestId + ", legacyType=" + legacyType +
+        return "NetworkRequest [ " + type + " id=" + requestId +
+                (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") +
                 ", " + networkCapabilities.toString() + " ]";
     }
 
@@ -248,13 +363,11 @@
         NetworkRequest that = (NetworkRequest)obj;
         return (that.legacyType == this.legacyType &&
                 that.requestId == this.requestId &&
-                ((that.networkCapabilities == null && this.networkCapabilities == null) ||
-                 (that.networkCapabilities != null &&
-                  that.networkCapabilities.equals(this.networkCapabilities))));
+                that.type == this.type &&
+                Objects.equals(that.networkCapabilities, this.networkCapabilities));
     }
 
     public int hashCode() {
-        return requestId + (legacyType * 1013) +
-                (networkCapabilities.hashCode() * 1051);
+        return Objects.hash(requestId, legacyType, networkCapabilities, type);
     }
 }
diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java
index 141af3d..35e3065 100644
--- a/core/java/android/net/NetworkUtils.java
+++ b/core/java/android/net/NetworkUtils.java
@@ -45,13 +45,20 @@
     public native static void attachDhcpFilter(FileDescriptor fd) throws SocketException;
 
     /**
-     * Attaches a socket filter that accepts ICMP6 router advertisement packets to the given socket.
+     * Attaches a socket filter that accepts ICMPv6 router advertisements to the given socket.
      * @param fd the socket's {@link FileDescriptor}.
      * @param packetType the hardware address type, one of ARPHRD_*.
      */
     public native static void attachRaFilter(FileDescriptor fd, int packetType) throws SocketException;
 
     /**
+     * Configures a socket for receiving ICMPv6 router solicitations and sending advertisements.
+     * @param fd the socket's {@link FileDescriptor}.
+     * @param ifIndex the interface index.
+     */
+    public native static void setupRaSocket(FileDescriptor fd, int ifIndex) throws SocketException;
+
+    /**
      * Binds the current process to the network designated by {@code netId}.  All sockets created
      * in the future (and not explicitly bound via a bound {@link SocketFactory} (see
      * {@link Network#getSocketFactory}) will be bound to this network.  Note that if this
diff --git a/core/java/android/net/metrics/ApfProgramEvent.java b/core/java/android/net/metrics/ApfProgramEvent.java
new file mode 100644
index 0000000..258d8e1
--- /dev/null
+++ b/core/java/android/net/metrics/ApfProgramEvent.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.metrics;
+
+import android.annotation.IntDef;
+import android.annotation.SystemApi;
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.text.TextUtils;
+import android.util.SparseArray;
+
+import com.android.internal.util.MessageUtils;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.List;
+
+/**
+ * An event logged when there is a change or event that requires updating the
+ * the APF program in place with a new APF program.
+ * {@hide}
+ */
+@SystemApi
+public final class ApfProgramEvent implements Parcelable {
+
+    // Bitflag constants describing what an Apf program filters.
+    // Bits are indexeds from LSB to MSB, starting at index 0.
+    public static final int FLAG_MULTICAST_FILTER_ON = 0;
+    public static final int FLAG_HAS_IPV4_ADDRESS    = 1;
+
+    /** {@hide} */
+    @IntDef(flag = true, value = {FLAG_MULTICAST_FILTER_ON, FLAG_HAS_IPV4_ADDRESS})
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface Flags {}
+
+    public final long lifetime;     // Lifetime of the program in seconds
+    public final int filteredRas;   // Number of RAs filtered by the APF program
+    public final int currentRas;    // Total number of current RAs at generation time
+    public final int programLength; // Length of the APF program in bytes
+    public final int flags;         // Bitfield compound of FLAG_* constants
+
+    /** {@hide} */
+    public ApfProgramEvent(
+            long lifetime, int filteredRas, int currentRas, int programLength, @Flags int flags) {
+        this.lifetime = lifetime;
+        this.filteredRas = filteredRas;
+        this.currentRas = currentRas;
+        this.programLength = programLength;
+        this.flags = flags;
+    }
+
+    private ApfProgramEvent(Parcel in) {
+        this.lifetime = in.readLong();
+        this.filteredRas = in.readInt();
+        this.currentRas = in.readInt();
+        this.programLength = in.readInt();
+        this.flags = in.readInt();
+    }
+
+    @Override
+    public void writeToParcel(Parcel out, int flags) {
+        out.writeLong(lifetime);
+        out.writeInt(filteredRas);
+        out.writeInt(currentRas);
+        out.writeInt(programLength);
+        out.writeInt(flags);
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public String toString() {
+        String lifetimeString = (lifetime < Long.MAX_VALUE) ? lifetime + "s" : "forever";
+        return String.format("ApfProgramEvent(%d/%d RAs %dB %s %s)",
+                filteredRas, currentRas, programLength, lifetimeString, namesOf(flags));
+    }
+
+    public static final Parcelable.Creator<ApfProgramEvent> CREATOR
+            = new Parcelable.Creator<ApfProgramEvent>() {
+        public ApfProgramEvent createFromParcel(Parcel in) {
+            return new ApfProgramEvent(in);
+        }
+
+        public ApfProgramEvent[] newArray(int size) {
+            return new ApfProgramEvent[size];
+        }
+    };
+
+    /** {@hide} */
+    public static @Flags int flagsFor(boolean hasIPv4, boolean multicastFilterOn) {
+        int bitfield = 0;
+        if (hasIPv4) {
+            bitfield |= (1 << FLAG_HAS_IPV4_ADDRESS);
+        }
+        if (multicastFilterOn) {
+            bitfield |= (1 << FLAG_MULTICAST_FILTER_ON);
+        }
+        return bitfield;
+    }
+
+    private static String namesOf(@Flags int bitfield) {
+        List<String> names = new ArrayList<>(Integer.bitCount(bitfield));
+        BitSet set = BitSet.valueOf(new long[]{bitfield & Integer.MAX_VALUE});
+        // Only iterate over flag bits which are set.
+        for (int bit = set.nextSetBit(0); bit >= 0; bit = set.nextSetBit(bit+1)) {
+            names.add(Decoder.constants.get(bit));
+        }
+        return TextUtils.join("|", names);
+    }
+
+    final static class Decoder {
+        static final SparseArray<String> constants =
+                MessageUtils.findMessageNames(
+                       new Class[]{ApfProgramEvent.class}, new String[]{"FLAG_"});
+    }
+}
diff --git a/core/java/android/net/metrics/ApfStats.java b/core/java/android/net/metrics/ApfStats.java
new file mode 100644
index 0000000..8451e53
--- /dev/null
+++ b/core/java/android/net/metrics/ApfStats.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.metrics;
+
+import android.annotation.SystemApi;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+/**
+ * An event logged for an interface with APF capabilities when its IpManager state machine exits.
+ * {@hide}
+ */
+@SystemApi
+public final class ApfStats implements Parcelable {
+
+    public final long durationMs;     // time interval in milliseconds these stastistics covers
+    public final int receivedRas;     // number of received RAs
+    public final int matchingRas;     // number of received RAs matching a known RA
+    public final int droppedRas;      // number of received RAs ignored due to the MAX_RAS limit
+    public final int zeroLifetimeRas; // number of received RAs with a minimum lifetime of 0
+    public final int parseErrors;     // number of received RAs that could not be parsed
+    public final int programUpdates;  // number of APF program updates
+    public final int maxProgramSize;  // maximum APF program size advertised by hardware
+
+    /** {@hide} */
+    public ApfStats(long durationMs, int receivedRas, int matchingRas, int droppedRas,
+            int zeroLifetimeRas, int parseErrors, int programUpdates, int maxProgramSize) {
+        this.durationMs = durationMs;
+        this.receivedRas = receivedRas;
+        this.matchingRas = matchingRas;
+        this.droppedRas = droppedRas;
+        this.zeroLifetimeRas = zeroLifetimeRas;
+        this.parseErrors = parseErrors;
+        this.programUpdates = programUpdates;
+        this.maxProgramSize = maxProgramSize;
+    }
+
+    private ApfStats(Parcel in) {
+        this.durationMs = in.readLong();
+        this.receivedRas = in.readInt();
+        this.matchingRas = in.readInt();
+        this.droppedRas = in.readInt();
+        this.zeroLifetimeRas = in.readInt();
+        this.parseErrors = in.readInt();
+        this.programUpdates = in.readInt();
+        this.maxProgramSize = in.readInt();
+    }
+
+    @Override
+    public void writeToParcel(Parcel out, int flags) {
+        out.writeLong(durationMs);
+        out.writeInt(receivedRas);
+        out.writeInt(matchingRas);
+        out.writeInt(droppedRas);
+        out.writeInt(zeroLifetimeRas);
+        out.writeInt(parseErrors);
+        out.writeInt(programUpdates);
+        out.writeInt(maxProgramSize);
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public String toString() {
+        return new StringBuilder("ApfStats(")
+                .append(String.format("%dms ", durationMs))
+                .append(String.format("%dB RA: {", maxProgramSize))
+                .append(String.format("%d received, ", receivedRas))
+                .append(String.format("%d matching, ", matchingRas))
+                .append(String.format("%d dropped, ", droppedRas))
+                .append(String.format("%d zero lifetime, ", zeroLifetimeRas))
+                .append(String.format("%d parse errors, ", parseErrors))
+                .append(String.format("%d program updates})", programUpdates))
+                .toString();
+    }
+
+    public static final Parcelable.Creator<ApfStats> CREATOR = new Parcelable.Creator<ApfStats>() {
+        public ApfStats createFromParcel(Parcel in) {
+            return new ApfStats(in);
+        }
+
+        public ApfStats[] newArray(int size) {
+            return new ApfStats[size];
+        }
+    };
+}
diff --git a/core/java/android/net/metrics/DefaultNetworkEvent.java b/core/java/android/net/metrics/DefaultNetworkEvent.java
index f8b5992..9f0bad7 100644
--- a/core/java/android/net/metrics/DefaultNetworkEvent.java
+++ b/core/java/android/net/metrics/DefaultNetworkEvent.java
@@ -22,10 +22,11 @@
 import android.os.Parcelable;
 
 /**
+ * An event recorded by ConnectivityService when there is a change in the default network.
  * {@hide}
  */
 @SystemApi
-public final class DefaultNetworkEvent extends IpConnectivityEvent implements Parcelable {
+public final class DefaultNetworkEvent implements Parcelable {
     // The ID of the network that has become the new default or NETID_UNSET if none.
     public final int netId;
     // The list of transport types of the new default network, for example TRANSPORT_WIFI, as
@@ -37,7 +38,8 @@
     public final boolean prevIPv4;
     public final boolean prevIPv6;
 
-    private DefaultNetworkEvent(int netId, int[] transportTypes,
+    /** {@hide} */
+    public DefaultNetworkEvent(int netId, int[] transportTypes,
                 int prevNetId, boolean prevIPv4, boolean prevIPv6) {
         this.netId = netId;
         this.transportTypes = transportTypes;
@@ -54,6 +56,7 @@
         this.prevIPv6 = (in.readByte() > 0);
     }
 
+    @Override
     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(netId);
         out.writeIntArray(transportTypes);
@@ -62,6 +65,7 @@
         out.writeByte(prevIPv6 ? (byte) 1 : (byte) 0);
     }
 
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -105,6 +109,5 @@
 
     public static void logEvent(
             int netId, int[] transports, int prevNetId, boolean hadIPv4, boolean hadIPv6) {
-        logEvent(new DefaultNetworkEvent(netId, transports, prevNetId, hadIPv4, hadIPv6));
     }
-};
+}
diff --git a/core/java/android/net/metrics/DhcpClientEvent.java b/core/java/android/net/metrics/DhcpClientEvent.java
index ec560bf..4a9ff05 100644
--- a/core/java/android/net/metrics/DhcpClientEvent.java
+++ b/core/java/android/net/metrics/DhcpClientEvent.java
@@ -21,35 +21,50 @@
 import android.os.Parcelable;
 
 /**
+ * An event recorded when a DhcpClient state machine transitions to a new state.
  * {@hide}
  */
 @SystemApi
-public final class DhcpClientEvent extends IpConnectivityEvent implements Parcelable {
+public final class DhcpClientEvent implements Parcelable {
+
+    // Names for recording DhcpClient pseudo-state transitions.
+    /** {@hide} Represents transitions from DhcpInitState to DhcpBoundState */
+    public static final String INITIAL_BOUND = "InitialBoundState";
+    /** {@hide} Represents transitions from and to DhcpBoundState via DhcpRenewingState */
+    public static final String RENEWING_BOUND = "RenewingBoundState";
+
     public final String ifName;
     public final String msg;
+    public final int durationMs;
 
-    private DhcpClientEvent(String ifName, String msg) {
+    /** {@hide} */
+    public DhcpClientEvent(String ifName, String msg, int durationMs) {
         this.ifName = ifName;
         this.msg = msg;
+        this.durationMs = durationMs;
     }
 
     private DhcpClientEvent(Parcel in) {
         this.ifName = in.readString();
         this.msg = in.readString();
+        this.durationMs = in.readInt();
     }
 
+    @Override
     public void writeToParcel(Parcel out, int flags) {
         out.writeString(ifName);
         out.writeString(msg);
+        out.writeInt(durationMs);
     }
 
+    @Override
     public int describeContents() {
         return 0;
     }
 
     @Override
     public String toString() {
-        return String.format("DhcpClientEvent(%s, %s)", ifName, msg);
+        return String.format("DhcpClientEvent(%s, %s, %dms)", ifName, msg, durationMs);
     }
 
     public static final Parcelable.Creator<DhcpClientEvent> CREATOR
@@ -64,6 +79,5 @@
     };
 
     public static void logStateEvent(String ifName, String state) {
-        logEvent(new DhcpClientEvent(ifName, state));
     }
-};
+}
diff --git a/core/java/android/net/metrics/DhcpErrorEvent.java b/core/java/android/net/metrics/DhcpErrorEvent.java
index 84795b8..c3abcf7 100644
--- a/core/java/android/net/metrics/DhcpErrorEvent.java
+++ b/core/java/android/net/metrics/DhcpErrorEvent.java
@@ -24,10 +24,11 @@
 import com.android.internal.util.MessageUtils;
 
 /**
- * {@hide} Event class used to record error events when parsing DHCP response packets.
+ * Event class used to record error events when parsing DHCP response packets.
+ * {@hide}
  */
 @SystemApi
-public final class DhcpErrorEvent extends IpConnectivityEvent implements Parcelable {
+public final class DhcpErrorEvent implements Parcelable {
     public static final int L2_ERROR   = 1;
     public static final int L3_ERROR   = 2;
     public static final int L4_ERROR   = 3;
@@ -49,9 +50,13 @@
     public static final int DHCP_INVALID_OPTION_LENGTH = makeErrorCode(DHCP_ERROR, 3);
     public static final int DHCP_NO_MSG_TYPE           = makeErrorCode(DHCP_ERROR, 4);
     public static final int DHCP_UNKNOWN_MSG_TYPE      = makeErrorCode(DHCP_ERROR, 5);
+    /** {@hide} */
+    public static final int DHCP_NO_COOKIE             = makeErrorCode(DHCP_ERROR, 6);
 
     public static final int BUFFER_UNDERFLOW           = makeErrorCode(MISC_ERROR, 1);
     public static final int RECEIVE_ERROR              = makeErrorCode(MISC_ERROR, 2);
+    /** {@hide} */
+    public static final int PARSING_ERROR              = makeErrorCode(MISC_ERROR, 3);
 
     public final String ifName;
     // error code byte format (MSB to LSB):
@@ -61,7 +66,8 @@
     // byte 3: optional code
     public final int errorCode;
 
-    private DhcpErrorEvent(String ifName, int errorCode) {
+    /** {@hide} */
+    public DhcpErrorEvent(String ifName, int errorCode) {
         this.ifName = ifName;
         this.errorCode = errorCode;
     }
@@ -71,11 +77,13 @@
         this.errorCode = in.readInt();
     }
 
+    @Override
     public void writeToParcel(Parcel out, int flags) {
         out.writeString(ifName);
         out.writeInt(errorCode);
     }
 
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -92,11 +100,9 @@
     };
 
     public static void logParseError(String ifName, int errorCode) {
-        logEvent(new DhcpErrorEvent(ifName, errorCode));
     }
 
     public static void logReceiveError(String ifName) {
-        logEvent(new DhcpErrorEvent(ifName, RECEIVE_ERROR));
     }
 
     public static int errorCodeWithOption(int errorCode, int option) {
@@ -113,8 +119,9 @@
     }
 
     final static class Decoder {
-        static final SparseArray<String> constants =
-                MessageUtils.findMessageNames(new Class[]{DhcpErrorEvent.class},
-                new String[]{"L2_", "L3_", "L4_", "BOOTP_", "DHCP_", "BUFFER_", "RECEIVE_"});
+        static final SparseArray<String> constants = MessageUtils.findMessageNames(
+                new Class[]{DhcpErrorEvent.class},
+                new String[]{"L2_", "L3_", "L4_", "BOOTP_", "DHCP_", "BUFFER_", "RECEIVE_",
+                "PARSING_"});
     }
 }
diff --git a/core/java/android/net/metrics/DnsEvent.java b/core/java/android/net/metrics/DnsEvent.java
index b94dda0..6176b2c 100644
--- a/core/java/android/net/metrics/DnsEvent.java
+++ b/core/java/android/net/metrics/DnsEvent.java
@@ -21,10 +21,11 @@
 import android.os.Parcelable;
 
 /**
+ * A DNS event recorded by NetdEventListenerService.
  * {@hide}
  */
 @SystemApi
-final public class DnsEvent extends IpConnectivityEvent implements Parcelable {
+final public class DnsEvent implements Parcelable {
     public final int netId;
 
     // The event type is currently only 1 or 2, so we store it as a byte.
@@ -37,7 +38,8 @@
     // queries.
     public final int[] latenciesMs;
 
-    private DnsEvent(int netId, byte[] eventTypes, byte[] returnCodes, int[] latenciesMs) {
+    /** {@hide} */
+    public DnsEvent(int netId, byte[] eventTypes, byte[] returnCodes, int[] latenciesMs) {
         this.netId = netId;
         this.eventTypes = eventTypes;
         this.returnCodes = returnCodes;
@@ -59,6 +61,7 @@
         out.writeIntArray(latenciesMs);
     }
 
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -82,6 +85,5 @@
 
     public static void logEvent(
             int netId, byte[] eventTypes, byte[] returnCodes, int[] latenciesMs) {
-        logEvent(new DnsEvent(netId, eventTypes, returnCodes, latenciesMs));
     }
 }
diff --git a/core/java/android/net/metrics/IpConnectivityEvent.java b/core/java/android/net/metrics/IpConnectivityEvent.java
deleted file mode 100644
index 95576c2..0000000
--- a/core/java/android/net/metrics/IpConnectivityEvent.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package android.net.metrics;
-
-import android.net.ConnectivityMetricsLogger;
-import android.os.Parcel;
-import android.os.Parcelable;
-
-/**
- * {@hide}
- */
-public abstract class IpConnectivityEvent {
-    private static final int COMPONENT_TAG = ConnectivityMetricsLogger.COMPONENT_TAG_CONNECTIVITY;
-
-    private static final ConnectivityMetricsLogger sMetricsLogger = new ConnectivityMetricsLogger();
-
-    public static <T extends IpConnectivityEvent & Parcelable> void logEvent(T event) {
-        // TODO: consider using different component for DNS event.
-        sMetricsLogger.logEvent(System.currentTimeMillis(), COMPONENT_TAG, 0, event);
-    }
-};
diff --git a/core/java/android/net/metrics/IpConnectivityLog.java b/core/java/android/net/metrics/IpConnectivityLog.java
new file mode 100644
index 0000000..173e5fd
--- /dev/null
+++ b/core/java/android/net/metrics/IpConnectivityLog.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.metrics;
+
+import android.net.ConnectivityMetricsEvent;
+import android.net.IIpConnectivityMetrics;
+import android.os.Parcelable;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.util.Log;
+import com.android.internal.annotations.VisibleForTesting;
+
+/**
+ * Class for logging IpConnectvity events with IpConnectivityMetrics
+ * {@hide}
+ */
+public class IpConnectivityLog {
+    private static final String TAG = IpConnectivityLog.class.getSimpleName();
+    private static final boolean DBG = false;
+
+    public static final String SERVICE_NAME = "connmetrics";
+
+    private IIpConnectivityMetrics mService;
+
+    public IpConnectivityLog() {
+    }
+
+    @VisibleForTesting
+    public IpConnectivityLog(IIpConnectivityMetrics service) {
+        mService = service;
+    }
+
+    private boolean checkLoggerService() {
+        if (mService != null) {
+            return true;
+        }
+        final IIpConnectivityMetrics service =
+                IIpConnectivityMetrics.Stub.asInterface(ServiceManager.getService(SERVICE_NAME));
+        if (service == null) {
+            return false;
+        }
+        // Two threads racing here will write the same pointer because getService
+        // is idempotent once MetricsLoggerService is initialized.
+        mService = service;
+        return true;
+    }
+
+    /**
+     * Log an IpConnectivity event.
+     * @param timestamp is the epoch timestamp of the event in ms.
+     * @param data is a Parcelable instance representing the event.
+     * @return true if the event was successfully logged.
+     */
+    public boolean log(long timestamp, Parcelable data) {
+        if (!checkLoggerService()) {
+            if (DBG) {
+                Log.d(TAG, SERVICE_NAME + " service was not ready");
+            }
+            return false;
+        }
+
+        try {
+            int left = mService.logEvent(new ConnectivityMetricsEvent(timestamp, 0, 0, data));
+            return left >= 0;
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error logging event", e);
+            return false;
+        }
+    }
+
+    public void log(Parcelable event) {
+        log(System.currentTimeMillis(), event);
+    }
+}
diff --git a/core/java/android/net/metrics/IpManagerEvent.java b/core/java/android/net/metrics/IpManagerEvent.java
index 0940bd0..a5b4eb5 100644
--- a/core/java/android/net/metrics/IpManagerEvent.java
+++ b/core/java/android/net/metrics/IpManagerEvent.java
@@ -16,6 +16,7 @@
 
 package android.net.metrics;
 
+import android.annotation.IntDef;
 import android.annotation.SystemApi;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -23,21 +24,32 @@
 
 import com.android.internal.util.MessageUtils;
 
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
 /**
+ * An event recorded by IpManager when IP provisioning completes for a network or
+ * when a network disconnects.
  * {@hide}
  */
 @SystemApi
-public final class IpManagerEvent extends IpConnectivityEvent implements Parcelable {
+public final class IpManagerEvent implements Parcelable {
 
     public static final int PROVISIONING_OK    = 1;
     public static final int PROVISIONING_FAIL  = 2;
     public static final int COMPLETE_LIFECYCLE = 3;
 
+    /** {@hide} */
+    @IntDef(value = {PROVISIONING_OK, PROVISIONING_FAIL, COMPLETE_LIFECYCLE})
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface EventType {}
+
     public final String ifName;
-    public final int eventType;
+    public final @EventType int eventType;
     public final long durationMs;
 
-    private IpManagerEvent(String ifName, int eventType, long duration) {
+    /** {@hide} */
+    public IpManagerEvent(String ifName, @EventType int eventType, long duration) {
         this.ifName = ifName;
         this.eventType = eventType;
         this.durationMs = duration;
@@ -49,12 +61,14 @@
         this.durationMs = in.readLong();
     }
 
+    @Override
     public void writeToParcel(Parcel out, int flags) {
         out.writeString(ifName);
         out.writeInt(eventType);
         out.writeLong(durationMs);
     }
 
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -71,7 +85,6 @@
     };
 
     public static void logEvent(int eventType, String ifName, long durationMs) {
-        logEvent(new IpManagerEvent(ifName, eventType, durationMs));
     }
 
     @Override
@@ -84,4 +97,4 @@
         static final SparseArray<String> constants = MessageUtils.findMessageNames(
                 new Class[]{IpManagerEvent.class}, new String[]{"PROVISIONING_", "COMPLETE_"});
     }
-};
+}
diff --git a/core/java/android/net/metrics/IpReachabilityEvent.java b/core/java/android/net/metrics/IpReachabilityEvent.java
index d40389c..ee09e22 100644
--- a/core/java/android/net/metrics/IpReachabilityEvent.java
+++ b/core/java/android/net/metrics/IpReachabilityEvent.java
@@ -24,24 +24,35 @@
 import com.android.internal.util.MessageUtils;
 
 /**
+ * An event recorded when IpReachabilityMonitor sends a neighbor probe or receives
+ * a neighbor probe result.
  * {@hide}
  */
 @SystemApi
-public final class IpReachabilityEvent extends IpConnectivityEvent implements Parcelable {
+public final class IpReachabilityEvent implements Parcelable {
 
-    public static final int PROBE             = 1 << 8;
-    public static final int NUD_FAILED        = 2 << 8;
-    public static final int PROVISIONING_LOST = 3 << 8;
+    // Event types.
+    /** A probe forced by IpReachabilityMonitor. */
+    public static final int PROBE                     = 1 << 8;
+    /** Neighbor unreachable after a forced probe. */
+    public static final int NUD_FAILED                = 2 << 8;
+    /** Neighbor unreachable after a forced probe, IP provisioning is also lost. */
+    public static final int PROVISIONING_LOST         = 3 << 8;
+    /** {@hide} Neighbor unreachable notification from kernel. */
+    public static final int NUD_FAILED_ORGANIC        = 4 << 8;
+    /** {@hide} Neighbor unreachable notification from kernel, IP provisioning is also lost. */
+    public static final int PROVISIONING_LOST_ORGANIC = 5 << 8;
 
     public final String ifName;
     // eventType byte format (MSB to LSB):
     // byte 0: unused
     // byte 1: unused
     // byte 2: type of event: PROBE, NUD_FAILED, PROVISIONING_LOST
-    // byte 3: kernel errno from RTNetlink or IpReachabilityMonitor
+    // byte 3: when byte 2 == PROBE, errno code from RTNetlink or IpReachabilityMonitor.
     public final int eventType;
 
-    private IpReachabilityEvent(String ifName, int eventType) {
+    /** {@hide} */
+    public IpReachabilityEvent(String ifName, int eventType) {
         this.ifName = ifName;
         this.eventType = eventType;
     }
@@ -51,11 +62,13 @@
         this.eventType = in.readInt();
     }
 
+    @Override
     public void writeToParcel(Parcel out, int flags) {
         out.writeString(ifName);
         out.writeInt(eventType);
     }
 
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -72,21 +85,32 @@
     };
 
     public static void logProbeEvent(String ifName, int nlErrorCode) {
-        logEvent(new IpReachabilityEvent(ifName, PROBE | (nlErrorCode & 0xFF)));
     }
 
     public static void logNudFailed(String ifName) {
-        logEvent(new IpReachabilityEvent(ifName, NUD_FAILED));
     }
 
     public static void logProvisioningLost(String ifName) {
-        logEvent(new IpReachabilityEvent(ifName, PROVISIONING_LOST));
+    }
+
+    /**
+     * Returns the NUD failure event type code corresponding to the given conditions.
+     * {@hide}
+     */
+    public static int nudFailureEventType(boolean isFromProbe, boolean isProvisioningLost) {
+        if (isFromProbe) {
+            return isProvisioningLost ? PROVISIONING_LOST : NUD_FAILED;
+        } else {
+            return isProvisioningLost ? PROVISIONING_LOST_ORGANIC : NUD_FAILED_ORGANIC;
+        }
     }
 
     @Override
     public String toString() {
-        return String.format("IpReachabilityEvent(%s, %s)", ifName,
-                Decoder.constants.get(eventType));
+        int hi = eventType & 0xff00;
+        int lo = eventType & 0x00ff;
+        String eventName = Decoder.constants.get(hi);
+        return String.format("IpReachabilityEvent(%s, %s:%02x)", ifName, eventName, lo);
     }
 
     final static class Decoder {
@@ -94,4 +118,4 @@
                 MessageUtils.findMessageNames(new Class[]{IpReachabilityEvent.class},
                 new String[]{"PROBE", "PROVISIONING_", "NUD_"});
     }
-};
+}
diff --git a/core/java/android/net/metrics/NetworkEvent.java b/core/java/android/net/metrics/NetworkEvent.java
index 08c9c75..3b3fa69 100644
--- a/core/java/android/net/metrics/NetworkEvent.java
+++ b/core/java/android/net/metrics/NetworkEvent.java
@@ -16,6 +16,7 @@
 
 package android.net.metrics;
 
+import android.annotation.IntDef;
 import android.annotation.SystemApi;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -23,11 +24,14 @@
 
 import com.android.internal.util.MessageUtils;
 
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
 /**
  * {@hide}
  */
 @SystemApi
-public final class NetworkEvent extends IpConnectivityEvent implements Parcelable {
+public final class NetworkEvent implements Parcelable {
 
     public static final int NETWORK_CONNECTED            = 1;
     public static final int NETWORK_VALIDATED            = 2;
@@ -37,28 +41,49 @@
     public static final int NETWORK_UNLINGER             = 6;
     public static final int NETWORK_DISCONNECTED         = 7;
 
+    /** {@hide} */
+    @IntDef(value = {
+            NETWORK_CONNECTED,
+            NETWORK_VALIDATED,
+            NETWORK_VALIDATION_FAILED,
+            NETWORK_CAPTIVE_PORTAL_FOUND,
+            NETWORK_LINGER,
+            NETWORK_UNLINGER,
+            NETWORK_DISCONNECTED,
+    })
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface EventType {}
+
     public final int netId;
-    public final int eventType;
+    public final @EventType int eventType;
     public final long durationMs;
 
-    private NetworkEvent(int netId, int eventType, long durationMs) {
+    /** {@hide} */
+    public NetworkEvent(int netId, @EventType int eventType, long durationMs) {
         this.netId = netId;
         this.eventType = eventType;
         this.durationMs = durationMs;
     }
 
+    /** {@hide} */
+    public NetworkEvent(int netId, @EventType int eventType) {
+        this(netId, eventType, 0);
+    }
+
     private NetworkEvent(Parcel in) {
         netId = in.readInt();
         eventType = in.readInt();
         durationMs = in.readLong();
     }
 
+    @Override
     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(netId);
         out.writeInt(eventType);
         out.writeLong(durationMs);
     }
 
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -75,15 +100,12 @@
     };
 
     public static void logEvent(int netId, int eventType) {
-        logEvent(new NetworkEvent(netId, eventType, 0));
     }
 
     public static void logValidated(int netId, long durationMs) {
-        logEvent(new NetworkEvent(netId, NETWORK_VALIDATED, durationMs));
     }
 
     public static void logCaptivePortalFound(int netId, long durationMs) {
-        logEvent(new NetworkEvent(netId, NETWORK_CAPTIVE_PORTAL_FOUND, durationMs));
     }
 
     @Override
@@ -96,4 +118,4 @@
         static final SparseArray<String> constants = MessageUtils.findMessageNames(
                 new Class[]{NetworkEvent.class}, new String[]{"NETWORK_"});
     }
-};
+}
diff --git a/core/java/android/net/metrics/RaEvent.java b/core/java/android/net/metrics/RaEvent.java
new file mode 100644
index 0000000..91bd023
--- /dev/null
+++ b/core/java/android/net/metrics/RaEvent.java
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.metrics;
+
+import android.annotation.SystemApi;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+/**
+ * An event logged when the APF packet socket receives an RA packet.
+ * {@hide}
+ */
+@SystemApi
+public final class RaEvent implements Parcelable {
+
+    /** {@hide} */
+    public static final long NO_LIFETIME = -1L;
+
+    // Lifetime in seconds of options found in a single RA packet.
+    // When an option is not set, the value of the associated field is -1;
+    public final long routerLifetime;
+    public final long prefixValidLifetime;
+    public final long prefixPreferredLifetime;
+    public final long routeInfoLifetime;
+    public final long rdnssLifetime;
+    public final long dnsslLifetime;
+
+    /** {@hide} */
+    public RaEvent(long routerLifetime, long prefixValidLifetime, long prefixPreferredLifetime,
+            long routeInfoLifetime, long rdnssLifetime, long dnsslLifetime) {
+        this.routerLifetime = routerLifetime;
+        this.prefixValidLifetime = prefixValidLifetime;
+        this.prefixPreferredLifetime = prefixPreferredLifetime;
+        this.routeInfoLifetime = routeInfoLifetime;
+        this.rdnssLifetime = rdnssLifetime;
+        this.dnsslLifetime = dnsslLifetime;
+    }
+
+    private RaEvent(Parcel in) {
+        routerLifetime          = in.readLong();
+        prefixValidLifetime     = in.readLong();
+        prefixPreferredLifetime = in.readLong();
+        routeInfoLifetime       = in.readLong();
+        rdnssLifetime           = in.readLong();
+        dnsslLifetime           = in.readLong();
+    }
+
+    @Override
+    public void writeToParcel(Parcel out, int flags) {
+        out.writeLong(routerLifetime);
+        out.writeLong(prefixValidLifetime);
+        out.writeLong(prefixPreferredLifetime);
+        out.writeLong(routeInfoLifetime);
+        out.writeLong(rdnssLifetime);
+        out.writeLong(dnsslLifetime);
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public String toString() {
+        return new StringBuilder("RaEvent(lifetimes: ")
+                .append(String.format("router=%ds, ", routerLifetime))
+                .append(String.format("prefix_valid=%ds, ", prefixValidLifetime))
+                .append(String.format("prefix_preferred=%ds, ", prefixPreferredLifetime))
+                .append(String.format("route_info=%ds, ", routeInfoLifetime))
+                .append(String.format("rdnss=%ds, ", rdnssLifetime))
+                .append(String.format("dnssl=%ds)", dnsslLifetime))
+                .toString();
+    }
+
+    public static final Parcelable.Creator<RaEvent> CREATOR = new Parcelable.Creator<RaEvent>() {
+        public RaEvent createFromParcel(Parcel in) {
+            return new RaEvent(in);
+        }
+
+        public RaEvent[] newArray(int size) {
+            return new RaEvent[size];
+        }
+    };
+
+    /** {@hide} */
+    public static class Builder {
+
+        long routerLifetime          = NO_LIFETIME;
+        long prefixValidLifetime     = NO_LIFETIME;
+        long prefixPreferredLifetime = NO_LIFETIME;
+        long routeInfoLifetime       = NO_LIFETIME;
+        long rdnssLifetime           = NO_LIFETIME;
+        long dnsslLifetime           = NO_LIFETIME;
+
+        public Builder() {
+        }
+
+        public RaEvent build() {
+            return new RaEvent(routerLifetime, prefixValidLifetime, prefixPreferredLifetime,
+                    routeInfoLifetime, rdnssLifetime, dnsslLifetime);
+        }
+
+        public Builder updateRouterLifetime(long lifetime) {
+            routerLifetime = updateLifetime(routerLifetime, lifetime);
+            return this;
+        }
+
+        public Builder updatePrefixValidLifetime(long lifetime) {
+            prefixValidLifetime = updateLifetime(prefixValidLifetime, lifetime);
+            return this;
+        }
+
+        public Builder updatePrefixPreferredLifetime(long lifetime) {
+            prefixPreferredLifetime = updateLifetime(prefixPreferredLifetime, lifetime);
+            return this;
+        }
+
+        public Builder updateRouteInfoLifetime(long lifetime) {
+            routeInfoLifetime = updateLifetime(routeInfoLifetime, lifetime);
+            return this;
+        }
+
+        public Builder updateRdnssLifetime(long lifetime) {
+            rdnssLifetime = updateLifetime(rdnssLifetime, lifetime);
+            return this;
+        }
+
+        public Builder updateDnsslLifetime(long lifetime) {
+            dnsslLifetime = updateLifetime(dnsslLifetime, lifetime);
+            return this;
+        }
+
+        private long updateLifetime(long currentLifetime, long newLifetime) {
+            if (currentLifetime == RaEvent.NO_LIFETIME) {
+                return newLifetime;
+            }
+            return Math.min(currentLifetime, newLifetime);
+        }
+    }
+}
diff --git a/core/java/android/net/metrics/ValidationProbeEvent.java b/core/java/android/net/metrics/ValidationProbeEvent.java
index 751c35f..1a31b56 100644
--- a/core/java/android/net/metrics/ValidationProbeEvent.java
+++ b/core/java/android/net/metrics/ValidationProbeEvent.java
@@ -16,6 +16,7 @@
 
 package android.net.metrics;
 
+import android.annotation.IntDef;
 import android.annotation.SystemApi;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -23,26 +24,44 @@
 
 import com.android.internal.util.MessageUtils;
 
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
 /**
+ * An event recorded by NetworkMonitor when sending a probe for finding captive portals.
  * {@hide}
  */
 @SystemApi
-public final class ValidationProbeEvent extends IpConnectivityEvent implements Parcelable {
+public final class ValidationProbeEvent implements Parcelable {
 
-    public static final int PROBE_DNS   = 0;
-    public static final int PROBE_HTTP  = 1;
-    public static final int PROBE_HTTPS = 2;
-    public static final int PROBE_PAC   = 3;
+    public static final int PROBE_DNS       = 0;
+    public static final int PROBE_HTTP      = 1;
+    public static final int PROBE_HTTPS     = 2;
+    public static final int PROBE_PAC       = 3;
+    /** {@hide} */
+    public static final int PROBE_FALLBACK  = 4;
 
     public static final int DNS_FAILURE = 0;
     public static final int DNS_SUCCESS = 1;
 
+    /** {@hide} */
+    @IntDef(value = {PROBE_DNS, PROBE_HTTP, PROBE_HTTPS, PROBE_PAC})
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface ProbeType {}
+
+    /** {@hide} */
+    @IntDef(value = {DNS_FAILURE, DNS_SUCCESS})
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface ReturnCode {}
+
     public final int netId;
     public final long durationMs;
-    public final int probeType;
-    public final int returnCode;
+    public final @ProbeType int probeType;
+    public final @ReturnCode int returnCode;
 
-    private ValidationProbeEvent(int netId, long durationMs, int probeType, int returnCode) {
+    /** {@hide} */
+    public ValidationProbeEvent(
+            int netId, long durationMs, @ProbeType int probeType, @ReturnCode int returnCode) {
         this.netId = netId;
         this.durationMs = durationMs;
         this.probeType = probeType;
@@ -56,6 +75,7 @@
         returnCode = in.readInt();
     }
 
+    @Override
     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(netId);
         out.writeLong(durationMs);
@@ -63,6 +83,7 @@
         out.writeInt(returnCode);
     }
 
+    @Override
     public int describeContents() {
         return 0;
     }
@@ -84,7 +105,6 @@
     }
 
     public static void logEvent(int netId, long durationMs, int probeType, int returnCode) {
-        logEvent(new ValidationProbeEvent(netId, durationMs, probeType, returnCode));
     }
 
     @Override
@@ -97,4 +117,4 @@
         static final SparseArray<String> constants = MessageUtils.findMessageNames(
                 new Class[]{ValidationProbeEvent.class}, new String[]{"PROBE_"});
     }
-};
+}
diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java
index 41ff9fd..783c25a 100644
--- a/core/java/android/nfc/NfcAdapter.java
+++ b/core/java/android/nfc/NfcAdapter.java
@@ -290,6 +290,7 @@
 
     // Guarded by NfcAdapter.class
     static boolean sIsInitialized = false;
+    static boolean sHasNfcFeature;
 
     // Final after first constructor, except for
     // attemptDeadServiceRecovery() when NFC crashes - we accept a best effort
@@ -435,42 +436,65 @@
     }
 
     /**
+     * Helper to check if this device is NFC HCE capable, by checking for
+     * FEATURE_NFC_HOST_CARD_EMULATION and/or FEATURE_NFC_HOST_CARD_EMULATION_NFCF,
+     * but without using a context.
+     */
+    private static boolean hasNfcHceFeature() {
+        IPackageManager pm = ActivityThread.getPackageManager();
+        if (pm == null) {
+            Log.e(TAG, "Cannot get package manager, assuming no NFC feature");
+            return false;
+        }
+        try {
+            return pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION, 0)
+                || pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION_NFCF, 0);
+        } catch (RemoteException e) {
+            Log.e(TAG, "Package manager query failed, assuming no NFC feature", e);
+            return false;
+        }
+    }
+
+    /**
      * Returns the NfcAdapter for application context,
      * or throws if NFC is not available.
      * @hide
      */
     public static synchronized NfcAdapter getNfcAdapter(Context context) {
         if (!sIsInitialized) {
+            sHasNfcFeature = hasNfcFeature();
+            boolean hasHceFeature = hasNfcHceFeature();
             /* is this device meant to have NFC */
-            if (!hasNfcFeature()) {
+            if (!sHasNfcFeature && !hasHceFeature) {
                 Log.v(TAG, "this device does not have NFC support");
                 throw new UnsupportedOperationException();
             }
-
             sService = getServiceInterface();
             if (sService == null) {
                 Log.e(TAG, "could not retrieve NFC service");
                 throw new UnsupportedOperationException();
             }
-            try {
-                sTagService = sService.getNfcTagInterface();
-            } catch (RemoteException e) {
-                Log.e(TAG, "could not retrieve NFC Tag service");
-                throw new UnsupportedOperationException();
+            if (sHasNfcFeature) {
+                try {
+                    sTagService = sService.getNfcTagInterface();
+                } catch (RemoteException e) {
+                    Log.e(TAG, "could not retrieve NFC Tag service");
+                    throw new UnsupportedOperationException();
+                }
             }
-
-            try {
-                sCardEmulationService = sService.getNfcCardEmulationInterface();
-            } catch (RemoteException e) {
-                Log.e(TAG, "could not retrieve card emulation service");
-                throw new UnsupportedOperationException();
-            }
-
-            try {
-                sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface();
-            } catch (RemoteException e) {
-                Log.e(TAG, "could not retrieve NFC-F card emulation service");
-                throw new UnsupportedOperationException();
+            if (hasHceFeature) {
+                try {
+                    sNfcFCardEmulationService = sService.getNfcFCardEmulationInterface();
+                } catch (RemoteException e) {
+                    Log.e(TAG, "could not retrieve NFC-F card emulation service");
+                    throw new UnsupportedOperationException();
+                }
+                try {
+                    sCardEmulationService = sService.getNfcCardEmulationInterface();
+                } catch (RemoteException e) {
+                    Log.e(TAG, "could not retrieve card emulation service");
+                    throw new UnsupportedOperationException();
+                }
             }
 
             sIsInitialized = true;
@@ -838,8 +862,14 @@
      *
      * @param uris an array of Uri(s) to push over Android Beam
      * @param activity activity for which the Uri(s) will be pushed
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void setBeamPushUris(Uri[] uris, Activity activity) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         if (activity == null) {
             throw new NullPointerException("activity cannot be null");
         }
@@ -914,8 +944,14 @@
      *
      * @param callback callback, or null to disable
      * @param activity activity for which the Uri(s) will be pushed
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void setBeamPushUrisCallback(CreateBeamUrisCallback callback, Activity activity) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         if (activity == null) {
             throw new NullPointerException("activity cannot be null");
         }
@@ -992,9 +1028,15 @@
      * @param activities optional additional activities, however we strongly recommend
      *        to only register one at a time, and to do so in that activity's
      *        {@link Activity#onCreate}
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void setNdefPushMessage(NdefMessage message, Activity activity,
             Activity ... activities) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         int targetSdkVersion = getSdkVersion();
         try {
             if (activity == null) {
@@ -1024,6 +1066,11 @@
      */
     @SystemApi
     public void setNdefPushMessage(NdefMessage message, Activity activity, int flags) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         if (activity == null) {
             throw new NullPointerException("activity cannot be null");
         }
@@ -1094,9 +1141,15 @@
      * @param activities optional additional activities, however we strongly recommend
      *        to only register one at a time, and to do so in that activity's
      *        {@link Activity#onCreate}
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void setNdefPushMessageCallback(CreateNdefMessageCallback callback, Activity activity,
             Activity ... activities) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         int targetSdkVersion = getSdkVersion();
         try {
             if (activity == null) {
@@ -1168,9 +1221,15 @@
      * @param activities optional additional activities, however we strongly recommend
      *        to only register one at a time, and to do so in that activity's
      *        {@link Activity#onCreate}
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void setOnNdefPushCompleteCallback(OnNdefPushCompleteCallback callback,
             Activity activity, Activity ... activities) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         int targetSdkVersion = getSdkVersion();
         try {
             if (activity == null) {
@@ -1227,9 +1286,15 @@
      * @param techLists the tech lists used to perform matching for dispatching of the
      *      {@link NfcAdapter#ACTION_TECH_DISCOVERED} intent
      * @throws IllegalStateException if the Activity is not currently in the foreground
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void enableForegroundDispatch(Activity activity, PendingIntent intent,
             IntentFilter[] filters, String[][] techLists) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         if (activity == null || intent == null) {
             throw new NullPointerException();
         }
@@ -1263,8 +1328,14 @@
      *
      * @param activity the Activity to disable dispatch to
      * @throws IllegalStateException if the Activity has already been paused
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void disableForegroundDispatch(Activity activity) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         ActivityThread.currentActivityThread().unregisterOnActivityPausedListener(activity,
                 mForegroundDispatchListener);
         disableForegroundDispatchInternal(activity, false);
@@ -1309,9 +1380,15 @@
      * @param callback the callback to be called when a tag is discovered
      * @param flags Flags indicating poll technologies and other optional parameters
      * @param extras Additional extras for configuring reader mode.
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void enableReaderMode(Activity activity, ReaderCallback callback, int flags,
             Bundle extras) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         mNfcActivityManager.enableReaderMode(activity, callback, flags, extras);
     }
 
@@ -1321,8 +1398,14 @@
      * all supported tag technologies.
      *
      * @param activity the Activity that currently has reader mode enabled
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public void disableReaderMode(Activity activity) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         mNfcActivityManager.disableReaderMode(activity);
     }
 
@@ -1349,8 +1432,14 @@
      *
      * @param activity the current foreground Activity that has registered data to share
      * @return whether the Beam animation was successfully invoked
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public boolean invokeBeam(Activity activity) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         if (activity == null) {
             throw new NullPointerException("activity may not be null.");
         }
@@ -1404,10 +1493,16 @@
      * @param activity foreground activity
      * @param message a NDEF Message to push over NFC
      * @throws IllegalStateException if the activity is not currently in the foreground
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      * @deprecated use {@link #setNdefPushMessage} instead
      */
     @Deprecated
     public void enableForegroundNdefPush(Activity activity, NdefMessage message) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         if (activity == null || message == null) {
             throw new NullPointerException();
         }
@@ -1432,10 +1527,16 @@
      *
      * @param activity the Foreground activity
      * @throws IllegalStateException if the Activity has already been paused
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      * @deprecated use {@link #setNdefPushMessage} instead
      */
     @Deprecated
     public void disableForegroundNdefPush(Activity activity) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         if (activity == null) {
             throw new NullPointerException();
         }
@@ -1452,6 +1553,9 @@
      */
     @SystemApi
     public boolean enableNdefPush() {
+        if (!sHasNfcFeature) {
+            throw new UnsupportedOperationException();
+        }
         try {
             return sService.enableNdefPush();
         } catch (RemoteException e) {
@@ -1467,6 +1571,11 @@
      */
     @SystemApi
     public boolean disableNdefPush() {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         try {
             return sService.disableNdefPush();
         } catch (RemoteException e) {
@@ -1497,8 +1606,14 @@
      *
      * @see android.provider.Settings#ACTION_NFCSHARING_SETTINGS
      * @return true if NDEF Push feature is enabled
+     * @throws UnsupportedOperationException if FEATURE_NFC is unavailable.
      */
     public boolean isNdefPushEnabled() {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         try {
             return sService.isNdefPushEnabled();
         } catch (RemoteException e) {
@@ -1623,6 +1738,11 @@
     @SystemApi
     public boolean addNfcUnlockHandler(final NfcUnlockHandler unlockHandler,
                                        String[] tagTechnologies) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         // If there are no tag technologies, don't bother adding unlock handler
         if (tagTechnologies.length == 0) {
             return false;
@@ -1666,6 +1786,11 @@
      */
     @SystemApi
     public boolean removeNfcUnlockHandler(NfcUnlockHandler unlockHandler) {
+        synchronized (NfcAdapter.class) {
+            if (!sHasNfcFeature) {
+                throw new UnsupportedOperationException();
+            }
+        }
         try {
             synchronized (mLock) {
                 if (mNfcUnlockHandlers.containsKey(unlockHandler)) {
diff --git a/core/java/android/os/AsyncTask.java b/core/java/android/os/AsyncTask.java
index 4f4e722..fea64ec 100644
--- a/core/java/android/os/AsyncTask.java
+++ b/core/java/android/os/AsyncTask.java
@@ -298,12 +298,19 @@
         mWorker = new WorkerRunnable<Params, Result>() {
             public Result call() throws Exception {
                 mTaskInvoked.set(true);
-
-                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
-                //noinspection unchecked
-                Result result = doInBackground(mParams);
-                Binder.flushPendingCommands();
-                return postResult(result);
+                Result result = null;
+                try {
+                    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
+                    //noinspection unchecked
+                    result = doInBackground(mParams);
+                    Binder.flushPendingCommands();
+                } catch (Throwable tr) {
+                    mCancelled.set(true);
+                    throw tr;
+                } finally {
+                    postResult(result);
+                }
+                return result;
             }
         };
 
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index 4acb729..a0c2efd 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -30,6 +30,8 @@
 import android.telephony.SignalStrength;
 import android.text.format.DateFormat;
 import android.util.ArrayMap;
+import android.util.Log;
+import android.util.LongSparseArray;
 import android.util.MutableBoolean;
 import android.util.Pair;
 import android.util.Printer;
@@ -47,6 +49,7 @@
  * @hide
  */
 public abstract class BatteryStats implements Parcelable {
+    private static final String TAG = "BatteryStats";
 
     private static final boolean LOCAL_LOGV = false;
 
@@ -175,8 +178,11 @@
 
     /**
      * Current version of checkin data format.
+     *
+     * New in version 19:
+     *   - Wakelock data (wl) gets current and max times.
      */
-    static final String CHECKIN_VERSION = "18";
+    static final String CHECKIN_VERSION = "19";
 
     /**
      * Old version, we hit 9 and ran out of room, need to remove.
@@ -352,6 +358,32 @@
         public abstract long getTimeSinceMarkLocked(long elapsedRealtimeUs);
 
         /**
+         * Returns the max duration if it is being tracked.
+         * Not all Timer subclasses track the max duration and the current duration.
+
+         */
+        public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
+            return -1;
+        }
+
+        /**
+         * Returns the current time the timer has been active, if it is being tracked.
+         * Not all Timer subclasses track the max duration and the current duration.
+         */
+        public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
+            return -1;
+        }
+
+        /**
+         * Returns whether the timer is currently running.  Some types of timers
+         * (e.g. BatchTimers) don't know whether the event is currently active,
+         * and report false.
+         */
+        public boolean isRunningLocked() {
+            return false;
+        }
+
+        /**
          * Temporary for debugging.
          */
         public abstract void logState(Printer pw, String prefix);
@@ -546,6 +578,20 @@
          */
         public abstract long getTimeAtCpuSpeed(int cluster, int step, int which);
 
+        /**
+         * Returns the number of times this UID woke up the Application Processor to
+         * process a mobile radio packet.
+         * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
+         */
+        public abstract long getMobileRadioApWakeupCount(int which);
+
+        /**
+         * Returns the number of times this UID woke up the Application Processor to
+         * process a WiFi packet.
+         * @param which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
+         */
+        public abstract long getWifiRadioApWakeupCount(int which);
+
         public static abstract class Sensor {
             /*
              * FIXME: it's not correct to use this magic value because it
@@ -1285,9 +1331,14 @@
         public static final int EVENT_TEMP_WHITELIST = 0x0011;
         // Event for the screen waking up.
         public static final int EVENT_SCREEN_WAKE_UP = 0x0012;
+        // Event for the UID that woke up the application processor.
+        // Used for wakeups coming from WiFi, modem, etc.
+        public static final int EVENT_WAKEUP_AP = 0x0013;
+        // Event for reporting that a specific partial wake lock has been held for a long duration.
+        public static final int EVENT_LONG_WAKE_LOCK = 0x0014;
 
         // Number of event types.
-        public static final int EVENT_COUNT = 0x0013;
+        public static final int EVENT_COUNT = 0x0015;
         // Mask to extract out only the type part of the event.
         public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
 
@@ -1315,6 +1366,10 @@
                 EVENT_TEMP_WHITELIST | EVENT_FLAG_START;
         public static final int EVENT_TEMP_WHITELIST_FINISH =
                 EVENT_TEMP_WHITELIST | EVENT_FLAG_FINISH;
+        public static final int EVENT_LONG_WAKE_LOCK_START =
+                EVENT_LONG_WAKE_LOCK | EVENT_FLAG_START;
+        public static final int EVENT_LONG_WAKE_LOCK_FINISH =
+                EVENT_LONG_WAKE_LOCK | EVENT_FLAG_FINISH;
 
         // For CMD_EVENT.
         public int eventCode;
@@ -1979,13 +2034,13 @@
     public static final String[] HISTORY_EVENT_NAMES = new String[] {
             "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
             "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active", "tmpwhitelist",
-            "screenwake",
+            "screenwake", "wakeupap", "longwake"
     };
 
     public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
             "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
             "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa", "Etw",
-            "Esw",
+            "Esw", "Ewa", "Elw"
     };
 
     /**
@@ -2535,6 +2590,22 @@
                 sb.append('(');
                 sb.append(count);
                 sb.append(" times)");
+                final long maxDurationMs = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
+                if (maxDurationMs >= 0) {
+                    sb.append(" max=");
+                    sb.append(maxDurationMs);
+                }
+                if (timer.isRunningLocked()) {
+                    final long currentMs = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
+                    if (currentMs >= 0) {
+                        sb.append(" (running for ");
+                        sb.append(currentMs);
+                        sb.append("ms)");
+                    } else {
+                        sb.append(" (running)");
+                    }
+                }
+
                 return ", ";
             }
         }
@@ -2542,6 +2613,7 @@
     }
 
     /**
+     * Prints details about a timer, if its total time was greater than 0.
      *
      * @param pw a PrintWriter object to print to.
      * @param sb a StringBuilder object.
@@ -2550,24 +2622,40 @@
      * @param which which one of STATS_SINCE_CHARGED, STATS_SINCE_UNPLUGGED, or STATS_CURRENT.
      * @param prefix a String to be prepended to each line of output.
      * @param type the name of the timer.
+     * @return true if anything was printed.
      */
     private static final boolean printTimer(PrintWriter pw, StringBuilder sb, Timer timer,
-            long rawRealtime, int which, String prefix, String type) {
+            long rawRealtimeUs, int which, String prefix, String type) {
         if (timer != null) {
             // Convert from microseconds to milliseconds with rounding
-            final long totalTime = (timer.getTotalTimeLocked(
-                    rawRealtime, which) + 500) / 1000;
+            final long totalTimeMs = (timer.getTotalTimeLocked(
+                    rawRealtimeUs, which) + 500) / 1000;
             final int count = timer.getCountLocked(which);
-            if (totalTime != 0) {
+            if (totalTimeMs != 0) {
                 sb.setLength(0);
                 sb.append(prefix);
                 sb.append("    ");
                 sb.append(type);
                 sb.append(": ");
-                formatTimeMs(sb, totalTime);
+                formatTimeMs(sb, totalTimeMs);
                 sb.append("realtime (");
                 sb.append(count);
                 sb.append(" times)");
+                final long maxDurationMs = timer.getMaxDurationMsLocked(rawRealtimeUs/1000);
+                if (maxDurationMs >= 0) {
+                    sb.append(" max=");
+                    sb.append(maxDurationMs);
+                }
+                if (timer.isRunningLocked()) {
+                    final long currentMs = timer.getCurrentDurationMsLocked(rawRealtimeUs/1000);
+                    if (currentMs >= 0) {
+                        sb.append(" (running for ");
+                        sb.append(currentMs);
+                        sb.append("ms)");
+                    } else {
+                        sb.append(" (running)");
+                    }
+                }
                 pw.println(sb.toString());
                 return true;
             }
@@ -2590,15 +2678,23 @@
             long elapsedRealtimeUs, String name, int which, String linePrefix) {
         long totalTimeMicros = 0;
         int count = 0;
+        long max = -1;
+        long current = -1;
         if (timer != null) {
             totalTimeMicros = timer.getTotalTimeLocked(elapsedRealtimeUs, which);
             count = timer.getCountLocked(which); 
+            current = timer.getCurrentDurationMsLocked(elapsedRealtimeUs/1000);
+            max = timer.getMaxDurationMsLocked(elapsedRealtimeUs/1000);
         }
         sb.append(linePrefix);
         sb.append((totalTimeMicros + 500) / 1000); // microseconds to milliseconds with rounding
         sb.append(',');
         sb.append(name != null ? name + "," : "");
         sb.append(count);
+        sb.append(',');
+        sb.append(current);
+        sb.append(',');
+        sb.append(max);
         return ",";
     }
     
@@ -3105,20 +3201,22 @@
             final long mobilePacketsTx = u.getNetworkActivityPackets(NETWORK_MOBILE_TX_DATA, which);
             final long mobileActiveTime = u.getMobileRadioActiveTime(which);
             final int mobileActiveCount = u.getMobileRadioActiveCount(which);
+            final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
             final long wifiPacketsRx = u.getNetworkActivityPackets(NETWORK_WIFI_RX_DATA, which);
             final long wifiPacketsTx = u.getNetworkActivityPackets(NETWORK_WIFI_TX_DATA, which);
+            final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
             final long btBytesRx = u.getNetworkActivityBytes(NETWORK_BT_RX_DATA, which);
             final long btBytesTx = u.getNetworkActivityBytes(NETWORK_BT_TX_DATA, which);
             if (mobileBytesRx > 0 || mobileBytesTx > 0 || wifiBytesRx > 0 || wifiBytesTx > 0
                     || mobilePacketsRx > 0 || mobilePacketsTx > 0 || wifiPacketsRx > 0
                     || wifiPacketsTx > 0 || mobileActiveTime > 0 || mobileActiveCount > 0
-                    || btBytesRx > 0 || btBytesTx > 0) {
+                    || btBytesRx > 0 || btBytesTx > 0 || mobileWakeup > 0 || wifiWakeup > 0) {
                 dumpLine(pw, uid, category, NETWORK_DATA, mobileBytesRx, mobileBytesTx,
                         wifiBytesRx, wifiBytesTx,
                         mobilePacketsRx, mobilePacketsTx,
                         wifiPacketsRx, wifiPacketsTx,
                         mobileActiveTime, mobileActiveCount,
-                        btBytesRx, btBytesTx);
+                        btBytesRx, btBytesTx, mobileWakeup, wifiWakeup);
             }
 
             // Dump modem controller data, per UID.
@@ -4125,6 +4223,9 @@
             final int wifiScanCount = u.getWifiScanCount(which);
             final long uidWifiRunningTime = u.getWifiRunningTime(rawRealtime, which);
 
+            final long mobileWakeup = u.getMobileRadioApWakeupCount(which);
+            final long wifiWakeup = u.getWifiRadioApWakeupCount(which);
+
             if (mobileRxBytes > 0 || mobileTxBytes > 0
                     || mobileRxPackets > 0 || mobileTxPackets > 0) {
                 pw.print(prefix); pw.print("    Mobile network: ");
@@ -4150,6 +4251,14 @@
                 pw.println(sb.toString());
             }
 
+            if (mobileWakeup > 0) {
+                sb.setLength(0);
+                sb.append(prefix);
+                sb.append("    Mobile radio AP wakeups: ");
+                sb.append(mobileWakeup);
+                pw.println(sb.toString());
+            }
+
             printControllerActivityIfInteresting(pw, sb, prefix + "  ", "Modem",
                     u.getModemControllerActivity(), which);
 
@@ -4181,6 +4290,14 @@
                 pw.println(sb.toString());
             }
 
+            if (wifiWakeup > 0) {
+                sb.setLength(0);
+                sb.append(prefix);
+                sb.append("    WiFi AP wakeups: ");
+                sb.append(wifiWakeup);
+                pw.println(sb.toString());
+            }
+
             printControllerActivityIfInteresting(pw, sb, prefix + "  ", "WiFi",
                     u.getWifiControllerActivity(), which);
 
@@ -4922,7 +5039,9 @@
                         pw.print(',');
                         pw.print(rec.stepDetails.statIdlTime);
                         pw.print(',');
-                        pw.print(rec.stepDetails.statPlatformIdleState);
+                        if (rec.stepDetails.statPlatformIdleState != null) {
+                            pw.print(rec.stepDetails.statPlatformIdleState);
+                        }
                         pw.println();
                     }
                 }
diff --git a/core/java/android/os/Build.java b/core/java/android/os/Build.java
index 06a4502..151239b 100644
--- a/core/java/android/os/Build.java
+++ b/core/java/android/os/Build.java
@@ -709,9 +709,22 @@
          * {@link android.widget.LinearLayout.LayoutParams LinearLayout.LayoutParams} to
          * {@link android.widget.RelativeLayout.LayoutParams RelativeLayout.LayoutParams}).</li>
          * <li>Your application processes will not be killed when the device density changes.</li>
+         * <li>Drag and drop. After a view receives the
+         * {@link android.view.DragEvent#ACTION_DRAG_ENTERED} event, when the drag shadow moves into
+         * a descendant view that can accept the data, the view receives the
+         * {@link android.view.DragEvent#ACTION_DRAG_EXITED} event and won’t receive
+         * {@link android.view.DragEvent#ACTION_DRAG_LOCATION} and
+         * {@link android.view.DragEvent#ACTION_DROP} events while the drag shadow is within that
+         * descendant view, even if the descendant view returns <code>false</code> from its handler
+         * for these events.</li>
          * </ul>
          */
         public static final int N = 24;
+
+        /**
+         * N MR1: Nougat++.
+         */
+        public static final int N_MR1 = 25;
     }
 
     /** The type of build, like "user" or "eng". */
diff --git a/core/java/android/os/Environment.java b/core/java/android/os/Environment.java
index 93fc13c..4616af8 100644
--- a/core/java/android/os/Environment.java
+++ b/core/java/android/os/Environment.java
@@ -345,6 +345,41 @@
     }
 
     /**
+     * Return preloads directory.
+     * <p>This directory may contain pre-loaded content such as
+     * {@link #getDataPreloadsDemoDirectory() demo videos} and
+     * {@link #getDataPreloadsAppsDirectory() APK files} .
+     * {@hide}
+     */
+    public static File getDataPreloadsDirectory() {
+        return new File(getDataDirectory(), "preloads");
+    }
+
+    /**
+     * @see #getDataPreloadsDirectory()
+     * {@hide}
+     */
+    public static File getDataPreloadsDemoDirectory() {
+        return new File(getDataPreloadsDirectory(), "demo");
+    }
+
+    /**
+     * @see #getDataPreloadsDirectory()
+     * {@hide}
+     */
+    public static File getDataPreloadsAppsDirectory() {
+        return new File(getDataPreloadsDirectory(), "apps");
+    }
+
+    /**
+     * @see #getDataPreloadsDirectory()
+     * {@hide}
+     */
+    public static File getDataPreloadsMediaDirectory() {
+        return new File(getDataPreloadsDirectory(), "media");
+    }
+
+    /**
      * Return the primary shared/external storage directory. This directory may
      * not currently be accessible if it has been mounted by the user on their
      * computer, has been removed from the device, or some other problem has
diff --git a/core/java/android/os/FileObserver.java b/core/java/android/os/FileObserver.java
index 4e705e0..dd85e15 100644
--- a/core/java/android/os/FileObserver.java
+++ b/core/java/android/os/FileObserver.java
@@ -16,6 +16,7 @@
 
 package android.os;
 
+import android.annotation.Nullable;
 import android.util.Log;
 
 import java.lang.ref.WeakReference;
@@ -204,7 +205,8 @@
      *
      * @param event The type of event which happened
      * @param path The path, relative to the main monitored file or directory,
-     *     of the file or directory which triggered the event
+     *     of the file or directory which triggered the event.  This value can
+     *     be {@code null} for certain events, such as {@link #MOVE_SELF}.
      */
-    public abstract void onEvent(int event, String path);
+    public abstract void onEvent(int event, @Nullable String path);
 }
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java
index fa32848..8e24caf 100644
--- a/core/java/android/os/FileUtils.java
+++ b/core/java/android/os/FileUtils.java
@@ -605,6 +605,22 @@
         return null;
     }
 
+    private static File buildUniqueFileWithExtension(File parent, String name, String ext)
+            throws FileNotFoundException {
+        File file = buildFile(parent, name, ext);
+
+        // If conflicting file, try adding counter suffix
+        int n = 0;
+        while (file.exists()) {
+            if (n++ >= 32) {
+                throw new FileNotFoundException("Failed to create unique file");
+            }
+            file = buildFile(parent, name + " (" + n + ")", ext);
+        }
+
+        return file;
+    }
+
     /**
      * Generates a unique file name under the given parent directory. If the display name doesn't
      * have an extension that matches the requested MIME type, the default extension for that MIME
@@ -619,20 +635,29 @@
     public static File buildUniqueFile(File parent, String mimeType, String displayName)
             throws FileNotFoundException {
         final String[] parts = splitFileName(mimeType, displayName);
-        final String name = parts[0];
-        final String ext = parts[1];
-        File file = buildFile(parent, name, ext);
+        return buildUniqueFileWithExtension(parent, parts[0], parts[1]);
+    }
 
-        // If conflicting file, try adding counter suffix
-        int n = 0;
-        while (file.exists()) {
-            if (n++ >= 32) {
-                throw new FileNotFoundException("Failed to create unique file");
-            }
-            file = buildFile(parent, name + " (" + n + ")", ext);
+    /**
+     * Generates a unique file name under the given parent directory, keeping
+     * any extension intact.
+     */
+    public static File buildUniqueFile(File parent, String displayName)
+            throws FileNotFoundException {
+        final String name;
+        final String ext;
+
+        // Extract requested extension from display name
+        final int lastDot = displayName.lastIndexOf('.');
+        if (lastDot >= 0) {
+            name = displayName.substring(0, lastDot);
+            ext = displayName.substring(lastDot + 1);
+        } else {
+            name = displayName;
+            ext = null;
         }
 
-        return file;
+        return buildUniqueFileWithExtension(parent, name, ext);
     }
 
     /**
diff --git a/core/java/android/os/INetworkManagementService.aidl b/core/java/android/os/INetworkManagementService.aidl
index e1dd02a..1c3d6bd 100644
--- a/core/java/android/os/INetworkManagementService.aidl
+++ b/core/java/android/os/INetworkManagementService.aidl
@@ -18,6 +18,7 @@
 package android.os;
 
 import android.net.InterfaceConfiguration;
+import android.net.INetd;
 import android.net.INetworkManagementEventObserver;
 import android.net.Network;
 import android.net.NetworkStats;
@@ -35,7 +36,7 @@
      **/
 
     /**
-     * Register an observer to receive events
+     * Register an observer to receive events.
      */
     void registerObserver(INetworkManagementEventObserver obs);
 
@@ -45,6 +46,11 @@
     void unregisterObserver(INetworkManagementEventObserver obs);
 
     /**
+     * Retrieve an INetd to talk to netd.
+     */
+    INetd getNetdService();
+
+    /**
      * Returns a list of currently known network interfaces
      */
     String[] listInterfaces();
@@ -414,6 +420,7 @@
 
     void addInterfaceToLocalNetwork(String iface, in List<RouteInfo> routes);
     void removeInterfaceFromLocalNetwork(String iface);
+    int removeRoutesFromLocalNetwork(in List<RouteInfo> routes);
 
     void setAllowOnlyVpnForUids(boolean enable, in UidRange[] uidRanges);
 }
diff --git a/core/java/android/os/IUserManager.aidl b/core/java/android/os/IUserManager.aidl
index b27cb32..eeb641d 100644
--- a/core/java/android/os/IUserManager.aidl
+++ b/core/java/android/os/IUserManager.aidl
@@ -81,4 +81,5 @@
     void clearSeedAccountData();
     boolean someUserHasSeedAccount(in String accountName, in String accountType);
     boolean isManagedProfile(int userId);
+    boolean isDemoUser(int userId);
 }
diff --git a/core/java/android/os/Looper.java b/core/java/android/os/Looper.java
index b58ff1f..d299672 100644
--- a/core/java/android/os/Looper.java
+++ b/core/java/android/os/Looper.java
@@ -147,7 +147,7 @@
             }
 
             final long traceTag = me.mTraceTag;
-            if (traceTag != 0) {
+            if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
                 Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
             }
             try {
diff --git a/core/java/android/os/Parcel.java b/core/java/android/os/Parcel.java
index 74dcc07..f6e6ad6 100644
--- a/core/java/android/os/Parcel.java
+++ b/core/java/android/os/Parcel.java
@@ -36,6 +36,7 @@
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamClass;
 import java.io.Serializable;
+import java.lang.reflect.Array;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
@@ -2571,6 +2572,20 @@
         return p;
     }
 
+    /** @hide */
+    public final <T extends Parcelable> T[] readParcelableArray(ClassLoader loader,
+            Class<T> clazz) {
+        int N = readInt();
+        if (N < 0) {
+            return null;
+        }
+        T[] p = (T[]) Array.newInstance(clazz, N);
+        for (int i = 0; i < N; i++) {
+            p[i] = readParcelable(loader);
+        }
+        return p;
+    }
+
     /**
      * Read and return a new Serializable object from the parcel.
      * @return the Serializable object, or null if the Serializable name
diff --git a/core/java/android/os/PatternMatcher.java b/core/java/android/os/PatternMatcher.java
index 56dc837..3890fbf 100644
--- a/core/java/android/os/PatternMatcher.java
+++ b/core/java/android/os/PatternMatcher.java
@@ -16,6 +16,10 @@
 
 package android.os;
 
+import android.util.Log;
+
+import java.util.Arrays;
+
 /**
  * A simple pattern matcher, which is safe to use on untrusted data: it does
  * not provide full reg-exp support, only simple globbing that can not be
@@ -44,13 +48,59 @@
      * wildcard part of a normal regexp. 
      */
     public static final int PATTERN_SIMPLE_GLOB = 2;
-    
+
+    /**
+     * Pattern type: the given pattern is interpreted with a regular
+     * expression-like syntax for matching against the string it is tested
+     * against. Supported tokens include dot ({@code .}) and sets ({@code [...]})
+     * with full support for character ranges and the not ({@code ^}) modifier.
+     * Supported modifiers include star ({@code *}) for zero-or-more, plus ({@code +})
+     * for one-or-more and full range ({@code {...}}) support. This is a simple
+     * evaulation implementation in which matching is done against the pattern in
+     * realtime with no backtracking support.
+     *
+     * {@hide} Pending approval for public API
+     */
+    public static final int PATTERN_ADVANCED_GLOB = 3;
+
+    // token types for advanced matching
+    private static final int TOKEN_TYPE_LITERAL = 0;
+    private static final int TOKEN_TYPE_ANY = 1;
+    private static final int TOKEN_TYPE_SET = 2;
+    private static final int TOKEN_TYPE_INVERSE_SET = 3;
+
+    // Return for no match
+    private static final int NO_MATCH = -1;
+
+    private static final String TAG = "PatternMatcher";
+
+    // Parsed placeholders for advanced patterns
+    private static final int PARSED_TOKEN_CHAR_SET_START = -1;
+    private static final int PARSED_TOKEN_CHAR_SET_INVERSE_START = -2;
+    private static final int PARSED_TOKEN_CHAR_SET_STOP = -3;
+    private static final int PARSED_TOKEN_CHAR_ANY = -4;
+    private static final int PARSED_MODIFIER_RANGE_START = -5;
+    private static final int PARSED_MODIFIER_RANGE_STOP = -6;
+    private static final int PARSED_MODIFIER_ZERO_OR_MORE = -7;
+    private static final int PARSED_MODIFIER_ONE_OR_MORE = -8;
+
     private final String mPattern;
     private final int mType;
-    
+    private final int[] mParsedPattern;
+
+
+    private static final int MAX_PATTERN_STORAGE = 2048;
+    // workspace to use for building a parsed advanced pattern;
+    private static final int[] sParsedPatternScratch = new int[MAX_PATTERN_STORAGE];
+
     public PatternMatcher(String pattern, int type) {
         mPattern = pattern;
         mType = type;
+        if (mType == PATTERN_ADVANCED_GLOB) {
+            mParsedPattern = parseAndVerifyAdvancedPattern(pattern);
+        } else {
+            mParsedPattern = null;
+        }
     }
 
     public final String getPath() {
@@ -62,7 +112,7 @@
     }
     
     public boolean match(String str) {
-        return matchPattern(mPattern, str, mType);
+        return matchPattern(str, mPattern, mParsedPattern, mType);
     }
 
     public String toString() {
@@ -77,6 +127,9 @@
             case PATTERN_SIMPLE_GLOB:
                 type = "GLOB: ";
                 break;
+            case PATTERN_ADVANCED_GLOB:
+                type = "ADVANCED: ";
+                break;
         }
         return "PatternMatcher{" + type + mPattern + "}";
     }
@@ -88,11 +141,13 @@
     public void writeToParcel(Parcel dest, int flags) {
         dest.writeString(mPattern);
         dest.writeInt(mType);
+        dest.writeIntArray(mParsedPattern);
     }
     
     public PatternMatcher(Parcel src) {
         mPattern = src.readString();
         mType = src.readInt();
+        mParsedPattern = src.createIntArray();
     }
     
     public static final Parcelable.Creator<PatternMatcher> CREATOR
@@ -106,16 +161,21 @@
         }
     };
     
-    static boolean matchPattern(String pattern, String match, int type) {
+    static boolean matchPattern(String match, String pattern, int[] parsedPattern, int type) {
         if (match == null) return false;
         if (type == PATTERN_LITERAL) {
             return pattern.equals(match);
         } if (type == PATTERN_PREFIX) {
             return match.startsWith(pattern);
-        } else if (type != PATTERN_SIMPLE_GLOB) {
-            return false;
+        } else if (type == PATTERN_SIMPLE_GLOB) {
+            return matchGlobPattern(pattern, match);
+        } else if (type == PATTERN_ADVANCED_GLOB) {
+            return matchAdvancedPattern(parsedPattern, match);
         }
-        
+        return false;
+    }
+
+    static boolean matchGlobPattern(String pattern, String match) {
         final int NP = pattern.length();
         if (NP <= 0) {
             return match.length() <= 0;
@@ -194,4 +254,310 @@
         
         return false;
     }
-}
+
+    /**
+     * Parses the advanced pattern and returns an integer array representation of it. The integer
+     * array treats each field as a character if positive and a unique token placeholder if
+     * negative. This method will throw on any pattern structure violations.
+     */
+    synchronized static int[] parseAndVerifyAdvancedPattern(String pattern) {
+        int ip = 0;
+        final int LP = pattern.length();
+
+        int it = 0;
+
+        boolean inSet = false;
+        boolean inRange = false;
+        boolean inCharClass = false;
+
+        boolean addToParsedPattern;
+
+        while (ip < LP) {
+            if (it > MAX_PATTERN_STORAGE - 3) {
+                throw new IllegalArgumentException("Pattern is too large!");
+            }
+
+            char c = pattern.charAt(ip);
+            addToParsedPattern = false;
+
+            switch (c) {
+                case '[':
+                    if (inSet) {
+                        addToParsedPattern = true; // treat as literal or char class in set
+                    } else {
+                        if (pattern.charAt(ip + 1) == '^') {
+                            sParsedPatternScratch[it++] = PARSED_TOKEN_CHAR_SET_INVERSE_START;
+                            ip++; // skip over the '^'
+                        } else {
+                            sParsedPatternScratch[it++] = PARSED_TOKEN_CHAR_SET_START;
+                        }
+                        ip++; // move to the next pattern char
+                        inSet = true;
+                        continue;
+                    }
+                    break;
+                case ']':
+                    if (!inSet) {
+                        addToParsedPattern = true; // treat as literal outside of set
+                    } else {
+                        int parsedToken = sParsedPatternScratch[it - 1];
+                        if (parsedToken == PARSED_TOKEN_CHAR_SET_START ||
+                            parsedToken == PARSED_TOKEN_CHAR_SET_INVERSE_START) {
+                            throw new IllegalArgumentException(
+                                    "You must define characters in a set.");
+                        }
+                        sParsedPatternScratch[it++] = PARSED_TOKEN_CHAR_SET_STOP;
+                        inSet = false;
+                        inCharClass = false;
+                    }
+                    break;
+                case '{':
+                    if (!inSet) {
+                        if (it == 0 || isParsedModifier(sParsedPatternScratch[it - 1])) {
+                            throw new IllegalArgumentException("Modifier must follow a token.");
+                        }
+                        sParsedPatternScratch[it++] = PARSED_MODIFIER_RANGE_START;
+                        ip++;
+                        inRange = true;
+                    }
+                    break;
+                case '}':
+                    if (inRange) { // only terminate the range if we're currently in one
+                        sParsedPatternScratch[it++] = PARSED_MODIFIER_RANGE_STOP;
+                        inRange = false;
+                    }
+                    break;
+                case '*':
+                    if (!inSet) {
+                        if (it == 0 || isParsedModifier(sParsedPatternScratch[it - 1])) {
+                            throw new IllegalArgumentException("Modifier must follow a token.");
+                        }
+                        sParsedPatternScratch[it++] = PARSED_MODIFIER_ZERO_OR_MORE;
+                    }
+                    break;
+                case '+':
+                    if (!inSet) {
+                        if (it == 0 || isParsedModifier(sParsedPatternScratch[it - 1])) {
+                            throw new IllegalArgumentException("Modifier must follow a token.");
+                        }
+                        sParsedPatternScratch[it++] = PARSED_MODIFIER_ONE_OR_MORE;
+                    }
+                    break;
+                case '.':
+                    if (!inSet) {
+                        sParsedPatternScratch[it++] = PARSED_TOKEN_CHAR_ANY;
+                    }
+                    break;
+                case '\\': // escape
+                    if (ip + 1 >= LP) {
+                        throw new IllegalArgumentException("Escape found at end of pattern!");
+                    }
+                    c = pattern.charAt(++ip);
+                    addToParsedPattern = true;
+                    break;
+                default:
+                    addToParsedPattern = true;
+                    break;
+            }
+            if (inSet) {
+                if (inCharClass) {
+                    sParsedPatternScratch[it++] = c;
+                    inCharClass = false;
+                } else {
+                    // look forward for character class
+                    if (ip + 2 < LP
+                            && pattern.charAt(ip + 1) == '-'
+                            && pattern.charAt(ip + 2) != ']') {
+                        inCharClass = true;
+                        sParsedPatternScratch[it++] = c; // set first token as lower end of range
+                        ip++; // advance past dash
+                    } else { // literal
+                        sParsedPatternScratch[it++] = c; // set first token as literal
+                        sParsedPatternScratch[it++] = c; // set second set as literal
+                    }
+                }
+            } else if (inRange) {
+                int endOfSet = pattern.indexOf('}', ip);
+                if (endOfSet < 0) {
+                    throw new IllegalArgumentException("Range not ended with '}'");
+                }
+                String rangeString = pattern.substring(ip, endOfSet);
+                int commaIndex = rangeString.indexOf(',');
+                try {
+                    final int rangeMin;
+                    final int rangeMax;
+                    if (commaIndex < 0) {
+                        int parsedRange = Integer.parseInt(rangeString);
+                        rangeMin = rangeMax = parsedRange;
+                    } else {
+                        rangeMin = Integer.parseInt(rangeString.substring(0, commaIndex));
+                        if (commaIndex == rangeString.length() - 1) { // e.g. {n,} (n or more)
+                            rangeMax = Integer.MAX_VALUE;
+                        } else {
+                            rangeMax = Integer.parseInt(rangeString.substring(commaIndex + 1));
+                        }
+                    }
+                    if (rangeMin > rangeMax) {
+                        throw new IllegalArgumentException(
+                            "Range quantifier minimum is greater than maximum");
+                    }
+                    sParsedPatternScratch[it++] = rangeMin;
+                    sParsedPatternScratch[it++] = rangeMax;
+                } catch (NumberFormatException e) {
+                    throw new IllegalArgumentException("Range number format incorrect", e);
+                }
+                ip = endOfSet;
+                continue; // don't increment ip
+            } else if (addToParsedPattern) {
+                sParsedPatternScratch[it++] = c;
+            }
+            ip++;
+        }
+        if (inSet) {
+            throw new IllegalArgumentException("Set was not terminated!");
+        }
+        return Arrays.copyOf(sParsedPatternScratch, it);
+    }
+
+    private static boolean isParsedModifier(int parsedChar) {
+        return parsedChar == PARSED_MODIFIER_ONE_OR_MORE ||
+                parsedChar == PARSED_MODIFIER_ZERO_OR_MORE ||
+                parsedChar == PARSED_MODIFIER_RANGE_STOP ||
+                parsedChar == PARSED_MODIFIER_RANGE_START;
+    }
+
+    static boolean matchAdvancedPattern(int[] parsedPattern, String match) {
+
+        // create indexes
+        int ip = 0, im = 0;
+
+        // one-time length check
+        final int LP = parsedPattern.length, LM = match.length();
+
+        // The current character being analyzed in the pattern
+        int patternChar;
+
+        int tokenType;
+
+        int charSetStart = 0, charSetEnd = 0;
+
+        while (ip < LP) { // we still have content in the pattern
+
+            patternChar = parsedPattern[ip];
+            // get the match type of the next verb
+
+            switch (patternChar) {
+                case PARSED_TOKEN_CHAR_ANY:
+                    tokenType = TOKEN_TYPE_ANY;
+                    ip++;
+                    break;
+                case PARSED_TOKEN_CHAR_SET_START:
+                case PARSED_TOKEN_CHAR_SET_INVERSE_START:
+                    tokenType = patternChar == PARSED_TOKEN_CHAR_SET_START
+                            ? TOKEN_TYPE_SET
+                            : TOKEN_TYPE_INVERSE_SET;
+                    charSetStart = ip + 1; // start from the char after the set start
+                    while (++ip < LP && parsedPattern[ip] != PARSED_TOKEN_CHAR_SET_STOP);
+                    charSetEnd = ip - 1; // we're on the set stop, end is the previous
+                    ip++; // move the pointer to the next pattern entry
+                    break;
+                default:
+                    charSetStart = ip;
+                    tokenType = TOKEN_TYPE_LITERAL;
+                    ip++;
+                    break;
+            }
+
+            final int minRepetition;
+            final int maxRepetition;
+
+            // look for a match length modifier
+            if (ip >= LP) {
+                minRepetition = maxRepetition = 1;
+            } else {
+                patternChar = parsedPattern[ip];
+                switch (patternChar) {
+                    case PARSED_MODIFIER_ZERO_OR_MORE:
+                        minRepetition = 0;
+                        maxRepetition = Integer.MAX_VALUE;
+                        ip++;
+                        break;
+                    case PARSED_MODIFIER_ONE_OR_MORE:
+                        minRepetition = 1;
+                        maxRepetition = Integer.MAX_VALUE;
+                        ip++;
+                        break;
+                    case PARSED_MODIFIER_RANGE_START:
+                        minRepetition = parsedPattern[++ip];
+                        maxRepetition = parsedPattern[++ip];
+                        ip += 2; // step over PARSED_MODIFIER_RANGE_STOP and on to the next token
+                        break;
+                    default:
+                        minRepetition = maxRepetition = 1; // implied literal
+                        break;
+                }
+            }
+            if (minRepetition > maxRepetition) {
+                return false;
+            }
+
+            // attempt to match as many characters as possible
+            int matched = matchChars(match, im, LM, tokenType, minRepetition, maxRepetition,
+                    parsedPattern, charSetStart, charSetEnd);
+
+            // if we found a conflict, return false immediately
+            if (matched == NO_MATCH) {
+                return false;
+            }
+
+            // move the match pointer the number of characters matched
+            im += matched;
+        }
+        return ip >= LP && im >= LM; // have parsed entire string and regex
+    }
+
+    private static int matchChars(String match, int im, final int lm, int tokenType,
+            int minRepetition, int maxRepetition, int[] parsedPattern,
+            int tokenStart, int tokenEnd) {
+        int matched = 0;
+
+        while(matched < maxRepetition
+                && matchChar(match, im + matched, lm, tokenType, parsedPattern, tokenStart,
+                    tokenEnd)) {
+            matched++;
+        }
+
+        return matched < minRepetition ? NO_MATCH : matched;
+    }
+
+    private static boolean matchChar(String match, int im, final int lm, int tokenType,
+            int[] parsedPattern, int tokenStart, int tokenEnd) {
+        if (im >= lm) { // we've overrun the string, no match
+            return false;
+        }
+        switch (tokenType) {
+            case TOKEN_TYPE_ANY:
+                return true;
+            case TOKEN_TYPE_SET:
+                for (int i = tokenStart; i < tokenEnd; i += 2) {
+                    char matchChar = match.charAt(im);
+                    if (matchChar >= parsedPattern[i] && matchChar <= parsedPattern[i + 1]) {
+                        return true;
+                    }
+                }
+                return false;
+            case TOKEN_TYPE_INVERSE_SET:
+                for (int i = tokenStart; i < tokenEnd; i += 2) {
+                    char matchChar = match.charAt(im);
+                    if (matchChar >= parsedPattern[i] && matchChar <= parsedPattern[i + 1]) {
+                        return false;
+                    }
+                }
+                return true;
+            case TOKEN_TYPE_LITERAL:
+                return match.charAt(im) == parsedPattern[tokenStart];
+            default:
+                return false;
+        }
+    }
+}
\ No newline at end of file
diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java
index ce7a124..8d4d0a5 100644
--- a/core/java/android/os/PowerManager.java
+++ b/core/java/android/os/PowerManager.java
@@ -1340,6 +1340,11 @@
         }
 
         /** @hide */
+        public String getTag() {
+            return mTag;
+        }
+
+        /** @hide */
         public void setHistoryTag(String tag) {
             mHistoryTag = tag;
         }
@@ -1358,5 +1363,34 @@
                     + " held=" + mHeld + ", refCount=" + mCount + "}";
             }
         }
+
+        /**
+         * Wraps a Runnable such that this method immediately acquires the wake lock and then
+         * once the Runnable is done the wake lock is released.
+         *
+         * <p>Example:
+         *
+         * <pre>
+         * mHandler.post(mWakeLock.wrap(() -> {
+         *     // do things on handler, lock is held while we're waiting for this
+         *     // to get scheduled and until the runnable is done executing.
+         * });
+         * </pre>
+         *
+         * <p>Note: you must make sure that the Runnable eventually gets executed, otherwise you'll
+         *    leak the wakelock!
+         *
+         * @hide
+         */
+        public Runnable wrap(Runnable r) {
+            acquire();
+            return () -> {
+                try {
+                    r.run();
+                } finally {
+                    release();
+                }
+            };
+        }
     }
 }
diff --git a/core/java/android/os/PowerManagerInternal.java b/core/java/android/os/PowerManagerInternal.java
index b3cf710..9bbe8f9 100644
--- a/core/java/android/os/PowerManagerInternal.java
+++ b/core/java/android/os/PowerManagerInternal.java
@@ -57,14 +57,19 @@
     /**
      * Power hint:
      * Interaction: The user is interacting with the device. The corresponding data field must be
-     * the expected duration of the fling, or 0 if unknown.
+     * the expected duration of the interaction, or 0 if unknown.
      *
-     * Sustained Performance Mode: Enable/Disables Sustained Performance Mode.
+     * Sustained Performance Mode: The corresponding data field must be Enable/Disable
+     * Sustained Performance Mode.
+     *
+     * Launch: This is specific for activity launching. The corresponding data field must be
+     * the expected duration of the required boost, or 0 if unknown.
      *
      * These must be kept in sync with the values in hardware/libhardware/include/hardware/power.h
      */
     public static final int POWER_HINT_INTERACTION = 2;
     public static final int POWER_HINT_SUSTAINED_PERFORMANCE_MODE = 6;
+    public static final int POWER_HINT_LAUNCH = 8;
 
     public static String wakefulnessToString(int wakefulness) {
         switch (wakefulness) {
diff --git a/core/java/android/os/Process.java b/core/java/android/os/Process.java
index 21b3f6e..e1b7fda 100644
--- a/core/java/android/os/Process.java
+++ b/core/java/android/os/Process.java
@@ -16,9 +16,11 @@
 
 package android.os;
 
+import android.annotation.TestApi;
 import android.net.LocalSocket;
 import android.net.LocalSocketAddress;
 import android.system.Os;
+import android.system.OsConstants;
 import android.util.Log;
 import com.android.internal.os.Zygote;
 import dalvik.system.VMRuntime;
@@ -322,6 +324,12 @@
      */
     public static final int SCHED_IDLE = 5;
 
+    /**
+     * Reset scheduler choice on fork.
+     * @hide
+     */
+    public static final int SCHED_RESET_ON_FORK = 0x40000000;
+
     // Keep in sync with SP_* constants of enum type SchedPolicy
     // declared in system/core/include/cutils/sched_policy.h,
     // except THREAD_GROUP_DEFAULT does not correspond to any SP_* value.
@@ -973,6 +981,9 @@
      * priority.
      * If the thread is a thread group leader, that is it's gettid() == getpid(),
      * then the other threads in the same thread group are _not_ affected.
+     *
+     * Does not set cpuset for some historical reason, just calls
+     * libcutils::set_sched_policy().
      */
     public static final native void setThreadGroup(int tid, int group)
             throws IllegalArgumentException, SecurityException;
@@ -994,6 +1005,8 @@
      * priority threads alone.  group == THREAD_GROUP_BG_NONINTERACTIVE moves all
      * threads, regardless of priority, to the background scheduling group.
      * group == THREAD_GROUP_FOREGROUND is not allowed.
+     *
+     * Always sets cpusets.
      */
     public static final native void setProcessGroup(int pid, int group)
             throws IllegalArgumentException, SecurityException;
@@ -1065,6 +1078,24 @@
             throws IllegalArgumentException;
     
     /**
+     * Return the current scheduling policy of a thread, based on Linux.
+     *
+     * @param tid The identifier of the thread/process to get the scheduling policy.
+     *
+     * @throws IllegalArgumentException Throws IllegalArgumentException if
+     * <var>tid</var> does not exist, or if <var>priority</var> is out of range for the policy.
+     * @throws SecurityException Throws SecurityException if your process does
+     * not have permission to modify the given thread, or to use the given
+     * scheduling policy or priority.
+     *
+     * {@hide}
+     */
+    
+    @TestApi
+    public static final native int getThreadScheduler(int tid)
+            throws IllegalArgumentException;
+
+    /**
      * Set the scheduling policy and priority of a thread, based on Linux.
      *
      * @param tid The identifier of the thread/process to change.
@@ -1079,6 +1110,7 @@
      *
      * {@hide}
      */
+
     public static final native void setThreadScheduler(int tid, int policy, int priority)
             throws IllegalArgumentException;
 
@@ -1252,4 +1284,26 @@
      * @hide
      */
     public static final native void removeAllProcessGroups();
+
+    /**
+     * Check to see if a thread belongs to a given process. This may require
+     * more permissions than apps generally have.
+     * @return true if this thread belongs to a process
+     * @hide
+     */
+    public static final boolean isThreadInProcess(int tid, int pid) {
+        StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
+        try {
+            if (Os.access("/proc/" + tid + "/task/" + pid, OsConstants.F_OK)) {
+                return true;
+            } else {
+                return false;
+            }
+        } catch (Exception e) {
+            return false;
+        } finally {
+            StrictMode.setThreadPolicy(oldPolicy);
+        }
+
+    }
 }
diff --git a/core/java/android/os/RecoverySystem.java b/core/java/android/os/RecoverySystem.java
index 662e14c..d48431a 100644
--- a/core/java/android/os/RecoverySystem.java
+++ b/core/java/android/os/RecoverySystem.java
@@ -578,18 +578,19 @@
      * @throws SecurityException if the current user is not allowed to wipe data.
      */
     public static void rebootWipeUserData(Context context) throws IOException {
-        rebootWipeUserData(context, false, context.getPackageName());
+        rebootWipeUserData(context, false /* shutdown */, context.getPackageName(),
+                false /* force */);
     }
 
     /** {@hide} */
     public static void rebootWipeUserData(Context context, String reason) throws IOException {
-        rebootWipeUserData(context, false, reason);
+        rebootWipeUserData(context, false /* shutdown */, reason, false /* force */);
     }
 
     /** {@hide} */
     public static void rebootWipeUserData(Context context, boolean shutdown)
             throws IOException {
-        rebootWipeUserData(context, shutdown, context.getPackageName());
+        rebootWipeUserData(context, shutdown, context.getPackageName(), false /* force */);
     }
 
     /**
@@ -603,6 +604,9 @@
      * @param shutdown  if true, the device will be powered down after
      *                  the wipe completes, rather than being rebooted
      *                  back to the regular system.
+     * @param reason    the reason for the wipe that is visible in the logs
+     * @param force     whether the {@link UserManager.DISALLOW_FACTORY_RESET} user restriction
+     *                  should be ignored
      *
      * @throws IOException  if writing the recovery command file
      * fails, or if the reboot itself fails.
@@ -610,10 +614,10 @@
      *
      * @hide
      */
-    public static void rebootWipeUserData(Context context, boolean shutdown, String reason)
-            throws IOException {
+    public static void rebootWipeUserData(Context context, boolean shutdown, String reason,
+            boolean force) throws IOException {
         UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
-        if (um.hasUserRestriction(UserManager.DISALLOW_FACTORY_RESET)) {
+        if (!force && um.hasUserRestriction(UserManager.DISALLOW_FACTORY_RESET)) {
             throw new SecurityException("Wiping data is not allowed for this user.");
         }
         final ConditionVariable condition = new ConditionVariable();
@@ -666,6 +670,31 @@
     }
 
     /**
+     * Reboot into recovery and wipe the A/B device.
+     *
+     * @param Context      the Context to use.
+     * @param packageFile  the wipe package to be applied.
+     * @param reason       the reason to wipe.
+     *
+     * @throws IOException if something goes wrong.
+     *
+     * @hide
+     */
+    @SystemApi
+    public static void rebootWipeAb(Context context, File packageFile, String reason)
+            throws IOException {
+        String reasonArg = null;
+        if (!TextUtils.isEmpty(reason)) {
+            reasonArg = "--reason=" + sanitizeArg(reason);
+        }
+
+        final String filename = packageFile.getCanonicalPath();
+        final String filenameArg = "--wipe_package=" + filename;
+        final String localeArg = "--locale=" + Locale.getDefault().toString();
+        bootCommand(context, "--wipe_ab", filenameArg, reasonArg, localeArg);
+    }
+
+    /**
      * Reboot into the recovery system with the supplied argument.
      * @param args to pass to the recovery utility.
      * @throws IOException if something goes wrong.
diff --git a/core/java/android/os/RemoteCallbackList.java b/core/java/android/os/RemoteCallbackList.java
index 5849350..3546e17 100644
--- a/core/java/android/os/RemoteCallbackList.java
+++ b/core/java/android/os/RemoteCallbackList.java
@@ -288,20 +288,22 @@
      * @see #beginBroadcast
      */
     public void finishBroadcast() {
-        if (mBroadcastCount < 0) {
-            throw new IllegalStateException(
-                    "finishBroadcast() called outside of a broadcast");
-        }
-        
-        Object[] active = mActiveBroadcast;
-        if (active != null) {
-            final int N = mBroadcastCount;
-            for (int i=0; i<N; i++) {
-                active[i] = null;
+        synchronized (mCallbacks) {
+            if (mBroadcastCount < 0) {
+                throw new IllegalStateException(
+                        "finishBroadcast() called outside of a broadcast");
             }
+
+            Object[] active = mActiveBroadcast;
+            if (active != null) {
+                final int N = mBroadcastCount;
+                for (int i=0; i<N; i++) {
+                    active[i] = null;
+                }
+            }
+
+            mBroadcastCount = -1;
         }
-        
-        mBroadcastCount = -1;
     }
 
     /**
diff --git a/core/java/android/os/Trace.java b/core/java/android/os/Trace.java
index 9e8103a..3ae28fd 100644
--- a/core/java/android/os/Trace.java
+++ b/core/java/android/os/Trace.java
@@ -81,6 +81,8 @@
     public static final long TRACE_TAG_SYSTEM_SERVER = 1L << 19;
     /** @hide */
     public static final long TRACE_TAG_DATABASE = 1L << 20;
+    /** @hide */
+    public static final long TRACE_TAG_NETWORK = 1L << 21;
 
     private static final long TRACE_TAG_NOT_READY = 1L << 63;
     private static final int MAX_SECTION_NAME_LEN = 127;
diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java
index bcc8d46..03d3cf2 100644
--- a/core/java/android/os/UserManager.java
+++ b/core/java/android/os/UserManager.java
@@ -580,6 +580,16 @@
     public static final String DISALLOW_CAMERA = "no_camera";
 
     /**
+     * Specifies if a user is not allowed to unmute the device's master volume.
+     *
+     * @see DevicePolicyManager#setMasterVolumeMuted(ComponentName, boolean)
+     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
+     * @see #getUserRestrictions()
+     * @hide
+     */
+    public static final String DISALLLOW_UNMUTE_DEVICE = "disallow_unmute_device";
+
+    /**
      * Specifies if a user is not allowed to use cellular data when roaming. This can only be set by
      * device owners. The default value is <code>false</code>.
      *
@@ -603,6 +613,18 @@
     public static final String DISALLOW_SET_USER_ICON = "no_set_user_icon";
 
     /**
+     * Specifies if a user is not allowed to enable the oem unlock setting. The default value is
+     * <code>false</code>. Setting this restriction has no effect if the bootloader is already
+     * unlocked.
+     *
+     * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
+     * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
+     * @see #getUserRestrictions()
+     * @hide
+     */
+    public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
+
+    /**
      * Allows apps in the parent profile to handle web links from the managed profile.
      *
      * This user restriction has an effect only in a managed profile.
@@ -789,7 +811,7 @@
      */
     public boolean isPrimaryUser() {
         UserInfo user = getUserInfo(UserHandle.myUserId());
-        return user != null ? user.isPrimary() : false;
+        return user != null && user.isPrimary();
     }
 
     /**
@@ -855,7 +877,21 @@
      */
     public boolean isGuestUser() {
         UserInfo user = getUserInfo(UserHandle.myUserId());
-        return user != null ? user.isGuest() : false;
+        return user != null && user.isGuest();
+    }
+
+    /**
+     * Checks if the calling app is running in a demo user. When running in a demo user,
+     * apps can be more helpful to the user, or explain their features in more detail.
+     *
+     * @return whether the caller is a demo user.
+     */
+    public boolean isDemoUser() {
+        try {
+            return mService.isDemoUser(UserHandle.myUserId());
+        } catch (RemoteException re) {
+            throw re.rethrowFromSystemServer();
+        }
     }
 
     /**
@@ -1975,6 +2011,10 @@
         if (!supportsMultipleUsers()) {
             return false;
         }
+        // If Demo Mode is on, don't show user switcher
+        if (isDeviceInDemoMode(mContext)) {
+            return false;
+        }
         List<UserInfo> users = getUsers(true);
         if (users == null) {
            return false;
@@ -1991,6 +2031,14 @@
     }
 
     /**
+     * @hide
+     */
+    public static boolean isDeviceInDemoMode(Context context) {
+        return Settings.Global.getInt(context.getContentResolver(),
+                Settings.Global.DEVICE_DEMO_MODE, 0) > 0;
+    }
+
+    /**
      * Returns a serial number on this device for a given userHandle. User handles can be recycled
      * when deleting and creating users, but serial numbers are not reused until the device is wiped.
      * @param userHandle
diff --git a/core/java/android/os/storage/StorageManager.java b/core/java/android/os/storage/StorageManager.java
index 485bbd1..c5507b9 100644
--- a/core/java/android/os/storage/StorageManager.java
+++ b/core/java/android/os/storage/StorageManager.java
@@ -20,6 +20,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.annotation.SdkConstant;
 import android.app.ActivityThread;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -45,8 +46,11 @@
 import com.android.internal.os.SomeArgs;
 import com.android.internal.util.Preconditions;
 
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -92,6 +96,19 @@
     /** {@hide} */
     public static final String UUID_PRIMARY_PHYSICAL = "primary_physical";
 
+
+    /**
+     * Activity Action: Allows the user to manage their storage. This activity provides the ability
+     * to free up space on the device by deleting data such as apps.
+     * <p>
+     * Input: Nothing.
+     * <p>
+     * Output: Nothing.
+     */
+    @SdkConstant(SdkConstant.SdkConstantType.ACTIVITY_INTENT_ACTION)
+    public static final String ACTION_MANAGE_STORAGE
+            = "android.os.storage.action.MANAGE_STORAGE";
+
     /** {@hide} */
     public static final int DEBUG_FORCE_ADOPTABLE = 1 << 0;
     /** {@hide} */
@@ -116,6 +133,15 @@
 
     private static volatile IMountService sMountService = null;
 
+    // TODO: the location of the primary storage block varies from device to device, so we need to
+    // try the most likely candidates - a long-term solution would be a device-specific vold
+    // function that returns the calculated size.
+    private static final String[] INTERNAL_STORAGE_SIZE_PATHS = {
+            "/sys/block/mmcblk0/size",
+            "/sys/block/sda/size"
+    };
+    private static final int INTERNAL_STORAGE_SECTOR_SIZE = 512;
+
     private final Context mContext;
     private final ContentResolver mResolver;
 
@@ -903,6 +929,27 @@
         return getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE)[0];
     }
 
+    /** {@hide} */
+    public long getPrimaryStorageSize() {
+        for (String path : INTERNAL_STORAGE_SIZE_PATHS) {
+            final long numberBlocks = readLong(path);
+            if (numberBlocks > 0) {
+                return numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE;
+            }
+        }
+        return 0;
+    }
+
+    private long readLong(String path) {
+        try (final FileInputStream fis = new FileInputStream(path);
+                final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));) {
+            return Long.parseLong(reader.readLine());
+        } catch (Exception e) {
+            Slog.w(TAG, "Could not read " + path, e);
+            return 0;
+        }
+    }
+
     /** @removed */
     public @NonNull StorageVolume[] getVolumeList() {
         return getVolumeList(mContext.getUserId(), 0);
diff --git a/core/java/android/preference/Preference.java b/core/java/android/preference/Preference.java
index b1cad05..ec1e102 100644
--- a/core/java/android/preference/Preference.java
+++ b/core/java/android/preference/Preference.java
@@ -719,6 +719,9 @@
      * @see #setIcon(Drawable)
      */
     public Drawable getIcon() {
+        if (mIcon == null && mIconResId != 0) {
+            mIcon = getContext().getDrawable(mIconResId);
+        }
         return mIcon;
     }
 
diff --git a/core/java/android/preference/PreferenceFragment.java b/core/java/android/preference/PreferenceFragment.java
index 3e496b6..73fa01e 100644
--- a/core/java/android/preference/PreferenceFragment.java
+++ b/core/java/android/preference/PreferenceFragment.java
@@ -27,12 +27,14 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
+import android.text.TextUtils;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnKeyListener;
 import android.view.ViewGroup;
 import android.widget.ListView;
+import android.widget.TextView;
 
 /**
  * Shows a hierarchy of {@link Preference} objects as
@@ -189,12 +191,10 @@
                 0);
 
         ListView lv = (ListView) view.findViewById(android.R.id.list);
-        if (lv != null) {
-            Drawable divider =
-                    a.getDrawable(com.android.internal.R.styleable.PreferenceFragment_divider);
-            if (divider != null) {
-                lv.setDivider(divider);
-            }
+        if (lv != null
+                && a.hasValueOrEmpty(com.android.internal.R.styleable.PreferenceFragment_divider)) {
+            lv.setDivider(
+                    a.getDrawable(com.android.internal.R.styleable.PreferenceFragment_divider));
         }
 
         a.recycle();
@@ -368,6 +368,20 @@
     private void bindPreferences() {
         final PreferenceScreen preferenceScreen = getPreferenceScreen();
         if (preferenceScreen != null) {
+            View root = getView();
+            if (root != null) {
+                View titleView = root.findViewById(android.R.id.title);
+                if (titleView instanceof TextView) {
+                    CharSequence title = preferenceScreen.getTitle();
+                    if (TextUtils.isEmpty(title)) {
+                        titleView.setVisibility(View.GONE);
+                    } else {
+                        ((TextView) titleView).setText(title);
+                        titleView.setVisibility(View.VISIBLE);
+                    }
+                }
+            }
+
             preferenceScreen.bind(getListView());
         }
         onBindPreferences();
diff --git a/core/java/android/preference/PreferenceScreen.java b/core/java/android/preference/PreferenceScreen.java
index b1317e6..2305b05 100644
--- a/core/java/android/preference/PreferenceScreen.java
+++ b/core/java/android/preference/PreferenceScreen.java
@@ -19,6 +19,8 @@
 import android.app.Dialog;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.res.TypedArray;
+import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Parcel;
 import android.os.Parcelable;
@@ -31,6 +33,7 @@
 import android.widget.AdapterView;
 import android.widget.ListAdapter;
 import android.widget.ListView;
+import android.widget.TextView;
 
 /**
  * Represents a top-level {@link Preference} that
@@ -91,13 +94,33 @@
     private Dialog mDialog;
 
     private ListView mListView;
-    
+
+    private int mLayoutResId = com.android.internal.R.layout.preference_list_fragment;
+    private Drawable mDividerDrawable;
+    private boolean mDividerSpecified;
+
     /**
      * Do NOT use this constructor, use {@link PreferenceManager#createPreferenceScreen(Context)}.
      * @hide-
      */
     public PreferenceScreen(Context context, AttributeSet attrs) {
         super(context, attrs, com.android.internal.R.attr.preferenceScreenStyle);
+
+        TypedArray a = context.obtainStyledAttributes(null,
+                com.android.internal.R.styleable.PreferenceScreen,
+                com.android.internal.R.attr.preferenceScreenStyle,
+                0);
+
+        mLayoutResId = a.getResourceId(
+                com.android.internal.R.styleable.PreferenceScreen_screenLayout,
+                mLayoutResId);
+        if (a.hasValueOrEmpty(com.android.internal.R.styleable.PreferenceScreen_divider)) {
+            mDividerDrawable =
+                    a.getDrawable(com.android.internal.R.styleable.PreferenceScreen_divider);
+            mDividerSpecified = true;
+        }
+
+        a.recycle();
     }
 
     /**
@@ -163,18 +186,30 @@
 
         LayoutInflater inflater = (LayoutInflater)
                 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-        View childPrefScreen = inflater.inflate(
-                com.android.internal.R.layout.preference_list_fragment, null);
+        View childPrefScreen = inflater.inflate(mLayoutResId, null);
+        View titleView = childPrefScreen.findViewById(android.R.id.title);
         mListView = (ListView) childPrefScreen.findViewById(android.R.id.list);
+        if (mDividerSpecified) {
+            mListView.setDivider(mDividerDrawable);
+        }
+
         bind(mListView);
 
         // Set the title bar if title is available, else no title bar
         final CharSequence title = getTitle();
         Dialog dialog = mDialog = new Dialog(context, context.getThemeResId());
         if (TextUtils.isEmpty(title)) {
+            if (titleView != null) {
+                titleView.setVisibility(View.GONE);
+            }
             dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
         } else {
-            dialog.setTitle(title);
+            if (titleView instanceof TextView) {
+                ((TextView) titleView).setText(title);
+                titleView.setVisibility(View.VISIBLE);
+            } else {
+                dialog.setTitle(title);
+            }
         }
         dialog.setContentView(childPrefScreen);
         dialog.setOnDismissListener(this);
diff --git a/core/java/android/print/PrintServiceRecommendationsLoader.java b/core/java/android/print/PrintServiceRecommendationsLoader.java
index bb5d065..c6a4d51 100644
--- a/core/java/android/print/PrintServiceRecommendationsLoader.java
+++ b/core/java/android/print/PrintServiceRecommendationsLoader.java
@@ -36,7 +36,7 @@
     private final @NonNull PrintManager mPrintManager;
 
     /** Handler to sequentialize the delivery of the results to the main thread */
-    private final Handler mHandler;
+    private final @NonNull Handler mHandler;
 
     /** Listens for updates to the data from the platform */
     private PrintManager.PrintServiceRecommendationsChangeListener mListener;
@@ -90,9 +90,7 @@
             mListener = null;
         }
 
-        if (mHandler != null) {
-            mHandler.removeMessages(0);
-        }
+        mHandler.removeMessages(0);
     }
 
     @Override
diff --git a/core/java/android/print/PrintServicesLoader.java b/core/java/android/print/PrintServicesLoader.java
index 60d7d66..4c9a69a 100644
--- a/core/java/android/print/PrintServicesLoader.java
+++ b/core/java/android/print/PrintServicesLoader.java
@@ -39,7 +39,7 @@
     private final @NonNull PrintManager mPrintManager;
 
     /** Handler to sequentialize the delivery of the results to the main thread */
-    private Handler mHandler;
+    private final @NonNull Handler mHandler;
 
     /** Listens for updates to the data from the platform */
     private PrintManager.PrintServicesChangeListener mListener;
@@ -54,6 +54,7 @@
     public PrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context,
             int selectionFlags) {
         super(Preconditions.checkNotNull(context));
+        mHandler = new MyHandler();
         mPrintManager = Preconditions.checkNotNull(printManager);
         mSelectionFlags = Preconditions.checkFlagsArgument(selectionFlags,
                 PrintManager.ALL_SERVICES);
@@ -75,7 +76,6 @@
 
     @Override
     protected void onStartLoading() {
-        mHandler = new MyHandler();
         mListener = new PrintManager.PrintServicesChangeListener() {
             @Override public void onPrintServicesChanged() {
                 queueNewResult();
@@ -95,10 +95,7 @@
             mListener = null;
         }
 
-        if (mHandler != null) {
-            mHandler.removeMessages(0);
-            mHandler = null;
-        }
+        mHandler.removeMessages(0);
     }
 
     @Override
@@ -119,8 +116,6 @@
 
         @Override
         public void handleMessage(Message msg) {
-            super.handleMessage(msg);
-
             if (isStarted()) {
                 deliverResult((List<PrintServiceInfo>) msg.obj);
             }
diff --git a/core/java/android/provider/CallLog.java b/core/java/android/provider/CallLog.java
index 893eb37..c7c6ceb 100644
--- a/core/java/android/provider/CallLog.java
+++ b/core/java/android/provider/CallLog.java
@@ -179,6 +179,7 @@
          * <li>{@link #VOICEMAIL_TYPE}</li>
          * <li>{@link #REJECTED_TYPE}</li>
          * <li>{@link #BLOCKED_TYPE}</li>
+         * <li>{@link #ANSWERED_EXTERNALLY_TYPE}</li>
          * </ul>
          * </p>
          */
@@ -200,7 +201,6 @@
          * Call log type for a call which was answered on another device.  Used in situations where
          * a call rings on multiple devices simultaneously and it ended up being answered on a
          * device other than the current one.
-         * @hide
          */
         public static final int ANSWERED_EXTERNALLY_TYPE = 7;
 
@@ -214,10 +214,7 @@
         /** Call had video. */
         public static final int FEATURES_VIDEO = 0x1;
 
-        /**
-         * Call was pulled externally.
-         * @hide
-         */
+        /** Call was pulled externally. */
         public static final int FEATURES_PULLED_EXTERNALLY = 0x2;
 
         /**
@@ -287,8 +284,10 @@
 
         /**
          * The cached name associated with the phone number, if it exists.
-         * This value is not guaranteed to be current, if the contact information
-         * associated with this number has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: TEXT</P>
          */
         public static final String CACHED_NAME = "name";
@@ -296,8 +295,10 @@
         /**
          * The cached number type (Home, Work, etc) associated with the
          * phone number, if it exists.
-         * This value is not guaranteed to be current, if the contact information
-         * associated with this number has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: INTEGER</P>
          */
         public static final String CACHED_NUMBER_TYPE = "numbertype";
@@ -305,8 +306,10 @@
         /**
          * The cached number label, for a custom number type, associated with the
          * phone number, if it exists.
-         * This value is not guaranteed to be current, if the contact information
-         * associated with this number has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: TEXT</P>
          */
         public static final String CACHED_NUMBER_LABEL = "numberlabel";
@@ -342,40 +345,50 @@
 
         /**
          * The cached URI to look up the contact associated with the phone number, if it exists.
-         * This value may not be current if the contact information associated with this number
-         * has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: TEXT</P>
          */
         public static final String CACHED_LOOKUP_URI = "lookup_uri";
 
         /**
          * The cached phone number of the contact which matches this entry, if it exists.
-         * This value may not be current if the contact information associated with this number
-         * has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: TEXT</P>
          */
         public static final String CACHED_MATCHED_NUMBER = "matched_number";
 
         /**
          * The cached normalized(E164) version of the phone number, if it exists.
-         * This value may not be current if the contact information associated with this number
-         * has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: TEXT</P>
          */
         public static final String CACHED_NORMALIZED_NUMBER = "normalized_number";
 
         /**
          * The cached photo id of the picture associated with the phone number, if it exists.
-         * This value may not be current if the contact information associated with this number
-         * has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: INTEGER (long)</P>
          */
         public static final String CACHED_PHOTO_ID = "photo_id";
 
         /**
          * The cached photo URI of the picture associated with the phone number, if it exists.
-         * This value may not be current if the contact information associated with this number
-         * has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: TEXT (URI)</P>
          */
         public static final String CACHED_PHOTO_URI = "photo_uri";
@@ -383,9 +396,10 @@
         /**
          * The cached phone number, formatted with formatting rules based on the country the
          * user was in when the call was made or received.
-         * This value is not guaranteed to be present, and may not be current if the contact
-         * information associated with this number
-         * has changed.
+         *
+         * <p>This value is typically filled in by the dialer app for the caching purpose,
+         * so it's not guaranteed to be present, and may not be current if the contact
+         * information associated with this number has changed.
          * <P>Type: TEXT</P>
          */
         public static final String CACHED_FORMATTED_NUMBER = "formatted_number";
diff --git a/core/java/android/provider/ContactsContract.java b/core/java/android/provider/ContactsContract.java
index c70304e..c495e6c 100644
--- a/core/java/android/provider/ContactsContract.java
+++ b/core/java/android/provider/ContactsContract.java
@@ -35,6 +35,7 @@
 import android.content.res.AssetFileDescriptor;
 import android.content.res.Resources;
 import android.database.Cursor;
+import android.database.CursorWrapper;
 import android.database.DatabaseUtils;
 import android.graphics.Rect;
 import android.net.Uri;
@@ -138,8 +139,20 @@
     public static final String DIRECTORY_PARAM_KEY = "directory";
 
     /**
-     * A query parameter that limits the number of results returned. The
+     * A query parameter that limits the number of results returned for supported URIs. The
      * parameter value should be an integer.
+     *
+     * <p>This parameter is not supported by all URIs.  Supported URIs include, but not limited to,
+     * {@link Contacts#CONTENT_URI},
+     * {@link RawContacts#CONTENT_URI},
+     * {@link Data#CONTENT_URI},
+     * {@link CommonDataKinds.Phone#CONTENT_URI},
+     * {@link CommonDataKinds.Callable#CONTENT_URI},
+     * {@link CommonDataKinds.Email#CONTENT_URI},
+     * {@link CommonDataKinds.Contactables#CONTENT_URI},
+     *
+     * <p>In order to limit the number of rows returned by a non-supported URI, you can implement a
+     * {@link CursorWrapper} and override the {@link CursorWrapper#getCount()} methods.
      */
     public static final String LIMIT_PARAM_KEY = "limit";
 
@@ -437,6 +450,9 @@
 
         /**
          * _ID of the default directory, which represents locally stored contacts.
+         * <b>This is only supported by {@link ContactsContract.Contacts#CONTENT_URI} and
+         * {@link ContactsContract.Contacts#CONTENT_FILTER_URI}.
+         * Other URLs do not support the concept of "visible" or "invisible" contacts.
          */
         public static final long DEFAULT = 0;
 
diff --git a/core/java/android/provider/DocumentsContract.java b/core/java/android/provider/DocumentsContract.java
index 1158776..d587ba8 100644
--- a/core/java/android/provider/DocumentsContract.java
+++ b/core/java/android/provider/DocumentsContract.java
@@ -235,7 +235,6 @@
          * @see #FLAG_DIR_PREFERS_GRID
          * @see #FLAG_DIR_PREFERS_LAST_MODIFIED
          * @see #FLAG_VIRTUAL_DOCUMENT
-         * @see #FLAG_ARCHIVE
          * @see #FLAG_SUPPORTS_COPY
          * @see #FLAG_SUPPORTS_MOVE
          * @see #FLAG_SUPPORTS_REMOVE
@@ -326,7 +325,7 @@
          * Flag indicating that a document can be renamed.
          *
          * @see #COLUMN_FLAGS
-         * @see DocumentsContract#renameDocument(ContentProviderClient, Uri,
+         * @see DocumentsContract#renameDocument(ContentResolver, Uri,
          *      String)
          * @see DocumentsProvider#renameDocument(String, String)
          */
@@ -337,7 +336,7 @@
          * within the same document provider.
          *
          * @see #COLUMN_FLAGS
-         * @see DocumentsContract#copyDocument(ContentProviderClient, Uri, Uri)
+         * @see DocumentsContract#copyDocument(ContentResolver, Uri, Uri)
          * @see DocumentsProvider#copyDocument(String, String)
          */
         public static final int FLAG_SUPPORTS_COPY = 1 << 7;
@@ -347,7 +346,7 @@
          * within the same document provider.
          *
          * @see #COLUMN_FLAGS
-         * @see DocumentsContract#moveDocument(ContentProviderClient, Uri, Uri, Uri)
+         * @see DocumentsContract#moveDocument(ContentResolver, Uri, Uri, Uri)
          * @see DocumentsProvider#moveDocument(String, String, String)
          */
         public static final int FLAG_SUPPORTS_MOVE = 1 << 8;
@@ -368,7 +367,7 @@
          * Flag indicating that a document can be removed from a parent.
          *
          * @see #COLUMN_FLAGS
-         * @see DocumentsContract#removeDocument(ContentProviderClient, Uri, Uri)
+         * @see DocumentsContract#removeDocument(ContentResolver, Uri, Uri)
          * @see DocumentsProvider#removeDocument(String, String)
          */
         public static final int FLAG_SUPPORTS_REMOVE = 1 << 10;
@@ -870,7 +869,7 @@
      * Test if the given URI represents a {@link Document} tree.
      *
      * @see #buildTreeDocumentUri(String, String)
-     * @see #getTreeDocumentId(Uri, String)
+     * @see #getTreeDocumentId(Uri)
      */
     public static boolean isTreeUri(Uri uri) {
         final List<String> paths = uri.getPathSegments();
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index ac2419e..37048b9 100755
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -44,7 +44,6 @@
 import android.net.Uri;
 import android.net.wifi.WifiManager;
 import android.os.BatteryManager;
-import android.os.Binder;
 import android.os.Bundle;
 import android.os.DropBoxManager;
 import android.os.IBinder;
@@ -440,6 +439,22 @@
             "android.settings.DISPLAY_SETTINGS";
 
     /**
+     * Activity Action: Show settings to allow configuration of Night display.
+     * <p>
+     * In some cases, a matching Activity may not exist, so ensure you
+     * safeguard against this.
+     * <p>
+     * Input: Nothing.
+     * <p>
+     * Output: Nothing.
+     *
+     * @hide
+     */
+    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
+    public static final String ACTION_NIGHT_DISPLAY_SETTINGS =
+            "android.settings.NIGHT_DISPLAY_SETTINGS";
+
+    /**
      * Activity Action: Show settings to allow configuration of locale.
      * <p>
      * In some cases, a matching Activity may not exist, so ensure you
@@ -1268,6 +1283,19 @@
             = "android.settings.VR_LISTENER_SETTINGS";
 
     /**
+     * Activity Action: Show Storage Manager settings.
+     * <p>
+     * Input: Nothing.
+     * <p>
+     * Output: Nothing.
+     *
+     * @hide
+     */
+    @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
+    public static final String ACTION_STORAGE_MANAGER_SETTINGS
+            = "android.settings.STORAGE_MANAGER_SETTINGS";
+
+    /**
      * Activity Action: Allows user to select current webview implementation.
      * <p>
      * Input: Nothing.
@@ -5295,15 +5323,6 @@
                 "accessibility_display_daltonizer";
 
         /**
-         * Float list that specifies the color matrix to apply to
-         * the display. Valid values are defined in AccessibilityManager.
-         *
-         * @hide
-         */
-        public static final String ACCESSIBILITY_DISPLAY_COLOR_MATRIX =
-                "accessibility_display_color_matrix";
-
-        /**
          * Setting that specifies whether automatic click when the mouse pointer stops moving is
          * enabled.
          *
@@ -5338,6 +5357,13 @@
         public static final String LONG_PRESS_TIMEOUT = "long_press_timeout";
 
         /**
+         * The duration in milliseconds between the first tap's up event and the second tap's
+         * down event for an interaction to be considered part of the same multi-press.
+         * @hide
+         */
+        public static final String MULTI_PRESS_TIMEOUT = "multi_press_timeout";
+
+        /**
          * List of the enabled print services.
          *
          * N and beyond uses {@link #DISABLED_PRINT_SERVICES}. But this might be used in an upgrade
@@ -5832,6 +5858,8 @@
         /**
          * If nonzero, ANRs in invisible background processes bring up a dialog.
          * Otherwise, the process will be silently killed.
+         *
+         * Also prevents ANRs and crash dialogs from being suppressed.
          * @hide
          */
         public static final String ANR_SHOW_BACKGROUND = "anr_show_background";
@@ -5908,6 +5936,36 @@
                 INCALL_POWER_BUTTON_BEHAVIOR_SCREEN_OFF;
 
         /**
+         * What happens when the user presses the Back button while in-call
+         * and the screen is on.<br/>
+         * <b>Values:</b><br/>
+         * 0 - The Back buttons does nothing different.<br/>
+         * 1 - The Back button hangs up the current call.<br/>
+         *
+         * @hide
+         */
+        public static final String INCALL_BACK_BUTTON_BEHAVIOR = "incall_back_button_behavior";
+
+        /**
+         * INCALL_BACK_BUTTON_BEHAVIOR value for no action.
+         * @hide
+         */
+        public static final int INCALL_BACK_BUTTON_BEHAVIOR_NONE = 0x0;
+
+        /**
+         * INCALL_BACK_BUTTON_BEHAVIOR value for "hang up".
+         * @hide
+         */
+        public static final int INCALL_BACK_BUTTON_BEHAVIOR_HANGUP = 0x1;
+
+        /**
+         * INCALL_POWER_BUTTON_BEHAVIOR default value.
+         * @hide
+         */
+        public static final int INCALL_BACK_BUTTON_BEHAVIOR_DEFAULT =
+                INCALL_BACK_BUTTON_BEHAVIOR_NONE;
+
+        /**
          * Whether the device should wake when the wake gesture sensor detects motion.
          * @hide
          */
@@ -5920,6 +5978,18 @@
         public static final String DOZE_ENABLED = "doze_enabled";
 
         /**
+         * Whether the device should pulse on pick up gesture.
+         * @hide
+         */
+        public static final String DOZE_PULSE_ON_PICK_UP = "doze_pulse_on_pick_up";
+
+        /**
+         * Whether the device should pulse on double tap gesture.
+         * @hide
+         */
+        public static final String DOZE_PULSE_ON_DOUBLE_TAP = "doze_pulse_on_double_tap";
+
+        /**
          * The current night mode that has been selected by the user.  Owned
          * and controlled by UiModeManagerService.  Constants are as per
          * UiModeManager.
@@ -6012,6 +6082,17 @@
         public static final String ASSIST_SCREENSHOT_ENABLED = "assist_screenshot_enabled";
 
         /**
+         * Specifies whether the screen will show an animation if screen contents are sent to the
+         * assist application (active voice interaction service).
+         *
+         * Note that the disclosure will be forced for third-party assistants or if the device
+         * does not support disabling it.
+         *
+         * @hide
+         */
+        public static final String ASSIST_DISCLOSURE_ENABLED = "assist_disclosure_enabled";
+
+        /**
          * Names of the service components that the current user has explicitly allowed to
          * see all of the user's notifications, separated by ':'.
          *
@@ -6147,44 +6228,39 @@
                 "camera_double_tap_power_gesture_disabled";
 
         /**
-
-        /**
-         * Behavior of twilight on the device.
-         * One of {@link #TWILIGHT_MODE_LOCKED_OFF}, {@link #TWILIGHT_MODE_LOCKED_ON}
-         * or {@link #TWILIGHT_MODE_AUTO}.
+         * Whether the camera double twist gesture to flip between front and back mode should be
+         * enabled.
+         *
          * @hide
          */
-        public static final String TWILIGHT_MODE = "twilight_mode";
+        public static final String CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED =
+                "camera_double_twist_to_flip_enabled";
 
         /**
-         * Twilight mode always off.
+         * Control whether Night display is currently activated.
          * @hide
          */
-        public static final int TWILIGHT_MODE_LOCKED_OFF = 0;
+        public static final String NIGHT_DISPLAY_ACTIVATED = "night_display_activated";
 
         /**
-         * Twilight mode always on.
+         * Control whether Night display will automatically activate/deactivate.
          * @hide
          */
-        public static final int TWILIGHT_MODE_LOCKED_ON = 1;
+        public static final String NIGHT_DISPLAY_AUTO_MODE = "night_display_auto_mode";
 
         /**
-         * Twilight mode auto.
+         * Custom time when Night display is scheduled to activate.
+         * Represented as milliseconds from midnight (e.g. 79200000 == 10pm).
          * @hide
          */
-        public static final int TWILIGHT_MODE_AUTO = 2;
+        public static final String NIGHT_DISPLAY_CUSTOM_START_TIME = "night_display_custom_start_time";
 
         /**
-         * Twilight mode auto, temporarily overriden to on.
+         * Custom time when Night display is scheduled to deactivate.
+         * Represented as milliseconds from midnight (e.g. 21600000 == 6am).
          * @hide
          */
-        public static final int TWILIGHT_MODE_AUTO_OVERRIDE_OFF = 3;
-
-        /**
-         * Twilight mode auto, temporarily overriden to off.
-         * @hide
-         */
-        public static final int TWILIGHT_MODE_AUTO_OVERRIDE_ON = 4;
+        public static final String NIGHT_DISPLAY_CUSTOM_END_TIME = "night_display_custom_end_time";
 
         /**
          * Whether brightness should automatically adjust based on twilight state.
@@ -6228,6 +6304,20 @@
         public static final int VR_DISPLAY_MODE_OFF = 1;
 
         /**
+         * Whether CarrierAppUtils#disableCarrierAppsUntilPrivileged has been executed at least
+         * once.
+         *
+         * <p>This is used to ensure that we only take one pass which will disable apps that are not
+         * privileged (if any). From then on, we only want to enable apps (when a matching SIM is
+         * inserted), to avoid disabling an app that the user might actively be using.
+         *
+         * <p>Will be set to 1 once executed.
+         *
+         * @hide
+         */
+        public static final String CARRIER_APPS_HANDLED = "carrier_apps_handled";
+
+        /**
          * Whether parent user can access remote contact in managed profile.
          *
          * @hide
@@ -6236,12 +6326,74 @@
                 "managed_profile_contact_remote_search";
 
         /**
+         * Whether or not the automatic storage manager is enabled and should run on the device.
+         *
+         * @hide
+         */
+        public static final String AUTOMATIC_STORAGE_MANAGER_ENABLED =
+                "automatic_storage_manager_enabled";
+
+        /**
+         * How many days of information for the automatic storage manager to retain on the device.
+         *
+         * @hide
+         */
+        public static final String AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN =
+                "automatic_storage_manager_days_to_retain";
+
+        /**
+         * Default number of days of information for the automatic storage manager to retain.
+         *
+         * @hide
+         */
+        public static final int AUTOMATIC_STORAGE_MANAGER_DAYS_TO_RETAIN_DEFAULT = 90;
+
+        /**
+         * How many bytes the automatic storage manager has cleared out.
+         *
+         * @hide
+         */
+        public static final String AUTOMATIC_STORAGE_MANAGER_BYTES_CLEARED =
+                "automatic_storage_manager_bytes_cleared";
+
+
+        /**
+         * Last run time for the automatic storage manager.
+         *
+         * @hide
+         */
+        public static final String AUTOMATIC_STORAGE_MANAGER_LAST_RUN =
+                "automatic_storage_manager_last_run";
+
+
+        /**
+         * Whether SystemUI navigation keys is enabled.
+         * @hide
+         */
+        public static final String SYSTEM_NAVIGATION_KEYS_ENABLED =
+                "system_navigation_keys_enabled";
+
+        /**
          * Holds comma separated list of ordering of QS tiles.
          * @hide
          */
         public static final String QS_TILES = "sysui_qs_tiles";
 
         /**
+         * Whether preloaded APKs have been installed for the user.
+         * @hide
+         */
+        public static final String DEMO_USER_SETUP_COMPLETE
+                = "demo_user_setup_complete";
+
+        /**
+         * Specifies whether the web action API is enabled.
+         *
+         * @hide
+         */
+        public static final String WEB_ACTION_ENABLED = "web_action_enabled";
+
+        /**
          * This are the settings to be backed up.
          *
          * NOTE: Settings are backed up and restored in the order they appear
@@ -6258,7 +6410,6 @@
             USB_MASS_STORAGE_ENABLED,                           // moved to global
             ACCESSIBILITY_DISPLAY_INVERSION_ENABLED,
             ACCESSIBILITY_DISPLAY_DALTONIZER,
-            ACCESSIBILITY_DISPLAY_COLOR_MATRIX,
             ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED,
             ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
             ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE,
@@ -6315,7 +6466,17 @@
             ENHANCED_VOICE_PRIVACY_ENABLED,
             TTY_MODE_ENABLED,
             INCALL_POWER_BUTTON_BEHAVIOR,
+            NIGHT_DISPLAY_CUSTOM_START_TIME,
+            NIGHT_DISPLAY_CUSTOM_END_TIME,
+            NIGHT_DISPLAY_AUTO_MODE,
+            NIGHT_DISPLAY_ACTIVATED,
+            CAMERA_DOUBLE_TWIST_TO_FLIP_ENABLED,
+            CAMERA_DOUBLE_TAP_POWER_GESTURE_DISABLED,
+            SYSTEM_NAVIGATION_KEYS_ENABLED,
             QS_TILES,
+            DOZE_ENABLED,
+            DOZE_PULSE_ON_PICK_UP,
+            DOZE_PULSE_ON_DOUBLE_TAP
         };
 
         /**
@@ -7317,27 +7478,54 @@
         public static final String WEBVIEW_DATA_REDUCTION_PROXY_KEY =
                 "webview_data_reduction_proxy_key";
 
-        /**
-         * Whether or not the WebView fallback mechanism should be enabled.
-         * 0=disabled, 1=enabled.
-         * @hide
-         */
-        public static final String WEBVIEW_FALLBACK_LOGIC_ENABLED =
-                "webview_fallback_logic_enabled";
+       /**
+        * Whether or not the WebView fallback mechanism should be enabled.
+        * 0=disabled, 1=enabled.
+        * @hide
+        */
+       public static final String WEBVIEW_FALLBACK_LOGIC_ENABLED =
+               "webview_fallback_logic_enabled";
 
-        /**
-         * Name of the package used as WebView provider (if unset the provider is instead determined
-         * by the system).
-         * @hide
-         */
-        public static final String WEBVIEW_PROVIDER = "webview_provider";
+       /**
+        * Name of the package used as WebView provider (if unset the provider is instead determined
+        * by the system).
+        * @hide
+        */
+       public static final String WEBVIEW_PROVIDER = "webview_provider";
 
-        /**
-         * Developer setting to enable WebView multiprocess rendering.
-         * @hide
-         */
-        @SystemApi
-        public static final String WEBVIEW_MULTIPROCESS = "webview_multiprocess";
+       /**
+        * Developer setting to enable WebView multiprocess rendering.
+        * @hide
+        */
+       @SystemApi
+       public static final String WEBVIEW_MULTIPROCESS = "webview_multiprocess";
+
+       /**
+        * The maximum number of notifications shown in 24 hours when switching networks.
+        * @hide
+        */
+       public static final String NETWORK_SWITCH_NOTIFICATION_DAILY_LIMIT =
+              "network_switch_notification_daily_limit";
+
+       /**
+        * The minimum time in milliseconds between notifications when switching networks.
+        * @hide
+        */
+       public static final String NETWORK_SWITCH_NOTIFICATION_RATE_LIMIT_MILLIS =
+              "network_switch_notification_rate_limit_millis";
+
+       /**
+        * Whether to automatically switch away from wifi networks that lose Internet access.
+        * Only meaningful if config_networkAvoidBadWifi is set to 0, otherwise the system always
+        * avoids such networks. Valid values are:
+        *
+        * 0: Don't avoid bad wifi, don't prompt the user. Get stuck on bad wifi like it's 2013.
+        * null: Ask the user whether to switch away from bad wifi.
+        * 1: Avoid bad wifi.
+        *
+        * @hide
+        */
+       public static final String NETWORK_AVOID_BAD_WIFI = "network_avoid_bad_wifi";
 
        /**
         * Whether Wifi display is enabled/disabled
@@ -7809,12 +7997,37 @@
         /**
          * The server used for captive portal detection upon a new conection. A
          * 204 response code from the server is used for validation.
+         * TODO: remove this deprecated symbol.
          *
          * @hide
          */
         public static final String CAPTIVE_PORTAL_SERVER = "captive_portal_server";
 
         /**
+         * The URL used for HTTPS captive portal detection upon a new connection.
+         * A 204 response code from the server is used for validation.
+         *
+         * @hide
+         */
+        public static final String CAPTIVE_PORTAL_HTTPS_URL = "captive_portal_https_url";
+
+        /**
+         * The URL used for HTTP captive portal detection upon a new connection.
+         * A 204 response code from the server is used for validation.
+         *
+         * @hide
+         */
+        public static final String CAPTIVE_PORTAL_HTTP_URL = "captive_portal_http_url";
+
+        /**
+         * The URL used for fallback HTTP captive portal detection when previous HTTP
+         * and HTTPS captive portal detection attemps did not return a conclusive answer.
+         *
+         * @hide
+         */
+        public static final String CAPTIVE_PORTAL_FALLBACK_URL = "captive_portal_fallback_url";
+
+        /**
          * Whether to use HTTPS for network validation. This is enabled by default and the setting
          * needs to be set to 0 to disable it. This setting is a misnomer because captive portals
          * don't actually use HTTPS, but it's consistent with the other settings.
@@ -7824,6 +8037,14 @@
         public static final String CAPTIVE_PORTAL_USE_HTTPS = "captive_portal_use_https";
 
         /**
+         * Which User-Agent string to use in the header of the captive portal detection probes.
+         * The User-Agent field is unset when this setting has no value (HttpUrlConnection default).
+         *
+         * @hide
+         */
+        public static final String CAPTIVE_PORTAL_USER_AGENT = "captive_portal_user_agent";
+
+        /**
          * Whether network service discovery is enabled.
          *
          * @hide
@@ -8196,6 +8417,13 @@
         public static final String CALL_AUTO_RETRY = "call_auto_retry";
 
         /**
+         * A setting that can be read whether the emergency affordance is currently needed.
+         * The value is a boolean (1 or 0).
+         * @hide
+         */
+        public static final String EMERGENCY_AFFORDANCE_NEEDED = "emergency_affordance_needed";
+
+        /**
          * See RIL_PreferredNetworkType in ril.h
          * @hide
          */
@@ -8480,8 +8708,6 @@
 
         /**
          * The name of the device
-         *
-         * @hide
          */
         public static final String DEVICE_NAME = "device_name";
 
@@ -8529,7 +8755,7 @@
         public static final String WFC_IMS_ENABLED = "wfc_ims_enabled";
 
         /**
-         * WFC Mode.
+         * WFC mode on home/non-roaming network.
          * <p>
          * Type: int - 2=Wi-Fi preferred, 1=Cellular preferred, 0=Wi-Fi only
          *
@@ -8538,6 +8764,15 @@
         public static final String WFC_IMS_MODE = "wfc_ims_mode";
 
         /**
+         * WFC mode on roaming network.
+         * <p>
+         * Type: int - see {@link WFC_IMS_MODE} for values
+         *
+         * @hide
+         */
+        public static final String WFC_IMS_ROAMING_MODE = "wfc_ims_roaming_mode";
+
+        /**
          * Whether WFC roaming is enabled
          * <p>
          * Type: int (0 for false, 1 for true)
@@ -8565,6 +8800,34 @@
                 "ephemeral_cookie_max_size_bytes";
 
         /**
+         * Toggle to enable/disable the entire ephemeral feature. By default, ephemeral is
+         * enabled. Set to zero to disable.
+         * <p>
+         * Type: int (0 for false, 1 for true)
+         *
+         * @hide
+         */
+        public static final String ENABLE_EPHEMERAL_FEATURE = "enable_ephemeral_feature";
+
+        /**
+         * A mask applied to the ephemeral hash to generate the hash prefix.
+         * <p>
+         * Type: int
+         *
+         * @hide
+         */
+        public static final String EPHEMERAL_HASH_PREFIX_MASK = "ephemeral_hash_prefix_mask";
+
+        /**
+         * Number of hash prefixes to send during ephemeral resolution.
+         * <p>
+         * Type: int
+         *
+         * @hide
+         */
+        public static final String EPHEMERAL_HASH_PREFIX_COUNT = "ephemeral_hash_prefix_count";
+
+        /**
          * The duration for caching uninstalled ephemeral apps.
          * <p>
          * Type: long
@@ -8600,6 +8863,41 @@
         public static final String SAFE_BOOT_DISALLOWED = "safe_boot_disallowed";
 
         /**
+         * Whether this device is currently in retail demo mode. If true, device
+         * usage is severely limited.
+         * <p>
+         * Type: int (0 for false, 1 for true)
+         * @hide
+         */
+        public static final String DEVICE_DEMO_MODE = "device_demo_mode";
+
+        /**
+         * Retail mode specific settings. This is encoded as a key=value list, separated by commas.
+         * Ex: "user_inactivity_timeout_ms=30000,warning_dialog_timeout_ms=10000". The following
+         * keys are supported:
+         *
+         * <pre>
+         * user_inactivity_timeout_ms  (long)
+         * warning_dialog_timeout_ms   (long)
+         * </pre>
+         * <p>
+         * Type: string
+         *
+         * @hide
+         */
+        public static final String RETAIL_DEMO_MODE_CONSTANTS = "retail_demo_mode_constants";
+
+        /**
+         * The reason for the settings database being downgraded. This is only for
+         * troubleshooting purposes and its value should not be interpreted in any way.
+         *
+         * Type: string
+         *
+         * @hide
+         */
+        public static final String DATABASE_DOWNGRADE_REASON = "database_downgrade_reason";
+
+        /**
          * Settings to backup. This is here so that it's in the same place as the settings
          * keys and easy to update.
          *
@@ -9004,6 +9302,12 @@
          * @hide
          */
         public static final String MAX_NOTIFICATION_ENQUEUE_RATE = "max_notification_enqueue_rate";
+
+        /**
+         * Whether cell is enabled/disabled
+         * @hide
+         */
+        public static final String CELL_ON = "cell_on";
     }
 
     /**
diff --git a/core/java/android/provider/VoicemailContract.java b/core/java/android/provider/VoicemailContract.java
index 6a3cc02..5099eeb 100644
--- a/core/java/android/provider/VoicemailContract.java
+++ b/core/java/android/provider/VoicemailContract.java
@@ -25,13 +25,11 @@
 import android.content.Context;
 import android.content.Intent;
 import android.database.ContentObserver;
-import android.database.Cursor;
 import android.net.Uri;
 import android.provider.CallLog.Calls;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.Voicemail;
-
 import java.util.List;
 
 /**
@@ -107,12 +105,74 @@
     public static final String ACTION_SYNC_VOICEMAIL = "android.provider.action.SYNC_VOICEMAIL";
 
     /**
+     * Broadcast intent to inform a new visual voicemail SMS has been received. This intent will
+     * only be delivered to the voicemail client. The intent will have the following extra values:
+     *
+     * <ul>
+     *   <li><em>{@link #EXTRA_VOICEMAIL_SMS_TYPE}</em> - (String) The event type of the SMS. Common
+     *   values are "SYNC" or "STATUS"</li>
+     *   <li><em>{@link #EXTRA_VOICEMAIL_SMS_DATA}</em> - (Bundle) The fields sent by the SMS</li>
+     *   <li><em>{@link #EXTRA_VOICEMAIL_SMS_SUBID}</em> - (Integer) The subscription ID of the
+     *   phone account that received the SMS</li>
+     * </ul>
+     */
+    /** @hide */
+    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
+    public static final String ACTION_VOICEMAIL_SMS_RECEIVED =
+            "android.intent.action.VOICEMAIL_SMS_RECEIVED";
+
+    /**
+     * Optional extra included in {@link #ACTION_VOICEMAIL_SMS_RECEIVED} broadcast intents to
+     * indicate the event type of the SMS. Common values are "SYNC" or "STATUS". The extra will not
+     * exist if the framework cannot parse the SMS as voicemail but the carrier pattern indicates
+     * it is.
+     */
+    /** @hide */
+    public static final String EXTRA_VOICEMAIL_SMS_PREFIX =
+            "com.android.voicemail.extra.VOICEMAIL_SMS_PREFIX";
+
+    /**
+     * Optional extra included in {@link #ACTION_VOICEMAIL_SMS_RECEIVED} broadcast intents to
+     * indicate the fields sent by the SMS. The extra will not exist if the framework cannot
+     * parse the SMS as voicemail but the carrier pattern indicates it is.
+     */
+    /** @hide */
+    public static final String EXTRA_VOICEMAIL_SMS_FIELDS =
+            "com.android.voicemail.extra.VOICEMAIL_SMS_FIELDS";
+
+    /**
+     * Extra included in {@link #ACTION_VOICEMAIL_SMS_RECEIVED} broadcast intents to indicate the
+     * message body of the SMS. This extra is included if the framework cannot
+     * parse the SMS as voicemail but the carrier pattern indicates it is.
+     */
+    /**
+     * @hide
+     */
+    public static final String EXTRA_VOICEMAIL_SMS_MESSAGE_BODY =
+        "com.android.voicemail.extra.VOICEMAIL_SMS_MESSAGE_BODY";
+
+    /**
+     * Extra included in {@link #ACTION_VOICEMAIL_SMS_RECEIVED} broadcast intents to indicate he
+     * subscription ID of the phone account that received the SMS.
+     */
+    /** @hide */
+    public static final String EXTRA_VOICEMAIL_SMS_SUBID =
+            "com.android.voicemail.extra.VOICEMAIL_SMS_SUBID";
+
+    /**
      * Extra included in {@link Intent#ACTION_PROVIDER_CHANGED} broadcast intents to indicate if the
      * receiving package made this change.
      */
     public static final String EXTRA_SELF_CHANGE = "com.android.voicemail.extra.SELF_CHANGE";
 
     /**
+     * Extra included in {@link #ACTION_SYNC_VOICEMAIL} broadcast intents to indicate which {@link
+     * PhoneAccountHandle} to sync.
+     */
+    public static final String EXTRA_PHONE_ACCOUNT_HANDLE =
+            "android.provider.extra.PHONE_ACCOUNT_HANDLE";
+
+    /**
      * Name of the source package field, which must be same across all voicemail related tables.
      * This is an internal field.
      * @hide
@@ -360,6 +420,20 @@
          */
         public static final String SOURCE_PACKAGE = SOURCE_PACKAGE_FIELD;
 
+        /**
+         * The type of the source, which determines how to interpret source-specific states.
+         * Typically this will be set to the same string as
+         * {@link android.telephony.CarrierConfigManager#KEY_VVM_TYPE_STRING}. For example,
+         * "vvm_type_omtp".
+         *
+         * <P>Type: TEXT</P>
+         *
+         * @see #CONFIGURATION_STATE
+         * @see #DATA_CHANNEL_STATE
+         * @see #NOTIFICATION_CHANNEL_STATE
+         */
+        public static final String SOURCE_TYPE = "source_type";
+
         // Note: Multiple entries may exist for a single source if they are differentiated by the
         // PHONE_ACCOUNT_* fields.
 
@@ -392,21 +466,21 @@
         public static final String VOICEMAIL_ACCESS_URI = "voicemail_access_uri";
         /**
          * The configuration state of the voicemail source.
+         *
+         * <P>Negative values are reserved to the source for source-specific states, see
+         * {@link #SOURCE_TYPE}
+         *
          * <P> Possible values:
          * {@link #CONFIGURATION_STATE_OK},
          * {@link #CONFIGURATION_STATE_NOT_CONFIGURED},
          * {@link #CONFIGURATION_STATE_CAN_BE_CONFIGURED}
+         * {@link #CONFIGURATION_STATE_CONFIGURING}
+         * {@link #CONFIGURATION_STATE_FAILED}
+         * {@link #CONFIGURATION_STATE_DISABLED}
          * <P>Type: INTEGER</P>
          */
         public static final String CONFIGURATION_STATE = "configuration_state";
-        /**
-         * Value of {@link #CONFIGURATION_STATE} passed into
-         * {@link #setStatus(Context, PhoneAccountHandle, int, int, int)} to indicate that the
-         * {@link #CONFIGURATION_STATE} field is not to be changed
-         *
-         * @hide
-         */
-        public static final int CONFIGURATION_STATE_IGNORE = -1;
+
         /** Value of {@link #CONFIGURATION_STATE} to indicate an all OK configuration status. */
         public static final int CONFIGURATION_STATE_OK = 0;
         /**
@@ -422,8 +496,27 @@
          */
         public static final int CONFIGURATION_STATE_CAN_BE_CONFIGURED = 2;
         /**
+         * Value of {@link #CONFIGURATION_STATE} to indicate that visual voicemail still is being
+         * configured.
+         */
+        public static final int CONFIGURATION_STATE_CONFIGURING = 3;
+        /**
+         * Value of {@link #CONFIGURATION_STATE} to indicate that visual voicemail has failed to
+         * be configured.
+         */
+        public static final int CONFIGURATION_STATE_FAILED = 4;
+        /**
+         * Value of {@link #CONFIGURATION_STATE} to indicate that visual voicemail is disabled by
+         * the user.
+         */
+        public static final int CONFIGURATION_STATE_DISABLED = 5;
+        /**
          * The data channel state of the voicemail source. This the channel through which the source
          * pulls voicemail data from a remote server.
+         *
+         * <P>Negative values are reserved to the source for source-specific states, see
+         * {@link #SOURCE_TYPE}
+         *
          * <P> Possible values:
          * {@link #DATA_CHANNEL_STATE_OK},
          * {@link #DATA_CHANNEL_STATE_NO_CONNECTION}
@@ -431,14 +524,7 @@
          * <P>Type: INTEGER</P>
          */
         public static final String DATA_CHANNEL_STATE = "data_channel_state";
-        /**
-         * Value of {@link #DATA_CHANNEL_STATE} passed into
-         * {@link #setStatus(Context, PhoneAccountHandle, int, int, int)} to indicate that the
-         * {@link #DATA_CHANNEL_STATE} field is not to be changed
-         *
-         * @hide
-         */
-        public static final int DATA_CHANNEL_STATE_IGNORE = -1;
+
         /**
          *  Value of {@link #DATA_CHANNEL_STATE} to indicate that data channel is working fine.
          */
@@ -478,6 +564,10 @@
         /**
          * The notification channel state of the voicemail source. This is the channel through which
          * the source gets notified of new voicemails on the remote server.
+         *
+         * <P>Negative values are reserved to the source for source-specific states, see
+         * {@link #SOURCE_TYPE}
+         *
          * <P> Possible values:
          * {@link #NOTIFICATION_CHANNEL_STATE_OK},
          * {@link #NOTIFICATION_CHANNEL_STATE_NO_CONNECTION},
@@ -486,14 +576,7 @@
          * <P>Type: INTEGER</P>
          */
         public static final String NOTIFICATION_CHANNEL_STATE = "notification_channel_state";
-        /**
-         * Value of {@link #NOTIFICATION_CHANNEL_STATE} passed into
-         * {@link #setStatus(Context, PhoneAccountHandle, int, int, int)} to indicate that the
-         * {@link #NOTIFICATION_CHANNEL_STATE} field is not to be changed
-         *
-         * @hide
-         */
-        public static final int NOTIFICATION_CHANNEL_STATE_IGNORE = -1;
+
         /**
          * Value of {@link #NOTIFICATION_CHANNEL_STATE} to indicate that the notification channel is
          * working fine.
@@ -543,67 +626,5 @@
             return Status.CONTENT_URI.buildUpon()
                     .appendQueryParameter(PARAM_KEY_SOURCE_PACKAGE, packageName).build();
         }
-
-        /**
-         * A helper method to set the status of a voicemail source.
-         *
-         * @param context The context from the package calling the method. This will be the source.
-         * @param accountHandle The handle for the account the source is associated with.
-         * @param configurationState See {@link Status#CONFIGURATION_STATE}
-         * @param dataChannelState See {@link Status#DATA_CHANNEL_STATE}
-         * @param notificationChannelState See {@link Status#NOTIFICATION_CHANNEL_STATE}
-         *
-         * @hide
-         */
-        public static void setStatus(Context context, PhoneAccountHandle accountHandle,
-                int configurationState, int dataChannelState, int notificationChannelState) {
-            ContentValues values = new ContentValues();
-            values.put(Status.PHONE_ACCOUNT_COMPONENT_NAME,
-                    accountHandle.getComponentName().flattenToString());
-            values.put(Status.PHONE_ACCOUNT_ID, accountHandle.getId());
-            if(configurationState != CONFIGURATION_STATE_IGNORE) {
-                values.put(Status.CONFIGURATION_STATE, configurationState);
-            }
-            if(dataChannelState != DATA_CHANNEL_STATE_IGNORE) {
-                values.put(Status.DATA_CHANNEL_STATE, dataChannelState);
-            }
-            if(notificationChannelState != NOTIFICATION_CHANNEL_STATE_IGNORE) {
-                values.put(Status.NOTIFICATION_CHANNEL_STATE, notificationChannelState);
-            }
-            ContentResolver contentResolver = context.getContentResolver();
-            Uri statusUri = buildSourceUri(context.getPackageName());
-            contentResolver.insert(statusUri, values);
-        }
-
-        /**
-         * A helper method to set the quota of a voicemail source. Unit is unspecified.
-         *
-         * @param context The context from the package calling the method. This will be the source.
-         * @param accountHandle The handle for the account the source is associated with.
-         * @param occupied See {@link Status#QUOTA_OCCUPIED}
-         * @param total See {@link Status#QUOTA_TOTAL}
-         *
-         * @hide
-         */
-        public static void setQuota(Context context, PhoneAccountHandle accountHandle, int occupied,
-                int total) {
-            if (occupied == QUOTA_UNAVAILABLE && total == QUOTA_UNAVAILABLE) {
-                return;
-            }
-            ContentValues values = new ContentValues();
-            values.put(Status.PHONE_ACCOUNT_COMPONENT_NAME,
-                    accountHandle.getComponentName().flattenToString());
-            values.put(Status.PHONE_ACCOUNT_ID, accountHandle.getId());
-            if (occupied != QUOTA_UNAVAILABLE) {
-                values.put(Status.QUOTA_OCCUPIED,occupied);
-            }
-            if (total != QUOTA_UNAVAILABLE) {
-                values.put(Status.QUOTA_TOTAL,total);
-            }
-
-            ContentResolver contentResolver = context.getContentResolver();
-            Uri statusUri = buildSourceUri(context.getPackageName());
-            contentResolver.insert(statusUri, values);
-        }
     }
 }
diff --git a/core/java/android/security/IKeystoreService.aidl b/core/java/android/security/IKeystoreService.aidl
index 8689dce..641e1ad 100644
--- a/core/java/android/security/IKeystoreService.aidl
+++ b/core/java/android/security/IKeystoreService.aidl
@@ -76,4 +76,5 @@
     int onUserAdded(int userId, int parentId);
     int onUserRemoved(int userId);
     int attestKey(String alias, in KeymasterArguments params, out KeymasterCertificateChain chain);
+    int onDeviceOffBody();
 }
diff --git a/core/java/android/security/net/config/DirectoryCertificateSource.java b/core/java/android/security/net/config/DirectoryCertificateSource.java
index e3c9d65..119f5d0 100644
--- a/core/java/android/security/net/config/DirectoryCertificateSource.java
+++ b/core/java/android/security/net/config/DirectoryCertificateSource.java
@@ -19,6 +19,7 @@
 import android.os.Environment;
 import android.os.UserHandle;
 import android.util.ArraySet;
+import android.util.Log;
 import android.util.Pair;
 import java.io.BufferedInputStream;
 import java.io.File;
@@ -44,6 +45,7 @@
  * @hide
  */
 abstract class DirectoryCertificateSource implements CertificateSource {
+    private static final String LOG_TAG = "DirectoryCertificateSrc";
     private final File mDir;
     private final Object mLock = new Object();
     private final CertificateFactory mCertFactory;
@@ -149,6 +151,9 @@
                 continue;
             }
             X509Certificate cert = readCertificate(fileName);
+            if (cert == null) {
+                continue;
+            }
             if (!subj.equals(cert.getSubjectX500Principal())) {
                 continue;
             }
@@ -173,6 +178,9 @@
                 continue;
             }
             X509Certificate cert = readCertificate(fileName);
+            if (cert == null) {
+                continue;
+            }
             if (!subj.equals(cert.getSubjectX500Principal())) {
                 continue;
             }
@@ -194,6 +202,7 @@
             is = new BufferedInputStream(new FileInputStream(new File(mDir, file)));
             return (X509Certificate) mCertFactory.generateCertificate(is);
         } catch (CertificateException | IOException e) {
+            Log.e(LOG_TAG, "Failed to read certificate from " + file, e);
             return null;
         } finally {
             IoUtils.closeQuietly(is);
diff --git a/core/java/android/service/carrier/CarrierIdentifier.java b/core/java/android/service/carrier/CarrierIdentifier.java
index a70c24d..b47e872 100644
--- a/core/java/android/service/carrier/CarrierIdentifier.java
+++ b/core/java/android/service/carrier/CarrierIdentifier.java
@@ -126,4 +126,13 @@
         mGid1 = in.readString();
         mGid2 = in.readString();
     }
+
+    /** @hide */
+    public interface MatchType {
+        int ALL = 0;
+        int SPN = 1;
+        int IMSI_PREFIX = 2;
+        int GID1 = 3;
+        int GID2 = 4;
+    }
 }
diff --git a/core/java/android/service/dreams/DreamService.java b/core/java/android/service/dreams/DreamService.java
index 0557d13..94505d3 100644
--- a/core/java/android/service/dreams/DreamService.java
+++ b/core/java/android/service/dreams/DreamService.java
@@ -27,6 +27,7 @@
 import android.graphics.drawable.ColorDrawable;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.IRemoteCallback;
 import android.os.PowerManager;
 import android.os.RemoteException;
 import android.os.ServiceManager;
@@ -942,8 +943,9 @@
      * Must run on mHandler.
      *
      * @param windowToken A window token that will allow a window to be created in the correct layer.
+     * @param started A callback that will be invoked once onDreamingStarted has completed.
      */
-    private final void attach(IBinder windowToken, boolean canDoze) {
+    private final void attach(IBinder windowToken, boolean canDoze, IRemoteCallback started) {
         if (mWindowToken != null) {
             Slog.e(TAG, "attach() called when already attached with token=" + mWindowToken);
             return;
@@ -1017,7 +1019,15 @@
                 if (mWindow != null || mWindowless) {
                     if (mDebug) Slog.v(TAG, "Calling onDreamingStarted()");
                     mStarted = true;
-                    onDreamingStarted();
+                    try {
+                        onDreamingStarted();
+                    } finally {
+                        try {
+                            started.sendResult(null);
+                        } catch (RemoteException e) {
+                            throw e.rethrowFromSystemServer();
+                        }
+                    }
                 }
             }
         });
@@ -1092,11 +1102,12 @@
 
     private final class DreamServiceWrapper extends IDreamService.Stub {
         @Override
-        public void attach(final IBinder windowToken, final boolean canDoze) {
+        public void attach(final IBinder windowToken, final boolean canDoze,
+                IRemoteCallback started) {
             mHandler.post(new Runnable() {
                 @Override
                 public void run() {
-                    DreamService.this.attach(windowToken, canDoze);
+                    DreamService.this.attach(windowToken, canDoze, started);
                 }
             });
         }
diff --git a/core/java/android/service/dreams/IDreamService.aidl b/core/java/android/service/dreams/IDreamService.aidl
index 9bb1804..ce04354 100644
--- a/core/java/android/service/dreams/IDreamService.aidl
+++ b/core/java/android/service/dreams/IDreamService.aidl
@@ -16,11 +16,13 @@
 
 package android.service.dreams;
 
+import android.os.IRemoteCallback;
+
 /**
  * @hide
  */
 oneway interface IDreamService {
-    void attach(IBinder windowToken, boolean canDoze);
+    void attach(IBinder windowToken, boolean canDoze, IRemoteCallback started);
     void detach();
     void wakeUp();
 }
diff --git a/core/java/android/service/notification/ZenModeConfig.java b/core/java/android/service/notification/ZenModeConfig.java
index 1d9784b..474a9b6 100644
--- a/core/java/android/service/notification/ZenModeConfig.java
+++ b/core/java/android/service/notification/ZenModeConfig.java
@@ -20,6 +20,8 @@
 import android.app.NotificationManager.Policy;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.content.res.Resources;
 import android.net.Uri;
 import android.os.Parcel;
@@ -42,6 +44,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
+import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.Locale;
 import java.util.Objects;
@@ -118,6 +121,7 @@
     private static final String RULE_ATT_ZEN = "zen";
     private static final String RULE_ATT_CONDITION_ID = "conditionId";
     private static final String RULE_ATT_CREATION_TIME = "creationTime";
+    private static final String RULE_ATT_ENABLER = "enabler";
 
     public boolean allowCalls = DEFAULT_ALLOW_CALLS;
     public boolean allowRepeatCallers = DEFAULT_ALLOW_REPEAT_CALLERS;
@@ -502,6 +506,7 @@
         rt.conditionId = safeUri(parser, RULE_ATT_CONDITION_ID);
         rt.component = safeComponentName(parser, RULE_ATT_COMPONENT);
         rt.creationTime = safeLong(parser, RULE_ATT_CREATION_TIME, 0);
+        rt.enabler = parser.getAttributeValue(null, RULE_ATT_ENABLER);
         rt.condition = readConditionXml(parser);
         return rt;
     }
@@ -520,6 +525,9 @@
             out.attribute(null, RULE_ATT_CONDITION_ID, rule.conditionId.toString());
         }
         out.attribute(null, RULE_ATT_CREATION_TIME, Long.toString(rule.creationTime));
+        if (rule.enabler != null) {
+            out.attribute(null, RULE_ATT_ENABLER, rule.enabler);
+        }
         if (rule.condition != null) {
             writeConditionXml(rule.condition, out);
         }
@@ -889,9 +897,13 @@
                     ", endHour=" + endHour +
                     ", endMinute=" + endMinute +
                     ", exitAtAlarm=" + exitAtAlarm +
-                    ", nextAlarm=" + nextAlarm +
+                    ", nextAlarm=" + ts(nextAlarm) +
                     '}';
         }
+
+        protected static String ts(long time) {
+            return new Date(time) + " (" + time + ")";
+        }
     }
 
     // ==== Built-in system condition: event ====
@@ -989,6 +1001,25 @@
         return UUID.randomUUID().toString().replace("-", "");
     }
 
+    private static String getOwnerCaption(Context context, String owner) {
+        final PackageManager pm = context.getPackageManager();
+        try {
+            final ApplicationInfo info = pm.getApplicationInfo(owner, 0);
+            if (info != null) {
+                final CharSequence seq = info.loadLabel(pm);
+                if (seq != null) {
+                    final String str = seq.toString().trim();
+                    if (str.length() > 0) {
+                        return str;
+                    }
+                }
+            }
+        } catch (Throwable e) {
+            Slog.w(TAG, "Error loading owner caption", e);
+        }
+        return "";
+    }
+
     public static String getConditionSummary(Context context, ZenModeConfig config,
             int userHandle, boolean shortVersion) {
         return getConditionLine(context, config, userHandle, false /*useLine1*/, shortVersion);
@@ -997,23 +1028,28 @@
     private static String getConditionLine(Context context, ZenModeConfig config,
             int userHandle, boolean useLine1, boolean shortVersion) {
         if (config == null) return "";
+        String summary = "";
         if (config.manualRule != null) {
             final Uri id = config.manualRule.conditionId;
-            if (id == null) {
-                return context.getString(com.android.internal.R.string.zen_mode_forever);
+            if (config.manualRule.enabler != null) {
+                summary = getOwnerCaption(context, config.manualRule.enabler);
+            } else {
+                if (id == null) {
+                    summary = context.getString(com.android.internal.R.string.zen_mode_forever);
+                } else {
+                    final long time = tryParseCountdownConditionId(id);
+                    Condition c = config.manualRule.condition;
+                    if (time > 0) {
+                        final long now = System.currentTimeMillis();
+                        final long span = time - now;
+                        c = toTimeCondition(context, time, Math.round(span / (float) MINUTES_MS),
+                                userHandle, shortVersion);
+                    }
+                    final String rt = c == null ? "" : useLine1 ? c.line1 : c.summary;
+                    summary = TextUtils.isEmpty(rt) ? "" : rt;
+                }
             }
-            final long time = tryParseCountdownConditionId(id);
-            Condition c = config.manualRule.condition;
-            if (time > 0) {
-                final long now = System.currentTimeMillis();
-                final long span = time - now;
-                c = toTimeCondition(context, time, Math.round(span / (float) MINUTES_MS),
-                        userHandle, shortVersion);
-            }
-            final String rt = c == null ? "" : useLine1 ? c.line1 : c.summary;
-            return TextUtils.isEmpty(rt) ? "" : rt;
         }
-        String summary = "";
         for (ZenRule automaticRule : config.automaticRules.values()) {
             if (automaticRule.isAutomaticActive()) {
                 if (summary.isEmpty()) {
@@ -1023,6 +1059,7 @@
                             .getString(R.string.zen_mode_rule_name_combination, summary,
                                     automaticRule.name);
                 }
+
             }
         }
         return summary;
@@ -1038,6 +1075,7 @@
         public ComponentName component;  // optional
         public String id;                // required for automatic (unique)
         public long creationTime;        // required for automatic
+        public String enabler;          // package name, only used for manual rules.
 
         public ZenRule() { }
 
@@ -1055,6 +1093,9 @@
                 id = source.readString();
             }
             creationTime = source.readLong();
+            if (source.readInt() == 1) {
+                enabler = source.readString();
+            }
         }
 
         @Override
@@ -1083,6 +1124,12 @@
                 dest.writeInt(0);
             }
             dest.writeLong(creationTime);
+            if (enabler != null) {
+                dest.writeInt(1);
+                dest.writeString(enabler);
+            } else {
+                dest.writeInt(0);
+            }
         }
 
         @Override
@@ -1097,6 +1144,7 @@
                     .append(",component=").append(component)
                     .append(",id=").append(id)
                     .append(",creationTime=").append(creationTime)
+                    .append(",enabler=").append(enabler)
                     .append(']').toString();
         }
 
@@ -1143,6 +1191,9 @@
             if (creationTime != to.creationTime) {
                 d.addLine(item, "creationTime", creationTime, to.creationTime);
             }
+            if (enabler != to.enabler) {
+                d.addLine(item, "enabler", enabler, to.enabler);
+            }
         }
 
         @Override
@@ -1158,13 +1209,14 @@
                     && Objects.equals(other.condition, condition)
                     && Objects.equals(other.component, component)
                     && Objects.equals(other.id, id)
-                    && other.creationTime == creationTime;
+                    && other.creationTime == creationTime
+                    && Objects.equals(other.enabler, enabler);
         }
 
         @Override
         public int hashCode() {
             return Objects.hash(enabled, snoozing, name, zenMode, conditionId, condition,
-                    component, id, creationTime);
+                    component, id, creationTime, enabler);
         }
 
         public boolean isAutomaticActive() {
diff --git a/core/java/android/service/persistentdata/PersistentDataBlockManager.java b/core/java/android/service/persistentdata/PersistentDataBlockManager.java
index cfeed51..0f92ed0 100644
--- a/core/java/android/service/persistentdata/PersistentDataBlockManager.java
+++ b/core/java/android/service/persistentdata/PersistentDataBlockManager.java
@@ -163,10 +163,9 @@
     /**
      * Retrieves available information about this device's flash lock state.
      *
-     * @return FLASH_LOCK_STATE_LOCKED if device bootloader is locked,
-     * FLASH_LOCK_STATE_UNLOCKED if device bootloader is unlocked,
-     * or FLASH_LOCK_STATE unknown if this information cannot be ascertained
-     * on this device.
+     * @return {@link #FLASH_LOCK_LOCKED} if device bootloader is locked,
+     * {@link #FLASH_LOCK_UNLOCKED} if device bootloader is unlocked, or {@link #FLASH_LOCK_UNKNOWN}
+     * if this information cannot be ascertained on this device.
      */
     @FlashLockState
     public int getFlashLockState() {
diff --git a/core/java/android/service/quicksettings/IQSService.aidl b/core/java/android/service/quicksettings/IQSService.aidl
index bf96357..d03ff93 100644
--- a/core/java/android/service/quicksettings/IQSService.aidl
+++ b/core/java/android/service/quicksettings/IQSService.aidl
@@ -23,16 +23,16 @@
  * @hide
  */
 interface IQSService {
-    Tile getTile(in ComponentName component);
-    void updateQsTile(in Tile tile);
-    void updateStatusIcon(in Tile tile, in Icon icon,
+    Tile getTile(in IBinder tile);
+    void updateQsTile(in Tile tile, in IBinder service);
+    void updateStatusIcon(in IBinder tile, in Icon icon,
             String contentDescription);
-    void onShowDialog(in Tile tile);
-    void onStartActivity(in Tile tile);
+    void onShowDialog(in IBinder tile);
+    void onStartActivity(in IBinder tile);
     boolean isLocked();
     boolean isSecure();
-    void startUnlockAndRun(in Tile tile);
+    void startUnlockAndRun(in IBinder tile);
 
-    void onDialogHidden(in Tile tile);
-    void onStartSuccessful(in Tile tile);
+    void onDialogHidden(in IBinder tile);
+    void onStartSuccessful(in IBinder tile);
 }
diff --git a/core/java/android/service/quicksettings/Tile.java b/core/java/android/service/quicksettings/Tile.java
index 3d7d53e..4b81a72 100644
--- a/core/java/android/service/quicksettings/Tile.java
+++ b/core/java/android/service/quicksettings/Tile.java
@@ -15,8 +15,8 @@
  */
 package android.service.quicksettings;
 
-import android.content.ComponentName;
 import android.graphics.drawable.Icon;
+import android.os.IBinder;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.os.RemoteException;
@@ -59,7 +59,7 @@
      */
     public static final int STATE_ACTIVE = 2;
 
-    private ComponentName mComponentName;
+    private IBinder mToken;
     private Icon mIcon;
     private CharSequence mLabel;
     private CharSequence mContentDescription;
@@ -78,29 +78,15 @@
     /**
      * @hide
      */
-    public Tile(ComponentName componentName) {
-        mComponentName = componentName;
+    public Tile() {
     }
 
     /**
      * @hide
      */
-    public void setService(IQSService service) {
+    public void setService(IQSService service, IBinder stub) {
         mService = service;
-    }
-
-    /**
-     * @hide
-     */
-    public ComponentName getComponentName() {
-        return mComponentName;
-    }
-
-    /**
-     * @hide
-     */
-    public IQSService getQsService() {
-        return mService;
+        mToken = stub;
     }
 
     /**
@@ -193,7 +179,7 @@
      */
     public void updateTile() {
         try {
-            mService.updateQsTile(this);
+            mService.updateQsTile(this, mToken);
         } catch (RemoteException e) {
             Log.e(TAG, "Couldn't update tile");
         }
@@ -201,12 +187,6 @@
 
     @Override
     public void writeToParcel(Parcel dest, int flags) {
-        if (mComponentName != null) {
-            dest.writeByte((byte) 1);
-            mComponentName.writeToParcel(dest, flags);
-        } else {
-            dest.writeByte((byte) 0);
-        }
         if (mIcon != null) {
             dest.writeByte((byte) 1);
             mIcon.writeToParcel(dest, flags);
@@ -220,11 +200,6 @@
 
     private void readFromParcel(Parcel source) {
         if (source.readByte() != 0) {
-            mComponentName = ComponentName.CREATOR.createFromParcel(source);
-        } else {
-            mComponentName = null;
-        }
-        if (source.readByte() != 0) {
             mIcon = Icon.CREATOR.createFromParcel(source);
         } else {
             mIcon = null;
diff --git a/core/java/android/service/quicksettings/TileService.java b/core/java/android/service/quicksettings/TileService.java
index 50411ab..887f4b6 100644
--- a/core/java/android/service/quicksettings/TileService.java
+++ b/core/java/android/service/quicksettings/TileService.java
@@ -123,6 +123,11 @@
     /**
      * @hide
      */
+    public static final String EXTRA_TOKEN = "token";
+
+    /**
+     * @hide
+     */
     public static final String EXTRA_COMPONENT = "android.service.quicksettings.extra.COMPONENT";
 
     private final H mHandler = new H(Looper.getMainLooper());
@@ -132,6 +137,7 @@
     private IBinder mToken;
     private IQSService mService;
     private Runnable mUnlockRunnable;
+    private IBinder mTileToken;
 
     @Override
     public void onDestroy() {
@@ -197,7 +203,7 @@
     public final void setStatusIcon(Icon icon, String contentDescription) {
         if (mService != null) {
             try {
-                mService.updateStatusIcon(mTile, icon, contentDescription);
+                mService.updateStatusIcon(mTileToken, icon, contentDescription);
             } catch (RemoteException e) {
             }
         }
@@ -224,14 +230,14 @@
             @Override
             public void onViewDetachedFromWindow(View v) {
                 try {
-                    mService.onDialogHidden(getQsTile());
+                    mService.onDialogHidden(mTileToken);
                 } catch (RemoteException e) {
                 }
             }
         });
         dialog.show();
         try {
-            mService.onShowDialog(mTile);
+            mService.onShowDialog(mTileToken);
         } catch (RemoteException e) {
         }
     }
@@ -246,7 +252,7 @@
     public final void unlockAndRun(Runnable runnable) {
         mUnlockRunnable = runnable;
         try {
-            mService.startUnlockAndRun(mTile);
+            mService.startUnlockAndRun(mTileToken);
         } catch (RemoteException e) {
         }
     }
@@ -292,7 +298,7 @@
     public final void startActivityAndCollapse(Intent intent) {
         startActivity(intent);
         try {
-            mService.onStartActivity(mTile);
+            mService.onStartActivity(mTileToken);
         } catch (RemoteException e) {
         }
     }
@@ -311,14 +317,14 @@
     @Override
     public IBinder onBind(Intent intent) {
         mService = IQSService.Stub.asInterface(intent.getIBinderExtra(EXTRA_SERVICE));
+        mTileToken = intent.getIBinderExtra(EXTRA_TOKEN);
         try {
-            ComponentName component = intent.getParcelableExtra(EXTRA_COMPONENT);
-            mTile = mService.getTile(component);
+            mTile = mService.getTile(mTileToken);
         } catch (RemoteException e) {
             throw new RuntimeException("Unable to reach IQSService", e);
         }
         if (mTile != null) {
-            mTile.setService(mService);
+            mTile.setService(mService, mTileToken);
             mHandler.sendEmptyMessage(H.MSG_START_SUCCESS);
         }
         return new IQSTileService.Stub() {
@@ -403,7 +409,7 @@
                     break;
                 case MSG_START_SUCCESS:
                     try {
-                        mService.onStartSuccessful(mTile);
+                        mService.onStartSuccessful(mTileToken);
                     } catch (RemoteException e) {
                     }
                     break;
diff --git a/core/java/android/service/wallpaper/WallpaperService.java b/core/java/android/service/wallpaper/WallpaperService.java
index de8133b..06d87f8 100644
--- a/core/java/android/service/wallpaper/WallpaperService.java
+++ b/core/java/android/service/wallpaper/WallpaperService.java
@@ -55,6 +55,7 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
+import android.view.WindowManager.LayoutParams;
 import android.view.WindowManagerGlobal;
 
 import java.io.FileDescriptor;
@@ -628,9 +629,9 @@
                     mCurWindowFlags = mWindowFlags;
                     mLayout.flags = mWindowFlags
                             | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
+                            | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                             | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
-                            | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
-                            ;
+                            | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
                     mCurWindowPrivateFlags = mWindowPrivateFlags;
                     mLayout.privateFlags = mWindowPrivateFlags;
 
diff --git a/core/java/android/text/Emoji.java b/core/java/android/text/Emoji.java
index b62cc66..b6d720d 100644
--- a/core/java/android/text/Emoji.java
+++ b/core/java/android/text/Emoji.java
@@ -32,111 +32,112 @@
         0x23EC, 0x23ED, 0x23EE, 0x23EF, 0x23F0, 0x23F1, 0x23F2, 0x23F3, 0x23F8, 0x23F9, 0x23FA,
         0x24C2, 0x25AA, 0x25AB, 0x25B6, 0x25C0, 0x25FB, 0x25FC, 0x25FD, 0x25FE, 0x2600, 0x2601,
         0x2602, 0x2603, 0x2604, 0x260E, 0x2611, 0x2614, 0x2615, 0x2618, 0x261D, 0x2620, 0x2622,
-        0x2623, 0x2626, 0x262A, 0x262E, 0x262F, 0x2638, 0x2639, 0x263A, 0x2648, 0x2649, 0x264A,
-        0x264B, 0x264C, 0x264D, 0x264E, 0x264F, 0x2650, 0x2651, 0x2652, 0x2653, 0x2660, 0x2663,
-        0x2665, 0x2666, 0x2668, 0x267B, 0x267F, 0x2692, 0x2693, 0x2694, 0x2696, 0x2697, 0x2699,
-        0x269B, 0x269C, 0x26A0, 0x26A1, 0x26AA, 0x26AB, 0x26B0, 0x26B1, 0x26BD, 0x26BE, 0x26C4,
-        0x26C5, 0x26C8, 0x26CE, 0x26CF, 0x26D1, 0x26D3, 0x26D4, 0x26E9, 0x26EA, 0x26F0, 0x26F1,
-        0x26F2, 0x26F3, 0x26F4, 0x26F5, 0x26F7, 0x26F8, 0x26F9, 0x26FA, 0x26FD, 0x2702, 0x2705,
-        0x2708, 0x2709, 0x270A, 0x270B, 0x270C, 0x270D, 0x270F, 0x2712, 0x2714, 0x2716, 0x271D,
-        0x2721, 0x2728, 0x2733, 0x2734, 0x2744, 0x2747, 0x274C, 0x274E, 0x2753, 0x2754, 0x2755,
-        0x2757, 0x2763, 0x2764, 0x2795, 0x2796, 0x2797, 0x27A1, 0x27B0, 0x27BF, 0x2934, 0x2935,
-        0x2B05, 0x2B06, 0x2B07, 0x2B1B, 0x2B1C, 0x2B50, 0x2B55, 0x3030, 0x303D, 0x3297, 0x3299,
-        0x1F004, 0x1F0CF, 0x1F170, 0x1F171, 0x1F17E, 0x1F17F, 0x1F18E, 0x1F191, 0x1F192, 0x1F193,
-        0x1F194, 0x1F195, 0x1F196, 0x1F197, 0x1F198, 0x1F199, 0x1F19A, 0x1F1E6, 0x1F1E7, 0x1F1E8,
-        0x1F1E9, 0x1F1EA, 0x1F1EB, 0x1F1EC, 0x1F1ED, 0x1F1EE, 0x1F1EF, 0x1F1F0, 0x1F1F1, 0x1F1F2,
-        0x1F1F3, 0x1F1F4, 0x1F1F5, 0x1F1F6, 0x1F1F7, 0x1F1F8, 0x1F1F9, 0x1F1FA, 0x1F1FB, 0x1F1FC,
-        0x1F1FD, 0x1F1FE, 0x1F1FF, 0x1F201, 0x1F202, 0x1F21A, 0x1F22F, 0x1F232, 0x1F233, 0x1F234,
-        0x1F235, 0x1F236, 0x1F237, 0x1F238, 0x1F239, 0x1F23A, 0x1F250, 0x1F251, 0x1F300, 0x1F301,
-        0x1F302, 0x1F303, 0x1F304, 0x1F305, 0x1F306, 0x1F307, 0x1F308, 0x1F309, 0x1F30A, 0x1F30B,
-        0x1F30C, 0x1F30D, 0x1F30E, 0x1F30F, 0x1F310, 0x1F311, 0x1F312, 0x1F313, 0x1F314, 0x1F315,
-        0x1F316, 0x1F317, 0x1F318, 0x1F319, 0x1F31A, 0x1F31B, 0x1F31C, 0x1F31D, 0x1F31E, 0x1F31F,
-        0x1F320, 0x1F321, 0x1F324, 0x1F325, 0x1F326, 0x1F327, 0x1F328, 0x1F329, 0x1F32A, 0x1F32B,
-        0x1F32C, 0x1F32D, 0x1F32E, 0x1F32F, 0x1F330, 0x1F331, 0x1F332, 0x1F333, 0x1F334, 0x1F335,
-        0x1F336, 0x1F337, 0x1F338, 0x1F339, 0x1F33A, 0x1F33B, 0x1F33C, 0x1F33D, 0x1F33E, 0x1F33F,
-        0x1F340, 0x1F341, 0x1F342, 0x1F343, 0x1F344, 0x1F345, 0x1F346, 0x1F347, 0x1F348, 0x1F349,
-        0x1F34A, 0x1F34B, 0x1F34C, 0x1F34D, 0x1F34E, 0x1F34F, 0x1F350, 0x1F351, 0x1F352, 0x1F353,
-        0x1F354, 0x1F355, 0x1F356, 0x1F357, 0x1F358, 0x1F359, 0x1F35A, 0x1F35B, 0x1F35C, 0x1F35D,
-        0x1F35E, 0x1F35F, 0x1F360, 0x1F361, 0x1F362, 0x1F363, 0x1F364, 0x1F365, 0x1F366, 0x1F367,
-        0x1F368, 0x1F369, 0x1F36A, 0x1F36B, 0x1F36C, 0x1F36D, 0x1F36E, 0x1F36F, 0x1F370, 0x1F371,
-        0x1F372, 0x1F373, 0x1F374, 0x1F375, 0x1F376, 0x1F377, 0x1F378, 0x1F379, 0x1F37A, 0x1F37B,
-        0x1F37C, 0x1F37D, 0x1F37E, 0x1F37F, 0x1F380, 0x1F381, 0x1F382, 0x1F383, 0x1F384, 0x1F385,
-        0x1F386, 0x1F387, 0x1F388, 0x1F389, 0x1F38A, 0x1F38B, 0x1F38C, 0x1F38D, 0x1F38E, 0x1F38F,
-        0x1F390, 0x1F391, 0x1F392, 0x1F393, 0x1F396, 0x1F397, 0x1F399, 0x1F39A, 0x1F39B, 0x1F39E,
-        0x1F39F, 0x1F3A0, 0x1F3A1, 0x1F3A2, 0x1F3A3, 0x1F3A4, 0x1F3A5, 0x1F3A6, 0x1F3A7, 0x1F3A8,
-        0x1F3A9, 0x1F3AA, 0x1F3AB, 0x1F3AC, 0x1F3AD, 0x1F3AE, 0x1F3AF, 0x1F3B0, 0x1F3B1, 0x1F3B2,
-        0x1F3B3, 0x1F3B4, 0x1F3B5, 0x1F3B6, 0x1F3B7, 0x1F3B8, 0x1F3B9, 0x1F3BA, 0x1F3BB, 0x1F3BC,
-        0x1F3BD, 0x1F3BE, 0x1F3BF, 0x1F3C0, 0x1F3C1, 0x1F3C2, 0x1F3C3, 0x1F3C4, 0x1F3C5, 0x1F3C6,
-        0x1F3C7, 0x1F3C8, 0x1F3C9, 0x1F3CA, 0x1F3CB, 0x1F3CC, 0x1F3CD, 0x1F3CE, 0x1F3CF, 0x1F3D0,
-        0x1F3D1, 0x1F3D2, 0x1F3D3, 0x1F3D4, 0x1F3D5, 0x1F3D6, 0x1F3D7, 0x1F3D8, 0x1F3D9, 0x1F3DA,
-        0x1F3DB, 0x1F3DC, 0x1F3DD, 0x1F3DE, 0x1F3DF, 0x1F3E0, 0x1F3E1, 0x1F3E2, 0x1F3E3, 0x1F3E4,
-        0x1F3E5, 0x1F3E6, 0x1F3E7, 0x1F3E8, 0x1F3E9, 0x1F3EA, 0x1F3EB, 0x1F3EC, 0x1F3ED, 0x1F3EE,
-        0x1F3EF, 0x1F3F0, 0x1F3F3, 0x1F3F4, 0x1F3F5, 0x1F3F7, 0x1F3F8, 0x1F3F9, 0x1F3FA, 0x1F3FB,
-        0x1F3FC, 0x1F3FD, 0x1F3FE, 0x1F3FF, 0x1F400, 0x1F401, 0x1F402, 0x1F403, 0x1F404, 0x1F405,
-        0x1F406, 0x1F407, 0x1F408, 0x1F409, 0x1F40A, 0x1F40B, 0x1F40C, 0x1F40D, 0x1F40E, 0x1F40F,
-        0x1F410, 0x1F411, 0x1F412, 0x1F413, 0x1F414, 0x1F415, 0x1F416, 0x1F417, 0x1F418, 0x1F419,
-        0x1F41A, 0x1F41B, 0x1F41C, 0x1F41D, 0x1F41E, 0x1F41F, 0x1F420, 0x1F421, 0x1F422, 0x1F423,
-        0x1F424, 0x1F425, 0x1F426, 0x1F427, 0x1F428, 0x1F429, 0x1F42A, 0x1F42B, 0x1F42C, 0x1F42D,
-        0x1F42E, 0x1F42F, 0x1F430, 0x1F431, 0x1F432, 0x1F433, 0x1F434, 0x1F435, 0x1F436, 0x1F437,
-        0x1F438, 0x1F439, 0x1F43A, 0x1F43B, 0x1F43C, 0x1F43D, 0x1F43E, 0x1F43F, 0x1F440, 0x1F441,
-        0x1F442, 0x1F443, 0x1F444, 0x1F445, 0x1F446, 0x1F447, 0x1F448, 0x1F449, 0x1F44A, 0x1F44B,
-        0x1F44C, 0x1F44D, 0x1F44E, 0x1F44F, 0x1F450, 0x1F451, 0x1F452, 0x1F453, 0x1F454, 0x1F455,
-        0x1F456, 0x1F457, 0x1F458, 0x1F459, 0x1F45A, 0x1F45B, 0x1F45C, 0x1F45D, 0x1F45E, 0x1F45F,
-        0x1F460, 0x1F461, 0x1F462, 0x1F463, 0x1F464, 0x1F465, 0x1F466, 0x1F467, 0x1F468, 0x1F469,
-        0x1F46A, 0x1F46B, 0x1F46C, 0x1F46D, 0x1F46E, 0x1F46F, 0x1F470, 0x1F471, 0x1F472, 0x1F473,
-        0x1F474, 0x1F475, 0x1F476, 0x1F477, 0x1F478, 0x1F479, 0x1F47A, 0x1F47B, 0x1F47C, 0x1F47D,
-        0x1F47E, 0x1F47F, 0x1F480, 0x1F481, 0x1F482, 0x1F483, 0x1F484, 0x1F485, 0x1F486, 0x1F487,
-        0x1F488, 0x1F489, 0x1F48A, 0x1F48B, 0x1F48C, 0x1F48D, 0x1F48E, 0x1F48F, 0x1F490, 0x1F491,
-        0x1F492, 0x1F493, 0x1F494, 0x1F495, 0x1F496, 0x1F497, 0x1F498, 0x1F499, 0x1F49A, 0x1F49B,
-        0x1F49C, 0x1F49D, 0x1F49E, 0x1F49F, 0x1F4A0, 0x1F4A1, 0x1F4A2, 0x1F4A3, 0x1F4A4, 0x1F4A5,
-        0x1F4A6, 0x1F4A7, 0x1F4A8, 0x1F4A9, 0x1F4AA, 0x1F4AB, 0x1F4AC, 0x1F4AD, 0x1F4AE, 0x1F4AF,
-        0x1F4B0, 0x1F4B1, 0x1F4B2, 0x1F4B3, 0x1F4B4, 0x1F4B5, 0x1F4B6, 0x1F4B7, 0x1F4B8, 0x1F4B9,
-        0x1F4BA, 0x1F4BB, 0x1F4BC, 0x1F4BD, 0x1F4BE, 0x1F4BF, 0x1F4C0, 0x1F4C1, 0x1F4C2, 0x1F4C3,
-        0x1F4C4, 0x1F4C5, 0x1F4C6, 0x1F4C7, 0x1F4C8, 0x1F4C9, 0x1F4CA, 0x1F4CB, 0x1F4CC, 0x1F4CD,
-        0x1F4CE, 0x1F4CF, 0x1F4D0, 0x1F4D1, 0x1F4D2, 0x1F4D3, 0x1F4D4, 0x1F4D5, 0x1F4D6, 0x1F4D7,
-        0x1F4D8, 0x1F4D9, 0x1F4DA, 0x1F4DB, 0x1F4DC, 0x1F4DD, 0x1F4DE, 0x1F4DF, 0x1F4E0, 0x1F4E1,
-        0x1F4E2, 0x1F4E3, 0x1F4E4, 0x1F4E5, 0x1F4E6, 0x1F4E7, 0x1F4E8, 0x1F4E9, 0x1F4EA, 0x1F4EB,
-        0x1F4EC, 0x1F4ED, 0x1F4EE, 0x1F4EF, 0x1F4F0, 0x1F4F1, 0x1F4F2, 0x1F4F3, 0x1F4F4, 0x1F4F5,
-        0x1F4F6, 0x1F4F7, 0x1F4F8, 0x1F4F9, 0x1F4FA, 0x1F4FB, 0x1F4FC, 0x1F4FD, 0x1F4FF, 0x1F500,
-        0x1F501, 0x1F502, 0x1F503, 0x1F504, 0x1F505, 0x1F506, 0x1F507, 0x1F508, 0x1F509, 0x1F50A,
-        0x1F50B, 0x1F50C, 0x1F50D, 0x1F50E, 0x1F50F, 0x1F510, 0x1F511, 0x1F512, 0x1F513, 0x1F514,
-        0x1F515, 0x1F516, 0x1F517, 0x1F518, 0x1F519, 0x1F51A, 0x1F51B, 0x1F51C, 0x1F51D, 0x1F51E,
-        0x1F51F, 0x1F520, 0x1F521, 0x1F522, 0x1F523, 0x1F524, 0x1F525, 0x1F526, 0x1F527, 0x1F528,
-        0x1F529, 0x1F52A, 0x1F52B, 0x1F52C, 0x1F52D, 0x1F52E, 0x1F52F, 0x1F530, 0x1F531, 0x1F532,
-        0x1F533, 0x1F534, 0x1F535, 0x1F536, 0x1F537, 0x1F538, 0x1F539, 0x1F53A, 0x1F53B, 0x1F53C,
-        0x1F53D, 0x1F549, 0x1F54A, 0x1F54B, 0x1F54C, 0x1F54D, 0x1F54E, 0x1F550, 0x1F551, 0x1F552,
-        0x1F553, 0x1F554, 0x1F555, 0x1F556, 0x1F557, 0x1F558, 0x1F559, 0x1F55A, 0x1F55B, 0x1F55C,
-        0x1F55D, 0x1F55E, 0x1F55F, 0x1F560, 0x1F561, 0x1F562, 0x1F563, 0x1F564, 0x1F565, 0x1F566,
-        0x1F567, 0x1F56F, 0x1F570, 0x1F573, 0x1F574, 0x1F575, 0x1F576, 0x1F577, 0x1F578, 0x1F579,
-        0x1F57A, 0x1F587, 0x1F58A, 0x1F58B, 0x1F58C, 0x1F58D, 0x1F590, 0x1F595, 0x1F596, 0x1F5A4,
-        0x1F5A5, 0x1F5A8, 0x1F5B1, 0x1F5B2, 0x1F5BC, 0x1F5C2, 0x1F5C3, 0x1F5C4, 0x1F5D1, 0x1F5D2,
-        0x1F5D3, 0x1F5DC, 0x1F5DD, 0x1F5DE, 0x1F5E1, 0x1F5E3, 0x1F5E8, 0x1F5EF, 0x1F5F3, 0x1F5FA,
-        0x1F5FB, 0x1F5FC, 0x1F5FD, 0x1F5FE, 0x1F5FF, 0x1F600, 0x1F601, 0x1F602, 0x1F603, 0x1F604,
-        0x1F605, 0x1F606, 0x1F607, 0x1F608, 0x1F609, 0x1F60A, 0x1F60B, 0x1F60C, 0x1F60D, 0x1F60E,
-        0x1F60F, 0x1F610, 0x1F611, 0x1F612, 0x1F613, 0x1F614, 0x1F615, 0x1F616, 0x1F617, 0x1F618,
-        0x1F619, 0x1F61A, 0x1F61B, 0x1F61C, 0x1F61D, 0x1F61E, 0x1F61F, 0x1F620, 0x1F621, 0x1F622,
-        0x1F623, 0x1F624, 0x1F625, 0x1F626, 0x1F627, 0x1F628, 0x1F629, 0x1F62A, 0x1F62B, 0x1F62C,
-        0x1F62D, 0x1F62E, 0x1F62F, 0x1F630, 0x1F631, 0x1F632, 0x1F633, 0x1F634, 0x1F635, 0x1F636,
-        0x1F637, 0x1F638, 0x1F639, 0x1F63A, 0x1F63B, 0x1F63C, 0x1F63D, 0x1F63E, 0x1F63F, 0x1F640,
-        0x1F641, 0x1F642, 0x1F643, 0x1F644, 0x1F645, 0x1F646, 0x1F647, 0x1F648, 0x1F649, 0x1F64A,
-        0x1F64B, 0x1F64C, 0x1F64D, 0x1F64E, 0x1F64F, 0x1F680, 0x1F681, 0x1F682, 0x1F683, 0x1F684,
-        0x1F685, 0x1F686, 0x1F687, 0x1F688, 0x1F689, 0x1F68A, 0x1F68B, 0x1F68C, 0x1F68D, 0x1F68E,
-        0x1F68F, 0x1F690, 0x1F691, 0x1F692, 0x1F693, 0x1F694, 0x1F695, 0x1F696, 0x1F697, 0x1F698,
-        0x1F699, 0x1F69A, 0x1F69B, 0x1F69C, 0x1F69D, 0x1F69E, 0x1F69F, 0x1F6A0, 0x1F6A1, 0x1F6A2,
-        0x1F6A3, 0x1F6A4, 0x1F6A5, 0x1F6A6, 0x1F6A7, 0x1F6A8, 0x1F6A9, 0x1F6AA, 0x1F6AB, 0x1F6AC,
-        0x1F6AD, 0x1F6AE, 0x1F6AF, 0x1F6B0, 0x1F6B1, 0x1F6B2, 0x1F6B3, 0x1F6B4, 0x1F6B5, 0x1F6B6,
-        0x1F6B7, 0x1F6B8, 0x1F6B9, 0x1F6BA, 0x1F6BB, 0x1F6BC, 0x1F6BD, 0x1F6BE, 0x1F6BF, 0x1F6C0,
-        0x1F6C1, 0x1F6C2, 0x1F6C3, 0x1F6C4, 0x1F6C5, 0x1F6CB, 0x1F6CC, 0x1F6CD, 0x1F6CE, 0x1F6CF,
-        0x1F6D0, 0x1F6D1, 0x1F6D2, 0x1F6E0, 0x1F6E1, 0x1F6E2, 0x1F6E3, 0x1F6E4, 0x1F6E5, 0x1F6E9,
-        0x1F6EB, 0x1F6EC, 0x1F6F0, 0x1F6F3, 0x1F6F4, 0x1F6F5, 0x1F6F6, 0x1F910, 0x1F911, 0x1F912,
-        0x1F913, 0x1F914, 0x1F915, 0x1F916, 0x1F917, 0x1F918, 0x1F919, 0x1F91A, 0x1F91B, 0x1F91C,
-        0x1F91D, 0x1F91E, 0x1F920, 0x1F921, 0x1F922, 0x1F923, 0x1F924, 0x1F925, 0x1F926, 0x1F927,
-        0x1F930, 0x1F933, 0x1F934, 0x1F935, 0x1F936, 0x1F937, 0x1F938, 0x1F939, 0x1F93A, 0x1F93B,
-        0x1F93C, 0x1F93D, 0x1F93E, 0x1F940, 0x1F941, 0x1F942, 0x1F943, 0x1F944, 0x1F945, 0x1F946,
-        0x1F947, 0x1F948, 0x1F949, 0x1F94A, 0x1F94B, 0x1F950, 0x1F951, 0x1F952, 0x1F953, 0x1F954,
-        0x1F955, 0x1F956, 0x1F957, 0x1F958, 0x1F959, 0x1F95A, 0x1F95B, 0x1F95C, 0x1F95D, 0x1F95E,
-        0x1F980, 0x1F981, 0x1F982, 0x1F983, 0x1F984, 0x1F985, 0x1F986, 0x1F987, 0x1F988, 0x1F989,
-        0x1F98A, 0x1F98B, 0x1F98C, 0x1F98D, 0x1F98E, 0x1F98F, 0x1F990, 0x1F991, 0x1F9C0
+        0x2623, 0x2626, 0x262A, 0x262E, 0x262F, 0x2638, 0x2639, 0x263A, 0x2640, 0x2642, 0x2648,
+        0x2649, 0x264A, 0x264B, 0x264C, 0x264D, 0x264E, 0x264F, 0x2650, 0x2651, 0x2652, 0x2653,
+        0x2660, 0x2663, 0x2665, 0x2666, 0x2668, 0x267B, 0x267F, 0x2692, 0x2693, 0x2694, 0x2695,
+        0x2696, 0x2697, 0x2699, 0x269B, 0x269C, 0x26A0, 0x26A1, 0x26AA, 0x26AB, 0x26B0, 0x26B1,
+        0x26BD, 0x26BE, 0x26C4, 0x26C5, 0x26C8, 0x26CE, 0x26CF, 0x26D1, 0x26D3, 0x26D4, 0x26E9,
+        0x26EA, 0x26F0, 0x26F1, 0x26F2, 0x26F3, 0x26F4, 0x26F5, 0x26F7, 0x26F8, 0x26F9, 0x26FA,
+        0x26FD, 0x2702, 0x2705, 0x2708, 0x2709, 0x270A, 0x270B, 0x270C, 0x270D, 0x270F, 0x2712,
+        0x2714, 0x2716, 0x271D, 0x2721, 0x2728, 0x2733, 0x2734, 0x2744, 0x2747, 0x274C, 0x274E,
+        0x2753, 0x2754, 0x2755, 0x2757, 0x2763, 0x2764, 0x2795, 0x2796, 0x2797, 0x27A1, 0x27B0,
+        0x27BF, 0x2934, 0x2935, 0x2B05, 0x2B06, 0x2B07, 0x2B1B, 0x2B1C, 0x2B50, 0x2B55, 0x3030,
+        0x303D, 0x3297, 0x3299, 0x1F004, 0x1F0CF, 0x1F170, 0x1F171, 0x1F17E, 0x1F17F, 0x1F18E,
+        0x1F191, 0x1F192, 0x1F193, 0x1F194, 0x1F195, 0x1F196, 0x1F197, 0x1F198, 0x1F199, 0x1F19A,
+        0x1F1E6, 0x1F1E7, 0x1F1E8, 0x1F1E9, 0x1F1EA, 0x1F1EB, 0x1F1EC, 0x1F1ED, 0x1F1EE, 0x1F1EF,
+        0x1F1F0, 0x1F1F1, 0x1F1F2, 0x1F1F3, 0x1F1F4, 0x1F1F5, 0x1F1F6, 0x1F1F7, 0x1F1F8, 0x1F1F9,
+        0x1F1FA, 0x1F1FB, 0x1F1FC, 0x1F1FD, 0x1F1FE, 0x1F1FF, 0x1F201, 0x1F202, 0x1F21A, 0x1F22F,
+        0x1F232, 0x1F233, 0x1F234, 0x1F235, 0x1F236, 0x1F237, 0x1F238, 0x1F239, 0x1F23A, 0x1F250,
+        0x1F251, 0x1F300, 0x1F301, 0x1F302, 0x1F303, 0x1F304, 0x1F305, 0x1F306, 0x1F307, 0x1F308,
+        0x1F309, 0x1F30A, 0x1F30B, 0x1F30C, 0x1F30D, 0x1F30E, 0x1F30F, 0x1F310, 0x1F311, 0x1F312,
+        0x1F313, 0x1F314, 0x1F315, 0x1F316, 0x1F317, 0x1F318, 0x1F319, 0x1F31A, 0x1F31B, 0x1F31C,
+        0x1F31D, 0x1F31E, 0x1F31F, 0x1F320, 0x1F321, 0x1F324, 0x1F325, 0x1F326, 0x1F327, 0x1F328,
+        0x1F329, 0x1F32A, 0x1F32B, 0x1F32C, 0x1F32D, 0x1F32E, 0x1F32F, 0x1F330, 0x1F331, 0x1F332,
+        0x1F333, 0x1F334, 0x1F335, 0x1F336, 0x1F337, 0x1F338, 0x1F339, 0x1F33A, 0x1F33B, 0x1F33C,
+        0x1F33D, 0x1F33E, 0x1F33F, 0x1F340, 0x1F341, 0x1F342, 0x1F343, 0x1F344, 0x1F345, 0x1F346,
+        0x1F347, 0x1F348, 0x1F349, 0x1F34A, 0x1F34B, 0x1F34C, 0x1F34D, 0x1F34E, 0x1F34F, 0x1F350,
+        0x1F351, 0x1F352, 0x1F353, 0x1F354, 0x1F355, 0x1F356, 0x1F357, 0x1F358, 0x1F359, 0x1F35A,
+        0x1F35B, 0x1F35C, 0x1F35D, 0x1F35E, 0x1F35F, 0x1F360, 0x1F361, 0x1F362, 0x1F363, 0x1F364,
+        0x1F365, 0x1F366, 0x1F367, 0x1F368, 0x1F369, 0x1F36A, 0x1F36B, 0x1F36C, 0x1F36D, 0x1F36E,
+        0x1F36F, 0x1F370, 0x1F371, 0x1F372, 0x1F373, 0x1F374, 0x1F375, 0x1F376, 0x1F377, 0x1F378,
+        0x1F379, 0x1F37A, 0x1F37B, 0x1F37C, 0x1F37D, 0x1F37E, 0x1F37F, 0x1F380, 0x1F381, 0x1F382,
+        0x1F383, 0x1F384, 0x1F385, 0x1F386, 0x1F387, 0x1F388, 0x1F389, 0x1F38A, 0x1F38B, 0x1F38C,
+        0x1F38D, 0x1F38E, 0x1F38F, 0x1F390, 0x1F391, 0x1F392, 0x1F393, 0x1F396, 0x1F397, 0x1F399,
+        0x1F39A, 0x1F39B, 0x1F39E, 0x1F39F, 0x1F3A0, 0x1F3A1, 0x1F3A2, 0x1F3A3, 0x1F3A4, 0x1F3A5,
+        0x1F3A6, 0x1F3A7, 0x1F3A8, 0x1F3A9, 0x1F3AA, 0x1F3AB, 0x1F3AC, 0x1F3AD, 0x1F3AE, 0x1F3AF,
+        0x1F3B0, 0x1F3B1, 0x1F3B2, 0x1F3B3, 0x1F3B4, 0x1F3B5, 0x1F3B6, 0x1F3B7, 0x1F3B8, 0x1F3B9,
+        0x1F3BA, 0x1F3BB, 0x1F3BC, 0x1F3BD, 0x1F3BE, 0x1F3BF, 0x1F3C0, 0x1F3C1, 0x1F3C2, 0x1F3C3,
+        0x1F3C4, 0x1F3C5, 0x1F3C6, 0x1F3C7, 0x1F3C8, 0x1F3C9, 0x1F3CA, 0x1F3CB, 0x1F3CC, 0x1F3CD,
+        0x1F3CE, 0x1F3CF, 0x1F3D0, 0x1F3D1, 0x1F3D2, 0x1F3D3, 0x1F3D4, 0x1F3D5, 0x1F3D6, 0x1F3D7,
+        0x1F3D8, 0x1F3D9, 0x1F3DA, 0x1F3DB, 0x1F3DC, 0x1F3DD, 0x1F3DE, 0x1F3DF, 0x1F3E0, 0x1F3E1,
+        0x1F3E2, 0x1F3E3, 0x1F3E4, 0x1F3E5, 0x1F3E6, 0x1F3E7, 0x1F3E8, 0x1F3E9, 0x1F3EA, 0x1F3EB,
+        0x1F3EC, 0x1F3ED, 0x1F3EE, 0x1F3EF, 0x1F3F0, 0x1F3F3, 0x1F3F4, 0x1F3F5, 0x1F3F7, 0x1F3F8,
+        0x1F3F9, 0x1F3FA, 0x1F3FB, 0x1F3FC, 0x1F3FD, 0x1F3FE, 0x1F3FF, 0x1F400, 0x1F401, 0x1F402,
+        0x1F403, 0x1F404, 0x1F405, 0x1F406, 0x1F407, 0x1F408, 0x1F409, 0x1F40A, 0x1F40B, 0x1F40C,
+        0x1F40D, 0x1F40E, 0x1F40F, 0x1F410, 0x1F411, 0x1F412, 0x1F413, 0x1F414, 0x1F415, 0x1F416,
+        0x1F417, 0x1F418, 0x1F419, 0x1F41A, 0x1F41B, 0x1F41C, 0x1F41D, 0x1F41E, 0x1F41F, 0x1F420,
+        0x1F421, 0x1F422, 0x1F423, 0x1F424, 0x1F425, 0x1F426, 0x1F427, 0x1F428, 0x1F429, 0x1F42A,
+        0x1F42B, 0x1F42C, 0x1F42D, 0x1F42E, 0x1F42F, 0x1F430, 0x1F431, 0x1F432, 0x1F433, 0x1F434,
+        0x1F435, 0x1F436, 0x1F437, 0x1F438, 0x1F439, 0x1F43A, 0x1F43B, 0x1F43C, 0x1F43D, 0x1F43E,
+        0x1F43F, 0x1F440, 0x1F441, 0x1F442, 0x1F443, 0x1F444, 0x1F445, 0x1F446, 0x1F447, 0x1F448,
+        0x1F449, 0x1F44A, 0x1F44B, 0x1F44C, 0x1F44D, 0x1F44E, 0x1F44F, 0x1F450, 0x1F451, 0x1F452,
+        0x1F453, 0x1F454, 0x1F455, 0x1F456, 0x1F457, 0x1F458, 0x1F459, 0x1F45A, 0x1F45B, 0x1F45C,
+        0x1F45D, 0x1F45E, 0x1F45F, 0x1F460, 0x1F461, 0x1F462, 0x1F463, 0x1F464, 0x1F465, 0x1F466,
+        0x1F467, 0x1F468, 0x1F469, 0x1F46A, 0x1F46B, 0x1F46C, 0x1F46D, 0x1F46E, 0x1F46F, 0x1F470,
+        0x1F471, 0x1F472, 0x1F473, 0x1F474, 0x1F475, 0x1F476, 0x1F477, 0x1F478, 0x1F479, 0x1F47A,
+        0x1F47B, 0x1F47C, 0x1F47D, 0x1F47E, 0x1F47F, 0x1F480, 0x1F481, 0x1F482, 0x1F483, 0x1F484,
+        0x1F485, 0x1F486, 0x1F487, 0x1F488, 0x1F489, 0x1F48A, 0x1F48B, 0x1F48C, 0x1F48D, 0x1F48E,
+        0x1F48F, 0x1F490, 0x1F491, 0x1F492, 0x1F493, 0x1F494, 0x1F495, 0x1F496, 0x1F497, 0x1F498,
+        0x1F499, 0x1F49A, 0x1F49B, 0x1F49C, 0x1F49D, 0x1F49E, 0x1F49F, 0x1F4A0, 0x1F4A1, 0x1F4A2,
+        0x1F4A3, 0x1F4A4, 0x1F4A5, 0x1F4A6, 0x1F4A7, 0x1F4A8, 0x1F4A9, 0x1F4AA, 0x1F4AB, 0x1F4AC,
+        0x1F4AD, 0x1F4AE, 0x1F4AF, 0x1F4B0, 0x1F4B1, 0x1F4B2, 0x1F4B3, 0x1F4B4, 0x1F4B5, 0x1F4B6,
+        0x1F4B7, 0x1F4B8, 0x1F4B9, 0x1F4BA, 0x1F4BB, 0x1F4BC, 0x1F4BD, 0x1F4BE, 0x1F4BF, 0x1F4C0,
+        0x1F4C1, 0x1F4C2, 0x1F4C3, 0x1F4C4, 0x1F4C5, 0x1F4C6, 0x1F4C7, 0x1F4C8, 0x1F4C9, 0x1F4CA,
+        0x1F4CB, 0x1F4CC, 0x1F4CD, 0x1F4CE, 0x1F4CF, 0x1F4D0, 0x1F4D1, 0x1F4D2, 0x1F4D3, 0x1F4D4,
+        0x1F4D5, 0x1F4D6, 0x1F4D7, 0x1F4D8, 0x1F4D9, 0x1F4DA, 0x1F4DB, 0x1F4DC, 0x1F4DD, 0x1F4DE,
+        0x1F4DF, 0x1F4E0, 0x1F4E1, 0x1F4E2, 0x1F4E3, 0x1F4E4, 0x1F4E5, 0x1F4E6, 0x1F4E7, 0x1F4E8,
+        0x1F4E9, 0x1F4EA, 0x1F4EB, 0x1F4EC, 0x1F4ED, 0x1F4EE, 0x1F4EF, 0x1F4F0, 0x1F4F1, 0x1F4F2,
+        0x1F4F3, 0x1F4F4, 0x1F4F5, 0x1F4F6, 0x1F4F7, 0x1F4F8, 0x1F4F9, 0x1F4FA, 0x1F4FB, 0x1F4FC,
+        0x1F4FD, 0x1F4FF, 0x1F500, 0x1F501, 0x1F502, 0x1F503, 0x1F504, 0x1F505, 0x1F506, 0x1F507,
+        0x1F508, 0x1F509, 0x1F50A, 0x1F50B, 0x1F50C, 0x1F50D, 0x1F50E, 0x1F50F, 0x1F510, 0x1F511,
+        0x1F512, 0x1F513, 0x1F514, 0x1F515, 0x1F516, 0x1F517, 0x1F518, 0x1F519, 0x1F51A, 0x1F51B,
+        0x1F51C, 0x1F51D, 0x1F51E, 0x1F51F, 0x1F520, 0x1F521, 0x1F522, 0x1F523, 0x1F524, 0x1F525,
+        0x1F526, 0x1F527, 0x1F528, 0x1F529, 0x1F52A, 0x1F52B, 0x1F52C, 0x1F52D, 0x1F52E, 0x1F52F,
+        0x1F530, 0x1F531, 0x1F532, 0x1F533, 0x1F534, 0x1F535, 0x1F536, 0x1F537, 0x1F538, 0x1F539,
+        0x1F53A, 0x1F53B, 0x1F53C, 0x1F53D, 0x1F549, 0x1F54A, 0x1F54B, 0x1F54C, 0x1F54D, 0x1F54E,
+        0x1F550, 0x1F551, 0x1F552, 0x1F553, 0x1F554, 0x1F555, 0x1F556, 0x1F557, 0x1F558, 0x1F559,
+        0x1F55A, 0x1F55B, 0x1F55C, 0x1F55D, 0x1F55E, 0x1F55F, 0x1F560, 0x1F561, 0x1F562, 0x1F563,
+        0x1F564, 0x1F565, 0x1F566, 0x1F567, 0x1F56F, 0x1F570, 0x1F573, 0x1F574, 0x1F575, 0x1F576,
+        0x1F577, 0x1F578, 0x1F579, 0x1F57A, 0x1F587, 0x1F58A, 0x1F58B, 0x1F58C, 0x1F58D, 0x1F590,
+        0x1F595, 0x1F596, 0x1F5A4, 0x1F5A5, 0x1F5A8, 0x1F5B1, 0x1F5B2, 0x1F5BC, 0x1F5C2, 0x1F5C3,
+        0x1F5C4, 0x1F5D1, 0x1F5D2, 0x1F5D3, 0x1F5DC, 0x1F5DD, 0x1F5DE, 0x1F5E1, 0x1F5E3, 0x1F5E8,
+        0x1F5EF, 0x1F5F3, 0x1F5FA, 0x1F5FB, 0x1F5FC, 0x1F5FD, 0x1F5FE, 0x1F5FF, 0x1F600, 0x1F601,
+        0x1F602, 0x1F603, 0x1F604, 0x1F605, 0x1F606, 0x1F607, 0x1F608, 0x1F609, 0x1F60A, 0x1F60B,
+        0x1F60C, 0x1F60D, 0x1F60E, 0x1F60F, 0x1F610, 0x1F611, 0x1F612, 0x1F613, 0x1F614, 0x1F615,
+        0x1F616, 0x1F617, 0x1F618, 0x1F619, 0x1F61A, 0x1F61B, 0x1F61C, 0x1F61D, 0x1F61E, 0x1F61F,
+        0x1F620, 0x1F621, 0x1F622, 0x1F623, 0x1F624, 0x1F625, 0x1F626, 0x1F627, 0x1F628, 0x1F629,
+        0x1F62A, 0x1F62B, 0x1F62C, 0x1F62D, 0x1F62E, 0x1F62F, 0x1F630, 0x1F631, 0x1F632, 0x1F633,
+        0x1F634, 0x1F635, 0x1F636, 0x1F637, 0x1F638, 0x1F639, 0x1F63A, 0x1F63B, 0x1F63C, 0x1F63D,
+        0x1F63E, 0x1F63F, 0x1F640, 0x1F641, 0x1F642, 0x1F643, 0x1F644, 0x1F645, 0x1F646, 0x1F647,
+        0x1F648, 0x1F649, 0x1F64A, 0x1F64B, 0x1F64C, 0x1F64D, 0x1F64E, 0x1F64F, 0x1F680, 0x1F681,
+        0x1F682, 0x1F683, 0x1F684, 0x1F685, 0x1F686, 0x1F687, 0x1F688, 0x1F689, 0x1F68A, 0x1F68B,
+        0x1F68C, 0x1F68D, 0x1F68E, 0x1F68F, 0x1F690, 0x1F691, 0x1F692, 0x1F693, 0x1F694, 0x1F695,
+        0x1F696, 0x1F697, 0x1F698, 0x1F699, 0x1F69A, 0x1F69B, 0x1F69C, 0x1F69D, 0x1F69E, 0x1F69F,
+        0x1F6A0, 0x1F6A1, 0x1F6A2, 0x1F6A3, 0x1F6A4, 0x1F6A5, 0x1F6A6, 0x1F6A7, 0x1F6A8, 0x1F6A9,
+        0x1F6AA, 0x1F6AB, 0x1F6AC, 0x1F6AD, 0x1F6AE, 0x1F6AF, 0x1F6B0, 0x1F6B1, 0x1F6B2, 0x1F6B3,
+        0x1F6B4, 0x1F6B5, 0x1F6B6, 0x1F6B7, 0x1F6B8, 0x1F6B9, 0x1F6BA, 0x1F6BB, 0x1F6BC, 0x1F6BD,
+        0x1F6BE, 0x1F6BF, 0x1F6C0, 0x1F6C1, 0x1F6C2, 0x1F6C3, 0x1F6C4, 0x1F6C5, 0x1F6CB, 0x1F6CC,
+        0x1F6CD, 0x1F6CE, 0x1F6CF, 0x1F6D0, 0x1F6D1, 0x1F6D2, 0x1F6E0, 0x1F6E1, 0x1F6E2, 0x1F6E3,
+        0x1F6E4, 0x1F6E5, 0x1F6E9, 0x1F6EB, 0x1F6EC, 0x1F6F0, 0x1F6F3, 0x1F6F4, 0x1F6F5, 0x1F6F6,
+        0x1F910, 0x1F911, 0x1F912, 0x1F913, 0x1F914, 0x1F915, 0x1F916, 0x1F917, 0x1F918, 0x1F919,
+        0x1F91A, 0x1F91B, 0x1F91C, 0x1F91D, 0x1F91E, 0x1F920, 0x1F921, 0x1F922, 0x1F923, 0x1F924,
+        0x1F925, 0x1F926, 0x1F927, 0x1F930, 0x1F933, 0x1F934, 0x1F935, 0x1F936, 0x1F937, 0x1F938,
+        0x1F939, 0x1F93A, 0x1F93B, 0x1F93C, 0x1F93D, 0x1F93E, 0x1F940, 0x1F941, 0x1F942, 0x1F943,
+        0x1F944, 0x1F945, 0x1F946, 0x1F947, 0x1F948, 0x1F949, 0x1F94A, 0x1F94B, 0x1F950, 0x1F951,
+        0x1F952, 0x1F953, 0x1F954, 0x1F955, 0x1F956, 0x1F957, 0x1F958, 0x1F959, 0x1F95A, 0x1F95B,
+        0x1F95C, 0x1F95D, 0x1F95E, 0x1F980, 0x1F981, 0x1F982, 0x1F983, 0x1F984, 0x1F985, 0x1F986,
+        0x1F987, 0x1F988, 0x1F989, 0x1F98A, 0x1F98B, 0x1F98C, 0x1F98D, 0x1F98E, 0x1F98F, 0x1F990,
+        0x1F991, 0x1F9C0
     };
 
     // See http://www.unicode.org/Public/emoji/3.0/emoji-data.txt
diff --git a/core/java/android/text/method/BaseKeyListener.java b/core/java/android/text/method/BaseKeyListener.java
index 3770a45..2f327f3 100644
--- a/core/java/android/text/method/BaseKeyListener.java
+++ b/core/java/android/text/method/BaseKeyListener.java
@@ -129,8 +129,8 @@
         // The offset is immediately before a variation selector.
         final int STATE_BEFORE_VS = 6;
 
-        // The offset is immediately before a ZWJ emoji.
-        final int STATE_BEFORE_ZWJ_EMOJI = 7;
+        // The offset is immediately before an emoji.
+        final int STATE_BEFORE_EMOJI = 7;
         // The offset is immediately before a ZWJ that were seen before a ZWJ emoji.
         final int STATE_BEFORE_ZWJ = 8;
         // The offset is immediately before a variation selector and a ZWJ that were seen before a
@@ -169,7 +169,7 @@
                     } else if (codePoint == Emoji.COMBINING_ENCLOSING_KEYCAP) {
                         state = STATE_BEFORE_KEYCAP;
                     } else if (Emoji.isEmoji(codePoint)) {
-                        state = STATE_BEFORE_ZWJ_EMOJI;
+                        state = STATE_BEFORE_EMOJI;
                     } else {
                         state = STATE_FINISHED;
                     }
@@ -232,7 +232,7 @@
                 case STATE_BEFORE_VS:
                     if (Emoji.isEmoji(codePoint)) {
                         deleteCharCount += Character.charCount(codePoint);
-                        state = STATE_BEFORE_ZWJ_EMOJI;
+                        state = STATE_BEFORE_EMOJI;
                         break;
                     }
 
@@ -242,7 +242,7 @@
                     }
                     state = STATE_FINISHED;
                     break;
-                case STATE_BEFORE_ZWJ_EMOJI:
+                case STATE_BEFORE_EMOJI:
                     if (codePoint == Emoji.ZERO_WIDTH_JOINER) {
                         state = STATE_BEFORE_ZWJ;
                     } else {
@@ -252,7 +252,8 @@
                 case STATE_BEFORE_ZWJ:
                     if (Emoji.isEmoji(codePoint)) {
                         deleteCharCount += Character.charCount(codePoint) + 1;  // +1 for ZWJ.
-                        state = STATE_BEFORE_ZWJ_EMOJI;
+                        state = Emoji.isEmojiModifier(codePoint) ?
+                                STATE_BEFORE_EMOJI_MODIFIER : STATE_BEFORE_EMOJI;
                     } else if (isVariationSelector(codePoint)) {
                         lastSeenVSCharCount = Character.charCount(codePoint);
                         state = STATE_BEFORE_VS_AND_ZWJ;
@@ -265,7 +266,7 @@
                         // +1 for ZWJ.
                         deleteCharCount += lastSeenVSCharCount + 1 + Character.charCount(codePoint);
                         lastSeenVSCharCount = 0;
-                        state = STATE_BEFORE_ZWJ_EMOJI;
+                        state = STATE_BEFORE_EMOJI;
                     } else {
                         state = STATE_FINISHED;
                     }
diff --git a/core/java/android/transition/Transition.java b/core/java/android/transition/Transition.java
index 316c7e3..8823605 100644
--- a/core/java/android/transition/Transition.java
+++ b/core/java/android/transition/Transition.java
@@ -1941,6 +1941,26 @@
     }
 
     /**
+     * Force the transition to move to its end state, ending all the animators.
+     *
+     * @hide
+     */
+    void forceToEnd(ViewGroup sceneRoot) {
+        ArrayMap<Animator, AnimationInfo> runningAnimators = getRunningAnimators();
+        int numOldAnims = runningAnimators.size();
+        if (sceneRoot != null) {
+            WindowId windowId = sceneRoot.getWindowId();
+            for (int i = numOldAnims - 1; i >= 0; i--) {
+                AnimationInfo info = runningAnimators.valueAt(i);
+                if (info.view != null && windowId != null && windowId.equals(info.windowId)) {
+                    Animator anim = runningAnimators.keyAt(i);
+                    anim.end();
+                }
+            }
+        }
+    }
+
+    /**
      * This method cancels a transition that is currently running.
      *
      * @hide
diff --git a/core/java/android/transition/TransitionManager.java b/core/java/android/transition/TransitionManager.java
index 71c8099..f2c871e3 100644
--- a/core/java/android/transition/TransitionManager.java
+++ b/core/java/android/transition/TransitionManager.java
@@ -440,7 +440,7 @@
             ArrayList<Transition> copy = new ArrayList(runningTransitions);
             for (int i = copy.size() - 1; i >= 0; i--) {
                 final Transition transition = copy.get(i);
-                transition.end();
+                transition.forceToEnd(sceneRoot);
             }
         }
 
diff --git a/core/java/android/transition/TransitionSet.java b/core/java/android/transition/TransitionSet.java
index 583dc0f..a41fe64 100644
--- a/core/java/android/transition/TransitionSet.java
+++ b/core/java/android/transition/TransitionSet.java
@@ -16,8 +16,6 @@
 
 package android.transition;
 
-import com.android.internal.R;
-
 import android.animation.TimeInterpolator;
 import android.content.Context;
 import android.content.res.TypedArray;
@@ -26,6 +24,8 @@
 import android.view.View;
 import android.view.ViewGroup;
 
+import com.android.internal.R;
+
 import java.util.ArrayList;
 
 /**
@@ -498,6 +498,16 @@
         }
     }
 
+    /** @hide */
+    @Override
+    void forceToEnd(ViewGroup sceneRoot) {
+        super.forceToEnd(sceneRoot);
+        int numTransitions = mTransitions.size();
+        for (int i = 0; i < numTransitions; ++i) {
+            mTransitions.get(i).forceToEnd(sceneRoot);
+        }
+    }
+
     @Override
     TransitionSet setSceneRoot(ViewGroup sceneRoot) {
         super.setSceneRoot(sceneRoot);
diff --git a/core/java/android/util/DisplayMetrics.java b/core/java/android/util/DisplayMetrics.java
index d201ade..f4db4d6 100644
--- a/core/java/android/util/DisplayMetrics.java
+++ b/core/java/android/util/DisplayMetrics.java
@@ -66,9 +66,23 @@
      * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
      * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
      */
+    public static final int DENSITY_260 = 260;
+
+    /**
+     * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
+     * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
+     * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
+     */
     public static final int DENSITY_280 = 280;
 
     /**
+     * Intermediate density for screens that sit between {@link #DENSITY_HIGH} (240dpi) and
+     * {@link #DENSITY_XHIGH} (320dpi). This is not a density that applications should target,
+     * instead relying on the system to scale their {@link #DENSITY_XHIGH} assets for them.
+     */
+    public static final int DENSITY_300 = 300;
+
+    /**
      * Standard quantized DPI for extra-high-density screens.
      */
     public static final int DENSITY_XHIGH = 320;
@@ -79,6 +93,14 @@
      * This is not a density that applications should target, instead relying
      * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
      */
+    public static final int DENSITY_340 = 340;
+
+    /**
+     * Intermediate density for screens that sit somewhere between
+     * {@link #DENSITY_XHIGH} (320 dpi) and {@link #DENSITY_XXHIGH} (480 dpi).
+     * This is not a density that applications should target, instead relying
+     * on the system to scale their {@link #DENSITY_XXHIGH} assets for them.
+     */
     public static final int DENSITY_360 = 360;
 
     /**
diff --git a/core/java/android/util/PackageUtils.java b/core/java/android/util/PackageUtils.java
new file mode 100644
index 0000000..6531aef
--- /dev/null
+++ b/core/java/android/util/PackageUtils.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.util;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.Signature;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * Helper functions applicable to packages.
+ * @hide
+ */
+public final class PackageUtils {
+    private final static char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
+
+    private PackageUtils() {
+        /* hide constructor */
+    }
+
+    /**
+     * Computes the SHA256 digest of the signing cert for a package.
+     * @param packageManager The package manager.
+     * @param packageName The package for which to generate the digest.
+     * @param userId The user for which to generate the digest.
+     * @return The digest or null if the package does not exist for this user.
+     */
+    public static @Nullable String computePackageCertSha256Digest(
+            @NonNull PackageManager packageManager,
+            @NonNull String packageName, int userId) {
+        final PackageInfo packageInfo;
+        try {
+            packageInfo = packageManager.getPackageInfoAsUser(packageName,
+                    PackageManager.GET_SIGNATURES, userId);
+        } catch (PackageManager.NameNotFoundException e) {
+            return null;
+        }
+        return computeCertSha256Digest(packageInfo.signatures[0]);
+    }
+
+    /**
+     * Computes the SHA256 digest of a cert.
+     * @param signature The signature.
+     * @return The digest or null if an error occurs.
+     */
+    public static @Nullable String computeCertSha256Digest(@NonNull Signature signature) {
+        return computeSha256Digest(signature.toByteArray());
+    }
+
+    /**
+     * Computes the SHA256 digest of some data.
+     * @param data The data.
+     * @return The digest or null if an error occurs.
+     */
+    public static @Nullable String computeSha256Digest(@NonNull byte[] data) {
+        MessageDigest messageDigest;
+        try {
+            messageDigest = MessageDigest.getInstance("SHA256");
+        } catch (NoSuchAlgorithmException e) {
+            /* can't happen */
+            return null;
+        }
+
+        messageDigest.update(data);
+
+        final byte[] digest = messageDigest.digest();
+        final int digestLength = digest.length;
+        final int charCount = 2 * digestLength;
+
+        final char[] chars = new char[charCount];
+        for (int i = 0; i < digestLength; i++) {
+            final int byteHex = digest[i] & 0xFF;
+            chars[i * 2] = HEX_ARRAY[byteHex >>> 4];
+            chars[i * 2 + 1] = HEX_ARRAY[byteHex & 0x0F];
+        }
+        return new String(chars);
+    }
+}
diff --git a/core/java/android/view/Choreographer.java b/core/java/android/view/Choreographer.java
index d3db74d..3316f3a 100644
--- a/core/java/android/view/Choreographer.java
+++ b/core/java/android/view/Choreographer.java
@@ -25,6 +25,7 @@
 import android.os.Trace;
 import android.util.Log;
 import android.util.TimeUtils;
+import android.view.animation.AnimationUtils;
 
 import java.io.PrintWriter;
 
@@ -608,6 +609,7 @@
 
         try {
             Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Choreographer#doFrame");
+            AnimationUtils.lockAnimationClock(frameTimeNanos / TimeUtils.NANOS_PER_MS);
 
             mFrameInfo.markInputHandlingStart();
             doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos);
@@ -620,6 +622,7 @@
 
             doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos);
         } finally {
+            AnimationUtils.unlockAnimationClock();
             Trace.traceEnd(Trace.TRACE_TAG_VIEW);
         }
 
diff --git a/core/java/android/view/Display.java b/core/java/android/view/Display.java
index 2f2fe57..85a4bf9 100644
--- a/core/java/android/view/Display.java
+++ b/core/java/android/view/Display.java
@@ -36,7 +36,7 @@
 import java.lang.annotation.RetentionPolicy;
 import java.util.Arrays;
 
-import static android.Manifest.permission.CONFIGURE_DISPLAY_COLOR_TRANSFORM;
+import static android.Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE;
 
 /**
  * Provides information about the size and density of a logical display.
@@ -284,6 +284,27 @@
      */
     public static final int STATE_DOZE_SUSPEND = 4;
 
+    /* The color mode constants defined below must be kept in sync with the ones in
+     * system/graphics.h */
+
+    /**
+     * Display color mode: The current color mode is unknown or invalid.
+     * @hide
+     */
+    public static final int COLOR_MODE_INVALID = -1;
+
+    /**
+     * Display color mode: The default or native gamut of the display.
+     * @hide
+     */
+    public static final int COLOR_MODE_DEFAULT = 0;
+
+    /**
+     * Display color mode: SRGB
+     * @hide
+     */
+    public static final int COLOR_MODE_SRGB = 7;
+
     /**
      * Internal method to create a display.
      * Applications should use {@link android.view.WindowManager#getDefaultDisplay()}
@@ -463,20 +484,29 @@
 
     /**
      * Gets the size of the display, in pixels.
+     * Value returned by this method does not necessarily represent the actual raw size
+     * (native resolution) of the display.
      * <p>
-     * Note that this value should <em>not</em> be used for computing layouts,
-     * since a device will typically have screen decoration (such as a status bar)
-     * along the edges of the display that reduce the amount of application
-     * space available from the size returned here.  Layouts should instead use
-     * the window size.
+     * 1. The returned size may be adjusted to exclude certain system decor elements
+     * that are always visible.
      * </p><p>
-     * The size is adjusted based on the current rotation of the display.
-     * </p><p>
-     * The size returned by this method does not necessarily represent the
-     * actual raw size (native resolution) of the display.  The returned size may
-     * be adjusted to exclude certain system decoration elements that are always visible.
-     * It may also be scaled to provide compatibility with older applications that
+     * 2. It may be scaled to provide compatibility with older applications that
      * were originally designed for smaller displays.
+     * </p><p>
+     * 3. It can be different depending on the WindowManager to which the display belongs.
+     * </p><p>
+     * - If requested from non-Activity context (e.g. Application context via
+     * {@code (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)})
+     * it will report the size of the entire display based on current rotation and with subtracted
+     * system decoration areas.
+     * </p><p>
+     * - If requested from activity (either using {@code getWindowManager()} or
+     * {@code (WindowManager) getSystemService(Context.WINDOW_SERVICE)}) resulting size will
+     * correspond to current app window size. In this case it can be smaller than physical size in
+     * multi-window mode.
+     * </p><p>
+     * Typically for the purposes of layout apps should make a request from activity context
+     * to obtain size available for the app content.
      * </p>
      *
      * @param outSize A {@link Point} object to receive the size information.
@@ -687,33 +717,22 @@
     }
 
     /**
-     * Request the display applies a color transform.
+     * Request the display applies a color mode.
      * @hide
      */
-    @RequiresPermission(CONFIGURE_DISPLAY_COLOR_TRANSFORM)
-    public void requestColorTransform(ColorTransform colorTransform) {
-        mGlobal.requestColorTransform(mDisplayId, colorTransform.getId());
+    @RequiresPermission(CONFIGURE_DISPLAY_COLOR_MODE)
+    public void requestColorMode(int colorMode) {
+        mGlobal.requestColorMode(mDisplayId, colorMode);
     }
 
     /**
-     * Returns the active color transform of this display
+     * Returns the active color mode of this display
      * @hide
      */
-    public ColorTransform getColorTransform() {
+    public int getColorMode() {
         synchronized (this) {
             updateDisplayInfoLocked();
-            return mDisplayInfo.getColorTransform();
-        }
-    }
-
-    /**
-     * Returns the default color transform of this display
-     * @hide
-     */
-    public ColorTransform getDefaultColorTransform() {
-        synchronized (this) {
-            updateDisplayInfoLocked();
-            return mDisplayInfo.getDefaultColorTransform();
+            return mDisplayInfo.colorMode;
         }
     }
 
@@ -728,14 +747,14 @@
     }
 
     /**
-     * Gets the supported color transforms of this device.
+     * Gets the supported color modes of this device.
      * @hide
      */
-    public ColorTransform[] getSupportedColorTransforms() {
+    public int[] getSupportedColorModes() {
         synchronized (this) {
             updateDisplayInfoLocked();
-            ColorTransform[] transforms = mDisplayInfo.supportedColorTransforms;
-            return Arrays.copyOf(transforms, transforms.length);
+            int[] colorModes = mDisplayInfo.supportedColorModes;
+            return Arrays.copyOf(colorModes, colorModes.length);
         }
     }
 
@@ -785,13 +804,16 @@
      * were originally designed for smaller displays.
      * </p><p>
      * 3. It can be different depending on the WindowManager to which the display belongs.
-     * <pre>
+     * </p><p>
      * - If requested from non-Activity context (e.g. Application context via
      * {@code (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)})
-     * metrics will report real size of the display based on current rotation.
-     * - If requested from activity resulting metrics will correspond to current window metrics.
-     * In this case the size can be smaller than physical size in multi-window mode.
-     * </pre>
+     * metrics will report the size of the entire display based on current rotation and with
+     * subtracted system decoration areas.
+     * </p><p>
+     * - If requested from activity (either using {@code getWindowManager()} or
+     * {@code (WindowManager) getSystemService(Context.WINDOW_SERVICE)}) resulting metrics will
+     * correspond to current app window metrics. In this case the size can be smaller than physical
+     * size in multi-window mode.
      * </p>
      *
      * @param outMetrics A {@link DisplayMetrics} object to receive the metrics.
@@ -1205,6 +1227,33 @@
             return mMinLuminance;
         }
 
+        @Override
+        public boolean equals(Object other) {
+            if (this == other) {
+                return true;
+            }
+
+            if (!(other instanceof HdrCapabilities)) {
+                return false;
+            }
+            HdrCapabilities that = (HdrCapabilities) other;
+
+            return Arrays.equals(mSupportedHdrTypes, that.mSupportedHdrTypes)
+                && mMaxLuminance == that.mMaxLuminance
+                && mMaxAverageLuminance == that.mMaxAverageLuminance
+                && mMinLuminance == that.mMinLuminance;
+        }
+
+        @Override
+        public int hashCode() {
+            int hash = 23;
+            hash = hash * 17 + Arrays.hashCode(mSupportedHdrTypes);
+            hash = hash * 17 + Float.floatToIntBits(mMaxLuminance);
+            hash = hash * 17 + Float.floatToIntBits(mMaxAverageLuminance);
+            hash = hash * 17 + Float.floatToIntBits(mMinLuminance);
+            return hash;
+        }
+
         public static final Creator<HdrCapabilities> CREATOR = new Creator<HdrCapabilities>() {
             @Override
             public HdrCapabilities createFromParcel(Parcel source) {
@@ -1251,89 +1300,4 @@
             return 0;
         }
     }
-
-    /**
-     * A color transform supported by a given display.
-     *
-     * @see Display#getSupportedColorTransforms()
-     * @hide
-     */
-    public static final class ColorTransform implements Parcelable {
-        public static final ColorTransform[] EMPTY_ARRAY = new ColorTransform[0];
-
-        private final int mId;
-        private final int mColorTransform;
-
-        public ColorTransform(int id, int colorTransform) {
-            mId = id;
-            mColorTransform = colorTransform;
-        }
-
-        public int getId() {
-            return mId;
-        }
-
-        public int getColorTransform() {
-            return mColorTransform;
-        }
-
-        @Override
-        public boolean equals(Object other) {
-            if (this == other) {
-                return true;
-            }
-            if (!(other instanceof ColorTransform)) {
-                return false;
-            }
-            ColorTransform that = (ColorTransform) other;
-            return mId == that.mId
-                && mColorTransform == that.mColorTransform;
-        }
-
-        @Override
-        public int hashCode() {
-            int hash = 1;
-            hash = hash * 17 + mId;
-            hash = hash * 17 + mColorTransform;
-            return hash;
-        }
-
-        @Override
-        public String toString() {
-            return new StringBuilder("{")
-                    .append("id=").append(mId)
-                    .append(", colorTransform=").append(mColorTransform)
-                    .append("}")
-                    .toString();
-        }
-
-        @Override
-        public int describeContents() {
-            return 0;
-        }
-
-        private ColorTransform(Parcel in) {
-            this(in.readInt(), in.readInt());
-        }
-
-        @Override
-        public void writeToParcel(Parcel out, int parcelableFlags) {
-            out.writeInt(mId);
-            out.writeInt(mColorTransform);
-        }
-
-        @SuppressWarnings("hiding")
-        public static final Parcelable.Creator<ColorTransform> CREATOR
-                = new Parcelable.Creator<ColorTransform>() {
-            @Override
-            public ColorTransform createFromParcel(Parcel in) {
-                return new ColorTransform(in);
-            }
-
-            @Override
-            public ColorTransform[] newArray(int size) {
-                return new ColorTransform[size];
-            }
-        };
-    }
 }
diff --git a/core/java/android/view/DisplayInfo.java b/core/java/android/view/DisplayInfo.java
index 8aeeffd..bc40849 100644
--- a/core/java/android/view/DisplayInfo.java
+++ b/core/java/android/view/DisplayInfo.java
@@ -169,14 +169,11 @@
      */
     public Display.Mode[] supportedModes = Display.Mode.EMPTY_ARRAY;
 
-    /** The active color transform. */
-    public int colorTransformId;
+    /** The active color mode. */
+    public int colorMode;
 
-    /** The default color transform. */
-    public int defaultColorTransformId;
-
-    /** The list of supported color transforms */
-    public Display.ColorTransform[] supportedColorTransforms = Display.ColorTransform.EMPTY_ARRAY;
+    /** The list of supported color modes */
+    public int[] supportedColorModes = { Display.COLOR_MODE_DEFAULT };
 
     /** The display's HDR capabilities */
     public Display.HdrCapabilities hdrCapabilities;
@@ -291,8 +288,8 @@
                 && rotation == other.rotation
                 && modeId == other.modeId
                 && defaultModeId == other.defaultModeId
-                && colorTransformId == other.colorTransformId
-                && defaultColorTransformId == other.defaultColorTransformId
+                && colorMode == other.colorMode
+                && Arrays.equals(supportedColorModes, other.supportedColorModes)
                 && Objects.equal(hdrCapabilities, other.hdrCapabilities)
                 && logicalDensityDpi == other.logicalDensityDpi
                 && physicalXDpi == other.physicalXDpi
@@ -332,10 +329,9 @@
         modeId = other.modeId;
         defaultModeId = other.defaultModeId;
         supportedModes = Arrays.copyOf(other.supportedModes, other.supportedModes.length);
-        colorTransformId = other.colorTransformId;
-        defaultColorTransformId = other.defaultColorTransformId;
-        supportedColorTransforms = Arrays.copyOf(
-                other.supportedColorTransforms, other.supportedColorTransforms.length);
+        colorMode = other.colorMode;
+        supportedColorModes = Arrays.copyOf(
+                other.supportedColorModes, other.supportedColorModes.length);
         hdrCapabilities = other.hdrCapabilities;
         logicalDensityDpi = other.logicalDensityDpi;
         physicalXDpi = other.physicalXDpi;
@@ -373,12 +369,11 @@
         for (int i = 0; i < nModes; i++) {
             supportedModes[i] = Display.Mode.CREATOR.createFromParcel(source);
         }
-        colorTransformId = source.readInt();
-        defaultColorTransformId = source.readInt();
-        int nColorTransforms = source.readInt();
-        supportedColorTransforms = new Display.ColorTransform[nColorTransforms];
-        for (int i = 0; i < nColorTransforms; i++) {
-            supportedColorTransforms[i] = Display.ColorTransform.CREATOR.createFromParcel(source);
+        colorMode = source.readInt();
+        int nColorModes = source.readInt();
+        supportedColorModes = new int[nColorModes];
+        for (int i = 0; i < nColorModes; i++) {
+            supportedColorModes[i] = source.readInt();
         }
         hdrCapabilities = source.readParcelable(null);
         logicalDensityDpi = source.readInt();
@@ -418,11 +413,10 @@
         for (int i = 0; i < supportedModes.length; i++) {
             supportedModes[i].writeToParcel(dest, flags);
         }
-        dest.writeInt(colorTransformId);
-        dest.writeInt(defaultColorTransformId);
-        dest.writeInt(supportedColorTransforms.length);
-        for (int i = 0; i < supportedColorTransforms.length; i++) {
-            supportedColorTransforms[i].writeToParcel(dest, flags);
+        dest.writeInt(colorMode);
+        dest.writeInt(supportedColorModes.length);
+        for (int i = 0; i < supportedColorModes.length; i++) {
+            dest.writeInt(supportedColorModes[i]);
         }
         dest.writeParcelable(hdrCapabilities, flags);
         dest.writeInt(logicalDensityDpi);
@@ -496,24 +490,6 @@
         return result;
     }
 
-    public Display.ColorTransform getColorTransform() {
-        return findColorTransform(colorTransformId);
-    }
-
-    public Display.ColorTransform getDefaultColorTransform() {
-        return findColorTransform(defaultColorTransformId);
-    }
-
-    private Display.ColorTransform findColorTransform(int colorTransformId) {
-        for (int i = 0; i < supportedColorTransforms.length; i++) {
-            Display.ColorTransform colorTransform = supportedColorTransforms[i];
-            if (colorTransform.getId() == colorTransformId) {
-                return colorTransform;
-            }
-        }
-        throw new IllegalStateException("Unable to locate color transform: " + colorTransformId);
-    }
-
     public void getAppMetrics(DisplayMetrics outMetrics) {
         getAppMetrics(outMetrics, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
     }
@@ -615,12 +591,10 @@
         sb.append(defaultModeId);
         sb.append(", modes ");
         sb.append(Arrays.toString(supportedModes));
-        sb.append(", colorTransformId ");
-        sb.append(colorTransformId);
-        sb.append(", defaultColorTransformId ");
-        sb.append(defaultColorTransformId);
-        sb.append(", supportedColorTransforms ");
-        sb.append(Arrays.toString(supportedColorTransforms));
+        sb.append(", colorMode ");
+        sb.append(colorMode);
+        sb.append(", supportedColorModes ");
+        sb.append(Arrays.toString(supportedColorModes));
         sb.append(", hdrCapabilities ");
         sb.append(hdrCapabilities);
         sb.append(", rotation ");
diff --git a/core/java/android/view/DragEvent.java b/core/java/android/view/DragEvent.java
index a394f35..2baa0b4 100644
--- a/core/java/android/view/DragEvent.java
+++ b/core/java/android/view/DragEvent.java
@@ -134,6 +134,7 @@
 
     Object mLocalState;
     boolean mDragResult;
+    boolean mEventHandlerWasCalled;
 
     private DragEvent mNext;
     private RuntimeException mRecycledLocation;
@@ -152,12 +153,16 @@
      * if it can accept a drop. The onDragEvent() or onDrag() methods usually inspect the metadata
      * from {@link #getClipDescription()} to determine if they can accept the data contained in
      * this drag. For an operation that doesn't represent data transfer, these methods may
-     * perform other actions to determine whether or not the View should accept the drag.
+     * perform other actions to determine whether or not the View should accept the data.
      * If the View wants to indicate that it is a valid drop target, it can also react by
      * changing its appearance.
      * <p>
-     * A View only receives further drag events if it returns {@code true} in response to
-     * ACTION_DRAG_STARTED.
+     *  Views added or becoming visible for the first time during a drag operation receive this
+     *  event when they are added or becoming visible.
+     * </p>
+     * <p>
+     *  A View only receives further drag events if it returns {@code true} in response to
+     *  ACTION_DRAG_STARTED.
      * </p>
      * @see #ACTION_DRAG_ENDED
      * @see #getX()
@@ -176,9 +181,10 @@
      * </p>
      * <p>
      * The system stops sending ACTION_DRAG_LOCATION events to a View once the user moves the
-     * drag shadow out of the View object's bounding box. If the user moves the drag shadow back
-     * into the View object's bounding box, the View receives an ACTION_DRAG_ENTERED again before
-     * receiving any more ACTION_DRAG_LOCATION events.
+     * drag shadow out of the View object's bounding box or into a descendant view that can accept
+     * the data. If the user moves the drag shadow back into the View object's bounding box or out
+     * of a descendant view that can accept the data, the View receives an ACTION_DRAG_ENTERED again
+     * before receiving any more ACTION_DRAG_LOCATION events.
      * </p>
      * @see #ACTION_DRAG_ENTERED
      * @see #getX()
@@ -188,7 +194,8 @@
 
     /**
      * Action constant returned by {@link #getAction()}: Signals to a View that the user
-     * has released the drag shadow, and the drag point is within the bounding box of the View.
+     * has released the drag shadow, and the drag point is within the bounding box of the View and
+     * not within a descendant view that can accept the data.
      * The View should retrieve the data from the DragEvent by calling {@link #getClipData()}.
      * The methods {@link #getX()} and {@link #getY()} return the X and Y position of the drop point
      * within the View object's bounding box.
@@ -211,8 +218,10 @@
      * operation has concluded.  A View that changed its appearance during the operation should
      * return to its usual drawing state in response to this event.
      * <p>
-     * All views that received an ACTION_DRAG_STARTED event will receive the
-     * ACTION_DRAG_ENDED event even if they are not currently visible when the drag ends.
+     *  All views with listeners that returned boolean <code>true</code> for the ACTION_DRAG_STARTED
+     *  event will receive the ACTION_DRAG_ENDED event even if they are not currently visible when
+     *  the drag ends. Views removed during the drag operation won't receive the ACTION_DRAG_ENDED
+     *  event.
      * </p>
      * <p>
      *  The View object can call {@link #getResult()} to see the result of the operation.
@@ -233,9 +242,10 @@
      *  drop target.
      * </p>
      * The system stops sending ACTION_DRAG_LOCATION events to a View once the user moves the
-     * drag shadow out of the View object's bounding box. If the user moves the drag shadow back
-     * into the View object's bounding box, the View receives an ACTION_DRAG_ENTERED again before
-     * receiving any more ACTION_DRAG_LOCATION events.
+     * drag shadow out of the View object's bounding box or into a descendant view that can accept
+     * the data. If the user moves the drag shadow back into the View object's bounding box or out
+     * of a descendant view that can accept the data, the View receives an ACTION_DRAG_ENTERED again
+     * before receiving any more ACTION_DRAG_LOCATION events.
      * </p>
      * @see #ACTION_DRAG_ENTERED
      * @see #ACTION_DRAG_LOCATION
@@ -244,7 +254,8 @@
 
     /**
      * Action constant returned by {@link #getAction()}: Signals that the user has moved the
-     * drag shadow outside the bounding box of the View.
+     * drag shadow out of the bounding box of the View or into a descendant view that can accept
+     * the data.
      * The View can react by changing its appearance in a way that tells the user that
      * View is no longer the immediate drop target.
      * <p>
@@ -439,6 +450,7 @@
         mClipData = null;
         mClipDescription = null;
         mLocalState = null;
+        mEventHandlerWasCalled = false;
 
         synchronized (gRecyclerLock) {
             if (gRecyclerUsed < MAX_RECYCLED) {
diff --git a/core/java/android/view/FrameMetrics.java b/core/java/android/view/FrameMetrics.java
index 5c4450a..2b938d0 100644
--- a/core/java/android/view/FrameMetrics.java
+++ b/core/java/android/view/FrameMetrics.java
@@ -198,7 +198,7 @@
         int SWAP_BUFFERS = 12;
         int FRAME_COMPLETED = 13;
 
-        int FRAME_STATS_COUNT = 14; // must always be last
+        int FRAME_STATS_COUNT = 16; // must always be last
     }
 
     /*
diff --git a/core/java/android/view/IWindowManager.aidl b/core/java/android/view/IWindowManager.aidl
index 72126d0..855b1bc 100644
--- a/core/java/android/view/IWindowManager.aidl
+++ b/core/java/android/view/IWindowManager.aidl
@@ -73,8 +73,8 @@
     void clearForcedDisplaySize(int displayId);
     int getInitialDisplayDensity(int displayId);
     int getBaseDisplayDensity(int displayId);
-    void setForcedDisplayDensity(int displayId, int density);
-    void clearForcedDisplayDensity(int displayId);
+    void setForcedDisplayDensityForUser(int displayId, int density, int userId);
+    void clearForcedDisplayDensityForUser(int displayId, int userId);
     void setForcedDisplayScalingMode(int displayId, int mode); // 0 = auto, 1 = disable
 
     void setOverscan(int displayId, int left, int top, int right, int bottom);
@@ -112,7 +112,7 @@
             int requestedOrientation, boolean fullscreen, boolean showWhenLocked, int userId,
             int configChanges, boolean voiceInteraction, boolean launchTaskBehind,
             in Rect taskBounds, in Configuration configuration, int taskResizeMode,
-            boolean alwaysFocusable, boolean homeTask, int targetSdkVersion);
+            boolean alwaysFocusable, boolean homeTask, int targetSdkVersion, int rotationAnimationHint);
     /**
      *
      * @param token The token we are adding to the input task Id.
@@ -173,7 +173,8 @@
             in CompatibilityInfo compatInfo, CharSequence nonLocalizedLabel, int labelRes,
             int icon, int logo, int windowFlags, IBinder transferFrom, boolean createIfNeeded);
     void setAppVisibility(IBinder token, boolean visible);
-    void notifyAppStopped(IBinder token, boolean stopped);
+    void notifyAppResumed(IBinder token, boolean wasStopped, boolean allowSavedSurface);
+    void notifyAppStopped(IBinder token);
     void startAppFreezingScreen(IBinder token, int configChanges);
     void stopAppFreezingScreen(IBinder token, boolean force);
     void removeAppToken(IBinder token);
@@ -306,6 +307,11 @@
     boolean isRotationFrozen();
 
     /**
+     * Screenshot the current wallpaper layer, including the whole screen.
+     */
+    Bitmap screenshotWallpaper();
+
+    /**
      * Used only for assist -- request a screenshot of the current application.
      */
     boolean requestAssistScreenshot(IAssistScreenshotReceiver receiver);
@@ -325,6 +331,16 @@
     oneway void statusBarVisibilityChanged(int visibility);
 
     /**
+     * Called by System UI to notify of changes to the visibility of Recents.
+     */
+    oneway void setRecentsVisibility(boolean visible);
+
+    /**
+     * Called by System UI to notify of changes to the visibility of PIP.
+     */
+    oneway void setTvPipVisibility(boolean visible);
+
+    /**
      * Device has a software navigation bar (separate from the status bar).
      */
     boolean hasNavigationBar();
diff --git a/core/java/android/view/KeyEvent.java b/core/java/android/view/KeyEvent.java
index d9df8e3..b73acda 100644
--- a/core/java/android/view/KeyEvent.java
+++ b/core/java/android/view/KeyEvent.java
@@ -796,8 +796,16 @@
     public static final int KEYCODE_COPY = 278;
     /** Key code constant: Paste key. */
     public static final int KEYCODE_PASTE = 279;
+    /** Key code constant: Consumed by the system for navigation up */
+    public static final int KEYCODE_SYSTEM_NAVIGATION_UP = 280;
+    /** Key code constant: Consumed by the system for navigation down */
+    public static final int KEYCODE_SYSTEM_NAVIGATION_DOWN = 281;
+    /** Key code constant: Consumed by the system for navigation left*/
+    public static final int KEYCODE_SYSTEM_NAVIGATION_LEFT = 282;
+    /** Key code constant: Consumed by the system for navigation right */
+    public static final int KEYCODE_SYSTEM_NAVIGATION_RIGHT = 283;
 
-    private static final int LAST_KEYCODE = KEYCODE_PASTE;
+    private static final int LAST_KEYCODE = KEYCODE_SYSTEM_NAVIGATION_RIGHT;
 
     // NOTE: If you add a new keycode here you must also add it to:
     //  isSystem()
@@ -1845,6 +1853,10 @@
             case KeyEvent.KEYCODE_BRIGHTNESS_DOWN:
             case KeyEvent.KEYCODE_BRIGHTNESS_UP:
             case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
+            case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP:
+            case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN:
+            case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT:
+            case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT:
                 return true;
         }
 
@@ -2930,11 +2942,13 @@
 
     public static final Parcelable.Creator<KeyEvent> CREATOR
             = new Parcelable.Creator<KeyEvent>() {
+        @Override
         public KeyEvent createFromParcel(Parcel in) {
             in.readInt(); // skip token, we already know this is a KeyEvent
             return KeyEvent.createFromParcelBody(in);
         }
 
+        @Override
         public KeyEvent[] newArray(int size) {
             return new KeyEvent[size];
         }
@@ -2958,6 +2972,7 @@
         mEventTime = in.readLong();
     }
 
+    @Override
     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(PARCEL_TOKEN_KEY_EVENT);
 
diff --git a/core/java/android/view/RenderNode.java b/core/java/android/view/RenderNode.java
index ab4cbcf..0164fcd 100644
--- a/core/java/android/view/RenderNode.java
+++ b/core/java/android/view/RenderNode.java
@@ -793,12 +793,12 @@
         return mOwningView != null && mOwningView.mAttachInfo != null;
     }
 
-    public void addAnimator(AnimatedVectorDrawable.VectorDrawableAnimatorRT animatorSet) {
+    public void registerVectorDrawableAnimator(
+            AnimatedVectorDrawable.VectorDrawableAnimatorRT animatorSet) {
         if (mOwningView == null || mOwningView.mAttachInfo == null) {
             throw new IllegalStateException("Cannot start this animator on a detached view!");
         }
-        nAddAnimator(mNativeRenderNode, animatorSet.getAnimatorNativePtr());
-        mOwningView.mAttachInfo.mViewRootImpl.registerAnimatingRenderNode(this);
+        mOwningView.mAttachInfo.mViewRootImpl.registerVectorDrawableAnimator(animatorSet);
     }
 
     public void endAllAnimators() {
diff --git a/core/java/android/view/RoundScrollbarRenderer.java b/core/java/android/view/RoundScrollbarRenderer.java
new file mode 100644
index 0000000..b77be8c
--- /dev/null
+++ b/core/java/android/view/RoundScrollbarRenderer.java
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.view;
+
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.RectF;
+import android.graphics.Rect;
+
+/**
+ * Helper class for drawing round scroll bars on round Wear devices.
+ */
+class RoundScrollbarRenderer {
+    // The range of the scrollbar position represented as an angle in degrees.
+    private static final int SCROLLBAR_ANGLE_RANGE = 90;
+    private static final int MAX_SCROLLBAR_ANGLE_SWIPE = 16;
+    private static final int MIN_SCROLLBAR_ANGLE_SWIPE = 6;
+    private static final float WIDTH_PERCENTAGE = 0.02f;
+    private static final int DEFAULT_THUMB_COLOR = 0xFF757575;
+    private static final int DEFAULT_TRACK_COLOR = 0x21FFFFFF;
+
+    private final Paint mThumbPaint = new Paint();
+    private final Paint mTrackPaint = new Paint();
+    private final RectF mRect = new RectF();
+    private final View mParent;
+
+    public RoundScrollbarRenderer(View parent) {
+        // Paints for the round scrollbar.
+        // Set up the thumb paint
+        mThumbPaint.setAntiAlias(true);
+        mThumbPaint.setStrokeCap(Paint.Cap.ROUND);
+        mThumbPaint.setStyle(Paint.Style.STROKE);
+
+        // Set up the track paint
+        mTrackPaint.setAntiAlias(true);
+        mTrackPaint.setStrokeCap(Paint.Cap.ROUND);
+        mTrackPaint.setStyle(Paint.Style.STROKE);
+
+        mParent = parent;
+    }
+
+    public void drawRoundScrollbars(Canvas canvas, float alpha, Rect bounds) {
+        if (alpha == 0) {
+            return;
+        }
+        // Get information about the current scroll state of the parent view.
+        float maxScroll = mParent.computeVerticalScrollRange();
+        float scrollExtent = mParent.computeVerticalScrollExtent();
+        if (scrollExtent <= 0 || maxScroll <= scrollExtent) {
+            return;
+        }
+        float currentScroll = Math.max(0, mParent.computeVerticalScrollOffset());
+        float linearThumbLength = mParent.computeVerticalScrollExtent();
+        float thumbWidth = mParent.getWidth() * WIDTH_PERCENTAGE;
+        mThumbPaint.setStrokeWidth(thumbWidth);
+        mTrackPaint.setStrokeWidth(thumbWidth);
+
+        setThumbColor(applyAlpha(DEFAULT_THUMB_COLOR, alpha));
+        setTrackColor(applyAlpha(DEFAULT_TRACK_COLOR, alpha));
+
+        // Normalize the sweep angle for the scroll bar.
+        float sweepAngle = (linearThumbLength / maxScroll) * SCROLLBAR_ANGLE_RANGE;
+        sweepAngle = clamp(sweepAngle, MIN_SCROLLBAR_ANGLE_SWIPE, MAX_SCROLLBAR_ANGLE_SWIPE);
+        // Normalize the start angle so that it falls on the track.
+        float startAngle = (currentScroll * (SCROLLBAR_ANGLE_RANGE - sweepAngle))
+                / (maxScroll - linearThumbLength) - SCROLLBAR_ANGLE_RANGE / 2;
+        startAngle = clamp(startAngle, -SCROLLBAR_ANGLE_RANGE / 2,
+                SCROLLBAR_ANGLE_RANGE / 2 - sweepAngle);
+
+        // Draw the track and the scroll bar.
+        mRect.set(
+                bounds.left - thumbWidth / 2,
+                bounds.top,
+                bounds.right - thumbWidth / 2,
+                bounds.bottom);
+
+        canvas.drawArc(mRect, -SCROLLBAR_ANGLE_RANGE / 2, SCROLLBAR_ANGLE_RANGE, false,
+                mTrackPaint);
+        canvas.drawArc(mRect, startAngle, sweepAngle, false, mThumbPaint);
+    }
+
+    private static float clamp(float val, float min, float max) {
+        if (val < min) {
+            return min;
+        } else if (val > max) {
+            return max;
+        } else {
+            return val;
+        }
+    }
+
+    private static int applyAlpha(int color, float alpha) {
+        int alphaByte = (int) (Color.alpha(color) * alpha);
+        return Color.argb(alphaByte, Color.red(color), Color.green(color), Color.blue(color));
+    }
+
+    private void setThumbColor(int thumbColor) {
+        if (mThumbPaint.getColor() != thumbColor) {
+            mThumbPaint.setColor(thumbColor);
+        }
+    }
+
+    private void setTrackColor(int trackColor) {
+        if (mTrackPaint.getColor() != trackColor) {
+            mTrackPaint.setColor(trackColor);
+        }
+    }
+}
diff --git a/core/java/android/view/Surface.java b/core/java/android/view/Surface.java
index 7da849a..22e68a3 100644
--- a/core/java/android/view/Surface.java
+++ b/core/java/android/view/Surface.java
@@ -33,6 +33,18 @@
 
 /**
  * Handle onto a raw buffer that is being managed by the screen compositor.
+ *
+ * <p>A Surface is generally created by or from a consumer of image buffers (such as a
+ * {@link android.graphics.SurfaceTexture}, {@link android.media.MediaRecorder}, or
+ * {@link android.renderscript.Allocation}), and is handed to some kind of producer (such as
+ * {@link android.opengl.EGL14#eglCreateWindowSurface(android.opengl.EGLDisplay,android.opengl.EGLConfig,java.lang.Object,int[],int) OpenGL},
+ * {@link android.media.MediaPlayer#setSurface MediaPlayer}, or
+ * {@link android.hardware.camera2.CameraDevice#createCaptureSession CameraDevice}) to draw
+ * into.</p>
+ *
+ * <p><strong>Note:</strong> A Surface acts like a
+ * {@link java.lang.ref.WeakReference weak reference} to the consumer it is associated with. By
+ * itself it will not keep its parent consumer from being reclaimed.</p>
  */
 public class Surface implements Parcelable {
     private static final String TAG = "Surface";
@@ -58,6 +70,8 @@
 
     private static native long nativeGetNextFrameNumber(long nativeObject);
     private static native int nativeSetScalingMode(long nativeObject, int scalingMode);
+    private static native void nativeSetBuffersTransform(long nativeObject, long transform);
+    private static native int nativeForceScopedDisconnect(long nativeObject);
 
     public static final Parcelable.Creator<Surface> CREATOR =
             new Parcelable.Creator<Surface>() {
@@ -95,6 +109,8 @@
 
     private HwuiContext mHwuiContext;
 
+    private boolean mIsSingleBuffered;
+
     /** @hide */
     @Retention(RetentionPolicy.SOURCE)
     @IntDef({SCALING_MODE_FREEZE, SCALING_MODE_SCALE_TO_WINDOW,
@@ -157,7 +173,7 @@
         if (surfaceTexture == null) {
             throw new IllegalArgumentException("surfaceTexture must not be null");
         }
-
+        mIsSingleBuffered = surfaceTexture.isSingleBuffered();
         synchronized (mLock) {
             mName = surfaceTexture.toString();
             setNativeObjectLocked(nativeCreateFromSurfaceTexture(surfaceTexture));
@@ -456,7 +472,10 @@
             // create a new native Surface and return it after reducing
             // the reference count on mNativeObject.  Either way, it is
             // not necessary to call nativeRelease() here.
+            // NOTE: This must be kept synchronized with the native parceling code
+            // in frameworks/native/libs/Surface.cpp
             mName = source.readString();
+            mIsSingleBuffered = source.readInt() != 0;
             setNativeObjectLocked(nativeReadFromParcel(mNativeObject, source));
         }
     }
@@ -467,7 +486,10 @@
             throw new IllegalArgumentException("dest must not be null");
         }
         synchronized (mLock) {
+            // NOTE: This must be kept synchronized with the native parceling code
+            // in frameworks/native/libs/Surface.cpp
             dest.writeString(mName);
+            dest.writeInt(mIsSingleBuffered ? 1 : 0);
             nativeWriteToParcel(mNativeObject, dest);
         }
         if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) {
@@ -529,6 +551,24 @@
         }
     }
 
+    void forceScopedDisconnect() {
+        synchronized (mLock) {
+            checkNotReleasedLocked();
+            int err = nativeForceScopedDisconnect(mNativeObject);
+            if (err != 0) {
+                throw new RuntimeException("Failed to disconnect Surface instance (bad object?)");
+            }
+        }
+    }
+
+    /**
+     * Returns whether or not this Surface is backed by a single-buffered SurfaceTexture
+     * @hide
+     */
+    public boolean isSingleBuffered() {
+        return mIsSingleBuffered;
+    }
+
     /**
      * Exception thrown when a Canvas couldn't be locked with {@link Surface#lockCanvas}, or
      * when a SurfaceTexture could not successfully be allocated.
diff --git a/core/java/android/view/SurfaceControl.java b/core/java/android/view/SurfaceControl.java
index 415e70c..e778a7f 100644
--- a/core/java/android/view/SurfaceControl.java
+++ b/core/java/android/view/SurfaceControl.java
@@ -51,7 +51,7 @@
 
     private static native void nativeSetLayer(long nativeObject, int zorder);
     private static native void nativeSetPosition(long nativeObject, float x, float y);
-    private static native void nativeSetPositionAppliesWithResize(long nativeObject);
+    private static native void nativeSetGeometryAppliesWithResize(long nativeObject);
     private static native void nativeSetSize(long nativeObject, int w, int h);
     private static native void nativeSetTransparentRegionHint(long nativeObject, Region region);
     private static native void nativeSetAlpha(long nativeObject, float alpha);
@@ -82,6 +82,10 @@
             IBinder displayToken);
     private static native int nativeGetActiveConfig(IBinder displayToken);
     private static native boolean nativeSetActiveConfig(IBinder displayToken, int id);
+    private static native int[] nativeGetDisplayColorModes(IBinder displayToken);
+    private static native int nativeGetActiveColorMode(IBinder displayToken);
+    private static native boolean nativeSetActiveColorMode(IBinder displayToken,
+            int colorMode);
     private static native void nativeSetDisplayPowerMode(
             IBinder displayToken, int mode);
     private static native void nativeDeferTransactionUntil(long nativeObject,
@@ -89,6 +93,8 @@
     private static native void nativeSetOverrideScalingMode(long nativeObject,
             int scalingMode);
     private static native IBinder nativeGetHandle(long nativeObject);
+    private static native boolean nativeGetTransformToDisplayInverse(long nativeObject);
+
     private static native Display.HdrCapabilities nativeGetHdrCapabilities(IBinder displayToken);
 
 
@@ -393,6 +399,10 @@
         return nativeGetHandle(mNativeObject);
     }
 
+    public boolean getTransformToDisplayInverse() {
+        return nativeGetTransformToDisplayInverse(mNativeObject);
+    }
+
     /** flag the transaction as an animation */
     public static void setAnimationTransaction() {
         nativeSetAnimationTransaction();
@@ -409,13 +419,15 @@
     }
 
     /**
-     * If the size changes in this transaction, position updates specified
+     * If the buffer size changes in this transaction, position and crop updates specified
      * in this transaction will not complete until a buffer of the new size
-     * arrives.
+     * arrives. As transform matrix and size are already frozen in this fashion,
+     * this enables totally freezing the surface until the resize has completed
+     * (at which point the geometry influencing aspects of this transaction will then occur)
      */
-    public void setPositionAppliesWithResize() {
+    public void setGeometryAppliesWithResize() {
         checkNotReleased();
-        nativeSetPositionAppliesWithResize(mNativeObject);
+        nativeSetGeometryAppliesWithResize(mNativeObject);
     }
 
     public void setSize(int w, int h) {
@@ -539,7 +551,6 @@
         public boolean secure;
         public long appVsyncOffsetNanos;
         public long presentationDeadlineNanos;
-        public int colorTransform;
 
         public PhysicalDisplayInfo() {
         }
@@ -563,8 +574,7 @@
                     && yDpi == other.yDpi
                     && secure == other.secure
                     && appVsyncOffsetNanos == other.appVsyncOffsetNanos
-                    && presentationDeadlineNanos == other.presentationDeadlineNanos
-                    && colorTransform == other.colorTransform;
+                    && presentationDeadlineNanos == other.presentationDeadlineNanos;
         }
 
         @Override
@@ -582,7 +592,6 @@
             secure = other.secure;
             appVsyncOffsetNanos = other.appVsyncOffsetNanos;
             presentationDeadlineNanos = other.presentationDeadlineNanos;
-            colorTransform = other.colorTransform;
         }
 
         // For debugging purposes
@@ -591,8 +600,7 @@
             return "PhysicalDisplayInfo{" + width + " x " + height + ", " + refreshRate + " fps, "
                     + "density " + density + ", " + xDpi + " x " + yDpi + " dpi, secure " + secure
                     + ", appVsyncOffset " + appVsyncOffsetNanos
-                    + ", bufferDeadline " + presentationDeadlineNanos
-                    + ", colorTransform " + colorTransform + "}";
+                    + ", bufferDeadline " + presentationDeadlineNanos + "}";
         }
     }
 
@@ -624,6 +632,27 @@
         return nativeSetActiveConfig(displayToken, id);
     }
 
+    public static int[] getDisplayColorModes(IBinder displayToken) {
+        if (displayToken == null) {
+            throw new IllegalArgumentException("displayToken must not be null");
+        }
+        return nativeGetDisplayColorModes(displayToken);
+    }
+
+    public static int getActiveColorMode(IBinder displayToken) {
+        if (displayToken == null) {
+            throw new IllegalArgumentException("displayToken must not be null");
+        }
+        return nativeGetActiveColorMode(displayToken);
+    }
+
+    public static boolean setActiveColorMode(IBinder displayToken, int colorMode) {
+        if (displayToken == null) {
+            throw new IllegalArgumentException("displayToken must not be null");
+        }
+        return nativeSetActiveColorMode(displayToken, colorMode);
+    }
+
     public static void setDisplayProjection(IBinder displayToken,
             int orientation, Rect layerStackRect, Rect displayRect) {
         if (displayToken == null) {
diff --git a/core/java/android/view/SurfaceView.java b/core/java/android/view/SurfaceView.java
index 203b825..4b9a570 100644
--- a/core/java/android/view/SurfaceView.java
+++ b/core/java/android/view/SurfaceView.java
@@ -115,6 +115,7 @@
     final Rect mStableInsets = new Rect();
     final Rect mOutsets = new Rect();
     final Rect mBackdropFrame = new Rect();
+    final Rect mTmpRect = new Rect();
     final Configuration mConfiguration = new Configuration();
 
     static final int KEEP_SCREEN_ON_MSG = 1;
@@ -193,26 +194,20 @@
     private boolean mGlobalListenersAdded;
 
     public SurfaceView(Context context) {
-        super(context);
-        init();
+        this(context, null);
     }
 
     public SurfaceView(Context context, AttributeSet attrs) {
-        super(context, attrs);
-        init();
+        this(context, attrs, 0);
     }
 
     public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
-        super(context, attrs, defStyleAttr);
-        init();
+        this(context, attrs, defStyleAttr, 0);
     }
 
     public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
         super(context, attrs, defStyleAttr, defStyleRes);
-        init();
-    }
 
-    private void init() {
         setWillNotDraw(true);
     }
 
@@ -233,6 +228,7 @@
         mSession = getWindowSession();
         mLayout.token = getWindowToken();
         mLayout.setTitle("SurfaceView - " + getViewRootImpl().getTitle());
+        mLayout.packageName = mContext.getOpPackageName();
         mViewVisibility = getVisibility() == VISIBLE;
 
         if (!mGlobalListenersAdded) {
@@ -499,7 +495,7 @@
                               | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                               | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                               ;
-                if (!creating && !force && !mUpdateWindowNeeded && !sizeChanged) {
+                if (!creating && !force && !sizeChanged) {
                     mLayout.privateFlags |=
                             WindowManager.LayoutParams.PRIVATE_FLAG_PRESERVE_GEOMETRY;
                 } else {
@@ -591,6 +587,20 @@
                             for (SurfaceHolder.Callback c : callbacks) {
                                 c.surfaceDestroyed(mSurfaceHolder);
                             }
+                            // Since Android N the same surface may be reused and given to us
+                            // again by the system server at a later point. However
+                            // as we didn't do this in previous releases, clients weren't
+                            // necessarily required to clean up properly in
+                            // surfaceDestroyed. This leads to problems for example when
+                            // clients don't destroy their EGL context, and try
+                            // and create a new one on the same surface following reuse.
+                            // Since there is no valid use of the surface in-between
+                            // surfaceDestroyed and surfaceCreated, we force a disconnect,
+                            // so the next connect will always work if we end up reusing
+                            // the surface.
+                            if (mSurface.isValid()) {
+                                mSurface.forceScopedDisconnect();
+                            }
                         }
                     }
 
@@ -666,21 +676,21 @@
 
                 transformFromViewToWindowSpace(mLocation);
 
-                mWinFrame.set(mWindowSpaceLeft, mWindowSpaceTop,
+                mTmpRect.set(mWindowSpaceLeft, mWindowSpaceTop,
                         mLocation[0], mLocation[1]);
 
                 if (mTranslator != null) {
-                    mTranslator.translateRectInAppWindowToScreen(mWinFrame);
+                    mTranslator.translateRectInAppWindowToScreen(mTmpRect);
                 }
 
                 if (!isHardwareAccelerated() || !mRtHandlingPositionUpdates) {
                     try {
                         if (DEBUG) Log.d(TAG, String.format("%d updateWindowPosition UI, " +
                                 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
-                                mWinFrame.left, mWinFrame.top,
-                                mWinFrame.right, mWinFrame.bottom));
-                        mSession.repositionChild(mWindow, mWinFrame.left, mWinFrame.top,
-                                mWinFrame.right, mWinFrame.bottom, -1, mWinFrame);
+                                mTmpRect.left, mTmpRect.top,
+                                mTmpRect.right, mTmpRect.bottom));
+                        mSession.repositionChild(mWindow, mTmpRect.left, mTmpRect.top,
+                                mTmpRect.right, mTmpRect.bottom, -1, mTmpRect);
                     } catch (RemoteException ex) {
                         Log.e(TAG, "Exception from relayout", ex);
                     }
@@ -692,10 +702,10 @@
     private Rect mRTLastReportedPosition = new Rect();
 
     /**
-     * Called by native on RenderThread to update the window position
+     * Called by native by a Rendering Worker thread to update the window position
      * @hide
      */
-    public final void updateWindowPositionRT(long frameNumber,
+    public final void updateWindowPosition_renderWorker(long frameNumber,
             int left, int top, int right, int bottom) {
         IWindowSession session = mSession;
         MyWindow window = mWindow;
@@ -720,7 +730,7 @@
         }
         try {
             if (DEBUG) {
-                Log.d(TAG, String.format("%d updateWindowPosition RT, frameNr = %d, " +
+                Log.d(TAG, String.format("%d updateWindowPosition RenderWorker, frameNr = %d, " +
                         "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
                         frameNumber, left, top, right, bottom));
             }
@@ -737,12 +747,12 @@
 
     /**
      * Called by native on RenderThread to notify that the window is no longer in the
-     * draw tree
+     * draw tree. UI thread is blocked at this point.
      * @hide
      */
-    public final void windowPositionLostRT(long frameNumber) {
+    public final void windowPositionLost_uiRtSync(long frameNumber) {
         if (DEBUG) {
-            Log.d(TAG, String.format("%d windowPositionLostRT RT, frameNr = %d",
+            Log.d(TAG, String.format("%d windowPositionLost, frameNr = %d",
                     System.identityHashCode(this), frameNumber));
         }
         IWindowSession session = mSession;
@@ -757,14 +767,18 @@
             // safely access other member variables at this time.
             // So do what the UI thread would have done if RT wasn't handling position
             // updates.
-            if (!mWinFrame.isEmpty() && !mWinFrame.equals(mRTLastReportedPosition)) {
+            mTmpRect.set(mLayout.x, mLayout.y,
+                    mLayout.x + mLayout.width,
+                    mLayout.y + mLayout.height);
+
+            if (!mTmpRect.isEmpty() && !mTmpRect.equals(mRTLastReportedPosition)) {
                 try {
                     if (DEBUG) Log.d(TAG, String.format("%d updateWindowPosition, " +
                             "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
-                            mWinFrame.left, mWinFrame.top,
-                            mWinFrame.right, mWinFrame.bottom));
-                    session.repositionChild(window, mWinFrame.left, mWinFrame.top,
-                            mWinFrame.right, mWinFrame.bottom, frameNumber, mWinFrame);
+                            mTmpRect.left, mTmpRect.top,
+                            mTmpRect.right, mTmpRect.bottom));
+                    session.repositionChild(window, mTmpRect.left, mTmpRect.top,
+                            mTmpRect.right, mTmpRect.bottom, frameNumber, mWinFrame);
                 } catch (RemoteException ex) {
                     Log.e(TAG, "Exception from relayout", ex);
                 }
diff --git a/core/java/android/view/TextureView.java b/core/java/android/view/TextureView.java
index 9461bea..645ab5c 100644
--- a/core/java/android/view/TextureView.java
+++ b/core/java/android/view/TextureView.java
@@ -228,6 +228,7 @@
      */
     @Override
     protected void destroyHardwareResources() {
+        super.destroyHardwareResources();
         destroyHardwareLayer();
     }
 
diff --git a/core/java/android/view/ThreadedRenderer.java b/core/java/android/view/ThreadedRenderer.java
index e650d95..2e0729b 100644
--- a/core/java/android/view/ThreadedRenderer.java
+++ b/core/java/android/view/ThreadedRenderer.java
@@ -16,6 +16,7 @@
 
 package android.view;
 
+import android.app.ActivityManagerNative;
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.content.Context;
@@ -23,6 +24,7 @@
 import android.graphics.Bitmap;
 import android.graphics.Point;
 import android.graphics.Rect;
+import android.graphics.drawable.AnimatedVectorDrawable;
 import android.os.Binder;
 import android.os.Handler;
 import android.os.IBinder;
@@ -518,16 +520,6 @@
     }
 
     /**
-     * This method should be invoked whenever the current hardware renderer
-     * context should be reset.
-     *
-     * @param surface The surface to hardware accelerate
-     */
-    void invalidate(Surface surface) {
-        updateSurface(surface);
-    }
-
-    /**
      * Detaches the layer's surface texture from the GL context and releases
      * the texture id
      */
@@ -881,6 +873,12 @@
         nRegisterAnimatingRenderNode(mRootNode.mNativeRenderNode, animator.mNativeRenderNode);
     }
 
+    void registerVectorDrawableAnimator(
+        AnimatedVectorDrawable.VectorDrawableAnimatorRT animator) {
+        nRegisterVectorDrawableAnimator(mRootNode.mNativeRenderNode,
+                animator.getAnimatorNativePtr());
+    }
+
     public void serializeDisplayListTree() {
         nSerializeDisplayListTree(mNativeProxy);
     }
@@ -910,10 +908,20 @@
         synchronized void init(Context context, long renderProxy) {
             if (mInitialized) return;
             mInitialized = true;
+            initSched(context, renderProxy);
             initGraphicsStats(context, renderProxy);
             initAssetAtlas(context, renderProxy);
         }
 
+        private static void initSched(Context context, long renderProxy) {
+            try {
+                int tid = nGetRenderThreadTid(renderProxy);
+                ActivityManagerNative.getDefault().setRenderThread(tid);
+            } catch (Throwable t) {
+                Log.w(LOG_TAG, "Failed to set scheduler for RenderThread", t);
+            }
+        }
+
         private static void initGraphicsStats(Context context, long renderProxy) {
             try {
                 IBinder binder = ServiceManager.getService("graphicsstats");
@@ -972,6 +980,7 @@
 
     private static native void nSetAtlas(long nativeProxy, GraphicBuffer buffer, long[] map);
     private static native void nSetProcessStatsBuffer(long nativeProxy, int fd);
+    private static native int nGetRenderThreadTid(long nativeProxy);
 
     private static native long nCreateRootRenderNode();
     private static native long nCreateProxy(boolean translucent, long rootRenderNode);
@@ -992,6 +1001,7 @@
     private static native int nSyncAndDrawFrame(long nativeProxy, long[] frameInfo, int size);
     private static native void nDestroy(long nativeProxy, long rootRenderNode);
     private static native void nRegisterAnimatingRenderNode(long rootRenderNode, long animatingNode);
+    private static native void nRegisterVectorDrawableAnimator(long rootRenderNode, long animator);
 
     private static native void nInvokeFunctor(long functor, boolean waitForCompletion);
 
diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java
index e462028..d13f6d6 100644
--- a/core/java/android/view/View.java
+++ b/core/java/android/view/View.java
@@ -820,6 +820,27 @@
     static boolean sTextureViewIgnoresDrawableSetters = false;
 
     /**
+     * Prior to N, some ViewGroups would not convert LayoutParams properly even though both extend
+     * MarginLayoutParams. For instance, converting LinearLayout.LayoutParams to
+     * RelativeLayout.LayoutParams would lose margin information. This is fixed on N but target API
+     * check is implemented for backwards compatibility.
+     *
+     * {@hide}
+     */
+    protected static boolean sPreserveMarginParamsInLayoutParamConversion;
+
+    /**
+     * Prior to N, when drag enters into child of a view that has already received an
+     * ACTION_DRAG_ENTERED event, the parent doesn't get a ACTION_DRAG_EXITED event.
+     * ACTION_DRAG_LOCATION and ACTION_DROP were delivered to the parent of a view that returned
+     * false from its event handler for these events.
+     * Starting from N, the parent will get ACTION_DRAG_EXITED event before the child gets its
+     * ACTION_DRAG_ENTERED. ACTION_DRAG_LOCATION and ACTION_DROP are never propagated to the parent.
+     * sCascadedDragDrop is true for pre-N apps for backwards compatibility implementation.
+     */
+    static boolean sCascadedDragDrop;
+
+    /**
      * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
      * calling setFlags.
      */
@@ -2435,6 +2456,7 @@
      *          1                        PFLAG3_OVERLAPPING_RENDERING_FORCED_VALUE
      *         1                         PFLAG3_HAS_OVERLAPPING_RENDERING_FORCED
      *        1                          PFLAG3_TEMPORARY_DETACH
+     *       1                           PFLAG3_NO_REVEAL_ON_FOCUS
      * |-------|-------|-------|-------|
      */
 
@@ -2676,6 +2698,16 @@
      */
     static final int PFLAG3_TEMPORARY_DETACH = 0x2000000;
 
+    /**
+     * Flag indicating that the view does not wish to be revealed within its parent
+     * hierarchy when it gains focus. Expressed in the negative since the historical
+     * default behavior is to reveal on focus; this flag suppresses that behavior.
+     *
+     * @see #setRevealOnFocusHint(boolean)
+     * @see #getRevealOnFocusHint()
+     */
+    private static final int PFLAG3_NO_REVEAL_ON_FOCUS = 0x4000000;
+
     /* End of masks for mPrivateFlags3 */
 
     /**
@@ -3064,20 +3096,6 @@
     /**
      * @hide
      *
-     * Whether Recents is visible or not.
-     */
-    public static final int RECENT_APPS_VISIBLE = 0x00004000;
-
-    /**
-     * @hide
-     *
-     * Whether the TV's picture-in-picture is visible or not.
-     */
-    public static final int TV_PICTURE_IN_PICTURE_VISIBLE = 0x00010000;
-
-    /**
-     * @hide
-     *
      * Makes navigation bar transparent (but not the status bar).
      */
     public static final int NAVIGATION_BAR_TRANSPARENT = 0x00008000;
@@ -3980,6 +3998,9 @@
      */
     String mStartActivityRequestWho;
 
+    @Nullable
+    private RoundScrollbarRenderer mRoundScrollbarRenderer;
+
     /**
      * Simple constructor to use when creating a view from code.
      *
@@ -4037,6 +4058,12 @@
             // On N+, we throw, but that breaks compatibility with apps that use these methods.
             sTextureViewIgnoresDrawableSetters = targetSdkVersion <= M;
 
+            // Prior to N, we would drop margins in LayoutParam conversions. The fix triggers bugs
+            // in apps so we target check it to avoid breaking existing apps.
+            sPreserveMarginParamsInLayoutParamConversion = targetSdkVersion >= N;
+
+            sCascadedDragDrop = targetSdkVersion < N;
+
             sCompatibilityDone = true;
         }
     }
@@ -4215,25 +4242,25 @@
                     setAlpha(a.getFloat(attr, 1f));
                     break;
                 case com.android.internal.R.styleable.View_transformPivotX:
-                    setPivotX(a.getDimensionPixelOffset(attr, 0));
+                    setPivotX(a.getDimension(attr, 0));
                     break;
                 case com.android.internal.R.styleable.View_transformPivotY:
-                    setPivotY(a.getDimensionPixelOffset(attr, 0));
+                    setPivotY(a.getDimension(attr, 0));
                     break;
                 case com.android.internal.R.styleable.View_translationX:
-                    tx = a.getDimensionPixelOffset(attr, 0);
+                    tx = a.getDimension(attr, 0);
                     transformSet = true;
                     break;
                 case com.android.internal.R.styleable.View_translationY:
-                    ty = a.getDimensionPixelOffset(attr, 0);
+                    ty = a.getDimension(attr, 0);
                     transformSet = true;
                     break;
                 case com.android.internal.R.styleable.View_translationZ:
-                    tz = a.getDimensionPixelOffset(attr, 0);
+                    tz = a.getDimension(attr, 0);
                     transformSet = true;
                     break;
                 case com.android.internal.R.styleable.View_elevation:
-                    elevation = a.getDimensionPixelOffset(attr, 0);
+                    elevation = a.getDimension(attr, 0);
                     transformSet = true;
                     break;
                 case com.android.internal.R.styleable.View_rotation:
@@ -5941,6 +5968,47 @@
     }
 
     /**
+     * Sets this view's preference for reveal behavior when it gains focus.
+     *
+     * <p>When set to true, this is a signal to ancestor views in the hierarchy that
+     * this view would prefer to be brought fully into view when it gains focus.
+     * For example, a text field that a user is meant to type into. Other views such
+     * as scrolling containers may prefer to opt-out of this behavior.</p>
+     *
+     * <p>The default value for views is true, though subclasses may change this
+     * based on their preferred behavior.</p>
+     *
+     * @param revealOnFocus true to request reveal on focus in ancestors, false otherwise
+     *
+     * @see #getRevealOnFocusHint()
+     */
+    public final void setRevealOnFocusHint(boolean revealOnFocus) {
+        if (revealOnFocus) {
+            mPrivateFlags3 &= ~PFLAG3_NO_REVEAL_ON_FOCUS;
+        } else {
+            mPrivateFlags3 |= PFLAG3_NO_REVEAL_ON_FOCUS;
+        }
+    }
+
+    /**
+     * Returns this view's preference for reveal behavior when it gains focus.
+     *
+     * <p>When this method returns true for a child view requesting focus, ancestor
+     * views responding to a focus change in {@link ViewParent#requestChildFocus(View, View)}
+     * should make a best effort to make the newly focused child fully visible to the user.
+     * When it returns false, ancestor views should preferably not disrupt scroll positioning or
+     * other properties affecting visibility to the user as part of the focus change.</p>
+     *
+     * @return true if this view would prefer to become fully visible when it gains focus,
+     *         false if it would prefer not to disrupt scroll positioning
+     *
+     * @see #setRevealOnFocusHint(boolean)
+     */
+    public final boolean getRevealOnFocusHint() {
+        return (mPrivateFlags3 & PFLAG3_NO_REVEAL_ON_FOCUS) == 0;
+    }
+
+    /**
      * Populates <code>outRect</code> with the hotspot bounds. By default,
      * the hotspot bounds are identical to the screen bounds.
      *
@@ -9781,6 +9849,18 @@
     }
 
     /**
+     * Tells whether the {@link View} is in the state between {@link #onStartTemporaryDetach()}
+     * and {@link #onFinishTemporaryDetach()}.
+     *
+     * <p>This method always returns {@code true} when called directly or indirectly from
+     * {@link #onStartTemporaryDetach()}. The return value when called directly or indirectly from
+     * {@link #onFinishTemporaryDetach()}, however, depends on the OS version.
+     * <ul>
+     *     <li>{@code true} on {@link android.os.Build.VERSION_CODES#N API 24}</li>
+     *     <li>{@code false} on {@link android.os.Build.VERSION_CODES#N_MR1 API 25}} and later</li>
+     * </ul>
+     * </p>
+     *
      * @return {@code true} when the View is in the state between {@link #onStartTemporaryDetach()}
      * and {@link #onFinishTemporaryDetach()}.
      */
@@ -9815,8 +9895,8 @@
      */
     @CallSuper
     public void dispatchFinishTemporaryDetach() {
-        onFinishTemporaryDetach();
         mPrivateFlags3 &= ~PFLAG3_TEMPORARY_DETACH;
+        onFinishTemporaryDetach();
         if (hasWindowFocus() && hasFocus()) {
             InputMethodManager.getInstance().focusIn(this);
         }
@@ -10302,7 +10382,7 @@
      *                  ancestors or by window visibility
      * @return true if this view is visible to the user, not counting clipping or overlapping
      */
-    @Visibility boolean dispatchVisibilityAggregated(boolean isVisible) {
+    boolean dispatchVisibilityAggregated(boolean isVisible) {
         final boolean thisVisible = getVisibility() == VISIBLE;
         // If we're not visible but something is telling us we are, ignore it.
         if (thisVisible || !isVisible) {
@@ -12306,6 +12386,10 @@
     public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) {
         ensureTransformationInfo();
         if (mTransformationInfo.mAlpha != alpha) {
+            // Report visibility changes, which can affect children, to accessibility
+            if ((alpha == 0) ^ (mTransformationInfo.mAlpha == 0)) {
+                notifySubtreeAccessibilityStateChangedIfNeeded();
+            }
             mTransformationInfo.mAlpha = alpha;
             if (onSetAlpha((int) (alpha * 255))) {
                 mPrivateFlags |= PFLAG_ALPHA_SET;
@@ -12316,8 +12400,6 @@
                 mPrivateFlags &= ~PFLAG_ALPHA_SET;
                 invalidateViewProperty(true, false);
                 mRenderNode.setAlpha(getFinalAlpha());
-                notifyViewAccessibilityStateChangedIfNeeded(
-                        AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED);
             }
         }
     }
@@ -14717,6 +14799,25 @@
     }
 
     private void getVerticalScrollBarBounds(Rect bounds) {
+        if (mRoundScrollbarRenderer == null) {
+            getStraightVerticalScrollBarBounds(bounds);
+        } else {
+            getRoundVerticalScrollBarBounds(bounds);
+        }
+    }
+
+    private void getRoundVerticalScrollBarBounds(Rect bounds) {
+        final int width = mRight - mLeft;
+        final int height = mBottom - mTop;
+        // Do not take padding into account as we always want the scrollbars
+        // to hug the screen for round wearable devices.
+        bounds.left = mScrollX;
+        bounds.top = mScrollY;
+        bounds.right = bounds.left + width;
+        bounds.bottom = mScrollY + height;
+    }
+
+    private void getStraightVerticalScrollBarBounds(Rect bounds) {
         final int inside = (mViewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
         final int size = getVerticalScrollbarWidth();
         int verticalScrollbarPosition = mVerticalScrollbarPosition;
@@ -14751,6 +14852,7 @@
     protected final void onDrawScrollBars(Canvas canvas) {
         // scrollbars are drawn only when the animation is running
         final ScrollabilityCache cache = mScrollCache;
+
         if (cache != null) {
 
             int state = cache.state;
@@ -14791,13 +14893,25 @@
             final boolean drawVerticalScrollBar = isVerticalScrollBarEnabled()
                     && !isVerticalScrollBarHidden();
 
-            if (drawVerticalScrollBar || drawHorizontalScrollBar) {
+            // Fork out the scroll bar drawing for round wearable devices.
+            if (mRoundScrollbarRenderer != null) {
+                if (drawVerticalScrollBar) {
+                    final Rect bounds = cache.mScrollBarBounds;
+                    getVerticalScrollBarBounds(bounds);
+                    mRoundScrollbarRenderer.drawRoundScrollbars(
+                            canvas, (float) cache.scrollBar.getAlpha() / 255f, bounds);
+                    if (invalidate) {
+                        invalidate();
+                    }
+                }
+                // Do not draw horizontal scroll bars for round wearable devices.
+            } else if (drawVerticalScrollBar || drawHorizontalScrollBar) {
                 final ScrollBarDrawable scrollBar = cache.scrollBar;
 
                 if (drawHorizontalScrollBar) {
                     scrollBar.setParameters(computeHorizontalScrollRange(),
-                                            computeHorizontalScrollOffset(),
-                                            computeHorizontalScrollExtent(), false);
+                            computeHorizontalScrollOffset(),
+                            computeHorizontalScrollExtent(), false);
                     final Rect bounds = cache.mScrollBarBounds;
                     getHorizontalScrollBarBounds(bounds);
                     onDrawHorizontalScrollBar(canvas, scrollBar, bounds.left, bounds.top,
@@ -14809,8 +14923,8 @@
 
                 if (drawVerticalScrollBar) {
                     scrollBar.setParameters(computeVerticalScrollRange(),
-                                            computeVerticalScrollOffset(),
-                                            computeVerticalScrollExtent(), true);
+                            computeVerticalScrollOffset(),
+                            computeVerticalScrollExtent(), true);
                     final Rect bounds = cache.mScrollBarBounds;
                     getVerticalScrollBarBounds(bounds);
                     onDrawVerticalScrollBar(canvas, scrollBar, bounds.left, bounds.top,
@@ -15411,7 +15525,7 @@
         if (vis != GONE) {
             onWindowVisibilityChanged(vis);
             if (isShown()) {
-                // Calling onVisibilityChanged directly here since the subtree will also
+                // Calling onVisibilityAggregated directly here since the subtree will also
                 // receive dispatchAttachedToWindow and this same call
                 onVisibilityAggregated(vis == VISIBLE);
             }
@@ -17521,6 +17635,15 @@
 
         if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
             onLayout(changed, l, t, r, b);
+
+            if (shouldDrawRoundScrollbar()) {
+                if(mRoundScrollbarRenderer == null) {
+                    mRoundScrollbarRenderer = new RoundScrollbarRenderer(this);
+                }
+            } else {
+                mRoundScrollbarRenderer = null;
+            }
+
             mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
 
             ListenerInfo li = mListenerInfo;
@@ -20155,8 +20278,14 @@
                 // remove it from the transparent region.
                 final int[] location = attachInfo.mTransparentLocation;
                 getLocationInWindow(location);
-                region.op(location[0], location[1], location[0] + mRight - mLeft,
-                        location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
+                // When a view has Z value, then it will be better to leave some area below the view
+                // for drawing shadow. The shadow outset is proportional to the Z value. Note that
+                // the bottom part needs more offset than the left, top and right parts due to the
+                // spot light effects.
+                int shadowOffset = getZ() > 0 ? (int) getZ() : 0;
+                region.op(location[0] - shadowOffset, location[1] - shadowOffset,
+                        location[0] + mRight - mLeft + shadowOffset,
+                        location[1] + mBottom - mTop + (shadowOffset * 3), Region.Op.DIFFERENCE);
             } else {
                 if (mBackground != null && mBackground.getOpacity() != PixelFormat.TRANSPARENT) {
                     // The SKIP_DRAW flag IS set and the background drawable exists, we remove
@@ -20539,7 +20668,6 @@
      *         <li>{@link #DRAG_FLAG_GLOBAL}</li>
      *         <li>{@link #DRAG_FLAG_GLOBAL_PERSISTABLE_URI_PERMISSION}</li>
      *         <li>{@link #DRAG_FLAG_GLOBAL_PREFIX_URI_PERMISSION}</li>
-     *         <li>{@link #DRAG_FLAG_GLOBAL_PREFIX_URI_PERMISSION}</li>
      *         <li>{@link #DRAG_FLAG_GLOBAL_URI_READ}</li>
      *         <li>{@link #DRAG_FLAG_GLOBAL_URI_WRITE}</li>
      *         <li>{@link #DRAG_FLAG_OPAQUE}</li>
@@ -20729,6 +20857,11 @@
         return false;
     }
 
+    // Dispatches ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED events for pre-Nougat apps.
+    boolean dispatchDragEnterExitInPreN(DragEvent event) {
+        return callDragEventHandler(event);
+    }
+
     /**
      * Detects if this View is enabled and has a drag event listener.
      * If both are true, then it calls the drag event listener with the
@@ -20746,13 +20879,44 @@
      * </p>
      */
     public boolean dispatchDragEvent(DragEvent event) {
+        event.mEventHandlerWasCalled = true;
+        if (event.mAction == DragEvent.ACTION_DRAG_LOCATION ||
+            event.mAction == DragEvent.ACTION_DROP) {
+            // About to deliver an event with coordinates to this view. Notify that now this view
+            // has drag focus. This will send exit/enter events as needed.
+            getViewRootImpl().setDragFocus(this, event);
+        }
+        return callDragEventHandler(event);
+    }
+
+    final boolean callDragEventHandler(DragEvent event) {
+        final boolean result;
+
         ListenerInfo li = mListenerInfo;
         //noinspection SimplifiableIfStatement
         if (li != null && li.mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
                 && li.mOnDragListener.onDrag(this, event)) {
-            return true;
+            result = true;
+        } else {
+            result = onDragEvent(event);
         }
-        return onDragEvent(event);
+
+        switch (event.mAction) {
+            case DragEvent.ACTION_DRAG_ENTERED: {
+                mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED;
+                refreshDrawableState();
+            } break;
+            case DragEvent.ACTION_DRAG_EXITED: {
+                mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
+                refreshDrawableState();
+            } break;
+            case DragEvent.ACTION_DRAG_ENDED: {
+                mPrivateFlags2 &= ~View.DRAG_MASK;
+                refreshDrawableState();
+            } break;
+        }
+
+        return result;
     }
 
     boolean canAcceptDrag() {
@@ -22856,7 +23020,7 @@
         /**
          * Last global system UI visibility reported by the window manager.
          */
-        int mGlobalSystemUiVisibility;
+        int mGlobalSystemUiVisibility = -1;
 
         /**
          * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
@@ -22903,7 +23067,7 @@
         final int[] mInvalidateChildLocation = new int[2];
 
         /**
-         * Global to the view hierarchy used as a temporary for dealng with
+         * Global to the view hierarchy used as a temporary for dealing with
          * computing absolute on-screen location.
          */
         final int[] mTmpLocation = new int[2];
@@ -23741,4 +23905,30 @@
         stream.addProperty("accessibility:labelFor", getLabelFor());
         stream.addProperty("accessibility:importantForAccessibility", getImportantForAccessibility());
     }
+
+    /**
+     * Determine if this view is rendered on a round wearable device and is the main view
+     * on the screen.
+     */
+    private boolean shouldDrawRoundScrollbar() {
+        if (!mResources.getConfiguration().isScreenRound() || mAttachInfo == null) {
+            return false;
+        }
+
+        final View rootView = getRootView();
+        final WindowInsets insets = getRootWindowInsets();
+
+        int height = getHeight();
+        int width = getWidth();
+        int displayHeight = rootView.getHeight();
+        int displayWidth = rootView.getWidth();
+
+        if (height != displayHeight || width != displayWidth) {
+            return false;
+        }
+
+        getLocationOnScreen(mAttachInfo.mTmpLocation);
+        return mAttachInfo.mTmpLocation[0] == insets.getStableInsetLeft()
+                && mAttachInfo.mTmpLocation[1] == insets.getStableInsetTop();
+    }
 }
diff --git a/core/java/android/view/ViewConfiguration.java b/core/java/android/view/ViewConfiguration.java
index 4d584a3..8b8525f 100644
--- a/core/java/android/view/ViewConfiguration.java
+++ b/core/java/android/view/ViewConfiguration.java
@@ -16,6 +16,7 @@
 
 package android.view;
 
+import android.annotation.SystemApi;
 import android.app.AppGlobals;
 import android.content.Context;
 import android.content.res.Configuration;
@@ -64,6 +65,12 @@
     private static final int DEFAULT_LONG_PRESS_TIMEOUT = 500;
 
     /**
+     * Defines the default duration in milliseconds between the first tap's up event and the second
+     * tap's down event for an interaction to be considered part of the same multi-press.
+     */
+    private static final int DEFAULT_MULTI_PRESS_TIMEOUT = 300;
+
+    /**
      * Defines the time between successive key repeats in milliseconds.
      */
     private static final int KEY_REPEAT_DELAY = 50;
@@ -213,6 +220,12 @@
     private static final int OVERFLING_DISTANCE = 6;
 
     /**
+     * Amount to scroll in response to a {@link MotionEvent#ACTION_SCROLL} event, in dips per
+     * axis value.
+     */
+    private static final int SCROLL_FACTOR = 64;
+
+    /**
      * Default duration to hide an action mode for.
      */
     private static final long ACTION_MODE_HIDE_DURATION_DEFAULT = 2000;
@@ -240,6 +253,7 @@
     private final int mOverflingDistance;
     private final boolean mFadingMarqueeEnabled;
     private final long mGlobalActionsKeyTimeout;
+    private final int mScrollFactor;
 
     private boolean sHasPermanentMenuKey;
     private boolean sHasPermanentMenuKeySet;
@@ -268,6 +282,7 @@
         mOverflingDistance = OVERFLING_DISTANCE;
         mFadingMarqueeEnabled = true;
         mGlobalActionsKeyTimeout = GLOBAL_ACTIONS_KEY_TIMEOUT;
+        mScrollFactor = SCROLL_FACTOR;
     }
 
     /**
@@ -351,6 +366,8 @@
                 com.android.internal.R.dimen.config_viewMaxFlingVelocity);
         mGlobalActionsKeyTimeout = res.getInteger(
                 com.android.internal.R.integer.config_globalActionsKeyTimeout);
+        mScrollFactor = res.getDimensionPixelSize(
+                com.android.internal.R.dimen.config_scrollFactor);
     }
 
     /**
@@ -441,6 +458,16 @@
     }
 
     /**
+     * @return the duration in milliseconds between the first tap's up event and the second tap's
+     * down event for an interaction to be considered part of the same multi-press.
+     * @hide
+     */
+    public static int getMultiPressTimeout() {
+        return AppGlobals.getIntCoreSetting(Settings.Secure.MULTI_PRESS_TIMEOUT,
+                DEFAULT_MULTI_PRESS_TIMEOUT);
+    }
+
+    /**
      * @return the time before the first key repeat in milliseconds.
      */
     public static int getKeyRepeatTimeout() {
@@ -653,6 +680,16 @@
     }
 
     /**
+     * @return Amount to scroll in response to a {@link MotionEvent#ACTION_SCROLL} event. Multiply
+     * this by the event's axis value to obtain the number of pixels to be scrolled.
+     * @hide
+     * @SystemApi
+     */
+    public int getScaledScrollFactor() {
+        return mScrollFactor;
+    }
+
+    /**
      * The maximum drawing cache size expressed in bytes.
      *
      * @return the maximum size of View's drawing cache expressed in bytes
diff --git a/core/java/android/view/ViewGroup.java b/core/java/android/view/ViewGroup.java
index 3ff8d4f..d4b7d3b 100644
--- a/core/java/android/view/ViewGroup.java
+++ b/core/java/android/view/ViewGroup.java
@@ -153,8 +153,9 @@
      */
     Transformation mInvalidationTransformation;
 
-    // View currently under an ongoing drag. Can be null, a child or this window.
-    private View mCurrentDragView;
+    // Current frontmost child that can accept drag and lies under the drag location.
+    // Used only to generate ENTER/EXIT events for pre-Nougat aps.
+    private View mCurrentDragChild;
 
     // Metadata about the ongoing drag
     private DragEvent mCurrentDragStartEvent;
@@ -1355,6 +1356,20 @@
         return mLocalPoint;
     }
 
+    @Override
+    boolean dispatchDragEnterExitInPreN(DragEvent event) {
+        if (event.mAction == DragEvent.ACTION_DRAG_EXITED && mCurrentDragChild != null) {
+            // The drag exited a sub-tree of views; notify of the exit all descendants that are in
+            // entered state.
+            // We don't need this recursive delivery for ENTERED events because they get generated
+            // from the recursive delivery of LOCATION/DROP events, and hence, don't need their own
+            // recursion.
+            mCurrentDragChild.dispatchDragEnterExitInPreN(event);
+            mCurrentDragChild = null;
+        }
+        return mIsInterestedInDrag && super.dispatchDragEnterExitInPreN(event);
+    }
+
     // TODO: Write real docs
     @Override
     public boolean dispatchDragEvent(DragEvent event) {
@@ -1362,15 +1377,13 @@
         final float tx = event.mX;
         final float ty = event.mY;
 
-        ViewRootImpl root = getViewRootImpl();
-
         // Dispatch down the view hierarchy
         final PointF localPoint = getLocalPoint();
 
         switch (event.mAction) {
         case DragEvent.ACTION_DRAG_STARTED: {
-            // clear state to recalculate which views we drag over
-            mCurrentDragView = null;
+            // Clear the state to recalculate which views we drag over.
+            mCurrentDragChild = null;
 
             // Set up our tracking of drag-started notifications
             mCurrentDragStartEvent = DragEvent.obtain(event);
@@ -1416,8 +1429,6 @@
                     if (child.dispatchDragEvent(event)) {
                         retval = true;
                     }
-                    child.mPrivateFlags2 &= ~View.DRAG_MASK;
-                    child.refreshDrawableState();
                 }
                 childrenInterestedInDrag.clear();
             }
@@ -1434,60 +1445,45 @@
             }
         } break;
 
-        case DragEvent.ACTION_DRAG_LOCATION: {
+        case DragEvent.ACTION_DRAG_LOCATION:
+        case DragEvent.ACTION_DROP: {
             // Find the [possibly new] drag target
             View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
+
+            if (target != mCurrentDragChild) {
+                if (sCascadedDragDrop) {
+                    // For pre-Nougat apps, make sure that the whole hierarchy of views that contain
+                    // the drag location is kept in the state between ENTERED and EXITED events.
+                    // (Starting with N, only the innermost view will be in that state).
+
+                    final int action = event.mAction;
+                    // Position should not be available for ACTION_DRAG_ENTERED and
+                    // ACTION_DRAG_EXITED.
+                    event.mX = 0;
+                    event.mY = 0;
+
+                    if (mCurrentDragChild != null) {
+                        event.mAction = DragEvent.ACTION_DRAG_EXITED;
+                        mCurrentDragChild.dispatchDragEnterExitInPreN(event);
+                    }
+
+                    if (target != null) {
+                        event.mAction = DragEvent.ACTION_DRAG_ENTERED;
+                        target.dispatchDragEnterExitInPreN(event);
+                    }
+
+                    event.mAction = action;
+                    event.mX = tx;
+                    event.mY = ty;
+                }
+                mCurrentDragChild = target;
+            }
+
             if (target == null && mIsInterestedInDrag) {
                 target = this;
             }
 
-            // If we've changed apparent drag target, tell the view root which view
-            // we're over now [for purposes of the eventual drag-recipient-changed
-            // notifications to the framework] and tell the new target that the drag
-            // has entered its bounds.  The root will see setDragFocus() calls all
-            // the way down to the final leaf view that is handling the LOCATION event
-            // before reporting the new potential recipient to the framework.
-            if (mCurrentDragView != target) {
-                root.setDragFocus(target);
-
-                final int action = event.mAction;
-                // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED.
-                event.mX = 0;
-                event.mY = 0;
-
-                // If we've dragged off of a child view or this window, send it the EXITED message
-                if (mCurrentDragView != null) {
-                    final View view = mCurrentDragView;
-                    event.mAction = DragEvent.ACTION_DRAG_EXITED;
-                    if (view != this) {
-                        view.dispatchDragEvent(event);
-                        view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
-                        view.refreshDrawableState();
-                    } else {
-                        super.dispatchDragEvent(event);
-                    }
-                }
-
-                mCurrentDragView = target;
-
-                // If we've dragged over a new child view, send it the ENTERED message, otherwise
-                // send it to this window.
-                if (target != null) {
-                    event.mAction = DragEvent.ACTION_DRAG_ENTERED;
-                    if (target != this) {
-                        target.dispatchDragEvent(event);
-                        target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED;
-                        target.refreshDrawableState();
-                    } else {
-                        super.dispatchDragEvent(event);
-                    }
-                }
-                event.mAction = action;  // restore the event's original state
-                event.mX = tx;
-                event.mY = ty;
-            }
-
-            // Dispatch the actual drag location notice, localized into its coordinates
+            // Dispatch the actual drag notice, localized into the target coordinates.
             if (target != null) {
                 if (target != this) {
                     event.mX = localPoint.x;
@@ -1497,58 +1493,24 @@
 
                     event.mX = tx;
                     event.mY = ty;
+
+                    if (mIsInterestedInDrag) {
+                        final boolean eventWasConsumed;
+                        if (sCascadedDragDrop) {
+                            eventWasConsumed = retval;
+                        } else {
+                            eventWasConsumed = event.mEventHandlerWasCalled;
+                        }
+
+                        if (!eventWasConsumed) {
+                            retval = super.dispatchDragEvent(event);
+                        }
+                    }
                 } else {
                     retval = super.dispatchDragEvent(event);
                 }
             }
         } break;
-
-        /* Entered / exited dispatch
-         *
-         * DRAG_ENTERED is not dispatched downwards from ViewGroup.  The reason for this is
-         * that we're about to get the corresponding LOCATION event, which we will use to
-         * determine which of our children is the new target; at that point we will
-         * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup].
-         * If no suitable child is detected, dispatch to this window.
-         *
-         * DRAG_EXITED *is* dispatched all the way down immediately: once we know the
-         * drag has left this ViewGroup, we know by definition that every contained subview
-         * is also no longer under the drag point.
-         */
-
-        case DragEvent.ACTION_DRAG_EXITED: {
-            if (mCurrentDragView != null) {
-                final View view = mCurrentDragView;
-                if (view != this) {
-                    view.dispatchDragEvent(event);
-                    view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED;
-                    view.refreshDrawableState();
-                } else {
-                    super.dispatchDragEvent(event);
-                }
-
-                mCurrentDragView = null;
-            }
-        } break;
-
-        case DragEvent.ACTION_DROP: {
-            if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event);
-            View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint);
-            if (target != null) {
-                if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "   dispatch drop to " + target);
-                event.mX = localPoint.x;
-                event.mY = localPoint.y;
-                retval = target.dispatchDragEvent(event);
-                event.mX = tx;
-                event.mY = ty;
-            } else if (mIsInterestedInDrag) {
-                retval = super.dispatchDragEvent(event);
-            } else {
-                if (ViewDebug.DEBUG_DRAG) {
-                    Log.d(View.VIEW_LOG_TAG, "   not dropped on an accepting view");
-                }
-            }
-        } break;
         }
 
         return retval;
@@ -1592,6 +1554,7 @@
         final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent);
         mCurrentDragStartEvent.mX = tx;
         mCurrentDragStartEvent.mY = ty;
+        mCurrentDragStartEvent.mEventHandlerWasCalled = false;
         if (canAccept) {
             mChildrenInterestedInDrag.add(child);
             if (!child.canAcceptDrag()) {
@@ -6406,16 +6369,28 @@
             return true;
         }
         super.gatherTransparentRegion(region);
-        final View[] children = mChildren;
-        final int count = mChildrenCount;
+        // Instead of naively traversing the view tree, we have to traverse according to the Z
+        // order here. We need to go with the same order as dispatchDraw().
+        // One example is that after surfaceView punch a hole, we will still allow other views drawn
+        // on top of that hole. In this case, those other views should be able to cut the
+        // transparent region into smaller area.
+        final int childrenCount = mChildrenCount;
         boolean noneOfTheChildrenAreTransparent = true;
-        for (int i = 0; i < count; i++) {
-            final View child = children[i];
-            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
-                if (!child.gatherTransparentRegion(region)) {
-                    noneOfTheChildrenAreTransparent = false;
+        if (childrenCount > 0) {
+            final ArrayList<View> preorderedList = buildOrderedChildList();
+            final boolean customOrder = preorderedList == null
+                    && isChildrenDrawingOrderEnabled();
+            final View[] children = mChildren;
+            for (int i = 0; i < childrenCount; i++) {
+                final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
+                final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);
+                if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {
+                    if (!child.gatherTransparentRegion(region)) {
+                        noneOfTheChildrenAreTransparent = false;
+                    }
                 }
             }
+            if (preorderedList != null) preorderedList.clear();
         }
         return meOpaque || noneOfTheChildrenAreTransparent;
     }
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index 195786d..f761b9b 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -44,6 +44,7 @@
 import android.graphics.PorterDuff;
 import android.graphics.Rect;
 import android.graphics.Region;
+import android.graphics.drawable.AnimatedVectorDrawable;
 import android.graphics.drawable.Drawable;
 import android.hardware.display.DisplayManager;
 import android.hardware.display.DisplayManager.DisplayListener;
@@ -822,6 +823,13 @@
         }
     }
 
+    public void registerVectorDrawableAnimator(
+            AnimatedVectorDrawable.VectorDrawableAnimatorRT animator) {
+        if (mAttachInfo.mHardwareRenderer != null) {
+            mAttachInfo.mHardwareRenderer.registerVectorDrawableAnimator(animator);
+        }
+    }
+
     private void enableHardwareAcceleration(WindowManager.LayoutParams attrs) {
         mAttachInfo.mHardwareAccelerated = false;
         mAttachInfo.mHardwareAccelerationRequested = false;
@@ -1459,6 +1467,8 @@
         final int viewVisibility = getHostVisibility();
         final boolean viewVisibilityChanged = !mFirst
                 && (mViewVisibility != viewVisibility || mNewSurfaceNeeded);
+        final boolean viewUserVisibilityChanged = !mFirst &&
+                ((mViewVisibility == View.VISIBLE) != (viewVisibility == View.VISIBLE));
 
         WindowManager.LayoutParams params = null;
         if (mWindowAttributesChanged) {
@@ -1532,7 +1542,9 @@
         if (viewVisibilityChanged) {
             mAttachInfo.mWindowVisibility = viewVisibility;
             host.dispatchWindowVisibilityChanged(viewVisibility);
-            host.dispatchVisibilityAggregated(viewVisibility == View.VISIBLE);
+            if (viewUserVisibilityChanged) {
+                host.dispatchVisibilityAggregated(viewVisibility == View.VISIBLE);
+            }
             if (viewVisibility != View.VISIBLE || mNewSurfaceNeeded) {
                 endDragResizing();
                 destroyHardwareResources();
@@ -1723,7 +1735,7 @@
             }
 
             boolean hwInitialized = false;
-            boolean framesChanged = false;
+            boolean contentInsetsChanged = false;
             boolean hadSurface = mSurface.isValid();
 
             try {
@@ -1763,7 +1775,7 @@
 
                 final boolean overscanInsetsChanged = !mPendingOverscanInsets.equals(
                         mAttachInfo.mOverscanInsets);
-                boolean contentInsetsChanged = !mPendingContentInsets.equals(
+                contentInsetsChanged = !mPendingContentInsets.equals(
                         mAttachInfo.mContentInsets);
                 final boolean visibleInsetsChanged = !mPendingVisibleInsets.equals(
                         mAttachInfo.mVisibleInsets);
@@ -1813,19 +1825,6 @@
                             + mAttachInfo.mVisibleInsets);
                 }
 
-                // If any of the insets changed, do a forceLayout on the view so that the
-                // measure cache is cleared. We might have a pending MSG_RESIZED_REPORT
-                // that is supposed to take care of it, but since pending insets are
-                // already modified here, it won't detect the frame change after this.
-                framesChanged = overscanInsetsChanged
-                        || contentInsetsChanged
-                        || stableInsetsChanged
-                        || visibleInsetsChanged
-                        || outsetsChanged;
-                if (mAdded && mView != null && framesChanged) {
-                    forceLayout(mView);
-                }
-
                 if (!hadSurface) {
                     if (mSurface.isValid()) {
                         // If we are creating a new surface, then we need to
@@ -2009,7 +2008,7 @@
                 boolean focusChangedDueToTouchMode = ensureTouchModeLocally(
                         (relayoutResult&WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE) != 0);
                 if (focusChangedDueToTouchMode || mWidth != host.getMeasuredWidth()
-                        || mHeight != host.getMeasuredHeight() || framesChanged ||
+                        || mHeight != host.getMeasuredHeight() || contentInsetsChanged ||
                         updatedConfiguration) {
                     int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
                     int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
@@ -2018,7 +2017,7 @@
                             + mWidth + " measuredWidth=" + host.getMeasuredWidth()
                             + " mHeight=" + mHeight
                             + " measuredHeight=" + host.getMeasuredHeight()
-                            + " framesChanged=" + framesChanged);
+                            + " coveredInsetsChanged=" + contentInsetsChanged);
 
                      // Ask host how big it wants to be
                     performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
@@ -2168,7 +2167,12 @@
         }
 
         if (changedVisibility || regainedFocus) {
-            host.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
+            // Toasts are presented as notifications - don't present them as windows as well
+            boolean isToast = (mWindowAttributes == null) ? false
+                    : (mWindowAttributes.type == WindowManager.LayoutParams.TYPE_TOAST);
+            if (!isToast) {
+                host.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
+            }
         }
 
         mFirst = false;
@@ -3178,7 +3182,7 @@
             }
             focusNode.recycle();
         }
-        if (mAccessibilityFocusedHost != null) {
+        if ((mAccessibilityFocusedHost != null) && (mAccessibilityFocusedHost != view))  {
             // Clear accessibility focus in the view.
             mAccessibilityFocusedHost.clearAccessibilityFocusNoCallbacks(
                     AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
@@ -5507,22 +5511,24 @@
         if (mView != null && mAdded) {
             final int what = event.mAction;
 
+            // Cache the drag description when the operation starts, then fill it in
+            // on subsequent calls as a convenience
+            if (what == DragEvent.ACTION_DRAG_STARTED) {
+                mCurrentDragView = null;    // Start the current-recipient tracking
+                mDragDescription = event.mClipDescription;
+            } else {
+                event.mClipDescription = mDragDescription;
+            }
+
             if (what == DragEvent.ACTION_DRAG_EXITED) {
                 // A direct EXITED event means that the window manager knows we've just crossed
                 // a window boundary, so the current drag target within this one must have
-                // just been exited.  Send it the usual notifications and then we're done
-                // for now.
-                mView.dispatchDragEvent(event);
-            } else {
-                // Cache the drag description when the operation starts, then fill it in
-                // on subsequent calls as a convenience
-                if (what == DragEvent.ACTION_DRAG_STARTED) {
-                    mCurrentDragView = null;    // Start the current-recipient tracking
-                    mDragDescription = event.mClipDescription;
-                } else {
-                    event.mClipDescription = mDragDescription;
+                // just been exited. Send the EXITED notification to the current drag view, if any.
+                if (View.sCascadedDragDrop) {
+                    mView.dispatchDragEnterExitInPreN(event);
                 }
-
+                setDragFocus(null, event);
+            } else {
                 // For events with a [screen] location, translate into window coordinates
                 if ((what == DragEvent.ACTION_DRAG_LOCATION) || (what == DragEvent.ACTION_DROP)) {
                     mDragPoint.set(event.mX, event.mY);
@@ -5544,6 +5550,12 @@
                 // Now dispatch the drag/drop event
                 boolean result = mView.dispatchDragEvent(event);
 
+                if (what == DragEvent.ACTION_DRAG_LOCATION && !event.mEventHandlerWasCalled) {
+                    // If the LOCATION event wasn't delivered to any handler, no view now has a drag
+                    // focus.
+                    setDragFocus(null, event);
+                }
+
                 // If we changed apparent drag target, tell the OS about it
                 if (prevDragView != mCurrentDragView) {
                     try {
@@ -5571,6 +5583,7 @@
 
                 // When the drag operation ends, reset drag-related state
                 if (what == DragEvent.ACTION_DRAG_ENDED) {
+                    mCurrentDragView = null;
                     setLocalDragState(null);
                     mAttachInfo.mDragToken = null;
                     if (mAttachInfo.mDragSurface != null) {
@@ -5630,10 +5643,33 @@
         return mLastTouchSource;
     }
 
-    public void setDragFocus(View newDragTarget) {
-        if (mCurrentDragView != newDragTarget) {
-            mCurrentDragView = newDragTarget;
+    public void setDragFocus(View newDragTarget, DragEvent event) {
+        if (mCurrentDragView != newDragTarget && !View.sCascadedDragDrop) {
+            // Send EXITED and ENTERED notifications to the old and new drag focus views.
+
+            final float tx = event.mX;
+            final float ty = event.mY;
+            final int action = event.mAction;
+            // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED.
+            event.mX = 0;
+            event.mY = 0;
+
+            if (mCurrentDragView != null) {
+                event.mAction = DragEvent.ACTION_DRAG_EXITED;
+                mCurrentDragView.callDragEventHandler(event);
+            }
+
+            if (newDragTarget != null) {
+                event.mAction = DragEvent.ACTION_DRAG_ENTERED;
+                newDragTarget.callDragEventHandler(event);
+            }
+
+            event.mAction = action;
+            event.mX = tx;
+            event.mY = ty;
         }
+
+        mCurrentDragView = newDragTarget;
     }
 
     private AudioManager getAudioManager() {
@@ -6044,7 +6080,8 @@
                 return true;
             }
             return mEvent instanceof MotionEvent
-                    && mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER);
+                    && (mEvent.isFromSource(InputDevice.SOURCE_CLASS_POINTER)
+                        || mEvent.isFromSource(InputDevice.SOURCE_ROTARY_ENCODER));
         }
 
         public boolean shouldSendToSynthesizer() {
diff --git a/core/java/android/view/WindowManager.java b/core/java/android/view/WindowManager.java
index fe24230..395f738 100644
--- a/core/java/android/view/WindowManager.java
+++ b/core/java/android/view/WindowManager.java
@@ -221,6 +221,7 @@
          * @see #TYPE_BASE_APPLICATION
          * @see #TYPE_APPLICATION
          * @see #TYPE_APPLICATION_STARTING
+         * @see #TYPE_DRAWN_APPLICATION
          * @see #TYPE_APPLICATION_PANEL
          * @see #TYPE_APPLICATION_MEDIA
          * @see #TYPE_APPLICATION_SUB_PANEL
@@ -244,6 +245,7 @@
             @ViewDebug.IntToString(from = TYPE_BASE_APPLICATION, to = "TYPE_BASE_APPLICATION"),
             @ViewDebug.IntToString(from = TYPE_APPLICATION, to = "TYPE_APPLICATION"),
             @ViewDebug.IntToString(from = TYPE_APPLICATION_STARTING, to = "TYPE_APPLICATION_STARTING"),
+            @ViewDebug.IntToString(from = TYPE_DRAWN_APPLICATION, to = "TYPE_DRAWN_APPLICATION"),
             @ViewDebug.IntToString(from = TYPE_APPLICATION_PANEL, to = "TYPE_APPLICATION_PANEL"),
             @ViewDebug.IntToString(from = TYPE_APPLICATION_MEDIA, to = "TYPE_APPLICATION_MEDIA"),
             @ViewDebug.IntToString(from = TYPE_APPLICATION_SUB_PANEL, to = "TYPE_APPLICATION_SUB_PANEL"),
@@ -315,6 +317,13 @@
         public static final int TYPE_APPLICATION_STARTING = 3;
 
         /**
+         * Window type: a variation on TYPE_APPLICATION that ensures the window
+         * manager will wait for this window to be drawn before the app is shown.
+         * In multiuser systems shows only on the owning user's window.
+         */
+        public static final int TYPE_DRAWN_APPLICATION = 4;
+
+        /**
          * End of types of application windows.
          */
         public static final int LAST_APPLICATION_WINDOW = 99;
@@ -636,7 +645,7 @@
 
         /**
          * Window type: shares similar characteristics with {@link #TYPE_DREAM}. The layer is
-         * reserved for screenshot region selection.
+         * reserved for screenshot region selection. These windows must not take input focus.
          * @hide
          */
         public static final int TYPE_SCREENSHOT = FIRST_SYSTEM_WINDOW + 36;
@@ -1581,6 +1590,15 @@
         public static final int ROTATION_ANIMATION_JUMPCUT = 2;
 
         /**
+         * Value for {@link #rotationAnimation} to specify seamless rotation mode.
+         * This works like JUMPCUT but will fall back to CROSSFADE if rotation
+         * can't be applied without pausing the screen.
+         *
+         * @hide
+         */
+        public static final int ROTATION_ANIMATION_SEAMLESS = 3;
+
+        /**
          * Define the exit and entry animations used on this window when the device is rotated.
          * This only has an affect if the incoming and outgoing topmost
          * opaque windows have the #FLAG_FULLSCREEN bit set and are not covered
@@ -1734,14 +1752,18 @@
         public CharSequence accessibilityTitle;
 
         /**
-         * Sets a timeout in milliseconds before which the window will be removed
+         * Sets a timeout in milliseconds before which the window will be hidden
          * by the window manager. Useful for transient notifications like toasts
          * so we don't have to rely on client cooperation to ensure the window
-         * is removed. Must be specified at window creation time.
+         * is hidden. Must be specified at window creation time. Note that apps
+         * are not prepared to handle their windows being removed without their
+         * explicit request and may try to interact with the removed window
+         * resulting in undefined behavior and crashes. Therefore, we do hide
+         * such windows to prevent them from overlaying other apps.
          *
          * @hide
          */
-        public long removeTimeoutMilliseconds = -1;
+        public long hideTimeoutMilliseconds = -1;
 
         public LayoutParams() {
             super(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
@@ -1877,7 +1899,7 @@
             out.writeInt(needsMenuKey);
             out.writeInt(accessibilityIdOfAnchor);
             TextUtils.writeToParcel(accessibilityTitle, out, parcelableFlags);
-            out.writeLong(removeTimeoutMilliseconds);
+            out.writeLong(hideTimeoutMilliseconds);
         }
 
         public static final Parcelable.Creator<LayoutParams> CREATOR
@@ -1931,7 +1953,7 @@
             needsMenuKey = in.readInt();
             accessibilityIdOfAnchor = in.readInt();
             accessibilityTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
-            removeTimeoutMilliseconds = in.readLong();
+            hideTimeoutMilliseconds = in.readLong();
         }
 
         @SuppressWarnings({"PointlessBitwiseExpression"})
@@ -2153,7 +2175,7 @@
             }
 
             // This can't change, it's only set at window creation time.
-            removeTimeoutMilliseconds = o.removeTimeoutMilliseconds;
+            hideTimeoutMilliseconds = o.hideTimeoutMilliseconds;
 
             return changes;
         }
diff --git a/core/java/android/view/WindowManagerPolicy.java b/core/java/android/view/WindowManagerPolicy.java
index e6f5b83..46a0194 100644
--- a/core/java/android/view/WindowManagerPolicy.java
+++ b/core/java/android/view/WindowManagerPolicy.java
@@ -136,6 +136,12 @@
             throws RemoteException;
 
     /**
+     * @return true if windows with FLAG_DISMISS_KEYGUARD should be allowed to show even if
+     *         the keyguard is locked.
+     */
+    boolean canShowDismissingWindowWhileLockedLw();
+
+    /**
      * Interface to the Window Manager state associated with a particular
      * window.  You can hold on to an instance of this interface from the call
      * to prepareAddWindow() until removeWindow().
@@ -416,6 +422,8 @@
          * screen with other application windows.
          */
         public boolean isInMultiWindowMode();
+
+        public int getRotationAnimationHint();
     }
 
     /**
@@ -476,6 +484,7 @@
         public void switchInputMethod(boolean forwardDirection);
 
         public void shutdown(boolean confirm);
+        public void reboot(boolean confirm);
         public void rebootSafeMode(boolean confirm);
 
         /**
@@ -498,6 +507,11 @@
          * Retrieves the {@param outBounds} from the stack with id {@param stackId}.
          */
         void getStackBounds(int stackId, Rect outBounds);
+
+        /**
+         * Overrides all currently playing app animations with {@param a}.
+         */
+        void overridePlayingAppAnimationsLw(Animation a);
     }
 
     public interface PointerEventListener {
@@ -1292,6 +1306,16 @@
     public int adjustSystemUiVisibilityLw(int visibility);
 
     /**
+     * Called by System UI to notify of changes to the visibility of Recents.
+     */
+    public void setRecentsVisibilityLw(boolean visible);
+
+    /**
+     * Called by System UI to notify of changes to the visibility of PIP.
+     */
+    public void setTvPipVisibilityLw(boolean visible);
+
+    /**
      * Specifies whether there is an on-screen navigation bar separate from the status bar.
      */
     public boolean hasNavigationBar();
@@ -1410,4 +1434,6 @@
      * Called when the configuration has changed, and it's safe to load new values from resources.
      */
     public void onConfigurationChanged();
+
+    public boolean shouldRotateSeamlessly(int oldRotation, int newRotation);
 }
diff --git a/core/java/android/view/accessibility/AccessibilityManager.java b/core/java/android/view/accessibility/AccessibilityManager.java
index 81ec330..2dfa8cd 100644
--- a/core/java/android/view/accessibility/AccessibilityManager.java
+++ b/core/java/android/view/accessibility/AccessibilityManager.java
@@ -305,7 +305,18 @@
                 return;
             }
             if (!mIsEnabled) {
-                throw new IllegalStateException("Accessibility off. Did you forget to check that?");
+                Looper myLooper = Looper.myLooper();
+                if (myLooper == Looper.getMainLooper()) {
+                    throw new IllegalStateException(
+                            "Accessibility off. Did you forget to check that?");
+                } else {
+                    // If we're not running on the thread with the main looper, it's possible for
+                    // the state of accessibility to change between checking isEnabled and
+                    // calling this method. So just log the error rather than throwing the
+                    // exception.
+                    Log.e(LOG_TAG, "AccessibilityEvent sent with accessibility disabled");
+                    return;
+                }
             }
             userId = mUserId;
         }
diff --git a/core/java/android/view/animation/AnimationUtils.java b/core/java/android/view/animation/AnimationUtils.java
index a54d94c..351b6db 100644
--- a/core/java/android/view/animation/AnimationUtils.java
+++ b/core/java/android/view/animation/AnimationUtils.java
@@ -44,6 +44,31 @@
     private static final int TOGETHER = 0;
     private static final int SEQUENTIALLY = 1;
 
+    private static class AnimationState {
+        boolean animationClockLocked;
+        long currentVsyncTimeMillis;
+        long lastReportedTimeMillis;
+    };
+
+    private static ThreadLocal<AnimationState> sAnimationState
+            = new ThreadLocal<AnimationState>() {
+        @Override
+        protected AnimationState initialValue() {
+            return new AnimationState();
+        }
+    };
+
+    /** @hide */
+    public static void lockAnimationClock(long vsyncMillis) {
+        AnimationState state = sAnimationState.get();
+        state.animationClockLocked = true;
+        state.currentVsyncTimeMillis = vsyncMillis;
+    }
+
+    /** @hide */
+    public static void unlockAnimationClock() {
+        sAnimationState.get().animationClockLocked = false;
+    }
 
     /**
      * Returns the current animation time in milliseconds. This time should be used when invoking
@@ -56,7 +81,14 @@
      * @see android.os.SystemClock
      */
     public static long currentAnimationTimeMillis() {
-        return SystemClock.uptimeMillis();
+        AnimationState state = sAnimationState.get();
+        if (state.animationClockLocked) {
+            // It's important that time never rewinds
+            return Math.max(state.currentVsyncTimeMillis,
+                    state.lastReportedTimeMillis);
+        }
+        state.lastReportedTimeMillis = SystemClock.uptimeMillis();
+        return state.lastReportedTimeMillis;
     }
 
     /**
diff --git a/core/java/android/view/inputmethod/BaseInputConnection.java b/core/java/android/view/inputmethod/BaseInputConnection.java
index 89dec2d..38962a3 100644
--- a/core/java/android/view/inputmethod/BaseInputConnection.java
+++ b/core/java/android/view/inputmethod/BaseInputConnection.java
@@ -851,4 +851,11 @@
         
         endBatchEdit();
     }
+
+    /**
+     * The default implementation does nothing.
+     */
+    public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) {
+        return false;
+    }
 }
diff --git a/core/java/android/view/inputmethod/EditorInfo.java b/core/java/android/view/inputmethod/EditorInfo.java
index 7b7ccae..8038089 100644
--- a/core/java/android/view/inputmethod/EditorInfo.java
+++ b/core/java/android/view/inputmethod/EditorInfo.java
@@ -25,6 +25,8 @@
 import android.text.TextUtils;
 import android.util.Printer;
 
+import java.util.Arrays;
+
 /**
  * An EditorInfo describes several attributes of a text editing object
  * that an input method is communicating with (typically an EditText), most
@@ -363,6 +365,18 @@
     @Nullable
     public LocaleList hintLocales = null;
 
+
+    /**
+     * List of acceptable MIME types for
+     * {@link InputConnection#commitContent(InputContentInfo, int, Bundle)}.
+     *
+     * <p>{@code null} or an empty array means that
+     * {@link InputConnection#commitContent(InputContentInfo, int, Bundle)} is not supported in this
+     * editor.</p>
+     */
+    @Nullable
+    public String[] contentMimeTypes = null;
+
     /**
      * Ensure that the data in this EditorInfo is compatible with an application
      * that was developed against the given target API version.  This can
@@ -418,6 +432,7 @@
                 + " fieldName=" + fieldName);
         pw.println(prefix + "extras=" + extras);
         pw.println(prefix + "hintLocales=" + hintLocales);
+        pw.println(prefix + "contentMimeTypes=" + Arrays.toString(contentMimeTypes));
     }
 
     /**
@@ -446,6 +461,7 @@
         } else {
             LocaleList.getEmptyLocaleList().writeToParcel(dest, flags);
         }
+        dest.writeStringArray(contentMimeTypes);
     }
 
     /**
@@ -471,6 +487,7 @@
                     res.extras = source.readBundle();
                     LocaleList hintLocales = LocaleList.CREATOR.createFromParcel(source);
                     res.hintLocales = hintLocales.isEmpty() ? null : hintLocales;
+                    res.contentMimeTypes = source.readStringArray();
                     return res;
                 }
 
diff --git a/core/java/android/view/inputmethod/InputConnection.java b/core/java/android/view/inputmethod/InputConnection.java
index 9f66429..8023201 100644
--- a/core/java/android/view/inputmethod/InputConnection.java
+++ b/core/java/android/view/inputmethod/InputConnection.java
@@ -16,6 +16,8 @@
 
 package android.view.inputmethod;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.os.Bundle;
 import android.os.Handler;
 import android.view.KeyCharacterMap;
@@ -41,12 +43,14 @@
  *     in {@link android.os.Build.VERSION_CODES#HONEYCOMB}.</li>
  *     <li>{@link #requestCursorUpdates(int)}, which was introduced in
  *     {@link android.os.Build.VERSION_CODES#LOLLIPOP}.</li>
- *     <li>{@link #deleteSurroundingTextInCodePoints(int, int)}}, which
+ *     <li>{@link #deleteSurroundingTextInCodePoints(int, int)}, which
  *     was introduced in {@link android.os.Build.VERSION_CODES#N}.</li>
- *     <li>{@link #getHandler()}}, which was introduced in
+ *     <li>{@link #getHandler()}, which was introduced in
  *     {@link android.os.Build.VERSION_CODES#N}.</li>
- *     <li>{@link #closeConnection()}}, which was introduced in
+ *     <li>{@link #closeConnection()}, which was introduced in
  *     {@link android.os.Build.VERSION_CODES#N}.</li>
+ *     <li>{@link #commitContent(InputContentInfo, int, Bundle)}, which was
+ *     introduced in {@link android.os.Build.VERSION_CODES#N_MR1}.</li>
  * </ul>
  *
  * <h3>Implementing an IME or an editor</h3>
@@ -836,4 +840,53 @@
      * <p>Note: This does nothing when called from input methods.</p>
      */
     public void closeConnection();
+
+    /**
+     * When this flag is used, the editor will be able to request read access to the content URI
+     * contained in the {@link InputContentInfo} object.
+     *
+     * <p>Make sure that the content provider owning the Uri sets the
+     * {@link android.R.styleable#AndroidManifestProvider_grantUriPermissions
+     * grantUriPermissions} attribute in its manifest or included the
+     * {@link android.R.styleable#AndroidManifestGrantUriPermission
+     * &lt;grant-uri-permissions&gt;} tag. Otherwise {@link InputContentInfo#requestPermission()}
+     * can fail.</p>
+     *
+     * <p>Although calling this API is allowed only for the IME that is currently selected, the
+     * client is able to request a temporary read-only access even after the current IME is switched
+     * to any other IME as long as the client keeps {@link InputContentInfo} object.</p>
+     **/
+    public static int INPUT_CONTENT_GRANT_READ_URI_PERMISSION =
+            android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;  // 0x00000001
+
+    /**
+     * Called by the input method to commit a content such as PNG image to the editor.
+     *
+     * <p>In order to avoid variety of compatibility issues, this focuses on a simple use case,
+     * where we expect editors and IMEs work cooperatively as follows:</p>
+     * <ul>
+     *     <li>Editor must keep {@link EditorInfo#contentMimeTypes} to be {@code null} if it does
+     *     not support this method at all.</li>
+     *     <li>Editor can ignore this request when the MIME type specified in
+     *     {@code inputContentInfo} does not match to any of {@link EditorInfo#contentMimeTypes}.
+     *     </li>
+     *     <li>Editor can ignore the cursor position when inserting the provided context.</li>
+     *     <li>Editor can return {@code true} asynchronously, even before it starts loading the
+     *     content.</li>
+     *     <li>Editor should provide a way to delete the content inserted by this method, or revert
+     *     the effect caused by this method.</li>
+     *     <li>IME should not call this method when there is any composing text, in case calling
+     *     this method causes focus change.</li>
+     *     <li>IME should grant a permission for the editor to read the content. See
+     *     {@link EditorInfo#packageName} about how to obtain the package name of the editor.</li>
+     * </ul>
+     *
+     * @param inputContentInfo Content to be inserted.
+     * @param flags {@code 0} or {@link #INPUT_CONTENT_GRANT_READ_URI_PERMISSION}.
+     * @param opts optional bundle data. This can be {@code null}.
+     * @return {@code true} if this request is accepted by the application, no matter if the request
+     * is already handled or still being handled in background.
+     */
+    public boolean commitContent(@NonNull InputContentInfo inputContentInfo, int flags,
+            @Nullable Bundle opts);
 }
diff --git a/core/java/android/view/inputmethod/InputConnectionInspector.java b/core/java/android/view/inputmethod/InputConnectionInspector.java
index 118a61f..2b292bb 100644
--- a/core/java/android/view/inputmethod/InputConnectionInspector.java
+++ b/core/java/android/view/inputmethod/InputConnectionInspector.java
@@ -19,6 +19,7 @@
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.os.Bundle;
 
 import java.lang.annotation.Retention;
 import java.lang.reflect.Method;
@@ -41,6 +42,8 @@
             MissingMethodFlags.REQUEST_CURSOR_UPDATES,
             MissingMethodFlags.DELETE_SURROUNDING_TEXT_IN_CODE_POINTS,
             MissingMethodFlags.GET_HANDLER,
+            MissingMethodFlags.CLOSE_CONNECTION,
+            MissingMethodFlags.COMMIT_CONTENT,
     })
     public @interface MissingMethodFlags {
         /**
@@ -78,6 +81,11 @@
          * {@link android.os.Build.VERSION_CODES#N} and later.
          */
         int CLOSE_CONNECTION = 1 << 6;
+        /**
+         * {@link InputConnection#commitContent(InputContentInfo, int, Bundle)} is available in
+         * {@link android.os.Build.VERSION_CODES#N} MR-1 and later.
+         */
+        int COMMIT_CONTENT = 1 << 7;
     }
 
     private static final Map<Class, Integer> sMissingMethodsMap = Collections.synchronizedMap(
@@ -127,6 +135,9 @@
         if (!hasCloseConnection(clazz)) {
             flags |= MissingMethodFlags.CLOSE_CONNECTION;
         }
+        if (!hasCommitContent(clazz)) {
+            flags |= MissingMethodFlags.COMMIT_CONTENT;
+        }
         sMissingMethodsMap.put(clazz, flags);
         return flags;
     }
@@ -195,6 +206,16 @@
         }
     }
 
+    private static boolean hasCommitContent(@NonNull final Class clazz) {
+        try {
+            final Method method = clazz.getMethod("commitContent", InputContentInfo.class,
+                    int.class, Bundle.class);
+            return !Modifier.isAbstract(method.getModifiers());
+        } catch (NoSuchMethodException e) {
+            return false;
+        }
+    }
+
     public static String getMissingMethodFlagsAsString(@MissingMethodFlags final int flags) {
         final StringBuilder sb = new StringBuilder();
         boolean isEmpty = true;
@@ -242,6 +263,12 @@
             }
             sb.append("closeConnection()");
         }
+        if ((flags & MissingMethodFlags.COMMIT_CONTENT) != 0) {
+            if (!isEmpty) {
+                sb.append(",");
+            }
+            sb.append("commitContent(InputContentInfo, Bundle)");
+        }
         return sb.toString();
     }
 }
diff --git a/core/java/android/view/inputmethod/InputConnectionWrapper.java b/core/java/android/view/inputmethod/InputConnectionWrapper.java
index e743f62..317730c 100644
--- a/core/java/android/view/inputmethod/InputConnectionWrapper.java
+++ b/core/java/android/view/inputmethod/InputConnectionWrapper.java
@@ -269,4 +269,12 @@
     public void closeConnection() {
         mTarget.closeConnection();
     }
+
+    /**
+     * {@inheritDoc}
+     * @throws NullPointerException if the target is {@code null}.
+     */
+    public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) {
+        return mTarget.commitContent(inputContentInfo, flags, opts);
+    }
 }
diff --git a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl b/core/java/android/view/inputmethod/InputContentInfo.aidl
similarity index 73%
copy from telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
copy to core/java/android/view/inputmethod/InputContentInfo.aidl
index b7e78d1..1afeee3b 100644
--- a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
+++ b/core/java/android/view/inputmethod/InputContentInfo.aidl
@@ -1,11 +1,11 @@
 /*
- * Copyright 2016, The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ *      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,9 +14,6 @@
  * limitations under the License.
  */
 
-package android.telecom;
+package android.view.inputmethod;
 
-/**
- * {@hide}
- */
-parcelable ParcelableCallAnalytics;
+parcelable InputContentInfo;
diff --git a/core/java/android/view/inputmethod/InputContentInfo.java b/core/java/android/view/inputmethod/InputContentInfo.java
new file mode 100644
index 0000000..b39705e
--- /dev/null
+++ b/core/java/android/view/inputmethod/InputContentInfo.java
@@ -0,0 +1,250 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package android.view.inputmethod;
+
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.ClipDescription;
+import android.net.Uri;
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.os.RemoteException;
+
+import com.android.internal.inputmethod.IInputContentUriToken;
+
+import java.security.InvalidParameterException;
+
+/**
+ * A container object with which input methods can send content files to the target application.
+ */
+public final class InputContentInfo implements Parcelable {
+
+    @NonNull
+    private final Uri mContentUri;
+    @NonNull
+    private final ClipDescription mDescription;
+    @Nullable
+    private final Uri mLinkUri;
+    @NonNull
+    private IInputContentUriToken mUriToken;
+
+    /**
+     * Constructs {@link InputContentInfo} object only with mandatory data.
+     *
+     * @param contentUri Content URI to be exported from the input method.
+     * This cannot be {@code null}.
+     * @param description A {@link ClipDescription} object that contains the metadata of
+     * {@code contentUri} such as MIME type(s). This object cannot be {@code null}. Also
+     * {@link ClipDescription#getLabel()} should be describing the content specified by
+     * {@code contentUri} for accessibility reasons.
+     */
+    public InputContentInfo(@NonNull Uri contentUri, @NonNull ClipDescription description) {
+        this(contentUri, description, null /* link Uri */);
+    }
+
+    /**
+     * Constructs {@link InputContentInfo} object with additional link URI.
+     *
+     * @param contentUri Content URI to be exported from the input method.
+     * This cannot be {@code null}.
+     * @param description A {@link ClipDescription} object that contains the metadata of
+     * {@code contentUri} such as MIME type(s). This object cannot be {@code null}. Also
+     * {@link ClipDescription#getLabel()} should be describing the content specified by
+     * {@code contentUri} for accessibility reasons.
+     * @param linkUri An optional {@code http} or {@code https} URI. The editor author may provide
+     * a way to navigate the user to the specified web page if this is not {@code null}.
+     * @throws InvalidParameterException if any invalid parameter is specified.
+     */
+    public InputContentInfo(@NonNull Uri contentUri, @NonNull ClipDescription description,
+            @Nullable Uri linkUri) {
+        validateInternal(contentUri, description, linkUri, true /* throwException */);
+        mContentUri = contentUri;
+        mDescription = description;
+        mLinkUri = linkUri;
+    }
+
+    /**
+     * @return {@code true} if all the fields are valid.
+     * @hide
+     */
+    public boolean validate() {
+        return validateInternal(mContentUri, mDescription, mLinkUri, false /* throwException */);
+    }
+
+    /**
+     * Constructs {@link InputContentInfo} object with additional link URI.
+     *
+     * @param contentUri Content URI to be exported from the input method.
+     * This cannot be {@code null}.
+     * @param description A {@link ClipDescription} object that contains the metadata of
+     * {@code contentUri} such as MIME type(s). This object cannot be {@code null}. Also
+     * {@link ClipDescription#getLabel()} should be describing the content specified by
+     * {@code contentUri} for accessibility reasons.
+     * @param linkUri An optional {@code http} or {@code https} URI. The editor author may provide
+     * a way to navigate the user to the specified web page if this is not {@code null}.
+     * @param throwException {@code true} if this method should throw an
+     * {@link InvalidParameterException}.
+     * @throws InvalidParameterException if any invalid parameter is specified.
+     */
+    private static boolean validateInternal(@NonNull Uri contentUri,
+            @NonNull ClipDescription description, @Nullable Uri linkUri, boolean throwException) {
+        if (contentUri == null) {
+            if (throwException) {
+                throw new NullPointerException("contentUri");
+            }
+            return false;
+        }
+        if (description == null) {
+            if (throwException) {
+                throw new NullPointerException("description");
+            }
+            return false;
+        }
+        final String contentUriScheme = contentUri.getScheme();
+        if (!"content".equals(contentUriScheme)) {
+            if (throwException) {
+                throw new InvalidParameterException("contentUri must have content scheme");
+            }
+            return false;
+        }
+        if (linkUri != null) {
+            final String scheme = linkUri.getScheme();
+            if (scheme == null ||
+                    (!scheme.equalsIgnoreCase("http") && !scheme.equalsIgnoreCase("https"))) {
+                if (throwException) {
+                    throw new InvalidParameterException(
+                            "linkUri must have either http or https scheme");
+                }
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * @return Content URI with which the content can be obtained.
+     */
+    @NonNull
+    public Uri getContentUri() { return mContentUri; }
+
+    /**
+     * @return {@link ClipDescription} object that contains the metadata of {@code #getContentUri()}
+     * such as MIME type(s). {@link ClipDescription#getLabel()} can be used for accessibility
+     * purpose.
+     */
+    @NonNull
+    public ClipDescription getDescription() { return mDescription; }
+
+    /**
+     * @return An optional {@code http} or {@code https} URI that is related to this content.
+     */
+    @Nullable
+    public Uri getLinkUri() { return mLinkUri; }
+
+    void setUriToken(IInputContentUriToken token) {
+        if (mUriToken != null) {
+            throw new IllegalStateException("URI token is already set");
+        }
+        mUriToken = token;
+    }
+
+    /**
+     * Requests a temporary read-only access permission for content URI associated with this object.
+     *
+     * <p>Does nothing if the temporary permission is already granted.</p>
+     */
+    public void requestPermission() {
+        if (mUriToken == null) {
+            return;
+        }
+        try {
+            mUriToken.take();
+        } catch (RemoteException e) {
+            e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Releases a temporary read-only access permission for content URI associated with this object.
+     *
+     * <p>Does nothing if the temporary permission is not granted.</p>
+     */
+    public void releasePermission() {
+        if (mUriToken == null) {
+            return;
+        }
+        try {
+            mUriToken.release();
+        } catch (RemoteException e) {
+            e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Used to package this object into a {@link Parcel}.
+     *
+     * @param dest The {@link Parcel} to be written.
+     * @param flags The flags used for parceling.
+     */
+    @Override
+    public void writeToParcel(Parcel dest, int flags) {
+        Uri.writeToParcel(dest, mContentUri);
+        mDescription.writeToParcel(dest, flags);
+        Uri.writeToParcel(dest, mLinkUri);
+        if (mUriToken != null) {
+            dest.writeInt(1);
+            dest.writeStrongBinder(mUriToken.asBinder());
+        } else {
+            dest.writeInt(0);
+        }
+    }
+
+    private InputContentInfo(@NonNull Parcel source) {
+        mContentUri = Uri.CREATOR.createFromParcel(source);
+        mDescription = ClipDescription.CREATOR.createFromParcel(source);
+        mLinkUri = Uri.CREATOR.createFromParcel(source);
+        if (source.readInt() == 1) {
+            mUriToken = IInputContentUriToken.Stub.asInterface(source.readStrongBinder());
+        } else {
+            mUriToken = null;
+        }
+    }
+
+    /**
+     * Used to make this class parcelable.
+     */
+    public static final Parcelable.Creator<InputContentInfo> CREATOR
+            = new Parcelable.Creator<InputContentInfo>() {
+        @Override
+        public InputContentInfo createFromParcel(Parcel source) {
+            return new InputContentInfo(source);
+        }
+
+        @Override
+        public InputContentInfo[] newArray(int size) {
+            return new InputContentInfo[size];
+        }
+    };
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+}
diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java
index 0b7ac88..2e9cbf2 100644
--- a/core/java/android/view/inputmethod/InputMethodManager.java
+++ b/core/java/android/view/inputmethod/InputMethodManager.java
@@ -16,6 +16,7 @@
 
 package android.view.inputmethod;
 
+import com.android.internal.inputmethod.IInputContentUriToken;
 import com.android.internal.os.SomeArgs;
 import com.android.internal.view.IInputConnectionWrapper;
 import com.android.internal.view.IInputContext;
@@ -30,6 +31,7 @@
 import android.annotation.RequiresPermission;
 import android.content.Context;
 import android.graphics.Rect;
+import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
@@ -56,6 +58,7 @@
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.security.InvalidParameterException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -2286,6 +2289,40 @@
         }
     }
 
+    /**
+     * Allow the receiver of {@link InputContentInfo} to obtain a temporary read-only access
+     * permission to the content.
+     *
+     * <p>See {@link android.inputmethodservice.InputMethodService#exposeContent(InputContentInfo, EditorInfo)}
+     * for details.</p>
+     *
+     * @param token Supplies the identifying token given to an input method when it was started,
+     * which allows it to perform this operation on itself.
+     * @param inputContentInfo Content to be temporarily exposed from the input method to the
+     * application.
+     * This cannot be {@code null}.
+     * @param editorInfo The editor that receives {@link InputContentInfo}.
+     * @hide
+     */
+    public void exposeContent(@NonNull IBinder token, @NonNull InputContentInfo inputContentInfo,
+            @NonNull EditorInfo editorInfo) {
+        final IInputContentUriToken uriToken;
+        final Uri contentUri = inputContentInfo.getContentUri();
+        try {
+            uriToken = mService.createInputContentUriToken(token, contentUri,
+                    editorInfo.packageName);
+            if (uriToken == null) {
+                return;
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "createInputContentAccessToken failed. contentUri=" + contentUri.toString()
+                    + " packageName=" + editorInfo.packageName, e);
+            return;
+        }
+        inputContentInfo.setUriToken(uriToken);
+        return;
+    }
+
     void doDump(FileDescriptor fd, PrintWriter fout, String[] args) {
         final Printer p = new PrintWriterPrinter(fout);
         p.println("Input method client state for " + this + ":");
diff --git a/core/java/android/widget/AbsListView.java b/core/java/android/widget/AbsListView.java
index 5ba1e9d..f1bfade 100644
--- a/core/java/android/widget/AbsListView.java
+++ b/core/java/android/widget/AbsListView.java
@@ -21,6 +21,7 @@
 import android.annotation.NonNull;
 import android.content.Context;
 import android.content.Intent;
+import android.content.res.Configuration;
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.Rect;
@@ -75,6 +76,7 @@
 import android.view.inputmethod.ExtractedText;
 import android.view.inputmethod.ExtractedTextRequest;
 import android.view.inputmethod.InputConnection;
+import android.view.inputmethod.InputContentInfo;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.RemoteViews.OnClickHandler;
 
@@ -615,6 +617,8 @@
     private int mTouchSlop;
     private float mDensityScale;
 
+    private float mScrollFactor;
+
     private InputConnection mDefInputConnection;
     private InputConnectionWrapper mPublicInputConnection;
 
@@ -856,6 +860,10 @@
                 R.styleable.AbsListView_fastScrollAlwaysVisible, false));
 
         a.recycle();
+
+        if (context.getResources().getConfiguration().uiMode == Configuration.UI_MODE_TYPE_WATCH) {
+            setRevealOnFocusHint(false);
+        }
     }
 
     private void initAbsListView() {
@@ -868,6 +876,7 @@
 
         final ViewConfiguration configuration = ViewConfiguration.get(mContext);
         mTouchSlop = configuration.getScaledTouchSlop();
+        mScrollFactor = configuration.getScaledScrollFactor();
         mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
         mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
         mOverscrollDistance = configuration.getScaledOverscrollDistance();
@@ -1545,7 +1554,7 @@
         switch (action) {
             case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD:
             case R.id.accessibilityActionScrollDown: {
-                if (isEnabled() && getLastVisiblePosition() < getCount() - 1) {
+                if (isEnabled() && canScrollDown()) {
                     final int viewportHeight = getHeight() - mListPadding.top - mListPadding.bottom;
                     smoothScrollBy(viewportHeight, PositionScroller.SCROLL_DURATION);
                     return true;
@@ -1553,7 +1562,7 @@
             } return false;
             case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD:
             case R.id.accessibilityActionScrollUp: {
-                if (isEnabled() && mFirstPosition > 0) {
+                if (isEnabled() && canScrollUp()) {
                     final int viewportHeight = getHeight() - mListPadding.top - mListPadding.bottom;
                     smoothScrollBy(-viewportHeight, PositionScroller.SCROLL_DURATION);
                     return true;
@@ -4200,21 +4209,26 @@
 
     @Override
     public boolean onGenericMotionEvent(MotionEvent event) {
-        if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
-            switch (event.getAction()) {
-                case MotionEvent.ACTION_SCROLL:
-                    if (mTouchMode == TOUCH_MODE_REST) {
-                        final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
-                        if (vscroll != 0) {
-                            final int delta = (int) (vscroll * getVerticalScrollFactor());
-                            if (!trackMotionScroll(delta, delta)) {
-                                return true;
-                            }
-                        }
-                    }
-                    break;
+        switch (event.getAction()) {
+            case MotionEvent.ACTION_SCROLL:
+                final float axisValue;
+                if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) {
+                    axisValue = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
+                } else if (event.isFromSource(InputDevice.SOURCE_ROTARY_ENCODER)) {
+                    axisValue = event.getAxisValue(MotionEvent.AXIS_SCROLL);
+                } else {
+                    axisValue = 0;
+                }
 
-                case MotionEvent.ACTION_BUTTON_PRESS:
+                final int delta = Math.round(axisValue * mScrollFactor);
+                if (delta != 0) {
+                    if (!trackMotionScroll(delta, delta)) {
+                        return true;
+                    }
+                }
+                break;
+            case MotionEvent.ACTION_BUTTON_PRESS:
+                if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) {
                     int actionButton = event.getActionButton();
                     if ((actionButton == MotionEvent.BUTTON_STYLUS_PRIMARY
                             || actionButton == MotionEvent.BUTTON_SECONDARY)
@@ -4224,8 +4238,8 @@
                             removeCallbacks(mPendingCheckForTap);
                         }
                     }
-                    break;
-            }
+                }
+                break;
         }
 
         return super.onGenericMotionEvent(event);
@@ -5976,6 +5990,11 @@
         public void closeConnection() {
             getTarget().closeConnection();
         }
+
+        @Override
+        public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) {
+            return getTarget().commitContent(inputContentInfo, flags, opts);
+        }
     }
 
     /**
diff --git a/core/java/android/widget/DatePicker.java b/core/java/android/widget/DatePicker.java
index 8613f99..54818a1 100644
--- a/core/java/android/widget/DatePicker.java
+++ b/core/java/android/widget/DatePicker.java
@@ -18,7 +18,9 @@
 
 import com.android.internal.R;
 
+import android.annotation.IntDef;
 import android.annotation.Nullable;
+import android.annotation.TestApi;
 import android.annotation.Widget;
 import android.content.Context;
 import android.content.res.Configuration;
@@ -32,6 +34,8 @@
 import android.view.View;
 import android.view.accessibility.AccessibilityEvent;
 
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.util.Locale;
 
 /**
@@ -75,11 +79,36 @@
  */
 @Widget
 public class DatePicker extends FrameLayout {
-    private static final int MODE_SPINNER = 1;
-    private static final int MODE_CALENDAR = 2;
+    /**
+     * Presentation mode for the Holo-style date picker that uses a set of
+     * {@link android.widget.NumberPicker}s.
+     *
+     * @see #getMode()
+     * @hide Visible for testing only.
+     */
+    @TestApi
+    public static final int MODE_SPINNER = 1;
+
+    /**
+     * Presentation mode for the Material-style date picker that uses a
+     * calendar.
+     *
+     * @see #getMode()
+     * @hide Visible for testing only.
+     */
+    @TestApi
+    public static final int MODE_CALENDAR = 2;
+
+    /** @hide */
+    @IntDef({MODE_SPINNER, MODE_CALENDAR})
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface DatePickerMode {}
 
     private final DatePickerDelegate mDelegate;
 
+    @DatePickerMode
+    private final int mMode;
+
     /**
      * The callback used to indicate the user changed the date.
      */
@@ -114,11 +143,20 @@
 
         final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DatePicker,
                 defStyleAttr, defStyleRes);
-        final int mode = a.getInt(R.styleable.DatePicker_datePickerMode, MODE_SPINNER);
+        final boolean isDialogMode = a.getBoolean(R.styleable.DatePicker_dialogMode, false);
+        final int requestedMode = a.getInt(R.styleable.DatePicker_datePickerMode, MODE_SPINNER);
         final int firstDayOfWeek = a.getInt(R.styleable.DatePicker_firstDayOfWeek, 0);
         a.recycle();
 
-        switch (mode) {
+        if (requestedMode == MODE_CALENDAR && isDialogMode) {
+            // You want MODE_CALENDAR? YOU CAN'T HANDLE MODE_CALENDAR! Well,
+            // maybe you can depending on your screen size. Let's check...
+            mMode = context.getResources().getInteger(R.integer.date_picker_mode);
+        } else {
+            mMode = requestedMode;
+        }
+
+        switch (mMode) {
             case MODE_CALENDAR:
                 mDelegate = createCalendarUIDelegate(context, attrs, defStyleAttr, defStyleRes);
                 break;
@@ -145,6 +183,18 @@
     }
 
     /**
+     * @return the picker's presentation mode, one of {@link #MODE_CALENDAR} or
+     *         {@link #MODE_SPINNER}
+     * @attr ref android.R.styleable#DatePicker_datePickerMode
+     * @hide Visible for testing only.
+     */
+    @DatePickerMode
+    @TestApi
+    public int getMode() {
+        return mMode;
+    }
+
+    /**
      * Initialize the state. If the provided values designate an inconsistent
      * date the values are normalized before updating the spinners.
      *
diff --git a/core/java/android/widget/DatePickerCalendarDelegate.java b/core/java/android/widget/DatePickerCalendarDelegate.java
index 2fd52b5..af22ec7 100755
--- a/core/java/android/widget/DatePickerCalendarDelegate.java
+++ b/core/java/android/widget/DatePickerCalendarDelegate.java
@@ -65,6 +65,8 @@
 
     private SimpleDateFormat mYearFormat;
     private SimpleDateFormat mMonthDayFormat;
+    private SimpleDateFormat mAccessibilityEventFormat;
+
 
     // Top-level container.
     private ViewGroup mContainer;
@@ -307,6 +309,9 @@
         mMonthDayFormat.setContext(DisplayContext.CAPITALIZATION_FOR_STANDALONE);
         mYearFormat = new SimpleDateFormat("y", locale);
 
+        // Clear out the lazily-initialized accessibility event formatter.
+        mAccessibilityEventFormat = null;
+
         // Update the header text.
         onCurrentDateChanged(false);
     }
@@ -586,7 +591,12 @@
 
     @Override
     public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
-        event.getText().add(mCurrentDate.getTime().toString());
+        if (mAccessibilityEventFormat == null) {
+            final String pattern = DateFormat.getBestDateTimePattern(mCurrentLocale, "EMMMMdy");
+            mAccessibilityEventFormat = new SimpleDateFormat(pattern);
+        }
+        final CharSequence text = mAccessibilityEventFormat.format(mCurrentDate.getTime());
+        event.getText().add(text);
     }
 
     public CharSequence getAccessibilityClassName() {
diff --git a/core/java/android/widget/ForwardingListener.java b/core/java/android/widget/ForwardingListener.java
index b383e1c..a5fcbc7 100644
--- a/core/java/android/widget/ForwardingListener.java
+++ b/core/java/android/widget/ForwardingListener.java
@@ -58,13 +58,14 @@
 
     public ForwardingListener(View src) {
         mSrc = src;
+        src.setLongClickable(true);
+        src.addOnAttachStateChangeListener(this);
+
         mScaledTouchSlop = ViewConfiguration.get(src.getContext()).getScaledTouchSlop();
         mTapTimeout = ViewConfiguration.getTapTimeout();
 
         // Use a medium-press timeout. Halfway between tap and long-press.
         mLongPressTimeout = (mTapTimeout + ViewConfiguration.getLongPressTimeout()) / 2;
-
-        src.addOnAttachStateChangeListener(this);
     }
 
     /**
diff --git a/core/java/android/widget/FrameLayout.java b/core/java/android/widget/FrameLayout.java
index 029313c..b8c74d8 100644
--- a/core/java/android/widget/FrameLayout.java
+++ b/core/java/android/widget/FrameLayout.java
@@ -382,13 +382,14 @@
 
     @Override
     protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
-        if (lp instanceof LayoutParams) {
-            return new LayoutParams((LayoutParams) lp);
-        } else if (lp instanceof MarginLayoutParams) {
-            return new LayoutParams((MarginLayoutParams) lp);
-        } else {
-            return new LayoutParams(lp);
+        if (sPreserveMarginParamsInLayoutParamConversion) {
+            if (lp instanceof LayoutParams) {
+                return new LayoutParams((LayoutParams) lp);
+            } else if (lp instanceof MarginLayoutParams) {
+                return new LayoutParams((MarginLayoutParams) lp);
+            }
         }
+        return new LayoutParams(lp);
     }
 
     @Override
diff --git a/core/java/android/widget/GridLayout.java b/core/java/android/widget/GridLayout.java
index 726586e..af2852c 100644
--- a/core/java/android/widget/GridLayout.java
+++ b/core/java/android/widget/GridLayout.java
@@ -868,13 +868,14 @@
 
     @Override
     protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
-        if (lp instanceof LayoutParams) {
-            return new LayoutParams((LayoutParams) lp);
-        } else if (lp instanceof MarginLayoutParams) {
-            return new LayoutParams((MarginLayoutParams) lp);
-        } else {
-            return new LayoutParams(lp);
+        if (sPreserveMarginParamsInLayoutParamConversion) {
+            if (lp instanceof LayoutParams) {
+                return new LayoutParams((LayoutParams) lp);
+            } else if (lp instanceof MarginLayoutParams) {
+                return new LayoutParams((MarginLayoutParams) lp);
+            }
         }
+        return new LayoutParams(lp);
     }
 
     // Draw grid
diff --git a/core/java/android/widget/HorizontalScrollView.java b/core/java/android/widget/HorizontalScrollView.java
index 00f368e..918b6c0 100644
--- a/core/java/android/widget/HorizontalScrollView.java
+++ b/core/java/android/widget/HorizontalScrollView.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.content.Context;
+import android.content.res.Configuration;
 import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.Rect;
@@ -128,6 +129,8 @@
     private int mOverscrollDistance;
     private int mOverflingDistance;
 
+    private float mScrollFactor;
+
     /**
      * ID of the active pointer. This is used to retain consistency during
      * drags/flings if multiple pointers are used.
@@ -165,6 +168,10 @@
         setFillViewport(a.getBoolean(android.R.styleable.HorizontalScrollView_fillViewport, false));
 
         a.recycle();
+
+        if (context.getResources().getConfiguration().uiMode == Configuration.UI_MODE_TYPE_WATCH) {
+            setRevealOnFocusHint(false);
+        }
     }
 
     @Override
@@ -217,6 +224,7 @@
         mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
         mOverscrollDistance = configuration.getScaledOverscrollDistance();
         mOverflingDistance = configuration.getScaledOverflingDistance();
+        mScrollFactor = configuration.getScaledScrollFactor();
     }
 
     @Override
@@ -719,30 +727,35 @@
 
     @Override
     public boolean onGenericMotionEvent(MotionEvent event) {
-        if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
-            switch (event.getAction()) {
-                case MotionEvent.ACTION_SCROLL: {
-                    if (!mIsBeingDragged) {
-                        final float hscroll;
+        switch (event.getAction()) {
+            case MotionEvent.ACTION_SCROLL: {
+                if (!mIsBeingDragged) {
+                    final float axisValue;
+                    if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) {
                         if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
-                            hscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
+                            axisValue = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
                         } else {
-                            hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
+                            axisValue = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
                         }
-                        if (hscroll != 0) {
-                            final int delta = (int) (hscroll * getHorizontalScrollFactor());
-                            final int range = getScrollRange();
-                            int oldScrollX = mScrollX;
-                            int newScrollX = oldScrollX + delta;
-                            if (newScrollX < 0) {
-                                newScrollX = 0;
-                            } else if (newScrollX > range) {
-                                newScrollX = range;
-                            }
-                            if (newScrollX != oldScrollX) {
-                                super.scrollTo(newScrollX, mScrollY);
-                                return true;
-                            }
+                    } else if (event.isFromSource(InputDevice.SOURCE_ROTARY_ENCODER)) {
+                        axisValue = event.getAxisValue(MotionEvent.AXIS_SCROLL);
+                    } else {
+                        axisValue = 0;
+                    }
+
+                    final int delta = Math.round(axisValue * mScrollFactor);
+                    if (delta != 0) {
+                        final int range = getScrollRange();
+                        int oldScrollX = mScrollX;
+                        int newScrollX = oldScrollX + delta;
+                        if (newScrollX < 0) {
+                            newScrollX = 0;
+                        } else if (newScrollX > range) {
+                            newScrollX = range;
+                        }
+                        if (newScrollX != oldScrollX) {
+                            super.scrollTo(newScrollX, mScrollY);
+                            return true;
                         }
                     }
                 }
@@ -1430,11 +1443,13 @@
 
     @Override
     public void requestChildFocus(View child, View focused) {
-        if (!mIsLayoutDirty) {
-            scrollToChild(focused);
-        } else {
-            // The child may not be laid out yet, we can't compute the scroll yet
-            mChildToScrollTo = focused;
+        if (focused.getRevealOnFocusHint()) {
+            if (!mIsLayoutDirty) {
+                scrollToChild(focused);
+            } else {
+                // The child may not be laid out yet, we can't compute the scroll yet
+                mChildToScrollTo = focused;
+            }
         }
         super.requestChildFocus(child, focused);
     }
diff --git a/core/java/android/widget/ImageView.java b/core/java/android/widget/ImageView.java
index ddebaea..d9cb269 100644
--- a/core/java/android/widget/ImageView.java
+++ b/core/java/android/widget/ImageView.java
@@ -22,7 +22,6 @@
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.res.ColorStateList;
-import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
@@ -115,11 +114,17 @@
     private int mBaseline = -1;
     private boolean mBaselineAlignBottom = false;
 
-    // AdjustViewBounds behavior will be in compatibility mode for older apps.
-    private boolean mAdjustViewBoundsCompat = false;
+    /** Compatibility modes dependent on targetSdkVersion of the app. */
+    private static boolean sCompatDone;
+
+    /** AdjustViewBounds behavior will be in compatibility mode for older apps. */
+    private static boolean sCompatAdjustViewBounds;
 
     /** Whether to pass Resources when creating the source from a stream. */
-    private boolean mUseCorrectStreamDensity;
+    private static boolean sCompatUseCorrectStreamDensity;
+
+    /** Whether to use pre-Nougat drawable visibility dispatching conditions. */
+    private static boolean sCompatDrawableVisibilityDispatch;
 
     private static final ScaleType[] sScaleTypeArray = {
         ScaleType.MATRIX,
@@ -206,9 +211,13 @@
         mMatrix = new Matrix();
         mScaleType = ScaleType.FIT_CENTER;
 
-        final int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
-        mAdjustViewBoundsCompat = targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR1;
-        mUseCorrectStreamDensity = targetSdkVersion > Build.VERSION_CODES.M;
+        if (!sCompatDone) {
+            final int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
+            sCompatAdjustViewBounds = targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR1;
+            sCompatUseCorrectStreamDensity = targetSdkVersion > Build.VERSION_CODES.M;
+            sCompatDrawableVisibilityDispatch = targetSdkVersion < Build.VERSION_CODES.N;
+            sCompatDone = true;
+        }
     }
 
     @Override
@@ -545,6 +554,13 @@
      * Subsequent calls to {@link #setImageDrawable(Drawable)} will automatically
      * mutate the drawable and apply the specified tint and tint mode using
      * {@link Drawable#setTintList(ColorStateList)}.
+     * <p>
+     * <em>Note:</em> The default tint mode used by this setter is NOT
+     * consistent with the default tint mode used by the
+     * {@link android.R.styleable#ImageView_tint android:tint}
+     * attribute. If the {@code android:tint} attribute is specified, the
+     * default tint mode will be set to {@link PorterDuff.Mode#SRC_ATOP} to
+     * ensure consistency with earlier versions of the platform.
      *
      * @param tint the tint to apply, may be {@code null} to clear tint
      *
@@ -874,8 +890,8 @@
             InputStream stream = null;
             try {
                 stream = mContext.getContentResolver().openInputStream(uri);
-                return Drawable.createFromResourceStream(
-                        mUseCorrectStreamDensity ? getResources() : null, null, stream, null);
+                return Drawable.createFromResourceStream(sCompatUseCorrectStreamDensity
+                        ? getResources() : null, null, stream, null);
             } catch (Exception e) {
                 Log.w(LOG_TAG, "Unable to open content: " + uri, e);
             } finally {
@@ -910,10 +926,13 @@
             mRecycleableBitmapDrawable.setBitmap(null);
         }
 
+        boolean sameDrawable = false;
+
         if (mDrawable != null) {
+            sameDrawable = mDrawable == d;
             mDrawable.setCallback(null);
             unscheduleDrawable(mDrawable);
-            if (isAttachedToWindow()) {
+            if (!sCompatDrawableVisibilityDispatch && !sameDrawable && isAttachedToWindow()) {
                 mDrawable.setVisible(false, false);
             }
         }
@@ -926,8 +945,11 @@
             if (d.isStateful()) {
                 d.setState(getDrawableState());
             }
-            if (isAttachedToWindow()) {
-                d.setVisible(getWindowVisibility() == VISIBLE && isShown(), true);
+            if (!sameDrawable || sCompatDrawableVisibilityDispatch) {
+                final boolean visible = sCompatDrawableVisibilityDispatch
+                        ? getVisibility() == VISIBLE
+                        : isAttachedToWindow() && getWindowVisibility() == VISIBLE && isShown();
+                d.setVisible(visible, true);
             }
             d.setLevel(mLevel);
             mDrawableWidth = d.getIntrinsicWidth();
@@ -1051,7 +1073,7 @@
                                 pleft + pright;
 
                         // Allow the width to outgrow its original estimate if height is fixed.
-                        if (!resizeHeight && !mAdjustViewBoundsCompat) {
+                        if (!resizeHeight && !sCompatAdjustViewBounds) {
                             widthSize = resolveAdjustedSize(newWidth, mMaxWidth, widthMeasureSpec);
                         }
 
@@ -1067,7 +1089,7 @@
                                 ptop + pbottom;
 
                         // Allow the height to outgrow its original estimate if width is fixed.
-                        if (!resizeWidth && !mAdjustViewBoundsCompat) {
+                        if (!resizeWidth && !sCompatAdjustViewBounds) {
                             heightSize = resolveAdjustedSize(newHeight, mMaxHeight,
                                     heightMeasureSpec);
                         }
@@ -1509,11 +1531,40 @@
     @Override
     public void onVisibilityAggregated(boolean isVisible) {
         super.onVisibilityAggregated(isVisible);
-        if (mDrawable != null) {
+        // Only do this for new apps post-Nougat
+        if (mDrawable != null && !sCompatDrawableVisibilityDispatch) {
             mDrawable.setVisible(isVisible, false);
         }
     }
 
+    @RemotableViewMethod
+    @Override
+    public void setVisibility(int visibility) {
+        super.setVisibility(visibility);
+        // Only do this for old apps pre-Nougat; new apps use onVisibilityAggregated
+        if (mDrawable != null && sCompatDrawableVisibilityDispatch) {
+            mDrawable.setVisible(visibility == VISIBLE, false);
+        }
+    }
+
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        // Only do this for old apps pre-Nougat; new apps use onVisibilityAggregated
+        if (mDrawable != null && sCompatDrawableVisibilityDispatch) {
+            mDrawable.setVisible(getVisibility() == VISIBLE, false);
+        }
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        // Only do this for old apps pre-Nougat; new apps use onVisibilityAggregated
+        if (mDrawable != null && sCompatDrawableVisibilityDispatch) {
+            mDrawable.setVisible(false, false);
+        }
+    }
+
     @Override
     public CharSequence getAccessibilityClassName() {
         return ImageView.class.getName();
diff --git a/core/java/android/widget/LinearLayout.java b/core/java/android/widget/LinearLayout.java
index 38d7cd4..f897372 100644
--- a/core/java/android/widget/LinearLayout.java
+++ b/core/java/android/widget/LinearLayout.java
@@ -1844,13 +1844,14 @@
 
     @Override
     protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
-        if (lp instanceof LayoutParams) {
-            return new LayoutParams((LayoutParams) lp);
-        } else if (lp instanceof MarginLayoutParams) {
-            return new LayoutParams((MarginLayoutParams) lp);
-        } else {
-            return new LayoutParams(lp);
+        if (sPreserveMarginParamsInLayoutParamConversion) {
+            if (lp instanceof LayoutParams) {
+                return new LayoutParams((LayoutParams) lp);
+            } else if (lp instanceof MarginLayoutParams) {
+                return new LayoutParams((MarginLayoutParams) lp);
+            }
         }
+        return new LayoutParams(lp);
     }
 
 
diff --git a/core/java/android/widget/ListView.java b/core/java/android/widget/ListView.java
index b8b7c55..b0f19d7 100644
--- a/core/java/android/widget/ListView.java
+++ b/core/java/android/widget/ListView.java
@@ -112,8 +112,8 @@
         public boolean isSelectable;
     }
 
-    private ArrayList<FixedViewInfo> mHeaderViewInfos = Lists.newArrayList();
-    private ArrayList<FixedViewInfo> mFooterViewInfos = Lists.newArrayList();
+    ArrayList<FixedViewInfo> mHeaderViewInfos = Lists.newArrayList();
+    ArrayList<FixedViewInfo> mFooterViewInfos = Lists.newArrayList();
 
     Drawable mDivider;
     int mDividerHeight;
@@ -279,7 +279,7 @@
         // Wrap the adapter if it wasn't already wrapped.
         if (mAdapter != null) {
             if (!(mAdapter instanceof HeaderViewListAdapter)) {
-                mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, mAdapter);
+                wrapHeaderListAdapterInternal();
             }
 
             // In the case of re-adding a header view, or adding one later on,
@@ -373,7 +373,7 @@
         // Wrap the adapter if it wasn't already wrapped.
         if (mAdapter != null) {
             if (!(mAdapter instanceof HeaderViewListAdapter)) {
-                mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, mAdapter);
+                wrapHeaderListAdapterInternal();
             }
 
             // In the case of re-adding a footer view, or adding one later on,
@@ -476,7 +476,7 @@
         mRecycler.clear();
 
         if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) {
-            mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter);
+            mAdapter = wrapHeaderListAdapterInternal(mHeaderViewInfos, mFooterViewInfos, adapter);
         } else {
             mAdapter = adapter;
         }
@@ -2050,6 +2050,8 @@
                 p.recycledHeaderFooter = true;
             }
             addViewInLayout(child, flowDown ? -1 : 0, p, true);
+            // add view in layout will reset the RTL properties. We have to re-resolve them
+            child.resolveRtlPropertiesIfNeeded();
         }
 
         if (needToMeasure) {
@@ -2226,7 +2228,7 @@
      * after the header views.
      */
     public void setSelectionAfterHeaderView() {
-        final int count = mHeaderViewInfos.size();
+        final int count = getHeaderViewsCount();
         if (count > 0) {
             mNextSelectedPosition = 0;
             return;
@@ -3354,7 +3356,7 @@
             bounds.right = mRight - mLeft - mPaddingRight;
 
             final int count = getChildCount();
-            final int headerCount = mHeaderViewInfos.size();
+            final int headerCount = getHeaderViewsCount();
             final int itemCount = mItemCount;
             final int footerLimit = (itemCount - mFooterViewInfos.size());
             final boolean headerDividers = mHeaderDividersEnabled;
@@ -3938,7 +3940,7 @@
         if (drawDividers) {
             final boolean fillForMissingDividers = isOpaque() && !super.isOpaque();
             final int itemCount = mItemCount;
-            final int headerCount = mHeaderViewInfos.size();
+            final int headerCount = getHeaderViewsCount();
             final int footerLimit = (itemCount - mFooterViewInfos.size());
             final boolean isHeader = (itemIndex < headerCount);
             final boolean isFooter = (itemIndex >= footerLimit);
@@ -4050,4 +4052,24 @@
 
         encoder.addProperty("recycleOnMeasure", recycleOnMeasure());
     }
+
+    /** @hide */
+    protected HeaderViewListAdapter wrapHeaderListAdapterInternal(
+            ArrayList<ListView.FixedViewInfo> headerViewInfos,
+            ArrayList<ListView.FixedViewInfo> footerViewInfos,
+            ListAdapter adapter) {
+        return new HeaderViewListAdapter(headerViewInfos, footerViewInfos, adapter);
+    }
+
+    /** @hide */
+    protected void wrapHeaderListAdapterInternal() {
+        mAdapter = wrapHeaderListAdapterInternal(mHeaderViewInfos, mFooterViewInfos, mAdapter);
+    }
+
+    /** @hide */
+    protected void dispatchDataSetObserverOnChangedInternal() {
+        if (mDataSetObserver != null) {
+            mDataSetObserver.onChanged();
+        }
+    }
 }
diff --git a/core/java/android/widget/NumberPicker.java b/core/java/android/widget/NumberPicker.java
index c3ddec7..25580fd 100644
--- a/core/java/android/widget/NumberPicker.java
+++ b/core/java/android/widget/NumberPicker.java
@@ -16,6 +16,8 @@
 
 package android.widget;
 
+import com.android.internal.R;
+
 import android.annotation.CallSuper;
 import android.annotation.IntDef;
 import android.annotation.Widget;
@@ -29,10 +31,12 @@
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
+import android.text.Editable;
 import android.text.InputFilter;
 import android.text.InputType;
 import android.text.Spanned;
 import android.text.TextUtils;
+import android.text.TextWatcher;
 import android.text.method.NumberKeyListener;
 import android.util.AttributeSet;
 import android.util.SparseArray;
@@ -52,9 +56,6 @@
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputMethodManager;
 
-import com.android.internal.R;
-import libcore.icu.LocaleData;
-
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
@@ -62,6 +63,8 @@
 import java.util.List;
 import java.util.Locale;
 
+import libcore.icu.LocaleData;
+
 /**
  * A widget that enables the user to select a number from a predefined range.
  * There are two flavors of this widget and which one is presented to the user
@@ -1991,7 +1994,7 @@
             removeCallbacks(mChangeCurrentByOneFromLongPressCommand);
         }
         if (mSetSelectionCommand != null) {
-            removeCallbacks(mSetSelectionCommand);
+            mSetSelectionCommand.cancel();
         }
         if (mBeginSoftInputOnLongPressCommand != null) {
             removeCallbacks(mBeginSoftInputOnLongPressCommand);
@@ -2033,18 +2036,14 @@
     }
 
     /**
-     * Posts an {@link SetSelectionCommand} from the given <code>selectionStart
-     * </code> to <code>selectionEnd</code>.
+     * Posts a {@link SetSelectionCommand} from the given
+     * {@code selectionStart} to {@code selectionEnd}.
      */
     private void postSetSelectionCommand(int selectionStart, int selectionEnd) {
         if (mSetSelectionCommand == null) {
-            mSetSelectionCommand = new SetSelectionCommand();
-        } else {
-            removeCallbacks(mSetSelectionCommand);
+            mSetSelectionCommand = new SetSelectionCommand(mInputText);
         }
-        mSetSelectionCommand.mSelectionStart = selectionStart;
-        mSetSelectionCommand.mSelectionEnd = selectionEnd;
-        post(mSetSelectionCommand);
+        mSetSelectionCommand.post(selectionStart, selectionEnd);
     }
 
     /**
@@ -2090,6 +2089,12 @@
         @Override
         public CharSequence filter(
                 CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
+            // We don't know what the output will be, so always cancel any
+            // pending set selection command.
+            if (mSetSelectionCommand != null) {
+                mSetSelectionCommand.cancel();
+            }
+
             if (mDisplayedValues == null) {
                 CharSequence filtered = super.filter(source, start, end, dest, dstart, dend);
                 if (filtered == null) {
@@ -2237,12 +2242,39 @@
     /**
      * Command for setting the input text selection.
      */
-    class SetSelectionCommand implements Runnable {
-        private int mSelectionStart;
+    private static class SetSelectionCommand implements Runnable {
+        private final EditText mInputText;
 
+        private int mSelectionStart;
         private int mSelectionEnd;
 
+        /** Whether this runnable is currently posted. */
+        private boolean mPosted;
+
+        public SetSelectionCommand(EditText inputText) {
+            mInputText = inputText;
+        }
+
+        public void post(int selectionStart, int selectionEnd) {
+            mSelectionStart = selectionStart;
+            mSelectionEnd = selectionEnd;
+
+            if (!mPosted) {
+                mInputText.post(this);
+                mPosted = true;
+            }
+        }
+
+        public void cancel() {
+            if (mPosted) {
+                mInputText.removeCallbacks(this);
+                mPosted = false;
+            }
+        }
+
+        @Override
         public void run() {
+            mPosted = false;
             mInputText.setSelection(mSelectionStart, mSelectionEnd);
         }
     }
diff --git a/core/java/android/widget/PopupWindow.java b/core/java/android/widget/PopupWindow.java
index 6432f70..9b89491 100644
--- a/core/java/android/widget/PopupWindow.java
+++ b/core/java/android/widget/PopupWindow.java
@@ -193,6 +193,8 @@
 
     private int mAnimationStyle = ANIMATION_STYLE_DEFAULT;
 
+    private int mGravity = Gravity.NO_GRAVITY;
+
     private static final int[] ABOVE_ANCHOR_STATE_SET = new int[] {
         com.android.internal.R.attr.state_above_anchor
     };
@@ -1141,15 +1143,11 @@
 
         mIsShowing = true;
         mIsDropdown = false;
+        mGravity = gravity;
 
         final WindowManager.LayoutParams p = createPopupLayoutParams(token);
         preparePopup(p);
 
-        // Only override the default if some gravity was specified.
-        if (gravity != Gravity.NO_GRAVITY) {
-            p.gravity = gravity;
-        }
-
         p.x = x;
         p.y = y;
 
@@ -1394,8 +1392,8 @@
     }
 
     private int computeGravity() {
-        int gravity = Gravity.START | Gravity.TOP;
-        if (mClipToScreen || mClippingEnabled) {
+        int gravity = mGravity == Gravity.NO_GRAVITY ?  Gravity.START | Gravity.TOP : mGravity;
+        if (mIsDropdown && (mClipToScreen || mClippingEnabled)) {
             gravity |= Gravity.DISPLAY_CLIP_VERTICAL;
         }
         return gravity;
@@ -1547,7 +1545,7 @@
         }
 
         // Let the window manager know to align the top to y.
-        outParams.gravity = Gravity.LEFT | Gravity.TOP;
+        outParams.gravity = computeGravity();
         outParams.width = width;
         outParams.height = height;
 
@@ -1760,11 +1758,22 @@
      */
     public int getMaxAvailableHeight(
             @NonNull View anchor, int yOffset, boolean ignoreBottomDecorations) {
-        final Rect displayFrame = new Rect();
+        Rect displayFrame = null;
+        final Rect visibleDisplayFrame = new Rect();
+
+        anchor.getWindowVisibleDisplayFrame(visibleDisplayFrame);
         if (ignoreBottomDecorations) {
+            // In the ignore bottom decorations case we want to
+            // still respect all other decorations so we use the inset visible
+            // frame on the top right and left and take the bottom
+            // value from the full frame.
+            displayFrame = new Rect();
             anchor.getWindowDisplayFrame(displayFrame);
+            displayFrame.top = visibleDisplayFrame.top;
+            displayFrame.right = visibleDisplayFrame.right;
+            displayFrame.left = visibleDisplayFrame.left;
         } else {
-            anchor.getWindowVisibleDisplayFrame(displayFrame);
+            displayFrame = visibleDisplayFrame;
         }
 
         final int[] anchorPos = mTmpDrawingLocation;
diff --git a/core/java/android/widget/ProgressBar.java b/core/java/android/widget/ProgressBar.java
index 3c967ac..1f379c9 100644
--- a/core/java/android/widget/ProgressBar.java
+++ b/core/java/android/widget/ProgressBar.java
@@ -22,6 +22,7 @@
 import android.annotation.Nullable;
 import android.content.Context;
 import android.content.res.ColorStateList;
+import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
@@ -230,7 +231,7 @@
     private Drawable mCurrentDrawable;
     private ProgressTintInfo mProgressTintInfo;
 
-    Bitmap mSampleTile;
+    int mSampleWidth = 0;
     private boolean mNoInvalidate;
     private Interpolator mInterpolator;
     private RefreshProgressRunnable mRefreshProgressRunnable;
@@ -505,15 +506,14 @@
         }
 
         if (drawable instanceof BitmapDrawable) {
-            final BitmapDrawable bitmap = (BitmapDrawable) drawable;
-            final Bitmap tileBitmap = bitmap.getBitmap();
-            if (mSampleTile == null) {
-                mSampleTile = tileBitmap;
-            }
-
-            final BitmapDrawable clone = (BitmapDrawable) bitmap.getConstantState().newDrawable();
+            final Drawable.ConstantState cs = drawable.getConstantState();
+            final BitmapDrawable clone = (BitmapDrawable) cs.newDrawable(getResources());
             clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
 
+            if (mSampleWidth <= 0) {
+                mSampleWidth = clone.getIntrinsicWidth();
+            }
+
             if (clip) {
                 return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL);
             } else {
diff --git a/core/java/android/widget/RadialTimePickerView.java b/core/java/android/widget/RadialTimePickerView.java
index 02ee2df..6f198e7 100644
--- a/core/java/android/widget/RadialTimePickerView.java
+++ b/core/java/android/widget/RadialTimePickerView.java
@@ -16,7 +16,11 @@
 
 package android.widget;
 
+import com.android.internal.R;
+import com.android.internal.widget.ExploreByTouchHelper;
+
 import android.animation.ObjectAnimator;
+import android.annotation.IntDef;
 import android.content.Context;
 import android.content.res.ColorStateList;
 import android.content.res.Resources;
@@ -43,9 +47,8 @@
 import android.view.accessibility.AccessibilityNodeInfo;
 import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
 
-import com.android.internal.R;
-import com.android.internal.widget.ExploreByTouchHelper;
-
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.util.Calendar;
 import java.util.Locale;
 
@@ -55,11 +58,16 @@
  * @hide
  */
 public class RadialTimePickerView extends View {
-
     private static final String TAG = "RadialTimePickerView";
 
     public static final int HOURS = 0;
     public static final int MINUTES = 1;
+
+    /** @hide */
+    @IntDef({HOURS, MINUTES})
+    @Retention(RetentionPolicy.SOURCE)
+    @interface PickerType {}
+
     private static final int HOURS_INNER = 2;
 
     private static final int SELECTOR_CIRCLE = 0;
@@ -185,8 +193,24 @@
 
     private boolean mInputEnabled = true;
 
-    public interface OnValueSelectedListener {
-        void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance);
+    interface OnValueSelectedListener {
+        /**
+         * Called when the selected value at a given picker index has changed.
+         *
+         * @param pickerType the type of value that has changed, one of:
+         *                   <ul>
+         *                       <li>{@link #MINUTES}
+         *                       <li>{@link #HOURS}
+         *                   </ul>
+         * @param newValue the new value as minute in hour (0-59) or hour in
+         *                 day (0-23)
+         * @param autoAdvance when the picker type is {@link #HOURS},
+         *                    {@code true} to switch to the {@link #MINUTES}
+         *                    picker or {@code false} to stay on the current
+         *                    picker. No effect when picker type is
+         *                    {@link #MINUTES}.
+         */
+        void onValueSelected(@PickerType int pickerType, int newValue, boolean autoAdvance);
     }
 
     /**
@@ -977,7 +1001,7 @@
         // Ensure we're showing the correct picker.
         animatePicker(mShowHours, ANIM_DURATION_TOUCH);
 
-        final int type;
+        final @PickerType int type;
         final int newValue;
         final boolean valueChanged;
 
diff --git a/core/java/android/widget/RatingBar.java b/core/java/android/widget/RatingBar.java
index 2230961..62dd90f 100644
--- a/core/java/android/widget/RatingBar.java
+++ b/core/java/android/widget/RatingBar.java
@@ -110,8 +110,8 @@
         }
 
         // A touch inside a star fill up to that fractional area (slightly more
-        // than 1 so boundaries round up).
-        mTouchProgressOffset = 1.1f;
+        // than 0.5 so boundaries round up).
+        mTouchProgressOffset = 0.6f;
     }
 
     public RatingBar(Context context, AttributeSet attrs) {
@@ -281,10 +281,8 @@
     protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
-        if (mSampleTile != null) {
-            // TODO: Once ProgressBar's TODOs are gone, this can be done more
-            // cleanly than mSampleTile
-            final int width = mSampleTile.getWidth() * mNumStars;
+        if (mSampleWidth > 0) {
+            final int width = mSampleWidth * mNumStars;
             setMeasuredDimension(resolveSizeAndState(width, widthMeasureSpec, 0),
                     getMeasuredHeight());
         }
diff --git a/core/java/android/widget/RelativeLayout.java b/core/java/android/widget/RelativeLayout.java
index 0136542..a189d3c 100644
--- a/core/java/android/widget/RelativeLayout.java
+++ b/core/java/android/widget/RelativeLayout.java
@@ -1104,13 +1104,14 @@
 
     @Override
     protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
-        if (lp instanceof LayoutParams) {
-            return new LayoutParams((LayoutParams) lp);
-        } else if (lp instanceof MarginLayoutParams) {
-            return new LayoutParams((MarginLayoutParams) lp);
-        } else {
-            return new LayoutParams(lp);
+        if (sPreserveMarginParamsInLayoutParamConversion) {
+            if (lp instanceof LayoutParams) {
+                return new LayoutParams((LayoutParams) lp);
+            } else if (lp instanceof MarginLayoutParams) {
+                return new LayoutParams((MarginLayoutParams) lp);
+            }
         }
+        return new LayoutParams(lp);
     }
 
     /** @hide */
diff --git a/core/java/android/widget/RemoteViews.java b/core/java/android/widget/RemoteViews.java
index 1f745185..d0d233e 100644
--- a/core/java/android/widget/RemoteViews.java
+++ b/core/java/android/widget/RemoteViews.java
@@ -2788,6 +2788,18 @@
     }
 
     /**
+     * @hide
+     * Equivalent to calling {@link android.widget.TextView#setTextColor(ColorStateList)}.
+     *
+     * @param viewId The id of the view whose text color should change
+     * @param colors the text colors to set
+     */
+    public void setTextColor(int viewId, @ColorInt ColorStateList colors) {
+        addAction(new ReflectionAction(viewId, "setTextColor", ReflectionAction.COLOR_STATE_LIST,
+                colors));
+    }
+
+    /**
      * Equivalent to calling {@link android.widget.AbsListView#setRemoteViewsAdapter(Intent)}.
      *
      * @param appWidgetId The id of the app widget which contains the specified view. (This
@@ -3195,7 +3207,9 @@
         // we don't add a filter to the static version returned by getSystemService.
         inflater = inflater.cloneInContext(inflationContext);
         inflater.setFilter(this);
-        return inflater.inflate(rv.getLayoutId(), parent, false);
+        View v = inflater.inflate(rv.getLayoutId(), parent, false);
+        v.setTagInternal(R.id.widget_frame, rv.getLayoutId());
+        return v;
     }
 
     private static void loadTransitionOverride(Context context,
@@ -3373,7 +3387,7 @@
         // across orientation change, and has the RemoteViews re-applied in the new orientation,
         // we throw an exception, since the layouts may be completely unrelated.
         if (hasLandscapeAndPortraitLayouts()) {
-            if (v.getId() != rvToApply.getLayoutId()) {
+            if ((Integer) v.getTag(R.id.widget_frame) != rvToApply.getLayoutId()) {
                 throw new RuntimeException("Attempting to re-apply RemoteViews to a view that" +
                         " that does not share the same root layout id.");
             }
@@ -3409,7 +3423,7 @@
         // across orientation change, and has the RemoteViews re-applied in the new orientation,
         // we throw an exception, since the layouts may be completely unrelated.
         if (hasLandscapeAndPortraitLayouts()) {
-            if (v.getId() != rvToApply.getLayoutId()) {
+            if ((Integer) v.getTag(R.id.widget_frame) != rvToApply.getLayoutId()) {
                 throw new RuntimeException("Attempting to re-apply RemoteViews to a view that" +
                         " that does not share the same root layout id.");
             }
diff --git a/core/java/android/widget/ScrollView.java b/core/java/android/widget/ScrollView.java
index 0555cd4..e696ff7 100644
--- a/core/java/android/widget/ScrollView.java
+++ b/core/java/android/widget/ScrollView.java
@@ -17,6 +17,7 @@
 package android.widget;
 
 import android.annotation.NonNull;
+import android.content.res.Configuration;
 import android.os.Build;
 import android.os.Build.VERSION_CODES;
 import android.os.Parcel;
@@ -134,6 +135,8 @@
     private int mOverscrollDistance;
     private int mOverflingDistance;
 
+    private int mScrollFactor;
+
     /**
      * ID of the active pointer. This is used to retain consistency during
      * drags/flings if multiple pointers are used.
@@ -186,6 +189,10 @@
         setFillViewport(a.getBoolean(R.styleable.ScrollView_fillViewport, false));
 
         a.recycle();
+
+        if (context.getResources().getConfiguration().uiMode == Configuration.UI_MODE_TYPE_WATCH) {
+            setRevealOnFocusHint(false);
+        }
     }
 
     @Override
@@ -243,6 +250,7 @@
         mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
         mOverscrollDistance = configuration.getScaledOverscrollDistance();
         mOverflingDistance = configuration.getScaledOverflingDistance();
+        mScrollFactor = configuration.getScaledScrollFactor();
     }
 
     @Override
@@ -777,30 +785,35 @@
 
     @Override
     public boolean onGenericMotionEvent(MotionEvent event) {
-        if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
-            switch (event.getAction()) {
-                case MotionEvent.ACTION_SCROLL: {
-                    if (!mIsBeingDragged) {
-                        final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
-                        if (vscroll != 0) {
-                            final int delta = (int) (vscroll * getVerticalScrollFactor());
-                            final int range = getScrollRange();
-                            int oldScrollY = mScrollY;
-                            int newScrollY = oldScrollY - delta;
-                            if (newScrollY < 0) {
-                                newScrollY = 0;
-                            } else if (newScrollY > range) {
-                                newScrollY = range;
-                            }
-                            if (newScrollY != oldScrollY) {
-                                super.scrollTo(mScrollX, newScrollY);
-                                return true;
-                            }
-                        }
+        switch (event.getAction()) {
+            case MotionEvent.ACTION_SCROLL:
+                final float axisValue;
+                if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) {
+                    axisValue = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
+                } else if (event.isFromSource(InputDevice.SOURCE_ROTARY_ENCODER)) {
+                    axisValue = event.getAxisValue(MotionEvent.AXIS_SCROLL);
+                } else {
+                    axisValue = 0;
+                }
+
+                final int delta = Math.round(axisValue * mScrollFactor);
+                if (delta != 0) {
+                    final int range = getScrollRange();
+                    int oldScrollY = mScrollY;
+                    int newScrollY = oldScrollY - delta;
+                    if (newScrollY < 0) {
+                        newScrollY = 0;
+                    } else if (newScrollY > range) {
+                        newScrollY = range;
+                    }
+                    if (newScrollY != oldScrollY) {
+                        super.scrollTo(mScrollX, newScrollY);
+                        return true;
                     }
                 }
-            }
+                break;
         }
+
         return super.onGenericMotionEvent(event);
     }
 
@@ -1455,11 +1468,13 @@
 
     @Override
     public void requestChildFocus(View child, View focused) {
-        if (!mIsLayoutDirty) {
-            scrollToChild(focused);
-        } else {
-            // The child may not be laid out yet, we can't compute the scroll yet
-            mChildToScrollTo = focused;
+        if (focused.getRevealOnFocusHint()) {
+            if (!mIsLayoutDirty) {
+                scrollToChild(focused);
+            } else {
+                // The child may not be laid out yet, we can't compute the scroll yet
+                mChildToScrollTo = focused;
+            }
         }
         super.requestChildFocus(child, focused);
     }
diff --git a/core/java/android/widget/SearchView.java b/core/java/android/widget/SearchView.java
index 5878cad..9139361 100644
--- a/core/java/android/widget/SearchView.java
+++ b/core/java/android/widget/SearchView.java
@@ -816,9 +816,11 @@
 
         switch (heightMode) {
             case MeasureSpec.AT_MOST:
-            case MeasureSpec.UNSPECIFIED:
                 height = Math.min(getPreferredHeight(), height);
                 break;
+            case MeasureSpec.UNSPECIFIED:
+                height = getPreferredHeight();
+                break;
         }
         heightMode = MeasureSpec.EXACTLY;
 
diff --git a/core/java/android/widget/TextClock.java b/core/java/android/widget/TextClock.java
index a585d75..49380b7 100644
--- a/core/java/android/widget/TextClock.java
+++ b/core/java/android/widget/TextClock.java
@@ -130,7 +130,7 @@
 
     private CharSequence mDescFormat;
 
-    private boolean mAttached;
+    private boolean mRegistered;
 
     private Calendar mTime;
     private String mTimeZone;
@@ -250,7 +250,7 @@
         }
 
         createTime(mTimeZone);
-        // Wait until onAttachedToWindow() to handle the ticker
+        // Wait until registering for events to handle the ticker
         chooseFormat(false);
     }
 
@@ -501,7 +501,7 @@
         boolean hadSeconds = mHasSeconds;
         mHasSeconds = DateFormat.hasSeconds(mFormat);
 
-        if (handleTicker && mAttached && hadSeconds != mHasSeconds) {
+        if (handleTicker && mRegistered && hadSeconds != mHasSeconds) {
             if (hadSeconds) getHandler().removeCallbacks(mTicker);
             else mTicker.run();
         }
@@ -515,11 +515,9 @@
     }
 
     @Override
-    protected void onAttachedToWindow() {
-        super.onAttachedToWindow();
-
-        if (!mAttached) {
-            mAttached = true;
+    public void onVisibilityAggregated(boolean isVisible) {
+        if (!mRegistered && isVisible) {
+            mRegistered = true;
 
             registerReceiver();
             registerObserver();
@@ -531,20 +529,13 @@
             } else {
                 onTimeChanged();
             }
-        }
-    }
-
-    @Override
-    protected void onDetachedFromWindow() {
-        super.onDetachedFromWindow();
-
-        if (mAttached) {
+        } else if (mRegistered && !isVisible) {
             unregisterReceiver();
             unregisterObserver();
 
             getHandler().removeCallbacks(mTicker);
 
-            mAttached = false;
+            mRegistered = false;
         }
     }
 
@@ -567,7 +558,7 @@
     }
 
     private void registerObserver() {
-        if (isAttachedToWindow()) {
+        if (mRegistered) {
             if (mFormatChangeObserver == null) {
                 mFormatChangeObserver = new FormatChangeObserver(getHandler());
             }
diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java
index 6faa2eb9..f8daa88 100644
--- a/core/java/android/widget/TextView.java
+++ b/core/java/android/widget/TextView.java
@@ -3259,6 +3259,7 @@
      *
      * @attr ref android.R.styleable#TextView_textColor
      */
+    @android.view.RemotableViewMethod
     public void setTextColor(ColorStateList colors) {
         if (colors == null) {
             throw new NullPointerException();
diff --git a/core/java/android/widget/TimePicker.java b/core/java/android/widget/TimePicker.java
index f2fc617..a1c854b 100644
--- a/core/java/android/widget/TimePicker.java
+++ b/core/java/android/widget/TimePicker.java
@@ -16,20 +16,24 @@
 
 package android.widget;
 
+import com.android.internal.R;
+
+import android.annotation.IntDef;
+import android.annotation.IntRange;
 import android.annotation.NonNull;
-import android.annotation.Nullable;
+import android.annotation.TestApi;
 import android.annotation.Widget;
 import android.content.Context;
-import android.content.res.Configuration;
 import android.content.res.TypedArray;
 import android.os.Parcel;
 import android.os.Parcelable;
-import android.os.Parcelable.Creator;
 import android.util.AttributeSet;
+import android.util.MathUtils;
 import android.view.View;
 import android.view.accessibility.AccessibilityEvent;
-import com.android.internal.R;
 
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.util.Locale;
 
 import libcore.icu.LocaleData;
@@ -45,11 +49,36 @@
  */
 @Widget
 public class TimePicker extends FrameLayout {
-    private static final int MODE_SPINNER = 1;
-    private static final int MODE_CLOCK = 2;
+    /**
+     * Presentation mode for the Holo-style time picker that uses a set of
+     * {@link android.widget.NumberPicker}s.
+     *
+     * @see #getMode()
+     * @hide Visible for testing only.
+     */
+    @TestApi
+    public static final int MODE_SPINNER = 1;
+
+    /**
+     * Presentation mode for the Material-style time picker that uses a clock
+     * face.
+     *
+     * @see #getMode()
+     * @hide Visible for testing only.
+     */
+    @TestApi
+    public static final int MODE_CLOCK = 2;
+
+    /** @hide */
+    @IntDef({MODE_SPINNER, MODE_CLOCK})
+    @Retention(RetentionPolicy.SOURCE)
+    public @interface TimePickerMode {}
 
     private final TimePickerDelegate mDelegate;
 
+    @TimePickerMode
+    private final int mMode;
+
     /**
      * The callback interface used to indicate the time has been adjusted.
      */
@@ -80,10 +109,19 @@
 
         final TypedArray a = context.obtainStyledAttributes(
                 attrs, R.styleable.TimePicker, defStyleAttr, defStyleRes);
-        final int mode = a.getInt(R.styleable.TimePicker_timePickerMode, MODE_SPINNER);
+        final boolean isDialogMode = a.getBoolean(R.styleable.TimePicker_dialogMode, false);
+        final int requestedMode = a.getInt(R.styleable.TimePicker_timePickerMode, MODE_SPINNER);
         a.recycle();
 
-        switch (mode) {
+        if (requestedMode == MODE_CLOCK && isDialogMode) {
+            // You want MODE_CLOCK? YOU CAN'T HANDLE MODE_CLOCK! Well, maybe
+            // you can depending on your screen size. Let's check...
+            mMode = context.getResources().getInteger(R.integer.time_picker_mode);
+        } else {
+            mMode = requestedMode;
+        }
+
+        switch (mMode) {
             case MODE_CLOCK:
                 mDelegate = new TimePickerClockDelegate(
                         this, context, attrs, defStyleAttr, defStyleRes);
@@ -97,13 +135,25 @@
     }
 
     /**
+     * @return the picker's presentation mode, one of {@link #MODE_CLOCK} or
+     *         {@link #MODE_SPINNER}
+     * @attr ref android.R.styleable#TimePicker_timePickerMode
+     * @hide Visible for testing only.
+     */
+    @TimePickerMode
+    @TestApi
+    public int getMode() {
+        return mMode;
+    }
+
+    /**
      * Sets the currently selected hour using 24-hour time.
      *
      * @param hour the hour to set, in the range (0-23)
      * @see #getHour()
      */
-    public void setHour(int hour) {
-        mDelegate.setHour(hour);
+    public void setHour(@IntRange(from = 0, to = 23) int hour) {
+        mDelegate.setHour(MathUtils.constrain(hour, 0, 23));
     }
 
     /**
@@ -117,13 +167,13 @@
     }
 
     /**
-     * Sets the currently selected minute..
+     * Sets the currently selected minute.
      *
      * @param minute the minute to set, in the range (0-59)
      * @see #getMinute()
      */
-    public void setMinute(int minute) {
-        mDelegate.setMinute(minute);
+    public void setMinute(@IntRange(from = 0, to = 59) int minute) {
+        mDelegate.setMinute(MathUtils.constrain(minute, 0, 59));
     }
 
     /**
@@ -137,8 +187,9 @@
     }
 
     /**
-     * Sets the current hour.
+     * Sets the currently selected hour using 24-hour time.
      *
+     * @param currentHour the hour to set, in the range (0-23)
      * @deprecated Use {@link #setHour(int)}
      */
     @Deprecated
@@ -147,33 +198,34 @@
     }
 
     /**
-     * @return the current hour in the range (0-23)
+     * @return the currently selected hour, in the range (0-23)
      * @deprecated Use {@link #getHour()}
      */
     @NonNull
     @Deprecated
     public Integer getCurrentHour() {
-        return mDelegate.getHour();
+        return getHour();
     }
 
     /**
-     * Set the current minute (0-59).
+     * Sets the currently selected minute.
      *
+     * @param currentMinute the minute to set, in the range (0-59)
      * @deprecated Use {@link #setMinute(int)}
      */
     @Deprecated
     public void setCurrentMinute(@NonNull Integer currentMinute) {
-        mDelegate.setMinute(currentMinute);
+        setMinute(currentMinute);
     }
 
     /**
-     * @return the current minute
+     * @return the currently selected minute, in the range (0-59)
      * @deprecated Use {@link #getMinute()}
      */
     @NonNull
     @Deprecated
     public Integer getCurrentMinute() {
-        return mDelegate.getMinute();
+        return getMinute();
     }
 
     /**
@@ -256,10 +308,10 @@
      * for the real behavior.
      */
     interface TimePickerDelegate {
-        void setHour(int hour);
+        void setHour(@IntRange(from = 0, to = 23) int hour);
         int getHour();
 
-        void setMinute(int minute);
+        void setMinute(@IntRange(from = 0, to = 59) int minute);
         int getMinute();
 
         void setIs24Hour(boolean is24Hour);
diff --git a/core/java/android/widget/TimePickerClockDelegate.java b/core/java/android/widget/TimePickerClockDelegate.java
index c21f1df..aa0b93d 100644
--- a/core/java/android/widget/TimePickerClockDelegate.java
+++ b/core/java/android/widget/TimePickerClockDelegate.java
@@ -61,9 +61,6 @@
     private static final int HOUR_INDEX = RadialTimePickerView.HOURS;
     private static final int MINUTE_INDEX = RadialTimePickerView.MINUTES;
 
-    // NOT a real index for the purpose of what's showing.
-    private static final int AMPM_INDEX = 2;
-
     private static final int[] ATTRS_TEXT_COLOR = new int[] {R.attr.textColor};
     private static final int[] ATTRS_DISABLED_ALPHA = new int[] {R.attr.disabledAlpha};
 
@@ -701,22 +698,21 @@
     /** Listener for RadialTimePickerView interaction. */
     private final OnValueSelectedListener mOnValueSelectedListener = new OnValueSelectedListener() {
         @Override
-        public void onValueSelected(int pickerIndex, int newValue, boolean autoAdvance) {
-            switch (pickerIndex) {
-                case HOUR_INDEX:
+        public void onValueSelected(int pickerType, int newValue, boolean autoAdvance) {
+            switch (pickerType) {
+                case RadialTimePickerView.HOURS:
                     final boolean isTransition = mAllowAutoAdvance && autoAdvance;
                     setHourInternal(newValue, true, !isTransition);
                     if (isTransition) {
                         setCurrentItemShowing(MINUTE_INDEX, true, false);
-                        mDelegator.announceForAccessibility(newValue + ". " + mSelectMinutes);
+
+                        final int localizedHour = getLocalizedHour(newValue);
+                        mDelegator.announceForAccessibility(localizedHour + ". " + mSelectMinutes);
                     }
                     break;
-                case MINUTE_INDEX:
+                case RadialTimePickerView.MINUTES:
                     setMinuteInternal(newValue, true);
                     break;
-                case AMPM_INDEX:
-                    updateAmPmLabelStates(newValue);
-                    break;
             }
 
             if (mOnTimeChangedListener != null) {
diff --git a/core/java/android/widget/Toast.java b/core/java/android/widget/Toast.java
index 7762675..a400d90 100644
--- a/core/java/android/widget/Toast.java
+++ b/core/java/android/widget/Toast.java
@@ -25,6 +25,8 @@
 import android.content.res.Resources;
 import android.graphics.PixelFormat;
 import android.os.Handler;
+import android.os.IBinder;
+import android.os.Message;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.util.Log;
@@ -326,13 +328,6 @@
     }
 
     private static class TN extends ITransientNotification.Stub {
-        final Runnable mShow = new Runnable() {
-            @Override
-            public void run() {
-                handleShow();
-            }
-        };
-
         final Runnable mHide = new Runnable() {
             @Override
             public void run() {
@@ -343,7 +338,13 @@
         };
 
         private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();
-        final Handler mHandler = new Handler();
+        final Handler mHandler = new Handler() {
+            @Override
+            public void handleMessage(Message msg) {
+                IBinder token = (IBinder) msg.obj;
+                handleShow(token);
+            }
+        };
 
         int mGravity;
         int mX, mY;
@@ -379,9 +380,9 @@
          * schedule handleShow into the right thread
          */
         @Override
-        public void show() {
+        public void show(IBinder windowToken) {
             if (localLOGV) Log.v(TAG, "SHOW: " + this);
-            mHandler.post(mShow);
+            mHandler.obtainMessage(0, windowToken).sendToTarget();
         }
 
         /**
@@ -393,7 +394,7 @@
             mHandler.post(mHide);
         }
 
-        public void handleShow() {
+        public void handleShow(IBinder windowToken) {
             if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView
                     + " mNextView=" + mNextView);
             if (mView != mNextView) {
@@ -422,8 +423,9 @@
                 mParams.verticalMargin = mVerticalMargin;
                 mParams.horizontalMargin = mHorizontalMargin;
                 mParams.packageName = packageName;
-                mParams.removeTimeoutMilliseconds = mDuration ==
+                mParams.hideTimeoutMilliseconds = mDuration ==
                     Toast.LENGTH_LONG ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT;
+                mParams.token = windowToken;
                 if (mView.getParent() != null) {
                     if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
                     mWM.removeView(mView);
@@ -458,7 +460,7 @@
                 // the view isn't yet added, so let's try not to crash.
                 if (mView.getParent() != null) {
                     if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this);
-                    mWM.removeView(mView);
+                    mWM.removeViewImmediate(mView);
                 }
 
                 mView = null;
diff --git a/core/java/android/widget/Toolbar.java b/core/java/android/widget/Toolbar.java
index 9cdb73a..0988c92 100644
--- a/core/java/android/widget/Toolbar.java
+++ b/core/java/android/widget/Toolbar.java
@@ -161,7 +161,7 @@
     private int mTitleMarginTop;
     private int mTitleMarginBottom;
 
-    private final RtlSpacingHelper mContentInsets = new RtlSpacingHelper();
+    private RtlSpacingHelper mContentInsets;
     private int mContentInsetStartWithNavigation;
     private int mContentInsetEndWithActions;
 
@@ -270,6 +270,7 @@
         final int contentInsetRight =
                 a.getDimensionPixelSize(R.styleable.Toolbar_contentInsetRight, 0);
 
+        ensureContentInsets();
         mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
 
         if (contentInsetStart != RtlSpacingHelper.UNDEFINED ||
@@ -463,13 +464,13 @@
      */
     public void setTitleMarginBottom(int margin) {
         mTitleMarginBottom = margin;
-
         requestLayout();
     }
 
     @Override
     public void onRtlPropertiesChanged(@ResolvedLayoutDir int layoutDirection) {
         super.onRtlPropertiesChanged(layoutDirection);
+        ensureContentInsets();
         mContentInsets.setDirection(layoutDirection == LAYOUT_DIRECTION_RTL);
     }
 
@@ -1092,6 +1093,7 @@
      * @attr ref android.R.styleable#Toolbar_contentInsetStart
      */
     public void setContentInsetsRelative(int contentInsetStart, int contentInsetEnd) {
+        ensureContentInsets();
         mContentInsets.setRelative(contentInsetStart, contentInsetEnd);
     }
 
@@ -1112,7 +1114,7 @@
      * @attr ref android.R.styleable#Toolbar_contentInsetStart
      */
     public int getContentInsetStart() {
-        return mContentInsets.getStart();
+        return mContentInsets != null ? mContentInsets.getStart() : 0;
     }
 
     /**
@@ -1132,7 +1134,7 @@
      * @attr ref android.R.styleable#Toolbar_contentInsetEnd
      */
     public int getContentInsetEnd() {
-        return mContentInsets.getEnd();
+        return mContentInsets != null ? mContentInsets.getEnd() : 0;
     }
 
     /**
@@ -1154,6 +1156,7 @@
      * @attr ref android.R.styleable#Toolbar_contentInsetRight
      */
     public void setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) {
+        ensureContentInsets();
         mContentInsets.setAbsolute(contentInsetLeft, contentInsetRight);
     }
 
@@ -1174,7 +1177,7 @@
      * @attr ref android.R.styleable#Toolbar_contentInsetLeft
      */
     public int getContentInsetLeft() {
-        return mContentInsets.getLeft();
+        return mContentInsets != null ? mContentInsets.getLeft() : 0;
     }
 
     /**
@@ -1194,7 +1197,7 @@
      * @attr ref android.R.styleable#Toolbar_contentInsetRight
      */
     public int getContentInsetRight() {
-        return mContentInsets.getRight();
+        return mContentInsets != null ? mContentInsets.getRight() : 0;
     }
 
     /**
@@ -2128,6 +2131,12 @@
         }
     }
 
+    private void ensureContentInsets() {
+        if (mContentInsets == null) {
+            mContentInsets = new RtlSpacingHelper();
+        }
+    }
+
     /**
      * Accessor to enable LayoutLib to get ActionMenuPresenter directly.
      */
diff --git a/core/java/android/widget/VideoView.java b/core/java/android/widget/VideoView.java
index f084db2..b973324 100644
--- a/core/java/android/widget/VideoView.java
+++ b/core/java/android/widget/VideoView.java
@@ -70,46 +70,49 @@
  */
 public class VideoView extends SurfaceView
         implements MediaPlayerControl, SubtitleController.Anchor {
-    private String TAG = "VideoView";
-    // settable by the client
-    private Uri         mUri;
-    private Map<String, String> mHeaders;
+    private static final String TAG = "VideoView";
 
     // all possible internal states
-    private static final int STATE_ERROR              = -1;
-    private static final int STATE_IDLE               = 0;
-    private static final int STATE_PREPARING          = 1;
-    private static final int STATE_PREPARED           = 2;
-    private static final int STATE_PLAYING            = 3;
-    private static final int STATE_PAUSED             = 4;
+    private static final int STATE_ERROR = -1;
+    private static final int STATE_IDLE = 0;
+    private static final int STATE_PREPARING = 1;
+    private static final int STATE_PREPARED = 2;
+    private static final int STATE_PLAYING = 3;
+    private static final int STATE_PAUSED = 4;
     private static final int STATE_PLAYBACK_COMPLETED = 5;
 
+    private final Vector<Pair<InputStream, MediaFormat>> mPendingSubtitleTracks = new Vector<>();
+
+    // settable by the client
+    private Uri mUri;
+    private Map<String, String> mHeaders;
+
     // mCurrentState is a VideoView object's current state.
     // mTargetState is the state that a method caller intends to reach.
     // For instance, regardless the VideoView object's current state,
     // calling pause() intends to bring the object to a target state
     // of STATE_PAUSED.
     private int mCurrentState = STATE_IDLE;
-    private int mTargetState  = STATE_IDLE;
+    private int mTargetState = STATE_IDLE;
 
     // All the stuff we need for playing and showing a video
     private SurfaceHolder mSurfaceHolder = null;
     private MediaPlayer mMediaPlayer = null;
-    private int         mAudioSession;
-    private int         mVideoWidth;
-    private int         mVideoHeight;
-    private int         mSurfaceWidth;
-    private int         mSurfaceHeight;
+    private int mAudioSession;
+    private int mVideoWidth;
+    private int mVideoHeight;
+    private int mSurfaceWidth;
+    private int mSurfaceHeight;
     private MediaController mMediaController;
     private OnCompletionListener mOnCompletionListener;
     private MediaPlayer.OnPreparedListener mOnPreparedListener;
-    private int         mCurrentBufferPercentage;
+    private int mCurrentBufferPercentage;
     private OnErrorListener mOnErrorListener;
-    private OnInfoListener  mOnInfoListener;
-    private int         mSeekWhenPrepared;  // recording the seek position while preparing
-    private boolean     mCanPause;
-    private boolean     mCanSeekBack;
-    private boolean     mCanSeekForward;
+    private OnInfoListener mOnInfoListener;
+    private int mSeekWhenPrepared;  // recording the seek position while preparing
+    private boolean mCanPause;
+    private boolean mCanSeekBack;
+    private boolean mCanSeekForward;
 
     /** Subtitle rendering widget overlaid on top of the video. */
     private RenderingWidget mSubtitleWidget;
@@ -118,13 +121,11 @@
     private RenderingWidget.OnChangedListener mSubtitlesChangedListener;
 
     public VideoView(Context context) {
-        super(context);
-        initVideoView();
+        this(context, null);
     }
 
     public VideoView(Context context, AttributeSet attrs) {
         this(context, attrs, 0);
-        initVideoView();
     }
 
     public VideoView(Context context, AttributeSet attrs, int defStyleAttr) {
@@ -133,7 +134,19 @@
 
     public VideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
         super(context, attrs, defStyleAttr, defStyleRes);
-        initVideoView();
+
+        mVideoWidth = 0;
+        mVideoHeight = 0;
+
+        getHolder().addCallback(mSHCallback);
+        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
+
+        setFocusable(true);
+        setFocusableInTouchMode(true);
+        requestFocus();
+
+        mCurrentState = STATE_IDLE;
+        mTargetState = STATE_IDLE;
     }
 
     @Override
@@ -209,19 +222,6 @@
         return getDefaultSize(desiredSize, measureSpec);
     }
 
-    private void initVideoView() {
-        mVideoWidth = 0;
-        mVideoHeight = 0;
-        getHolder().addCallback(mSHCallback);
-        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
-        setFocusable(true);
-        setFocusableInTouchMode(true);
-        requestFocus();
-        mPendingSubtitleTracks = new Vector<Pair<InputStream, MediaFormat>>();
-        mCurrentState = STATE_IDLE;
-        mTargetState  = STATE_IDLE;
-    }
-
     /**
      * Sets video path.
      *
@@ -294,8 +294,6 @@
         }
     }
 
-    private Vector<Pair<InputStream, MediaFormat>> mPendingSubtitleTracks;
-
     public void stopPlayback() {
         if (mMediaPlayer != null) {
             mMediaPlayer.stop();
diff --git a/core/java/com/android/internal/app/AlertActivity.java b/core/java/com/android/internal/app/AlertActivity.java
index ed48b0d..35ffa71 100644
--- a/core/java/com/android/internal/app/AlertActivity.java
+++ b/core/java/com/android/internal/app/AlertActivity.java
@@ -49,7 +49,7 @@
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
-        mAlert = new AlertController(this, this, getWindow());
+        mAlert = AlertController.create(this, this, getWindow());
         mAlertParams = new AlertController.AlertParams(this);
     }
 
diff --git a/core/java/com/android/internal/app/AlertController.java b/core/java/com/android/internal/app/AlertController.java
index b7ac600..95c291a 100644
--- a/core/java/com/android/internal/app/AlertController.java
+++ b/core/java/com/android/internal/app/AlertController.java
@@ -24,6 +24,7 @@
 import android.app.AlertDialog;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.res.Configuration;
 import android.content.res.TypedArray;
 import android.database.Cursor;
 import android.graphics.drawable.Drawable;
@@ -61,14 +62,15 @@
 import java.lang.ref.WeakReference;
 
 public class AlertController {
+    public static final int MICRO = 1;
 
     private final Context mContext;
     private final DialogInterface mDialogInterface;
-    private final Window mWindow;
+    protected final Window mWindow;
 
     private CharSequence mTitle;
-    private CharSequence mMessage;
-    private ListView mListView;
+    protected CharSequence mMessage;
+    protected ListView mListView;
     private View mView;
 
     private int mViewLayoutResId;
@@ -91,14 +93,14 @@
     private CharSequence mButtonNeutralText;
     private Message mButtonNeutralMessage;
 
-    private ScrollView mScrollView;
+    protected ScrollView mScrollView;
 
     private int mIconId = 0;
     private Drawable mIcon;
 
     private ImageView mIconView;
     private TextView mTitleView;
-    private TextView mMessageView;
+    protected TextView mMessageView;
     private View mCustomTitleView;
 
     private boolean mForceInverseBackground;
@@ -176,7 +178,21 @@
         return outValue.data != 0;
     }
 
-    public AlertController(Context context, DialogInterface di, Window window) {
+    public static final AlertController create(Context context, DialogInterface di, Window window) {
+        final TypedArray a = context.obtainStyledAttributes(
+                null, R.styleable.AlertDialog, R.attr.alertDialogStyle, 0);
+        int controllerType = a.getInt(R.styleable.AlertDialog_controllerType, 0);
+        a.recycle();
+
+        switch (controllerType) {
+            case MICRO:
+                return new MicroAlertController(context, di, window);
+            default:
+                return new AlertController(context, di, window);
+        }
+    }
+
+    protected AlertController(Context context, DialogInterface di, Window window) {
         mContext = context;
         mDialogInterface = di;
         mWindow = window;
@@ -597,7 +613,7 @@
         }
     }
 
-    private void setupTitle(ViewGroup topPanel) {
+    protected void setupTitle(ViewGroup topPanel) {
         if (mCustomTitleView != null && mShowTitle) {
             // Add the custom title view directly to the topPanel layout
             final LayoutParams lp = new LayoutParams(
@@ -643,7 +659,7 @@
         }
     }
 
-    private void setupContent(ViewGroup contentPanel) {
+    protected void setupContent(ViewGroup contentPanel) {
         mScrollView = (ScrollView) contentPanel.findViewById(R.id.scrollView);
         mScrollView.setFocusable(false);
 
@@ -680,7 +696,7 @@
         }
     }
 
-    private void setupButtons(ViewGroup buttonPanel) {
+    protected void setupButtons(ViewGroup buttonPanel) {
         int BIT_BUTTON_POSITIVE = 1;
         int BIT_BUTTON_NEGATIVE = 2;
         int BIT_BUTTON_NEUTRAL = 4;
@@ -872,7 +888,8 @@
             final int checkedItem = mCheckedItem;
             if (checkedItem > -1) {
                 listView.setItemChecked(checkedItem, true);
-                listView.setSelection(checkedItem);
+                listView.setSelectionFromTop(checkedItem,
+                        a.getDimensionPixelSize(R.styleable.AlertDialog_selectionScrollOffset, 0));
             }
         }
     }
@@ -1072,7 +1089,8 @@
                         public void bindView(View view, Context context, Cursor cursor) {
                             CheckedTextView text = (CheckedTextView) view.findViewById(R.id.text1);
                             text.setText(cursor.getString(mLabelIndex));
-                            listView.setItemChecked(cursor.getPosition(),
+                            listView.setItemChecked(
+                                    cursor.getPosition(),
                                     cursor.getInt(mIsCheckedIndex) == 1);
                         }
 
diff --git a/core/java/com/android/internal/app/AssistUtils.java b/core/java/com/android/internal/app/AssistUtils.java
index d552e54..2940079 100644
--- a/core/java/com/android/internal/app/AssistUtils.java
+++ b/core/java/com/android/internal/app/AssistUtils.java
@@ -16,10 +16,13 @@
 
 package com.android.internal.app;
 
+import com.android.internal.R;
+
 import android.app.SearchManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.os.Bundle;
@@ -132,6 +135,16 @@
         }
     }
 
+    public void registerVoiceInteractionSessionListener(IVoiceInteractionSessionListener listener) {
+        try {
+            if (mVoiceInteractionManagerService != null) {
+                mVoiceInteractionManagerService.registerVoiceInteractionSessionListener(listener);
+            }
+        } catch (RemoteException e) {
+            Log.w(TAG, "Failed to register voice interaction listener", e);
+        }
+    }
+
     public ComponentName getAssistComponentForUser(int userId) {
         final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
                 Settings.Secure.ASSISTANT, userId);
@@ -156,4 +169,41 @@
         return null;
     }
 
+    public static boolean isPreinstalledAssistant(Context context, ComponentName assistant) {
+        if (assistant == null) {
+            return false;
+        }
+        ApplicationInfo applicationInfo;
+        try {
+            applicationInfo = context.getPackageManager().getApplicationInfo(
+                    assistant.getPackageName(), 0);
+        } catch (PackageManager.NameNotFoundException e) {
+            return false;
+        }
+        return applicationInfo.isSystemApp() || applicationInfo.isUpdatedSystemApp();
+    }
+
+    private static boolean isDisclosureEnabled(Context context) {
+        return Settings.Secure.getInt(context.getContentResolver(),
+                Settings.Secure.ASSIST_DISCLOSURE_ENABLED, 0) != 0;
+    }
+
+    /**
+     * @return if the disclosure animation should trigger for the given assistant.
+     *
+     * Third-party assistants will always need to disclose, while the user can configure this for
+     * pre-installed assistants.
+     */
+    public static boolean shouldDisclose(Context context, ComponentName assistant) {
+        if (!allowDisablingAssistDisclosure(context)) {
+            return true;
+        }
+
+        return isDisclosureEnabled(context) || !isPreinstalledAssistant(context, assistant);
+    }
+
+    public static boolean allowDisablingAssistDisclosure(Context context) {
+        return context.getResources().getBoolean(
+                com.android.internal.R.bool.config_allowDisablingAssistDisclosure);
+    }
 }
diff --git a/core/java/com/android/internal/app/IBatteryStats.aidl b/core/java/com/android/internal/app/IBatteryStats.aidl
index 216a4f1..5623a2c 100644
--- a/core/java/com/android/internal/app/IBatteryStats.aidl
+++ b/core/java/com/android/internal/app/IBatteryStats.aidl
@@ -79,6 +79,8 @@
             String newHistoryName, int newType, boolean newUnimportantForLogging);
     void noteStopWakelockFromSource(in WorkSource ws, int pid, String name, String historyName,
             int type);
+    void noteLongPartialWakelockStart(String name, String historyName, int uid);
+    void noteLongPartialWakelockFinish(String name, String historyName, int uid);
 
     void noteVibratorOn(int uid, long durationMillis);
     void noteVibratorOff(int uid);
@@ -118,7 +120,7 @@
     void noteWifiBatchedScanStoppedFromSource(in WorkSource ws);
     void noteWifiMulticastEnabledFromSource(in WorkSource ws);
     void noteWifiMulticastDisabledFromSource(in WorkSource ws);
-    void noteWifiRadioPowerState(int powerState, long timestampNs);
+    void noteWifiRadioPowerState(int powerState, long timestampNs, int uid);
     void noteNetworkInterfaceType(String iface, int type);
     void noteNetworkStatsEnabled();
     void noteDeviceIdleMode(int mode, String activeReason, int activeUid);
diff --git a/core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl b/core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl
index 1a963f3..033dd13 100644
--- a/core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl
+++ b/core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl
@@ -22,6 +22,7 @@
 
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
 import com.android.internal.app.IVoiceInteractor;
+import com.android.internal.app.IVoiceInteractionSessionListener;
 import android.hardware.soundtrigger.IRecognitionStatusCallback;
 import android.hardware.soundtrigger.SoundTrigger;
 import android.service.voice.IVoiceInteractionService;
@@ -136,4 +137,9 @@
      * Called when the lockscreen got shown.
      */
     void onLockscreenShown();
+
+    /**
+     * Register a voice interaction listener.
+     */
+    void registerVoiceInteractionSessionListener(IVoiceInteractionSessionListener listener);
 }
diff --git a/core/java/com/android/internal/app/IEphemeralResolver.aidl b/core/java/com/android/internal/app/IVoiceInteractionSessionListener.aidl
similarity index 63%
copy from core/java/com/android/internal/app/IEphemeralResolver.aidl
copy to core/java/com/android/internal/app/IVoiceInteractionSessionListener.aidl
index 40429ee..87749d2 100644
--- a/core/java/com/android/internal/app/IEphemeralResolver.aidl
+++ b/core/java/com/android/internal/app/IVoiceInteractionSessionListener.aidl
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -14,11 +14,16 @@
  * limitations under the License.
  */
 
-package com.android.internal.app;
+ package com.android.internal.app;
 
-import android.content.Intent;
-import android.os.IRemoteCallback;
+ oneway interface IVoiceInteractionSessionListener {
+    /**
+     * Called when a voice session is shown.
+     */
+    void onVoiceSessionShown();
 
-oneway interface IEphemeralResolver {
-    void getEphemeralResolveInfoList(IRemoteCallback callback, int digestPrefix, int sequence);
-}
+    /**
+     * Called when a voice session is hidden.
+     */
+    void onVoiceSessionHidden();
+ }
\ No newline at end of file
diff --git a/core/java/com/android/internal/app/MicroAlertController.java b/core/java/com/android/internal/app/MicroAlertController.java
new file mode 100644
index 0000000..4431f3c
--- /dev/null
+++ b/core/java/com/android/internal/app/MicroAlertController.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.app;
+
+import android.content.Context;
+import android.content.DialogInterface;
+import android.text.TextUtils;
+import android.view.Gravity;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.Window;
+import android.widget.FrameLayout;
+import android.widget.ImageView;
+import android.widget.ScrollView;
+import android.widget.TextView;
+
+import com.android.internal.app.AlertController;
+import com.android.internal.R;
+
+public class MicroAlertController extends AlertController {
+    public MicroAlertController(Context context, DialogInterface di, Window window) {
+        super(context, di, window);
+    }
+
+    @Override
+    protected void setupContent(ViewGroup contentPanel) {
+        // Special case for small screen - the scroll view is higher in hierarchy
+        mScrollView = (ScrollView) mWindow.findViewById(R.id.scrollView);
+
+        // Special case for users that only want to display a String
+        mMessageView = (TextView) contentPanel.findViewById(R.id.message);
+        if (mMessageView == null) {
+            return;
+        }
+
+        if (mMessage != null) {
+            mMessageView.setText(mMessage);
+        } else {
+            // no message, remove associated views
+            mMessageView.setVisibility(View.GONE);
+            contentPanel.removeView(mMessageView);
+
+            if (mListView != null) {
+                // has ListView, swap scrollView with ListView
+
+                // move topPanel into top of scrollParent
+                View topPanel = mScrollView.findViewById(R.id.topPanel);
+                ((ViewGroup) topPanel.getParent()).removeView(topPanel);
+                FrameLayout.LayoutParams topParams =
+                        new FrameLayout.LayoutParams(topPanel.getLayoutParams());
+                topParams.gravity = Gravity.TOP;
+                topPanel.setLayoutParams(topParams);
+
+                // move buttonPanel into bottom of scrollParent
+                View buttonPanel = mScrollView.findViewById(R.id.buttonPanel);
+                ((ViewGroup) buttonPanel.getParent()).removeView(buttonPanel);
+                FrameLayout.LayoutParams buttonParams =
+                        new FrameLayout.LayoutParams(buttonPanel.getLayoutParams());
+                buttonParams.gravity = Gravity.BOTTOM;
+                buttonPanel.setLayoutParams(buttonParams);
+
+                // remove scrollview
+                final ViewGroup scrollParent = (ViewGroup) mScrollView.getParent();
+                final int childIndex = scrollParent.indexOfChild(mScrollView);
+                scrollParent.removeViewAt(childIndex);
+
+                // add list view
+                scrollParent.addView(mListView,
+                        new ViewGroup.LayoutParams(
+                                ViewGroup.LayoutParams.MATCH_PARENT,
+                                ViewGroup.LayoutParams.MATCH_PARENT));
+
+                // add top and button panel
+                scrollParent.addView(topPanel);
+                scrollParent.addView(buttonPanel);
+            } else {
+                // no content, just hide everything
+                contentPanel.setVisibility(View.GONE);
+            }
+        }
+    }
+
+    @Override
+    protected void setupTitle(ViewGroup topPanel) {
+        super.setupTitle(topPanel);
+        if (topPanel.getVisibility() == View.GONE) {
+            topPanel.setVisibility(View.INVISIBLE);
+        }
+    }
+
+    @Override
+    protected void setupButtons(ViewGroup buttonPanel) {
+        super.setupButtons(buttonPanel);
+        if (buttonPanel.getVisibility() == View.GONE) {
+            buttonPanel.setVisibility(View.INVISIBLE);
+        }
+    }
+}
diff --git a/core/java/com/android/internal/app/NightDisplayController.java b/core/java/com/android/internal/app/NightDisplayController.java
new file mode 100644
index 0000000..03cd729
--- /dev/null
+++ b/core/java/com/android/internal/app/NightDisplayController.java
@@ -0,0 +1,421 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.app;
+
+import android.annotation.IntDef;
+import android.annotation.NonNull;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.UserHandle;
+import android.provider.Settings.Secure;
+import android.util.Slog;
+
+import com.android.internal.R;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.util.Calendar;
+import java.util.Locale;
+
+/**
+ * Controller for managing Night display settings.
+ * <p/>
+ * Night display tints your screen red at night. This makes it easier to look at your screen in
+ * dim light and may help you fall asleep more easily.
+ */
+public final class NightDisplayController {
+
+    private static final String TAG = "NightDisplayController";
+    private static final boolean DEBUG = false;
+
+    /** @hide */
+    @Retention(RetentionPolicy.SOURCE)
+    @IntDef({ AUTO_MODE_DISABLED, AUTO_MODE_CUSTOM, AUTO_MODE_TWILIGHT })
+    public @interface AutoMode {}
+
+    /**
+     * Auto mode value to prevent Night display from being automatically activated. It can still
+     * be activated manually via {@link #setActivated(boolean)}.
+     *
+     * @see #setAutoMode(int)
+     */
+    public static final int AUTO_MODE_DISABLED = 0;
+    /**
+     * Auto mode value to automatically activate Night display at a specific start and end time.
+     *
+     * @see #setAutoMode(int)
+     * @see #setCustomStartTime(LocalTime)
+     * @see #setCustomEndTime(LocalTime)
+     */
+    public static final int AUTO_MODE_CUSTOM = 1;
+    /**
+     * Auto mode value to automatically activate Night display from sunset to sunrise.
+     *
+     * @see #setAutoMode(int)
+     */
+    public static final int AUTO_MODE_TWILIGHT = 2;
+
+    private final Context mContext;
+    private final int mUserId;
+
+    private final ContentObserver mContentObserver;
+
+    private Callback mCallback;
+
+    public NightDisplayController(@NonNull Context context) {
+        this(context, UserHandle.myUserId());
+    }
+
+    public NightDisplayController(@NonNull Context context, int userId) {
+        mContext = context.getApplicationContext();
+        mUserId = userId;
+
+        mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
+            @Override
+            public void onChange(boolean selfChange, Uri uri) {
+                super.onChange(selfChange, uri);
+
+                final String setting = uri == null ? null : uri.getLastPathSegment();
+                if (setting != null) {
+                    onSettingChanged(setting);
+                }
+            }
+        };
+    }
+
+    /**
+     * Returns {@code true} when Night display is activated (the display is tinted red).
+     */
+    public boolean isActivated() {
+        return Secure.getIntForUser(mContext.getContentResolver(),
+                Secure.NIGHT_DISPLAY_ACTIVATED, 0, mUserId) == 1;
+    }
+
+    /**
+     * Sets whether Night display should be activated.
+     *
+     * @param activated {@code true} if Night display should be activated
+     * @return {@code true} if the activated value was set successfully
+     */
+    public boolean setActivated(boolean activated) {
+        return Secure.putIntForUser(mContext.getContentResolver(),
+                Secure.NIGHT_DISPLAY_ACTIVATED, activated ? 1 : 0, mUserId);
+    }
+
+    /**
+     * Returns the current auto mode value controlling when Night display will be automatically
+     * activated. One of {@link #AUTO_MODE_DISABLED}, {@link #AUTO_MODE_CUSTOM}, or
+     * {@link #AUTO_MODE_TWILIGHT}.
+     */
+    public @AutoMode int getAutoMode() {
+        int autoMode = Secure.getIntForUser(mContext.getContentResolver(),
+                Secure.NIGHT_DISPLAY_AUTO_MODE, -1, mUserId);
+        if (autoMode == -1) {
+            if (DEBUG) {
+                Slog.d(TAG, "Using default value for setting: " + Secure.NIGHT_DISPLAY_AUTO_MODE);
+            }
+            autoMode = mContext.getResources().getInteger(
+                    R.integer.config_defaultNightDisplayAutoMode);
+        }
+
+        if (autoMode != AUTO_MODE_DISABLED
+                && autoMode != AUTO_MODE_CUSTOM
+                && autoMode != AUTO_MODE_TWILIGHT) {
+            Slog.e(TAG, "Invalid autoMode: " + autoMode);
+            autoMode = AUTO_MODE_DISABLED;
+        }
+
+        return autoMode;
+    }
+
+    /**
+     * Sets the current auto mode value controlling when Night display will be automatically
+     * activated. One of {@link #AUTO_MODE_DISABLED}, {@link #AUTO_MODE_CUSTOM}, or
+     * {@link #AUTO_MODE_TWILIGHT}.
+     *
+     * @param autoMode the new auto mode to use
+     * @return {@code true} if new auto mode was set successfully
+     */
+    public boolean setAutoMode(@AutoMode int autoMode) {
+        if (autoMode != AUTO_MODE_DISABLED
+                && autoMode != AUTO_MODE_CUSTOM
+                && autoMode != AUTO_MODE_TWILIGHT) {
+            throw new IllegalArgumentException("Invalid autoMode: " + autoMode);
+        }
+
+        return Secure.putIntForUser(mContext.getContentResolver(),
+                Secure.NIGHT_DISPLAY_AUTO_MODE, autoMode, mUserId);
+    }
+
+    /**
+     * Returns the local time when Night display will be automatically activated when using
+     * {@link #AUTO_MODE_CUSTOM}.
+     */
+    public @NonNull LocalTime getCustomStartTime() {
+        int startTimeValue = Secure.getIntForUser(mContext.getContentResolver(),
+                Secure.NIGHT_DISPLAY_CUSTOM_START_TIME, -1, mUserId);
+        if (startTimeValue == -1) {
+            if (DEBUG) {
+                Slog.d(TAG, "Using default value for setting: "
+                        + Secure.NIGHT_DISPLAY_CUSTOM_START_TIME);
+            }
+            startTimeValue = mContext.getResources().getInteger(
+                    R.integer.config_defaultNightDisplayCustomStartTime);
+        }
+
+        return LocalTime.valueOf(startTimeValue);
+    }
+
+    /**
+     * Sets the local time when Night display will be automatically activated when using
+     * {@link #AUTO_MODE_CUSTOM}.
+     *
+     * @param startTime the local time to automatically activate Night display
+     * @return {@code true} if the new custom start time was set successfully
+     */
+    public boolean setCustomStartTime(@NonNull LocalTime startTime) {
+        if (startTime == null) {
+            throw new IllegalArgumentException("startTime cannot be null");
+        }
+        return Secure.putIntForUser(mContext.getContentResolver(),
+                Secure.NIGHT_DISPLAY_CUSTOM_START_TIME, startTime.toMillis(), mUserId);
+    }
+
+    /**
+     * Returns the local time when Night display will be automatically deactivated when using
+     * {@link #AUTO_MODE_CUSTOM}.
+     */
+    public @NonNull LocalTime getCustomEndTime() {
+        int endTimeValue = Secure.getIntForUser(mContext.getContentResolver(),
+                Secure.NIGHT_DISPLAY_CUSTOM_END_TIME, -1, mUserId);
+        if (endTimeValue == -1) {
+            if (DEBUG) {
+                Slog.d(TAG, "Using default value for setting: "
+                        + Secure.NIGHT_DISPLAY_CUSTOM_END_TIME);
+            }
+            endTimeValue = mContext.getResources().getInteger(
+                    R.integer.config_defaultNightDisplayCustomEndTime);
+        }
+
+        return LocalTime.valueOf(endTimeValue);
+    }
+
+    /**
+     * Sets the local time when Night display will be automatically deactivated when using
+     * {@link #AUTO_MODE_CUSTOM}.
+     *
+     * @param endTime the local time to automatically deactivate Night display
+     * @return {@code true} if the new custom end time was set successfully
+     */
+    public boolean setCustomEndTime(@NonNull LocalTime endTime) {
+        if (endTime == null) {
+            throw new IllegalArgumentException("endTime cannot be null");
+        }
+        return Secure.putIntForUser(mContext.getContentResolver(),
+                Secure.NIGHT_DISPLAY_CUSTOM_END_TIME, endTime.toMillis(), mUserId);
+    }
+
+    private void onSettingChanged(@NonNull String setting) {
+        if (DEBUG) {
+            Slog.d(TAG, "onSettingChanged: " + setting);
+        }
+
+        if (mCallback != null) {
+            switch (setting) {
+                case Secure.NIGHT_DISPLAY_ACTIVATED:
+                    mCallback.onActivated(isActivated());
+                    break;
+                case Secure.NIGHT_DISPLAY_AUTO_MODE:
+                    mCallback.onAutoModeChanged(getAutoMode());
+                    break;
+                case Secure.NIGHT_DISPLAY_CUSTOM_START_TIME:
+                    mCallback.onCustomStartTimeChanged(getCustomStartTime());
+                    break;
+                case Secure.NIGHT_DISPLAY_CUSTOM_END_TIME:
+                    mCallback.onCustomEndTimeChanged(getCustomEndTime());
+                    break;
+            }
+        }
+    }
+
+    /**
+     * Register a callback to be invoked whenever the Night display settings are changed.
+     */
+    public void setListener(Callback callback) {
+        final Callback oldCallback = mCallback;
+        if (oldCallback != callback) {
+            mCallback = callback;
+
+            if (callback == null) {
+                // Stop listening for changes now that there IS NOT a listener.
+                mContext.getContentResolver().unregisterContentObserver(mContentObserver);
+            } else if (oldCallback == null) {
+                // Start listening for changes now that there IS a listener.
+                final ContentResolver cr = mContext.getContentResolver();
+                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_ACTIVATED),
+                        false /* notifyForDescendants */, mContentObserver, mUserId);
+                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_AUTO_MODE),
+                        false /* notifyForDescendants */, mContentObserver, mUserId);
+                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_START_TIME),
+                        false /* notifyForDescendants */, mContentObserver, mUserId);
+                cr.registerContentObserver(Secure.getUriFor(Secure.NIGHT_DISPLAY_CUSTOM_END_TIME),
+                        false /* notifyForDescendants */, mContentObserver, mUserId);
+            }
+        }
+    }
+
+    /**
+     * Returns {@code true} if Night display is supported by the device.
+     */
+    public static boolean isAvailable(Context context) {
+        return context.getResources().getBoolean(R.bool.config_nightDisplayAvailable);
+    }
+
+    /**
+     * A time without a time-zone or date.
+     */
+    public static class LocalTime {
+
+        /**
+         * The hour of the day from 0 - 23.
+         */
+        public final int hourOfDay;
+        /**
+         * The minute within the hour from 0 - 59.
+         */
+        public final int minute;
+
+        public LocalTime(int hourOfDay, int minute) {
+            if (hourOfDay < 0 || hourOfDay > 23) {
+                throw new IllegalArgumentException("Invalid hourOfDay: " + hourOfDay);
+            } else if (minute < 0 || minute > 59) {
+                throw new IllegalArgumentException("Invalid minute: " + minute);
+            }
+
+            this.hourOfDay = hourOfDay;
+            this.minute = minute;
+        }
+
+        /**
+         * Returns the first date time corresponding to this local time that occurs before the
+         * provided date time.
+         *
+         * @param time the date time to compare against
+         * @return the prior date time corresponding to this local time
+         */
+        public Calendar getDateTimeBefore(Calendar time) {
+            final Calendar c = Calendar.getInstance();
+            c.set(Calendar.YEAR, time.get(Calendar.YEAR));
+            c.set(Calendar.DAY_OF_YEAR, time.get(Calendar.DAY_OF_YEAR));
+
+            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
+            c.set(Calendar.MINUTE, minute);
+            c.set(Calendar.SECOND, 0);
+            c.set(Calendar.MILLISECOND, 0);
+
+            // Check if the local time has past, if so return the same time tomorrow.
+            if (c.after(time)) {
+                c.add(Calendar.DATE, -1);
+            }
+
+            return c;
+        }
+
+        /**
+         * Returns the first date time corresponding to this local time that occurs after the
+         * provided date time.
+         *
+         * @param time the date time to compare against
+         * @return the next date time corresponding to this local time
+         */
+        public Calendar getDateTimeAfter(Calendar time) {
+            final Calendar c = Calendar.getInstance();
+            c.set(Calendar.YEAR, time.get(Calendar.YEAR));
+            c.set(Calendar.DAY_OF_YEAR, time.get(Calendar.DAY_OF_YEAR));
+
+            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
+            c.set(Calendar.MINUTE, minute);
+            c.set(Calendar.SECOND, 0);
+            c.set(Calendar.MILLISECOND, 0);
+
+            // Check if the local time has past, if so return the same time tomorrow.
+            if (c.before(time)) {
+                c.add(Calendar.DATE, 1);
+            }
+
+            return c;
+        }
+
+        /**
+         * Returns a local time corresponding the given number of milliseconds from midnight.
+         *
+         * @param millis the number of milliseconds from midnight
+         * @return the corresponding local time
+         */
+        private static LocalTime valueOf(int millis) {
+            final int hourOfDay = (millis / 3600000) % 24;
+            final int minutes = (millis / 60000) % 60;
+            return new LocalTime(hourOfDay, minutes);
+        }
+
+        /**
+         * Returns the local time represented as milliseconds from midnight.
+         */
+        private int toMillis() {
+            return hourOfDay * 3600000 + minute * 60000;
+        }
+
+        @Override
+        public String toString() {
+            return String.format(Locale.US, "%02d:%02d", hourOfDay, minute);
+        }
+    }
+
+    /**
+     * Callback invoked whenever the Night display settings are changed.
+     */
+    public interface Callback {
+        /**
+         * Callback invoked when the activated state changes.
+         *
+         * @param activated {@code true} if Night display is activated
+         */
+        default void onActivated(boolean activated) {}
+        /**
+         * Callback invoked when the auto mode changes.
+         *
+         * @param autoMode the auto mode to use
+         */
+        default void onAutoModeChanged(int autoMode) {}
+        /**
+         * Callback invoked when the time to automatically activate Night display changes.
+         *
+         * @param startTime the local time to automatically activate Night display
+         */
+        default void onCustomStartTimeChanged(LocalTime startTime) {}
+        /**
+         * Callback invoked when the time to automatically deactivate Night display changes.
+         *
+         * @param endTime the local time to automatically deactivate Night display
+         */
+        default void onCustomEndTimeChanged(LocalTime endTime) {}
+    }
+}
diff --git a/core/java/com/android/internal/app/ResolverActivity.java b/core/java/com/android/internal/app/ResolverActivity.java
index 085e159..1e26c92 100644
--- a/core/java/com/android/internal/app/ResolverActivity.java
+++ b/core/java/com/android/internal/app/ResolverActivity.java
@@ -69,9 +69,13 @@
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.Toast;
+
+import com.android.internal.logging.MetricsLogger;
+import com.android.internal.logging.MetricsProto;
 import com.android.internal.widget.ResolverDrawerLayout;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -359,6 +363,12 @@
         if (isVoiceInteraction()) {
             onSetupVoiceInteraction();
         }
+        final Set<String> categories = intent.getCategories();
+        MetricsLogger.action(this, mAdapter.hasFilteredItem()
+                ? MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_APP_FEATURED
+                : MetricsProto.MetricsEvent.ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED,
+                intent.getAction() + ":" + intent.getType() + ":"
+                        + (categories != null ? Arrays.toString(categories.toArray()) : ""));
     }
 
     public final void setFilteredComponents(ComponentName[] components) {
@@ -649,6 +659,19 @@
 
         TargetInfo target = mAdapter.targetInfoForPosition(which, filtered);
         if (onTargetSelected(target, always)) {
+            if (always && filtered) {
+                MetricsLogger.action(
+                        this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_ALWAYS);
+            } else if (filtered) {
+                MetricsLogger.action(
+                        this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_JUST_ONCE);
+            } else {
+                MetricsLogger.action(
+                        this, MetricsProto.MetricsEvent.ACTION_APP_DISAMBIG_TAP);
+            }
+            MetricsLogger.action(this, mAdapter.hasFilteredItem()
+                            ? MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_APP_FEATURED
+                            : MetricsProto.MetricsEvent.ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED);
             finish();
         }
     }
@@ -1446,7 +1469,7 @@
                 boolean found = false;
                 // Only loop to the end of into as it was before we started; no dupes in from.
                 for (int j = 0; j < intoCount; j++) {
-                    final ResolvedComponentInfo rci = into.get(i);
+                    final ResolvedComponentInfo rci = into.get(j);
                     if (isSameResolvedComponent(newInfo, rci)) {
                         found = true;
                         rci.add(intent, newInfo);
diff --git a/core/java/com/android/internal/app/ResolverComparator.java b/core/java/com/android/internal/app/ResolverComparator.java
index 03a3a38..4d4c7ce 100644
--- a/core/java/com/android/internal/app/ResolverComparator.java
+++ b/core/java/com/android/internal/app/ResolverComparator.java
@@ -157,7 +157,10 @@
 
         // We want to put the one targeted to another user at the end of the dialog.
         if (lhs.targetUserId != UserHandle.USER_CURRENT) {
-            return 1;
+            return rhs.targetUserId != UserHandle.USER_CURRENT ? 0 : 1;
+        }
+        if (rhs.targetUserId != UserHandle.USER_CURRENT) {
+            return -1;
         }
 
         if (mHttp) {
diff --git a/core/java/com/android/internal/app/SuggestedLocaleAdapter.java b/core/java/com/android/internal/app/SuggestedLocaleAdapter.java
index a4b5a8e..cb2b019 100644
--- a/core/java/com/android/internal/app/SuggestedLocaleAdapter.java
+++ b/core/java/com/android/internal/app/SuggestedLocaleAdapter.java
@@ -145,7 +145,11 @@
                 if (itemType == TYPE_HEADER_SUGGESTED) {
                     textView.setText(R.string.language_picker_section_suggested);
                 } else {
-                    textView.setText(R.string.language_picker_section_all);
+                    if (mCountryMode) {
+                        textView.setText(R.string.region_picker_section_all);
+                    } else {
+                        textView.setText(R.string.language_picker_section_all);
+                    }
                 }
                 textView.setTextLocale(Locale.getDefault());
                 break;
diff --git a/core/java/com/android/internal/app/UnlaunchableAppActivity.java b/core/java/com/android/internal/app/UnlaunchableAppActivity.java
index d24cefe..0a539f1 100644
--- a/core/java/com/android/internal/app/UnlaunchableAppActivity.java
+++ b/core/java/com/android/internal/app/UnlaunchableAppActivity.java
@@ -37,6 +37,7 @@
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.Window;
 import android.widget.TextView;
 
 import com.android.internal.R;
@@ -59,6 +60,9 @@
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        // As this activity has nothing to show, we should hide the title bar also
+        // TODO: Use AlertActivity so we don't need to hide title bar and create a dialog
+        requestWindowFeature(Window.FEATURE_NO_TITLE);
         Intent intent = getIntent();
         mReason = intent.getIntExtra(EXTRA_UNLAUNCHABLE_REASON, -1);
         mUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
diff --git a/core/java/com/android/internal/appwidget/IAppWidgetService.aidl b/core/java/com/android/internal/appwidget/IAppWidgetService.aidl
index 4260e50..951a45a 100644
--- a/core/java/com/android/internal/appwidget/IAppWidgetService.aidl
+++ b/core/java/com/android/internal/appwidget/IAppWidgetService.aidl
@@ -34,7 +34,7 @@
     // for AppWidgetHost
     //
     ParceledListSlice startListening(IAppWidgetHost host, String callingPackage, int hostId,
-            in int[] appWidgetIds, out int[] updatedIds);
+            in int[] appWidgetIds);
     void stopListening(String callingPackage, int hostId);
     int allocateAppWidgetId(String callingPackage, int hostId);
     void deleteAppWidgetId(String callingPackage, int appWidgetId);
diff --git a/core/java/com/android/internal/graphics/drawable/AnimationScaleListDrawable.java b/core/java/com/android/internal/graphics/drawable/AnimationScaleListDrawable.java
new file mode 100644
index 0000000..62f18ea
--- /dev/null
+++ b/core/java/com/android/internal/graphics/drawable/AnimationScaleListDrawable.java
@@ -0,0 +1,254 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.graphics.drawable;
+
+import android.animation.ValueAnimator;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.content.res.Resources;
+import android.content.res.Resources.Theme;
+import android.content.res.TypedArray;
+import android.graphics.drawable.Animatable;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.DrawableContainer;
+import android.util.AttributeSet;
+
+import com.android.internal.R;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
+import java.io.IOException;
+
+/**
+ * An internal DrawableContainer class, used to draw different things depending on animation scale.
+ * i.e: animation scale can be 0 in battery saver mode.
+ * This class contains 2 drawable, one is animatable, the other is static. When animation scale is
+ * not 0, the animatable drawable will the drawn. Otherwise, the static drawable will be drawn.
+ * <p>This class implements Animatable since ProgressBar can pick this up similarly as an
+ * AnimatedVectorDrawable.
+ * <p>It can be defined in an XML file with the {@code <AnimationScaleListDrawable>}
+ * element.
+ */
+public class AnimationScaleListDrawable extends DrawableContainer implements Animatable {
+    private static final String TAG = "AnimationScaleListDrawable";
+    private AnimationScaleListState mAnimationScaleListState;
+    private boolean mMutated;
+
+    public AnimationScaleListDrawable() {
+        this(null, null);
+    }
+
+    private AnimationScaleListDrawable(@Nullable AnimationScaleListState state,
+            @Nullable Resources res) {
+        // Every scale list drawable has its own constant state.
+        final AnimationScaleListState newState = new AnimationScaleListState(state, this, res);
+        setConstantState(newState);
+        onStateChange(getState());
+    }
+
+    /**
+     * Set the current drawable according to the animation scale. If scale is 0, then pick the
+     * static drawable, otherwise, pick the animatable drawable.
+     */
+    @Override
+    protected boolean onStateChange(int[] stateSet) {
+        final boolean changed = super.onStateChange(stateSet);
+        int idx = mAnimationScaleListState.getCurrentDrawableIndexBasedOnScale();
+        return selectDrawable(idx) || changed;
+    }
+
+
+    @Override
+    public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
+            @NonNull AttributeSet attrs, @Nullable Theme theme)
+            throws XmlPullParserException, IOException {
+        final TypedArray a = obtainAttributes(r, theme, attrs,
+                R.styleable.AnimationScaleListDrawable);
+        updateDensity(r);
+        a.recycle();
+
+        inflateChildElements(r, parser, attrs, theme);
+
+        onStateChange(getState());
+    }
+
+    /**
+     * Inflates child elements from XML.
+     */
+    private void inflateChildElements(@NonNull Resources r, @NonNull XmlPullParser parser,
+            @NonNull AttributeSet attrs, @Nullable Theme theme)
+            throws XmlPullParserException, IOException {
+        final AnimationScaleListState state = mAnimationScaleListState;
+        final int innerDepth = parser.getDepth() + 1;
+        int type;
+        int depth;
+        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
+                && ((depth = parser.getDepth()) >= innerDepth
+                || type != XmlPullParser.END_TAG)) {
+            if (type != XmlPullParser.START_TAG) {
+                continue;
+            }
+
+            if (depth > innerDepth || !parser.getName().equals("item")) {
+                continue;
+            }
+
+            // Either pick up the android:drawable attribute.
+            final TypedArray a = obtainAttributes(r, theme, attrs,
+                    R.styleable.AnimationScaleListDrawableItem);
+            Drawable dr = a.getDrawable(R.styleable.AnimationScaleListDrawableItem_drawable);
+            a.recycle();
+
+            // Or parse the child element under <item>.
+            if (dr == null) {
+                while ((type = parser.next()) == XmlPullParser.TEXT) {
+                }
+                if (type != XmlPullParser.START_TAG) {
+                    throw new XmlPullParserException(
+                            parser.getPositionDescription()
+                                    + ": <item> tag requires a 'drawable' attribute or "
+                                    + "child tag defining a drawable");
+                }
+                dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
+            }
+
+            state.addDrawable(dr);
+        }
+    }
+
+    @Override
+    public Drawable mutate() {
+        if (!mMutated && super.mutate() == this) {
+            mAnimationScaleListState.mutate();
+            mMutated = true;
+        }
+        return this;
+    }
+
+    @Override
+    public void clearMutated() {
+        super.clearMutated();
+        mMutated = false;
+    }
+
+    @Override
+    public void start() {
+        Drawable dr = getCurrent();
+        if (dr != null && dr instanceof Animatable) {
+            ((Animatable) dr).start();
+        }
+    }
+
+    @Override
+    public void stop() {
+        Drawable dr = getCurrent();
+        if (dr != null && dr instanceof Animatable) {
+            ((Animatable) dr).stop();
+        }
+    }
+
+    @Override
+    public boolean isRunning() {
+        boolean result = false;
+        Drawable dr = getCurrent();
+        if (dr != null && dr instanceof Animatable) {
+            result = ((Animatable) dr).isRunning();
+        }
+        return result;
+    }
+
+    static class AnimationScaleListState extends DrawableContainerState {
+        int[] mThemeAttrs = null;
+        // The index of the last static drawable.
+        int mStaticDrawableIndex = -1;
+        // The index of the last animatable drawable.
+        int mAnimatableDrawableIndex = -1;
+
+        AnimationScaleListState(AnimationScaleListState orig, AnimationScaleListDrawable owner,
+                Resources res) {
+            super(orig, owner, res);
+
+            if (orig != null) {
+                // Perform a shallow copy and rely on mutate() to deep-copy.
+                mThemeAttrs = orig.mThemeAttrs;
+
+                mStaticDrawableIndex = orig.mStaticDrawableIndex;
+                mAnimatableDrawableIndex = orig.mAnimatableDrawableIndex;
+            }
+
+        }
+
+        void mutate() {
+            mThemeAttrs = mThemeAttrs != null ? mThemeAttrs.clone() : null;
+        }
+
+        /**
+         * Add the drawable into the container.
+         * This class only keep track one animatable drawable, and one static. If there are multiple
+         * defined in the XML, then pick the last one.
+         */
+        int addDrawable(Drawable drawable) {
+            final int pos = addChild(drawable);
+            if (drawable instanceof Animatable) {
+                mAnimatableDrawableIndex = pos;
+            } else {
+                mStaticDrawableIndex = pos;
+            }
+            return pos;
+        }
+
+        @Override
+        public Drawable newDrawable() {
+            return new AnimationScaleListDrawable(this, null);
+        }
+
+        @Override
+        public Drawable newDrawable(Resources res) {
+            return new AnimationScaleListDrawable(this, res);
+        }
+
+        @Override
+        public boolean canApplyTheme() {
+            return mThemeAttrs != null || super.canApplyTheme();
+        }
+
+        public int getCurrentDrawableIndexBasedOnScale() {
+            if (ValueAnimator.getDurationScale() == 0) {
+                return mStaticDrawableIndex;
+            }
+            return mAnimatableDrawableIndex;
+        }
+    }
+
+    @Override
+    public void applyTheme(@NonNull Theme theme) {
+        super.applyTheme(theme);
+
+        onStateChange(getState());
+    }
+
+    @Override
+    protected void setConstantState(@NonNull DrawableContainerState state) {
+        super.setConstantState(state);
+
+        if (state instanceof AnimationScaleListState) {
+            mAnimationScaleListState = (AnimationScaleListState) state;
+        }
+    }
+}
+
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/java/com/android/internal/inputmethod/IInputContentUriToken.aidl
similarity index 72%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/java/com/android/internal/inputmethod/IInputContentUriToken.aidl
index 529527b..8abc807 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/java/com/android/internal/inputmethod/IInputContentUriToken.aidl
@@ -1,5 +1,5 @@
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -14,6 +14,14 @@
 ** limitations under the License.
 */
 
-package com.android.internal.app;
+package com.android.internal.inputmethod;
 
-parcelable EphemeralResolveInfo;
+import android.os.IBinder;
+
+/**
+ * {@hide}
+ */
+interface IInputContentUriToken {
+    void take();
+    void release();
+}
diff --git a/core/java/com/android/internal/net/VpnProfile.java b/core/java/com/android/internal/net/VpnProfile.java
index a1f92cd..ab52f2f 100644
--- a/core/java/com/android/internal/net/VpnProfile.java
+++ b/core/java/com/android/internal/net/VpnProfile.java
@@ -176,6 +176,11 @@
      * connection.
      */
     public boolean isValidLockdownProfile() {
+        // b/7064069: lockdown firewall blocks ports that would be used for PPTP
+        if (type == TYPE_PPTP) {
+            return false;
+        }
+
         try {
             InetAddress.parseNumericAddress(server);
 
diff --git a/core/java/com/android/internal/os/BatteryStatsImpl.java b/core/java/com/android/internal/os/BatteryStatsImpl.java
index a1df8c1..3b3344e 100644
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -108,7 +108,7 @@
     private static final int MAGIC = 0xBA757475; // 'BATSTATS'
 
     // Current on-disk Parcel version
-    private static final int VERSION = 147 + (USE_OLD_HISTORY ? 1000 : 0);
+    private static final int VERSION = 150 + (USE_OLD_HISTORY ? 1000 : 0);
 
     // Maximum number of items we will record in the history.
     private static final int MAX_HISTORY_ITEMS = 2000;
@@ -1415,22 +1415,6 @@
             mUnpluggedReportedCount = 0;
             return true;
         }
-
-        @Override
-        public void writeSummaryFromParcelLocked(Parcel out, long batteryRealtime) {
-            super.writeSummaryFromParcelLocked(out, batteryRealtime);
-            out.writeLong(mCurrentReportedTotalTime);
-            out.writeInt(mCurrentReportedCount);
-            out.writeInt(mTrackingReportedValues ? 1 : 0);
-        }
-
-        @Override
-        public void readSummaryFromParcelLocked(Parcel in) {
-            super.readSummaryFromParcelLocked(in);
-            mUnpluggedReportedTotalTime = mCurrentReportedTotalTime = in.readLong();
-            mUnpluggedReportedCount = mCurrentReportedCount = in.readInt();
-            mTrackingReportedValues = in.readInt() == 1;
-        }
     }
 
     /**
@@ -1566,6 +1550,186 @@
         }
     }
 
+
+    /**
+     * A StopwatchTimer that also tracks the total and max individual
+     * time spent active according to the given timebase.  Whereas
+     * StopwatchTimer apportions the time amongst all in the pool,
+     * the total and max durations are not apportioned.
+     */
+    public static class DurationTimer extends StopwatchTimer {
+        /**
+         * The time (in ms) that the timer was last acquired or the time base
+         * last (re-)started. Increasing the nesting depth does not reset this time.
+         *
+         * -1 if the timer is currently not running or the time base is not running.
+         *
+         * If written to a parcel, the start time is reset, as is mNesting in the base class
+         * StopwatchTimer.
+         */
+        long mStartTimeMs = -1;
+
+        /**
+         * The longest time period (in ms) that the timer has been active.
+         */
+        long mMaxDurationMs;
+
+        /**
+         * The total time (in ms) that that the timer has been active since reset().
+         */
+        long mCurrentDurationMs;
+
+        public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
+                TimeBase timeBase, Parcel in) {
+            super(clocks, uid, type, timerPool, timeBase, in);
+            mMaxDurationMs = in.readLong();
+        }
+
+        public DurationTimer(Clocks clocks, Uid uid, int type, ArrayList<StopwatchTimer> timerPool,
+                TimeBase timeBase) {
+            super(clocks, uid, type, timerPool, timeBase);
+        }
+
+        @Override
+        public void writeToParcel(Parcel out, long elapsedRealtimeUs) {
+            super.writeToParcel(out, elapsedRealtimeUs);
+            out.writeLong(mMaxDurationMs);
+        }
+
+        /**
+         * Write the summary to the parcel.
+         *
+         * Since the time base is probably meaningless after we come back, reading
+         * from this will have the effect of stopping the timer. So here all we write
+         * is the max duration.
+         */
+        @Override
+        public void writeSummaryFromParcelLocked(Parcel out, long elapsedRealtimeUs) {
+            super.writeSummaryFromParcelLocked(out, elapsedRealtimeUs);
+            out.writeLong(mMaxDurationMs);
+        }
+
+        /**
+         * Read the summary parcel.
+         *
+         * Has the side effect of stopping the timer.
+         */
+        @Override
+        public void readSummaryFromParcelLocked(Parcel in) {
+            super.readSummaryFromParcelLocked(in);
+            mMaxDurationMs = in.readLong();
+            mStartTimeMs = -1;
+            mCurrentDurationMs = 0;
+        }
+
+        /**
+         * The TimeBase time started (again).
+         *
+         * If the timer is also running, store the start time.
+         */
+        public void onTimeStarted(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
+            super.onTimeStarted(elapsedRealtimeUs, baseUptime, baseRealtime);
+            if (mNesting > 0) {
+                mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
+            }
+        }
+
+        /**
+         * The TimeBase stopped running.
+         *
+         * If the timer is running, add the duration into mCurrentDurationMs.
+         */
+        @Override
+        public void onTimeStopped(long elapsedRealtimeUs, long baseUptime, long baseRealtime) {
+            super.onTimeStopped(elapsedRealtimeUs, baseUptime, baseRealtime);
+            if (mNesting > 0) {
+                mCurrentDurationMs += (elapsedRealtimeUs / 1000) - mStartTimeMs;
+            }
+            mStartTimeMs = -1;
+        }
+
+        @Override
+        public void logState(Printer pw, String prefix) {
+            super.logState(pw, prefix);
+        }
+
+        @Override
+        public void startRunningLocked(long elapsedRealtimeMs) {
+            super.startRunningLocked(elapsedRealtimeMs);
+            if (mNesting == 1 && mTimeBase.isRunning()) {
+                // Just started
+                mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
+            }
+        }
+
+        /**
+         * Decrements the mNesting ref-count on this timer.
+         *
+         * If it actually stopped (mNesting went to 0), then possibly update
+         * mMaxDuration if the current duration was the longest ever.
+         */
+        @Override
+        public void stopRunningLocked(long elapsedRealtimeMs) {
+            super.stopRunningLocked(elapsedRealtimeMs);
+            if (mNesting == 0) {
+                final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
+                if (durationMs > mMaxDurationMs) {
+                    mMaxDurationMs = durationMs;
+                }
+                mStartTimeMs = -1;
+                mCurrentDurationMs = 0;
+            }
+        }
+
+        @Override
+        public boolean reset(boolean detachIfReset) {
+            boolean result = super.reset(detachIfReset);
+            mMaxDurationMs = 0;
+            mCurrentDurationMs = 0;
+            if (mNesting > 0) {
+                mStartTimeMs = mTimeBase.getRealtime(mClocks.elapsedRealtime()*1000) / 1000;
+            } else {
+                mStartTimeMs = -1;
+            }
+            return result;
+        }
+
+        /**
+         * Returns the max duration that this timer has ever seen.
+         *
+         * Note that this time is NOT split between the timers in the timer group that
+         * this timer is attached to.  It is the TOTAL time.
+         */
+        @Override
+        public long getMaxDurationMsLocked(long elapsedRealtimeMs) {
+            if (mNesting > 0) {
+                final long durationMs = getCurrentDurationMsLocked(elapsedRealtimeMs);
+                if (durationMs > mMaxDurationMs) {
+                    return durationMs;
+                }
+            }
+            return mMaxDurationMs;
+        }
+
+        /**
+         * Returns the time since the timer was started.
+         *
+         * Note that this time is NOT split between the timers in the timer group that
+         * this timer is attached to.  It is the TOTAL time.
+         */
+        @Override
+        public long getCurrentDurationMsLocked(long elapsedRealtimeMs) {
+            long durationMs = mCurrentDurationMs;
+            if (mNesting > 0) {
+                if (mTimeBase.isRunning()) {
+                    durationMs += (mTimeBase.getRealtime(elapsedRealtimeMs*1000)/1000)
+                            - mStartTimeMs;
+                }
+            }
+            return durationMs;
+        }
+    }
+
     /**
      * State for keeping track of timing information.
      */
@@ -1791,11 +1955,17 @@
     public abstract class OverflowArrayMap<T> {
         private static final String OVERFLOW_NAME = "*overflow*";
 
+        final int mUid;
         final ArrayMap<String, T> mMap = new ArrayMap<>();
         T mCurOverflow;
         ArrayMap<String, MutableInt> mActiveOverflow;
+        long mLastOverflowTime;
+        long mLastOverflowFinishTime;
+        long mLastClearTime;
+        long mLastCleanupTime;
 
-        public OverflowArrayMap() {
+        public OverflowArrayMap(int uid) {
+            mUid = uid;
         }
 
         public ArrayMap<String, T> getMap() {
@@ -1803,6 +1973,7 @@
         }
 
         public void clear() {
+            mLastClearTime = SystemClock.elapsedRealtime();
             mMap.clear();
             mCurOverflow = null;
             mActiveOverflow = null;
@@ -1819,6 +1990,7 @@
         }
 
         public void cleanup() {
+            mLastCleanupTime = SystemClock.elapsedRealtime();
             if (mActiveOverflow != null) {
                 if (mActiveOverflow.size() == 0) {
                     mActiveOverflow = null;
@@ -1885,6 +2057,7 @@
                     mActiveOverflow = new ArrayMap<>();
                 }
                 mActiveOverflow.put(name, new MutableInt(1));
+                mLastOverflowTime = SystemClock.elapsedRealtime();
                 return obj;
             }
 
@@ -1914,6 +2087,7 @@
                         over.value--;
                         if (over.value <= 0) {
                             mActiveOverflow.remove(name);
+                            mLastOverflowFinishTime = SystemClock.elapsedRealtime();
                         }
                         return obj;
                     }
@@ -1922,9 +2096,35 @@
 
             // Huh, they are stopping an active operation but we can't find one!
             // That's not good.
-            Slog.wtf(TAG, "Unable to find object for " + name + " mapsize="
-                    + mMap.size() + " activeoverflow=" + mActiveOverflow
-                    + " curoverflow=" + mCurOverflow);
+            StringBuilder sb = new StringBuilder();
+            sb.append("Unable to find object for ");
+            sb.append(name);
+            sb.append(" in uid ");
+            sb.append(mUid);
+            sb.append(" mapsize=");
+            sb.append(mMap.size());
+            sb.append(" activeoverflow=");
+            sb.append(mActiveOverflow);
+            sb.append(" curoverflow=");
+            sb.append(mCurOverflow);
+            long now = SystemClock.elapsedRealtime();
+            if (mLastOverflowTime != 0) {
+                sb.append(" lastOverflowTime=");
+                TimeUtils.formatDuration(mLastOverflowTime-now, sb);
+            }
+            if (mLastOverflowFinishTime != 0) {
+                sb.append(" lastOverflowFinishTime=");
+                TimeUtils.formatDuration(mLastOverflowFinishTime-now, sb);
+            }
+            if (mLastClearTime != 0) {
+                sb.append(" lastClearTime=");
+                TimeUtils.formatDuration(mLastClearTime-now, sb);
+            }
+            if (mLastCleanupTime != 0) {
+                sb.append(" lastCleanupTime=");
+                TimeUtils.formatDuration(mLastCleanupTime-now, sb);
+            }
+            Slog.wtf(TAG, sb.toString());
             return null;
         }
 
@@ -3285,6 +3485,36 @@
         }
     }
 
+    public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
+        uid = mapUid(uid);
+        final long elapsedRealtime = mClocks.elapsedRealtime();
+        final long uptime = mClocks.uptimeMillis();
+        if (historyName == null) {
+            historyName = name;
+        }
+        if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_START, historyName, uid,
+                0)) {
+            return;
+        }
+        addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_START,
+                historyName, uid);
+    }
+
+    public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
+        uid = mapUid(uid);
+        final long elapsedRealtime = mClocks.elapsedRealtime();
+        final long uptime = mClocks.uptimeMillis();
+        if (historyName == null) {
+            historyName = name;
+        }
+        if (!mActiveEvents.updateState(HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH, historyName, uid,
+                0)) {
+            return;
+        }
+        addHistoryEventLocked(elapsedRealtime, uptime, HistoryItem.EVENT_LONG_WAKE_LOCK_FINISH,
+                historyName, uid);
+    }
+
     void aggregateLastWakeupUptimeLocked(long uptimeMs) {
         if (mLastWakeupReason != null) {
             long deltaUptime = uptimeMs - mLastWakeupUptimeMs;
@@ -3554,6 +3784,14 @@
         mNumConnectivityChange++;
     }
 
+    private void noteMobileRadioApWakeupLocked(final long elapsedRealtimeMillis,
+            final long uptimeMillis, int uid) {
+        uid = mapUid(uid);
+        addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
+                uid);
+        getUidStatsLocked(uid).noteMobileRadioApWakeupLocked();
+    }
+
     public void noteMobileRadioPowerState(int powerState, long timestampNs, int uid) {
         final long elapsedRealtime = mClocks.elapsedRealtime();
         final long uptime = mClocks.uptimeMillis();
@@ -3563,6 +3801,10 @@
                     powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
                             || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
             if (active) {
+                if (uid > 0) {
+                    noteMobileRadioApWakeupLocked(elapsedRealtime, uptime, uid);
+                }
+
                 mMobileRadioActiveStartTime = realElapsedRealtimeMs = timestampNs / (1000 * 1000);
                 mHistoryCur.states |= HistoryItem.STATE_MOBILE_RADIO_ACTIVE_FLAG;
             } else {
@@ -4239,7 +4481,15 @@
         }
     }
 
-    public void noteWifiRadioPowerState(int powerState, long timestampNs) {
+    private void noteWifiRadioApWakeupLocked(final long elapsedRealtimeMillis,
+            final long uptimeMillis, int uid) {
+        uid = mapUid(uid);
+        addHistoryEventLocked(elapsedRealtimeMillis, uptimeMillis, HistoryItem.EVENT_WAKEUP_AP, "",
+                uid);
+        getUidStatsLocked(uid).noteWifiRadioApWakeupLocked();
+    }
+
+    public void noteWifiRadioPowerState(int powerState, long timestampNs, int uid) {
         final long elapsedRealtime = mClocks.elapsedRealtime();
         final long uptime = mClocks.uptimeMillis();
         if (mWifiRadioPowerState != powerState) {
@@ -4247,6 +4497,9 @@
                     powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_MEDIUM
                             || powerState == DataConnectionRealTimeInfo.DC_POWER_STATE_HIGH;
             if (active) {
+                if (uid > 0) {
+                    noteWifiRadioApWakeupLocked(elapsedRealtime, uptime, uid);
+                }
                 mHistoryCur.states |= HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
             } else {
                 mHistoryCur.states &= ~HistoryItem.STATE_WIFI_RADIO_ACTIVE_FLAG;
@@ -4874,6 +5127,33 @@
         return mUidStats;
     }
 
+    private static void detachTimerIfNotNull(BatteryStatsImpl.Timer timer) {
+        if (timer != null) {
+            timer.detach();
+        }
+    }
+
+    private static boolean resetTimerIfNotNull(BatteryStatsImpl.Timer timer,
+            boolean detachIfReset) {
+        if (timer != null) {
+            return timer.reset(detachIfReset);
+        }
+        return true;
+    }
+
+    private static void detachLongCounterIfNotNull(LongSamplingCounter counter) {
+        if (counter != null) {
+            counter.detach();
+        }
+    }
+
+    private static void resetLongCounterIfNotNull(LongSamplingCounter counter,
+            boolean detachIfReset) {
+        if (counter != null) {
+            counter.reset(detachIfReset);
+        }
+    }
+
     /**
      * The statistics associated with a particular uid.
      */
@@ -4921,6 +5201,16 @@
         LongSamplingCounter mMobileRadioActiveCount;
 
         /**
+         * How many times this UID woke up the Application Processor due to a Mobile radio packet.
+         */
+        private LongSamplingCounter mMobileRadioApWakeupCount;
+
+        /**
+         * How many times this UID woke up the Application Processor due to a Wifi packet.
+         */
+        private LongSamplingCounter mWifiRadioApWakeupCount;
+
+        /**
          * The amount of time this uid has kept the WiFi controller in idle, tx, and rx mode.
          * Can be null if the UID has had no such activity.
          */
@@ -4994,18 +5284,18 @@
             mSystemCpuTime = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
             mCpuPower = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
 
-            mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>() {
+            mWakelockStats = mBsi.new OverflowArrayMap<Wakelock>(uid) {
                 @Override public Wakelock instantiateObject() {
                     return new Wakelock(mBsi, Uid.this);
                 }
             };
-            mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
+            mSyncStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
                 @Override public StopwatchTimer instantiateObject() {
                     return new StopwatchTimer(mBsi.mClocks, Uid.this, SYNC, null,
                             mBsi.mOnBatteryTimeBase);
                 }
             };
-            mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>() {
+            mJobStats = mBsi.new OverflowArrayMap<StopwatchTimer>(uid) {
                 @Override public StopwatchTimer instantiateObject() {
                     return new StopwatchTimer(mBsi.mClocks, Uid.this, JOB, null,
                             mBsi.mOnBatteryTimeBase);
@@ -5629,6 +5919,36 @@
             return 0;
         }
 
+        public void noteMobileRadioApWakeupLocked() {
+            if (mMobileRadioApWakeupCount == null) {
+                mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
+            }
+            mMobileRadioApWakeupCount.addCountLocked(1);
+        }
+
+        @Override
+        public long getMobileRadioApWakeupCount(int which) {
+            if (mMobileRadioApWakeupCount != null) {
+                return mMobileRadioApWakeupCount.getCountLocked(which);
+            }
+            return 0;
+        }
+
+        public void noteWifiRadioApWakeupLocked() {
+            if (mWifiRadioApWakeupCount == null) {
+                mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase);
+            }
+            mWifiRadioApWakeupCount.addCountLocked(1);
+        }
+
+        @Override
+        public long getWifiRadioApWakeupCount(int which) {
+            if (mWifiRadioApWakeupCount != null) {
+                return mWifiRadioApWakeupCount.getCountLocked(which);
+            }
+            return 0;
+        }
+
         void initNetworkActivityLocked() {
             mNetworkByteActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
             mNetworkPacketActivityCounters = new LongSamplingCounter[NUM_NETWORK_ACTIVITY_TYPES];
@@ -5671,24 +5991,14 @@
                 active |= !mWifiMulticastTimer.reset(false);
                 active |= mWifiMulticastEnabled;
             }
-            if (mAudioTurnedOnTimer != null) {
-                active |= !mAudioTurnedOnTimer.reset(false);
-            }
-            if (mVideoTurnedOnTimer != null) {
-                active |= !mVideoTurnedOnTimer.reset(false);
-            }
-            if (mFlashlightTurnedOnTimer != null) {
-                active |= !mFlashlightTurnedOnTimer.reset(false);
-            }
-            if (mCameraTurnedOnTimer != null) {
-                active |= !mCameraTurnedOnTimer.reset(false);
-            }
-            if (mForegroundActivityTimer != null) {
-                active |= !mForegroundActivityTimer.reset(false);
-            }
-            if (mBluetoothScanTimer != null) {
-                active |= !mBluetoothScanTimer.reset(false);
-            }
+
+            active |= !resetTimerIfNotNull(mAudioTurnedOnTimer, false);
+            active |= !resetTimerIfNotNull(mVideoTurnedOnTimer, false);
+            active |= !resetTimerIfNotNull(mFlashlightTurnedOnTimer, false);
+            active |= !resetTimerIfNotNull(mCameraTurnedOnTimer, false);
+            active |= !resetTimerIfNotNull(mForegroundActivityTimer, false);
+            active |= !resetTimerIfNotNull(mBluetoothScanTimer, false);
+
             if (mProcessStateTimer != null) {
                 for (int i = 0; i < NUM_PROCESS_STATE; i++) {
                     if (mProcessStateTimer[i] != null) {
@@ -5749,6 +6059,9 @@
                 }
             }
 
+            resetLongCounterIfNotNull(mMobileRadioApWakeupCount, false);
+            resetLongCounterIfNotNull(mWifiRadioApWakeupCount, false);
+
             final ArrayMap<String, Wakelock> wakeStats = mWakelockStats.getMap();
             for (int iw=wakeStats.size()-1; iw>=0; iw--) {
                 Wakelock wl = wakeStats.valueAt(iw);
@@ -5908,6 +6221,9 @@
                         }
                     }
                 }
+
+                detachLongCounterIfNotNull(mMobileRadioApWakeupCount);
+                detachLongCounterIfNotNull(mWifiRadioApWakeupCount);
             }
 
             return !active;
@@ -6114,6 +6430,20 @@
             } else {
                 out.writeInt(0);
             }
+
+            if (mMobileRadioApWakeupCount != null) {
+                out.writeInt(1);
+                mMobileRadioApWakeupCount.writeToParcel(out);
+            } else {
+                out.writeInt(0);
+            }
+
+            if (mWifiRadioApWakeupCount != null) {
+                out.writeInt(1);
+                mWifiRadioApWakeupCount.writeToParcel(out);
+            } else {
+                out.writeInt(0);
+            }
         }
 
         void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
@@ -6338,6 +6668,18 @@
             } else {
                 mCpuClusterSpeed = null;
             }
+
+            if (in.readInt() != 0) {
+                mMobileRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
+            } else {
+                mMobileRadioApWakeupCount = null;
+            }
+
+            if (in.readInt() != 0) {
+                mWifiRadioApWakeupCount = new LongSamplingCounter(mBsi.mOnBatteryTimeBase, in);
+            } else {
+                mWifiRadioApWakeupCount = null;
+            }
         }
 
         /**
@@ -6357,7 +6699,7 @@
             /**
              * How long (in ms) this uid has been keeping the device partially awake.
              */
-            StopwatchTimer mTimerPartial;
+            DurationTimer mTimerPartial;
 
             /**
              * How long (in ms) this uid has been keeping the device fully awake.
@@ -6386,8 +6728,8 @@
              * @param in the Parcel to be read from.
              * return a new Timer, or null.
              */
-            private StopwatchTimer readTimerFromParcel(int type, ArrayList<StopwatchTimer> pool,
-                    TimeBase timeBase, Parcel in) {
+            private StopwatchTimer readStopwatchTimerFromParcel(int type,
+                    ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
                 if (in.readInt() == 0) {
                     return null;
                 }
@@ -6395,6 +6737,22 @@
                 return new StopwatchTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
             }
 
+            /**
+             * Reads a possibly null Timer from a Parcel.  The timer is associated with the
+             * proper timer pool from the given BatteryStatsImpl object.
+             *
+             * @param in the Parcel to be read from.
+             * return a new Timer, or null.
+             */
+            private DurationTimer readDurationTimerFromParcel(int type,
+                    ArrayList<StopwatchTimer> pool, TimeBase timeBase, Parcel in) {
+                if (in.readInt() == 0) {
+                    return null;
+                }
+
+                return new DurationTimer(mBsi.mClocks, mUid, type, pool, timeBase, in);
+            }
+
             boolean reset() {
                 boolean wlactive = false;
                 if (mTimerFull != null) {
@@ -6431,11 +6789,14 @@
             }
 
             void readFromParcelLocked(TimeBase timeBase, TimeBase screenOffTimeBase, Parcel in) {
-                mTimerPartial = readTimerFromParcel(WAKE_TYPE_PARTIAL,
+                mTimerPartial = readDurationTimerFromParcel(WAKE_TYPE_PARTIAL,
                         mBsi.mPartialTimers, screenOffTimeBase, in);
-                mTimerFull = readTimerFromParcel(WAKE_TYPE_FULL, mBsi.mFullTimers, timeBase, in);
-                mTimerWindow = readTimerFromParcel(WAKE_TYPE_WINDOW, mBsi.mWindowTimers, timeBase, in);
-                mTimerDraw = readTimerFromParcel(WAKE_TYPE_DRAW, mBsi.mDrawTimers, timeBase, in);
+                mTimerFull = readStopwatchTimerFromParcel(WAKE_TYPE_FULL,
+                        mBsi.mFullTimers, timeBase, in);
+                mTimerWindow = readStopwatchTimerFromParcel(WAKE_TYPE_WINDOW,
+                        mBsi.mWindowTimers, timeBase, in);
+                mTimerDraw = readStopwatchTimerFromParcel(WAKE_TYPE_DRAW,
+                        mBsi.mDrawTimers, timeBase, in);
             }
 
             void writeToParcelLocked(Parcel out, long elapsedRealtimeUs) {
@@ -6457,40 +6818,43 @@
             }
 
             public StopwatchTimer getStopwatchTimer(int type) {
-                StopwatchTimer t;
                 switch (type) {
-                    case WAKE_TYPE_PARTIAL:
-                        t = mTimerPartial;
+                    case WAKE_TYPE_PARTIAL: {
+                        DurationTimer t = mTimerPartial;
                         if (t == null) {
-                            t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
+                            t = new DurationTimer(mBsi.mClocks, mUid, WAKE_TYPE_PARTIAL,
                                     mBsi.mPartialTimers, mBsi.mOnBatteryScreenOffTimeBase);
                             mTimerPartial = t;
                         }
                         return t;
-                    case WAKE_TYPE_FULL:
-                        t = mTimerFull;
+                    }
+                    case WAKE_TYPE_FULL: {
+                        StopwatchTimer t = mTimerFull;
                         if (t == null) {
                             t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_FULL,
                                     mBsi.mFullTimers, mBsi.mOnBatteryTimeBase);
                             mTimerFull = t;
                         }
                         return t;
-                    case WAKE_TYPE_WINDOW:
-                        t = mTimerWindow;
+                    }
+                    case WAKE_TYPE_WINDOW: {
+                        StopwatchTimer t = mTimerWindow;
                         if (t == null) {
                             t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_WINDOW,
                                     mBsi.mWindowTimers, mBsi.mOnBatteryTimeBase);
                             mTimerWindow = t;
                         }
                         return t;
-                    case WAKE_TYPE_DRAW:
-                        t = mTimerDraw;
+                    }
+                    case WAKE_TYPE_DRAW: {
+                        StopwatchTimer t = mTimerDraw;
                         if (t == null) {
                             t = new StopwatchTimer(mBsi.mClocks, mUid, WAKE_TYPE_DRAW,
                                     mBsi.mDrawTimers, mBsi.mOnBatteryTimeBase);
                             mTimerDraw = t;
                         }
                         return t;
+                    }
                     default:
                         throw new IllegalArgumentException("type=" + type);
                 }
@@ -10348,8 +10712,22 @@
                 u.mCpuClusterSpeed = null;
             }
 
+            if (in.readInt() != 0) {
+                u.mMobileRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
+                u.mMobileRadioApWakeupCount.readSummaryFromParcelLocked(in);
+            } else {
+                u.mMobileRadioApWakeupCount = null;
+            }
+
+            if (in.readInt() != 0) {
+                u.mWifiRadioApWakeupCount = new LongSamplingCounter(mOnBatteryTimeBase);
+                u.mWifiRadioApWakeupCount.readSummaryFromParcelLocked(in);
+            } else {
+                u.mWifiRadioApWakeupCount = null;
+            }
+
             int NW = in.readInt();
-            if (NW > 100) {
+            if (NW > (MAX_WAKELOCKS_PER_UID+1)) {
                 throw new ParcelFormatException("File corrupt: too many wake locks " + NW);
             }
             for (int iw = 0; iw < NW; iw++) {
@@ -10358,7 +10736,7 @@
             }
 
             int NS = in.readInt();
-            if (NS > 100) {
+            if (NS > (MAX_WAKELOCKS_PER_UID+1)) {
                 throw new ParcelFormatException("File corrupt: too many syncs " + NS);
             }
             for (int is = 0; is < NS; is++) {
@@ -10367,7 +10745,7 @@
             }
 
             int NJ = in.readInt();
-            if (NJ > 100) {
+            if (NJ > (MAX_WAKELOCKS_PER_UID+1)) {
                 throw new ParcelFormatException("File corrupt: too many job timers " + NJ);
             }
             for (int ij = 0; ij < NJ; ij++) {
@@ -10708,6 +11086,20 @@
                 out.writeInt(0);
             }
 
+            if (u.mMobileRadioApWakeupCount != null) {
+                out.writeInt(1);
+                u.mMobileRadioApWakeupCount.writeSummaryFromParcelLocked(out);
+            } else {
+                out.writeInt(0);
+            }
+
+            if (u.mWifiRadioApWakeupCount != null) {
+                out.writeInt(1);
+                u.mWifiRadioApWakeupCount.writeSummaryFromParcelLocked(out);
+            } else {
+                out.writeInt(0);
+            }
+
             final ArrayMap<String, Uid.Wakelock> wakeStats = u.mWakelockStats.getMap();
             int NW = wakeStats.size();
             out.writeInt(NW);
diff --git a/core/java/com/android/internal/os/KernelUidCpuTimeReader.java b/core/java/com/android/internal/os/KernelUidCpuTimeReader.java
index c828d11..e8919ed 100644
--- a/core/java/com/android/internal/os/KernelUidCpuTimeReader.java
+++ b/core/java/com/android/internal/os/KernelUidCpuTimeReader.java
@@ -120,7 +120,7 @@
                             sb.append(" s=");
                             TimeUtils.formatDuration(systemTimeDeltaUs / 1000, sb);
                             sb.append(" p=").append(powerDeltaMaUs / 1000).append("mAms");
-                            Slog.wtf(TAG, sb.toString());
+                            Slog.e(TAG, sb.toString());
 
                             userTimeDeltaUs = 0;
                             systemTimeDeltaUs = 0;
diff --git a/core/java/com/android/internal/os/ProcessCpuTracker.java b/core/java/com/android/internal/os/ProcessCpuTracker.java
index b7e5718..e46dfc4 100644
--- a/core/java/com/android/internal/os/ProcessCpuTracker.java
+++ b/core/java/com/android/internal/os/ProcessCpuTracker.java
@@ -39,6 +39,7 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.Date;
+import java.util.List;
 import java.util.StringTokenizer;
 
 public class ProcessCpuTracker {
@@ -177,6 +178,11 @@
 
     private byte[] mBuffer = new byte[4096];
 
+    public interface FilterStats {
+        /** Which stats to pick when filtering */
+        boolean needed(Stats stats);
+    }
+
     public static class Stats {
         public final int pid;
         public final int uid;
@@ -695,6 +701,18 @@
         return mProcStats.get(index);
     }
 
+    final public List<Stats> getStats(FilterStats filter) {
+        final ArrayList<Stats> statses = new ArrayList<>(mProcStats.size());
+        final int N = mProcStats.size();
+        for (int p = 0; p < N; p++) {
+            Stats stats = mProcStats.get(p);
+            if (filter.needed(stats)) {
+                statses.add(stats);
+            }
+        }
+        return statses;
+    }
+
     final public int countWorkingStats() {
         buildWorkingProcs();
         return mWorkingProcs.size();
diff --git a/core/java/com/android/internal/os/ZygoteInit.java b/core/java/com/android/internal/os/ZygoteInit.java
index 93205d0..5393118 100644
--- a/core/java/com/android/internal/os/ZygoteInit.java
+++ b/core/java/com/android/internal/os/ZygoteInit.java
@@ -586,21 +586,42 @@
                 // System server is fully AOTed and never profiled
                 // for profile guided compilation.
                 // TODO: Make this configurable between INTERPRET_ONLY, SPEED, SPACE and EVERYTHING?
-                final int dexoptNeeded = DexFile.getDexOptNeeded(
+
+                int dexoptNeeded;
+                try {
+                    dexoptNeeded = DexFile.getDexOptNeeded(
                         classPathElement, instructionSet, "speed",
                         false /* newProfile */);
-                if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
-                    installer.dexopt(classPathElement, Process.SYSTEM_UID, instructionSet,
-                            dexoptNeeded, 0 /*dexFlags*/, "speed", null /*volumeUuid*/,
-                            sharedLibraries);
+                } catch (FileNotFoundException ignored) {
+                    // Do not add to the classpath.
+                    Log.w(TAG, "Missing classpath element for system server: " + classPathElement);
+                    continue;
+                } catch (IOException e) {
+                    // Not fully clear what to do here as we don't know the cause of the
+                    // IO exception. Add to the classpath to be conservative, but don't
+                    // attempt to compile it.
+                    Log.w(TAG, "Error checking classpath element for system server: "
+                            + classPathElement, e);
+                    dexoptNeeded = DexFile.NO_DEXOPT_NEEDED;
                 }
+
+                if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
+                    try {
+                        installer.dexopt(classPathElement, Process.SYSTEM_UID, instructionSet,
+                                dexoptNeeded, 0 /*dexFlags*/, "speed", null /*volumeUuid*/,
+                                sharedLibraries);
+                    } catch (InstallerException e) {
+                        // Ignore (but log), we need this on the classpath for fallback mode.
+                        Log.w(TAG, "Failed compiling classpath element for system server: "
+                                + classPathElement, e);
+                    }
+                }
+
                 if (!sharedLibraries.isEmpty()) {
                     sharedLibraries += ":";
                 }
                 sharedLibraries += classPathElement;
             }
-        } catch (IOException | InstallerException e) {
-            throw new RuntimeException("Error starting system_server", e);
         } finally {
             installer.disconnect();
         }
@@ -761,7 +782,7 @@
             closeServerSocket();
         } catch (MethodAndArgsCaller caller) {
             caller.run();
-        } catch (RuntimeException ex) {
+        } catch (Throwable ex) {
             Log.e(TAG, "Zygote died with exception", ex);
             closeServerSocket();
             throw ex;
diff --git a/core/java/com/android/internal/policy/BackdropFrameRenderer.java b/core/java/com/android/internal/policy/BackdropFrameRenderer.java
index 488f769..1abb59b 100644
--- a/core/java/com/android/internal/policy/BackdropFrameRenderer.java
+++ b/core/java/com/android/internal/policy/BackdropFrameRenderer.java
@@ -110,12 +110,15 @@
             int statusBarColor, int navigationBarColor) {
         mDecorView = decorView;
         mResizingBackgroundDrawable = resizingBackgroundDrawable != null
+                        && resizingBackgroundDrawable.getConstantState() != null
                 ? resizingBackgroundDrawable.getConstantState().newDrawable()
                 : null;
         mCaptionBackgroundDrawable = captionBackgroundDrawableDrawable != null
+                        && captionBackgroundDrawableDrawable.getConstantState() != null
                 ? captionBackgroundDrawableDrawable.getConstantState().newDrawable()
                 : null;
         mUserCaptionBackgroundDrawable = userCaptionBackgroundDrawable != null
+                        && userCaptionBackgroundDrawable.getConstantState() != null
                 ? userCaptionBackgroundDrawable.getConstantState().newDrawable()
                 : null;
         if (mCaptionBackgroundDrawable == null) {
@@ -371,6 +374,8 @@
                 systemInsets.bottom);
         final int rightInset = DecorView.getColorViewRightInset(stableInsets.right,
                 systemInsets.right);
+        final int leftInset = DecorView.getColorViewLeftInset(stableInsets.left,
+                systemInsets.left);
         if (mStatusBarColor != null) {
             mStatusBarColor.setBounds(0, 0, left + width, topInset);
             mStatusBarColor.draw(canvas);
@@ -380,9 +385,11 @@
         // don't want the navigation bar background be moving around when resizing in docked mode.
         // However, we need it for the transitions into/out of docked mode.
         if (mNavigationBarColor != null && fullscreen) {
-            final int size = DecorView.getNavBarSize(bottomInset, rightInset);
+            final int size = DecorView.getNavBarSize(bottomInset, rightInset, leftInset);
             if (DecorView.isNavBarToRightEdge(bottomInset, rightInset)) {
                 mNavigationBarColor.setBounds(width - size, 0, width, height);
+            } else if (DecorView.isNavBarToLeftEdge(bottomInset, leftInset)) {
+                mNavigationBarColor.setBounds(0, 0, size, height);
             } else {
                 mNavigationBarColor.setBounds(0, height - size, width, height);
             }
diff --git a/core/java/com/android/internal/policy/DecorView.java b/core/java/com/android/internal/policy/DecorView.java
index 3cf7a4e..92ab324 100644
--- a/core/java/com/android/internal/policy/DecorView.java
+++ b/core/java/com/android/internal/policy/DecorView.java
@@ -97,11 +97,11 @@
 import static android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN;
 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
-import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
 import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
 import static android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
 import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
+import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
 import static com.android.internal.policy.PhoneWindow.FEATURE_OPTIONS_PANEL;
 
 /** @hide */
@@ -162,13 +162,13 @@
 
     private final ColorViewState mStatusColorViewState = new ColorViewState(
             SYSTEM_UI_FLAG_FULLSCREEN, FLAG_TRANSLUCENT_STATUS,
-            Gravity.TOP, Gravity.LEFT,
+            Gravity.TOP, Gravity.LEFT, Gravity.RIGHT,
             Window.STATUS_BAR_BACKGROUND_TRANSITION_NAME,
             com.android.internal.R.id.statusBarBackground,
             FLAG_FULLSCREEN);
     private final ColorViewState mNavigationColorViewState = new ColorViewState(
             SYSTEM_UI_FLAG_HIDE_NAVIGATION, FLAG_TRANSLUCENT_NAVIGATION,
-            Gravity.BOTTOM, Gravity.RIGHT,
+            Gravity.BOTTOM, Gravity.RIGHT, Gravity.LEFT,
             Window.NAVIGATION_BAR_BACKGROUND_TRANSITION_NAME,
             com.android.internal.R.id.navigationBarBackground,
             0 /* hideWindowFlag */);
@@ -184,9 +184,11 @@
     private int mLastTopInset = 0;
     private int mLastBottomInset = 0;
     private int mLastRightInset = 0;
+    private int mLastLeftInset = 0;
     private boolean mLastHasTopStableInset = false;
     private boolean mLastHasBottomStableInset = false;
     private boolean mLastHasRightStableInset = false;
+    private boolean mLastHasLeftStableInset = false;
     private int mLastWindowFlags = 0;
     private boolean mLastShouldAlwaysConsumeNavBar = false;
 
@@ -991,12 +993,21 @@
         return Math.min(stableRight, systemRight);
     }
 
+    static int getColorViewLeftInset(int stableLeft, int systemLeft) {
+        return Math.min(stableLeft, systemLeft);
+    }
+
     static boolean isNavBarToRightEdge(int bottomInset, int rightInset) {
         return bottomInset == 0 && rightInset > 0;
     }
 
-    static int getNavBarSize(int bottomInset, int rightInset) {
-        return isNavBarToRightEdge(bottomInset, rightInset) ? rightInset : bottomInset;
+    static boolean isNavBarToLeftEdge(int bottomInset, int leftInset) {
+        return bottomInset == 0 && leftInset > 0;
+    }
+
+    static int getNavBarSize(int bottomInset, int rightInset, int leftInset) {
+        return isNavBarToRightEdge(bottomInset, rightInset) ? rightInset
+                : isNavBarToLeftEdge(bottomInset, leftInset) ? leftInset : bottomInset;
     }
 
     WindowInsets updateColorViews(WindowInsets insets, boolean animate) {
@@ -1016,6 +1027,8 @@
                         insets.getSystemWindowInsetBottom());
                 mLastRightInset = getColorViewRightInset(insets.getStableInsetRight(),
                         insets.getSystemWindowInsetRight());
+                mLastLeftInset = getColorViewRightInset(insets.getStableInsetLeft(),
+                        insets.getSystemWindowInsetLeft());
 
                 // Don't animate if the presence of stable insets has changed, because that
                 // indicates that the window was either just added and received them for the
@@ -1031,21 +1044,32 @@
                 boolean hasRightStableInset = insets.getStableInsetRight() != 0;
                 disallowAnimate |= (hasRightStableInset != mLastHasRightStableInset);
                 mLastHasRightStableInset = hasRightStableInset;
+
+                boolean hasLeftStableInset = insets.getStableInsetLeft() != 0;
+                disallowAnimate |= (hasLeftStableInset != mLastHasLeftStableInset);
+                mLastHasLeftStableInset = hasLeftStableInset;
+
                 mLastShouldAlwaysConsumeNavBar = insets.shouldAlwaysConsumeNavBar();
             }
 
             boolean navBarToRightEdge = isNavBarToRightEdge(mLastBottomInset, mLastRightInset);
-            int navBarSize = getNavBarSize(mLastBottomInset, mLastRightInset);
+            boolean navBarToLeftEdge = isNavBarToLeftEdge(mLastBottomInset, mLastLeftInset);
+            int navBarSize = getNavBarSize(mLastBottomInset, mLastRightInset, mLastLeftInset);
             updateColorViewInt(mNavigationColorViewState, sysUiVisibility,
-                    mWindow.mNavigationBarColor, navBarSize, navBarToRightEdge,
-                    0 /* rightInset */, animate && !disallowAnimate, false /* force */);
+                    mWindow.mNavigationBarColor, navBarSize, navBarToRightEdge || navBarToLeftEdge,
+                    navBarToLeftEdge,
+                    0 /* sideInset */, animate && !disallowAnimate, false /* force */);
 
             boolean statusBarNeedsRightInset = navBarToRightEdge
                     && mNavigationColorViewState.present;
-            int statusBarRightInset = statusBarNeedsRightInset ? mLastRightInset : 0;
+            boolean statusBarNeedsLeftInset = navBarToLeftEdge
+                    && mNavigationColorViewState.present;
+            int statusBarSideInset = statusBarNeedsRightInset ? mLastRightInset
+                    : statusBarNeedsLeftInset ? mLastLeftInset : 0;
             updateColorViewInt(mStatusColorViewState, sysUiVisibility,
                     calculateStatusBarColor(), mLastTopInset,
-                    false /* matchVertical */, statusBarRightInset, animate && !disallowAnimate,
+                    false /* matchVertical */, statusBarNeedsLeftInset, statusBarSideInset,
+                    animate && !disallowAnimate,
                     mForceWindowDrawsStatusBarBackground);
         }
 
@@ -1070,15 +1094,17 @@
         int consumedTop = consumingStatusBar ? mLastTopInset : 0;
         int consumedRight = consumingNavBar ? mLastRightInset : 0;
         int consumedBottom = consumingNavBar ? mLastBottomInset : 0;
+        int consumedLeft = consumingNavBar ? mLastLeftInset : 0;
 
         if (mContentRoot != null
                 && mContentRoot.getLayoutParams() instanceof MarginLayoutParams) {
             MarginLayoutParams lp = (MarginLayoutParams) mContentRoot.getLayoutParams();
             if (lp.topMargin != consumedTop || lp.rightMargin != consumedRight
-                    || lp.bottomMargin != consumedBottom) {
+                    || lp.bottomMargin != consumedBottom || lp.leftMargin != consumedLeft) {
                 lp.topMargin = consumedTop;
                 lp.rightMargin = consumedRight;
                 lp.bottomMargin = consumedBottom;
+                lp.leftMargin = consumedLeft;
                 mContentRoot.setLayoutParams(lp);
 
                 if (insets == null) {
@@ -1089,7 +1115,7 @@
             }
             if (insets != null) {
                 insets = insets.replaceSystemWindowInsets(
-                        insets.getSystemWindowInsetLeft(),
+                        insets.getSystemWindowInsetLeft() - consumedLeft,
                         insets.getSystemWindowInsetTop() - consumedTop,
                         insets.getSystemWindowInsetRight() - consumedRight,
                         insets.getSystemWindowInsetBottom() - consumedBottom);
@@ -1126,11 +1152,12 @@
      * @param size the current size in the non-parent-matching dimension.
      * @param verticalBar if true the view is attached to a vertical edge, otherwise to a
      *                    horizontal edge,
-     * @param rightMargin rightMargin for the color view.
+     * @param sideMargin sideMargin for the color view.
      * @param animate if true, the change will be animated.
      */
     private void updateColorViewInt(final ColorViewState state, int sysUiVis, int color,
-            int size, boolean verticalBar, int rightMargin, boolean animate, boolean force) {
+            int size, boolean verticalBar, boolean seascape, int sideMargin,
+            boolean animate, boolean force) {
         state.present = (sysUiVis & state.systemUiHideFlag) == 0
                 && (mWindow.getAttributes().flags & state.hideWindowFlag) == 0
                 && ((mWindow.getAttributes().flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0
@@ -1145,7 +1172,9 @@
 
         int resolvedHeight = verticalBar ? LayoutParams.MATCH_PARENT : size;
         int resolvedWidth = verticalBar ? size : LayoutParams.MATCH_PARENT;
-        int resolvedGravity = verticalBar ? state.horizontalGravity : state.verticalGravity;
+        int resolvedGravity = verticalBar
+                ? (seascape ? state.seascapeGravity : state.horizontalGravity)
+                : state.verticalGravity;
 
         if (view == null) {
             if (showView) {
@@ -1159,7 +1188,11 @@
 
                 LayoutParams lp = new LayoutParams(resolvedWidth, resolvedHeight,
                         resolvedGravity);
-                lp.rightMargin = rightMargin;
+                if (seascape) {
+                    lp.leftMargin = sideMargin;
+                } else {
+                    lp.rightMargin = sideMargin;
+                }
                 addView(view, lp);
                 updateColorViewTranslations();
             }
@@ -1168,12 +1201,16 @@
             visibilityChanged = state.targetVisibility != vis;
             state.targetVisibility = vis;
             LayoutParams lp = (LayoutParams) view.getLayoutParams();
+            int rightMargin = seascape ? 0 : sideMargin;
+            int leftMargin = seascape ? sideMargin : 0;
             if (lp.height != resolvedHeight || lp.width != resolvedWidth
-                    || lp.gravity != resolvedGravity || lp.rightMargin != rightMargin) {
+                    || lp.gravity != resolvedGravity || lp.rightMargin != rightMargin
+                    || lp.leftMargin != leftMargin) {
                 lp.height = resolvedHeight;
                 lp.width = resolvedWidth;
                 lp.gravity = resolvedGravity;
                 lp.rightMargin = rightMargin;
+                lp.leftMargin = leftMargin;
                 view.setLayoutParams(lp);
             }
             if (showView) {
@@ -1824,7 +1861,7 @@
         }
         final WindowManager.LayoutParams attrs = mWindow.getAttributes();
         final boolean isApplication = attrs.type == TYPE_BASE_APPLICATION ||
-                attrs.type == TYPE_APPLICATION;
+                attrs.type == TYPE_APPLICATION || attrs.type == TYPE_DRAWN_APPLICATION;
         // Only a non floating application window on one of the allowed workspaces can get a caption
         if (!mWindow.isFloating() && isApplication && StackId.hasWindowDecor(mStackId)) {
             // Dependent on the brightness of the used title we either use the
@@ -2217,17 +2254,19 @@
         final int translucentFlag;
         final int verticalGravity;
         final int horizontalGravity;
+        final int seascapeGravity;
         final String transitionName;
         final int hideWindowFlag;
 
         ColorViewState(int systemUiHideFlag,
                 int translucentFlag, int verticalGravity, int horizontalGravity,
-                String transitionName, int id, int hideWindowFlag) {
+                int seascapeGravity, String transitionName, int id, int hideWindowFlag) {
             this.id = id;
             this.systemUiHideFlag = systemUiHideFlag;
             this.translucentFlag = translucentFlag;
             this.verticalGravity = verticalGravity;
             this.horizontalGravity = horizontalGravity;
+            this.seascapeGravity = seascapeGravity;
             this.transitionName = transitionName;
             this.hideWindowFlag = hideWindowFlag;
         }
diff --git a/core/java/com/android/internal/policy/EmergencyAffordanceManager.java b/core/java/com/android/internal/policy/EmergencyAffordanceManager.java
new file mode 100644
index 0000000..bed7c1ba
--- /dev/null
+++ b/core/java/com/android/internal/policy/EmergencyAffordanceManager.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.internal.policy;
+
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Build;
+import android.provider.Settings;
+
+/**
+ * A class that manages emergency affordances and enables immediate calling to emergency services
+ */
+public class EmergencyAffordanceManager {
+
+    public static final boolean ENABLED = true;
+
+    /**
+     * Global setting override with the number to call with the emergency affordance.
+     * @hide
+     */
+    private static final String EMERGENCY_CALL_NUMBER_SETTING = "emergency_affordance_number";
+
+    /**
+     * Global setting, whether the emergency affordance should be shown regardless of device state.
+     * The value is a boolean (1 or 0).
+     * @hide
+     */
+    private static final String FORCE_EMERGENCY_AFFORDANCE_SETTING = "force_emergency_affordance";
+
+    private final Context mContext;
+
+    public EmergencyAffordanceManager(Context context) {
+        mContext = context;
+    }
+
+    /**
+     * perform an emergency call.
+     */
+    public final void performEmergencyCall() {
+        performEmergencyCall(mContext);
+    }
+
+    private static Uri getPhoneUri(Context context) {
+        String number = context.getResources().getString(
+                com.android.internal.R.string.config_emergency_call_number);
+        if (Build.IS_DEBUGGABLE) {
+            String override = Settings.Global.getString(
+                    context.getContentResolver(), EMERGENCY_CALL_NUMBER_SETTING);
+            if (override != null) {
+                number = override;
+            }
+        }
+        return Uri.fromParts("tel", number, null);
+    }
+
+    private static void performEmergencyCall(Context context) {
+        Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY);
+        intent.setData(getPhoneUri(context));
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        context.startActivity(intent);
+    }
+
+    /**
+     * @return whether emergency affordance should be active.
+     */
+    public boolean needsEmergencyAffordance() {
+        if (!ENABLED) {
+            return false;
+        }
+        if (forceShowing()) {
+            return true;
+        }
+        return isEmergencyAffordanceNeeded();
+    }
+
+    private boolean isEmergencyAffordanceNeeded() {
+        return Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.EMERGENCY_AFFORDANCE_NEEDED, 0) != 0;
+    }
+
+
+    private boolean forceShowing() {
+        return Settings.Global.getInt(mContext.getContentResolver(),
+                FORCE_EMERGENCY_AFFORDANCE_SETTING, 0) != 0;
+    }
+}
diff --git a/core/java/com/android/internal/policy/IKeyguardService.aidl b/core/java/com/android/internal/policy/IKeyguardService.aidl
index 171a264..e51ad3f 100644
--- a/core/java/com/android/internal/policy/IKeyguardService.aidl
+++ b/core/java/com/android/internal/policy/IKeyguardService.aidl
@@ -28,13 +28,14 @@
      * FLAG_SHOW_ON_LOCK_SCREEN.
      *
      * @param isOccluded Whether the Keyguard is occluded by another window.
+     * @param animate Whether to play an animation for the state change.
      */
-    void setOccluded(boolean isOccluded);
+    void setOccluded(boolean isOccluded, boolean animate);
 
     void addStateMonitorCallback(IKeyguardStateCallback callback);
     void verifyUnlock(IKeyguardExitCallback callback);
     void keyguardDone(boolean authenticated, boolean wakeup);
-    void dismiss();
+    void dismiss(boolean allowWhileOccluded);
     void onDreamingStarted();
     void onDreamingStopped();
 
diff --git a/core/java/com/android/internal/policy/IKeyguardStateCallback.aidl b/core/java/com/android/internal/policy/IKeyguardStateCallback.aidl
index db3b40b..8e454db 100644
--- a/core/java/com/android/internal/policy/IKeyguardStateCallback.aidl
+++ b/core/java/com/android/internal/policy/IKeyguardStateCallback.aidl
@@ -19,4 +19,6 @@
     void onShowingStateChanged(boolean showing);
     void onSimSecureStateChanged(boolean simSecure);
     void onInputRestrictedStateChanged(boolean inputRestricted);
+    void onTrustedChanged(boolean trusted);
+    void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper);
 }
\ No newline at end of file
diff --git a/core/java/com/android/internal/policy/PhoneFallbackEventHandler.java b/core/java/com/android/internal/policy/PhoneFallbackEventHandler.java
index 2cb9c25..fb0edea 100644
--- a/core/java/com/android/internal/policy/PhoneFallbackEventHandler.java
+++ b/core/java/com/android/internal/policy/PhoneFallbackEventHandler.java
@@ -150,6 +150,7 @@
                         sendCloseSystemWindows();
                         // Broadcast an intent that the Camera button was longpressed
                         Intent intent = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
+                        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
                         intent.putExtra(Intent.EXTRA_KEY_EVENT, event);
                         mContext.sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF,
                                 null, null, null, 0, null, null);
diff --git a/core/java/com/android/internal/policy/PhoneWindow.java b/core/java/com/android/internal/policy/PhoneWindow.java
index 9ad750d..878f3a6 100644
--- a/core/java/com/android/internal/policy/PhoneWindow.java
+++ b/core/java/com/android/internal/policy/PhoneWindow.java
@@ -92,8 +92,6 @@
 import android.util.Log;
 import android.util.SparseArray;
 import android.util.TypedValue;
-import android.view.accessibility.AccessibilityNodeInfo;
-import android.view.accessibility.AccessibilityNodeProvider;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.widget.FrameLayout;
@@ -2057,9 +2055,6 @@
     }
 
     static private final String FOCUSED_ID_TAG = "android:focusedViewId";
-    static private final String ACCESSIBILITY_FOCUSED_ID_TAG = "android:accessibilityFocusedViewId";
-    static private final String ACCESSIBILITY_FOCUSED_VIRTUAL_ID_TAG =
-            "android:accessibilityFocusedVirtualViewId";
     static private final String VIEWS_TAG = "android:views";
     static private final String PANELS_TAG = "android:Panels";
     static private final String ACTION_BAR_TAG = "android:ActionBar";
@@ -2082,26 +2077,6 @@
             outState.putInt(FOCUSED_ID_TAG, focusedView.getId());
         }
 
-        // Save the accessibility focused view ID.
-        if (mDecor != null) {
-            final ViewRootImpl viewRootImpl = mDecor.getViewRootImpl();
-            if (viewRootImpl != null) {
-                final View accessFocusHost = viewRootImpl.getAccessibilityFocusedHost();
-                if (accessFocusHost != null && accessFocusHost.getId() != View.NO_ID) {
-                    outState.putInt(ACCESSIBILITY_FOCUSED_ID_TAG, accessFocusHost.getId());
-
-                    // If we have a focused virtual node ID, save that too.
-                    final AccessibilityNodeInfo accessFocusedNode =
-                            viewRootImpl.getAccessibilityFocusedVirtualView();
-                    if (accessFocusedNode != null) {
-                        final int virtualNodeId = AccessibilityNodeInfo.getVirtualDescendantId(
-                                accessFocusedNode.getSourceNodeId());
-                        outState.putInt(ACCESSIBILITY_FOCUSED_VIRTUAL_ID_TAG, virtualNodeId);
-                    }
-                }
-            }
-        }
-
         // save the panels
         SparseArray<Parcelable> panelStates = new SparseArray<Parcelable>();
         savePanelState(panelStates);
@@ -2144,13 +2119,6 @@
             }
         }
 
-        // Restore the accessibility focused view.
-        final int accessFocusHostViewId = savedInstanceState.getInt(
-                ACCESSIBILITY_FOCUSED_ID_TAG, View.NO_ID);
-        final int accessFocusVirtualViewId = savedInstanceState.getInt(
-                ACCESSIBILITY_FOCUSED_VIRTUAL_ID_TAG, AccessibilityNodeInfo.UNDEFINED_ITEM_ID);
-        tryRestoreAccessibilityFocus(accessFocusHostViewId, accessFocusVirtualViewId);
-
         // Restore the panels.
         SparseArray<Parcelable> panelStates = savedInstanceState.getSparseParcelableArray(PANELS_TAG);
         if (panelStates != null) {
@@ -2170,33 +2138,6 @@
         }
     }
 
-    private void tryRestoreAccessibilityFocus(int hostViewId, int virtualViewId) {
-        if (hostViewId != View.NO_ID && mDecor != null) {
-            final View needsAccessFocus = mDecor.findViewById(hostViewId);
-            if (needsAccessFocus != null) {
-                if (!tryFocusingVirtualView(needsAccessFocus, virtualViewId)
-                        && !needsAccessFocus.requestAccessibilityFocus()) {
-                    Log.w(TAG, "Failed to restore focus to previously accessibility"
-                            + " focused view with id " + hostViewId);
-                }
-            } else {
-                Log.w(TAG, "Previously accessibility focused view reported id " + hostViewId
-                        + " during save, but can't be found during restore.");
-            }
-        }
-    }
-
-    private boolean tryFocusingVirtualView(View host, int virtualViewId) {
-        if (virtualViewId != AccessibilityNodeInfo.UNDEFINED_ITEM_ID) {
-            final AccessibilityNodeProvider nodeProvider = host.getAccessibilityNodeProvider();
-            if (nodeProvider != null) {
-                return nodeProvider.performAction(virtualViewId,
-                        AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
-            }
-        }
-        return false;
-    }
-
     /**
      * Invoked when the panels should freeze their state.
      *
diff --git a/core/java/com/android/internal/statusbar/IStatusBar.aidl b/core/java/com/android/internal/statusbar/IStatusBar.aidl
index 7706ff7..20f10b3 100644
--- a/core/java/com/android/internal/statusbar/IStatusBar.aidl
+++ b/core/java/com/android/internal/statusbar/IStatusBar.aidl
@@ -112,4 +112,5 @@
     void addQsTile(in ComponentName tile);
     void remQsTile(in ComponentName tile);
     void clickQsTile(in ComponentName tile);
+    void handleSystemNavigationKey(in int key);
 }
diff --git a/core/java/com/android/internal/statusbar/IStatusBarService.aidl b/core/java/com/android/internal/statusbar/IStatusBarService.aidl
index 3d05422..698e387 100644
--- a/core/java/com/android/internal/statusbar/IStatusBarService.aidl
+++ b/core/java/com/android/internal/statusbar/IStatusBarService.aidl
@@ -66,4 +66,5 @@
     void addTile(in ComponentName tile);
     void remTile(in ComponentName tile);
     void clickTile(in ComponentName tile);
+    void handleSystemNavigationKey(in int key);
 }
diff --git a/core/java/com/android/internal/util/MimeIconUtils.java b/core/java/com/android/internal/util/MimeIconUtils.java
new file mode 100644
index 0000000..841ec7c
--- /dev/null
+++ b/core/java/com/android/internal/util/MimeIconUtils.java
@@ -0,0 +1,230 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.util;
+
+import android.content.Context;
+import android.graphics.drawable.Drawable;
+import android.provider.DocumentsContract;
+
+import com.android.internal.R;
+
+import java.util.HashMap;
+
+public class MimeIconUtils {
+
+    private static HashMap<String, Integer> sMimeIcons = new HashMap<>();
+
+    private static void add(String mimeType, int resId) {
+        if (sMimeIcons.put(mimeType, resId) != null) {
+            throw new RuntimeException(mimeType + " already registered!");
+        }
+    }
+
+    static {
+        int icon;
+
+        // Package
+        icon = R.drawable.ic_doc_apk;
+        add("application/vnd.android.package-archive", icon);
+
+        // Audio
+        icon = R.drawable.ic_doc_audio;
+        add("application/ogg", icon);
+        add("application/x-flac", icon);
+
+        // Certificate
+        icon = R.drawable.ic_doc_certificate;
+        add("application/pgp-keys", icon);
+        add("application/pgp-signature", icon);
+        add("application/x-pkcs12", icon);
+        add("application/x-pkcs7-certreqresp", icon);
+        add("application/x-pkcs7-crl", icon);
+        add("application/x-x509-ca-cert", icon);
+        add("application/x-x509-user-cert", icon);
+        add("application/x-pkcs7-certificates", icon);
+        add("application/x-pkcs7-mime", icon);
+        add("application/x-pkcs7-signature", icon);
+
+        // Source code
+        icon = R.drawable.ic_doc_codes;
+        add("application/rdf+xml", icon);
+        add("application/rss+xml", icon);
+        add("application/x-object", icon);
+        add("application/xhtml+xml", icon);
+        add("text/css", icon);
+        add("text/html", icon);
+        add("text/xml", icon);
+        add("text/x-c++hdr", icon);
+        add("text/x-c++src", icon);
+        add("text/x-chdr", icon);
+        add("text/x-csrc", icon);
+        add("text/x-dsrc", icon);
+        add("text/x-csh", icon);
+        add("text/x-haskell", icon);
+        add("text/x-java", icon);
+        add("text/x-literate-haskell", icon);
+        add("text/x-pascal", icon);
+        add("text/x-tcl", icon);
+        add("text/x-tex", icon);
+        add("application/x-latex", icon);
+        add("application/x-texinfo", icon);
+        add("application/atom+xml", icon);
+        add("application/ecmascript", icon);
+        add("application/json", icon);
+        add("application/javascript", icon);
+        add("application/xml", icon);
+        add("text/javascript", icon);
+        add("application/x-javascript", icon);
+
+        // Compressed
+        icon = R.drawable.ic_doc_compressed;
+        add("application/mac-binhex40", icon);
+        add("application/rar", icon);
+        add("application/zip", icon);
+        add("application/x-apple-diskimage", icon);
+        add("application/x-debian-package", icon);
+        add("application/x-gtar", icon);
+        add("application/x-iso9660-image", icon);
+        add("application/x-lha", icon);
+        add("application/x-lzh", icon);
+        add("application/x-lzx", icon);
+        add("application/x-stuffit", icon);
+        add("application/x-tar", icon);
+        add("application/x-webarchive", icon);
+        add("application/x-webarchive-xml", icon);
+        add("application/gzip", icon);
+        add("application/x-7z-compressed", icon);
+        add("application/x-deb", icon);
+        add("application/x-rar-compressed", icon);
+
+        // Contact
+        icon = R.drawable.ic_doc_contact;
+        add("text/x-vcard", icon);
+        add("text/vcard", icon);
+
+        // Event
+        icon = R.drawable.ic_doc_event;
+        add("text/calendar", icon);
+        add("text/x-vcalendar", icon);
+
+        // Font
+        icon = R.drawable.ic_doc_font;
+        add("application/x-font", icon);
+        add("application/font-woff", icon);
+        add("application/x-font-woff", icon);
+        add("application/x-font-ttf", icon);
+
+        // Image
+        icon = R.drawable.ic_doc_image;
+        add("application/vnd.oasis.opendocument.graphics", icon);
+        add("application/vnd.oasis.opendocument.graphics-template", icon);
+        add("application/vnd.oasis.opendocument.image", icon);
+        add("application/vnd.stardivision.draw", icon);
+        add("application/vnd.sun.xml.draw", icon);
+        add("application/vnd.sun.xml.draw.template", icon);
+
+        // PDF
+        icon = R.drawable.ic_doc_pdf;
+        add("application/pdf", icon);
+
+        // Presentation
+        icon = R.drawable.ic_doc_presentation;
+        add("application/vnd.stardivision.impress", icon);
+        add("application/vnd.sun.xml.impress", icon);
+        add("application/vnd.sun.xml.impress.template", icon);
+        add("application/x-kpresenter", icon);
+        add("application/vnd.oasis.opendocument.presentation", icon);
+
+        // Spreadsheet
+        icon = R.drawable.ic_doc_spreadsheet;
+        add("application/vnd.oasis.opendocument.spreadsheet", icon);
+        add("application/vnd.oasis.opendocument.spreadsheet-template", icon);
+        add("application/vnd.stardivision.calc", icon);
+        add("application/vnd.sun.xml.calc", icon);
+        add("application/vnd.sun.xml.calc.template", icon);
+        add("application/x-kspread", icon);
+
+        // Document
+        icon = R.drawable.ic_doc_document;
+        add("application/vnd.oasis.opendocument.text", icon);
+        add("application/vnd.oasis.opendocument.text-master", icon);
+        add("application/vnd.oasis.opendocument.text-template", icon);
+        add("application/vnd.oasis.opendocument.text-web", icon);
+        add("application/vnd.stardivision.writer", icon);
+        add("application/vnd.stardivision.writer-global", icon);
+        add("application/vnd.sun.xml.writer", icon);
+        add("application/vnd.sun.xml.writer.global", icon);
+        add("application/vnd.sun.xml.writer.template", icon);
+        add("application/x-abiword", icon);
+        add("application/x-kword", icon);
+
+        // Video
+        icon = R.drawable.ic_doc_video;
+        add("application/x-quicktimeplayer", icon);
+        add("application/x-shockwave-flash", icon);
+
+        // Word
+        icon = R.drawable.ic_doc_word;
+        add("application/msword", icon);
+        add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", icon);
+        add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", icon);
+
+        // Excel
+        icon = R.drawable.ic_doc_excel;
+        add("application/vnd.ms-excel", icon);
+        add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", icon);
+        add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", icon);
+
+        // Powerpoint
+        icon = R.drawable.ic_doc_powerpoint;
+        add("application/vnd.ms-powerpoint", icon);
+        add("application/vnd.openxmlformats-officedocument.presentationml.presentation", icon);
+        add("application/vnd.openxmlformats-officedocument.presentationml.template", icon);
+        add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", icon);
+    }
+
+    public static Drawable loadMimeIcon(Context context, String mimeType) {
+        if (DocumentsContract.Document.MIME_TYPE_DIR.equals(mimeType)) {
+            return context.getDrawable(R.drawable.ic_doc_folder);
+        }
+
+        // Look for exact match first
+        Integer resId = sMimeIcons.get(mimeType);
+        if (resId != null) {
+            return context.getDrawable(resId);
+        }
+
+        if (mimeType == null) {
+            // TODO: generic icon?
+            return null;
+        }
+
+        // Otherwise look for partial match
+        final String typeOnly = mimeType.split("/")[0];
+        if ("audio".equals(typeOnly)) {
+            return context.getDrawable(R.drawable.ic_doc_audio);
+        } else if ("image".equals(typeOnly)) {
+            return context.getDrawable(R.drawable.ic_doc_image);
+        } else if ("text".equals(typeOnly)) {
+            return context.getDrawable(R.drawable.ic_doc_text);
+        } else if ("video".equals(typeOnly)) {
+            return context.getDrawable(R.drawable.ic_doc_video);
+        } else {
+            return context.getDrawable(R.drawable.ic_doc_generic);
+        }
+    }
+}
diff --git a/core/java/com/android/internal/util/NotificationColorUtil.java b/core/java/com/android/internal/util/NotificationColorUtil.java
index 48bcc09..4748e6f 100644
--- a/core/java/com/android/internal/util/NotificationColorUtil.java
+++ b/core/java/com/android/internal/util/NotificationColorUtil.java
@@ -271,7 +271,7 @@
      * Finds a text color with sufficient contrast over bg that has the same hue as the original
      * color, assuming it is for large text.
      */
-    private static int ensureLargeTextContrast(int color, int bg) {
+    public static int ensureLargeTextContrast(int color, int bg) {
         return findContrastColor(color, bg, true, 3);
     }
 
@@ -341,6 +341,20 @@
     }
 
     /**
+     * Lighten a color by a specified value
+     * @param baseColor the base color to lighten
+     * @param amount the amount to lighten the color from 0 to 100. This corresponds to the L
+     *               increase in the LAB color space.
+     * @return the lightened color
+     */
+    public static int lightenColor(int baseColor, int amount) {
+        final double[] result = ColorUtilsFromCompat.getTempDouble3Array();
+        ColorUtilsFromCompat.colorToLAB(baseColor, result);
+        result[0] = Math.min(100, result[0] + amount);
+        return ColorUtilsFromCompat.LABToColor(result[0], result[1], result[2]);
+    }
+
+    /**
      * Framework copy of functions needed from android.support.v4.graphics.ColorUtils.
      */
     private static class ColorUtilsFromCompat {
@@ -434,7 +448,7 @@
          * Convert RGB components to its CIE Lab representative components.
          *
          * <ul>
-         * <li>outLab[0] is L [0 ...1)</li>
+         * <li>outLab[0] is L [0 ...100)</li>
          * <li>outLab[1] is a [-128...127)</li>
          * <li>outLab[2] is b [-128...127)</li>
          * </ul>
@@ -516,7 +530,7 @@
          * 2° Standard Observer (1931).</p>
          *
          * <ul>
-         * <li>outLab[0] is L [0 ...1)</li>
+         * <li>outLab[0] is L [0 ...100)</li>
          * <li>outLab[1] is a [-128...127)</li>
          * <li>outLab[2] is b [-128...127)</li>
          * </ul>
@@ -634,7 +648,7 @@
                     : (XYZ_KAPPA * component + 16) / 116;
         }
 
-        private static double[] getTempDouble3Array() {
+        public static double[] getTempDouble3Array() {
             double[] result = TEMP_ARRAY.get();
             if (result == null) {
                 result = new double[3];
diff --git a/core/java/com/android/internal/util/WakeupMessage.java b/core/java/com/android/internal/util/WakeupMessage.java
index 7d222c7..46098c5 100644
--- a/core/java/com/android/internal/util/WakeupMessage.java
+++ b/core/java/com/android/internal/util/WakeupMessage.java
@@ -108,7 +108,7 @@
         }
         if (stillScheduled) {
             Message msg = mHandler.obtainMessage(mCmd, mArg1, mArg2, mObj);
-            mHandler.handleMessage(msg);
+            mHandler.dispatchMessage(msg);
             msg.recycle();
         }
     }
diff --git a/core/java/com/android/internal/view/IInputConnectionWrapper.java b/core/java/com/android/internal/view/IInputConnectionWrapper.java
index 59b4b35..644c7e9 100644
--- a/core/java/com/android/internal/view/IInputConnectionWrapper.java
+++ b/core/java/com/android/internal/view/IInputConnectionWrapper.java
@@ -33,6 +33,7 @@
 import android.view.inputmethod.InputConnection;
 import android.view.inputmethod.InputConnectionInspector;
 import android.view.inputmethod.InputConnectionInspector.MissingMethodFlags;
+import android.view.inputmethod.InputContentInfo;
 
 public abstract class IInputConnectionWrapper extends IInputContext.Stub {
     static final String TAG = "IInputConnectionWrapper";
@@ -61,6 +62,7 @@
     private static final int DO_CLEAR_META_KEY_STATES = 130;
     private static final int DO_REQUEST_UPDATE_CURSOR_ANCHOR_INFO = 140;
     private static final int DO_CLOSE_CONNECTION = 150;
+    private static final int DO_COMMIT_CONTENT = 160;
 
     @GuardedBy("mLock")
     @Nullable
@@ -241,6 +243,12 @@
         dispatchMessage(obtainMessage(DO_CLOSE_CONNECTION));
     }
 
+    public void commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts,
+            int seq, IInputContextCallback callback) {
+        dispatchMessage(obtainMessageIOOSC(DO_COMMIT_CONTENT, flags, inputContentInfo, opts, seq,
+                callback));
+    }
+
     void dispatchMessage(Message msg) {
         // If we are calling this from the main thread, then we can call
         // right through.  Otherwise, we need to send the message to the
@@ -552,6 +560,41 @@
                 }
                 return;
             }
+            case DO_COMMIT_CONTENT: {
+                final int flags = msg.arg1;
+                final boolean grantUriPermission =
+                        (flags & InputConnection.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0;
+                SomeArgs args = (SomeArgs) msg.obj;
+                try {
+                    InputConnection ic = getInputConnection();
+                    if (ic == null || !isActive()) {
+                        Log.w(TAG, "commitContent on inactive InputConnection");
+                        args.callback.setCommitContentResult(false, args.seq);
+                        return;
+                    }
+                    final InputContentInfo inputContentInfo = (InputContentInfo) args.arg1;
+                    if (inputContentInfo == null || !inputContentInfo.validate()) {
+                        Log.w(TAG, "commitContent with invalid inputContentInfo="
+                                + inputContentInfo);
+                        args.callback.setCommitContentResult(false, args.seq);
+                        return;
+                    }
+                    if (grantUriPermission) {
+                        inputContentInfo.requestPermission();
+                    }
+                    final boolean result =
+                            ic.commitContent(inputContentInfo, flags, (Bundle) args.arg2);
+                    // If this request is not handled, then there is no reason to keep the URI
+                    // permission.
+                    if (grantUriPermission && !result) {
+                        inputContentInfo.releasePermission();
+                    }
+                    args.callback.setCommitContentResult(result, args.seq);
+                } catch (RemoteException e) {
+                    Log.w(TAG, "Got RemoteException calling commitContent", e);
+                }
+                return;
+            }
         }
         Log.w(TAG, "Unhandled message code: " + msg.what);
     }
@@ -582,12 +625,14 @@
         return mH.obtainMessage(what, arg1, arg2, args);
     }
 
-    Message obtainMessageOSC(int what, Object arg1, int seq, IInputContextCallback callback) {
+    Message obtainMessageIOOSC(int what, int arg1, Object objArg1, Object objArg2, int seq,
+            IInputContextCallback callback) {
         SomeArgs args = new SomeArgs();
-        args.arg1 = arg1;
+        args.arg1 = objArg1;
+        args.arg2 = objArg2;
         args.callback = callback;
         args.seq = seq;
-        return mH.obtainMessage(what, 0, 0, args);
+        return mH.obtainMessage(what, arg1, 0, args);
     }
 
     Message obtainMessageIOSC(int what, int arg1, Object arg2, int seq,
diff --git a/core/java/com/android/internal/view/IInputContext.aidl b/core/java/com/android/internal/view/IInputContext.aidl
index f7ec420..728c557 100644
--- a/core/java/com/android/internal/view/IInputContext.aidl
+++ b/core/java/com/android/internal/view/IInputContext.aidl
@@ -21,6 +21,7 @@
 import android.view.inputmethod.CompletionInfo;
 import android.view.inputmethod.CorrectionInfo;
 import android.view.inputmethod.ExtractedTextRequest;
+import android.view.inputmethod.InputContentInfo;
 
 import com.android.internal.view.IInputContextCallback;
 
@@ -74,6 +75,9 @@
 
     void getSelectedText(int flags, int seq, IInputContextCallback callback);
 
-    void requestUpdateCursorAnchorInfo(in int cursorUpdateMode, int seq,
+    void requestUpdateCursorAnchorInfo(int cursorUpdateMode, int seq,
+            IInputContextCallback callback);
+
+    void commitContent(in InputContentInfo inputContentInfo, int flags, in Bundle opts, int sec,
             IInputContextCallback callback);
 }
diff --git a/core/java/com/android/internal/view/IInputContextCallback.aidl b/core/java/com/android/internal/view/IInputContextCallback.aidl
index 54ea306..0f40a83 100644
--- a/core/java/com/android/internal/view/IInputContextCallback.aidl
+++ b/core/java/com/android/internal/view/IInputContextCallback.aidl
@@ -28,4 +28,5 @@
     void setExtractedText(in ExtractedText extractedText, int seq);
     void setSelectedText(CharSequence selectedText, int seq);
     void setRequestUpdateCursorAnchorInfoResult(boolean result, int seq);
+    void setCommitContentResult(boolean result, int seq);
 }
diff --git a/core/java/com/android/internal/view/IInputMethodManager.aidl b/core/java/com/android/internal/view/IInputMethodManager.aidl
index cb7c3bf..9e4b43b 100644
--- a/core/java/com/android/internal/view/IInputMethodManager.aidl
+++ b/core/java/com/android/internal/view/IInputMethodManager.aidl
@@ -16,11 +16,13 @@
 
 package com.android.internal.view;
 
+import android.net.Uri;
 import android.os.ResultReceiver;
 import android.text.style.SuggestionSpan;
 import android.view.inputmethod.InputMethodInfo;
 import android.view.inputmethod.InputMethodSubtype;
 import android.view.inputmethod.EditorInfo;
+import com.android.internal.inputmethod.IInputContentUriToken;
 import com.android.internal.view.InputBindResult;
 import com.android.internal.view.IInputContext;
 import com.android.internal.view.IInputMethodClient;
@@ -81,5 +83,8 @@
     int getInputMethodWindowVisibleHeight();
     void clearLastInputMethodWindowForTransition(in IBinder token);
 
+    IInputContentUriToken createInputContentUriToken(in IBinder token, in Uri contentUri,
+            in String packageName);
+
     oneway void notifyUserAction(int sequenceNumber);
 }
diff --git a/core/java/com/android/internal/view/InputConnectionWrapper.java b/core/java/com/android/internal/view/InputConnectionWrapper.java
index f9884d8..9a09dcc 100644
--- a/core/java/com/android/internal/view/InputConnectionWrapper.java
+++ b/core/java/com/android/internal/view/InputConnectionWrapper.java
@@ -16,6 +16,8 @@
 
 package com.android.internal.view;
 
+import android.annotation.NonNull;
+import android.inputmethodservice.AbstractInputMethodService;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.RemoteException;
@@ -29,10 +31,16 @@
 import android.view.inputmethod.InputConnection;
 import android.view.inputmethod.InputConnectionInspector;
 import android.view.inputmethod.InputConnectionInspector.MissingMethodFlags;
+import android.view.inputmethod.InputContentInfo;
+
+import java.lang.ref.WeakReference;
 
 public class InputConnectionWrapper implements InputConnection {
     private static final int MAX_WAIT_TIME_MILLIS = 2000;
     private final IInputContext mIInputContext;
+    @NonNull
+    private final WeakReference<AbstractInputMethodService> mInputMethodService;
+
     @MissingMethodFlags
     private final int mMissingMethods;
 
@@ -46,7 +54,8 @@
         public ExtractedText mExtractedText;
         public int mCursorCapsMode;
         public boolean mRequestUpdateCursorAnchorInfoResult;
-        
+        public boolean mCommitContentResult;
+
         // A 'pool' of one InputContextCallback.  Each ICW request will attempt to gain
         // exclusive access to this object.
         private static InputContextCallback sInstance = new InputContextCallback();
@@ -172,6 +181,19 @@
             }
         }
 
+        public void setCommitContentResult(boolean result, int seq) {
+            synchronized (this) {
+                if (seq == mSeq) {
+                    mCommitContentResult = result;
+                    mHaveValue = true;
+                    notifyAll();
+                } else {
+                    Log.i(TAG, "Got out-of-sequence callback " + seq + " (expected " + mSeq
+                            + ") in setCommitContentResult, ignoring.");
+                }
+            }
+        }
+
         /**
          * Waits for a result for up to {@link #MAX_WAIT_TIME_MILLIS} milliseconds.
          * 
@@ -195,8 +217,10 @@
         }
     }
 
-    public InputConnectionWrapper(IInputContext inputContext,
-            @MissingMethodFlags final int missingMethods) {
+    public InputConnectionWrapper(
+            @NonNull WeakReference<AbstractInputMethodService> inputMethodService,
+            IInputContext inputContext, @MissingMethodFlags final int missingMethods) {
+        mInputMethodService = inputMethodService;
         mIInputContext = inputContext;
         mMissingMethods = missingMethods;
     }
@@ -491,6 +515,37 @@
         // Nothing should happen when called from input method.
     }
 
+    public boolean commitContent(InputContentInfo inputContentInfo, int flags, Bundle opts) {
+        boolean result = false;
+        if (isMethodMissing(MissingMethodFlags.COMMIT_CONTENT)) {
+            // This method is not implemented.
+            return false;
+        }
+        try {
+            if ((flags & InputConnection.INPUT_CONTENT_GRANT_READ_URI_PERMISSION) != 0) {
+                final AbstractInputMethodService inputMethodService = mInputMethodService.get();
+                if (inputMethodService == null) {
+                    // This basically should not happen, because it's the the caller of this method.
+                    return false;
+                }
+                inputMethodService.exposeContent(inputContentInfo, this);
+            }
+
+            InputContextCallback callback = InputContextCallback.getInstance();
+            mIInputContext.commitContent(inputContentInfo, flags, opts, callback.mSeq, callback);
+            synchronized (callback) {
+                callback.waitForResultLocked();
+                if (callback.mHaveValue) {
+                    result = callback.mCommitContentResult;
+                }
+            }
+            callback.dispose();
+        } catch (RemoteException e) {
+            return false;
+        }
+        return result;
+    }
+
     private boolean isMethodMissing(@MissingMethodFlags final int methodFlag) {
         return (mMissingMethods & methodFlag) == methodFlag;
     }
diff --git a/core/java/com/android/internal/view/animation/FallbackLUTInterpolator.java b/core/java/com/android/internal/view/animation/FallbackLUTInterpolator.java
index 9e2cbdb..d28ab07 100644
--- a/core/java/com/android/internal/view/animation/FallbackLUTInterpolator.java
+++ b/core/java/com/android/internal/view/animation/FallbackLUTInterpolator.java
@@ -30,6 +30,8 @@
 @HasNativeInterpolator
 public class FallbackLUTInterpolator implements NativeInterpolatorFactory, TimeInterpolator {
 
+    // If the duration of an animation is more than 300 frames, we cap the sample size to 300.
+    private static final int MAX_SAMPLE_POINTS = 300;
     private TimeInterpolator mSourceInterpolator;
     private final float mLut[];
 
@@ -47,6 +49,7 @@
         int animIntervalMs = (int) (frameIntervalNanos / TimeUtils.NANOS_PER_MS);
         // We need 2 frame values as the minimal.
         int numAnimFrames = Math.max(2, (int) Math.ceil(((double) duration) / animIntervalMs));
+        numAnimFrames = Math.min(numAnimFrames, MAX_SAMPLE_POINTS);
         float values[] = new float[numAnimFrames];
         float lastFrame = numAnimFrames - 1;
         for (int i = 0; i < numAnimFrames; i++) {
diff --git a/core/java/com/android/internal/view/menu/CascadingMenuPopup.java b/core/java/com/android/internal/view/menu/CascadingMenuPopup.java
index ddf3a76..69e974c 100644
--- a/core/java/com/android/internal/view/menu/CascadingMenuPopup.java
+++ b/core/java/com/android/internal/view/menu/CascadingMenuPopup.java
@@ -366,7 +366,7 @@
         final int menuWidth = measureIndividualMenuWidth(adapter, null, mContext, mMenuMaxWidth);
         final MenuPopupWindow popupWindow = createPopupWindow();
         popupWindow.setAdapter(adapter);
-        popupWindow.setWidth(menuWidth);
+        popupWindow.setContentWidth(menuWidth);
         popupWindow.setDropDownGravity(mDropDownGravity);
 
         final CascadingMenuInfo parentInfo;
diff --git a/core/java/com/android/internal/widget/AlertDialogLayout.java b/core/java/com/android/internal/widget/AlertDialogLayout.java
index 891c920..9bf0948 100644
--- a/core/java/com/android/internal/widget/AlertDialogLayout.java
+++ b/core/java/com/android/internal/widget/AlertDialogLayout.java
@@ -20,7 +20,9 @@
 import android.annotation.Nullable;
 import android.annotation.StyleRes;
 import android.content.Context;
+import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
+import android.view.Gravity;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
@@ -265,4 +267,92 @@
 
         return 0;
     }
+
+    @Override
+    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+        final int paddingLeft = mPaddingLeft;
+
+        // Where right end of child should go
+        final int width = right - left;
+        final int childRight = width - mPaddingRight;
+
+        // Space available for child
+        final int childSpace = width - paddingLeft - mPaddingRight;
+
+        final int totalLength = getMeasuredHeight();
+        final int count = getChildCount();
+        final int gravity = getGravity();
+        final int majorGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
+        final int minorGravity = gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
+
+        int childTop;
+        switch (majorGravity) {
+            case Gravity.BOTTOM:
+                // totalLength contains the padding already
+                childTop = mPaddingTop + bottom - top - totalLength;
+                break;
+
+            // totalLength contains the padding already
+            case Gravity.CENTER_VERTICAL:
+                childTop = mPaddingTop + (bottom - top - totalLength) / 2;
+                break;
+
+            case Gravity.TOP:
+            default:
+                childTop = mPaddingTop;
+                break;
+        }
+
+        final Drawable dividerDrawable = getDividerDrawable();
+        final int dividerHeight = dividerDrawable == null ?
+                0 : dividerDrawable.getIntrinsicHeight();
+
+        for (int i = 0; i < count; i++) {
+            final View child = getChildAt(i);
+            if (child != null && child.getVisibility() != GONE) {
+                final int childWidth = child.getMeasuredWidth();
+                final int childHeight = child.getMeasuredHeight();
+
+                final LinearLayout.LayoutParams lp =
+                        (LinearLayout.LayoutParams) child.getLayoutParams();
+
+                int layoutGravity = lp.gravity;
+                if (layoutGravity < 0) {
+                    layoutGravity = minorGravity;
+                }
+                final int layoutDirection = getLayoutDirection();
+                final int absoluteGravity = Gravity.getAbsoluteGravity(
+                        layoutGravity, layoutDirection);
+
+                final int childLeft;
+                switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
+                    case Gravity.CENTER_HORIZONTAL:
+                        childLeft = paddingLeft + ((childSpace - childWidth) / 2)
+                                + lp.leftMargin - lp.rightMargin;
+                        break;
+
+                    case Gravity.RIGHT:
+                        childLeft = childRight - childWidth - lp.rightMargin;
+                        break;
+
+                    case Gravity.LEFT:
+                    default:
+                        childLeft = paddingLeft + lp.leftMargin;
+                        break;
+                }
+
+                if (hasDividerBeforeChildAt(i)) {
+                    childTop += dividerHeight;
+                }
+
+                childTop += lp.topMargin;
+                setChildFrame(child, childLeft, childTop, childWidth, childHeight);
+                childTop += childHeight + lp.bottomMargin;
+            }
+        }
+    }
+
+    private void setChildFrame(View child, int left, int top, int width, int height) {
+        child.layout(left, top, left + width, top + height);
+    }
 }
diff --git a/core/java/com/android/internal/widget/CachingIconView.java b/core/java/com/android/internal/widget/CachingIconView.java
new file mode 100644
index 0000000..293b77b
--- /dev/null
+++ b/core/java/com/android/internal/widget/CachingIconView.java
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.internal.widget;
+
+import android.annotation.DrawableRes;
+import android.annotation.Nullable;
+import android.content.Context;
+import android.content.res.Configuration;
+import android.graphics.Bitmap;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.Icon;
+import android.net.Uri;
+import android.text.TextUtils;
+import android.util.AttributeSet;
+import android.view.RemotableViewMethod;
+import android.widget.ImageView;
+import android.widget.RemoteViews;
+
+import libcore.util.Objects;
+
+/**
+ * An ImageView for displaying an Icon. Avoids reloading the Icon when possible.
+ */
+@RemoteViews.RemoteView
+public class CachingIconView extends ImageView {
+
+    private String mLastPackage;
+    private int mLastResId;
+    private boolean mInternalSetDrawable;
+
+    public CachingIconView(Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    @Override
+    @RemotableViewMethod(asyncImpl="setImageIconAsync")
+    public void setImageIcon(@Nullable Icon icon) {
+        if (!testAndSetCache(icon)) {
+            mInternalSetDrawable = true;
+            // This calls back to setImageDrawable, make sure we don't clear the cache there.
+            super.setImageIcon(icon);
+            mInternalSetDrawable = false;
+        }
+    }
+
+    @Override
+    public Runnable setImageIconAsync(@Nullable Icon icon) {
+        resetCache();
+        return super.setImageIconAsync(icon);
+    }
+
+    @Override
+    @RemotableViewMethod(asyncImpl="setImageResourceAsync")
+    public void setImageResource(@DrawableRes int resId) {
+        if (!testAndSetCache(resId)) {
+            mInternalSetDrawable = true;
+            // This calls back to setImageDrawable, make sure we don't clear the cache there.
+            super.setImageResource(resId);
+            mInternalSetDrawable = false;
+        }
+    }
+
+    @Override
+    public Runnable setImageResourceAsync(@DrawableRes int resId) {
+        resetCache();
+        return super.setImageResourceAsync(resId);
+    }
+
+    @Override
+    @RemotableViewMethod(asyncImpl="setImageURIAsync")
+    public void setImageURI(@Nullable Uri uri) {
+        resetCache();
+        super.setImageURI(uri);
+    }
+
+    @Override
+    public Runnable setImageURIAsync(@Nullable Uri uri) {
+        resetCache();
+        return super.setImageURIAsync(uri);
+    }
+
+    @Override
+    public void setImageDrawable(@Nullable Drawable drawable) {
+        if (!mInternalSetDrawable) {
+            // Only clear the cache if we were externally called.
+            resetCache();
+        }
+        super.setImageDrawable(drawable);
+    }
+
+    @Override
+    @RemotableViewMethod
+    public void setImageBitmap(Bitmap bm) {
+        resetCache();
+        super.setImageBitmap(bm);
+    }
+
+    @Override
+    protected void onConfigurationChanged(Configuration newConfig) {
+        super.onConfigurationChanged(newConfig);
+        resetCache();
+    }
+
+    /**
+     * @return true if the currently set image is the same as {@param icon}
+     */
+    private synchronized boolean testAndSetCache(Icon icon) {
+        if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
+            String iconPackage = normalizeIconPackage(icon);
+
+            boolean isCached = mLastResId != 0
+                    && icon.getResId() == mLastResId
+                    && Objects.equal(iconPackage, mLastPackage);
+
+            mLastPackage = iconPackage;
+            mLastResId = icon.getResId();
+
+            return isCached;
+        } else {
+            resetCache();
+            return false;
+        }
+    }
+
+    /**
+     * @return true if the currently set image is the same as {@param resId}
+     */
+    private synchronized boolean testAndSetCache(int resId) {
+        boolean isCached;
+        if (resId == 0 || mLastResId == 0) {
+            isCached = false;
+        } else {
+            isCached = resId == mLastResId && null == mLastPackage;
+        }
+        mLastPackage = null;
+        mLastResId = resId;
+        return isCached;
+    }
+
+    /**
+     * Returns the normalized package name of {@param icon}.
+     * @return null if icon is null or if the icons package is null, empty or matches the current
+     *         context. Otherwise returns the icon's package context.
+     */
+    private String normalizeIconPackage(Icon icon) {
+        if (icon == null) {
+            return null;
+        }
+
+        String pkg = icon.getResPackage();
+        if (TextUtils.isEmpty(pkg)) {
+            return null;
+        }
+        if (pkg.equals(mContext.getPackageName())) {
+            return null;
+        }
+        return pkg;
+    }
+
+    private synchronized void resetCache() {
+        mLastResId = 0;
+        mLastPackage = null;
+    }
+}
diff --git a/core/java/com/android/internal/widget/DecorCaptionView.java b/core/java/com/android/internal/widget/DecorCaptionView.java
index e59d7ba..fc68b84 100644
--- a/core/java/com/android/internal/widget/DecorCaptionView.java
+++ b/core/java/com/android/internal/widget/DecorCaptionView.java
@@ -244,6 +244,11 @@
         return mTouchDispatchList;
     }
 
+    @Override
+    public boolean shouldDelayChildPressedState() {
+        return false;
+    }
+
     private boolean passedSlop(int x, int y) {
         return Math.abs(x - mTouchDownX) > mDragSlop || Math.abs(y - mTouchDownY) > mDragSlop;
     }
diff --git a/core/java/com/android/internal/widget/FloatingToolbar.java b/core/java/com/android/internal/widget/FloatingToolbar.java
index 594581a..6604208 100644
--- a/core/java/com/android/internal/widget/FloatingToolbar.java
+++ b/core/java/com/android/internal/widget/FloatingToolbar.java
@@ -737,7 +737,7 @@
                 protected void applyTransformation(float interpolatedTime, Transformation t) {
                     int deltaWidth = (int) (interpolatedTime * (targetWidth - startWidth));
                     setWidth(mContentContainer, startWidth + deltaWidth);
-                    if (isRTL()) {
+                    if (isInRTLMode()) {
                         mContentContainer.setX(left);
 
                         // Lock the panels in place.
@@ -766,7 +766,7 @@
                 }
             };
             final float overflowButtonStartX = mOverflowButton.getX();
-            final float overflowButtonTargetX = isRTL() ?
+            final float overflowButtonTargetX = isInRTLMode() ?
                     overflowButtonStartX + targetWidth - mOverflowButton.getWidth() :
                     overflowButtonStartX - targetWidth + mOverflowButton.getWidth();
             Animation overflowButtonAnimation = new Animation() {
@@ -774,7 +774,7 @@
                 protected void applyTransformation(float interpolatedTime, Transformation t) {
                     float overflowButtonX = overflowButtonStartX
                             + interpolatedTime * (overflowButtonTargetX - overflowButtonStartX);
-                    float deltaContainerWidth = isRTL() ?
+                    float deltaContainerWidth = isInRTLMode() ?
                             0 :
                             mContentContainer.getWidth() - startWidth;
                     float actualOverflowButtonX = overflowButtonX + deltaContainerWidth;
@@ -812,7 +812,7 @@
                 protected void applyTransformation(float interpolatedTime, Transformation t) {
                     int deltaWidth = (int) (interpolatedTime * (targetWidth - startWidth));
                     setWidth(mContentContainer, startWidth + deltaWidth);
-                    if (isRTL()) {
+                    if (isInRTLMode()) {
                         mContentContainer.setX(left);
 
                         // Lock the panels in place.
@@ -843,7 +843,7 @@
                 }
             };
             final float overflowButtonStartX = mOverflowButton.getX();
-            final float overflowButtonTargetX = isRTL() ?
+            final float overflowButtonTargetX = isInRTLMode() ?
                     overflowButtonStartX - startWidth + mOverflowButton.getWidth() :
                     overflowButtonStartX + startWidth - mOverflowButton.getWidth();
             Animation overflowButtonAnimation = new Animation() {
@@ -851,7 +851,7 @@
                 protected void applyTransformation(float interpolatedTime, Transformation t) {
                     float overflowButtonX = overflowButtonStartX
                             + interpolatedTime * (overflowButtonTargetX - overflowButtonStartX);
-                    float deltaContainerWidth = isRTL() ?
+                    float deltaContainerWidth = isInRTLMode() ?
                             0 :
                             mContentContainer.getWidth() - startWidth;
                     float actualOverflowButtonX = overflowButtonX + deltaContainerWidth;
@@ -903,7 +903,7 @@
                         R.string.floating_toolbar_close_overflow_description));
 
                 // Update x-coordinates depending on RTL state.
-                if (isRTL()) {
+                if (isInRTLMode()) {
                     mContentContainer.setX(mMarginHorizontal);  // align left
                     mMainPanel.setX(0);  // align left
                     mOverflowButton.setX(  // align right
@@ -947,7 +947,7 @@
 
                 if (hasOverflow()) {
                     // Update x-coordinates depending on RTL state.
-                    if (isRTL()) {
+                    if (isInRTLMode()) {
                         mContentContainer.setX(mMarginHorizontal);  // align left
                         mMainPanel.setX(0);  // align left
                         mOverflowButton.setX(0);  // align left
@@ -1087,9 +1087,10 @@
             viewTreeObserver.addOnComputeInternalInsetsListener(mInsetsComputer);
         }
 
-        private boolean isRTL() {
-            return mContext.getResources().getConfiguration().getLayoutDirection()
-                    == View.LAYOUT_DIRECTION_RTL;
+        private boolean isInRTLMode() {
+            return mContext.getApplicationInfo().hasRtlSupport()
+                    && mContext.getResources().getConfiguration().getLayoutDirection()
+                            == View.LAYOUT_DIRECTION_RTL;
         }
 
         private boolean hasOverflow() {
@@ -1203,7 +1204,7 @@
             // The positioning of contents in RTL is wrong when the view is first rendered.
             // Hide the view and post a runnable to recalculate positions and render the view.
             // TODO: Investigate why this happens and fix.
-            if (isRTL()) {
+            if (isInRTLMode()) {
                 mContentContainer.setAlpha(0);
                 mContentContainer.post(mPreparePopupContentRTLHelper);
             }
diff --git a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl b/core/java/com/android/internal/widget/ICheckCredentialProgressCallback.aidl
similarity index 67%
copy from telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
copy to core/java/com/android/internal/widget/ICheckCredentialProgressCallback.aidl
index b7e78d1..79717cf 100644
--- a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
+++ b/core/java/com/android/internal/widget/ICheckCredentialProgressCallback.aidl
@@ -1,11 +1,11 @@
 /*
- * Copyright 2016, The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ *      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,9 +14,9 @@
  * limitations under the License.
  */
 
-package android.telecom;
+package com.android.internal.widget;
 
-/**
- * {@hide}
- */
-parcelable ParcelableCallAnalytics;
+/** {@hide} */
+oneway interface ICheckCredentialProgressCallback {
+    void onCredentialVerified();
+}
diff --git a/core/java/com/android/internal/widget/ILockSettings.aidl b/core/java/com/android/internal/widget/ILockSettings.aidl
index 05b839d..9fa558e 100644
--- a/core/java/com/android/internal/widget/ILockSettings.aidl
+++ b/core/java/com/android/internal/widget/ILockSettings.aidl
@@ -17,6 +17,7 @@
 package com.android.internal.widget;
 
 import android.app.trust.IStrongAuthTracker;
+import com.android.internal.widget.ICheckCredentialProgressCallback;
 import com.android.internal.widget.VerifyCredentialResponse;
 
 /** {@hide} */
@@ -29,10 +30,12 @@
     String getString(in String key, in String defaultValue, in int userId);
     void setLockPattern(in String pattern, in String savedPattern, int userId);
     void resetKeyStore(int userId);
-    VerifyCredentialResponse checkPattern(in String pattern, int userId);
+    VerifyCredentialResponse checkPattern(in String pattern, int userId,
+            in ICheckCredentialProgressCallback progressCallback);
     VerifyCredentialResponse verifyPattern(in String pattern, long challenge, int userId);
     void setLockPassword(in String password, in String savedPassword, int userId);
-    VerifyCredentialResponse checkPassword(in String password, int userId);
+    VerifyCredentialResponse checkPassword(in String password, int userId,
+            in ICheckCredentialProgressCallback progressCallback);
     VerifyCredentialResponse verifyPassword(in String password, long challenge, int userId);
     VerifyCredentialResponse verifyTiedProfileChallenge(String password, boolean isPattern, long challenge, int userId);
     boolean checkVoldPassword(int userId);
diff --git a/core/java/com/android/internal/widget/ImageFloatingTextView.java b/core/java/com/android/internal/widget/ImageFloatingTextView.java
index 926ebd1..358be60 100644
--- a/core/java/com/android/internal/widget/ImageFloatingTextView.java
+++ b/core/java/com/android/internal/widget/ImageFloatingTextView.java
@@ -40,6 +40,9 @@
     /** Number of lines from the top to indent */
     private int mIndentLines;
 
+    /** Resolved layout direction */
+    private int mResolvedDirection = LAYOUT_DIRECTION_UNDEFINED;
+
     public ImageFloatingTextView(Context context) {
         this(context, null);
     }
@@ -82,7 +85,7 @@
                 margins[i] = endMargin;
             }
         }
-        if (getLayoutDirection() == LAYOUT_DIRECTION_RTL) {
+        if (mResolvedDirection == LAYOUT_DIRECTION_RTL) {
             builder.setIndents(margins, null);
         } else {
             builder.setIndents(null, margins);
@@ -91,6 +94,19 @@
         return builder.build();
     }
 
+    @Override
+    public void onRtlPropertiesChanged(int layoutDirection) {
+        super.onRtlPropertiesChanged(layoutDirection);
+
+        if (layoutDirection != mResolvedDirection && isLayoutDirectionResolved()) {
+            mResolvedDirection = layoutDirection;
+            if (mIndentLines > 0) {
+                // Invalidate layout.
+                setHint(getHint());
+            }
+        }
+    }
+
     @RemotableViewMethod
     public void setHasImage(boolean hasImage) {
         setNumIndentLines(hasImage ? 2 : 0);
diff --git a/core/java/com/android/internal/widget/LockPatternChecker.java b/core/java/com/android/internal/widget/LockPatternChecker.java
index 713f56f..6defd1e 100644
--- a/core/java/com/android/internal/widget/LockPatternChecker.java
+++ b/core/java/com/android/internal/widget/LockPatternChecker.java
@@ -4,6 +4,7 @@
 
 import com.android.internal.widget.LockPatternUtils.RequestThrottledException;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -14,6 +15,13 @@
      * Interface for a callback to be invoked after security check.
      */
     public interface OnCheckCallback {
+
+        /**
+         * Invoked as soon as possible we know that the credentials match. This will be called
+         * earlier than {@link #onChecked} but only if the credentials match.
+         */
+        default void onEarlyMatched() {}
+
         /**
          * Invoked when a security check is finished.
          *
@@ -54,11 +62,19 @@
             final OnVerifyCallback callback) {
         AsyncTask<Void, Void, byte[]> task = new AsyncTask<Void, Void, byte[]>() {
             private int mThrottleTimeout;
+            private List<LockPatternView.Cell> patternCopy;
+
+            @Override
+            protected void onPreExecute() {
+                // Make a copy of the pattern to prevent race conditions.
+                // No need to clone the individual cells because they are immutable.
+                patternCopy = new ArrayList(pattern);
+            }
 
             @Override
             protected byte[] doInBackground(Void... args) {
                 try {
-                    return utils.verifyPattern(pattern, challenge, userId);
+                    return utils.verifyPattern(patternCopy, challenge, userId);
                 } catch (RequestThrottledException ex) {
                     mThrottleTimeout = ex.getTimeoutMs();
                     return null;
@@ -88,11 +104,19 @@
             final OnCheckCallback callback) {
         AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
             private int mThrottleTimeout;
+            private List<LockPatternView.Cell> patternCopy;
+
+            @Override
+            protected void onPreExecute() {
+                // Make a copy of the pattern to prevent race conditions.
+                // No need to clone the individual cells because they are immutable.
+                patternCopy = new ArrayList(pattern);
+            }
 
             @Override
             protected Boolean doInBackground(Void... args) {
                 try {
-                    return utils.checkPattern(pattern, userId);
+                    return utils.checkPattern(patternCopy, userId, callback::onEarlyMatched);
                 } catch (RequestThrottledException ex) {
                     mThrottleTimeout = ex.getTimeoutMs();
                     return false;
@@ -199,7 +223,7 @@
             @Override
             protected Boolean doInBackground(Void... args) {
                 try {
-                    return utils.checkPassword(password, userId);
+                    return utils.checkPassword(password, userId, callback::onEarlyMatched);
                 } catch (RequestThrottledException ex) {
                     mThrottleTimeout = ex.getTimeoutMs();
                     return false;
diff --git a/core/java/com/android/internal/widget/LockPatternUtils.java b/core/java/com/android/internal/widget/LockPatternUtils.java
index 7f2f740..479b3b7 100644
--- a/core/java/com/android/internal/widget/LockPatternUtils.java
+++ b/core/java/com/android/internal/widget/LockPatternUtils.java
@@ -17,6 +17,7 @@
 package com.android.internal.widget;
 
 import android.annotation.IntDef;
+import android.annotation.Nullable;
 import android.app.admin.DevicePolicyManager;
 import android.app.trust.IStrongAuthTracker;
 import android.app.trust.TrustManager;
@@ -32,7 +33,6 @@
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemClock;
-import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.storage.IMountService;
@@ -149,6 +149,7 @@
     private DevicePolicyManager mDevicePolicyManager;
     private ILockSettings mLockSettingsService;
     private UserManager mUserManager;
+    private final Handler mHandler;
 
     /**
      * Use {@link TrustManager#isTrustUsuallyManaged(int)}.
@@ -230,6 +231,9 @@
     public LockPatternUtils(Context context) {
         mContext = context;
         mContentResolver = context.getContentResolver();
+
+        Looper looper = Looper.myLooper();
+        mHandler = looper != null ? new Handler(looper) : null;
     }
 
     private ILockSettings getLockSettings() {
@@ -284,7 +288,6 @@
     public void reportFailedPasswordAttempt(int userId) {
         getDevicePolicyManager().reportFailedPasswordAttempt(userId);
         getTrustManager().reportUnlockAttempt(false /* authenticated */, userId);
-        requireStrongAuth(StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL, userId);
     }
 
     public void reportSuccessfulPasswordAttempt(int userId) {
@@ -341,10 +344,23 @@
      */
     public boolean checkPattern(List<LockPatternView.Cell> pattern, int userId)
             throws RequestThrottledException {
+        return checkPattern(pattern, userId, null /* progressCallback */);
+    }
+
+    /**
+     * Check to see if a pattern matches the saved pattern.  If no pattern exists,
+     * always returns true.
+     * @param pattern The pattern to check.
+     * @return Whether the pattern matches the stored one.
+     */
+    public boolean checkPattern(List<LockPatternView.Cell> pattern, int userId,
+            @Nullable CheckCredentialProgressCallback progressCallback)
+            throws RequestThrottledException {
         throwIfCalledOnMainThread();
         try {
             VerifyCredentialResponse response =
-                    getLockSettings().checkPattern(patternToString(pattern), userId);
+                    getLockSettings().checkPattern(patternToString(pattern), userId,
+                            wrapCallback(progressCallback));
 
             if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) {
                 return true;
@@ -423,10 +439,22 @@
      * @return Whether the password matches the stored one.
      */
     public boolean checkPassword(String password, int userId) throws RequestThrottledException {
+        return checkPassword(password, userId, null /* progressCallback */);
+    }
+
+    /**
+     * Check to see if a password matches the saved password.  If no password exists,
+     * always returns true.
+     * @param password The password to check.
+     * @return Whether the password matches the stored one.
+     */
+    public boolean checkPassword(String password, int userId,
+            @Nullable CheckCredentialProgressCallback progressCallback)
+            throws RequestThrottledException {
         throwIfCalledOnMainThread();
         try {
             VerifyCredentialResponse response =
-                    getLockSettings().checkPassword(password, userId);
+                    getLockSettings().checkPassword(password, userId, wrapCallback(progressCallback));
             if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) {
                 return true;
             } else if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_RETRY) {
@@ -1475,6 +1503,37 @@
         return (getStrongAuthForUser(userId) & ~StrongAuthTracker.ALLOWING_FINGERPRINT) == 0;
     }
 
+    private ICheckCredentialProgressCallback wrapCallback(
+            final CheckCredentialProgressCallback callback) {
+        if (callback == null) {
+            return null;
+        } else {
+            if (mHandler == null) {
+                throw new IllegalStateException("Must construct LockPatternUtils on a looper thread"
+                        + " to use progress callbacks.");
+            }
+            return new ICheckCredentialProgressCallback.Stub() {
+
+                @Override
+                public void onCredentialVerified() throws RemoteException {
+                    mHandler.post(callback::onEarlyMatched);
+                }
+            };
+        }
+    }
+
+    /**
+     * Callback to be notified about progress when checking credentials.
+     */
+    public interface CheckCredentialProgressCallback {
+
+        /**
+         * Called as soon as possible when we know that the credentials match but the user hasn't
+         * been fully unlocked.
+         */
+        void onEarlyMatched();
+    }
+
     /**
      * Tracks the global strong authentication state.
      */
@@ -1484,7 +1543,8 @@
                 value = { STRONG_AUTH_NOT_REQUIRED,
                         STRONG_AUTH_REQUIRED_AFTER_BOOT,
                         STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW,
-                        SOME_AUTH_REQUIRED_AFTER_USER_REQUEST})
+                        SOME_AUTH_REQUIRED_AFTER_USER_REQUEST,
+                        STRONG_AUTH_REQUIRED_AFTER_LOCKOUT})
         @Retention(RetentionPolicy.SOURCE)
         public @interface StrongAuthFlags {}
 
@@ -1515,13 +1575,12 @@
         public static final int STRONG_AUTH_REQUIRED_AFTER_LOCKOUT = 0x8;
 
         /**
-         * Some authentication is required because the user has entered a wrong credential.
+         * Strong auth flags that do not prevent fingerprint from being accepted as auth.
+         *
+         * If any other flags are set, fingerprint is disabled.
          */
-        public static final int SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL = 0x10;
-
         private static final int ALLOWING_FINGERPRINT = STRONG_AUTH_NOT_REQUIRED
-                | SOME_AUTH_REQUIRED_AFTER_USER_REQUEST
-                | SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL;
+                | SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
 
         private final SparseIntArray mStrongAuthRequiredForUser = new SparseIntArray();
         private final H mHandler;
diff --git a/core/java/com/android/internal/widget/NotificationActionListLayout.java b/core/java/com/android/internal/widget/NotificationActionListLayout.java
index 9dd118c..073aac5 100644
--- a/core/java/com/android/internal/widget/NotificationActionListLayout.java
+++ b/core/java/com/android/internal/widget/NotificationActionListLayout.java
@@ -17,11 +17,13 @@
 package com.android.internal.widget;
 
 import android.content.Context;
+import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.util.Pair;
 import android.view.Gravity;
+import android.view.RemotableViewMethod;
 import android.view.View;
-import android.view.ViewGroup;
+import android.widget.LinearLayout;
 import android.widget.RemoteViews;
 import android.widget.TextView;
 
@@ -33,11 +35,14 @@
  * the remaining available width, and the last action consumes the remaining space.
  */
 @RemoteViews.RemoteView
-public class NotificationActionListLayout extends ViewGroup {
+public class NotificationActionListLayout extends LinearLayout {
 
     private int mTotalWidth = 0;
     private ArrayList<Pair<Integer, TextView>> mMeasureOrderTextViews = new ArrayList<>();
     private ArrayList<View> mMeasureOrderOther = new ArrayList<>();
+    private boolean mMeasureLinearly;
+    private int mDefaultPaddingEnd;
+    private Drawable mDefaultBackground;
 
     public NotificationActionListLayout(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -45,6 +50,10 @@
 
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        if (mMeasureLinearly) {
+            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+            return;
+        }
         final int N = getChildCount();
         int textViews = 0;
         int otherViews = 0;
@@ -186,6 +195,10 @@
 
     @Override
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+        if (mMeasureLinearly) {
+            super.onLayout(changed, left, top, right, bottom);
+            return;
+        }
         final boolean isLayoutRtl = isLayoutRtl();
         final int paddingTop = mPaddingTop;
 
@@ -241,26 +254,24 @@
     }
 
     @Override
-    public LayoutParams generateLayoutParams(AttributeSet attrs) {
-        return new MarginLayoutParams(getContext(), attrs);
+    protected void onFinishInflate() {
+        super.onFinishInflate();
+        mDefaultPaddingEnd = getPaddingEnd();
+        mDefaultBackground = getBackground();
     }
 
-    @Override
-    protected LayoutParams generateDefaultLayoutParams() {
-        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
-    }
-
-    @Override
-    protected LayoutParams generateLayoutParams(LayoutParams p) {
-        if (p instanceof MarginLayoutParams) {
-            return new MarginLayoutParams((MarginLayoutParams)p);
-        }
-        return new MarginLayoutParams(p);
-    }
-
-    @Override
-    protected boolean checkLayoutParams(LayoutParams p) {
-        return p instanceof MarginLayoutParams;
+    /**
+     * Set whether the list is in a mode where some actions are emphasized. This will trigger an
+     * equal measuring where all actions are full height and change a few parameters like
+     * the padding.
+     */
+    @RemotableViewMethod
+    public void setEmphasizedMode(boolean emphasizedMode) {
+        mMeasureLinearly = emphasizedMode;
+        setPaddingRelative(getPaddingStart(), getPaddingTop(),
+                emphasizedMode ? 0 : mDefaultPaddingEnd, getPaddingBottom());
+        setBackground(emphasizedMode ? null : mDefaultBackground);
+        requestLayout();
     }
 
     public static final Comparator<Pair<Integer, TextView>> MEASURE_ORDER_COMPARATOR
diff --git a/core/java/com/android/internal/widget/ViewPager.java b/core/java/com/android/internal/widget/ViewPager.java
index 277fafd..d5b6def 100644
--- a/core/java/com/android/internal/widget/ViewPager.java
+++ b/core/java/com/android/internal/widget/ViewPager.java
@@ -55,34 +55,7 @@
 import java.util.Comparator;
 
 /**
- * Layout manager that allows the user to flip left and right
- * through pages of data.  You supply an implementation of a
- * {@link android.support.v4.view.PagerAdapter} to generate the pages that the view shows.
- *
- * <p>Note this class is currently under early design and
- * development.  The API will likely change in later updates of
- * the compatibility library, requiring changes to the source code
- * of apps when they are compiled against the newer version.</p>
- *
- * <p>ViewPager is most often used in conjunction with {@link android.app.Fragment},
- * which is a convenient way to supply and manage the lifecycle of each page.
- * There are standard adapters implemented for using fragments with the ViewPager,
- * which cover the most common use cases.  These are
- * {@link android.support.v4.app.FragmentPagerAdapter} and
- * {@link android.support.v4.app.FragmentStatePagerAdapter}; each of these
- * classes have simple code showing how to build a full user interface
- * with them.
- *
- * <p>For more information about how to use ViewPager, read <a
- * href="{@docRoot}training/implementing-navigation/lateral.html">Creating Swipe Views with
- * Tabs</a>.</p>
- *
- * <p>Below is a more complicated example of ViewPager, using it in conjunction
- * with {@link android.app.ActionBar} tabs.  You can find other examples of using
- * ViewPager in the API 4+ Support Demos and API 13+ Support Demos sample code.
- *
- * {@sample development/samples/Support13Demos/src/com/example/android/supportv13/app/ActionBarTabsPager.java
- *      complete}
+ * Framework copy of the support-v4 ViewPager class.
  */
 public class ViewPager extends ViewGroup {
     private static final String TAG = "ViewPager";
diff --git a/core/java/com/android/internal/widget/WatchHeaderListView.java b/core/java/com/android/internal/widget/WatchHeaderListView.java
new file mode 100644
index 0000000..4fd19c3
--- /dev/null
+++ b/core/java/com/android/internal/widget/WatchHeaderListView.java
@@ -0,0 +1,196 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.widget;
+
+import android.annotation.IdRes;
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.ListAdapter;
+import android.widget.ListView;
+import android.widget.HeaderViewListAdapter;
+
+import java.util.ArrayList;
+
+import com.android.internal.util.Predicate;
+
+public class WatchHeaderListView extends ListView {
+    private View mTopPanel;
+
+    public WatchHeaderListView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public WatchHeaderListView(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    public WatchHeaderListView(
+            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+    }
+
+    @Override
+    protected HeaderViewListAdapter wrapHeaderListAdapterInternal(
+            ArrayList<ListView.FixedViewInfo> headerViewInfos,
+            ArrayList<ListView.FixedViewInfo> footerViewInfos,
+            ListAdapter adapter) {
+        return new WatchHeaderListAdapter(headerViewInfos, footerViewInfos, adapter);
+    }
+
+    @Override
+    public void addView(View child, ViewGroup.LayoutParams params) {
+        if (mTopPanel == null) {
+            setTopPanel(child);
+        } else {
+            throw new IllegalStateException("WatchHeaderListView can host only one header");
+        }
+    }
+
+    public void setTopPanel(View v) {
+        mTopPanel = v;
+        wrapAdapterIfNecessary();
+    }
+
+    @Override
+    public void setAdapter(ListAdapter adapter) {
+        super.setAdapter(adapter);
+        wrapAdapterIfNecessary();
+    }
+
+    @Override
+    protected View findViewTraversal(@IdRes int id) {
+        View v = super.findViewTraversal(id);
+        if (v == null && mTopPanel != null && !mTopPanel.isRootNamespace()) {
+            return mTopPanel.findViewById(id);
+        }
+        return v;
+    }
+
+    @Override
+    protected View findViewWithTagTraversal(Object tag) {
+        View v = super.findViewWithTagTraversal(tag);
+        if (v == null && mTopPanel != null && !mTopPanel.isRootNamespace()) {
+            return mTopPanel.findViewWithTag(tag);
+        }
+        return v;
+    }
+
+    @Override
+    protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
+        View v = super.findViewByPredicateTraversal(predicate, childToSkip);
+        if (v == null && mTopPanel != null && mTopPanel != childToSkip
+                && !mTopPanel.isRootNamespace()) {
+            return mTopPanel.findViewByPredicate(predicate);
+        }
+        return v;
+    }
+
+    @Override
+    public int getHeaderViewsCount() {
+        return mTopPanel == null ? super.getHeaderViewsCount()
+                : super.getHeaderViewsCount() + (mTopPanel.getVisibility() == GONE ? 0 : 1);
+    }
+
+    private void wrapAdapterIfNecessary() {
+        ListAdapter adapter = getAdapter();
+        if (adapter != null && mTopPanel != null) {
+            if (!(adapter instanceof WatchHeaderListAdapter)) {
+                wrapHeaderListAdapterInternal();
+            }
+
+            ((WatchHeaderListAdapter) getAdapter()).setTopPanel(mTopPanel);
+            dispatchDataSetObserverOnChangedInternal();
+        }
+    }
+
+    private static class WatchHeaderListAdapter extends HeaderViewListAdapter {
+        private View mTopPanel;
+
+        public WatchHeaderListAdapter(
+                ArrayList<ListView.FixedViewInfo> headerViewInfos,
+                ArrayList<ListView.FixedViewInfo> footerViewInfos,
+                ListAdapter adapter) {
+            super(headerViewInfos, footerViewInfos, adapter);
+        }
+
+        public void setTopPanel(View v) {
+            mTopPanel = v;
+        }
+
+        private int getTopPanelCount() {
+            return (mTopPanel == null || mTopPanel.getVisibility() == GONE) ? 0 : 1;
+        }
+
+        @Override
+        public int getCount() {
+            return super.getCount() + getTopPanelCount();
+        }
+
+        @Override
+        public boolean areAllItemsEnabled() {
+            return getTopPanelCount() == 0 && super.areAllItemsEnabled();
+        }
+
+        @Override
+        public boolean isEnabled(int position) {
+            int topPanelCount = getTopPanelCount();
+            return position < topPanelCount ? false : super.isEnabled(position - topPanelCount);
+        }
+
+        @Override
+        public Object getItem(int position) {
+            int topPanelCount = getTopPanelCount();
+            return position < topPanelCount ? null : super.getItem(position - topPanelCount);
+        }
+
+        @Override
+        public long getItemId(int position) {
+            int numHeaders = getHeadersCount() + getTopPanelCount();
+            if (getWrappedAdapter() != null && position >= numHeaders) {
+                int adjPosition = position - numHeaders;
+                int adapterCount = getWrappedAdapter().getCount();
+                if (adjPosition < adapterCount) {
+                    return getWrappedAdapter().getItemId(adjPosition);
+                }
+            }
+            return -1;
+        }
+
+        @Override
+        public View getView(int position, View convertView, ViewGroup parent) {
+            int topPanelCount = getTopPanelCount();
+            return position < topPanelCount
+                    ? mTopPanel : super.getView(position - topPanelCount, convertView, parent);
+        }
+
+        @Override
+        public int getItemViewType(int position) {
+            int numHeaders = getHeadersCount() + getTopPanelCount();
+            if (getWrappedAdapter() != null && position >= numHeaders) {
+                int adjPosition = position - numHeaders;
+                int adapterCount = getWrappedAdapter().getCount();
+                if (adjPosition < adapterCount) {
+                    return getWrappedAdapter().getItemViewType(adjPosition);
+                }
+            }
+
+            return AdapterView.ITEM_VIEW_TYPE_HEADER_OR_FOOTER;
+        }
+    }
+}
diff --git a/core/java/com/android/internal/widget/WatchListDecorLayout.java b/core/java/com/android/internal/widget/WatchListDecorLayout.java
new file mode 100644
index 0000000..5b49611
--- /dev/null
+++ b/core/java/com/android/internal/widget/WatchListDecorLayout.java
@@ -0,0 +1,328 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.internal.widget;
+
+import android.content.Context;
+import android.graphics.drawable.Drawable;
+import android.graphics.Rect;
+import android.util.AttributeSet;
+import android.view.Gravity;
+import android.view.View;
+import android.view.ViewTreeObserver;
+import android.widget.ListView;
+import android.widget.FrameLayout;
+
+import java.util.ArrayList;
+
+
+/**
+ * Layout for the decor for ListViews on watch-type devices with small screens.
+ * <p>
+ * Supports one panel with the gravity set to top, and one panel with gravity set to bottom.
+ * <p>
+ * Use with one ListView child. The top and bottom panels will track the ListView's scrolling.
+ * If there is no ListView child, it will act like a normal FrameLayout.
+ */
+public class WatchListDecorLayout extends FrameLayout
+        implements ViewTreeObserver.OnScrollChangedListener {
+
+    private int mForegroundPaddingLeft = 0;
+    private int mForegroundPaddingTop = 0;
+    private int mForegroundPaddingRight = 0;
+    private int mForegroundPaddingBottom = 0;
+
+    private final ArrayList<View> mMatchParentChildren = new ArrayList<>(1);
+
+    /** Track the amount the ListView has to scroll up to account for padding change difference. */
+    private int mPendingScroll;
+    private View mBottomPanel;
+    private View mTopPanel;
+    private ListView mListView;
+    private ViewTreeObserver mObserver;
+
+
+    public WatchListDecorLayout(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public WatchListDecorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    public WatchListDecorLayout(
+            Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+    }
+
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+
+        mPendingScroll = 0;
+
+        for (int i = 0; i < getChildCount(); ++i) {
+            View child = getChildAt(i);
+            if (child instanceof ListView) {
+                if (mListView != null) {
+                    throw new IllegalArgumentException("only one ListView child allowed");
+                }
+                mListView = (ListView) child;
+
+                mListView.setNestedScrollingEnabled(true);
+                mObserver = mListView.getViewTreeObserver();
+                mObserver.addOnScrollChangedListener(this);
+            } else {
+                int gravity = (((LayoutParams) child.getLayoutParams()).gravity
+                        & Gravity.VERTICAL_GRAVITY_MASK);
+                if (gravity == Gravity.TOP && mTopPanel == null) {
+                    mTopPanel = child;
+                } else if (gravity == Gravity.BOTTOM && mBottomPanel == null) {
+                    mBottomPanel = child;
+                }
+            }
+        }
+    }
+
+    @Override
+    public void onDetachedFromWindow() {
+        mListView = null;
+        mBottomPanel = null;
+        mTopPanel = null;
+        if (mObserver != null) {
+            if (mObserver.isAlive()) {
+                mObserver.removeOnScrollChangedListener(this);
+            }
+            mObserver = null;
+        }
+    }
+
+    private void applyMeasureToChild(View child, int widthMeasureSpec, int heightMeasureSpec) {
+        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
+
+        final int childWidthMeasureSpec;
+        if (lp.width == LayoutParams.MATCH_PARENT) {
+            final int width = Math.max(0, getMeasuredWidth()
+                    - getPaddingLeftWithForeground() - getPaddingRightWithForeground()
+                    - lp.leftMargin - lp.rightMargin);
+            childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
+                    width, MeasureSpec.EXACTLY);
+        } else {
+            childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,
+                    getPaddingLeftWithForeground() + getPaddingRightWithForeground() +
+                    lp.leftMargin + lp.rightMargin,
+                    lp.width);
+        }
+
+        final int childHeightMeasureSpec;
+        if (lp.height == LayoutParams.MATCH_PARENT) {
+            final int height = Math.max(0, getMeasuredHeight()
+                    - getPaddingTopWithForeground() - getPaddingBottomWithForeground()
+                    - lp.topMargin - lp.bottomMargin);
+            childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
+                    height, MeasureSpec.EXACTLY);
+        } else {
+            childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec,
+                    getPaddingTopWithForeground() + getPaddingBottomWithForeground() +
+                    lp.topMargin + lp.bottomMargin,
+                    lp.height);
+        }
+
+        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
+    }
+
+    private int measureAndGetHeight(View child, int widthMeasureSpec, int heightMeasureSpec) {
+        if (child != null) {
+            if (child.getVisibility() != GONE) {
+                applyMeasureToChild(mBottomPanel, widthMeasureSpec, heightMeasureSpec);
+                return child.getMeasuredHeight();
+            } else if (getMeasureAllChildren()) {
+                applyMeasureToChild(mBottomPanel, widthMeasureSpec, heightMeasureSpec);
+            }
+        }
+        return 0;
+    }
+
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        int count = getChildCount();
+
+        final boolean measureMatchParentChildren =
+                MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
+                MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
+        mMatchParentChildren.clear();
+
+        int maxHeight = 0;
+        int maxWidth = 0;
+        int childState = 0;
+
+        for (int i = 0; i < count; i++) {
+            final View child = getChildAt(i);
+            if (getMeasureAllChildren() || child.getVisibility() != GONE) {
+                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
+                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
+                maxWidth = Math.max(maxWidth,
+                        child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
+                maxHeight = Math.max(maxHeight,
+                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
+                childState = combineMeasuredStates(childState, child.getMeasuredState());
+                if (measureMatchParentChildren) {
+                    if (lp.width == LayoutParams.MATCH_PARENT ||
+                            lp.height == LayoutParams.MATCH_PARENT) {
+                        mMatchParentChildren.add(child);
+                    }
+                }
+            }
+        }
+
+        // Account for padding too
+        maxWidth += getPaddingLeftWithForeground() + getPaddingRightWithForeground();
+        maxHeight += getPaddingTopWithForeground() + getPaddingBottomWithForeground();
+
+        // Check against our minimum height and width
+        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
+        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
+
+        // Check against our foreground's minimum height and width
+        final Drawable drawable = getForeground();
+        if (drawable != null) {
+            maxHeight = Math.max(maxHeight, drawable.getMinimumHeight());
+            maxWidth = Math.max(maxWidth, drawable.getMinimumWidth());
+        }
+
+        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
+                resolveSizeAndState(maxHeight, heightMeasureSpec,
+                        childState << MEASURED_HEIGHT_STATE_SHIFT));
+
+        if (mListView != null) {
+            if (mPendingScroll != 0) {
+                mListView.scrollListBy(mPendingScroll);
+                mPendingScroll = 0;
+            }
+
+            int paddingTop = Math.max(mListView.getPaddingTop(),
+                    measureAndGetHeight(mTopPanel, widthMeasureSpec, heightMeasureSpec));
+            int paddingBottom = Math.max(mListView.getPaddingBottom(),
+                    measureAndGetHeight(mBottomPanel, widthMeasureSpec, heightMeasureSpec));
+
+            if (paddingTop != mListView.getPaddingTop()
+                    || paddingBottom != mListView.getPaddingBottom()) {
+                mPendingScroll += mListView.getPaddingTop() - paddingTop;
+                mListView.setPadding(
+                        mListView.getPaddingLeft(), paddingTop,
+                        mListView.getPaddingRight(), paddingBottom);
+            }
+        }
+
+        count = mMatchParentChildren.size();
+        if (count > 1) {
+            for (int i = 0; i < count; i++) {
+                final View child = mMatchParentChildren.get(i);
+                if (mListView == null || (child != mTopPanel && child != mBottomPanel)) {
+                    applyMeasureToChild(child, widthMeasureSpec, heightMeasureSpec);
+                }
+            }
+        }
+    }
+
+    @Override
+    public void setForegroundGravity(int foregroundGravity) {
+        if (getForegroundGravity() != foregroundGravity) {
+            super.setForegroundGravity(foregroundGravity);
+
+            // calling get* again here because the set above may apply default constraints
+            final Drawable foreground = getForeground();
+            if (getForegroundGravity() == Gravity.FILL && foreground != null) {
+                Rect padding = new Rect();
+                if (foreground.getPadding(padding)) {
+                    mForegroundPaddingLeft = padding.left;
+                    mForegroundPaddingTop = padding.top;
+                    mForegroundPaddingRight = padding.right;
+                    mForegroundPaddingBottom = padding.bottom;
+                }
+            } else {
+                mForegroundPaddingLeft = 0;
+                mForegroundPaddingTop = 0;
+                mForegroundPaddingRight = 0;
+                mForegroundPaddingBottom = 0;
+            }
+        }
+    }
+
+    private int getPaddingLeftWithForeground() {
+        return isForegroundInsidePadding() ? Math.max(mPaddingLeft, mForegroundPaddingLeft) :
+            mPaddingLeft + mForegroundPaddingLeft;
+    }
+
+    private int getPaddingRightWithForeground() {
+        return isForegroundInsidePadding() ? Math.max(mPaddingRight, mForegroundPaddingRight) :
+            mPaddingRight + mForegroundPaddingRight;
+    }
+
+    private int getPaddingTopWithForeground() {
+        return isForegroundInsidePadding() ? Math.max(mPaddingTop, mForegroundPaddingTop) :
+            mPaddingTop + mForegroundPaddingTop;
+    }
+
+    private int getPaddingBottomWithForeground() {
+        return isForegroundInsidePadding() ? Math.max(mPaddingBottom, mForegroundPaddingBottom) :
+            mPaddingBottom + mForegroundPaddingBottom;
+    }
+
+    @Override
+    public void onScrollChanged() {
+        if (mListView == null) {
+            return;
+        }
+
+        if (mTopPanel != null) {
+            if (mListView.getChildCount() > 0) {
+                if (mListView.getFirstVisiblePosition() == 0) {
+                    View firstChild = mListView.getChildAt(0);
+                    setScrolling(mTopPanel,
+                            firstChild.getY() - mTopPanel.getHeight() - mTopPanel.getTop());
+                } else {
+                    // shift to hide the frame, last child is not the last position
+                    setScrolling(mTopPanel, -mTopPanel.getHeight());
+                }
+            } else {
+                setScrolling(mTopPanel, 0); // no visible child, fallback to default behaviour
+            }
+        }
+
+        if (mBottomPanel != null) {
+            if (mListView.getChildCount() > 0) {
+                if (mListView.getLastVisiblePosition() >= mListView.getCount() - 1) {
+                    View lastChild = mListView.getChildAt(mListView.getChildCount() - 1);
+                    setScrolling(mBottomPanel, Math.max(
+                            0,
+                            lastChild.getY() + lastChild.getHeight() - mBottomPanel.getTop()));
+                } else {
+                    // shift to hide the frame, last child is not the last position
+                    setScrolling(mBottomPanel, mBottomPanel.getHeight());
+                }
+            } else {
+                setScrolling(mBottomPanel, 0); // no visible child, fallback to default behaviour
+            }
+        }
+    }
+
+    /** Only set scrolling for the panel if there is a change in its translationY. */
+    private void setScrolling(View panel, float translationY) {
+        if (panel.getTranslationY() != translationY) {
+            panel.setTranslationY(translationY);
+        }
+    }
+}
diff --git a/core/java/com/android/server/BootReceiver.java b/core/java/com/android/server/BootReceiver.java
index fbc51cd..5a50fbf 100644
--- a/core/java/com/android/server/BootReceiver.java
+++ b/core/java/com/android/server/BootReceiver.java
@@ -79,6 +79,9 @@
     private static final String LOG_FILES_FILE = "log-files.xml";
     private static final AtomicFile sFile = new AtomicFile(new File(
             Environment.getDataSystemDirectory(), LOG_FILES_FILE));
+    private static final String LAST_HEADER_FILE = "last-header.txt";
+    private static final File lastHeaderFile = new File(
+            Environment.getDataSystemDirectory(), LAST_HEADER_FILE);
 
     @Override
     public void onReceive(final Context context, Intent intent) {
@@ -113,9 +116,17 @@
         Downloads.removeAllDownloadsByPackage(context, OLD_UPDATER_PACKAGE, OLD_UPDATER_CLASS);
     }
 
-    private void logBootEvents(Context ctx) throws IOException {
-        final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE);
-        final String headers = new StringBuilder(512)
+    private String getPreviousBootHeaders() {
+        try {
+            return FileUtils.readTextFile(lastHeaderFile, 0, null);
+        } catch (IOException e) {
+            Slog.e(TAG, "Error reading " + lastHeaderFile, e);
+            return null;
+        }
+    }
+
+    private String getCurrentBootHeaders() throws IOException {
+        return new StringBuilder(512)
             .append("Build: ").append(Build.FINGERPRINT).append("\n")
             .append("Hardware: ").append(Build.BOARD).append("\n")
             .append("Revision: ")
@@ -125,6 +136,31 @@
             .append("Kernel: ")
             .append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n"))
             .append("\n").toString();
+    }
+
+
+    private String getBootHeadersToLogAndUpdate() throws IOException {
+        final String oldHeaders = getPreviousBootHeaders();
+        final String newHeaders = getCurrentBootHeaders();
+
+        try {
+            FileUtils.stringToFile(lastHeaderFile, newHeaders);
+        } catch (IOException e) {
+            Slog.e(TAG, "Error writing " + lastHeaderFile, e);
+        }
+
+        if (oldHeaders == null) {
+            // If we failed to read the old headers, use the current headers
+            // but note this in the headers so we know
+            return "isPrevious: false\n" + newHeaders;
+        }
+
+        return "isPrevious: true\n" + oldHeaders;
+    }
+
+    private void logBootEvents(Context ctx) throws IOException {
+        final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE);
+        final String headers = getBootHeadersToLogAndUpdate();
         final String bootReason = SystemProperties.get("ro.boot.bootreason", null);
 
         String recovery = RecoverySystem.handleAftermath(ctx);
diff --git a/core/java/com/android/server/SystemConfig.java b/core/java/com/android/server/SystemConfig.java
index 886265a..429131b 100644
--- a/core/java/com/android/server/SystemConfig.java
+++ b/core/java/com/android/server/SystemConfig.java
@@ -42,6 +42,8 @@
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 
 /**
  * Loads global system configuration info.
@@ -122,6 +124,11 @@
     // These are the permitted backup transport service components
     final ArraySet<ComponentName> mBackupTransportWhitelist = new ArraySet<>();
 
+    // These are the packages of carrier-associated apps which should be disabled until used until
+    // a SIM is inserted which grants carrier privileges to that carrier app.
+    final ArrayMap<String, List<String>> mDisabledUntilUsedPreinstalledCarrierAssociatedApps =
+            new ArrayMap<>();
+
     public static SystemConfig getInstance() {
         synchronized (SystemConfig.class) {
             if (sInstance == null) {
@@ -183,6 +190,10 @@
         return mBackupTransportWhitelist;
     }
 
+    public ArrayMap<String, List<String>> getDisabledUntilUsedPreinstalledCarrierAssociatedApps() {
+        return mDisabledUntilUsedPreinstalledCarrierAssociatedApps;
+    }
+
     SystemConfig() {
         // Read configuration from system
         readPermissions(Environment.buildPath(
@@ -476,6 +487,26 @@
                         }
                     }
                     XmlUtils.skipCurrentTag(parser);
+                } else if ("disabled-until-used-preinstalled-carrier-associated-app".equals(name)
+                        && allowAppConfigs) {
+                    String pkgname = parser.getAttributeValue(null, "package");
+                    String carrierPkgname = parser.getAttributeValue(null, "carrierAppPackage");
+                    if (pkgname == null || carrierPkgname == null) {
+                        Slog.w(TAG, "<disabled-until-used-preinstalled-carrier-associated-app"
+                                + " without package or carrierAppPackage in " + permFile + " at "
+                                + parser.getPositionDescription());
+                    } else {
+                        List<String> associatedPkgs =
+                                mDisabledUntilUsedPreinstalledCarrierAssociatedApps.get(
+                                        carrierPkgname);
+                        if (associatedPkgs == null) {
+                            associatedPkgs = new ArrayList<>();
+                            mDisabledUntilUsedPreinstalledCarrierAssociatedApps.put(
+                                    carrierPkgname, associatedPkgs);
+                        }
+                        associatedPkgs.add(pkgname);
+                    }
+                    XmlUtils.skipCurrentTag(parser);
                 } else {
                     XmlUtils.skipCurrentTag(parser);
                     continue;
diff --git a/core/java/com/android/server/backup/AccountManagerBackupHelper.java b/core/java/com/android/server/backup/AccountManagerBackupHelper.java
new file mode 100644
index 0000000..39b18c0
--- /dev/null
+++ b/core/java/com/android/server/backup/AccountManagerBackupHelper.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.backup;
+
+import android.accounts.AccountManagerInternal;
+import android.app.backup.BlobBackupHelper;
+import android.os.UserHandle;
+import android.util.Slog;
+import com.android.server.LocalServices;
+
+/**
+ * Helper for handling backup of account manager specific state.
+ */
+public class AccountManagerBackupHelper extends BlobBackupHelper {
+    private static final String TAG = "AccountsBackup";
+    private static final boolean DEBUG = false;
+
+    // current schema of the backup state blob
+    private static final int STATE_VERSION = 1;
+
+    // key under which the account access grant state blob is committed to backup
+    private static final String KEY_ACCOUNT_ACCESS_GRANTS = "account_access_grants";
+
+    public AccountManagerBackupHelper() {
+        super(STATE_VERSION, KEY_ACCOUNT_ACCESS_GRANTS);
+    }
+
+    @Override
+    protected byte[] getBackupPayload(String key) {
+        AccountManagerInternal am = LocalServices.getService(AccountManagerInternal.class);
+        if (DEBUG) {
+            Slog.d(TAG, "Handling backup of " + key);
+        }
+        try {
+            switch (key) {
+                case KEY_ACCOUNT_ACCESS_GRANTS: {
+                    return am.backupAccountAccessPermissions(UserHandle.USER_SYSTEM);
+                }
+
+                default: {
+                    Slog.w(TAG, "Unexpected backup key " + key);
+                }
+            }
+        } catch (Exception e) {
+            Slog.e(TAG, "Unable to store payload " + key);
+        }
+
+        return new byte[0];
+    }
+
+    @Override
+    protected void applyRestoredPayload(String key, byte[] payload) {
+        AccountManagerInternal am = LocalServices.getService(AccountManagerInternal.class);
+        if (DEBUG) {
+            Slog.d(TAG, "Handling restore of " + key);
+        }
+        try {
+            switch (key) {
+                case KEY_ACCOUNT_ACCESS_GRANTS: {
+                    am.restoreAccountAccessPermissions(payload, UserHandle.USER_SYSTEM);
+                } break;
+
+                default: {
+                    Slog.w(TAG, "Unexpected restore key " + key);
+                }
+            }
+        } catch (Exception e) {
+            Slog.w(TAG, "Unable to restore key " + key);
+        }
+    }
+}
diff --git a/core/java/com/android/server/backup/SystemBackupAgent.java b/core/java/com/android/server/backup/SystemBackupAgent.java
index 9d296fa..5375651 100644
--- a/core/java/com/android/server/backup/SystemBackupAgent.java
+++ b/core/java/com/android/server/backup/SystemBackupAgent.java
@@ -49,6 +49,7 @@
     private static final String PERMISSION_HELPER = "permissions";
     private static final String USAGE_STATS_HELPER = "usage_stats";
     private static final String SHORTCUT_MANAGER_HELPER = "shortcut_manager";
+    private static final String ACCOUNT_MANAGER_HELPER = "account_manager";
 
     // These paths must match what the WallpaperManagerService uses.  The leaf *_FILENAME
     // are also used in the full-backup file format, so must not change unless steps are
@@ -82,6 +83,7 @@
         addHelper(PERMISSION_HELPER, new PermissionBackupHelper());
         addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
         addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper());
+        addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper());
         super.onBackup(oldState, data, newState);
     }
 
@@ -111,6 +113,7 @@
         addHelper(PERMISSION_HELPER, new PermissionBackupHelper());
         addHelper(USAGE_STATS_HELPER, new UsageStatsBackupHelper(this));
         addHelper(SHORTCUT_MANAGER_HELPER, new ShortcutBackupHelper());
+        addHelper(ACCOUNT_MANAGER_HELPER, new AccountManagerBackupHelper());
 
         try {
             super.onRestore(data, appVersionCode, newState);
diff --git a/core/jni/android/graphics/Bitmap.cpp b/core/jni/android/graphics/Bitmap.cpp
index 36211bb..18c4ee3 100755
--- a/core/jni/android/graphics/Bitmap.cpp
+++ b/core/jni/android/graphics/Bitmap.cpp
@@ -18,6 +18,7 @@
 #include "CreateJavaOutputStreamAdaptor.h"
 #include <Caches.h>
 #include <hwui/Paint.h>
+#include <renderthread/RenderProxy.h>
 
 #include "core_jni_helpers.h"
 
@@ -1361,6 +1362,14 @@
     return reinterpret_cast<jlong>(pixelRef);
 }
 
+static void Bitmap_prepareToDraw(JNIEnv* env, jobject, jlong bitmapPtr) {
+    LocalScopedBitmap bitmapHandle(bitmapPtr);
+    if (!bitmapHandle.valid()) return;
+    SkBitmap bitmap;
+    bitmapHandle->getSkBitmap(&bitmap);
+    android::uirenderer::renderthread::RenderProxy::prepareToDraw(bitmap);
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 static const JNINativeMethod gBitmapMethods[] = {
@@ -1404,6 +1413,7 @@
                                             (void*)Bitmap_copyPixelsFromBuffer },
     {   "nativeSameAs",             "(JJ)Z", (void*)Bitmap_sameAs },
     {   "nativeRefPixelRef",        "(J)J", (void*)Bitmap_refPixelRef },
+    {   "nativePrepareToDraw",      "(J)V", (void*)Bitmap_prepareToDraw },
 };
 
 int register_android_graphics_Bitmap(JNIEnv* env)
diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp
index 898cf77..85092ad 100644
--- a/core/jni/android/graphics/Paint.cpp
+++ b/core/jni/android/graphics/Paint.cpp
@@ -768,6 +768,21 @@
         return false;
     }
 
+    // Don't count glyphs that are the recommended "space" glyph and are zero width.
+    // This logic makes assumptions about HarfBuzz layout, but does correctly handle
+    // cases where ligatures form and zero width space glyphs are left in as
+    // placeholders.
+    static size_t countNonSpaceGlyphs(const Layout& layout) {
+        size_t count = 0;
+        static unsigned int kSpaceGlyphId = 3;
+        for (size_t i = 0; i < layout.nGlyphs(); i++) {
+            if (layout.getGlyphId(i) != kSpaceGlyphId || layout.getCharAdvance(i) != 0.0) {
+                count++;
+            }
+        }
+        return count;
+    }
+
     // Returns true if the given string is exact one pair of regional indicators.
     static bool isFlag(const jchar* str, size_t length) {
         const jchar RI_LEAD_SURROGATE = 0xD83C;
@@ -831,7 +846,7 @@
         Layout layout;
         MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, str.get(), 0, str.size(),
                 str.size());
-        size_t nGlyphs = layout.nGlyphs();
+        size_t nGlyphs = countNonSpaceGlyphs(layout);
         if (nGlyphs != 1 && nChars > 1) {
             // multiple-character input, and was not a ligature
             // TODO: handle ZWJ/ZWNJ characters specially so we can detect certain ligatures
diff --git a/core/jni/android/graphics/pdf/PdfEditor.cpp b/core/jni/android/graphics/pdf/PdfEditor.cpp
index d2d39cd..0468b64 100644
--- a/core/jni/android/graphics/pdf/PdfEditor.cpp
+++ b/core/jni/android/graphics/pdf/PdfEditor.cpp
@@ -52,11 +52,9 @@
 } gRectClassInfo;
 
 // Also used in PdfRenderer.cpp
-Mutex sPdfiumLock;
 int sUnmatchedPdfiumInitRequestCount = 0;
 
 static void initializeLibraryIfNeeded() {
-    Mutex::Autolock _l(sPdfiumLock);
     if (sUnmatchedPdfiumInitRequestCount == 0) {
         FPDF_InitLibrary();
     }
@@ -64,7 +62,6 @@
 }
 
 static void destroyLibraryIfNeeded() {
-    Mutex::Autolock _l(sPdfiumLock);
     sUnmatchedPdfiumInitRequestCount--;
     if (sUnmatchedPdfiumInitRequestCount == 0) {
        FPDF_DestroyLibrary();
diff --git a/core/jni/android/graphics/pdf/PdfRenderer.cpp b/core/jni/android/graphics/pdf/PdfRenderer.cpp
index 71bec78..43550ac 100644
--- a/core/jni/android/graphics/pdf/PdfRenderer.cpp
+++ b/core/jni/android/graphics/pdf/PdfRenderer.cpp
@@ -44,11 +44,9 @@
 } gPointClassInfo;
 
 // See PdfEditor.cpp
-extern Mutex sPdfiumLock;
 extern int sUnmatchedPdfiumInitRequestCount;
 
 static void initializeLibraryIfNeeded() {
-    Mutex::Autolock _l(sPdfiumLock);
     if (sUnmatchedPdfiumInitRequestCount == 0) {
         FPDF_InitLibrary();
     }
@@ -56,7 +54,6 @@
 }
 
 static void destroyLibraryIfNeeded() {
-    Mutex::Autolock _l(sPdfiumLock);
     sUnmatchedPdfiumInitRequestCount--;
     if (sUnmatchedPdfiumInitRequestCount == 0) {
        FPDF_DestroyLibrary();
diff --git a/core/jni/android_app_ApplicationLoaders.cpp b/core/jni/android_app_ApplicationLoaders.cpp
index 24ee959..3e7c039 100644
--- a/core/jni/android_app_ApplicationLoaders.cpp
+++ b/core/jni/android_app_ApplicationLoaders.cpp
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "ApplicationLoaders"
+
 #include <nativehelper/ScopedUtfChars.h>
 #include <nativeloader/native_loader.h>
 #include <vulkan/vulkan_loader_data.h>
@@ -22,10 +24,17 @@
 
 static void setupVulkanLayerPath_native(JNIEnv* env, jobject clazz,
         jobject classLoader, jstring librarySearchPath) {
+    android_namespace_t* ns = android::FindNamespaceByClassLoader(env, classLoader);
     ScopedUtfChars layerPathChars(env, librarySearchPath);
+
     vulkan::LoaderData& loader_data = vulkan::LoaderData::GetInstance();
-    loader_data.layer_path = layerPathChars.c_str();
-    loader_data.app_namespace = android::FindNamespaceByClassLoader(env, classLoader);
+    if (loader_data.layer_path.empty()) {
+        loader_data.layer_path = layerPathChars.c_str();
+        loader_data.app_namespace = ns;
+    } else {
+        ALOGD("ignored Vulkan layer search path %s for namespace %p",
+                layerPathChars.c_str(), ns);
+    }
 }
 
 static const JNINativeMethod g_methods[] = {
diff --git a/core/jni/android_database_CursorWindow.cpp b/core/jni/android_database_CursorWindow.cpp
index 7213414..3fc3aaf 100644
--- a/core/jni/android_database_CursorWindow.cpp
+++ b/core/jni/android_database_CursorWindow.cpp
@@ -16,6 +16,7 @@
 
 #undef LOG_TAG
 #define LOG_TAG "CursorWindow"
+#define LOG_NDEBUG 0
 
 #include <inttypes.h>
 #include <jni.h>
@@ -30,6 +31,11 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <dirent.h>
+
+#undef LOG_NDEBUG
+#define LOG_NDEBUG 1
 
 #include <androidfw/CursorWindow.h>
 #include "android_os_Parcel.h"
@@ -61,6 +67,22 @@
     jniThrowException(env, "java/lang/IllegalStateException", msg.string());
 }
 
+static int getFdCount() {
+    char fdpath[PATH_MAX];
+    int count = 0;
+    snprintf(fdpath, PATH_MAX, "/proc/%d/fd", getpid());
+    DIR *dir = opendir(fdpath);
+    if (dir != NULL) {
+        struct dirent *dirent;
+        while ((dirent = readdir(dir))) {
+            count++;
+        }
+        count -= 2; // discount "." and ".."
+        closedir(dir);
+    }
+    return count;
+}
+
 static jlong nativeCreate(JNIEnv* env, jclass clazz, jstring nameObj, jint cursorWindowSize) {
     String8 name;
     const char* nameStr = env->GetStringUTFChars(nameObj, NULL);
@@ -85,7 +107,8 @@
     CursorWindow* window;
     status_t status = CursorWindow::createFromParcel(parcel, &window);
     if (status || !window) {
-        ALOGE("Could not create CursorWindow from Parcel due to error %d.", status);
+        ALOGE("Could not create CursorWindow from Parcel due to error %d, process fd count=%d",
+                status, getFdCount());
         return 0;
     }
 
diff --git a/core/jni/android_graphics_Canvas.cpp b/core/jni/android_graphics_Canvas.cpp
index ded4dac..2ab4a35 100644
--- a/core/jni/android_graphics_Canvas.cpp
+++ b/core/jni/android_graphics_Canvas.cpp
@@ -614,10 +614,10 @@
     {"native_drawPath","!(JJJ)V", (void*) CanvasJNI::drawPath},
     {"nativeDrawVertices", "!(JII[FI[FI[II[SIIJ)V", (void*)CanvasJNI::drawVertices},
     {"native_drawNinePatch", "!(JJJFFFFJII)V", (void*)CanvasJNI::drawNinePatch},
-    {"native_drawBitmap","!(JLandroid/graphics/Bitmap;FFJIII)V", (void*) CanvasJNI::drawBitmap},
+    {"native_drawBitmap","(JLandroid/graphics/Bitmap;FFJIII)V", (void*) CanvasJNI::drawBitmap},
     {"nativeDrawBitmapMatrix", "!(JLandroid/graphics/Bitmap;JJ)V", (void*)CanvasJNI::drawBitmapMatrix},
-    {"native_drawBitmap","!(JLandroid/graphics/Bitmap;FFFFFFFFJII)V", (void*) CanvasJNI::drawBitmapRect},
-    {"native_drawBitmap", "!(J[IIIFFIIZJ)V", (void*)CanvasJNI::drawBitmapArray},
+    {"native_drawBitmap","(JLandroid/graphics/Bitmap;FFFFFFFFJII)V", (void*) CanvasJNI::drawBitmapRect},
+    {"native_drawBitmap", "(J[IIIFFIIZJ)V", (void*)CanvasJNI::drawBitmapArray},
     {"nativeDrawBitmapMesh", "!(JLandroid/graphics/Bitmap;II[FI[IIJ)V", (void*)CanvasJNI::drawBitmapMesh},
     {"native_drawText","!(J[CIIFFIJJ)V", (void*) CanvasJNI::drawTextChars},
     {"native_drawText","!(JLjava/lang/String;IIFFIJJ)V", (void*) CanvasJNI::drawTextString},
diff --git a/core/jni/android_graphics_drawable_AnimatedVectorDrawable.cpp b/core/jni/android_graphics_drawable_AnimatedVectorDrawable.cpp
index 4b2a72d..ade718b 100644
--- a/core/jni/android_graphics_drawable_AnimatedVectorDrawable.cpp
+++ b/core/jni/android_graphics_drawable_AnimatedVectorDrawable.cpp
@@ -83,11 +83,13 @@
 }
 
 static void addAnimator(JNIEnv*, jobject, jlong animatorSetPtr, jlong propertyHolderPtr,
-        jlong interpolatorPtr, jlong startDelay, jlong duration, jint repeatCount) {
+        jlong interpolatorPtr, jlong startDelay, jlong duration, jint repeatCount,
+        jint repeatMode) {
     PropertyValuesAnimatorSet* set = reinterpret_cast<PropertyValuesAnimatorSet*>(animatorSetPtr);
     PropertyValuesHolder* holder = reinterpret_cast<PropertyValuesHolder*>(propertyHolderPtr);
     Interpolator* interpolator = reinterpret_cast<Interpolator*>(interpolatorPtr);
-    set->addPropertyAnimator(holder, interpolator, startDelay, duration, repeatCount);
+    RepeatMode mode = static_cast<RepeatMode>(repeatMode);
+    set->addPropertyAnimator(holder, interpolator, startDelay, duration, repeatCount, mode);
 }
 
 static jlong createAnimatorSet(JNIEnv*, jobject) {
@@ -95,6 +97,12 @@
     return reinterpret_cast<jlong>(animatorSet);
 }
 
+static void setVectorDrawableTarget(JNIEnv*, jobject,jlong animatorPtr, jlong vectorDrawablePtr) {
+    VectorDrawable::Tree* tree = reinterpret_cast<VectorDrawable::Tree*>(vectorDrawablePtr);
+    PropertyValuesAnimatorSet* set = reinterpret_cast<PropertyValuesAnimatorSet*>(animatorPtr);
+    set->setVectorDrawable(tree);
+}
+
 static jlong createGroupPropertyHolder(JNIEnv*, jobject, jlong nativePtr, jint propertyId,
         jfloat startValue, jfloat endValue) {
     VectorDrawable::Group* group = reinterpret_cast<VectorDrawable::Group*>(nativePtr);
@@ -136,14 +144,24 @@
             startValue, endValue);
     return reinterpret_cast<jlong>(newHolder);
 }
-static void setPropertyHolderData(JNIEnv* env, jobject, jlong propertyHolderPtr,
+static void setFloatPropertyHolderData(JNIEnv* env, jobject, jlong propertyHolderPtr,
         jfloatArray srcData, jint length) {
-
     jfloat* propertyData = env->GetFloatArrayElements(srcData, nullptr);
-    PropertyValuesHolder* holder = reinterpret_cast<PropertyValuesHolder*>(propertyHolderPtr);
+    PropertyValuesHolderImpl<float>* holder =
+            reinterpret_cast<PropertyValuesHolderImpl<float>*>(propertyHolderPtr);
     holder->setPropertyDataSource(propertyData, length);
     env->ReleaseFloatArrayElements(srcData, propertyData, JNI_ABORT);
 }
+
+static void setIntPropertyHolderData(JNIEnv* env, jobject, jlong propertyHolderPtr,
+        jintArray srcData, jint length) {
+    jint* propertyData = env->GetIntArrayElements(srcData, nullptr);
+    PropertyValuesHolderImpl<int>* holder =
+            reinterpret_cast<PropertyValuesHolderImpl<int>*>(propertyHolderPtr);
+    holder->setPropertyDataSource(propertyData, length);
+    env->ReleaseIntArrayElements(srcData, propertyData, JNI_ABORT);
+}
+
 static void start(JNIEnv* env, jobject, jlong animatorSetPtr, jobject finishListener, jint id) {
     PropertyValuesAnimatorSet* set = reinterpret_cast<PropertyValuesAnimatorSet*>(animatorSetPtr);
     AnimationListener* listener = createAnimationListener(env, finishListener, id);
@@ -168,13 +186,15 @@
 
 static const JNINativeMethod gMethods[] = {
     {"nCreateAnimatorSet", "()J", (void*)createAnimatorSet},
-    {"nAddAnimator", "(JJJJJI)V", (void*)addAnimator},
+    {"nSetVectorDrawableTarget", "(JJ)V", (void*)setVectorDrawableTarget},
+    {"nAddAnimator", "(JJJJJII)V", (void*)addAnimator},
     {"nCreateGroupPropertyHolder", "!(JIFF)J", (void*)createGroupPropertyHolder},
     {"nCreatePathDataPropertyHolder", "!(JJJ)J", (void*)createPathDataPropertyHolder},
     {"nCreatePathColorPropertyHolder", "!(JIII)J", (void*)createPathColorPropertyHolder},
     {"nCreatePathPropertyHolder", "!(JIFF)J", (void*)createPathPropertyHolder},
     {"nCreateRootAlphaPropertyHolder", "!(JFF)J", (void*)createRootAlphaPropertyHolder},
-    {"nSetPropertyHolderData", "(J[FI)V", (void*)setPropertyHolderData},
+    {"nSetPropertyHolderData", "(J[FI)V", (void*)setFloatPropertyHolderData},
+    {"nSetPropertyHolderData", "(J[II)V", (void*)setIntPropertyHolderData},
     {"nStart", "(JLandroid/graphics/drawable/AnimatedVectorDrawable$VectorDrawableAnimatorRT;I)V", (void*)start},
     {"nReverse", "(JLandroid/graphics/drawable/AnimatedVectorDrawable$VectorDrawableAnimatorRT;I)V", (void*)reverse},
     {"nEnd", "!(J)V", (void*)end},
diff --git a/core/jni/android_hardware_Camera.cpp b/core/jni/android_hardware_Camera.cpp
index 9459257..b926270 100644
--- a/core/jni/android_hardware_Camera.cpp
+++ b/core/jni/android_hardware_Camera.cpp
@@ -350,9 +350,16 @@
     postData(msgType, dataPtr, NULL);
 }
 
-void JNICameraContext::postRecordingFrameHandleTimestamp(nsecs_t, native_handle_t*) {
-    // This is not needed at app layer. This should not be called because JNICameraContext cannot
-    // start video recording.
+void JNICameraContext::postRecordingFrameHandleTimestamp(nsecs_t, native_handle_t* handle) {
+    // Video buffers are not needed at app layer so just return the video buffers here.
+    // This may be called when stagefright just releases camera but there are still outstanding
+    // video buffers.
+    if (mCamera != nullptr) {
+        mCamera->releaseRecordingFrameHandle(handle);
+    } else {
+        native_handle_close(handle);
+        native_handle_delete(handle);
+    }
 }
 
 void JNICameraContext::postMetadata(JNIEnv *env, int32_t msgType, camera_frame_metadata_t *metadata)
diff --git a/core/jni/android_hardware_camera2_CameraMetadata.cpp b/core/jni/android_hardware_camera2_CameraMetadata.cpp
index f37fd78..518f99e 100644
--- a/core/jni/android_hardware_camera2_CameraMetadata.cpp
+++ b/core/jni/android_hardware_camera2_CameraMetadata.cpp
@@ -43,8 +43,6 @@
 #include <sys/types.h> // for socketpair
 #include <sys/socket.h> // for socketpair
 
-static const bool kIsDebug = false;
-
 // fully-qualified class name
 #define CAMERA_METADATA_CLASS_NAME "android/hardware/camera2/impl/CameraMetadataNative"
 #define CHARACTERISTICS_KEY_CLASS_NAME "android/hardware/camera2/CameraCharacteristics$Key"
@@ -787,109 +785,16 @@
         // exception thrown by ScopedUtfChars
         return 0;
     }
-    size_t keyLength = strlen(key);
-
     ALOGV("%s (key = '%s')", __FUNCTION__, key);
 
-    sp<VendorTagDescriptor> vTags = VendorTagDescriptor::getGlobalVendorTagDescriptor();
-
-    SortedVector<String8> vendorSections;
-    size_t vendorSectionCount = 0;
-
-    if (vTags != NULL) {
-        vendorSections = vTags->getAllSectionNames();
-        vendorSectionCount = vendorSections.size();
-    }
-
-    // First, find the section by the longest string match
-    const char *section = NULL;
-    size_t sectionIndex = 0;
-    size_t sectionLength = 0;
-    size_t totalSectionCount = ANDROID_SECTION_COUNT + vendorSectionCount;
-    for (size_t i = 0; i < totalSectionCount; ++i) {
-
-        const char *str = (i < ANDROID_SECTION_COUNT) ? camera_metadata_section_names[i] :
-                vendorSections[i - ANDROID_SECTION_COUNT].string();
-        if (kIsDebug) {
-            ALOGV("%s: Trying to match against section '%s'", __FUNCTION__, str);
-        }
-        if (strstr(key, str) == key) { // key begins with the section name
-            size_t strLength = strlen(str);
-
-            if (kIsDebug) {
-                ALOGV("%s: Key begins with section name", __FUNCTION__);
-            }
-
-            // section name is the longest we've found so far
-            if (section == NULL || sectionLength < strLength) {
-                section = str;
-                sectionIndex = i;
-                sectionLength = strLength;
-
-                if (kIsDebug) {
-                    ALOGV("%s: Found new best section (%s)", __FUNCTION__, section);
-                }
-            }
-        }
-    }
-
-    // TODO: Make above get_camera_metadata_section_from_name ?
-
-    if (section == NULL) {
-        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
-                             "Could not find section name for key '%s')", key);
-        return 0;
-    } else {
-        ALOGV("%s: Found matched section '%s' (%zu)",
-              __FUNCTION__, section, sectionIndex);
-    }
-
-    // Get the tag name component of the key
-    const char *keyTagName = key + sectionLength + 1; // x.y.z -> z
-    if (sectionLength + 1 >= keyLength) {
-        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
-                             "Key length too short for key '%s')", key);
-        return 0;
-    }
-
-    // Match rest of name against the tag names in that section only
     uint32_t tag = 0;
-    if (sectionIndex < ANDROID_SECTION_COUNT) {
-        // Match built-in tags (typically android.*)
-        uint32_t tagBegin, tagEnd; // [tagBegin, tagEnd)
-        tagBegin = camera_metadata_section_bounds[sectionIndex][0];
-        tagEnd = camera_metadata_section_bounds[sectionIndex][1];
-
-        for (tag = tagBegin; tag < tagEnd; ++tag) {
-            const char *tagName = get_camera_metadata_tag_name(tag);
-
-            if (strcmp(keyTagName, tagName) == 0) {
-                ALOGV("%s: Found matched tag '%s' (%d)",
-                      __FUNCTION__, tagName, tag);
-                break;
-            }
-        }
-
-        if (tag == tagEnd) {
-            jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
-                                 "Could not find tag name for key '%s')", key);
-            return 0;
-        }
-    } else if (vTags != NULL) {
-        // Match vendor tags (typically com.*)
-        const String8 sectionName(section);
-        const String8 tagName(keyTagName);
-
-        status_t res = OK;
-        if ((res = vTags->lookupTag(tagName, sectionName, &tag)) != OK) {
-            jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
-                    "%s: No vendor tag matches key '%s'", __FUNCTION__, key);
-            return 0;
-        }
+    sp<VendorTagDescriptor> vTags =
+            VendorTagDescriptor::getGlobalVendorTagDescriptor();
+    status_t res = CameraMetadata::getTagFromName(key, vTags.get(), &tag);
+    if (res != OK) {
+        jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
+                             "Could not find tag for key '%s')", key);
     }
-
-    // TODO: Make above get_camera_metadata_tag_from_name ?
-
     return tag;
 }
 
diff --git a/core/jni/android_hardware_location_ContextHubService.cpp b/core/jni/android_hardware_location_ContextHubService.cpp
index 3881d6d..8eb39e1 100644
--- a/core/jni/android_hardware_location_ContextHubService.cpp
+++ b/core/jni/android_hardware_location_ContextHubService.cpp
@@ -21,28 +21,43 @@
 
 #include <inttypes.h>
 #include <jni.h>
-#include <queue>
-#include <unordered_map>
+#include <mutex>
 #include <string.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 
+// TOOD: On master, alphabetize these and move <mutex> into this
+//     grouping.
+#include <chrono>
+#include <unordered_map>
+#include <queue>
+
 #include <cutils/log.h>
 
 #include "JNIHelp.h"
 #include "core_jni_helpers.h"
 
-static constexpr int OS_APP_ID=-1;
+static constexpr jint OS_APP_ID = -1;
+static constexpr jint INVALID_APP_ID = -2;
+static constexpr uint64_t ALL_APPS = UINT64_C(0xFFFFFFFFFFFFFFFF);
 
-static constexpr int MIN_APP_ID=1;
-static constexpr int MAX_APP_ID=128;
+static constexpr jint MIN_APP_ID = 1;
+static constexpr jint MAX_APP_ID = 128;
 
-static constexpr size_t MSG_HEADER_SIZE=4;
-static constexpr int HEADER_FIELD_MSG_TYPE=0;
-//static constexpr int HEADER_FIELD_MSG_VERSION=1;
-static constexpr int HEADER_FIELD_HUB_HANDLE=2;
-static constexpr int HEADER_FIELD_APP_INSTANCE=3;
+static constexpr size_t MSG_HEADER_SIZE = 4;
+static constexpr size_t HEADER_FIELD_MSG_TYPE = 0;
+static constexpr size_t HEADER_FIELD_MSG_VERSION = 1;
+static constexpr size_t HEADER_FIELD_HUB_HANDLE = 2;
+static constexpr size_t HEADER_FIELD_APP_INSTANCE = 3;
+
+static constexpr size_t HEADER_FIELD_LOAD_APP_ID_LO = MSG_HEADER_SIZE;
+static constexpr size_t HEADER_FIELD_LOAD_APP_ID_HI = MSG_HEADER_SIZE + 1;
+static constexpr size_t MSG_HEADER_SIZE_LOAD_APP = MSG_HEADER_SIZE + 2;
+
+// Monotonically increasing clock we use to determine if we can cancel
+// a transaction.
+using std::chrono::steady_clock;
 
 namespace android {
 
@@ -83,6 +98,7 @@
 
     jmethodID contextHubServiceMsgReceiptCallback;
     jmethodID contextHubServiceAddAppInstance;
+    jmethodID contextHubServiceDeleteAppInstance;
 };
 
 struct context_hub_info_s {
@@ -93,24 +109,151 @@
 };
 
 struct app_instance_info_s {
-    uint32_t hubHandle; // Id of the hub this app is on
-    int instanceId; // systemwide unique instance id - assigned
+    uint64_t truncName;          // Possibly truncated name for logging
+    uint32_t hubHandle;          // Id of the hub this app is on
+    jint instanceId;             // system wide unique instance id - assigned
     struct hub_app_info appInfo; // returned from the HAL
-    uint64_t truncName; // Possibly truncated name - logging
+};
+
+
+// If a transaction takes longer than this, we'll allow it to be
+// canceled by a new transaction.  Note we do _not_ automatically
+// cancel a transaction after this much time.  We can have a
+// legal transaction which takes longer than this amount of time,
+// as long as no other new transactions are attempted after this
+// time has expired.
+// TODO(b/31105001): Establish a clean timing approach for all
+// of our HAL interactions.
+constexpr auto kMinTransactionCancelTime = std::chrono::seconds(29);
+
+/*
+ * TODO(ashutoshj): From original code review:
+ *
+ * So, I feel like we could possible do a better job of organizing this code,
+ * and being more C++-y.  Consider something like this:
+ * class TxnManager {
+ *  public:
+ *   TxnManager();
+ *   ~TxnManager();
+ *   int add(hub_message_e identifier, void *data);
+ *   int close();
+ *   bool isPending() const;
+ *   int fetchData(hub_message_e *identifier, void **data) const;
+ *
+ *  private:
+ *   bool mPending;
+ *   mutable std::mutex mLock;
+ *   hub_message_e mIdentifier;
+ *   void *mData;
+ * };
+ *
+ * And then, for example, we'd have things like:
+ * TxnManager::TxnManager() : mPending(false), mLock(), mIdentifier(), mData(nullptr) {}
+ * int TxnManager::add(hub_message_e identifier, void *data) {
+ *    std::lock_guard<std::mutex> lock(mLock);
+ *    mPending = true;
+ *    mData = txnData;
+ *    mIdentifier = txnIdentifier;
+ *    return 0;
+ *  }
+ * And then calling code would look like:
+ *    if (!db.txnManager.add(CONTEXT_HUB_LOAD_APP, txnInfo)) {
+ *
+ * This would make it clearer the nothing is manipulating any state within TxnManager
+ * unsafely and outside of these couple of calls.
+ */
+struct txnManager_s {
+    bool txnPending;              // Is a transaction pending
+    std::mutex m;                 // mutex for manager
+    hub_messages_e txnIdentifier; // What are we doing
+    void *txnData;                // Details
+    steady_clock::time_point firstTimeTxnCanBeCanceled;
 };
 
 struct contextHubServiceDb_s {
     int initialized;
     context_hub_info_s hubInfo;
     jniInfo_s jniInfo;
-    std::queue<int> freeIds;
-    std::unordered_map<int, app_instance_info_s> appInstances;
+    std::queue<jint> freeIds;
+    std::unordered_map<jint, app_instance_info_s> appInstances;
+    txnManager_s txnManager;
 };
 
 }  // unnamed namespace
 
 static contextHubServiceDb_s db;
 
+static bool initTxnManager() {
+    txnManager_s *mgr = &db.txnManager;
+
+    mgr->txnData = nullptr;
+    mgr->txnPending = false;
+    return true;
+}
+
+static int addTxn(hub_messages_e txnIdentifier, void *txnData) {
+    txnManager_s *mgr = &db.txnManager;
+
+    std::lock_guard<std::mutex>lock(mgr->m);
+
+    mgr->txnPending = true;
+    mgr->firstTimeTxnCanBeCanceled = steady_clock::now() +
+        kMinTransactionCancelTime;
+    mgr->txnData = txnData;
+    mgr->txnIdentifier = txnIdentifier;
+
+    return 0;
+}
+
+// Only call this if you hold the db.txnManager.m lock.
+static void closeTxnUnlocked() {
+    txnManager_s *mgr = &db.txnManager;
+    mgr->txnPending = false;
+    free(mgr->txnData);
+    mgr->txnData = nullptr;
+}
+
+static int closeTxn() {
+    std::lock_guard<std::mutex>lock(db.txnManager.m);
+    closeTxnUnlocked();
+    return 0;
+}
+
+// If a transaction has been pending for longer than
+// kMinTransactionCancelTime, this call will "cancel" that
+// transaction and return that there are none pending.
+static bool isTxnPending() {
+    txnManager_s *mgr = &db.txnManager;
+    std::lock_guard<std::mutex>lock(mgr->m);
+    if (mgr->txnPending) {
+        if (steady_clock::now() >= mgr->firstTimeTxnCanBeCanceled) {
+            ALOGW("Transaction canceled");
+            closeTxnUnlocked();
+        }
+    }
+    return mgr->txnPending;
+}
+
+static int fetchTxnData(hub_messages_e *id, void **data) {
+    txnManager_s *mgr = &db.txnManager;
+
+    if (!id || !data) {
+        ALOGW("Null params id %p, data %p", id, data);
+        return -1;
+    }
+
+    std::lock_guard<std::mutex>lock(mgr->m);
+    if (!mgr->txnPending) {
+        ALOGW("No Transactions pending");
+        return -1;
+    }
+
+    // else
+    *id = mgr->txnIdentifier;
+    *data = mgr->txnData;
+    return 0;
+}
+
 int context_hub_callback(uint32_t hubId, const struct hub_message_t *msg,
                          void *cookie);
 
@@ -152,18 +295,27 @@
     }
 }
 
-static int get_hub_id_for_app_instance(int id) {
+static int get_hub_handle_for_app_instance(jint id) {
     if (!db.appInstances.count(id)) {
-        ALOGD("%s: Cannot find app for app instance %d", __FUNCTION__, id);
+        ALOGD("%s: Cannot find app for app instance %" PRId32,
+              __FUNCTION__, id);
         return -1;
     }
 
-    int hubHandle = db.appInstances[id].hubHandle;
+    return db.appInstances[id].hubHandle;
+}
+
+static int get_hub_id_for_app_instance(jint id) {
+    int hubHandle = get_hub_handle_for_app_instance(id);
+
+    if (hubHandle < 0) {
+        return -1;
+    }
 
     return db.hubInfo.hubs[hubHandle].hub_id;
 }
 
-static int get_app_instance_for_app_id(uint64_t app_id) {
+static jint get_app_instance_for_app_id(uint64_t app_id) {
     auto end = db.appInstances.end();
     for (auto current = db.appInstances.begin(); current != end; ++current) {
         if (current->second.appInfo.app_name.id == app_id) {
@@ -174,9 +326,10 @@
     return -1;
 }
 
-static int set_dest_app(hub_message_t *msg, int id) {
+static int set_dest_app(hub_message_t *msg, jint id) {
     if (!db.appInstances.count(id)) {
-        ALOGD("%s: Cannot find app for app instance %d", __FUNCTION__, id);
+        ALOGD("%s: Cannot find app for app instance %" PRId32,
+              __FUNCTION__, id);
         return -1;
     }
 
@@ -184,31 +337,47 @@
     return 0;
 }
 
-static void send_query_for_apps() {
+static void query_hub_for_apps(uint32_t hubHandle) {
     hub_message_t msg;
+    query_apps_request_t queryMsg;
+
+    // TODO(b/30835598): When we're able to tell which request our
+    //     response matches, then we should allow this to be more
+    //     targetted, instead of always being every app in the
+    //     system.
+    queryMsg.app_name.id = ALL_APPS;
 
     msg.message_type = CONTEXT_HUB_QUERY_APPS;
-    msg.message_len  = 0;
+    msg.message_len  = sizeof(queryMsg);
+    msg.message = &queryMsg;
 
-    for (int i = 0; i < db.hubInfo.numHubs; i++ ) {
-        ALOGD("Sending query for apps to hub %d", i);
-        set_os_app_as_destination(&msg, i);
-        if (send_msg_to_hub(&msg, i) != 0) {
-          ALOGW("Could not query hub %i for apps", i);
-        }
+    ALOGD("Sending query for apps to hub %" PRIu32, hubHandle);
+    set_os_app_as_destination(&msg, hubHandle);
+    if (send_msg_to_hub(&msg, hubHandle) != 0) {
+        ALOGW("Could not query hub %" PRIu32 " for apps", hubHandle);
     }
 }
 
-static int return_id(int id) {
-    // Note : This method is not thread safe.
-    // id returned is guarenteed to be in use
-    db.freeIds.push(id);
-    return 0;
+static void sendQueryForApps() {
+    for (int i = 0; i < db.hubInfo.numHubs; i++ ) {
+        query_hub_for_apps(i);
+    }
 }
 
-static int generate_id(void) {
+static int return_id(jint id) {
     // Note : This method is not thread safe.
-    int retVal = -1;
+    // id returned is guaranteed to be in use
+    if (id >= 0) {
+        db.freeIds.push(id);
+        return 0;
+    }
+
+    return -1;
+}
+
+static jint generate_id() {
+    // Note : This method is not thread safe.
+    jint retVal = -1;
 
     if (!db.freeIds.empty()) {
         retVal = db.freeIds.front();
@@ -218,55 +387,113 @@
     return retVal;
 }
 
-int add_app_instance(const hub_app_info *appInfo, uint32_t hubHandle, JNIEnv *env) {
+
+static jint add_app_instance(const hub_app_info *appInfo, uint32_t hubHandle,
+        jint appInstanceHandle, JNIEnv *env) {
     // Not checking if the apps are indeed distinct
     app_instance_info_s entry;
-    int appInstanceHandle = generate_id();
-
     assert(appInfo);
 
-    if (appInstanceHandle < 0) {
-        ALOGE("Cannot find resources to add app instance %d",
-              appInstanceHandle);
-        return -1;
-    }
+    const char *action =
+        (db.appInstances.count(appInstanceHandle) == 0) ? "Added" : "Updated";
 
     entry.appInfo = *appInfo;
+
     entry.instanceId = appInstanceHandle;
     entry.truncName = appInfo->app_name.id;
     entry.hubHandle = hubHandle;
+
     db.appInstances[appInstanceHandle] = entry;
 
-    // Finally - let the service know of this app instance
+    // Finally - let the service know of this app instance, to populate
+    // the Java cache.
     env->CallIntMethod(db.jniInfo.jContextHubService,
                        db.jniInfo.contextHubServiceAddAppInstance,
                        hubHandle, entry.instanceId, entry.truncName,
                        entry.appInfo.version);
 
-    ALOGW("Added App 0x%" PRIx64 " on hub Handle %" PRId32
-          " as appInstance %d", entry.truncName,
+    ALOGI("%s App 0x%" PRIx64 " on hub Handle %" PRId32
+          " as appInstance %" PRId32, action, entry.truncName,
           entry.hubHandle, appInstanceHandle);
 
     return appInstanceHandle;
 }
 
-int delete_app_instance(int id) {
-    if (!db.appInstances.count(id)) {
+int delete_app_instance(jint id, JNIEnv *env) {
+    bool fullyDeleted = true;
+
+    if (db.appInstances.count(id)) {
+        db.appInstances.erase(id);
+    } else {
+        ALOGW("Cannot delete App id (%" PRId32 ") from the JNI C++ cache", id);
+        fullyDeleted = false;
+    }
+    return_id(id);
+
+    if ((env == nullptr) ||
+        (env->CallIntMethod(db.jniInfo.jContextHubService,
+                       db.jniInfo.contextHubServiceDeleteAppInstance,
+                       id) != 0)) {
+        ALOGW("Cannot delete App id (%" PRId32 ") from Java cache", id);
+        fullyDeleted = false;
+    }
+
+    if (fullyDeleted) {
+        ALOGI("Deleted App id : %" PRId32, id);
+        return 0;
+    }
+    return -1;
+}
+
+static int startLoadAppTxn(uint64_t appId, int hubHandle) {
+    app_instance_info_s *txnInfo = (app_instance_info_s *)malloc(sizeof(app_instance_info_s));
+    jint instanceId = generate_id();
+
+    if (!txnInfo || instanceId < 0) {
+        return_id(instanceId);
+        free(txnInfo);
         return -1;
     }
 
-    return_id(id);
-    db.appInstances.erase(id);
+    txnInfo->truncName = appId;
+    txnInfo->hubHandle = hubHandle;
+    txnInfo->instanceId = instanceId;
+
+    txnInfo->appInfo.app_name.id = appId;
+    txnInfo->appInfo.num_mem_ranges = 0;
+    txnInfo->appInfo.version = -1; // Awaited
+
+    if (addTxn(CONTEXT_HUB_LOAD_APP, txnInfo) != 0) {
+        return_id(instanceId);
+        free(txnInfo);
+        return -1;
+    }
 
     return 0;
 }
 
+static int startUnloadAppTxn(jint appInstanceHandle) {
+    jint *txnData = (jint *) malloc(sizeof(jint));
+    if (!txnData) {
+        ALOGW("Cannot allocate memory to start unload transaction");
+        return -1;
+    }
+
+    *txnData = appInstanceHandle;
+
+    if (addTxn(CONTEXT_HUB_UNLOAD_APP, txnData) != 0) {
+        free(txnData);
+        ALOGW("Cannot start transaction to unload app");
+        return -1;
+    }
+
+    return 0;
+}
 
 static void initContextHubService() {
     int err = 0;
     db.hubInfo.hubs = nullptr;
     db.hubInfo.numHubs = 0;
-    int i;
 
     err = hw_get_module(CONTEXT_HUB_MODULE_ID,
                         (hw_module_t const**)(&db.hubInfo.contextHubModule));
@@ -277,10 +504,11 @@
     }
 
     // Prep for storing app info
-    for(i = MIN_APP_ID; i <= MAX_APP_ID; i++) {
+    for (jint i = MIN_APP_ID; i <= MAX_APP_ID; i++) {
         db.freeIds.push(i);
     }
 
+    initTxnManager();
     if (db.hubInfo.contextHubModule) {
         int retNumHubs = db.hubInfo.contextHubModule->get_hubs(db.hubInfo.contextHubModule,
                                                                  &db.hubInfo.hubs);
@@ -296,8 +524,9 @@
                 return;
             }
 
-            for (i = 0; i < db.hubInfo.numHubs; i++) {
+            for (int i = 0; i < db.hubInfo.numHubs; i++) {
                 db.hubInfo.cookies[i] = db.hubInfo.hubs[i].hub_id;
+                ALOGI("Subscribing to hubHandle %d with OS App name %" PRIu64, i, db.hubInfo.hubs[i].os_app_name.id);
                 if (db.hubInfo.contextHubModule->subscribe_messages(db.hubInfo.hubs[i].hub_id,
                                                                     context_hub_callback,
                                                                     &db.hubInfo.cookies[i]) == 0) {
@@ -305,7 +534,7 @@
             }
         }
 
-        send_query_for_apps();
+        sendQueryForApps();
     } else {
         ALOGW("No Context Hub Module present");
     }
@@ -342,61 +571,303 @@
     return ret;
 }
 
-int handle_query_apps_response(char *msg, int msgLen, uint32_t hubHandle) {
+int handle_query_apps_response(const uint8_t *msg, int msgLen,
+                               uint32_t hubHandle) {
     JNIEnv *env;
     if ((db.jniInfo.vm)->AttachCurrentThread(&env, nullptr) != JNI_OK) {
             return -1;
     }
 
-    int numApps = msgLen/sizeof(hub_app_info);
-    hub_app_info info;
-    hub_app_info *unalignedInfoAddr = (hub_app_info*)msg;
+    int numApps = msgLen / sizeof(hub_app_info);
+    const hub_app_info *unalignedInfoAddr = (const hub_app_info*)msg;
 
-    for (int i = 0; i < numApps; i++, unalignedInfoAddr++) {
-        memcpy(&info, unalignedInfoAddr, sizeof(info));
-        add_app_instance(&info, hubHandle, env);
+    // We use this information to sync our JNI and Java caches of nanoapp info.
+    // We want to accomplish two things here:
+    // 1) Remove entries from our caches which are stale, and pertained to
+    //    apps no longer running on Context Hub.
+    // 2) Populate our caches with the latest information of all these apps.
+
+    // We make a couple of assumptions here:
+    // A) The JNI and Java caches are in sync with each other (this isn't
+    //    necessarily true; any failure of a single call into Java land to
+    //    update its cache will leave that cache in a bad state.  For NYC,
+    //    we're willing to tolerate this for now).
+    // B) The total number of apps is relatively small, so horribly inefficent
+    //    algorithms aren't too painful.
+    // C) We're going to call this relatively infrequently, so its inefficency
+    //    isn't a big impact.
+
+
+    // (1).  Looking for stale cache entries.  Yes, this is O(N^2).  See
+    // assumption (B).  Per assumption (A), it is sufficient to iterate
+    // over just the JNI cache.
+    auto end = db.appInstances.end();
+    for (auto current = db.appInstances.begin(); current != end; ) {
+        app_instance_info_s cache_entry = current->second;
+        // We perform our iteration here because if we call
+        // delete_app_instance() below, it will erase() this entry.
+        current++;
+        bool entryIsStale = true;
+        for (int i = 0; i < numApps; i++) {
+            // We use memcmp since this could be unaligned.
+            if (memcmp(&unalignedInfoAddr[i].app_name.id,
+                       &cache_entry.appInfo.app_name.id,
+                       sizeof(cache_entry.appInfo.app_name.id)) == 0) {
+                // We found a match; this entry is current.
+                entryIsStale = false;
+                break;
+            }
+        }
+        if (entryIsStale) {
+            delete_app_instance(cache_entry.instanceId, env);
+        }
+    }
+
+    // (2).  Update our caches with the latest.
+    for (int i = 0; i < numApps; i++) {
+        hub_app_info query_info;
+        memcpy(&query_info, &unalignedInfoAddr[i], sizeof(query_info));
+        // We will only have one instance of the app
+        // TODO : Change this logic once we support multiple instances of the same app
+        jint appInstance = get_app_instance_for_app_id(query_info.app_name.id);
+        if (appInstance == -1) {
+            // This is a previously unknown app, let's allocate an "id" for it.
+            appInstance = generate_id();
+        }
+        add_app_instance(&query_info, hubHandle, appInstance, env);
     }
 
     return 0;
 }
 
+// TODO(b/30807327): Do not use raw bytes for additional data.  Use the
+//     JNI interfaces for the appropriate types.
+static void passOnOsResponse(uint32_t hubHandle, uint32_t msgType,
+                             status_response_t *rsp, int8_t *additionalData,
+                             size_t additionalDataLen) {
+    JNIEnv *env;
 
-int handle_os_message(uint32_t msgType, uint32_t hubHandle,
-                      char *msg, int msgLen) {
-    int retVal;
+    if ((db.jniInfo.vm)->AttachCurrentThread(&env, nullptr) != JNI_OK) {
+        ALOGW("Cannot latch to JNI env, dropping OS response %" PRIu32, msgType);
+        return;
+    }
 
-    //ALOGD("Rcd OS message from hubHandle %" PRIu32 " type %" PRIu32 " length %d",
-    //      hubHandle, msgType, msgLen);
+    uint32_t header[MSG_HEADER_SIZE];
+    memset(header, 0, sizeof(header));
+
+    if (!additionalData) {
+        additionalDataLen = 0; // clamp
+    }
+    int msgLen = 1 + additionalDataLen;
+
+    int8_t *msg = new int8_t[msgLen];
+
+    if (!msg) {
+        ALOGW("Unexpected : Ran out of memory, cannot send response");
+        return;
+    }
+
+    header[HEADER_FIELD_MSG_TYPE] = msgType;
+    header[HEADER_FIELD_MSG_VERSION] = 0;
+    header[HEADER_FIELD_HUB_HANDLE] = hubHandle;
+    header[HEADER_FIELD_APP_INSTANCE] = OS_APP_ID;
+
+    // Due to API constraints, at the moment we can't change the fact that
+    // we're changing our 4-byte response to a 1-byte value.  But we can prevent
+    // the possible change in sign (and thus meaning) that would happen from
+    // a naive cast.  Further, we can log when we're losing part of the value.
+    // TODO(b/30918279): Don't truncate this result.
+    int8_t truncatedResult;
+    bool neededToTruncate;
+    if (rsp->result < INT8_MIN) {
+        neededToTruncate = true;
+        truncatedResult = INT8_MIN;
+    } else if (rsp->result > INT8_MAX) {
+        neededToTruncate = true;
+        truncatedResult = INT8_MAX;
+    } else {
+        neededToTruncate = false;
+        // Since this value fits within an int8_t, this is a safe cast which
+        // won't change the value or sign.
+        truncatedResult = static_cast<int8_t>(rsp->result);
+    }
+    if (neededToTruncate) {
+        ALOGW("Response from Context Hub truncated.  Value was %" PRId32
+              ", but giving Java layer %" PRId8,
+              rsp->result, (int)truncatedResult);
+    }
+
+    msg[0] = truncatedResult;
+
+    if (additionalData) {
+        memcpy(&msg[1], additionalData, additionalDataLen);
+    }
+
+    jbyteArray jmsg = env->NewByteArray(msgLen);
+    jintArray jheader = env->NewIntArray(sizeof(header));
+
+    env->SetByteArrayRegion(jmsg, 0, msgLen, (jbyte *)msg);
+    env->SetIntArrayRegion(jheader, 0, sizeof(header), (jint *)header);
+
+    ALOGI("Passing msg type %" PRIu32 " from app %" PRIu32 " from hub %" PRIu32,
+          header[HEADER_FIELD_MSG_TYPE], header[HEADER_FIELD_APP_INSTANCE],
+          header[HEADER_FIELD_HUB_HANDLE]);
+
+    env->CallIntMethod(db.jniInfo.jContextHubService,
+                       db.jniInfo.contextHubServiceMsgReceiptCallback,
+                       jheader, jmsg);
+    env->DeleteLocalRef(jmsg);
+    env->DeleteLocalRef(jheader);
+
+    delete[] msg;
+}
+
+void closeUnloadTxn(bool success) {
+    void *txnData = nullptr;
+    hub_messages_e txnId;
+
+    if (success && fetchTxnData(&txnId, &txnData) == 0 &&
+        txnId == CONTEXT_HUB_UNLOAD_APP) {
+        JNIEnv *env;
+        if ((db.jniInfo.vm)->AttachCurrentThread(&env, nullptr) != JNI_OK) {
+            ALOGW("Could not attach to JVM !");
+            env = nullptr;
+        }
+        jint handle = *reinterpret_cast<jint *>(txnData);
+        delete_app_instance(handle, env);
+    } else {
+        ALOGW("Could not unload the app successfully ! success %d, txnData %p", success, txnData);
+    }
+
+    closeTxn();
+}
+
+static bool closeLoadTxn(bool success, jint *appInstanceHandle) {
+    void *txnData;
+    hub_messages_e txnId;
+
+    if (success && fetchTxnData(&txnId, &txnData) == 0 &&
+        txnId == CONTEXT_HUB_LOAD_APP) {
+        app_instance_info_s *info = (app_instance_info_s *)txnData;
+        *appInstanceHandle = info->instanceId;
+
+        JNIEnv *env;
+        if ((db.jniInfo.vm)->AttachCurrentThread(&env, nullptr) == JNI_OK) {
+            add_app_instance(&info->appInfo, info->hubHandle, info->instanceId, env);
+        } else {
+            ALOGW("Could not attach to JVM !");
+            success = false;
+        }
+        // While we just called add_app_instance above, our info->appInfo was
+        // incomplete (for example, the 'version' is hardcoded to -1).  So we
+        // trigger an additional query to the CHRE, so we'll be able to get
+        // all the app "info", and have our JNI and Java caches with the
+        // full information.
+        sendQueryForApps();
+    } else {
+        ALOGW("Could not load the app successfully ! Unexpected failure");
+        *appInstanceHandle = INVALID_APP_ID;
+        success = false;
+    }
+
+    closeTxn();
+    return success;
+}
+
+static bool isValidOsStatus(const uint8_t *msg, size_t msgLen,
+                            status_response_t *rsp) {
+    // Workaround a bug in some HALs
+    if (msgLen == 1) {
+        rsp->result = msg[0];
+        return true;
+    }
+
+    if (!msg || msgLen != sizeof(*rsp)) {
+        ALOGW("Received invalid response %p of size %zu", msg, msgLen);
+        return false;
+    }
+
+    memcpy(rsp, msg, sizeof(*rsp));
+
+    // No sanity checks on return values
+    return true;
+}
+
+static int handle_os_message(uint32_t msgType, uint32_t hubHandle,
+                             const uint8_t *msg, int msgLen) {
+    int retVal = -1;
+
+    ALOGD("Rcd OS message from hubHandle %" PRIu32 " type %" PRIu32 " length %d",
+          hubHandle, msgType, msgLen);
+
+    struct status_response_t rsp;
 
     switch(msgType) {
-        case CONTEXT_HUB_APPS_ENABLE:
-            retVal = 0;
-            break;
 
-        case CONTEXT_HUB_APPS_DISABLE:
-            retVal = 0;
-            break;
+      case CONTEXT_HUB_APPS_ENABLE:
+      case CONTEXT_HUB_APPS_DISABLE:
+      case CONTEXT_HUB_LOAD_APP:
+      case CONTEXT_HUB_UNLOAD_APP:
+          if (isValidOsStatus(msg, msgLen, &rsp)) {
+              if (msgType == CONTEXT_HUB_LOAD_APP) {
+                  jint appInstanceHandle = INVALID_APP_ID;
+                  bool appRunningOnHub = (rsp.result == 0);
+                  if (!(closeLoadTxn(appRunningOnHub, &appInstanceHandle))) {
+                      if (appRunningOnHub) {
+                          // Now we're in an odd situation.  Our nanoapp
+                          // is up and running on the Context Hub.  However,
+                          // something went wrong in our Service code so that
+                          // we're not able to properly track this nanoapp
+                          // in our Service code.  If we tell the Java layer
+                          // things are good, it's a lie because the handle
+                          // we give them will fail when used with the Service.
+                          // If we tell the Java layer this failed, it's kind
+                          // of a lie as well, since this nanoapp is running.
+                          //
+                          // We leave a more robust fix for later, and for
+                          // now just tell the user things have failed.
+                          //
+                          // TODO(b/30835981): Make this situation better.
+                          rsp.result = -1;
+                      }
+                  }
+                  passOnOsResponse(hubHandle, msgType, &rsp, (int8_t *)(&appInstanceHandle),
+                                   sizeof(appInstanceHandle));
+              } else if (msgType == CONTEXT_HUB_UNLOAD_APP) {
+                  closeUnloadTxn(rsp.result == 0);
+                  passOnOsResponse(hubHandle, msgType, &rsp, nullptr, 0);
+              } else {
+                  passOnOsResponse(hubHandle, msgType, &rsp, nullptr, 0);
+              }
+              retVal = 0;
+          }
+          break;
 
-        case CONTEXT_HUB_LOAD_APP:
-            retVal = 0;
-            break;
+      case CONTEXT_HUB_QUERY_APPS:
+          rsp.result = 0;
+          retVal = handle_query_apps_response(msg, msgLen, hubHandle);
+          passOnOsResponse(hubHandle, msgType, &rsp, nullptr, 0);
+          break;
 
-        case CONTEXT_HUB_UNLOAD_APP:
-            retVal = 0;
-            break;
+      case CONTEXT_HUB_QUERY_MEMORY:
+          // Deferring this use
+          retVal = 0;
+          break;
 
-        case CONTEXT_HUB_QUERY_APPS:
-            retVal = handle_query_apps_response(msg, msgLen, hubHandle);
-            break;
+      case CONTEXT_HUB_OS_REBOOT:
+          if (isValidOsStatus(msg, msgLen, &rsp)) {
+              rsp.result = 0;
+              ALOGW("Context Hub handle %d restarted", hubHandle);
+              closeTxn();
+              passOnOsResponse(hubHandle, msgType, &rsp, nullptr, 0);
+              query_hub_for_apps(hubHandle);
+              retVal = 0;
+          }
+          break;
 
-        case CONTEXT_HUB_QUERY_MEMORY:
-            retVal = 0;
-            break;
-
-        default:
-            retVal = -1;
-            break;
-
+      default:
+          retVal = -1;
+          break;
     }
 
     return retVal;
@@ -416,10 +887,12 @@
     }
 }
 
+
 int context_hub_callback(uint32_t hubId,
                          const struct hub_message_t *msg,
                          void *cookie) {
     if (!msg) {
+        ALOGW("NULL message");
         return -1;
     }
     if (!sanity_check_cookie(cookie, hubId)) {
@@ -429,13 +902,14 @@
         return -1;
     }
 
+
     uint32_t messageType = msg->message_type;
     uint32_t hubHandle = *(uint32_t*) cookie;
 
     if (messageType < CONTEXT_HUB_TYPE_PRIVATE_MSG_BASE) {
-        handle_os_message(messageType, hubHandle, (char*) msg->message, msg->message_len);
+        handle_os_message(messageType, hubHandle, (uint8_t*) msg->message, msg->message_len);
     } else {
-        int appHandle = get_app_instance_for_app_id(msg->app_name.id);
+        jint appHandle = get_app_instance_for_app_id(msg->app_name.id);
         if (appHandle < 0) {
             ALOGE("Filtering out message due to invalid App Instance.");
         } else {
@@ -524,7 +998,9 @@
                  env->GetMethodID(db.jniInfo.contextHubServiceClass,
                                     "addAppInstance", "(IIJI)I");
 
-
+    db.jniInfo.contextHubServiceDeleteAppInstance =
+                 env->GetMethodID(db.jniInfo.contextHubServiceClass,
+                                    "deleteAppInstance", "(I)I");
 
     return 0;
 }
@@ -534,8 +1010,6 @@
     jintArray jintBuf;
     jobjectArray jmemBuf;
 
-    int dummyConnectedSensors[] = {1, 2, 3, 4, 5};
-
     jobject jHub = env->NewObject(db.jniInfo.contextHubInfoClass,
                                   db.jniInfo.contextHubInfoCtor);
     env->CallVoidMethod(jHub, db.jniInfo.contextHubInfoSetId, hub->hub_id);
@@ -565,11 +1039,21 @@
                         hub->max_supported_msg_len);
 
 
-    // TODO : jintBuf = env->NewIntArray(hub->num_connected_sensors);
-    // TODO : env->SetIntArrayRegion(jintBuf, 0, hub->num_connected_sensors,
-    //                               hub->connected_sensors);
-    jintBuf = env->NewIntArray(array_length(dummyConnectedSensors));
-    env->SetIntArrayRegion(jintBuf, 0, hub->num_connected_sensors, dummyConnectedSensors);
+    jintBuf = env->NewIntArray(hub->num_connected_sensors);
+    int *connectedSensors = new int[hub->num_connected_sensors];
+
+    if (!connectedSensors) {
+      ALOGW("Cannot allocate memory! Unexpected");
+      assert(false);
+    } else {
+      for (unsigned int i = 0; i < hub->num_connected_sensors; i++) {
+        connectedSensors[i] = hub->connected_sensors[i].sensor_id;
+      }
+    }
+
+    env->SetIntArrayRegion(jintBuf, 0, hub->num_connected_sensors,
+                           connectedSensors);
+
     env->CallVoidMethod(jHub, db.jniInfo.contextHubInfoSetSupportedSensors, jintBuf);
     env->DeleteLocalRef(jintBuf);
 
@@ -580,6 +1064,7 @@
     env->DeleteLocalRef(jmemBuf);
 
 
+    delete[] connectedSensors;
     return jHub;
 }
 
@@ -618,33 +1103,100 @@
     jbyte *data = env->GetByteArrayElements(data_, 0);
     int dataBufferLength = env->GetArrayLength(data_);
 
+    if (numHeaderElements < MSG_HEADER_SIZE) {
+        ALOGW("Malformed header len");
+        return -1;
+    }
 
-    if (numHeaderElements >= MSG_HEADER_SIZE) {
-        bool setAddressSuccess;
-        int hubId;
-        hub_message_t msg;
+    uint32_t appInstanceHandle = header[HEADER_FIELD_APP_INSTANCE];
+    uint32_t msgType = header[HEADER_FIELD_MSG_TYPE];
+    int hubHandle = -1;
+    int hubId;
+    uint64_t appId;
+
+    if (msgType == CONTEXT_HUB_UNLOAD_APP) {
+        hubHandle = get_hub_handle_for_app_instance(appInstanceHandle);
+    } else if (msgType == CONTEXT_HUB_LOAD_APP) {
+        if (numHeaderElements < MSG_HEADER_SIZE_LOAD_APP) {
+            return -1;
+        }
+        uint64_t appIdLo = header[HEADER_FIELD_LOAD_APP_ID_LO];
+        uint64_t appIdHi = header[HEADER_FIELD_LOAD_APP_ID_HI];
+        appId = appIdHi << 32 | appIdLo;
+
+        hubHandle = header[HEADER_FIELD_HUB_HANDLE];
+    } else {
+        hubHandle = header[HEADER_FIELD_HUB_HANDLE];
+    }
+
+    if (hubHandle < 0) {
+        ALOGD("Invalid hub Handle %d", hubHandle);
+        return -1;
+    }
+
+    if (msgType == CONTEXT_HUB_LOAD_APP ||
+        msgType == CONTEXT_HUB_UNLOAD_APP) {
+
+        if (isTxnPending()) {
+            ALOGW("Cannot load or unload app while a transaction is pending !");
+            return -1;
+        }
+
+        if (msgType == CONTEXT_HUB_LOAD_APP) {
+            if (startLoadAppTxn(appId, hubHandle) != 0) {
+                ALOGW("Cannot Start Load Transaction");
+                return -1;
+            }
+        } else if (msgType == CONTEXT_HUB_UNLOAD_APP) {
+            if (startUnloadAppTxn(appInstanceHandle) != 0) {
+                ALOGW("Cannot Start UnLoad Transaction");
+                return -1;
+            }
+        }
+    }
+
+    bool setAddressSuccess = false;
+    hub_message_t msg;
+
+    msg.message_type = msgType;
+
+    if (msgType == CONTEXT_HUB_UNLOAD_APP) {
+        msg.message_len = sizeof(db.appInstances[appInstanceHandle].appInfo.app_name);
+        msg.message = &db.appInstances[appInstanceHandle].appInfo.app_name;
+        setAddressSuccess = (set_os_app_as_destination(&msg, hubHandle) == 0);
+        hubId = get_hub_id_for_hub_handle(hubHandle);
+    } else {
+        msg.message_len = dataBufferLength;
+        msg.message = data;
 
         if (header[HEADER_FIELD_APP_INSTANCE] == OS_APP_ID) {
-            setAddressSuccess = (set_os_app_as_destination(&msg, header[HEADER_FIELD_HUB_HANDLE]) == 0);
-            hubId = get_hub_id_for_hub_handle(header[HEADER_FIELD_HUB_HANDLE]);
+            setAddressSuccess = (set_os_app_as_destination(&msg, hubHandle) == 0);
+            hubId = get_hub_id_for_hub_handle(hubHandle);
         } else {
             setAddressSuccess = (set_dest_app(&msg, header[HEADER_FIELD_APP_INSTANCE]) == 0);
             hubId = get_hub_id_for_app_instance(header[HEADER_FIELD_APP_INSTANCE]);
         }
+    }
 
-        if (setAddressSuccess && hubId >= 0) {
-            msg.message_type = header[HEADER_FIELD_MSG_TYPE];
-            msg.message_len = dataBufferLength;
-            msg.message = data;
-            retVal = db.hubInfo.contextHubModule->send_message(hubId, &msg);
-        } else {
-          ALOGD("Could not find app instance %d on hubHandle %d, setAddress %d",
-                header[HEADER_FIELD_APP_INSTANCE],
-                header[HEADER_FIELD_HUB_HANDLE],
-                (int)setAddressSuccess);
-        }
+    if (setAddressSuccess && hubId >= 0) {
+        ALOGD("Asking HAL to remove app");
+        retVal = db.hubInfo.contextHubModule->send_message(hubId, &msg);
     } else {
-        ALOGD("Malformed header len");
+      ALOGD("Could not find app instance %" PRId32 " on hubHandle %" PRId32
+            ", setAddress %d",
+            header[HEADER_FIELD_APP_INSTANCE],
+            header[HEADER_FIELD_HUB_HANDLE],
+            (int)setAddressSuccess);
+    }
+
+    if (retVal != 0) {
+        ALOGD("Send Message failure - %d", retVal);
+        if (msgType == CONTEXT_HUB_LOAD_APP) {
+            jint ignored;
+            closeLoadTxn(false, &ignored);
+        } else if (msgType == CONTEXT_HUB_UNLOAD_APP) {
+            closeUnloadTxn(false);
+        }
     }
 
     env->ReleaseIntArrayElements(header_, header, 0);
diff --git a/core/jni/android_media_AudioFormat.h b/core/jni/android_media_AudioFormat.h
index 6513304..092aaf6 100644
--- a/core/jni/android_media_AudioFormat.h
+++ b/core/jni/android_media_AudioFormat.h
@@ -20,18 +20,20 @@
 #include <system/audio.h>
 
 // keep these values in sync with AudioFormat.java
-#define ENCODING_PCM_16BIT  2
-#define ENCODING_PCM_8BIT   3
-#define ENCODING_PCM_FLOAT  4
-#define ENCODING_AC3        5
-#define ENCODING_E_AC3      6
-#define ENCODING_DTS        7
-#define ENCODING_DTS_HD     8
-#define ENCODING_MP3        9
-#define ENCODING_AAC_LC     10
-#define ENCODING_AAC_HE_V1  11
-#define ENCODING_AAC_HE_V2  12
-#define ENCODING_IEC61937   13
+#define ENCODING_PCM_16BIT      2
+#define ENCODING_PCM_8BIT       3
+#define ENCODING_PCM_FLOAT      4
+#define ENCODING_AC3            5
+#define ENCODING_E_AC3          6
+#define ENCODING_DTS            7
+#define ENCODING_DTS_HD         8
+#define ENCODING_MP3            9
+#define ENCODING_AAC_LC         10
+#define ENCODING_AAC_HE_V1      11
+#define ENCODING_AAC_HE_V2      12
+#define ENCODING_IEC61937       13
+#define ENCODING_DOLBY_TRUEHD   14
+
 #define ENCODING_INVALID    0
 #define ENCODING_DEFAULT    1
 
@@ -65,6 +67,8 @@
         return AUDIO_FORMAT_AAC_HE_V1;
     case ENCODING_AAC_HE_V2:
         return AUDIO_FORMAT_AAC_HE_V2;
+    case ENCODING_DOLBY_TRUEHD:
+        return AUDIO_FORMAT_DOLBY_TRUEHD;
     case ENCODING_IEC61937:
         return AUDIO_FORMAT_IEC61937;
     case ENCODING_DEFAULT:
@@ -108,6 +112,8 @@
         return ENCODING_AAC_HE_V2;
     case AUDIO_FORMAT_IEC61937:
         return ENCODING_IEC61937;
+    case AUDIO_FORMAT_DOLBY_TRUEHD:
+        return ENCODING_DOLBY_TRUEHD;
     case AUDIO_FORMAT_DEFAULT:
         return ENCODING_DEFAULT;
     default:
diff --git a/core/jni/android_media_AudioSystem.cpp b/core/jni/android_media_AudioSystem.cpp
index ef16ef5..4976002 100644
--- a/core/jni/android_media_AudioSystem.cpp
+++ b/core/jni/android_media_AudioSystem.cpp
@@ -20,13 +20,14 @@
 #define LOG_TAG "AudioSystem-JNI"
 #include <utils/Log.h>
 
+#include <sstream>
 #include <jni.h>
 #include <JNIHelp.h>
 #include "core_jni_helpers.h"
 
 #include <media/AudioSystem.h>
 #include <media/AudioPolicy.h>
-
+#include <nativehelper/ScopedLocalRef.h>
 #include <system/audio.h>
 #include <system/audio_policy.h>
 #include "android_media_AudioFormat.h"
@@ -903,6 +904,12 @@
     return false; // not found
 }
 
+// TODO: pull out to separate file
+template <typename T, size_t N>
+static constexpr size_t array_size(const T (&)[N]) {
+    return N;
+}
+
 static jint convertAudioPortFromNative(JNIEnv *env,
                                            jobject *jAudioPort, const struct audio_port *nAudioPort)
 {
@@ -923,6 +930,38 @@
     ALOGV("convertAudioPortFromNative id %d role %d type %d name %s",
         nAudioPort->id, nAudioPort->role, nAudioPort->type, nAudioPort->name);
 
+    // Verify audio port array count info.
+    if (nAudioPort->num_sample_rates > array_size(nAudioPort->sample_rates)
+            || nAudioPort->num_channel_masks > array_size(nAudioPort->channel_masks)
+            || nAudioPort->num_formats > array_size(nAudioPort->formats)
+            || nAudioPort->num_gains > array_size(nAudioPort->gains)) {
+
+        std::stringstream ss;
+        ss << "convertAudioPortFromNative array count out of bounds:"
+                << " num_sample_rates " << nAudioPort->num_sample_rates
+                << " num_channel_masks " << nAudioPort->num_channel_masks
+                << " num_formats " << nAudioPort->num_formats
+                << " num_gains " << nAudioPort->num_gains
+                ;
+        std::string s = ss.str();
+
+        // Prefer to log through Java wtf instead of native ALOGE.
+        ScopedLocalRef<jclass> jLogClass(env, env->FindClass("android/util/Log"));
+        jmethodID jWtfId = (jLogClass.get() == nullptr)
+                ? nullptr
+                : env->GetStaticMethodID(jLogClass.get(), "wtf",
+                        "(Ljava/lang/String;Ljava/lang/String;)I");
+        if (jWtfId != nullptr) {
+            ScopedLocalRef<jstring> jMessage(env, env->NewStringUTF(s.c_str()));
+            ScopedLocalRef<jstring> jTag(env, env->NewStringUTF(LOG_TAG));
+            (void)env->CallStaticIntMethod(jLogClass.get(), jWtfId, jTag.get(), jMessage.get());
+        } else {
+            ALOGE("%s", s.c_str());
+        }
+        jStatus = (jint)AUDIO_JAVA_ERROR;
+        goto exit;
+    }
+
     jSamplingRates = env->NewIntArray(nAudioPort->num_sample_rates);
     if (jSamplingRates == NULL) {
         jStatus = (jint)AUDIO_JAVA_ERROR;
@@ -1066,7 +1105,7 @@
                                                        &jAudioPortConfig,
                                                        &nAudioPort->active_config);
     if (jStatus != AUDIO_JAVA_SUCCESS) {
-        return jStatus;
+        goto exit;
     }
 
     env->SetObjectField(*jAudioPort, gAudioPortFields.mActiveConfig, jAudioPortConfig);
diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp
index f79254d..26a2cf0 100644
--- a/core/jni/android_net_NetUtils.cpp
+++ b/core/jni/android_net_NetUtils.cpp
@@ -122,6 +122,99 @@
     }
 }
 
+static void android_net_utils_setupRaSocket(JNIEnv *env, jobject clazz, jobject javaFd,
+        jint ifIndex)
+{
+    static const int kLinkLocalHopLimit = 255;
+
+    int fd = jniGetFDFromFileDescriptor(env, javaFd);
+
+    // Set an ICMPv6 filter that only passes Router Solicitations.
+    struct icmp6_filter rs_only;
+    ICMP6_FILTER_SETBLOCKALL(&rs_only);
+    ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &rs_only);
+    socklen_t len = sizeof(rs_only);
+    if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &rs_only, len) != 0) {
+        jniThrowExceptionFmt(env, "java/net/SocketException",
+                "setsockopt(ICMP6_FILTER): %s", strerror(errno));
+        return;
+    }
+
+    // Most/all of the rest of these options can be set via Java code, but
+    // because we're here on account of setting an icmp6_filter go ahead
+    // and do it all natively for now.
+    //
+    // TODO: Consider moving these out to Java.
+
+    // Set the multicast hoplimit to 255 (link-local only).
+    int hops = kLinkLocalHopLimit;
+    len = sizeof(hops);
+    if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops, len) != 0) {
+        jniThrowExceptionFmt(env, "java/net/SocketException",
+                "setsockopt(IPV6_MULTICAST_HOPS): %s", strerror(errno));
+        return;
+    }
+
+    // Set the unicast hoplimit to 255 (link-local only).
+    hops = kLinkLocalHopLimit;
+    len = sizeof(hops);
+    if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hops, len) != 0) {
+        jniThrowExceptionFmt(env, "java/net/SocketException",
+                "setsockopt(IPV6_UNICAST_HOPS): %s", strerror(errno));
+        return;
+    }
+
+    // Explicitly disable multicast loopback.
+    int off = 0;
+    len = sizeof(off);
+    if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, len) != 0) {
+        jniThrowExceptionFmt(env, "java/net/SocketException",
+                "setsockopt(IPV6_MULTICAST_LOOP): %s", strerror(errno));
+        return;
+    }
+
+    // Specify the IPv6 interface to use for outbound multicast.
+    len = sizeof(ifIndex);
+    if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifIndex, len) != 0) {
+        jniThrowExceptionFmt(env, "java/net/SocketException",
+                "setsockopt(IPV6_MULTICAST_IF): %s", strerror(errno));
+        return;
+    }
+
+    // Additional options to be considered:
+    //     - IPV6_TCLASS
+    //     - IPV6_RECVPKTINFO
+    //     - IPV6_RECVHOPLIMIT
+
+    // Bind to [::].
+    const struct sockaddr_in6 sin6 = {
+            .sin6_family = AF_INET6,
+            .sin6_port = 0,
+            .sin6_flowinfo = 0,
+            .sin6_addr = IN6ADDR_ANY_INIT,
+            .sin6_scope_id = 0,
+    };
+    auto sa = reinterpret_cast<const struct sockaddr *>(&sin6);
+    len = sizeof(sin6);
+    if (bind(fd, sa, len) != 0) {
+        jniThrowExceptionFmt(env, "java/net/SocketException",
+                "bind(IN6ADDR_ANY): %s", strerror(errno));
+        return;
+    }
+
+    // Join the all-routers multicast group, ff02::2%index.
+    struct ipv6_mreq all_rtrs = {
+        .ipv6mr_multiaddr = {{{0xff,2,0,0,0,0,0,0,0,0,0,0,0,0,0,2}}},
+        .ipv6mr_interface = ifIndex,
+    };
+    len = sizeof(all_rtrs);
+    if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &all_rtrs, len) != 0) {
+        jniThrowExceptionFmt(env, "java/net/SocketException",
+                "setsockopt(IPV6_JOIN_GROUP): %s", strerror(errno));
+        return;
+    }
+}
+
 static jboolean android_net_utils_bindProcessToNetwork(JNIEnv *env, jobject thiz, jint netId)
 {
     return (jboolean) !setNetworkForProcess(netId);
@@ -170,6 +263,7 @@
     { "queryUserAccess", "(II)Z", (void*)android_net_utils_queryUserAccess },
     { "attachDhcpFilter", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_attachDhcpFilter },
     { "attachRaFilter", "(Ljava/io/FileDescriptor;I)V", (void*) android_net_utils_attachRaFilter },
+    { "setupRaSocket", "(Ljava/io/FileDescriptor;I)V", (void*) android_net_utils_setupRaSocket },
 };
 
 int register_android_net_NetworkUtils(JNIEnv* env)
diff --git a/core/jni/android_util_AssetManager.cpp b/core/jni/android_util_AssetManager.cpp
index 1e6b125..428159a 100644
--- a/core/jni/android_util_AssetManager.cpp
+++ b/core/jni/android_util_AssetManager.cpp
@@ -25,6 +25,7 @@
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
+#include <sys/system_properties.h>
 
 #include <private/android_filesystem_config.h> // for AID_SYSTEM
 
@@ -41,6 +42,7 @@
 #include "ScopedUtfChars.h"
 #include "utils/Log.h"
 #include "utils/misc.h"
+#include "utils/String8.h"
 
 extern "C" int capget(cap_user_header_t hdrp, cap_user_data_t datap);
 extern "C" int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
@@ -184,11 +186,20 @@
                 argv[argc++] = AssetManager::TARGET_APK_PATH;
                 argv[argc++] = AssetManager::IDMAP_DIR;
 
-                // Directories to scan for overlays
-                // /vendor/overlay
-                if (stat(AssetManager::OVERLAY_DIR, &st) == 0) {
-                    argv[argc++] = AssetManager::OVERLAY_DIR;
-                 }
+                // Directories to scan for overlays: if OVERLAY_SKU_DIR_PROPERTY is defined,
+                // use OVERLAY_DIR/<value of OVERLAY_SKU_DIR_PROPERTY> if exists, otherwise
+                // use OVERLAY_DIR if exists.
+                char subdir[PROP_VALUE_MAX];
+                int len = __system_property_get(AssetManager::OVERLAY_SKU_DIR_PROPERTY, subdir);
+                String8 overlayPath;
+                if (len > 0) {
+                    overlayPath = String8(AssetManager::OVERLAY_DIR) + "/" + subdir;
+                } else {
+                    overlayPath = String8(AssetManager::OVERLAY_DIR);
+                }
+                if (stat(overlayPath.string(), &st) == 0) {
+                    argv[argc++] = overlayPath.string();
+                }
 
                 // Finally, invoke idmap (if any overlay directory exists)
                 if (argc > 5) {
diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp
index 426136b..1cfbd97 100644
--- a/core/jni/android_util_Process.cpp
+++ b/core/jni/android_util_Process.cpp
@@ -57,21 +57,19 @@
 #endif
 
 // For both of these, err should be in the errno range (positive), not a status_t (negative)
-
-static void signalExceptionForPriorityError(JNIEnv* env, int err)
-{
+static void signalExceptionForError(JNIEnv* env, int err, int tid) {
     switch (err) {
         case EINVAL:
-            jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
+            jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
+                                 "Invalid argument: %d", tid);
             break;
         case ESRCH:
-            jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
+            jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
+                                 "Given thread %d does not exist", tid);
             break;
         case EPERM:
-            jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
-            break;
-        case EACCES:
-            jniThrowException(env, "java/lang/SecurityException", "No permission to set to given priority");
+            jniThrowExceptionFmt(env, "java/lang/SecurityException",
+                                 "No permission to modify given thread %d", tid);
             break;
         default:
             jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
@@ -79,23 +77,27 @@
     }
 }
 
-static void signalExceptionForGroupError(JNIEnv* env, int err)
-{
+static void signalExceptionForPriorityError(JNIEnv* env, int err, int tid) {
     switch (err) {
-        case EINVAL:
-            jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
-            break;
-        case ESRCH:
-            jniThrowException(env, "java/lang/IllegalArgumentException", "Given thread does not exist");
-            break;
-        case EPERM:
-            jniThrowException(env, "java/lang/SecurityException", "No permission to modify given thread");
-            break;
         case EACCES:
-            jniThrowException(env, "java/lang/SecurityException", "No permission to set to given group");
+            jniThrowExceptionFmt(env, "java/lang/SecurityException",
+                                 "No permission to set the priority of %d", tid);
             break;
         default:
-            jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
+            signalExceptionForError(env, err, tid);
+            break;
+    }
+
+}
+
+static void signalExceptionForGroupError(JNIEnv* env, int err, int tid) {
+    switch (err) {
+        case EACCES:
+            jniThrowExceptionFmt(env, "java/lang/SecurityException",
+                                 "No permission to set the group of %d", tid);
+            break;
+        default:
+            signalExceptionForError(env, err, tid);
             break;
     }
 }
@@ -170,7 +172,7 @@
     SchedPolicy sp = (SchedPolicy) grp;
     int res = set_sched_policy(tid, sp);
     if (res != NO_ERROR) {
-        signalExceptionForGroupError(env, -res);
+        signalExceptionForGroupError(env, -res, tid);
     }
 }
 
@@ -182,7 +184,7 @@
     struct dirent *de;
 
     if ((grp == SP_FOREGROUND) || (grp > SP_MAX)) {
-        signalExceptionForGroupError(env, EINVAL);
+        signalExceptionForGroupError(env, EINVAL, pid);
         return;
     }
 
@@ -218,7 +220,7 @@
     if (!(d = opendir(proc_path))) {
         // If the process exited on us, don't generate an exception
         if (errno != ENOENT)
-            signalExceptionForGroupError(env, errno);
+            signalExceptionForGroupError(env, errno, pid);
         return;
     }
 
@@ -253,7 +255,7 @@
 #ifdef ENABLE_CPUSETS
                 int err = set_cpuset_policy(t_pid, sp);
                 if (err != NO_ERROR) {
-                    signalExceptionForGroupError(env, -err);
+                    signalExceptionForGroupError(env, -err, t_pid);
                     break;
                 }
 #endif
@@ -265,14 +267,14 @@
         // set both cpuset and cgroup for general threads
         err = set_cpuset_policy(t_pid, sp);
         if (err != NO_ERROR) {
-            signalExceptionForGroupError(env, -err);
+            signalExceptionForGroupError(env, -err, t_pid);
             break;
         }
 #endif
 
         err = set_sched_policy(t_pid, sp);
         if (err != NO_ERROR) {
-            signalExceptionForGroupError(env, -err);
+            signalExceptionForGroupError(env, -err, t_pid);
             break;
         }
 
@@ -284,7 +286,7 @@
 {
     SchedPolicy sp;
     if (get_sched_policy(pid, &sp) != 0) {
-        signalExceptionForGroupError(env, errno);
+        signalExceptionForGroupError(env, errno, pid);
     }
     return (int) sp;
 }
@@ -399,7 +401,7 @@
     jintArray cpus;
     int pid = getpid();
     if (get_sched_policy(pid, &sp) != 0) {
-        signalExceptionForGroupError(env, errno);
+        signalExceptionForGroupError(env, errno, pid);
         return NULL;
     }
     get_exclusive_cpuset_cores(sp, &cpu_set);
@@ -439,6 +441,23 @@
 #endif
 }
 
+jint android_os_Process_getThreadScheduler(JNIEnv* env, jclass clazz,
+                                              jint tid)
+{
+    int policy = 0;
+// linux has sched_getscheduler(), others don't.
+#if defined(__linux__)
+    errno = 0;
+    policy = sched_getscheduler(tid);
+    if (errno != 0) {
+        signalExceptionForPriorityError(env, errno, tid);
+    }
+#else
+    signalExceptionForPriorityError(env, ENOSYS, tid);
+#endif
+    return policy;
+}
+
 void android_os_Process_setThreadScheduler(JNIEnv* env, jclass clazz,
                                               jint tid, jint policy, jint pri)
 {
@@ -448,10 +467,10 @@
     param.sched_priority = pri;
     int rc = sched_setscheduler(tid, policy, &param);
     if (rc) {
-        signalExceptionForPriorityError(env, errno);
+        signalExceptionForPriorityError(env, errno, tid);
     }
 #else
-    signalExceptionForPriorityError(env, ENOSYS);
+    signalExceptionForPriorityError(env, ENOSYS, tid);
 #endif
 }
 
@@ -476,9 +495,9 @@
     int rc = androidSetThreadPriority(pid, pri);
     if (rc != 0) {
         if (rc == INVALID_OPERATION) {
-            signalExceptionForPriorityError(env, errno);
+            signalExceptionForPriorityError(env, errno, pid);
         } else {
-            signalExceptionForGroupError(env, errno);
+            signalExceptionForGroupError(env, errno, pid);
         }
     }
 
@@ -498,7 +517,7 @@
     errno = 0;
     jint pri = getpriority(PRIO_PROCESS, pid);
     if (errno != 0) {
-        signalExceptionForPriorityError(env, errno);
+        signalExceptionForPriorityError(env, errno, pid);
     }
     //ALOGI("Returning priority of %" PRId32 ": %" PRId32 "\n", pid, pri);
     return pri;
@@ -1188,6 +1207,7 @@
     {"setCanSelfBackground", "(Z)V", (void*)android_os_Process_setCanSelfBackground},
     {"setThreadPriority",   "(I)V", (void*)android_os_Process_setCallingThreadPriority},
     {"getThreadPriority",   "(I)I", (void*)android_os_Process_getThreadPriority},
+    {"getThreadScheduler",   "(I)I", (void*)android_os_Process_getThreadScheduler},
     {"setThreadGroup",      "(II)V", (void*)android_os_Process_setThreadGroup},
     {"setProcessGroup",     "(II)V", (void*)android_os_Process_setProcessGroup},
     {"getProcessGroup",     "(I)I", (void*)android_os_Process_getProcessGroup},
diff --git a/core/jni/android_view_RenderNode.cpp b/core/jni/android_view_RenderNode.cpp
index 4fc546c..1bad5f8 100644
--- a/core/jni/android_view_RenderNode.cpp
+++ b/core/jni/android_view_RenderNode.cpp
@@ -573,8 +573,9 @@
                 bounds.roundOut();
             }
 
+            incStrong(0);
             auto functor = std::bind(
-                std::mem_fn(&SurfaceViewPositionUpdater::doUpdatePosition), this,
+                std::mem_fn(&SurfaceViewPositionUpdater::doUpdatePositionAsync), this,
                 (jlong) info.canvasContext.getFrameNumber(),
                 (jint) bounds.left, (jint) bounds.top,
                 (jint) bounds.right, (jint) bounds.bottom);
@@ -585,15 +586,18 @@
         virtual void onPositionLost(RenderNode& node, const TreeInfo* info) override {
             if (CC_UNLIKELY(!mWeakRef || (info && !info->updateWindowPositions))) return;
 
-            if (info) {
-                auto functor = std::bind(
-                    std::mem_fn(&SurfaceViewPositionUpdater::doNotifyPositionLost), this,
-                    (jlong) info->canvasContext.getFrameNumber());
-
-                info->canvasContext.enqueueFrameWork(std::move(functor));
-            } else {
-                doNotifyPositionLost(0);
+            ATRACE_NAME("SurfaceView position lost");
+            JNIEnv* env = jnienv();
+            jobject localref = env->NewLocalRef(mWeakRef);
+            if (CC_UNLIKELY(!localref)) {
+                jnienv()->DeleteWeakGlobalRef(mWeakRef);
+                mWeakRef = nullptr;
+                return;
             }
+
+            env->CallVoidMethod(localref, gSurfaceViewPositionLostMethod,
+                    info ? info->canvasContext.getFrameNumber() : 0);
+            env->DeleteLocalRef(localref);
         }
 
     private:
@@ -605,36 +609,23 @@
             return env;
         }
 
-        void doUpdatePosition(jlong frameNumber, jint left, jint top,
+        void doUpdatePositionAsync(jlong frameNumber, jint left, jint top,
                 jint right, jint bottom) {
             ATRACE_NAME("Update SurfaceView position");
 
             JNIEnv* env = jnienv();
             jobject localref = env->NewLocalRef(mWeakRef);
             if (CC_UNLIKELY(!localref)) {
-                jnienv()->DeleteWeakGlobalRef(mWeakRef);
+                env->DeleteWeakGlobalRef(mWeakRef);
                 mWeakRef = nullptr;
-                return;
+            } else {
+                env->CallVoidMethod(localref, gSurfaceViewPositionUpdateMethod,
+                        frameNumber, left, top, right, bottom);
+                env->DeleteLocalRef(localref);
             }
 
-            env->CallVoidMethod(localref, gSurfaceViewPositionUpdateMethod,
-                    frameNumber, left, top, right, bottom);
-            env->DeleteLocalRef(localref);
-        }
-
-        void doNotifyPositionLost(jlong frameNumber) {
-            ATRACE_NAME("SurfaceView position lost");
-
-            JNIEnv* env = jnienv();
-            jobject localref = env->NewLocalRef(mWeakRef);
-            if (CC_UNLIKELY(!localref)) {
-                jnienv()->DeleteWeakGlobalRef(mWeakRef);
-                mWeakRef = nullptr;
-                return;
-            }
-
-            env->CallVoidMethod(localref, gSurfaceViewPositionLostMethod, frameNumber);
-            env->DeleteLocalRef(localref);
+            // We need to release ourselves here
+            decStrong(0);
         }
 
         JavaVM* mVm;
@@ -730,9 +721,9 @@
 int register_android_view_RenderNode(JNIEnv* env) {
     jclass clazz = FindClassOrDie(env, "android/view/SurfaceView");
     gSurfaceViewPositionUpdateMethod = GetMethodIDOrDie(env, clazz,
-            "updateWindowPositionRT", "(JIIII)V");
+            "updateWindowPosition_renderWorker", "(JIIII)V");
     gSurfaceViewPositionLostMethod = GetMethodIDOrDie(env, clazz,
-            "windowPositionLostRT", "(J)V");
+            "windowPositionLost_uiRtSync", "(J)V");
     clazz = FindClassOrDie(env, "android/view/RenderNode");
     gOnRenderNodeDetached = GetMethodIDOrDie(env, clazz,
             "onRenderNodeDetached", "()V");
diff --git a/core/jni/android_view_Surface.cpp b/core/jni/android_view_Surface.cpp
index 21e4d2f..a0c62c3 100644
--- a/core/jni/android_view_Surface.cpp
+++ b/core/jni/android_view_Surface.cpp
@@ -487,6 +487,11 @@
     return surface->setScalingMode(scalingMode);
 }
 
+static jint nativeForceScopedDisconnect(JNIEnv *env, jclass clazz, jlong nativeObject) {
+    Surface* surface = reinterpret_cast<Surface*>(nativeObject);
+    return surface->disconnect(-1, IGraphicBufferProducer::DisconnectMode::AllLocal);
+}
+
 namespace uirenderer {
 
 using namespace android::uirenderer::renderthread;
@@ -564,6 +569,7 @@
     {"nativeGetHeight", "(J)I", (void*)nativeGetHeight },
     {"nativeGetNextFrameNumber", "(J)J", (void*)nativeGetNextFrameNumber },
     {"nativeSetScalingMode", "(JI)I", (void*)nativeSetScalingMode },
+    {"nativeForceScopedDisconnect", "(J)I", (void*)nativeForceScopedDisconnect},
 
     // HWUI context
     {"nHwuiCreate", "(JJ)J", (void*) hwui::create },
diff --git a/core/jni/android_view_SurfaceControl.cpp b/core/jni/android_view_SurfaceControl.cpp
index ff75677..73b3f52 100644
--- a/core/jni/android_view_SurfaceControl.cpp
+++ b/core/jni/android_view_SurfaceControl.cpp
@@ -32,6 +32,7 @@
 #include <jni.h>
 #include <memory>
 #include <stdio.h>
+#include <system/graphics.h>
 #include <ui/DisplayInfo.h>
 #include <ui/HdrCapabilities.h>
 #include <ui/FrameStats.h>
@@ -58,7 +59,6 @@
     jfieldID secure;
     jfieldID appVsyncOffsetNanos;
     jfieldID presentationDeadlineNanos;
-    jfieldID colorTransform;
 } gPhysicalDisplayInfoClassInfo;
 
 static struct {
@@ -248,10 +248,10 @@
     }
 }
 
-static void nativeSetPositionAppliesWithResize(JNIEnv* env, jclass clazz,
+static void nativeSetGeometryAppliesWithResize(JNIEnv* env, jclass clazz,
         jlong nativeObject) {
     SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
-    status_t err = ctrl->setPositionAppliesWithResize();
+    status_t err = ctrl->setGeometryAppliesWithResize();
     if (err < 0 && err != NO_INIT) {
         doThrowIAE(env);
     }
@@ -371,7 +371,12 @@
     if (sur != NULL) {
         bufferProducer = sur->getIGraphicBufferProducer();
     }
-    SurfaceComposerClient::setDisplaySurface(token, bufferProducer);
+    status_t err = SurfaceComposerClient::setDisplaySurface(token,
+            bufferProducer);
+    if (err != NO_ERROR) {
+        doThrowIAE(env, "Illegal Surface, could not enable async mode. Was this"
+                " Surface created with singleBufferMode?");
+    }
 }
 
 static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz,
@@ -429,8 +434,6 @@
                 info.appVsyncOffset);
         env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos,
                 info.presentationDeadline);
-        env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.colorTransform,
-                info.colorTransform);
         env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj);
         env->DeleteLocalRef(infoObj);
     }
@@ -451,6 +454,43 @@
     return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
 }
 
+static jintArray nativeGetDisplayColorModes(JNIEnv* env, jclass, jobject tokenObj) {
+    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
+    if (token == NULL) return NULL;
+    Vector<android_color_mode_t> colorModes;
+    if (SurfaceComposerClient::getDisplayColorModes(token, &colorModes) != NO_ERROR ||
+            colorModes.isEmpty()) {
+        return NULL;
+    }
+
+    jintArray colorModesArray = env->NewIntArray(colorModes.size());
+    if (colorModesArray == NULL) {
+        jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
+        return NULL;
+    }
+    jint* colorModesArrayValues = env->GetIntArrayElements(colorModesArray, 0);
+    for (size_t i = 0; i < colorModes.size(); i++) {
+        colorModesArrayValues[i] = static_cast<jint>(colorModes[i]);
+    }
+    env->ReleaseIntArrayElements(colorModesArray, colorModesArrayValues, 0);
+    return colorModesArray;
+}
+
+static jint nativeGetActiveColorMode(JNIEnv* env, jclass, jobject tokenObj) {
+    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
+    if (token == NULL) return -1;
+    return static_cast<jint>(SurfaceComposerClient::getActiveColorMode(token));
+}
+
+static jboolean nativeSetActiveColorMode(JNIEnv* env, jclass,
+        jobject tokenObj, jint colorMode) {
+    sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
+    if (token == NULL) return JNI_FALSE;
+    status_t err = SurfaceComposerClient::setActiveColorMode(token,
+            static_cast<android_color_mode_t>(colorMode));
+    return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
+}
+
 static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) {
     sp<IBinder> token(ibinderForJavaObject(env, tokenObj));
     if (token == NULL) return;
@@ -626,6 +666,16 @@
     return javaObjectForIBinder(env, ctrl->getHandle());
 }
 
+static jboolean nativeGetTransformToDisplayInverse(JNIEnv* env, jclass clazz, jlong nativeObject) {
+    bool out = false;
+    auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject);
+    status_t status = ctrl->getTransformToDisplayInverse(&out);
+    if (status != NO_ERROR) {
+        return false;
+    }
+    return out;
+}
+
 static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) {
     sp<IBinder> token(ibinderForJavaObject(env, tokenObject));
     if (token == NULL) return NULL;
@@ -667,8 +717,8 @@
             (void*)nativeSetLayer },
     {"nativeSetPosition", "(JFF)V",
             (void*)nativeSetPosition },
-    {"nativeSetPositionAppliesWithResize", "(J)V",
-            (void*)nativeSetPositionAppliesWithResize },
+    {"nativeSetGeometryAppliesWithResize", "(J)V",
+            (void*)nativeSetGeometryAppliesWithResize },
     {"nativeSetSize", "(JII)V",
             (void*)nativeSetSize },
     {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V",
@@ -705,6 +755,12 @@
             (void*)nativeGetActiveConfig },
     {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z",
             (void*)nativeSetActiveConfig },
+    {"nativeGetDisplayColorModes", "(Landroid/os/IBinder;)[I",
+            (void*)nativeGetDisplayColorModes},
+    {"nativeGetActiveColorMode", "(Landroid/os/IBinder;)I",
+            (void*)nativeGetActiveColorMode},
+    {"nativeSetActiveColorMode", "(Landroid/os/IBinder;I)Z",
+            (void*)nativeSetActiveColorMode},
     {"nativeGetHdrCapabilities", "(Landroid/os/IBinder;)Landroid/view/Display$HdrCapabilities;",
             (void*)nativeGetHdrCapabilities },
     {"nativeClearContentFrameStats", "(J)Z",
@@ -722,7 +778,9 @@
     {"nativeSetOverrideScalingMode", "(JI)V",
             (void*)nativeSetOverrideScalingMode },
     {"nativeGetHandle", "(J)Landroid/os/IBinder;",
-            (void*)nativeGetHandle }
+            (void*)nativeGetHandle },
+    {"nativeGetTransformToDisplayInverse", "(J)Z",
+            (void*)nativeGetTransformToDisplayInverse },
 };
 
 int register_android_view_SurfaceControl(JNIEnv* env)
@@ -745,8 +803,6 @@
             clazz, "appVsyncOffsetNanos", "J");
     gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env,
             clazz, "presentationDeadlineNanos", "J");
-    gPhysicalDisplayInfoClassInfo.colorTransform = GetFieldIDOrDie(env, clazz,
-            "colorTransform", "I");
 
     jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect");
     gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I");
diff --git a/core/jni/android_view_ThreadedRenderer.cpp b/core/jni/android_view_ThreadedRenderer.cpp
index 172e12d..9c0a65a 100644
--- a/core/jni/android_view_ThreadedRenderer.cpp
+++ b/core/jni/android_view_ThreadedRenderer.cpp
@@ -32,6 +32,7 @@
 #include <utils/Looper.h>
 #include <utils/RefBase.h>
 #include <utils/StrongPointer.h>
+#include <utils/Timers.h>
 #include <android_runtime/android_view_Surface.h>
 #include <system/window.h>
 
@@ -44,6 +45,7 @@
 #include <FrameMetricsObserver.h>
 #include <IContextFactory.h>
 #include <JankTracker.h>
+#include <PropertyValuesAnimatorSet.h>
 #include <RenderNode.h>
 #include <renderthread/CanvasContext.h>
 #include <renderthread/RenderProxy.h>
@@ -122,6 +124,31 @@
     std::vector<OnFinishedEvent> mOnFinishedEvents;
 };
 
+class FinishAndInvokeListener : public MessageHandler {
+public:
+    explicit FinishAndInvokeListener(PropertyValuesAnimatorSet* anim)
+            : mAnimator(anim) {
+        mListener = anim->getOneShotListener();
+        mRequestId = anim->getRequestId();
+    }
+
+    virtual void handleMessage(const Message& message) {
+        if (mAnimator->getRequestId() == mRequestId) {
+            // Request Id has not changed, meaning there's no animation lifecyle change since the
+            // message is posted, so go ahead and call finish to make sure the PlayState is properly
+            // updated. This is needed because before the next frame comes in from UI thread to
+            // trigger an animation update, there could be reverse/cancel etc. So we need to update
+            // the playstate in time to ensure all the subsequent events get chained properly.
+            mAnimator->end();
+        }
+        mListener->onAnimationFinished(nullptr);
+    }
+private:
+    sp<PropertyValuesAnimatorSet> mAnimator;
+    sp<AnimationListener> mListener;
+    uint32_t mRequestId;
+};
+
 class RenderingException : public MessageHandler {
 public:
     RenderingException(JavaVM* vm, const std::string& message)
@@ -160,6 +187,23 @@
 
     virtual void prepareTree(TreeInfo& info) override {
         info.errorHandler = this;
+
+        for (auto& anim : mRunningVDAnimators) {
+            // Assume that the property change in VD from the animators will not be consumed. Mark
+            // otherwise if the VDs are found in the display list tree. For VDs that are not in
+            // the display list tree, we stop providing animation pulses by 1) removing them from
+            // the animation list, 2) post a delayed message to end them at end time so their
+            // listeners can receive the corresponding callbacks.
+            anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false);
+            // Mark the VD dirty so it will damage itself during prepareTree.
+            anim->getVectorDrawable()->markDirty();
+        }
+        if (info.mode == TreeInfo::MODE_FULL) {
+            for (auto &anim : mPausedVDAnimators) {
+                anim->getVectorDrawable()->setPropertyChangeWillBeConsumed(false);
+                anim->getVectorDrawable()->markDirty();
+            }
+        }
         // TODO: This is hacky
         info.windowInsetLeft = -stagingProperties().getLeft();
         info.windowInsetTop = -stagingProperties().getTop();
@@ -175,10 +219,39 @@
         mLooper->sendMessage(handler, 0);
     }
 
+    void sendMessageDelayed(const sp<MessageHandler>& handler, nsecs_t delayInMs) {
+        mLooper->sendMessageDelayed(ms2ns(delayInMs), handler, 0);
+    }
+
     void attachAnimatingNode(RenderNode* animatingNode) {
         mPendingAnimatingRenderNodes.push_back(animatingNode);
     }
 
+    void attachPendingVectorDrawableAnimators() {
+        mRunningVDAnimators.insert(mPendingVectorDrawableAnimators.begin(),
+                mPendingVectorDrawableAnimators.end());
+        mPendingVectorDrawableAnimators.clear();
+    }
+
+    void detachAnimators() {
+        // Remove animators from the list and post a delayed message in future to end the animator
+        for (auto& anim : mRunningVDAnimators) {
+            detachVectorDrawableAnimator(anim.get());
+        }
+        mRunningVDAnimators.clear();
+        mPausedVDAnimators.clear();
+    }
+
+    // Move all the animators to the paused list, and send a delayed message to notify the finished
+    // listener.
+    void pauseAnimators() {
+        mPausedVDAnimators.insert(mRunningVDAnimators.begin(), mRunningVDAnimators.end());
+        for (auto& anim : mRunningVDAnimators) {
+            detachVectorDrawableAnimator(anim.get());
+        }
+        mRunningVDAnimators.clear();
+    }
+
     void doAttachAnimatingNodes(AnimationContext* context) {
         for (size_t i = 0; i < mPendingAnimatingRenderNodes.size(); i++) {
             RenderNode* node = mPendingAnimatingRenderNodes[i].get();
@@ -187,17 +260,144 @@
         mPendingAnimatingRenderNodes.clear();
     }
 
+    // Run VectorDrawable animators after prepareTree.
+    void runVectorDrawableAnimators(AnimationContext* context, TreeInfo& info) {
+        // Push staging.
+        if (info.mode == TreeInfo::MODE_FULL) {
+            pushStagingVectorDrawableAnimators(context);
+        }
+
+        // Run the animators in the running list.
+        for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) {
+            if ((*it)->animate(*context)) {
+                it = mRunningVDAnimators.erase(it);
+            } else {
+                it++;
+            }
+        }
+
+        // Run the animators in paused list during full sync.
+        if (info.mode == TreeInfo::MODE_FULL) {
+            // During full sync we also need to pulse paused animators, in case their targets
+            // have been added back to the display list. All the animators that passed the
+            // scheduled finish time will be removed from the paused list.
+            for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
+                if ((*it)->animate(*context)) {
+                    // Animator has finished, remove from the list.
+                    it = mPausedVDAnimators.erase(it);
+                } else {
+                    it++;
+                }
+            }
+        }
+
+        // Move the animators with a target not in DisplayList to paused list.
+        for (auto it = mRunningVDAnimators.begin(); it != mRunningVDAnimators.end();) {
+            if (!(*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) {
+                // Vector Drawable is not in the display list, we should remove this animator from
+                // the list, put it in the paused list, and post a delayed message to end the
+                // animator.
+                detachVectorDrawableAnimator(it->get());
+                mPausedVDAnimators.insert(*it);
+                it = mRunningVDAnimators.erase(it);
+            } else {
+                it++;
+            }
+        }
+
+        // Move the animators with a target in DisplayList from paused list to running list, and
+        // trim paused list.
+        if (info.mode == TreeInfo::MODE_FULL) {
+            // Check whether any paused animator's target is back in Display List. If so, put the
+            // animator back in the running list.
+            for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
+                if ((*it)->getVectorDrawable()->getPropertyChangeWillBeConsumed()) {
+                    mRunningVDAnimators.insert(*it);
+                    it = mPausedVDAnimators.erase(it);
+                } else {
+                    it++;
+                }
+            }
+            // Trim paused VD animators at full sync, so that when Java loses reference to an
+            // animator, we know we won't be requested to animate it any more, then we remove such
+            // animators from the paused list so they can be properly freed. We also remove the
+            // animators from paused list when the time elapsed since start has exceeded duration.
+            trimPausedVDAnimators(context);
+        }
+
+        info.out.hasAnimations |= !mRunningVDAnimators.empty();
+    }
+
+    void trimPausedVDAnimators(AnimationContext* context) {
+        // Trim paused vector drawable animator list.
+        for (auto it = mPausedVDAnimators.begin(); it != mPausedVDAnimators.end();) {
+            // Remove paused VD animator if no one else is referencing it. Note that animators that
+            // have passed scheduled finish time are removed from list when they are being pulsed
+            // before prepare tree.
+            // TODO: this is a bit hacky, need to figure out a better way to track when the paused
+            // animators should be freed.
+            if ((*it)->getStrongCount() == 1) {
+                it = mPausedVDAnimators.erase(it);
+            } else {
+                it++;
+            }
+        }
+    }
+
+    void pushStagingVectorDrawableAnimators(AnimationContext* context) {
+        for (auto& anim : mRunningVDAnimators) {
+            anim->pushStaging(*context);
+        }
+    }
+
     void destroy() {
         for (auto& renderNode : mPendingAnimatingRenderNodes) {
             renderNode->animators().endAllStagingAnimators();
         }
         mPendingAnimatingRenderNodes.clear();
+        mPendingVectorDrawableAnimators.clear();
+    }
+
+    void addVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) {
+        mPendingVectorDrawableAnimators.insert(anim);
     }
 
 private:
     sp<Looper> mLooper;
     JavaVM* mVm;
     std::vector< sp<RenderNode> > mPendingAnimatingRenderNodes;
+    std::set< sp<PropertyValuesAnimatorSet> > mPendingVectorDrawableAnimators;
+    std::set< sp<PropertyValuesAnimatorSet> > mRunningVDAnimators;
+    // mPausedVDAnimators stores a list of animators that have not yet passed the finish time, but
+    // their VectorDrawable targets are no longer in the DisplayList. We skip these animators when
+    // render thread runs animators independent of UI thread (i.e. RT_ONLY mode). These animators
+    // need to be re-activated once their VD target is added back into DisplayList. Since that could
+    // only happen when we do a full sync, we need to make sure to pulse these paused animators at
+    // full sync. If any animator's VD target is found in DisplayList during a full sync, we move
+    // the animator back to the running list.
+    std::set< sp<PropertyValuesAnimatorSet> > mPausedVDAnimators;
+    void detachVectorDrawableAnimator(PropertyValuesAnimatorSet* anim) {
+        if (anim->isInfinite() || !anim->isRunning()) {
+            // Do not need to post anything if the animation is infinite (i.e. no meaningful
+            // end listener action), or if the animation has already ended.
+            return;
+        }
+        nsecs_t remainingTimeInMs = anim->getRemainingPlayTime();
+        // Post a delayed onFinished event that is scheduled to be handled when the animator ends.
+        if (anim->getOneShotListener()) {
+            // VectorDrawable's oneshot listener is updated when there are user triggered animation
+            // lifecycle changes, such as start(), end(), etc. By using checking and clearing
+            // one shot listener, we ensure the same end listener event gets posted only once.
+            // Therefore no duplicates. Another benefit of using one shot listener is that no
+            // removal is necessary: the end time of animation will not change unless triggered by
+            // user events, in which case the already posted listener's id will become stale, and
+            // the onFinished callback will then be ignored.
+            sp<FinishAndInvokeListener> message
+                    = new FinishAndInvokeListener(anim);
+            sendMessageDelayed(message, remainingTimeInMs);
+            anim->clearOneShotListener();
+        }
+    }
 };
 
 class AnimationContextBridge : public AnimationContext {
@@ -213,6 +413,7 @@
     virtual void startFrame(TreeInfo::TraversalMode mode) {
         if (mode == TreeInfo::MODE_FULL) {
             mRootNode->doAttachAnimatingNodes(this);
+            mRootNode->attachPendingVectorDrawableAnimators();
         }
         AnimationContext::startFrame(mode);
     }
@@ -220,9 +421,14 @@
     // Runs any animations still left in mCurrentFrameAnimations
     virtual void runRemainingAnimations(TreeInfo& info) {
         AnimationContext::runRemainingAnimations(info);
+        mRootNode->runVectorDrawableAnimators(this, info);
         postOnFinishedEvents();
     }
 
+    virtual void pauseAnimators() override {
+        mRootNode->pauseAnimators();
+    }
+
     virtual void callOnFinished(BaseRenderNodeAnimator* animator, AnimationListener* listener) {
         OnFinishedEvent event(animator, listener);
         mOnFinishedEvents.push_back(event);
@@ -230,6 +436,7 @@
 
     virtual void destroy() {
         AnimationContext::destroy();
+        mRootNode->detachAnimators();
         postOnFinishedEvents();
     }
 
@@ -415,6 +622,12 @@
     proxy->setProcessStatsBuffer(fd);
 }
 
+static jint android_view_ThreadedRenderer_getRenderThreadTid(JNIEnv* env, jobject clazz,
+        jlong proxyPtr) {
+    RenderProxy* proxy = reinterpret_cast<RenderProxy*>(proxyPtr);
+    return proxy->getRenderThreadTid();
+}
+
 static jlong android_view_ThreadedRenderer_createRootRenderNode(JNIEnv* env, jobject clazz) {
     RootRenderNode* node = new RootRenderNode(env);
     node->incStrong(0);
@@ -527,6 +740,13 @@
     rootRenderNode->attachAnimatingNode(animatingNode);
 }
 
+static void android_view_ThreadedRenderer_registerVectorDrawableAnimator(JNIEnv* env, jobject clazz,
+        jlong rootNodePtr, jlong animatorPtr) {
+    RootRenderNode* rootRenderNode = reinterpret_cast<RootRenderNode*>(rootNodePtr);
+    PropertyValuesAnimatorSet* animator = reinterpret_cast<PropertyValuesAnimatorSet*>(animatorPtr);
+    rootRenderNode->addVectorDrawableAnimator(animator);
+}
+
 static void android_view_ThreadedRenderer_invokeFunctor(JNIEnv* env, jobject clazz,
         jlong functorPtr, jboolean waitForCompletion) {
     Functor* functor = reinterpret_cast<Functor*>(functorPtr);
@@ -723,6 +943,7 @@
 static const JNINativeMethod gMethods[] = {
     { "nSetAtlas", "(JLandroid/view/GraphicBuffer;[J)V",   (void*) android_view_ThreadedRenderer_setAtlas },
     { "nSetProcessStatsBuffer", "(JI)V", (void*) android_view_ThreadedRenderer_setProcessStatsBuffer },
+    { "nGetRenderThreadTid", "(J)I", (void*) android_view_ThreadedRenderer_getRenderThreadTid },
     { "nCreateRootRenderNode", "()J", (void*) android_view_ThreadedRenderer_createRootRenderNode },
     { "nCreateProxy", "(ZJ)J", (void*) android_view_ThreadedRenderer_createProxy },
     { "nDeleteProxy", "(J)V", (void*) android_view_ThreadedRenderer_deleteProxy },
@@ -738,6 +959,7 @@
     { "nSyncAndDrawFrame", "(J[JI)I", (void*) android_view_ThreadedRenderer_syncAndDrawFrame },
     { "nDestroy", "(JJ)V", (void*) android_view_ThreadedRenderer_destroy },
     { "nRegisterAnimatingRenderNode", "(JJ)V", (void*) android_view_ThreadedRenderer_registerAnimatingRenderNode },
+    { "nRegisterVectorDrawableAnimator", "(JJ)V", (void*) android_view_ThreadedRenderer_registerVectorDrawableAnimator },
     { "nInvokeFunctor", "(JZ)V", (void*) android_view_ThreadedRenderer_invokeFunctor },
     { "nCreateTextureLayer", "(J)J", (void*) android_view_ThreadedRenderer_createTextureLayer },
     { "nBuildLayer", "(JJ)V", (void*) android_view_ThreadedRenderer_buildLayer },
diff --git a/core/jni/com_android_internal_os_Zygote.cpp b/core/jni/com_android_internal_os_Zygote.cpp
index 959c9d7..cff54dd 100644
--- a/core/jni/com_android_internal_os_Zygote.cpp
+++ b/core/jni/com_android_internal_os_Zygote.cpp
@@ -178,7 +178,9 @@
   }
   int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
   if (rc == -1) {
-    RuntimeAbort(env, __LINE__, "setgroups failed");
+    std::ostringstream oss;
+    oss << "setgroups failed: " << strerror(errno) << ", gids.size=" << gids.size();
+    RuntimeAbort(env, __LINE__, oss.str().c_str());
   }
 }
 
diff --git a/core/jni/fd_utils-inl.h b/core/jni/fd_utils-inl.h
index db56c32..af27069 100644
--- a/core/jni/fd_utils-inl.h
+++ b/core/jni/fd_utils-inl.h
@@ -20,6 +20,7 @@
 #include <vector>
 #include <algorithm>
 
+#include <android-base/strings.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <grp.h>
@@ -50,7 +51,6 @@
   "/dev/null",
   "/dev/socket/zygote",
   "/dev/socket/zygote_secondary",
-  "/system/etc/event-log-tags",
   "/sys/kernel/debug/tracing/trace_marker",
   "/system/framework/framework-res.apk",
   "/dev/urandom",
@@ -242,7 +242,8 @@
 
   // Returns true iff. a given path is whitelisted. A path is whitelisted
   // if it belongs to the whitelist (see kPathWhitelist) or if it's a path
-  // under /system/framework that ends with ".jar".
+  // under /system/framework that ends with ".jar" or if it is a system
+  // framework overlay.
   static bool IsWhitelisted(const std::string& path) {
     for (size_t i = 0; i < (sizeof(kPathWhitelist) / sizeof(kPathWhitelist[0])); ++i) {
       if (kPathWhitelist[i] == path) {
@@ -250,12 +251,34 @@
       }
     }
 
-    static const std::string kFrameworksPrefix = "/system/framework/";
-    static const std::string kJarSuffix = ".jar";
-    if (path.compare(0, kFrameworksPrefix.size(), kFrameworksPrefix) == 0 &&
-        path.compare(path.size() - kJarSuffix.size(), kJarSuffix.size(), kJarSuffix) == 0) {
+    static const char* kFrameworksPrefix = "/system/framework/";
+    static const char* kJarSuffix = ".jar";
+    if (android::base::StartsWith(path, kFrameworksPrefix)
+        && android::base::EndsWith(path, kJarSuffix)) {
       return true;
     }
+
+    // Whitelist files needed for Runtime Resource Overlay, like these:
+    // /system/vendor/overlay/framework-res.apk
+    // /system/vendor/overlay/PG/android-framework-runtime-resource-overlay.apk
+    // /data/resource-cache/system@vendor@overlay@framework-res.apk@idmap
+    // /data/resource-cache/system@vendor@overlay@PG@framework-res.apk@idmap
+    static const char* kOverlayDir = "/system/vendor/overlay/";
+    static const char* kApkSuffix = ".apk";
+
+    if (android::base::StartsWith(path, kOverlayDir)
+        && android::base::EndsWith(path, kApkSuffix)
+        && path.find("/../") == std::string::npos) {
+      return true;
+    }
+
+    static const char* kOverlayIdmapPrefix = "/data/resource-cache/";
+    static const char* kOverlayIdmapSuffix = ".apk@idmap";
+    if (android::base::StartsWith(path, kOverlayIdmapPrefix)
+        && android::base::EndsWith(path, kOverlayIdmapSuffix)) {
+      return true;
+    }
+
     return false;
   }
 
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index ae560f9..586d091 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -49,6 +49,7 @@
     <protected-broadcast android:name="android.intent.action.PACKAGE_VERIFIED" />
     <protected-broadcast android:name="android.intent.action.PACKAGES_SUSPENDED" />
     <protected-broadcast android:name="android.intent.action.PACKAGES_UNSUSPENDED" />
+    <protected-broadcast android:name="android.intent.action.ACTION_PREFERRED_ACTIVITY_CHANGED" />
     <protected-broadcast android:name="android.intent.action.UID_REMOVED" />
     <protected-broadcast android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
     <protected-broadcast android:name="android.intent.action.CONFIGURATION_CHANGED" />
@@ -403,6 +404,8 @@
 
     <protected-broadcast android:name="android.bluetooth.adapter.action.BLE_STATE_CHANGED" />
     <protected-broadcast android:name="com.android.bluetooth.map.USER_CONFIRM_TIMEOUT" />
+    <protected-broadcast android:name="com.android.bluetooth.BluetoothMapContentObserver.action.MESSAGE_SENT" />
+    <protected-broadcast android:name="com.android.bluetooth.BluetoothMapContentObserver.action.MESSAGE_DELIVERY" />
     <protected-broadcast android:name="android.content.jobscheduler.JOB_DELAY_EXPIRED" />
     <protected-broadcast android:name="android.content.syncmanager.SYNC_ALARM" />
     <protected-broadcast android:name="android.media.INTERNAL_RINGER_MODE_CHANGED_ACTION" />
@@ -480,12 +483,6 @@
     <protected-broadcast android:name="android.net.wifi.PASSPOINT_ICON_RECEIVED" />
     <protected-broadcast android:name="com.android.server.notification.CountdownConditionProvider" />
 
-    <protected-broadcast android:name="com.android.ims.IMS_SERVICE_UP" />
-    <protected-broadcast android:name="com.android.ims.IMS_INCOMING_CALL" />
-    <protected-broadcast android:name="com.android.ims.internal.uce.UCE_SERVICE_UP" />
-    <protected-broadcast android:name="com.android.intent.action.IMS_FEATURE_CHANGED" />
-    <protected-broadcast android:name="com.android.intent.action.IMS_CONFIG_CHANGED" />
-
     <protected-broadcast android:name="com.android.internal.location.ALARM_WAKEUP" />
     <protected-broadcast android:name="com.android.internal.location.ALARM_TIMEOUT" />
     <protected-broadcast android:name="android.intent.action.GLOBAL_BUTTON" />
@@ -494,6 +491,8 @@
     <protected-broadcast android:name="android.intent.action.MANAGED_PROFILE_UNAVAILABLE" />
     <protected-broadcast android:name="com.android.server.pm.DISABLE_QUIET_MODE_AFTER_UNLOCK" />
 
+    <protected-broadcast android:name="com.android.server.retaildemo.ACTION_RESET_DEMO" />
+
     <!-- ====================================================================== -->
     <!--                          RUNTIME PERMISSIONS                           -->
     <!-- ====================================================================== -->
@@ -1275,6 +1274,11 @@
     <permission android:name="android.permission.CONNECTIVITY_INTERNAL"
         android:protectionLevel="signature|privileged" />
 
+    <!-- Allows an internal user to use restricted Networks.
+         @hide -->
+    <permission android:name="android.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS"
+        android:protectionLevel="signature|privileged" />
+
     <!-- Allows a system application to access hardware packet offload capabilities.
          @hide -->
     <permission android:name="android.permission.PACKET_KEEPALIVE_OFFLOAD"
@@ -1316,6 +1320,7 @@
         android:protectionLevel="dangerous"
         android:description="@string/permdesc_getAccounts"
         android:label="@string/permlab_getAccounts" />
+    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
 
     <!-- @SystemApi Allows applications to call into AccountAuthenticators.
     <p>Not for use by third-party applications. -->
@@ -1636,7 +1641,7 @@
 
     <!-- @hide Allows an application to create, remove users and get the list of
          users on the device. Applications holding this permission can only create restricted,
-         guest, managed, and ephemeral users. For creating other kind of users,
+         guest, managed, demo, and ephemeral users. For creating other kind of users,
          {@link android.Manifest.permission#MANAGE_USERS} is needed.
          This permission is not available to third party applications. -->
     <permission android:name="android.permission.CREATE_USERS"
@@ -2521,11 +2526,10 @@
     <permission android:name="android.permission.CONTROL_WIFI_DISPLAY"
         android:protectionLevel="signature" />
 
-    <!-- Allows an application to control the color transforms applied to
-         displays system-wide.
+    <!-- Allows an application to control the color modes set for displays system-wide.
          <p>Not for use by third-party applications.</p>
          @hide -->
-    <permission android:name="android.permission.CONFIGURE_DISPLAY_COLOR_TRANSFORM"
+    <permission android:name="android.permission.CONFIGURE_DISPLAY_COLOR_MODE"
         android:protectionLevel="signature" />
 
     <!-- @SystemApi Allows an application to control VPN.
@@ -3120,7 +3124,7 @@
                  android:killAfterRestore="false"
                  android:icon="@drawable/ic_launcher_android"
                  android:supportsRtl="true"
-                 android:theme="@style/Theme.Material.Light.DarkActionBar"
+                 android:theme="@style/Theme.DeviceDefault.Light.DarkActionBar"
                  android:defaultToDeviceProtectedStorage="true"
                  android:directBootAware="true">
         <activity android:name="com.android.internal.app.ChooserActivity"
@@ -3157,7 +3161,7 @@
                 android:label="@string/managed_profile_label">
         </activity-alias>
         <activity android:name="com.android.internal.app.HeavyWeightSwitcherActivity"
-                android:theme="@style/Theme.Material.Light.Dialog"
+                android:theme="@style/Theme.DeviceDefault.Light.Dialog"
                 android:label="@string/heavy_weight_switcher_title"
                 android:finishOnCloseSystemDialogs="true"
                 android:excludeFromRecents="true"
@@ -3190,7 +3194,7 @@
         <activity android:name="android.accounts.ChooseAccountActivity"
                 android:excludeFromRecents="true"
                 android:exported="true"
-                android:theme="@style/Theme.Material.Light.Dialog"
+                android:theme="@style/Theme.DeviceDefault.Light.Dialog"
                 android:label="@string/choose_account_label"
                 android:process=":ui">
         </activity>
@@ -3198,14 +3202,14 @@
         <activity android:name="android.accounts.ChooseTypeAndAccountActivity"
                 android:excludeFromRecents="true"
                 android:exported="true"
-                android:theme="@style/Theme.Material.Light.Dialog"
+                android:theme="@style/Theme.DeviceDefault.Light.Dialog"
                 android:label="@string/choose_account_label"
                 android:process=":ui">
         </activity>
 
         <activity android:name="android.accounts.ChooseAccountTypeActivity"
                 android:excludeFromRecents="true"
-                android:theme="@style/Theme.Material.Light.Dialog"
+                android:theme="@style/Theme.DeviceDefault.Light.Dialog"
                 android:label="@string/choose_account_label"
                 android:process=":ui">
         </activity>
@@ -3213,19 +3217,19 @@
         <activity android:name="android.accounts.CantAddAccountActivity"
                 android:excludeFromRecents="true"
                 android:exported="true"
-                android:theme="@style/Theme.Material.Light.Dialog.NoActionBar"
+                android:theme="@style/Theme.DeviceDefault.Light.Dialog.NoActionBar"
                 android:process=":ui">
         </activity>
 
         <activity android:name="android.accounts.GrantCredentialsPermissionActivity"
                 android:excludeFromRecents="true"
                 android:exported="true"
-                android:theme="@style/Theme.Material.Light.DialogWhenLarge"
+                android:theme="@style/Theme.DeviceDefault.Light.DialogWhenLarge"
                 android:process=":ui">
         </activity>
 
         <activity android:name="android.content.SyncActivityTooManyDeletes"
-               android:theme="@style/Theme.Material.Light.Dialog"
+               android:theme="@style/Theme.DeviceDefault.Light.Dialog"
                android:label="@string/sync_too_many_deletes"
                android:process=":ui">
         </activity>
@@ -3245,7 +3249,7 @@
         </activity>
 
         <activity android:name="com.android.internal.app.NetInitiatedActivity"
-                android:theme="@style/Theme.Material.Light.Dialog.Alert"
+                android:theme="@style/Theme.DeviceDefault.Light.Dialog.Alert"
                 android:excludeFromRecents="true"
                 android:process=":ui">
         </activity>
@@ -3266,7 +3270,7 @@
         <activity android:name="com.android.internal.app.ConfirmUserCreationActivity"
                 android:excludeFromRecents="true"
                 android:process=":ui"
-                android:theme="@style/Theme.Material.Light.Dialog.Alert">
+                android:theme="@style/Theme.DeviceDefault.Light.Dialog.Alert">
             <intent-filter android:priority="1000">
                 <action android:name="android.os.action.CREATE_USER" />
                 <category android:name="android.intent.category.DEFAULT" />
@@ -3274,7 +3278,7 @@
         </activity>
 
         <activity android:name="com.android.internal.app.UnlaunchableAppActivity"
-                android:theme="@style/Theme.Material.Light.Dialog.Alert"
+                android:theme="@style/Theme.DeviceDefault.Light.Dialog.Alert"
                 android:excludeFromRecents="true"
                 android:process=":ui">
         </activity>
diff --git a/core/res/assets/images/android-logo-mask.png b/core/res/assets/images/android-logo-mask.png
index ad40645..5512c0ad 100644
--- a/core/res/assets/images/android-logo-mask.png
+++ b/core/res/assets/images/android-logo-mask.png
Binary files differ
diff --git a/core/res/assets/images/android-logo-shine.png b/core/res/assets/images/android-logo-shine.png
index cb65f22..c5d1263 100644
--- a/core/res/assets/images/android-logo-shine.png
+++ b/core/res/assets/images/android-logo-shine.png
Binary files differ
diff --git a/core/res/assets/images/clock64.png b/core/res/assets/images/clock64.png
deleted file mode 100644
index 493a1ea..0000000
--- a/core/res/assets/images/clock64.png
+++ /dev/null
Binary files differ
diff --git a/core/res/assets/images/clock_font.png b/core/res/assets/images/clock_font.png
new file mode 100644
index 0000000..be927ae
--- /dev/null
+++ b/core/res/assets/images/clock_font.png
Binary files differ
diff --git a/core/res/assets/sounds/bootanim0.raw b/core/res/assets/sounds/bootanim0.raw
deleted file mode 100644
index 46b8c0f..0000000
--- a/core/res/assets/sounds/bootanim0.raw
+++ /dev/null
Binary files differ
diff --git a/core/res/assets/sounds/bootanim1.raw b/core/res/assets/sounds/bootanim1.raw
deleted file mode 100644
index ce69944..0000000
--- a/core/res/assets/sounds/bootanim1.raw
+++ /dev/null
Binary files differ
diff --git a/core/res/assets/webkit/hyph_en_US.dic b/core/res/assets/webkit/hyph_en_US.dic
deleted file mode 100644
index d91204b..0000000
--- a/core/res/assets/webkit/hyph_en_US.dic
+++ /dev/null
@@ -1,9784 +0,0 @@
-ISO8859-1
-LEFTHYPHENMIN 2
-RIGHTHYPHENMIN 3
-.a2ch4
-.ad4der
-.a2d
-.ad1d4
-.a2f1t
-.a2f
-.a4l3t
-.am5at
-.4a1ma
-.an5c
-.a2n
-.2ang4
-.an1i5m
-.an1t4
-.an3te
-.anti5s
-.ant2i
-.a4r5s2
-.2a2r
-.ar4t2ie4
-.ar1ti
-.ar4ty
-.as3c
-.as1p
-.a2s1s
-.aster5
-.a2tom5
-.a1to
-.au1d
-.av4i
-.awn4
-.ba4g
-.ba5na
-.ba2n
-.bas4e
-.ber4
-.be5r1a
-.be3s1m
-.4bes4
-.b4e5s2to
-.bri2
-.but4ti
-.bu4t3t2
-.cam4pe
-.1ca
-.ca4m1p
-.can5c
-.ca2n
-.capa5b
-.ca1pa
-.car5ol
-.c2a2r
-.ca4t
-.ce4la
-.2ch4
-.chill5i
-.ch4il2
-.chil1l
-.1ci2
-.cit5r
-.2c1it
-.co3e2
-.1co
-.co4r
-.cor5n1er
-.corn2e
-.de4moi2
-.d4em
-.de1mo
-.de3o
-.de3r1a
-.de3r1i
-.de1s4c
-.des2
-.dic1t2io5
-.3di2c1t
-.do4t
-.1do
-.du4c
-.1du
-.du4m1b5
-.earth5
-.ear2t
-.e2a2r
-.eas3i
-.2e1b4
-.eer4
-.eg2
-.e2l5d
-.el3em
-.enam3
-.e1na
-.en3g
-.e2n3s2
-.eq5ui5t
-.e1q
-.equ2
-.eq2ui2
-.er4ri
-.er1r4
-.es3
-.4eu3
-.eye5
-.fes3
-.for5mer
-.1fo
-.fo2r
-.for1m
-.for2me
-.1ga2
-.ge2
-.gen3t4
-.1gen
-.ge5o2g
-.1geo
-.1g2i5a
-.gi4b
-.go4r
-.1go
-.hand5i
-.ha2n
-.h4and
-.ha4n5k2
-.he2
-.hero5i2
-.h2ero
-.h1es3
-.he4t3
-.hi3b
-.hi3er
-.h2ie4
-.hon5ey
-.ho2n
-.hon3o
-.hov5
-.id4l
-.2id
-.idol3
-.i1do
-.im3m
-.im5p1i2n
-.i4m1p
-.im2pi
-.in1
-.in3ci
-.2ine2
-.4i4n2k2
-.2i2n3s2
-.ir5r4
-.4ir
-.is4i
-.ju3r
-.la4cy
-.la4m
-.lat5er
-.l4ath5
-.le2
-.leg5e
-.len4
-.lep5
-.lev1
-.l2i4g
-.li1g5a
-.li2n
-.l2i3o
-.l1i4t
-.ma1g5a5
-.1ma
-.mal5o
-.ma1n5a
-.ma2n
-.mar5ti
-.m2a2r
-.me2
-.mer3c
-.me5ter
-.me1te
-.m2is1
-.mis4t5i
-.mon3e
-.1mo
-.mo2n
-.mo3ro
-.mo2r
-.mu5ta
-.1mu
-.mu2ta5b
-.ni4c
-.od2
-.od1d5
-.of5te
-.o2ft
-.or5a1to
-.o1ra
-.or3c
-.or1d
-.or3t
-.os3
-.os4tl
-.4oth3
-.out3
-.ou2
-.ped5al
-.2p2ed
-.p2e2d2a
-.pe5te
-.pe2t
-.pe5tit
-.p2i4e4
-.pio5n4
-.3p2i1o
-.pi2t
-.pre3m
-.pr2
-.ra4c
-.ran4t
-.ra2n
-.ratio5n1a
-.ratio2n4
-.ra1t2io
-.ree2
-.re5mit
-.res2
-.re5stat
-.res2t
-.res1ta
-.r2i4g
-.ri2t5u
-.ro4q
-.ros5t
-.row5d
-.ru4d
-.3s4c2i3e4
-.s1ci
-.5se2l2f5
-.sel1l5
-.se2n
-.se5r2ie4
-.ser1i
-.s2h2
-.si2
-.s3ing4
-.2s1in
-.st4
-.sta5b2l2
-.s1ta
-.s2tab
-.s4y2
-.1ta4
-.te4
-.3ten5a2n
-.te1na
-.th2
-.ti2
-.til4
-.ti1m5o5
-.1tim
-.ting4
-.2t1in
-.t4i4n5k2
-.to1n4a
-.1to
-.to2n
-.to4p
-.top5i
-.to2u5s
-.tou2
-.trib5ut
-.tr4ib
-.u1n1a
-.un3ce
-.under5
-.un1de
-.u2n1e
-.u4n5k2
-.un5o
-.un3u4
-.up3
-.ure3
-.us5a2
-.2us
-.ven4de
-.ve5r1a
-.wil5i
-.wi2
-.wil2
-.ye4
-4ab.
-a5bal
-a5ba2n
-abe2
-ab5erd
-ab2i5a
-ab5i2t5ab
-abi2t
-abi1ta
-ab5lat
-ab2l2
-ab5o5l1iz
-abol2i
-4abr
-ab5rog
-ab3ul
-a4c2a2r
-a1ca
-ac5ard
-ac5aro
-a5ceou2
-ac1er
-a5che4t
-a2ch
-ache2
-4a2ci
-a3c2ie4
-a2c1in
-a3c2io
-ac5rob
-act5if2
-a2c1t
-ac3ul
-ac4um
-a2d
-ad4d1in
-ad1d4
-ad5er.
-2adi
-a3d4i3a
-ad3i1ca
-adi4er
-ad2ie4
-a3d2io
-a3dit
-a5di1u
-ad4le
-ad3ow
-a1do
-ad5ra2n
-a1dr
-ad4su
-a2d1s2
-4a1du
-a3du2c
-ad5um
-ae4r
-aer2i4e4
-aer1i
-a2f
-a4f1f4
-a4gab
-a1ga
-aga4n
-ag5el1l
-a1ge4o
-4ag4eu
-ag1i
-4ag4l2
-ag1n
-a2go
-3a3g4o4g
-ag3o3ni
-ago2n2
-a5guer
-a2gue
-ag5ul
-a4gy
-a3ha
-a3he
-a4h4l4
-a3ho
-ai2
-a5i1a
-a3ic.
-ai5ly
-a4i4n
-ain5in
-a2ini
-a2i1n5o
-ait5en
-a2ite
-a1j
-ak1en
-al5ab
-al3a2d
-a4l2a2r
-4aldi4
-a2ld
-2ale
-al3end
-a4lent2i
-a1len1t
-a5le5o
-al1i
-al4ia.
-al2i1a
-al2i4e4
-al5lev
-al1l
-al2le
-4allic
-all2i
-4a2lm
-a5log.
-a4ly.
-a1ly
-4a2lys4
-5a5lys1t
-5alyt
-3alyz
-4a1ma
-a2m5ab
-am3ag
-ama5ra
-am2a2r
-am5asc
-a4ma3tis
-a4m5a1to
-am5er1a
-am3ic
-am5if
-am5i1ly
-am1in
-am2i4no
-a2mo
-a5mo2n
-amor5i
-amo2r
-amp5en
-a4m1p
-a2n
-an3age
-a1na
-3ana1ly
-a3n2a2r
-an3ar3c
-anar4i
-a3nati
-an2at
-4and
-ande4s2
-an1de
-an3dis1
-an1dl
-an4dow
-an1do
-a5nee
-a3nen
-an5e2st.
-a1nes
-a2nest
-a3n4eu
-2ang
-ang5ie4
-an1gl2
-a4n1ic
-a3nies
-an2ie4
-an3i3f
-an4ime
-an1im
-a5nim1i
-a5n2ine
-an1in
-an3i4o
-a3n2ip
-an3is2h
-an3it
-a3ni1u
-an4kli
-a4nk2
-an1k1l
-5anniz
-a4n1n2
-ano4
-an5ot
-an4oth5
-an2sa2
-a2n1s2
-an4s1co
-ans4c
-an4s1n4
-an2sp
-ans3po
-an4st
-an4su2r
-an1su
-anta2l4
-an1t
-an1ta
-an4t2ie4
-ant2i
-4an1to
-an2tr
-an4tw4
-an3u1a
-an3ul
-a5nur
-4ao
-ap2a2r4
-a1pa
-ap5at
-ap5er3o
-a3ph4er
-4aphi
-a4pilla
-apil1l
-ap5ill2a2r
-ap3i2n
-ap3i1ta
-a3pi2tu
-a2p2l2
-apo4c5
-ap5o1la
-apor5i
-a1p4or
-apos3t
-a1pos
-aps5e4s
-a2p1s2
-ap2se
-a3pu
-aque5
-aqu2
-2a2r
-ar3a2c1t
-a5rade
-ara2d
-ar5adis1
-ar2adi
-ar3al
-a5rame1te
-aram3et
-ar2an4g
-ara2n
-ara3p
-ar4at
-a5ra1t2io
-ar5a1t2iv
-a5rau
-ar5av4
-araw4
-arbal4
-ar1b
-ar4cha2n
-ar1c
-ar3cha
-ar2ch
-ar5d2ine
-ard2i
-ard1in4
-ar4dr
-ar5eas
-a3ree
-ar3en1t
-a5r2e2ss
-ar4fi
-ar1f
-ar4f4l2
-ar1i
-ar5i2al
-ar2i3a
-ar3i2a2n
-a3ri5et
-ar2ie4
-ar4im
-ar5in2at
-ar2i1na
-ar3i1o
-ar2iz
-ar2mi
-ar1m
-ar5o5d
-a5roni
-aro2n
-a3roo2
-ar2p
-ar3q
-arre4
-ar1r4
-ar4sa2
-a4rs2
-ar2s2h
-4as.
-a2s4ab
-asa2
-as3an1t
-asa2n
-ashi4
-as2h
-a5sia.
-as2i1a
-a3si1b
-a3sic
-5a5si4t
-ask3i
-ask2
-as4l2
-a4soc
-a1so
-as5ph
-as4s2h
-a2ss
-as3ten
-as1t4r
-asu1r5a
-a1su
-asu2r
-a2ta
-at3ab2l2
-a2tab
-at5ac
-at3alo
-ata2l
-at5ap
-ate5c
-at5e2ch
-at3e1go
-ateg4
-at3en.
-at3er1a
-ater5n
-a5ter1na
-at3est
-at5ev
-4ath
-ath5em
-ath2e
-a5the2n
-at4ho
-ath5om
-4ati.
-a5t2i1a
-a2t5i5b
-at1ic
-at3if2
-ation5a2r
-a1t2io
-atio2n
-atio1n1a
-at3i1tu
-a4tog
-a1to
-a2tom
-at5om2iz
-a4top
-a4tos2
-a1tr
-at5rop
-at4sk2
-a4t1s2
-at4tag
-a4t3t2
-at1ta
-at5te
-at4th
-a2tu
-at5u1a
-a4t5ue
-at3ul
-at3u1ra
-a2ty
-au4b
-augh3
-au3gu
-au4l2
-aun5d
-au3r
-au5si1b
-a2us
-a4ut5en
-au1th
-a2va
-av3ag4
-a5va2n
-av4e4no
-av3er1a
-av5ern
-av5ery
-av1i
-avi4er
-av2ie4
-av3ig
-av5oc
-a1vor
-3away
-aw3i2
-aw4ly
-aws4
-ax4i5c
-ax3i
-ax4id
-ay5al
-aye4
-ays4
-azi4er
-a2z1i
-az2ie4
-az2z5i
-a4z1z2
-5ba.
-bad5ger
-ba2d
-ba4ge
-bal1a
-ban5dag
-ba2n
-b4and
-ban1d2a
-ban4e
-ban3i
-barbi5
-b2a2r
-bar1b
-bar2i4a
-bar1i
-bas4si
-ba2ss
-1bat
-ba4z
-2b1b
-b2be
-b3ber
-bbi4na
-4b1d
-4be.
-beak4
-bea2t3
-4be2d
-b2e3d2a
-be3de
-b4e3di
-be3gi
-be5gu
-1bel
-be1l2i
-be3lo
-4be5m
-be5n2ig
-be5nu
-4bes4
-be3sp
-b2e5st4r
-3bet
-be1t5iz
-be5tr
-be3tw4
-be3w
-be5y1o4
-2bf
-4b3h
-bi2b
-b2i4d
-3b2ie4
-bi5en
-bi4er
-2b3if
-1bil
-bi3l2iz
-bil1i
-bin2a5r4
-bi1na
-b4in4d
-bi5net
-b2ine
-bi3o2gr
-b2io
-bi5ou2
-bi2t
-3b2i3t2io
-bi1ti
-bi3tr
-3bit5u1a
-bi1tu
-b5i4tz
-b1j
-bk4
-b2l2
-bl4ath5
-b4le.
-blen4
-5ble1sp
-bles2
-b3lis
-b4lo
-blun4t
-4b1m
-4b3n
-bne5g
-3bod
-bod3i
-bo4e
-bol3ic
-bol2i
-bom4bi
-bo4m1b
-bo1n4a
-bo2n
-bon5at
-3boo2
-5bor.
-4b1o1ra
-bor5d
-5bore
-5bori
-5bos4
-b5o1ta
-b4oth5
-bo4to
-boun2d3
-bou2
-4bp
-4brit
-br4oth3
-2b5s2
-bsor4
-b1so
-2bt
-b2t4l
-b4to
-b3tr
-buf4fer1
-bu4f1f
-bu4ga
-bu3l2i
-bu1mi4
-bu4n
-bunt4i
-bun1t
-bu3re
-bus5ie4
-b2us
-buss4e
-bu2ss
-5bust
-4bu1ta
-3bu1t2io
-b4u1t2i
-b5u1to
-b1v
-4b5w
-5by.
-bys4
-1ca
-cab3in
-ca1b2l2
-ca2ch4
-ca5den
-ca2d
-4cag4
-2c5ah
-ca3lat
-cal4la
-cal1l
-cal2l5in4
-call2i
-4calo
-c4an5d
-ca2n
-can4e
-ca4n4ic
-can5is
-can3iz
-can4ty
-can1t
-cany4
-ca5per
-car5om
-c2a2r
-cast5er
-cas5t2ig
-cast2i
-4cas4y
-c4a4th
-4ca1t2iv
-cav5al
-ca2va
-c3c
-ccha5
-c2ch
-c3c2i4a
-c1ci
-ccom1pa5
-c1co
-cco4m1p
-cco2n4
-ccou3t
-ccou2
-2ce.
-4ced.
-4ce1den
-3cei2
-5cel.
-3cel1l
-1cen
-3cenc
-2cen4e
-4ceni
-3cen1t
-3cep
-ce5ram
-cer1a
-4ce1s4a2
-3ces1si
-c2e2ss
-ces5si5b
-ces5t
-cet4
-c5e4ta
-cew4
-2ch
-4ch.
-4ch3ab
-5cha4n1ic
-cha2n
-ch5a5nis
-che2
-cheap3
-4ch4ed
-ch5e5lo
-3chemi
-ch5ene
-che2n
-ch3er.
-ch3e4r1s2
-4ch1in
-5chi2ne.
-ch2ine
-ch5i5n2e2ss
-chi1nes
-5ch2ini
-5ch2io
-3chit
-chi2z
-3cho2
-ch4ti
-1ci
-3c2i1a
-ci2a5b
-ci2a5r
-ci5c
-4cier
-c2ie4
-5c4i2f3ic.
-ci1fi
-4c4i5i4
-ci4la
-3cil1i
-2cim
-2cin
-c4i1na
-3cin2at
-cin3em
-c2ine
-c1ing
-c5ing.
-5c2i1no
-cio2n4
-c2io
-4cipe4
-c2ip
-ci3ph
-4cip4ic
-cip3i
-4cis1ta
-4cis1t2i
-2c1it
-ci1t3iz
-ci1ti
-5ciz
-ck1
-ck3i
-1c4l4
-4cl2a2r
-c5la5ra1t2io
-clar4at
-5clare
-cle4m
-4clic
-clim4
-c1ly4
-c5n
-1co
-co5ag
-c4oa
-coe2
-2cog
-co4gr
-coi4
-co3inc
-col5i
-5colo
-col3o4r
-com5er
-co2me
-co1n4a
-co2n
-c4one
-con3g
-con5t
-co3pa
-cop3ic
-co4p2l2
-4cor1b
-coro3n
-cos4e
-cov1
-cove4
-cow5a
-co2z5e
-co5z1i
-c1q
-cras5t
-cr2as
-5crat.
-5crat1ic
-cre3a2t
-5c2r2ed
-4c3re1ta
-cre4v2
-cri2
-cri5f
-c4rin
-cr2is4
-5cri1ti
-cro4p2l2
-crop5o
-cros4e
-cru4d
-4c3s2
-2c1t
-c2ta4b
-c1ta
-ct5ang
-cta2n
-c5tan1t
-c2te
-c3ter
-c4t4ic1u
-ctim3i
-c1tim
-ctu4r
-c1tu
-c4tw4
-cud5
-c4uf
-c4ui2
-cu5i1ty
-5cul2i
-cul4tis4
-cul1ti
-cu4lt
-3c4ul1tu2
-cu2ma
-c3ume
-cu4mi
-3cun
-cu3pi
-cu5py
-cu2r5a4b
-cu1ra
-cu5r2i3a
-1c2us
-cus1s4i
-cu2ss
-3c4ut
-cu4t2ie4
-c4u1t2i
-4c5u1t2iv
-4cutr
-1cy
-c2ze4
-1d2a
-5da.
-2d3a4b
-da2ch4
-4da2f
-2dag
-da2m2
-d2an3g
-da2n
-dard5
-d2a2r
-dark5
-4dary
-3dat
-4da1t2iv
-4da1to
-5dav4
-dav5e
-5day
-d1b
-d5c
-d1d4
-2de.
-dea2f5
-de4b5i2t
-d2e1b
-de4bo2n
-deca2n4
-de1ca
-de4cil
-de1c2i
-de5com
-de1co
-2d1ed
-4dee.
-de5if
-dei2
-del2i4e4
-del2i
-de4l5i5q
-de5lo
-d4em
-5dem.
-3demic
-dem5ic.
-de5mil
-de4mo2n3s2
-de1mo
-demo2n
-demo2r5
-1den
-de4n2a2r
-de1na
-d4e3no
-denti5f2
-den1t
-dent2i
-de3nu
-de1p
-de3pa
-depi4
-de2pu
-d3e1q
-d4er1h4
-5der3m4
-d5ern5iz
-de4r5s2
-des2
-d2es.
-de1s2c
-de2s5o
-des3t2i
-d2e3st4r
-de4su
-de1t
-de2to
-de1v
-de2v3i4l
-de1vi
-4dey
-4d1f
-d4ga
-d3ge4t
-dg1i
-d2gy
-d1h2
-5di.
-1d4i3a
-dia5b
-d4i4cam
-di1ca
-d4ice
-3di2c1t
-3d2id
-5di3en
-d2ie4
-d1if
-di3ge
-d2ig
-di4la1to
-di1la
-d1in
-1di1na
-3di2ne.
-d2ine
-5d2ini
-di5niz
-1d2io
-dio5g
-di4p2l2
-d2ip
-d4ir2
-di1re
-dir1t5i
-dis1
-5disi
-d4is3t
-d2i1ti
-1d2i1v
-d1j
-d5k2
-4d5la
-3dle.
-3dled
-3dles.
-dles2
-4d3l2e2ss
-2d3lo
-4d5lu
-2d1ly
-d1m
-4d1n4
-1do
-3do.
-do5de
-5doe
-2d5of
-d4og
-do4la
-dol2i4
-do5lo4r
-dom5iz
-do3n2at
-do2n
-do1n1a
-doni4
-doo3d
-doo2
-do4p4p
-d4or
-3dos
-4d5out
-dou2
-do4v
-3dox
-d1p
-1dr
-drag5o2n2
-dra2go
-4dr2ai2
-dre4
-dre2a5r
-5dren
-dr4i4b
-dril4
-dro4p
-4drow
-5drupli
-dru3p2l2
-4dry
-2d1s2
-ds4p
-d4sw2
-d4s4y
-d2th
-1du
-d1u1a
-du2c
-d1u3ca
-duc5er
-4duct.
-du2c1t
-4duc4t1s2
-du5el
-du4g
-d3ul4e
-dum4be
-du4m1b
-du4n
-4dup
-du4pe
-d1v
-d1w
-d2y
-5dyn
-dy4s2e
-dys5p
-e1a4b
-e3a2c1t
-ea2d1
-ead5ie4
-e2adi
-ea4ge
-ea5ger
-ea4l
-eal5er
-e2ale
-eal3ou2
-eam3er
-e5and
-ea2n
-ear3a
-e2a2r
-ear4c
-ear5es
-ear4ic
-ear1i
-ear4il
-ear5k
-ear2t
-eart3e
-ea5sp
-e3a2ss
-east3
-ea2t
-eat5en
-eath3i
-e4ath
-e5at3if2
-e4a3tu
-ea2v
-eav3en
-eav5i
-eav5o
-2e1b
-e4bel.
-e1bel
-e4be2l1s2
-e4ben
-e4bi2t
-e3br
-e4ca2d
-e1ca
-ecan5c
-eca2n
-ec1ca5
-ec3c
-e1ce
-ec5es1sa2
-ec2e2ss
-e1c2i
-e4cib
-ec5ificat
-eci1fi
-ecifi1ca
-ec5i3f2ie4
-ec5i1fy
-e2c3im
-e2c1i4t
-e5c2ite
-e4clam
-e1c4l4
-e4cl2us
-e2col
-e1co
-e4com1m
-e4compe
-eco4m1p
-e4con1c
-eco2n
-e2cor
-ec3o1ra
-eco5ro
-e1cr
-e4crem
-ec4ta2n
-e2c1t
-ec1ta
-ec4te
-e1cu
-e4cul
-ec3u1la
-2e2d2a
-4ed3d4
-e4d1er
-ede4s2
-4edi
-e3d4i3a
-ed3ib
-ed3i1ca
-ed3im
-ed1it
-edi5z
-4e1do
-e4dol
-edo2n2
-e4dri
-e1dr
-e4dul
-e1du
-ed5u1l4o
-ee2c
-e4ed3i
-ee2f
-eel3i
-ee4ly
-ee2m
-ee4na
-ee4p1
-ee2s4
-eest4
-ee4ty
-e5ex
-e1f
-e4f3ere
-efer1
-1e4f1f
-e4fic
-e1fi
-5ef2i1c4i
-efil4
-e3f2i2ne
-e2fin
-ef5i5n2ite
-ef2ini
-efin2it
-3efit
-efor5es
-e1fo
-efo2r
-e4fu4se.
-e3fu
-ef2us
-4egal
-e1ga
-eger4
-eg5ib
-eg4ic
-eg5ing
-e5git5
-eg5n
-e4go.
-e1go
-e4gos
-eg1ul
-e5gur
-5e1gy
-e1h4
-eher4
-ei2
-e5ic
-e2i5d
-e2ig2
-ei5g4l2
-e3i4m1b
-e3in3f
-e1ing
-e5inst
-e2i2n1s2
-eir4d
-e4ir
-e2it3e
-e2i3th
-e5i1ty
-e1j
-e4jud
-ej5udi
-eki4n
-ek1i
-ek4la
-ek1l
-e1la
-e4la.
-e4lac
-e3l4an4d
-ela2n
-e4l5a1t2iv
-e4law
-elax1a4
-e3le2a
-el5ebra
-el2e1b
-ele3br
-5elec
-e4led
-el3e1ga
-e5len
-e4l1er
-e1les2
-e2l2f
-el2i
-e3libe4
-e4l5ic.
-el3i1ca
-e3lier
-el2ie4
-el5i3gib
-el2ig
-el4igi
-e5lim
-e4l3ing
-e3l2io
-e2lis
-el5is2h
-e3l2iv3
-4ella
-el1l
-el4lab
-ell4o4
-e5loc
-el5og
-el3op.
-el2s2h
-e2l1s2
-el4ta
-e4lt
-e5lud
-el5ug
-e4mac
-e1ma
-e4mag
-e5ma2n
-em5a1na
-e4m5b
-e1me
-e2mel
-e4met
-em3i1ca
-em2i4e4
-em5igra
-em2ig4
-emi1gr
-em1in2
-em5ine
-em3i3ni
-e4m2is
-em5is2h
-e5m4i2s1s
-em3iz
-5emniz
-e4m1n
-emo4g
-e1mo
-emo3n2i5o
-emo2n
-em3pi
-e4m1p
-e4mul
-e1mu
-em5u1la
-emu3n2
-e3my
-en5a2mo
-e1na
-e4nan1t
-en2a2n
-ench4er
-en2ch
-enche2
-en3dic
-e5nea
-e5nee
-en3em
-en5ero
-en1er
-en5e1si
-e1nes
-e2n5est
-en3etr
-e3ne4w
-en5i4c3s2
-e5n2ie4
-e5nil
-e3n2i4o
-en3is2h
-en3it
-e5ni1u
-5eniz
-4e4n1n2
-4eno
-e4no4g
-e4nos
-en3ov
-en4sw2
-e2n1s2
-ent5age
-en1t
-en1ta
-4enth1es
-enth2e
-en3u1a
-en5uf
-e3ny.
-4e4n3z
-e5of
-eo2g
-e4oi4
-e3ol
-eop3a2r
-eo2pa
-e1or
-eo3re
-eo5rol
-eos4
-e4ot
-eo4to
-e5out
-eou2
-e5ow
-e2pa
-e3p4ai2
-ep5anc
-epa2n
-e5pel
-e3pen1t
-ep5e5t2i1t2io
-epe2t
-epeti1ti
-ephe4
-e4pli
-e1p2l2
-e1po
-e4prec
-epr2
-ep5re1ca
-e4p2r2ed
-ep3re1h4
-e3pro
-e4prob
-ep4s4h
-e2p1s2
-ep5ti5b
-e2p1t
-e4pu2t
-ep5u1ta
-e1q
-equi3l
-equ2
-eq2ui2
-e4q3ui3s
-er1a
-e2ra4b
-4er4and
-era2n
-er3a2r
-4er4ati.
-2er1b
-er4b2l2
-er3ch
-er1c
-er4che2
-2e2re.
-e3re1a4l
-ere5co
-ere3in
-erei2
-er5el.
-er3e1mo
-er5e1na
-er5ence
-4erene
-er3en1t
-ere4q
-er5e2ss
-er3es2t
-eret4
-er1h4
-er1i
-e1r2i3a4
-5erick1
-e3rien
-er2ie4
-eri4er
-er3in4e
-e1r2i1o
-4erit
-er4i1u
-er2i4v
-e4ri1va
-er3m4
-er4nis4
-4er3n2it
-5erniz
-er3no4
-2ero
-er5ob
-e5r2oc
-ero4r
-er1ou2
-e4r1s2
-er3set
-er2se
-ert3er
-4er2tl
-er3tw4
-4eru
-eru4t
-5erwau
-er1w
-e1s4a2
-e4sa2ge.
-e4sages
-es2c
-e2s1ca
-es5ca2n
-e3scr
-es5cu
-e1s2e
-e2sec
-es5e1cr
-e4s5enc
-e4sert.
-e4ser4t1s2
-e4ser1va
-4es2h
-e3sha
-esh5e2n
-e1si
-e2sic
-e2s2id
-es5i1den
-e4s5ig1n4a
-es2ig
-e2s5im
-e2s4i4n
-esis4te
-e1sis
-e5si4u
-e5skin
-esk2
-esk1i
-es4mi
-e2s1m
-e2sol
-e1so
-es3olu
-e2so2n
-es5o1n1a4
-e1sp
-e2s3per
-es5pi1ra
-esp4ir
-es4pre
-espr2
-2e2ss
-es4si4b
-es1si
-esta2n4
-es1ta
-es3t2ig
-est2i
-es5tim
-4es2to
-e3sto2n
-2est4r
-e5stro
-estruc5
-e2su2r
-e1su
-es5ur1r4
-es4w2
-e2ta4b
-e1ta
-e3ten4d
-e3teo
-ethod3
-et1ic
-e5tide
-et2id
-e2t1in4
-et2i4no
-e5t4ir
-e5t2i1t2io
-eti1ti
-et5i1t2iv
-4e2t1n2
-et5o1n1a
-e1to
-eto2n
-e3tra
-e3tre
-et3ric
-et5rif
-et3rog
-et5ros
-et3u1a
-e1tu
-et5ym
-e1ty
-e4t5z
-4eu
-e5un
-e3up
-eu3ro
-e2us4
-eute4
-euti5l
-e4u1t2i
-eu5tr
-eva2p5
-e1va
-e2vas
-ev5ast
-e5vea
-ev3el1l
-eve4l3o
-e5veng
-even4i
-ev1er
-e5v2er1b
-e1vi
-ev3id
-e2vi4l
-e4v1in
-e3v2i4v
-e5voc
-e5vu
-e1wa
-e4wag
-e5wee
-e3wh
-ewil5
-ewi2
-ew3in4g
-e3wit
-1ex3p
-5ey1c
-5eye.
-eys4
-1fa
-fa3b2l2
-f4ab3r
-fa4ce
-4fag
-fa4i4n4
-fai2
-fal2l5e
-fal1l
-4f4a4ma
-fam5is
-5f2a2r
-far5th
-fa3ta
-fa3th2e
-f4ath
-4fa1to
-fau4lt5
-fau4l2
-4f5b
-4fd
-4fe.
-feas4
-fe4ath3
-fea2t
-f2e4b
-4fe1ca
-5fe2c1t
-2fed
-fe3l2i
-fe4mo
-fen2d
-fen1d5e
-fer1
-5fer1r4
-fev4
-4f1f
-f4fes
-f4f2ie4
-f1fi
-f5f2in.
-f2fin
-f2f5is
-f4f2ly5
-ff4l2
-f2fy
-4fh
-1fi
-f2i3a
-2f3ic.
-4f3ical
-fi1ca
-f3ica2n
-4ficate
-f3i1cen
-fi3cer
-f2i1c4i
-5fi3c2i1a
-5fic2ie4
-4fi4c3s2
-fi3cu
-fi5del
-f2id
-fight5
-f2ig
-fil5i
-fil2l5in4
-fil1l
-fill2i
-4fi1ly
-2fin
-5fi1na
-f4in2d5
-f2i2ne
-f1in3g
-f2i4n4n2
-fis4t2i
-f4l2
-f5l2e2ss
-fles2
-flin4
-flo3re
-f2ly5
-4fm
-4fn
-1fo
-5fo2n
-fon4de
-f2ond
-fon4t
-fo2r
-fo5rat
-fo1ra
-for5ay
-fore5t
-for4i
-for1t5a
-fos5
-4f5p
-fra4t
-f5rea
-fres5c
-fri2
-fril4
-frol5
-2f3s
-2ft
-f4to
-f2ty
-3fu
-fu5el
-4fug
-fu4min
-fu1mi
-fu5ne
-fu3ri
-fusi4
-f2us
-fu2s4s
-4fu1ta
-1fy
-1ga
-ga2f4
-5gal.
-3gal1i
-ga3lo
-2gam
-ga5met
-g5a2mo
-gan5is
-ga2n
-ga3niz
-gani5za1
-4gano4
-gar5n4
-g2a2r
-ga2ss4
-g4ath3
-4ga1t2iv
-4gaz
-g3b
-gd4
-2ge.
-2ged
-geez4
-gel4in
-gel2i
-ge5lis
-ge5l1iz
-4ge1ly
-1gen
-ge4n2at
-ge1na
-g5e5niz
-4g4eno
-4geny
-1geo
-ge3om
-g4ery
-5ge1si
-geth5
-4ge1to
-ge4ty
-ge4v
-4g1g2
-g2ge
-g3ger
-gglu5
-ggl2
-g1go4
-gh3in
-gh5out
-ghou2
-gh4to
-5gi.
-1g2i4a
-gi2a5r
-g1ic
-5gi3c2i1a
-g2i1ci
-g4i1co
-gien5
-g2ie4
-5gies.
-gil4
-g3i1men
-3g4in.
-g4in5ge
-5g4i2n1s2
-5g2io
-3g4ir
-gir4l
-g3is1l2
-gi4u
-5g2iv
-3giz
-gl2
-gla4
-gl2ad5i
-gla2d
-5glas
-1gle
-gli4b
-g3l2ig
-3glo
-glo3r
-g1m
-g4my
-g1n4a
-g4na.
-gne4t4t2
-g1ni
-g2n1in
-g4n2i4o
-g1no
-g4no4n
-1go
-3go.
-gob5
-5goe
-3g4o4g
-go3is
-goi2
-go2n2
-4g3o3n1a
-gon5do5
-g2ond
-go3ni
-5goo2
-go5riz
-gor5ou2
-5gos.
-gov1
-g3p
-1gr
-4gra1d2a
-gra2d
-g4r2ai2
-gra2n2
-5gra4ph.
-g5ra3ph4er
-5graph1ic
-gr4aphi
-4g3ra1phy
-4gray
-gre4n
-4gress.
-gr2e2ss
-4grit
-g4ro
-gruf4
-gs2
-g5ste
-gth3
-gu4a
-3guar2d
-gu2a2r
-2gue
-5gui5t
-g2ui2
-3gun
-3g2us
-4gu4t
-g3w
-1gy
-2g5y3n
-gy5ra
-h3ab4l2
-ha2ch4
-hae4m
-hae4t
-h5agu
-ha3la
-hala3m
-ha4m
-han4ci
-ha2n
-han4cy
-5hand.
-h4and
-h2an4g
-hang5er
-han1g5o
-h5a5niz
-ha4n4k2
-han4te
-han1t
-ha2p3l2
-ha2p5t
-ha3ra2n
-h2a2r
-ha5r2as
-har2d
-hard3e
-har4le4
-har1l
-harp5en
-har2p
-har5ter
-ha2s5s
-haun4
-5haz
-haz3a1
-h1b
-1hea2d1
-3he2a2r
-he4ca2n
-he1ca
-h5ecat
-h4ed
-h4e5do5
-he3l4i
-hel4lis
-hel1l
-hell2i
-hel4ly
-h5elo
-he4m4p
-he2n
-he1na4
-hen5at
-he1o5r
-hep5
-h4er1a
-hera3p
-her4ba
-h2er1b
-here5a
-h3ern
-h5er1ou2
-h2ero
-h3ery
-h1es
-he2s5p
-he4t
-he2t4ed
-h4eu4
-h1f
-h1h
-hi5a2n
-h2i1a
-hi4co
-high5
-h2ig
-h4il2
-himer4
-h4i1na
-hion4e
-h2io
-hio2n
-h2i4p
-hir4l
-h4ir
-hi3ro
-hir4p
-hir4r4
-his3el
-h4ise
-h4i2s4s
-hith5er
-h2ith
-hith2e
-h2i2v
-4hk
-4h1l4
-hla2n4
-h2lo
-hlo3ri
-4h1m
-hmet4
-2h1n
-h5odiz
-h5o2d1s2
-ho4g
-ho1ge4
-hol5a2r
-ho1la
-3hol4e
-ho4ma
-ho2me3
-ho1n4a
-ho2n
-ho5ny
-3hood
-hoo2
-hoo2n4
-hor5at
-ho1ra
-ho5r2is
-hort3e
-ho5ru
-hos4e
-ho5sen
-hos1p
-1ho2us
-hou2
-house3
-hov5el
-4h5p
-4hr4
-hree5
-hro5niz
-hro2n
-hro3po
-4h1s2
-h4s2h
-h4t2a2r
-h1ta
-ht1en
-ht5es
-h4ty
-hu4g
-hu4min
-hu1mi
-hun5ke
-hu4nk2
-hun4t
-hus3t4
-h2us
-hu4t
-h1w
-h4war4t
-hw2a2r
-hy3pe
-hy3ph
-hy2s
-2i1a
-i2al
-iam4
-iam5e1te
-i2a2n
-4ianc
-ian3i
-4ian4t
-ia5pe
-ia2ss4
-i4a1t2iv
-ia4tric
-ia1tr
-i4a2tu
-ibe4
-ib3er1a
-ib5ert
-ib5i1a
-ib3in
-ib5it.
-ibi2t
-ib5ite
-i1b2l2
-ib3li
-i5bo
-i1br
-i2b5ri
-i5bu4n
-4icam
-i1ca
-5icap
-4ic2a2r
-i4car.
-i4cara
-icas5
-i4cay
-iccu4
-ic3c
-4iceo
-4i2ch
-2i1ci
-i5c2id
-ic5i1na
-i2cin
-i2c2ip
-ic3i1pa
-i4c1ly4
-i1c4l4
-i2c5oc
-i1co
-4i1cr
-5icra
-i4cry
-ic4te
-i2c1t
-ic1tu2
-ic4t3u1a
-ic3u1la
-ic4um
-ic5uo
-i3cur
-2id
-i4dai2
-i1d2a
-id5anc
-ida2n
-id5d4
-ide3a4l
-ide4s2
-i2di
-id5i2a2n
-i1d4i3a
-idi4a2r
-i5d2ie4
-i1d3io
-idi5ou2
-id1it
-id5i1u
-i3dle
-i4dom
-i1do
-id3ow
-i4dr
-i2du
-id5uo
-2ie4
-ied4e
-5ie5ga
-ie2ld3
-ie1n5a4
-ien4e
-i5e4n1n2
-i3ent2i
-ien1t
-i1er.
-i3es2c
-i1est
-i3et
-4if.
-if5ero
-ifer1
-iff5en
-i4f1f
-if4fr
-4i2f3ic.
-i1fi
-i3f2ie4
-i3f4l2
-4i2ft
-2ig
-iga5b
-i1ga
-ig3er1a
-ight3i
-4igi
-i3gib
-ig3il4
-ig3in
-ig3it
-i4g4l2
-i2go
-ig3or
-ig5ot
-i5gre
-i1gr
-ig2u5i2
-ig1ur
-i3h
-4i5i4
-i3j
-4ik
-i1la
-il3a4b
-i4l4ade
-ila2d
-i2l5am
-ila5ra
-il2a2r
-i3leg
-il1er
-ilev4
-i2l5f
-il1i
-il3i1a
-il2ib
-il3io
-il4ist
-2il1it
-il2iz
-ill5ab
-il1l
-4i2l1n2
-il3o1q
-il4ty
-i4lt
-il5ur
-il3v
-i4mag
-i1ma
-im3age
-ima5ry
-im2a2r
-iment2a5r
-i1men
-i3men1t
-imen1ta
-4imet
-im1i
-im5i1d4a
-im2id
-imi5le
-i5m2ini
-4imit
-im4ni
-i4m1n
-i3mo2n
-i1mo
-i2mu
-im3u1la
-2in.
-i4n3au
-i1na
-4inav
-incel4
-in3cer
-4ind
-in5dling
-2ine
-i3nee
-in4er4a2r
-in1er
-iner1a
-i5n2e2ss
-i1nes
-4in1ga
-4inge
-in5gen
-4ingi
-in5gling
-ingl2
-4in1go
-4in1gu
-2ini
-i5ni.
-i4n4i1a
-in3i4o
-in1is
-i5ni4te.
-in2it
-in2ite
-5i3n2i1t2io
-ini1ti
-in3i1ty
-4i4nk2
-4i4n1l
-2i4n1n2
-2i1no
-i4no4c
-ino4s
-i4not
-2i2n1s2
-in3se
-insu1r5a
-in1su
-insu2r
-2int.
-in1t
-2in4th
-in1u
-i5n2us
-4iny
-2io
-4io.
-io1ge4
-io2gr
-i1ol
-io4m
-ion3at
-io2n
-io1n1a
-ion4ery
-ion1er
-ion3i
-i2o5ph
-ior3i
-i4os
-i4o5th
-i5oti
-io4to
-i4our
-iou2
-2ip
-ipe4
-iphr2as4
-ip4hr4
-ip3i
-ip4ic
-ip4re4
-ipr2
-ip3ul
-i3qua
-iqu2
-iq5ue1f
-iq3u2id
-iq2ui2
-iq3ui3t
-4ir
-i1ra
-i2ra4b
-i4rac
-ird5e
-ire4de
-i2r2ed
-i4re1f
-i4rel4
-i4res
-ir5gi
-irg2
-ir1i
-iri5de
-ir2id
-ir4is
-iri3tu
-5i5r2iz
-ir4min
-ir1m
-iro4g
-5iron.
-iro2n
-ir5ul
-2is.
-is5ag
-isa2
-is3a2r
-isas5
-2is1c
-is3ch2
-4ise
-is3er
-3i4s3f
-is5ha2n
-is2h
-is3ho2n3
-isho4
-ish5op
-is3i1b
-is2i4d
-i5sis
-is5i1t2iv
-isi1ti
-4is4k2
-isla2n4
-is1l2
-4is4m1s2
-i2s1m
-i2so
-iso5mer
-i3som
-iso2me
-is1p
-is2pi
-is4py
-4i2s1s
-is4sal
-is1sa2
-issen4
-is4s1e4s
-is4ta.
-is1ta
-is1te
-is1t2i
-ist4ly
-is2tl
-4istral
-ist4r
-is1tra
-i2su
-is5us
-4i3ta.
-i1ta
-ita4bi
-i2tab
-i4tag
-4ita5m
-i3ta2n
-i3tat
-2ite
-it3er1a
-i5ter1i
-it4es
-2ith
-i1ti
-4i1t2i1a
-4i2tic
-it3i1ca
-5i5tick1
-i2t3ig
-it5il1l
-i2tim
-2i1t2io
-4itis
-i4ti2s4m
-i2t5o5m
-i1to
-4ito2n
-i4tram
-i1tra
-it5ry
-4i4t3t2
-it3u1at
-i1tu
-itu1a
-i5tud2
-it3ul
-4itz.
-i4tz
-i1u
-2iv
-iv3el1l
-iv3en.
-i4v3er.
-i4vers.
-ive4r1s2
-iv5il.
-i2vil
-iv5io
-iv1it
-i5vore
-iv3o3ro
-i4v3ot
-4i5w
-ix4o
-4iy
-4iz2a2r2
-iza1
-i2z1i4
-5izon1t
-i1zo
-izo2n
-5ja
-jac4q
-ja4p
-1je
-je4r5s2
-4jes4t2ie4
-jest2i
-4jes2ty
-jew3
-jo4p
-5judg
-3ka.
-k3ab
-k5ag
-kais4
-kai2
-kal4
-k1b
-k2ed
-1kee
-ke4g
-ke5l2i
-k3en4d
-k1er
-kes4
-k3e2st.
-ke4ty
-k3f
-kh4
-k1i
-5ki.
-5k2ic
-k4il1l
-kilo5
-k4im
-k4in.
-kin4de
-k4ind
-k5i5n2e2ss
-k2ine
-ki1nes
-kin4g
-k2i4p
-kis4
-k5is2h
-kk4
-k1l
-4k3ley
-4k1ly
-k1m
-k5nes
-1k2no
-ko5r
-kos2h4
-k3ou2
-kro5n
-4k1s2
-k4sc
-ks4l2
-k4s4y
-k5t
-k1w
-lab3ic
-l4abo
-l4a2ci4
-l4ade
-la2d
-la3d2y
-lag4n
-la2m3o
-3l4and
-la2n
-lan4dl
-lan5et
-lan4te
-lan1t
-lar4g2
-l2a2r
-lar3i
-las4e
-la5ta2n
-la2ta
-4latel2i4
-4la1t2iv
-4lav
-la4v4a
-2l1b
-lbin4
-4l1c2
-lce4
-l3ci
-2ld
-l2de
-ld4ere
-ld4er1i
-ldi4
-ld5is1
-l3dr
-l4dri
-le2a
-le4bi
-l2e1b
-le2ft5
-le1f
-5leg.
-5le4g1g2
-le4mat
-le1ma
-lem5at1ic
-4len.
-3lenc
-5le2ne.
-1len1t
-le3ph
-le4pr2
-le2ra5b
-ler1a
-ler4e
-3lerg2
-3l4er1i
-l4ero
-les2
-le5s1co
-les2c
-5lesq
-3l2e2ss
-5less.
-l3e1va
-lev4er.
-lev1er
-lev4er1a
-lev4e4r1s2
-3ley
-4leye
-2lf
-l5fr
-4l1g4
-l5ga
-lg2a2r3
-l4ges
-l1go3
-2l3h
-li4ag
-l2i1a
-li2am4
-liar5iz
-li2a2r
-liar1i
-li4as
-li4a1to
-li5bi
-5lic2io
-l2i1ci
-li4cor
-li1co
-4li4c3s2
-4lict.
-li2c1t
-l4icu
-l3i1cy
-l3i1d2a
-l2id
-lid5er
-3li2di
-lif3er1
-l4i4f1f
-li4f4l2
-5ligate
-l2ig
-li1ga
-3ligh
-li4gra
-li1gr
-3l4ik
-4l4i4l
-lim4b2l2
-li4m1b
-lim3i
-li4mo
-l4i4m4p
-l4i1na
-1l4ine
-lin3ea
-l2in3i
-link5er
-l4i4nk2
-li5og
-l2io
-4l4iq
-lis4p
-l1it
-l2it.
-5lit3i1ca
-li1ti
-l4i2tic
-l5i5ti4c3s2
-liv3er
-l2iv
-l1iz
-4lj
-lka3
-l3kal4
-lka4t
-l1l
-l4law
-l2le
-l5le2a
-l3lec
-l3leg
-l3lel
-l3le4n
-l3le4t
-ll2i
-l2lin4
-l5l4i1na
-ll4o
-lloq2ui5
-llo1q
-lloqu2
-l2l5out
-llou2
-l5low
-2lm
-l5met
-lm3ing
-l4mo2d1
-l1mo
-lmo2n4
-2l1n2
-3lo.
-lob5al
-lo4ci
-4lof
-3log1ic
-l5o1go
-3logu
-lom3er
-lo2me
-5long
-lo2n
-lon4i
-l3o3niz
-lood5
-loo2
-5lo4pe.
-lop3i
-l3o4p1m
-lo1ra4
-lo4ra1to
-lo5r2ie4
-lor5ou2
-5los.
-los5et
-5los5o3phiz
-lo2so
-los4op
-los2oph
-5los5o1phy
-los4t
-lo4ta
-loun5d
-lou2
-2lout
-4lov
-2lp
-lpa5b
-l1pa
-l3pha
-l5phi
-lp5ing
-lpi2n
-l3pit
-l4p2l2
-l5pr2
-4l1r
-2l1s2
-l4sc
-l2se
-l4s2ie4
-4lt
-lt5ag
-l1ta
-ltane5
-lta2n
-l1te
-lten4
-lter1a4
-lth3i
-l5ties.
-lt2ie4
-ltis4
-l1tr
-l1tu2
-ltu1r3a
-lu5a
-lu3br
-lu2ch4
-lu3ci
-lu3en
-luf4
-lu5id
-l2ui2
-lu4ma
-5lu1mi
-l5umn.
-lu4m1n
-5lum3n4i1a
-lu3o
-luo3r
-4lup
-lu2ss4
-l2us
-lus3te
-1lut
-l5ven
-l5vet4
-2l1w
-1ly
-4lya
-4ly1b
-ly5me4
-ly3no
-2lys4
-l5y3s2e
-1ma
-2mab
-ma2ca
-ma5ch2ine
-ma2ch
-ma4ch1in
-ma4c4l4
-mag5in
-mag1i
-5mag1n
-2mah
-ma2id5
-mai2
-4ma2ld
-ma3l2ig
-mal1i
-ma5lin
-mal4l2i
-mal1l
-mal4ty
-ma4lt
-5ma3n4i1a
-ma2n
-man5is
-man3iz
-4map
-ma5ri2ne.
-m2a2r
-mar1i
-mar2in4e
-ma5r2iz
-mar4ly
-mar1l
-mar3v
-ma5sce
-mas4e
-mas1t
-5mate
-m4ath3
-ma3tis
-4mati3za1
-ma1tiz
-4m1b
-m1ba4t5
-m5bil
-m4b3ing
-mb2i4v
-4m5c
-4me.
-2med
-4med.
-5me3d4i3a
-m4edi
-me3d2ie4
-m5e5d2y
-me2g
-mel5o2n
-me4l4t
-me2m
-me1m1o3
-1men
-me1n4a
-men5ac
-men4de
-4mene
-men4i
-me2n1s4
-men1su5
-3men1t
-men4te
-me5o2n
-m5er1sa2
-me4r1s2
-2mes
-3mest2i
-me4ta
-met3a2l
-me1te
-me5thi
-m4etr
-5met3ric
-me5tr2ie4
-me3try
-me4v
-4m1f
-2mh
-5mi.
-m2i3a
-mi1d4a
-m2id
-mid4g
-m2ig4
-3mil3i1a
-mil1i
-m5i5l2ie4
-m4il1l
-mi1n4a
-3m4ind
-m5i3nee
-m2ine
-m4ingl2
-min5gli
-m5ing1ly
-min4t
-m4in1u
-miot4
-m2io
-m2is
-mi4s4er.
-m4ise
-mis3er
-mis5l2
-mis4t2i
-m5i4stry
-mist4r
-4m2ith
-m2iz
-4mk
-4m1l
-m1m
-mma5ry
-m1ma
-mm2a2r
-4m1n
-m1n4a
-m4n1in
-mn4o
-1mo
-4mocr
-5moc5ra1tiz
-mo2d1
-mo4go
-mois2
-moi2
-mo4i5se
-4m2ok
-mo5lest
-moles2
-mo3me
-mon5et
-mo2n
-mon5ge
-mo3n4i3a
-mon4i2s1m
-mon1is
-mon4ist
-mo3niz
-monol4
-mo3ny.
-mo2r
-4mo5ra.
-mo1ra
-mos2
-mo5sey
-mo3sp
-m4oth3
-m5ouf
-mou2
-3mo2us
-mo2v
-4m1p
-mpara5
-m1pa
-mp2a2r
-mpa5rab
-mp4a4r5i
-m3pe2t
-mphas4
-m2pi
-mp2i4a
-mp5ies
-mp2ie4
-m4p1i2n
-m5p4ir
-mp5is
-mpo3ri
-m1p4or
-mpos5ite
-m1pos
-m4po2us
-mpou2
-mpov5
-mp4tr
-m2p1t
-m2py
-4m3r
-4m1s2
-m4s2h
-m5si
-4mt
-1mu
-mul2a5r4
-mu1la
-5mu4lt
-mul1ti3
-3mum
-mun2
-4mup
-mu4u
-4mw
-1na
-2n1a2b
-n4abu
-4nac.
-na4ca
-n5a2c1t
-nag5er.
-nak4
-na4l1i
-na5l2i1a
-4na4lt
-na5mit
-n2a2n
-nan1ci4
-nan4it
-na4nk4
-nar3c
-n2a2r
-4nare
-nar3i
-nar4l
-n5ar1m
-n4as
-nas4c
-nas5t2i
-n2at
-na3ta2l
-na2ta
-nat5o5m2iz
-na2tom
-na1to
-n2au
-nau3se
-na2us
-3naut
-nav4e
-4n1b4
-nc2a2r5
-n1ca
-n4ces.
-n3cha
-n2ch
-n5cheo
-nche2
-n5ch4il2
-n3chis
-n2c1in
-n1ci
-n2c4it
-ncou1r5a
-n1co
-ncou2
-n1cr
-n1cu
-n4dai2
-n1d2a
-n5da2n
-n1de
-nd5e2st.
-ndes2
-ndi4b
-n5d2if
-n1dit
-n3diz
-n5du2c
-n1du
-ndu4r
-nd2we
-nd1w
-2ne.
-n3e2a2r
-n2e2b
-neb3u
-ne2c
-5neck1
-2ned
-ne4gat
-ne1ga
-ne4g5a1t2iv
-5nege
-ne4la
-nel5iz
-nel2i
-ne5mi
-ne4mo
-1nen
-4nene
-3neo
-ne4po
-ne2q
-n1er
-ne2ra5b
-ner1a
-n4er3a2r
-n2ere
-n4er5i
-ner4r4
-1nes
-2nes.
-4ne1sp
-2nest
-4nes4w2
-3net1ic
-ne4v
-n5eve
-ne4w
-n3f
-n4gab
-n1ga
-n3gel
-nge4n4e
-n1gen
-n5gere
-n3ger1i
-ng5ha
-n3gib
-ng1in
-n5git
-n4gla4
-ngl2
-ngov4
-n1go
-ng5s2h
-ngs2
-n1gu
-n4gum
-n2gy
-4n1h4
-nha4
-nhab3
-nhe4
-3n4i1a
-ni3a2n
-ni4ap
-ni3ba
-ni4b2l2
-n2i4d
-ni5di
-ni4er
-n2ie4
-ni2fi
-ni5ficat
-nifi1ca
-n5i1gr
-n2ig
-n4ik4
-n1im
-ni3m2iz
-nim1i
-n1in
-5ni2ne.
-n2ine
-nin4g
-n2i4o
-5n2is.
-nis4ta
-n2it
-n4ith
-3n2i1t2io
-ni1ti
-n3itor
-ni1to
-ni3tr
-n1j
-4nk2
-n5k2ero
-nk1er
-n3ket
-nk3in
-nk1i
-n1k1l
-4n1l
-n5m
-nme4
-nmet4
-4n1n2
-nne4
-nni3al
-n3n4i1a
-nn2i4v
-nob4l2
-no3ble
-n5o1c4l4
-4n3o2d
-3noe
-4nog
-no1ge4
-nois5i
-noi2
-no5l4i
-5nol1o1gis
-3nomic
-n5o5m2iz
-no4mo
-no3my
-no4n
-non4ag
-no1n1a
-non5i
-n5oniz
-4nop
-5nop5o5l2i
-no2r5ab
-no1ra
-no4rary
-nor2a2r
-4nos2c
-nos4e
-nos5t
-no5ta
-1nou2
-3noun
-nov3el3
-nowl3
-n1p4
-npi4
-npre4c
-npr2
-n1q
-n1r
-nru4
-2n1s2
-n2s5ab
-nsa2
-nsati4
-ns4c
-n2se
-n4s3e4s
-ns2id1
-ns2ig4
-n2s1l2
-n2s3m
-n4soc
-n1so
-ns4pe
-n5spi
-nsta5b2l2
-ns1ta
-ns2tab
-n1t
-n2ta4b
-n1ta
-nte4r3s2
-nt2i
-n5ti2b
-nti4er
-nt2ie4
-nti2f2
-n3t2ine
-n2t1in
-n4t3ing
-nt2i4p
-ntrol5l2i
-ntrol1l
-n4t4s2
-ntu3me
-n1tu
-n3tum
-nu1a
-nu4d
-nu5en
-nuf4fe
-nu4f1f
-n3ui4n
-n2ui2
-3nu3it
-n4um
-nu1me
-n5u1mi
-3nu4n
-n3uo
-nu3tr
-n1v2
-n1w4
-nym4
-nyp4
-4nz
-n3za1
-4oa
-oa2d3
-o5a5les2
-o2ale
-oard3
-o2a2r
-oas4e
-oast5e
-oat5i
-ob3a3b
-o5b2a2r
-o1be4l
-o1bi
-o2bin
-ob5ing
-o3br
-ob3ul
-o1ce
-o2ch4
-o3che4t
-oche2
-ocif3
-o1ci
-o4cil
-o4clam
-o1c4l4
-o4cod
-o1co
-oc3rac
-oc5ra1tiz
-ocre3
-5ocrit
-ocri2
-octo2r5a
-o2c1t
-oc1to
-oc3u1la
-o5cure
-od5d1ed
-od1d4
-od3ic
-o1d2i3o
-o2do4
-od4or3
-o4d5uct.
-o1du
-odu2c
-odu2c1t
-o4d5uc4t1s2
-o4el
-o5eng
-o3er
-oe4ta
-o3ev
-o2fi
-of5ite
-of4i4t4t2
-o2g5a5r
-o1ga
-o4g5a1t2iv
-o4ga1to
-o1ge
-o5gene
-o1gen
-o5geo
-o4ger
-o3g2ie4
-1o1gis
-og3it
-o4gl2
-o5g2ly
-3ogniz
-og1ni
-o4g4ro
-o1gr
-og2u5i2
-1o1gy
-2o2g5y3n
-o1h2
-ohab5
-oi2
-oic3es
-oi3der
-o2id
-oi4f1f4
-o2ig4
-oi5let
-o3ing
-oint5er
-oin1t
-o5i2s1m
-oi5so2n
-oi2so
-oist5en
-ois1te
-oi3ter
-o2ite
-o5j
-2ok
-o3ken
-ok5ie4
-ok1i
-o1la
-o4la2n
-ola2ss4
-o2l2d
-ol2d1e
-ol3er
-o3les2c
-oles2
-o3let
-ol4fi
-o2lf
-ol2i
-o3l2i1a
-o3lice
-ol5id.
-ol2id
-o3li4f
-o5l4i4l
-ol3ing
-o5l2io
-o5l2is.
-ol3is2h
-o5l2ite
-ol1it
-o5l2i1t2io
-oli1ti
-o5l2iv
-oll2i4e4
-ol1l
-oll2i
-ol5o3giz
-olo4r
-ol5p2l2
-o2lp
-o4l2t
-ol3ub
-ol3ume
-ol3un
-o5l2us
-ol2v
-o2ly
-o2m5ah
-o1ma
-oma5l
-om5a1tiz
-om2be
-o4m1b
-om4b2l2
-o2me
-om3e1n4a
-o1men
-om5er2se
-ome4r1s2
-o4met
-om5e3try
-om4etr
-o3m2i3a
-om3ic.
-om3i1ca
-o5m2id
-om1in
-o5m2ini
-5ommend
-om1m
-om1men
-omo4ge
-o1mo
-o4mo2n
-om3pi
-o4m1p
-ompro5
-ompr2
-o2n
-o1n1a
-on4ac
-o3n2a2n
-on1c
-3oncil
-on1ci
-2ond
-on5do
-o3nen
-o2n5est
-o1nes
-on4gu
-on1ic
-o3n2i4o
-on1is
-o5ni1u
-on3key
-o4nk2
-on4odi
-o4n3o2d
-on3o3my
-o2n3s2
-on5spi4
-onspi1r5a
-onsp4ir
-on1su4
-onten4
-on1t
-on3t4i
-onti2f5
-on5um
-on1va5
-on1v2
-oo2
-ood5e
-ood5i
-o2o4k
-oop3i
-o3ord
-oost5
-o2pa
-o2p2e5d
-op1er
-3oper1a
-4op4erag
-2oph
-o5pha2n
-o5ph4er
-op3ing
-opi2n
-o3pit
-o5po2n
-o4posi
-o1pos
-o1pr2
-op1u
-opy5
-o1q
-o1ra
-o5ra.
-o4r3ag
-or5al1iz
-oral1i
-or5an4ge
-ora2n
-or2ang
-ore5a
-o5re1a4l
-or3ei2
-or4e5s2h
-or5e2st.
-ores2t
-orew4
-or4gu
-org2
-4o5r2i3a
-or3i1ca
-o5ril
-or1in
-o1r2i1o
-or3i1ty
-o3ri1u
-or2mi
-or1m
-orn2e
-o5rof
-or3oug
-orou2
-or5pe
-or1p
-3orrh4
-or1r4
-or4se
-o4rs2
-ors5en
-orst4
-or3thi
-or3thy
-or4ty
-o5rum
-o1ry
-os3al
-osa2
-os2c
-os4ce
-o3scop
-os1co
-4oscopi
-o5scr
-os4i4e4
-os5i1t2iv
-osi1ti
-os3i1to
-os3i1ty
-o5si4u
-os4l2
-o2so
-o2s4pa
-os4po
-os2ta
-o5stati
-os5til
-ost2i
-os5tit
-o4ta2n
-o1ta
-otele4g
-ot3er.
-ot5e4r1s2
-o4tes
-4oth
-oth5e1si
-oth2e
-oth1es
-oth3i4
-ot3ic.
-ot5i1ca
-o3tice
-o3tif2
-o3tis
-oto5s2
-o1to
-ou2
-ou3b2l2
-ouch5i
-ou2ch
-ou5et
-ou4l
-ounc5er
-oun2d
-ou5v2
-ov4en
-over4ne
-ove4r3s2
-ov4ert
-o3vis
-o4vi1ti4
-o5v4ol
-ow3der
-ow3el
-ow5est3
-ow1i2
-own5i
-o4wo2
-oy1a
-1pa
-pa4ca
-pa4ce
-pa2c4t
-p4a2d
-5paga4n
-pa1ga
-p3agat
-p4ai2
-pa4i4n4
-p4al
-pa1n4a
-pa2n
-pan3el
-pan4ty
-pan1t
-pa3ny
-pa1p
-pa4pu
-para5b2l2
-p2a2r
-pa2rab
-par5age
-par5d2i
-3pare
-par5el
-p4a4r1i
-par4is
-pa2te
-pa5ter
-5pathic
-p4ath
-pa5thy
-pa4tric
-pa1tr
-pav4
-3pay
-4p1b
-pd4
-4pe.
-3pe4a
-pear4l
-pe2a2r
-pe2c
-2p2ed
-3pede
-3p4edi
-pe3d4i3a4
-ped4ic
-p4ee
-pee4d
-pek4
-pe4la
-pel2i4e4
-pel2i
-pe4n2a2n
-pe1na
-p4enc
-pen4th
-pen1t
-pe5o2n
-p4era.
-per1a
-pera5b2l2
-pe2ra4b
-p4erag
-p4er1i
-peri5st
-per2is
-per4mal
-per3m4
-per1ma
-per2me5
-p4ern
-p2er3o
-per3ti
-p4e5ru
-per1v
-pe2t
-pe5ten
-pe5tiz
-4pf
-4pg
-4ph.
-phar5i
-ph2a2r
-ph4e3no
-phe2n
-ph4er
-ph4es.
-ph1es
-ph1ic
-5ph2ie4
-ph5ing
-5phis1t2i
-3phiz
-p4h2l4
-3phob
-3phone
-pho2n
-5phoni
-pho4r
-4p4h1s2
-ph3t
-5phu
-1phy
-p2i3a
-pi2a2n4
-pi4c2ie4
-p2i1ci
-pi4cy
-p4id
-p5i1d2a
-pi3de
-5pi2di
-3piec
-p2ie4
-pi3en
-pi4grap
-p2ig
-pi1gr
-pi3lo
-pi2n
-p4in.
-p4ind4
-p4i1no
-3p2i1o
-pio2n4
-p3ith
-pi5tha
-pi2tu
-2p3k2
-1p2l2
-3pla2n
-plas5t
-pl2i3a
-pli5er
-pl2ie4
-4pl2ig
-pli4n
-ploi4
-plu4m
-plu4m4b
-4p1m
-2p3n
-po4c
-5pod.
-po5em
-po3et5
-5po4g
-poin2
-poi2
-5poin1t
-poly5t
-po2ly
-po4ni
-po2n
-po4p
-1p4or
-po4ry
-1pos
-po2s1s
-p4ot
-po4ta
-5poun
-pou2
-4p1p
-ppa5ra
-p1pa
-pp2a2r
-p2pe
-p4p2ed
-p5pel
-p3pen
-p3per
-p3pe2t
-ppo5s2ite
-p1pos
-pr2
-pray4e4
-5pre1c2i
-pre5co
-pre3e2m
-pre4f5ac
-pre1f
-pre1fa
-pre4la
-pr1e3r4
-p3re1s2e
-3pr2e2ss
-pre5ten
-pre3v2
-5pr2i4e4
-prin4t3
-pr2i4s
-pri2s3o
-p3ro1ca
-pr2oc
-prof5it
-pro2fi
-pro3l
-pros3e
-pro1t
-2p1s2
-p2se
-ps4h
-p4si1b
-2p1t
-p2t5a4b
-p1ta
-p2te
-p2th
-p1ti3m
-ptu4r
-p1tu
-p4tw4
-pub3
-pue4
-puf4
-pu4l3c2
-pu4m
-pu2n
-pur4r4
-5p2us
-pu2t
-5pute
-put3er
-pu3tr
-put4t1ed
-pu4t3t2
-put4t1in
-p3w
-qu2
-qua5v4
-2que.
-3quer
-3quet
-2rab
-ra3bi
-rach4e2
-ra2ch
-r5a1c4l4
-raf5fi
-ra2f
-ra4f1f4
-ra2f4t
-r2ai2
-ra4lo
-ram3et
-r2ami
-ra3ne5o
-ra2n
-ran4ge
-r2ang
-r4ani
-ra5no4
-rap3er
-3ra1phy
-rar5c
-r2a2r
-rare4
-rar5e1f
-4raril
-rar1i
-r2as
-ratio2n4
-ra1t2io
-rau4t
-ra5vai2
-ra2va
-rav3el
-ra5z2ie4
-ra2z1i
-r1b
-r4bab
-r4bag
-rbi2
-r2b3i4f
-r2bin
-r5b2ine
-rb5ing.
-rb4o
-r1c
-r2ce
-r1cen4
-r3cha
-r2ch
-rch4er
-rche2
-r4ci4b
-r1ci
-r2c4it
-rcum3
-r4dal
-r1d2a
-rd2i
-r1d4i4a
-rdi4er
-rd2ie4
-rd1in4
-rd3ing
-2re.
-re1a4l
-re3a2n
-re5ar1r4
-re2a2r
-5rea2v
-re4aw
-r5ebrat
-r2e1b
-re3br
-rec5ol1l
-re2col
-re1co
-re4c5ompe
-reco4m1p
-re4cre
-re1cr
-2r2ed
-re1de
-re3dis1
-r4edi
-red5it
-re4fac
-re1f
-re1fa
-re2fe
-re5fer.
-refer1
-re3fi
-re4fy
-reg3is
-re5it
-rei2
-re1l2i
-re5lu
-r4en4ta
-ren1t
-ren4te
-re1o
-re5pi2n
-re4posi
-re1po
-re1pos
-re1pu
-r1er4
-r4er1i
-r2ero4
-r4e5ru
-r4es.
-re4spi
-re1sp
-res4s5i4b
-r2e2ss
-res1si
-res2t
-re5s2ta2l
-res1ta
-r2e3st4r
-re4ter
-re4ti4z
-re3tri
-r4eu2
-re5u1t2i
-rev2
-re4val
-re1va
-rev3el
-r5ev5er.
-rev1er
-re5ve4r1s2
-re5vert
-re5vi4l
-re1vi
-rev5olu
-re4wh
-r1f
-r3fu4
-r4fy
-rg2
-rg3er
-r3get
-r3g1ic
-rgi4n
-rg3ing
-r5gis
-r5git
-r1gl2
-rgo4n2
-r1go
-r3gu
-rh4
-4rh.
-4rhal
-r2i3a
-ria4b
-ri4ag
-r4ib
-rib3a
-ric5as5
-ri1ca
-r4ice
-4r2i1ci
-5ri5c2id
-ri4c2ie4
-r4i1co
-rid5er
-r2id
-ri3enc
-r2ie4
-ri3en1t
-ri1er
-ri5et
-rig5a2n
-r2ig
-ri1ga
-5r4igi
-ril3iz
-ril1i
-5rima2n
-ri1ma
-rim5i
-3ri1mo
-rim4pe
-ri4m1p
-r2i1na
-5rina.
-r4in4d
-r2in4e
-rin4g
-r2i1o
-5riph
-r2ip
-riph5e
-ri2p2l2
-rip5lic
-r4iq
-r2is
-r4is.
-r2is4c
-r3is2h
-ris4p
-ri3ta3b
-ri1ta
-r5ited.
-r2ite
-ri2t1ed
-rit5er.
-rit5e4r1s2
-r4i2t3ic
-ri1ti
-ri2tu
-rit5ur
-riv5el
-r2iv
-riv3et
-riv3i
-r3j
-r3ket
-rk4le
-rk1l
-rk4lin
-r1l
-rle4
-r2led
-r4l2ig
-r4lis
-rl5is2h
-r3lo4
-r1m
-rma5c
-r1ma
-r2me
-r3men
-rm5e4r1s2
-rm3ing
-r4ming.
-r4m2io
-r3mit
-r4my
-r4n2a2r
-r1na
-r3nel
-r4n1er
-r5net
-r3ney
-r5nic
-r1nis4
-r3n2it
-r3n2iv
-rno4
-r4nou2
-r3nu
-rob3l2
-r2oc
-ro3cr
-ro4e
-ro1fe
-ro5fil
-ro2fi
-r2ok2
-ro5k1er
-5role.
-rom5e1te
-ro2me
-ro4met
-rom4i
-ro4m4p
-ron4al
-ro2n
-ro1n1a
-ron4e
-ro5n4is
-ron4ta
-ron1t
-1room
-roo2
-5root
-ro3pel
-rop3ic
-ror3i
-ro5ro
-ro2s5per
-ro2s4s
-ro4th2e
-r4oth
-ro4ty
-ro4va
-rov5el
-rox5
-r1p
-r4pe4a
-r5pen1t
-rp5er.
-r3pe2t
-rp4h4
-rp3ing
-rpi2n
-r3po
-r1r4
-rre4c
-rre4f
-r4re1o
-rre4s2t
-rr2i4o
-rr2i4v
-rro2n4
-rros4
-rrys4
-4rs2
-r1sa2
-rsa5ti
-rs4c
-r2se
-r3sec
-rse4cr
-r4s5er.
-rs3e4s
-r5se5v2
-r1s2h
-r5sha
-r1si
-r4si4b
-rso2n3
-r1so
-r1sp
-r5sw2
-rta2ch4
-r1ta
-r4tag
-r3t2e1b
-r3ten4d
-r1te5o
-r1ti
-r2t5i2b
-rt2i4d
-r4tier
-rt2ie4
-r3t2ig
-rtil3i
-rtil4l
-r4ti1ly
-r4tist
-r4t2iv
-r3tri
-rtr2oph4
-rt4s2h4
-r4t1s2
-ru3a
-ru3e4l
-ru3en
-ru4gl2
-ru3i4n
-r2ui2
-rum3p2l2
-ru4m2p
-ru2n
-ru4nk5
-run4ty
-run1t
-r5usc2
-r2us
-ru2t1i5n
-r4u1t2i
-rv4e
-rvel4i
-r3ven
-rv5er.
-r5vest
-rv4e2s
-r3vey
-r3vic
-r3v2i4v
-r3vo
-r1w
-ry4c
-5rynge
-ryn5g
-ry3t
-sa2
-2s1ab
-5sack1
-sac3ri2
-s3a2c1t
-5sai2
-sa4l2a2r4
-s4a2l4m
-sa5lo
-sa4l4t
-3sanc
-sa2n
-san4de
-s4and
-s1ap
-sa5ta
-5sa3t2io
-sa2t3u
-sau4
-sa5vor
-5saw
-4s5b
-scan4t5
-s1ca
-sca2n
-sca4p
-scav5
-s4ced
-4s3cei2
-s4ces
-s2ch2
-s4cho2
-3s4c2ie4
-s1ci
-5sc4in4d
-s2cin
-scle5
-s1c4l4
-s4cli
-scof4
-s1co
-4scopy5
-scou1r5a
-scou2
-s1cu
-4s5d
-4se.
-se4a
-seas4
-sea5w
-se2c3o
-3se2c1t
-4s4ed
-se4d4e
-s5edl
-se2g
-se1g3r
-5sei2
-se1le
-5se2l2f
-5selv
-4se1me
-se4mol
-se1mo
-sen5at
-se1na
-4senc
-sen4d
-s5e2ned
-sen5g
-s5en1in
-4sen4t1d
-sen1t
-4sen2tl
-se2p3a3
-4s1er.
-s4er1l
-s2er4o
-4ser3vo
-s1e4s
-s4e5s2h
-ses5t
-5se5um
-s4eu
-5sev
-sev3en
-sew4i2
-5sex
-4s3f
-2s3g
-s2h
-2sh.
-sh1er
-5shev
-sh1in
-sh3io
-3sh2i4p
-sh2i2v5
-sho4
-sh5o2l2d
-sho2n3
-shor4
-short5
-4sh1w
-si1b
-s5ic3c
-3si2de.
-s2id
-5side4s2
-5si2di
-si5diz
-4sig1n4a
-s2ig
-sil4e
-4si1ly
-2s1in
-s2i1na
-5si2ne.
-s2ine
-s3ing
-1s2io
-5sio2n
-sio1n5a
-s4i2r
-si1r5a
-1sis
-3s2i1t2io
-si1ti
-5si1u
-1s2iv
-5siz
-sk2
-4ske
-s3ket
-sk5ine
-sk1i
-sk5in4g
-s1l2
-s3lat
-s2le
-sl2ith5
-sl1it
-2s1m
-s3ma
-smal1l3
-sma2n3
-smel4
-s5men
-5s4m2ith
-smo2l5d4
-s1mo
-s1n4
-1so
-so4ce
-so2ft3
-so4lab
-so1la
-so2l3d2
-so3lic
-sol2i
-5sol2v
-3som
-3s4on.
-so2n
-so1n1a4
-son4g
-s4op
-5soph1ic
-s2oph
-s5o3phiz
-s5o1phy
-sor5c
-sor5d
-4sov
-so5vi
-2s1pa
-5sp4ai2
-spa4n
-spen4d
-2s5peo
-2sper
-s2phe
-3sph4er
-spho5
-spil4
-sp5ing
-spi2n
-4s3p2i1o
-s4p1ly
-s1p2l2
-s4po2n
-s1p4or4
-4sp4ot
-squal4l
-squ2
-s1r
-2ss
-s1sa2
-ssas3
-s2s5c
-s3sel
-s5sen5g
-s4ses.
-ss1e4s
-s5set
-s1si
-s4s2ie4
-ssi4er
-s4s5i1ly
-s4s1l2
-ss4li
-s4s1n4
-sspen4d4
-ss2t
-ssu1r5a
-s1su
-ssu2r
-ss5w2
-2st.
-s2tag
-s1ta
-s2ta2l
-stam4i
-5st4and
-sta2n
-s4ta4p
-5stat.
-s4t1ed
-stern5i
-s5t2ero
-ste2w
-ste1w5a
-s3th2e
-st2i
-s4ti.
-s5t2i1a
-s1tic
-5s4tick1
-s4t2ie4
-s3tif2
-st3ing
-s2t1in
-5st4ir
-s1tle
-s2tl
-5stock1
-s1to
-sto2m3a
-5stone
-sto2n
-s4top
-3store
-st4r
-s4tra2d
-s1tra
-5stra2tu
-s4tray
-s4tr2id
-4stry
-4st3w4
-s2ty
-1su
-su1al
-su4b3
-su2g3
-su5is
-s2ui2
-suit3
-s4ul
-su2m
-su1m3i
-su2n
-su2r
-4sv
-sw2
-4s1wo2
-s4y
-4sy1c
-3syl
-syn5o
-sy5rin
-1ta
-3ta.
-2tab
-ta5bles2
-tab2l2
-5tab5o5l1iz
-tabol2i
-4t4a2ci
-ta5do
-ta2d
-4ta2f4
-tai5lo
-tai2
-ta2l
-ta5la
-tal5en
-t2ale
-tal3i
-4talk
-tal4lis
-tal1l
-tall2i
-ta5log
-ta5mo
-tan4de
-ta2n
-t4and
-1tan1ta3
-tan1t
-ta5per
-ta5p2l2
-tar4a
-t2a2r
-4tar1c
-4tare
-ta3r2iz
-tar1i
-tas4e
-ta5s4y
-4tat1ic
-ta4tur
-ta2tu
-taun4
-tav4
-2taw
-tax4is
-tax3i
-2t1b
-4tc
-t4ch
-tch5e4t
-tche2
-4t1d
-4te.
-te2ad4i
-tea2d1
-4tea2t
-te1ce4
-5te2c1t
-2t1ed
-t4e5di
-1tee
-teg4
-te5ger4
-te5gi
-3tel.
-tel2i4
-5te2l1s2
-te2ma2
-tem3at
-3ten2a2n
-te1na
-3tenc
-3tend
-4te1nes
-1ten1t
-ten4tag
-ten1ta
-1teo
-te4p
-te5pe
-ter3c
-5ter3d
-1ter1i
-ter5ies
-ter2ie4
-ter3is
-teri5za1
-5t4er3n2it
-ter5v
-4tes.
-4t2e2ss
-t3ess.
-teth5e
-3t4eu
-3tex
-4tey
-2t1f
-4t1g
-2th.
-tha2n4
-th2e
-4thea
-th3eas
-the5a2t
-the3is
-thei2
-3the4t
-th5ic.
-th5i1ca
-4th4il2
-5th4i4nk2
-4t4h1l4
-th5ode
-5thod3ic
-4thoo2
-thor5it
-tho5riz
-2t4h1s2
-1t2i1a
-ti4ab
-ti4a1to
-2ti2b
-4tick1
-t4i1co
-t4ic1u
-5ti2di
-t2id
-3tien
-t2ie4
-tif2
-ti5fy
-2t2ig
-5tigu
-til2l5in4
-til1l
-till2i
-1tim
-4ti4m1p
-tim5ul
-ti2mu
-2t1in
-t2i1na
-3ti2ne.
-t2ine
-3t2ini
-1t2io
-ti5oc
-tion5ee
-tio2n
-5tiq
-ti3sa2
-3t4ise
-ti2s4m
-ti5so
-tis4p
-5tisti1ca
-tis1t2i
-tis1tic
-ti3tl
-ti4u
-1t2iv
-ti1v4a
-1tiz
-ti3za1
-ti3ze4n
-ti2ze
-2tl
-t5la
-tla2n4
-3tle.
-3tled
-3tles.
-tles2
-t5let.
-t5lo
-4t1m
-tme4
-2t1n2
-1to
-to3b
-to5crat
-4to2do4
-2tof
-to2gr
-to5ic
-toi2
-to2ma
-to4m4b
-to3my
-ton4a4l1i
-to2n
-to1n1a
-to3n2at
-4tono
-4tony
-to2ra
-to3r2ie4
-tor5iz
-tos2
-5tour
-tou2
-4tout
-to3w2a2r
-4t1p
-1tra
-t2ra3b
-tra5ch
-tr4a2ci4
-tra2c4it
-trac4te
-tra2c1t
-tr2as4
-tra5ven
-trav5e2s5
-tre5f
-tre4m
-trem5i
-5tr2i3a
-tri5ces
-tr4ice
-5tri3c2i1a
-t4r2i1ci
-4tri4c3s2
-2trim
-tr2i4v
-tro5m4i
-tron5i
-tro2n
-4trony
-tro5phe
-tr2oph
-tro3sp
-tro3v
-tr2u5i2
-tr2us4
-4t1s2
-t4sc
-ts2h4
-t4sw2
-4t3t2
-t4tes
-t5to
-t1tu4
-1tu
-tu1a
-tu3a2r
-tu4b4i
-tud2
-4tue
-4tuf4
-5t2u3i2
-3tum
-tu4nis
-tu1ni
-2t3up.
-3ture
-5turi
-tur3is
-tur5o
-tu5ry
-3t2us
-4tv
-tw4
-4t1wa
-twis4
-twi2
-4t1wo2
-1ty
-4tya
-2tyl
-type3
-ty5ph
-4tz
-t2z4e
-4uab
-uac4
-ua5na
-ua2n
-uan4i
-uar5an1t
-u2a2r
-uara2n
-uar2d
-uar3i
-uar3t
-u1at
-uav4
-ub4e
-u4bel
-u3ber
-u4b2ero
-u1b4i
-u4b5ing
-u3b4le.
-ub2l2
-u3ca
-uci4b
-u1ci
-u2c4it
-ucle3
-u1c4l4
-u3cr
-u3cu
-u4cy
-ud5d4
-ud3er
-ud5est
-udes2
-ude1v4
-u1dic
-ud3ied
-ud2ie4
-ud3ies
-ud5is1
-u5dit
-u4do2n
-u1do
-ud4si
-u2d1s2
-u4du
-u4ene
-ue2n1s4
-uen4te
-uen1t
-uer4il
-uer1i
-3u1fa
-u3f4l2
-ugh3e2n
-ug5in
-2ui2
-uil5iz
-uil1i
-ui4n
-u1ing
-uir4m
-u4ir
-ui1ta4
-u2iv3
-ui4v4er.
-u5j
-4uk
-u1la
-ula5b
-u5lati
-ul2ch4
-u4l1c2
-5ulche2
-ul3der
-u2ld
-ul2de
-ul4e
-u1len
-ul4gi
-u4l1g4
-ul2i
-u5l2i1a
-ul3ing
-ul5is2h
-ul4l2a2r
-ul1l
-ul4li4b
-ull2i
-ul4lis
-4u2l3m
-u1l4o
-4u2l1s2
-uls5e4s
-ul2se
-ul1ti
-u4lt
-ul1tra3
-ul1tr
-4ul1tu2
-u3lu
-ul5ul
-ul5v
-u2m5ab
-u1ma
-um4bi
-u4m1b
-um4b1ly
-umb2l2
-u1mi
-u4m3ing
-umor5o
-u1mo
-umo2r
-u4m2p
-un2at4
-u1na
-u2ne
-un4er
-u1ni
-un4im
-u2n1in
-un5is2h
-un2i3v
-u2n3s4
-un4sw2
-un2t3a4b
-un1t
-un1ta
-un4ter.
-un4tes
-unu4
-un5y
-u4n5z
-u4o4rs2
-u5os
-u1ou2
-u1pe
-upe4r5s2
-u5p2i3a
-up3ing
-upi2n
-u3p2l2
-u4p3p
-upport5
-up1p4or
-up2t5i2b
-u2p1t
-up1tu4
-u1ra
-4ura.
-u4rag
-u4r2as
-ur4be
-ur1b
-ur1c4
-ur1d
-ure5a2t
-ur4fer1
-ur1f
-ur4fr
-u3rif
-uri4fic
-uri1fi
-ur1in
-u3r2i1o
-u1rit
-ur3iz
-ur2l
-url5ing.
-ur4no4
-uros4
-ur4pe
-ur1p
-ur4pi
-urs5er
-u4rs2
-ur2se
-ur5tes
-ur3th2e
-ur1ti4
-ur4t2ie4
-u3ru
-2us
-u5sa2d
-usa2
-u5sa2n
-us4ap
-usc2
-us3ci
-use5a
-u5s2i1a
-u3sic
-us4lin
-us1l2
-us1p
-us5s1l2
-u2ss
-us5tere
-us1t4r
-u2su
-usu2r4
-u2ta4b
-u1ta
-u3tat
-4u4te.
-4utel
-4uten
-uten4i
-4u1t2i
-uti5l2iz
-util1i
-u3t2ine
-u2t1in
-ut3ing
-utio1n5a
-u1t2io
-utio2n
-u4tis
-5u5tiz
-u4t1l
-u2t5of
-u1to
-uto5g
-uto5mat1ic
-uto2ma
-u5to2n
-u4tou2
-u4t1s4
-u3u
-uu4m
-u1v2
-ux1u3
-u2z4e
-1va
-5va.
-2v1a4b
-vac5il
-v4a2ci
-vac3u
-vag4
-va4ge
-va5l2i4e4
-val1i
-val5o
-val1u
-va5mo
-va5niz
-va2n
-va5pi
-var5ied
-v2a2r
-var1i
-var2ie4
-3vat
-4ve.
-4ved
-veg3
-v3el.
-vel3l2i
-vel1l
-ve4lo
-v4e1ly
-ven3om
-v4eno
-v5enue
-v4erd
-5v2e2re.
-v4erel
-v3eren
-ver5enc
-v4eres
-ver3ie4
-ver1i
-vermi4n
-ver3m4
-3ver2se
-ve4r1s2
-ver3th
-v4e2s
-4ves.
-ves4te
-ve4te
-vet3er
-ve4ty
-vi5al1i
-v2i1a
-vi2al
-5vi2a2n
-5vi2de.
-v2id
-5vi2d1ed
-4v3i1den
-5vide4s2
-5vi2di
-v3if
-vi5gn
-v2ig
-v4ik4
-2vil
-5v2il1it
-vil1i
-v3i3l2iz
-v1in
-4vi4na
-v2inc
-v4in5d
-4ving
-vi1o3l
-v2io
-v3io4r
-vi1ou2
-v2i4p
-vi5ro
-v4ir
-vis3it
-vi3so
-vi3su
-4vi1ti
-vit3r
-4vi1ty
-3v2iv
-5vo.
-voi4
-3v2ok
-vo4la
-v5ole
-5vo4l2t
-3vol2v
-vom5i
-vo2r5ab
-vo1ra
-vori4
-vo4ry
-vo4ta
-4vo1tee
-4vv4
-v4y
-w5ab2l2
-2wac
-wa5ger
-wa2g5o
-wait5
-wai2
-w5al.
-wam4
-war4t
-w2a2r
-was4t
-wa1te
-wa5ver
-w1b
-wea5r2ie4
-we2a2r
-wear1i
-we4ath3
-wea2t
-we4d4n4
-weet3
-wee5v
-wel4l
-w1er
-west3
-w3ev
-whi4
-wi2
-wil2
-wil2l5in4
-wil1l
-will2i
-win4de
-w4ind
-win4g
-w4ir4
-3w4ise
-w2ith3
-wiz5
-w4k
-wl4es2
-wl3in
-w4no
-1wo2
-wom1
-wo5v4en
-w5p
-wra4
-wri4
-wri1ta4
-w3s2h
-ws4l2
-ws4pe
-w5s4t
-4wt
-wy4
-x1a
-xac5e
-x4a2go
-xam3
-x4ap
-xas5
-x3c2
-x1e
-xe4cu1to
-xe1cu
-xe3c4ut
-x2ed
-xer4i
-x2e5ro
-x1h
-xhi2
-xh4il5
-xhu4
-x3i
-x2i5a
-xi5c
-xi5di
-x2id
-x4ime
-xi5m2iz
-xim1i
-x3o
-x4ob
-x3p
-xp4an4d
-x1pa
-xpa2n
-xpec1to5
-xpe2c
-xpe2c1t
-x2p2e3d
-x1t2
-x3ti
-x1u
-xu3a
-xx4
-y5ac
-3y2a2r4
-y5at
-y1b
-y1c
-y2ce
-yc5er
-y3ch
-ych4e2
-ycom4
-y1co
-ycot4
-y1d
-y5ee
-y1er
-y4er1f
-yes4
-ye4t
-y5gi
-4y3h
-y1i
-y3la
-ylla5b2l2
-yl1l
-y3lo
-y5lu
-ymbol5
-y4m1b
-yme4
-ym1pa3
-y4m1p
-yn3c4hr4
-yn2ch
-yn5d
-yn5g
-yn5ic
-5ynx
-y1o4
-yo5d
-y4o5g
-yom4
-yo5net
-yo2n
-y4o2n3s2
-y4os
-y4p2ed
-yper5
-yp3i
-y3po
-y4po4c
-yp2ta
-y2p1t
-y5pu
-yra5m
-yr5i3a
-y3ro
-yr4r4
-ys4c
-y3s2e
-ys3i1ca
-y1s3io
-3y1sis
-y4so
-y2ss4
-ys1t
-ys3ta
-ysu2r4
-y1su
-y3thin
-yt3ic
-y1w
-za1
-z5a2b
-z2a2r2
-4zb
-2ze
-ze4n
-ze4p
-z1er
-z2e3ro
-zet4
-2z1i
-z4il
-z4is
-5zl
-4zm
-1zo
-zo4m
-zo5ol
-zoo2
-zte4
-4z1z2
-z4zy
-.as9s8o9c8i8a8te.
-.as1so
-.asso1ci
-.asso3c2i1a
-.as9s8o9c8i8a8t8es.
-.de8c9l8i9n8a9t8i8on.
-.de1c4l4
-.decl4i1na
-.declin2at
-.declina1t2io
-.declinatio2n
-.ob8l8i8g9a9t8o8ry.
-.ob2l2
-.obl2ig
-.obli1ga
-.obliga1to
-.obligato1ry
-.ph8i8l9a8n9t8h8r8o8p8ic.
-.ph4il2
-.phi1la
-.phila2n
-.philan1t
-.philant4hr4
-.philanthrop3ic
-.pr8e8s8e8nt.
-.p3re1s2e
-.presen1t
-.pr8e8s8e8n8ts.
-.presen4t4s2
-.pr8o8j8e8ct.
-.pro5j
-.pro1je
-.proje2c1t
-.pr8o8j8e8c8ts.
-.projec4t1s2
-.re8c9i9p8r8o8c9i9t8y.
-.re1c2i
-.rec2ip
-.recipr2
-.recipr2oc
-.re1cipro1ci
-.recipro2c1it
-.reciproci1ty
-.re9c8o8g9n8i9z8a8n8ce.
-.re1co
-.re2cog
-.rec3ogniz
-.recog1ni
-.recogniza1
-.recogniza2n
-.re8f9o8r9m8a9t8i8on.
-.re1f
-.re1fo
-.refo2r
-.refor1m
-.refor1ma
-.reforma1t2io
-.reformatio2n
-.re8t9r8i9b8u9t8i8on.
-.re3tri
-.retr4ib
-.retri3bu1t2io
-.retrib4u1t2i
-.retributio2n
-.ta9b8le.
-.2tab
-.tab2l2
-.ac8a8d9e9m8y.
-.a1ca
-.aca2d
-.acad4em
-.acade3my
-.ac8a8d9e9m8i8e8s.
-.academ2i4e4
-.ac9c8u9s8a9t8i8v8e.
-.ac3c
-.ac1c2us
-.accusa2
-.accusa1t2iv
-.ac8r8o9n8y8m.
-.acro2n
-.acronym4
-.ac8r8y8l9a8m8i8d8e.
-.acry3la
-.acrylam2id
-.ac8r8y8l9a8m8i8d8e8s.
-.acrylamide4s2
-.ac8r8y8l9a8l8d8e9h8y8d8e.
-.acryla2ld
-.acrylal2de
-.acrylalde1h4
-.acrylaldehy1d
-.ad8d9a9b8l8e.
-.ad1d2a
-.ad2d3a4b
-.addab2l2
-.ad8d9i9b8l8e.
-.addi1b2l2
-.ad8r8e8n9a9l8i8n8e.
-.a1dr
-.adre4
-.a5dren
-.adre1na
-.adrena4l1i
-.adrena1l4ine
-.ae8r8o9s8p8a8c8e.
-.ae4r
-.a2ero
-.aero2s4pa
-.aerospa4ce
-.af9t8e8r9t8h8o8u8g8h8t.
-.afterthou2
-.af9t8e8r9t8h8o8u8g8h8t8s.
-.afterthough4t1s2
-.ag8r8o8n9o9m8i8s8t.
-.a1gr
-.ag4ro
-.agro2n
-.agronom2is
-.ag8r8o8n9o9m8i8s8t8s.
-.agronomis4t1s2
-.al9g8e9b8r8a9i9c8a8l9l8y.
-.a4l1g4
-.alg2e1b
-.alge3br
-.algebr2ai2
-.algebrai1ca
-.algebraical1l
-.algebraical1ly
-.am9p8h8e8t9a9m8i8n8e.
-.a4m1p
-.amphe4t
-.amphe1ta
-.amphetam1in
-.amphetam2ine
-.am9p8h8e8t9a9m8i8n8e8s.
-.amphetami1nes
-.an9a9l8y8s8e.
-.3ana1ly
-.a1na
-.an4a2lys4
-.anal5y3s2e
-.an9a9l8y8s8e8d.
-.analy4s4ed
-.an8a8l8y9s8e8s.
-.analys1e4s
-.an9i8s8o9t8r8o8p9i8c.
-.ani2so
-.anisotrop3ic
-.an9i8s8o9t8r8o8p9i9c8a8l9l8y.
-.anisotropi1ca
-.anisotropical1l
-.anisotropical1ly
-.an9i8s8o8t9r8o9p8i8s8m.
-.anisotropi2s1m
-.an9i8s8o8t9r8o8p8y.
-.anisotropy5
-.an8o8m9a8l8y.
-.ano4
-.anoma5l
-.ano1ma
-.anoma1ly
-.an8o8m9a8l8i8e8s.
-.anomal1i
-.anomal2i4e4
-.an8t8i9d8e8r8i8v9a9t8i8v8e.
-.ant2id
-.antider1i
-.antider2i4v
-.antide4ri1va
-.antideri3vat
-.antider2iva1t2iv
-.an8t8i9d8e8r8i8v9a9t8i8v8e8s.
-.antiderivativ4e2s
-.an8t8i9h8o8l8o9m8o8r9p8h8i8c.
-.anti3h
-.antiholo1mo
-.antiholomo2r
-.antiholomor1p
-.antiholomorp4h4
-.antiholomorph1ic
-.an9t8i8n9o9m8y.
-.an2t1in
-.ant2i1no
-.antino3my
-.an9t8i8n9o9m8i8e8s.
-.antinom2ie4
-.an9t8i9n8u9c8l8e8a8r.
-.antin1u
-.antinucle3
-.antinu1c4l4
-.antinucle2a
-.antinucle2a2r
-.an9t8i9n8u9c8l8e9o8n.
-.antinucleo2n
-.an9t8i9r8e8v9o9l8u9t8i8o8n9a8r8y.
-.ant4ir
-.antirev2
-.antirev5olu
-.antirevo1lut
-.antirevol4u1t2i
-.antirevolutio1n5a
-.antirevolu1t2io
-.antirevolutio2n
-.antirevolution2a2r
-.ap8o8t8h9e9o9s8e8s.
-.ap4ot
-.ap4oth
-.apoth2e
-.apotheos4
-.apotheos1e4s
-.ap8o8t8h9e9o9s8i8s.
-.apotheo1sis
-.ap9p8e8n9d8i8x.
-.a4p1p
-.ap2pe
-.ap3pen
-.ar9c8h8i9m8e9d8e8a8n.
-.ar1c
-.ar2ch
-.archi2med
-.archimedea2n
-.ar9c8h8i9p8e8l9a8g8o.
-.arch2i4p
-.archipe4
-.archipe4la
-.archipela2go
-.ar9c8h8i9p8e8l9a9g8o8s.
-.ar9c8h8i8v8e.
-.arch2i2v
-.ar9c8h8i8v8e8s.
-.archiv4e2s
-.ar9c8h8i8v9i8n8g.
-.archiv1in
-.archi4ving
-.ar9c8h8i8v9i8s8t.
-.ar9c8h8i8v9i8s8t8s.
-.archivis4t1s2
-.ar9c8h8e9t8y8p9a8l.
-.arche2
-.arche4t
-.arche1ty
-.archety1pa
-.archetyp4al
-.ar9c8h8e9t8y8p9i9c8a8l.
-.archetyp3i
-.archetypi1ca
-.ar8c9t8a8n9g8e8n8t.
-.ar2c1t
-.arct5ang
-.arc1ta
-.arcta2n
-.arctan1gen
-.arctangen1t
-.ar8c9t8a8n9g8e8n8t8s.
-.arctangen4t4s2
-.as9s8i8g8n9a9b8l8e.
-.as1si
-.as4sig1n4a
-.ass2ig
-.assig2n1a2b
-.assignab2l2
-.as9s8i8g8n9o8r.
-.assig1no
-.as9s8i8g8n9o8r8s.
-.assigno4rs2
-.as9s8i8s8t9a8n8t9s8h8i8p.
-.as1sis
-.assis1ta
-.assista2n
-.assistan1t
-.assistan4t4s2
-.assistants2h4
-.assistant3sh2i4p
-.as9s8i8s8t9a8n8t9s8h8i8p8s.
-.assistantshi2p1s2
-.as8y8m8p9t8o9m8a8t8i8c.
-.as4y
-.asy4m1p
-.asym2p1t
-.asymp1to
-.asympto2ma
-.asymptomat1ic
-.as9y8m8p9t8o8t9i8c.
-.as8y8n9c8h8r8o9n8o8u8s.
-.asyn3c4hr4
-.asyn2ch
-.asynchro2n
-.asynchro1nou2
-.asynchrono2us
-.at8h9e8r9o9s8c8l8e9r8o9s8i8s.
-.4ath
-.ath2e
-.ath2ero
-.atheros2c
-.atheroscle5
-.atheros1c4l4
-.ath2eroscl4ero
-.atherosclero1sis
-.at9m8o8s9p8h8e8r8e.
-.a4t1m
-.at1mo
-.atmos2
-.atmo3sp
-.atmos2phe
-.atmo3sph4er
-.at9m8o8s9p8h8e8r8e8s.
-.at9t8r8i8b9u8t8e8d.
-.a4t3t2
-.attr4ib
-.attribu2t1ed
-.at9t8r8i8b9u8t9a8b8l8e.
-.attri4bu1ta
-.attribu2ta4b
-.attributab2l2
-.au9t8o9m8a9t8i8o8n.
-.au1to
-.auto2ma
-.automa1t2io
-.automatio2n
-.au9t8o8m9a9t8o8n.
-.automa1to
-.automato2n
-.au9t8o8m9a9t8a.
-.automa2ta
-.au9t8o9n8u8m9b8e8r9i8n8g.
-.au5to2n
-.auton5um
-.autonu4m1b
-.autonumber1i
-.autonumberin4g
-.au9t8o8n9o9m8o8u8s.
-.au4tono
-.autono4mo
-.autono3mo2us
-.autonomou2
-.au8t8o9r8o8u8n8d9i8n8g.
-.autorou2
-.autoroun2d
-.autoround1in
-.av9o8i8r9d8u9p8o8i8s.
-.avoi4
-.avo4ir
-.avoir1du
-.avoir4dup
-.avoirdupoi2
-.ba8n8d9l8e8a8d8e8r.
-.b4and
-.ban1dl
-.bandle2a
-.bandlea2d1
-.ba8n8d9l8e8a8d8e8r8s.
-.bandleade4r5s2
-.ba8n8k9r8u8p8t.
-.ba4nk2
-.bankru2p1t
-.ba8n8k9r8u8p8t9c8y.
-.bankrup4tc
-.bankrupt1cy
-.ba8n8k9r8u8p8t9c8i8e8s.
-.bankrupt1ci
-.bankruptc2ie4
-.ba8r9o8n8i8e8s.
-.b2a2r
-.ba5roni
-.baro2n
-.baron2ie4
-.ba8s8e9l8i8n8e9s8k8i8p.
-.basel2i
-.base1l4ine
-.baseli1nes
-.baselinesk2
-.baselinesk1i
-.baselinesk2i4p
-.ba9t8h8y8m9e9t8r8y.
-.1bat
-.b4ath
-.bathyme4
-.bathym4etr
-.bathyme3try
-.ba8t8h8y9s8c8a8p8h8e.
-.bathy2s
-.bathys4c
-.bathysca4p
-.bathys1ca
-.be8a8n9i8e8s.
-.bea2n
-.bea3nies
-.bean2ie4
-.be9h8a8v9i8o8u8r.
-.be1h4
-.behav1i
-.behavi1ou2
-.behav2io
-.behavi4our
-.be9h8a8v9i8o8u8r8s.
-.behaviou4rs2
-.be8v8i8e8s.
-.be1vi
-.bev2ie4
-.bi8b9l8i9o8g9r8a9p8h8y9s8t8y8l8e.
-.bi2b
-.bi1b2l2
-.bib3li
-.bibli5og
-.bibl2io
-.biblio2gr
-.biblio4g3ra1phy
-.bibliography2s
-.bibliographys1t
-.bibliographys2ty
-.bibliographys2tyl
-.bi9d8i8f9f8e8r9e8n9t8i8a8l.
-.b2i4d
-.bi2di
-.bid1if
-.bidi4f1f
-.bidiffer1
-.bidiffer3en1t
-.bidifferent2i
-.bidifferen1t2i1a
-.bidifferenti2al
-.bi8g9g8e8s8t.
-.b2ig
-.bi4g1g2
-.big2ge
-.bi8l8l9a8b8l8e.
-.1bil
-.bill5ab
-.bil1l
-.billab2l2
-.bi8o9m8a8t8h9e9m8a8t9i8c8s.
-.b2io
-.bio4m
-.bio1ma
-.biom4ath3
-.biomath5em
-.biomath2e
-.biomathe1ma
-.biomathemat1ic
-.biomathemati4c3s2
-.bi8o9m8e8d9i9c8a8l.
-.bio2me
-.bio2med
-.biom4edi
-.biomed3i1ca
-.bi8o9m8e8d9i9c8i8n8e.
-.biomed2i1ci
-.biomedi2cin
-.biomedic2ine
-.bi8o9r8h8y8t8h8m8s.
-.biorh4
-.biorhyt4h1m
-.biorhyth4m1s2
-.bi8t9m8a8p.
-.bi2t
-.bi4t1m
-.bit1ma
-.bit4map
-.bi8t9m8a8p8s.
-.bitma2p1s2
-.bl8a8n8d9e8r.
-.b2l2
-.b3l4and
-.bla2n
-.blan1de
-.bl8a8n8d9e8s8t.
-.blande4s2
-.bl8i8n8d9e8r.
-.bl4ind
-.blin1de
-.bl8o8n8d8e8s.
-.b4lo
-.blo2n
-.bl2ond
-.blon1de
-.blondes2
-.bl8u8e9p8r8i8n8t.
-.bluepr2
-.blueprin4t3
-.bl8u8e9p8r8i8n8t8s.
-.blueprin4t4s2
-.bo9l8o8m9e9t8e8r.
-.bolo2me
-.bolo4met
-.bolome1te
-.bo8o8k9s8e8l8l9e8r.
-.3boo2
-.bo2o4k
-.boo4k1s2
-.booksel1l
-.booksel2le
-.bo8o8k9s8e8l8l9e8r8s.
-.bookselle4r1s2
-.bo8o8l9e8a8n.
-.boole2a
-.boolea2n
-.bo8o8l9e8a8n8s.
-.boolea2n1s2
-.bo8r9n8o9l8o8g9i9c8a8l.
-.borno4
-.borno3log1ic
-.bornologi1ca
-.bo8t9u9l8i8s8m.
-.bo1tu
-.botul2i
-.botuli2s1m
-.br8u8s8q8u8e8r.
-.br2us
-.brusqu2
-.brus3quer
-.bu8f9f8e8r.
-.buf4fer1
-.bu4f1f
-.bu8f9f8e8r8s.
-.buffe4r1s2
-.bu8s8i8e8r.
-.bus5ie4
-.b2us
-.bu8s8i8e8s8t.
-.busi1est
-.bu8s8s8i8n8g.
-.bu2ss
-.bus1si
-.bus2s1in
-.buss3ing
-.bu8t8t8e8d.
-.but2t1ed
-.bu8z8z9w8o8r8d.
-.bu4z1z2
-.buzz1wo2
-.bu8z8z9w8o8r8d8s.
-.buzzwor2d1s2
-.ca9c8o8p8h9o9n8y.
-.ca1co
-.cac2oph
-.cacopho5ny
-.cacopho2n
-.ca9c8o8p8h9o9n8i8e8s.
-.caco5phoni
-.cacophon2ie4
-.ca8l8l9e8r.
-.cal1l
-.cal2le
-.ca8l8l9e8r8s.
-.calle4r1s2
-.ca8m9e8r8a9m8e8n.
-.cam5er1a
-.camera1men
-.ca8r8t9w8h8e8e8l.
-.cartw4
-.ca8r8t9w8h8e8e8l8s.
-.cartwhee2l1s2
-.ca9t8a8r8r8h8s.
-.ca2ta
-.cat2a2r
-.catar1r4
-.catarrh4
-.catarr4h1s2
-.ca8t9a9s8t8r8o8p8h9i8c.
-.catas1t4r
-.catastr2oph
-.catastroph1ic
-.ca8t9a9s8t8r8o8p8h9i9c8a8l8l8y.
-.catastrophi1ca
-.catastrophical1l
-.catastrophical1ly
-.ca8t9e9n8o8i8d.
-.cat4eno
-.catenoi2
-.cateno2id
-.ca8t9e9n8o8i8d8s.
-.catenoi2d1s2
-.ca8u9l8i9f8l8o8w9e8r.
-.cau4l2
-.caul2i
-.cauli4f4l2
-.cauliflow1er
-.ch8a8p9a8r9r8a8l.
-.chap2a2r4
-.cha1pa
-.chapar1r4
-.ch8a8r9t8r8e8u8s8e.
-.ch2a2r
-.chartr4eu2
-.chartre2us4
-.ch8e8m8o9t8h8e8r9a8p8y.
-.che2
-.che1mo
-.chem4oth3
-.chemoth2e
-.chemoth4er1a
-.chemothera3p
-.ch8e8m8o9t8h8e8r9a9p8i8e8s.
-.chemotherap2ie4
-.ch8l8o8r8o9m8e8t8h9a8n8e.
-.c4h1l4
-.ch2lo
-.chloro2me
-.chloro4met
-.chlorometha2n4
-.ch8l8o8r8o9m8e8t8h9a8n8e8s.
-.chlorometha1nes
-.ch8o9l8e8s9t8e8r8i8c.
-.3cho2
-.c3hol4e
-.choles2
-.choles1ter1i
-.ci8g9a9r8e8t8t8e.
-.c2ig
-.ci1ga
-.cig2a2r
-.cigare4t3t2
-.ci8g9a9r8e8t8t8e8s.
-.cigaret4tes
-.ci8n8q8u8e9f8o8i8l.
-.2cin
-.cin1q
-.cinqu2
-.cinque1f
-.cinque1fo
-.cinquefoi2
-.co9a8s8s8o9c8i8a9t8i8v8e.
-.c4oa
-.coa2ss
-.coas1so
-.coasso1ci
-.coasso3c2i1a
-.coassoci4a1t2iv
-.co9g8n8a8c.
-.2cog
-.cog1n4a
-.co9g8n8a8c8s.
-.cogna4c3s2
-.co9k8e8r9n8e8l.
-.c2ok
-.cok1er
-.coker3nel
-.co9k8e8r9n8e8l8s.
-.cokerne2l1s2
-.co8l9l8i8n9e8a9t8i8o8n.
-.col1l
-.coll2i
-.col2lin4
-.col1l4ine
-.collin3ea
-.collinea2t
-.collinea1t2io
-.collineatio2n
-.co8l9u8m8n8s.
-.colu4m1n
-.colum2n1s2
-.co8m9p8a8r9a8n8d.
-.co4m1p
-.compara5
-.com1pa
-.comp2a2r
-.compara2n
-.compar4and
-.co8m9p8a8r9a8n8d8s.
-.comparan2d1s2
-.co8m9p8e8n9d8i8u8m.
-.compendi1u
-.co8m9p8o9n8e8n8t9w8i8s8e.
-.compo2n
-.compo3nen
-.componen1t
-.componentw4
-.componentwis4
-.componentwi2
-.component3w4ise
-.co8m8p9t8r8o8l9l8e8r.
-.comp4tr
-.com2p1t
-.comptrol1l
-.comptrol2le
-.co8m8p9t8r8o8l9l8e8r8s.
-.comptrolle4r1s2
-.co8n9f8o8r8m9a8b8l8e.
-.co2n
-.con3f
-.con1fo
-.confo2r
-.confor1m
-.confor1ma
-.confor2mab
-.conformab2l2
-.co8n9f8o8r8m9i8s8t.
-.confor2mi
-.conform2is
-.co8n9f8o8r8m9i8s8t8s.
-.conformis4t1s2
-.co8n9f8o8r8m9i8t8y.
-.confor3mit
-.conformi1ty
-.co8n9g8r8e8s8s.
-.con3g
-.con1gr
-.congr2e2ss
-.co8n9g8r8e8s8s8e8s.
-.congress1e4s
-.co8n9t8r8i8b9u8t8e.
-.con5t
-.contr4ib
-.co8n9t8r8i8b9u8t8e8s.
-.co8n9t8r8i8b9u8t8e8d.
-.contribu2t1ed
-.co9r8e9l8a9t8i8o8n.
-.core1la
-.corela1t2io
-.corelatio2n
-.co9r8e9l8a9t8i8o8n8s.
-.corelatio2n3s2
-.co9r8e9l8i9g8i8o8n9i8s8t.
-.core1l2i
-.corel2ig
-.corel4igi
-.coreli5g2io
-.coreligion3i
-.coreligio2n
-.coreligion1is
-.co9r8e9l8i9g8i8o8n9i8s8t8s.
-.coreligionis4t1s2
-.co9r8e9o8p9s8i8s.
-.core1o
-.coreo2p1s2
-.coreop1sis
-.co9r8e9s8p8o8n9d8e8n8t.
-.core1sp
-.cores4po2n
-.coresp2ond
-.corespon1de
-.corespon1den
-.coresponden1t
-.co9r8e9s8p8o8n9d8e8n8t8s.
-.coresponden4t4s2
-.co9s8e9c8a8n8t.
-.cos4e
-.cose1ca
-.coseca2n
-.cosecan1t
-.co9t8a8n9g8e8n8t.
-.co4ta2n
-.co1ta
-.cot2ang
-.cotan1gen
-.cotangen1t
-.co8u8r9s8e8s.
-.cou2
-.cou4rs2
-.cour2se
-.cours3e4s
-.co9w8o8r8k9e8r.
-.co4wo2
-.cowork1er
-.co9w8o8r8k9e8r8s.
-.coworke4r1s2
-.cr8a8n8k9c8a8s8e.
-.cra2n
-.cra4nk2
-.crank1ca
-.cr8a8n8k9s8h8a8f8t.
-.cran4k1s2
-.cranks2h
-.cranksha2f
-.cranksha2ft
-.cr8o8c9o9d8i8l8e.
-.cr2oc
-.cro4cod
-.cro1co
-.cr8o8c9o9d8i8l8e8s.
-.crocodiles2
-.cr8o8s8s9h8a8t8c8h.
-.cro2s4s
-.cross2h
-.crossha4tc
-.crosshat4ch
-.cr8o8s8s9h8a8t8c8h8e8d.
-.crosshatche2
-.crosshat4ch4ed
-.cr8o8s8s9o8v8e8r.
-.cros1so
-.cros4sov
-.cr8y8p9t8o9g8r8a8m.
-.cry2p1t
-.cryp1to
-.crypto2gr
-.cr8y8p9t8o9g8r8a8m8s.
-.cryptogra4m1s2
-.cu8f8f9l8i8n8k.
-.c4uf
-.cu4f1f
-.cuff4l2
-.cufflin4
-.cuffl4i4nk2
-.cu8f8f9l8i8n8k8s.
-.cufflin4k1s2
-.cu9n8e8i9f8o8r8m.
-.3cun
-.cu2ne
-.cunei2
-.cunei1fo
-.cuneifo2r
-.cuneifor1m
-.cu8s9t8o8m9i8z9a9b8l8e.
-.1c2us
-.cus1to
-.custom2iz
-.customiza1
-.customiz5a2b
-.customizab2l2
-.cu8s9t8o8m9i8z8e.
-.customi2ze
-.cu8s9t8o8m9i8z8e8s.
-.cu8s9t8o8m9i8z8e8d.
-.da8c8h8s9h8u8n8d.
-.1d2a
-.da2ch4
-.dac4h1s2
-.dach4s2h
-.da8m9s8e8l9f8l8y.
-.da2m2
-.da4m1s2
-.dam5se2l2f
-.damself4l2
-.damself2ly5
-.da8m9s8e8l9f8l8i8e8s.
-.damselfl2ie4
-.da8c8t8y8l9o9g8r8a8m.
-.da2c1t
-.dac1ty
-.dac2tyl
-.dacty3lo
-.dactylo1gr
-.da8c8t8y8l9o9g8r8a8p8h.
-.da8t8a9b8a8s8e.
-.3dat
-.da2ta
-.da2tab
-.da8t8a9b8a8s8e8s.
-.databas1e4s
-.da8t8a9p8a8t8h.
-.dat5ap
-.datap5at
-.data1pa
-.datap4ath
-.da8t8a9p8a8t8h8s.
-.datapa2t4h1s2
-.da8t8e9s8t8a8m8p.
-.dat3est
-.dates1ta
-.datesta4m1p
-.da8t8e9s8t8a8m8p8s.
-.datestam2p1s2
-.de9c8l8a8r9a8b8l8e.
-.de4cl2a2r
-.decla2rab
-.declarab2l2
-.de9f8i8n9i9t8i8v8e.
-.de1f
-.de1fi
-.de2fin
-.def2ini
-.defin2it
-.defini1ti
-.defini1t2iv
-.de9l8e8c9t8a9b8l8e.
-.d5elec
-.dele2c1t
-.delec2ta4b
-.delec1ta
-.delectab2l2
-.de8m8i9s8e8m8i9q8u8a9v8e8r.
-.de4m2is
-.dem4ise
-.demisemi3qua
-.demisemiqu2
-.demisemiqua5v4
-.de8m8i9s8e8m8i9q8u8a9v8e8r8s.
-.demisemiquave4r1s2
-.de9m8o8c9r8a9t8i8s8m.
-.de4mocr
-.democrati2s4m
-.de8m8o8s.
-.demos2
-.de9r8i8v9a9t8i8v8e.
-.der2i4v
-.de4ri1va
-.deri3vat
-.der2iva1t2iv
-.de9r8i8v9a9t8i8v8e8s.
-.derivativ4e2s
-.di8a9l8e8c9t8i8c.
-.1d4i3a
-.di2al
-.di2ale
-.diale2c1t
-.di8a9l8e8c9t8i8c8s.
-.dialecti4c3s2
-.di8a9l8e8c9t8i9c8i8a8n.
-.dialect2i1ci
-.d2i1alecti3c2i1a
-.dialectici2a2n
-.di8a9l8e8c9t8i9c8i8a8n8s.
-.dialecticia2n1s2
-.di9c8h8l8o8r8o9m8e8t8h9a8n8e.
-.d4i2ch
-.dic4h1l4
-.dich2lo
-.dichloro2me
-.dichloro4met
-.dichlorometha2n4
-.di8f9f8r8a8c8t.
-.d1if
-.dif4fr
-.di4f1f
-.diffra2c1t
-.di8f9f8r8a8c8t8s.
-.diffrac4t1s2
-.di8f9f8r8a8c9t8i8o8n.
-.diffrac1t2io
-.diffractio2n
-.di8f9f8r8a8c9t8i8o8n8s.
-.diffractio2n3s2
-.di8r8e8r.
-.d4ir2
-.di1re
-.dir1er4
-.di8r8e9n8e8s8s.
-.dire1nes
-.diren2e2ss
-.di8s9p8a8r9a8n8d.
-.dis1
-.dis1p
-.di2s1pa
-.disp2a2r
-.dispara2n
-.dispar4and
-.di8s9p8a8r9a8n8d8s.
-.disparan2d1s2
-.di8s9t8r8a8u8g8h8t9l8y.
-.d4is3t
-.dist4r
-.dis1tra
-.distraugh3
-.distraugh2tl
-.distraught1ly
-.di8s9t8r8i8b9u8t8e.
-.distr4ib
-.di8s9t8r8i8b9u8t8e8s.
-.di8s9t8r8i8b9u8t8e8d.
-.distribu2t1ed
-.do8u9b8l8e9s8p8a8c8e.
-.dou2
-.dou3b2l2
-.dou5ble1sp
-.doubles2
-.double2s1pa
-.doublespa4ce
-.do8u9b8l8e9s8p8a8c9i8n8g.
-.doublesp4a2ci
-.doublespa2c1in
-.doublespac1ing
-.do8l8l9i8s8h.
-.dol1l
-.doll2i
-.dollis2h
-.dr8i8f8t9a8g8e.
-.1dr
-.dr4i2ft
-.drif1ta
-.dr8i8v9e8r8s.
-.dr2iv
-.drive4r1s2
-.dr8o8m9e9d8a8r8y.
-.dro2me
-.dro2med
-.drom2e2d2a
-.drome4dary
-.dromed2a2r
-.dr8o8m9e9d8a8r8i8e8s.
-.dromedar1i
-.dromedar2ie4
-.du9o8p9o9l8i8s8t.
-.duopol2i
-.du9o8p9o9l8i8s8t8s.
-.duopolis4t1s2
-.du9o8p9o8l8y.
-.duopo2ly
-.dy8s9l8e8x8i8a.
-.d2y
-.dys1l2
-.dys2le
-.dyslex3i
-.dyslex2i5a
-.dy8s9l8e8c9t8i8c.
-.dysle2c1t
-.ea8s8t9e8n8d9e8r8s.
-.east3
-.eas3ten
-.eas3tend
-.easten1de
-.eastende4r5s2
-.ec8o9n8o8m9i8c8s.
-.e1co
-.eco2n
-.eco3nomic
-.economi4c3s2
-.ec8o8n9o9m8i8s8t.
-.econom2is
-.ec8o8n9o9m8i8s8t8s.
-.economis4t1s2
-.ei9g8e8n9c8l8a8s8s.
-.ei2
-.e2ig2
-.ei1gen
-.eigen1c4l4
-.eigencla2ss
-.ei9g8e8n9c8l8a8s8s8e8s.
-.eigenclass1e4s
-.ei9g8e8n9v8a8l9u8e.
-.eigen1v2
-.eigen1va
-.eigenval1u
-.ei9g8e8n9v8a8l9u8e8s.
-.el8e8c8t8r8o9m8e8c8h8a8n9i9c8a8l.
-.5elec
-.ele2c1t
-.electro2me
-.electrome2ch
-.electrome5cha4n1ic
-.electromecha2n
-.electromechani1ca
-.el8e8c8t8r8o9m8e8c8h8a8n8o9a8c8o8u8s8t8i8c.
-.electromechano4
-.electromechan4oa
-.electromechanoa1co
-.electromechanoacou2
-.electromechanoaco2us
-.electromechanoacoust2i
-.electromechanoacous1tic
-.el8i8t9i8s8t.
-.el2i
-.el1it
-.eli1ti
-.el4itis
-.el8i8t9i8s8t8s.
-.elitis4t1s2
-.en9t8r8e9p8r8e9n8e8u8r.
-.en1t
-.entrepr2
-.entrepren4eu
-.en9t8r8e9p8r8e9n8e8u8r9i8a8l.
-.entrepreneur2i3a
-.entrepreneuri2al
-.ep9i9n8e8p8h9r8i8n8e.
-.epi2n
-.ep2ine
-.epinep4hr4
-.ep2inephr2in4e
-.eq8u8i9v8a8r8i9a8n8t.
-.equ2iv3
-.equi1va
-.equiv2a2r
-.equivar1i
-.equivar3i2a2n
-.equivar2i3a
-.equivar4ian4t
-.eq8u8i9v8a8r8i9a8n8c8e.
-.equivar4ianc
-.et8h9a8n8e.
-.etha2n4
-.et8h9y8l9e8n8e.
-.ev8e8r9s8i9b8l8e.
-.ev1er
-.eve4r1s2
-.ever1si
-.ever4si4b
-.eversi1b2l2
-.ev8e8r8t.
-.ev8e8r8t8s.
-.ever4t1s2
-.ev8e8r8t9e8d.
-.ever2t1ed
-.ev8e8r8t9i8n8g.
-.ever1ti
-.ever2t1in
-.ex9q8u8i8s9i8t8e.
-.exqu2
-.exq2ui2
-.exquis2ite
-.ex9t8r8a9o8r9d8i9n8a8r8y.
-.ex1t2
-.ex1tra
-.extr4ao
-.extraord2i
-.extraord1in4
-.extraor1di1na
-.extraordin2a2r
-.fa8l8l9i8n8g.
-.1fa
-.fal1l
-.fall2i
-.fal2lin4
-.fe8r8m8i9o8n8s.
-.fer1
-.fer3m4
-.fer4m2io
-.fermio2n
-.fermio2n3s2
-.fi9n8i8t8e9l8y.
-.1fi
-.2fin
-.f2ini
-.fin2it
-.fin2ite
-.finite1ly
-.fl8a9g8e8l9l8u8m.
-.f4l2
-.flag5el1l
-.fl8a9g8e8l9l8a.
-.flag4ella
-.fl8a8m9m8a9b8l8e8s.
-.flam1m
-.flam1ma
-.flam2mab
-.flammab2l2
-.flammables2
-.fl8e8d8g9l8i8n8g.
-.fledgl2
-.fl8o8w9c8h8a8r8t.
-.flow2ch
-.flowch2a2r
-.fl8o8w9c8h8a8r8t8s.
-.flowchar4t1s2
-.fl8u8o8r8o9c8a8r9b8o8n.
-.flu3o
-.fluo3r
-.fluor2oc
-.fluoro1ca
-.fluoroc2a2r
-.fluorocar1b
-.fluorocarb4o
-.fluorocarbo2n
-.fo8r9m8i9d8a9b8l8e.
-.for2mi
-.formi1d4a
-.form2id
-.formi2d3a4b
-.formidab2l2
-.fo8r9m8i9d8a9b8l8y.
-.formidab1ly
-.fo8r9s8y8t8h9i8a.
-.fo4rs2
-.fors4y
-.forsyth2i1a
-.fo8r8t8h9r8i8g8h8t.
-.fort4hr4
-.forthr2ig
-.fr8e8e9l8o8a8d8e8r.
-.freel4oa
-.freeloa2d3
-.fr8e8e9l8o8a8d8e8r8s.
-.freeloade4r5s2
-.fr8i8e8n8d9l8i8e8r.
-.fri2
-.fr2ie4
-.friendl2ie4
-.fr8i9v8o8l9i8t8y.
-.fr2iv
-.frivol2i
-.frivol1it
-.frivoli1ty
-.fr8i9v8o8l9i9t8i8e8s.
-.frivoli1ti
-.frivolit2ie4
-.fr8i8v9o9l8o8u8s.
-.frivolou2
-.frivolo2us
-.ga9l8a8c9t8i8c.
-.gala2c1t
-.ga8l9a8x8y.
-.ga8l9a8x9i8e8s.
-.galax3i
-.galax2ie4
-.ga8s9o8m9e9t8e8r.
-.ga1so
-.ga3som
-.gaso2me
-.gaso4met
-.gasome1te
-.ge9o9d8e8s9i8c.
-.geodes2
-.geode1si
-.geode2sic
-.ge9o9d8e8t9i8c.
-.geode1t
-.geodet1ic
-.ge8o9m8e8t9r8i8c.
-.ge3om
-.geo2me
-.geo4met
-.geom4etr
-.geo5met3ric
-.ge8o9m8e8t9r8i8c8s.
-.geome4tri4c3s2
-.ge9o9s8t8r8o8p8h8i8c.
-.geos4
-.geost4r
-.geostr2oph
-.geostroph1ic
-.ge8o9t8h8e8r9m8a8l.
-.ge4ot
-.ge4oth
-.geoth2e
-.geother3m4
-.geother1ma
-.ge9o8t9r8o9p8i8s8m.
-.geotropi2s1m
-.gn8o9m8o8n.
-.g1no
-.gno4mo
-.gno4mo2n
-.gn8o9m8o8n8s.
-.gnomo2n3s2
-.gr8a8n8d9u8n8c8l8e.
-.1gr
-.gra2n2
-.gr4and
-.gran1du
-.grandu4n
-.grandun1c4l4
-.gr8a8n8d9u8n8c8l8e8s.
-.granduncles2
-.gr8i8e8v9a8n8c8e.
-.gr2ie4
-.grie1va
-.grieva2n
-.gr8i8e8v9a8n8c8e8s.
-.gr8i8e8v9o8u8s.
-.grievou2
-.grievo2us
-.gr8i8e8v9o8u8s9l8y.
-.grievous1l2
-.grievous1ly
-.ha8i8r9s8t8y8l8e.
-.hai2
-.ha4ir
-.hai4rs2
-.hairs2ty
-.hairs2tyl
-.ha8i8r9s8t8y8l8e8s.
-.hairstyles2
-.ha8i8r9s8t8y8l9i8s8t.
-.ha8i8r9s8t8y8l9i8s8t8s.
-.hairstylis4t1s2
-.ha8l8f9s8p8a8c8e.
-.ha2lf
-.hal2f3s
-.half2s1pa
-.halfspa4ce
-.ha8l8f9s8p8a8c8e8s.
-.ha8l8f9w8a8y.
-.ha8r9b8i8n9g8e8r.
-.h2a2r
-.har1b
-.harbi2
-.har2bin
-.harb4inge
-.ha8r9b8i8n9g8e8r8s.
-.harbinge4r1s2
-.ha8r9l8e9q8u8i8n.
-.har4le4
-.har1l
-.harle1q
-.harlequ2
-.harleq2ui2
-.harlequi4n
-.ha8r9l8e9q8u8i8n8s.
-.harlequ2i2n1s2
-.ha8t8c8h9e8r8i8e8s.
-.ha4tc
-.hat4ch
-.hatche2
-.hatcher1i
-.hatcher2ie4
-.he8m8i9d8e8m8i9s8e8m8i9q8u8a9v8e8r.
-.hem2id
-.hemid4em
-.hemide4m2is
-.hemidem4ise
-.hemidemisemi3qua
-.hemidemisemiqu2
-.hemidemisemiqua5v4
-.he8m8i9d8e8m8i9s8e8m8i9q8u8a9v8e8r8s.
-.hemidemisemiquave4r1s2
-.he9m8o9g8l8o9b8i8n.
-.hemo4g
-.he1mo
-.hemo4gl2
-.hemo3glo
-.hemoglo1bi
-.hemoglo2bin
-.he9m8o9p8h8i8l9i8a.
-.hem2oph
-.hemoph4il2
-.hemophil1i
-.hemophil3i1a
-.he9m8o9p8h8i8l9i8a8c.
-.he9m8o9p8h8i8l9i8a8c8s.
-.hemophilia4c3s2
-.he8m8o9r8h8e9o8l9o8g8y.
-.hemo2r
-.hemorh4
-.hemorhe3ol
-.hemorheol1o1gy
-.he9p8a8t9i8c.
-.hep5
-.he2pa
-.hepat1ic
-.he8r9m8a8p8h9r8o9d8i8t8e.
-.her3m4
-.her1ma
-.her4map
-.hermap4hr4
-.hermaphrod2ite
-.he8r9m8a8p8h9r8o9d8i8t9i8c.
-.hermaphrod2i1ti
-.hermaphrod4i2tic
-.he9r8o8e8s.
-.hero4e
-.he8x8a9d8e8c9i9m8a8l.
-.hex1a
-.hexa2d
-.hexade1c2i
-.hexade2c3im
-.hexadeci1ma
-.ho9l8o9n8o9m8y.
-.holo2n
-.holon3o3my
-.ho9m8e8o9m8o8r9p8h8i8c.
-.ho2me3
-.homeo1mo
-.homeomo2r
-.homeomor1p
-.homeomorp4h4
-.homeomorph1ic
-.ho9m8e8o9m8o8r9p8h8i8s8m.
-.homeomorphi2s1m
-.ho9m8o9t8h8e8t8i8c.
-.ho1mo
-.hom4oth3
-.homoth2e
-.homo3the4t
-.homothet1ic
-.ho8r8s8e9r8a8d9i8s8h.
-.hor4se
-.ho4rs2
-.horser1a
-.horsera2d
-.horser2adi
-.horseradis1
-.horseradis2h
-.ho8t9b8e8d.
-.ho2t1b
-.hot4be2d
-.ho8t9b8e8d8s.
-.hotbe2d1s2
-.hy9d8r8o9t8h8e8r9m8a8l.
-.hy1d
-.hy1dr
-.hydro4th2e
-.hydr4oth
-.hydrother3m4
-.hydrother1ma
-.hy9p8o9t8h8a8l9a9m8u8s.
-.hy3po
-.hyp4ot
-.hyp4oth
-.hypotha3la
-.hypothala3m
-.hypothala1mu
-.hypothalam2us
-.id8e8a8l8s.
-.ide3a4l
-.idea2l1s2
-.id8e8o9g8r8a8p8h8s.
-.ideo2g
-.ideo1gr
-.ideogra4p4h1s2
-.id8i8o9s8y8n9c8r8a8s8y.
-.i2di
-.i1d3io
-.idi4os
-.idios4y
-.idiosyn1cr
-.idiosyncr2as
-.idiosyncras4y
-.id8i8o9s8y8n9c8r8a9s8i8e8s.
-.idiosyncras2ie4
-.id8i8o9s8y8n9c8r8a8t8i8c.
-.idiosyn5crat1ic
-.id8i8o9s8y8n9c8r8a8t9i9c8a8l9l8y.
-.idiosyncrati1ca
-.idiosyncratical1l
-.idiosyncratical1ly
-.ig9n8i8t9e8r.
-.2ig
-.ig1ni
-.ign2it
-.ign2ite
-.ig9n8i8t9e8r8s.
-.ignite4r1s2
-.ig9n8i9t8o8r.
-.ign3itor
-.igni1to
-.ig8n8o8r8e9s8p8a8c8e8s.
-.ig1no
-.ignore1sp
-.ignore2s1pa
-.ignorespa4ce
-.im9p8e8d9a8n8c8e.
-.im2p2ed
-.imp2e2d2a
-.impeda2n
-.im9p8e8d9a8n8c8e8s.
-.in9d8u9b8i9t8a9b8l8e.
-.4ind
-.in1du
-.indu1b4i
-.indubi2t
-.indubi1ta
-.indubi2tab
-.indubitab2l2
-.in9f8i8n9i8t8e9l8y.
-.in3f
-.in1fi
-.in2fin
-.inf2ini
-.infin2it
-.infin2ite
-.infinite1ly
-.in9f8i8n9i9t8e8s9i9m8a8l.
-.infinit4es
-.infinite1si
-.infinite2s5im
-.infinitesi1ma
-.in9f8r8a9s8t8r8u8c9t8u8r8e.
-.infr2as
-.infras1t4r
-.infrastru2c1t
-.infrastructu4r
-.infrastruc1tu
-.infrastruc3ture
-.in9f8r8a9s8t8r8u8c9t8u8r8e8s.
-.in9s8t8a8l8l9e8r.
-.ins2ta2l
-.ins1ta
-.instal1l
-.instal2le
-.in9s8t8a8l8l9e8r8s.
-.installe4r1s2
-.in9t8e8r9d8i8s9c8i9p8l8i9n8a8r8y.
-.in1t
-.in5ter3d
-.interd2i
-.interdis1
-.interd2is1c
-.interdis1ci
-.interdisc2ip
-.interdisci1p2l2
-.interdiscipli4n
-.interdiscipl4i1na
-.interdisciplin2a2r
-.in9t8e8r9g8a9l8a8c9t8i8c.
-.interg2
-.inter1ga
-.intergala2c1t
-.in9u8t8i8l8e.
-.in1u
-.in4u1t2i
-.in9u8t8i8l9i9t8y.
-.inutil1i
-.inut2il1it
-.inutili1ty
-.ir9r8e9d8u8c9i8b8l8e.
-.ir2r2ed
-.irre1du
-.irredu2c
-.irreduci4b
-.irredu1ci
-.irreduci1b2l2
-.ir9r8e9d8u8c9i8b8l8y.
-.irreducib1ly
-.ir9r8e8v9o9c8a9b8l8e.
-.irrev2
-.irre5voc
-.irrevo1ca
-.irrevoca1b2l2
-.is8o8t9r8o8p8y.
-.i2so
-.isotropy5
-.is8o9t8r8o8p9i8c.
-.isotrop3ic
-.it8i8n9e8r9a8r8y.
-.i1ti
-.i2t1in
-.it2ine
-.itin4er4a2r
-.itin1er
-.itiner1a
-.it8i8n9e8r9a8r9i8e8s.
-.itinerar1i
-.itinerar2ie4
-.je9r8e9m8i9a8d8s.
-.1je
-.jerem2i3a
-.jeremia2d
-.jeremia2d1s2
-.ke8y9n8o8t8e.
-.ke8y9n8o8t8e8s.
-.keyno4tes
-.ke8y9s8t8r8o8k8e.
-.keys4
-.keys1t
-.keyst4r
-.keystr2ok2
-.ke8y9s8t8r8o8k8e8s.
-.keystrokes4
-.ki8l8n9i8n8g.
-.k1i
-.k4i2l1n2
-.kiln1in
-.kilnin4g
-.la8c9i9e8s8t.
-.l4a2ci4
-.la3c2ie4
-.laci1est
-.la8m9e8n9t8a9b8l8e.
-.la1men
-.la3men1t
-.lamen2ta4b
-.lamen1ta
-.lamentab2l2
-.la8n8d9s8c8a8p9e8r.
-.3l4and
-.la2n
-.lan2d1s2
-.landsca4p
-.lands1ca
-.landsca5per
-.la8n8d9s8c8a8p9e8r8s.
-.landscape4r1s2
-.la8r9c8e9n8y.
-.l2a2r
-.lar1c
-.lar2ce
-.lar1cen4
-.la8r9c8e9n9i8s8t.
-.lar4ceni
-.le8a8f9h8o8p9p8e8r.
-.le2a
-.lea2f
-.lea4fh
-.leafho4p1p
-.leafhop2pe
-.leafhop3per
-.le8a8f9h8o8p9p8e8r8s.
-.leafhoppe4r1s2
-.le8t9t8e8r9s8p8a8c9i8n8g.
-.le4t3t2
-.lette4r1s2
-.letter1sp
-.letter2s1pa
-.lettersp4a2ci
-.letterspa2c1in
-.letterspac1ing
-.li8f8e9s8p8a8n.
-.life1sp
-.life2s1pa
-.lifespa4n
-.li8f8e9s8p8a8n8s.
-.lifespa2n1s2
-.li8f8e9s8t8y8l8e.
-.lifes2ty
-.lifes2tyl
-.li8f8e9s8t8y8l8e8s.
-.lifestyles2
-.li8g8h8t9w8e8i8g8h8t.
-.3ligh
-.lightw4
-.lightwei2
-.l2ightwe2ig2
-.li8m9o8u9s8i8n8e8s.
-.li4mo
-.li3mo2us
-.limou2
-.limou2s1in
-.limous2ine
-.limousi1nes
-.li8n8e9b8a8c8k8e8r.
-.1l4ine
-.lin2e2b
-.lineback1
-.lineback1er
-.li8n8e9s8p8a8c8i8n8g.
-.li1nes
-.li4ne1sp
-.line2s1pa
-.linesp4a2ci
-.linespa2c1in
-.linespac1ing
-.li9o8n9e8s8s.
-.lio2n
-.lio1nes
-.lion2e2ss
-.li8t8h9o9g8r8a8p8h8e8d.
-.l2ith
-.litho4g
-.litho1gr
-.lithograph4ed
-.li8t8h9o9g8r8a8p8h8s.
-.lithogra4p4h1s2
-.lo9b8o8t9o8m8y.
-.lobo4to
-.loboto3my
-.lo9b8o8t9o8m9i8z8e.
-.lobotom2iz
-.lobotomi2ze
-.lo8g8e8s.
-.lo1ge
-.lo8n8g9e8s8t.
-.5long
-.lo2n
-.lo9q8u8a8c9i8t8y.
-.lo1q
-.loqu2
-.loquac4
-.loqu4a2ci
-.loqua2c1it
-.loquaci1ty
-.lo8v8e9s8t8r8u8c8k.
-.4lov
-.lov4e2s
-.lov2est4r
-.lovestruc5
-.lovestruck1
-.ma8c8r8o9e8c8o9n8o8m8i8c8s.
-.macro4e
-.macroe1co
-.macroeco2n
-.macroeco3nomic
-.macroeconomi4c3s2
-.ma8l9a9p8r8o8p9i8s8m.
-.malapr2
-.malapropi2s1m
-.ma8l9a9p8r8o8p9i8s8m8s.
-.malaprop4is4m1s2
-.ma8n9s8l8a8u8g8h9t8e8r.
-.ma2n1s2
-.man2s1l2
-.manslaugh3
-.ma8n9u9s8c8r8i8p8t.
-.man2us
-.manusc2
-.manuscri2
-.manuscr2ip
-.manuscri2p1t
-.ma8r9g8i8n9a8l.
-.marg2
-.margi4n
-.margi1na
-.ma8t8h9e9m8a9t8i9c8i8a8n.
-.m4ath3
-.math5em
-.math2e
-.1mathe1ma
-.mathemat1ic
-.mathemat2i1ci
-.mathemati3c2i1a
-.mathematici2a2n
-.ma8t8h9e9m8a9t8i9c8i8a8n8s.
-.mathematicia2n1s2
-.ma8t8t8e8s.
-.mat5te
-.ma4t3t2
-.mat4tes
-.me8d9i8c9a8i8d.
-.2med
-.m4edi
-.med3i1ca
-.medicai2
-.medica2id
-.me8d8i9o8c8r8e.
-.me1d2io
-.mediocre3
-.me8d8i9o8c9r8i9t8i8e8s.
-.medi5ocrit
-.mediocri2
-.medio5cri1ti
-.mediocrit2ie4
-.me8g8a9l8i8t8h.
-.me2g
-.m4egal
-.me1ga
-.me3gal1i
-.megal1it
-.megal2ith
-.me8g8a9l8i8t8h8s.
-.megali2t4h1s2
-.me8t8a9b8o8l9i8c.
-.me4ta
-.me2ta4b
-.metabol3ic
-.metabol2i
-.me9t8a8b9o9l8i8s8m.
-.metaboli2s1m
-.me9t8a8b9o9l8i8s8m8s.
-.metabol4is4m1s2
-.me9t8a8b9o9l8i8t8e.
-.metabo5l2ite
-.metabol1it
-.me9t8a8b9o9l8i8t8e8s.
-.metabolit4es
-.me8t8a9l8a8n9g8u8a8g8e.
-.met3a2l
-.meta5la
-.metala2n
-.metal2ang
-.metalan1gu
-.metalangu4a
-.me8t8a9l8a8n9g8u8a8g8e8s.
-.me8t8a9p8h8o8r9i8c.
-.metapho4r
-.me8t8h9a8n8e.
-.metha2n4
-.me9t8r8o8p9o9l8i8s.
-.m4etr
-.metropol2i
-.me9t8r8o8p9o9l8i8s8e8s.
-.metropol4ise
-.metropolis1e4s
-.me8t9r8o9p8o8l9i9t8a8n.
-.metropol1it
-.metropoli3ta2n
-.metropoli1ta
-.me8t9r8o9p8o8l9i9t8a8n8s.
-.metropolita2n1s2
-.mi8c8r8o9e8c8o9n8o8m8i8c8s.
-.m4i1cr
-.micro4e
-.microe1co
-.microeco2n
-.microeco3nomic
-.microeconomi4c3s2
-.mi9c8r8o9f8i8c8h8e.
-.micro2fi
-.microf4i2ch
-.microfiche2
-.mi9c8r8o9f8i8c8h8e8s.
-.microfich1es
-.mi8c8r8o9o8r8g8a8n9i8s8m.
-.microo2
-.microorg2
-.microor1ga
-.microorgan5is
-.microorga2n
-.microorgani2s1m
-.mi8c8r8o9o8r8g8a8n9i8s8m8s.
-.microorgan4is4m1s2
-.mi8l8l9a8g8e.
-.m4il1l
-.mi8l9l8i9l8i8t8e8r.
-.mill2i
-.mil4l4i4l
-.millil1i
-.mill2il1it
-.millil2ite
-.mi8m8e8o9g8r8a8p8h8e8d.
-.mimeo2g
-.mimeo1gr
-.mimeograph4ed
-.mi8m8e8o9g8r8a8p8h8s.
-.mimeogra4p4h1s2
-.mi8m9i8c9r8i8e8s.
-.mim1i
-.mim4i1cr
-.mimicri2
-.mimicr2ie4
-.mi8n9i8s.
-.m2ini
-.min1is
-.mi8n8i9s8y8m9p8o9s8i8u8m.
-.minis4y
-.minisy4m1p
-.minisym1pos
-.minisympo5si4u
-.mi8n8i9s8y8m9p8o9s8i8a.
-.minisympos2i1a
-.mi9n8u8t9e8r.
-.m4in1u
-.mi9n8u8t9e8s8t.
-.mi8s9c8h8i8e9v8o8u8s9l8y.
-.m2is1c
-.mis3ch2
-.misch2ie4
-.mischievou2
-.mischievo2us
-.mischievous1l2
-.mischievous1ly
-.mi9s8e8r8s.
-.m4ise
-.mis3er
-.mise4r1s2
-.mi9s8o8g9a9m8y.
-.mi2so
-.miso1ga
-.miso2gam
-.mo8d9e8l9l8i8n8g.
-.mo2d1
-.model1l
-.modell2i
-.model2lin4
-.mo8l9e9c8u8l8e.
-.mole1cu
-.mole4cul
-.molecul4e
-.mo8l9e9c8u8l8e8s.
-.molecules2
-.mo8n9a8r8c8h8s.
-.mo1n1a
-.monar3c
-.mon2a2r
-.monar2ch
-.monarc4h1s2
-.mo8n8e8y9l8e8n9d8e8r.
-.moneylen1de
-.mo8n8e8y9l8e8n9d8e8r8s.
-.moneylende4r5s2
-.mo8n8o9c8h8r8o8m8e.
-.mono2ch4
-.monoc4hr4
-.monochro2me
-.mo8n8o9e8n9e8r9g8e8t8i8c.
-.mo3noe
-.monoen1er
-.monoenerg2
-.monoener3get
-.monoenerget1ic
-.mo8n9o8i8d.
-.monoi2
-.mono2id
-.mo8n8o9p8o8l8e.
-.mo4nop
-.mo8n8o9p8o8l8e8s.
-.monopoles2
-.mo9n8o8p9o8l8y.
-.monopo2ly
-.mo8n8o9s8p8l8i8n8e.
-.monos1p2l2
-.monospli4n
-.monosp1l4ine
-.mo8n8o9s8p8l8i8n8e8s.
-.monospli1nes
-.mo8n8o9s8t8r8o8f8i8c.
-.monos5t
-.monost4r
-.monostro2fi
-.mo9n8o8t9o9n8i8e8s.
-.mono1to
-.mo2noto2n
-.monoton2ie4
-.mo9n8o8t9o9n8o8u8s.
-.mono4tono
-.monoto1nou2
-.monotono2us
-.mo9r8o8n9i8s8m.
-.moro5n4is
-.moro2n
-.moroni2s1m
-.mo8s9q8u8i9t8o.
-.mos2
-.mosqu2
-.mosq2ui2
-.mosqui1to
-.mo8s9q8u8i9t8o8s.
-.mosquitos2
-.mo8s9q8u8i9t8o8e8s.
-.mu8d9r8o8o8m.
-.mu1dr
-.mud1room
-.mudroo2
-.mu8d9r8o8o8m8s.
-.mudroo4m1s2
-.mu8l9t8i9f8a8c9e8t8e8d.
-.5mu4lt
-.mul1ti3
-.multif2
-.multi1fa
-.multifa4ce
-.multifacet4
-.multiface2t1ed
-.mu8l9t8i9p8l8i8c9a8b8l8e.
-.mult2ip
-.multi1p2l2
-.multipli1ca
-.multiplica1b2l2
-.mu8l8t8i9u8s8e8r.
-.multi4u
-.multi2us
-.ne8o9f8i8e8l8d8s.
-.3neo
-.ne5of
-.neo2fi
-.neof2ie4
-.neofie2ld3
-.neofiel2d1s2
-.ne8o9n8a8z8i.
-.neo2n
-.neo1n1a
-.neona2z1i
-.ne8o9n8a8z8i8s.
-.neonaz4is
-.ne8p8h9e8w8s.
-.nephe4
-.ne8p8h9r8i8t8e.
-.nep4hr4
-.nephr2ite
-.ne8p8h9r8i8t8i8c.
-.nephr4i2t3ic
-.nephri1ti
-.ne8w9e8s8t.
-.ne4w
-.newest3
-.ne8w8s9l8e8t9t8e8r.
-.news4l2
-.news2le
-.newsle4t3t2
-.ne8w8s9l8e8t9t8e8r8s.
-.newslette4r1s2
-.ni8t8r8o9m8e8t8h9a8n8e.
-.n2it
-.ni3tr
-.nitro2me
-.nitro4met
-.nitrometha2n4
-.no9n8a8m8e.
-.no4n
-.no1n1a
-.no8n9a8r9i8t8h9m8e8t9i8c.
-.nonar3i
-.non2a2r
-.nonar2ith
-.nonarit4h1m
-.nonarithmet4
-.nonarithmet1ic
-.no8n9e8m8e8r9g8e8n8c8y.
-.none1me
-.nonemerg2
-.nonemer1gen
-.nonemergen1cy
-.no8n9e8q8u8i9v8a8r8i9a8n8c8e.
-.none2q
-.nonequ2
-.noneq2ui2
-.nonequ2iv3
-.nonequi1va
-.nonequiv2a2r
-.nonequivar1i
-.nonequivar3i2a2n
-.nonequivar2i3a
-.nonequivar4ianc
-.no8n8e9t8h8e9l8e8s8s.
-.noneth2e
-.nonethe1les2
-.nonethe3l2e2ss
-.no8n9e8u8c8l8i8d9e8a8n.
-.non4eu
-.noneu1c4l4
-.noneucl2id
-.noneuclidea2n
-.no8n9i8s8o9m8o8r9p8h8i8c.
-.non5i
-.non1is
-.noni2so
-.noni3som
-.noniso1mo
-.nonisomo2r
-.nonisomor1p
-.nonisomorp4h4
-.nonisomorph1ic
-.no8n9p8s8e8u8d8o9c8o8m9p8a8c8t.
-.non1p4
-.non2p1s2
-.nonp2se
-.nonps4eu
-.nonpseu1do
-.nonpseudo1co
-.nonpseudoco4m1p
-.nonpseudocom1pa
-.nonpseudocompa2c4t
-.no8n9s8m8o8o8t8h.
-.no2n3s2
-.non2s3m
-.nons1mo
-.nonsmoo2
-.nonsmo4oth
-.no8n9u8n8i9f8o8r8m.
-.no3nu4n
-.nonu1ni
-.nonuni1fo
-.nonunifo2r
-.nonunifor1m
-.no8n9u8n8i9f8o8r8m9l8y.
-.nonunifor4m1l
-.nonuniform1ly
-.no8r9e8p9i9n8e8p8h9r8i8n8e.
-.nore5pi2n
-.norep2ine
-.norepinep4hr4
-.norep2inephr2in4e
-.no8t9w8i8t8h9s8t8a8n8d9i8n8g.
-.notw4
-.notwi2
-.notw2ith3
-.notwi2t4h1s2
-.notwith5st4and
-.notwiths1ta
-.notwithsta2n
-.notwithstand1in
-.nu9c8l8e8o9t8i8d8e.
-.nucle3
-.nu1c4l4
-.nucle4ot
-.nucleot2id
-.nu9c8l8e8o9t8i8d8e8s.
-.nucleotide4s2
-.nu8t9c8r8a8c8k9e8r.
-.nu4tc
-.nutcrack1
-.nutcrack1er
-.nu8t9c8r8a8c8k9e8r8s.
-.nutcracke4r1s2
-.oe8r9s8t8e8d8s.
-.o3er
-.oe4r1s2
-.oers4t1ed
-.oerste2d1s2
-.of8f9l8i8n8e.
-.o4f1f
-.off4l2
-.offlin4
-.off1l4ine
-.of8f9l8o8a8d.
-.offl4oa
-.offloa2d3
-.of8f9l8o8a8d8s.
-.offloa2d1s2
-.of8f9l8o8a8d8e8d.
-.offloa2d1ed
-.ol8i9g8o8p9o9l8i8s8t.
-.ol2i
-.ol2ig
-.oli2go
-.ol2igopol2i
-.ol8i9g8o8p9o9l8i8s8t8s.
-.oligopolis4t1s2
-.ol8i9g8o8p9o8l8y.
-.oligopo2ly
-.ol8i9g8o8p9o8l9i8e8s.
-.oligopol2ie4
-.op9e8r9a8n8d.
-.op1er
-.3oper1a
-.op4er4and
-.opera2n
-.op9e8r9a8n8d8s.
-.operan2d1s2
-.or8a8n8g9u8t8a8n.
-.ora2n
-.or2ang
-.oran1gu
-.oran4gu4t
-.orangu1ta
-.ora2nguta2n
-.or8a8n8g9u8t8a8n8s.
-.oranguta2n1s2
-.or9t8h8o9d8o8n9t8i8s8t.
-.ortho2do4
-.orthodo2n
-.orthodon3t4i
-.orthodon1t
-.or9t8h8o9d8o8n9t8i8s8t8s.
-.orthodontis4t1s2
-.or9t8h8o9k8e8r9a9t8o8l9o8g8y.
-.orth2ok
-.orthok1er
-.orthoker1a
-.orthokera1to
-.orthokeratol1o1gy
-.or8t8h8o9n8i8t8r8o9t8o8l8u8e8n8e.
-.ortho2n
-.orthon2it
-.orthoni3tr
-.orthonitro1to
-.orthonitrotolu3en
-.orthonitrotolu4ene
-.ov8e8r9v8i8e8w.
-.overv2ie4
-.ov8e8r9v8i8e8w8s.
-.ox9i8d9i8c.
-.ox3i
-.oxi5di
-.ox2id
-.pa8d9d8i8n8g.
-.1pa
-.p4a2d
-.pad4d1in
-.pad1d4
-.pa8i8n9l8e8s8s9l8y.
-.p4ai2
-.pa4i4n4
-.pa4i4n1l
-.painles2
-.pain3l2e2ss
-.painles4s1l2
-.painless1ly
-.pa8l9e8t8t8e.
-.p4al
-.p2ale
-.pale4t3t2
-.pa8l9e8t8t8e8s.
-.palet4tes
-.pa8r9a9b8o8l8a.
-.p2a2r
-.pa2rab
-.parabo1la
-.pa8r9a9b8o8l9i8c.
-.parabol3ic
-.parabol2i
-.pa9r8a8b9o9l8o8i8d.
-.paraboloi2
-.parabolo2id
-.pa8r9a9d8i8g8m.
-.para2d
-.par2adi
-.parad2ig
-.paradig1m
-.pa8r9a9d8i8g8m8s.
-.paradig4m1s2
-.pa8r8a9c8h8u8t8e.
-.para2ch
-.parachu4t
-.pa8r8a9c8h8u8t8e8s.
-.pa8r8a9d8i9m8e8t8h8y8l9b8e8n8z8e8n8e.
-.parad4imet
-.paradimethy2l1b
-.paradimethylb4e4n3z
-.paradimethylben2ze
-.paradimethylbenze4n
-.pa8r8a9f8l8u8o8r8o9t8o8l8u8e8n8e.
-.para2f
-.paraf4l2
-.paraflu3o
-.parafluo3r
-.parafluoro1to
-.parafluorotolu3en
-.parafluorotolu4ene
-.pa8r8a9g8r8a8p8h9e8r.
-.para1gr
-.parag5ra3ph4er
-.pa8r8a9l8e9g8a8l.
-.par3al
-.par2ale
-.paral4egal
-.parale1ga
-.pa8r9a8l9l8e8l9i8s8m.
-.paral1l
-.paral2le
-.paral3lel
-.parallel2i
-.paralle2lis
-.paralleli2s1m
-.pa8r8a9m8a8g9n8e8t9i8s8m.
-.par4a1ma
-.param3ag
-.para5mag1n
-.paramagneti2s4m
-.pa8r8a9m8e8d8i8c.
-.para2med
-.param4edi
-.pa8r8a9m8e8t8h8y8l9a8n8i8s8o8l8e.
-.param3et
-.paramethy3la
-.paramethyla2n
-.paramethylani2so
-.pa9r8a8m9e9t8r8i8z8e.
-.param4etr
-.parametri2ze
-.pa8r8a9m8i8l9i9t8a8r8y.
-.par2ami
-.paramil1i
-.param2il1it
-.paramili1ta
-.paramilit2a2r
-.pa8r8a9m8o8u8n8t.
-.para2mo
-.paramou2
-.paramoun1t
-.pa8t8h9o9g8e8n9i8c.
-.p4ath
-.pat4ho
-.patho4g
-.patho1ge4
-.patho1gen
-.pe8e8v9i8s8h.
-.p4ee
-.pee1vi
-.peevis2h
-.pe8e8v9i8s8h9n8e8s8s.
-.peevis2h1n
-.peevish1nes
-.peevishn2e2ss
-.pe8n9t8a9g8o8n.
-.pen1t
-.pen1ta
-.penta2go
-.pentago2n2
-.pe8n9t8a9g8o8n8s.
-.pentago2n3s2
-.pe9t8r8o9l8e9u8m.
-.petrol4eu
-.ph8e9n8o8m9e9n8o8n.
-.ph4e3no
-.phe2n
-.pheno2me
-.pheno1men
-.phenom4eno
-.phenomeno4n
-.ph8e8n8y8l9a8l8a9n8i8n8e.
-.pheny3la
-.phenylala2n
-.phenylala5n2ine
-.phenylalan1in
-.ph8i9l8a8t9e9l8i8s8t.
-.phi4latel2i4
-.philate2lis
-.ph8i9l8a8t9e9l8i8s8t8s.
-.philatelis4t1s2
-.ph8o9n8e8m8e.
-.3phone
-.pho2n
-.phone1me
-.ph8o9n8e8m8e8s.
-.phone2mes
-.ph8o9n8e9m8i8c.
-.phone5mi
-.ph8o8s9p8h8o8r9i8c.
-.phos1p
-.phospho5
-.phospho4r
-.ph8o9t8o9g8r8a8p8h8s.
-.pho1to
-.photo2gr
-.photogra4p4h1s2
-.ph8o9t8o9o8f8f9s8e8t.
-.photoo2
-.photoo4f1f
-.photoof2f3s
-.pi8c9a9d8o8r.
-.pi1ca
-.pica2d
-.pica1do
-.picad4or
-.pi8c9a9d8o8r8s.
-.picado4rs2
-.pi8p8e9l8i8n8e.
-.p2ip
-.pipe4
-.pipel2i
-.pipe1l4ine
-.pi8p8e9l8i8n8e8s.
-.pipeli1nes
-.pi8p8e9l8i8n9i8n8g.
-.pipel2in3i
-.pipelin1in
-.pipelinin4g
-.pi9r8a9n8h8a8s.
-.p4ir
-.pi1ra
-.pira2n
-.pira4n1h4
-.piranha4
-.pl8a8c8a9b8l8e.
-.1p2l2
-.pla1ca
-.placa1b2l2
-.pl8a8n8t9h8o8p9p8e8r.
-.3pla2n
-.plan1t
-.plantho4p1p
-.planthop2pe
-.planthop3per
-.pl8a8n8t9h8o8p9p8e8r8s.
-.planthoppe4r1s2
-.pl8e8a8s9a8n8c8e.
-.ple2a
-.pleasa2
-.plea3sanc
-.pleasa2n
-.pl8u8g9i8n.
-.plug5in
-.pl8u8g9i8n8s.
-.plu5g4i2n1s2
-.po8l9t8e8r9g8e8i8s8t.
-.po4l2t
-.pol1te
-.polterg2
-.poltergei2
-.po8l8y9e8n8e.
-.po2ly
-.po8l8y9e8t8h9y8l9e8n8e.
-.polye4t
-.po9l8y8g9a9m8i8s8t.
-.poly1ga
-.poly2gam
-.polygam2is
-.po9l8y8g9a9m8i8s8t8s.
-.polygamis4t1s2
-.po8l8y8g9o8n9i9z8a9t8i8o8n.
-.poly1go
-.polygo2n2
-.polygo3ni
-.polygoniza1
-.polygoniza1t2io
-.polygonizatio2n
-.po9l8y8p8h9o9n8o8u8s.
-.polypho2n
-.polypho1nou2
-.polyphono2us
-.po8l8y9s8t8y8r8e8n8e.
-.po2lys4
-.polys1t
-.polys2ty
-.po8m8e9g8r8a8n9a8t8e.
-.po2me
-.pome2g
-.pome1gr
-.pomegra2n2
-.pomegra1na
-.pomegran2at
-.po8r8o9e8l8a8s9t8i8c.
-.1p4or
-.poro4e
-.poro4el
-.poroe1la
-.poroelast2i
-.poroelas1tic
-.po8r9o8u8s.
-.porou2
-.poro2us
-.po8r9t8a9b8l8e.
-.por1ta
-.por2tab
-.portab2l2
-.po8s8t9a8m9b8l8e.
-.1pos
-.pos2ta
-.posta4m1b
-.postamb2l2
-.po8s8t9a8m9b8l8e8s.
-.postambles2
-.po8s8t9h8u9m8o8u8s.
-.posthu1mo
-.posthu3mo2us
-.posthumou2
-.po8s8t9s8c8r8i8p8t.
-.pos4t1s2
-.post4sc
-.postscri2
-.postscr2ip
-.postscri2p1t
-.po8s8t9s8c8r8i8p8t8s.
-.postscrip4t1s2
-.po8s9t8u8r9a8l.
-.pos1tu
-.postu1ra
-.pr8e9a8m9b8l8e.
-.prea4m1b
-.preamb2l2
-.pr8e9a8m9b8l8e8s.
-.preambles2
-.pr8e9l8o8a8d8e8d.
-.prel4oa
-.preloa2d3
-.preloa2d1ed
-.pr8e9p8a8r9i8n8g.
-.pre2pa
-.prep4a4r1i
-.prep2a2r
-.preparin4g
-.pr8e9p8r8i8n8t.
-.pr2epr2
-.preprin4t3
-.pr8e9p8r8i8n8t8s.
-.preprin4t4s2
-.pr8e9p8r8o8c8e8s9s8o8r.
-.pre3pro
-.prepr2oc
-.prepro1ce
-.preproc2e2ss
-.preproces1so
-.pr8e9p8r8o8c8e8s9s8o8r8s.
-.preprocesso4rs2
-.pr8e9s8p8l8i8t9t8i8n8g.
-.pre1sp
-.pres1p2l2
-.prespl1it
-.prespl4i4t3t2
-.presplit2t1in
-.pr8e9w8r8a8p.
-.prewra4
-.pr8e9w8r8a8p8p8e8d.
-.prewra4p1p
-.prewrap2pe
-.prewrap4p2ed
-.pr8i8e8s8t9e8s8s8e8s.
-.5pr2i4e4
-.pri1est
-.pries4t2e2ss
-.priestess1e4s
-.pr8e8t9t8y9p8r8i8n9t8e8r.
-.pre4t3t2
-.pret1ty
-.pr2ettypr2
-.prettyprin4t3
-.pr8e8t9t8y9p8r8i8n9t8i8n8g.
-.prettyprint2i
-.prettyprin4t3ing
-.prettyprin2t1in
-.pr8o9c8e9d8u8r9a8l.
-.pr2oc
-.pro1ce
-.proce1du
-.procedu1ra
-.pr8o8c8e8s8s.
-.proc2e2ss
-.pr8o9c8u8r9a8n8c8e.
-.procu1ra
-.procura2n
-.pr8o8g9e9n8i8e8s.
-.pro1ge
-.pro1gen
-.proge5n2ie4
-.pr8o8g9e9n8y.
-.pro4geny
-.pr8o9g8r8a8m9m8a8b8l8e.
-.pro1gr
-.program1m
-.program1ma
-.program2mab
-.programmab2l2
-.pr8o8m9i9n8e8n8t.
-.prom4i
-.prom1in
-.prom2ine
-.promi1nen
-.prominen1t
-.pr8o9m8i8s9c8u9o8u8s.
-.prom2is
-.prom2is1c
-.promis1cu
-.promiscu1ou2
-.promiscuo2us
-.pr8o8m9i8s9s8o8r8y.
-.prom4i2s1s
-.promis1so
-.promisso1ry
-.pr8o8m9i8s8e.
-.prom4ise
-.pr8o8m9i8s8e8s.
-.promis1e4s
-.pr8o9p8e8l9l8e8r.
-.pro3pel
-.propel1l
-.propel2le
-.pr8o9p8e8l9l8e8r8s.
-.propelle4r1s2
-.pr8o9p8e8l9l8i8n8g.
-.propell2i
-.propel2lin4
-.pr8o9h8i8b9i9t8i8v8e.
-.pro1h2
-.prohibi2t
-.prohibi1ti
-.prohibi1t2iv
-.pr8o9h8i8b9i9t8i8v8e9l8y.
-.prohibitiv4e1ly
-.pr8o9s8c8i8u8t9t8o.
-.pros2c
-.pros1ci
-.prosci1u
-.prosciu4t3t2
-.prosciut5to
-.pr8o9t8e8s8t9e8r.
-.pro1t
-.pro4tes
-.pr8o9t8e8s8t9e8r8s.
-.proteste4r1s2
-.pr8o9t8e8s9t8o8r.
-.prot4es2to
-.pr8o9t8e8s9t8o8r8s.
-.protesto4rs2
-.pr8o9t8o9l8a8n9g8u8a8g8e.
-.pro1to
-.proto1la
-.proto4la2n
-.protol2ang
-.protolan1gu
-.protolangu4a
-.pr8o9t8o9t8y8p9a8l.
-.proto1ty
-.prototy1pa
-.prototyp4al
-.pr8o8v9i8n8c8e.
-.prov1in
-.prov2inc
-.pr8o8v9i8n8c8e8s.
-.pr8o9v8i8n9c8i8a8l.
-.provin1ci
-.provin3c2i1a
-.provinci2al
-.pr8o8w9e8s8s.
-.prow2e2ss
-.ps8e8u9d8o9d8i8f9f8e8r9e8n9t8i8a8l.
-.2p1s2
-.p2se
-.ps4eu
-.pseu1do
-.pseudod1if
-.pseudodi4f1f
-.pseudodiffer1
-.pseudodiffer3en1t
-.pseudodifferent2i
-.pseudodifferen1t2i1a
-.pseudodifferenti2al
-.ps8e8u9d8o9f8i9n8i8t8e.
-.pseu2d5of
-.pseudo2fi
-.pseudo2fin
-.pseudof2ini
-.pseudofin2it
-.pseudofin2ite
-.ps8e8u9d8o9f8i9n8i8t8e9l8y.
-.pseudofinite1ly
-.ps8e8u9d8o9f8o8r8c8e8s.
-.pseudo1fo
-.pseudofo2r
-.pseudofor1c
-.pseudofor2ce
-.ps8e8u9d8o8g9r8a9p8h8e8r.
-.pseud4og
-.pseudo1gr
-.pseudog5ra3ph4er
-.ps8e8u9d8o9g8r8o8u8p.
-.pseudo4g4ro
-.pseudogrou2
-.ps8e8u9d8o9g8r8o8u8p8s.
-.pseudogrou2p1s2
-.ps8e8u9d8o9n8y8m.
-.pseu4do2n
-.pseudonym4
-.ps8e8u9d8o9n8y8m8s.
-.pseudony4m1s2
-.ps8e8u9d8o9w8o8r8d.
-.pseudo4wo2
-.ps8e8u9d8o9w8o8r8d8s.
-.pseudowor2d1s2
-.ps8y9c8h8e9d8e8l9i8c.
-.ps4y
-.p4sy1c
-.psy3ch
-.psych4e2
-.psy4ch4ed
-.psychedel2i
-.ps8y8c8h8s.
-.psyc4h1s2
-.pu9b8e8s9c8e8n8c8e.
-.pub3
-.pub4e
-.pu4bes4
-.pubes2c
-.pubes1cen
-.pubes3cenc
-.qu8a8d9d8i8n8g.
-.qu2
-.qua2d
-.quad4d1in
-.quad1d4
-.qu8a9d8r8a8t9i8c.
-.qua1dr
-.quadrat1ic
-.qu8a9d8r8a8t9i8c8s.
-.quadrati4c3s2
-.qu8a8d9r8a9t8u8r8e.
-.quadra2tu
-.quadra3ture
-.qu8a8d9r8i9p8l8e8g9i8c.
-.quadri2p2l2
-.quadr2ip
-.quadripleg4ic
-.qu8a8i8n8t9e8r.
-.quai2
-.qua4i4n
-.quain1t
-.qu8a8i8n8t9e8s8t.
-.qu8a9s8i9e8q8u8i8v9a9l8e8n8c8e.
-.quas2ie4
-.quasie1q
-.qu2asiequ2
-.quasieq2ui2
-.quasiequ2iv3
-.quasiequi1va
-.quasiequiv2ale
-.quasiequiva3lenc
-.qu8a9s8i9e8q8u8i8v9a9l8e8n8c8e8s.
-.qu8a9s8i9e8q8u8i8v9a9l8e8n8t.
-.quasiequiva1len1t
-.qu8a9s8i9h8y9p8o9n8o8r9m8a8l.
-.quasi3h
-.quasihy3po
-.quasihypo2n
-.quasihyponor1m
-.quasihyponor1ma
-.qu8a9s8i9r8a8d9i9c8a8l.
-.quas4i2r
-.quasi1r5a
-.quasira2d
-.quasir2adi
-.quasirad3i1ca
-.qu8a9s8i9r8e8s8i8d9u8a8l.
-.quasi4res
-.quasire1si
-.quasire2s2id
-.quasiresi2du
-.quasiresid1u1a
-.qu8a9s8i9s8m8o8o8t8h.
-.qua1sis
-.quasi2s1m
-.quasis1mo
-.quasismoo2
-.quasismo4oth
-.qu8a9s8i9s8t8a9t8i8o8n9a8r8y.
-.quasis1ta
-.quasistation5a2r
-.quasista1t2io
-.quasistatio2n
-.quasistatio1n1a
-.qu8a9s8i9t8o8p8o8s.
-.qu5a5si4t
-.quasi1to
-.quasito1pos
-.qu8a9s8i9t8r8i9a8n9g8u9l8a8r.
-.quasi5tr2i3a
-.quasitri2a2n
-.quasitri2ang
-.quasitrian1gu
-.quasitriangu1la
-.quasitriangul2a2r
-.qu8a9s8i9t8r8i8v9i8a8l.
-.quasitr2i4v
-.quasitriv3i
-.quasitriv2i1a
-.quasitrivi2al
-.qu8i8n9t8e8s9s8e8n8c8e.
-.q2ui2
-.qui4n
-.quin1t
-.quin4t2e2ss
-.quintes4senc
-.qu8i8n9t8e8s9s8e8n8c8e8s.
-.qu8i8n9t8e8s9s8e8n9t8i8a8l.
-.quintessen1t
-.quintessent2i
-.quintessen1t2i1a
-.quintessenti2al
-.ra8b9b8i8t9r8y.
-.2rab
-.ra2b1b
-.rabbi2t
-.rabbi3tr
-.rabbit5ry
-.ra9d8i9o8g9r8a9p8h8y.
-.ra2d
-.r2adi
-.ra3d2io
-.radio5g
-.radio2gr
-.radio4g3ra1phy
-.ra8f8f9i8s8h.
-.raf5fi
-.ra2f
-.ra4f1f4
-.raf2f5is
-.raffis2h
-.ra8f8f9i8s8h9l8y.
-.raffis4h1l4
-.raffish1ly
-.ra8m9s8h8a8c8k8l8e.
-.ra4m1s2
-.ram4s2h
-.ramshack1
-.ramshack1l
-.ra8v9e8n9o8u8s.
-.rav4e4no
-.rave1nou2
-.raveno2us
-.re9a8r8r8a8n8g8e9m8e8n8t.
-.re5ar1r4
-.re2a2r
-.rearran4ge
-.rearra2n
-.rearr2ang
-.rearrange1me
-.rearrange1men
-.rearrange3men1t
-.re9a8r8r8a8n8g8e9m8e8n8t8s.
-.rearrangemen4t4s2
-.re8c9i9p8r8o8c9i9t8i8e8s.
-.reciproci1ti
-.reciprocit2ie4
-.re8c9t8a8n9g8l8e.
-.rec4ta2n
-.re2c1t
-.rect5ang
-.rec1ta
-.rectan1gl2
-.rectan1gle
-.re8c9t8a8n9g8l8e8s.
-.rectangles2
-.re8c9t8a8n9g8u9l8a8r.
-.rectan1gu
-.rectangu1la
-.rectangul2a2r
-.re9d8i9r8e8c8t.
-.2r2ed
-.r4edi
-.red4ir2
-.redi1re
-.redire2c1t
-.re9d8i9r8e8c8t9i8o8n.
-.redirec1t2io
-.redirectio2n
-.re9d8u8c9i8b8l8e.
-.re1du
-.redu2c
-.reduci4b
-.redu1ci
-.reduci1b2l2
-.re9e8c8h8o.
-.ree2c
-.ree2ch
-.ree3cho2
-.re9p8h8r8a8s8e.
-.rep4hr4
-.rephr2as
-.re9p8h8r8a8s8e8s.
-.rephras1e4s
-.re9p8h8r8a8s8e8d.
-.rephra4s4ed
-.re9p8o9s8i9t8i8o8n.
-.re4posi
-.re1po
-.re1pos
-.repo3s2i1t2io
-.reposi1ti
-.repositio2n
-.re9p8o9s8i9t8i8o8n8s.
-.repositio2n3s2
-.re9p8r8i8n8t.
-.repr2
-.reprin4t3
-.re9p8r8i8n8t8s.
-.reprin4t4s2
-.re9s8t8o8r9a8b8l8e.
-.r4es2to
-.resto2ra
-.resto2rab
-.restorab2l2
-.re8t8r8o9f8i8t.
-.retro2fi
-.re8t8r8o9f8i8t9t8e8d.
-.retrof4i4t4t2
-.retrofit2t1ed
-.re9u8s9a8b8l8e.
-.r4eu2
-.re2us4
-.reusa2
-.reu2s1ab
-.reusab2l2
-.re9u8s8e.
-.re9w8i8r8e.
-.rewi2
-.rew4ir4
-.re9w8r8a8p.
-.rewra4
-.re9w8r8a8p8p8e8d.
-.rewra4p1p
-.rewrap2pe
-.rewrap4p2ed
-.re9w8r8i8t8e.
-.rewri4
-.rewr2ite
-.rh8i9n8o8c9e8r9o8s.
-.rh4
-.rh2i1no
-.rhi4no4c
-.rhino1ce
-.rhinoc2ero
-.ri8g8h8t9e8o8u8s.
-.righ1teo
-.righteou2
-.righteo2us
-.ri8g8h8t9e8o8u8s9n8e8s8s.
-.righteous1n4
-.righteous1nes
-.righteousn2e2ss
-.ri8n8g9l8e8a8d8e8r.
-.rin4g
-.ringl2
-.rin1gle
-.ringle2a
-.ringlea2d1
-.ri8n8g9l8e8a8d8e8r8s.
-.ringleade4r5s2
-.ro9b8o8t.
-.ro9b8o8t8s.
-.robo4t1s2
-.ro9b8o8t8i8c.
-.ro9b8o8t9i8c8s.
-.roboti4c3s2
-.ro8u8n8d9t8a8b8l8e.
-.rou2
-.roun2d
-.round1ta
-.round2tab
-.roundtab2l2
-.ro8u8n8d9t8a8b8l8e8s.
-.roundta5bles2
-.sa8l8e8s9c8l8e8r8k.
-.sa2
-.s2ale
-.sales2
-.sales2c
-.salescle5
-.sales1c4l4
-.sa8l8e8s9c8l8e8r8k8s.
-.salescler4k1s2
-.sa8l8e8s9w8o8m8a8n.
-.sales4w2
-.sale4s1wo2
-.saleswom1
-.saleswo1ma
-.saleswoma2n
-.sa8l8e8s9w8o8m8e8n.
-.saleswo2me
-.saleswo1men
-.sa8l9m8o9n8e8l9l8a.
-.s4a2l4m
-.salmo2n4
-.sal1mo
-.salmon4ella
-.salmonel1l
-.sa8l9t8a9t8i8o8n.
-.sa4l4t
-.sal1ta
-.salta1t2io
-.saltatio2n
-.sa8r9s8a9p8a8r9i8l9l8a.
-.s2a2r
-.sa2r4sa2
-.sa4rs2
-.sars1ap
-.s2a2rsap2a2r4
-.sarsa1pa
-.sarsap4a4r1i
-.sarsaparil1l
-.sa8u8e8r9k8r8a8u8t.
-.sau4
-.sauerkrau4t
-.sc8a8t9o9l8o8g9i9c8a8l.
-.s1ca
-.sca1to
-.scato3log1ic
-.scatologi1ca
-.sc8h8e8d9u8l9i8n8g.
-.s2ch2
-.sche2
-.s4ch4ed
-.sche4dul
-.sche1du
-.schedul2i
-.schedul3ing
-.sc8h8i8z9o9p8h8r8e8n8i8c.
-.schi2z
-.schi1zo
-.schiz2oph
-.schizop4hr4
-.sc8h8n8a8u9z8e8r.
-.sc2h1n
-.sch1na
-.schn2au
-.schnau2z4e
-.schnauz1er
-.sc8h8o8o8l9c8h8i8l8d.
-.s4cho2
-.schoo2
-.schoo4l1c2
-.s2chool2ch
-.schoolch4il2
-.schoolchi2ld
-.sc8h8o8o8l9c8h8i8l8d9r8e8n.
-.schoolchil3dr
-.schoolchildre4
-.schoolchil5dren
-.sc8h8o8o8l9t8e8a8c8h8e8r.
-.schoo4l2t
-.school1te
-.s2chooltea2ch
-.schoolteache2
-.sc8h8o8o8l9t8e8a8c8h9e8r8s.
-.schoolteach3e4r1s2
-.sc8r8u9t8i9n8y.
-.scru2t1i5n
-.scr4u1t2i
-.scrut4iny
-.sc8y8t8h9i8n8g.
-.s1cy
-.scy3thin
-.se8l8l9e8r.
-.sel2le
-.se8l8l9e8r8s.
-.selle4r1s2
-.se8c9r8e9t8a8r9i8a8t.
-.se1cr
-.se4c3re1ta
-.secret2a2r
-.secretar1i
-.secretar2i3a
-.se8c9r8e9t8a8r9i8a8t8s.
-.secretaria4t1s2
-.se8m9a9p8h8o8r8e.
-.se1ma
-.se4map
-.semapho4r
-.se8m9a9p8h8o8r8e8s.
-.se9m8e8s9t8e8r.
-.4se1me
-.se2mes
-.se8m8i9d8e8f9i9n8i8t8e.
-.sem2id
-.semide1f
-.semidef5i5n2ite
-.semide1fi
-.semide2fin
-.semidef2ini
-.semidefin2it
-.se8m8i9d8i9r8e8c8t.
-.semi2di
-.semid4ir2
-.semidi1re
-.semidire2c1t
-.se8m8i9h8o9m8o9t8h8e8t9i8c.
-.semi3h
-.semiho1mo
-.semihom4oth3
-.semihomoth2e
-.semihomo3the4t
-.semihomothet1ic
-.se8m8i9r8i8n8g.
-.sem4ir
-.semir1i
-.semirin4g
-.se8m8i9r8i8n8g8s.
-.semirings2
-.se8m8i9s8i8m9p8l8e.
-.se4m2is
-.semisi4m1p
-.semisim1p2l2
-.se8m8i9s8k8i8l8l8e8d.
-.sem4is4k2
-.semisk1i
-.semisk4il1l
-.semiskil2le
-.se8r8o9e8p8i9d8e9m8i9o9l8o8g9i9c8a8l.
-.s2er4o
-.sero4e
-.seroep4id
-.seroepi3de
-.seroepid4em
-.seroepidem2io
-.seroepidemi1ol
-.seroepidemio3log1ic
-.seroepidemiologi1ca
-.se8r9v8o9m8e8c8h9a8n8i8s8m.
-.4ser3vo
-.servo2me
-.servome2ch
-.servomech5a5nis
-.servomecha2n
-.servomechani2s1m
-.se8r9v8o9m8e8c8h9a8n8i8s8m8s.
-.servomechan4is4m1s2
-.se8s9q8u8i9p8e9d8a9l8i8a8n.
-.s1e4s
-.sesqu2
-.sesq2ui2
-.sesqu2ip
-.sesquipe4
-.sesqui2p2ed
-.sesquip2e2d2a
-.sesquipedal1i
-.sesquipedal2i1a
-.sesquipedali2a2n
-.se8t9u8p.
-.se1tu
-.se8t9u8p8s.
-.setu2p1s2
-.se9v8e8r8e9l8y.
-.5sev
-.sev1er
-.sev4erel
-.severe1ly
-.sh8a8p8e9a8b8l8e.
-.sha3pe4a
-.shape1a4b
-.shapeab2l2
-.sh8o8e9s8t8r8i8n8g.
-.sho4
-.sho2est4r
-.shoestrin4g
-.sh8o8e9s8t8r8i8n8g8s.
-.shoestrings2
-.si8d8e9s8t8e8p.
-.5side4s2
-.s2id
-.sideste4p
-.si8d8e9s8t8e8p8s.
-.sideste2p1s2
-.si8d8e9s8w8i8p8e.
-.sides4w2
-.sideswi2
-.sidesw2ip
-.sideswipe4
-.sk8y9s8c8r8a8p8e8r.
-.sk2
-.skys4c
-.skyscrap3er
-.sk8y9s8c8r8a8p8e8r8s.
-.skyscrape4r1s2
-.sm8o8k8e9s8t8a8c8k.
-.2s1m
-.s1mo
-.s4m2ok
-.smokes4
-.smokes1ta
-.smokestack1
-.sm8o8k8e9s8t8a8c8k8s.
-.smokestac4k1s2
-.sn8o8r9k8e8l9i8n8g.
-.s1n4
-.snorke5l2i
-.snorke4l3ing
-.so9l8e9n8o8i8d.
-.1so
-.sol4eno
-.solenoi2
-.soleno2id
-.so9l8e9n8o8i8d8s.
-.solenoi2d1s2
-.so8l8u8t8e.
-.so1lut
-.so8l8u8t8e8s.
-.so8v9e8r9e8i8g8n.
-.4sov
-.soverei2
-.sovere2ig2
-.so8v9e8r9e8i8g8n8s.
-.sovereig2n1s2
-.sp8a9c8e8s.
-.2s1pa
-.spa4ce
-.sp8e9c8i8o8u8s.
-.spe2c
-.spe1c2i
-.spec2io
-.speciou2
-.specio2us
-.sp8e8l8l9e8r.
-.spel1l
-.spel2le
-.sp8e8l8l9e8r8s.
-.spelle4r1s2
-.sp8e8l8l9i8n8g.
-.spell2i
-.spel2lin4
-.sp8e9l8u8n8k9e8r.
-.spelu4nk2
-.spelunk1er
-.sp8e8n8d9t8h8r8i8f8t.
-.spen4d
-.spend2th
-.spendt4hr4
-.spendthr4i2ft
-.sp8h8e8r9o8i8d.
-.s2phe
-.3sph4er
-.sph2ero
-.spheroi2
-.sphero2id
-.sp8h8e8r9o8i8d9a8l.
-.spheroi1d2a
-.sp8h8i8n9g8e8s.
-.sph5ing
-.sph4inge
-.sp8i8c9i9l8y.
-.sp2i1ci
-.spici1ly
-.sp8i8n9o8r8s.
-.spi2n
-.sp4i1no
-.spino4rs2
-.sp8o8k8e8s9w8o8m8a8n.
-.sp2ok
-.spokes4
-.spokes4w2
-.spoke4s1wo2
-.spokeswom1
-.spokeswo1ma
-.spokeswoma2n
-.sp8o8k8e8s9w8o8m8e8n.
-.spokeswo2me
-.spokeswo1men
-.sp8o8r8t8s9c8a8s8t.
-.s1p4or4
-.spor4t1s2
-.sport4sc
-.sports1ca
-.sp8o8r8t8s9c8a8s8t9e8r.
-.sportscast5er
-.sp8o8r9t8i8v8e9l8y.
-.spor1ti
-.spor4t2iv
-.sportiv4e1ly
-.sp8o8r8t8s9w8e8a8r.
-.sport4sw2
-.sportswe2a2r
-.sp8o8r8t8s9w8r8i8t8e8r.
-.sportswri4
-.sportswr2ite
-.sp8o8r8t8s9w8r8i8t8e8r8s.
-.sportswrit5e4r1s2
-.sp8r8i8g8h8t9l8i8e8r.
-.spr2
-.spr2ig
-.sprigh2tl
-.sprightl2ie4
-.sq8u8e8a9m8i8s8h.
-.squ2
-.squeam2is
-.squeamis2h
-.st8a8n8d9a8l8o8n8e.
-.5st4and
-.sta2n
-.stan1d2a
-.standalo2n
-.st8a8r9t8l8i8n8g.
-.st2a2r
-.star2tl
-.st8a8r9t8l8i8n8g9l8y.
-.startlingl2
-.startling1ly
-.st8a9t8i8s9t8i8c8s.
-.statis1t2i
-.statis1tic
-.statisti4c3s2
-.st8e8a8l8t8h9i8l8y.
-.stea4l
-.stea4lt
-.stealth3i
-.steal4th4il2
-.stealthi1ly
-.st8e8e8p8l8e9c8h8a8s8e.
-.s1tee
-.stee4p1
-.stee1p2l2
-.steeple2ch
-.st8e8r8e8o9g8r8a8p8h9i8c.
-.stere1o
-.stereo2g
-.stereo1gr
-.stereo5graph1ic
-.stereogr4aphi
-.st8o9c8h8a8s9t8i8c.
-.s1to
-.sto2ch4
-.stochast2i
-.stochas1tic
-.st8r8a8n8g8e9n8e8s8s.
-.st4r
-.s1tra
-.stran4ge
-.stra2n
-.str2ang
-.strange4n4e
-.stran1gen
-.strange1nes
-.strangen2e2ss
-.st8r8a8p9h8a8n8g8e8r.
-.straph2an4g
-.straphang5er
-.strapha2n
-.st8r8a8t9a9g8e8m.
-.stra2ta
-.st8r8a8t9a9g8e8m8s.
-.stratage4m1s2
-.st8r8e8t8c8h9i9e8r.
-.stre4tc
-.stret4ch
-.stretch2ie4
-.st8r8i8p9t8e8a8s8e.
-.str2ip
-.stri2p1t
-.strip2te
-.st8r8o8n8g9h8o8l8d.
-.stro2n
-.strongho2l2d
-.st8r8o8n8g9e8s8t.
-.st8u9p8i8d9e8r.
-.s1tu
-.stup4id
-.stupi3de
-.st8u9p8i8d9e8s8t.
-.stupide4s2
-.su8b9d8i8f9f8e8r9e8n9t8i8a8l.
-.1su
-.su4b3
-.su4b1d
-.subd1if
-.subdi4f1f
-.subdiffer1
-.subdiffer3en1t
-.subdifferent2i
-.subdifferen1t2i1a
-.subdifferenti2al
-.su8b9e8x9p8r8e8s9s8i8o8n.
-.sub4e
-.sub1ex3p
-.subexpr2
-.subex3pr2e2ss
-.subexpres1si
-.subexpres1s2io
-.subexpres5sio2n
-.su8b9e8x9p8r8e8s9s8i8o8n8s.
-.subexpressio2n3s2
-.su8m9m8a9b8l8e.
-.su2m
-.sum1m
-.sum1ma
-.sum2mab
-.summab2l2
-.su8p8e8r9e8g8o.
-.su1pe
-.supere1go
-.su8p8e8r9e8g8o8s.
-.supere4gos
-.su9p8r8e8m9a9c8i8s8t.
-.supr2
-.supre4mac
-.supre1ma
-.suprem4a2ci
-.su9p8r8e8m9a9c8i8s8t8s.
-.supremacis4t1s2
-.su8r9v8e8i8l9l8a8n8c8e.
-.su2r
-.surv4e
-.survei2
-.surveil1l
-.surveilla2n
-.sw8i8m9m8i8n8g9l8y.
-.sw2
-.swi2
-.swim1m
-.swimm4ingl2
-.swimm5ing1ly
-.sy8m8p9t8o9m8a8t8i8c.
-.sy4m1p
-.sym2p1t
-.symp1to
-.sympto2ma
-.symptomat1ic
-.sy8n9c8h8r8o9m8e8s8h.
-.syn3c4hr4
-.syn2ch
-.synchro2me
-.synchro2mes
-.synchrom4es2h
-.sy8n9c8h8r8o9n8o8u8s.
-.synchro2n
-.synchro1nou2
-.synchrono2us
-.sy8n9c8h8r8o9t8r8o8n.
-.synchrotro2n
-.ta8f8f9r8a8i8l.
-.4ta2f4
-.ta4f1f4
-.taffr2ai2
-.ta8l8k9a9t8i8v8e.
-.ta2l
-.4talk
-.talka3
-.talka4t
-.talka1t2iv
-.ta9p8e8s9t8r8y.
-.tap2est4r
-.tape4stry
-.ta9p8e8s9t8r8i8e8s.
-.tapestr2ie4
-.ta8r9p8a8u9l8i8n.
-.t2a2r
-.tar2p
-.tar1pa
-.tarpau4l2
-.tarpaul2i
-.ta8r9p8a8u9l8i8n8s.
-.tarpaul2i2n1s2
-.te9l8e8g9r8a9p8h8e8r.
-.tele1gr
-.teleg5ra3ph4er
-.te9l8e8g9r8a9p8h8e8r8s.
-.telegraphe4r1s2
-.te8l8e9k8i9n8e8t9i8c.
-.teleki4n
-.telek1i
-.telek2ine
-.teleki3net1ic
-.te8l8e9k8i9n8e8t9i8c8s.
-.telekineti4c3s2
-.te8l8e9r8o9b8o8t9i8c8s.
-.te4l1er
-.tel4ero
-.teler5ob
-.teleroboti4c3s2
-.te8l8l9e8r.
-.tel1l
-.tel2le
-.te8l8l9e8r8s.
-.telle4r1s2
-.te8m9p8o9r8a8r9i8l8y.
-.te4m1p
-.tem1p4or
-.tempo1ra
-.tempo4raril
-.tempor2a2r
-.temporar1i
-.temporari1ly
-.te8n9u8r8e.
-.te8s8t9b8e8d.
-.tes2t1b
-.test4be2d
-.te8x8t9w8i8d8t8h.
-.3tex
-.tex1t2
-.textw4
-.textwi2
-.textw2id
-.textwid2th
-.th8a8l9a9m8u8s.
-.tha3la
-.thala3m
-.thala1mu
-.thalam2us
-.th8e8r9m8o9e8l8a8s9t8i8c.
-.th2e
-.ther3m4
-.ther1mo
-.thermo4el
-.thermoe1la
-.thermoelast2i
-.thermoelas1tic
-.ti8m8e9s8t8a8m8p.
-.ti2mes
-.times1ta
-.timesta4m1p
-.ti8m8e9s8t8a8m8p8s.
-.timestam2p1s2
-.to8o8l9k8i8t.
-.too2
-.toolk1i
-.to8o8l9k8i8t8s.
-.toolki4t1s2
-.to8p8o9g8r8a8p8h9i9c8a8l.
-.to5po4g
-.topo1gr
-.topo5graph1ic
-.topogr4aphi
-.topographi1ca
-.to8q8u8e8s.
-.to1q
-.toqu2
-.tr8a8i9t8o8r9o8u8s.
-.1tra
-.tr2ai2
-.trai1to
-.traitorou2
-.traitoro2us
-.tr8a8n8s9c8e8i8v8e8r.
-.tra2n
-.tra2n1s2
-.trans4c
-.tran4s3cei2
-.transce2iv
-.tr8a8n8s9c8e8i8v8e8r8s.
-.transceive4r1s2
-.tr8a8n8s9g8r8e8s8s.
-.tran2s3g
-.trans1gr
-.transgr2e2ss
-.tr8a8n8s9v8e8r9s8a8l.
-.tran4sv
-.transve4r1s2
-.transver1sa2
-.tr8a8n8s9v8e8r9s8a8l8s.
-.transversa2l1s2
-.tr8a8n8s9v8e8s9t8i8t8e.
-.transv4e2s
-.transvest2i
-.transvest2ite
-.tr8a8n8s9v8e8s9t8i8t8e8s.
-.transvestit4es
-.tr8a9v8e8r8s9a9b8l8e.
-.trave4r1s2
-.traver1sa2
-.traver2s1ab
-.traversab2l2
-.tr8a9v8e8r9s8a8l.
-.tr8a9v8e8r9s8a8l8s.
-.traversa2l1s2
-.tr8i9e8t8h8y8l9a8m8i8n8e.
-.tri5et
-.tr2ie4
-.triethy3la
-.triethylam1in
-.triethylam2ine
-.tr8e8a8c8h9e8r8i8e8s.
-.trea2ch
-.treache2
-.treacher1i
-.treacher2ie4
-.tr8o8u9b8a9d8o8u8r.
-.trou2
-.trouba2d
-.trouba1do
-.troubadou2
-.tu8r9k8e8y.
-.1tu
-.tu8r9k8e8y8s.
-.turkeys4
-.tu8r8n9a8r8o8u8n8d.
-.tur4n2a2r
-.tur1na
-.turnarou2
-.turnaroun2d
-.tu8r8n9a8r8o8u8n8d8s.
-.turnaroun2d1s2
-.ty8p9a8l.
-.1ty
-.ty1pa
-.typ4al
-.un9a8t9t8a8c8h8e8d.
-.un2at4
-.una4t3t2
-.unat1ta
-.unatta2ch
-.unattache2
-.unatta4ch4ed
-.un9e8r8r9i8n8g9l8y.
-.un4er
-.uner4r4
-.unerrin4g
-.unerringl2
-.unerring1ly
-.un9f8r8i8e8n8d9l8y.
-.un3f
-.unfri2
-.unfr2ie4
-.unfrien2d1ly
-.un9f8r8i8e8n8d9l8i9e8r.
-.unfriendl2ie4
-.va8g8u8e8r.
-.1va
-.vag4
-.va5guer
-.va2gue
-.va8u8d8e9v8i8l8l8e.
-.vaude1v4
-.vaude2v3i4l
-.vaude1vi
-.vaudevil1l
-.vaudevil2le
-.vi8c9a8r8s.
-.v4ic2a2r
-.vi1ca
-.vica4rs2
-.vi8l9l8a8i8n9e8s8s.
-.2vil
-.vil1l
-.villai2
-.villa4i4n
-.villa2ine
-.villai5n2e2ss
-.villai1nes
-.vi8s9u8a8l.
-.vi3su
-.visu1al
-.vi8s9u8a8l9l8y.
-.visual1l
-.visual1ly
-.vi9v8i8p9a9r8o8u8s.
-.3v2iv
-.viv2i4p
-.vivi1pa
-.vivip2a2r
-.viviparou2
-.viviparo2us
-.vo8i8c8e9p8r8i8n8t.
-.voi4
-.voi3cep
-.voicepr2
-.voiceprin4t3
-.vs8p8a8c8e.
-.v2s1pa
-.vspa4ce
-.wa8d9d8i8n8g.
-.wa2d
-.wad4d1in
-.wad1d4
-.wa8l8l9f8l8o8w8e8r.
-.wal1l
-.wal2lf
-.wallf4l2
-.wallflow1er
-.wa8l8l9f8l8o8w9e8r8s.
-.wallflowe4r1s2
-.wa8r8m9e8s8t.
-.w2a2r
-.war1m
-.war2me
-.war2mes
-.wa8s8t8e9w8a8t8e8r.
-.was4t
-.waste2w
-.waste1w5a
-.wastewa1te
-.wa8v8e9g8u8i8d8e.
-.waveg3
-.waveg2ui2
-.wavegu2id
-.wa8v8e9g8u8i8d8e8s.
-.waveguide4s2
-.wa8v8e9l8e8t.
-.wa8v8e9l8e8t8s.
-.wavele4t1s2
-.we8b9l8i8k8e.
-.w2e1b
-.web2l2
-.web3l4ik
-.we8e8k9n8i8g8h8t.
-.weekn2ig
-.we8e8k9n8i8g8h8t8s.
-.weeknigh4t1s2
-.wh8e8e8l9c8h8a8i8r.
-.whee4l1c2
-.wheel2ch
-.wheelchai2
-.wheelcha4ir
-.wh8e8e8l9c8h8a8i8r8s.
-.wheelchai4rs2
-.wh8i8c8h9e8v8e8r.
-.whi4
-.wh4i2ch
-.whiche2
-.whichev1er
-.wh8i8t8e9s8i8d8e8d.
-.wh2ite
-.whit4es
-.white1si
-.white2s2id
-.whitesi2d1ed
-.wh8i8t8e9s8p8a8c8e.
-.white1sp
-.white2s1pa
-.whitespa4ce
-.wh8i8t8e9s8p8a8c8e8s.
-.wi8d8e9s8p8r8e8a8d.
-.w2id
-.wide4s2
-.wide1sp
-.wides4pre
-.widespr2
-.widesprea2d1
-.wi8n8g9s8p8a8n.
-.win4g
-.wings2
-.wing2s1pa
-.wingspa4n
-.wi8n8g9s8p8a8n8s.
-.wingspa2n1s2
-.wi8n8g9s8p8r8e8a8d.
-.wingspr2
-.wingsprea2d1
-.wi8t8c8h9c8r8a8f8t.
-.wi4tc
-.wit4ch
-.witchcra2f4t
-.witchcra2f
-.wo8r8d9s8p8a8c9i8n8g.
-.1wo2
-.wor2d1s2
-.words4p
-.word2s1pa
-.wordsp4a2ci
-.wordspa2c1in
-.wordspac1ing
-.wo8r8k9a8r8o8u8n8d.
-.work2a2r
-.workarou2
-.workaroun2d
-.wo8r8k9a8r8o8u8n8d8s.
-.workaroun2d1s2
-.wo8r8k9h8o8r8s8e.
-.workh4
-.workhor4se
-.workho4rs2
-.wo8r8k9h8o8r8s8e8s.
-.workhors3e4s
-.wr8a8p9a8r8o8u8n8d.
-.wra4
-.wrap2a2r4
-.wra1pa
-.wraparou2
-.wraparoun2d
-.wr8e8t8c8h9e8d.
-.wre4tc
-.wret4ch
-.wretche2
-.wret4ch4ed
-.wr8e8t8c8h9e8d9l8y.
-.wretche2d1ly
-.ye8s9t8e8r9y8e8a8r.
-.yes4
-.yesterye2a2r
-.al9g8e9b8r8a8i9s8c8h8e.
-.algebra2is1c
-.algebrais3ch2
-.algebraische2
-.al9l8e9g8h8e9n8y.
-.al1l
-.al2le
-.al3leg
-.alleghe2n
-.ar9k8a8n9s8a8s.
-.arka2n
-.arkan2sa2
-.arka2n1s2
-.at8p9a8s8e.
-.a4t1p
-.at1pa
-.at8p9a8s8e8s.
-.atpas1e4s
-.au8s9t8r8a8l9a8s8i8a8n.
-.a2us
-.aus1t4r
-.aus1tra
-.australas2i1a
-.australasi2a2n
-.au8t8o9m8a8t8i9s8i8e8r9t8e8r.
-.automa3tis
-.automatis2ie4
-.automatisiert3er
-.be9d8i8e9n8u8n8g.
-.4be2d
-.b4e3di
-.be5di3en
-.bed2ie4
-.bedie3nu4n
-.be8m8b8o.
-.4be5m
-.be4m5b
-.bi8b9l8i9o9g8r8a9p8h8i9s8c8h8e.
-.bibliogr4aphi
-.bibliograph2is1c
-.bibliographis3ch2
-.bibliographische2
-.bo8s9t8o8n.
-.5bos4
-.bos1to
-.bosto2n
-.br8o8w8n9i8a8n.
-.brown5i
-.brow3n4i1a
-.browni3a2n
-.br8u8n8s9w8i8c8k.
-.bru2n
-.bru2n3s4
-.brun4sw2
-.brunswi2
-.brunswick1
-.bu9d8a9p8e8s8t.
-.bu1d2a
-.ca8r9i8b9b8e8a8n.
-.car1i
-.car4ib
-.cari2b1b
-.carib2be
-.caribbea2n
-.ch8a8r8l8e8s9t8o8n.
-.char4le4
-.char1l
-.charles2
-.charl4es2to
-.charle3sto2n
-.ch8a8r9l8o8t8t8e8s9v8i8l8l8e.
-.char3lo4
-.charlo4t3t2
-.charlot4tes
-.charlotte4sv
-.charlottes2vil
-.charlottesvil1l
-.charlottesvil2le
-.co9l8u8m9b8i8a.
-.colum4bi
-.colu4m1b
-.columb2i1a
-.cz8e8c8h8o9s8l8o9v8a9k8i8a.
-.c2ze4
-.cze2ch
-.cze3cho2
-.czechos4l2
-.czechos4lov
-.czechoslo1va
-.czechoslovak1i
-.czechoslovak2i1a
-.de8l9a9w8a8r8e.
-.de1la
-.de4law
-.delaw2a2r
-.di8j8k9s8t8r8a.
-.di3j
-.dij4k1s2
-.dijkst4r
-.dijks1tra
-.du8a8n8e.
-.d1u1a
-.dua2n
-.dy9n8a9m8i9s8c8h8e.
-.5dyn
-.dy1na
-.dynam2is
-.dynam2is1c
-.dynamis3ch2
-.dynamische2
-.en8g9l8i8s8h.
-.engl2
-.englis2h
-.eu8l8e8r9i8a8n.
-.eul4e
-.eu3l4er1i
-.eule1r2i3a4
-.euleri2a2n
-.ev8a8n9s8t8o8n.
-.e1va
-.eva2n
-.evan4st
-.eva2n1s2
-.evans1to
-.evansto2n
-.fe8b9r8u9a8r8y.
-.f2e4b
-.fe3br
-.febru3a
-.febru2a2r
-.fe8s8t9s8c8h8r8i8f8t.
-.fes4t1s2
-.fest4sc
-.fests2ch2
-.festsc4hr4
-.festschr4i2ft
-.fl8o8r9i9d8a.
-.flor2id
-.flori1d2a
-.fl8o8r9i9d9i8a8n.
-.flori2di
-.florid5i2a2n
-.flori1d4i3a
-.fo8r9s8c8h8u8n8g8s9i8n9s8t8i9t8u8t.
-.fors4c
-.fors2ch2
-.forschungs2
-.forschung2s1in
-.forschungs2i2n1s2
-.forschungsinst2i
-.forschungsinsti1tu
-.fr8e8e9b8s8d.
-.fre2e1b
-.free2b5s2
-.freeb4s5d
-.fu8n8k9t8s8i8o8n8a8l.
-.3fu
-.fu4nk2
-.funk5t
-.funk4t1s2
-.funkt1s2io
-.funkt5sio2n
-.funktsio1n5a
-.ga8u8s8s9i8a8n.
-.ga2us
-.gau2ss
-.gaus1si
-.gauss2i1a
-.gaussi2a2n
-.gh8o8s8t9s8c8r8i8p8t.
-.ghos4t1s2
-.ghost4sc
-.ghostscri2
-.ghostscr2ip
-.ghostscri2p1t
-.gh8o8s8t9v8i8e8w.
-.ghos4tv
-.ghostv2ie4
-.gr8a8s8s9m8a8n8n9i8a8n.
-.gr2as
-.gra2ss
-.gras2s1m
-.grass3ma
-.grassma2n3
-.grassma4n1n2
-.grassman3n4i1a
-.grassma2nni3a2n
-.gr8e8i8f8s9w8a8l8d.
-.grei2
-.grei2f3s
-.greifsw2
-.greifswa2ld
-.gr8o8t8h8e8n9d8i8e8c8k.
-.g4ro
-.gro4th2e
-.gr4oth
-.grothe2n
-.grothend2ie4
-.grothendieck1
-.gr8u8n8d9l8e8h9r8e8n.
-.gru2n
-.grundle1h4
-.grundle4hr4
-.ha9d8a9m8a8r8d.
-.ha2d
-.ha1d2a
-.hada2m2
-.had4a1ma
-.hadam2a2r
-.ha8i9f8a.
-.hai1fa
-.ha8m8i8l9t8o8n9i8a8n.
-.ha4m
-.hami4lt
-.hamil1to
-.hamilto2n
-.hamilto3n4i1a
-.hamiltoni3a2n
-.he8l9s8i8n8k8i.
-.he2l1s2
-.hel2s1in
-.hels4i4nk2
-.helsink1i
-.he8r9m8i8t9i8a8n.
-.her3mit
-.hermi1ti
-.herm4i1t2i1a
-.hermiti2a2n
-.hi8b8b8s.
-.hi2b1b
-.hib2b5s2
-.ho8k9k8a8i9d8o.
-.h2ok
-.hokk4
-.hokkai2
-.hokka2id
-.hokkai1do
-.ja8c9k8o8w9s8k8i.
-.5ja
-.jack1
-.jackowsk2
-.jackowsk1i
-.ja8n9u9a8r8y.
-.ja2n
-.jan3u1a
-.janu2a2r
-.ja9p8a9n8e8s8e.
-.ja4p
-.ja1pa
-.japa2n
-.japa1nes
-.japane1s2e
-.ka8d9o8m9t8s8e8v.
-.ka2d
-.ka1do
-.kado4mt
-.kadom4t1s2
-.kadomt5sev
-.ka8n9s8a8s.
-.ka2n
-.kan2sa2
-.ka2n1s2
-.ka8r8l8s9r8u8h8e.
-.k2a2r
-.kar1l
-.kar2l1s2
-.karls1r
-.ko8r9t8e9w8e8g.
-.ko5r
-.kr8i8s8h8n8a.
-.kr2is
-.kr3is2h
-.kris2h1n
-.krish1na
-.kr8i8s8h9n8a9i8s8m.
-.krishnai2
-.krishnai2s1m
-.kr8i8s8h9n8a8n.
-.krishn2a2n
-.la8n9c8a8s9t8e8r.
-.lan1ca
-.lancast5er
-.le9g8e8n8d8r8e.
-.le1gen
-.legen1dr
-.legendre4
-.le8i8c8e8s9t8e8r.
-.lei2
-.le5ic
-.leices5t
-.li8p9s8c8h8i8t8z.
-.l2ip
-.li2p1s2
-.lips2ch2
-.lips3chit
-.lipschi4tz
-.li8p9s8c8h8i8t8z9i8a8n.
-.lipschit2z1i
-.lipschitz2i1a
-.lipschitzi2a2n
-.lo8j9b8a8n.
-.lo5j
-.lojba2n
-.lo8u9i9s8i9a8n8a.
-.lou2
-.lo2ui2
-.louis2i1a
-.louisi2a2n
-.louisia1na
-.ma8c9o8s.
-.ma1co
-.ma8n9c8h8e8s9t8e8r.
-.man2ch
-.manche2
-.manch1es
-.ma8r9k8o8v9i8a8n.
-.marko5vi2a2n
-.markov2i1a
-.ma8r8k8t9o8b8e8r9d8o8r8f.
-.mark5t
-.mark1to
-.markto3b
-.marktober1do
-.marktoberd4or
-.marktoberdor1f
-.ma8s8s9a9c8h8u9s8e8t8t8s.
-.ma2ss
-.mas1sa2
-.massa2ch
-.massach2us
-.massachuse4t3t2
-.massachuset4t1s2
-.ma8x9w8e8l8l.
-.maxwel4l
-.mi9c8r8o9s8o8f8t.
-.micro2so
-.microso2ft3
-.mi8n9n8e9a8p9o9l8i8s.
-.m2i4n1n2
-.minne4
-.minneapol2i
-.mi8n9n8e9s8o8t8a.
-.min1nes
-.minne1so
-.minneso1ta
-.mo8s9c8o8w.
-.mos2c
-.mos1co
-.na8c8h9r8i8c8h8t8e8n.
-.1na
-.na2ch
-.nac4hr4
-.na2chr4i2ch
-.nachricht1en
-.na8s8h9v8i8l8l8e.
-.n4as
-.nas2h
-.nash2vil
-.nashvil1l
-.nashvil2le
-.ne8t9b8s8d.
-.ne2t1b
-.net2b5s2
-.netb4s5d
-.ne8t9s8c8a8p8e.
-.ne4t1s2
-.net4sc
-.netsca4p
-.nets1ca
-.ni8j9m8e9g8e8n.
-.ni3j
-.nijme2g
-.nijme1gen
-.no8e9t8h8e8r9i8a8n.
-.3noe
-.noeth2e
-.noether1i
-.noethe1r2i3a4
-.noetheri2a2n
-.no8o8r8d9w8i8j8k8e8r9h8o8u8t.
-.noo2
-.no3ord
-.noord1w
-.noordwi2
-.noordwi3j
-.noordwijk1er
-.noordwijker1h4
-.noordwijkerhou2
-.no9v8e8m9b8e8r.
-.nove4m5b
-.op8e8n9b8s8d.
-.ope4n1b4
-.open2b5s2
-.openb4s5d
-.op8e8n9o8f8f8i8c8e.
-.op4eno
-.openo4f1f
-.openof1fi
-.pa8l8a9t8i8n8o.
-.pala2t1in
-.palat2i1no
-.pa9l8e8r9m8o.
-.paler3m4
-.paler1mo
-.pe9t8r8o8v9s8k8i.
-.petro3v
-.petrovsk2
-.petrovsk1i
-.pf8a8f8f9i8a8n.
-.4pf
-.p1fa
-.pfa2f
-.pfa4f1f4
-.pfaf1fi
-.pfaff2i3a
-.pfaffi2a2n
-.ph8i8l9a9d8e8l9p8h8i8a.
-.phi4l4ade
-.phila2d
-.philade2lp
-.philadel5phi
-.philadelph2i1a
-.ph8i8l9o9s8o8p8h9i9s8c8h8e.
-.philo2so
-.philos4op
-.philos2oph
-.philosoph2is1c
-.philosophis3ch2
-.philosophische2
-.po8i8n9c8a8r8e.
-.poin2
-.poi2
-.poinc2a2r5
-.poin1ca
-.po9t8e8n9t8i8a8l9g8l8e8i9c8h8u8n8g.
-.p4ot
-.po1ten1t
-.potent2i
-.poten1t2i1a
-.potenti2al
-.potentia4l1g4
-.potentialgl2
-.potential1gle
-.potentialglei2
-.potentialgle5ic
-.potentialgle4i2ch
-.ra9d8h8a9k8r8i8s8h9n8a8n.
-.rad1h2
-.radhakr2is
-.radhakr3is2h
-.radhakris2h1n
-.radhakrish1na
-.radhakrishn2a2n
-.ra8t8h8s9k8e8l9l8e8r.
-.r4ath
-.ra2t4h1s2
-.rathsk2
-.rath4ske
-.rathskel1l
-.rathskel2le
-.ri8e9m8a8n8n9i8a8n.
-.r2ie4
-.rie5ma2n
-.rie1ma
-.riema4n1n2
-.rieman3n4i1a
-.riema2nni3a2n
-.ry8d9b8e8r8g.
-.ry1d
-.ryd1b
-.rydberg2
-.sc8h8o8t9t8i8s8c8h8e.
-.scho4t3t2
-.schott2is1c
-.s2ch2ottis3ch2
-.schottische2
-.sc8h8r8o9d8i8n8g9e8r.
-.sc4hr4
-.schrod1in
-.schrod4inge
-.sc8h8w8a9b8a9c8h8e8r.
-.sch1w
-.schwaba2ch
-.schwabache2
-.sc8h8w8a8r8z9s8c8h8i8l8d.
-.schw2a2r
-.s2chwarzs2ch2
-.schwarzsch4il2
-.schwarzschi2ld
-.se8p9t8e8m9b8e8r.
-.se2p1t
-.sep2te
-.septe4m5b
-.st8o8k8e8s9s8c8h8e.
-.st2ok
-.stokes4
-.stok2e2ss
-.stokes2s5c
-.stokess2ch2
-.stokessche2
-.st8u8t8t9g8a8r8t.
-.stu4t3t2
-.stut4t1g
-.stutt1ga
-.stuttg2a2r
-.su8s9q8u8e9h8a8n9n8a.
-.s2us
-.susqu2
-.susque1h4
-.susqueha2n
-.susqueha4n1n2
-.susquehan1na
-.ta8u9b8e8r9i8a8n.
-.tau4b
-.taub4e
-.tau3ber
-.tauber1i
-.taube1r2i3a4
-.tauberi2a2n
-.te8c8h9n8i9s8c8h8e.
-.te2ch
-.tec2h1n
-.techn2is1c
-.te2chnis3ch2
-.technische2
-.te8n9n8e8s9s8e8e.
-.t4e4n1n2
-.tenne4
-.ten1nes
-.tenn2e2ss
-.to9m8a9s8z8e8w9s8k8i.
-.to2ma
-.tomas2ze
-.tomaszewsk2
-.tomaszewsk1i
-.ty9p8o9g8r8a8p8h8i8q8u8e.
-.ty3po
-.ty5po4g
-.typo1gr
-.typogr4aphi
-.typographiqu2
-.uk8r8a8i8n9i8a8n.
-.4uk
-.ukr2ai2
-.ukra4i4n
-.ukra2ini
-.ukrai4n4i1a
-.ukraini3a2n
-.ve8r9a8l8l9g8e9m8e8i8n9e8r8t8e.
-.veral1l
-.veral4l1g4
-.verallge1me
-.verallgemei2
-.verallgeme2ine
-.verallgemein1er
-.ve8r9e8i8n9i9g8u8n8g.
-.vere3in
-.verei2
-.vere2ini
-.verein2ig
-.vereini3gun
-.ve8r9t8e8i9l8u8n9g8e8n.
-.vertei2
-.verteilun1gen
-.vi8i8i8t8h.
-.v4i5i4
-.vi4i5i4
-.vii2ith
-.vi8i8t8h.
-.vi2ith
-.wa8h8r9s8c8h8e8i8n9l8i8c8h9k8e8i8t8s9t8h8e8o9r8i8e.
-.wa4hr4
-.wah4rs2
-.wahrs4c
-.wahrs2ch2
-.wahrsche2
-.wahrschei2
-.wahrsche4i4n1l
-.wahrs2cheinl4i2ch
-.wahrscheinlic4hk
-.wahrscheinlichkei2
-.wahrscheinlichkei4t1s2
-.wahrscheinlichkeits3th2e
-.wahrscheinlichkeitsthe1o5r
-.wahrscheinlichkeitstheor2ie4
-.we8r9n8e8r.
-.w1er
-.wer4n1er
-.we8r9t8h8e8r9i8a8n.
-.werth2e
-.werther1i
-.werthe1r2i3a4
-.wertheri2a2n
-.wi8n9c8h8e8s9t8e8r.
-.win2ch
-.winche2
-.winch1es
-.wi8r8t9s8c8h8a8f8t.
-.w4ir4
-.wir4t1s2
-.wirt4sc
-.wirts2ch2
-.wirtscha2f
-.wirtscha2ft
-.wi8s9s8e8n9s8c8h8a8f8t9l8i8c8h.
-.w4i2s1s
-.wissen4
-.wisse2n1s2
-.wissens4c
-.wissens2ch2
-.wissenscha2f
-.wissenscha2ft
-.wissenschaf2tl
-.wissens2chaftl4i2ch
-.xv8i8i8i8t8h.
-.xv4i5i4
-.xvi4i5i4
-.xvii2ith
-.xv8i8i8t8h.
-.xvi2ith
-.xx8i8i8i8r8d.
-.xx4
-.xx3i
-.xx4i5i4
-.xxi4i5i4
-.xxii4ir
-.xx8i8i8n8d.
-.xxi4ind
-.yi8n8g9y8o8n8g.
-.y1i
-.yin2gy
-.yingy1o4
-.yingyo2n
-.sh8u9x8u8e.
-.shux1u3
-.ji9s8u8a8n.
-.ji2su
-.jisua2n
-.ze8a9l8a8n8d.
-.2ze
-.zea4l
-.zea3l4and
-.zeala2n
-.ze8i8t9s8c8h8r8i8f8t.
-.zei2
-.zei4t1s2
-.zeit4sc
-.zeits2ch2
-.zeitsc4hr4
-.zeitschr4i2ft
diff --git a/core/res/assets/webkit/incognito_mode_start_page.html b/core/res/assets/webkit/incognito_mode_start_page.html
deleted file mode 100644
index 5d7a3fb..0000000
--- a/core/res/assets/webkit/incognito_mode_start_page.html
+++ /dev/null
@@ -1,24 +0,0 @@
-<html>
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
-    <title>New incognito window</title>
-  </head>
-  <body>
-    <p><strong>You've gone incognito</strong>. Pages you view in this window
-      won't appear in your browser history or search history, and they won't
-      leave other traces, like cookies, on your device after you close the
-      incognito window. Any files you download or bookmarks you create will be
-      preserved, however.</p>
-
-    <p><strong>Going incognito doesn't affect the behavior of other people,
-      servers, or software. Be wary of:</strong></p>
-
-    <ul>
-      <li>Websites that collect or share information about you</li>
-      <li>Internet service providers or employers that track the pages you visit</li>
-      <li>Malicious software that tracks your keystrokes in exchange for free smileys</li>
-      <li>Surveillance by secret agents</li>
-      <li>People standing behind you</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/assets/webkit/missingImage.png b/core/res/assets/webkit/missingImage.png
deleted file mode 100644
index f49a98d..0000000
--- a/core/res/assets/webkit/missingImage.png
+++ /dev/null
Binary files differ
diff --git a/core/res/assets/webkit/nullPlugin.png b/core/res/assets/webkit/nullPlugin.png
deleted file mode 100644
index 96a52e3..0000000
--- a/core/res/assets/webkit/nullPlugin.png
+++ /dev/null
Binary files differ
diff --git a/core/res/assets/webkit/play.png b/core/res/assets/webkit/play.png
deleted file mode 100644
index 26fe286..0000000
--- a/core/res/assets/webkit/play.png
+++ /dev/null
Binary files differ
diff --git a/core/res/assets/webkit/textAreaResizeCorner.png b/core/res/assets/webkit/textAreaResizeCorner.png
deleted file mode 100644
index 777eff0..0000000
--- a/core/res/assets/webkit/textAreaResizeCorner.png
+++ /dev/null
Binary files differ
diff --git a/core/res/assets/webkit/togglePlugin.png b/core/res/assets/webkit/togglePlugin.png
deleted file mode 100644
index 008333c..0000000
--- a/core/res/assets/webkit/togglePlugin.png
+++ /dev/null
Binary files differ
diff --git a/core/res/assets/webkit/youtube.html b/core/res/assets/webkit/youtube.html
deleted file mode 100644
index 8e103c1..0000000
--- a/core/res/assets/webkit/youtube.html
+++ /dev/null
@@ -1,71 +0,0 @@
-<html>
-  <head>
-    <style type="text/css">
-      body      { background-color: black; }
-      a:hover   { text-decoration: none; }
-      a:link    { color: black; }
-      a:visited { color: black; }
-      #main {
-        position: absolute;
-        left: 0%;
-        top: 0%;
-        width: 100%;
-        height: 100%;
-        padding: 0%;
-        z-index: 10;
-        background-size: 100%;
-        background: no-repeat;
-        background-position: center;
-      }
-      #play {
-        position: absolute;
-        left: 50%;
-        top: 50%;
-      }
-      #logo {
-        position: absolute;
-        bottom: 0;
-        right: 0;
-      }
-    </style>
-  </head>
-  <body id="body">
-  <script type="text/javascript">
-    function setup() {
-        var width = document.body.clientWidth;
-        var height = document.body.clientHeight;
-        var mainElement = document.getElementById("main");
-        var playElement = document.getElementById("play");
-        var loadcount = 0;
-        var POSTER = "http://img.youtube.com/vi/VIDEO_ID/0.jpg";
-
-        function doload() {
-            if (++loadcount == 2) {
-                // Resize the element to the right size
-                mainElement.width = width;
-                mainElement.height = height;
-                mainElement.style.backgroundImage = "url('" + POSTER + "')";
-                // Center the play button
-                playElement.style.marginTop = "-" + play.height/2 + "px";
-                playElement.style.marginLeft = "-" + play.width/2 + "px";
-                playElement.addEventListener("click", function(e) {
-                    top.location.href = "vnd.youtube:VIDEO_ID";
-                }, false);
-            }
-        }
-        var background = new Image();
-        background.onload = doload;
-        background.src = POSTER;
-        play = new Image();
-        play.onload = doload;
-        play.src = "play.png";
-    }
-
-    window.onload = setup;
-  </script>
-    <div id="main">
-        <img src="play.png" id="play"></img>
-        <img src="youtube.png" id="logo"></img>
-    </div>
-  </body>
-</html>
diff --git a/core/res/assets/webkit/youtube.png b/core/res/assets/webkit/youtube.png
deleted file mode 100644
index 87779b1..0000000
--- a/core/res/assets/webkit/youtube.png
+++ /dev/null
Binary files differ
diff --git a/core/res/res/anim-watch/progress_indeterminate_material.xml b/core/res/res/anim-watch/progress_indeterminate_material.xml
new file mode 100644
index 0000000..8f00d6c
--- /dev/null
+++ b/core/res/res/anim-watch/progress_indeterminate_material.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<set xmlns:android="http://schemas.android.com/apk/res/android" >
+    <objectAnimator
+        android:duration="3333"
+        android:interpolator="@interpolator/trim_start_interpolator"
+        android:propertyName="trimPathStart"
+        android:repeatCount="-1"
+        android:valueFrom="0"
+        android:valueTo="0.75"
+        android:valueType="floatType" />
+    <objectAnimator
+        android:duration="3333"
+        android:interpolator="@interpolator/trim_end_interpolator"
+        android:propertyName="trimPathEnd"
+        android:repeatCount="-1"
+        android:valueFrom="0"
+        android:valueTo="0.75"
+        android:valueType="floatType" />
+    <objectAnimator
+        android:duration="3333"
+        android:interpolator="@interpolator/trim_offset_interpolator"
+        android:propertyName="trimPathOffset"
+        android:repeatCount="-1"
+        android:valueFrom="0"
+        android:valueTo="0.25"
+        android:valueType="floatType" />
+</set>
diff --git a/core/res/res/anim-watch/progress_indeterminate_rotation_material.xml b/core/res/res/anim-watch/progress_indeterminate_rotation_material.xml
new file mode 100644
index 0000000..63e66ec
--- /dev/null
+++ b/core/res/res/anim-watch/progress_indeterminate_rotation_material.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
+    android:interpolator="@interpolator/progress_indeterminate_rotation_interpolator"
+    android:duration="16666"
+    android:propertyName="rotation"
+    android:repeatCount="-1"
+    android:valueFrom="0"
+    android:valueTo="720"
+    android:valueType="floatType" />
diff --git a/core/res/res/anim/dock_bottom_exit_keyguard.xml b/core/res/res/anim/dock_bottom_exit_keyguard.xml
new file mode 100644
index 0000000..4de3ce5
--- /dev/null
+++ b/core/res/res/anim/dock_bottom_exit_keyguard.xml
@@ -0,0 +1,22 @@
+<!--
+  ~ Copyright (C) 2016 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+
+<!-- Animation for when a dock window at the bottom of the screen is exiting while on Keyguard -->
+<set xmlns:android="http://schemas.android.com/apk/res/android"
+    android:interpolator="@android:interpolator/fast_out_linear_in">
+    <translate android:fromYDelta="0" android:toYDelta="100%"
+        android:duration="200"/>
+</set>
\ No newline at end of file
diff --git a/core/res/res/color/hint_foreground_material_dark.xml b/core/res/res/color/hint_foreground_material_dark.xml
index 77883d9..5cc9559 100644
--- a/core/res/res/color/hint_foreground_material_dark.xml
+++ b/core/res/res/color/hint_foreground_material_dark.xml
@@ -15,6 +15,10 @@
 -->
 
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:state_enabled="true"
+          android:state_pressed="true"
+          android:alpha="@dimen/hint_pressed_alpha_material_dark"
+          android:color="@color/foreground_material_dark" />
     <item android:alpha="@dimen/hint_alpha_material_dark"
           android:color="@color/foreground_material_dark" />
 </selector>
diff --git a/core/res/res/color/hint_foreground_material_light.xml b/core/res/res/color/hint_foreground_material_light.xml
index 99168fd..f7465e0 100644
--- a/core/res/res/color/hint_foreground_material_light.xml
+++ b/core/res/res/color/hint_foreground_material_light.xml
@@ -15,6 +15,10 @@
 -->
 
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:state_enabled="true"
+          android:state_pressed="true"
+          android:alpha="@dimen/hint_pressed_alpha_material_light"
+          android:color="@color/foreground_material_light" />
     <item android:alpha="@dimen/hint_alpha_material_light"
           android:color="@color/foreground_material_light" />
 </selector>
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/core/res/res/color/watch_switch_thumb_color_material.xml
similarity index 64%
copy from packages/SystemUI/res/layout/night_mode_settings.xml
copy to core/res/res/color/watch_switch_thumb_color_material.xml
index 3725e78..f78d9b6 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/core/res/res/color/watch_switch_thumb_color_material.xml
@@ -1,24 +1,18 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- Copyright (C) 2016 The Android Open Source Project
-
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
-
           http://www.apache.org/licenses/LICENSE-2.0
-
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-
-    <include layout="@layout/switch_bar" />
-
-</LinearLayout>
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:color="?attr/colorButtonNormal" android:alpha="?attr/disabledAlpha"
+            android:state_enabled="false" />
+    <item android:color="?attr/colorControlActivated" android:state_checked="true" />
+    <item android:color="?attr/colorButtonNormal" />
+</selector>
diff --git a/core/res/res/drawable-hdpi/vpn_connected.png b/core/res/res/drawable-hdpi/vpn_connected.png
index c3547e8..e05e76f 100644
--- a/core/res/res/drawable-hdpi/vpn_connected.png
+++ b/core/res/res/drawable-hdpi/vpn_connected.png
Binary files differ
diff --git a/core/res/res/drawable-hdpi/vpn_disconnected.png b/core/res/res/drawable-hdpi/vpn_disconnected.png
index 10a9065..3508984 100644
--- a/core/res/res/drawable-hdpi/vpn_disconnected.png
+++ b/core/res/res/drawable-hdpi/vpn_disconnected.png
Binary files differ
diff --git a/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_14w.png b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_14w.png
new file mode 100644
index 0000000..371469c
--- /dev/null
+++ b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_14w.png
Binary files differ
diff --git a/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_15w.png b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_15w.png
new file mode 100644
index 0000000..e477260
--- /dev/null
+++ b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_15w.png
Binary files differ
diff --git a/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_16w.png b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_16w.png
new file mode 100644
index 0000000..19a1bd3
--- /dev/null
+++ b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_16w.png
Binary files differ
diff --git a/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_17w.png b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_17w.png
new file mode 100644
index 0000000..79dc733
--- /dev/null
+++ b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_17w.png
Binary files differ
diff --git a/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_18w.png b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_18w.png
new file mode 100644
index 0000000..6d921c0
--- /dev/null
+++ b/core/res/res/drawable-hdpi/watch_switch_thumb_mtrl_18w.png
Binary files differ
diff --git a/core/res/res/drawable-hdpi/watch_switch_track_mtrl_alpha.png b/core/res/res/drawable-hdpi/watch_switch_track_mtrl_alpha.png
new file mode 100644
index 0000000..ecee3e1
--- /dev/null
+++ b/core/res/res/drawable-hdpi/watch_switch_track_mtrl_alpha.png
Binary files differ
diff --git a/core/res/res/drawable-mdpi/vpn_connected.png b/core/res/res/drawable-mdpi/vpn_connected.png
index 7e167f8..f7ac2a1 100644
--- a/core/res/res/drawable-mdpi/vpn_connected.png
+++ b/core/res/res/drawable-mdpi/vpn_connected.png
Binary files differ
diff --git a/core/res/res/drawable-mdpi/vpn_disconnected.png b/core/res/res/drawable-mdpi/vpn_disconnected.png
index a08c42a..9db4199 100644
--- a/core/res/res/drawable-mdpi/vpn_disconnected.png
+++ b/core/res/res/drawable-mdpi/vpn_disconnected.png
Binary files differ
diff --git a/core/res/res/drawable-nodpi/vpn_connected.xml b/core/res/res/drawable-nodpi/vpn_connected.xml
new file mode 100644
index 0000000..22a4a6cb4
--- /dev/null
+++ b/core/res/res/drawable-nodpi/vpn_connected.xml
@@ -0,0 +1,24 @@
+<!--
+Copyright (C) 2016 The Android Open Source Project
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="48dp"
+        android:height="48dp"
+        android:viewportWidth="48.0"
+        android:viewportHeight="48.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M25.3,20c-1.65,-4.66 -6.08,-8 -11.3,-8 -6.63,0 -12,5.37 -12,12s5.37,12 12,12c5.22,0 9.65,-3.34 11.3,-8H34v8h8v-8h4v-8H25.3zM14,28c-2.21,0 -4,-1.79 -4,-4s1.79,-4 4,-4 4,1.79 4,4 -1.79,4 -4,4z"/>
+</vector>
diff --git a/core/res/res/drawable-nodpi/vpn_disconnected.xml b/core/res/res/drawable-nodpi/vpn_disconnected.xml
new file mode 100644
index 0000000..22a4a6cb4
--- /dev/null
+++ b/core/res/res/drawable-nodpi/vpn_disconnected.xml
@@ -0,0 +1,24 @@
+<!--
+Copyright (C) 2016 The Android Open Source Project
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="48dp"
+        android:height="48dp"
+        android:viewportWidth="48.0"
+        android:viewportHeight="48.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M25.3,20c-1.65,-4.66 -6.08,-8 -11.3,-8 -6.63,0 -12,5.37 -12,12s5.37,12 12,12c5.22,0 9.65,-3.34 11.3,-8H34v8h8v-8h4v-8H25.3zM14,28c-2.21,0 -4,-1.79 -4,-4s1.79,-4 4,-4 4,1.79 4,4 -1.79,4 -4,4z"/>
+</vector>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/drawable-watch/dialog_background_material.xml
similarity index 73%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/drawable-watch/dialog_background_material.xml
index 9e13001..de52f08 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/drawable-watch/dialog_background_material.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -13,8 +13,7 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
-</resources>
+
+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
+    <solid android:color="?attr/colorBackground" />
+</shape>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/drawable-watch/ic_input_extract_action_done.xml
similarity index 60%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/drawable-watch/ic_input_extract_action_done.xml
index 9e13001..a04b944 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/drawable-watch/ic_input_extract_action_done.xml
@@ -1,5 +1,5 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!--
+     Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -13,8 +13,7 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
-</resources>
+<vector android:height="22dp" android:viewportHeight="22.0"
+    android:viewportWidth="22.0" android:width="22dp" xmlns:android="http://schemas.android.com/apk/res/android">
+    <path android:fillColor="#FFFFFF" android:pathData="M9.25,14.82L5.43,11l-1.3,1.3 5.12,5.12 11,-11 -1.3,-1.3 -9.7,9.7z"/>
+</vector>
diff --git a/core/res/res/drawable-watch/ic_input_extract_action_send.xml b/core/res/res/drawable-watch/ic_input_extract_action_send.xml
new file mode 100644
index 0000000..3689172
--- /dev/null
+++ b/core/res/res/drawable-watch/ic_input_extract_action_send.xml
@@ -0,0 +1,24 @@
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="22dp"
+        android:height="22dp"
+        android:viewportWidth="22.0"
+        android:viewportHeight="22.0">
+    <path
+        android:pathData="M2.77,19.32L22,11.07 2.78,2.82v6.42l13.74,1.83L2.77,12.9v6.42z"
+        android:fillColor="#FFFFFF"/>
+</vector>
diff --git a/core/res/res/drawable-xhdpi/vpn_connected.png b/core/res/res/drawable-xhdpi/vpn_connected.png
index 1f46be2..a8761c9 100644
--- a/core/res/res/drawable-xhdpi/vpn_connected.png
+++ b/core/res/res/drawable-xhdpi/vpn_connected.png
Binary files differ
diff --git a/core/res/res/drawable-xhdpi/vpn_disconnected.png b/core/res/res/drawable-xhdpi/vpn_disconnected.png
index 847d3f5..7118918 100644
--- a/core/res/res/drawable-xhdpi/vpn_disconnected.png
+++ b/core/res/res/drawable-xhdpi/vpn_disconnected.png
Binary files differ
diff --git a/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_14w.png b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_14w.png
new file mode 100644
index 0000000..7f7ca14
--- /dev/null
+++ b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_14w.png
Binary files differ
diff --git a/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_15w.png b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_15w.png
new file mode 100644
index 0000000..52120b8
--- /dev/null
+++ b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_15w.png
Binary files differ
diff --git a/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_16w.png b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_16w.png
new file mode 100644
index 0000000..d6e9be9
--- /dev/null
+++ b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_16w.png
Binary files differ
diff --git a/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_17w.png b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_17w.png
new file mode 100644
index 0000000..8d76393
--- /dev/null
+++ b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_17w.png
Binary files differ
diff --git a/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_18w.png b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_18w.png
new file mode 100644
index 0000000..ca9c66e
--- /dev/null
+++ b/core/res/res/drawable-xhdpi/watch_switch_thumb_mtrl_18w.png
Binary files differ
diff --git a/core/res/res/drawable-xhdpi/watch_switch_track_mtrl_alpha.png b/core/res/res/drawable-xhdpi/watch_switch_track_mtrl_alpha.png
new file mode 100644
index 0000000..1aa5442
--- /dev/null
+++ b/core/res/res/drawable-xhdpi/watch_switch_track_mtrl_alpha.png
Binary files differ
diff --git a/core/res/res/drawable-xxhdpi/vpn_connected.png b/core/res/res/drawable-xxhdpi/vpn_connected.png
index ea4930c..16b1e4e 100644
--- a/core/res/res/drawable-xxhdpi/vpn_connected.png
+++ b/core/res/res/drawable-xxhdpi/vpn_connected.png
Binary files differ
diff --git a/core/res/res/drawable-xxhdpi/vpn_disconnected.png b/core/res/res/drawable-xxhdpi/vpn_disconnected.png
index 4cd0dd4..a025818 100644
--- a/core/res/res/drawable-xxhdpi/vpn_disconnected.png
+++ b/core/res/res/drawable-xxhdpi/vpn_disconnected.png
Binary files differ
diff --git a/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_14w.png b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_14w.png
new file mode 100644
index 0000000..c0d72d7
--- /dev/null
+++ b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_14w.png
Binary files differ
diff --git a/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_15w.png b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_15w.png
new file mode 100644
index 0000000..d7c0ec0
--- /dev/null
+++ b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_15w.png
Binary files differ
diff --git a/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_16w.png b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_16w.png
new file mode 100644
index 0000000..5815ba9
--- /dev/null
+++ b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_16w.png
Binary files differ
diff --git a/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_17w.png b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_17w.png
new file mode 100644
index 0000000..41da8c0
--- /dev/null
+++ b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_17w.png
Binary files differ
diff --git a/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_18w.png b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_18w.png
new file mode 100644
index 0000000..975eb01
--- /dev/null
+++ b/core/res/res/drawable-xxhdpi/watch_switch_thumb_mtrl_18w.png
Binary files differ
diff --git a/core/res/res/drawable-xxhdpi/watch_switch_track_mtrl_alpha.png b/core/res/res/drawable-xxhdpi/watch_switch_track_mtrl_alpha.png
new file mode 100644
index 0000000..af2042b
--- /dev/null
+++ b/core/res/res/drawable-xxhdpi/watch_switch_track_mtrl_alpha.png
Binary files differ
diff --git a/core/res/res/drawable/emergency_icon.xml b/core/res/res/drawable/emergency_icon.xml
new file mode 100644
index 0000000..b2ffa2b
--- /dev/null
+++ b/core/res/res/drawable/emergency_icon.xml
@@ -0,0 +1,40 @@
+<!--
+Copyright (C) 2014 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="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0"
+        android:tint="?attr/colorControlNormal">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M6.8,17.3C5.3,15.9 4.5,14.0 4.5,12.0c0.0,-2.0 0.8,-3.8 2.1,-5.2l1.4,1.4c-1.0,1.0 -1.6,2.4 -1.6,3.8c0.0,1.5 0.6,2.9 1.6,3.9L6.8,17.3z"/>
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M3.3,20.2C1.2,18.0 0.0,15.1 0.0,12.0c0.0,-3.1 1.2,-6.0 3.3,-8.2l1.4,1.4C3.0,7.0 2.0,9.4 2.0,12.0s1.0,5.0 2.7,6.9L3.3,20.2z"/>
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M17.2,17.3l-1.4,-1.4c1.1,-1.0 1.6,-2.4 1.6,-3.9c0.0,-1.4 -0.6,-2.8 -1.6,-3.8l1.4,-1.4c1.4,1.4 2.1,3.3 2.1,5.2C19.5,14.0 18.7,15.9 17.2,17.3z"/>
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M20.7,20.2l-1.4,-1.4C21.0,17.0 22.0,14.6 22.0,12.0c0.0,-2.6 -1.0,-5.0 -2.7,-6.9l1.4,-1.4C22.8,6.0 24.0,8.9 24.0,12.0C24.0,15.1 22.8,18.0 20.7,20.2z"/>
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M11.0,15.0l2.0,0.0l0.0,2.0l-2.0,0.0z"/>
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M11.0,7.0l2.0,0.0l0.0,6.0l-2.0,0.0z"/>
+</vector>
diff --git a/core/res/res/drawable/ic_check_circle_24px.xml b/core/res/res/drawable/ic_check_circle_24px.xml
index 066a8a7..e9af9e4 100644
--- a/core/res/res/drawable/ic_check_circle_24px.xml
+++ b/core/res/res/drawable/ic_check_circle_24px.xml
@@ -19,9 +19,6 @@
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:pathData="M0 0h24v24H0z"
-        android:fillColor="#00000000"/>
-    <path
         android:fillColor="#FF000000"
         android:pathData="M12.0,2.0C6.48,2.0 2.0,6.48 2.0,12.0s4.48,10.0 10.0,10.0 10.0,-4.48 10.0,-10.0S17.52,2.0 12.0,2.0zm-2.0,15.0l-5.0,-5.0 1.41,-1.41L10.0,14.17l7.59,-7.59L19.0,8.0l-9.0,9.0z"/>
 </vector>
diff --git a/core/res/res/drawable/ic_collapse_notification.xml b/core/res/res/drawable/ic_collapse_notification.xml
index 603c159..124e99e 100644
--- a/core/res/res/drawable/ic_collapse_notification.xml
+++ b/core/res/res/drawable/ic_collapse_notification.xml
@@ -22,7 +22,4 @@
     <path
         android:fillColor="#FF000000"
         android:pathData="M12.0,8.0l-6.0,6.0l1.4,1.4l4.6,-4.6l4.6,4.6L18.0,14.0L12.0,8.0z"/>
-    <path
-        android:pathData="M0,0h24v24H0V0z"
-        android:fillColor="#00000000"/>
 </vector>
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_apk.xml b/core/res/res/drawable/ic_doc_apk.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_apk.xml
rename to core/res/res/drawable/ic_doc_apk.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_audio.xml b/core/res/res/drawable/ic_doc_audio.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_audio.xml
rename to core/res/res/drawable/ic_doc_audio.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_certificate.xml b/core/res/res/drawable/ic_doc_certificate.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_certificate.xml
rename to core/res/res/drawable/ic_doc_certificate.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_codes.xml b/core/res/res/drawable/ic_doc_codes.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_codes.xml
rename to core/res/res/drawable/ic_doc_codes.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_compressed.xml b/core/res/res/drawable/ic_doc_compressed.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_compressed.xml
rename to core/res/res/drawable/ic_doc_compressed.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_contact.xml b/core/res/res/drawable/ic_doc_contact.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_contact.xml
rename to core/res/res/drawable/ic_doc_contact.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_document.xml b/core/res/res/drawable/ic_doc_document.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_document.xml
rename to core/res/res/drawable/ic_doc_document.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_event.xml b/core/res/res/drawable/ic_doc_event.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_event.xml
rename to core/res/res/drawable/ic_doc_event.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_excel.xml b/core/res/res/drawable/ic_doc_excel.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_excel.xml
rename to core/res/res/drawable/ic_doc_excel.xml
diff --git a/core/res/res/drawable/ic_doc_folder.xml b/core/res/res/drawable/ic_doc_folder.xml
new file mode 100644
index 0000000..dcbce01
--- /dev/null
+++ b/core/res/res/drawable/ic_doc_folder.xml
@@ -0,0 +1,24 @@
+<!--
+Copyright (C) 2015 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">
+    <path
+        android:fillColor="#FF737373"
+        android:pathData="M10 4H4c-1.1 0,-1.99.9,-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2,-.9 2,-2V8c0,-1.1,-.9,-2,-2,-2h-8l-2,-2z"/>
+</vector>
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_font.xml b/core/res/res/drawable/ic_doc_font.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_font.xml
rename to core/res/res/drawable/ic_doc_font.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_generic.xml b/core/res/res/drawable/ic_doc_generic.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_generic.xml
rename to core/res/res/drawable/ic_doc_generic.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_image.xml b/core/res/res/drawable/ic_doc_image.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_image.xml
rename to core/res/res/drawable/ic_doc_image.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_pdf.xml b/core/res/res/drawable/ic_doc_pdf.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_pdf.xml
rename to core/res/res/drawable/ic_doc_pdf.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_powerpoint.xml b/core/res/res/drawable/ic_doc_powerpoint.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_powerpoint.xml
rename to core/res/res/drawable/ic_doc_powerpoint.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_presentation.xml b/core/res/res/drawable/ic_doc_presentation.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_presentation.xml
rename to core/res/res/drawable/ic_doc_presentation.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_spreadsheet.xml b/core/res/res/drawable/ic_doc_spreadsheet.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_spreadsheet.xml
rename to core/res/res/drawable/ic_doc_spreadsheet.xml
diff --git a/core/res/res/drawable/ic_doc_text.xml b/core/res/res/drawable/ic_doc_text.xml
new file mode 100644
index 0000000..7fc04e8
--- /dev/null
+++ b/core/res/res/drawable/ic_doc_text.xml
@@ -0,0 +1,24 @@
+<!--
+Copyright (C) 2015 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">
+    <path
+        android:fillColor="#FF737373"
+        android:pathData="M14 2H6c-1.1 0,-1.99.9,-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2,-.9 2,-2V8l-6,-6zm2 16H8v-2h8v2zm0,-4H8v-2h8v2zm-3,-5V3.5L18.5 9H13z"/>
+</vector>
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_video.xml b/core/res/res/drawable/ic_doc_video.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_video.xml
rename to core/res/res/drawable/ic_doc_video.xml
diff --git a/packages/DocumentsUI/res/drawable/ic_doc_word.xml b/core/res/res/drawable/ic_doc_word.xml
similarity index 100%
rename from packages/DocumentsUI/res/drawable/ic_doc_word.xml
rename to core/res/res/drawable/ic_doc_word.xml
diff --git a/core/res/res/drawable/ic_expand_more_48dp.xml b/core/res/res/drawable/ic_expand_more_48dp.xml
index 11323e3..5a71669 100644
--- a/core/res/res/drawable/ic_expand_more_48dp.xml
+++ b/core/res/res/drawable/ic_expand_more_48dp.xml
@@ -21,7 +21,4 @@
     <path
         android:fillColor="#FF000000"
         android:pathData="M33.17,17.17L24.0,26.34l-9.17,-9.17L12.0,20.0l12.0,12.0 12.0,-12.0z"/>
-    <path
-        android:pathData="M0 0h48v48H0z"
-        android:fillColor="#00000000"/>
 </vector>
diff --git a/core/res/res/drawable/ic_expand_notification.xml b/core/res/res/drawable/ic_expand_notification.xml
index db7d3eb..847e326 100644
--- a/core/res/res/drawable/ic_expand_notification.xml
+++ b/core/res/res/drawable/ic_expand_notification.xml
@@ -22,7 +22,4 @@
     <path
         android:fillColor="#FF000000"
         android:pathData="M16.6,8.6L12.0,13.2L7.4,8.6L6.0,10.0l6.0,6.0l6.0,-6.0L16.6,8.6z"/>
-    <path
-        android:pathData="M0,0h24v24H0V0z"
-        android:fillColor="#00000000"/>
 </vector>
diff --git a/core/res/res/drawable/ic_feedback.xml b/core/res/res/drawable/ic_feedback.xml
index b2d1cb8..365863d 100644
--- a/core/res/res/drawable/ic_feedback.xml
+++ b/core/res/res/drawable/ic_feedback.xml
@@ -19,9 +19,6 @@
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:pathData="M0 0h24v24H0z"
-        android:fillColor="#00000000"/>
-    <path
         android:fillColor="#FF000000"
         android:pathData="M20.0,2.0L4.0,2.0c-1.1,0.0 -1.9,0.9 -1.99,2.0L2.0,22.0l4.0,-4.0l14.0,0.0c1.1,0.0 2.0,-0.9 2.0,-2.0L22.0,4.0c0.0,-1.1 -0.9,-2.0 -2.0,-2.0zm-7.0,12.0l-2.0,0.0l0.0,-2.0l2.0,0.0l0.0,2.0zm0.0,-4.0l-2.0,0.0L11.0,6.0l2.0,0.0l0.0,4.0z"/>
 </vector>
diff --git a/core/res/res/drawable/ic_lock_power_off.xml b/core/res/res/drawable/ic_lock_power_off.xml
index 718f17e..babd1be 100644
--- a/core/res/res/drawable/ic_lock_power_off.xml
+++ b/core/res/res/drawable/ic_lock_power_off.xml
@@ -1,19 +1,25 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2014 The Android Open Source Project
+<!--
+    Copyright (C) 2016 The Android Open Source Project
 
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
+    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,
-     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.
+    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.
 -->
-
-<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
-    android:src="@drawable/ic_lock_power_off_alpha"
-    android:tint="?attr/colorControlNormal" />
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportHeight="24.0"
+        android:viewportWidth="24.0"
+        android:tint="?attr/colorControlNormal">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M13.0,3.0l-2.0,0.0l0.0,10.0l2.0,0.0L13.0,3.0zm4.83,2.17l-1.42,1.42C17.99,7.86 19.0,9.81 19.0,12.0c0.0,3.87 -3.13,7.0 -7.0,7.0s-7.0,-3.13 -7.0,-7.0c0.0,-2.19 1.01,-4.14 2.58,-5.42L6.17,5.17C4.23,6.82 3.0,9.26 3.0,12.0c0.0,4.97 4.03,9.0 9.0,9.0s9.0,-4.03 9.0,-9.0c0.0,-2.74 -1.23,-5.18 -3.17,-6.83z"/>
+</vector>
diff --git a/core/res/res/drawable/ic_more_items.xml b/core/res/res/drawable/ic_more_items.xml
index 5fdcdce..0ec754c 100644
--- a/core/res/res/drawable/ic_more_items.xml
+++ b/core/res/res/drawable/ic_more_items.xml
@@ -24,6 +24,5 @@
         android:fillColor="#000000"
         android:pathData="M3 13h2v-2H3v2zm0 4h2v-2H3v2zm0-8h2V7H3v2zm4 4h14v-2H7v2zm0 4h14v-2H7v2zM7
 7v2h14V7H7z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
+
 </vector>
\ No newline at end of file
diff --git a/core/res/res/drawable/ic_refresh.xml b/core/res/res/drawable/ic_refresh.xml
index 1f67168..1297407 100644
--- a/core/res/res/drawable/ic_refresh.xml
+++ b/core/res/res/drawable/ic_refresh.xml
@@ -21,7 +21,4 @@
     <path
         android:fillColor="#FF000000"
         android:pathData="M17.65,6.35C16.2,4.9 14.21,4.0 12.0,4.0c-4.42,0.0 -7.99,3.58 -7.99,8.0s3.57,8.0 7.99,8.0c3.73,0.0 6.84,-2.55 7.73,-6.0l-2.08,0.0c-0.82,2.33 -3.04,4.0 -5.65,4.0 -3.31,0.0 -6.0,-2.69 -6.0,-6.0s2.69,-6.0 6.0,-6.0c1.66,0.0 3.1,0.69 4.22,1.78L13.0,11.0l7.0,0.0L20.0,4.0l-2.35,2.35z"/>
-    <path
-        android:pathData="M0 0h24v24H0z"
-        android:fillColor="#00000000"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_night_mode_disabled.xml b/core/res/res/drawable/ic_restart.xml
similarity index 60%
copy from packages/SystemUI/res/drawable/ic_night_mode_disabled.xml
copy to core/res/res/drawable/ic_restart.xml
index 010815a..47ac483 100644
--- a/packages/SystemUI/res/drawable/ic_night_mode_disabled.xml
+++ b/core/res/res/drawable/ic_restart.xml
@@ -16,9 +16,13 @@
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:width="24.0dp"
         android:height="24.0dp"
+        android:viewportHeight="24.0"
         android:viewportWidth="24.0"
-        android:viewportHeight="24.0">
+        android:tint="?attr/colorControlNormal">
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M20.71,5.63l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0.0l-3.12,3.12 -1.93,-1.91 -1.41,1.41 1.42,1.42L3.0,16.25L3.0,21.0l4.75,0.0l8.92,-8.92 1.42,1.42 1.41,-1.41 -1.92,-1.92 3.12,-3.12c0.4,0.0 0.4,-1.0 0.01,-1.42zM6.92,19.0L5.0,17.08l8.06,-8.06 1.92,1.92L6.92,19.0z"/>
+        android:fillColor="#FF000000"
+        android:pathData="M12.0,4.0L12.0,1.0L8.0,5.0l4.0,4.0L12.0,6.0c3.9,0.0 7.0,3.1 7.0,7.0c0.0,3.9 -3.1,7.0 -7.0,7.0l0.0,2.0c5.0,0.0 9.0,-4.0 9.0,-9.0C21.0,8.0 17.0,4.0 12.0,4.0z"/>
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M5.0,12.9C5.0,11.0 5.8,9.2 7.2,7.9L5.8,6.4C4.0,8.1 3.0,10.5 3.0,12.9c0.0,4.0 2.7,7.6 6.5,8.7l0.5,-1.9C7.1,18.8 5.0,16.1 5.0,12.9z"/>
 </vector>
diff --git a/core/res/res/drawable/ic_schedule.xml b/core/res/res/drawable/ic_schedule.xml
index 899dc82..55d54b1 100644
--- a/core/res/res/drawable/ic_schedule.xml
+++ b/core/res/res/drawable/ic_schedule.xml
@@ -22,9 +22,6 @@
         android:fillColor="#FF000000"
         android:pathData="M11.99,2.0C6.47,2.0 2.0,6.48 2.0,12.0s4.47,10.0 9.99,10.0C17.52,22.0 22.0,17.52 22.0,12.0S17.52,2.0 11.99,2.0zM12.0,20.0c-4.42,0.0 -8.0,-3.58 -8.0,-8.0s3.58,-8.0 8.0,-8.0 8.0,3.58 8.0,8.0 -3.58,8.0 -8.0,8.0z"/>
     <path
-        android:pathData="M0 0h24v24H0z"
-        android:fillColor="#00000000"/>
-    <path
         android:fillColor="#FF000000"
         android:pathData="M12.5,7.0L11.0,7.0l0.0,6.0l5.25,3.1 0.75,-1.23 -4.5,-2.67z"/>
 </vector>
diff --git a/core/res/res/drawable/perm_group_calendar.xml b/core/res/res/drawable/perm_group_calendar.xml
index 4dc7b37..85a783e 100644
--- a/core/res/res/drawable/perm_group_calendar.xml
+++ b/core/res/res/drawable/perm_group_calendar.xml
@@ -24,6 +24,5 @@
         android:fillColor="#000000"
         android:pathData="M17 12h-5v5h5v-5zM16 1v2H8V1H6v2H5c-1.11 0-1.99 .9 -1.99 2L3 19c0 1.1 .89 2 2
 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2h-1V1h-2zm3 18H5V8h14v11z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
+
 </vector>
\ No newline at end of file
diff --git a/core/res/res/drawable/perm_group_camera.xml b/core/res/res/drawable/perm_group_camera.xml
index 741a40e..61903a5 100644
--- a/core/res/res/drawable/perm_group_camera.xml
+++ b/core/res/res/drawable/perm_group_camera.xml
@@ -28,6 +28,4 @@
         android:pathData="M9 2L7.17 4H4c-1.1 0-2 .9-2 2v12c0 1.1 .9 2 2 2h16c1.1 0 2-.9
 2-2V6c0-1.1-.9-2-2-2h-3.17L15 2H9zm3 15c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5
 5-2.24 5-5 5z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
 </vector>
\ No newline at end of file
diff --git a/core/res/res/drawable/perm_group_contacts.xml b/core/res/res/drawable/perm_group_contacts.xml
index d698fd1..8f9dc3e 100644
--- a/core/res/res/drawable/perm_group_contacts.xml
+++ b/core/res/res/drawable/perm_group_contacts.xml
@@ -21,8 +21,6 @@
     android:viewportHeight="24">
 
     <path
-        android:pathData="M0 0h24v24H0zm0 0h24v24H0zm0 0h24v24H0z" />
-    <path
         android:fillColor="#000000"
         android:pathData="M20 0H4v2h16V0zM4 24h16v-2H4v2zM20 4H4c-1.1 0-2 .9-2 2v12c0 1.1 .9 2 2 2h16c1.1
 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm-8 2.75c1.24 0 2.25 1.01 2.25 2.25s-1.01 2.25-2.25
diff --git a/core/res/res/drawable/perm_group_location.xml b/core/res/res/drawable/perm_group_location.xml
index fbc6066..cc1ec90 100644
--- a/core/res/res/drawable/perm_group_location.xml
+++ b/core/res/res/drawable/perm_group_location.xml
@@ -24,6 +24,4 @@
         android:fillColor="#000000"
         android:pathData="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0
 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
 </vector>
\ No newline at end of file
diff --git a/core/res/res/drawable/perm_group_microphone.xml b/core/res/res/drawable/perm_group_microphone.xml
index c565d20..d494e67 100644
--- a/core/res/res/drawable/perm_group_microphone.xml
+++ b/core/res/res/drawable/perm_group_microphone.xml
@@ -25,6 +25,4 @@
         android:pathData="M12 14c1.66 0 2.99-1.34 2.99-3L15 5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3
 3 3zm5.3-3c0 3-2.54 5.1-5.3 5.1S6.7 14 6.7 11H5c0 3.41 2.72 6.23 6
 6.72V21h2v-3.28c3.28-.48 6-3.3 6-6.72h-1.7z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
 </vector>
\ No newline at end of file
diff --git a/core/res/res/drawable/perm_group_phone_calls.xml b/core/res/res/drawable/perm_group_phone_calls.xml
index a647894..324d864 100644
--- a/core/res/res/drawable/perm_group_phone_calls.xml
+++ b/core/res/res/drawable/perm_group_phone_calls.xml
@@ -19,9 +19,6 @@
     android:height="24dp"
     android:viewportWidth="24"
     android:viewportHeight="24">
-
-    <path
-        android:pathData="M0 0h24v24H0z" />
     <path
         android:fillColor="#000000"
         android:pathData="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27 .67 -.36 1.02-.24 1.12
diff --git a/core/res/res/drawable/perm_group_sms.xml b/core/res/res/drawable/perm_group_sms.xml
index 9b32c601..47bca19e 100644
--- a/core/res/res/drawable/perm_group_sms.xml
+++ b/core/res/res/drawable/perm_group_sms.xml
@@ -24,6 +24,4 @@
         android:fillColor="#000000"
         android:pathData="M20 2H4c-1.1 0-1.99 .9 -1.99 2L2 22l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zM9
 11H7V9h2v2zm4 0h-2V9h2v2zm4 0h-2V9h2v2z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
 </vector>
\ No newline at end of file
diff --git a/core/res/res/drawable/perm_group_storage.xml b/core/res/res/drawable/perm_group_storage.xml
index 477270d..1ff1693 100644
--- a/core/res/res/drawable/perm_group_storage.xml
+++ b/core/res/res/drawable/perm_group_storage.xml
@@ -24,6 +24,4 @@
         android:fillColor="#000000"
         android:pathData="M10 4H4c-1.1 0-1.99 .9 -1.99 2L2 18c0 1.1 .9 2 2 2h16c1.1 0 2-.9
 2-2V8c0-1.1-.9-2-2-2h-8l-2-2z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
 </vector>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/core/res/res/drawable/progress_indeterminate_anim_large_material.xml
similarity index 60%
copy from packages/SystemUI/res/layout/night_mode_settings.xml
copy to core/res/res/drawable/progress_indeterminate_anim_large_material.xml
index 3725e78..560ec5a 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/core/res/res/drawable/progress_indeterminate_anim_large_material.xml
@@ -13,12 +13,14 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
+<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
+                 android:drawable="@drawable/vector_drawable_progress_bar_large" >
+    <target
+            android:name="progressBar"
+            android:animation="@anim/progress_indeterminate_material" />
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
+    <target
+            android:name="root"
+            android:animation="@anim/progress_indeterminate_rotation_material" />
+</animated-vector>
 
-    <include layout="@layout/switch_bar" />
-
-</LinearLayout>
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/core/res/res/drawable/progress_indeterminate_anim_medium_material.xml
similarity index 62%
copy from packages/SystemUI/res/layout/night_mode_settings.xml
copy to core/res/res/drawable/progress_indeterminate_anim_medium_material.xml
index 3725e78..fbea22f 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/core/res/res/drawable/progress_indeterminate_anim_medium_material.xml
@@ -13,12 +13,15 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
+<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:drawable="@drawable/vector_drawable_progress_bar_medium" >
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
+    <target
+        android:name="progressBar"
+        android:animation="@anim/progress_indeterminate_material" />
 
-    <include layout="@layout/switch_bar" />
+    <target
+        android:name="root"
+        android:animation="@anim/progress_indeterminate_rotation_material" />
 
-</LinearLayout>
+</animated-vector>
\ No newline at end of file
diff --git a/core/res/res/drawable/progress_large_material.xml b/core/res/res/drawable/progress_large_material.xml
index 526f914..ee82e35 100644
--- a/core/res/res/drawable/progress_large_material.xml
+++ b/core/res/res/drawable/progress_large_material.xml
@@ -13,16 +13,8 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-
-<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
-    android:drawable="@drawable/vector_drawable_progress_bar_large" >
-
-    <target
-        android:name="progressBar"
-        android:animation="@anim/progress_indeterminate_material" />
-
-    <target
-        android:name="root"
-        android:animation="@anim/progress_indeterminate_rotation_material" />
-
-</animated-vector>
+<com.android.internal.graphics.drawable.AnimationScaleListDrawable
+        xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:drawable="@drawable/progress_static_material" />
+    <item android:drawable="@drawable/progress_indeterminate_anim_large_material" />
+</com.android.internal.graphics.drawable.AnimationScaleListDrawable>
diff --git a/core/res/res/drawable/progress_medium_material.xml b/core/res/res/drawable/progress_medium_material.xml
index cc35816..5c92600 100644
--- a/core/res/res/drawable/progress_medium_material.xml
+++ b/core/res/res/drawable/progress_medium_material.xml
@@ -13,15 +13,8 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
-    android:drawable="@drawable/vector_drawable_progress_bar_medium" >
-
-    <target
-        android:name="progressBar"
-        android:animation="@anim/progress_indeterminate_material" />
-
-    <target
-        android:name="root"
-        android:animation="@anim/progress_indeterminate_rotation_material" />
-
-</animated-vector>
\ No newline at end of file
+<com.android.internal.graphics.drawable.AnimationScaleListDrawable
+        xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:drawable="@drawable/progress_static_material" />
+    <item android:drawable="@drawable/progress_indeterminate_anim_medium_material" />
+</com.android.internal.graphics.drawable.AnimationScaleListDrawable>
diff --git a/core/res/res/drawable/progress_small_material.xml b/core/res/res/drawable/progress_small_material.xml
index c6e4380..cec9d95 100644
--- a/core/res/res/drawable/progress_small_material.xml
+++ b/core/res/res/drawable/progress_small_material.xml
@@ -13,16 +13,17 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-
-<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
-    android:drawable="@drawable/vector_drawable_progress_bar_small" >
-
-    <target
-        android:name="progressBar"
-        android:animation="@anim/progress_indeterminate_material" />
-
-    <target
-        android:name="root"
-        android:animation="@anim/progress_indeterminate_rotation_material" />
-
-</animated-vector>
+<com.android.internal.graphics.drawable.AnimationScaleListDrawable
+        xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:drawable="@drawable/progress_static_material" />
+    <item>
+        <animated-vector android:drawable="@drawable/vector_drawable_progress_bar_small" >
+            <target
+                    android:name="progressBar"
+                    android:animation="@anim/progress_indeterminate_material" />
+            <target
+                    android:name="root"
+                    android:animation="@anim/progress_indeterminate_rotation_material" />
+        </animated-vector>
+    </item>
+</com.android.internal.graphics.drawable.AnimationScaleListDrawable>
diff --git a/core/res/res/drawable/progress_static_material.xml b/core/res/res/drawable/progress_static_material.xml
new file mode 100644
index 0000000..b078fa9
--- /dev/null
+++ b/core/res/res/drawable/progress_static_material.xml
@@ -0,0 +1,25 @@
+<!--
+Copyright (C) 2016 The Android Open Source Project
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0"
+        >
+    <path
+        android:fillColor="?attr/colorControlActivated"
+        android:pathData="M17.65,6.35C16.2,4.9 14.21,4.0 12.0,4.0c-4.42,0.0 -7.99,3.58 -7.99,8.0s3.57,8.0 7.99,8.0c3.73,0.0 6.84,-2.55 7.73,-6.0l-2.08,0.0c-0.82,2.33 -3.04,4.0 -5.65,4.0 -3.31,0.0 -6.0,-2.69 -6.0,-6.0s2.69,-6.0 6.0,-6.0c1.66,0.0 3.1,0.69 4.22,1.78L13.0,11.0l7.0,0.0L20.0,4.0l-2.35,2.35z"/>
+</vector>
diff --git a/core/res/res/drawable/watch_switch_thumb_material_anim.xml b/core/res/res/drawable/watch_switch_thumb_material_anim.xml
new file mode 100644
index 0000000..9e3e893
--- /dev/null
+++ b/core/res/res/drawable/watch_switch_thumb_material_anim.xml
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+          http://www.apache.org/licenses/LICENSE-2.0
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<animated-selector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:constantSize="true">
+    <item
+        android:id="@+id/off"
+        android:drawable="@drawable/watch_switch_thumb_mtrl_14w"
+        android:state_enabled="false" />
+    <item
+        android:id="@+id/on"
+        android:drawable="@drawable/watch_switch_thumb_mtrl_14w"
+        android:state_checked="true" />
+    <item
+        android:id="@+id/off"
+        android:drawable="@drawable/watch_switch_thumb_mtrl_14w" />
+    <transition
+        android:fromId="@id/off"
+        android:toId="@id/on">
+        <animation-list>
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_14w"
+                android:duration="30" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_15w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_16w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_17w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_18w"
+                android:duration="75" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_17w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_16w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_15w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_14w"
+                android:duration="30" />
+        </animation-list>
+    </transition>
+    <transition
+        android:fromId="@id/on"
+        android:toId="@id/off">
+        <animation-list>
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_14w"
+                android:duration="30" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_15w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_16w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_17w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_18w"
+                android:duration="75" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_17w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_16w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_15w"
+                android:duration="15" />
+            <item
+                android:drawable="@drawable/watch_switch_thumb_mtrl_14w"
+                android:duration="30" />
+        </animation-list>
+    </transition>
+</animated-selector>
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/core/res/res/drawable/watch_switch_track_material.xml
similarity index 65%
copy from packages/SystemUI/res/layout/night_mode_settings.xml
copy to core/res/res/drawable/watch_switch_track_material.xml
index 3725e78..79e92a3 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/core/res/res/drawable/watch_switch_track_material.xml
@@ -1,12 +1,9 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!-- Copyright (C) 2016 The Android Open Source Project
-
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
-
           http://www.apache.org/licenses/LICENSE-2.0
-
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -14,11 +11,11 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-
-    <include layout="@layout/switch_bar" />
-
-</LinearLayout>
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:state_enabled="false">
+        <bitmap android:alpha="0.1" android:src="@drawable/watch_switch_track_mtrl_alpha" />
+    </item>
+    <item>
+        <bitmap android:alpha="0.2" android:src="@drawable/watch_switch_track_mtrl_alpha" />
+    </item>
+</selector>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/interpolator-watch/progress_indeterminate_rotation_interpolator.xml
similarity index 60%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/interpolator-watch/progress_indeterminate_rotation_interpolator.xml
index 9e13001..ed2655c 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/interpolator-watch/progress_indeterminate_rotation_interpolator.xml
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!--
+ Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -13,8 +14,5 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
-</resources>
+<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
+    android:pathData="M 0.0,0.0 c 0.032,0.0 0.016,0.2 0.08,0.2 l 0.12,0.0 c 0.032,0.0 0.016,0.2 0.08,0.2 l 0.12,0.0 c 0.032,0.0 0.016,0.2 0.08,0.2 l 0.12,0.0 c 0.032,0.0 0.016,0.2 0.08,0.2 l 0.12,0.0 c 0.032,0.0 0.016,0.2 0.08,0.2 l 0.12,0.0 L 1.0,1.0" />
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/interpolator-watch/trim_end_interpolator.xml
similarity index 73%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/interpolator-watch/trim_end_interpolator.xml
index 9e13001..f46d5e0 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/interpolator-watch/trim_end_interpolator.xml
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!--
+ Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -13,8 +14,5 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
-</resources>
+<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
+    android:pathData="M 0.0,0.0 c 0.08,0.0 0.04,1.0 0.2,1.0 l 0.8,0.0 L 1.0,1.0" />
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/interpolator-watch/trim_offset_interpolator.xml
similarity index 74%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/interpolator-watch/trim_offset_interpolator.xml
index 9e13001..d58672e 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/interpolator-watch/trim_offset_interpolator.xml
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!--
+ Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -13,8 +14,5 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
-</resources>
+<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
+    android:pathData="M 0.0,0.0 l 0.4,1.0 l 0.6,0.0 L 1.0,1.0" />
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/interpolator-watch/trim_start_interpolator.xml
similarity index 72%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/interpolator-watch/trim_start_interpolator.xml
index 9e13001..365609c 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/interpolator-watch/trim_start_interpolator.xml
@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!--
+ Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -13,8 +14,5 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
-</resources>
+<pathInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
+    android:pathData="M 0.0,0.0 l 0.2,0.0 c 0.08,0.0 0.04,1.0 0.2,1.0 l 0.6,0.0 L 1.0,1.0" />
diff --git a/core/res/res/layout-notround-watch/alert_dialog_title_material.xml b/core/res/res/layout-notround-watch/alert_dialog_title_material.xml
new file mode 100644
index 0000000..307c6db
--- /dev/null
+++ b/core/res/res/layout-notround-watch/alert_dialog_title_material.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2016 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+<LinearLayout
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:orientation="vertical"
+        android:gravity="top|center_horizontal"
+        android:minHeight="@dimen/alert_dialog_title_height">
+    <ImageView android:id="@+id/icon"
+            android:maxHeight="24dp"
+            android:maxWidth="24dp"
+            android:layout_marginTop="8dp"
+            android:layout_marginStart="8dp"
+            android:layout_marginBottom="8dp"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:src="@null" />
+    <TextView android:id="@+id/alertTitle"
+            style="?android:attr/windowTitleStyle"
+            android:layout_marginBottom="8dp"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content" />
+</LinearLayout>
diff --git a/core/res/res/layout-round-watch/alert_dialog_title_material.xml b/core/res/res/layout-round-watch/alert_dialog_title_material.xml
new file mode 100644
index 0000000..0279911
--- /dev/null
+++ b/core/res/res/layout-round-watch/alert_dialog_title_material.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2016 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+<FrameLayout
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="top|center_horizontal"
+        android:minHeight="@dimen/alert_dialog_title_height">
+    <ImageView android:id="@+id/icon"
+            android:maxHeight="24dp"
+            android:maxWidth="24dp"
+            android:layout_marginTop="12dp"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:src="@null" />
+    <TextView android:id="@+id/alertTitle"
+            style="?android:attr/windowTitleStyle"
+            android:layout_marginTop="36dp"
+            android:layout_marginBottom="8dp"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content" />
+</FrameLayout>
diff --git a/core/res/res/layout-sw600dp/date_picker_dialog.xml b/core/res/res/layout-sw600dp/date_picker_dialog.xml
index 5e3ca14..cd6af46 100644
--- a/core/res/res/layout-sw600dp/date_picker_dialog.xml
+++ b/core/res/res/layout-sw600dp/date_picker_dialog.xml
@@ -22,4 +22,4 @@
     android:layout_height="wrap_content"
     android:spinnersShown="true"
     android:calendarViewShown="true"
-    android:datePickerMode="@integer/date_picker_mode" />
+    android:dialogMode="true" />
diff --git a/core/res/res/layout-watch/alert_dialog_material.xml b/core/res/res/layout-watch/alert_dialog_material.xml
new file mode 100644
index 0000000..2fe13de
--- /dev/null
+++ b/core/res/res/layout-watch/alert_dialog_material.xml
@@ -0,0 +1,109 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2016 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+<com.android.internal.widget.WatchListDecorLayout
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/parentPanel"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent">
+    <ScrollView
+            android:id="@+id/scrollView"
+            android:fillViewport="true"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent">
+        <LinearLayout
+                android:orientation="vertical"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content">
+            <!-- Top Panel -->
+            <FrameLayout
+                    android:paddingLeft="?dialogPreferredPadding"
+                    android:paddingRight="?dialogPreferredPadding"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:id="@+id/topPanel"
+                    android:minHeight="@dimen/dialog_list_padding_top_no_title">
+                <include android:id="@+id/title_template"
+                        android:layout_width="match_parent"
+                        android:layout_height="wrap_content"
+                        layout="@layout/alert_dialog_title_material"/>
+            </FrameLayout>
+
+            <!-- Content Panel -->
+            <FrameLayout android:id="@+id/contentPanel"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content"
+                    android:clipToPadding="false">
+                <TextView android:id="@+id/message"
+                        android:layout_width="match_parent"
+                        android:layout_height="wrap_content"
+                        android:gravity="@integer/config_dialogTextGravity"
+                        android:textAppearance="@style/TextAppearance.Material.Subhead"
+                        android:paddingStart="?dialogPreferredPadding"
+                        android:paddingEnd="?dialogPreferredPadding"
+                        android:paddingTop="8dip"
+                        android:paddingBottom="8dip"/>
+            </FrameLayout>
+
+            <!-- Custom Panel, to replace content panel if needed -->
+            <FrameLayout android:id="@+id/customPanel"
+                    android:layout_width="match_parent"
+                    android:layout_height="match_parent"
+                    android:minHeight="64dp">
+                <FrameLayout android:id="@+android:id/custom"
+                        android:layout_width="match_parent"
+                        android:layout_height="wrap_content" />
+            </FrameLayout>
+
+            <!-- Button Panel -->
+            <FrameLayout
+                    android:id="@+id/buttonPanel"
+                    android:minHeight="@dimen/dialog_list_padding_bottom_no_buttons"
+                    android:layout_weight="1"
+                    android:layout_width="match_parent"
+                    android:layout_height="wrap_content">
+                <LinearLayout
+                        android:layout_width="match_parent"
+                        android:layout_height="wrap_content"
+                        android:layout_gravity="bottom"
+                        android:orientation="vertical"
+                        android:minHeight="@dimen/alert_dialog_button_bar_height"
+                        android:paddingBottom="?dialogPreferredPadding"
+                        style="?android:attr/buttonBarStyle"
+                        android:measureWithLargestChild="true">
+                    <Button android:id="@+id/button1"
+                            android:layout_gravity="start"
+                            android:layout_weight="1"
+                            style="?android:attr/buttonBarButtonStyle"
+                            android:layout_width="match_parent"
+                            android:layout_height="wrap_content" />
+                    <Button android:id="@+id/button3"
+                            android:layout_gravity="start"
+                            android:layout_weight="1"
+                            style="?android:attr/buttonBarButtonStyle"
+                            android:layout_width="match_parent"
+                            android:layout_height="wrap_content" />
+                    <Button android:id="@+id/button2"
+                            android:layout_gravity="start"
+                            android:layout_weight="1"
+                            style="?android:attr/buttonBarButtonStyle"
+                            android:layout_width="match_parent"
+                            android:layout_height="wrap_content" />
+                </LinearLayout>
+            </FrameLayout>
+        </LinearLayout>
+    </ScrollView>
+</com.android.internal.widget.WatchListDecorLayout>
diff --git a/core/res/res/layout-watch/date_picker_dialog.xml b/core/res/res/layout-watch/date_picker_dialog.xml
new file mode 100644
index 0000000..b8772bc
--- /dev/null
+++ b/core/res/res/layout-watch/date_picker_dialog.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2007 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.
+-->
+
+<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/datePicker"
+    android:layout_gravity="center_horizontal"
+    android:gravity="center_horizontal"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:spinnersShown="true"
+    android:calendarViewShown="false"
+    android:datePickerMode="@integer/date_picker_mode" />
diff --git a/core/res/res/layout-watch/input_method_extract_view.xml b/core/res/res/layout-watch/input_method_extract_view.xml
index e3cd2ce..3478bb5 100644
--- a/core/res/res/layout-watch/input_method_extract_view.xml
+++ b/core/res/res/layout-watch/input_method_extract_view.xml
@@ -31,10 +31,10 @@
         android:inputType="text"
         android:layout_weight="1"
         android:fontFamily="sans-serif-condensed-light"
-        android:textColor="@color/primary_text_default_material_dark"
+        android:textColor="@color/primary_text_material_dark"
+        android:textColorHint="@color/secondary_text_material_dark"
         android:textColorHighlight="@color/accent_material_dark"
         android:textSize="18dp"
-        android:cursorVisible="false"
         android:gravity="bottom|right"
         />
 
@@ -49,7 +49,7 @@
             android:layout_width="@dimen/input_extract_action_button_width"
             android:layout_height="@dimen/input_extract_action_button_width"
             android:background="@drawable/input_extract_action_bg_material_dark"
-            android:padding="4dp"
+            android:padding="@dimen/input_extract_action_icon_padding"
             android:scaleType="centerInside" />
     </FrameLayout>
 </android.inputmethodservice.CompactExtractEditLayout>
diff --git a/core/res/res/layout-watch/preference_list_fragment_material.xml b/core/res/res/layout-watch/preference_list_fragment_material.xml
new file mode 100644
index 0000000..ae8f203
--- /dev/null
+++ b/core/res/res/layout-watch/preference_list_fragment_material.xml
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:orientation="vertical"
+    android:layout_height="match_parent"
+    android:layout_width="match_parent"
+    android:background="@android:color/transparent"
+    android:layout_removeBorders="true">
+
+    <FrameLayout
+        android:id="@android:id/list_container"
+        android:layout_width="match_parent"
+        android:layout_height="0px"
+        android:layout_weight="1">
+        <com.android.internal.widget.WatchHeaderListView
+            android:id="@android:id/list"
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            style="?attr/preferenceFragmentListStyle"
+            android:scrollbarStyle="@integer/preference_fragment_scrollbarStyle"
+            android:clipToPadding="false"
+            android:drawSelectorOnTop="false"
+            android:cacheColorHint="@android:color/transparent"
+            android:scrollbarAlwaysDrawVerticalTrack="true">
+             <TextView
+                android:id="@android:id/title"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:paddingStart="@dimen/dialog_padding_material"
+                android:paddingEnd="@dimen/dialog_padding_material"
+                android:paddingBottom="8dp"
+                android:textAppearance="@style/TextAppearance.Material.Title"
+                android:gravity="center" />
+        </com.android.internal.widget.WatchHeaderListView>
+    </FrameLayout>
+
+    <TextView android:id="@android:id/empty"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:padding="@dimen/preference_fragment_padding_side"
+        android:gravity="center"
+        android:visibility="gone" />
+
+    <RelativeLayout android:id="@+id/button_bar"
+        android:layout_height="wrap_content"
+        android:layout_width="match_parent"
+        android:layout_weight="0"
+        android:visibility="gone">
+
+        <Button android:id="@+id/back_button"
+            android:layout_width="150dip"
+            android:layout_height="wrap_content"
+            android:layout_margin="5dip"
+            android:layout_alignParentStart="true"
+            android:text="@string/back_button_label"
+        />
+        <LinearLayout
+            android:orientation="horizontal"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_alignParentEnd="true">
+
+            <Button android:id="@+id/skip_button"
+                android:layout_width="150dip"
+                android:layout_height="wrap_content"
+                android:layout_margin="5dip"
+                android:text="@string/skip_button_label"
+                android:visibility="gone"
+            />
+
+            <Button android:id="@+id/next_button"
+                android:layout_width="150dip"
+                android:layout_height="wrap_content"
+                android:layout_margin="5dip"
+                android:text="@string/next_button_label"
+            />
+        </LinearLayout>
+    </RelativeLayout>
+</LinearLayout>
diff --git a/core/res/res/layout-watch/preference_material.xml b/core/res/res/layout-watch/preference_material.xml
new file mode 100644
index 0000000..ad217db
--- /dev/null
+++ b/core/res/res/layout-watch/preference_material.xml
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- Layout for a Preference in a PreferenceActivity. The
+     Preference is able to place a specific widget for its particular
+     type in the "widget_frame" layout. -->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:minHeight="?attr/listPreferredItemHeightSmall"
+    android:gravity="center_vertical"
+    android:paddingStart="?attr/listPreferredItemPaddingStart"
+    android:paddingEnd="?attr/listPreferredItemPaddingEnd"
+    android:background="?attr/activatedBackgroundIndicator"
+    android:clipToPadding="false">
+
+    <LinearLayout
+        android:id="@+id/icon_frame"
+        android:layout_width="40dp"
+        android:layout_height="40dp"
+        android:gravity="center"
+        android:orientation="horizontal"
+        android:layout_marginEnd="8dp"
+        android:layout_marginTop="8dp"
+        android:layout_marginBottom="8dp">
+        <com.android.internal.widget.PreferenceImageView
+            android:id="@+id/icon"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:maxWidth="40dp"
+            android:maxHeight="40dp" />
+    </LinearLayout>
+
+    <!-- Preference should place its actual preference widget here. -->
+    <LinearLayout android:id="@+id/widget_frame"
+        android:layout_width="40dp"
+        android:layout_height="40dp"
+        android:gravity="center"
+        android:orientation="horizontal"
+        android:layout_marginEnd="8dp"
+        android:layout_marginTop="8dp"
+        android:layout_marginBottom="8dp" />
+
+    <RelativeLayout
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:layout_marginTop="8dp"
+        android:layout_marginBottom="8dp">
+
+        <TextView android:id="@+id/title"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:maxLines="3"
+            android:textAppearance="?attr/textAppearanceListItem"
+            android:ellipsize="end" />
+
+        <TextView android:id="@+id/summary"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_below="@id/title"
+            android:layout_alignStart="@id/title"
+            android:textAppearance="?attr/textAppearanceListItemSecondary"
+            android:textColor="?attr/textColorSecondary"
+            android:maxLines="10" />
+
+    </RelativeLayout>
+</LinearLayout>
diff --git a/core/res/res/layout-watch/preference_widget_switch.xml b/core/res/res/layout-watch/preference_widget_switch.xml
new file mode 100644
index 0000000..5881cf0
--- /dev/null
+++ b/core/res/res/layout-watch/preference_widget_switch.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- Layout for a SwitchPreference -->
+<Switch xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+android:id/switch_widget"
+    android:layout_width="40dp"
+    android:layout_height="40dp"
+    android:switchMinWidth="40dp"
+    android:layout_gravity="center"
+    android:thumb="@drawable/watch_switch_thumb_material_anim"
+    android:thumbTint="@color/watch_switch_thumb_color_material"
+    android:thumbTintMode="multiply"
+    android:track="@drawable/watch_switch_track_material"
+    android:focusable="false"
+    android:clickable="false"
+    android:background="@null" />
diff --git a/core/res/res/layout-watch/time_picker_dialog.xml b/core/res/res/layout-watch/time_picker_dialog.xml
new file mode 100644
index 0000000..788602b
--- /dev/null
+++ b/core/res/res/layout-watch/time_picker_dialog.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+**
+** Copyright 2007, 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.
+*/
+-->
+
+<TimePicker xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/timePicker"
+    android:layout_gravity="center_horizontal"
+    android:gravity="center_horizontal"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:timePickerMode="@integer/time_picker_mode" />
diff --git a/core/res/res/layout/alert_dialog_micro.xml b/core/res/res/layout/alert_dialog_micro.xml
deleted file mode 100644
index abdbd16..0000000
--- a/core/res/res/layout/alert_dialog_micro.xml
+++ /dev/null
@@ -1,140 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-  ~ Copyright (C) 2014 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
-  -->
-
-<LinearLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/parentPanel"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:background="@android:color/white"
-    android:layout_gravity="center"
-    android:orientation="vertical">
-
-    <LinearLayout android:id="@+id/topPanel"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:orientation="vertical">
-        <View android:id="@+id/titleDividerTop"
-            android:layout_width="match_parent"
-            android:layout_height="2dip"
-            android:visibility="gone"
-            android:background="@android:color/holo_blue_light" />
-        <LinearLayout android:id="@+id/title_template"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:orientation="horizontal"
-            android:gravity="center_vertical|start"
-            android:minHeight="@dimen/alert_dialog_title_height"
-            android:layout_marginStart="16dip"
-            android:layout_marginEnd="16dip">
-            <ImageView android:id="@+id/icon"
-                android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:paddingEnd="8dip"
-                android:src="@null" />
-            <com.android.internal.widget.DialogTitle android:id="@+id/alertTitle"
-                style="?android:attr/windowTitleStyle"
-                android:singleLine="true"
-                android:ellipsize="end"
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content"
-                android:textAlignment="viewStart" />
-        </LinearLayout>
-        <View android:id="@+id/titleDivider"
-            android:layout_width="match_parent"
-            android:layout_height="2dip"
-            android:visibility="gone"
-            android:background="@android:color/holo_blue_light" />
-        <!-- If the client uses a customTitle, it will be added here. -->
-    </LinearLayout>
-
-    <LinearLayout android:id="@+id/contentPanel"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_weight="1"
-        android:orientation="vertical"
-        android:minHeight="64dp">
-        <ScrollView android:id="@+id/scrollView"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:clipToPadding="false">
-            <TextView android:id="@+id/message"
-                style="?android:attr/textAppearanceMedium"
-                android:layout_width="match_parent"
-                android:layout_height="wrap_content"
-                android:paddingStart="16dip"
-                android:paddingEnd="16dip"
-                android:paddingTop="8dip"
-                android:paddingBottom="8dip"/>
-        </ScrollView>
-    </LinearLayout>
-
-    <FrameLayout android:id="@+id/customPanel"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_weight="1"
-        android:minHeight="64dp">
-        <FrameLayout android:id="@+android:id/custom"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content" />
-    </FrameLayout>
-
-    <LinearLayout android:id="@+id/buttonPanel"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:minHeight="@dimen/alert_dialog_button_bar_height"
-        android:orientation="vertical"
-        android:divider="?android:attr/dividerHorizontal"
-        android:showDividers="beginning"
-        android:dividerPadding="0dip">
-        <LinearLayout
-            style="?android:attr/buttonBarStyle"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:orientation="horizontal"
-            android:layoutDirection="locale"
-            android:measureWithLargestChild="true">
-            <Button android:id="@+id/button2"
-                android:layout_width="wrap_content"
-                android:layout_gravity="start"
-                android:layout_weight="1"
-                android:maxLines="2"
-                style="?android:attr/buttonBarButtonStyle"
-                android:textSize="14sp"
-                android:minHeight="@dimen/alert_dialog_button_bar_height"
-                android:layout_height="wrap_content" />
-            <Button android:id="@+id/button3"
-                android:layout_width="wrap_content"
-                android:layout_gravity="center_horizontal"
-                android:layout_weight="1"
-                android:maxLines="2"
-                style="?android:attr/buttonBarButtonStyle"
-                android:textSize="14sp"
-                android:minHeight="@dimen/alert_dialog_button_bar_height"
-                android:layout_height="wrap_content" />
-            <Button android:id="@+id/button1"
-                android:layout_width="wrap_content"
-                android:layout_gravity="end"
-                android:layout_weight="1"
-                android:maxLines="2"
-                android:minHeight="@dimen/alert_dialog_button_bar_height"
-                style="?android:attr/buttonBarButtonStyle"
-                android:textSize="14sp"
-                android:layout_height="wrap_content" />
-        </LinearLayout>
-     </LinearLayout>
-</LinearLayout>
diff --git a/core/res/res/layout/chooser_grid.xml b/core/res/res/layout/chooser_grid.xml
index d12c8ba..d8dd447 100644
--- a/core/res/res/layout/chooser_grid.xml
+++ b/core/res/res/layout/chooser_grid.xml
@@ -41,7 +41,7 @@
                   android:visibility="gone"
                   style="?attr/borderlessButtonStyle"
                   android:textAppearance="?attr/textAppearanceButton"
-                  android:textColor="@color/material_deep_teal_500"
+                  android:textColor="?attr/colorAccent"
                   android:gravity="center_vertical"
                   android:layout_alignParentTop="true"
                   android:layout_alignParentRight="true"
diff --git a/core/res/res/layout/date_picker_dialog.xml b/core/res/res/layout/date_picker_dialog.xml
index 8f36e95..32a7360 100644
--- a/core/res/res/layout/date_picker_dialog.xml
+++ b/core/res/res/layout/date_picker_dialog.xml
@@ -22,4 +22,4 @@
     android:layout_height="wrap_content"
     android:spinnersShown="true"
     android:calendarViewShown="false"
-    android:datePickerMode="@integer/date_picker_mode" />
+    android:dialogMode="true" />
diff --git a/core/res/res/layout/date_picker_header_material.xml b/core/res/res/layout/date_picker_header_material.xml
index a0e2b1d..755317e 100644
--- a/core/res/res/layout/date_picker_header_material.xml
+++ b/core/res/res/layout/date_picker_header_material.xml
@@ -54,7 +54,7 @@
             android:textAppearance="@style/TextAppearance.Material.DatePicker.DateLabel"
             android:includeFontPadding="false"
             android:gravity="start"
-            android:maxLines="2"
+            android:maxLines="@integer/date_picker_header_max_lines_material"
             android:ellipsize="none" />
     </LinearLayout>
 </LinearLayout>
diff --git a/core/res/res/layout/date_picker_legacy_holo.xml b/core/res/res/layout/date_picker_legacy_holo.xml
index b465d97..a6e93c9 100644
--- a/core/res/res/layout/date_picker_legacy_holo.xml
+++ b/core/res/res/layout/date_picker_legacy_holo.xml
@@ -41,8 +41,8 @@
             android:id="@+id/month"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_marginTop="16dip"
-            android:layout_marginBottom="16dip"
+            android:layout_marginTop="@dimen/picker_top_margin"
+            android:layout_marginBottom="@dimen/picker_bottom_margin"
             android:layout_marginStart="8dip"
             android:layout_marginEnd="8dip"
             android:focusable="true"
@@ -54,8 +54,8 @@
             android:id="@+id/day"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_marginTop="16dip"
-            android:layout_marginBottom="16dip"
+            android:layout_marginTop="@dimen/picker_top_margin"
+            android:layout_marginBottom="@dimen/picker_bottom_margin"
             android:layout_marginStart="8dip"
             android:layout_marginEnd="8dip"
             android:focusable="true"
@@ -67,8 +67,8 @@
             android:id="@+id/year"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_marginTop="16dip"
-            android:layout_marginBottom="16dip"
+            android:layout_marginTop="@dimen/picker_top_margin"
+            android:layout_marginBottom="@dimen/picker_bottom_margin"
             android:layout_marginStart="8dip"
             android:layout_marginEnd="16dip"
             android:focusable="true"
diff --git a/core/res/res/layout/immersive_mode_cling.xml b/core/res/res/layout/immersive_mode_cling.xml
index 28fbea5..b08b0f4 100644
--- a/core/res/res/layout/immersive_mode_cling.xml
+++ b/core/res/res/layout/immersive_mode_cling.xml
@@ -16,7 +16,7 @@
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:background="#ff009688"
+        android:background="?android:attr/colorAccent"
         android:gravity="center_vertical"
         android:paddingBottom="24dp">
 
@@ -47,7 +47,7 @@
                 android:paddingTop="8dp"
                 android:scaleType="center"
                 android:src="@drawable/ic_expand_more_48dp"
-                android:tint="#ff009688"/>
+                android:tint="?android:attr/colorAccent"/>
     </FrameLayout>
 
     <TextView
diff --git a/core/res/res/layout/notification_material_action_emphasized.xml b/core/res/res/layout/notification_material_action_emphasized.xml
new file mode 100644
index 0000000..1e364cc
--- /dev/null
+++ b/core/res/res/layout/notification_material_action_emphasized.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2016 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/button_holder"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:layout_weight="1"
+    android:background="#ff000000">
+    <Button
+        style="@android:style/Widget.Material.Light.Button.Borderless.Small"
+        android:id="@+id/action0"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:gravity="center"
+        android:textColor="@color/notification_default_color"
+        android:singleLine="true"
+        android:ellipsize="end"
+        android:background="@drawable/notification_material_action_background"
+        />
+</FrameLayout>
diff --git a/core/res/res/layout/notification_template_header.xml b/core/res/res/layout/notification_template_header.xml
index 38f671c2..1f71a18 100644
--- a/core/res/res/layout/notification_template_header.xml
+++ b/core/res/res/layout/notification_template_header.xml
@@ -26,7 +26,7 @@
     android:paddingBottom="16dp"
     android:paddingStart="@dimen/notification_content_margin_start"
     android:paddingEnd="16dp">
-    <ImageView
+    <com.android.internal.widget.CachingIconView
         android:id="@+id/icon"
         android:layout_width="18dp"
         android:layout_height="18dp"
diff --git a/core/res/res/layout/number_picker_material.xml b/core/res/res/layout/number_picker_material.xml
index b045585..6fbd2b2 100644
--- a/core/res/res/layout/number_picker_material.xml
+++ b/core/res/res/layout/number_picker_material.xml
@@ -22,4 +22,4 @@
       android:gravity="center"
       android:singleLine="true"
       android:background="@null"
-      android:textAppearance="@style/TextAppearance.Material.Body1" />
+      android:textAppearance="@style/TextAppearance.Material.NumberPicker" />
diff --git a/core/res/res/layout/number_picker_with_selector_wheel_micro.xml b/core/res/res/layout/number_picker_with_selector_wheel_micro.xml
deleted file mode 100644
index a1c0921..0000000
--- a/core/res/res/layout/number_picker_with_selector_wheel_micro.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-**
-** Copyright 2012, 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.
-*/
--->
-
-<merge xmlns:android="http://schemas.android.com/apk/res/android">
-
-    <view class="android.widget.NumberPicker$CustomEditText"
-        android:textAppearance="?android:attr/textAppearanceLarge"
-        android:id="@+id/numberpicker_input"
-        android:layout_width="fill_parent"
-        android:layout_height="wrap_content"
-        android:gravity="center"
-        android:singleLine="true"
-        android:background="@null" />
-
-</merge>
diff --git a/core/res/res/layout/preference_list_fragment.xml b/core/res/res/layout/preference_list_fragment.xml
index fc53a1a..c43975e 100644
--- a/core/res/res/layout/preference_list_fragment.xml
+++ b/core/res/res/layout/preference_list_fragment.xml
@@ -33,8 +33,6 @@
             style="?attr/preferenceFragmentListStyle"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
-            android:paddingTop="0dip"
-            android:paddingBottom="@dimen/preference_fragment_padding_bottom"
             android:scrollbarStyle="@integer/preference_fragment_scrollbarStyle"
             android:clipToPadding="false"
             android:drawSelectorOnTop="false"
diff --git a/core/res/res/layout/preference_list_fragment_material.xml b/core/res/res/layout/preference_list_fragment_material.xml
index e411c0e..db2fe7d 100644
--- a/core/res/res/layout/preference_list_fragment_material.xml
+++ b/core/res/res/layout/preference_list_fragment_material.xml
@@ -32,8 +32,6 @@
         <ListView android:id="@android:id/list"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
-            android:paddingTop="0dip"
-            android:paddingBottom="@dimen/preference_fragment_padding_bottom"
             style="?attr/preferenceFragmentListStyle"
             android:scrollbarStyle="@integer/preference_fragment_scrollbarStyle"
             android:clipToPadding="false"
diff --git a/core/res/res/layout/resolver_list.xml b/core/res/res/layout/resolver_list.xml
index ae94503..c4e8e9c 100644
--- a/core/res/res/layout/resolver_list.xml
+++ b/core/res/res/layout/resolver_list.xml
@@ -42,7 +42,7 @@
             android:visibility="gone"
             style="?attr/borderlessButtonStyle"
             android:textAppearance="?attr/textAppearanceButton"
-            android:textColor="@color/material_deep_teal_500"
+            android:textColor="?attr/colorAccent"
             android:gravity="center_vertical"
             android:layout_alignParentTop="true"
             android:layout_alignParentRight="true"
diff --git a/core/res/res/layout/time_picker_dialog.xml b/core/res/res/layout/time_picker_dialog.xml
index d1f3902..ada18d1 100644
--- a/core/res/res/layout/time_picker_dialog.xml
+++ b/core/res/res/layout/time_picker_dialog.xml
@@ -22,4 +22,4 @@
     android:layout_gravity="center_horizontal"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:timePickerMode="@integer/time_picker_mode" />
+    android:dialogMode="true" />
diff --git a/core/res/res/layout/time_picker_legacy_material.xml b/core/res/res/layout/time_picker_legacy_material.xml
index c6b7d3a..ee56266 100644
--- a/core/res/res/layout/time_picker_legacy_material.xml
+++ b/core/res/res/layout/time_picker_legacy_material.xml
@@ -40,8 +40,8 @@
             android:id="@+id/hour"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_marginTop="16dip"
-            android:layout_marginBottom="16dip"
+            android:layout_marginTop="@dimen/picker_top_margin"
+            android:layout_marginBottom="@dimen/picker_bottom_margin"
             android:focusable="true"
             android:focusableInTouchMode="true"
             />
@@ -62,8 +62,8 @@
             android:id="@+id/minute"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:layout_marginTop="16dip"
-            android:layout_marginBottom="16dip"
+            android:layout_marginTop="@dimen/picker_top_margin"
+            android:layout_marginBottom="@dimen/picker_bottom_margin"
             android:focusable="true"
             android:focusableInTouchMode="true"
             />
@@ -75,8 +75,8 @@
         android:id="@+id/amPm"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:layout_marginTop="16dip"
-        android:layout_marginBottom="16dip"
+        android:layout_marginTop="@dimen/picker_top_margin"
+        android:layout_marginBottom="@dimen/picker_bottom_margin"
         android:layout_marginStart="8dip"
         android:layout_marginEnd="8dip"
         android:focusable="true"
diff --git a/core/res/res/raw-ar-xlarge/incognito_mode_start_page.html b/core/res/res/raw-ar-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 002be41..0000000
--- a/core/res/res/raw-ar-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="RTL">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>نافذة جديدة للتصفح المتخفي</title>
-  </head>
-  <body>
-    <p><strong>أنت الآن في وضع التصفح المتخفي</strong> الصفحات التي تشاهدها في هذه النافذة لن تظهر في سجل المتصفح أو سجلّ البحث، ولن تترك آثارًا أخرى للتتبع، مثل ملفات تعريف الارتباط على جهازك بعد أن تغلق نافذة التصفح المتخفي. ورغم ذلك، سيتم الاحتفاظ بأي ملفات تنزلها أو أية إشارات مرجعية تقوم بإنشائها.</p>
-
-    <p><strong>العمل في وضع التصفح المخفي لا يؤثر على طريقة عمل الأشخاص الآخرين أو الخوادم أو البرامج الأخرى. كن على حذر مما يلي:</strong></p>
-
-    <ul>
-      <li>مواقع الويب التي تجمع معلومات عنك أو تشارك الآخرين فيها</li>
-      <li>مزوّدو خدمة الإنترنت أو الموظفون الذين يتتبعون الصفحات التي تزورها</li>
-      <li>البرامج الضارة التي تتبع ضغطات المفاتيح التي تقوم بها مقابل تنزيل وجوه رمزية مجانًا</li>
-      <li>المراقبة من قبل العملاء السريين</li>
-      <li>الأشخاص الذين يقفون خلفك</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ar/incognito_mode_start_page.html b/core/res/res/raw-ar/incognito_mode_start_page.html
deleted file mode 100644
index 002be41..0000000
--- a/core/res/res/raw-ar/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="RTL">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>نافذة جديدة للتصفح المتخفي</title>
-  </head>
-  <body>
-    <p><strong>أنت الآن في وضع التصفح المتخفي</strong> الصفحات التي تشاهدها في هذه النافذة لن تظهر في سجل المتصفح أو سجلّ البحث، ولن تترك آثارًا أخرى للتتبع، مثل ملفات تعريف الارتباط على جهازك بعد أن تغلق نافذة التصفح المتخفي. ورغم ذلك، سيتم الاحتفاظ بأي ملفات تنزلها أو أية إشارات مرجعية تقوم بإنشائها.</p>
-
-    <p><strong>العمل في وضع التصفح المخفي لا يؤثر على طريقة عمل الأشخاص الآخرين أو الخوادم أو البرامج الأخرى. كن على حذر مما يلي:</strong></p>
-
-    <ul>
-      <li>مواقع الويب التي تجمع معلومات عنك أو تشارك الآخرين فيها</li>
-      <li>مزوّدو خدمة الإنترنت أو الموظفون الذين يتتبعون الصفحات التي تزورها</li>
-      <li>البرامج الضارة التي تتبع ضغطات المفاتيح التي تقوم بها مقابل تنزيل وجوه رمزية مجانًا</li>
-      <li>المراقبة من قبل العملاء السريين</li>
-      <li>الأشخاص الذين يقفون خلفك</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-bg-xlarge/incognito_mode_start_page.html b/core/res/res/raw-bg-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index ee25ae4..0000000
--- a/core/res/res/raw-bg-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Нов прозорец „инкогнито“</title>
-  </head>
-  <body>
-    <p><strong>Влязохте в режим „инкогнито“</strong>. Страниците, които разглеждате в този прозорец, няма да се показват в историята на браузъра ви, нито в историята на търсенията ви. Те също няма да оставят други следи като „бисквитки“ в устройството ви, след като затворите прозореца в режим „инкогнито“. Ще се съхранят обаче всички файлове, които изтеглите, или отметки, които създадете.</p>
-
-    <p><strong>Преминаването в режим „инкогнито“ не засяга поведението на други хора, сървъри или софтуер. </strong>Внимавайте за:</p>
-
-    <ul>
-      <li>уебсайтове, които събират или споделят информация за вас;</li>
-      <li>доставчици на интернет услуги или служители, които проследяват посещаваните от вас страници;</li>
-      <li>злонамерен софтуер, който ви дава безплатни емотикони, но в замяна проследява натисканията на клавишите от вас;</li>
-      <li>наблюдение от тайните служби;</li>
-      <li>хора, които стоят зад вас.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-bg/incognito_mode_start_page.html b/core/res/res/raw-bg/incognito_mode_start_page.html
deleted file mode 100644
index ee25ae4..0000000
--- a/core/res/res/raw-bg/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Нов прозорец „инкогнито“</title>
-  </head>
-  <body>
-    <p><strong>Влязохте в режим „инкогнито“</strong>. Страниците, които разглеждате в този прозорец, няма да се показват в историята на браузъра ви, нито в историята на търсенията ви. Те също няма да оставят други следи като „бисквитки“ в устройството ви, след като затворите прозореца в режим „инкогнито“. Ще се съхранят обаче всички файлове, които изтеглите, или отметки, които създадете.</p>
-
-    <p><strong>Преминаването в режим „инкогнито“ не засяга поведението на други хора, сървъри или софтуер. </strong>Внимавайте за:</p>
-
-    <ul>
-      <li>уебсайтове, които събират или споделят информация за вас;</li>
-      <li>доставчици на интернет услуги или служители, които проследяват посещаваните от вас страници;</li>
-      <li>злонамерен софтуер, който ви дава безплатни емотикони, но в замяна проследява натисканията на клавишите от вас;</li>
-      <li>наблюдение от тайните служби;</li>
-      <li>хора, които стоят зад вас.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ca-xlarge/incognito_mode_start_page.html b/core/res/res/raw-ca-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index bec3dac..0000000
--- a/core/res/res/raw-ca-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nova finestra d'incògnit</title>
-  </head>
-  <body>
-    <p><strong>Has passat a l'estat d'incògnit</strong>. Les pàgines que visualitzis en aquesta finestra no apareixeran a l'historial del navegador ni a l'historial de cerques, i no deixaran cap pista, com ara galetes, al dispositiu després de tancar la finestra d'incògnit. Tanmateix, es conservaran tots els fitxers que baixis o les adreces d'interès que creïs.</p>
-
-    <p><strong>Utilitzar el mode d'incògnit no afecta el comportament d'altres usuaris, servidors ni programari. Vés amb compte amb:</strong></p>
-
-    <ul>
-      <li>llocs web que recopilen o comparteixen informació sobre la teva identitat,</li>
-      <li>proveïdors de serveis d'Internet o treballadors que segueixen les pàgines que visites,</li>
-      <li>programari maliciós que segueix les teves pulsacions del teclat a canvi d'emoticones,</li>
-      <li>vigilància per part d'agents secrets,</li>
-      <li>persones que estan darrere teu.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ca/incognito_mode_start_page.html b/core/res/res/raw-ca/incognito_mode_start_page.html
deleted file mode 100644
index bec3dac..0000000
--- a/core/res/res/raw-ca/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nova finestra d'incògnit</title>
-  </head>
-  <body>
-    <p><strong>Has passat a l'estat d'incògnit</strong>. Les pàgines que visualitzis en aquesta finestra no apareixeran a l'historial del navegador ni a l'historial de cerques, i no deixaran cap pista, com ara galetes, al dispositiu després de tancar la finestra d'incògnit. Tanmateix, es conservaran tots els fitxers que baixis o les adreces d'interès que creïs.</p>
-
-    <p><strong>Utilitzar el mode d'incògnit no afecta el comportament d'altres usuaris, servidors ni programari. Vés amb compte amb:</strong></p>
-
-    <ul>
-      <li>llocs web que recopilen o comparteixen informació sobre la teva identitat,</li>
-      <li>proveïdors de serveis d'Internet o treballadors que segueixen les pàgines que visites,</li>
-      <li>programari maliciós que segueix les teves pulsacions del teclat a canvi d'emoticones,</li>
-      <li>vigilància per part d'agents secrets,</li>
-      <li>persones que estan darrere teu.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-cs-xlarge/incognito_mode_start_page.html b/core/res/res/raw-cs-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 7420393..0000000
--- a/core/res/res/raw-cs-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nové anonymní okno</title>
-  </head>
-  <body>
-    <p><strong>Spustili jste anonymní režim</strong>. Stránky, které zobrazíte v tomto okně, se nezahrnou do historie prohlížeče ani historie vyhledávání a dokonce po zavření tohoto anonymního okna ve vašem zařízení nezanechají ani žádné jiné stopy například v podobě souborů cookie. Veškeré stažené soubory nebo vytvořené záložky však budou zachovány.</p>
-
-    <p><strong>Použití anonymního režimu nemá vliv na chování jiných osob, serverů nebo softwaru. Dejte si pozor na:</strong></p>
-
-    <ul>
-      <li>Weby, které sbírají nebo sdílejí informace o vás</li>
-      <li>Poskytovatele internetových služeb nebo zaměstnavatele, kteří sledují stránky, které navštěvujete</li>
-      <li>Škodlivý software, který sleduje stisknuté klávesy a výměnou nabízí nové emotikony</li>
-      <li>Tajné agenty</li>
-      <li>Lidi, kteří vám koukají přes rameno</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-cs/incognito_mode_start_page.html b/core/res/res/raw-cs/incognito_mode_start_page.html
deleted file mode 100644
index 7420393..0000000
--- a/core/res/res/raw-cs/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nové anonymní okno</title>
-  </head>
-  <body>
-    <p><strong>Spustili jste anonymní režim</strong>. Stránky, které zobrazíte v tomto okně, se nezahrnou do historie prohlížeče ani historie vyhledávání a dokonce po zavření tohoto anonymního okna ve vašem zařízení nezanechají ani žádné jiné stopy například v podobě souborů cookie. Veškeré stažené soubory nebo vytvořené záložky však budou zachovány.</p>
-
-    <p><strong>Použití anonymního režimu nemá vliv na chování jiných osob, serverů nebo softwaru. Dejte si pozor na:</strong></p>
-
-    <ul>
-      <li>Weby, které sbírají nebo sdílejí informace o vás</li>
-      <li>Poskytovatele internetových služeb nebo zaměstnavatele, kteří sledují stránky, které navštěvujete</li>
-      <li>Škodlivý software, který sleduje stisknuté klávesy a výměnou nabízí nové emotikony</li>
-      <li>Tajné agenty</li>
-      <li>Lidi, kteří vám koukají přes rameno</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-da-xlarge/incognito_mode_start_page.html b/core/res/res/raw-da-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index eae989f..0000000
--- a/core/res/res/raw-da-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nyt inkognitovindue</title>
-  </head>
-  <body>
-    <p><strong>Nu er du inkognito</strong>. De sider, du besøger i dette vindue, vises ikke i din browser- eller søgeoversigt, og de efterlader ikke andre spor, såsom cookies, på din enhed, når du lukker incognitovinduet. Filer, som du downloader eller bogmærker, som du opretter, gemmes dog.</p>
-
-    <p><strong>At være inkognito er ikke noget, der påvirker andre folk, servere eller software. Vær opmærksom på:</strong></p>
-
-    <ul>
-      <li>Websider, der indsamler eller deler oplysninger om dig</li>
-      <li>Internetserviceudbydere eller arbejdsgivere, der registrerer de sider, du besøger</li>
-      <li>Ondsindet software, der registrerer dine tasteslag til gengæld for gratis smileys</li>
-      <li>Overvågning af hemmelige agenter</li>
-      <li>Folk, der kigger dig over skulderen</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-da/incognito_mode_start_page.html b/core/res/res/raw-da/incognito_mode_start_page.html
deleted file mode 100644
index eae989f..0000000
--- a/core/res/res/raw-da/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nyt inkognitovindue</title>
-  </head>
-  <body>
-    <p><strong>Nu er du inkognito</strong>. De sider, du besøger i dette vindue, vises ikke i din browser- eller søgeoversigt, og de efterlader ikke andre spor, såsom cookies, på din enhed, når du lukker incognitovinduet. Filer, som du downloader eller bogmærker, som du opretter, gemmes dog.</p>
-
-    <p><strong>At være inkognito er ikke noget, der påvirker andre folk, servere eller software. Vær opmærksom på:</strong></p>
-
-    <ul>
-      <li>Websider, der indsamler eller deler oplysninger om dig</li>
-      <li>Internetserviceudbydere eller arbejdsgivere, der registrerer de sider, du besøger</li>
-      <li>Ondsindet software, der registrerer dine tasteslag til gengæld for gratis smileys</li>
-      <li>Overvågning af hemmelige agenter</li>
-      <li>Folk, der kigger dig over skulderen</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-de-xlarge/incognito_mode_start_page.html b/core/res/res/raw-de-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 1d2cb69..0000000
--- a/core/res/res/raw-de-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Neues Inkognito-Fenster</title>
-  </head>
-  <body>
-    <p><strong>Sie haben den Modus für anonymes Browsen aktiviert</strong>. In diesem Fenster aufgerufene Seiten erscheinen nicht in Ihrem Browser- oder Suchverlauf. Zudem werden nach dem Schließen des Inkognito-Fensters keine anderen Spuren wie etwa Cookies auf Ihrem Gerät gespeichert. Heruntergeladene Dateien oder hinzugefügte Lesezeichen werden jedoch beibehalten.</p>
-
-    <p><strong>Das anonyme Browsen wirkt sich nicht auf das Verhalten von Menschen, Servern oder Software aus. </strong>Vorsicht ist geboten bei:</p>
-
-    <ul>
-      <li>Websites, auf denen Informationen über Sie gesammelt oder weitergegeben werden</li>
-      <li>Internetanbietern oder Arbeitgebern, die die von Ihnen aufgerufenen Seiten protokollieren</li>
-      <li>Bösartiger Software, die Ihnen kostenlose Smileys bietet, dafür aber Ihre Tastatureingaben speichert</li>
-      <li>Geheimagenten</li>
-      <li>Personen, die hinter Ihnen stehen</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-de/incognito_mode_start_page.html b/core/res/res/raw-de/incognito_mode_start_page.html
deleted file mode 100644
index 1d2cb69..0000000
--- a/core/res/res/raw-de/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Neues Inkognito-Fenster</title>
-  </head>
-  <body>
-    <p><strong>Sie haben den Modus für anonymes Browsen aktiviert</strong>. In diesem Fenster aufgerufene Seiten erscheinen nicht in Ihrem Browser- oder Suchverlauf. Zudem werden nach dem Schließen des Inkognito-Fensters keine anderen Spuren wie etwa Cookies auf Ihrem Gerät gespeichert. Heruntergeladene Dateien oder hinzugefügte Lesezeichen werden jedoch beibehalten.</p>
-
-    <p><strong>Das anonyme Browsen wirkt sich nicht auf das Verhalten von Menschen, Servern oder Software aus. </strong>Vorsicht ist geboten bei:</p>
-
-    <ul>
-      <li>Websites, auf denen Informationen über Sie gesammelt oder weitergegeben werden</li>
-      <li>Internetanbietern oder Arbeitgebern, die die von Ihnen aufgerufenen Seiten protokollieren</li>
-      <li>Bösartiger Software, die Ihnen kostenlose Smileys bietet, dafür aber Ihre Tastatureingaben speichert</li>
-      <li>Geheimagenten</li>
-      <li>Personen, die hinter Ihnen stehen</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-el-xlarge/incognito_mode_start_page.html b/core/res/res/raw-el-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 5641650..0000000
--- a/core/res/res/raw-el-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Νέο παράθυρο για ανώνυμη περιήγηση</title>
-  </head>
-  <body>
-    <p><strong>Είστε σε κατάσταση ανώνυμης περιήγησης</strong>. Οι σελίδες που θα προβάλλετε σε αυτό το παράθυρο δεν θα εμφανιστούν στο ιστορικό του πρόγράμματος περιήγησης ή στο ιστορικό αναζήτησης. Επίσης, δεν θα αφήσουν ίχνη, όπως cookie, στη συσκευή σας αφού κλείσετε το παράθυρο ανώνυμης περιήγησης. Ωστόσο, τα αρχεία και οι σελιδοδείκτες που θα δημιουργήσετε θα διατηρηθούν.</p>
-
-    <p><strong>Η κατάσταση ανώνυμης περιήγησης δεν επηρεάζει την συμπεριφορά άλλων, διακομιστών ή λογισμικού. Αλλά προσοχή σε:</strong></p>
-
-    <ul>
-      <li>Ιστοτόπους που συλλέγουν ή μοιράζονται πληροφορίες για εσάς</li>
-      <li>Πάροχους υπηρεσιών διαδικτύου ή εργοδότες που παρακολουθούν τις ιστοσελίδες που επισκέπτεστε</li>
-      <li>Κακόβουλο λογισμικό που καταγράφει ότι πληκτρολογείτε με αντάλλαγμα δωρεάν "φατσούλες"</li>
-      <li>Παρακολούθηση από μυστικούς πράκτορες</li>
-      <li>Άτομα που στέκονται πίσω σας</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-el/incognito_mode_start_page.html b/core/res/res/raw-el/incognito_mode_start_page.html
deleted file mode 100644
index 5641650..0000000
--- a/core/res/res/raw-el/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Νέο παράθυρο για ανώνυμη περιήγηση</title>
-  </head>
-  <body>
-    <p><strong>Είστε σε κατάσταση ανώνυμης περιήγησης</strong>. Οι σελίδες που θα προβάλλετε σε αυτό το παράθυρο δεν θα εμφανιστούν στο ιστορικό του πρόγράμματος περιήγησης ή στο ιστορικό αναζήτησης. Επίσης, δεν θα αφήσουν ίχνη, όπως cookie, στη συσκευή σας αφού κλείσετε το παράθυρο ανώνυμης περιήγησης. Ωστόσο, τα αρχεία και οι σελιδοδείκτες που θα δημιουργήσετε θα διατηρηθούν.</p>
-
-    <p><strong>Η κατάσταση ανώνυμης περιήγησης δεν επηρεάζει την συμπεριφορά άλλων, διακομιστών ή λογισμικού. Αλλά προσοχή σε:</strong></p>
-
-    <ul>
-      <li>Ιστοτόπους που συλλέγουν ή μοιράζονται πληροφορίες για εσάς</li>
-      <li>Πάροχους υπηρεσιών διαδικτύου ή εργοδότες που παρακολουθούν τις ιστοσελίδες που επισκέπτεστε</li>
-      <li>Κακόβουλο λογισμικό που καταγράφει ότι πληκτρολογείτε με αντάλλαγμα δωρεάν "φατσούλες"</li>
-      <li>Παρακολούθηση από μυστικούς πράκτορες</li>
-      <li>Άτομα που στέκονται πίσω σας</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-en-rGB-xlarge/incognito_mode_start_page.html b/core/res/res/raw-en-rGB-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 7436f98..0000000
--- a/core/res/res/raw-en-rGB-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>New incognito window</title>
-  </head>
-  <body>
-    <p><strong>You've gone incognito</strong>. Pages that you view in this window won't appear in your browser history or search history, and they won't leave other traces, such as cookies, on your device after you close the incognito window. However, any files that you download or bookmarks that you create will be preserved.</p>
-
-    <p><strong>Going incognito doesn't affect the behaviour of other people, servers or software. Be cautious of:</strong></p>
-
-    <ul>
-      <li>Websites that collect or share information about you</li>
-      <li>Internet service providers or employers that track the pages that you visit</li>
-      <li>Malicious software that tracks your keystrokes in exchange for free smileys</li>
-      <li>Surveillance by secret agents</li>
-      <li>People standing behind you</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-en-rGB/incognito_mode_start_page.html b/core/res/res/raw-en-rGB/incognito_mode_start_page.html
deleted file mode 100644
index 7436f98..0000000
--- a/core/res/res/raw-en-rGB/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>New incognito window</title>
-  </head>
-  <body>
-    <p><strong>You've gone incognito</strong>. Pages that you view in this window won't appear in your browser history or search history, and they won't leave other traces, such as cookies, on your device after you close the incognito window. However, any files that you download or bookmarks that you create will be preserved.</p>
-
-    <p><strong>Going incognito doesn't affect the behaviour of other people, servers or software. Be cautious of:</strong></p>
-
-    <ul>
-      <li>Websites that collect or share information about you</li>
-      <li>Internet service providers or employers that track the pages that you visit</li>
-      <li>Malicious software that tracks your keystrokes in exchange for free smileys</li>
-      <li>Surveillance by secret agents</li>
-      <li>People standing behind you</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-es-rUS-xlarge/incognito_mode_start_page.html b/core/res/res/raw-es-rUS-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index d283df5..0000000
--- a/core/res/res/raw-es-rUS-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nueva ventana de incógnito</title>
-  </head>
-  <body>
-    <p><strong>Estás de incógnito</strong>. Las páginas que veas en esta ventana no aparecerán en el historial de tu navegador ni en el historial de búsquedas, y cuando cierres la ventana de incógnito, tampoco quedará ningún otro rastro, como cookies, en tu dispositivo. Sin embargo, sí se preservarán los archivos que descargues o los favoritos que marques.</p>
-
-    <p><strong>Estar de incógnito no afecta el comportamiento de otras personas, servidores o programas. Ten cuidado con:</strong></p>
-
-    <ul>
-      <li>Sitios web que recaban o comparten tu información</li>
-      <li>Proveedores de servicio de Internet o empleadores que hacen un seguimiento de las páginas que visitas</li>
-      <li>Programas de software maliciosos que hacen un seguimiento de la actividad de tu teclado a cambio de emoticones gratuitos</li>
-      <li>Vigilancia a cargo de agentes secretos</li>
-      <li>Personas paradas atrás tuyo</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-es-rUS/incognito_mode_start_page.html b/core/res/res/raw-es-rUS/incognito_mode_start_page.html
deleted file mode 100644
index d283df5..0000000
--- a/core/res/res/raw-es-rUS/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nueva ventana de incógnito</title>
-  </head>
-  <body>
-    <p><strong>Estás de incógnito</strong>. Las páginas que veas en esta ventana no aparecerán en el historial de tu navegador ni en el historial de búsquedas, y cuando cierres la ventana de incógnito, tampoco quedará ningún otro rastro, como cookies, en tu dispositivo. Sin embargo, sí se preservarán los archivos que descargues o los favoritos que marques.</p>
-
-    <p><strong>Estar de incógnito no afecta el comportamiento de otras personas, servidores o programas. Ten cuidado con:</strong></p>
-
-    <ul>
-      <li>Sitios web que recaban o comparten tu información</li>
-      <li>Proveedores de servicio de Internet o empleadores que hacen un seguimiento de las páginas que visitas</li>
-      <li>Programas de software maliciosos que hacen un seguimiento de la actividad de tu teclado a cambio de emoticones gratuitos</li>
-      <li>Vigilancia a cargo de agentes secretos</li>
-      <li>Personas paradas atrás tuyo</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-es-xlarge/incognito_mode_start_page.html b/core/res/res/raw-es-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 6d9b501..0000000
--- a/core/res/res/raw-es-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nueva ventana de incógnito</title>
-  </head>
-  <body>
-    <p><strong>Estás navegando de incógnito</strong>. Las páginas que consultes a través de esta ventana no quedarán registradas en el historial del navegador ni en el historial de búsquedas, y tampoco dejarán otros rastros en el ordenador (como cookies) una vez cerrada. Los archivos que descargues y los marcadores que guardes sí se almacenarán. </p>
-
-    <p><strong>La función de navegación de incógnito no afecta al comportamiento de los servidores o programas de software. Ten cuidado con:</strong></p>
-
-    <ul>
-      <li>sitios web que recopilan o comparten información personal</li>
-      <li>proveedores de servicios de Internet o trabajadores de estas empresas que supervisan las páginas que visitas</li>
-      <li>software malicioso que realiza un seguimiento de las teclas que pulsas a cambio de emoticonos gratuitos</li>
-      <li>actividades de seguimiento por parte de terceros</li>
-      <li>personas merodeando cerca de tu ordenador</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-es/incognito_mode_start_page.html b/core/res/res/raw-es/incognito_mode_start_page.html
deleted file mode 100644
index 6d9b501..0000000
--- a/core/res/res/raw-es/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nueva ventana de incógnito</title>
-  </head>
-  <body>
-    <p><strong>Estás navegando de incógnito</strong>. Las páginas que consultes a través de esta ventana no quedarán registradas en el historial del navegador ni en el historial de búsquedas, y tampoco dejarán otros rastros en el ordenador (como cookies) una vez cerrada. Los archivos que descargues y los marcadores que guardes sí se almacenarán. </p>
-
-    <p><strong>La función de navegación de incógnito no afecta al comportamiento de los servidores o programas de software. Ten cuidado con:</strong></p>
-
-    <ul>
-      <li>sitios web que recopilan o comparten información personal</li>
-      <li>proveedores de servicios de Internet o trabajadores de estas empresas que supervisan las páginas que visitas</li>
-      <li>software malicioso que realiza un seguimiento de las teclas que pulsas a cambio de emoticonos gratuitos</li>
-      <li>actividades de seguimiento por parte de terceros</li>
-      <li>personas merodeando cerca de tu ordenador</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-fa-xlarge/incognito_mode_start_page.html b/core/res/res/raw-fa-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index f004120..0000000
--- a/core/res/res/raw-fa-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="RTL">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>پنجره ناشناس جدید</title>
-  </head>
-  <body>
-    <p><strong>شما به صورت ناشناس وارد شده اید</strong> صفحاتی که شما در این پنجره مشاهده میکنید در سابقه مرورگر یا سابقه جستجوی شما ظاهر نمیشوند، و پس از بستن پنجره ناشناس، دنباله ای از خود مانند کوکی ها روی دستگاه شما بر جای نمیگذارند. به هر حال، فایل های دانلود شده و نشانکهای صفحه ای که ایجاد کرده اید باقی خواهند ماند.</p>
-
-    <p><strong>وارد شدن به صورت ناشناس، تاثیری بر روی رفتار افراد دیگر، سرورها و یا نرم افزارها ندارد. در خصوص موارد زیر هشیار باشید:</strong></p>
-
-    <ul>
-      <li>وبسایت هایی که اطلاعاتی را در مورد شما جمع آوری کرده و به اشتراک میگذارند</li>
-      <li>تامین کننده های خدمات اینترنتی شما یا کارمندانی که صفحاتی که شما بازدید کرده اید را پیگیری میکنند</li>
-      <li>نرم افزارهای مخربی که در ازای نشانک های رایگان، ضربات کلیدهای شما را ذخیره میکنند</li>
-      <li>نظارت توسط نمایندگان سری</li>
-      <li>افرادی که پشت سرتان ایستاده اند</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-fa/incognito_mode_start_page.html b/core/res/res/raw-fa/incognito_mode_start_page.html
deleted file mode 100644
index f004120..0000000
--- a/core/res/res/raw-fa/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="RTL">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>پنجره ناشناس جدید</title>
-  </head>
-  <body>
-    <p><strong>شما به صورت ناشناس وارد شده اید</strong> صفحاتی که شما در این پنجره مشاهده میکنید در سابقه مرورگر یا سابقه جستجوی شما ظاهر نمیشوند، و پس از بستن پنجره ناشناس، دنباله ای از خود مانند کوکی ها روی دستگاه شما بر جای نمیگذارند. به هر حال، فایل های دانلود شده و نشانکهای صفحه ای که ایجاد کرده اید باقی خواهند ماند.</p>
-
-    <p><strong>وارد شدن به صورت ناشناس، تاثیری بر روی رفتار افراد دیگر، سرورها و یا نرم افزارها ندارد. در خصوص موارد زیر هشیار باشید:</strong></p>
-
-    <ul>
-      <li>وبسایت هایی که اطلاعاتی را در مورد شما جمع آوری کرده و به اشتراک میگذارند</li>
-      <li>تامین کننده های خدمات اینترنتی شما یا کارمندانی که صفحاتی که شما بازدید کرده اید را پیگیری میکنند</li>
-      <li>نرم افزارهای مخربی که در ازای نشانک های رایگان، ضربات کلیدهای شما را ذخیره میکنند</li>
-      <li>نظارت توسط نمایندگان سری</li>
-      <li>افرادی که پشت سرتان ایستاده اند</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-fi-xlarge/incognito_mode_start_page.html b/core/res/res/raw-fi-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index feb9f4f..0000000
--- a/core/res/res/raw-fi-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Uusi incognito-ikkuna</title>
-  </head>
-  <body>
-    <p><strong>Olet nyt incognito-tilassa</strong>. Incognito-tilassa katsellut sivut eivät näy selainhistoriassa eikä hakuhistoriassa. Ne eivät myöskään jätä muita jälkiä, kuten evästeitä, sivun sulkemisen jälkeen. Lataamasi tiedostot tai luomasi kirjanmerkit tosin tallentuvat.</p>
-
-    <p><strong>Incognito-tilaan siirtyminen ei vaikuta muiden ihmisten, palvelimien tai tietokoneohjelmien toimintaan. Varo:</strong></p>
-
-    <ul>
-      <li>Verkkosivustoja jotka keräävät sinusta tietoa ja/tai jakavat sitä eteenpäin</li>
-      <li>Internet-palveluntarjoajia ja työnantajia jotka seuraavat sivuja, joilla käyt</li>
-      <li>Haittaohjelmia jotka seuraavat näppäimistön toimintaa ja tarjoavat vastineeksi ilmaisia hymiöitä</li>
-      <li>Salaisten agenttien seurantaa</li>
-      <li>Ihmisiä jotka seisovat takanasi</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-fi/incognito_mode_start_page.html b/core/res/res/raw-fi/incognito_mode_start_page.html
deleted file mode 100644
index feb9f4f..0000000
--- a/core/res/res/raw-fi/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Uusi incognito-ikkuna</title>
-  </head>
-  <body>
-    <p><strong>Olet nyt incognito-tilassa</strong>. Incognito-tilassa katsellut sivut eivät näy selainhistoriassa eikä hakuhistoriassa. Ne eivät myöskään jätä muita jälkiä, kuten evästeitä, sivun sulkemisen jälkeen. Lataamasi tiedostot tai luomasi kirjanmerkit tosin tallentuvat.</p>
-
-    <p><strong>Incognito-tilaan siirtyminen ei vaikuta muiden ihmisten, palvelimien tai tietokoneohjelmien toimintaan. Varo:</strong></p>
-
-    <ul>
-      <li>Verkkosivustoja jotka keräävät sinusta tietoa ja/tai jakavat sitä eteenpäin</li>
-      <li>Internet-palveluntarjoajia ja työnantajia jotka seuraavat sivuja, joilla käyt</li>
-      <li>Haittaohjelmia jotka seuraavat näppäimistön toimintaa ja tarjoavat vastineeksi ilmaisia hymiöitä</li>
-      <li>Salaisten agenttien seurantaa</li>
-      <li>Ihmisiä jotka seisovat takanasi</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-fr-xlarge/incognito_mode_start_page.html b/core/res/res/raw-fr-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 8962cdf..0000000
--- a/core/res/res/raw-fr-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nouvelle fenêtre de navigation privée</title>
-  </head>
-  <body>
-    <p><strong>Vous êtes passé en navigation privée</strong>. Les pages que vous consultez dans cette fenêtre n'apparaîtront ni dans l'historique de votre navigateur, ni dans l'historique des recherches, et ne laisseront aucune trace (comme les cookies) sur votre appareil une fois que vous aurez fermé la fenêtre de navigation privée. Tous les fichiers téléchargés et les favoris créés seront toutefois conservés.</p>
-
-    <p><strong>Passer en navigation privée n'a aucun effet sur les autres utilisateurs, serveurs ou logiciels. Méfiez-vous :</strong></p>
-
-    <ul>
-      <li>Des sites Web qui collectent ou partagent des informations vous concernant</li>
-      <li>Des fournisseurs d'accès Internet ou des employeurs qui conservent une trace des pages que vous visitez</li>
-      <li>Des programmes indésirables qui enregistrent vos frappes en échange d'émoticônes gratuites</li>
-      <li>Des personnes qui pourraient surveiller vos activités</li>
-      <li>Des personnes qui se tiennent derrière vous</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-fr/incognito_mode_start_page.html b/core/res/res/raw-fr/incognito_mode_start_page.html
deleted file mode 100644
index 8962cdf..0000000
--- a/core/res/res/raw-fr/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nouvelle fenêtre de navigation privée</title>
-  </head>
-  <body>
-    <p><strong>Vous êtes passé en navigation privée</strong>. Les pages que vous consultez dans cette fenêtre n'apparaîtront ni dans l'historique de votre navigateur, ni dans l'historique des recherches, et ne laisseront aucune trace (comme les cookies) sur votre appareil une fois que vous aurez fermé la fenêtre de navigation privée. Tous les fichiers téléchargés et les favoris créés seront toutefois conservés.</p>
-
-    <p><strong>Passer en navigation privée n'a aucun effet sur les autres utilisateurs, serveurs ou logiciels. Méfiez-vous :</strong></p>
-
-    <ul>
-      <li>Des sites Web qui collectent ou partagent des informations vous concernant</li>
-      <li>Des fournisseurs d'accès Internet ou des employeurs qui conservent une trace des pages que vous visitez</li>
-      <li>Des programmes indésirables qui enregistrent vos frappes en échange d'émoticônes gratuites</li>
-      <li>Des personnes qui pourraient surveiller vos activités</li>
-      <li>Des personnes qui se tiennent derrière vous</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-hi-xlarge/incognito_mode_start_page.html b/core/res/res/raw-hi-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index f73c41d..0000000
--- a/core/res/res/raw-hi-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>नई गुप्त विंडो</title>
-  </head>
-  <body>
-    <p><strong>आप गुप्त मोड में हैं</strong>. इस विंडो में देखे गए पृष्ठ आपके ब्राउज़र इतिहास  या खोज इतिहास में प्रकट नहीं होंगे, और वे आपके डिवाइस पर गुप्त विंडो बंद करने के बाद और कोई चिह्न जैसे कुकीज़, नहीं छोड़ते. हालांकि डाउनलोड की गई या बुकमार्क की गई कोई भी फ़ाइल संरक्षित रखी जाएगी.</p>
-
-    <p><strong>गुप्त मोड में होने से दूसरे लोगों, सर्वर. या सॉफ़्टवेयर पर कोई प्रभाव नहीं होता. इनका ध्यान रखें</strong></p>
-
-    <ul>
-      <li>वे वेबसाइट जो आपके बारे में जानकारी एकत्र या शेयर करती हैं</li>
-      <li>इंटरनेट सेवा प्रदाता या नियोक्ता जो आपके द्वारा विज़िट किए गए पृष्ठों पर नज़र रखते हैं</li>
-      <li>दुर्भावनापूर्ण सॉफ़्टवेयर जो मुफ़्त स्माइली के बदले आपके कीस्ट्रोक पर नज़र रखते हैं.</li>
-      <li>गुप्तचरों द्वारा निगरानी</li>
-      <li>आपके पीछे खड़े लोग</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-hi/incognito_mode_start_page.html b/core/res/res/raw-hi/incognito_mode_start_page.html
deleted file mode 100644
index f73c41d..0000000
--- a/core/res/res/raw-hi/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>नई गुप्त विंडो</title>
-  </head>
-  <body>
-    <p><strong>आप गुप्त मोड में हैं</strong>. इस विंडो में देखे गए पृष्ठ आपके ब्राउज़र इतिहास  या खोज इतिहास में प्रकट नहीं होंगे, और वे आपके डिवाइस पर गुप्त विंडो बंद करने के बाद और कोई चिह्न जैसे कुकीज़, नहीं छोड़ते. हालांकि डाउनलोड की गई या बुकमार्क की गई कोई भी फ़ाइल संरक्षित रखी जाएगी.</p>
-
-    <p><strong>गुप्त मोड में होने से दूसरे लोगों, सर्वर. या सॉफ़्टवेयर पर कोई प्रभाव नहीं होता. इनका ध्यान रखें</strong></p>
-
-    <ul>
-      <li>वे वेबसाइट जो आपके बारे में जानकारी एकत्र या शेयर करती हैं</li>
-      <li>इंटरनेट सेवा प्रदाता या नियोक्ता जो आपके द्वारा विज़िट किए गए पृष्ठों पर नज़र रखते हैं</li>
-      <li>दुर्भावनापूर्ण सॉफ़्टवेयर जो मुफ़्त स्माइली के बदले आपके कीस्ट्रोक पर नज़र रखते हैं.</li>
-      <li>गुप्तचरों द्वारा निगरानी</li>
-      <li>आपके पीछे खड़े लोग</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-hr-xlarge/incognito_mode_start_page.html b/core/res/res/raw-hr-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 6f20fe0..0000000
--- a/core/res/res/raw-hr-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Novi anonimni prozor</title>
-  </head>
-  <body>
-    <p><strong>Sada ste anonimni</strong>. Stranice koje pregledavate u ovom prozoru neće se pojaviti u povijesti preglednika ili povijesti pretraživanja, neće ostaviti tragove, poput kolačića, na vašem uređaju nakon što zatvorite anonimni prozor. Međutim, sve datoteke koje preuzmete ili oznake koje stvorite bit će sačuvane.</p>
-
-    <p><strong>Anonimno pregledavanje neće utjecati na ponašanje drugih osoba, poslužitelja ili softvera. Pazite na sljedeće:</strong></p>
-
-    <ul>
-      <li>Web-lokacije koje prikupljaju ili dijele informacije o vama</li>
-      <li>Davatelje internetskih usluga ili poslodavce koji prate stranice koje posjećujete</li>
-      <li>Zlonamjerni softver koji prati koje tipke pritišćete u zamjenu za besplatne emotikone</li>
-      <li>Nadzor tajnih službi</li>
-      <li>Osobe koje stoje iza vas</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-hr/incognito_mode_start_page.html b/core/res/res/raw-hr/incognito_mode_start_page.html
deleted file mode 100644
index 6f20fe0..0000000
--- a/core/res/res/raw-hr/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Novi anonimni prozor</title>
-  </head>
-  <body>
-    <p><strong>Sada ste anonimni</strong>. Stranice koje pregledavate u ovom prozoru neće se pojaviti u povijesti preglednika ili povijesti pretraživanja, neće ostaviti tragove, poput kolačića, na vašem uređaju nakon što zatvorite anonimni prozor. Međutim, sve datoteke koje preuzmete ili oznake koje stvorite bit će sačuvane.</p>
-
-    <p><strong>Anonimno pregledavanje neće utjecati na ponašanje drugih osoba, poslužitelja ili softvera. Pazite na sljedeće:</strong></p>
-
-    <ul>
-      <li>Web-lokacije koje prikupljaju ili dijele informacije o vama</li>
-      <li>Davatelje internetskih usluga ili poslodavce koji prate stranice koje posjećujete</li>
-      <li>Zlonamjerni softver koji prati koje tipke pritišćete u zamjenu za besplatne emotikone</li>
-      <li>Nadzor tajnih službi</li>
-      <li>Osobe koje stoje iza vas</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-hu-xlarge/incognito_mode_start_page.html b/core/res/res/raw-hu-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 38b0806..0000000
--- a/core/res/res/raw-hu-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Új inkognitóablak</title>
-  </head>
-  <body>
-    <p><strong>Ön inkognitó módra váltott</strong>. Az inkognitóablakban megtekintett oldalak nem jelennek meg böngészője előzményeiben, illetve keresési előzményeiben, és nem hagynak más nyomot pl. cookie-t sem a számítógépén az inkognitóablak bezárását követően. A letöltött fájlok, valamint a létrehozott könyvjelzők azonban megmaradnak.</p>
-
-    <p><strong>Az inkognitó üzemmód nem befolyásolja a többi felhasználó, szerver vagy szoftver viselkedését. Ügyeljen a következőkre:</strong></p>
-
-    <ul>
-      <li>Olyan webhelyek, amelyek információt gyűjtenek vagy osztanak meg Önről</li>
-      <li>Olyan internetszolgáltatók vagy alkalmazottaik, akik nyomon követik az Ön által látogatott oldalakat</li>
-      <li>Olyan kártékony szoftverek, amelyek ingyenes hangulatjelekért cserébe nyomon követik billentyűbeviteleit</li>
-      <li>Titkos ügynökök megfigyelése</li>
-      <li>Az Ön mögött álló emberek</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-hu/incognito_mode_start_page.html b/core/res/res/raw-hu/incognito_mode_start_page.html
deleted file mode 100644
index 38b0806..0000000
--- a/core/res/res/raw-hu/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Új inkognitóablak</title>
-  </head>
-  <body>
-    <p><strong>Ön inkognitó módra váltott</strong>. Az inkognitóablakban megtekintett oldalak nem jelennek meg böngészője előzményeiben, illetve keresési előzményeiben, és nem hagynak más nyomot pl. cookie-t sem a számítógépén az inkognitóablak bezárását követően. A letöltött fájlok, valamint a létrehozott könyvjelzők azonban megmaradnak.</p>
-
-    <p><strong>Az inkognitó üzemmód nem befolyásolja a többi felhasználó, szerver vagy szoftver viselkedését. Ügyeljen a következőkre:</strong></p>
-
-    <ul>
-      <li>Olyan webhelyek, amelyek információt gyűjtenek vagy osztanak meg Önről</li>
-      <li>Olyan internetszolgáltatók vagy alkalmazottaik, akik nyomon követik az Ön által látogatott oldalakat</li>
-      <li>Olyan kártékony szoftverek, amelyek ingyenes hangulatjelekért cserébe nyomon követik billentyűbeviteleit</li>
-      <li>Titkos ügynökök megfigyelése</li>
-      <li>Az Ön mögött álló emberek</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-id-xlarge/incognito_mode_start_page.html b/core/res/res/raw-id-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 788c088..0000000
--- a/core/res/res/raw-id-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Jendela penyamaran baru</title>
-  </head>
-  <body>
-    <p><strong>Anda menggunakan penyamaran</strong>. Laman yang Anda lihat di jendela ini tidak akan ditampilkan dalam riwayat peramban atau riwayat penelusuran, dan tidak akan meninggalkan jejak, seperti kuki, di perangkat setelah jendela penyamaran ditutup. Namun, berkas yang diunduh atau bookmark yang dibuat akan disimpan.</p>
-
-    <p><strong>Menggunakan penyamaran tidak mempengaruhi perilaku orang lain, server, atau perangkat lunak. Waspadai:</strong></p>
-
-    <ul>
-      <li>Situs web yang mengumpulkan atau berbagi informasi tentang Anda</li>
-      <li>Penyedia layanan internet atau tempat kerja yang melacak laman yang Anda kunjungi</li>
-      <li>Perangkat lunak jahat yang melacak penekanan tombol dengan imbalan smiley gratis</li>
-      <li>Pemantauan oleh agen rahasia</li>
-      <li>Orang yang berdiri di belakang Anda</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-id/incognito_mode_start_page.html b/core/res/res/raw-id/incognito_mode_start_page.html
deleted file mode 100644
index 788c088..0000000
--- a/core/res/res/raw-id/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Jendela penyamaran baru</title>
-  </head>
-  <body>
-    <p><strong>Anda menggunakan penyamaran</strong>. Laman yang Anda lihat di jendela ini tidak akan ditampilkan dalam riwayat peramban atau riwayat penelusuran, dan tidak akan meninggalkan jejak, seperti kuki, di perangkat setelah jendela penyamaran ditutup. Namun, berkas yang diunduh atau bookmark yang dibuat akan disimpan.</p>
-
-    <p><strong>Menggunakan penyamaran tidak mempengaruhi perilaku orang lain, server, atau perangkat lunak. Waspadai:</strong></p>
-
-    <ul>
-      <li>Situs web yang mengumpulkan atau berbagi informasi tentang Anda</li>
-      <li>Penyedia layanan internet atau tempat kerja yang melacak laman yang Anda kunjungi</li>
-      <li>Perangkat lunak jahat yang melacak penekanan tombol dengan imbalan smiley gratis</li>
-      <li>Pemantauan oleh agen rahasia</li>
-      <li>Orang yang berdiri di belakang Anda</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-it-xlarge/incognito_mode_start_page.html b/core/res/res/raw-it-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 4a34874..0000000
--- a/core/res/res/raw-it-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nuova finestra di navigazione in incognito</title>
-  </head>
-  <body>
-    <p><strong>Sei passato alla navigazione in incognito</strong>. Le pagine aperte in questa finestra non vengono registrate nella cronologia di navigazione o di ricerca, e non lasciano traccia sul tuo computer, ad esempio sotto forma di cookie, una volta chiusa la finestra. Tuttavia, qualsiasi file scaricato o preferito creato verrà conservato.</p>
-
-    <p><strong>La navigazione in incognito non influisce sul comportamento di altri utenti, server o software. Diffida di:</strong></p>
-
-    <ul>
-      <li>Siti web che raccolgono o condividono informazioni su di te</li>
-      <li>Provider di servizi Internet o datori di lavoro che registrano le pagine da te visitate</li>
-      <li>Software dannosi che registrano le sequenze di tasti da te utilizzate in cambio di smiley gratuiti</li>
-      <li>Agenti segreti</li>
-      <li>Persone che ti stanno alle spalle</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-it/incognito_mode_start_page.html b/core/res/res/raw-it/incognito_mode_start_page.html
deleted file mode 100644
index 4a34874..0000000
--- a/core/res/res/raw-it/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nuova finestra di navigazione in incognito</title>
-  </head>
-  <body>
-    <p><strong>Sei passato alla navigazione in incognito</strong>. Le pagine aperte in questa finestra non vengono registrate nella cronologia di navigazione o di ricerca, e non lasciano traccia sul tuo computer, ad esempio sotto forma di cookie, una volta chiusa la finestra. Tuttavia, qualsiasi file scaricato o preferito creato verrà conservato.</p>
-
-    <p><strong>La navigazione in incognito non influisce sul comportamento di altri utenti, server o software. Diffida di:</strong></p>
-
-    <ul>
-      <li>Siti web che raccolgono o condividono informazioni su di te</li>
-      <li>Provider di servizi Internet o datori di lavoro che registrano le pagine da te visitate</li>
-      <li>Software dannosi che registrano le sequenze di tasti da te utilizzate in cambio di smiley gratuiti</li>
-      <li>Agenti segreti</li>
-      <li>Persone che ti stanno alle spalle</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-iw-xlarge/incognito_mode_start_page.html b/core/res/res/raw-iw-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 368dea0..0000000
--- a/core/res/res/raw-iw-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="RTL">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>חלון חדש של גלישה בסתר</title>
-  </head>
-  <body>
-    <p><strong>עברת למצב של גלישה בסתר</strong>. דפים שאתה רואה בחלון זה לא יופיעו בהיסטוריית הדפדפן או בהיסטוריית החיפושים שלך, והם לא ישאירו עקבות אחרים, כמו קובצי cookie, לאחר שתסגור את חלון הגלישה בסתר. עם זאת, כל קובץ שאתה מוריד או כוכביות שאתה יוצר יישמרו.</p>
-
-    <p><strong> גלישה בסתר לא משפיעה על התנהגותם של אנשים אחרים, שרתים  אחרים או תוכנות אחרות. היזהר מ:</strong></p>
-
-    <ul>
-      <li>אתרים שאוספים נתונים או משתפים מידע לגביך</li>
-      <li>ספקי שירות אינטרנט או עובדים שעוקבים אחר הדפים שבהם אתה מבקר</li>
-      <li>תוכנה זדונית שעוקבת אחר ההקשות שלך על המקשים בתמורה לסימני סמיילי בחינם</li>
-      <li>מעקב של סוכנים חשאיים</li>
-      <li>אנשים שעומדים מאחוריך</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-iw/incognito_mode_start_page.html b/core/res/res/raw-iw/incognito_mode_start_page.html
deleted file mode 100644
index 368dea0..0000000
--- a/core/res/res/raw-iw/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="RTL">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>חלון חדש של גלישה בסתר</title>
-  </head>
-  <body>
-    <p><strong>עברת למצב של גלישה בסתר</strong>. דפים שאתה רואה בחלון זה לא יופיעו בהיסטוריית הדפדפן או בהיסטוריית החיפושים שלך, והם לא ישאירו עקבות אחרים, כמו קובצי cookie, לאחר שתסגור את חלון הגלישה בסתר. עם זאת, כל קובץ שאתה מוריד או כוכביות שאתה יוצר יישמרו.</p>
-
-    <p><strong> גלישה בסתר לא משפיעה על התנהגותם של אנשים אחרים, שרתים  אחרים או תוכנות אחרות. היזהר מ:</strong></p>
-
-    <ul>
-      <li>אתרים שאוספים נתונים או משתפים מידע לגביך</li>
-      <li>ספקי שירות אינטרנט או עובדים שעוקבים אחר הדפים שבהם אתה מבקר</li>
-      <li>תוכנה זדונית שעוקבת אחר ההקשות שלך על המקשים בתמורה לסימני סמיילי בחינם</li>
-      <li>מעקב של סוכנים חשאיים</li>
-      <li>אנשים שעומדים מאחוריך</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ja-xlarge/incognito_mode_start_page.html b/core/res/res/raw-ja-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index a58ad05..0000000
--- a/core/res/res/raw-ja-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>新しいシークレットウィンドウ</title>
-  </head>
-  <body>
-    <p><strong>シークレットモードを使用中です</strong>。このウィンドウで開いたページはブラウザの履歴や検索履歴に残りません。このウィンドウを閉じるとCookieなどの記録も端末から消去されます。ただし、ダウンロードしたファイルやブックマークしたページは保存されます。</p>
-
-    <p><strong>シークレットモードが他のユーザーやサーバー、ソフトウェアの動作に影響することはありません。なお、下記のようなケースにご注意ください。</strong></p>
-
-    <ul>
-      <li>ユーザーの情報を収集、共有するウェブサイト</li>
-      <li>アクセスしたページをトラッキングするインターネットサービスプロバイダや雇用主</li>
-      <li>無料ダウンロードなどと一緒にインストールされ、キーストロークを記録するマルウェア</li>
-      <li>スパイ、諜報活動</li>
-      <li>背後にいる人</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ja/incognito_mode_start_page.html b/core/res/res/raw-ja/incognito_mode_start_page.html
deleted file mode 100644
index a58ad05..0000000
--- a/core/res/res/raw-ja/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>新しいシークレットウィンドウ</title>
-  </head>
-  <body>
-    <p><strong>シークレットモードを使用中です</strong>。このウィンドウで開いたページはブラウザの履歴や検索履歴に残りません。このウィンドウを閉じるとCookieなどの記録も端末から消去されます。ただし、ダウンロードしたファイルやブックマークしたページは保存されます。</p>
-
-    <p><strong>シークレットモードが他のユーザーやサーバー、ソフトウェアの動作に影響することはありません。なお、下記のようなケースにご注意ください。</strong></p>
-
-    <ul>
-      <li>ユーザーの情報を収集、共有するウェブサイト</li>
-      <li>アクセスしたページをトラッキングするインターネットサービスプロバイダや雇用主</li>
-      <li>無料ダウンロードなどと一緒にインストールされ、キーストロークを記録するマルウェア</li>
-      <li>スパイ、諜報活動</li>
-      <li>背後にいる人</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ko-xlarge/incognito_mode_start_page.html b/core/res/res/raw-ko-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 0e703b1..0000000
--- a/core/res/res/raw-ko-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>새 시크릿 창</title>
-  </head>
-  <body>
-    <p><strong>시크릿 모드로 들어오셨습니다.</strong> 이 창에서 보는 페이지는 브라우저 기록이나 검색기록에 남지 않으며, 시크릿 창을 닫은 뒤 기기에 쿠기와 같은 흔적도 남기지 않습니다. 다운로드한 파일이나 생성한 북마크는 보관됩니다. </p>
-
-    <p><strong>시크릿 모드를 이용해도 다른 사용자, 서버, 소프트웨어에 영향을 주지는 않습니다. 다음을 주의하세요.</strong></p>
-
-    <ul>
-      <li>사용자에 대한 정보를 모으고 공유하는 웹사이트</li>
-      <li>방문 페이지를 추적하는 인터넷 서비스 제공업체나 직원 </li>
-      <li>스마일 이모티콘을 제공한다는 명목으로 입력 내용을 추적하는 악성 소프트웨어</li>
-      <li>비밀 개체를 통한 감시</li>
-      <li>뒤에서 사용자의 작업내용을 지켜보는 사람</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ko/incognito_mode_start_page.html b/core/res/res/raw-ko/incognito_mode_start_page.html
deleted file mode 100644
index 0e703b1..0000000
--- a/core/res/res/raw-ko/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>새 시크릿 창</title>
-  </head>
-  <body>
-    <p><strong>시크릿 모드로 들어오셨습니다.</strong> 이 창에서 보는 페이지는 브라우저 기록이나 검색기록에 남지 않으며, 시크릿 창을 닫은 뒤 기기에 쿠기와 같은 흔적도 남기지 않습니다. 다운로드한 파일이나 생성한 북마크는 보관됩니다. </p>
-
-    <p><strong>시크릿 모드를 이용해도 다른 사용자, 서버, 소프트웨어에 영향을 주지는 않습니다. 다음을 주의하세요.</strong></p>
-
-    <ul>
-      <li>사용자에 대한 정보를 모으고 공유하는 웹사이트</li>
-      <li>방문 페이지를 추적하는 인터넷 서비스 제공업체나 직원 </li>
-      <li>스마일 이모티콘을 제공한다는 명목으로 입력 내용을 추적하는 악성 소프트웨어</li>
-      <li>비밀 개체를 통한 감시</li>
-      <li>뒤에서 사용자의 작업내용을 지켜보는 사람</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-lt-xlarge/incognito_mode_start_page.html b/core/res/res/raw-lt-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 9244ae4..0000000
--- a/core/res/res/raw-lt-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Naujas inkognito langas</title>
-  </head>
-  <body>
-    <p><strong>Naršote inkognito režimu</strong>. Šiame lange peržiūrimi puslapiai nebus išsaugomi naršyklės istorijoje ar paieškos istorijoje ir uždarius inkognito langą nepaliks jokių kitų žymių, pvz., slapukų. Tačiau bus išsaugoti atsisiųsti failai ar sukurtos žymos.</p>
-
-    <p><strong>Naršymas inkognito režimu nedaro jokios įtakos kitiems asmenims, serveriams ar programinei įrangai. Saugokitės:</strong></p>
-
-    <ul>
-      <li>svetainių, kurios renka ar platina informaciją apie jus</li>
-      <li>interneto paslaugos teikėjų ar darbdavių, kurie stebi, kuriuos puslapius peržiūrite</li>
-      <li>kenkėjiškos programinės įrangos, kuri siūlydama nemokamų šypsniukų fiksuoja klavišų paspaudimus</li>
-      <li>jus galinčių sekti slaptųjų agentų</li>
-      <li>už nugaros stovinčių asmenų</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-lt/incognito_mode_start_page.html b/core/res/res/raw-lt/incognito_mode_start_page.html
deleted file mode 100644
index 9244ae4..0000000
--- a/core/res/res/raw-lt/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Naujas inkognito langas</title>
-  </head>
-  <body>
-    <p><strong>Naršote inkognito režimu</strong>. Šiame lange peržiūrimi puslapiai nebus išsaugomi naršyklės istorijoje ar paieškos istorijoje ir uždarius inkognito langą nepaliks jokių kitų žymių, pvz., slapukų. Tačiau bus išsaugoti atsisiųsti failai ar sukurtos žymos.</p>
-
-    <p><strong>Naršymas inkognito režimu nedaro jokios įtakos kitiems asmenims, serveriams ar programinei įrangai. Saugokitės:</strong></p>
-
-    <ul>
-      <li>svetainių, kurios renka ar platina informaciją apie jus</li>
-      <li>interneto paslaugos teikėjų ar darbdavių, kurie stebi, kuriuos puslapius peržiūrite</li>
-      <li>kenkėjiškos programinės įrangos, kuri siūlydama nemokamų šypsniukų fiksuoja klavišų paspaudimus</li>
-      <li>jus galinčių sekti slaptųjų agentų</li>
-      <li>už nugaros stovinčių asmenų</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-lv-xlarge/incognito_mode_start_page.html b/core/res/res/raw-lv-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index f325ef5..0000000
--- a/core/res/res/raw-lv-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Jauns inkognito logs</title>
-  </head>
-  <body>
-    <p><strong>Jūs esat ieslēdzis inkognito režīmu</strong>. Šajā logā aplūkotās lapas neparādīsies jūsu pārlūkprogrammas vēsturē vai meklēšanas vēsturē, tās arī neatstās citas pēdas, piemēram, sīkfailus, jūsu ierīcē pēc inkognito režīma loga aizvēršanas. Tomēr visi jūsu lejupielādētie faili vai izveidotās grāmatzīmes tiks saglabātas.</p>
-
-    <p><strong>Inkognito režīma ieslēgšana neietekmēs citu personu, serveru vai programmatūras darbību. Uzmanieties no</strong></p>
-
-    <ul>
-      <li>vietnēm, kas apkopo vai koplieto informāciju par jums;</li>
-      <li>interneta pakalpojumu sniedzējiem vai darba devējiem, kas izseko jūsu apmeklētajām lapām;</li>
-      <li>maldprogrammatūras, kas izseko jūsu taustiņsitieniem apmaiņā par bezmaksas smaidiņiem;</li>
-      <li>slepeno aģentu veiktas izmeklēšanas;</li>
-      <li>personām, kas stāv aiz jums.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-lv/incognito_mode_start_page.html b/core/res/res/raw-lv/incognito_mode_start_page.html
deleted file mode 100644
index f325ef5..0000000
--- a/core/res/res/raw-lv/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Jauns inkognito logs</title>
-  </head>
-  <body>
-    <p><strong>Jūs esat ieslēdzis inkognito režīmu</strong>. Šajā logā aplūkotās lapas neparādīsies jūsu pārlūkprogrammas vēsturē vai meklēšanas vēsturē, tās arī neatstās citas pēdas, piemēram, sīkfailus, jūsu ierīcē pēc inkognito režīma loga aizvēršanas. Tomēr visi jūsu lejupielādētie faili vai izveidotās grāmatzīmes tiks saglabātas.</p>
-
-    <p><strong>Inkognito režīma ieslēgšana neietekmēs citu personu, serveru vai programmatūras darbību. Uzmanieties no</strong></p>
-
-    <ul>
-      <li>vietnēm, kas apkopo vai koplieto informāciju par jums;</li>
-      <li>interneta pakalpojumu sniedzējiem vai darba devējiem, kas izseko jūsu apmeklētajām lapām;</li>
-      <li>maldprogrammatūras, kas izseko jūsu taustiņsitieniem apmaiņā par bezmaksas smaidiņiem;</li>
-      <li>slepeno aģentu veiktas izmeklēšanas;</li>
-      <li>personām, kas stāv aiz jums.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-nb-xlarge/incognito_mode_start_page.html b/core/res/res/raw-nb-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 724e734..0000000
--- a/core/res/res/raw-nb-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nytt inkognitovindu</title>
-  </head>
-  <body>
-    <p><strong></strong>Du er nå inkognito Sider du besøker i dette vinduet blir ikke lagret i nettleser- eller søkelogg, og etterlater ikke andre spor, f. eks. informasjonskapsler, på enheten din etter at du har lukket vinduet. Filer du laster ned eller bokmerker du lager blir derimot lagret.</p>
-
-    <p><strong>Det at du er inkognito endrer ikke hvordan andre mennesker, tjenere eller programmer oppfører seg. Pass deg for:</strong></p>
-
-    <ul>
-      <li>Nettsider som samler eller deler informasjon om deg</li>
-      <li>Nettleverandører eller arbeidsgivere som overvåker hvilke sider du besøker</li>
-      <li>Skadelige programmer som følger med på tastetrykk i bytte mot smilefjes</li>
-      <li>Hemmelige agenter som spionerer på deg</li>
-      <li>Folk som titter over skulderen din</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-nb/incognito_mode_start_page.html b/core/res/res/raw-nb/incognito_mode_start_page.html
deleted file mode 100644
index 724e734..0000000
--- a/core/res/res/raw-nb/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nytt inkognitovindu</title>
-  </head>
-  <body>
-    <p><strong></strong>Du er nå inkognito Sider du besøker i dette vinduet blir ikke lagret i nettleser- eller søkelogg, og etterlater ikke andre spor, f. eks. informasjonskapsler, på enheten din etter at du har lukket vinduet. Filer du laster ned eller bokmerker du lager blir derimot lagret.</p>
-
-    <p><strong>Det at du er inkognito endrer ikke hvordan andre mennesker, tjenere eller programmer oppfører seg. Pass deg for:</strong></p>
-
-    <ul>
-      <li>Nettsider som samler eller deler informasjon om deg</li>
-      <li>Nettleverandører eller arbeidsgivere som overvåker hvilke sider du besøker</li>
-      <li>Skadelige programmer som følger med på tastetrykk i bytte mot smilefjes</li>
-      <li>Hemmelige agenter som spionerer på deg</li>
-      <li>Folk som titter over skulderen din</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-nl-xlarge/incognito_mode_start_page.html b/core/res/res/raw-nl-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 92fedd7..0000000
--- a/core/res/res/raw-nl-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nieuw incognitovenster</title>
-  </head>
-  <body>
-    <p><strong>U bent nu incognito</strong> De pagina's die u in dit venster bekijkt worden niet opgenomen in de browsergeschiedenis en de zoekgeschiedenis en laten geen cookies of andere sporen na op uw apparaat nadat u het incognitovenster hebt gesloten. Alle bestanden die u downloadt en bladwijzers die u maakt, blijven echter behouden.</p>
-
-    <p><strong>Incognito zijn heeft geen invloed op het gedrag van andere personen, servers of software. Wees op uw hoede voor:</strong></p>
-
-    <ul>
-      <li>Websites die informatie over u verzamelen of delen</li>
-      <li>Internetproviders of werkgevers die bijhouden welke pagina's u bezoekt</li>
-      <li>Schadelijke software die uw toetsaanslagen registreert in ruil voor gratis emoticons</li>
-      <li>Spionage door geheim agenten</li>
-      <li>Mensen die achter u staan</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-nl/incognito_mode_start_page.html b/core/res/res/raw-nl/incognito_mode_start_page.html
deleted file mode 100644
index 92fedd7..0000000
--- a/core/res/res/raw-nl/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nieuw incognitovenster</title>
-  </head>
-  <body>
-    <p><strong>U bent nu incognito</strong> De pagina's die u in dit venster bekijkt worden niet opgenomen in de browsergeschiedenis en de zoekgeschiedenis en laten geen cookies of andere sporen na op uw apparaat nadat u het incognitovenster hebt gesloten. Alle bestanden die u downloadt en bladwijzers die u maakt, blijven echter behouden.</p>
-
-    <p><strong>Incognito zijn heeft geen invloed op het gedrag van andere personen, servers of software. Wees op uw hoede voor:</strong></p>
-
-    <ul>
-      <li>Websites die informatie over u verzamelen of delen</li>
-      <li>Internetproviders of werkgevers die bijhouden welke pagina's u bezoekt</li>
-      <li>Schadelijke software die uw toetsaanslagen registreert in ruil voor gratis emoticons</li>
-      <li>Spionage door geheim agenten</li>
-      <li>Mensen die achter u staan</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-pl-xlarge/incognito_mode_start_page.html b/core/res/res/raw-pl-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 9748ead..0000000
--- a/core/res/res/raw-pl-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nowe okno incognito</title>
-  </head>
-  <body>
-    <p><strong>Jesteś teraz incognito</strong> Strony przeglądane w tym oknie nie będą wyświetlane w historii przeglądarki ani w historii wyszukiwania. Po zamknięciu okna incognito na komputerze nie zostanie po nich żaden ślad np. w postaci plików cookie. Zachowane zostaną jednak pobrane pliki lub utworzone zakładki.</p>
-
-    <p><strong>Przejście do trybu incognito nie ma wpływu na działania innych osób, serwery ani oprogramowanie. Należy uważać na:</strong></p>
-
-    <ul>
-      <li>witryny zbierające lub udostępniające dane na temat użytkowników</li>
-      <li>dostawców usług internetowych oraz pracowników monitorujących strony odwiedzane przez użytkowników</li>
-      <li>złośliwe oprogramowanie śledzące naciśnięcia klawiszy (np. w zamian za darmowe emotikony)</li>
-      <li>aktywność wywiadowczą tajnych agentów</li>
-      <li>osoby stojące za plecami</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-pl/incognito_mode_start_page.html b/core/res/res/raw-pl/incognito_mode_start_page.html
deleted file mode 100644
index 9748ead..0000000
--- a/core/res/res/raw-pl/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nowe okno incognito</title>
-  </head>
-  <body>
-    <p><strong>Jesteś teraz incognito</strong> Strony przeglądane w tym oknie nie będą wyświetlane w historii przeglądarki ani w historii wyszukiwania. Po zamknięciu okna incognito na komputerze nie zostanie po nich żaden ślad np. w postaci plików cookie. Zachowane zostaną jednak pobrane pliki lub utworzone zakładki.</p>
-
-    <p><strong>Przejście do trybu incognito nie ma wpływu na działania innych osób, serwery ani oprogramowanie. Należy uważać na:</strong></p>
-
-    <ul>
-      <li>witryny zbierające lub udostępniające dane na temat użytkowników</li>
-      <li>dostawców usług internetowych oraz pracowników monitorujących strony odwiedzane przez użytkowników</li>
-      <li>złośliwe oprogramowanie śledzące naciśnięcia klawiszy (np. w zamian za darmowe emotikony)</li>
-      <li>aktywność wywiadowczą tajnych agentów</li>
-      <li>osoby stojące za plecami</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-pt-rPT-xlarge/incognito_mode_start_page.html b/core/res/res/raw-pt-rPT-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 89a5ab0..0000000
--- a/core/res/res/raw-pt-rPT-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nova janela de navegação anónima</title>
-  </head>
-  <body>
-    <p><strong>Está no modo de navegação anónima</strong>. As páginas que visualizar nesta janela não vão aparecer nos históricos de pesquisa ou de navegação e não deixarão quaisquer vestígios (por exemplo cookies) no computador depois de fechar a janela. No entanto se transferir ficheiros ou criar marcadores, estes serão preservados.</p>
-
-    <p><strong>Navegar no modo de navegação anónima não afecta o comportamento de outras pessoas, nem o comportamento de servidores ou programas. Tenha cuidado com:</strong></p>
-
-    <ul>
-      <li>Web sites que recolhem ou partilham informações sobre si</li>
-      <li>Serviços de fornecimento de internet ou empregadores que monitorizam as páginas que você visita</li>
-      <li>Programas maliciosos que monitorizam as teclas em que carrega em troca de ícones expressivos ("smileys")</li>
-      <li>Vigilância de agentes secretos</li>
-      <li>Pessoas que estejam perto de si</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-pt-rPT/incognito_mode_start_page.html b/core/res/res/raw-pt-rPT/incognito_mode_start_page.html
deleted file mode 100644
index 89a5ab0..0000000
--- a/core/res/res/raw-pt-rPT/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nova janela de navegação anónima</title>
-  </head>
-  <body>
-    <p><strong>Está no modo de navegação anónima</strong>. As páginas que visualizar nesta janela não vão aparecer nos históricos de pesquisa ou de navegação e não deixarão quaisquer vestígios (por exemplo cookies) no computador depois de fechar a janela. No entanto se transferir ficheiros ou criar marcadores, estes serão preservados.</p>
-
-    <p><strong>Navegar no modo de navegação anónima não afecta o comportamento de outras pessoas, nem o comportamento de servidores ou programas. Tenha cuidado com:</strong></p>
-
-    <ul>
-      <li>Web sites que recolhem ou partilham informações sobre si</li>
-      <li>Serviços de fornecimento de internet ou empregadores que monitorizam as páginas que você visita</li>
-      <li>Programas maliciosos que monitorizam as teclas em que carrega em troca de ícones expressivos ("smileys")</li>
-      <li>Vigilância de agentes secretos</li>
-      <li>Pessoas que estejam perto de si</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-pt-xlarge/incognito_mode_start_page.html b/core/res/res/raw-pt-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index b301eda..0000000
--- a/core/res/res/raw-pt-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nova janela anônima</title>
-  </head>
-  <body>
-    <p><strong>Você ficou anônimo</strong>. As páginas que você vê nesta janela não aparecerão no histórico do seu navegador ou da sua pesquisa e não deixarão rastros, como cookies, no seu dispositivo depois que você fechar a janela anônima. Quaisquer arquivos que você fizer o download ou favoritos que criar serão preservados.</p>
-
-    <p><strong>Tornar-se anônimo não afeta o comportamento de outras pessoas, servidores ou software. Esteja atento a:</strong></p>
-
-    <ul>
-      <li>Websites que coletam ou compartilham informações sobre você</li>
-      <li>Provedores de serviços de internet ou funcionários que rastreiam as páginas que você visita</li>
-      <li>Softwares maliciosos que rastreiam os seus toques de teclado em troca de ícones gratuitos</li>
-      <li>Vigilância por agentes secretos</li>
-      <li>Pessoas paradas detrás de você</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-pt/incognito_mode_start_page.html b/core/res/res/raw-pt/incognito_mode_start_page.html
deleted file mode 100644
index b301eda..0000000
--- a/core/res/res/raw-pt/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nova janela anônima</title>
-  </head>
-  <body>
-    <p><strong>Você ficou anônimo</strong>. As páginas que você vê nesta janela não aparecerão no histórico do seu navegador ou da sua pesquisa e não deixarão rastros, como cookies, no seu dispositivo depois que você fechar a janela anônima. Quaisquer arquivos que você fizer o download ou favoritos que criar serão preservados.</p>
-
-    <p><strong>Tornar-se anônimo não afeta o comportamento de outras pessoas, servidores ou software. Esteja atento a:</strong></p>
-
-    <ul>
-      <li>Websites que coletam ou compartilham informações sobre você</li>
-      <li>Provedores de serviços de internet ou funcionários que rastreiam as páginas que você visita</li>
-      <li>Softwares maliciosos que rastreiam os seus toques de teclado em troca de ícones gratuitos</li>
-      <li>Vigilância por agentes secretos</li>
-      <li>Pessoas paradas detrás de você</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ro-xlarge/incognito_mode_start_page.html b/core/res/res/raw-ro-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 3e499d3..0000000
--- a/core/res/res/raw-ro-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Fereastră incognito nouă</title>
-  </head>
-  <body>
-    <p><strong>Navigaţi incognito</strong>. Paginile pe care le afişaţi în această fereastră nu vor apărea în istoricul browserului sau al căutărilor şi nu vor lăsa alte urme, precum cookie-uri, pe dispozitivul dvs. după ce închideţi fereastra incognito. Dar fişierele descărcate şi marcajele create vor fi păstrate.</p>
-
-    <p><strong>Navigarea incognito nu influenţează comportamentul altor persoane, servere sau aplicaţii software. Fiţi atent(ă) la:</strong></p>
-
-    <ul>
-      <li>site-urile web care colectează sau distribuie informaţii despre dvs.;</li>
-      <li>furnizorii de servicii de internet sau angajatorii care urmăresc paginile pe care le accesaţi;</li>
-      <li>aplicaţiile software rău intenţionate care vă urmăresc apăsările pe taste promiţându-vă că vă oferă emoticonuri gratuite;</li>
-      <li>acţiunile de monitorizare efectuate de agenţi secreţi;</li>
-      <li>persoanele din spatele dvs.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ro/incognito_mode_start_page.html b/core/res/res/raw-ro/incognito_mode_start_page.html
deleted file mode 100644
index 3e499d3..0000000
--- a/core/res/res/raw-ro/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Fereastră incognito nouă</title>
-  </head>
-  <body>
-    <p><strong>Navigaţi incognito</strong>. Paginile pe care le afişaţi în această fereastră nu vor apărea în istoricul browserului sau al căutărilor şi nu vor lăsa alte urme, precum cookie-uri, pe dispozitivul dvs. după ce închideţi fereastra incognito. Dar fişierele descărcate şi marcajele create vor fi păstrate.</p>
-
-    <p><strong>Navigarea incognito nu influenţează comportamentul altor persoane, servere sau aplicaţii software. Fiţi atent(ă) la:</strong></p>
-
-    <ul>
-      <li>site-urile web care colectează sau distribuie informaţii despre dvs.;</li>
-      <li>furnizorii de servicii de internet sau angajatorii care urmăresc paginile pe care le accesaţi;</li>
-      <li>aplicaţiile software rău intenţionate care vă urmăresc apăsările pe taste promiţându-vă că vă oferă emoticonuri gratuite;</li>
-      <li>acţiunile de monitorizare efectuate de agenţi secreţi;</li>
-      <li>persoanele din spatele dvs.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ru-xlarge/incognito_mode_start_page.html b/core/res/res/raw-ru-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index ae7b59c..0000000
--- a/core/res/res/raw-ru-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Новое окно в режиме инкогнито</title>
-  </head>
-  <body>
-    <p><strong>Вы перешли в режим инкогнито</strong>. Страницы, которые вы просматриваете в окне в режиме инкогнито, не появятся в истории вашего браузера или истории поиска, а также не оставят на вашем компьютере других следов, таких как файлы cookie, когда вы закроете это окно. Тем не менее, все файлы, которые вы загружаете, или закладки, которые вы создаете, останутся в целости и сохранности. </p>
-
-    <p><strong>Переход в режим инкогнито не влияет на поведение других пользователей, серверов или программ. Опасайтесь:</strong></p>
-
-    <ul>
-      <li>Веб-сайтов, которые собирают информацию о вас или передают ее другим</li>
-      <li>Поставщиков услуг Интернета или их сотрудников, которые отслеживают, какие страницы вы посещаете</li>
-      <li>Вредоносного ПО, которое отслеживает нажатие клавиш клавиатуры в обмен на бесплатные смайлики</li>
-      <li>Слежки тайными агентами</li>
-      <li>Людей, которые стоят у вас за спиной</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-ru/incognito_mode_start_page.html b/core/res/res/raw-ru/incognito_mode_start_page.html
deleted file mode 100644
index ae7b59c..0000000
--- a/core/res/res/raw-ru/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Новое окно в режиме инкогнито</title>
-  </head>
-  <body>
-    <p><strong>Вы перешли в режим инкогнито</strong>. Страницы, которые вы просматриваете в окне в режиме инкогнито, не появятся в истории вашего браузера или истории поиска, а также не оставят на вашем компьютере других следов, таких как файлы cookie, когда вы закроете это окно. Тем не менее, все файлы, которые вы загружаете, или закладки, которые вы создаете, останутся в целости и сохранности. </p>
-
-    <p><strong>Переход в режим инкогнито не влияет на поведение других пользователей, серверов или программ. Опасайтесь:</strong></p>
-
-    <ul>
-      <li>Веб-сайтов, которые собирают информацию о вас или передают ее другим</li>
-      <li>Поставщиков услуг Интернета или их сотрудников, которые отслеживают, какие страницы вы посещаете</li>
-      <li>Вредоносного ПО, которое отслеживает нажатие клавиш клавиатуры в обмен на бесплатные смайлики</li>
-      <li>Слежки тайными агентами</li>
-      <li>Людей, которые стоят у вас за спиной</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-sk-xlarge/incognito_mode_start_page.html b/core/res/res/raw-sk-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 5b138f1..0000000
--- a/core/res/res/raw-sk-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nové okno inkognito</title>
-  </head>
-  <body>
-    <p><strong>Ste v režime inkognito</strong> Stránky, ktoré si pozriete v tomto okne, sa nezobrazia v histórii prehliadača ani v histórii vyhľadávania. Po zavretí okna inkognito na zariadení nezostanú ani žiadne iné stopy, ako sú napr. súbory cookie. Napriek tomu však zostanú zachované všetky prevzaté súbory aj záložky, ktoré ste vytvorili.</p>
-
-    <p><strong>Režim inkognito neovplyvňuje správanie iných ľudí, serverov ani softvéru. Dávajte si pozor na:</strong></p>
-
-    <ul>
-      <li>webové stránky, ktoré zbierajú alebo zdieľajú vaše informácie;</li>
-      <li>poskytovateľov internetových služieb alebo zamestnancov, ktorí sledujú vaše navštívené stránky;</li>
-      <li>škodlivý softvér, ktorý sleduje ktoré klávesy stláčate výmenou za smajlíkov zadarmo;</li>
-      <li>sledovanie tajnými agentmi;</li>
-      <li>ľudí, ktorí stoja za vami.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-sk/incognito_mode_start_page.html b/core/res/res/raw-sk/incognito_mode_start_page.html
deleted file mode 100644
index 5b138f1..0000000
--- a/core/res/res/raw-sk/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nové okno inkognito</title>
-  </head>
-  <body>
-    <p><strong>Ste v režime inkognito</strong> Stránky, ktoré si pozriete v tomto okne, sa nezobrazia v histórii prehliadača ani v histórii vyhľadávania. Po zavretí okna inkognito na zariadení nezostanú ani žiadne iné stopy, ako sú napr. súbory cookie. Napriek tomu však zostanú zachované všetky prevzaté súbory aj záložky, ktoré ste vytvorili.</p>
-
-    <p><strong>Režim inkognito neovplyvňuje správanie iných ľudí, serverov ani softvéru. Dávajte si pozor na:</strong></p>
-
-    <ul>
-      <li>webové stránky, ktoré zbierajú alebo zdieľajú vaše informácie;</li>
-      <li>poskytovateľov internetových služieb alebo zamestnancov, ktorí sledujú vaše navštívené stránky;</li>
-      <li>škodlivý softvér, ktorý sleduje ktoré klávesy stláčate výmenou za smajlíkov zadarmo;</li>
-      <li>sledovanie tajnými agentmi;</li>
-      <li>ľudí, ktorí stoja za vami.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-sl-xlarge/incognito_mode_start_page.html b/core/res/res/raw-sl-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 33a8b08..0000000
--- a/core/res/res/raw-sl-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Novo okno brez beleženja zgodovine</title>
-  </head>
-  <body>
-    <p><strong>Ste v načinu brez beleženja zgodovine.</strong> Strani, ki si jih ogledate v tem oknu, ne bodo prikazane v zgodovini brskalnika ali zgodovini iskanja, prav tako v vaši napravi ne bodo pustile sledi, kot npr. piškotkov, ko zaprete stran, ki jo imate odprto v tem načinu. Datoteke, ki jih prenesete ali zaznamki, ki jih ustvarite, bodo ohranjeni.</p>
-
-    <p><strong>Funkcije brez beleženja zgodovine ne vplivajo na obnašanje drugih oseb, strežnikov ali programske opreme. Pazite na:</strong></p>
-
-    <ul>
-      <li>Spletna mesta, ki zbirajo informacije o vas ali jih dajejo v skupno rabo.</li>
-      <li>Ponudnike internetnih storitev ali zaposlene, ki spremljajo spletna mesta, ki ste jih obiskali.</li>
-      <li>Zlonamerno programsko opremo, ki spremlja vaše tipkanje, v zameno pa vam ponuja brezplačne čustvene simbole.</li>
-      <li>Nadzor tajnih agentov.</li>
-      <li>Osebe, ki stojijo za vašim hrbtom.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-sl/incognito_mode_start_page.html b/core/res/res/raw-sl/incognito_mode_start_page.html
deleted file mode 100644
index 33a8b08..0000000
--- a/core/res/res/raw-sl/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Novo okno brez beleženja zgodovine</title>
-  </head>
-  <body>
-    <p><strong>Ste v načinu brez beleženja zgodovine.</strong> Strani, ki si jih ogledate v tem oknu, ne bodo prikazane v zgodovini brskalnika ali zgodovini iskanja, prav tako v vaši napravi ne bodo pustile sledi, kot npr. piškotkov, ko zaprete stran, ki jo imate odprto v tem načinu. Datoteke, ki jih prenesete ali zaznamki, ki jih ustvarite, bodo ohranjeni.</p>
-
-    <p><strong>Funkcije brez beleženja zgodovine ne vplivajo na obnašanje drugih oseb, strežnikov ali programske opreme. Pazite na:</strong></p>
-
-    <ul>
-      <li>Spletna mesta, ki zbirajo informacije o vas ali jih dajejo v skupno rabo.</li>
-      <li>Ponudnike internetnih storitev ali zaposlene, ki spremljajo spletna mesta, ki ste jih obiskali.</li>
-      <li>Zlonamerno programsko opremo, ki spremlja vaše tipkanje, v zameno pa vam ponuja brezplačne čustvene simbole.</li>
-      <li>Nadzor tajnih agentov.</li>
-      <li>Osebe, ki stojijo za vašim hrbtom.</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-sr-xlarge/incognito_mode_start_page.html b/core/res/res/raw-sr-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index b1fbcb1..0000000
--- a/core/res/res/raw-sr-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Нов прозор без архивирања</title>
-  </head>
-  <body>
-    <p><strong>Ушли сте у режим без архивирања</strong> Странице које гледате у овом прозору се неће појавити у историји прегледања ни историји претраге, нити ће оставити друге трагове, попут колачића, на вашем уређају када затворите овај прозор. Међутим, ако преузмете датотеке или направите обележиваче, они ће бити сачувани.</p>
-
-    <p><strong>Режим без архивирања не утиче на понашање других људи, сервера нити софтвера. Чувајте се:</strong></p>
-
-    <ul>
-      <li>Веб сајтова који прикупљају и деле податке о вама</li>
-      <li>Добављача интернет услуга или запослених који прате странице које посетите</li>
-      <li>Злонамерног софтвера који прати шта куцате</li>
-      <li>Надзора тајних агената</li>
-      <li>Људи који вам стоје иза леђа</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-sr/incognito_mode_start_page.html b/core/res/res/raw-sr/incognito_mode_start_page.html
deleted file mode 100644
index b1fbcb1..0000000
--- a/core/res/res/raw-sr/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Нов прозор без архивирања</title>
-  </head>
-  <body>
-    <p><strong>Ушли сте у режим без архивирања</strong> Странице које гледате у овом прозору се неће појавити у историји прегледања ни историји претраге, нити ће оставити друге трагове, попут колачића, на вашем уређају када затворите овај прозор. Међутим, ако преузмете датотеке или направите обележиваче, они ће бити сачувани.</p>
-
-    <p><strong>Режим без архивирања не утиче на понашање других људи, сервера нити софтвера. Чувајте се:</strong></p>
-
-    <ul>
-      <li>Веб сајтова који прикупљају и деле податке о вама</li>
-      <li>Добављача интернет услуга или запослених који прате странице које посетите</li>
-      <li>Злонамерног софтвера који прати шта куцате</li>
-      <li>Надзора тајних агената</li>
-      <li>Људи који вам стоје иза леђа</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-sv-xlarge/incognito_mode_start_page.html b/core/res/res/raw-sv-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 5ebbb22..0000000
--- a/core/res/res/raw-sv-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nytt inkognitofönster</title>
-  </head>
-  <body>
-    <p><strong>Du använder inkognitoläget</strong>. Sidor som du har läst i detta fönster visas inte i din webbläsarhistorik eller sökhistorik. De lämnar inga spår efter sig, till exempel cookies, i din enhet efter att du stängt inkognitofönstret. Alla filer som du hämtar eller bokmärken du skapar sparas dock.</p>
-
-    <p><strong>Att använda datorn inkognito påverkar inte användare, servrar eller program. Se upp för:</strong></p>
-
-    <ul>
-      <li>Webbplatser som samlar eller delar information om dig</li>
-      <li>Internetleverantörer eller arbetsgivare som spårar var du surfar</li>
-      <li>Skadlig programvara som spårar dina tangenttryckningar som nyckelloggare</li>
-      <li>Övervakning av hemliga agenter</li>
-      <li>Personer som står bakom dig</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-sv/incognito_mode_start_page.html b/core/res/res/raw-sv/incognito_mode_start_page.html
deleted file mode 100644
index 5ebbb22..0000000
--- a/core/res/res/raw-sv/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Nytt inkognitofönster</title>
-  </head>
-  <body>
-    <p><strong>Du använder inkognitoläget</strong>. Sidor som du har läst i detta fönster visas inte i din webbläsarhistorik eller sökhistorik. De lämnar inga spår efter sig, till exempel cookies, i din enhet efter att du stängt inkognitofönstret. Alla filer som du hämtar eller bokmärken du skapar sparas dock.</p>
-
-    <p><strong>Att använda datorn inkognito påverkar inte användare, servrar eller program. Se upp för:</strong></p>
-
-    <ul>
-      <li>Webbplatser som samlar eller delar information om dig</li>
-      <li>Internetleverantörer eller arbetsgivare som spårar var du surfar</li>
-      <li>Skadlig programvara som spårar dina tangenttryckningar som nyckelloggare</li>
-      <li>Övervakning av hemliga agenter</li>
-      <li>Personer som står bakom dig</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-th-xlarge/incognito_mode_start_page.html b/core/res/res/raw-th-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 29c64eb..0000000
--- a/core/res/res/raw-th-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>หน้าต่างใหม่ที่ไม่ระบุตัวตน</title>
-  </head>
-  <body>
-    <p><strong>คุณได้เข้าสู่โหมดไม่ระบุตัวตนแล้ว</strong> หน้าเว็บที่คุณดูในหน้าต่างนี้จะไม่ปรากฏในประวัติของเบราว์เซอร์หรือประวัติการค้นหาของคุณ และจะไม่ทิ้งร่องรอยอื่นๆ เช่น คุกกี้ ไว้บนอุปกรณ์หลังจากที่คุณปิดหน้าต่างที่ไม่ระบุตัวตนนี้แล้ว อย่างไรก็ตาม ไฟล์ที่คุณดาวน์โหลดหรือบุ๊กมาร์กที่สร้างขึ้นจะถูกเก็บไว้</p>
-
-    <p><strong>การเข้าสู่โหมดไม่ระบุตัวตนจะไม่กระทบต่อการทำงานของบุคคล เซิร์ฟเวอร์ หรือซอฟต์แวร์อื่น โปรดระวัง:</strong></p>
-
-    <ul>
-      <li>เว็บไซต์ที่เก็บหรือแบ่งปันข้อมูลเกี่ยวกับคุณ</li>
-      <li>ผู้ให้บริการอินเทอร์เน็ตหรือนายจ้างที่ติดตามหน้าเว็บที่คุณเข้าชม</li>
-      <li>ซอฟต์แวร์มุ่งร้ายที่ติดตามการกดแป้นพิมพ์โดยมากับของฟรี</li>
-      <li>การตรวจสอบของหน่วยสืบราชการลับ</li>
-      <li>บุคคลที่ยืนอยู่ข้างหลังคุณ</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-th/incognito_mode_start_page.html b/core/res/res/raw-th/incognito_mode_start_page.html
deleted file mode 100644
index 29c64eb..0000000
--- a/core/res/res/raw-th/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>หน้าต่างใหม่ที่ไม่ระบุตัวตน</title>
-  </head>
-  <body>
-    <p><strong>คุณได้เข้าสู่โหมดไม่ระบุตัวตนแล้ว</strong> หน้าเว็บที่คุณดูในหน้าต่างนี้จะไม่ปรากฏในประวัติของเบราว์เซอร์หรือประวัติการค้นหาของคุณ และจะไม่ทิ้งร่องรอยอื่นๆ เช่น คุกกี้ ไว้บนอุปกรณ์หลังจากที่คุณปิดหน้าต่างที่ไม่ระบุตัวตนนี้แล้ว อย่างไรก็ตาม ไฟล์ที่คุณดาวน์โหลดหรือบุ๊กมาร์กที่สร้างขึ้นจะถูกเก็บไว้</p>
-
-    <p><strong>การเข้าสู่โหมดไม่ระบุตัวตนจะไม่กระทบต่อการทำงานของบุคคล เซิร์ฟเวอร์ หรือซอฟต์แวร์อื่น โปรดระวัง:</strong></p>
-
-    <ul>
-      <li>เว็บไซต์ที่เก็บหรือแบ่งปันข้อมูลเกี่ยวกับคุณ</li>
-      <li>ผู้ให้บริการอินเทอร์เน็ตหรือนายจ้างที่ติดตามหน้าเว็บที่คุณเข้าชม</li>
-      <li>ซอฟต์แวร์มุ่งร้ายที่ติดตามการกดแป้นพิมพ์โดยมากับของฟรี</li>
-      <li>การตรวจสอบของหน่วยสืบราชการลับ</li>
-      <li>บุคคลที่ยืนอยู่ข้างหลังคุณ</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-tl-xlarge/incognito_mode_start_page.html b/core/res/res/raw-tl-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 6ce5853..0000000
--- a/core/res/res/raw-tl-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Bagong incognito window</title>
-  </head>
-  <body>
-    <p><strong>Nag-incognito ka</strong>. Hindi lalabas sa iyong kasaysayan ng pag-browse at kasaysayan ng paghahanap ang mga pahinang tiningnan mo sa window na ito, at hindi sila mag-iiwan ng ibang bakas, gaya ng cookies, sa iyong device matapos mong isara ang window na incognito. Gayunpaman, pananatiliin ang anumang mga file na iyong na-download o ang iyong mga ginawang bookmark.</p>
-
-    <p><strong>Hindi nakakaapekto ang pagiging incognito sa gawi ng ibang mga tao, server, o software. Maging maingat sa:</strong></p>
-
-    <ul>
-      <li>Mga website na kumokolekta o nagbabahagi ng impormasyong tungkol sa iyo</li>
-      <li>Mga internet service provider o mga employer na  sinusubaybayan ang mga pahinang binibisita mo</li>
-      <li>Malicious software na sinusubaybayan ang iyong mga keystroke kapalit ng mga libreng smiley</li>
-      <li>Pagmamasid ng mga secret agent</li>
-      <li>Mga tao na nakatayo sa likuran mo</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-tl/incognito_mode_start_page.html b/core/res/res/raw-tl/incognito_mode_start_page.html
deleted file mode 100644
index 6ce5853..0000000
--- a/core/res/res/raw-tl/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Bagong incognito window</title>
-  </head>
-  <body>
-    <p><strong>Nag-incognito ka</strong>. Hindi lalabas sa iyong kasaysayan ng pag-browse at kasaysayan ng paghahanap ang mga pahinang tiningnan mo sa window na ito, at hindi sila mag-iiwan ng ibang bakas, gaya ng cookies, sa iyong device matapos mong isara ang window na incognito. Gayunpaman, pananatiliin ang anumang mga file na iyong na-download o ang iyong mga ginawang bookmark.</p>
-
-    <p><strong>Hindi nakakaapekto ang pagiging incognito sa gawi ng ibang mga tao, server, o software. Maging maingat sa:</strong></p>
-
-    <ul>
-      <li>Mga website na kumokolekta o nagbabahagi ng impormasyong tungkol sa iyo</li>
-      <li>Mga internet service provider o mga employer na  sinusubaybayan ang mga pahinang binibisita mo</li>
-      <li>Malicious software na sinusubaybayan ang iyong mga keystroke kapalit ng mga libreng smiley</li>
-      <li>Pagmamasid ng mga secret agent</li>
-      <li>Mga tao na nakatayo sa likuran mo</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-tr-xlarge/incognito_mode_start_page.html b/core/res/res/raw-tr-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index c570cc7..0000000
--- a/core/res/res/raw-tr-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Yeni gizli pencere</title>
-  </head>
-  <body>
-    <p><strong>Gizli moda geçtiniz</strong> Bu pencerede görüntülediğiniz sayfalar tarayıcı geçmişinizde veya arama geçmişinizde görünmez ve gizli pencereyi kapatmanızın ardından cihazınızda çerezler gibi izler bırakmaz. Ancak, indirdiğiniz dosyalar ve oluşturduğunuz favoriler korunur.</p>
-
-    <p><strong>Gizli moda geçmeniz diğer kişilerin, sunucuların veya yazılımların davranışlarını etkilemez. Şu konularda dikkatli olun:</strong></p>
-
-    <ul>
-      <li>Hakkınızda bilgi toplayan veya paylaşan web siteleri</li>
-      <li>Ziyaret ettiğiniz sayfaları izleyen şirket çalışanları veya servis sağlayıcıları</li>
-      <li>Ücretsiz ifade simgeleri karşılığında tuş vuruşlarınızı takip eden kötü niyetli yazılımlar</li>
-      <li>Gizli ajanlar tarafından takip edilme</li>
-      <li>Arkanızda dikilip ne yaptığınıza bakan kişiler</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-tr/incognito_mode_start_page.html b/core/res/res/raw-tr/incognito_mode_start_page.html
deleted file mode 100644
index c570cc7..0000000
--- a/core/res/res/raw-tr/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Yeni gizli pencere</title>
-  </head>
-  <body>
-    <p><strong>Gizli moda geçtiniz</strong> Bu pencerede görüntülediğiniz sayfalar tarayıcı geçmişinizde veya arama geçmişinizde görünmez ve gizli pencereyi kapatmanızın ardından cihazınızda çerezler gibi izler bırakmaz. Ancak, indirdiğiniz dosyalar ve oluşturduğunuz favoriler korunur.</p>
-
-    <p><strong>Gizli moda geçmeniz diğer kişilerin, sunucuların veya yazılımların davranışlarını etkilemez. Şu konularda dikkatli olun:</strong></p>
-
-    <ul>
-      <li>Hakkınızda bilgi toplayan veya paylaşan web siteleri</li>
-      <li>Ziyaret ettiğiniz sayfaları izleyen şirket çalışanları veya servis sağlayıcıları</li>
-      <li>Ücretsiz ifade simgeleri karşılığında tuş vuruşlarınızı takip eden kötü niyetli yazılımlar</li>
-      <li>Gizli ajanlar tarafından takip edilme</li>
-      <li>Arkanızda dikilip ne yaptığınıza bakan kişiler</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-uk-xlarge/incognito_mode_start_page.html b/core/res/res/raw-uk-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 8852854..0000000
--- a/core/res/res/raw-uk-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Нове анонімне вікно</title>
-  </head>
-  <body>
-    <p><strong>Ви в анонімному режимі</strong>. Сторінки, які ви переглядаєте в цьому вікні, не з’являться в історії веб-переглядача чи в історії пошуку. Вони також не залишать жодних слідів, як-от файлів cookie, у вашому пристрої після того, як ви закриєте анонімне вікно. Однак, завантажені файли та збережені закладки залишаться.</p>
-
-    <p><strong>Анонімний режим не впливає на поведінку інших людей, серверів чи програмного забезпечення. Остерігайтеся:</strong></p>
-
-    <ul>
-      <li>веб-сайтів, які збирають чи поширюють інформацію про вас</li>
-      <li>постачальників інтернет-послуг або роботодавців, які відстежують сторінки, які ви відвідуєте</li>
-      <li>зловмисного програмного забезпечення, яке пропонує безкоштовні смайли, натомість реєструючи клавіші, які ви натискаєте</li>
-      <li>нагляду збоку секретних служб</li>
-      <li>людей за вашою спиною</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-uk/incognito_mode_start_page.html b/core/res/res/raw-uk/incognito_mode_start_page.html
deleted file mode 100644
index 8852854..0000000
--- a/core/res/res/raw-uk/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Нове анонімне вікно</title>
-  </head>
-  <body>
-    <p><strong>Ви в анонімному режимі</strong>. Сторінки, які ви переглядаєте в цьому вікні, не з’являться в історії веб-переглядача чи в історії пошуку. Вони також не залишать жодних слідів, як-от файлів cookie, у вашому пристрої після того, як ви закриєте анонімне вікно. Однак, завантажені файли та збережені закладки залишаться.</p>
-
-    <p><strong>Анонімний режим не впливає на поведінку інших людей, серверів чи програмного забезпечення. Остерігайтеся:</strong></p>
-
-    <ul>
-      <li>веб-сайтів, які збирають чи поширюють інформацію про вас</li>
-      <li>постачальників інтернет-послуг або роботодавців, які відстежують сторінки, які ви відвідуєте</li>
-      <li>зловмисного програмного забезпечення, яке пропонує безкоштовні смайли, натомість реєструючи клавіші, які ви натискаєте</li>
-      <li>нагляду збоку секретних служб</li>
-      <li>людей за вашою спиною</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-vi-xlarge/incognito_mode_start_page.html b/core/res/res/raw-vi-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 9902cde..0000000
--- a/core/res/res/raw-vi-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Cửa sổ ẩn danh mới</title>
-  </head>
-  <body>
-    <p><strong>Bạn đã sử dụng cửa sổ ẩn danh</strong>. Các trang bạn xem trong cửa sổ này sẽ không xuất hiện trong lịch sử của trình duyệt hoặc lịch sử tìm kiếm và chúng cũng không để lại dấu vết như cookie trên thiết bị sau khi bạn đóng cửa sổ ẩn danh. Tuy nhiên, mọi tệp bạn tải xuống hoặc mọi dấu trang bạn tạo sẽ được giữ nguyên.</p>
-
-    <p><strong>Sử dụng cửa sổ ẩn danh không ảnh hưởng đến hành vi của người khác, của máy chủ hoặc phần mềm. Hãy cảnh giác với:</strong></p>
-
-    <ul>
-      <li>Các trang web thu thập hoặc chia sẻ thông tin về bạn</li>
-      <li>Nhà cung cấp dịch vụ Internet hoặc ông chủ muốn theo dõi những trang bạn đã truy cập</li>
-      <li>Phần mềm độc hại theo dõi thao tác gõ phím khi nhập các mặt cười</li>
-      <li>Sự theo dõi của các cơ quan tình báo</li>
-      <li>Những người đứng đằng sau bạn</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-vi/incognito_mode_start_page.html b/core/res/res/raw-vi/incognito_mode_start_page.html
deleted file mode 100644
index 9902cde..0000000
--- a/core/res/res/raw-vi/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>Cửa sổ ẩn danh mới</title>
-  </head>
-  <body>
-    <p><strong>Bạn đã sử dụng cửa sổ ẩn danh</strong>. Các trang bạn xem trong cửa sổ này sẽ không xuất hiện trong lịch sử của trình duyệt hoặc lịch sử tìm kiếm và chúng cũng không để lại dấu vết như cookie trên thiết bị sau khi bạn đóng cửa sổ ẩn danh. Tuy nhiên, mọi tệp bạn tải xuống hoặc mọi dấu trang bạn tạo sẽ được giữ nguyên.</p>
-
-    <p><strong>Sử dụng cửa sổ ẩn danh không ảnh hưởng đến hành vi của người khác, của máy chủ hoặc phần mềm. Hãy cảnh giác với:</strong></p>
-
-    <ul>
-      <li>Các trang web thu thập hoặc chia sẻ thông tin về bạn</li>
-      <li>Nhà cung cấp dịch vụ Internet hoặc ông chủ muốn theo dõi những trang bạn đã truy cập</li>
-      <li>Phần mềm độc hại theo dõi thao tác gõ phím khi nhập các mặt cười</li>
-      <li>Sự theo dõi của các cơ quan tình báo</li>
-      <li>Những người đứng đằng sau bạn</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-xlarge/incognito_mode_start_page.html b/core/res/res/raw-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 492658d..0000000
--- a/core/res/res/raw-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,24 +0,0 @@
-<html>
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
-    <title>New incognito tab</title>
-  </head>
-  <body>
-    <p><strong>You've gone incognito</strong>. Pages you view in this tab
-      won't appear in your browser history or search history, and they won't
-      leave other traces, like cookies, on your device after you close the
-      incognito tab. Any files you download or bookmarks you create will be
-      preserved, however.</p>
-
-    <p><strong>Going incognito doesn't affect the behavior of other people,
-      servers, or software. Be wary of:</strong></p>
-
-    <ul>
-      <li>Websites that collect or share information about you</li>
-      <li>Internet service providers or employers that track the pages you visit</li>
-      <li>Malicious software that tracks your keystrokes in exchange for free smileys</li>
-      <li>Surveillance by secret agents</li>
-      <li>People standing behind you</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-zh-rCN-xlarge/incognito_mode_start_page.html b/core/res/res/raw-zh-rCN-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 2caf8f8b..0000000
--- a/core/res/res/raw-zh-rCN-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>新建隐身窗口</title>
-  </head>
-  <body>
-    <p><strong>您已进入隐身模式</strong>。当关闭隐身窗口后,您在此窗口中查看的网页将不会出现在您的浏览器历史记录或搜索记录中,也不会在您的设备留下任何踪迹(如 cookie)。但是,您下载的任何文件或您创建的书签会予以保留。</p>
-
-    <p><strong>进入隐身模式不会影响他人、其他服务器或软件的行为。敬请提防:</strong></p>
-
-    <ul>
-      <li>搜集并分享有关您的信息的网站</li>
-      <li>跟踪您所访问的网页的互联网服务提供商或雇主</li>
-      <li>跟踪您的键盘输入内容以换取免费表情符号的恶意软件</li>
-      <li>对您进行监视的秘密代理</li>
-      <li>站在您身后的人</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-zh-rCN/incognito_mode_start_page.html b/core/res/res/raw-zh-rCN/incognito_mode_start_page.html
deleted file mode 100644
index 2caf8f8b..0000000
--- a/core/res/res/raw-zh-rCN/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>新建隐身窗口</title>
-  </head>
-  <body>
-    <p><strong>您已进入隐身模式</strong>。当关闭隐身窗口后,您在此窗口中查看的网页将不会出现在您的浏览器历史记录或搜索记录中,也不会在您的设备留下任何踪迹(如 cookie)。但是,您下载的任何文件或您创建的书签会予以保留。</p>
-
-    <p><strong>进入隐身模式不会影响他人、其他服务器或软件的行为。敬请提防:</strong></p>
-
-    <ul>
-      <li>搜集并分享有关您的信息的网站</li>
-      <li>跟踪您所访问的网页的互联网服务提供商或雇主</li>
-      <li>跟踪您的键盘输入内容以换取免费表情符号的恶意软件</li>
-      <li>对您进行监视的秘密代理</li>
-      <li>站在您身后的人</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-zh-rTW-xlarge/incognito_mode_start_page.html b/core/res/res/raw-zh-rTW-xlarge/incognito_mode_start_page.html
deleted file mode 100644
index 54eb40b..0000000
--- a/core/res/res/raw-zh-rTW-xlarge/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>新的無痕式視窗</title>
-  </head>
-  <body>
-    <p><strong>您已開啟無痕式視窗</strong>。透過這個視窗開啟的網頁都不會出現在瀏覽器記錄和搜尋記錄中,而且您關閉此類視窗之後,裝置上並不會保留任何相關記錄 (例如 cookie)。不過系統會保存您的下載檔案和書籤。</p>
-
-    <p><strong>無痕式視窗無法掌控人、伺服器和軟體的行為,所以請小心下列的人事物:</strong></p>
-
-    <ul>
-      <li>會收集或分享您的相關資訊的網站</li>
-      <li>會追蹤您造訪網頁的網路服務供應商或雇主</li>
-      <li>以免費下載為誘因引誘您點擊的連結,其中通常隱藏鍵盤記錄惡意軟體</li>
-      <li>情報特務的監控</li>
-      <li>站在您身後的人</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw-zh-rTW/incognito_mode_start_page.html b/core/res/res/raw-zh-rTW/incognito_mode_start_page.html
deleted file mode 100644
index 54eb40b..0000000
--- a/core/res/res/raw-zh-rTW/incognito_mode_start_page.html
+++ /dev/null
@@ -1,19 +0,0 @@
-<html DIR="LTR">
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
-    <title>新的無痕式視窗</title>
-  </head>
-  <body>
-    <p><strong>您已開啟無痕式視窗</strong>。透過這個視窗開啟的網頁都不會出現在瀏覽器記錄和搜尋記錄中,而且您關閉此類視窗之後,裝置上並不會保留任何相關記錄 (例如 cookie)。不過系統會保存您的下載檔案和書籤。</p>
-
-    <p><strong>無痕式視窗無法掌控人、伺服器和軟體的行為,所以請小心下列的人事物:</strong></p>
-
-    <ul>
-      <li>會收集或分享您的相關資訊的網站</li>
-      <li>會追蹤您造訪網頁的網路服務供應商或雇主</li>
-      <li>以免費下載為誘因引誘您點擊的連結,其中通常隱藏鍵盤記錄惡意軟體</li>
-      <li>情報特務的監控</li>
-      <li>站在您身後的人</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/raw/incognito_mode_start_page.html b/core/res/res/raw/incognito_mode_start_page.html
deleted file mode 100644
index 5d7a3fb..0000000
--- a/core/res/res/raw/incognito_mode_start_page.html
+++ /dev/null
@@ -1,24 +0,0 @@
-<html>
-  <head>
-    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
-    <title>New incognito window</title>
-  </head>
-  <body>
-    <p><strong>You've gone incognito</strong>. Pages you view in this window
-      won't appear in your browser history or search history, and they won't
-      leave other traces, like cookies, on your device after you close the
-      incognito window. Any files you download or bookmarks you create will be
-      preserved, however.</p>
-
-    <p><strong>Going incognito doesn't affect the behavior of other people,
-      servers, or software. Be wary of:</strong></p>
-
-    <ul>
-      <li>Websites that collect or share information about you</li>
-      <li>Internet service providers or employers that track the pages you visit</li>
-      <li>Malicious software that tracks your keystrokes in exchange for free smileys</li>
-      <li>Surveillance by secret agents</li>
-      <li>People standing behind you</li>
-    </ul>
-  </body>
-</html>
diff --git a/core/res/res/values-land/integers.xml b/core/res/res/values-land/integers.xml
index 020fd23..a578989 100644
--- a/core/res/res/values-land/integers.xml
+++ b/core/res/res/values-land/integers.xml
@@ -23,4 +23,6 @@
     <integer name="kg_widget_region_weight">45</integer>
     <integer name="kg_security_flipper_weight">55</integer>
     <integer name="kg_glowpad_rotation_offset">-90</integer>
+
+    <integer name="date_picker_header_max_lines_material">4</integer>
 </resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-mcc001/config.xml
similarity index 74%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-mcc001/config.xml
index 529527b..93cde03 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-mcc001/config.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,7 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
-
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+-->
+<resources>
+    <bool name="config_use_sim_language_file">true</bool>
+</resources>
diff --git a/core/res/res/values-mcc222-mnc10/config.xml b/core/res/res/values-mcc222-mnc10/config.xml
index cd6e8c6..c819de2 100644
--- a/core/res/res/values-mcc222-mnc10/config.xml
+++ b/core/res/res/values-mcc222-mnc10/config.xml
@@ -28,29 +28,4 @@
     <string-array translatable="false" name="config_tether_apndata">
       <item>Tethering Internet,web.omnitel.it,,,,,,,,,222,10,,DUN</item>
     </string-array>
-
-    <string-array translatable="false" name="config_operatorConsideredNonRoaming">
-        <item>21401</item>
-        <item>21402</item>
-        <item>21403</item>
-        <item>21404</item>
-        <item>21405</item>
-        <item>21406</item>
-        <item>21407</item>
-        <item>21408</item>
-        <item>21409</item>
-        <item>21410</item>
-        <item>21411</item>
-        <item>21412</item>
-        <item>21413</item>
-        <item>21414</item>
-        <item>21415</item>
-        <item>21416</item>
-        <item>21417</item>
-        <item>21418</item>
-        <item>21419</item>
-        <item>21420</item>
-        <item>21421</item>
-    </string-array>
-
 </resources>
diff --git a/core/res/res/values-mcc232-mnc10/config.xml b/core/res/res/values-mcc232-mnc10/config.xml
new file mode 100644
index 0000000..bdf83016
--- /dev/null
+++ b/core/res/res/values-mcc232-mnc10/config.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+ ** Copyright 2016, The Android Open Source Project
+ **
+ ** Licensed under the Apache License, Version 2.0 (the "License");
+ ** you may not use this file except in compliance with the License.
+ ** You may obtain a copy of the License at
+ **
+ **     http://www.apache.org/licenses/LICENSE-2.0
+ **
+ ** Unless required by applicable law or agreed to in writing, software
+ ** distributed under the License is distributed on an "AS IS" BASIS,
+ ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ** See the License for the specific language governing permissions and
+ ** limitations under the License.
+ */
+-->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Don't use roaming icon for considered operators -->
+    <string-array translatable="false" name="config_operatorConsideredNonRoaming">
+        <item>23203</item>
+        <item>23205</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-mcc232-mnc13/config.xml b/core/res/res/values-mcc232-mnc13/config.xml
new file mode 100644
index 0000000..2c14f87
--- /dev/null
+++ b/core/res/res/values-mcc232-mnc13/config.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+ ** Copyright 2016, The Android Open Source Project
+ **
+ ** Licensed under the Apache License, Version 2.0 (the "License");
+ ** you may not use this file except in compliance with the License.
+ ** You may obtain a copy of the License at
+ **
+ **     http://www.apache.org/licenses/LICENSE-2.0
+ **
+ ** Unless required by applicable law or agreed to in writing, software
+ ** distributed under the License is distributed on an "AS IS" BASIS,
+ ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ** See the License for the specific language governing permissions and
+ ** limitations under the License.
+ */
+-->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Don't use roaming icon for considered operators -->
+    <string-array translatable="false" name="config_operatorConsideredNonRoaming">
+        <item>23203</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-mcc302-mnc220/config.xml b/core/res/res/values-mcc302-mnc220/config.xml
index 454e4b6..d638b89 100644
--- a/core/res/res/values-mcc302-mnc220/config.xml
+++ b/core/res/res/values-mcc302-mnc220/config.xml
@@ -42,4 +42,23 @@
         <item>[ApnSettingV3]Tethered Public Mobile,isp.mb.com,,,,,,,,,302,220,,DUN,,,true,0,,,,,,,gid,4D4F</item>
     </string-array>
 
+    <!-- Values for GPS configuration (Telus) -->
+    <string-array translatable="false" name="config_gpsParameters">
+        <item>SUPL_HOST=supl.telusmobility.com</item>
+        <item>SUPL_PORT=7275</item>
+        <item>XTRA_SERVER_1=http://xtrapath1.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_2=http://xtrapath2.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_3=http://xtrapath3.izatcloud.net/xtra3grc.bin</item>
+        <item>NTP_SERVER=north-america.pool.ntp.org</item>
+        <item>SUPL_MODE=1</item>
+        <item>SUPL_VER=0x20000</item>
+        <item>LPP_PROFILE=2</item>
+        <item>NMEA_PROVIDER=0</item>
+        <item>A_GLONASS_POS_PROTOCOL_SELECT=0</item>
+        <item>ERR_ESTIMATE=0</item>
+        <item>INTERMEDIATE_POS=0</item>
+        <item>GPS_LOCK=0</item>
+        <item>SUPL_ES=0</item>
+    </string-array>
+
 </resources>
diff --git a/core/res/res/values-mcc302-mnc221/config.xml b/core/res/res/values-mcc302-mnc221/config.xml
index ffc9d6c..1444250 100644
--- a/core/res/res/values-mcc302-mnc221/config.xml
+++ b/core/res/res/values-mcc302-mnc221/config.xml
@@ -32,4 +32,23 @@
         <item>[ApnSettingV3]Koodo,sp.koodo.com,,,,,,,,,302,221,,DUN,,,true,0,,,,,,,gid,4B</item>
     </string-array>
 
+    <!-- Values for GPS configuration (Telus) -->
+    <string-array translatable="false" name="config_gpsParameters">
+        <item>SUPL_HOST=supl.telusmobility.com</item>
+        <item>SUPL_PORT=7275</item>
+        <item>XTRA_SERVER_1=http://xtrapath1.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_2=http://xtrapath2.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_3=http://xtrapath3.izatcloud.net/xtra3grc.bin</item>
+        <item>NTP_SERVER=north-america.pool.ntp.org</item>
+        <item>SUPL_MODE=1</item>
+        <item>SUPL_VER=0x20000</item>
+        <item>LPP_PROFILE=2</item>
+        <item>NMEA_PROVIDER=0</item>
+        <item>A_GLONASS_POS_PROTOCOL_SELECT=0</item>
+        <item>ERR_ESTIMATE=0</item>
+        <item>INTERMEDIATE_POS=0</item>
+        <item>GPS_LOCK=0</item>
+        <item>SUPL_ES=0</item>
+    </string-array>
+
 </resources>
diff --git a/core/res/res/values-mcc302-mnc370/config.xml b/core/res/res/values-mcc302-mnc370/config.xml
index 5e7e8bc..05265c7 100644
--- a/core/res/res/values-mcc302-mnc370/config.xml
+++ b/core/res/res/values-mcc302-mnc370/config.xml
@@ -43,4 +43,23 @@
         <item>302780</item>
     </string-array>
 
+  <!-- Values for GPS configuration (Rogers) -->
+    <string-array translatable="false" name="config_gpsParameters">
+        <item>SUPL_HOST=supl.google.com</item>
+        <item>SUPL_PORT=7275</item>
+        <item>XTRA_SERVER_1=http://xtrapath1.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_2=http://xtrapath2.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_3=http://xtrapath3.izatcloud.net/xtra3grc.bin</item>
+        <item>NTP_SERVER=north-america.pool.ntp.org</item>
+        <item>SUPL_MODE=1</item>
+        <item>SUPL_VER=0x20000</item>
+        <item>LPP_PROFILE=2</item>
+        <item>NMEA_PROVIDER=0</item>
+        <item>A_GLONASS_POS_PROTOCOL_SELECT=0</item>
+        <item>ERR_ESTIMATE=0</item>
+        <item>INTERMEDIATE_POS=0</item>
+        <item>GPS_LOCK=0</item>
+        <item>SUPL_ES=0</item>
+    </string-array>
+
 </resources>
diff --git a/core/res/res/values-mcc302-mnc500/config.xml b/core/res/res/values-mcc302-mnc500/config.xml
new file mode 100644
index 0000000..77f6419
--- /dev/null
+++ b/core/res/res/values-mcc302-mnc500/config.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+ ** Copyright 2016, The Android Open Source Project
+ **
+ ** Licensed under the Apache License, Version 2.0 (the "License");
+ ** you may not use this file except in compliance with the License.
+ ** You may obtain a copy of the License at
+ **
+ **     http://www.apache.org/licenses/LICENSE-2.0
+ **
+ ** Unless required by applicable law or agreed to in writing, software
+ ** distributed under the License is distributed on an "AS IS" BASIS,
+ ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ** See the License for the specific language governing permissions and
+ ** limitations under the License.
+ */
+-->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Don't use roaming icon for considered operators -->
+    <string-array translatable="false" name="config_operatorConsideredNonRoaming">
+        <item>302</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-mcc302-mnc510/config.xml b/core/res/res/values-mcc302-mnc510/config.xml
new file mode 100644
index 0000000..77f6419
--- /dev/null
+++ b/core/res/res/values-mcc302-mnc510/config.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+ ** Copyright 2016, The Android Open Source Project
+ **
+ ** Licensed under the Apache License, Version 2.0 (the "License");
+ ** you may not use this file except in compliance with the License.
+ ** You may obtain a copy of the License at
+ **
+ **     http://www.apache.org/licenses/LICENSE-2.0
+ **
+ ** Unless required by applicable law or agreed to in writing, software
+ ** distributed under the License is distributed on an "AS IS" BASIS,
+ ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ** See the License for the specific language governing permissions and
+ ** limitations under the License.
+ */
+-->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Don't use roaming icon for considered operators -->
+    <string-array translatable="false" name="config_operatorConsideredNonRoaming">
+        <item>302</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-mcc302-mnc610/config.xml b/core/res/res/values-mcc302-mnc610/config.xml
index 81cec96..0af2c39 100644
--- a/core/res/res/values-mcc302-mnc610/config.xml
+++ b/core/res/res/values-mcc302-mnc610/config.xml
@@ -27,4 +27,22 @@
     -->
     <integer name="config_mobile_mtu">1428</integer>
 
+    <!-- Values for GPS configuration (Bell) -->
+    <string-array translatable="false" name="config_gpsParameters">
+        <item>SUPL_HOST=supl.google.com</item>
+        <item>SUPL_PORT=7275</item>
+        <item>XTRA_SERVER_1=http://xtrapath1.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_2=http://xtrapath2.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_3=http://xtrapath3.izatcloud.net/xtra3grc.bin</item>
+        <item>NTP_SERVER=north-america.pool.ntp.org</item>
+        <item>SUPL_MODE=1</item>
+        <item>SUPL_VER=0x20000</item>
+        <item>LPP_PROFILE=2</item>
+        <item>NMEA_PROVIDER=0</item>
+        <item>A_GLONASS_POS_PROTOCOL_SELECT=0</item>
+        <item>ERR_ESTIMATE=0</item>
+        <item>INTERMEDIATE_POS=0</item>
+        <item>GPS_LOCK=0</item>
+        <item>SUPL_ES=0</item>
+    </string-array>
 </resources>
diff --git a/core/res/res/values-mcc302-mnc640/config.xml b/core/res/res/values-mcc302-mnc640/config.xml
index 706570c..e005bc0 100644
--- a/core/res/res/values-mcc302-mnc640/config.xml
+++ b/core/res/res/values-mcc302-mnc640/config.xml
@@ -22,4 +22,23 @@
     <string-array translatable="false" name="config_operatorConsideredNonRoaming">
         <item>302</item>
     </string-array>
+
+    <!-- Values for GPS configuration (Bell) -->
+    <string-array translatable="false" name="config_gpsParameters">
+        <item>SUPL_HOST=supl.google.com</item>
+        <item>SUPL_PORT=7275</item>
+        <item>XTRA_SERVER_1=http://xtrapath1.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_2=http://xtrapath2.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_3=http://xtrapath3.izatcloud.net/xtra3grc.bin</item>
+        <item>NTP_SERVER=north-america.pool.ntp.org</item>
+        <item>SUPL_MODE=1</item>
+        <item>SUPL_VER=0x20000</item>
+        <item>LPP_PROFILE=2</item>
+        <item>NMEA_PROVIDER=0</item>
+        <item>A_GLONASS_POS_PROTOCOL_SELECT=0</item>
+        <item>ERR_ESTIMATE=0</item>
+        <item>INTERMEDIATE_POS=0</item>
+        <item>GPS_LOCK=0</item>
+        <item>SUPL_ES=0</item>
+    </string-array>
 </resources>
diff --git a/core/res/res/values-mcc302-mnc720/config.xml b/core/res/res/values-mcc302-mnc720/config.xml
index dcfa5c5..7a3540a 100644
--- a/core/res/res/values-mcc302-mnc720/config.xml
+++ b/core/res/res/values-mcc302-mnc720/config.xml
@@ -45,4 +45,23 @@
         <item>302780</item>
     </string-array>
 
+  <!-- Values for GPS configuration (Rogers) -->
+    <string-array translatable="false" name="config_gpsParameters">
+        <item>SUPL_HOST=supl.google.com</item>
+        <item>SUPL_PORT=7275</item>
+        <item>XTRA_SERVER_1=http://xtrapath1.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_2=http://xtrapath2.izatcloud.net/xtra3grc.bin</item>
+        <item>XTRA_SERVER_3=http://xtrapath3.izatcloud.net/xtra3grc.bin</item>
+        <item>NTP_SERVER=north-america.pool.ntp.org</item>
+        <item>SUPL_MODE=1</item>
+        <item>SUPL_VER=0x20000</item>
+        <item>LPP_PROFILE=2</item>
+        <item>NMEA_PROVIDER=0</item>
+        <item>A_GLONASS_POS_PROTOCOL_SELECT=0</item>
+        <item>ERR_ESTIMATE=0</item>
+        <item>INTERMEDIATE_POS=0</item>
+        <item>GPS_LOCK=0</item>
+        <item>SUPL_ES=0</item>
+    </string-array>
+
 </resources>
diff --git a/core/res/res/values-mcc311-mnc480/config.xml b/core/res/res/values-mcc311-mnc480/config.xml
index 8d7fd61..714b312 100755
--- a/core/res/res/values-mcc311-mnc480/config.xml
+++ b/core/res/res/values-mcc311-mnc480/config.xml
@@ -64,4 +64,5 @@
         <item>true</item>
     </string-array>
     <string translatable="false" name="prohibit_manual_network_selection_in_gobal_mode">true</string>
+
 </resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-mcc466-mnc01/config.xml
similarity index 74%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-mcc466-mnc01/config.xml
index 529527b..3c379d14 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-mcc466-mnc01/config.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,7 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
-
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+-->
+<resources>
+    <bool name="config_use_sim_language_file">false</bool>
+</resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-mcc466-mnc02/config.xml
similarity index 74%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-mcc466-mnc02/config.xml
index 529527b..3c379d14 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-mcc466-mnc02/config.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,7 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
-
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+-->
+<resources>
+    <bool name="config_use_sim_language_file">false</bool>
+</resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-mcc466-mnc03/config.xml
similarity index 74%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-mcc466-mnc03/config.xml
index 529527b..3c379d14 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-mcc466-mnc03/config.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,7 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
-
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+-->
+<resources>
+    <bool name="config_use_sim_language_file">false</bool>
+</resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-mcc466-mnc06/config.xml
similarity index 74%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-mcc466-mnc06/config.xml
index 529527b..3c379d14 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-mcc466-mnc06/config.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,7 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
-
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+-->
+<resources>
+    <bool name="config_use_sim_language_file">false</bool>
+</resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-mcc466-mnc07/config.xml
similarity index 74%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-mcc466-mnc07/config.xml
index 529527b..3c379d14 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-mcc466-mnc07/config.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,7 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
-
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+-->
+<resources>
+    <bool name="config_use_sim_language_file">false</bool>
+</resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-mcc466-mnc11/config.xml
similarity index 74%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-mcc466-mnc11/config.xml
index 529527b..3c379d14 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-mcc466-mnc11/config.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,7 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
-
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+-->
+<resources>
+    <bool name="config_use_sim_language_file">false</bool>
+</resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-mcc466-mnc92/config.xml
similarity index 74%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-mcc466-mnc92/config.xml
index 529527b..3c379d14 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-mcc466-mnc92/config.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2016, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,7 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
-
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+-->
+<resources>
+    <bool name="config_use_sim_language_file">false</bool>
+</resources>
diff --git a/core/res/res/values-mcc722-mnc36/config.xml b/core/res/res/values-mcc722-mnc36/config.xml
new file mode 100644
index 0000000..daf5373
--- /dev/null
+++ b/core/res/res/values-mcc722-mnc36/config.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+** Copyright 2016, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You my 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">
+    <!-- Don't use roaming icon for considered operators -->
+    <string-array translatable="false" name="config_operatorConsideredNonRoaming">
+        <item>72234</item>
+    </string-array>
+</resources>
diff --git a/core/res/res/values-notround-watch/config_material.xml b/core/res/res/values-notround-watch/config_material.xml
new file mode 100644
index 0000000..a99674f
--- /dev/null
+++ b/core/res/res/values-notround-watch/config_material.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- These resources are around just to allow their values to be customized
+     for different hardware and product builds, only for Material theme.  Do not translate.
+
+     NOTE: The naming convention is "config_camelCaseValue".  -->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Gravity that should be used for dialog text styles. Equivalent to: Gravity.START | Gravity.TOP -->
+    <integer name="config_dialogTextGravity">0x00800033</integer>
+</resources>
diff --git a/core/res/res/values-watch/dimens.xml b/core/res/res/values-notround-watch/dimens.xml
similarity index 94%
rename from core/res/res/values-watch/dimens.xml
rename to core/res/res/values-notround-watch/dimens.xml
index f103aa9..f0204d0 100644
--- a/core/res/res/values-watch/dimens.xml
+++ b/core/res/res/values-notround-watch/dimens.xml
@@ -24,4 +24,5 @@
     <item name="input_extract_action_margin_bottom" type="fraction">0%</item>
     <item name="input_extract_action_button_width" type="dimen">24dp</item>
     <item name="input_extract_action_button_height" type="dimen">24dp</item>
+    <item name="input_extract_action_icon_padding" type="dimen">3dp</item>
 </resources>
diff --git a/core/res/res/values-notround-watch/dimens_material.xml b/core/res/res/values-notround-watch/dimens_material.xml
new file mode 100644
index 0000000..9cacb11
--- /dev/null
+++ b/core/res/res/values-notround-watch/dimens_material.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+    <dimen name="dialog_padding_material">8dp</dimen>
+    <dimen name="preference_fragment_padding_vertical_material">0dp</dimen>
+
+    <dimen name="list_item_padding_horizontal_material">16dp</dimen>
+    <dimen name="list_item_padding_start_material">16dp</dimen>
+    <dimen name="list_item_padding_end_material">16dp</dimen>
+
+    <dimen name="dialog_list_padding_top_no_title">8dp</dimen>
+    <dimen name="dialog_list_padding_bottom_no_buttons">8dp</dimen>
+</resources>
diff --git a/core/res/res/values-round-watch/config_material.xml b/core/res/res/values-round-watch/config_material.xml
new file mode 100644
index 0000000..1179870
--- /dev/null
+++ b/core/res/res/values-round-watch/config_material.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- These resources are around just to allow their values to be customized
+     for watch products.  Do not translate. -->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Don't clip on round screens so the list can scroll past the rounded edges. -->
+    <bool name="config_preferenceFragmentClipToPadding">false</bool>
+
+    <!-- Gravity that should be used for dialog text styles. Equivalent to: Gravity.CENTER_HORIZONTAL | Gravity.TOP -->
+    <integer name="config_dialogTextGravity">0x00000031</integer>
+
+    <!-- The amount to offset when scrolling to a selection in an AlertDialog -->
+    <dimen name="config_alertDialogSelectionScrollOffset">@dimen/screen_percentage_15</dimen>
+</resources>
diff --git a/core/res/res/values-round-watch/dimens.xml b/core/res/res/values-round-watch/dimens.xml
index a12f499..1d8c669 100644
--- a/core/res/res/values-round-watch/dimens.xml
+++ b/core/res/res/values-round-watch/dimens.xml
@@ -24,4 +24,5 @@
     <item name="input_extract_action_margin_bottom" type="fraction">2.1%</item>
     <item name="input_extract_action_button_width" type="dimen">32dp</item>
     <item name="input_extract_action_button_height" type="dimen">32dp</item>
+    <item name="input_extract_action_icon_padding" type="dimen">5dp</item>
 </resources>
diff --git a/core/res/res/values-round-watch/dimens_material.xml b/core/res/res/values-round-watch/dimens_material.xml
new file mode 100644
index 0000000..f2de4e0
--- /dev/null
+++ b/core/res/res/values-round-watch/dimens_material.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+    <dimen name="dialog_padding_material">@dimen/screen_percentage_15</dimen>
+    <dimen name="preference_fragment_padding_vertical_material">@dimen/screen_percentage_15</dimen>
+
+    <dimen name="list_item_padding_horizontal_material">@dimen/screen_percentage_15</dimen>
+    <dimen name="list_item_padding_start_material">@dimen/screen_percentage_15</dimen>
+    <dimen name="list_item_padding_end_material">@dimen/screen_percentage_10</dimen>
+
+    <dimen name="dialog_list_padding_top_no_title">@dimen/screen_percentage_15</dimen>
+    <dimen name="dialog_list_padding_bottom_no_buttons">@dimen/screen_percentage_15</dimen>
+</resources>
diff --git a/core/res/res/values-w180dp-notround-watch/dimens_material.xml b/core/res/res/values-w180dp-notround-watch/dimens_material.xml
new file mode 100644
index 0000000..79acf84
--- /dev/null
+++ b/core/res/res/values-w180dp-notround-watch/dimens_material.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+    <dimen name="text_size_display_4_material">80sp</dimen>
+    <dimen name="text_size_display_3_material">50sp</dimen>
+    <dimen name="text_size_display_2_material">40sp</dimen>
+    <dimen name="text_size_display_1_material">30sp</dimen>
+    <dimen name="text_size_headline_material">20sp</dimen>
+    <dimen name="text_size_title_material">18sp</dimen>
+    <dimen name="text_size_subhead_material">18sp</dimen>
+    <dimen name="text_size_title_material_toolbar">18dp</dimen>
+    <dimen name="text_size_subtitle_material_toolbar">18dp</dimen>
+    <dimen name="text_size_menu_material">18sp</dimen>
+    <dimen name="text_size_menu_header_material">16sp</dimen>
+    <dimen name="text_size_body_2_material">16sp</dimen>
+    <dimen name="text_size_body_1_material">16sp</dimen>
+    <dimen name="text_size_caption_material">14sp</dimen>
+    <dimen name="text_size_button_material">16sp</dimen>
+
+    <dimen name="text_size_large_material">18sp</dimen>
+    <dimen name="text_size_medium_material">16sp</dimen>
+    <dimen name="text_size_small_material">14sp</dimen>
+</resources>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/values-w192dp/dimens_material.xml
similarity index 74%
rename from packages/DocumentsUI/res/values/attrs.xml
rename to core/res/res/values-w192dp/dimens_material.xml
index 9e13001..797bf5a 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/values-w192dp/dimens_material.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
      limitations under the License.
 -->
 <resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
+    <dimen name="screen_percentage_05">9.6dp</dimen>
+    <dimen name="screen_percentage_10">19.2dp</dimen>
+    <dimen name="screen_percentage_15">28.8dp</dimen>
 </resources>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/values-w205dp/dimens_material.xml
similarity index 73%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/values-w205dp/dimens_material.xml
index 9e13001..94907ee 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/values-w205dp/dimens_material.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
      limitations under the License.
 -->
 <resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
+    <dimen name="screen_percentage_05">10.25dp</dimen>
+    <dimen name="screen_percentage_10">20.5dp</dimen>
+    <dimen name="screen_percentage_15">30.75dp</dimen>
 </resources>
diff --git a/core/res/res/values-w210dp-round-watch/dimens_material.xml b/core/res/res/values-w210dp-round-watch/dimens_material.xml
new file mode 100644
index 0000000..79acf84
--- /dev/null
+++ b/core/res/res/values-w210dp-round-watch/dimens_material.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+    <dimen name="text_size_display_4_material">80sp</dimen>
+    <dimen name="text_size_display_3_material">50sp</dimen>
+    <dimen name="text_size_display_2_material">40sp</dimen>
+    <dimen name="text_size_display_1_material">30sp</dimen>
+    <dimen name="text_size_headline_material">20sp</dimen>
+    <dimen name="text_size_title_material">18sp</dimen>
+    <dimen name="text_size_subhead_material">18sp</dimen>
+    <dimen name="text_size_title_material_toolbar">18dp</dimen>
+    <dimen name="text_size_subtitle_material_toolbar">18dp</dimen>
+    <dimen name="text_size_menu_material">18sp</dimen>
+    <dimen name="text_size_menu_header_material">16sp</dimen>
+    <dimen name="text_size_body_2_material">16sp</dimen>
+    <dimen name="text_size_body_1_material">16sp</dimen>
+    <dimen name="text_size_caption_material">14sp</dimen>
+    <dimen name="text_size_button_material">16sp</dimen>
+
+    <dimen name="text_size_large_material">18sp</dimen>
+    <dimen name="text_size_medium_material">16sp</dimen>
+    <dimen name="text_size_small_material">14sp</dimen>
+</resources>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/values-w213dp/dimens_material.xml
similarity index 73%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/values-w213dp/dimens_material.xml
index 9e13001..8a4e3a0 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/values-w213dp/dimens_material.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
      limitations under the License.
 -->
 <resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
+    <dimen name="screen_percentage_05">10.65dp</dimen>
+    <dimen name="screen_percentage_10">21.3dp</dimen>
+    <dimen name="screen_percentage_15">31.95dp</dimen>
 </resources>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/values-w225dp/dimens_material.xml
similarity index 73%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/values-w225dp/dimens_material.xml
index 9e13001..aa822a3 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/values-w225dp/dimens_material.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
      limitations under the License.
 -->
 <resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
+    <dimen name="screen_percentage_05">11.25dp</dimen>
+    <dimen name="screen_percentage_10">22.5dp</dimen>
+    <dimen name="screen_percentage_15">33.75dp</dimen>
 </resources>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/values-w228dp/dimens_material.xml
similarity index 73%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/values-w228dp/dimens_material.xml
index 9e13001..a200975 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/values-w228dp/dimens_material.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
      limitations under the License.
 -->
 <resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
+    <dimen name="screen_percentage_05">11.4dp</dimen>
+    <dimen name="screen_percentage_10">22.8dp</dimen>
+    <dimen name="screen_percentage_15">34.2dp</dimen>
 </resources>
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/core/res/res/values-w240dp/dimens_material.xml
similarity index 74%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to core/res/res/values-w240dp/dimens_material.xml
index 9e13001..a4b58fa 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/core/res/res/values-w240dp/dimens_material.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
      limitations under the License.
 -->
 <resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
+    <dimen name="screen_percentage_05">12dp</dimen>
+    <dimen name="screen_percentage_10">24dp</dimen>
+    <dimen name="screen_percentage_15">36dp</dimen>
 </resources>
diff --git a/core/res/res/values-watch/colors_material.xml b/core/res/res/values-watch/colors_material.xml
new file mode 100644
index 0000000..54eece4
--- /dev/null
+++ b/core/res/res/values-watch/colors_material.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright (C) 2014 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>
+    <color name="background_material_dark">#ff232e33</color>
+    <color name="background_floating_material_dark">#ff3e5059</color>
+
+    <color name="accent_material_dark">#ff5e97f6</color>
+    <color name="accent_material_light">#ff4285f4</color>
+
+    <color name="primary_material_dark">#4D4D4D</color>
+
+    <color name="button_material_dark">#ff919699</color>
+</resources>
diff --git a/core/res/res/values-watch/config.xml b/core/res/res/values-watch/config.xml
index 919519e..7c05b7a 100644
--- a/core/res/res/values-watch/config.xml
+++ b/core/res/res/values-watch/config.xml
@@ -57,4 +57,9 @@
 
     <!-- Use a custom transition for RemoteViews. -->
     <bool name="config_overrideRemoteViewsActivityTransition">true</bool>
+
+    <!-- Default value for android:focusableInTouchMode for some framework scrolling containers.
+         ListView/GridView are notably absent since this is their default anyway.
+         Set to true for watch devices. -->
+    <bool name="config_focusScrollContainersInTouchMode">true</bool>
 </resources>
diff --git a/core/res/res/values-watch/config_material.xml b/core/res/res/values-watch/config_material.xml
new file mode 100644
index 0000000..104d122
--- /dev/null
+++ b/core/res/res/values-watch/config_material.xml
@@ -0,0 +1,32 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- These resources are around just to allow their values to be customized
+     for watch products.  Do not translate. -->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Watch type devices have limited screen real-estate, and thus action bars should not be
+         used. -->
+    <bool name="config_windowActionBarSupported">false</bool>
+
+    <!-- Watch type devices have limited screen real-estate, and thus titles should not be used. -->
+    <bool name="config_windowNoTitleDefault">true</bool>
+
+    <!-- Use micro alert controller -->
+    <integer name="config_alertDialogController">1</integer>
+
+    <!-- Always overscan by default to ensure onApplyWindowInsets will always be called. -->
+    <bool name="config_windowOverscanByDefault">true</bool>
+</resources>
diff --git a/core/res/res/values-watch/dimens_material.xml b/core/res/res/values-watch/dimens_material.xml
new file mode 100644
index 0000000..b48cde6
--- /dev/null
+++ b/core/res/res/values-watch/dimens_material.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources>
+    <dimen name="text_size_display_4_material">71sp</dimen>
+    <dimen name="text_size_display_3_material">44sp</dimen>
+    <dimen name="text_size_display_2_material">36sp</dimen>
+    <dimen name="text_size_display_1_material">27sp</dimen>
+    <dimen name="text_size_headline_material">18sp</dimen>
+    <dimen name="text_size_title_material">16sp</dimen>
+    <dimen name="text_size_subhead_material">16sp</dimen>
+    <dimen name="text_size_title_material_toolbar">16dp</dimen>
+    <dimen name="text_size_subtitle_material_toolbar">16dp</dimen>
+    <dimen name="text_size_menu_material">16sp</dimen>
+    <dimen name="text_size_menu_header_material">14sp</dimen>
+    <dimen name="text_size_body_2_material">14sp</dimen>
+    <dimen name="text_size_body_1_material">14sp</dimen>
+    <dimen name="text_size_caption_material">12sp</dimen>
+    <dimen name="text_size_button_material">14sp</dimen>
+
+    <dimen name="text_size_large_material">16sp</dimen>
+    <dimen name="text_size_medium_material">14sp</dimen>
+    <dimen name="text_size_small_material">12sp</dimen>
+
+    <item name="text_line_spacing_multiplier_material" format="float" type="dimen">1.2</item>
+
+    <!-- Date and time picker legacy dimens -->
+    <dimen name="picker_top_margin">1dip</dimen>
+    <dimen name="picker_bottom_margin">1dip</dimen>
+</resources>
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/core/res/res/values-watch/donottranslate.xml
similarity index 63%
copy from packages/SystemUI/res/layout/night_mode_settings.xml
copy to core/res/res/values-watch/donottranslate.xml
index 3725e78..d247ff6 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/core/res/res/values-watch/donottranslate.xml
@@ -14,11 +14,9 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-
-    <include layout="@layout/switch_bar" />
-
-</LinearLayout>
+<resources>
+    <!-- DO NOT TRANSLATE Spans within this text are applied to style composing regions
+    within an EditText widget. The text content is ignored and not used.
+    Note: This is @color/material_deep_teal_200, cannot use @color references here. -->
+    <string name="candidates_style" translatable="false"><font color="#80cbc4">candidates</font></string>
+ </resources>
diff --git a/core/res/res/values-watch/donottranslate_material.xml b/core/res/res/values-watch/donottranslate_material.xml
new file mode 100644
index 0000000..a6f2ff4
--- /dev/null
+++ b/core/res/res/values-watch/donottranslate_material.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<resources>
+    <string name="font_family_display_4_material">sans-serif-condensed-light</string>
+    <string name="font_family_display_3_material">sans-serif-condensed-light</string>
+    <string name="font_family_display_2_material">sans-serif-condensed-light</string>
+    <string name="font_family_display_1_material">sans-serif-condensed-light</string>
+    <string name="font_family_headline_material">sans-serif-condensed-light</string>
+    <string name="font_family_title_material">sans-serif-condensed</string>
+    <string name="font_family_subhead_material">sans-serif-condensed-light</string>
+    <string name="font_family_menu_material">sans-serif-condensed-light</string>
+    <string name="font_family_body_2_material">sans-serif-condensed</string>
+    <string name="font_family_body_1_material">sans-serif-condensed-light</string>
+    <string name="font_family_caption_material">sans-serif-condensed-light</string>
+    <string name="font_family_button_material">sans-serif-condensed</string>
+ </resources>
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/core/res/res/values-watch/integers.xml
similarity index 61%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to core/res/res/values-watch/integers.xml
index 529527b..46ed97d 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/core/res/res/values-watch/integers.xml
@@ -1,5 +1,7 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
 /*
-** Copyright 2015, The Android Open Source Project
+** Copyright 2012, The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -13,7 +15,11 @@
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */
+-->
+<resources>
+  <!-- Specifies date picker mode to be 'spinner' -->
+  <integer name="date_picker_mode_material">1</integer>
 
-package com.android.internal.app;
-
-parcelable EphemeralResolveInfo;
+  <!-- Specifies time picker mode to be 'spinner' -->
+  <integer name="time_picker_mode_material">1</integer>
+</resources>
diff --git a/core/res/res/values-watch/styles_material.xml b/core/res/res/values-watch/styles_material.xml
new file mode 100644
index 0000000..8a080d9c
--- /dev/null
+++ b/core/res/res/values-watch/styles_material.xml
@@ -0,0 +1,104 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!--
+===============================================================
+                        PLEASE READ
+===============================================================
+
+The Material themes must not be modified in order to pass CTS.
+Many related themes and styles depend on other values defined in this file.
+If you would like to provide custom themes and styles for your device,
+please see styles_device_defaults.xml.
+
+===============================================================
+                        PLEASE READ
+===============================================================
+ -->
+<resources>
+    <style name="Animation.Material.Activity" parent="Animation.Activity">
+        <item name="activityOpenEnterAnimation">@anim/slide_in_enter_micro</item>
+        <item name="activityOpenRemoteViewsEnterAnimation">@anim/slide_in_enter_micro</item>
+        <item name="activityOpenExitAnimation">@anim/slide_in_exit_micro</item>
+        <item name="activityCloseEnterAnimation">@null</item>
+        <item name="activityCloseExitAnimation">@anim/slide_out_micro</item>
+        <item name="taskOpenEnterAnimation">@anim/slide_in_enter_micro</item>
+        <item name="taskOpenExitAnimation">@anim/slide_in_exit_micro</item>
+        <item name="taskCloseEnterAnimation">@null</item>
+        <item name="taskCloseExitAnimation">@anim/slide_out_micro</item>
+        <item name="taskToFrontEnterAnimation">@anim/slide_in_enter_micro</item>
+        <item name="taskToFrontExitAnimation">@anim/slide_in_exit_micro</item>
+        <item name="taskToBackEnterAnimation">@null</item>
+        <item name="taskToBackExitAnimation">@anim/slide_out_micro</item>
+        <item name="wallpaperOpenEnterAnimation">@null</item>
+        <item name="wallpaperOpenExitAnimation">@anim/slide_out_micro</item>
+        <item name="wallpaperCloseEnterAnimation">@anim/slide_in_enter_micro</item>
+        <item name="wallpaperCloseExitAnimation">@anim/slide_in_exit_micro</item>
+        <item name="wallpaperIntraOpenEnterAnimation">@null</item>
+        <item name="wallpaperIntraOpenExitAnimation">@anim/slide_out_micro</item>
+        <item name="wallpaperIntraCloseEnterAnimation">@anim/slide_in_enter_micro</item>
+        <item name="wallpaperIntraCloseExitAnimation">@anim/slide_in_exit_micro</item>
+    </style>
+
+    <style name="PreferenceFragment.Material" parent="BasePreferenceFragment">
+        <item name="divider">@empty</item>
+    </style>
+
+    <style name="Preference.Material.PreferenceScreen" parent="Preference.Material.BasePreferenceScreen">
+        <item name="divider">@empty</item>
+    </style>
+
+    <style name="TextAppearance.Material.ListItem" parent="TextAppearance.Material.Body1" />
+    <style name="TextAppearance.Material.ListItemSecondary" parent="TextAppearance.Material.Caption" />
+
+    <style name="Widget.Material.TextView" parent="Widget.TextView">
+        <item name="breakStrategy">balanced</item>
+    </style>
+
+    <style name="TextAppearance.Material.NumberPicker" parent="TextAppearance.Material.Body1">
+        <item name="textSize">@dimen/text_size_medium_material</item>
+    </style>
+
+    <!-- Alert dialog button bar button -->
+    <style name="Widget.Material.Button.ButtonBar.AlertDialog" parent="Widget.Material.Button.Borderless.Small">
+        <item name="paddingStart">@dimen/list_item_padding_start_material</item>
+        <item name="paddingEnd">@dimen/list_item_padding_end_material</item>
+        <item name="drawablePadding">8dp</item>
+        <item name="gravity">center_vertical|left</item>
+        <item name="minWidth">64dp</item>
+        <item name="minHeight">@dimen/alert_dialog_button_bar_height</item>
+    </style>
+
+    <style name="Widget.Material.NumberPicker" parent="Widget.NumberPicker">
+        <item name="internalLayout">@layout/number_picker_material</item>
+        <item name="solidColor">@color/transparent</item>
+        <item name="selectionDivider">@drawable/numberpicker_selection_divider</item>
+        <item name="selectionDividerHeight">2dp</item>
+        <item name="selectionDividersDistance">24dp</item>
+        <item name="internalMinWidth">32dp</item>
+        <item name="internalMaxHeight">90dp</item>
+        <item name="virtualButtonPressedDrawable">?selectableItemBackground</item>
+        <item name="descendantFocusability">blocksDescendants</item>
+    </style>
+
+    <style name="DialogWindowTitle.Material">
+        <item name="maxLines">3</item>
+        <item name="scrollHorizontally">false</item>
+        <item name="textAppearance">@style/TextAppearance.Material.DialogWindowTitle</item>
+        <item name="gravity">@integer/config_dialogTextGravity</item>
+        <item name="ellipsize">end</item>
+    </style>
+</resources>
diff --git a/core/res/res/values-watch/themes.xml b/core/res/res/values-watch/themes.xml
index 6d6065f..04df180 100644
--- a/core/res/res/values-watch/themes.xml
+++ b/core/res/res/values-watch/themes.xml
@@ -1,5 +1,5 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- Copyright (C) 2014 The Android Open Source Project
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -14,11 +14,10 @@
      limitations under the License.
 -->
 <resources>
-    <style name="Theme.Dialog.Alert" parent="Theme.Micro.Dialog.Alert" />
-    <style name="Theme.Dialog.AppError" parent="Theme.Micro.Dialog.AppError" />
-    <style name="Theme.Holo.Dialog.Alert" parent="Theme.Micro.Dialog.Alert" />
-    <style name="Theme.Holo.Light.Dialog.Alert" parent="Theme.Micro.Dialog.Alert" />
-    <style name="Theme.InputMethod" parent="Theme.Micro.InputMethod" />
-    <style name="Theme.Material.Dialog.Alert" parent="Theme.Micro.Dialog.Alert" />
-    <style name="Theme.Material.Light.Dialog.Alert" parent="Theme.Micro.Dialog.Alert" />
+    <!-- Theme for the dialog shown when an app crashes or ANRs. Override to make it dark. -->
+    <style name="Theme.Dialog.AppError" parent="Theme.DeviceDefault.Dialog.Alert">
+        <item name="windowContentTransitions">false</item>
+        <item name="windowActivityTransitions">false</item>
+        <item name="windowCloseOnTouchOutside">false</item>
+    </style>
 </resources>
diff --git a/core/res/res/values-watch/themes_device_defaults.xml b/core/res/res/values-watch/themes_device_defaults.xml
index 66509fb..2313b26 100644
--- a/core/res/res/values-watch/themes_device_defaults.xml
+++ b/core/res/res/values-watch/themes_device_defaults.xml
@@ -13,22 +13,31 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<resources>
-    <style name="Theme.DeviceDefault" parent="Theme.Micro" />
-    <style name="Theme.DeviceDefault.NoActionBar" parent="Theme.Micro" />
-    <style name="Theme.DeviceDefault.Dialog" parent="Theme.Micro.Dialog" />
-    <style name="Theme.DeviceDefault.DialogWhenLarge" parent="Theme.Micro.Dialog" />
-    <style name="Theme.DeviceDefault.Dialog.Alert" parent="Theme.Micro.Dialog.Alert" />
-    <style name="Theme.DeviceDefault.InputMethod" parent="Theme.Micro.InputMethod"  />
-    <style name="Theme.DeviceDefault.Panel" parent="Theme.Micro.Panel"  />
-    <style name="Theme.DeviceDefault.Light" parent="Theme.Micro.Light" />
-    <style name="Theme.DeviceDefault.Light.NoActionBar" parent="Theme.Micro.Light" />
-    <style name="Theme.DeviceDefault.Light.DarkActionBar" parent="Theme.Micro.Light" />
-    <style name="Theme.DeviceDefault.Light.Dialog" parent="Theme.Micro.Dialog" />
-    <style name="Theme.DeviceDefault.Light.DialogWhenLarge" parent="Theme.Micro.Dialog" />
-    <style name="Theme.DeviceDefault.Light.Dialog.Alert" parent="Theme.Micro.Dialog.Alert" />
-    <style name="Theme.DeviceDefault.Light.Panel" parent="Theme.Micro.Light.Panel"  />
-    <style name="Theme.DeviceDefault.Settings" parent="Theme.Micro" />
-    <style name="Theme.DeviceDefault.Wallpaper" parent="Theme.Micro" />
-</resources>
 
+<!--
+===============================================================
+                        PLEASE READ
+===============================================================
+This file contains the themes that are the Device Defaults.
+If you want to edit themes to skin your device, do it here.
+We recommend that you do not edit themes.xml and instead edit
+this file.
+
+Editing this file instead of themes.xml will greatly simplify
+merges for future platform versions and CTS compliance will be
+easier.
+===============================================================
+                        PLEASE READ
+===============================================================
+ -->
+<resources>
+    <!-- Theme used for the intent picker activity. -->
+    <style name="Theme.DeviceDefault.Resolver" parent="Theme.Material">
+        <item name="colorControlActivated">?attr/colorControlHighlight</item>
+        <item name="listPreferredItemPaddingStart">?attr/dialogPreferredPadding</item>
+        <item name="listPreferredItemPaddingEnd">?attr/dialogPreferredPadding</item>
+    </style>
+
+    <!-- Use a dark theme for watches. -->
+    <style name="Theme.DeviceDefault.System" parent="Theme.Material" />
+</resources>
diff --git a/core/res/res/values-watch/themes_material.xml b/core/res/res/values-watch/themes_material.xml
new file mode 100644
index 0000000..84bc25f
--- /dev/null
+++ b/core/res/res/values-watch/themes_material.xml
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!--
+===============================================================
+                        PLEASE READ
+===============================================================
+
+The Material themes must not be modified in order to pass CTS.
+Many related themes and styles depend on other values defined in this file.
+If you would like to provide custom themes and styles for your device,
+please see styles_device_defaults.xml.
+
+===============================================================
+                        PLEASE READ
+===============================================================
+ -->
+<resources>
+    <!-- Default theme for material style input methods, which is used by the
+         {@link android.inputmethodservice.InputMethodService} class.
+         this inherits from Theme.Panel, but sets up IME appropriate animations
+         and a few custom attributes. -->
+    <style name="Theme.Material.InputMethod" parent="Theme.Material.Panel">
+        <item name="windowAnimationStyle">@style/Animation.InputMethod</item>
+        <item name="imeFullscreenBackground">?colorBackground</item>
+        <item name="imeExtractEnterAnimation">@anim/input_method_extract_enter</item>
+    </style>
+
+    <!-- Override behaviour to set the theme colours for dialogs, keep them the same. -->
+    <style name="ThemeOverlay.Material.Dialog" parent="ThemeOverlay.Material.BaseDialog">
+        <item name="windowIsFloating">false</item>
+    </style>
+
+    <!-- Force the background and floating colours to be the default colours. -->
+    <style name="Theme.Material.Dialog" parent="Theme.Material.BaseDialog">
+        <item name="colorBackground">@color/background_material_dark</item>
+        <item name="colorBackgroundFloating">@color/background_floating_material_dark</item>
+        <item name="colorBackgroundCacheHint">@color/background_cache_hint_selector_material_dark</item>
+        <item name="windowIsFloating">false</item>
+    </style>
+
+    <!-- Force the background and floating colours to be the default colours. -->
+    <style name="Theme.Material.Light.Dialog" parent="Theme.Material.Light.BaseDialog">
+        <item name="colorBackground">@color/background_material_light</item>
+        <item name="colorBackgroundFloating">@color/background_floating_material_light</item>
+        <item name="colorBackgroundCacheHint">@color/background_cache_hint_selector_material_light</item>
+        <item name="windowIsFloating">false</item>
+    </style>
+
+    <!-- Force all settings themes to use normal Material theme. -->
+    <style name="Theme.Material.Settings" parent="Theme.Material"/>
+    <style name="Theme.Material.Settings.NoActionBar" parent="Theme.Material"/>
+    <style name="Theme.Material.Settings.BaseDialog" parent="Theme.Material.Dialog"/>
+    <style name="Theme.Material.Settings.Dialog" parent="Theme.Material.Settings.BaseDialog"/>
+    <style name="Theme.Material.Settings.Dialog.BaseAlert" parent="Theme.Material.Dialog.BaseAlert"/>
+    <style name="Theme.Material.Settings.Dialog.Alert" parent="Theme.Material.Settings.Dialog.BaseAlert"/>
+    <style name="Theme.Material.Settings.DialogWhenLarge" parent="Theme.Material.DialogWhenLarge"/>
+    <style name="Theme.Material.Settings.DialogWhenLarge.NoActionBar" parent="Theme.Material.DialogWhenLarge.NoActionBar"/>
+    <style name="Theme.Material.Settings.Dialog.Presentation" parent="Theme.Material.Dialog.Presentation"/>
+    <style name="Theme.Material.Settings.SearchBar" parent="Theme.Material.SearchBar"/>
+    <style name="Theme.Material.Settings.CompactMenu" parent="Theme.Material.CompactMenu"/>
+</resources>
diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml
index 759b5a4..4c9f33e 100644
--- a/core/res/res/values/attrs.xml
+++ b/core/res/res/values/attrs.xml
@@ -1048,6 +1048,9 @@
              the status bar (via statusBarColor) and navigation bar (via navigationBarColor). -->
         <attr name="colorPrimaryDark" format="color" />
 
+        <!-- The secondary branding color for the app. -->
+        <attr name="colorSecondary" format="color" />
+
         <!-- Bright complement to the primary branding color. By default, this is the color applied
              to framework controls (via colorControlActivated). -->
         <attr name="colorAccent" format="color" />
@@ -1832,6 +1835,10 @@
         <enum name="KEYCODE_CUT" value="277" />
         <enum name="KEYCODE_COPY" value="278" />
         <enum name="KEYCODE_PASTE" value="279" />
+        <enum name="KEYCODE_SYSTEM_NAVIGATION_UP" value="280" />
+        <enum name="KEYCODE_SYSTEM_NAVIGATION_DOWN" value="281" />
+        <enum name="KEYCODE_SYSTEM_NAVIGATION_LEFT" value="282" />
+        <enum name="KEYCODE_SYSTEM_NAVIGATION_RIGHT" value="283" />
     </attr>
 
     <!-- ***************************************************************** -->
@@ -2043,6 +2050,15 @@
         <attr name="showTitle" format="boolean" />
         <!-- @hide Whether fullDark, etc. should use default values if null. -->
         <attr name="needsDefaultBackgrounds" format="boolean" />
+        <!-- @hide Workaround until we replace AlertController with custom layout. -->
+        <attr name="controllerType">
+            <!-- The default controller. -->
+            <enum name="normal" value="0" />
+            <!-- Controller for micro specific layout. -->
+            <enum name="micro" value="1" />
+        </attr>
+        <!-- @hide Offset when scrolling to a selection. -->
+        <attr name="selectionScrollOffset" format="dimension" />
     </declare-styleable>
 
     <!-- @hide -->
@@ -3777,7 +3793,10 @@
         <!-- An optional argument to supply a maximum height for this view.
              See {see android.widget.ImageView#setMaxHeight} for details. -->
         <attr name="maxHeight" format="dimension" />
-        <!-- Set a tinting color for the image. By default, the tint will blend using SRC_ATOP mode. -->
+        <!-- The tinting color for the image. By default, the tint will blend using SRC_ATOP mode.
+             Please note that for compatibility reasons, this is NOT consistent with the default
+             SRC_IN tint mode used by {@link android.widget.ImageView#setImageTintList} and by
+             similar tint attributes on other views. -->
         <attr name="tint" format="color" />
         <!-- If true, the image view will be baseline aligned with based on its
              bottom edge. -->
@@ -4822,6 +4841,11 @@
         <!-- The list year's selected circle color in the list.
              {@deprecated No longer displayed.} -->
         <attr name="yearListSelectorColor" format="color" />
+
+        <!-- @hide Whether this time picker is being displayed within a dialog,
+             in which case it may ignore the requested time picker mode due to
+             space considerations. -->
+        <attr name="dialogMode" format="boolean" />
     </declare-styleable>
 
     <declare-styleable name="TwoLineListItem">
@@ -5150,6 +5174,11 @@
         <!-- The background color state list for the AM/PM selectors.
              {@deprecated Use headerBackground instead.}-->
         <attr name="amPmBackgroundColor" format="color" />
+
+        <!-- @hide Whether this time picker is being displayed within a dialog,
+             in which case it may ignore the requested time picker mode due to
+             space considerations. -->
+        <attr name="dialogMode" />
     </declare-styleable>
 
     <!-- ========================= -->
@@ -5296,6 +5325,21 @@
         <attr name="alpha" />
     </declare-styleable>
 
+    <!-- Drawable used to render according to the animation scale. Esp. when it is 0 due to battery
+         saver mode. It should contain one animatable drawable and one static drawable.
+         @hide -->
+    <declare-styleable name="AnimationScaleListDrawable">
+    </declare-styleable>
+
+    <!-- Attributes that can be assigned to a AnimationScaleListDrawable item.
+         @hide -->
+    <declare-styleable name="AnimationScaleListDrawableItem">
+        <!-- Reference to a drawable resource to use for the state. If not
+             given, the drawable must be defined by the first child tag. -->
+        <attr name="drawable" />
+    </declare-styleable>
+
+
     <!-- Drawable used to render a geometric shape, with a gradient or a solid color. -->
     <declare-styleable name="GradientDrawable">
         <!-- Indicates whether the drawable should intially be visible. -->
@@ -5325,7 +5369,10 @@
         <attr name="innerRadius" format="dimension" />
         <!-- Thickness of the ring. When defined, thicknessRatio is ignored. -->
         <attr name="thickness" format="dimension" />
-        <!-- Indicates whether the drawable's level affects the way the gradient is drawn. -->
+        <!-- Whether the drawable level value (see
+             {@link android.graphics.drawable.Drawable#getLevel()}) is used to scale the shape.
+             Scaling behavior depends on the shape type. For "ring", the angle is scaled from 0 to
+             360. For all other types, there is no effect. The default value is true. -->
         <attr name="useLevel" />
         <!-- If set, specifies the color to apply to the drawable as a tint. By default,
              no tint is applied. May be a color state list. -->
@@ -5359,28 +5406,37 @@
     <declare-styleable name="GradientDrawableGradient">
         <!-- Start color of the gradient. -->
         <attr name="startColor" format="color" />
-        <!-- Optional center color. For linear gradients, use centerX or centerY
-             to place the center color. -->
+        <!-- Optional center color. For linear gradients, use centerX or centerY to place the center
+             color. -->
         <attr name="centerColor" format="color" />
         <!-- End color of the gradient. -->
         <attr name="endColor" format="color" />
+        <!-- Whether the drawable level value (see
+             {@link android.graphics.drawable.Drawable#getLevel()}) is used to scale the gradient.
+             Scaling behavior varies based on gradient type. For "linear", adjusts the ending
+             position along the gradient's axis of orientation. For "radial", adjusts the outer
+             radius. For "sweep", adjusts the ending angle. The default value is false. -->
         <attr name="useLevel" format="boolean" />
-        <!-- Angle of the gradient. -->
+        <!-- Angle of the gradient, used only with linear gradient. Must be a multiple of 45 in the
+             range [0, 315]. -->
         <attr name="angle" format="float" />
         <!-- Type of gradient. The default type is linear. -->
         <attr name="type">
-            <!-- Linear gradient. -->
+            <!-- Linear gradient extending across the center point. -->
             <enum name="linear" value="0" />
-            <!-- Radial, or circular, gradient. -->
+            <!-- Radial gradient extending from the center point outward. -->
             <enum name="radial" value="1" />
-            <!-- Sweep, or angled or diamond, gradient. -->
+            <!-- Sweep (or angular) gradient sweeping counter-clockwise around the center point. -->
             <enum name="sweep"  value="2" />
         </attr>
-        <!-- X coordinate of the origin of the gradient within the shape. -->
+        <!-- X-position of the center point of the gradient within the shape as a fraction of the
+             width. The default value is 0.5. -->
         <attr name="centerX" format="float|fraction" />
-        <!-- Y coordinate of the origin of the gradient within the shape. -->
+        <!-- Y-position of the center point of the gradient within the shape as a fraction of the
+             height. The default value is 0.5. -->
         <attr name="centerY" format="float|fraction" />
-        <!-- Radius of the gradient, used only with radial gradient. -->
+        <!-- Radius of the gradient, used only with radial gradient. May be an explicit dimension
+             or a fractional value relative to the shape's minimum dimension. -->
         <attr name="gradientRadius" format="float|fraction|dimension" />
     </declare-styleable>
 
@@ -7325,14 +7381,28 @@
     <declare-styleable name="Wallpaper">
         <attr name="settingsActivity" />
 
-        <!-- Reference to a the wallpaper's thumbnail bitmap. -->
+        <!-- Reference to the wallpaper's thumbnail bitmap. -->
         <attr name="thumbnail" format="reference" />
 
-        <!-- Name of the author of this component, e.g. Google. -->
+        <!-- Name of the author and/or source/collection of this component, e.g. Art Collection, Picasso. -->
         <attr name="author" format="reference" />
 
         <!-- Short description of the component's purpose or behavior. -->
         <attr name="description" />
+
+        <!-- Uri that specifies a link for further context of this wallpaper, e.g. http://www.picasso.org. -->
+        <attr name="contextUri" format="reference" />
+
+        <!-- Title of the uri that specifies a link for further context of this wallpaper, e.g. Explore collection. -->
+        <attr name="contextDescription" format="reference" />
+
+        <!-- Whether to show any metadata when previewing the wallpaper. If this value is
+             set to true, any component that shows a preview of this live wallpaper should also show
+             accompanying information like the title, the description, the author and the context
+             description of this wallpaper so the user gets to know further information about this
+             wallpaper. -->
+        <attr name="showMetadataInPreview" format="boolean" />
+
     </declare-styleable>
 
     <!-- Use <code>dream</code> as the root tag of the XML resource that
@@ -7871,6 +7941,13 @@
         <attr name="divider" />
     </declare-styleable>
 
+    <!-- Base attributes available to PreferenceScreen. -->
+    <declare-styleable name="PreferenceScreen">
+        <!-- The layout for the PreferenceScreen. This should rarely need to be changed. -->
+        <attr name="screenLayout" format="reference" />
+        <attr name="divider" />
+    </declare-styleable>
+
     <!-- Base attributes available to PreferenceActivity. -->
     <declare-styleable name="PreferenceActivity">
         <!-- The layout for the Preference Activity. This should rarely need to be changed. -->
@@ -8227,4 +8304,17 @@
                    color. -->
         <attr name="colorBackground" />
     </declare-styleable>
+
+    <declare-styleable name="Shortcut">
+        <attr name="shortcutId" format="string" />
+        <attr name="enabled" />
+        <attr name="icon" />
+        <attr name="shortcutShortLabel" format="reference" />
+        <attr name="shortcutLongLabel" format="reference" />
+        <attr name="shortcutDisabledMessage" format="reference" />
+    </declare-styleable>
+
+    <declare-styleable name="ShortcutCategories">
+        <attr name="name" />
+    </declare-styleable>
 </resources>
diff --git a/core/res/res/values/attrs_manifest.xml b/core/res/res/values/attrs_manifest.xml
index d69d73d..0872ef9 100644
--- a/core/res/res/values/attrs_manifest.xml
+++ b/core/res/res/values/attrs_manifest.xml
@@ -62,6 +62,20 @@
          a reference to a Drawable resource containing the image definition. -->
     <attr name="icon" format="reference" />
 
+    <!-- A Drawable resource providing a graphical representation of its
+         associated item.  Use with the
+         application tag (to supply a default round icon for all application
+         components), or with the activity, receiver, service, or instrumentation
+         tag (to supply a specific round icon for that component).  It may also be
+         used with the intent-filter tag to supply a round icon to show to the
+         user when an activity is being selected based on a particular Intent.
+
+         <p>The given round icon will be used to display to the user a graphical
+         representation of its associated component; for example, as the round icon
+         for main activity that is displayed in the launcher.  This must be
+         a reference to a Drawable resource containing the image definition. -->
+    <attr name="roundIcon" format="reference" />
+
     <!-- A Drawable resource providing an extended graphical banner for its
          associated item. Use with the application tag (to supply a default
          banner for all application activities), or with the activity, tag to
@@ -1236,6 +1250,7 @@
         <attr name="theme" />
         <attr name="label" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="description" />
@@ -1335,6 +1350,7 @@
         <attr name="name" />
         <attr name="label" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="permissionGroup" />
@@ -1362,6 +1378,7 @@
         <attr name="name" />
         <attr name="label" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="description" />
@@ -1395,6 +1412,7 @@
         <attr name="name" />
         <attr name="label" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
     </declare-styleable>
@@ -1676,6 +1694,7 @@
         <attr name="label" />
         <attr name="description" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="process" />
@@ -1759,6 +1778,7 @@
         <attr name="label" />
         <attr name="description" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="permission" />
@@ -1807,6 +1827,7 @@
         <attr name="label" />
         <attr name="description" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="permission" />
@@ -1843,6 +1864,7 @@
         <attr name="label" />
         <attr name="description" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="launchMode" />
@@ -1926,6 +1948,7 @@
         <attr name="label" />
         <attr name="description" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="permission" />
@@ -1998,6 +2021,7 @@
          parent="AndroidManifestActivity AndroidManifestReceiver AndroidManifestService">
         <attr name="label" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="priority" />
@@ -2128,6 +2152,7 @@
         <attr name="targetPackage" />
         <attr name="label" />
         <attr name="icon" />
+        <attr name="roundIcon" />
         <attr name="banner" />
         <attr name="logo" />
         <attr name="handleProfiling" />
diff --git a/core/res/res/values/colors.xml b/core/res/res/values/colors.xml
index bddd225..de86cef 100644
--- a/core/res/res/values/colors.xml
+++ b/core/res/res/values/colors.xml
@@ -74,6 +74,8 @@
     <drawable name="input_method_fullscreen_background">#fff9f9f9</drawable>
     <color name="input_method_navigation_guard">#ff000000</color>
 
+    <color name="system_error">#fff4511e</color> <!-- deep orange 600 -->
+
     <!-- For date picker widget -->
     <drawable name="selected_day_background">#ff0092f4</drawable>
 
@@ -118,7 +120,7 @@
     <!-- LockPatternView -->
     <color name="lock_pattern_view_regular_color">#ffffffff</color>
     <color name="lock_pattern_view_success_color">#ffffffff</color>
-    <color name="lock_pattern_view_error_color">#fff4511e</color>
+    <color name="lock_pattern_view_error_color">@color/system_error</color>
 
     <!-- FaceLock -->
     <color name="facelock_spotlight_mask">#CC000000</color>
@@ -138,6 +140,7 @@
     <color name="notification_progress_background_color">@color/secondary_text_material_light</color>
 
     <color name="notification_action_list">#ffeeeeee</color>
+    <color name="notification_action_list_dark">#ffe0e0e0</color>
 
     <!-- Keyguard colors -->
     <color name="keyguard_avatar_frame_color">#ffffffff</color>
@@ -151,7 +154,7 @@
     <color name="battery_saver_mode_color">#fff4511e</color><!-- deep orange 600 -->
 
     <!-- Default user icon colors -->
-    <color name="user_icon_1">#ff00bcd4</color><!-- teal 500 -->
+    <color name="user_icon_1">#ff00bcd4</color><!-- cyan 500 -->
     <color name="user_icon_2">#ff3f51b5</color><!-- indigo 500 -->
     <color name="user_icon_3">#ff4285f4</color><!-- blue 500 -->
     <color name="user_icon_4">#ffe91e63</color><!-- pink 500 -->
diff --git a/core/res/res/values/colors_device_defaults.xml b/core/res/res/values/colors_device_defaults.xml
new file mode 100644
index 0000000..89691e9
--- /dev/null
+++ b/core/res/res/values/colors_device_defaults.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- Colors specific to DeviceDefault themes. These are mostly pass-throughs to enable
+     overlaying new theme colors. -->
+<resources>
+    <color name="primary_device_default_dark">@color/primary_material_dark</color>
+    <color name="primary_device_default_light">@color/primary_material_light</color>
+    <color name="primary_device_default_settings">@color/primary_material_settings</color>
+    <color name="primary_dark_device_default_dark">@color/primary_dark_material_dark</color>
+    <color name="primary_dark_device_default_light">@color/primary_dark_material_light</color>
+    <color name="primary_dark_device_default_settings">@color/primary_dark_material_settings</color>
+
+    <color name="secondary_device_default_settings">@color/secondary_material_settings</color>
+    <color name="tertiary_device_default_settings">@color/tertiary_material_settings</color>
+    <color name="quaternary_device_default_settings">@color/quaternary_material_settings</color>
+
+    <color name="accent_device_default_700">@color/material_deep_teal_700</color>
+    <color name="accent_device_default_light">@color/accent_material_light</color>
+    <color name="accent_device_default_dark">@color/accent_material_dark</color>
+    <color name="accent_device_default_50">@color/material_deep_teal_50</color>
+
+    <color name="background_device_default_dark">@color/background_material_dark</color>
+    <color name="background_device_default_light">@color/background_material_light</color>
+    <color name="background_floating_device_default_dark">@color/background_floating_material_dark</color>
+    <color name="background_floating_device_default_light">@color/background_floating_material_light</color>
+</resources>
diff --git a/core/res/res/values/colors_material.xml b/core/res/res/values/colors_material.xml
index c8ca116..92426c6 100644
--- a/core/res/res/values/colors_material.xml
+++ b/core/res/res/values/colors_material.xml
@@ -26,9 +26,15 @@
 
     <color name="primary_material_dark">@color/material_grey_900</color>
     <color name="primary_material_light">@color/material_grey_100</color>
+    <color name="primary_material_settings">@color/material_blue_grey_900</color>
     <color name="primary_dark_material_dark">@color/black</color>
     <color name="primary_dark_material_light">@color/material_grey_600</color>
     <color name="primary_dark_material_light_light_status_bar">@color/material_grey_300</color>
+    <color name="primary_dark_material_settings">@color/material_blue_grey_950</color>
+
+    <color name="secondary_material_settings">@color/material_blue_grey_800</color>
+    <color name="tertiary_material_settings">@color/material_blue_grey_700</color>
+    <color name="quaternary_material_settings">@color/material_blue_grey_200</color>
 
     <color name="accent_material_light">@color/material_deep_teal_500</color>
     <color name="accent_material_dark">@color/material_deep_teal_200</color>
@@ -55,7 +61,10 @@
     <color name="secondary_text_default_material_dark">#b3ffffff</color>
 
     <item name="hint_alpha_material_dark" format="float" type="dimen">0.50</item>
-    <item name="hint_alpha_material_light" format="float" type="dimen">0.54</item>
+    <item name="hint_alpha_material_light" format="float" type="dimen">0.38</item>
+
+    <item name="hint_pressed_alpha_material_dark" format="float" type="dimen">0.70</item>
+    <item name="hint_pressed_alpha_material_light" format="float" type="dimen">0.54</item>
 
     <item name="disabled_alpha_material_light" format="float" type="dimen">0.26</item>
     <item name="disabled_alpha_material_dark" format="float" type="dimen">0.30</item>
@@ -75,11 +84,15 @@
     <color name="material_grey_100">#fff5f5f5</color>
     <color name="material_grey_50">#fffafafa</color>
 
+    <color name="material_deep_teal_50">#ffe0f2f1</color>
     <color name="material_deep_teal_100">#ffb2dfdb</color>
     <color name="material_deep_teal_200">#ff80cbc4</color>
     <color name="material_deep_teal_300">#ff4db6ac</color>
     <color name="material_deep_teal_500">#ff009688</color>
+    <color name="material_deep_teal_700">#ff00796b</color>
 
+    <color name="material_blue_grey_200">#ffb0bec5</color>
+    <color name="material_blue_grey_700">#ff455a64</color>
     <color name="material_blue_grey_800">#ff37474f</color>
     <color name="material_blue_grey_900">#ff263238</color>
     <color name="material_blue_grey_950">#ff21272b</color>
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 05d9a28..c3a5ff0 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -268,6 +268,18 @@
     <!-- The maximum duration (in milliseconds) we expect a network transition to take -->
     <integer name="config_networkTransitionTimeout">60000</integer>
 
+    <!-- Whether/how to notify the user on network switches. See LingerMonitor.java. -->
+    <integer translatable="false" name="config_networkNotifySwitchType">0</integer>
+
+    <!-- What types of network switches to notify. See LingerMonitor.java. -->
+    <string-array translatable="false" name="config_networkNotifySwitches">
+    </string-array>
+
+    <!-- Whether the device should automatically switch away from Wi-Fi networks that lose
+         Internet access. Actual device behaviour is controlled by
+         Settings.Global.NETWORK_AVOID_BAD_WIFI. This is the default value of that setting. -->
+    <integer translatable="false" name="config_networkAvoidBadWifi">1</integer>
+
     <!-- List of regexpressions describing the interface (if any) that represent tetherable
          USB interfaces.  If the device doesn't want to support tethering over USB this should
          be empty.  An example would be "usb.*" -->
@@ -539,9 +551,12 @@
          Software implementation will be used if config_hardware_auto_brightness_available is not set -->
     <bool name="config_automatic_brightness_available">false</bool>
 
-    <!-- Fast brightness animation ramp rate -->
+    <!-- Fast brightness animation ramp rate in brightness units per second-->
     <integer translatable="false" name="config_brightness_ramp_rate_fast">200</integer>
 
+    <!-- Slow brightness animation ramp rate in brightness units per second-->
+    <integer translatable="false" name="config_brightness_ramp_rate_slow">40</integer>
+
     <!-- Don't name config resources like this.  It should look like config_annoyDianne -->
     <bool name="config_annoy_dianne">true</bool>
 
@@ -754,6 +769,26 @@
     -->
     <integer name="config_defaultNightMode">1</integer>
 
+    <!-- Control whether Night display is available. This should only be enabled on devices
+         with HWC 2.0 or higher. -->
+    <bool name="config_nightDisplayAvailable">false</bool>
+
+    <!-- Default mode to control how Night display is automatically activated.
+         One of the following values (see NightDisplayController.java):
+             0 - AUTO_MODE_DISABLED
+             1 - AUTO_MODE_CUSTOM
+             2 - AUTO_MODE_TWILIGHT
+    -->
+    <integer name="config_defaultNightDisplayAutoMode">0</integer>
+
+    <!-- Default time when Night display is automatically activated.
+         Represented as milliseconds from midnight (e.g. 79200000 == 10pm). -->
+    <integer name="config_defaultNightDisplayCustomStartTime">79200000</integer>
+
+    <!-- Default time when Night display is automatically deactivated.
+         Represented as milliseconds from midnight (e.g. 21600000 == 6am). -->
+    <integer name="config_defaultNightDisplayCustomEndTime">21600000</integer>
+
     <!-- Indicate whether to allow the device to suspend when the screen is off
          due to the proximity sensor.  This resource should only be set to true
          if the sensor HAL correctly handles the proximity sensor as a wake-up source.
@@ -776,6 +811,12 @@
     -->
     <integer name="config_longPressOnBackBehavior">0</integer>
 
+    <!-- Control the behavior when the user panic presses the back button.
+            0 - Nothing
+            1 - Go to home
+    -->
+    <integer name="config_backPanicBehavior">0</integer>
+
     <!-- Control the behavior when the user short presses the power button.
             0 - Nothing
             1 - Go to sleep (doze)
@@ -1103,6 +1144,40 @@
     <integer-array name="config_autoBrightnessKeyboardBacklightValues">
     </integer-array>
 
+    <!-- Array of hysteresis constraint values for brightening, represented as tenths of a
+         percent. The length of this array is assumed to be one greater than
+         config_dynamicHysteresisLuxLevels. The brightening threshold is calculated as
+         lux * (1.0f + CONSTRAINT_VALUE). When the current lux is higher than this threshold,
+         the screen brightness is recalculated. See the config_dynamicHysteresisLuxLevels
+         description for how the constraint value is chosen. -->
+    <integer-array name="config_dynamicHysteresisBrightLevels">
+        <item>100</item>
+    </integer-array>
+
+    <!-- Array of hysteresis constraint values for darkening, represented as tenths of a
+         percent. The length of this array is assumed to be one greater than
+         config_dynamicHysteresisLuxLevels. The darkening threshold is calculated as
+         lux * (1.0f - CONSTRAINT_VALUE). When the current lux is lower than this threshold,
+         the screen brightness is recalculated. See the config_dynamicHysteresisLuxLevels
+         description for how the constraint value is chosen. -->
+    <integer-array name="config_dynamicHysteresisDarkLevels">
+        <item>200</item>
+    </integer-array>
+
+    <!-- Array of ambient lux threshold values. This is used for determining hysteresis constraint
+         values by calculating the index to use for lookup and then setting the constraint value
+         to the corresponding value of the array. The new brightening hysteresis constraint value
+         is the n-th element of config_dynamicHysteresisBrightLevels, and the new darkening
+         hysteresis constraint value is the n-th element of config_dynamicHysteresisDarkLevels.
+
+         The (zero-based) index is calculated as follows: (MAX is the largest index of the array)
+         condition                      calculated index
+         value < lux[0]                 0
+         lux[n] <= value < lux[n+1]     n+1
+         lux[MAX] <= value              MAX+1 -->
+    <integer-array name="config_dynamicHysteresisLuxLevels">
+    </integer-array>
+
     <!-- Amount of time it takes for the light sensor to warm up in milliseconds.
          For this time after the screen turns on, the Power Manager
          will not debounce light sensor readings -->
@@ -1727,6 +1802,10 @@
     <!-- Amount of time in ms the user needs to press the relevant key to bring up the global actions dialog -->
     <integer name="config_globalActionsKeyTimeout">500</integer>
 
+    <!-- Distance that should be scrolled in response to a {@link MotionEvent#ACTION_SCROLL event}
+         with an axis value of 1. -->
+    <dimen name="config_scrollFactor">64dp</dimen>
+
     <!-- Maximum number of grid columns permitted in the ResolverActivity
          used for picking activities to handle an intent. -->
     <integer name="config_maxResolverActivityColumns">3</integer>
@@ -1808,28 +1887,6 @@
     -->
     <bool name="config_enableWifiDisplay">false</bool>
 
-    <!-- The color transform values that correspond to each respective configuration mode for the
-         built-in display, or -1 if the mode is unsupported by the device. The possible
-         configuration modes are:
-            1. Wide-gamut ("Vibrant")
-            2. Adobe RGB ("Natural")
-            3. sRGB ("Standard")
-
-        For example, if a device had Wide-gamut as color transform mode 1, sRGB mode as color
-        transform mode 7, and did not support Adobe RGB at all this would look like:
-
-            <integer-array name="config_colorTransforms">
-                <item>1</item>
-                <item>-1</item>
-                <item>7</item>
-            </integer-array>
-    -->
-    <integer-array name="config_colorTransforms">
-        <item>-1</item>
-        <item>-1</item>
-        <item>-1</item>
-    </integer-array>
-
     <!-- When true, local displays that do not contain any of their own content will automatically
          mirror the content of the default display. -->
     <bool name="config_localDisplaysMirrorContent">true</bool>
@@ -2076,9 +2133,11 @@
          "bugreport" = Take bug report, if available
          "silent" = silent mode
          "users" = list of users
+         "restart" = restart device
          -->
     <string-array translatable="false" name="config_globalActionsList">
         <item>power</item>
+        <item>restart</item>
         <item>bugreport</item>
         <item>users</item>
     </string-array>
@@ -2149,6 +2208,11 @@
     <!-- Flag specifying whether VT is available on device -->
     <bool name="config_device_vt_available">false</bool>
 
+    <!-- Flag specifying whether the device will use the "allow_hold_in_ims_call" carrier config
+         option.  When false, the device will support holding of IMS calls, regardless of the
+         carrier config setting. -->
+    <bool name="config_device_respects_hold_carrier_config">true</bool>
+
     <!-- Flag specifying whether VT should be available for carrier: independent of
          carrier provisioning. If false: hard disabled. If true: then depends on carrier
          provisioning, availability etc -->
@@ -2291,8 +2355,8 @@
     <!-- An array of CDMA roaming indicators which means international roaming -->
     <integer-array translatable="false" name="config_cdma_international_roaming_indicators" />
 
-    <!-- set the system language as value of EF LI/EF PL -->
-    <bool name="config_use_sim_language_file">true</bool>
+    <!-- flag to indicate if EF LI/EF PL should be used for system language -->
+    <bool name="config_use_sim_language_file">false</bool>
 
     <!-- Use ERI text for network name on CDMA LTE -->
     <bool name="config_LTE_eri_for_network_name">true</bool>
@@ -2467,6 +2531,10 @@
     <!-- True if the device supports Sustained Performance Mode-->
     <bool name="config_sustainedPerformanceModeSupported">false</bool>
 
+    <!-- File used to enable the double touch gesture.
+         TODO: move to input HAL once ready. -->
+    <string name="config_doubleTouchGestureEnableFile"></string>
+
     <!-- Controls how we deal with externally connected physical keyboards.
          0 - When using this device, it is not clear for users to recognize when the physical
              keyboard is (should be) connected and when it is (should be) disconnected.  Most of
@@ -2492,4 +2560,68 @@
     <string-array translatable="false" name="config_defaultPinnerServiceFiles">
     </string-array>
 
+    <!-- True if camera app should be pinned via Pinner Service -->
+    <bool name="config_pinnerCameraApp">false</bool>
+
+    <!-- Component that is the default launcher when demo mode is enabled. -->
+    <string name="config_demoModeLauncherComponent">com.android.retaildemo/.DemoPlayer</string>
+
+    <!-- Flag indicating whether round icons should be parsed from the application manifest. -->
+    <bool name="config_useRoundIcon">false</bool>
+
+    <!-- Flag indicating whether the assist disclosure can be disabled using
+         ASSIST_DISCLOSURE_ENABLED. -->
+    <bool name="config_allowDisablingAssistDisclosure">false</bool>
+
+    <!-- True if the device supports system navigation keys. -->
+    <bool name="config_supportSystemNavigationKeys">false</bool>
+
+    <!-- emergency call number for the emergency affordance -->
+    <string name="config_emergency_call_number" translatable="false">112</string>
+
+    <!-- Do not translate. Mcc codes whose existence trigger the presence of emergency
+         affordances-->
+    <integer-array name="config_emergency_mcc_codes" translatable="false">
+        <item>404</item>
+        <item>405</item>
+    </integer-array>
+
+    <!-- Package name for the device provisioning package. -->
+    <string name="config_deviceProvisioningPackage"></string>
+
+    <!-- Colon separated list of package names that should be granted DND access -->
+    <string name="config_defaultDndAccessPackages" translatable="false">com.android.camera2</string>
+
+    <!-- User restrictions set when the first user is created.
+         Note: Also update appropriate overlay files. -->
+    <string-array translatable="false" name="config_defaultFirstUserRestrictions">
+    </string-array>
+
+    <!-- Default value for android:focusableInTouchMode for some framework scrolling containers.
+         ListView/GridView are notably absent since this is their default anyway.
+         Set to true for watch devices. -->
+    <bool name="config_focusScrollContainersInTouchMode">false</bool>
+
+    <string name="config_networkOverLimitComponent" translatable="false">com.android.systemui/com.android.systemui.net.NetworkOverLimitActivity</string>
+    <string name="config_dataUsageSummaryComponent" translatable="false">com.android.settings/com.android.settings.Settings$DataUsageSummaryActivity</string>
+
+    <!-- A array of regex to treat a SMS as VVM SMS if the message body matches.
+         Each item represents an entry, which consists of two parts:
+         a comma (,) separated list of MCCMNC the regex applies to, followed by a semicolon (;), and
+         then the regex itself. -->
+    <string-array translatable="false" name="config_vvmSmsFilterRegexes">
+        <!-- Verizon requires any SMS that starts with //VZWVVM to be treated as a VVM SMS-->
+        <item>310004,310010,310012,310013,310590,310890,310910,311110,311270,311271,311272,311273,311274,311275,311276,311277,311278,311279,311280,311281,311282,311283,311284,311285,311286,311287,311288,311289,311390,311480,311481,311482,311483,311484,311485,311486,311487,311488,311489;^//VZWVVM.*</item>
+    </string-array>
+    <!-- This config is holding calling number conversion map - expected to convert to emergency
+         number. Formats for this config as below:
+         <item>[dialstring1],[dialstring2],[dialstring3]:[replacement]</item>
+
+         E.g. for Taiwan Type Approval, 110 and 119 should be converted to 112.
+         <item>110,119:112</item>
+    -->
+    <string-array translatable="false" name="config_convert_to_emergency_number_map" />
+
+    <!-- An array of packages for which notifications cannot be blocked. -->
+    <string-array translatable="false" name="config_nonBlockableNotificationPackages" />
 </resources>
diff --git a/core/res/res/values/config_material.xml b/core/res/res/values/config_material.xml
new file mode 100644
index 0000000..29494db
--- /dev/null
+++ b/core/res/res/values/config_material.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- These resources are around just to allow their values to be customized
+     for different hardware and product builds, only for Material theme.  Do not translate.
+
+     NOTE: The naming convention is "config_camelCaseValue".  -->
+
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- True if the device supports action bars. -->
+    <bool name="config_windowActionBarSupported">true</bool>
+
+    <!-- True if the device should have titles by default. -->
+    <bool name="config_windowNoTitleDefault">false</bool>
+
+    <!-- The alert controller to use for alert dialogs. -->
+    <integer name="config_alertDialogController">0</integer>
+
+    <!-- True if windowOverscan should be on by default. -->
+    <bool name="config_windowOverscanByDefault">false</bool>
+
+    <!-- True if preference fragment should clip to padding. -->
+    <bool name="config_preferenceFragmentClipToPadding">true</bool>
+
+    <!-- The amount to offset when scrolling to a selection in an AlertDialog -->
+    <dimen name="config_alertDialogSelectionScrollOffset">0dp</dimen>
+</resources>
diff --git a/core/res/res/values/dimens_material.xml b/core/res/res/values/dimens_material.xml
index 00e48a0..ae31165 100644
--- a/core/res/res/values/dimens_material.xml
+++ b/core/res/res/values/dimens_material.xml
@@ -24,6 +24,8 @@
 
     <!-- Preference fragment padding, sides -->
     <dimen name="preference_fragment_padding_side_material">0dp</dimen>
+    <!-- Preference fragment padding, vertical -->
+    <dimen name="preference_fragment_padding_vertical_material">0dp</dimen>
 
     <!-- Preference breadcrumbs padding, start padding -->
     <dimen name="preference_breadcrumbs_padding_start_material">12dp</dimen>
@@ -53,6 +55,8 @@
     <!-- Default padding for list items. This should match the action bar
          content inset so that ListActivity items line up correctly. -->
     <dimen name="list_item_padding_horizontal_material">@dimen/action_bar_content_inset_material</dimen>
+    <dimen name="list_item_padding_start_material">@dimen/action_bar_content_inset_material</dimen>
+    <dimen name="list_item_padding_end_material">@dimen/action_bar_content_inset_material</dimen>
 
     <!-- Padding to add to the start of the overflow action button. -->
     <dimen name="action_bar_overflow_padding_start_material">6dp</dimen>
@@ -84,6 +88,8 @@
     <dimen name="text_size_medium_material">18sp</dimen>
     <dimen name="text_size_small_material">14sp</dimen>
 
+    <item name="text_line_spacing_multiplier_material" format="float" type="dimen">1.0</item>
+
     <dimen name="text_edit_floating_toolbar_elevation">2dp</dimen>
     <dimen name="text_edit_floating_toolbar_margin">20dp</dimen>
 
@@ -183,4 +189,8 @@
     <dimen name="day_picker_button_margin_top">0dp</dimen>
 
     <dimen name="datepicker_view_animator_height">226dp</dimen>
+
+    <!-- Date and time picker legacy dimens -->
+    <dimen name="picker_top_margin">16dip</dimen>
+    <dimen name="picker_bottom_margin">16dip</dimen>
 </resources>
diff --git a/core/res/res/values/donottranslate.xml b/core/res/res/values/donottranslate.xml
index a139529..3a1679c 100644
--- a/core/res/res/values/donottranslate.xml
+++ b/core/res/res/values/donottranslate.xml
@@ -26,4 +26,7 @@
     <string name="icu_abbrev_wday_month_day_no_year">eeeMMMMd</string>
     <!-- @hide DO NOT TRANSLATE. date formatting pattern for system ui.-->
     <string name="system_ui_date_pattern">@string/icu_abbrev_wday_month_day_no_year</string>
+    <!-- @hide DO NOT TRANSLATE Spans within this text are applied to style composing regions
+    within an EditText widget. The text content is ignored and not used. -->
+    <string name="candidates_style" translatable="false"><u>candidates</u></string>
 </resources>
diff --git a/core/res/res/values/ids.xml b/core/res/res/values/ids.xml
index 7f8acd3..5c165e6 100644
--- a/core/res/res/values/ids.xml
+++ b/core/res/res/values/ids.xml
@@ -122,9 +122,11 @@
 
   <!-- Accessibility action identifier for {@link android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction#ACTION_SET_PROGRESS}. -->
   <item type="id" name="accessibilityActionSetProgress" />
-  
+
   <!-- Accessibility action identifier for {@link android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction#ACTION_CONTEXT_CLICK}. -->
   <item type="id" name="accessibilityActionContextClick" />
 
   <item type="id" name="remote_input_tag" />
+
+  <item type="id" name="cross_task_transition" />
 </resources>
diff --git a/core/res/res/values/integers.xml b/core/res/res/values/integers.xml
index 8f8d59e..2b69c75b 100644
--- a/core/res/res/values/integers.xml
+++ b/core/res/res/values/integers.xml
@@ -26,4 +26,12 @@
 
     <integer name="date_picker_mode">1</integer>
     <integer name="time_picker_mode">1</integer>
+
+    <!-- Specifies date picker mode to be 'calendar' -->
+    <integer name="date_picker_mode_material">2</integer>
+
+    <!-- Specifies time picker mode to be 'clock' -->
+    <integer name="time_picker_mode_material">2</integer>
+
+    <integer name="date_picker_header_max_lines_material">2</integer>
 </resources>
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index 6797799..77de87d 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -2728,4 +2728,18 @@
     <public type="id" name="icon_frame" id="0x0102003e" />
     <public type="id" name="list_container" id="0x0102003f" />
     <public type="id" name="switch_widget" id="0x01020040" />
+  <!-- ===============================================================
+       Resources added in version N MR1 of the platform
+       =============================================================== -->
+    <eat-comment />
+    <public type="attr" name="shortcutId" id="0x01010528" />
+    <public type="attr" name="shortcutShortLabel" id="0x01010529" />
+    <public type="attr" name="shortcutLongLabel" id="0x0101052a" />
+    <public type="attr" name="shortcutDisabledMessage" id="0x0101052b" />
+    <public type="attr" name="roundIcon" id="0x0101052c" />
+    <public type="attr" name="contextUri" id="0x0101052d" />
+    <public type="attr" name="contextDescription" id="0x0101052e" />
+    <public type="attr" name="showMetadataInPreview" id="0x0101052f" />
+    <public type="attr" name="colorSecondary" id="0x01010530" />
+
 </resources>
diff --git a/core/res/res/values/strings.xml b/core/res/res/values/strings.xml
index b55a9b22..5d399c1 100644
--- a/core/res/res/values/strings.xml
+++ b/core/res/res/values/strings.xml
@@ -482,6 +482,13 @@
     <!-- label for item that turns off power in phone options dialog -->
     <string name="global_action_power_off">Power off</string>
 
+    <!-- label for item that restarts phone in phone options dialog -->
+    <!-- TODO: promote to separate string-->
+    <string name="global_action_restart" translatable="false">@string/sim_restart_button</string>
+
+    <!-- label for item that starts emergency call -->
+    <string name="global_action_emergency">Emergency</string>
+
     <!-- label for item that generates a bug report in the phone options dialog -->
     <string name="global_action_bug_report">Bug report</string>
 
@@ -1756,6 +1763,10 @@
     <string name="lockscreen_pattern_wrong">Try again</string>
     <!-- On the unlock password screen, shown when the user enters the wrong lock password and must try again. -->
     <string name="lockscreen_password_wrong">Try again</string>
+
+    <!-- On the keyguard screen, this string explains that some features or data may not be available until the device is unlocked. [CHAR LIMIT=48] -->
+    <string name="lockscreen_storage_locked">Unlock for all features and data</string>
+
     <!-- Shown when face unlock failed multiple times so we're just using the backup -->
     <string name="faceunlock_multiple_failures">Maximum Face Unlock attempts exceeded</string>
 
@@ -1949,7 +1960,7 @@
     <string name="lockscreen_access_pattern_cleared">Pattern cleared</string>
     <!-- Accessibility description sent when user adds a dot to the pattern. [CHAR LIMIT=NONE]  -->
     <string name="lockscreen_access_pattern_cell_added">Cell added</string>
-    <!-- Accessibility description sent when user adds a dot to the pattern. Announces the 
+    <!-- Accessibility description sent when user adds a dot to the pattern. Announces the
     actual cell when headphones are connected [CHAR LIMIT=NONE]  -->
     <string name="lockscreen_access_pattern_cell_added_verbose">
             Cell <xliff:g id="cell_index" example="3">%1$s</xliff:g> added</string>
@@ -2029,6 +2040,12 @@
     <!-- Button to restart the device after the factory test. -->
     <string name="factorytest_reboot">Reboot</string>
 
+    <!-- Do not translate.  timepicker mode, overridden for watch -->
+    <string name="time_picker_mode" translatable="false">"clock"</string>
+
+    <!-- Do not translate.  datepicker mode, overridden for watch -->
+    <string name="date_picker_mode" translatable="false">"calendar"</string>
+
     <!-- Do not translate.  WebView User Agent string -->
     <string name="web_user_agent" translatable="false">Mozilla/5.0 (Linux; U; <xliff:g id="x">Android %s</xliff:g>)
         AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 <xliff:g id="mobile">%s</xliff:g>Safari/534.30</string>
@@ -2789,11 +2806,14 @@
     <!-- [CHAR LIMIT=NONE] Message shown in upgrading dialog when doing an fstrim. -->
     <string name="android_upgrading_fstrim">Optimizing storage.</string>
 
-    <!-- [CHAR LIMIT=40] Title of notification that is shown when performing a system upgrade. -->
-    <string name="android_upgrading_notification_title">Android is upgrading</string>
+    <!-- [CHAR LIMIT=40] Title of notification that is shown when finishing a system upgrade. -->
+    <string name="android_upgrading_notification_title">Finishing Android update\u2026</string>
     <!-- [CHAR LIMIT=200] Body of notification that is shown when performing a system upgrade. -->
     <string name="android_upgrading_notification_body">Some apps may not work properly until the upgrade finishes</string>
 
+    <!-- [CHAR LIMIT=40] Toast that is shown when an app is still upgrading. -->
+    <string name="app_upgrading_toast"><xliff:g id="application">%1$s</xliff:g> is upgrading\u2026</string>
+
     <!-- [CHAR LIMIT=NONE] Message shown in upgrading dialog for each .apk that is optimized. -->
     <string name="android_upgrading_apk">Optimizing app
         <xliff:g id="number" example="123">%1$d</xliff:g> of
@@ -2918,6 +2938,27 @@
     <!-- A notification is shown when the user connects to a Wi-Fi network and the system detects that that network has no Internet access. This is the notification's message. -->
     <string name="wifi_no_internet_detailed">Tap for options</string>
 
+    <!-- A notification might be shown if the device switches to another network type (e.g., cellular data) because it detects that the network it was using (e.g., Wi-Fi) has lost Internet connectivity. This is the notification's title. %1$s is the network type that the device switched to, e.g., cellular data. It is one of the strings in the network_switch_type_name array. -->
+    <string name="network_switch_metered">Switched to <xliff:g id="network_type">%1$s</xliff:g></string>
+
+    <!-- A notification might be shown if the device switches to another network type (e.g., cellular data) because it detects that the network it was using (e.g., Wi-Fi) has lost Internet connectivity. This is the notification's message. %1$s is the network that the device switched to, e.g., cellular data. %2$s is the network type the device switched from, e.g., Wi-Fi. Both are strings in the network_switch_type_name array. -->
+    <string name="network_switch_metered_detail">Device uses <xliff:g id="new_network">%1$s</xliff:g> when <xliff:g id="previous_network">%2$s</xliff:g> has no Internet access. Charges may apply.</string>
+
+    <!-- A toast might be shown if the device switches to another network type (e.g., cellular data) because it detects that the network it was using (e.g., Wi-Fi) has lost Internet connectivity. This is the text of the toast. %1$s is the network that the device switched from, e.g., Wi-Fi. %2$s is the network type the device switched from, e.g., cellular data. Both are strings in the network_switch_type_name array. -->
+    <string name="network_switch_metered_toast">Switched from <xliff:g id="previous_network">%1$s</xliff:g> to <xliff:g id="new_network">%2$s</xliff:g></string>
+
+    <!-- Network type names used in the network_switch_metered and network_switch_metered_detail strings. These must be kept in the sync with the values NetworkCapabilities.TRANSPORT_xxx values, and in the same order. -->
+    <string-array name="network_switch_type_name">
+        <item>cellular data</item>
+        <item>Wi-Fi</item>
+        <item>Bluetooth</item>
+        <item>Ethernet</item>
+        <item>VPN</item>
+    </string-array>
+
+    <!-- Network type name displayed if one of the types is not found in network_switch_type_name. -->
+    <string name="network_switch_type_name_unknown">an unknown network type</string>
+
      <!-- A notification is shown when a user's selected SSID is later disabled due to connectivity problems.  This is the notification's title / ticker. -->
      <string name="wifi_watchdog_network_disabled">Couldn\'t connect to Wi-Fi</string>
      <!-- A notification is shown when a user's selected SSID is later disabled due to connectivity problems.  The complete alert msg is: <hotspot name> + this string, i.e. "Linksys has a poor internet connection" -->
@@ -3090,8 +3131,6 @@
     <string name="fast_scroll_alphabet">\u0020ABCDEFGHIJKLMNOPQRSTUVWXYZ</string>
     <string name="fast_scroll_numeric_alphabet">\u00200123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ</string>
 
-    <string name="candidates_style"><u>candidates</u></string>
-
     <!-- External media notification strings -->
     <skip />
 
@@ -3294,10 +3333,12 @@
     <string name="vpn_lockdown_connecting">Always-on VPN connecting\u2026</string>
     <!-- Notification title when connected to lockdown VPN. -->
     <string name="vpn_lockdown_connected">Always-on VPN connected</string>
+    <!-- Notification title when not connected to lockdown VPN. -->
+    <string name="vpn_lockdown_disconnected">Always-on VPN disconnected</string>
     <!-- Notification title when error connecting to lockdown VPN. -->
     <string name="vpn_lockdown_error">Always-on VPN error</string>
     <!-- Notification body that indicates user can touch to configure lockdown VPN connection. -->
-    <string name="vpn_lockdown_config">Tap to configure</string>
+    <string name="vpn_lockdown_config">Tap to set up</string>
 
     <!-- Localized strings for WebView -->
     <!-- Label for button in a WebView that will open a chooser to choose a file to upload -->
@@ -3518,8 +3559,8 @@
     <!-- Button text for the edit menu in input method extract mode. [CHAR LIMIT=16] -->
     <string name="extract_edit_menu_button">Edit</string>
 
-    <!-- Notification title when data usage has exceeded warning threshold. [CHAR LIMIT=32] -->
-    <string name="data_usage_warning_title">Data usage warning</string>
+    <!-- Notification title when data usage has exceeded warning threshold. [CHAR LIMIT=50] -->
+    <string name="data_usage_warning_title">Data usage alert</string>
     <!-- Notification body when data usage has exceeded warning threshold. [CHAR LIMIT=32] -->
     <string name="data_usage_warning_body">Tap to view usage and settings.</string>
 
@@ -4158,7 +4199,7 @@
 
     <!-- [CHAR_LIMIT=NONE] Data saver: Feature description -->
     <string name="data_saver_description">To help reduce data usage, Data Saver prevents some apps from sending or receiving data in the background. An app you’re currently using can access data, but may do so less frequently. This may mean, for example, that images don’t display until you tap them.</string>
-    <!-- [CHAR_LIMIT=30] Data saver: Title on first-time dialogFeature description -->
+    <!-- [CHAR_LIMIT=35] Data saver: Title on first-time dialog -->
     <string name="data_saver_enable_title">Turn on Data Saver?</string>
     <!-- [CHAR_LIMIT=16] Data saver: Button to turn it on on first-time dialog -->
     <string name="data_saver_enable_button">Turn on</string>
@@ -4325,6 +4366,9 @@
     <string name="language_picker_section_suggested">Suggested</string>
     <!-- List section subheader for the language picker, containing a list of all languages available [CHAR LIMIT=30] -->
     <string name="language_picker_section_all">All languages</string>
+    <!-- List section subheader for the region picker, containing a list of all regions supported for the selected language.
+    Warning: this is a more 'neutral' term for 'country', not for the sub-divisions of a country. [CHAR LIMIT=30] -->
+    <string name="region_picker_section_all">All regions</string>
 
     <!-- Menu item in the locale menu  [CHAR LIMIT=30] -->
     <string name="locale_search_menu">Search</string>
@@ -4371,6 +4415,23 @@
     <!-- The representation of a time duration when negative. An example is -1:14. This can be used with a countdown timer for example.-->
     <string name="negative_duration">\u2212<xliff:g id="time" example="1:14">%1$s</xliff:g></string>
 
+    <!-- Title of notification to start a new demo session when device is in retail mode [CHAR LIMIT=NONE] -->
+    <string name="reset_retail_demo_mode_title">Reset device?</string>
+    <!-- Text of notification to start a new demo session when device is in retail mode [CHAR LIMIT=NONE] -->
+    <string name="reset_retail_demo_mode_text">Tap to reset device</string>
+    <!-- Text of dialog shown when starting a demo user for the first time [CHAR LIMIT=40] -->
+    <string name="demo_starting_message">Starting demo\u2026</string>
+    <!-- Text of dialog shown when starting a new demo user in retail demo mode [CHAR LIMIT=40] -->
+    <string name="demo_restarting_message">Resetting device\u2026</string>
+    <!-- Title of the dialog shown when user inactivity times out in retail demo mode [CHAR LIMIT=40] -->
+    <string name="demo_user_inactivity_timeout_title">Reset device?</string>
+    <!-- Warning message shown when user inactivity times out in retail demo mode [CHAR LIMIT=none] -->
+    <string name="demo_user_inactivity_timeout_countdown">You\u2019ll lose any changes and the demo will start again in <xliff:g id="timeout" example="9">%1$s</xliff:g> seconds\u2026</string>
+    <!-- Text of button to allow user to abort countdown and continue current session in retail demo mode [CHAR LIMIT=40] -->
+    <string name="demo_user_inactivity_timeout_left_button">Cancel</string>
+    <!-- Text of button to allow user to abort countdown and immediately start another session in retail demo mode [CHAR LIMIT=40] -->
+    <string name="demo_user_inactivity_timeout_right_button">Reset now</string>
+
     <!-- Title of notification shown when device has been forced to safe mode after a security compromise. -->
     <string name="audit_safemode_notification">Factory reset to use this device without restrictions</string>
     <!-- Description of notification shown when device has been forced to safe mode after a security compromise. -->
@@ -4379,4 +4440,6 @@
     <!-- Accessibilty string added to a widget that has been suspended [CHAR LIMIT=20] -->
     <string name="suspended_widget_accessibility">Disabled <xliff:g id="label" example="Calendar">%1$s</xliff:g></string>
 
+    <!-- Label used by Telephony code, assigned as the display name for conference calls [CHAR LIMIT=60] -->
+    <string name="conference_call">Conference Call</string>
 </resources>
diff --git a/core/res/res/values/styles.xml b/core/res/res/values/styles.xml
index 790dcfa..937428b 100644
--- a/core/res/res/values/styles.xml
+++ b/core/res/res/values/styles.xml
@@ -649,11 +649,13 @@
     <style name="Widget.ScrollView">
         <item name="scrollbars">vertical</item>
         <item name="fadingEdge">vertical</item>
+        <item name="focusableInTouchMode">@bool/config_focusScrollContainersInTouchMode</item>
     </style>
 
     <style name="Widget.HorizontalScrollView">
         <item name="scrollbars">horizontal</item>
         <item name="fadingEdge">horizontal</item>
+        <item name="focusableInTouchMode">@bool/config_focusScrollContainersInTouchMode</item>
     </style>
 
     <style name="Widget.ListView" parent="Widget.AbsListView">
@@ -1122,6 +1124,8 @@
     <style name="PreferenceFragmentList">
         <item name="paddingStart">@dimen/preference_fragment_padding_side</item>
         <item name="paddingEnd">@dimen/preference_fragment_padding_side</item>
+        <item name="paddingTop">0dp</item>
+        <item name="paddingBottom">@dimen/preference_fragment_padding_bottom</item>
     </style>
 
     <!-- Other Misc Styles -->
@@ -1408,7 +1412,7 @@
         <item name="paddingEnd">?attr/dialogPreferredPadding</item>
         <item name="background">?attr/selectableItemBackground</item>
         <item name="drawablePadding">32dp</item>
-        <item name="drawableTint">@color/accent_material_light</item>
+        <item name="drawableTint">?android:attr/colorAccent</item>
         <item name="drawableTintMode">src_atop</item>
     </style>
 
diff --git a/core/res/res/values/styles_material.xml b/core/res/res/values/styles_material.xml
index bb07834..22bdf7e 100644
--- a/core/res/res/values/styles_material.xml
+++ b/core/res/res/values/styles_material.xml
@@ -36,13 +36,18 @@
         <item name="layout">@layout/preference_material</item>
     </style>
 
-    <style name="PreferenceFragment.Material">
+    <style name="BasePreferenceFragment">
         <item name="layout">@layout/preference_list_fragment_material</item>
         <item name="paddingStart">@dimen/preference_fragment_padding_side_material</item>
         <item name="paddingEnd">@dimen/preference_fragment_padding_side_material</item>
+        <item name="paddingTop">@dimen/preference_fragment_padding_vertical_material</item>
+        <item name="paddingBottom">@dimen/preference_fragment_padding_vertical_material</item>
         <item name="divider">?attr/listDivider</item>
+        <item name="clipToPadding">@bool/config_preferenceFragmentClipToPadding</item>
     </style>
 
+    <style name="PreferenceFragment.Material" parent="BasePreferenceFragment"/>
+
     <style name="PreferenceActivity.Material">
         <item name="layout">@layout/preference_list_content_material</item>
         <item name="headerLayout">@layout/preference_header_item_material</item>
@@ -76,7 +81,12 @@
         <item name="layout">@layout/preference_widget_seekbar_material</item>
     </style>
 
-    <style name="Preference.Material.PreferenceScreen"/>
+    <style name="Preference.Material.BasePreferenceScreen">
+        <item name="screenLayout">@layout/preference_list_fragment_material</item>
+        <item name="divider">?attr/listDivider</item>
+    </style>
+
+    <style name="Preference.Material.PreferenceScreen" parent="Preference.Material.BasePreferenceScreen"/>
 
     <style name="Preference.Material.DialogPreference">
         <item name="positiveButtonText">@string/ok</item>
@@ -134,6 +144,8 @@
     <style name="PreferenceFragmentList.Material">
         <item name="paddingStart">@dimen/preference_fragment_padding_side_material</item>
         <item name="paddingEnd">@dimen/preference_fragment_padding_side_material</item>
+        <item name="paddingTop">@dimen/preference_fragment_padding_vertical_material</item>
+        <item name="paddingBottom">@dimen/preference_fragment_padding_vertical_material</item>
     </style>
 
     <!-- Begin Material theme styles -->
@@ -147,6 +159,7 @@
         <item name="textColorLink">?attr/textColorLink</item>
         <item name="textSize">@dimen/text_size_body_1_material</item>
         <item name="fontFamily">@string/font_family_body_1_material</item>
+        <item name="lineSpacingMultiplier">@dimen/text_line_spacing_multiplier_material</item>
     </style>
 
     <style name="TextAppearance.Material.Display4">
@@ -242,6 +255,8 @@
         <item name="textColor">?attr/textColorPrimary</item>
     </style>
 
+    <style name="TextAppearance.Material.NumberPicker" parent="TextAppearance.Material.Body1"/>
+
     <!-- Deprecated text styles -->
 
     <style name="TextAppearance.Material.Inverse">
@@ -462,6 +477,9 @@
         <item name="textColor">#66000000</item>
     </style>
 
+    <style name="TextAppearance.Material.ListItem" parent="TextAppearance.Material.Subhead" />
+    <style name="TextAppearance.Material.ListItemSecondary" parent="TextAppearance.Material.Body1" />
+
     <style name="Widget.Material.Notification.ProgressBar" parent="Widget.Material.Light.ProgressBar.Horizontal" />
 
     <style name="Widget.Material.Notification.MessagingText" parent="Widget.Material.Light.TextView">
@@ -667,7 +685,7 @@
     </style>
 
     <style name="Widget.Material.TimePicker">
-        <item name="timePickerMode">clock</item>
+        <item name="timePickerMode">@integer/time_picker_mode_material</item>
         <item name="legacyLayout">@layout/time_picker_legacy_material</item>
         <!-- Attributes for new-style TimePicker. -->
         <item name="internalLayout">@layout/time_picker_material</item>
@@ -681,7 +699,7 @@
     </style>
 
     <style name="Widget.Material.DatePicker">
-        <item name="datePickerMode">calendar</item>
+        <item name="datePickerMode">@integer/date_picker_mode_material</item>
         <item name="legacyLayout">@layout/date_picker_legacy_holo</item>
         <item name="calendarViewShown">true</item>
         <!-- Attributes for new-style DatePicker. -->
@@ -1190,12 +1208,18 @@
         <item name="listItemLayout">@layout/select_dialog_item_material</item>
         <item name="multiChoiceItemLayout">@layout/select_dialog_multichoice_material</item>
         <item name="singleChoiceItemLayout">@layout/select_dialog_singlechoice_material</item>
+        <item name="controllerType">@integer/config_alertDialogController</item>
+        <item name="selectionScrollOffset">@dimen/config_alertDialogSelectionScrollOffset</item>
     </style>
 
     <style name="AlertDialog.Material.Light" />
 
     <style name="DatePickerDialog.Material" parent="AlertDialog.Material">
-        <item name="showTitle">true</item>
+        <item name="showTitle">false</item>
+    </style>
+
+    <style name="TimePickerDialog.Material" parent="AlertDialog.Material">
+        <item name="showTitle">false</item>
     </style>
 
     <!-- Window title -->
diff --git a/core/res/res/values/styles_micro.xml b/core/res/res/values/styles_micro.xml
deleted file mode 100644
index 341a0a4..0000000
--- a/core/res/res/values/styles_micro.xml
+++ /dev/null
@@ -1,93 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- Copyright (C) 2014 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-<resources>
-    <style name="Animation.Micro"/>
-
-    <style name="Animation.Micro.Activity" parent="Animation.Material.Activity">
-        <item name="activityOpenEnterAnimation">@anim/slide_in_enter_micro</item>
-        <item name="activityOpenRemoteViewsEnterAnimation">@anim/slide_in_enter_micro</item>
-        <item name="activityOpenExitAnimation">@anim/slide_in_exit_micro</item>
-        <item name="activityCloseEnterAnimation">@null</item>
-        <item name="activityCloseExitAnimation">@anim/slide_out_micro</item>
-        <item name="taskOpenEnterAnimation">@anim/slide_in_enter_micro</item>
-        <item name="taskOpenExitAnimation">@anim/slide_in_exit_micro</item>
-        <item name="taskCloseEnterAnimation">@null</item>
-        <item name="taskCloseExitAnimation">@anim/slide_out_micro</item>
-        <item name="taskToFrontEnterAnimation">@anim/slide_in_enter_micro</item>
-        <item name="taskToFrontExitAnimation">@anim/slide_in_exit_micro</item>
-        <item name="taskToBackEnterAnimation">@null</item>
-        <item name="taskToBackExitAnimation">@anim/slide_out_micro</item>
-        <item name="wallpaperOpenEnterAnimation">@null</item>
-        <item name="wallpaperOpenExitAnimation">@anim/slide_out_micro</item>
-        <item name="wallpaperCloseEnterAnimation">@anim/slide_in_enter_micro</item>
-        <item name="wallpaperCloseExitAnimation">@anim/slide_in_exit_micro</item>
-        <item name="wallpaperIntraOpenEnterAnimation">@null</item>
-        <item name="wallpaperIntraOpenExitAnimation">@anim/slide_out_micro</item>
-        <item name="wallpaperIntraCloseEnterAnimation">@anim/slide_in_enter_micro</item>
-        <item name="wallpaperIntraCloseExitAnimation">@anim/slide_in_exit_micro</item>
-    </style>
-
-    <style name="AlertDialog.Micro" parent="AlertDialog.Material.Light">
-        <item name="fullDark">@null</item>
-        <item name="topDark">@null</item>
-        <item name="centerDark">@null</item>
-        <item name="bottomDark">@null</item>
-        <item name="fullBright">@null</item>
-        <item name="topBright">@null</item>
-        <item name="centerBright">@null</item>
-        <item name="bottomBright">@null</item>
-        <item name="bottomMedium">@null</item>
-        <item name="centerMedium">@null</item>
-        <item name="layout">@layout/alert_dialog_micro</item>
-    </style>
-
-    <style name="DialogWindowTitle.Micro">
-        <item name="maxLines">1</item>
-        <item name="scrollHorizontally">true</item>
-        <item name="textAppearance">@style/TextAppearance.Micro.DialogWindowTitle</item>
-    </style>
-
-    <style name="TextAppearance.Micro" parent="TextAppearance.Material">
-        <item name="textSize">20sp</item>
-        <item name="fontFamily">sans-serif-condensed-light</item>
-        <item name="textColor">@color/micro_text_light</item>
-    </style>
-
-    <style name="TextAppearance.Micro.DialogWindowTitle" parent="TextAppearance.Material.DialogWindowTitle">
-        <item name="textSize">20sp</item>
-        <item name="fontFamily">sans-serif-condensed-light</item>
-        <item name="textColor">@color/micro_text_light</item>
-    </style>
-
-    <style name="Widget.Micro" parent="Widget.Material" />
-
-    <style name="Widget.Micro.TextView">
-        <item name="fontFamily">sans-serif-condensed</item>
-    </style>
-
-    <style name="Widget.Micro.NumberPicker">
-        <item name="internalLayout">@layout/number_picker_with_selector_wheel_micro</item>
-        <item name="solidColor">@color/transparent</item>
-        <item name="selectionDivider">@drawable/numberpicker_selection_divider</item>
-        <item name="selectionDividerHeight">0dip</item>
-        <item name="selectionDividersDistance">104dip</item>
-        <item name="internalMinWidth">64dip</item>
-        <item name="internalMaxHeight">180dip</item>
-        <item name="virtualButtonPressedDrawable">?attr/selectableItemBackground</item>
-        <item name="descendantFocusability">blocksDescendants</item>
-    </style>
-
-</resources>
diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml
index e64942a..42dc1d1 100644
--- a/core/res/res/values/symbols.xml
+++ b/core/res/res/values/symbols.xml
@@ -373,6 +373,7 @@
   <java-symbol type="integer" name="config_immersive_mode_confirmation_panic" />
   <java-symbol type="integer" name="config_longPressOnPowerBehavior" />
   <java-symbol type="integer" name="config_longPressOnBackBehavior" />
+  <java-symbol type="integer" name="config_backPanicBehavior" />
   <java-symbol type="integer" name="config_lowMemoryKillerMinFreeKbytesAdjust" />
   <java-symbol type="integer" name="config_lowMemoryKillerMinFreeKbytesAbsolute" />
   <java-symbol type="integer" name="config_max_pan_devices" />
@@ -413,6 +414,7 @@
   <java-symbol type="dimen" name="config_viewConfigurationTouchSlop" />
   <java-symbol type="dimen" name="config_viewMinFlingVelocity" />
   <java-symbol type="dimen" name="config_viewMaxFlingVelocity" />
+  <java-symbol type="dimen" name="config_scrollFactor" />
   <java-symbol type="dimen" name="default_app_widget_padding_bottom" />
   <java-symbol type="dimen" name="default_app_widget_padding_left" />
   <java-symbol type="dimen" name="default_app_widget_padding_right" />
@@ -945,6 +947,11 @@
   <java-symbol type="string" name="wifi_available_sign_in" />
   <java-symbol type="string" name="network_available_sign_in" />
   <java-symbol type="string" name="network_available_sign_in_detailed" />
+  <java-symbol type="string" name="network_switch_metered" />
+  <java-symbol type="string" name="network_switch_metered_detail" />
+  <java-symbol type="string" name="network_switch_metered_toast" />
+  <java-symbol type="array" name="network_switch_type_name" />
+  <java-symbol type="string" name="network_switch_type_name_unknown" />
   <java-symbol type="string" name="wifi_no_internet" />
   <java-symbol type="string" name="wifi_no_internet_detailed" />
   <java-symbol type="string" name="wifi_connect_alert_title" />
@@ -1102,6 +1109,11 @@
   <java-symbol type="string" name="lockscreen_transport_pause_description" />
   <java-symbol type="string" name="config_ethernet_tcp_buffers" />
   <java-symbol type="string" name="config_wifi_tcp_buffers" />
+  <java-symbol type="string" name="config_demoModeLauncherComponent" />
+  <java-symbol type="string" name="demo_starting_message" />
+  <java-symbol type="string" name="demo_restarting_message" />
+  <java-symbol type="string" name="conference_call" />
+
 
   <java-symbol type="plurals" name="bugreport_countdown" />
   <java-symbol type="plurals" name="duration_hours" />
@@ -1135,7 +1147,6 @@
   <java-symbol type="array" name="config_telephonyHardware" />
   <java-symbol type="array" name="config_keySystemUuidMapping" />
   <java-symbol type="array" name="config_gpsParameters" />
-  <java-symbol type="array" name="config_colorTransforms" />
 
   <java-symbol type="drawable" name="default_wallpaper" />
   <java-symbol type="drawable" name="default_lock_wallpaper" />
@@ -1396,7 +1407,6 @@
   <java-symbol type="raw" name="color_fade_vert" />
   <java-symbol type="raw" name="color_fade_frag" />
   <java-symbol type="raw" name="accessibility_gestures" />
-  <java-symbol type="raw" name="incognito_mode_start_page" />
   <java-symbol type="raw" name="loaderror" />
   <java-symbol type="raw" name="nodomain" />
 
@@ -1448,6 +1458,7 @@
   <java-symbol type="anim" name="dock_top_exit" />
   <java-symbol type="anim" name="dock_bottom_enter" />
   <java-symbol type="anim" name="dock_bottom_exit" />
+  <java-symbol type="anim" name="dock_bottom_exit_keyguard" />
   <java-symbol type="anim" name="dock_left_enter" />
   <java-symbol type="anim" name="dock_left_exit" />
   <java-symbol type="anim" name="dock_right_enter" />
@@ -1583,6 +1594,7 @@
   <java-symbol type="string" name="bugreport_title" />
   <java-symbol type="string" name="faceunlock_multiple_failures" />
   <java-symbol type="string" name="global_action_power_off" />
+  <java-symbol type="string" name="global_action_restart" />
   <java-symbol type="string" name="global_actions_airplane_mode_off_status" />
   <java-symbol type="string" name="global_actions_airplane_mode_on_status" />
   <java-symbol type="string" name="global_actions_toggle_airplane_mode" />
@@ -1631,6 +1643,9 @@
   <java-symbol type="array" name="config_autoBrightnessKeyboardBacklightValues" />
   <java-symbol type="array" name="config_autoBrightnessLcdBacklightValues" />
   <java-symbol type="array" name="config_autoBrightnessLevels" />
+  <java-symbol type="array" name="config_dynamicHysteresisBrightLevels" />
+  <java-symbol type="array" name="config_dynamicHysteresisDarkLevels" />
+  <java-symbol type="array" name="config_dynamicHysteresisLuxLevels" />
   <java-symbol type="array" name="config_protectedNetworks" />
   <java-symbol type="array" name="config_statusBarIcons" />
   <java-symbol type="array" name="config_tether_bluetooth_regexs" />
@@ -1732,6 +1747,9 @@
   <java-symbol type="integer" name="config_lowBatteryWarningLevel" />
   <java-symbol type="integer" name="config_networkPolicyDefaultWarning" />
   <java-symbol type="integer" name="config_networkTransitionTimeout" />
+  <java-symbol type="integer" name="config_networkNotifySwitchType" />
+  <java-symbol type="array" name="config_networkNotifySwitches" />
+  <java-symbol type="integer" name="config_networkAvoidBadWifi" />
   <java-symbol type="integer" name="config_notificationsBatteryFullARGB" />
   <java-symbol type="integer" name="config_notificationsBatteryLedOff" />
   <java-symbol type="integer" name="config_notificationsBatteryLedOn" />
@@ -1750,6 +1768,7 @@
   <java-symbol type="integer" name="config_undockedHdmiRotation" />
   <java-symbol type="integer" name="config_virtualKeyQuietTimeMillis" />
   <java-symbol type="integer" name="config_brightness_ramp_rate_fast" />
+  <java-symbol type="integer" name="config_brightness_ramp_rate_slow" />
   <java-symbol type="layout" name="am_compat_mode_dialog" />
   <java-symbol type="layout" name="launch_warning" />
   <java-symbol type="layout" name="safe_mode" />
@@ -1854,11 +1873,11 @@
   <java-symbol type="string" name="vpn_title_long" />
   <java-symbol type="string" name="vpn_lockdown_connecting" />
   <java-symbol type="string" name="vpn_lockdown_connected" />
+  <java-symbol type="string" name="vpn_lockdown_disconnected" />
   <java-symbol type="string" name="vpn_lockdown_error" />
   <java-symbol type="string" name="vpn_lockdown_config" />
   <java-symbol type="string" name="wallpaper_binding_label" />
   <java-symbol type="style" name="Theme.Dialog.AppError" />
-  <java-symbol type="style" name="Theme.Micro.Dialog.Alert" />
   <java-symbol type="style" name="Theme.Leanback.Dialog.Alert" />
   <java-symbol type="style" name="Theme.Toast" />
   <java-symbol type="xml" name="storage_list" />
@@ -1886,6 +1905,12 @@
   <java-symbol type="string" name="config_persistentDataPackageName" />
   <java-symbol type="string" name="audit_safemode_notification" />
   <java-symbol type="string" name="audit_safemode_notification_details" />
+  <java-symbol type="string" name="reset_retail_demo_mode_title" />
+  <java-symbol type="string" name="reset_retail_demo_mode_text" />
+  <java-symbol type="string" name="demo_user_inactivity_timeout_title" />
+  <java-symbol type="string" name="demo_user_inactivity_timeout_countdown" />
+  <java-symbol type="string" name="demo_user_inactivity_timeout_left_button" />
+  <java-symbol type="string" name="demo_user_inactivity_timeout_right_button" />
 
   <java-symbol type="layout" name="resolver_list" />
   <java-symbol type="id" name="resolver_list" />
@@ -1923,6 +1948,7 @@
   <java-symbol type="anim" name="lock_screen_behind_enter_fade_in" />
   <java-symbol type="anim" name="lock_screen_wallpaper_exit" />
   <java-symbol type="anim" name="launch_task_behind_source" />
+  <java-symbol type="anim" name="wallpaper_open_exit" />
 
   <java-symbol type="bool" name="config_alwaysUseCdmaRssi" />
   <java-symbol type="dimen" name="status_bar_icon_size" />
@@ -2182,6 +2208,7 @@
   <java-symbol type="style" name="TextAppearance.Material.TimePicker.TimeLabel" />
   <java-symbol type="attr" name="seekBarPreferenceStyle" />
   <java-symbol type="style" name="Theme.DeviceDefault.Resolver" />
+  <java-symbol type="style" name="Theme.DeviceDefault.System" />
   <java-symbol type="attr" name="preferenceActivityStyle" />
   <java-symbol type="attr" name="preferenceFragmentStyle" />
   <java-symbol type="bool" name="skipHoldBeforeMerge" />
@@ -2191,6 +2218,7 @@
   <java-symbol type="bool" name="config_carrier_volte_provisioned" />
   <java-symbol type="bool" name="config_carrier_volte_tty_supported" />
   <java-symbol type="bool" name="config_device_vt_available" />
+  <java-symbol type="bool" name="config_device_respects_hold_carrier_config" />
   <java-symbol type="bool" name="config_carrier_vt_available" />
   <java-symbol type="bool" name="config_device_wfc_ims_available" />
   <java-symbol type="bool" name="config_carrier_wfc_ims_available" />
@@ -2253,6 +2281,8 @@
   <java-symbol type="string" name="prohibit_manual_network_selection_in_gobal_mode" />
   <java-symbol type="id" name="profile_button" />
 
+  <java-symbol type="array" name="config_vvmSmsFilterRegexes" />
+
   <!-- Cascading submenus -->
   <java-symbol type="dimen" name="cascading_menus_min_smallest_width" />
 
@@ -2482,6 +2512,7 @@
   <java-symbol type="menu" name="language_selection_list" />
   <java-symbol type="string" name="country_selection_title" />
   <java-symbol type="string" name="language_picker_section_all" />
+  <java-symbol type="string" name="region_picker_section_all" />
   <java-symbol type="string" name="language_picker_section_suggested" />
   <java-symbol type="string" name="language_selection_title" />
   <java-symbol type="string" name="search_language_hint" />
@@ -2546,6 +2577,7 @@
 
   <java-symbol type="color" name="notification_action_list" />
   <java-symbol type="color" name="notification_material_background_color" />
+  <java-symbol type="color" name="notification_action_list_dark" />
 
   <!-- Resolver target actions -->
   <java-symbol type="array" name="resolver_target_actions_pin" />
@@ -2606,9 +2638,86 @@
 
   <!-- Pinner Service -->
   <java-symbol type="array" name="config_defaultPinnerServiceFiles" />
+  <java-symbol type="bool" name="config_pinnerCameraApp" />
+
+  <java-symbol type="string" name="config_doubleTouchGestureEnableFile" />
 
   <java-symbol type="string" name="suspended_widget_accessibility" />
 
+  <!-- Used internally for assistant to launch activity transitions -->
+  <java-symbol type="id" name="cross_task_transition" />
+
+  <java-symbol type="id" name="button_holder" />
+
+  <java-symbol type="bool" name="config_useRoundIcon" />
+
+  <!-- For System navigation keys -->
+  <java-symbol type="bool" name="config_supportSystemNavigationKeys" />
+
   <java-symbol type="layout" name="unsupported_display_size_dialog_content" />
   <java-symbol type="string" name="unsupported_display_size_message" />
+
+  <java-symbol type="layout" name="notification_material_action_emphasized" />
+
+  <!-- Package name for the device provisioning package -->
+  <java-symbol type="string" name="config_deviceProvisioningPackage" />
+
+  <!-- Colon separated list of package names that should be granted DND access -->
+  <java-symbol type="string" name="config_defaultDndAccessPackages" />
+
+  <!-- For NetworkPolicyManagerService -->
+  <java-symbol type="string" name="config_networkOverLimitComponent" />
+  <java-symbol type="string" name="config_dataUsageSummaryComponent" />
+
+  <java-symbol type="string" name="lockscreen_storage_locked" />
+
+  <java-symbol type="string" name="global_action_emergency" />
+  <java-symbol type="string" name="config_emergency_call_number" />
+  <java-symbol type="array" name="config_emergency_mcc_codes" />
+
+  <!-- Used for MimeIconUtils. -->
+  <java-symbol type="drawable" name="ic_doc_apk" />
+  <java-symbol type="drawable" name="ic_doc_audio" />
+  <java-symbol type="drawable" name="ic_doc_certificate" />
+  <java-symbol type="drawable" name="ic_doc_codes" />
+  <java-symbol type="drawable" name="ic_doc_compressed" />
+  <java-symbol type="drawable" name="ic_doc_contact" />
+  <java-symbol type="drawable" name="ic_doc_event" />
+  <java-symbol type="drawable" name="ic_doc_font" />
+  <java-symbol type="drawable" name="ic_doc_image" />
+  <java-symbol type="drawable" name="ic_doc_pdf" />
+  <java-symbol type="drawable" name="ic_doc_presentation" />
+  <java-symbol type="drawable" name="ic_doc_spreadsheet" />
+  <java-symbol type="drawable" name="ic_doc_document" />
+  <java-symbol type="drawable" name="ic_doc_video" />
+  <java-symbol type="drawable" name="ic_doc_word" />
+  <java-symbol type="drawable" name="ic_doc_excel" />
+  <java-symbol type="drawable" name="ic_doc_powerpoint" />
+  <java-symbol type="drawable" name="ic_doc_folder" />
+  <java-symbol type="drawable" name="ic_doc_audio" />
+  <java-symbol type="drawable" name="ic_doc_image" />
+  <java-symbol type="drawable" name="ic_doc_text" />
+  <java-symbol type="drawable" name="ic_doc_video" />
+  <java-symbol type="drawable" name="ic_doc_generic" />
+
+  <java-symbol type="bool" name="config_nightDisplayAvailable" />
+  <java-symbol type="bool" name="config_allowDisablingAssistDisclosure" />
+  <java-symbol type="integer" name="config_defaultNightDisplayAutoMode" />
+  <java-symbol type="integer" name="config_defaultNightDisplayCustomStartTime" />
+  <java-symbol type="integer" name="config_defaultNightDisplayCustomEndTime" />
+
+  <!-- Default first user restrictions -->
+  <java-symbol type="array" name="config_defaultFirstUserRestrictions" />
+
+  <java-symbol type="drawable" name="ic_restart" />
+
+  <java-symbol type="drawable" name="emergency_icon" />
+
+  <java-symbol type="array" name="config_convert_to_emergency_number_map" />
+
+  <java-symbol type="array" name="config_nonBlockableNotificationPackages" />
+
+  <!-- Screen-size-dependent modes for picker dialogs. -->
+  <java-symbol type="integer" name="time_picker_mode" />
+  <java-symbol type="integer" name="date_picker_mode" />
 </resources>
diff --git a/core/res/res/values/themes.xml b/core/res/res/values/themes.xml
index a77b951..5b2522f 100644
--- a/core/res/res/values/themes.xml
+++ b/core/res/res/values/themes.xml
@@ -57,6 +57,7 @@
 
         <item name="colorPrimaryDark">@color/legacy_primary_dark</item>
         <item name="colorPrimary">@color/legacy_primary</item>
+        <item name="colorSecondary">?attr/colorPrimary</item>
         <item name="colorControlActivated">@color/legacy_control_activated</item>
         <item name="colorControlNormal">@color/legacy_control_normal</item>
         <item name="colorControlHighlight">@color/legacy_button_pressed</item>
diff --git a/core/res/res/values/themes_device_defaults.xml b/core/res/res/values/themes_device_defaults.xml
index 11bb106..0e98ade 100644
--- a/core/res/res/values/themes_device_defaults.xml
+++ b/core/res/res/values/themes_device_defaults.xml
@@ -199,25 +199,50 @@
 
         <item name="mediaRouteButtonStyle">@style/Widget.DeviceDefault.MediaRouteButton</item>
 
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+
     </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault} with no action bar -->
-    <style name="Theme.DeviceDefault.NoActionBar" parent="Theme.Material.NoActionBar"  />
+    <style name="Theme.DeviceDefault.NoActionBar" parent="Theme.Material.NoActionBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault} with no action bar and no status bar.  This theme
          sets {@link android.R.attr#windowFullscreen} to true.  -->
-    <style name="Theme.DeviceDefault.NoActionBar.Fullscreen" parent="Theme.Material.NoActionBar.Fullscreen"  />
+    <style name="Theme.DeviceDefault.NoActionBar.Fullscreen" parent="Theme.Material.NoActionBar.Fullscreen">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault} with no action bar and no status bar and
     extending in to overscan region.  This theme
     sets {@link android.R.attr#windowFullscreen} and {@link android.R.attr#windowOverscan}
     to true. -->
-    <style name="Theme.DeviceDefault.NoActionBar.Overscan" parent="Theme.Material.NoActionBar.Overscan"  />
+    <style name="Theme.DeviceDefault.NoActionBar.Overscan" parent="Theme.Material.NoActionBar.Overscan">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault} that has no title bar and translucent
          system decor.  This theme sets {@link android.R.attr#windowTranslucentStatus} and
          {@link android.R.attr#windowTranslucentNavigation} to true. -->
-    <style name="Theme.DeviceDefault.NoActionBar.TranslucentDecor" parent="Theme.Material.NoActionBar.TranslucentDecor"  />
+    <style name="Theme.DeviceDefault.NoActionBar.TranslucentDecor" parent="Theme.Material.NoActionBar.TranslucentDecor">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- DeviceDefault theme for dialog windows and activities. This changes the window to be
     floating (not fill the entire screen), and puts a frame around its contents. You can set this
@@ -231,18 +256,38 @@
 
         <item name="textAppearance">@style/TextAppearance.DeviceDefault</item>
         <item name="textAppearanceInverse">@style/TextAppearance.DeviceDefault.Inverse</item>
+
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
     </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Dialog} that has a nice minimum width for a
     regular dialog. -->
-    <style name="Theme.DeviceDefault.Dialog.MinWidth" parent="Theme.Material.Dialog.MinWidth" />
+    <style name="Theme.DeviceDefault.Dialog.MinWidth" parent="Theme.Material.Dialog.MinWidth">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Dialog} without an action bar -->
-    <style name="Theme.DeviceDefault.Dialog.NoActionBar" parent="Theme.Material.Dialog.NoActionBar" />
+    <style name="Theme.DeviceDefault.Dialog.NoActionBar" parent="Theme.Material.Dialog.NoActionBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Dialog_NoActionBar} that has a nice minimum width
     for a regular dialog. -->
-    <style name="Theme.DeviceDefault.Dialog.NoActionBar.MinWidth" parent="Theme.Material.Dialog.NoActionBar.MinWidth" />
+    <style name="Theme.DeviceDefault.Dialog.NoActionBar.MinWidth" parent="Theme.Material.Dialog.NoActionBar.MinWidth">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- Variant of Theme.DeviceDefault.Dialog that has a fixed size. -->
     <style name="Theme.DeviceDefault.Dialog.FixedSize">
@@ -262,44 +307,99 @@
 
     <!-- DeviceDefault theme for a window that will be displayed either full-screen on smaller
     screens (small, normal) or as a dialog on larger screens (large, xlarge). -->
-    <style name="Theme.DeviceDefault.DialogWhenLarge" parent="Theme.Material.DialogWhenLarge"  />
+    <style name="Theme.DeviceDefault.DialogWhenLarge" parent="Theme.Material.DialogWhenLarge">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- DeviceDefault theme for a window without an action bar that will be displayed either
     full-screen on smaller screens (small, normal) or as a dialog on larger screens (large,
     xlarge). -->
-    <style name="Theme.DeviceDefault.DialogWhenLarge.NoActionBar" parent="Theme.Material.DialogWhenLarge.NoActionBar"  />
+    <style name="Theme.DeviceDefault.DialogWhenLarge.NoActionBar" parent="Theme.Material.DialogWhenLarge.NoActionBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- DeviceDefault theme for a presentation window on a secondary display. -->
-    <style name="Theme.DeviceDefault.Dialog.Presentation" parent="Theme.Material.Dialog.Presentation" />
+    <style name="Theme.DeviceDefault.Dialog.Presentation" parent="Theme.Material.Dialog.Presentation">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- DeviceDefault theme for panel windows. This removes all extraneous window
     decorations, so you basically have an empty rectangle in which to place your content. It makes
     the window floating, with a transparent background, and turns off dimming behind the window. -->
-    <style name="Theme.DeviceDefault.Panel" parent="Theme.Material.Panel"  />
+    <style name="Theme.DeviceDefault.Panel" parent="Theme.Material.Panel">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- DeviceDefault theme for windows that want to have the user's selected wallpaper appear
     behind them. -->
-    <style name="Theme.DeviceDefault.Wallpaper" parent="Theme.Material.Wallpaper"  />
+    <style name="Theme.DeviceDefault.Wallpaper" parent="Theme.Material.Wallpaper">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- DeviceDefault theme for windows that want to have the user's selected wallpaper appear
     behind them and without an action bar. -->
-    <style name="Theme.DeviceDefault.Wallpaper.NoTitleBar" parent="Theme.Material.Wallpaper.NoTitleBar"  />
+    <style name="Theme.DeviceDefault.Wallpaper.NoTitleBar" parent="Theme.Material.Wallpaper.NoTitleBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- DeviceDefault style for input methods, which is used by the
          {@link android.inputmethodservice.InputMethodService} class.-->
-    <style name="Theme.DeviceDefault.InputMethod" parent="Theme.Material.InputMethod"  />
+    <style name="Theme.DeviceDefault.InputMethod" parent="Theme.Material.InputMethod">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- DeviceDefault style for input methods, which is used by the
          {@link android.service.voice.VoiceInteractionSession} class.-->
-    <style name="Theme.DeviceDefault.VoiceInteractionSession" parent="Theme.Material.VoiceInteractionSession" >
-
+    <style name="Theme.DeviceDefault.VoiceInteractionSession" parent="Theme.Material.VoiceInteractionSession">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
     </style>
+
     <style name="Theme.DeviceDefault.Dialog.Alert" parent="Theme.Material.Dialog.Alert">
         <item name="windowTitleStyle">@style/DialogWindowTitle.DeviceDefault</item>
+
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
     </style>
 
-    <style name="Theme.DeviceDefault.SearchBar" parent="Theme.Material.SearchBar" />
-    <style name="Theme.DeviceDefault.Dialog.NoFrame" parent="Theme.Material.Dialog.NoFrame" />
+    <style name="Theme.DeviceDefault.SearchBar" parent="Theme.Material.SearchBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
+
+    <style name="Theme.DeviceDefault.Dialog.NoFrame" parent="Theme.Material.Dialog.NoFrame">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault} with a light-colored style -->
     <style name="Theme.DeviceDefault.Light" parent="Theme.Material.Light" >
@@ -447,34 +547,63 @@
 
         <item name="mediaRouteButtonStyle">@style/Widget.DeviceDefault.Light.MediaRouteButton</item>
 
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
     </style>
 
     <!-- Variant of the DeviceDefault (light) theme that has a solid (opaque) action bar with an
     inverse color profile. -->
-    <style name="Theme.DeviceDefault.Light.DarkActionBar" parent="Theme.Material.Light.DarkActionBar" />
+    <style name="Theme.DeviceDefault.Light.DarkActionBar" parent="Theme.Material.Light.DarkActionBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_dark</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_dark</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Light} with no action bar -->
-    <style name="Theme.DeviceDefault.Light.NoActionBar" parent="Theme.Material.Light.NoActionBar"  />
+    <style name="Theme.DeviceDefault.Light.NoActionBar" parent="Theme.Material.Light.NoActionBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Light} with no action bar and no status bar.
          This theme sets {@link android.R.attr#windowFullscreen} to true.  -->
-    <style name="Theme.DeviceDefault.Light.NoActionBar.Fullscreen" parent="Theme.Material.Light.NoActionBar.Fullscreen"  />
+    <style name="Theme.DeviceDefault.Light.NoActionBar.Fullscreen" parent="Theme.Material.Light.NoActionBar.Fullscreen">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Light} with no action bar and no status bar
     and extending in to overscan region.  This theme
     sets {@link android.R.attr#windowFullscreen} and {@link android.R.attr#windowOverscan}
     to true. -->
-    <style name="Theme.DeviceDefault.Light.NoActionBar.Overscan" parent="Theme.Material.Light.NoActionBar.Overscan" />
+    <style name="Theme.DeviceDefault.Light.NoActionBar.Overscan" parent="Theme.Material.Light.NoActionBar.Overscan">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Light} that has no title bar and translucent
          system decor.  This theme sets {@link android.R.attr#windowTranslucentStatus} and
          {@link android.R.attr#windowTranslucentNavigation} to true. -->
-    <style name="Theme.DeviceDefault.Light.NoActionBar.TranslucentDecor" parent="Theme.Material.Light.NoActionBar.TranslucentDecor" />
+    <style name="Theme.DeviceDefault.Light.NoActionBar.TranslucentDecor" parent="Theme.Material.Light.NoActionBar.TranslucentDecor">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- DeviceDefault light theme for dialog windows and activities. This changes the window to be
     floating (not fill the entire screen), and puts a frame around its contents. You can set this
     theme on an activity if you would like to make an activity that looks like a Dialog.-->
-    <style name="Theme.DeviceDefault.Light.Dialog" parent="Theme.Material.Light.Dialog" >
+    <style name="Theme.DeviceDefault.Light.Dialog" parent="Theme.Material.Light.Dialog">
         <item name="windowTitleStyle">@style/DialogWindowTitle.DeviceDefault.Light</item>
         <item name="windowAnimationStyle">@style/Animation.DeviceDefault.Dialog</item>
 
@@ -483,18 +612,38 @@
 
         <item name="textAppearance">@style/TextAppearance.DeviceDefault</item>
         <item name="textAppearanceInverse">@style/TextAppearance.DeviceDefault.Inverse</item>
+
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
     </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Light_Dialog} that has a nice minimum width for a
     regular dialog. -->
-    <style name="Theme.DeviceDefault.Light.Dialog.MinWidth" parent="Theme.Material.Light.Dialog.MinWidth" />
+    <style name="Theme.DeviceDefault.Light.Dialog.MinWidth" parent="Theme.Material.Light.Dialog.MinWidth">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
      <!-- Variant of {@link #Theme_DeviceDefault_Light_Dialog} without an action bar -->
-    <style name="Theme.DeviceDefault.Light.Dialog.NoActionBar" parent="Theme.Material.Light.Dialog.NoActionBar" />
+    <style name="Theme.DeviceDefault.Light.Dialog.NoActionBar" parent="Theme.Material.Light.Dialog.NoActionBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- Variant of {@link #Theme_DeviceDefault_Light_Dialog_NoActionBar} that has a nice minimum
     width for a regular dialog. -->
-    <style name="Theme.DeviceDefault.Light.Dialog.NoActionBar.MinWidth" parent="Theme.Material.Light.Dialog.NoActionBar.MinWidth" />
+    <style name="Theme.DeviceDefault.Light.Dialog.NoActionBar.MinWidth" parent="Theme.Material.Light.Dialog.NoActionBar.MinWidth">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- Variant of Theme.DeviceDefault.Dialog that has a fixed size. -->
     <style name="Theme.DeviceDefault.Light.Dialog.FixedSize">
@@ -502,6 +651,11 @@
         <item name="windowFixedWidthMinor">@dimen/dialog_fixed_width_minor</item>
         <item name="windowFixedHeightMajor">@dimen/dialog_fixed_height_major</item>
         <item name="windowFixedHeightMinor">@dimen/dialog_fixed_height_minor</item>
+
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
     </style>
 
     <!-- Variant of Theme.DeviceDefault.Dialog.NoActionBar that has a fixed size. -->
@@ -510,33 +664,125 @@
         <item name="windowFixedWidthMinor">@dimen/dialog_fixed_width_minor</item>
         <item name="windowFixedHeightMajor">@dimen/dialog_fixed_height_major</item>
         <item name="windowFixedHeightMinor">@dimen/dialog_fixed_height_minor</item>
+
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
     </style>
 
     <!-- DeviceDefault light theme for a window that will be displayed either full-screen on smaller
     screens (small, normal) or as a dialog on larger screens (large, xlarge). -->
-    <style name="Theme.DeviceDefault.Light.DialogWhenLarge" parent="Theme.Material.Light.DialogWhenLarge"  />
+    <style name="Theme.DeviceDefault.Light.DialogWhenLarge" parent="Theme.Material.Light.DialogWhenLarge">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- DeviceDefault light theme for a window without an action bar that will be displayed either
     full-screen on smaller screens (small, normal) or as a dialog on larger screens (large,
     xlarge). -->
-    <style name="Theme.DeviceDefault.Light.DialogWhenLarge.NoActionBar" parent="Theme.Material.Light.DialogWhenLarge.NoActionBar"  />
+    <style name="Theme.DeviceDefault.Light.DialogWhenLarge.NoActionBar" parent="Theme.Material.Light.DialogWhenLarge.NoActionBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- DeviceDefault light theme for a presentation window on a secondary display. -->
-    <style name="Theme.DeviceDefault.Light.Dialog.Presentation" parent="Theme.Material.Light.Dialog.Presentation" />
+    <style name="Theme.DeviceDefault.Light.Dialog.Presentation" parent="Theme.Material.Light.Dialog.Presentation">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- DeviceDefault light theme for panel windows. This removes all extraneous window
     decorations, so you basically have an empty rectangle in which to place your content. It makes
     the window floating, with a transparent background, and turns off dimming behind the window. -->
-    <style name="Theme.DeviceDefault.Light.Panel" parent="Theme.Material.Light.Panel"  />
+    <style name="Theme.DeviceDefault.Light.Panel" parent="Theme.Material.Light.Panel">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <style name="Theme.DeviceDefault.Light.Dialog.Alert" parent="Theme.Material.Light.Dialog.Alert">
         <item name="windowTitleStyle">@style/DialogWindowTitle.DeviceDefault.Light</item>
+
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
     </style>
 
-    <style name="Theme.DeviceDefault.Light.SearchBar" parent="Theme.Material.Light.SearchBar" />
+    <style name="Theme.DeviceDefault.Light.SearchBar" parent="Theme.Material.Light.SearchBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
+
+    <style name="Theme.DeviceDefault.Light.Voice" parent="Theme.Material.Light.Voice">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- DeviceDefault theme for a window that should look like the Settings app.  -->
-    <style name="Theme.DeviceDefault.Settings" parent="Theme.Material.Settings" />
+    <style name="Theme.DeviceDefault.Settings" parent="Theme.Material.Settings">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_settings</item>
+        <item name="colorSecondary">@color/secondary_device_default_settings</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
+
+    <!-- DeviceDefault theme for a window that should use Settings theme colors but has
+         a full dark palette (instead of Light with dark action bar like
+         Theme.DeviceDefault.Settings.  -->
+    <style name="Theme.DeviceDefault.Settings.Dark" parent="Theme.Material">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_settings</item>
+        <item name="colorSecondary">@color/secondary_device_default_settings</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
+
+    <!-- Variant of {@link #Theme_DeviceDefault_Settings_Dark} with no action bar -->
+    <style name="Theme.DeviceDefault.Settings.Dark.NoActionBar" parent="Theme.Material.NoActionBar">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_settings</item>
+        <item name="colorSecondary">@color/secondary_device_default_settings</item>
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
+
+    <style name="Theme.DeviceDefault.Settings.Dialog" parent="Theme.Material.Settings.Dialog">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_settings</item>
+        <item name="colorSecondary">@color/secondary_device_default_settings</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
+
+    <style name="Theme.DeviceDefault.Settings.DialogWhenLarge" parent="Theme.Material.Settings.DialogWhenLarge">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_settings</item>
+        <item name="colorSecondary">@color/secondary_device_default_settings</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
+
+    <style name="Theme.DeviceDefault.Settings.Dialog.Alert" parent="Theme.Material.Settings.Dialog.Alert">
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_settings</item>
+        <item name="colorSecondary">@color/secondary_device_default_settings</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
 
     <!-- Theme used for the intent picker activity. -->
     <style name="Theme.DeviceDefault.Resolver" parent="Theme.Material.Light">
@@ -549,6 +795,28 @@
         <item name="colorControlActivated">?attr/colorControlHighlight</item>
         <item name="listPreferredItemPaddingStart">?attr/dialogPreferredPadding</item>
         <item name="listPreferredItemPaddingEnd">?attr/dialogPreferredPadding</item>
+
+        <!-- Color palette -->
+        <item name="colorPrimary">@color/primary_device_default_light</item>
+        <item name="colorPrimaryDark">@color/primary_dark_device_default_light</item>
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
+
+    <!-- DeviceDefault theme for the default system theme.  -->
+    <style name="Theme.DeviceDefault.System" parent="Theme.DeviceDefault.Light.DarkActionBar" />
+
+    <style name="ThemeOverlay.DeviceDefault" />
+
+    <style name="ThemeOverlay.DeviceDefault.Accent">
+        <item name="colorAccent">@color/accent_device_default_dark</item>
+    </style>
+
+    <style name="ThemeOverlay.DeviceDefault.Accent.Light">
+        <item name="colorAccent">@color/accent_device_default_light</item>
+    </style>
+
+    <style name="ThemeOverlay.DeviceDefault.Dark.ActionBar.Accent" parent="ThemeOverlay.Material.Dark.ActionBar">
+        <item name="colorAccent">@color/accent_device_default_dark</item>
     </style>
 
 </resources>
diff --git a/core/res/res/values/themes_material.xml b/core/res/res/values/themes_material.xml
index 2ea5c5e..0eb4c8d 100644
--- a/core/res/res/values/themes_material.xml
+++ b/core/res/res/values/themes_material.xml
@@ -114,13 +114,13 @@
         <item name="listPreferredItemHeightSmall">48dip</item>
         <item name="listPreferredItemHeightLarge">80dip</item>
         <item name="dropdownListPreferredItemHeight">?attr/listPreferredItemHeightSmall</item>
-        <item name="textAppearanceListItem">@style/TextAppearance.Material.Subhead</item>
-        <item name="textAppearanceListItemSmall">@style/TextAppearance.Material.Subhead</item>
-        <item name="textAppearanceListItemSecondary">@style/TextAppearance.Material.Body1</item>
+        <item name="textAppearanceListItem">@style/TextAppearance.Material.ListItem</item>
+        <item name="textAppearanceListItemSmall">@style/TextAppearance.Material.ListItem</item>
+        <item name="textAppearanceListItemSecondary">@style/TextAppearance.Material.ListItemSecondary</item>
         <item name="listPreferredItemPaddingLeft">@dimen/list_item_padding_horizontal_material</item>
         <item name="listPreferredItemPaddingRight">@dimen/list_item_padding_horizontal_material</item>
-        <item name="listPreferredItemPaddingStart">@dimen/list_item_padding_horizontal_material</item>
-        <item name="listPreferredItemPaddingEnd">@dimen/list_item_padding_horizontal_material</item>
+        <item name="listPreferredItemPaddingStart">@dimen/list_item_padding_start_material</item>
+        <item name="listPreferredItemPaddingEnd">@dimen/list_item_padding_end_material</item>
 
         <!-- @hide -->
         <item name="searchResultListItemHeight">58dip</item>
@@ -153,9 +153,9 @@
         <item name="windowBackground">?attr/colorBackground</item>
         <item name="windowClipToOutline">true</item>
         <item name="windowFrame">@null</item>
-        <item name="windowNoTitle">false</item>
+        <item name="windowNoTitle">@bool/config_windowNoTitleDefault</item>
         <item name="windowFullscreen">false</item>
-        <item name="windowOverscan">false</item>
+        <item name="windowOverscan">@bool/config_windowOverscanByDefault</item>
         <item name="windowIsFloating">false</item>
         <item name="windowContentOverlay">@null</item>
         <item name="windowShowWallpaper">false</item>
@@ -164,7 +164,7 @@
         <item name="windowTitleBackgroundStyle">@style/WindowTitleBackground.Material</item>
         <item name="windowAnimationStyle">@style/Animation.Material.Activity</item>
         <item name="windowSoftInputMode">stateUnspecified|adjustUnspecified</item>
-        <item name="windowActionBar">true</item>
+        <item name="windowActionBar">@bool/config_windowActionBarSupported</item>
         <item name="windowActionModeOverlay">false</item>
         <item name="windowDrawsSystemBarBackgrounds">true</item>
         <item name="windowActionBarFullscreenDecorLayout">@layout/screen_toolbar</item>
@@ -370,7 +370,7 @@
         <item name="timePickerStyle">@style/Widget.Material.TimePicker</item>
 
         <!-- TimePicker dialog theme -->
-        <item name="timePickerDialogTheme">?attr/dialogTheme</item>
+        <item name="timePickerDialogTheme">@style/ThemeOverlay.Material.Dialog.TimePicker</item>
 
         <!-- DatePicker style -->
         <item name="datePickerStyle">@style/Widget.Material.DatePicker</item>
@@ -475,13 +475,13 @@
         <item name="listPreferredItemHeightSmall">48dip</item>
         <item name="listPreferredItemHeightLarge">80dip</item>
         <item name="dropdownListPreferredItemHeight">?attr/listPreferredItemHeightSmall</item>
-        <item name="textAppearanceListItem">@style/TextAppearance.Material.Subhead</item>
-        <item name="textAppearanceListItemSmall">@style/TextAppearance.Material.Subhead</item>
-        <item name="textAppearanceListItemSecondary">@style/TextAppearance.Material.Body1</item>
+        <item name="textAppearanceListItem">@style/TextAppearance.Material.ListItem</item>
+        <item name="textAppearanceListItemSmall">@style/TextAppearance.Material.ListItem</item>
+        <item name="textAppearanceListItemSecondary">@style/TextAppearance.Material.ListItemSecondary</item>
         <item name="listPreferredItemPaddingLeft">@dimen/list_item_padding_horizontal_material</item>
         <item name="listPreferredItemPaddingRight">@dimen/list_item_padding_horizontal_material</item>
-        <item name="listPreferredItemPaddingStart">@dimen/list_item_padding_horizontal_material</item>
-        <item name="listPreferredItemPaddingEnd">@dimen/list_item_padding_horizontal_material</item>
+        <item name="listPreferredItemPaddingStart">@dimen/list_item_padding_start_material</item>
+        <item name="listPreferredItemPaddingEnd">@dimen/list_item_padding_end_material</item>
 
         <!-- @hide -->
         <item name="searchResultListItemHeight">58dip</item>
@@ -514,9 +514,9 @@
         <item name="windowBackground">?attr/colorBackground</item>
         <item name="windowClipToOutline">true</item>
         <item name="windowFrame">@null</item>
-        <item name="windowNoTitle">false</item>
+        <item name="windowNoTitle">@bool/config_windowNoTitleDefault</item>
         <item name="windowFullscreen">false</item>
-        <item name="windowOverscan">false</item>
+        <item name="windowOverscan">@bool/config_windowOverscanByDefault</item>
         <item name="windowIsFloating">false</item>
         <item name="windowContentOverlay">@null</item>
         <item name="windowShowWallpaper">false</item>
@@ -525,7 +525,7 @@
         <item name="windowTitleBackgroundStyle">@style/WindowTitleBackground.Material</item>
         <item name="windowAnimationStyle">@style/Animation.Material.Activity</item>
         <item name="windowSoftInputMode">stateUnspecified|adjustUnspecified</item>
-        <item name="windowActionBar">true</item>
+        <item name="windowActionBar">@bool/config_windowActionBarSupported</item>
         <item name="windowActionModeOverlay">false</item>
         <item name="windowDrawsSystemBarBackgrounds">true</item>
         <item name="windowActionBarFullscreenDecorLayout">@layout/screen_toolbar</item>
@@ -733,7 +733,7 @@
         <item name="timePickerStyle">@style/Widget.Material.Light.TimePicker</item>
 
         <!-- TimePicker dialog theme -->
-        <item name="timePickerDialogTheme">?attr/dialogTheme</item>
+        <item name="timePickerDialogTheme">@style/ThemeOverlay.Material.Dialog.TimePicker</item>
 
         <!-- DatePicker style -->
         <item name="datePickerStyle">@style/Widget.Material.Light.DatePicker</item>
@@ -864,11 +864,8 @@
         <item name="searchViewStyle">@style/Widget.Material.SearchView.ActionBar</item>
     </style>
 
-    <!-- Theme overlay that overrides window properties to display as a dialog. -->
-    <style name="ThemeOverlay.Material.Dialog">
-        <item name="colorBackgroundCacheHint">@null</item>
-        <item name="colorBackground">?attr/colorBackgroundFloating</item>
-
+    <!-- Base theme for overlay dialogs, customize the colours in the actual dialog theme. -->
+    <style name="ThemeOverlay.Material.BaseDialog">
         <item name="windowFrame">@null</item>
         <item name="windowTitleStyle">@style/DialogWindowTitle.Material</item>
         <item name="windowTitleBackgroundStyle">@style/DialogWindowTitleBackground.Material</item>
@@ -897,6 +894,17 @@
         <item name="windowFixedHeightMinor">@null</item>
     </style>
 
+    <!-- Theme overlay that overrides window properties to display as a dialog. -->
+    <style name="ThemeOverlay.Material.Dialog" parent="ThemeOverlay.Material.BaseDialog">
+        <item name="colorBackgroundCacheHint">@null</item>
+        <item name="colorBackground">?attr/colorBackgroundFloating</item>
+    </style>
+
+    <!-- Theme overlay that overrides window properties to display as a time picker dialog. -->
+    <style name="ThemeOverlay.Material.Dialog.TimePicker">
+        <item name="alertDialogStyle">@style/TimePickerDialog.Material</item>
+    </style>
+
     <!-- Theme overlay that overrides window properties to display as a date picker dialog. -->
     <style name="ThemeOverlay.Material.Dialog.DatePicker">
         <item name="alertDialogStyle">@style/DatePickerDialog.Material</item>
@@ -1090,10 +1098,10 @@
 
         <item name="colorBackgroundCacheHint">@null</item>
 
-        <item name="listPreferredItemPaddingLeft">24dip</item>
-        <item name="listPreferredItemPaddingRight">24dip</item>
-        <item name="listPreferredItemPaddingStart">24dip</item>
-        <item name="listPreferredItemPaddingEnd">24dip</item>
+        <item name="listPreferredItemPaddingLeft">?attr/dialogPreferredPadding</item>
+        <item name="listPreferredItemPaddingRight">?attr/dialogPreferredPadding</item>
+        <item name="listPreferredItemPaddingStart">?attr/dialogPreferredPadding</item>
+        <item name="listPreferredItemPaddingEnd">?attr/dialogPreferredPadding</item>
 
         <item name="listDivider">@null</item>
 
@@ -1201,10 +1209,10 @@
 
         <item name="colorBackgroundCacheHint">@null</item>
 
-        <item name="listPreferredItemPaddingLeft">24dip</item>
-        <item name="listPreferredItemPaddingRight">24dip</item>
-        <item name="listPreferredItemPaddingStart">24dip</item>
-        <item name="listPreferredItemPaddingEnd">24dip</item>
+        <item name="listPreferredItemPaddingLeft">?attr/dialogPreferredPadding</item>
+        <item name="listPreferredItemPaddingRight">?attr/dialogPreferredPadding</item>
+        <item name="listPreferredItemPaddingStart">?attr/dialogPreferredPadding</item>
+        <item name="listPreferredItemPaddingEnd">?attr/dialogPreferredPadding</item>
 
         <item name="listDivider">@null</item>
 
@@ -1300,8 +1308,9 @@
 
     <!-- Default theme for Settings and activities launched from Settings. -->
     <style name="Theme.Material.Settings" parent="Theme.Material.Light.DarkActionBar">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
 
         <item name="presentationTheme">@style/Theme.Material.Settings.Dialog.Presentation</item>
         <item name="searchDialogTheme">@style/Theme.Material.Settings.SearchBar</item>
@@ -1310,8 +1319,9 @@
 
     <!-- Default theme for Settings and activities launched from Settings. -->
     <style name="Theme.Material.Settings.NoActionBar" parent="Theme.Material.Light.NoActionBar">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
 
         <item name="presentationTheme">@style/Theme.Material.Settings.Dialog.Presentation</item>
         <item name="searchDialogTheme">@style/Theme.Material.Settings.SearchBar</item>
@@ -1319,41 +1329,48 @@
     </style>
 
     <style name="Theme.Material.Settings.BaseDialog" parent="Theme.Material.Light.BaseDialog">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
     </style>
 
     <style name="Theme.Material.Settings.Dialog" parent="Theme.Material.Settings.BaseDialog" />
 
     <style name="Theme.Material.Settings.Dialog.BaseAlert" parent="Theme.Material.Light.Dialog.BaseAlert">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
     </style>
 
     <style name="Theme.Material.Settings.Dialog.Alert" parent="Theme.Material.Settings.Dialog.BaseAlert" />
 
     <style name="Theme.Material.Settings.DialogWhenLarge" parent="Theme.Material.Light.DialogWhenLarge.DarkActionBar">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
     </style>
 
     <style name="Theme.Material.Settings.DialogWhenLarge.NoActionBar" parent="Theme.Material.Light.DialogWhenLarge.NoActionBar">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
     </style>
 
     <style name="Theme.Material.Settings.Dialog.Presentation" parent="Theme.Material.Light.Dialog.Presentation">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
     </style>
 
     <style name="Theme.Material.Settings.SearchBar" parent="Theme.Material.Light.SearchBar">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
     </style>
 
     <style name="Theme.Material.Settings.CompactMenu" parent="Theme.Material.Light.CompactMenu">
-        <item name="colorPrimary">@color/material_blue_grey_900</item>
-        <item name="colorPrimaryDark">@color/material_blue_grey_950</item>
+        <item name="colorPrimary">@color/primary_material_settings</item>
+        <item name="colorPrimaryDark">@color/primary_dark_material_settings</item>
+        <item name="colorSecondary">@color/secondary_material_settings</item>
     </style>
 </resources>
diff --git a/core/res/res/values/themes_micro.xml b/core/res/res/values/themes_micro.xml
deleted file mode 100644
index 25a6e00..0000000
--- a/core/res/res/values/themes_micro.xml
+++ /dev/null
@@ -1,100 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- Copyright (C) 2014 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-<resources>
-    <style name="Theme.MicroBase" parent="Theme.Material.NoActionBar">
-        <item name="alertDialogTheme">@style/Theme.Micro.Dialog.Alert</item>
-        <item name="alertDialogStyle">@style/AlertDialog.Micro</item>
-        <item name="dialogTheme">@style/Theme.Micro.Dialog</item>
-        <item name="textViewStyle">@style/Widget.Micro.TextView</item>
-        <item name="numberPickerStyle">@style/Widget.Micro.NumberPicker</item>
-        <item name="windowAnimationStyle">@style/Animation.Micro.Activity</item>
-        <item name="windowBackground">@color/black</item>
-        <item name="windowContentOverlay">@null</item>
-        <item name="windowIsFloating">false</item>
-        <!-- Required to force windowInsets dispatch through application UI. -->
-        <item name="windowOverscan">true</item>
-    </style>
-
-    <style name="Theme.Micro" parent="Theme.MicroBase">
-    </style>
-
-    <style name="Theme.Micro.LightBase" parent="Theme.Material.Light.NoActionBar">
-        <item name="alertDialogTheme">@style/Theme.Micro.Dialog.Alert</item>
-        <item name="alertDialogStyle">@style/AlertDialog.Micro</item>
-        <item name="dialogTheme">@style/Theme.Micro.Dialog</item>
-        <item name="textViewStyle">@style/Widget.Micro.TextView</item>
-        <item name="numberPickerStyle">@style/Widget.Micro.NumberPicker</item>
-        <item name="windowAnimationStyle">@style/Animation.Micro.Activity</item>
-        <item name="windowBackground">@color/white</item>
-        <item name="windowContentOverlay">@null</item>
-        <item name="windowIsFloating">false</item>
-        <!-- Required to force windowInsets dispatch through application UI. -->
-        <item name="windowOverscan">true</item>
-    </style>
-
-    <!-- Indirection needed for overlays to make sure there is a common base parent -->
-    <style name="Theme.Micro.Light" parent="Theme.Micro.LightBase">
-    </style>
-
-    <style name="Theme.Micro.DialogBase" parent="Theme.Material.Light.Dialog">
-        <item name="windowTitleStyle">@android:style/DialogWindowTitle.Micro</item>
-        <item name="windowIsFloating">false</item>
-        <item name="windowFullscreen">true</item>
-        <item name="textAppearance">@style/TextAppearance.Micro</item>
-        <item name="textAppearanceInverse">@style/TextAppearance.Micro</item>
-        <!-- Required to force windowInsets dispatch through application UI. -->
-        <item name="windowOverscan">true</item>
-    </style>
-
-    <!-- Indirection needed for overlays to make sure there is a common base parent -->
-    <style name="Theme.Micro.Dialog" parent="Theme.Micro.DialogBase">
-    </style>
-
-    <style name="Theme.Micro.Dialog.Alert">
-        <item name="windowTitleStyle">@style/DialogWindowTitle.Micro</item>
-        <item name="alertDialogStyle">@style/AlertDialog.Micro</item>
-        <item name="windowIsFloating">false</item>
-        <item name="windowBackground">@android:color/transparent</item>
-        <item name="windowOverscan">true</item>
-        <item name="windowContentOverlay">@null</item>
-        <item name="android:windowMinWidthMajor">@android:dimen/dialog_min_width_major</item>
-        <item name="android:windowMinWidthMinor">@android:dimen/dialog_min_width_minor</item>
-    </style>
-
-    <style name="Theme.Micro.Dialog.AppError" parent="Theme.Micro.Dialog">
-        <item name="windowBackground">@null</item>
-        <item name="alertDialogStyle">@style/AlertDialog.Micro</item>
-        <item name="windowOverscan">true</item>
-        <item name="windowCloseOnTouchOutside">false</item>
-        <item name="textSize">20sp</item>
-        <item name="fontFamily">sans-serif-condensed-light</item>
-        <item name="textColor">@color/micro_text_light</item>
-    </style>
-
-   <style name="Theme.Micro.Panel" parent="Theme.Material.Panel"  />
-   <style name="Theme.Micro.Light.Panel" parent="Theme.Material.Light.Panel"  />
-
-    <!-- Default theme for material style input methods, which is used by the
-         {@link android.inputmethodservice.InputMethodService} class.
-         This inherits from Theme.Panel, but sets up IME appropriate animations
-         and a few custom attributes. -->
-    <style name="Theme.Micro.InputMethod" parent="Theme.Micro.Panel">
-        <item name="windowAnimationStyle">@style/Animation.InputMethod</item>
-        <item name="imeFullscreenBackground">#1e282c</item>
-        <item name="imeExtractEnterAnimation">@anim/input_method_extract_enter</item>
-        <item name="imeExtractExitAnimation">@anim/input_method_extract_exit</item>
-    </style>
-</resources>
diff --git a/core/res/res/xml-watch/default_zen_mode_config.xml b/core/res/res/xml-watch/default_zen_mode_config.xml
new file mode 100644
index 0000000..938cc0c
--- /dev/null
+++ b/core/res/res/xml-watch/default_zen_mode_config.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright 2016, The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+        http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+
+<!-- Default configuration for zen mode.  See android.service.notification.ZenModeConfig. -->
+<zen version="2">
+    <!-- Allow starred contacts to go through only.
+    Repeated calls, calls, messages, reminders, events off. -->
+    <allow from="2" repeatCallers="false" calls="false" messages="false" reminders="false"
+           events="false"/>
+</zen>
diff --git a/core/tests/bluetoothtests/src/android/bluetooth/BluetoothStressTest.java b/core/tests/bluetoothtests/src/android/bluetooth/BluetoothStressTest.java
index 755e7c4..31ce95e 100644
--- a/core/tests/bluetoothtests/src/android/bluetooth/BluetoothStressTest.java
+++ b/core/tests/bluetoothtests/src/android/bluetooth/BluetoothStressTest.java
@@ -35,6 +35,7 @@
     /** The amount of time to sleep between issuing start/stop SCO in ms. */
     private static final long SCO_SLEEP_TIME = 2 * 1000;
 
+    private BluetoothAdapter mAdapter;
     private BluetoothTestUtils mTestUtils;
 
     @Override
@@ -42,13 +43,18 @@
         super.setUp();
 
         Context context = getInstrumentation().getTargetContext();
+        mAdapter = BluetoothAdapter.getDefaultAdapter();
         mTestUtils = new BluetoothTestUtils(context, TAG, OUTPUT_FILE);
+
+        // Start all tests in a disabled state.
+        if (mAdapter.isEnabled()) {
+            mTestUtils.disable(mAdapter);
+        }
     }
 
     @Override
     protected void tearDown() throws Exception {
         super.tearDown();
-
         mTestUtils.close();
     }
 
@@ -61,13 +67,10 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        mTestUtils.disable(adapter);
-
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("enable iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.enable(adapter);
-            mTestUtils.disable(adapter);
+            mTestUtils.enable(mAdapter);
+            mTestUtils.disable(mAdapter);
         }
     }
 
@@ -80,18 +83,14 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.undiscoverable(adapter);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.undiscoverable(mAdapter);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("discoverable iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.discoverable(adapter);
-            mTestUtils.undiscoverable(adapter);
+            mTestUtils.discoverable(mAdapter);
+            mTestUtils.undiscoverable(mAdapter);
         }
-
-        mTestUtils.disable(adapter);
     }
 
     /**
@@ -103,18 +102,14 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.stopScan(adapter);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.stopScan(mAdapter);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("scan iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.startScan(adapter);
-            mTestUtils.stopScan(adapter);
+            mTestUtils.startScan(mAdapter);
+            mTestUtils.stopScan(mAdapter);
         }
-
-        mTestUtils.disable(adapter);
     }
 
     /**
@@ -125,19 +120,16 @@
         if (iterations == 0) {
             return;
         }
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.disablePan(adapter);
+
+        mTestUtils.enable(mAdapter);
+        mTestUtils.disablePan(mAdapter);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("testEnablePan iteration " + (i + 1) + " of "
                     + iterations);
-            mTestUtils.enablePan(adapter);
-            mTestUtils.disablePan(adapter);
+            mTestUtils.enablePan(mAdapter);
+            mTestUtils.disablePan(mAdapter);
         }
-
-        mTestUtils.disable(adapter);
     }
 
     /**
@@ -152,19 +144,16 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.unpair(adapter, device);
+        BluetoothDevice device = mAdapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.unpair(mAdapter, device);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("pair iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.pair(adapter, device, BluetoothTestRunner.sDevicePairPasskey,
+            mTestUtils.pair(mAdapter, device, BluetoothTestRunner.sDevicePairPasskey,
                     BluetoothTestRunner.sDevicePairPin);
-            mTestUtils.unpair(adapter, device);
+            mTestUtils.unpair(mAdapter, device);
         }
-        mTestUtils.disable(adapter);
     }
 
     /**
@@ -178,19 +167,16 @@
         if (iterations == 0) {
             return;
         }
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.unpair(adapter, device);
+        BluetoothDevice device = mAdapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.unpair(mAdapter, device);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("acceptPair iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.acceptPair(adapter, device, BluetoothTestRunner.sDevicePairPasskey,
+            mTestUtils.acceptPair(mAdapter, device, BluetoothTestRunner.sDevicePairPasskey,
                     BluetoothTestRunner.sDevicePairPin);
-            mTestUtils.unpair(adapter, device);
+            mTestUtils.unpair(mAdapter, device);
         }
-        mTestUtils.disable(adapter);
     }
 
     /**
@@ -205,25 +191,22 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.pair(adapter, device, BluetoothTestRunner.sDevicePairPasskey,
+        BluetoothDevice device = mAdapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.unpair(mAdapter, device);
+        mTestUtils.pair(mAdapter, device, BluetoothTestRunner.sDevicePairPasskey,
                 BluetoothTestRunner.sDevicePairPin);
-        mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.A2DP, null);
+        mTestUtils.disconnectProfile(mAdapter, device, BluetoothProfile.A2DP, null);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("connectA2dp iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.connectProfile(adapter, device, BluetoothProfile.A2DP,
+            mTestUtils.connectProfile(mAdapter, device, BluetoothProfile.A2DP,
                     String.format("connectA2dp(device=%s)", device));
-            mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.A2DP,
+            mTestUtils.disconnectProfile(mAdapter, device, BluetoothProfile.A2DP,
                     String.format("disconnectA2dp(device=%s)", device));
         }
 
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.disable(adapter);
+        mTestUtils.unpair(mAdapter, device);
     }
 
     /**
@@ -238,25 +221,22 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.pair(adapter, device, BluetoothTestRunner.sDevicePairPasskey,
+        BluetoothDevice device = mAdapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.unpair(mAdapter, device);
+        mTestUtils.pair(mAdapter, device, BluetoothTestRunner.sDevicePairPasskey,
                 BluetoothTestRunner.sDevicePairPin);
-        mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.HEADSET, null);
+        mTestUtils.disconnectProfile(mAdapter, device, BluetoothProfile.HEADSET, null);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("connectHeadset iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.connectProfile(adapter, device, BluetoothProfile.HEADSET,
+            mTestUtils.connectProfile(mAdapter, device, BluetoothProfile.HEADSET,
                     String.format("connectHeadset(device=%s)", device));
-            mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.HEADSET,
+            mTestUtils.disconnectProfile(mAdapter, device, BluetoothProfile.HEADSET,
                     String.format("disconnectHeadset(device=%s)", device));
         }
 
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.disable(adapter);
+        mTestUtils.unpair(mAdapter, device);
     }
 
     /**
@@ -271,25 +251,22 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.pair(adapter, device, BluetoothTestRunner.sDevicePairPasskey,
+        BluetoothDevice device = mAdapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.unpair(mAdapter, device);
+        mTestUtils.pair(mAdapter, device, BluetoothTestRunner.sDevicePairPasskey,
                 BluetoothTestRunner.sDevicePairPin);
-        mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.INPUT_DEVICE, null);
+        mTestUtils.disconnectProfile(mAdapter, device, BluetoothProfile.INPUT_DEVICE, null);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("connectInput iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.connectProfile(adapter, device, BluetoothProfile.INPUT_DEVICE,
+            mTestUtils.connectProfile(mAdapter, device, BluetoothProfile.INPUT_DEVICE,
                     String.format("connectInput(device=%s)", device));
-            mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.INPUT_DEVICE,
+            mTestUtils.disconnectProfile(mAdapter, device, BluetoothProfile.INPUT_DEVICE,
                     String.format("disconnectInput(device=%s)", device));
         }
 
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.disable(adapter);
+        mTestUtils.unpair(mAdapter, device);
     }
 
     /**
@@ -304,22 +281,19 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.pair(adapter, device, BluetoothTestRunner.sDevicePairPasskey,
+        BluetoothDevice device = mAdapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.unpair(mAdapter, device);
+        mTestUtils.pair(mAdapter, device, BluetoothTestRunner.sDevicePairPasskey,
                 BluetoothTestRunner.sDevicePairPin);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("connectPan iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.connectPan(adapter, device);
-            mTestUtils.disconnectPan(adapter, device);
+            mTestUtils.connectPan(mAdapter, device);
+            mTestUtils.disconnectPan(mAdapter, device);
         }
 
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.disable(adapter);
+        mTestUtils.unpair(mAdapter, device);
     }
 
     /**
@@ -334,26 +308,23 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.disablePan(adapter);
-        mTestUtils.enablePan(adapter);
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.acceptPair(adapter, device, BluetoothTestRunner.sDevicePairPasskey,
+        BluetoothDevice device = mAdapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.disablePan(mAdapter);
+        mTestUtils.enablePan(mAdapter);
+        mTestUtils.unpair(mAdapter, device);
+        mTestUtils.acceptPair(mAdapter, device, BluetoothTestRunner.sDevicePairPasskey,
                 BluetoothTestRunner.sDevicePairPin);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("incomingPanConnection iteration " + (i + 1) + " of "
                     + iterations);
-            mTestUtils.incomingPanConnection(adapter, device);
-            mTestUtils.incomingPanDisconnection(adapter, device);
+            mTestUtils.incomingPanConnection(mAdapter, device);
+            mTestUtils.incomingPanDisconnection(mAdapter, device);
         }
 
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.disablePan(adapter);
-        mTestUtils.disable(adapter);
+        mTestUtils.unpair(mAdapter, device);
+        mTestUtils.disablePan(mAdapter);
     }
 
     /**
@@ -368,28 +339,25 @@
             return;
         }
 
-        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-        BluetoothDevice device = adapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
-        mTestUtils.disable(adapter);
-        mTestUtils.enable(adapter);
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.pair(adapter, device, BluetoothTestRunner.sDevicePairPasskey,
+        BluetoothDevice device = mAdapter.getRemoteDevice(BluetoothTestRunner.sDeviceAddress);
+        mTestUtils.enable(mAdapter);
+        mTestUtils.unpair(mAdapter, device);
+        mTestUtils.pair(mAdapter, device, BluetoothTestRunner.sDevicePairPasskey,
                 BluetoothTestRunner.sDevicePairPin);
-        mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.HEADSET, null);
-        mTestUtils.connectProfile(adapter, device, BluetoothProfile.HEADSET, null);
-        mTestUtils.stopSco(adapter, device);
+        mTestUtils.disconnectProfile(mAdapter, device, BluetoothProfile.HEADSET, null);
+        mTestUtils.connectProfile(mAdapter, device, BluetoothProfile.HEADSET, null);
+        mTestUtils.stopSco(mAdapter, device);
 
         for (int i = 0; i < iterations; i++) {
             mTestUtils.writeOutput("startStopSco iteration " + (i + 1) + " of " + iterations);
-            mTestUtils.startSco(adapter, device);
+            mTestUtils.startSco(mAdapter, device);
             sleep(SCO_SLEEP_TIME);
-            mTestUtils.stopSco(adapter, device);
+            mTestUtils.stopSco(mAdapter, device);
             sleep(SCO_SLEEP_TIME);
         }
 
-        mTestUtils.disconnectProfile(adapter, device, BluetoothProfile.HEADSET, null);
-        mTestUtils.unpair(adapter, device);
-        mTestUtils.disable(adapter);
+        mTestUtils.disconnectProfile(mAdapter, device, BluetoothProfile.HEADSET, null);
+        mTestUtils.unpair(mAdapter, device);
     }
 
     private void sleep(long time) {
diff --git a/core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java b/core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java
index 0d9980a..ee15978 100644
--- a/core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java
+++ b/core/tests/bluetoothtests/src/android/bluetooth/BluetoothTestUtils.java
@@ -35,6 +35,8 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
 
 public class BluetoothTestUtils extends Assert {
 
@@ -423,57 +425,40 @@
      * @param adapter The BT adapter.
      */
     public void enable(BluetoothAdapter adapter) {
-        int mask = (BluetoothReceiver.STATE_TURNING_ON_FLAG | BluetoothReceiver.STATE_ON_FLAG
-                | BluetoothReceiver.SCAN_MODE_CONNECTABLE_FLAG);
-        long start = -1;
-        BluetoothReceiver receiver = getBluetoothReceiver(mask);
-
-        int state = adapter.getState();
-        switch (state) {
-            case BluetoothAdapter.STATE_ON:
-                assertTrue(adapter.isEnabled());
-                removeReceiver(receiver);
-                return;
-            case BluetoothAdapter.STATE_TURNING_ON:
-                assertFalse(adapter.isEnabled());
-                mask = 0; // Don't check for received intents since we might have missed them.
-                break;
-            case BluetoothAdapter.STATE_OFF:
-                assertFalse(adapter.isEnabled());
-                start = System.currentTimeMillis();
-                assertTrue(adapter.enable());
-                break;
-            case BluetoothAdapter.STATE_TURNING_OFF:
-                start = System.currentTimeMillis();
-                assertTrue(adapter.enable());
-                break;
-            default:
-                removeReceiver(receiver);
-                fail(String.format("enable() invalid state: state=%d", state));
-        }
-
-        long s = System.currentTimeMillis();
-        while (System.currentTimeMillis() - s < ENABLE_DISABLE_TIMEOUT) {
-            state = adapter.getState();
-            if (state == BluetoothAdapter.STATE_ON
-                    && (receiver.getFiredFlags() & mask) == mask) {
-                assertTrue(adapter.isEnabled());
-                long finish = receiver.getCompletedTime();
-                if (start != -1 && finish != -1) {
-                    writeOutput(String.format("enable() completed in %d ms", (finish - start)));
-                } else {
-                    writeOutput("enable() completed");
+        writeOutput("Enabling Bluetooth adapter.");
+        assertFalse(adapter.isEnabled());
+        int btState = adapter.getState();
+        final Semaphore completionSemaphore = new Semaphore(0);
+        final BroadcastReceiver receiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                final String action = intent.getAction();
+                if (!BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
+                    return;
                 }
-                removeReceiver(receiver);
-                return;
+                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
+                        BluetoothAdapter.ERROR);
+                if (state == BluetoothAdapter.STATE_ON) {
+                    completionSemaphore.release();
+                }
             }
-            sleep(POLL_TIME);
-        }
+        };
 
-        int firedFlags = receiver.getFiredFlags();
-        removeReceiver(receiver);
-        fail(String.format("enable() timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)",
-                state, BluetoothAdapter.STATE_ON, firedFlags, mask));
+        final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
+        mContext.registerReceiver(receiver, filter);
+        assertTrue(adapter.enable());
+        boolean success = false;
+        try {
+            success = completionSemaphore.tryAcquire(ENABLE_DISABLE_TIMEOUT, TimeUnit.MILLISECONDS);
+            writeOutput(String.format("enable() completed in 0 ms"));
+        } catch (final InterruptedException e) {
+            // This should never happen but just in case it does, the test will fail anyway.
+        }
+        mContext.unregisterReceiver(receiver);
+        if (!success) {
+            fail(String.format("enable() timeout: state=%d (expected %d)", btState,
+                    BluetoothAdapter.STATE_ON));
+        }
     }
 
     /**
@@ -483,57 +468,40 @@
      * @param adapter The BT adapter.
      */
     public void disable(BluetoothAdapter adapter) {
-        int mask = (BluetoothReceiver.STATE_TURNING_OFF_FLAG | BluetoothReceiver.STATE_OFF_FLAG
-                | BluetoothReceiver.SCAN_MODE_NONE_FLAG);
-        long start = -1;
-        BluetoothReceiver receiver = getBluetoothReceiver(mask);
-
-        int state = adapter.getState();
-        switch (state) {
-            case BluetoothAdapter.STATE_OFF:
-                assertFalse(adapter.isEnabled());
-                removeReceiver(receiver);
-                return;
-            case BluetoothAdapter.STATE_TURNING_ON:
-                assertFalse(adapter.isEnabled());
-                start = System.currentTimeMillis();
-                break;
-            case BluetoothAdapter.STATE_ON:
-                assertTrue(adapter.isEnabled());
-                start = System.currentTimeMillis();
-                assertTrue(adapter.disable());
-                break;
-            case BluetoothAdapter.STATE_TURNING_OFF:
-                assertFalse(adapter.isEnabled());
-                mask = 0; // Don't check for received intents since we might have missed them.
-                break;
-            default:
-                removeReceiver(receiver);
-                fail(String.format("disable() invalid state: state=%d", state));
-        }
-
-        long s = System.currentTimeMillis();
-        while (System.currentTimeMillis() - s < ENABLE_DISABLE_TIMEOUT) {
-            state = adapter.getState();
-            if (state == BluetoothAdapter.STATE_OFF
-                    && (receiver.getFiredFlags() & mask) == mask) {
-                assertFalse(adapter.isEnabled());
-                long finish = receiver.getCompletedTime();
-                if (start != -1 && finish != -1) {
-                    writeOutput(String.format("disable() completed in %d ms", (finish - start)));
-                } else {
-                    writeOutput("disable() completed");
+        writeOutput("Disabling Bluetooth adapter.");
+        assertTrue(adapter.isEnabled());
+        int btState = adapter.getState();
+        final Semaphore completionSemaphore = new Semaphore(0);
+        final BroadcastReceiver receiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                final String action = intent.getAction();
+                if (!BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
+                    return;
                 }
-                removeReceiver(receiver);
-                return;
+                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
+                        BluetoothAdapter.ERROR);
+                if (state == BluetoothAdapter.STATE_OFF) {
+                    completionSemaphore.release();
+                }
             }
-            sleep(POLL_TIME);
-        }
+        };
 
-        int firedFlags = receiver.getFiredFlags();
-        removeReceiver(receiver);
-        fail(String.format("disable() timeout: state=%d (expected %d), flags=0x%x (expected 0x%x)",
-                state, BluetoothAdapter.STATE_OFF, firedFlags, mask));
+        final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
+        mContext.registerReceiver(receiver, filter);
+        assertTrue(adapter.disable());
+        boolean success = false;
+        try {
+            success = completionSemaphore.tryAcquire(ENABLE_DISABLE_TIMEOUT, TimeUnit.MILLISECONDS);
+            writeOutput(String.format("disable() completed in 0 ms"));
+        } catch (final InterruptedException e) {
+            // This should never happen but just in case it does, the test will fail anyway.
+        }
+        mContext.unregisterReceiver(receiver);
+        if (!success) {
+            fail(String.format("disable() timeout: state=%d (expected %d)", btState,
+                    BluetoothAdapter.STATE_OFF));
+        }
     }
 
     /**
@@ -543,40 +511,47 @@
      * @param adapter The BT adapter.
      */
     public void discoverable(BluetoothAdapter adapter) {
-        int mask = BluetoothReceiver.SCAN_MODE_CONNECTABLE_DISCOVERABLE_FLAG;
-
         if (!adapter.isEnabled()) {
             fail("discoverable() bluetooth not enabled");
         }
 
         int scanMode = adapter.getScanMode();
-        if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
+        if (scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE) {
             return;
         }
 
-        BluetoothReceiver receiver = getBluetoothReceiver(mask);
-
-        assertEquals(BluetoothAdapter.SCAN_MODE_CONNECTABLE, scanMode);
-        long start = System.currentTimeMillis();
-        assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
-
-        while (System.currentTimeMillis() - start < DISCOVERABLE_UNDISCOVERABLE_TIMEOUT) {
-            scanMode = adapter.getScanMode();
-            if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE
-                    && (receiver.getFiredFlags() & mask) == mask) {
-                writeOutput(String.format("discoverable() completed in %d ms",
-                        (receiver.getCompletedTime() - start)));
-                removeReceiver(receiver);
-                return;
+        final Semaphore completionSemaphore = new Semaphore(0);
+        final BroadcastReceiver receiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                final String action = intent.getAction();
+                if (!BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
+                    return;
+                }
+                final int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,
+                        BluetoothAdapter.SCAN_MODE_NONE);
+                if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
+                    completionSemaphore.release();
+                }
             }
-            sleep(POLL_TIME);
-        }
+        };
 
-        int firedFlags = receiver.getFiredFlags();
-        removeReceiver(receiver);
-        fail(String.format("discoverable() timeout: scanMode=%d (expected %d), flags=0x%x "
-                + "(expected 0x%x)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE,
-                firedFlags, mask));
+        final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
+        mContext.registerReceiver(receiver, filter);
+        assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
+        boolean success = false;
+        try {
+            success = completionSemaphore.tryAcquire(DISCOVERABLE_UNDISCOVERABLE_TIMEOUT,
+                    TimeUnit.MILLISECONDS);
+            writeOutput(String.format("discoverable() completed in 0 ms"));
+        } catch (final InterruptedException e) {
+            // This should never happen but just in case it does, the test will fail anyway.
+        }
+        mContext.unregisterReceiver(receiver);
+        if (!success) {
+            fail(String.format("discoverable() timeout: scanMode=%d (expected %d)", scanMode,
+                    BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
+        }
     }
 
     /**
@@ -586,40 +561,47 @@
      * @param adapter The BT adapter.
      */
     public void undiscoverable(BluetoothAdapter adapter) {
-        int mask = BluetoothReceiver.SCAN_MODE_CONNECTABLE_FLAG;
-
         if (!adapter.isEnabled()) {
             fail("undiscoverable() bluetooth not enabled");
         }
 
         int scanMode = adapter.getScanMode();
-        if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE) {
+        if (scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
             return;
         }
 
-        BluetoothReceiver receiver = getBluetoothReceiver(mask);
-
-        assertEquals(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, scanMode);
-        long start = System.currentTimeMillis();
-        assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE));
-
-        while (System.currentTimeMillis() - start < DISCOVERABLE_UNDISCOVERABLE_TIMEOUT) {
-            scanMode = adapter.getScanMode();
-            if (scanMode == BluetoothAdapter.SCAN_MODE_CONNECTABLE
-                    && (receiver.getFiredFlags() & mask) == mask) {
-                writeOutput(String.format("undiscoverable() completed in %d ms",
-                        (receiver.getCompletedTime() - start)));
-                removeReceiver(receiver);
-                return;
+        final Semaphore completionSemaphore = new Semaphore(0);
+        final BroadcastReceiver receiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                final String action = intent.getAction();
+                if (!BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
+                    return;
+                }
+                final int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE,
+                        BluetoothAdapter.SCAN_MODE_NONE);
+                if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE) {
+                    completionSemaphore.release();
+                }
             }
-            sleep(POLL_TIME);
-        }
+        };
 
-        int firedFlags = receiver.getFiredFlags();
-        removeReceiver(receiver);
-        fail(String.format("undiscoverable() timeout: scanMode=%d (expected %d), flags=0x%x "
-                + "(expected 0x%x)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE, firedFlags,
-                mask));
+        final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
+        mContext.registerReceiver(receiver, filter);
+        assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE));
+        boolean success = false;
+        try {
+            success = completionSemaphore.tryAcquire(DISCOVERABLE_UNDISCOVERABLE_TIMEOUT,
+                    TimeUnit.MILLISECONDS);
+            writeOutput(String.format("undiscoverable() completed in 0 ms"));
+        } catch (InterruptedException e) {
+            // This should never happen but just in case it does, the test will fail anyway.
+        }
+        mContext.unregisterReceiver(receiver);
+        if (!success) {
+            fail(String.format("undiscoverable() timeout: scanMode=%d (expected %d)", scanMode,
+                    BluetoothAdapter.SCAN_MODE_CONNECTABLE));
+        }
     }
 
     /**
diff --git a/core/tests/coretests/AndroidManifest.xml b/core/tests/coretests/AndroidManifest.xml
index 2452cfd..4416402 100644
--- a/core/tests/coretests/AndroidManifest.xml
+++ b/core/tests/coretests/AndroidManifest.xml
@@ -1147,6 +1147,8 @@
         </activity>
         <activity android:name="com.android.internal.policy.PhoneWindowActionModeTestActivity">
         </activity>
+        <activity android:name="android.app.EmptyActivity">
+        </activity>
 
         <receiver android:name="android.app.activity.AbortReceiver">
             <intent-filter android:priority="1">
diff --git a/core/tests/coretests/src/android/app/ApplicationErrorReportTest.java b/core/tests/coretests/src/android/app/ApplicationErrorReportTest.java
new file mode 100644
index 0000000..19a390a
--- /dev/null
+++ b/core/tests/coretests/src/android/app/ApplicationErrorReportTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.app;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import android.app.ApplicationErrorReport.CrashInfo;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(AndroidJUnit4.class)
+@SmallTest
+public class ApplicationErrorReportTest {
+
+    @Test
+    public void testHugeStacktraceLeadsToReasonableReport() {
+        Throwable deepStackTrace = deepStackTrace();
+        CrashInfo crashInfo = new CrashInfo(deepStackTrace);
+
+        assertTrue("stack trace is longer than 50'000 characters",
+                crashInfo.stackTrace.length() < 50000);
+    }
+
+    @Test
+    public void testHugeExceptionMessageLeadsToReasonableReport() {
+        StringBuilder msg = new StringBuilder();
+        for (int i = 0; i < 1000000; i++) {
+            msg.append('x');
+        }
+
+        CrashInfo crashInfo = new CrashInfo(new Throwable(msg.toString()));
+
+        assertTrue("message is longer than 50'000 characters",
+                crashInfo.exceptionMessage.length() < 50000);
+    }
+
+    @Test
+    public void testTruncationKeepsStartAndEndIntact() {
+        StringBuilder msg = new StringBuilder("start");
+        for (int i = 0; i < 1000000; i++) {
+            msg.append('x');
+        }
+        msg.append("end");
+
+        CrashInfo crashInfo = new CrashInfo(new Throwable(msg.toString()));
+
+        String exceptionMessage = crashInfo.exceptionMessage;
+        assertEquals("start", exceptionMessage.substring(0, "start".length()));
+        assertEquals("end", exceptionMessage.substring(exceptionMessage.length() - "end".length()));
+    }
+
+    /**
+     * @return a Throwable with a very long stack trace.
+     */
+    private Throwable deepStackTrace() {
+        return stackTraceGenerator__aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa(1000);
+    }
+
+    private Throwable stackTraceGenerator__aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa(
+            int d) {
+        if (d > 0) {
+            return stackTraceGenerator__aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa_aaaaaaaaa(d - 1);
+        } else {
+            return new Throwable("here");
+        }
+    }
+}
diff --git a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl b/core/tests/coretests/src/android/app/EmptyActivity.java
similarity index 73%
copy from telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
copy to core/tests/coretests/src/android/app/EmptyActivity.java
index b7e78d1..fefd7b7 100644
--- a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
+++ b/core/tests/coretests/src/android/app/EmptyActivity.java
@@ -1,11 +1,11 @@
 /*
- * Copyright 2016, The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ *      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,9 +14,8 @@
  * limitations under the License.
  */
 
-package android.telecom;
 
-/**
- * {@hide}
- */
-parcelable ParcelableCallAnalytics;
+package android.app;
+
+public class EmptyActivity extends Activity {
+}
diff --git a/core/tests/coretests/src/android/app/LoaderLifecycleTest.java b/core/tests/coretests/src/android/app/LoaderLifecycleTest.java
new file mode 100644
index 0000000..1850d57
--- /dev/null
+++ b/core/tests/coretests/src/android/app/LoaderLifecycleTest.java
@@ -0,0 +1,228 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package android.app;
+
+import android.content.Context;
+import android.os.Handler;
+import android.os.Parcelable;
+import android.support.test.filters.MediumTest;
+import android.support.test.rule.ActivityTestRule;
+import android.support.test.runner.AndroidJUnit4;
+import android.util.ArrayMap;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static junit.framework.TestCase.assertNotNull;
+import static junit.framework.TestCase.assertNotSame;
+import static junit.framework.TestCase.assertSame;
+
+@RunWith(AndroidJUnit4.class)
+public class LoaderLifecycleTest {
+    @Rule
+    public ActivityTestRule<EmptyActivity> mActivityRule =
+            new ActivityTestRule<>(EmptyActivity.class);
+    @Test
+    @MediumTest
+    public void loaderIdentityTest() throws Throwable{
+        mActivityRule.runOnUiThread(() -> {
+            final Handler h = new Handler();
+            final FragmentController fc1 = FragmentController.createController(
+                    new TestFragmentHostCallback(mActivityRule.getActivity(), h, 0));
+
+            fc1.attachHost(null);
+            fc1.dispatchCreate();
+
+            final FragmentManager fm1 = fc1.getFragmentManager();
+
+            final Fragment f1 = new Fragment();
+            fm1.beginTransaction().add(f1, "one").commitNow();
+
+            // Removing and re-adding a fragment completely will destroy its LoaderManager.
+            // Keep the first one here to confirm this later.
+            final LoaderManager lm1 = f1.getLoaderManager();
+
+            // Remove the fragment, add a second one, and re-add the first to
+            // force its internal index to change. The tests below should still remain consistent.
+            final Fragment f2 = new Fragment();
+            fm1.beginTransaction().remove(f1).commitNow();
+            fm1.beginTransaction().add(f2, "two").commitNow();
+            fm1.beginTransaction().add(f1, "one").commitNow();
+
+            // We'll check this to see if we get the same instance back later
+            // as passed through NonConfigurationInstance. If the keys stay consistent
+            // across fragment remove/re-add, this will be consistent.
+            final LoaderManager lm12 = f1.getLoaderManager();
+
+            assertNotSame("fully removed and re-added fragment got same LoaderManager", lm1, lm12);
+
+            fc1.dispatchActivityCreated();
+            fc1.noteStateNotSaved();
+            fc1.execPendingActions();
+            fc1.doLoaderStart();
+            fc1.dispatchStart();
+            fc1.reportLoaderStart();
+            fc1.dispatchResume();
+            fc1.execPendingActions();
+
+            // Bring the state back down to destroyed, simulating an activity restart
+            fc1.dispatchPause();
+            final Parcelable savedState = fc1.saveAllState();
+            fc1.doLoaderStop(true);
+            fc1.dispatchStop();
+            final FragmentManagerNonConfig nonconf = fc1.retainNestedNonConfig();
+
+            final ArrayMap<String, LoaderManager> loaderNonConfig = fc1.retainLoaderNonConfig();
+            assertNotNull("loaderNonConfig was null", loaderNonConfig);
+
+            fc1.dispatchDestroy();
+
+            // Create the new controller and restore state
+            final FragmentController fc2 = FragmentController.createController(
+                    new TestFragmentHostCallback(mActivityRule.getActivity(), h, 0));
+
+            final FragmentManager fm2 = fc2.getFragmentManager();
+
+            fc2.attachHost(null);
+            // Make sure nothing blows up on a null here
+            fc2.restoreLoaderNonConfig(null);
+            // for real this time
+            fc2.restoreLoaderNonConfig(loaderNonConfig);
+            fc2.restoreAllState(savedState, nonconf);
+            fc2.dispatchCreate();
+
+
+            fc2.dispatchActivityCreated();
+            fc2.noteStateNotSaved();
+            fc2.execPendingActions();
+            fc2.doLoaderStart();
+            fc2.dispatchStart();
+            fc2.reportLoaderStart();
+            fc2.dispatchResume();
+            fc2.execPendingActions();
+
+            // Test that the fragments are in the configuration we expect
+            final Fragment restoredOne = fm2.findFragmentByTag("one");
+            final LoaderManager lm2 = restoredOne.getLoaderManager();
+
+            assertSame("didn't get same LoaderManager instance back", lm2, lm12);
+
+            // Bring the state back down to destroyed before we finish the test
+            fc2.dispatchPause();
+            fc2.saveAllState();
+            fc2.dispatchStop();
+            fc2.dispatchDestroy();
+        });
+    }
+
+    @Test
+    @MediumTest
+    public void backStackLoaderIdentityTest() throws Throwable{
+        mActivityRule.runOnUiThread(() -> {
+            final Handler h = new Handler();
+            final FragmentHostCallback host1 =
+                    new TestFragmentHostCallback(mActivityRule.getActivity(), h, 0);
+            final FragmentController fc1 = FragmentController.createController(host1);
+
+            fc1.attachHost(null);
+            fc1.dispatchCreate();
+
+            final FragmentManager fm1 = fc1.getFragmentManager();
+
+            final Fragment f1 = new Fragment();
+            fm1.beginTransaction().add(f1, "one").commitNow();
+
+            final LoaderManager lm1 = f1.getLoaderManager();
+
+            // Put the fragment on the back stack.
+            fm1.beginTransaction().remove(f1).addToBackStack("backentry").commit();
+            fm1.executePendingTransactions();
+
+            fc1.dispatchActivityCreated();
+            fc1.noteStateNotSaved();
+            fc1.execPendingActions();
+            fc1.doLoaderStart();
+            fc1.dispatchStart();
+            fc1.reportLoaderStart();
+            fc1.dispatchResume();
+            fc1.execPendingActions();
+
+            // Bring the state back down to destroyed, simulating an activity restart
+            fc1.dispatchPause();
+            final Parcelable savedState = fc1.saveAllState();
+            fc1.doLoaderStop(true);
+            fc1.dispatchStop();
+            final FragmentManagerNonConfig nonconf = fc1.retainNestedNonConfig();
+
+            final ArrayMap<String, LoaderManager> loaderNonConfig = fc1.retainLoaderNonConfig();
+            assertNotNull("loaderNonConfig was null", loaderNonConfig);
+
+            fc1.dispatchDestroy();
+
+            // Create the new controller and restore state
+            final FragmentHostCallback host2 =
+                    new TestFragmentHostCallback(mActivityRule.getActivity(), h, 0);
+            final FragmentController fc2 = FragmentController.createController(host2);
+
+            final FragmentManager fm2 = fc2.getFragmentManager();
+
+            fc2.attachHost(null);
+            fc2.restoreLoaderNonConfig(loaderNonConfig);
+            fc2.restoreAllState(savedState, nonconf);
+            fc2.dispatchCreate();
+
+
+            fc2.dispatchActivityCreated();
+            fc2.noteStateNotSaved();
+            fc2.execPendingActions();
+            fc2.doLoaderStart();
+            fc2.dispatchStart();
+            fc2.reportLoaderStart();
+            fc2.dispatchResume();
+            fc2.execPendingActions();
+
+            assertNotSame("LoaderManager kept reference to old FragmentHostCallback",
+                    host1, lm1.getFragmentHostCallback());
+            assertSame("LoaderManager did not refrence new FragmentHostCallback",
+                    host2, lm1.getFragmentHostCallback());
+
+            // Test that the fragments are in the configuration we expect
+            final Fragment restoredOne = fm2.findFragmentByTag("one");
+            final LoaderManager lm2 = restoredOne.getLoaderManager();
+
+            assertSame("didn't get same LoaderManager instance back", lm2, lm1);
+
+            // Bring the state back down to destroyed before we finish the test
+            fc2.dispatchPause();
+            fc2.saveAllState();
+            fc2.dispatchStop();
+            fc2.dispatchDestroy();
+        });
+    }
+
+    public class TestFragmentHostCallback extends FragmentHostCallback<LoaderLifecycleTest> {
+        public TestFragmentHostCallback(Context context, Handler handler, int windowAnimations) {
+            super(context, handler, windowAnimations);
+        }
+
+        @Override
+        public LoaderLifecycleTest onGetHost() {
+            return LoaderLifecycleTest.this;
+        }
+    }
+}
diff --git a/core/tests/coretests/src/android/os/FileUtilsTest.java b/core/tests/coretests/src/android/os/FileUtilsTest.java
index ac5abad..bd90079 100644
--- a/core/tests/coretests/src/android/os/FileUtilsTest.java
+++ b/core/tests/coretests/src/android/os/FileUtilsTest.java
@@ -297,6 +297,20 @@
                 FileUtils.buildUniqueFile(mTarget, "image/jpeg", "test.jpg"));
     }
 
+    public void testBuildUniqueFile_mimeless() throws Exception {
+        assertNameEquals("test.jpg", FileUtils.buildUniqueFile(mTarget, "test.jpg"));
+        new File(mTarget, "test.jpg").createNewFile();
+        assertNameEquals("test (1).jpg", FileUtils.buildUniqueFile(mTarget, "test.jpg"));
+
+        assertNameEquals("test", FileUtils.buildUniqueFile(mTarget, "test"));
+        new File(mTarget, "test").createNewFile();
+        assertNameEquals("test (1)", FileUtils.buildUniqueFile(mTarget, "test"));
+
+        assertNameEquals("test.foo.bar", FileUtils.buildUniqueFile(mTarget, "test.foo.bar"));
+        new File(mTarget, "test.foo.bar").createNewFile();
+        assertNameEquals("test.foo (1).bar", FileUtils.buildUniqueFile(mTarget, "test.foo.bar"));
+    }
+
     private static void assertNameEquals(String expected, File actual) {
         assertEquals(expected, actual.getName());
     }
diff --git a/core/tests/coretests/src/android/os/OsTests.java b/core/tests/coretests/src/android/os/OsTests.java
index 582bf1a..985fa4f 100644
--- a/core/tests/coretests/src/android/os/OsTests.java
+++ b/core/tests/coretests/src/android/os/OsTests.java
@@ -32,6 +32,7 @@
         suite.addTestSuite(IdleHandlerTest.class);
         suite.addTestSuite(MessageQueueTest.class);
         suite.addTestSuite(MessengerTest.class);
+        suite.addTestSuite(PatternMatcherTest.class);
         suite.addTestSuite(SystemPropertiesTest.class);
 
         return suite;
diff --git a/core/tests/coretests/src/android/os/PatternMatcherTest.java b/core/tests/coretests/src/android/os/PatternMatcherTest.java
new file mode 100644
index 0000000..9645ccc
--- /dev/null
+++ b/core/tests/coretests/src/android/os/PatternMatcherTest.java
@@ -0,0 +1,234 @@
+package android.os;
+
+import android.test.suitebuilder.annotation.SmallTest;
+import junit.framework.TestCase;
+import org.junit.runner.RunWith;
+import org.junit.Test;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+@SmallTest
+public class PatternMatcherTest extends TestCase{
+
+    @Test
+    public void testAdvancedPatternMatchesAnyToken() {
+        PatternMatcher matcher = new PatternMatcher(".", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches("a", matcher);
+        assertMatches("b", matcher);
+        assertNotMatches("", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesSetToken() {
+        PatternMatcher matcher = new PatternMatcher("[a]", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches("a", matcher);
+        assertNotMatches("b", matcher);
+
+        matcher = new PatternMatcher("[.*+{}\\]\\\\[]", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches(".", matcher);
+        assertMatches("*", matcher);
+        assertMatches("+", matcher);
+        assertMatches("{", matcher);
+        assertMatches("}", matcher);
+        assertMatches("]", matcher);
+        assertMatches("\\", matcher);
+        assertMatches("[", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesSetCharacterClassToken() {
+        PatternMatcher matcher = new PatternMatcher("[a-z]", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches("a", matcher);
+        assertMatches("b", matcher);
+        assertNotMatches("A", matcher);
+        assertNotMatches("1", matcher);
+
+        matcher = new PatternMatcher("[a-z][0-9]", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches("a1", matcher);
+        assertNotMatches("1a", matcher);
+        assertNotMatches("aa", matcher);
+
+        matcher = new PatternMatcher("[z-a]", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertNotMatches("a", matcher);
+        assertNotMatches("z", matcher);
+        assertNotMatches("A", matcher);
+
+        matcher = new PatternMatcher("[^0-9]", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches("a", matcher);
+        assertMatches("z", matcher);
+        assertMatches("A", matcher);
+        assertNotMatches("9", matcher);
+        assertNotMatches("5", matcher);
+        assertNotMatches("0", matcher);
+
+        assertPoorlyFormattedPattern("[]a]");
+        matcher = new PatternMatcher("[\\[a]", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches("a", matcher);
+        assertMatches("[", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesEscapedCharacters() {
+        PatternMatcher matcher = new PatternMatcher("\\.", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches(".", matcher);
+        assertNotMatches("a", matcher);
+        assertNotMatches("1", matcher);
+
+        matcher = new PatternMatcher("a\\+", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches("a+", matcher);
+        assertNotMatches("a", matcher);
+        assertNotMatches("aaaaa", matcher);
+
+        matcher = new PatternMatcher("[\\a-\\z]", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertMatches("a", matcher);
+        assertMatches("z", matcher);
+        assertNotMatches("A", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesLiteralTokens() {
+        PatternMatcher matcher = new PatternMatcher("a", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertNotMatches("", matcher);
+        assertMatches("a", matcher);
+        assertNotMatches("z", matcher);
+
+        matcher = new PatternMatcher("az", PatternMatcher.PATTERN_ADVANCED_GLOB);
+        assertNotMatches("", matcher);
+        assertMatches("az", matcher);
+        assertNotMatches("za", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesSetZeroOrMore() {
+        PatternMatcher matcher = new PatternMatcher("[a-z]*", PatternMatcher.PATTERN_ADVANCED_GLOB);
+
+        assertMatches("", matcher);
+        assertMatches("a", matcher);
+        assertMatches("abcdefg", matcher);
+        assertNotMatches("abc1", matcher);
+        assertNotMatches("1abc", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesSetOneOrMore() {
+        PatternMatcher matcher = new PatternMatcher("[a-z]+", PatternMatcher.PATTERN_ADVANCED_GLOB);
+
+        assertNotMatches("", matcher);
+        assertMatches("a", matcher);
+        assertMatches("abcdefg", matcher);
+        assertNotMatches("abc1", matcher);
+        assertNotMatches("1abc", matcher);
+    }
+
+
+    @Test
+    public void testAdvancedPatternMatchesSingleRange() {
+        PatternMatcher matcher = new PatternMatcher("[a-z]{1}",
+                PatternMatcher.PATTERN_ADVANCED_GLOB);
+
+        assertNotMatches("", matcher);
+        assertMatches("a", matcher);
+        assertMatches("z", matcher);
+        assertNotMatches("1", matcher);
+        assertNotMatches("aa", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesFullRange() {
+        PatternMatcher matcher = new PatternMatcher("[a-z]{1,5}",
+                PatternMatcher.PATTERN_ADVANCED_GLOB);
+
+        assertNotMatches("", matcher);
+        assertMatches("a", matcher);
+        assertMatches("zazaz", matcher);
+        assertNotMatches("azazaz", matcher);
+        assertNotMatches("11111", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesPartialRange() {
+        PatternMatcher matcher = new PatternMatcher("[a-z]{3,}",
+                PatternMatcher.PATTERN_ADVANCED_GLOB);
+
+        assertNotMatches("", matcher);
+        assertMatches("aza", matcher);
+        assertMatches("zazaz", matcher);
+        assertMatches("azazazazazaz", matcher);
+        assertNotMatches("aa", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternMatchesComplexPatterns() {
+        PatternMatcher matcher = new PatternMatcher(
+                "/[0-9]{4}/[0-9]{2}/[0-9]{2}/[a-zA-Z0-9_]+\\.html",
+                PatternMatcher.PATTERN_ADVANCED_GLOB);
+
+        assertNotMatches("", matcher);
+        assertMatches("/2016/09/07/got_this_working.html", matcher);
+        assertMatches("/2016/09/07/got_this_working2.html", matcher);
+        assertNotMatches("/2016/09/07/got_this_working2dothtml", matcher);
+        assertNotMatches("/2016/9/7/got_this_working.html", matcher);
+
+        matcher = new PatternMatcher(
+                "/b*a*bar.*",
+                PatternMatcher.PATTERN_ADVANCED_GLOB);
+
+        assertMatches("/babar", matcher);
+        assertMatches("/babarfff", matcher);
+        assertMatches("/bbaabarfff", matcher);
+        assertMatches("/babar?blah", matcher);
+        assertMatches("/baaaabar?blah", matcher);
+        assertNotMatches("?bar", matcher);
+        assertNotMatches("/bar", matcher);
+        assertNotMatches("/baz", matcher);
+        assertNotMatches("/ba/bar", matcher);
+        assertNotMatches("/barf", matcher);
+        assertNotMatches("/", matcher);
+        assertNotMatches("?blah", matcher);
+    }
+
+    @Test
+    public void testAdvancedPatternPoorFormatThrowsIllegalArgumentException() {
+        assertPoorlyFormattedPattern("[a-z");
+        assertPoorlyFormattedPattern("a{,4}");
+        assertPoorlyFormattedPattern("a{0,a}");
+        assertPoorlyFormattedPattern("a{\\1, 2}");
+        assertPoorlyFormattedPattern("[]");
+        assertPoorlyFormattedPattern("a{}");
+        assertPoorlyFormattedPattern("{3,4}");
+        assertPoorlyFormattedPattern("a+{3,4}");
+        assertPoorlyFormattedPattern("*.");
+        assertPoorlyFormattedPattern(".+*");
+        assertPoorlyFormattedPattern("a{3,4");
+        assertPoorlyFormattedPattern("[a");
+        assertPoorlyFormattedPattern("abc\\");
+        assertPoorlyFormattedPattern("+.");
+
+        StringBuilder charSet = new StringBuilder("[");
+        for (int i = 0; i < 1024; i++) {
+            charSet.append('a' + (i % 26));
+        }
+        charSet.append("]");
+        assertPoorlyFormattedPattern(charSet.toString());
+    }
+
+    private void assertMatches(String string, PatternMatcher matcher) {
+        assertTrue("'" + string + "' should match '" + matcher.toString() + "'",
+                matcher.match(string));
+    }
+
+    private void assertNotMatches(String string, PatternMatcher matcher) {
+        assertTrue("'" + string + "' should not match '" + matcher.toString() + "'",
+                !matcher.match(string));
+    }
+
+    private void assertPoorlyFormattedPattern(String format) {
+        try {
+            new PatternMatcher(format, PatternMatcher.PATTERN_ADVANCED_GLOB);
+        } catch (IllegalArgumentException e) {
+            return;// expected
+        }
+
+        fail("'" + format + "' was erroneously created");
+    }
+}
diff --git a/core/tests/coretests/src/android/text/method/BackspaceTest.java b/core/tests/coretests/src/android/text/method/BackspaceTest.java
index a9fa4dd..fd686b9 100644
--- a/core/tests/coretests/src/android/text/method/BackspaceTest.java
+++ b/core/tests/coretests/src/android/text/method/BackspaceTest.java
@@ -170,10 +170,27 @@
         backspace(state, 0);
         state.assertEquals("|");
 
+        state.setByString("U+1F469 U+200D U+1F373 |");
+        backspace(state, 0);
+        state.assertEquals("|");
+
+        state.setByString("U+1F487 U+200D U+2640 |");
+        backspace(state, 0);
+        state.assertEquals("|");
+
+        state.setByString("U+1F487 U+200D U+2640 U+FE0F |");
+        backspace(state, 0);
+        state.assertEquals("|");
+
         state.setByString("U+1F468 U+200D U+2764 U+FE0F U+200D U+1F48B U+200D U+1F468 |");
         backspace(state, 0);
         state.assertEquals("|");
 
+        // Emoji modifier can be appended to the first emoji.
+        state.setByString("U+1F469 U+1F3FB U+200D U+1F4BC |");
+        backspace(state, 0);
+        state.assertEquals("|");
+
         // End with ZERO WIDTH JOINER
         state.setByString("U+1F441 U+200D |");
         backspace(state, 0);
@@ -445,13 +462,6 @@
         backspace(state, 0);
         state.assertEquals("|");
 
-        // Emoji modifier + ZERO WIDTH JOINER
-        state.setByString("U+1F466 U+1F3FB U+200D U+1F469 |");
-        backspace(state, 0);
-        state.assertEquals("U+1F466 |");
-        backspace(state, 0);
-        state.assertEquals("|");
-
         // Regional indicator symbol + Emoji modifier
         state.setByString("U+1F1FA U+1F3FB |");
         backspace(state, 0);
diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsDurationTimerTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsDurationTimerTest.java
new file mode 100644
index 0000000..a15e367
--- /dev/null
+++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsDurationTimerTest.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.android.internal.os;
+
+import android.os.BatteryStats;
+import android.os.Parcel;
+import android.support.test.filters.SmallTest;
+import android.util.Log;
+
+import junit.framework.TestCase;
+
+import org.mockito.Mockito;
+
+/**
+ * Test BatteryStatsImpl.DurationTimer.
+ *
+ * In these tests, unless otherwise commented, the time increments by
+ * 2x + 100, to make the subtraction unlikely to alias to another time.
+ */
+public class BatteryStatsDurationTimerTest extends TestCase {
+
+    @SmallTest
+    public void testStartStop() throws Exception {
+        final MockClocks clocks = new MockClocks();
+
+        final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
+        timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
+
+        final BatteryStatsImpl.DurationTimer timer = new BatteryStatsImpl.DurationTimer(clocks, 
+                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase);
+
+        // TimeBase running, timer not running: current and max are 0
+        timeBase.setRunning(true, /* uptimeUs */ 0, /* realtimeUs */ 100*1000);
+        assertFalse(timer.isRunningLocked());
+        assertEquals(0, timer.getCurrentDurationMsLocked(300));
+        assertEquals(0, timer.getMaxDurationMsLocked(301));
+
+        // Start timer: current and max advance
+        timer.startRunningLocked(700);
+        assertTrue(timer.isRunningLocked());
+        assertEquals(800, timer.getCurrentDurationMsLocked(1500));
+        assertEquals(801, timer.getMaxDurationMsLocked(1501));
+
+        // Stop timer: current resets to 0, max remains
+        timer.stopRunningLocked(3100);
+        assertFalse(timer.isRunningLocked());
+        assertEquals(0, timer.getCurrentDurationMsLocked(6300));
+        assertEquals(2400, timer.getMaxDurationMsLocked(6301));
+
+        // Start time again, but check with a short time, and make sure max doesn't
+        // increment.
+        timer.startRunningLocked(12700);
+        assertTrue(timer.isRunningLocked());
+        assertEquals(100, timer.getCurrentDurationMsLocked(12800));
+        assertEquals(2400, timer.getMaxDurationMsLocked(12801));
+
+        // And stop it again, but with a short time, and make sure it doesn't increment.
+        timer.stopRunningLocked(12900);
+        assertFalse(timer.isRunningLocked());
+        assertEquals(0, timer.getCurrentDurationMsLocked(13000));
+        assertEquals(2400, timer.getMaxDurationMsLocked(13001));
+
+        // Now start and check that the time doesn't increase if the two times are the same.
+        timer.startRunningLocked(27000);
+        assertTrue(timer.isRunningLocked());
+        assertEquals(0, timer.getCurrentDurationMsLocked(27000));
+        assertEquals(2400, timer.getMaxDurationMsLocked(27000));
+
+        // Stop the TimeBase. The values should be frozen.
+        timeBase.setRunning(false, /* uptimeUs */ 10, /* realtimeUs */ 55000*1000);
+        assertTrue(timer.isRunningLocked());
+        assertEquals(28100, timer.getCurrentDurationMsLocked(110100)); // Why 28100 and not 28000?
+        assertEquals(28100, timer.getMaxDurationMsLocked(110101));
+
+        // Start the TimeBase. The values should be the old value plus the delta
+        // between when the timer restarted and the current time
+        timeBase.setRunning(true, /* uptimeUs */ 10, /* realtimeUs */ 220100*1000);
+        assertTrue(timer.isRunningLocked());
+        assertEquals(28300, timer.getCurrentDurationMsLocked(220300)); // extra 100 from above??
+        assertEquals(28301, timer.getMaxDurationMsLocked(220301));
+    }
+
+    @SmallTest
+    public void testReset() throws Exception {
+    }
+
+    @SmallTest
+    public void testParceling() throws Exception {
+        final MockClocks clocks = new MockClocks();
+
+        final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
+        timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
+
+        final BatteryStatsImpl.DurationTimer timer = new BatteryStatsImpl.DurationTimer(clocks, 
+                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase);
+
+        // Start running on battery.
+        clocks.realtime = 100;
+        clocks.uptime = 10;
+        timeBase.setRunning(true, clocks.uptimeMillis()*1000, clocks.elapsedRealtime()*1000);
+
+        timer.startRunningLocked(300);
+
+        // Check that it did start running
+        assertEquals(400, timer.getMaxDurationMsLocked(700));
+        assertEquals(401, timer.getCurrentDurationMsLocked(701));
+
+        // Write summary
+        final Parcel summaryParcel = Parcel.obtain();
+        timer.writeSummaryFromParcelLocked(summaryParcel, 1500*1000);
+        summaryParcel.setDataPosition(0);
+
+        // Read summary
+        final BatteryStatsImpl.DurationTimer summary = new BatteryStatsImpl.DurationTimer(clocks, 
+                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase);
+        summary.startRunningLocked(3100);
+        summary.readSummaryFromParcelLocked(summaryParcel);
+        // The new one shouldn't be running, and therefore 0 for current time
+        assertFalse(summary.isRunningLocked());
+        assertEquals(0, summary.getCurrentDurationMsLocked(6300));
+        // The new one should have the max duration that we had when we wrote it
+        assertEquals(1200, summary.getMaxDurationMsLocked(6301));
+
+        // Write full
+        final Parcel fullParcel = Parcel.obtain();
+        timer.writeToParcel(fullParcel, 1500*1000);
+        fullParcel.setDataPosition(0);
+ 
+        // Read full - Should be the same as the summary as far as DurationTimer is concerned.
+        final BatteryStatsImpl.DurationTimer full = new BatteryStatsImpl.DurationTimer(clocks, 
+                null, BatteryStats.WAKE_TYPE_PARTIAL, null, timeBase, fullParcel);
+        // The new one shouldn't be running, and therefore 0 for current time
+        assertFalse(full.isRunningLocked());
+        assertEquals(0, full.getCurrentDurationMsLocked(6300));
+        // The new one should have the max duration that we had when we wrote it
+        assertEquals(1200, full.getMaxDurationMsLocked(6301));
+    }
+}
diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsSamplingTimerTest.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsSamplingTimerTest.java
index ce6879d..b4afdda 100644
--- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsSamplingTimerTest.java
+++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsSamplingTimerTest.java
@@ -178,19 +178,40 @@
                 clocks.elapsedRealtime() * 1000);
         offBatterySummaryParcel.setDataPosition(0);
 
-        // Read the on battery summary from the parcel.
-        BatteryStatsImpl.SamplingTimer unparceledTimer = new BatteryStatsImpl.SamplingTimer(
-                clocks, timeBase);
-        unparceledTimer.readSummaryFromParcelLocked(onBatterySummaryParcel);
+        // Set the timebase running again. That way any issues with tracking reported values
+        // get tested when we unparcel the timers below.
+        timeBase.setRunning(true, clocks.uptimeMillis(), clocks.elapsedRealtime());
 
-        assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED));
-        assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
+        // Read the on battery summary from the parcel.
+        BatteryStatsImpl.SamplingTimer unparceledOnBatteryTimer =
+                new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
+        unparceledOnBatteryTimer.readSummaryFromParcelLocked(onBatterySummaryParcel);
+
+        assertEquals(10, unparceledOnBatteryTimer.getTotalTimeLocked(0,
+                BatteryStats.STATS_SINCE_CHARGED));
+        assertEquals(1, unparceledOnBatteryTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
 
         // Read the off battery summary from the parcel.
-        unparceledTimer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
-        unparceledTimer.readSummaryFromParcelLocked(offBatterySummaryParcel);
+        BatteryStatsImpl.SamplingTimer unparceledOffBatteryTimer =
+                new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
+        unparceledOffBatteryTimer.readSummaryFromParcelLocked(offBatterySummaryParcel);
 
-        assertEquals(10, unparceledTimer.getTotalTimeLocked(0, BatteryStats.STATS_SINCE_CHARGED));
-        assertEquals(1, unparceledTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
+        assertEquals(10, unparceledOffBatteryTimer.getTotalTimeLocked(0,
+                BatteryStats.STATS_SINCE_CHARGED));
+        assertEquals(1, unparceledOffBatteryTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
+
+        // Now, just like with a fresh timer, the first update should be absorbed to account for
+        // data being collected when we weren't recording.
+        unparceledOnBatteryTimer.update(10, 10);
+
+        assertEquals(10, unparceledOnBatteryTimer.getTotalTimeLocked(0,
+                BatteryStats.STATS_SINCE_CHARGED));
+        assertEquals(1, unparceledOnBatteryTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
+
+        unparceledOffBatteryTimer.update(10, 10);
+
+        assertEquals(10, unparceledOffBatteryTimer.getTotalTimeLocked(0,
+                BatteryStats.STATS_SINCE_CHARGED));
+        assertEquals(1, unparceledOffBatteryTimer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
     }
 }
diff --git a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
index 78bcbbc..9518219 100644
--- a/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
+++ b/core/tests/coretests/src/com/android/internal/os/BatteryStatsTests.java
@@ -5,6 +5,7 @@
 
 @RunWith(Suite.class)
 @Suite.SuiteClasses({
+        BatteryStatsDurationTimerTest.class,
         BatteryStatsSamplingTimerTest.class,
         BatteryStatsServTest.class,
         BatteryStatsTimeBaseTest.class,
diff --git a/data/sounds/alarms/material/ogg/Bounce_OG7_1ch_48k.ogg b/data/sounds/alarms/material/ogg/Bounce_OG7_1ch_48k.ogg
index 6af4d70..3d1f8ea 100755
--- a/data/sounds/alarms/material/ogg/Bounce_OG7_1ch_48k.ogg
+++ b/data/sounds/alarms/material/ogg/Bounce_OG7_1ch_48k.ogg
Binary files differ
diff --git a/docs/html/guide/topics/manifest/uses-feature-element.jd b/docs/html/guide/topics/manifest/uses-feature-element.jd
index 7db0503..843fe1c 100755
--- a/docs/html/guide/topics/manifest/uses-feature-element.jd
+++ b/docs/html/guide/topics/manifest/uses-feature-element.jd
@@ -1231,7 +1231,7 @@
     </p>
 
     <p>
-      When declared as required, this feature indicates that the app is
+      By default, your app requires this feature. This feature indicates that the app is
       compatible with a device only if that device emulates a touchscreen
       ("fake touch" interface) or has an actual touchscreen.
     </p>
@@ -1241,19 +1241,12 @@
       that emulates a subset of a touchscreen's capabilities. For example, a
       mouse or remote control could drive an on-screen cursor. If your app
       requires basic point and click interaction (in other words, it won't work
-      with only a d-pad controller), you should declare this feature. Because
+      with only a d-pad controller), you should declare this feature or simply
+      avoid declaring any {@code android.hardware.touchscreen.*} features. Because
       this is the minimum level of touch interaction, you can also use an app
       that declares this feature on devices that offer more complex touch
       interfaces.
     </p>
-
-    <p class="note">
-      <strong>Note:</strong> Apps require the {@code android.hardware.touchscreen}
-      feature by default. If you want your app to be available to devices that
-      provide a fake touch interface, you must also explicitly declare that a
-      touchscreen is not required as follows:
-    </p>
-    <pre>&lt;uses-feature android:name="android.hardware.touchscreen" <strong>android:required="false"</strong> /&gt;</pre>
   </dd>
 
   <dt>
@@ -1328,21 +1321,9 @@
     </p>
 
     <p>
-      By default, your app requires this feature. As such, your app is not
-      available to devices that provide only an emulated touch interface ("fake
-      touch") by default. If you want to make your app available on devices
-      that provide a fake touch interface (or even on devices that provide only
-      a d-pad controller), you must explicitly declare that a touchscreen is
-      not required by declaring {@code android.hardware.touchscreen} with
-      {@code android:required="false"}. You should add this declaration if your
-      app uses—but does not require—a real touchscreen interface.
-    </p>
-
-    <p>
       If your app in fact requires a touch interface (to perform more advanced
-      touch gestures such as fling), then you don't need to declare any touch
-      interface features because they're required by default. However, it's
-      best if you explicitly declare all features that your app uses.
+      touch gestures such as fling), then you must explicitly declare this feature
+      or any advanced touchscreen features.
     </p>
 
     <p>
diff --git a/docs/html/guide/topics/ui/drag-drop.jd b/docs/html/guide/topics/ui/drag-drop.jd
index 8871c87..8e4297f 100644
--- a/docs/html/guide/topics/ui/drag-drop.jd
+++ b/docs/html/guide/topics/ui/drag-drop.jd
@@ -152,7 +152,7 @@
     drag event listeners or callback methods of each View in the layout. The listeners or callback
     methods can use the metadata to decide if they want to accept the data when it is dropped.
     If the user drops the data over a View object, and that View object's listener or callback
-    method has previously told the system that it wants to accept the drop, then the system sends
+    method has previously told the system that it wants to accept the data, then the system sends
     the data to the listener or callback method in a drag event.
 </p>
 <p>
@@ -226,7 +226,8 @@
     </dt>
     <dd>
         The user releases the drag shadow within the bounding box of a View that can accept the
-        data. The system sends the View object's listener a drag event with action type
+        data, but not within its descendant view that can accept the data. The system sends the View
+        object's listener a drag event with action type
         {@link android.view.DragEvent#ACTION_DROP}. The drag event contains the data that was
         passed to the system in the call to
         {@link android.view.View#startDrag(ClipData,View.DragShadowBuilder,Object,int) startDrag()}
@@ -317,6 +318,10 @@
             application calls
 {@link android.view.View#startDrag(ClipData,View.DragShadowBuilder,Object,int) startDrag()} and
             gets a drag shadow.
+            <p>
+                If the listener wants to continue receiving drag events for this operation, it must
+                return boolean <code>true</code> to the system.
+            </p>
         </td>
     </tr>
     <tr>
@@ -324,9 +329,7 @@
         <td>
             A View object's drag event listener receives this event action type when the drag shadow
             has just entered the bounding box of the View. This is the first event action type the
-            listener receives when the drag shadow enters the bounding box. If the listener wants to
-            continue receiving drag events for this operation, it must return boolean
-            <code>true</code> to the system.
+            listener receives when the drag shadow enters the bounding box.
         </td>
     </tr>
     <tr>
@@ -334,7 +337,8 @@
         <td>
             A View object's drag event listener receives this event action type after it receives a
             {@link android.view.DragEvent#ACTION_DRAG_ENTERED} event while the drag shadow is
-            still within the bounding box of the View.
+            still within the bounding box of the View and not within a descendant view that can
+            accept the data.
         </td>
     </tr>
     <tr>
@@ -343,7 +347,8 @@
             A View object's drag event listener receives this event action type after it receives a
             {@link android.view.DragEvent#ACTION_DRAG_ENTERED} and at least one
             {@link android.view.DragEvent#ACTION_DRAG_LOCATION} event, and after the user has moved
-            the drag shadow outside the bounding box of the View.
+            the drag shadow outside the bounding box of the View or into a descendant view that can
+            accept the data.
         </td>
     </tr>
     <tr>
@@ -395,7 +400,7 @@
         <td style="text-align: center;">X</td>
         <td style="text-align: center;">X</td>
         <td style="text-align: center;">X</td>
-        <td style="text-align: center;">&nbsp;</td>
+        <td style="text-align: center;">X</td>
         <td style="text-align: center;">&nbsp;</td>
         <td style="text-align: center;">&nbsp;</td>
     </tr>
@@ -711,8 +716,7 @@
         If the listener can accept a drop, it should return <code>true</code>. This tells
         the system to continue to send drag events to the listener.
         If it can't accept a drop, it should return <code>false</code>, and the system
-        will stop sending drag events until it sends out
-        {@link android.view.DragEvent#ACTION_DRAG_ENDED}.
+        will stop sending drag events for the current drag operation.
     </li>
 </ol>
 <p>
@@ -754,7 +758,8 @@
     <li>
         {@link android.view.DragEvent#ACTION_DRAG_EXITED}:  This event is sent to a listener that
         previously received {@link android.view.DragEvent#ACTION_DRAG_ENTERED}, after
-        the drag shadow is no longer within the bounding box of the listener's View.
+        the drag shadow is no longer within the bounding box of the listener's View or it's within
+        the bounding box of a descendant view that can accept the data.
     </li>
 </ul>
 <p>
diff --git a/docs/html/topic/libraries/support-library/revisions.jd b/docs/html/topic/libraries/support-library/revisions.jd
index db99ec8..9a24d15 100644
--- a/docs/html/topic/libraries/support-library/revisions.jd
+++ b/docs/html/topic/libraries/support-library/revisions.jd
@@ -2974,8 +2974,6 @@
         <ul>
           <li>Added {@link android.support.v7.widget.GridLayout} to provide support for the
             {@link android.widget.GridLayout} layout object.</li>
-          <li>Added {@link android.support.v7.widget.Space} which can be used to create blank areas
-            within a {@link android.support.v7.widget.GridLayout} layout object.</li>
         </ul>
     </dl>
   </div>
diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java
index 1fdc1f5..49721cf 100644
--- a/graphics/java/android/graphics/Bitmap.java
+++ b/graphics/java/android/graphics/Bitmap.java
@@ -1667,10 +1667,10 @@
      * and therefore is harmless.
      */
     public void prepareToDraw() {
-        // TODO: Consider having this start an async upload?
-        // With inPurgeable no-op'd there's currently no use for this
-        // method, but it could have interesting future uses.
         checkRecycled("Can't prepareToDraw on a recycled bitmap!");
+        // Kick off an update/upload of the bitmap outside of the normal
+        // draw path.
+        nativePrepareToDraw(mNativePtr);
     }
 
     /**
@@ -1741,4 +1741,5 @@
     private static native void nativeSetHasMipMap(long nativeBitmap, boolean hasMipMap);
     private static native boolean nativeSameAs(long nativeBitmap0, long nativeBitmap1);
     private static native long nativeRefPixelRef(long nativeBitmap);
+    private static native void nativePrepareToDraw(long nativeBitmap);
 }
diff --git a/graphics/java/android/graphics/Canvas.java b/graphics/java/android/graphics/Canvas.java
index cb6c92e..40b877d 100644
--- a/graphics/java/android/graphics/Canvas.java
+++ b/graphics/java/android/graphics/Canvas.java
@@ -788,7 +788,7 @@
      * @return     true if the resulting is non-empty
      */
     public boolean clipPath(@NonNull Path path, @NonNull Region.Op op) {
-        return native_clipPath(mNativeCanvasWrapper, path.ni(), op.nativeInt);
+        return native_clipPath(mNativeCanvasWrapper, path.readOnlyNI(), op.nativeInt);
     }
 
     /**
@@ -907,7 +907,7 @@
      *                    does not intersect with the canvas' clip
      */
     public boolean quickReject(@NonNull Path path, @NonNull EdgeType type) {
-        return native_quickReject(mNativeCanvasWrapper, path.ni());
+        return native_quickReject(mNativeCanvasWrapper, path.readOnlyNI());
     }
 
     /**
@@ -1259,7 +1259,7 @@
         if (path.isSimplePath && path.rects != null) {
             native_drawRegion(mNativeCanvasWrapper, path.rects.mNativeRegion, paint.getNativeInstance());
         } else {
-            native_drawPath(mNativeCanvasWrapper, path.ni(), paint.getNativeInstance());
+            native_drawPath(mNativeCanvasWrapper, path.readOnlyNI(), paint.getNativeInstance());
         }
     }
 
@@ -1895,7 +1895,7 @@
             throw new ArrayIndexOutOfBoundsException();
         }
         native_drawTextOnPath(mNativeCanvasWrapper, text, index, count,
-                path.ni(), hOffset, vOffset,
+                path.readOnlyNI(), hOffset, vOffset,
                 paint.mBidiFlags, paint.getNativeInstance(), paint.mNativeTypeface);
     }
 
@@ -1915,7 +1915,7 @@
     public void drawTextOnPath(@NonNull String text, @NonNull Path path, float hOffset,
             float vOffset, @NonNull Paint paint) {
         if (text.length() > 0) {
-            native_drawTextOnPath(mNativeCanvasWrapper, text, path.ni(), hOffset, vOffset,
+            native_drawTextOnPath(mNativeCanvasWrapper, text, path.readOnlyNI(), hOffset, vOffset,
                     paint.mBidiFlags, paint.getNativeInstance(), paint.mNativeTypeface);
         }
     }
diff --git a/graphics/java/android/graphics/Paint.java b/graphics/java/android/graphics/Paint.java
index 054e29f..cfbe672 100644
--- a/graphics/java/android/graphics/Paint.java
+++ b/graphics/java/android/graphics/Paint.java
@@ -1021,7 +1021,7 @@
      *                 drawn with a hairline (width == 0)
      */
     public boolean getFillPath(Path src, Path dst) {
-        return nGetFillPath(mNativePaint, src.ni(), dst.ni());
+        return nGetFillPath(mNativePaint, src.readOnlyNI(), dst.mutateNI());
     }
 
     /**
@@ -2394,7 +2394,7 @@
             throw new ArrayIndexOutOfBoundsException();
         }
         nGetTextPath(mNativePaint, mNativeTypeface, mBidiFlags, text, index, count, x, y,
-                path.ni());
+                path.mutateNI());
     }
 
     /**
@@ -2416,7 +2416,7 @@
             throw new IndexOutOfBoundsException();
         }
         nGetTextPath(mNativePaint, mNativeTypeface, mBidiFlags, text, start, end, x, y,
-                path.ni());
+                path.mutateNI());
     }
 
     /**
diff --git a/graphics/java/android/graphics/Path.java b/graphics/java/android/graphics/Path.java
index de391af..be31bbe 100644
--- a/graphics/java/android/graphics/Path.java
+++ b/graphics/java/android/graphics/Path.java
@@ -775,7 +775,12 @@
         }
     }
 
-    final long ni() {
+    final long readOnlyNI() {
+        return mNativePath;
+    }
+
+    final long mutateNI() {
+        isSimplePath = false;
         return mNativePath;
     }
 
diff --git a/graphics/java/android/graphics/PathDashPathEffect.java b/graphics/java/android/graphics/PathDashPathEffect.java
index 4f43f68..2b6a6ed 100644
--- a/graphics/java/android/graphics/PathDashPathEffect.java
+++ b/graphics/java/android/graphics/PathDashPathEffect.java
@@ -41,7 +41,7 @@
      */
     public PathDashPathEffect(Path shape, float advance, float phase,
                               Style style) {
-        native_instance = nativeCreate(shape.ni(), advance, phase,
+        native_instance = nativeCreate(shape.readOnlyNI(), advance, phase,
                                        style.native_style);
     }
     
diff --git a/graphics/java/android/graphics/PathMeasure.java b/graphics/java/android/graphics/PathMeasure.java
index 2848949..78d892e 100644
--- a/graphics/java/android/graphics/PathMeasure.java
+++ b/graphics/java/android/graphics/PathMeasure.java
@@ -50,7 +50,7 @@
     public PathMeasure(Path path, boolean forceClosed) {
         // The native implementation does not copy the path, prevent it from being GC'd
         mPath = path;
-        native_instance = native_create(path != null ? path.ni() : 0,
+        native_instance = native_create(path != null ? path.readOnlyNI() : 0,
                                         forceClosed);
     }
 
@@ -60,7 +60,7 @@
     public void setPath(Path path, boolean forceClosed) {
         mPath = path;
         native_setPath(native_instance,
-                       path != null ? path.ni() : 0,
+                       path != null ? path.readOnlyNI() : 0,
                        forceClosed);
     }
 
@@ -134,8 +134,7 @@
             return false;
         }
 
-        dst.isSimplePath = false;
-        return native_getSegment(native_instance, startD, stopD, dst.ni(), startWithMoveTo);
+        return native_getSegment(native_instance, startD, stopD, dst.mutateNI(), startWithMoveTo);
     }
 
     /**
diff --git a/graphics/java/android/graphics/Picture.java b/graphics/java/android/graphics/Picture.java
index 28d8690..08eeaff 100644
--- a/graphics/java/android/graphics/Picture.java
+++ b/graphics/java/android/graphics/Picture.java
@@ -21,11 +21,14 @@
 
 /**
  * A Picture records drawing calls (via the canvas returned by beginRecording)
- * and can then play them back into Canvas (via {@link Picture#draw(Canvas)} or 
+ * and can then play them back into Canvas (via {@link Picture#draw(Canvas)} or
  * {@link Canvas#drawPicture(Picture)}).For most content (e.g. text, lines, rectangles),
  * drawing a sequence from a picture can be faster than the equivalent API
  * calls, since the picture performs its playback without incurring any
  * method-call overhead.
+ *
+ * <p class="note"><strong>Note:</strong> Prior to API level 23 a picture cannot
+ * be replayed on a hardware accelerated canvas.</p>
  */
 public class Picture {
     private Canvas mRecordingCanvas;
@@ -135,10 +138,6 @@
      * have been persisted across device restarts are not guaranteed to decode
      * properly and are highly discouraged.
      *
-     * <p>
-     * <strong>Note:</strong> Prior to API level 23 a picture created from an
-     * input stream cannot be replayed on a hardware accelerated canvas.
-     *
      * @see #writeToStream(java.io.OutputStream)
      * @deprecated The recommended alternative is to not use writeToStream and
      * instead draw the picture into a Bitmap from which you can persist it as
@@ -155,10 +154,6 @@
      * The resulting stream is NOT to be persisted across device restarts as
      * there is no guarantee that the Picture can be successfully reconstructed.
      *
-     * <p>
-     * <strong>Note:</strong> Prior to API level 23 a picture created from an
-     * input stream cannot be replayed on a hardware accelerated canvas.
-     *
      * @see #createFromStream(java.io.InputStream)
      * @deprecated The recommended alternative is to draw the picture into a
      * Bitmap from which you can persist it as raw or compressed pixels.
diff --git a/graphics/java/android/graphics/Region.java b/graphics/java/android/graphics/Region.java
index de89ad0..dca6d9e 100644
--- a/graphics/java/android/graphics/Region.java
+++ b/graphics/java/android/graphics/Region.java
@@ -110,7 +110,7 @@
      * (with no antialiasing).
      */
     public boolean setPath(Path path, Region clip) {
-        return nativeSetPath(mNativeRegion, path.ni(), clip.mNativeRegion);
+        return nativeSetPath(mNativeRegion, path.readOnlyNI(), clip.mNativeRegion);
     }
 
     /**
@@ -155,7 +155,7 @@
      */
     public Path getBoundaryPath() {
         Path path = new Path();
-        nativeGetBoundaryPath(mNativeRegion, path.ni());
+        nativeGetBoundaryPath(mNativeRegion, path.mutateNI());
         return path;
     }
 
@@ -164,7 +164,7 @@
      * path will also be empty.
      */
     public boolean getBoundaryPath(Path path) {
-        return nativeGetBoundaryPath(mNativeRegion, path.ni());
+        return nativeGetBoundaryPath(mNativeRegion, path.mutateNI());
     }
         
     /**
diff --git a/graphics/java/android/graphics/SurfaceTexture.java b/graphics/java/android/graphics/SurfaceTexture.java
index 5c54324..c386108 100644
--- a/graphics/java/android/graphics/SurfaceTexture.java
+++ b/graphics/java/android/graphics/SurfaceTexture.java
@@ -77,6 +77,8 @@
     private long mProducer;
     private long mFrameAvailableListener;
 
+    private boolean mIsSingleBuffered;
+
     /**
      * Callback interface for being notified that a new stream frame is available.
      */
@@ -130,6 +132,7 @@
      */
     public SurfaceTexture(int texName, boolean singleBufferMode) {
         mCreatorLooper = Looper.myLooper();
+        mIsSingleBuffered = singleBufferMode;
         nativeInit(false, texName, singleBufferMode, new WeakReference<SurfaceTexture>(this));
     }
 
@@ -157,6 +160,7 @@
      */
     public SurfaceTexture(boolean singleBufferMode) {
         mCreatorLooper = Looper.myLooper();
+        mIsSingleBuffered = singleBufferMode;
         nativeInit(true, 0, singleBufferMode, new WeakReference<SurfaceTexture>(this));
     }
 
@@ -378,6 +382,14 @@
         }
     }
 
+    /**
+     * Returns true if the SurfaceTexture is single-buffered
+     * @hide
+     */
+    public boolean isSingleBuffered() {
+        return mIsSingleBuffered;
+    }
+
     private native void nativeInit(boolean isDetached, int texName,
             boolean singleBufferMode, WeakReference<SurfaceTexture> weakSelf)
             throws Surface.OutOfResourcesException;
diff --git a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java
index ee67807..c24d313 100644
--- a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java
+++ b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java
@@ -46,6 +46,7 @@
 import android.util.Log;
 import android.util.LongArray;
 import android.util.PathParser;
+import android.util.Property;
 import android.util.TimeUtils;
 import android.view.Choreographer;
 import android.view.DisplayListCanvas;
@@ -64,19 +65,36 @@
 import java.util.ArrayList;
 
 /**
- * This class uses {@link android.animation.ObjectAnimator} and
- * {@link android.animation.AnimatorSet} to animate the properties of a
- * {@link android.graphics.drawable.VectorDrawable} to create an animated drawable.
+ * This class animates properties of a {@link android.graphics.drawable.VectorDrawable} with
+ * animations defined using {@link android.animation.ObjectAnimator} or
+ * {@link android.animation.AnimatorSet}.
  * <p>
- * AnimatedVectorDrawable are normally defined as 3 separate XML files.
+ * Starting from API 25, AnimatedVectorDrawable runs on RenderThread (as opposed to on UI thread for
+ * earlier APIs). This means animations in AnimatedVectorDrawable can remain smooth even when there
+ * is heavy workload on the UI thread. Note: If the UI thread is unresponsive, RenderThread may
+ * continue animating until the UI thread is capable of pushing another frame. Therefore, it is not
+ * possible to precisely coordinate a RenderThread-enabled AnimatedVectorDrawable with UI thread
+ * animations. Additionally,
+ * {@link android.graphics.drawable.Animatable2.AnimationCallback#onAnimationEnd(Drawable)} will be
+ * called the frame after the AnimatedVectorDrawable finishes on the RenderThread.
  * </p>
  * <p>
- * First is the XML file for {@link android.graphics.drawable.VectorDrawable}.
- * Note that we allow the animation to happen on the group's attributes and path's
- * attributes, which requires they are uniquely named in this XML file. Groups
- * and paths without animations do not need names.
+ * AnimatedVectorDrawable can be defined in either <a href="#ThreeXML">three separate XML files</a>,
+ * or <a href="#OneXML">one XML</a>.
  * </p>
- * <li>Here is a simple VectorDrawable in this vectordrawable.xml file.
+ * <a name="ThreeXML"></a>
+ * <h3>Define an AnimatedVectorDrawable in three separate XML files</h3>
+ * <ul>
+ * <a name="VDExample"></a>
+ * <li><h4>XML for the VectorDrawable containing properties to be animated</h4>
+ * <p>
+ * Animations can be performed on both group and path attributes, which requires groups and paths to
+ * have unique names in the same VectorDrawable. Groups and paths without animations do not need to
+ * be named.
+ * </p>
+ * Below is an example of a VectorDrawable defined in vectordrawable.xml. This VectorDrawable is
+ * referred to by its file name (not including file suffix) in the
+ * <a href="AVDExample">AnimatedVectorDrawable XML example</a>.
  * <pre>
  * &lt;vector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  *     android:height=&quot;64dp&quot;
@@ -95,17 +113,20 @@
  *     &lt;/group&gt;
  * &lt;/vector&gt;
  * </pre></li>
+ *
+ * <a name="AVDExample"></a>
+ * <li><h4>XML for AnimatedVectorDrawable</h4>
  * <p>
- * Second is the AnimatedVectorDrawable's XML file, which defines the target
- * VectorDrawable, the target paths and groups to animate, the properties of the
- * path and group to animate and the animations defined as the ObjectAnimators
- * or AnimatorSets.
+ * An AnimatedVectorDrawable element has a VectorDrawable attribute, and one or more target
+ * element(s). The target elements can be the path or group to be animated. Each target element
+ * contains a name attribute that references a property (of a path or a group) to animate, and an
+ * animation attribute that points to an ObjectAnimator or an AnimatorSet.
  * </p>
- * <li>Here is a simple AnimatedVectorDrawable defined in this avd.xml file.
- * Note how we use the names to refer to the groups and paths in the vectordrawable.xml.
+ * The following code sample defines an AnimatedVectorDrawable. Note that the names refer to the
+ * groups and paths in the <a href="#VDExample">VectorDrawable XML above</a>.
  * <pre>
  * &lt;animated-vector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
- *   android:drawable=&quot;@drawable/vectordrawable&quot; &gt;
+ *     android:drawable=&quot;@drawable/vectordrawable&quot; &gt;
  *     &lt;target
  *         android:name=&quot;rotationGroup&quot;
  *         android:animation=&quot;@anim/rotation&quot; /&gt;
@@ -113,36 +134,89 @@
  *         android:name=&quot;v&quot;
  *         android:animation=&quot;@anim/path_morph&quot; /&gt;
  * &lt;/animated-vector&gt;
- * </pre></li>
+ * </pre>
+ * </li>
+ *
+ * <li><h4>XML for Animations defined using ObjectAnimator or AnimatorSet</h4>
  * <p>
- * Last is the Animator XML file, which is the same as a normal ObjectAnimator
- * or AnimatorSet.
- * To complete this example, here are the 2 animator files used in avd.xml:
- * rotation.xml and path_morph.xml.
+ * From the previous <a href="#AVDExample">example of AnimatedVectorDrawable</a>, two animations
+ * were used: rotation.xml and path_morph.xml.
  * </p>
- * <li>Here is the rotation.xml, which will rotate the target group for 360 degrees.
+ * rotation.xml rotates the target group from 0 degree to 360 degrees over 6000ms:
  * <pre>
  * &lt;objectAnimator
  *     android:duration=&quot;6000&quot;
  *     android:propertyName=&quot;rotation&quot;
  *     android:valueFrom=&quot;0&quot;
  *     android:valueTo=&quot;360&quot; /&gt;
- * </pre></li>
- * <li>Here is the path_morph.xml, which will morph the path from one shape to
- * the other. Note that the paths must be compatible for morphing.
- * In more details, the paths should have exact same length of commands , and
- * exact same length of parameters for each commands.
- * Note that the path strings are better stored in strings.xml for reusing.
+ * </pre>
+ *
+ * path_morph.xml morphs the path from one shape into the other. Note that the paths must be
+ * compatible for morphing. Specifically, the paths must have the same commands, in the same order,
+ * and must have the same number of parameters for each command. It is recommended to store path
+ * strings as string resources for reuse.
  * <pre>
  * &lt;set xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
  *     &lt;objectAnimator
  *         android:duration=&quot;3000&quot;
  *         android:propertyName=&quot;pathData&quot;
- *         android:valueFrom=&quot;M300,70 l 0,-70 70,70 0,0   -70,70z&quot;
+ *         android:valueFrom=&quot;M300,70 l 0,-70 70,70 0,0 -70,70z&quot;
  *         android:valueTo=&quot;M300,70 l 0,-70 70,0  0,140 -70,0 z&quot;
  *         android:valueType=&quot;pathType&quot;/&gt;
  * &lt;/set&gt;
- * </pre></li>
+ * </pre>
+ * </ul>
+ * <a name="OneXML"></a>
+ * <h3>Define an AnimatedVectorDrawable all in one XML file</h3>
+ * <p>
+ * Since the AAPT tool supports a new format that bundles several related XML files together, we can
+ * merge the XML files from the previous examples into one XML file:
+ * </p>
+ * <pre>
+ * &lt;animated-vector xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; &gt;
+ *     &lt;aapt:attr name="android:drawable"&gt;
+ *         &lt;vector
+ *             android:height=&quot;64dp&quot;
+ *             android:width=&quot;64dp&quot;
+ *             android:viewportHeight=&quot;600&quot;
+ *             android:viewportWidth=&quot;600&quot; &gt;
+ *             &lt;group
+ *                 android:name=&quot;rotationGroup&quot;
+ *                 android:pivotX=&quot;300.0&quot;
+ *                 android:pivotY=&quot;300.0&quot;
+ *                 android:rotation=&quot;45.0&quot; &gt;
+ *                 &lt;path
+ *                     android:name=&quot;v&quot;
+ *                     android:fillColor=&quot;#000000&quot;
+ *                     android:pathData=&quot;M300,70 l 0,-70 70,70 0,0 -70,70z&quot; /&gt;
+ *             &lt;/group&gt;
+ *         &lt;/vector&gt;
+ *     &lt;/aapt:attr&gt;
+ *
+ *     &lt;target android:name=&quot;rotationGroup&quot;&gt; *
+ *         &lt;aapt:attr name="android:animation"&gt;
+ *             &lt;objectAnimator
+ *             android:duration=&quot;6000&quot;
+ *             android:propertyName=&quot;rotation&quot;
+ *             android:valueFrom=&quot;0&quot;
+ *             android:valueTo=&quot;360&quot; /&gt;
+ *         &lt;/aapt:attr&gt;
+ *     &lt;/target&gt;
+ *
+ *     &lt;target android:name=&quot;v&quot; &gt;
+ *         &lt;aapt:attr name="android:animation"&gt;
+ *             &lt;set&gt;
+ *                 &lt;objectAnimator
+ *                     android:duration=&quot;3000&quot;
+ *                     android:propertyName=&quot;pathData&quot;
+ *                     android:valueFrom=&quot;M300,70 l 0,-70 70,70 0,0 -70,70z&quot;
+ *                     android:valueTo=&quot;M300,70 l 0,-70 70,0  0,140 -70,0 z&quot;
+ *                     android:valueType=&quot;pathType&quot;/&gt;
+ *             &lt;/set&gt;
+ *         &lt;/aapt:attr&gt;
+ *      &lt;/target&gt;
+ * &lt;/animated-vector&gt;
+ * </pre>
  *
  * @attr ref android.R.styleable#AnimatedVectorDrawable_drawable
  * @attr ref android.R.styleable#AnimatedVectorDrawableTarget_name
@@ -157,7 +231,7 @@
     private static final boolean DBG_ANIMATION_VECTOR_DRAWABLE = false;
 
     /** Local, mutable animator set. */
-    private VectorDrawableAnimator mAnimatorSet = new VectorDrawableAnimatorUI(this);
+    private VectorDrawableAnimator mAnimatorSet;
 
     /**
      * The resources against which this drawable was created. Used to attempt
@@ -182,6 +256,7 @@
 
     private AnimatedVectorDrawable(AnimatedVectorDrawableState state, Resources res) {
         mAnimatedVectorState = new AnimatedVectorDrawableState(state, mCallback, res);
+        mAnimatorSet = new VectorDrawableAnimatorRT(this);
         mRes = res;
     }
 
@@ -235,8 +310,27 @@
         return super.getChangingConfigurations() | mAnimatedVectorState.getChangingConfigurations();
     }
 
+    /**
+     * Draws the AnimatedVectorDrawable into the given canvas.
+     * <p>
+     * <strong>Note:</strong> Calling this method with a software canvas when the
+     * AnimatedVectorDrawable is being animated on RenderThread (for API 25 and later) may yield
+     * outdated result, as the UI thread is not guaranteed to be in sync with RenderThread on
+     * VectorDrawable's property changes during RenderThread animations.
+     * </p>
+     *
+     * @param canvas The canvas to draw into
+     */
     @Override
     public void draw(Canvas canvas) {
+        if (!canvas.isHardwareAccelerated() && mAnimatorSet instanceof VectorDrawableAnimatorRT) {
+            // If we have SW canvas and the RT animation is waiting to start, We need to fallback
+            // to UI thread animation for AVD.
+            if (!mAnimatorSet.isRunning() &&
+                    ((VectorDrawableAnimatorRT) mAnimatorSet).mPendingAnimationActions.size() > 0) {
+                fallbackOntoUI();
+            }
+        }
         mAnimatorSet.onDraw(canvas);
         mAnimatedVectorState.mVectorDrawable.draw(canvas);
     }
@@ -262,9 +356,9 @@
     }
 
     /**
-     * AnimatedVectorDrawable is running on render thread now. Therefore, if the root alpha is being
-     * animated, then the root alpha value we get from this call could be out of sync with alpha
-     * value used in the render thread. Otherwise, the root alpha should be always the same value.
+     * For API 25 and later, AnimatedVectorDrawable runs on RenderThread. Therefore, when the
+     * root alpha is being animated, this getter does not guarantee to return an up-to-date alpha
+     * value.
      *
      * @return the containing vector drawable's root alpha value.
      */
@@ -361,7 +455,11 @@
 
         int eventType = parser.getEventType();
         float pathErrorScale = 1;
-        while (eventType != XmlPullParser.END_DOCUMENT) {
+        final int innerDepth = parser.getDepth() + 1;
+
+        // Parse everything until the end of the animated-vector element.
+        while (eventType != XmlPullParser.END_DOCUMENT
+                && (parser.getDepth() >= innerDepth || eventType != XmlPullParser.END_TAG)) {
             if (eventType == XmlPullParser.START_TAG) {
                 final String tagName = parser.getName();
                 if (ANIMATED_VECTOR.equals(tagName)) {
@@ -390,9 +488,12 @@
                             R.styleable.AnimatedVectorDrawableTarget_animation, 0);
                     if (animResId != 0) {
                         if (theme != null) {
-                            final Animator objectAnimator = AnimatorInflater.loadAnimator(
+                            // The animator here could be ObjectAnimator or AnimatorSet.
+                            final Animator animator = AnimatorInflater.loadAnimator(
                                     res, theme, animResId, pathErrorScale);
-                            state.addTargetAnimator(target, objectAnimator);
+                            updateAnimatorProperty(animator, target, state.mVectorDrawable,
+                                    state.mShouldIgnoreInvalidAnim);
+                            state.addTargetAnimator(target, animator);
                         } else {
                             // The animation may be theme-dependent. As a
                             // workaround until Animator has full support for
@@ -414,6 +515,55 @@
         mRes = state.mPendingAnims == null ? null : res;
     }
 
+    private static void updateAnimatorProperty(Animator animator, String targetName,
+            VectorDrawable vectorDrawable, boolean ignoreInvalidAnim) {
+        if (animator instanceof ObjectAnimator) {
+            // Change the property of the Animator from using reflection based on the property
+            // name to a Property object that wraps the setter and getter for modifying that
+            // specific property for a given object. By replacing the reflection with a direct call,
+            // we can largely reduce the time it takes for a animator to modify a VD property.
+            PropertyValuesHolder[] holders = ((ObjectAnimator) animator).getValues();
+            for (int i = 0; i < holders.length; i++) {
+                PropertyValuesHolder pvh = holders[i];
+                String propertyName = pvh.getPropertyName();
+                Object targetNameObj = vectorDrawable.getTargetByName(targetName);
+                Property property = null;
+                if (targetNameObj instanceof VectorDrawable.VObject) {
+                    property = ((VectorDrawable.VObject) targetNameObj).getProperty(propertyName);
+                } else if (targetNameObj instanceof VectorDrawable.VectorDrawableState) {
+                    property = ((VectorDrawable.VectorDrawableState) targetNameObj)
+                            .getProperty(propertyName);
+                }
+                if (property != null) {
+                    if (containsSameValueType(pvh, property)) {
+                        pvh.setProperty(property);
+                    } else if (!ignoreInvalidAnim) {
+                        throw new RuntimeException("Wrong valueType for Property: " + propertyName
+                                + ".  Expected type: " + property.getType().toString() + ". Actual "
+                                + "type defined in resources: " + pvh.getValueType().toString());
+
+                    }
+                }
+            }
+        } else if (animator instanceof AnimatorSet) {
+            for (Animator anim : ((AnimatorSet) animator).getChildAnimations()) {
+                updateAnimatorProperty(anim, targetName, vectorDrawable, ignoreInvalidAnim);
+            }
+        }
+    }
+
+    private static boolean containsSameValueType(PropertyValuesHolder holder, Property property) {
+        Class type1 = holder.getValueType();
+        Class type2 = property.getType();
+        if (type1 == float.class || type1 == Float.class) {
+            return type2 == float.class || type2 == Float.class;
+        } else if (type1 == int.class || type1 == Integer.class) {
+            return type2 == int.class || type2 == Integer.class;
+        } else {
+            return type1 == type2;
+        }
+    }
+
     /**
      * Force to animate on UI thread.
      * @hide
@@ -425,10 +575,22 @@
                 throw new UnsupportedOperationException("Cannot force Animated Vector Drawable to" +
                         " run on UI thread when the animation has started on RenderThread.");
             }
+            fallbackOntoUI();
+        }
+    }
+
+    private void fallbackOntoUI() {
+        if (mAnimatorSet instanceof VectorDrawableAnimatorRT) {
+            VectorDrawableAnimatorRT oldAnim = (VectorDrawableAnimatorRT) mAnimatorSet;
             mAnimatorSet = new VectorDrawableAnimatorUI(this);
             if (mAnimatorSetFromXml != null) {
                 mAnimatorSet.init(mAnimatorSetFromXml);
             }
+            // Transfer the listener from RT animator to UI animator
+            if (oldAnim.mListener != null) {
+                mAnimatorSet.setListener(oldAnim.mListener);
+            }
+            oldAnim.transferPendingActions(mAnimatorSet);
         }
     }
 
@@ -462,6 +624,8 @@
         @Config int mChangingConfigurations;
         VectorDrawable mVectorDrawable;
 
+        private final boolean mShouldIgnoreInvalidAnim;
+
         /** Animators that require a theme before inflation. */
         ArrayList<PendingAnimator> mPendingAnims;
 
@@ -473,6 +637,7 @@
 
         public AnimatedVectorDrawableState(AnimatedVectorDrawableState copy,
                 Callback owner, Resources res) {
+            mShouldIgnoreInvalidAnim = shouldIgnoreInvalidAnimation();
             if (copy != null) {
                 mChangingConfigurations = copy.mChangingConfigurations;
 
@@ -616,8 +781,10 @@
 
                 for (int i = 0, count = pendingAnims.size(); i < count; i++) {
                     final PendingAnimator pendingAnimator = pendingAnims.get(i);
-                    final Animator objectAnimator = pendingAnimator.newInstance(res, t);
-                    addTargetAnimator(pendingAnimator.target, objectAnimator);
+                    final Animator animator = pendingAnimator.newInstance(res, t);
+                    updateAnimatorProperty(animator, pendingAnimator.target, mVectorDrawable,
+                            mShouldIgnoreInvalidAnim);
+                    addTargetAnimator(pendingAnimator.target, animator);
                 }
             }
         }
@@ -982,6 +1149,9 @@
         private static final int REVERSE_ANIMATION = 2;
         private static final int RESET_ANIMATION = 3;
         private static final int END_ANIMATION = 4;
+
+        // If the duration of an animation is more than 300 frames, we cap the sample size to 300.
+        private static final int MAX_SAMPLE_POINTS = 300;
         private AnimatorListener mListener = null;
         private final LongArray mStartDelays = new LongArray();
         private PropertyValuesHolder.PropertyValues mTmpValues =
@@ -992,14 +1162,12 @@
         private boolean mInitialized = false;
         private boolean mIsReversible = false;
         private boolean mIsInfinite = false;
-        // This needs to be set before parsing starts.
-        private boolean mShouldIgnoreInvalidAnim;
         // TODO: Consider using NativeAllocationRegistery to track native allocation
         private final VirtualRefBasePtr mSetRefBasePtr;
         private WeakReference<RenderNode> mLastSeenTarget = null;
         private int mLastListenerId = 0;
         private final IntArray mPendingAnimationActions = new IntArray();
-        private final Drawable mDrawable;
+        private final AnimatedVectorDrawable mDrawable;
 
         VectorDrawableAnimatorRT(AnimatedVectorDrawable drawable) {
             mDrawable = drawable;
@@ -1016,8 +1184,10 @@
                 throw new UnsupportedOperationException("VectorDrawableAnimator cannot be " +
                         "re-initialized");
             }
-            mShouldIgnoreInvalidAnim = shouldIgnoreInvalidAnimation();
             parseAnimatorSet(set, 0);
+            long vectorDrawableTreePtr = mDrawable.mAnimatedVectorState.mVectorDrawable
+                    .getNativeTree();
+            nSetVectorDrawableTarget(mSetPtr, vectorDrawableTreePtr);
             mInitialized = true;
             mIsInfinite = set.getTotalDuration() == Animator.DURATION_INFINITE;
 
@@ -1077,7 +1247,7 @@
                     }  else if (target instanceof VectorDrawable.VFullPath) {
                         createRTAnimatorForFullPath(animator, (VectorDrawable.VFullPath) target,
                                 startTime);
-                    } else if (!mShouldIgnoreInvalidAnim) {
+                    } else if (!mDrawable.mAnimatedVectorState.mShouldIgnoreInvalidAnim) {
                         throw new IllegalArgumentException("ClipPath only supports PathData " +
                                 "property");
                     }
@@ -1086,7 +1256,7 @@
             } else if (target instanceof VectorDrawable.VectorDrawableState) {
                 createRTAnimatorForRootGroup(values, animator,
                         (VectorDrawable.VectorDrawableState) target, startTime);
-            } else if (!mShouldIgnoreInvalidAnim) {
+            } else if (!mDrawable.mAnimatedVectorState.mShouldIgnoreInvalidAnim) {
                 // Should never get here
                 throw new UnsupportedOperationException("Target should be either VGroup, VPath, " +
                         "or ConstantState, " + target == null ? "Null target" : target.getClass() +
@@ -1121,8 +1291,8 @@
                 long propertyPtr = nCreateGroupPropertyHolder(nativePtr, propertyId,
                         (Float) mTmpValues.startValue, (Float) mTmpValues.endValue);
                 if (mTmpValues.dataSource != null) {
-                    float[] dataPoints = createDataPoints(mTmpValues.dataSource, animator
-                            .getDuration());
+                    float[] dataPoints = createFloatDataPoints(mTmpValues.dataSource,
+                            animator.getDuration());
                     nSetPropertyHolderData(propertyPtr, dataPoints, dataPoints.length);
                 }
                 createNativeChildAnimator(propertyPtr, startTime, animator);
@@ -1149,7 +1319,7 @@
             long nativePtr = target.getNativePtr();
             if (mTmpValues.type == Float.class || mTmpValues.type == float.class) {
                 if (propertyId < 0) {
-                    if (mShouldIgnoreInvalidAnim) {
+                    if (mDrawable.mAnimatedVectorState.mShouldIgnoreInvalidAnim) {
                         return;
                     } else {
                         throw new IllegalArgumentException("Property: " + mTmpValues.propertyName
@@ -1158,12 +1328,24 @@
                 }
                 propertyPtr = nCreatePathPropertyHolder(nativePtr, propertyId,
                         (Float) mTmpValues.startValue, (Float) mTmpValues.endValue);
+                if (mTmpValues.dataSource != null) {
+                    // Pass keyframe data to native, if any.
+                    float[] dataPoints = createFloatDataPoints(mTmpValues.dataSource,
+                            animator.getDuration());
+                    nSetPropertyHolderData(propertyPtr, dataPoints, dataPoints.length);
+                }
 
             } else if (mTmpValues.type == Integer.class || mTmpValues.type == int.class) {
                 propertyPtr = nCreatePathColorPropertyHolder(nativePtr, propertyId,
                         (Integer) mTmpValues.startValue, (Integer) mTmpValues.endValue);
+                if (mTmpValues.dataSource != null) {
+                    // Pass keyframe data to native, if any.
+                    int[] dataPoints = createIntDataPoints(mTmpValues.dataSource,
+                            animator.getDuration());
+                    nSetPropertyHolderData(propertyPtr, dataPoints, dataPoints.length);
+                }
             } else {
-                if (mShouldIgnoreInvalidAnim) {
+                if (mDrawable.mAnimatedVectorState.mShouldIgnoreInvalidAnim) {
                     return;
                 } else {
                     throw new UnsupportedOperationException("Unsupported type: " +
@@ -1171,45 +1353,63 @@
                             "supported for Paths.");
                 }
             }
-            if (mTmpValues.dataSource != null) {
-                float[] dataPoints = createDataPoints(mTmpValues.dataSource, animator
-                        .getDuration());
-                nSetPropertyHolderData(propertyPtr, dataPoints, dataPoints.length);
-            }
             createNativeChildAnimator(propertyPtr, startTime, animator);
         }
 
         private void createRTAnimatorForRootGroup(PropertyValuesHolder[] values,
                 ObjectAnimator animator, VectorDrawable.VectorDrawableState target,
                 long startTime) {
-                long nativePtr = target.getNativeRenderer();
-                if (!animator.getPropertyName().equals("alpha")) {
-                    if (mShouldIgnoreInvalidAnim) {
-                        return;
-                    } else {
-                        throw new UnsupportedOperationException("Only alpha is supported for root "
-                                + "group");
-                    }
+            long nativePtr = target.getNativeRenderer();
+            if (!animator.getPropertyName().equals("alpha")) {
+                if (mDrawable.mAnimatedVectorState.mShouldIgnoreInvalidAnim) {
+                    return;
+                } else {
+                    throw new UnsupportedOperationException("Only alpha is supported for root "
+                            + "group");
                 }
-                Float startValue = null;
-                Float endValue = null;
-                for (int i = 0; i < values.length; i++) {
-                    values[i].getPropertyValues(mTmpValues);
-                    if (mTmpValues.propertyName.equals("alpha")) {
-                        startValue = (Float) mTmpValues.startValue;
-                        endValue = (Float) mTmpValues.endValue;
-                        break;
-                    }
+            }
+            Float startValue = null;
+            Float endValue = null;
+            for (int i = 0; i < values.length; i++) {
+                values[i].getPropertyValues(mTmpValues);
+                if (mTmpValues.propertyName.equals("alpha")) {
+                    startValue = (Float) mTmpValues.startValue;
+                    endValue = (Float) mTmpValues.endValue;
+                    break;
                 }
-                if (startValue == null && endValue == null) {
-                    if (mShouldIgnoreInvalidAnim) {
-                        return;
-                    } else {
-                        throw new UnsupportedOperationException("No alpha values are specified");
-                    }
+            }
+            if (startValue == null && endValue == null) {
+                if (mDrawable.mAnimatedVectorState.mShouldIgnoreInvalidAnim) {
+                    return;
+                } else {
+                    throw new UnsupportedOperationException("No alpha values are specified");
                 }
-                long propertyPtr = nCreateRootAlphaPropertyHolder(nativePtr, startValue, endValue);
-                createNativeChildAnimator(propertyPtr, startTime, animator);
+            }
+            long propertyPtr = nCreateRootAlphaPropertyHolder(nativePtr, startValue, endValue);
+            if (mTmpValues.dataSource != null) {
+                // Pass keyframe data to native, if any.
+                float[] dataPoints = createFloatDataPoints(mTmpValues.dataSource,
+                        animator.getDuration());
+                nSetPropertyHolderData(propertyPtr, dataPoints, dataPoints.length);
+            }
+            createNativeChildAnimator(propertyPtr, startTime, animator);
+        }
+
+        /**
+         * Calculate the amount of frames an animation will run based on duration.
+         */
+        private static int getFrameCount(long duration) {
+            long frameIntervalNanos = Choreographer.getInstance().getFrameIntervalNanos();
+            int animIntervalMs = (int) (frameIntervalNanos / TimeUtils.NANOS_PER_MS);
+            int numAnimFrames = (int) Math.ceil(((double) duration) / animIntervalMs);
+            // We need 2 frames of data minimum.
+            numAnimFrames = Math.max(2, numAnimFrames);
+            if (numAnimFrames > MAX_SAMPLE_POINTS) {
+                Log.w("AnimatedVectorDrawable", "Duration for the animation is too long :" +
+                        duration + ", the animation will subsample the keyframe or path data.");
+                numAnimFrames = MAX_SAMPLE_POINTS;
+            }
+            return numAnimFrames;
         }
 
         // These are the data points that define the value of the animating properties.
@@ -1217,11 +1417,9 @@
         // a point on the path corresponds to the values of translateX and translateY.
         // TODO: (Optimization) We should pass the path down in native and chop it into segments
         // in native.
-        private static float[] createDataPoints(
+        private static float[] createFloatDataPoints(
                 PropertyValuesHolder.PropertyValues.DataSource dataSource, long duration) {
-            long frameIntervalNanos = Choreographer.getInstance().getFrameIntervalNanos();
-            int animIntervalMs = (int) (frameIntervalNanos / TimeUtils.NANOS_PER_MS);
-            int numAnimFrames = (int) Math.ceil(((double) duration) / animIntervalMs);
+            int numAnimFrames = getFrameCount(duration);
             float values[] = new float[numAnimFrames];
             float lastFrame = numAnimFrames - 1;
             for (int i = 0; i < numAnimFrames; i++) {
@@ -1231,6 +1429,18 @@
             return values;
         }
 
+        private static int[] createIntDataPoints(
+                PropertyValuesHolder.PropertyValues.DataSource dataSource, long duration) {
+            int numAnimFrames = getFrameCount(duration);
+            int values[] = new int[numAnimFrames];
+            float lastFrame = numAnimFrames - 1;
+            for (int i = 0; i < numAnimFrames; i++) {
+                float fraction = i / lastFrame;
+                values[i] = (Integer) dataSource.getValueAtFraction(fraction);
+            }
+            return values;
+        }
+
         private void createNativeChildAnimator(long propertyPtr, long extraDelay,
                                                ObjectAnimator animator) {
             long duration = animator.getDuration();
@@ -1245,7 +1455,7 @@
 
             mStartDelays.add(startDelay);
             nAddAnimator(mSetPtr, propertyPtr, nativeInterpolator, startDelay, duration,
-                    repeatCount);
+                    repeatCount, animator.getRepeatMode());
         }
 
         /**
@@ -1254,16 +1464,19 @@
          * to the last seen RenderNode target and start right away.
          */
         protected void recordLastSeenTarget(DisplayListCanvas canvas) {
-            mLastSeenTarget = new WeakReference<RenderNode>(
-                    RenderNodeAnimatorSetHelper.getTarget(canvas));
-            if (mPendingAnimationActions.size() > 0 && useLastSeenTarget()) {
-                if (DBG_ANIMATION_VECTOR_DRAWABLE) {
-                    Log.d(LOGTAG, "Target is set in the next frame");
+            final RenderNode node = RenderNodeAnimatorSetHelper.getTarget(canvas);
+            mLastSeenTarget = new WeakReference<RenderNode>(node);
+            // Add the animator to the list of animators on every draw
+            if (mInitialized || mPendingAnimationActions.size() > 0) {
+                if (useTarget(node)) {
+                    if (DBG_ANIMATION_VECTOR_DRAWABLE) {
+                        Log.d(LOGTAG, "Target is set in the next frame");
+                    }
+                    for (int i = 0; i < mPendingAnimationActions.size(); i++) {
+                        handlePendingAction(mPendingAnimationActions.get(i));
+                    }
+                    mPendingAnimationActions.clear();
                 }
-                for (int i = 0; i < mPendingAnimationActions.size(); i++) {
-                    handlePendingAction(mPendingAnimationActions.get(i));
-                }
-                mPendingAnimationActions.clear();
             }
         }
 
@@ -1285,10 +1498,15 @@
         private boolean useLastSeenTarget() {
             if (mLastSeenTarget != null) {
                 final RenderNode target = mLastSeenTarget.get();
-                if (target != null && target.isAttached()) {
-                    target.addAnimator(this);
-                    return true;
-                }
+                return useTarget(target);
+            }
+            return false;
+        }
+
+        private boolean useTarget(RenderNode target) {
+            if (target != null && target.isAttached()) {
+                target.registerVectorDrawableAnimator(this);
+                return true;
             }
             return false;
         }
@@ -1316,7 +1534,6 @@
             } else {
                 addPendingAction(START_ANIMATION);
             }
-
         }
 
         @Override
@@ -1480,11 +1697,32 @@
         private static void callOnFinished(VectorDrawableAnimatorRT set, int id) {
             set.onAnimationEnd(id);
         }
+
+        private void transferPendingActions(VectorDrawableAnimator animatorSet) {
+            for (int i = 0; i < mPendingAnimationActions.size(); i++) {
+                int pendingAction = mPendingAnimationActions.get(i);
+                if (pendingAction == START_ANIMATION) {
+                    animatorSet.start();
+                } else if (pendingAction == END_ANIMATION) {
+                    animatorSet.end();
+                } else if (pendingAction == REVERSE_ANIMATION) {
+                    animatorSet.reverse();
+                } else if (pendingAction == RESET_ANIMATION) {
+                    animatorSet.reset();
+                } else {
+                    throw new UnsupportedOperationException("Animation action " +
+                            pendingAction + "is not supported");
+                }
+            }
+            mPendingAnimationActions.clear();
+        }
     }
 
     private static native long nCreateAnimatorSet();
+    private static native void nSetVectorDrawableTarget(long animatorPtr, long vectorDrawablePtr);
     private static native void nAddAnimator(long setPtr, long propertyValuesHolder,
-             long nativeInterpolator, long startDelay, long duration, int repeatCount);
+            long nativeInterpolator, long startDelay, long duration, int repeatCount,
+            int repeatMode);
 
     private static native long nCreateGroupPropertyHolder(long nativePtr, int propertyId,
             float startValue, float endValue);
@@ -1498,6 +1736,7 @@
     private static native long nCreateRootAlphaPropertyHolder(long nativePtr, float startValue,
             float endValue);
     private static native void nSetPropertyHolderData(long nativePtr, float[] data, int length);
+    private static native void nSetPropertyHolderData(long nativePtr, int[] data, int length);
     private static native void nStart(long animatorSetPtr, VectorDrawableAnimatorRT set, int id);
     private static native void nReverse(long animatorSetPtr, VectorDrawableAnimatorRT set, int id);
     private static native void nEnd(long animatorSetPtr);
diff --git a/graphics/java/android/graphics/drawable/BitmapDrawable.java b/graphics/java/android/graphics/drawable/BitmapDrawable.java
index 9d8ede0..df107f5 100644
--- a/graphics/java/android/graphics/drawable/BitmapDrawable.java
+++ b/graphics/java/android/graphics/drawable/BitmapDrawable.java
@@ -463,31 +463,14 @@
         return isAutoMirrored() && getLayoutDirection() == LayoutDirection.RTL;
     }
 
-    private void updateMirrorMatrix(float dx) {
-        if (mMirrorMatrix == null) {
-            mMirrorMatrix = new Matrix();
-        }
-        mMirrorMatrix.setTranslate(dx, 0);
-        mMirrorMatrix.preScale(-1.0f, 1.0f);
-    }
-
     @Override
     protected void onBoundsChange(Rect bounds) {
         mDstRectAndInsetsDirty = true;
 
+        final Bitmap bitmap = mBitmapState.mBitmap;
         final Shader shader = mBitmapState.mPaint.getShader();
-        if (shader != null) {
-            if (needMirroring()) {
-                updateMirrorMatrix(bounds.right - bounds.left);
-                shader.setLocalMatrix(mMirrorMatrix);
-                mBitmapState.mPaint.setShader(shader);
-            } else {
-                if (mMirrorMatrix != null) {
-                    mMirrorMatrix = null;
-                    shader.setLocalMatrix(Matrix.IDENTITY_MATRIX);
-                    mBitmapState.mPaint.setShader(shader);
-                }
-            }
+        if (bitmap != null && shader != null) {
+            updateShaderMatrix(bitmap, mBitmapState.mPaint, shader, needMirroring());
         }
     }
 
@@ -548,19 +531,7 @@
                 canvas.restore();
             }
         } else {
-            if (needMirroring) {
-                // Mirror the bitmap
-                updateMirrorMatrix(mDstRect.right - mDstRect.left);
-                shader.setLocalMatrix(mMirrorMatrix);
-                paint.setShader(shader);
-            } else {
-                if (mMirrorMatrix != null) {
-                    mMirrorMatrix = null;
-                    shader.setLocalMatrix(Matrix.IDENTITY_MATRIX);
-                    paint.setShader(shader);
-                }
-            }
-
+            updateShaderMatrix(bitmap, paint, shader, needMirroring);
             canvas.drawRect(mDstRect, paint);
         }
 
@@ -573,6 +544,51 @@
         }
     }
 
+    /**
+     * Updates the {@code paint}'s shader matrix to be consistent with the
+     * destination size and layout direction.
+     *
+     * @param bitmap the bitmap to be drawn
+     * @param paint the paint used to draw the bitmap
+     * @param shader the shader to set on the paint
+     * @param needMirroring whether the bitmap should be mirrored
+     */
+    private void updateShaderMatrix(@NonNull Bitmap bitmap, @NonNull Paint paint,
+            @NonNull Shader shader, boolean needMirroring) {
+        final int sourceDensity = bitmap.getDensity();
+        final int targetDensity = mTargetDensity;
+        final boolean needScaling = sourceDensity != 0 && sourceDensity != targetDensity;
+        if (needScaling || needMirroring) {
+            final Matrix matrix = getOrCreateMirrorMatrix();
+            matrix.reset();
+
+            if (needMirroring) {
+                final int dx = mDstRect.right - mDstRect.left;
+                matrix.setTranslate(dx, 0);
+                matrix.setScale(-1, 1);
+            }
+
+            if (needScaling) {
+                final float densityScale = targetDensity / (float) sourceDensity;
+                matrix.postScale(densityScale, densityScale);
+            }
+
+            shader.setLocalMatrix(matrix);
+        } else {
+            mMirrorMatrix = null;
+            shader.setLocalMatrix(Matrix.IDENTITY_MATRIX);
+        }
+
+        paint.setShader(shader);
+    }
+
+    private Matrix getOrCreateMirrorMatrix() {
+        if (mMirrorMatrix == null) {
+            mMirrorMatrix = new Matrix();
+        }
+        return mMirrorMatrix;
+    }
+
     private void updateDstRectAndInsetsIfDirty() {
         if (mDstRectAndInsetsDirty) {
             if (mBitmapState.mTileModeX == null && mBitmapState.mTileModeY == null) {
diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java
index 31019ce..c2e302e 100644
--- a/graphics/java/android/graphics/drawable/Drawable.java
+++ b/graphics/java/android/graphics/drawable/Drawable.java
@@ -1425,9 +1425,10 @@
     /**
      * Obtains styled attributes from the theme, if available, or unstyled
      * resources if the theme is null.
+     * @hide
      */
-    static @NonNull TypedArray obtainAttributes(@NonNull Resources res, @Nullable Theme theme,
-            @NonNull AttributeSet set, @NonNull int[] attrs) {
+    protected static @NonNull TypedArray obtainAttributes(@NonNull Resources res,
+            @Nullable Theme theme, @NonNull AttributeSet set, @NonNull int[] attrs) {
         if (theme == null) {
             return res.obtainAttributes(set, attrs);
         }
diff --git a/graphics/java/android/graphics/drawable/DrawableContainer.java b/graphics/java/android/graphics/drawable/DrawableContainer.java
index cc7f5c7..c7a3c75 100644
--- a/graphics/java/android/graphics/drawable/DrawableContainer.java
+++ b/graphics/java/android/graphics/drawable/DrawableContainer.java
@@ -27,8 +27,8 @@
 import android.graphics.Insets;
 import android.graphics.Outline;
 import android.graphics.PixelFormat;
-import android.graphics.Rect;
 import android.graphics.PorterDuff.Mode;
+import android.graphics.Rect;
 import android.os.SystemClock;
 import android.util.DisplayMetrics;
 import android.util.LayoutDirection;
@@ -601,8 +601,9 @@
      * during inflation.
      *
      * @param res the resources used to inflate density-dependent values
+     * @hide
      */
-    final void updateDensity(Resources res) {
+    protected final void updateDensity(Resources res) {
         mDrawableContainerState.updateDensity(res);
     }
 
@@ -711,7 +712,10 @@
         boolean mHasTintList;
         boolean mHasTintMode;
 
-        DrawableContainerState(DrawableContainerState orig, DrawableContainer owner,
+        /**
+         * @hide
+         */
+        protected DrawableContainerState(DrawableContainerState orig, DrawableContainer owner,
                 Resources res) {
             mOwner = owner;
             mSourceRes = res != null ? res : (orig != null ? orig.mSourceRes : null);
diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java
index bcc354c..8c633b0 100644
--- a/graphics/java/android/graphics/drawable/GradientDrawable.java
+++ b/graphics/java/android/graphics/drawable/GradientDrawable.java
@@ -475,16 +475,17 @@
     }
 
     /**
-     * Sets the center location in pixels of the gradient. The radius is
-     * honored only when the gradient type is set to {@link #RADIAL_GRADIENT}
-     * or {@link #SWEEP_GRADIENT}.
+     * Sets the position of the center of the gradient as a fraction of the
+     * width and height.
+     * <p>
+     * The default value is (0.5, 0.5).
      * <p>
      * <strong>Note</strong>: changing this property will affect all instances
      * of a drawable loaded from a resource. It is recommended to invoke
      * {@link #mutate()} before changing this property.
      *
-     * @param x the x coordinate of the gradient's center in pixels
-     * @param y the y coordinate of the gradient's center in pixels
+     * @param x the X-position of the center of the gradient
+     * @param y the Y-position of the center of the gradient
      *
      * @see #mutate()
      * @see #setGradientType(int)
@@ -498,9 +499,10 @@
     }
 
     /**
-     * Returns the center X location of this gradient in pixels.
+     * Returns the X-position of the center of the gradient as a fraction of
+     * the width.
      *
-     * @return the center X location of this gradient in pixels
+     * @return the X-position of the center of the gradient
      * @see #setGradientCenter(float, float)
      */
     public float getGradientCenterX() {
@@ -508,9 +510,10 @@
     }
 
     /**
-     * Returns the center Y location of this gradient in pixels.
+     * Returns the Y-position of the center of this gradient as a fraction of
+     * the height.
      *
-     * @return the center Y location of this gradient in pixels
+     * @return the Y-position of the center of the gradient
      * @see #setGradientCenter(float, float)
      */
     public float getGradientCenterY() {
@@ -554,19 +557,43 @@
     }
 
     /**
-     * Sets whether or not this drawable will honor its {@code level} property.
+     * Sets whether this drawable's {@code level} property will be used to
+     * scale the gradient. If a gradient is not used, this property has no
+     * effect.
      * <p>
-     * <strong>Note</strong>: changing this property will affect all instances
+     * Scaling behavior varies based on gradient type:
+     * <ul>
+     *     <li>{@link #LINEAR_GRADIENT} adjusts the ending position along the
+     *         gradient's axis of orientation (see {@link #getOrientation()})
+     *     <li>{@link #RADIAL_GRADIENT} adjusts the outer radius
+     *     <li>{@link #SWEEP_GRADIENT} adjusts the ending angle
+     * <ul>
+     * <p>
+     * The default value for this property is {@code false}.
+     * <p>
+     * <strong>Note</strong>: This property corresponds to the
+     * {@code android:useLevel} attribute on the inner {@code &lt;gradient&gt;}
+     * tag, NOT the {@code android:useLevel} attribute on the outer
+     * {@code &lt;shape&gt;} tag. For example,
+     * <pre>{@code
+     * <shape ...>
+     *     <gradient
+     *         ...
+     *         android:useLevel="true" />
+     * </shape>
+     * }</pre><p>
+     * <strong>Note</strong>: Changing this property will affect all instances
      * of a drawable loaded from a resource. It is recommended to invoke
      * {@link #mutate()} before changing this property.
      *
-     * @param useLevel {@code true} if this drawable should honor its level,
-     *                 {@code false} otherwise
+     * @param useLevel {@code true} if the gradient should be scaled based on
+     *                 level, {@code false} otherwise
      *
      * @see #mutate()
      * @see #setLevel(int)
      * @see #getLevel()
      * @see #getUseLevel()
+     * @attr ref android.R.styleable#GradientDrawableGradient_useLevel
      */
     public void setUseLevel(boolean useLevel) {
         mGradientState.mUseLevel = useLevel;
@@ -575,12 +602,13 @@
     }
 
     /**
-     * Returns whether or not this drawable will honor its {@code level}
-     * property.
+     * Returns whether this drawable's {@code level} property will be used to
+     * scale the gradient.
      *
-     * @return {@code true} if this drawable should honor its level,
+     * @return {@code true} if the gradient should be scaled based on level,
      *         {@code false} otherwise
      * @see #setUseLevel(boolean)
+     * @attr ref android.R.styleable#GradientDrawableGradient_useLevel
      */
     public boolean getUseLevel() {
         return mGradientState.mUseLevel;
@@ -699,7 +727,7 @@
             float rad = mStrokePaint.getStrokeWidth();
             canvas.saveLayer(mRect.left - rad, mRect.top - rad,
                              mRect.right + rad, mRect.bottom + rad,
-                             mLayerPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
+                             mLayerPaint);
 
             // don't perform the filter in our individual paints
             // since the layer will do it for us
diff --git a/graphics/java/android/graphics/drawable/LayerDrawable.java b/graphics/java/android/graphics/drawable/LayerDrawable.java
index 1864206..c30c4c2 100644
--- a/graphics/java/android/graphics/drawable/LayerDrawable.java
+++ b/graphics/java/android/graphics/drawable/LayerDrawable.java
@@ -269,7 +269,8 @@
 
             // If the layer doesn't have a drawable or unresolved theme
             // attribute for a drawable, attempt to parse one from the child
-            // element.
+            // element. If multiple child elements exist, we'll only use the
+            // first one.
             if (layer.mDrawable == null && (layer.mThemeAttrs == null ||
                     layer.mThemeAttrs[R.styleable.LayerDrawableItem_drawable] == 0)) {
                 while ((type = parser.next()) == XmlPullParser.TEXT) {
@@ -279,13 +280,12 @@
                             + ": <item> tag requires a 'drawable' attribute or "
                             + "child tag defining a drawable");
                 }
-                layer.mDrawable = Drawable.createFromXmlInner(r, parser, attrs, theme);
-            }
 
-            if (layer.mDrawable != null) {
+                // We found a child drawable. Take ownership.
+                layer.mDrawable = Drawable.createFromXmlInner(r, parser, attrs, theme);
+                layer.mDrawable.setCallback(this);
                 state.mChildrenChangingConfigurations |=
                         layer.mDrawable.getChangingConfigurations();
-                layer.mDrawable.setCallback(this);
             }
 
             addLayer(layer);
@@ -387,7 +387,19 @@
 
         final Drawable dr = a.getDrawable(R.styleable.LayerDrawableItem_drawable);
         if (dr != null) {
+            if (layer.mDrawable != null) {
+                // It's possible that a drawable was already set, in which case
+                // we should clear the callback. We may have also integrated the
+                // drawable's changing configurations, but we don't have enough
+                // information to revert that change.
+                layer.mDrawable.setCallback(null);
+            }
+
+            // Take ownership of the new drawable.
             layer.mDrawable = dr;
+            layer.mDrawable.setCallback(this);
+            state.mChildrenChangingConfigurations |=
+                    layer.mDrawable.getChangingConfigurations();
         }
     }
 
diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java
index c855c4c..1ca1552 100644
--- a/graphics/java/android/graphics/drawable/VectorDrawable.java
+++ b/graphics/java/android/graphics/drawable/VectorDrawable.java
@@ -27,16 +27,19 @@
 import android.graphics.ColorFilter;
 import android.graphics.Insets;
 import android.graphics.PixelFormat;
+import android.graphics.PorterDuff.Mode;
 import android.graphics.PorterDuffColorFilter;
 import android.graphics.Rect;
-import android.graphics.PorterDuff.Mode;
 import android.graphics.Shader;
 import android.util.ArrayMap;
 import android.util.AttributeSet;
 import android.util.DisplayMetrics;
+import android.util.FloatProperty;
+import android.util.IntProperty;
 import android.util.LayoutDirection;
 import android.util.Log;
 import android.util.PathParser;
+import android.util.Property;
 import android.util.Xml;
 
 import com.android.internal.R;
@@ -73,27 +76,36 @@
  * <dl>
  * <dt><code>android:name</code></dt>
  * <dd>Defines the name of this vector drawable.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:width</code></dt>
  * <dd>Used to define the intrinsic width of the drawable.
  * This support all the dimension units, normally specified with dp.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:height</code></dt>
  * <dd>Used to define the intrinsic height the drawable.
  * This support all the dimension units, normally specified with dp.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:viewportWidth</code></dt>
  * <dd>Used to define the width of the viewport space. Viewport is basically
  * the virtual canvas where the paths are drawn on.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:viewportHeight</code></dt>
  * <dd>Used to define the height of the viewport space. Viewport is basically
  * the virtual canvas where the paths are drawn on.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:tint</code></dt>
  * <dd>The color to apply to the drawable as a tint. By default, no tint is applied.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:tintMode</code></dt>
  * <dd>The Porter-Duff blending mode for the tint color. The default value is src_in.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:autoMirrored</code></dt>
  * <dd>Indicates if the drawable needs to be mirrored when its layout direction is
  * RTL (right-to-left).</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:alpha</code></dt>
  * <dd>The opacity of this drawable.</dd>
+ * <dd>Animatable : Yes.</dd>
  * </dl></dd>
  * </dl>
  *
@@ -105,24 +117,32 @@
  * <dl>
  * <dt><code>android:name</code></dt>
  * <dd>Defines the name of the group.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:rotation</code></dt>
  * <dd>The degrees of rotation of the group.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:pivotX</code></dt>
  * <dd>The X coordinate of the pivot for the scale and rotation of the group.
  * This is defined in the viewport space.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:pivotY</code></dt>
  * <dd>The Y coordinate of the pivot for the scale and rotation of the group.
  * This is defined in the viewport space.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:scaleX</code></dt>
  * <dd>The amount of scale on the X Coordinate.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:scaleY</code></dt>
  * <dd>The amount of scale on the Y coordinate.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:translateX</code></dt>
  * <dd>The amount of translation on the X coordinate.
  * This is defined in the viewport space.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:translateY</code></dt>
  * <dd>The amount of translation on the Y coordinate.
  * This is defined in the viewport space.</dd>
+ * <dd>Animatable : Yes.</dd>
  * </dl></dd>
  * </dl>
  *
@@ -132,40 +152,60 @@
  * <dl>
  * <dt><code>android:name</code></dt>
  * <dd>Defines the name of the path.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:pathData</code></dt>
  * <dd>Defines path data using exactly same format as "d" attribute
  * in the SVG's path data. This is defined in the viewport space.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:fillColor</code></dt>
  * <dd>Specifies the color used to fill the path. May be a color or, for SDK 24+, a color state list
- * or a gradient color. If this property is animated, any value set by the animation will
- * override the original value. No path fill is drawn if this property is not specified.</dd>
+ * or a gradient color (See {@link android.R.styleable#GradientColor}
+ * and {@link android.R.styleable#GradientColorItem}).
+ * If this property is animated, any value set by the animation will override the original value.
+ * No path fill is drawn if this property is not specified.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:strokeColor</code></dt>
  * <dd>Specifies the color used to draw the path outline. May be a color or, for SDK 24+, a color
- * state list or a gradient color. If this property is animated, any value set by the animation will
- * override the original value. No path outline is drawn if this property is not specified.</dd>
+ * state list or a gradient color (See {@link android.R.styleable#GradientColor}
+ * and {@link android.R.styleable#GradientColorItem}).
+ * If this property is animated, any value set by the animation will override the original value.
+ * No path outline is drawn if this property is not specified.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:strokeWidth</code></dt>
  * <dd>The width a path stroke.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:strokeAlpha</code></dt>
  * <dd>The opacity of a path stroke.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:fillAlpha</code></dt>
  * <dd>The opacity to fill the path with.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:trimPathStart</code></dt>
  * <dd>The fraction of the path to trim from the start, in the range from 0 to 1.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:trimPathEnd</code></dt>
  * <dd>The fraction of the path to trim from the end, in the range from 0 to 1.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:trimPathOffset</code></dt>
  * <dd>Shift trim region (allows showed region to include the start and end), in the range
  * from 0 to 1.</dd>
+ * <dd>Animatable : Yes.</dd>
  * <dt><code>android:strokeLineCap</code></dt>
  * <dd>Sets the linecap for a stroked path: butt, round, square.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:strokeLineJoin</code></dt>
  * <dd>Sets the lineJoin for a stroked path: miter,round,bevel.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:strokeMiterLimit</code></dt>
  * <dd>Sets the Miter limit for a stroked path.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:fillType</code></dt>
- * <dd>Sets the fillType for a path. It is the same as SVG's "fill-rule" properties.
- * For more details, see https://www.w3.org/TR/SVG/painting.html#FillRuleProperty</dd>
+ * <dd>Sets the fillType for a path. The types can be either "evenOdd" or "nonZero". They behave the
+ * same as SVG's "fill-rule" properties. For more details, see
+ * <a href="https://www.w3.org/TR/SVG/painting.html#FillRuleProperty">FillRuleProperty</a></dd>
+ * <dd>Animatable : No.</dd>
  * </dl></dd>
+ *
  * </dl>
  *
  * <dl>
@@ -175,9 +215,11 @@
  * <dl>
  * <dt><code>android:name</code></dt>
  * <dd>Defines the name of the clip path.</dd>
+ * <dd>Animatable : No.</dd>
  * <dt><code>android:pathData</code></dt>
  * <dd>Defines clip path using the same format as "d" attribute
  * in the SVG's path data.</dd>
+ * <dd>Animatable : Yes.</dd>
  * </dl></dd>
  * </dl>
  * <li>Here is a simple VectorDrawable in this vectordrawable.xml file.
@@ -198,7 +240,26 @@
  *             android:pathData=&quot;M300,70 l 0,-70 70,70 0,0 -70,70z&quot; /&gt;
  *     &lt;/group&gt;
  * &lt;/vector&gt;
- * </pre></li>
+ * </pre>
+ * </li>
+ * <li>And here is an example of linear gradient color, which is supported in SDK 24+.
+ * See more details in {@link android.R.styleable#GradientColor} and
+ * {@link android.R.styleable#GradientColorItem}.
+ * <pre>
+ * &lt;gradient xmlns:android="http://schemas.android.com/apk/res/android"
+ *     android:angle="90"
+ *     android:startColor="?android:attr/colorPrimary"
+ *     android:endColor="?android:attr/colorControlActivated"
+ *     android:centerColor="#f00"
+ *     android:startX="0"
+ *     android:startY="0"
+ *     android:endX="100"
+ *     android:endY="100"
+ *     android:type="linear"&gt;
+ * &lt;/gradient&gt;
+ * </pre>
+ * </li>
+ *
  */
 
 public class VectorDrawable extends Drawable {
@@ -232,7 +293,7 @@
     private final Rect mTmpBounds = new Rect();
 
     public VectorDrawable() {
-        this(new VectorDrawableState(), null);
+        this(new VectorDrawableState(null), null);
     }
 
     /**
@@ -683,7 +744,11 @@
         groupStack.push(state.mRootGroup);
 
         int eventType = parser.getEventType();
-        while (eventType != XmlPullParser.END_DOCUMENT) {
+        final int innerDepth = parser.getDepth() + 1;
+
+        // Parse everything until the end of the vector element.
+        while (eventType != XmlPullParser.END_DOCUMENT
+                && (parser.getDepth() >= innerDepth || eventType != XmlPullParser.END_TAG)) {
             if (eventType == XmlPullParser.START_TAG) {
                 final String tagName = parser.getName();
                 final VGroup currentGroup = groupStack.peek();
@@ -763,6 +828,13 @@
         return mVectorState.mAutoMirrored;
     }
 
+    /**
+     * @hide
+     */
+    public long getNativeTree() {
+        return mVectorState.getNativeRenderer();
+    }
+
     static class VectorDrawableState extends ConstantState {
         // Variables below need to be copied (deep copy if applicable) for mutation.
         int[] mThemeAttrs;
@@ -795,12 +867,33 @@
         int mLastSWCachePixelCount = 0;
         int mLastHWCachePixelCount = 0;
 
+        final static Property<VectorDrawableState, Float> ALPHA =
+                new FloatProperty<VectorDrawableState>("alpha") {
+                    @Override
+                    public void setValue(VectorDrawableState state, float value) {
+                        state.setAlpha(value);
+                    }
+
+                    @Override
+                    public Float get(VectorDrawableState state) {
+                        return state.getAlpha();
+                    }
+                };
+
+        Property getProperty(String propertyName) {
+            if (ALPHA.getName().equals(propertyName)) {
+                return ALPHA;
+            }
+            return null;
+        }
+
         // This tracks the total native allocation for all the nodes.
         private int mAllocationOfAllNodes = 0;
 
         private static final int NATIVE_ALLOCATION_SIZE = 316;
 
-        // Deep copy for mutate() or implicitly mutate.
+        // If copy is not null, deep copy the given VectorDrawableState. Otherwise, create a
+        // native vector drawable tree with an empty root group.
         public VectorDrawableState(VectorDrawableState copy) {
             if (copy != null) {
                 mThemeAttrs = copy.mThemeAttrs;
@@ -821,8 +914,11 @@
                 if (copy.mRootName != null) {
                     mVGTargetsMap.put(copy.mRootName, this);
                 }
-                onTreeConstructionFinished();
+            } else {
+                mRootGroup = new VGroup();
+                createNativeTree(mRootGroup);
             }
+            onTreeConstructionFinished();
         }
 
         private void createNativeTree(VGroup rootGroup) {
@@ -840,7 +936,8 @@
             VMRuntime.getRuntime().registerNativeAllocation(NATIVE_ALLOCATION_SIZE);
         }
 
-
+        // This should be called every time after a new RootGroup and all its subtrees are created
+        // (i.e. in constructors of VectorDrawableState and in inflate).
         void onTreeConstructionFinished() {
             mRootGroup.setTree(mNativeTree);
             mAllocationOfAllNodes = mRootGroup.getNativeSize();
@@ -888,11 +985,6 @@
                     || super.canApplyTheme();
         }
 
-        public VectorDrawableState() {
-            mRootGroup = new VGroup();
-            createNativeTree(mRootGroup);
-        }
-
         @Override
         public Drawable newDrawable() {
             return new VectorDrawable(this, null);
@@ -972,7 +1064,7 @@
     }
 
     static class VGroup extends VObject {
-        private static final int ROTATE_INDEX = 0;
+        private static final int ROTATION_INDEX = 0;
         private static final int PIVOT_X_INDEX = 1;
         private static final int PIVOT_Y_INDEX = 2;
         private static final int SCALE_X_INDEX = 3;
@@ -983,7 +1075,7 @@
 
         private static final int NATIVE_ALLOCATION_SIZE = 100;
 
-        private static final HashMap<String, Integer> sPropertyMap =
+        private static final HashMap<String, Integer> sPropertyIndexMap =
                 new HashMap<String, Integer>() {
                     {
                         put("translateX", TRANSLATE_X_INDEX);
@@ -992,19 +1084,123 @@
                         put("scaleY", SCALE_Y_INDEX);
                         put("pivotX", PIVOT_X_INDEX);
                         put("pivotY", PIVOT_Y_INDEX);
-                        put("rotation", ROTATE_INDEX);
+                        put("rotation", ROTATION_INDEX);
                     }
                 };
 
         static int getPropertyIndex(String propertyName) {
-            if (sPropertyMap.containsKey(propertyName)) {
-                return sPropertyMap.get(propertyName);
+            if (sPropertyIndexMap.containsKey(propertyName)) {
+                return sPropertyIndexMap.get(propertyName);
             } else {
                 // property not found
                 return -1;
             }
         }
 
+        // Below are the Properties that wrap the setters to avoid reflection overhead in animations
+        private static final Property<VGroup, Float> TRANSLATE_X =
+                new FloatProperty<VGroup> ("translateX") {
+                    @Override
+                    public void setValue(VGroup object, float value) {
+                        object.setTranslateX(value);
+                    }
+
+                    @Override
+                    public Float get(VGroup object) {
+                        return object.getTranslateX();
+                    }
+                };
+
+        private static final Property<VGroup, Float> TRANSLATE_Y =
+                new FloatProperty<VGroup> ("translateY") {
+                    @Override
+                    public void setValue(VGroup object, float value) {
+                        object.setTranslateY(value);
+                    }
+
+                    @Override
+                    public Float get(VGroup object) {
+                        return object.getTranslateY();
+                    }
+        };
+
+        private static final Property<VGroup, Float> SCALE_X =
+                new FloatProperty<VGroup> ("scaleX") {
+                    @Override
+                    public void setValue(VGroup object, float value) {
+                        object.setScaleX(value);
+                    }
+
+                    @Override
+                    public Float get(VGroup object) {
+                        return object.getScaleX();
+                    }
+                };
+
+        private static final Property<VGroup, Float> SCALE_Y =
+                new FloatProperty<VGroup> ("scaleY") {
+                    @Override
+                    public void setValue(VGroup object, float value) {
+                        object.setScaleY(value);
+                    }
+
+                    @Override
+                    public Float get(VGroup object) {
+                        return object.getScaleY();
+                    }
+                };
+
+        private static final Property<VGroup, Float> PIVOT_X =
+                new FloatProperty<VGroup> ("pivotX") {
+                    @Override
+                    public void setValue(VGroup object, float value) {
+                        object.setPivotX(value);
+                    }
+
+                    @Override
+                    public Float get(VGroup object) {
+                        return object.getPivotX();
+                    }
+                };
+
+        private static final Property<VGroup, Float> PIVOT_Y =
+                new FloatProperty<VGroup> ("pivotY") {
+                    @Override
+                    public void setValue(VGroup object, float value) {
+                        object.setPivotY(value);
+                    }
+
+                    @Override
+                    public Float get(VGroup object) {
+                        return object.getPivotY();
+                    }
+                };
+
+        private static final Property<VGroup, Float> ROTATION =
+                new FloatProperty<VGroup> ("rotation") {
+                    @Override
+                    public void setValue(VGroup object, float value) {
+                        object.setRotation(value);
+                    }
+
+                    @Override
+                    public Float get(VGroup object) {
+                        return object.getRotation();
+                    }
+                };
+
+        private static final HashMap<String, Property> sPropertyMap =
+                new HashMap<String, Property>() {
+                    {
+                        put("translateX", TRANSLATE_X);
+                        put("translateY", TRANSLATE_Y);
+                        put("scaleX", SCALE_X);
+                        put("scaleY", SCALE_Y);
+                        put("pivotX", PIVOT_X);
+                        put("pivotY", PIVOT_Y);
+                        put("rotation", ROTATION);
+                    }
+                };
         // Temp array to store transform values obtained from native.
         private float[] mTransform;
         /////////////////////////////////////////////////////
@@ -1060,6 +1256,15 @@
             mNativePtr = nCreateGroup();
         }
 
+        Property getProperty(String propertyName) {
+            if (sPropertyMap.containsKey(propertyName)) {
+                return sPropertyMap.get(propertyName);
+            } else {
+                // property not found
+                return null;
+            }
+        }
+
         public String getGroupName() {
             return mGroupName;
         }
@@ -1107,7 +1312,7 @@
                 throw new RuntimeException("Error: inconsistent property count");
             }
             float rotate = a.getFloat(R.styleable.VectorDrawableGroup_rotation,
-                    mTransform[ROTATE_INDEX]);
+                    mTransform[ROTATION_INDEX]);
             float pivotX = a.getFloat(R.styleable.VectorDrawableGroup_pivotX,
                     mTransform[PIVOT_X_INDEX]);
             float pivotY = a.getFloat(R.styleable.VectorDrawableGroup_pivotY,
@@ -1293,6 +1498,27 @@
         String mPathName;
         @Config int mChangingConfigurations;
 
+        private static final Property<VPath, PathParser.PathData> PATH_DATA =
+                new Property<VPath, PathParser.PathData>(PathParser.PathData.class, "pathData") {
+                    @Override
+                    public void set(VPath object, PathParser.PathData data) {
+                        object.setPathData(data);
+                    }
+
+                    @Override
+                    public PathParser.PathData get(VPath object) {
+                        return object.getPathData();
+                    }
+                };
+
+        Property getProperty(String propertyName) {
+            if (PATH_DATA.getName().equals(propertyName)) {
+                return PATH_DATA;
+            }
+            // property not found
+            return null;
+        }
+
         public VPath() {
             // Empty constructor.
         }
@@ -1415,7 +1641,7 @@
 
         private static final int NATIVE_ALLOCATION_SIZE = 264;
         // Property map for animatable attributes.
-        private final static HashMap<String, Integer> sPropertyMap
+        private final static HashMap<String, Integer> sPropertyIndexMap
                 = new HashMap<String, Integer> () {
             {
                 put("strokeWidth", STROKE_WIDTH_INDEX);
@@ -1429,6 +1655,125 @@
             }
         };
 
+        // Below are the Properties that wrap the setters to avoid reflection overhead in animations
+        private static final Property<VFullPath, Float> STROKE_WIDTH =
+                new FloatProperty<VFullPath> ("strokeWidth") {
+                    @Override
+                    public void setValue(VFullPath object, float value) {
+                        object.setStrokeWidth(value);
+                    }
+
+                    @Override
+                    public Float get(VFullPath object) {
+                        return object.getStrokeWidth();
+                    }
+                };
+
+        private static final Property<VFullPath, Integer> STROKE_COLOR =
+                new IntProperty<VFullPath> ("strokeColor") {
+                    @Override
+                    public void setValue(VFullPath object, int value) {
+                        object.setStrokeColor(value);
+                    }
+
+                    @Override
+                    public Integer get(VFullPath object) {
+                        return object.getStrokeColor();
+                    }
+                };
+
+        private static final Property<VFullPath, Float> STROKE_ALPHA =
+                new FloatProperty<VFullPath> ("strokeAlpha") {
+                    @Override
+                    public void setValue(VFullPath object, float value) {
+                        object.setStrokeAlpha(value);
+                    }
+
+                    @Override
+                    public Float get(VFullPath object) {
+                        return object.getStrokeAlpha();
+                    }
+                };
+
+        private static final Property<VFullPath, Integer> FILL_COLOR =
+                new IntProperty<VFullPath>("fillColor") {
+                    @Override
+                    public void setValue(VFullPath object, int value) {
+                        object.setFillColor(value);
+                    }
+
+                    @Override
+                    public Integer get(VFullPath object) {
+                        return object.getFillColor();
+                    }
+                };
+
+        private static final Property<VFullPath, Float> FILL_ALPHA =
+                new FloatProperty<VFullPath> ("fillAlpha") {
+                    @Override
+                    public void setValue(VFullPath object, float value) {
+                        object.setFillAlpha(value);
+                    }
+
+                    @Override
+                    public Float get(VFullPath object) {
+                        return object.getFillAlpha();
+                    }
+                };
+
+        private static final Property<VFullPath, Float> TRIM_PATH_START =
+                new FloatProperty<VFullPath> ("trimPathStart") {
+                    @Override
+                    public void setValue(VFullPath object, float value) {
+                        object.setTrimPathStart(value);
+                    }
+
+                    @Override
+                    public Float get(VFullPath object) {
+                        return object.getTrimPathStart();
+                    }
+                };
+
+        private static final Property<VFullPath, Float> TRIM_PATH_END =
+                new FloatProperty<VFullPath> ("trimPathEnd") {
+                    @Override
+                    public void setValue(VFullPath object, float value) {
+                        object.setTrimPathEnd(value);
+                    }
+
+                    @Override
+                    public Float get(VFullPath object) {
+                        return object.getTrimPathEnd();
+                    }
+                };
+
+        private static final Property<VFullPath, Float> TRIM_PATH_OFFSET =
+                new FloatProperty<VFullPath> ("trimPathOffset") {
+                    @Override
+                    public void setValue(VFullPath object, float value) {
+                        object.setTrimPathOffset(value);
+                    }
+
+                    @Override
+                    public Float get(VFullPath object) {
+                        return object.getTrimPathOffset();
+                    }
+                };
+
+        private final static HashMap<String, Property> sPropertyMap
+                = new HashMap<String, Property> () {
+            {
+                put("strokeWidth", STROKE_WIDTH);
+                put("strokeColor", STROKE_COLOR);
+                put("strokeAlpha", STROKE_ALPHA);
+                put("fillColor", FILL_COLOR);
+                put("fillAlpha", FILL_ALPHA);
+                put("trimPathStart", TRIM_PATH_START);
+                put("trimPathEnd", TRIM_PATH_END);
+                put("trimPathOffset", TRIM_PATH_OFFSET);
+            }
+        };
+
         // Temp array to store property data obtained from native getter.
         private byte[] mPropertyData;
         /////////////////////////////////////////////////////
@@ -1451,11 +1796,24 @@
             mFillColors = copy.mFillColors;
         }
 
+        Property getProperty(String propertyName) {
+            Property p = super.getProperty(propertyName);
+            if (p != null) {
+                return p;
+            }
+            if (sPropertyMap.containsKey(propertyName)) {
+                return sPropertyMap.get(propertyName);
+            } else {
+                // property not found
+                return null;
+            }
+        }
+
         int getPropertyIndex(String propertyName) {
-            if (!sPropertyMap.containsKey(propertyName)) {
+            if (!sPropertyIndexMap.containsKey(propertyName)) {
                 return -1;
             } else {
-                return sPropertyMap.get(propertyName);
+                return sPropertyIndexMap.get(propertyName);
             }
         }
 
@@ -1789,6 +2147,7 @@
         abstract boolean onStateChange(int[] state);
         abstract boolean isStateful();
         abstract int getNativeSize();
+        abstract Property getProperty(String propertyName);
     }
 
     private static native long nCreateTree(long rootGroupPtr);
diff --git a/graphics/java/android/graphics/pdf/PdfEditor.java b/graphics/java/android/graphics/pdf/PdfEditor.java
index 2b70b6a..cd1f8de 100644
--- a/graphics/java/android/graphics/pdf/PdfEditor.java
+++ b/graphics/java/android/graphics/pdf/PdfEditor.java
@@ -79,8 +79,12 @@
         }
 
         mInput = input;
-        mNativeDocument = nativeOpen(mInput.getFd(), size);
-        mPageCount = nativeGetPageCount(mNativeDocument);
+
+        synchronized (PdfRenderer.sPdfiumLock) {
+            mNativeDocument = nativeOpen(mInput.getFd(), size);
+            mPageCount = nativeGetPageCount(mNativeDocument);
+        }
+
         mCloseGuard.open("close");
     }
 
@@ -102,7 +106,10 @@
     public void removePage(int pageIndex) {
         throwIfClosed();
         throwIfPageNotInDocument(pageIndex);
-        mPageCount = nativeRemovePage(mNativeDocument, pageIndex);
+
+        synchronized (PdfRenderer.sPdfiumLock) {
+            mPageCount = nativeRemovePage(mNativeDocument, pageIndex);
+        }
     }
 
     /**
@@ -125,11 +132,16 @@
         if (clip == null) {
             Point size = new Point();
             getPageSize(pageIndex, size);
-            nativeSetTransformAndClip(mNativeDocument, pageIndex, transform.native_instance,
-                    0, 0, size.x, size.y);
+
+            synchronized (PdfRenderer.sPdfiumLock) {
+                nativeSetTransformAndClip(mNativeDocument, pageIndex, transform.native_instance,
+                        0, 0, size.x, size.y);
+            }
         } else {
-            nativeSetTransformAndClip(mNativeDocument, pageIndex, transform.native_instance,
-                    clip.left, clip.top, clip.right, clip.bottom);
+            synchronized (PdfRenderer.sPdfiumLock) {
+                nativeSetTransformAndClip(mNativeDocument, pageIndex, transform.native_instance,
+                        clip.left, clip.top, clip.right, clip.bottom);
+            }
         }
     }
 
@@ -143,7 +155,10 @@
         throwIfClosed();
         throwIfOutSizeNull(outSize);
         throwIfPageNotInDocument(pageIndex);
-        nativeGetPageSize(mNativeDocument, pageIndex, outSize);
+
+        synchronized (PdfRenderer.sPdfiumLock) {
+            nativeGetPageSize(mNativeDocument, pageIndex, outSize);
+        }
     }
 
     /**
@@ -156,7 +171,10 @@
         throwIfClosed();
         throwIfOutMediaBoxNull(outMediaBox);
         throwIfPageNotInDocument(pageIndex);
-        return nativeGetPageMediaBox(mNativeDocument, pageIndex, outMediaBox);
+
+        synchronized (PdfRenderer.sPdfiumLock) {
+            return nativeGetPageMediaBox(mNativeDocument, pageIndex, outMediaBox);
+        }
     }
 
     /**
@@ -169,7 +187,10 @@
         throwIfClosed();
         throwIfMediaBoxNull(mediaBox);
         throwIfPageNotInDocument(pageIndex);
-        nativeSetPageMediaBox(mNativeDocument, pageIndex, mediaBox);
+
+        synchronized (PdfRenderer.sPdfiumLock) {
+            nativeSetPageMediaBox(mNativeDocument, pageIndex, mediaBox);
+        }
     }
 
     /**
@@ -182,7 +203,10 @@
         throwIfClosed();
         throwIfOutCropBoxNull(outCropBox);
         throwIfPageNotInDocument(pageIndex);
-        return nativeGetPageCropBox(mNativeDocument, pageIndex, outCropBox);
+
+        synchronized (PdfRenderer.sPdfiumLock) {
+            return nativeGetPageCropBox(mNativeDocument, pageIndex, outCropBox);
+        }
     }
 
     /**
@@ -195,7 +219,10 @@
         throwIfClosed();
         throwIfCropBoxNull(cropBox);
         throwIfPageNotInDocument(pageIndex);
-        nativeSetPageCropBox(mNativeDocument, pageIndex, cropBox);
+
+        synchronized (PdfRenderer.sPdfiumLock) {
+            nativeSetPageCropBox(mNativeDocument, pageIndex, cropBox);
+        }
     }
 
     /**
@@ -205,7 +232,10 @@
      */
     public boolean shouldScaleForPrinting() {
         throwIfClosed();
-        return nativeScaleForPrinting(mNativeDocument);
+
+        synchronized (PdfRenderer.sPdfiumLock) {
+            return nativeScaleForPrinting(mNativeDocument);
+        }
     }
 
     /**
@@ -219,7 +249,10 @@
     public void write(ParcelFileDescriptor output) throws IOException {
         try {
             throwIfClosed();
-            nativeWrite(mNativeDocument, output.getFd());
+
+            synchronized (PdfRenderer.sPdfiumLock) {
+                nativeWrite(mNativeDocument, output.getFd());
+            }
         } finally {
             IoUtils.closeQuietly(output);
         }
@@ -247,7 +280,9 @@
     }
 
     private void doClose() {
-        nativeClose(mNativeDocument);
+        synchronized (PdfRenderer.sPdfiumLock) {
+            nativeClose(mNativeDocument);
+        }
         IoUtils.closeQuietly(mInput);
         mInput = null;
         mCloseGuard.close();
diff --git a/graphics/java/android/graphics/pdf/PdfRenderer.java b/graphics/java/android/graphics/pdf/PdfRenderer.java
index 520ebe5..cfc1309 100644
--- a/graphics/java/android/graphics/pdf/PdfRenderer.java
+++ b/graphics/java/android/graphics/pdf/PdfRenderer.java
@@ -99,6 +99,12 @@
  * @see #close()
  */
 public final class PdfRenderer implements AutoCloseable {
+    /**
+     * Any call the native pdfium code has to be single threaded as the library does not support
+     * parallel use.
+     */
+    final static Object sPdfiumLock = new Object();
+
     private final CloseGuard mCloseGuard = CloseGuard.get();
 
     private final Point mTempPoint = new Point();
@@ -154,8 +160,12 @@
         }
 
         mInput = input;
-        mNativeDocument = nativeCreate(mInput.getFd(), size);
-        mPageCount = nativeGetPageCount(mNativeDocument);
+
+        synchronized (sPdfiumLock) {
+            mNativeDocument = nativeCreate(mInput.getFd(), size);
+            mPageCount = nativeGetPageCount(mNativeDocument);
+        }
+
         mCloseGuard.open("close");
     }
 
@@ -189,7 +199,10 @@
      */
     public boolean shouldScaleForPrinting() {
         throwIfClosed();
-        return nativeScaleForPrinting(mNativeDocument);
+
+        synchronized (sPdfiumLock) {
+            return nativeScaleForPrinting(mNativeDocument);
+        }
     }
 
     /**
@@ -224,7 +237,9 @@
         if (mCurrentPage != null) {
             mCurrentPage.close();
         }
-        nativeClose(mNativeDocument);
+        synchronized (sPdfiumLock) {
+            nativeClose(mNativeDocument);
+        }
         try {
             mInput.close();
         } catch (IOException ioe) {
@@ -277,7 +292,9 @@
 
         private Page(int index) {
             Point size = mTempPoint;
-            mNativePage = nativeOpenPageAndGetSize(mNativeDocument, index, size);
+            synchronized (sPdfiumLock) {
+                mNativePage = nativeOpenPageAndGetSize(mNativeDocument, index, size);
+            }
             mIndex = index;
             mWidth = size.x;
             mHeight = size.y;
@@ -384,8 +401,10 @@
 
             final long transformPtr = (transform != null) ? transform.native_instance : 0;
 
-            nativeRenderPage(mNativeDocument, mNativePage, destination, contentLeft,
-                    contentTop, contentRight, contentBottom, transformPtr, renderMode);
+            synchronized (sPdfiumLock) {
+                nativeRenderPage(mNativeDocument, mNativePage, destination, contentLeft,
+                        contentTop, contentRight, contentBottom, transformPtr, renderMode);
+            }
         }
 
         /**
@@ -412,7 +431,9 @@
         }
 
         private void doClose() {
-            nativeClosePage(mNativePage);
+            synchronized (sPdfiumLock) {
+                nativeClosePage(mNativePage);
+            }
             mNativePage = 0;
             mCloseGuard.close();
             mCurrentPage = null;
diff --git a/include/androidfw/AssetManager.h b/include/androidfw/AssetManager.h
index 914ac3d..099d82e 100644
--- a/include/androidfw/AssetManager.h
+++ b/include/androidfw/AssetManager.h
@@ -72,6 +72,12 @@
     static const char* RESOURCES_FILENAME;
     static const char* IDMAP_BIN;
     static const char* OVERLAY_DIR;
+    /*
+     * If OVERLAY_SKU_DIR_PROPERTY is set, search for runtime resource overlay
+     * APKs in OVERLAY_DIR/<value of OVERLAY_SKU_DIR_PROPERTY> rather than in
+     * OVERLAY_DIR.
+     */
+    static const char* OVERLAY_SKU_DIR_PROPERTY;
     static const char* TARGET_PACKAGE_NAME;
     static const char* TARGET_APK_PATH;
     static const char* IDMAP_DIR;
diff --git a/keystore/java/android/security/KeyChain.java b/keystore/java/android/security/KeyChain.java
index cce58c2..a96ca39 100644
--- a/keystore/java/android/security/KeyChain.java
+++ b/keystore/java/android/security/KeyChain.java
@@ -20,6 +20,7 @@
 import android.annotation.WorkerThread;
 import android.app.Activity;
 import android.app.PendingIntent;
+import android.app.Service;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -356,6 +357,9 @@
      *
      * <p> This method may block while waiting for a connection to another process, and must never
      * be called from the main thread.
+     * <p> As {@link Activity} and {@link Service} contexts are short-lived and can be destroyed
+     * at any time from the main thread, it is safer to rely on a long-lived context such as one
+     * returned from {@link Context#getApplicationContext()}.
      *
      * @param alias The alias of the desired private key, typically returned via
      *              {@link KeyChainAliasCallback#alias}.
@@ -368,7 +372,7 @@
         if (alias == null) {
             throw new NullPointerException("alias == null");
         }
-        KeyChainConnection keyChainConnection = bind(context);
+        KeyChainConnection keyChainConnection = bind(context.getApplicationContext());
         try {
             final IKeyChainService keyChainService = keyChainConnection.getService();
             final String keyId = keyChainService.requestPrivateKey(alias);
@@ -400,6 +404,9 @@
      *
      * <p> This method may block while waiting for a connection to another process, and must never
      * be called from the main thread.
+     * <p> As {@link Activity} and {@link Service} contexts are short-lived and can be destroyed
+     * at any time from the main thread, it is safer to rely on a long-lived context such as one
+     * returned from {@link Context#getApplicationContext()}.
      *
      * @param alias The alias of the desired certificate chain, typically
      * returned via {@link KeyChainAliasCallback#alias}.
@@ -412,7 +419,7 @@
         if (alias == null) {
             throw new NullPointerException("alias == null");
         }
-        KeyChainConnection keyChainConnection = bind(context);
+        KeyChainConnection keyChainConnection = bind(context.getApplicationContext());
         try {
             IKeyChainService keyChainService = keyChainConnection.getService();
 
diff --git a/keystore/java/android/security/KeyStore.java b/keystore/java/android/security/KeyStore.java
index 70e4b6f..00d786a 100644
--- a/keystore/java/android/security/KeyStore.java
+++ b/keystore/java/android/security/KeyStore.java
@@ -626,6 +626,16 @@
         }
     }
 
+    /**
+     * Notify keystore that the device went off-body.
+     */
+    public void onDeviceOffBody() {
+        try {
+            mBinder.onDeviceOffBody();
+        } catch (RemoteException e) {
+            Log.w(TAG, "Cannot connect to keystore", e);
+        }
+    }
 
     /**
      * Returns a {@link KeyStoreException} corresponding to the provided keystore/keymaster error
diff --git a/keystore/java/android/security/keystore/AndroidKeyStoreProvider.java b/keystore/java/android/security/keystore/AndroidKeyStoreProvider.java
index 8c20ddc..f36c00c 100644
--- a/keystore/java/android/security/keystore/AndroidKeyStoreProvider.java
+++ b/keystore/java/android/security/keystore/AndroidKeyStoreProvider.java
@@ -228,7 +228,7 @@
         if (exportResult.resultCode != KeyStore.NO_ERROR) {
             throw (UnrecoverableKeyException)
                     new UnrecoverableKeyException("Failed to obtain X.509 form of public key")
-                    .initCause(KeyStore.getKeyStoreException(errorCode));
+                    .initCause(KeyStore.getKeyStoreException(exportResult.resultCode));
         }
         final byte[] x509EncodedPublicKey = exportResult.exportData;
 
diff --git a/libs/androidfw/AssetManager.cpp b/libs/androidfw/AssetManager.cpp
index 8ea25d6..0439898 100644
--- a/libs/androidfw/AssetManager.cpp
+++ b/libs/androidfw/AssetManager.cpp
@@ -76,6 +76,7 @@
 const char* AssetManager::RESOURCES_FILENAME = "resources.arsc";
 const char* AssetManager::IDMAP_BIN = "/system/bin/idmap";
 const char* AssetManager::OVERLAY_DIR = "/vendor/overlay";
+const char* AssetManager::OVERLAY_SKU_DIR_PROPERTY = "ro.boot.vendor.overlay.sku";
 const char* AssetManager::TARGET_PACKAGE_NAME = "android";
 const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk";
 const char* AssetManager::IDMAP_DIR = "/data/resource-cache";
diff --git a/libs/hwui/Android.mk b/libs/hwui/Android.mk
index 5dcfa4e..4e9765d 100644
--- a/libs/hwui/Android.mk
+++ b/libs/hwui/Android.mk
@@ -3,6 +3,7 @@
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
 
 HWUI_NEW_OPS := true
+BUGREPORT_FONT_CACHE_USAGE := true
 
 # Enables fine-grained GLES error checking
 # If set to true, every GLES call is wrapped & error checked
@@ -113,6 +114,10 @@
     -DATRACE_TAG=ATRACE_TAG_VIEW -DLOG_TAG=\"OpenGLRenderer\" \
     -Wall -Wno-unused-parameter -Wunreachable-code -Werror
 
+ifeq ($(TARGET_USES_HWC2),true)
+    hwui_cflags += -DUSE_HWC2
+endif
+
 # GCC false-positives on this warning, and since we -Werror that's
 # a problem
 hwui_cflags += -Wno-free-nonheap-object
@@ -131,6 +136,13 @@
 
 endif
 
+ifeq (true, $(BUGREPORT_FONT_CACHE_USAGE))
+    hwui_src_files += \
+        font/FontCacheHistoryTracker.cpp
+    hwui_cflags += -DBUGREPORT_FONT_CACHE_USAGE
+endif
+
+
 ifndef HWUI_COMPILE_SYMBOLS
     hwui_cflags += -fvisibility=hidden
 endif
diff --git a/libs/hwui/AnimationContext.h b/libs/hwui/AnimationContext.h
index 395dc73..71ee860 100644
--- a/libs/hwui/AnimationContext.h
+++ b/libs/hwui/AnimationContext.h
@@ -100,6 +100,8 @@
 
     ANDROID_API virtual void destroy();
 
+    ANDROID_API virtual void pauseAnimators() {}
+
 private:
     friend class AnimationHandle;
     void addAnimationHandle(AnimationHandle* handle);
diff --git a/libs/hwui/Animator.cpp b/libs/hwui/Animator.cpp
index 4d65782..74aa303 100644
--- a/libs/hwui/Animator.cpp
+++ b/libs/hwui/Animator.cpp
@@ -123,22 +123,27 @@
         mPlayTime = (mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) ?
                         mPlayTime : 0;
         mPlayState = PlayState::Running;
+        mPendingActionUponFinish = Action::None;
         break;
     case Request::Reverse:
         mPlayTime = (mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) ?
                         mPlayTime : mDuration;
         mPlayState = PlayState::Reversing;
+        mPendingActionUponFinish = Action::None;
         break;
     case Request::Reset:
         mPlayTime = 0;
         mPlayState = PlayState::Finished;
+        mPendingActionUponFinish = Action::Reset;
         break;
     case Request::Cancel:
         mPlayState = PlayState::Finished;
+        mPendingActionUponFinish = Action::None;
         break;
     case Request::End:
         mPlayTime = mPlayState == PlayState::Reversing ? 0 : mDuration;
         mPlayState = PlayState::Finished;
+        mPendingActionUponFinish = Action::End;
         break;
     default:
         LOG_ALWAYS_FATAL("Invalid staging request: %d", static_cast<int>(request));
@@ -176,8 +181,6 @@
         mStagingRequests.clear();
 
         if (mStagingPlayState == PlayState::Finished) {
-            // Set the staging play time and end the animation
-            updatePlayTime(mPlayTime);
             callOnFinishedListener(context);
         } else if (mStagingPlayState == PlayState::Running
                 || mStagingPlayState == PlayState::Reversing) {
@@ -236,6 +239,15 @@
         return false;
     }
     if (mPlayState == PlayState::Finished) {
+        if (mPendingActionUponFinish == Action::Reset) {
+            // Skip to start.
+            updatePlayTime(0);
+        } else if (mPendingActionUponFinish == Action::End) {
+            // Skip to end.
+            updatePlayTime(mDuration);
+        }
+        // Reset pending action.
+        mPendingActionUponFinish = Action ::None;
         return true;
     }
 
@@ -274,6 +286,10 @@
     return playTime >= mDuration;
 }
 
+nsecs_t BaseRenderNodeAnimator::getRemainingPlayTime() {
+    return mPlayState == PlayState::Reversing ? mPlayTime : mDuration - mPlayTime;
+}
+
 void BaseRenderNodeAnimator::forceEndNow(AnimationContext& context) {
     if (mPlayState < PlayState::Finished) {
         mPlayState = PlayState::Finished;
diff --git a/libs/hwui/Animator.h b/libs/hwui/Animator.h
index 1954b88..aac355c 100644
--- a/libs/hwui/Animator.h
+++ b/libs/hwui/Animator.h
@@ -44,6 +44,12 @@
     ANDROID_API virtual ~AnimationListener() {}
 };
 
+enum class RepeatMode {
+    // These are the same values as the RESTART and REVERSE in ValueAnimator.java.
+    Restart = 1,
+    Reverse = 2
+};
+
 class BaseRenderNodeAnimator : public VirtualLightRefBase {
     PREVENT_COPY_AND_ASSIGN(BaseRenderNodeAnimator);
 public:
@@ -62,19 +68,23 @@
     }
     bool mayRunAsync() { return mMayRunAsync; }
     ANDROID_API void start();
-    ANDROID_API void reset();
+    ANDROID_API virtual void reset();
     ANDROID_API void reverse();
     // Terminates the animation at its current progress.
     ANDROID_API void cancel();
 
     // Terminates the animation and skip to the end of the animation.
-    ANDROID_API void end();
+    ANDROID_API virtual void end();
 
     void attach(RenderNode* target);
     virtual void onAttached() {}
     void detach() { mTarget = nullptr; }
-    void pushStaging(AnimationContext& context);
-    bool animate(AnimationContext& context);
+    ANDROID_API void pushStaging(AnimationContext& context);
+    ANDROID_API bool animate(AnimationContext& context);
+
+    // Returns the remaining time in ms for the animation. Note this should only be called during
+    // an animation on RenderThread.
+    ANDROID_API nsecs_t getRemainingPlayTime();
 
     bool isRunning() { return mPlayState == PlayState::Running
             || mPlayState == PlayState::Reversing; }
@@ -155,6 +165,17 @@
         Cancel,
         End
     };
+
+    // Defines different actions upon finish.
+    enum class Action {
+        // For animations that got canceled or finished normally. no more action needs to be done.
+        None,
+        // For animations that get reset, the reset will happen in the next animation pulse.
+        Reset,
+        // For animations being ended, in the next animation pulse the animation will skip to end.
+        End
+    };
+
     inline void checkMutable();
     virtual void transitionToRunning(AnimationContext& context);
     void doSetStartValue(float value);
@@ -162,7 +183,7 @@
     void resolveStagingRequest(Request request);
 
     std::vector<Request> mStagingRequests;
-
+    Action mPendingActionUponFinish = Action::None;
 };
 
 class RenderPropertyAnimator : public BaseRenderNodeAnimator {
diff --git a/libs/hwui/Caches.cpp b/libs/hwui/Caches.cpp
index 949ad45..a8ced9b 100644
--- a/libs/hwui/Caches.cpp
+++ b/libs/hwui/Caches.cpp
@@ -21,6 +21,9 @@
 #include "Properties.h"
 #include "renderstate/RenderState.h"
 #include "ShadowTessellator.h"
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+#include "font/FontCacheHistoryTracker.h"
+#endif
 #include "utils/GLUtils.h"
 
 #include <cutils/properties.h>
@@ -195,12 +198,7 @@
     log.appendFormat("  PatchCache           %8d / %8d\n",
             patchCache.getSize(), patchCache.getMaxSize());
 
-    const uint32_t sizeA8 = fontRenderer.getFontRendererSize(GL_ALPHA);
-    const uint32_t sizeRGBA = fontRenderer.getFontRendererSize(GL_RGBA);
-    log.appendFormat("  FontRenderer A8    %8d / %8d\n", sizeA8, sizeA8);
-    log.appendFormat("  FontRenderer RGBA  %8d / %8d\n", sizeRGBA, sizeRGBA);
-    log.appendFormat("  FontRenderer total %8d / %8d\n", sizeA8 + sizeRGBA,
-            sizeA8 + sizeRGBA);
+    fontRenderer.dumpMemoryUsage(log);
 
     log.appendFormat("Other:\n");
     log.appendFormat("  FboCache             %8d / %8d\n",
@@ -213,11 +211,14 @@
     total += tessellationCache.getSize();
     total += dropShadowCache.getSize();
     total += patchCache.getSize();
-    total += fontRenderer.getFontRendererSize(GL_ALPHA);
-    total += fontRenderer.getFontRendererSize(GL_RGBA);
+    total += fontRenderer.getSize();
 
     log.appendFormat("Total memory usage:\n");
     log.appendFormat("  %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
+
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+    fontRenderer.getFontRenderer().historyTracker().dump(log);
+#endif
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/libs/hwui/ClipArea.cpp b/libs/hwui/ClipArea.cpp
index fe68239..84451ba 100644
--- a/libs/hwui/ClipArea.cpp
+++ b/libs/hwui/ClipArea.cpp
@@ -464,10 +464,7 @@
             }
             case ClipMode::Region:
                 other = getRegion(recordedClip);
-
-                // TODO: handle non-translate transforms properly!
-                other.translate(recordedClipTransform.getTranslateX(),
-                        recordedClipTransform.getTranslateY());
+                applyTransformToRegion(recordedClipTransform, &other);
             }
 
             ClipRegion* regionClip = allocator.create<ClipRegion>();
@@ -527,11 +524,29 @@
         }
     } else {
         SkRegion region(getRegion(clip));
-        // TODO: handle non-translate transforms properly!
-        region.translate(transform.getTranslateX(), transform.getTranslateY());
+        applyTransformToRegion(transform, &region);
         clipRegion(region, SkRegion::kIntersect_Op);
     }
 }
 
+void ClipArea::applyTransformToRegion(const Matrix4& transform, SkRegion* region) {
+    if (transform.rectToRect() && !transform.isPureTranslate()) {
+        // handle matrices with scale manually by mapping each rect
+        SkRegion other;
+        SkRegion::Iterator it(*region);
+        while (!it.done()) {
+            Rect rect(it.rect());
+            transform.mapRect(rect);
+            rect.snapGeometryToPixelBoundaries(true);
+            other.op(rect.left, rect.top, rect.right, rect.bottom, SkRegion::kUnion_Op);
+            it.next();
+        }
+        region->swap(other);
+    } else {
+        // TODO: handle non-translate transforms properly!
+        region->translate(transform.getTranslateX(), transform.getTranslateY());
+    }
+}
+
 } /* namespace uirenderer */
 } /* namespace android */
diff --git a/libs/hwui/ClipArea.h b/libs/hwui/ClipArea.h
index 32ab501..2e56160 100644
--- a/libs/hwui/ClipArea.h
+++ b/libs/hwui/ClipArea.h
@@ -179,6 +179,8 @@
             const ClipBase* recordedClip, const Matrix4& recordedClipTransform);
     void applyClip(const ClipBase* recordedClip, const Matrix4& recordedClipTransform);
 
+    static void applyTransformToRegion(const Matrix4& transform, SkRegion* region);
+
 private:
     void enterRectangleMode();
     void rectangleModeClipRectWithTransform(const Rect& r, const mat4* transform, SkRegion::Op op);
diff --git a/libs/hwui/DisplayList.cpp b/libs/hwui/DisplayList.cpp
index b572bda..28be05c 100644
--- a/libs/hwui/DisplayList.cpp
+++ b/libs/hwui/DisplayList.cpp
@@ -45,7 +45,7 @@
         , regions(stdAllocator)
         , referenceHolders(stdAllocator)
         , functors(stdAllocator)
-        , pushStagingFunctors(stdAllocator)
+        , vectorDrawables(stdAllocator)
         , hasDrawOps(false) {
 }
 
diff --git a/libs/hwui/DisplayList.h b/libs/hwui/DisplayList.h
index 5b3227b..ccf71c6 100644
--- a/libs/hwui/DisplayList.h
+++ b/libs/hwui/DisplayList.h
@@ -69,6 +69,11 @@
 typedef DrawRenderNodeOp NodeOpType;
 #endif
 
+namespace VectorDrawable {
+class Tree;
+};
+typedef uirenderer::VectorDrawable::Tree VectorDrawableRoot;
+
 /**
  * Holds data used in the playback a tree of DisplayLists.
  */
@@ -110,16 +115,6 @@
     LinearAllocator mReplayAllocator;
 };
 
-/**
- * Functor that can be used for objects with data in both UI thread and RT to keep the data
- * in sync. This functor, when added to DisplayList, will be call during DisplayList sync.
- */
-struct PushStagingFunctor {
-    PushStagingFunctor() {}
-    virtual ~PushStagingFunctor() {}
-    virtual void operator ()() {}
-};
-
 struct FunctorContainer {
     Functor* functor;
     GlFunctorLifecycleListener* listener;
@@ -161,7 +156,7 @@
 
     const LsaVector<const SkBitmap*>& getBitmapResources() const { return bitmapResources; }
     const LsaVector<FunctorContainer>& getFunctors() const { return functors; }
-    const LsaVector<PushStagingFunctor*>& getPushStagingFunctors() { return pushStagingFunctors; }
+    const LsaVector<VectorDrawableRoot*>& getVectorDrawables() { return vectorDrawables; }
 
     size_t addChild(NodeOpType* childOp);
 
@@ -203,10 +198,10 @@
     // List of functors
     LsaVector<FunctorContainer> functors;
 
-    // List of functors that need to be notified of pushStaging. Note that this list gets nothing
+    // List of VectorDrawables that need to be notified of pushStaging. Note that this list gets nothing
     // but a callback during sync DisplayList, unlike the list of functors defined above, which
     // gets special treatment exclusive for webview.
-    LsaVector<PushStagingFunctor*> pushStagingFunctors;
+    LsaVector<VectorDrawableRoot*> vectorDrawables;
 
     bool hasDrawOps; // only used if !HWUI_NEW_OPS
 
diff --git a/libs/hwui/DisplayListCanvas.cpp b/libs/hwui/DisplayListCanvas.cpp
index ca968ce..bec66295 100644
--- a/libs/hwui/DisplayListCanvas.cpp
+++ b/libs/hwui/DisplayListCanvas.cpp
@@ -417,7 +417,7 @@
 
 void DisplayListCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
     mDisplayList->ref(tree);
-    mDisplayList->pushStagingFunctors.push_back(tree->getFunctor());
+    mDisplayList->vectorDrawables.push_back(tree);
     addDrawOp(new (alloc()) DrawVectorDrawableOp(tree, tree->stagingProperties()->getBounds()));
 }
 
diff --git a/libs/hwui/FontRenderer.cpp b/libs/hwui/FontRenderer.cpp
index 276c18d..681cf55 100644
--- a/libs/hwui/FontRenderer.cpp
+++ b/libs/hwui/FontRenderer.cpp
@@ -168,10 +168,17 @@
 
     for (uint32_t i = 0; i < mACacheTextures.size(); i++) {
         mACacheTextures[i]->init();
+
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+        mHistoryTracker.glyphsCleared(mACacheTextures[i]);
+#endif
     }
 
     for (uint32_t i = 0; i < mRGBACacheTextures.size(); i++) {
         mRGBACacheTextures[i]->init();
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+        mHistoryTracker.glyphsCleared(mRGBACacheTextures[i]);
+#endif
     }
 
     mDrawn = false;
@@ -183,6 +190,9 @@
         CacheTexture* cacheTexture = cacheTextures[i];
         if (cacheTexture->getPixelBuffer()) {
             cacheTexture->init();
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+            mHistoryTracker.glyphsCleared(cacheTexture);
+#endif
             LruCache<Font::FontDescription, Font*>::Iterator it(mActiveFonts);
             while (it.next()) {
                 it.value()->invalidateTextureCache(cacheTexture);
@@ -385,6 +395,10 @@
     }
 
     cachedGlyph->mIsValid = true;
+
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+    mHistoryTracker.glyphUploaded(cacheTexture, startX, startY, glyph.fWidth, glyph.fHeight);
+#endif
 }
 
 CacheTexture* FontRenderer::createCacheTexture(int width, int height, GLenum format,
@@ -747,18 +761,67 @@
     return size;
 }
 
-uint32_t FontRenderer::getCacheSize(GLenum format) const {
-    switch (format) {
-        case GL_ALPHA: {
-            return calculateCacheSize(mACacheTextures);
-        }
-        case GL_RGBA: {
-            return calculateCacheSize(mRGBACacheTextures);
-        }
-        default: {
-            return 0;
+static uint32_t calculateFreeCacheSize(const std::vector<CacheTexture*>& cacheTextures) {
+    uint32_t size = 0;
+    for (uint32_t i = 0; i < cacheTextures.size(); i++) {
+        CacheTexture* cacheTexture = cacheTextures[i];
+        if (cacheTexture && cacheTexture->getPixelBuffer()) {
+            size += cacheTexture->calculateFreeMemory();
         }
     }
+    return size;
+}
+
+const std::vector<CacheTexture*>& FontRenderer::cacheTexturesForFormat(GLenum format) const {
+    switch (format) {
+        case GL_ALPHA: {
+            return mACacheTextures;
+        }
+        case GL_RGBA: {
+            return mRGBACacheTextures;
+        }
+        default: {
+            LOG_ALWAYS_FATAL("Unsupported format: %d", format);
+            // Impossible to hit this, but the compiler doesn't know that
+            return *(new std::vector<CacheTexture*>());
+        }
+    }
+}
+
+static void dumpTextures(String8& log, const char* tag,
+        const std::vector<CacheTexture*>& cacheTextures) {
+    for (uint32_t i = 0; i < cacheTextures.size(); i++) {
+        CacheTexture* cacheTexture = cacheTextures[i];
+        if (cacheTexture && cacheTexture->getPixelBuffer()) {
+            uint32_t free = cacheTexture->calculateFreeMemory();
+            uint32_t total = cacheTexture->getPixelBuffer()->getSize();
+            log.appendFormat("    %-4s texture %d     %8d / %8d\n", tag, i, total - free, total);
+        }
+    }
+}
+
+void FontRenderer::dumpMemoryUsage(String8& log) const {
+    const uint32_t sizeA8 = getCacheSize(GL_ALPHA);
+    const uint32_t usedA8 = sizeA8 - getFreeCacheSize(GL_ALPHA);
+    const uint32_t sizeRGBA = getCacheSize(GL_RGBA);
+    const uint32_t usedRGBA = sizeRGBA - getFreeCacheSize(GL_RGBA);
+    log.appendFormat("  FontRenderer A8      %8d / %8d\n", usedA8, sizeA8);
+    dumpTextures(log, "A8", cacheTexturesForFormat(GL_ALPHA));
+    log.appendFormat("  FontRenderer RGBA    %8d / %8d\n", usedRGBA, sizeRGBA);
+    dumpTextures(log, "RGBA", cacheTexturesForFormat(GL_RGBA));
+    log.appendFormat("  FontRenderer total   %8d / %8d\n", usedA8 + usedRGBA, sizeA8 + sizeRGBA);
+}
+
+uint32_t FontRenderer::getCacheSize(GLenum format) const {
+    return calculateCacheSize(cacheTexturesForFormat(format));
+}
+
+uint32_t FontRenderer::getFreeCacheSize(GLenum format) const {
+    return calculateFreeCacheSize(cacheTexturesForFormat(format));
+}
+
+uint32_t FontRenderer::getSize() const {
+    return getCacheSize(GL_ALPHA) + getCacheSize(GL_RGBA);
 }
 
 }; // namespace uirenderer
diff --git a/libs/hwui/FontRenderer.h b/libs/hwui/FontRenderer.h
index d656864..1e59a96 100644
--- a/libs/hwui/FontRenderer.h
+++ b/libs/hwui/FontRenderer.h
@@ -21,8 +21,12 @@
 #include "font/CacheTexture.h"
 #include "font/CachedGlyphInfo.h"
 #include "font/Font.h"
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+#include "font/FontCacheHistoryTracker.h"
+#endif
 
 #include <utils/LruCache.h>
+#include <utils/String8.h>
 #include <utils/StrongPointer.h>
 
 #include <SkPaint.h>
@@ -132,7 +136,12 @@
         mLinearFiltering = linearFiltering;
     }
 
-    uint32_t getCacheSize(GLenum format) const;
+    uint32_t getSize() const;
+    void dumpMemoryUsage(String8& log) const;
+
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+    FontCacheHistoryTracker& historyTracker() { return mHistoryTracker; }
+#endif
 
 private:
     friend class Font;
@@ -175,6 +184,10 @@
         mUploadTexture = true;
     }
 
+    const std::vector<CacheTexture*>& cacheTexturesForFormat(GLenum format) const;
+    uint32_t getCacheSize(GLenum format) const;
+    uint32_t getFreeCacheSize(GLenum format) const;
+
     uint32_t mSmallCacheWidth;
     uint32_t mSmallCacheHeight;
     uint32_t mLargeCacheWidth;
@@ -199,6 +212,10 @@
 
     bool mLinearFiltering;
 
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+    FontCacheHistoryTracker mHistoryTracker;
+#endif
+
 #ifdef ANDROID_ENABLE_RENDERSCRIPT
     // RS constructs
     RSC::sp<RSC::RS> mRs;
diff --git a/libs/hwui/FrameInfo.cpp b/libs/hwui/FrameInfo.cpp
index 41e2233..826f0bb 100644
--- a/libs/hwui/FrameInfo.cpp
+++ b/libs/hwui/FrameInfo.cpp
@@ -35,8 +35,17 @@
     "IssueDrawCommandsStart",
     "SwapBuffers",
     "FrameCompleted",
+    "DequeueBufferDuration",
+    "QueueBufferDuration",
 };
 
+static_assert((sizeof(FrameInfoNames)/sizeof(FrameInfoNames[0]))
+        == static_cast<int>(FrameInfoIndex::NumIndexes),
+        "size mismatch: FrameInfoNames doesn't match the enum!");
+
+static_assert(static_cast<int>(FrameInfoIndex::NumIndexes) == 16,
+        "Must update value in FrameMetrics.java#FRAME_STATS_COUNT (and here)");
+
 void FrameInfo::importUiThreadInfo(int64_t* info) {
     memcpy(mFrameInfo, info, UI_THREAD_FRAME_INFO_SIZE * sizeof(int64_t));
 }
diff --git a/libs/hwui/FrameInfo.h b/libs/hwui/FrameInfo.h
index afab84c..bac9d12d 100644
--- a/libs/hwui/FrameInfo.h
+++ b/libs/hwui/FrameInfo.h
@@ -48,7 +48,11 @@
     SwapBuffers,
     FrameCompleted,
 
+    DequeueBufferDuration,
+    QueueBufferDuration,
+
     // Must be the last value!
+    // Also must be kept in sync with FrameMetrics.java#FRAME_STATS_COUNT
     NumIndexes
 };
 
diff --git a/libs/hwui/GammaFontRenderer.h b/libs/hwui/GammaFontRenderer.h
index 5813e7f..bd27a1a 100644
--- a/libs/hwui/GammaFontRenderer.h
+++ b/libs/hwui/GammaFontRenderer.h
@@ -22,6 +22,8 @@
 
 #include <SkPaint.h>
 
+#include <utils/String8.h>
+
 namespace android {
 namespace uirenderer {
 
@@ -46,8 +48,16 @@
         return *mRenderer;
     }
 
-    uint32_t getFontRendererSize(GLenum format) const {
-        return mRenderer ? mRenderer->getCacheSize(format) : 0;
+    void dumpMemoryUsage(String8& log) const {
+        if (mRenderer) {
+            mRenderer->dumpMemoryUsage(log);
+        } else {
+            log.appendFormat("FontRenderer doesn't exist.\n");
+        }
+    }
+
+    uint32_t getSize() const {
+        return mRenderer ? mRenderer->getSize() : 0;
     }
 
     void endPrecaching();
diff --git a/libs/hwui/JankTracker.cpp b/libs/hwui/JankTracker.cpp
index ebe9c42..ed6b211 100644
--- a/libs/hwui/JankTracker.cpp
+++ b/libs/hwui/JankTracker.cpp
@@ -16,6 +16,7 @@
 #include "JankTracker.h"
 
 #include "Properties.h"
+#include "utils/TimeUtils.h"
 
 #include <algorithm>
 #include <cutils/ashmem.h>
@@ -119,11 +120,27 @@
     return index;
 }
 
-JankTracker::JankTracker(nsecs_t frameIntervalNanos) {
+JankTracker::JankTracker(const DisplayInfo& displayInfo) {
     // By default this will use malloc memory. It may be moved later to ashmem
     // if there is shared space for it and a request comes in to do that.
     mData = new ProfileData;
     reset();
+    nsecs_t frameIntervalNanos = static_cast<nsecs_t>(1_s / displayInfo.fps);
+#if USE_HWC2
+    nsecs_t sfOffset = frameIntervalNanos - (displayInfo.presentationDeadline - 1_ms);
+    nsecs_t offsetDelta = sfOffset - displayInfo.appVsyncOffset;
+    // There are two different offset cases. If the offsetDelta is positive
+    // and small, then the intention is to give apps extra time by leveraging
+    // pipelining between the UI & RT threads. If the offsetDelta is large or
+    // negative, the intention is to subtract time from the total duration
+    // in which case we can't afford to wait for dequeueBuffer blockage.
+    if (offsetDelta <= 4_ms && offsetDelta >= 0) {
+        // SF will begin composition at VSYNC-app + offsetDelta. If we are triple
+        // buffered, this is the expected time at which dequeueBuffer will
+        // return due to the staggering of VSYNC-app & VSYNC-sf.
+        mDequeueTimeForgiveness = offsetDelta + 4_ms;
+    }
+#endif
     setFrameInterval(frameIntervalNanos);
 }
 
@@ -213,6 +230,19 @@
     mData->totalFrameCount++;
     // Fast-path for jank-free frames
     int64_t totalDuration = frame.duration(sFrameStart, FrameInfoIndex::FrameCompleted);
+    if (mDequeueTimeForgiveness
+            && frame[FrameInfoIndex::DequeueBufferDuration] > 500_us) {
+        nsecs_t expectedDequeueDuration =
+                mDequeueTimeForgiveness + frame[FrameInfoIndex::Vsync]
+                - frame[FrameInfoIndex::IssueDrawCommandsStart];
+        if (expectedDequeueDuration > 0) {
+            // Forgive only up to the expected amount, but not more than
+            // the actual time spent blocked.
+            nsecs_t forgiveAmount = std::min(expectedDequeueDuration,
+                    frame[FrameInfoIndex::DequeueBufferDuration]);
+            totalDuration -= forgiveAmount;
+        }
+    }
     uint32_t framebucket = frameCountIndexForFrameTime(totalDuration);
     // Keep the fast path as fast as possible.
     if (CC_LIKELY(totalDuration < mFrameInterval)) {
diff --git a/libs/hwui/JankTracker.h b/libs/hwui/JankTracker.h
index a23dd78..8b482d5 100644
--- a/libs/hwui/JankTracker.h
+++ b/libs/hwui/JankTracker.h
@@ -21,6 +21,7 @@
 #include "utils/RingBuffer.h"
 
 #include <cutils/compiler.h>
+#include <ui/DisplayInfo.h>
 
 #include <array>
 #include <memory>
@@ -56,7 +57,7 @@
 // TODO: Replace DrawProfiler with this
 class JankTracker {
 public:
-    explicit JankTracker(nsecs_t frameIntervalNanos);
+    explicit JankTracker(const DisplayInfo& displayInfo);
     ~JankTracker();
 
     void addFrame(const FrameInfo& frame);
@@ -79,6 +80,14 @@
 
     std::array<int64_t, NUM_BUCKETS> mThresholds;
     int64_t mFrameInterval;
+    // The amount of time we will erase from the total duration to account
+    // for SF vsync offsets with HWC2 blocking dequeueBuffers.
+    // (Vsync + mDequeueBlockTolerance) is the point at which we expect
+    // SF to have released the buffer normally, so we will forgive up to that
+    // point in time by comparing to (IssueDrawCommandsStart + DequeueDuration)
+    // This is only used if we are in pipelined mode and are using HWC2,
+    // otherwise it's 0.
+    nsecs_t mDequeueTimeForgiveness = 0;
     ProfileData* mData;
     bool mIsMapped = false;
 };
diff --git a/libs/hwui/PropertyValuesAnimatorSet.cpp b/libs/hwui/PropertyValuesAnimatorSet.cpp
index b29f91f..38fb70a 100644
--- a/libs/hwui/PropertyValuesAnimatorSet.cpp
+++ b/libs/hwui/PropertyValuesAnimatorSet.cpp
@@ -23,13 +23,17 @@
 namespace uirenderer {
 
 void PropertyValuesAnimatorSet::addPropertyAnimator(PropertyValuesHolder* propertyValuesHolder,
-            Interpolator* interpolator, nsecs_t startDelay,
-            nsecs_t duration, int repeatCount) {
+        Interpolator* interpolator, nsecs_t startDelay, nsecs_t duration, int repeatCount,
+        RepeatMode repeatMode) {
 
     PropertyAnimator* animator = new PropertyAnimator(propertyValuesHolder,
-            interpolator, startDelay, duration, repeatCount);
+            interpolator, startDelay, duration, repeatCount, repeatMode);
     mAnimators.emplace_back(animator);
-    setListener(new PropertyAnimatorSetListener(this));
+
+    // Check whether any child animator is infinite after adding it them to the set.
+    if (repeatCount == -1) {
+        mIsInfinite = true;
+    }
 }
 
 PropertyValuesAnimatorSet::PropertyValuesAnimatorSet()
@@ -37,6 +41,7 @@
     setStartValue(0);
     mLastFraction = 0.0f;
     setInterpolator(new LinearInterpolator());
+    setListener(new PropertyAnimatorSetListener(this));
 }
 
 void PropertyValuesAnimatorSet::onFinished(BaseRenderNodeAnimator* animator) {
@@ -61,14 +66,9 @@
             // Note that this set may containing animators modifying the same property, so when we
             // reset the animators, we need to make sure the animators that end the first will
             // have the final say on what the property value should be.
-            (*it)->setFraction(0);
+            (*it)->setFraction(0, 0);
         }
-    } else if (playTime >= mDuration) {
-        // Skip all the animators to end
-        for (auto& anim : mAnimators) {
-            anim->setFraction(1);
-        }
-    } else {
+    } else  {
         for (auto& anim : mAnimators) {
             anim->setCurrentPlayTime(playTime);
         }
@@ -78,15 +78,27 @@
 void PropertyValuesAnimatorSet::start(AnimationListener* listener) {
     init();
     mOneShotListener = listener;
+    mRequestId++;
     BaseRenderNodeAnimator::start();
 }
 
 void PropertyValuesAnimatorSet::reverse(AnimationListener* listener) {
     init();
     mOneShotListener = listener;
+    mRequestId++;
     BaseRenderNodeAnimator::reverse();
 }
 
+void PropertyValuesAnimatorSet::reset() {
+    mRequestId++;
+    BaseRenderNodeAnimator::reset();
+}
+
+void PropertyValuesAnimatorSet::end() {
+    mRequestId++;
+    BaseRenderNodeAnimator::end();
+}
+
 void PropertyValuesAnimatorSet::init() {
     if (mInitialized) {
         return;
@@ -98,7 +110,7 @@
     std::sort(mAnimators.begin(), mAnimators.end(), [](auto& a, auto&b) {
         return a->getTotalDuration() < b->getTotalDuration();
     });
-    mDuration = mAnimators[mAnimators.size() - 1]->getTotalDuration();
+    mDuration = mAnimators.empty() ? 0 : mAnimators[mAnimators.size() - 1]->getTotalDuration();
     mInitialized = true;
 }
 
@@ -107,7 +119,8 @@
 }
 
 PropertyAnimator::PropertyAnimator(PropertyValuesHolder* holder, Interpolator* interpolator,
-        nsecs_t startDelay, nsecs_t duration, int repeatCount)
+        nsecs_t startDelay, nsecs_t duration, int repeatCount,
+        RepeatMode repeatMode)
         : mPropertyValuesHolder(holder), mInterpolator(interpolator), mStartDelay(startDelay),
           mDuration(duration) {
     if (repeatCount < 0) {
@@ -115,24 +128,44 @@
     } else {
         mRepeatCount = repeatCount;
     }
+    mRepeatMode = repeatMode;
     mTotalDuration = ((nsecs_t) mRepeatCount + 1) * mDuration + mStartDelay;
 }
 
 void PropertyAnimator::setCurrentPlayTime(nsecs_t playTime) {
-    if (playTime >= mStartDelay && playTime < mTotalDuration) {
-         nsecs_t currentIterationPlayTime = (playTime - mStartDelay) % mDuration;
-         float fraction = currentIterationPlayTime / (float) mDuration;
-         setFraction(fraction);
-    } else if (mLatestFraction < 1.0f && playTime >= mTotalDuration) {
-        // This makes sure we only set the fraction = 1 once. It is needed because there might
-        // be another animator modifying the same property after this animator finishes, we need
-        // to make sure we don't set conflicting values on the same property within one frame.
-        setFraction(1.0f);
+    if (playTime < mStartDelay) {
+        return;
     }
+
+    float currentIterationFraction;
+    long iteration;
+    if (playTime >= mTotalDuration) {
+        // Reached the end of the animation.
+        iteration = mRepeatCount;
+        currentIterationFraction = 1.0f;
+    } else {
+        // play time here is in range [mStartDelay, mTotalDuration)
+        iteration = (playTime - mStartDelay) / mDuration;
+        currentIterationFraction = ((playTime - mStartDelay) % mDuration) / (float) mDuration;
+    }
+    setFraction(currentIterationFraction, iteration);
 }
 
-void PropertyAnimator::setFraction(float fraction) {
-    mLatestFraction = fraction;
+void PropertyAnimator::setFraction(float fraction, long iteration) {
+    double totalFraction = fraction + iteration;
+    // This makes sure we only set the fraction = repeatCount + 1 once. It is needed because there
+    // might be another animator modifying the same property after this animator finishes, we need
+    // to make sure we don't set conflicting values on the same property within one frame.
+    if ((mLatestFraction == mRepeatCount + 1.0) && (totalFraction >= mRepeatCount + 1.0)) {
+        return;
+    }
+
+    mLatestFraction = totalFraction;
+    // Check the play direction (i.e. reverse or restart) every other iteration, and calculate the
+    // fraction based on the play direction.
+    if (iteration % 2 && mRepeatMode == RepeatMode::Reverse) {
+        fraction = 1.0f - fraction;
+    }
     float interpolatedFraction = mInterpolator->interpolate(fraction);
     mPropertyValuesHolder->setFraction(interpolatedFraction);
 }
diff --git a/libs/hwui/PropertyValuesAnimatorSet.h b/libs/hwui/PropertyValuesAnimatorSet.h
index 602fd91..a5d9e86 100644
--- a/libs/hwui/PropertyValuesAnimatorSet.h
+++ b/libs/hwui/PropertyValuesAnimatorSet.h
@@ -26,12 +26,13 @@
 class PropertyAnimator {
 public:
     PropertyAnimator(PropertyValuesHolder* holder, Interpolator* interpolator, nsecs_t startDelay,
-            nsecs_t duration, int repeatCount);
+            nsecs_t duration, int repeatCount, RepeatMode repeatMode);
     void setCurrentPlayTime(nsecs_t playTime);
     nsecs_t getTotalDuration() {
         return mTotalDuration;
     }
-    void setFraction(float fraction);
+    // fraction range: [0, 1], iteration range [0, repeatCount]
+    void setFraction(float fraction, long iteration);
 
 private:
     std::unique_ptr<PropertyValuesHolder> mPropertyValuesHolder;
@@ -40,9 +41,11 @@
     nsecs_t mDuration;
     uint32_t mRepeatCount;
     nsecs_t mTotalDuration;
-    float mLatestFraction = 0.0f;
+    RepeatMode mRepeatMode;
+    double mLatestFraction = 0;
 };
 
+// TODO: This class should really be named VectorDrawableAnimator
 class ANDROID_API PropertyValuesAnimatorSet : public BaseRenderNodeAnimator {
 public:
     friend class PropertyAnimatorSetListener;
@@ -50,11 +53,19 @@
 
     void start(AnimationListener* listener);
     void reverse(AnimationListener* listener);
+    virtual void reset() override;
+    virtual void end() override;
 
     void addPropertyAnimator(PropertyValuesHolder* propertyValuesHolder,
             Interpolator* interpolators, int64_t startDelays,
-            nsecs_t durations, int repeatCount);
+            nsecs_t durations, int repeatCount, RepeatMode repeatMode);
     virtual uint32_t dirtyMask();
+    bool isInfinite() { return mIsInfinite; }
+    void setVectorDrawable(VectorDrawableRoot* vd) { mVectorDrawable = vd; }
+    VectorDrawableRoot* getVectorDrawable() const { return mVectorDrawable.get(); }
+    AnimationListener* getOneShotListener() { return mOneShotListener.get(); }
+    void clearOneShotListener() { mOneShotListener = nullptr; }
+    uint32_t getRequestId() const { return mRequestId; }
 
 protected:
     virtual float getValue(RenderNode* target) const override;
@@ -69,6 +80,11 @@
     std::vector< std::unique_ptr<PropertyAnimator> > mAnimators;
     float mLastFraction = 0.0f;
     bool mInitialized = false;
+    sp<VectorDrawableRoot> mVectorDrawable;
+    bool mIsInfinite = false;
+    // This request id gets incremented (on UI thread only) when a new request to modfiy the
+    // lifecycle of an animation happens, namely when start/end/reset/reverse is called.
+    uint32_t mRequestId = 0;
 };
 
 class PropertyAnimatorSetListener : public AnimationListener {
diff --git a/libs/hwui/PropertyValuesHolder.cpp b/libs/hwui/PropertyValuesHolder.cpp
index 0932d65..6ba0ab5 100644
--- a/libs/hwui/PropertyValuesHolder.cpp
+++ b/libs/hwui/PropertyValuesHolder.cpp
@@ -25,7 +25,27 @@
 
 using namespace VectorDrawable;
 
-float PropertyValuesHolder::getValueFromData(float fraction) {
+inline U8CPU lerp(U8CPU fromValue, U8CPU toValue, float fraction) {
+    return (U8CPU) (fromValue * (1 - fraction) + toValue * fraction);
+}
+
+// TODO: Add a test for this
+void ColorEvaluator::evaluate(SkColor* outColor,
+        const SkColor& fromColor, const SkColor& toColor, float fraction) const {
+    U8CPU alpha = lerp(SkColorGetA(fromColor), SkColorGetA(toColor), fraction);
+    U8CPU red = lerp(SkColorGetR(fromColor), SkColorGetR(toColor), fraction);
+    U8CPU green = lerp(SkColorGetG(fromColor), SkColorGetG(toColor), fraction);
+    U8CPU blue = lerp(SkColorGetB(fromColor), SkColorGetB(toColor), fraction);
+    *outColor = SkColorSetARGB(alpha, red, green, blue);
+}
+
+void PathEvaluator::evaluate(PathData* out,
+        const PathData& from, const PathData& to, float fraction) const {
+    VectorDrawableUtils::interpolatePaths(out, from, to, fraction);
+}
+
+template<typename T>
+const T PropertyValuesHolderImpl<T>::getValueFromData(float fraction) const {
     if (mDataSource.size() == 0) {
         LOG_ALWAYS_FATAL("No data source is defined");
         return 0;
@@ -41,57 +61,44 @@
     int lowIndex = floor(fraction);
     fraction -= lowIndex;
 
-    float value = mDataSource[lowIndex] * (1.0f - fraction)
-            + mDataSource[lowIndex + 1] * fraction;
+    T value;
+    mEvaluator->evaluate(&value, mDataSource[lowIndex], mDataSource[lowIndex + 1], fraction);
     return value;
 }
 
-void GroupPropertyValuesHolder::setFraction(float fraction) {
-    float animatedValue;
+template<typename T>
+const T PropertyValuesHolderImpl<T>::calculateAnimatedValue(float fraction) const {
     if (mDataSource.size() > 0) {
-        animatedValue = getValueFromData(fraction);
+        return getValueFromData(fraction);
     } else {
-        animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction;
+        T value;
+        mEvaluator->evaluate(&value, mStartValue, mEndValue, fraction);
+        return value;
     }
+}
+
+void GroupPropertyValuesHolder::setFraction(float fraction) {
+    float animatedValue = calculateAnimatedValue(fraction);
     mGroup->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
 }
 
-inline U8CPU lerp(U8CPU fromValue, U8CPU toValue, float fraction) {
-    return (U8CPU) (fromValue * (1 - fraction) + toValue * fraction);
-}
-
-// TODO: Add a test for this
-SkColor FullPathColorPropertyValuesHolder::interpolateColors(SkColor fromColor, SkColor toColor,
-        float fraction) {
-    U8CPU alpha = lerp(SkColorGetA(fromColor), SkColorGetA(toColor), fraction);
-    U8CPU red = lerp(SkColorGetR(fromColor), SkColorGetR(toColor), fraction);
-    U8CPU green = lerp(SkColorGetG(fromColor), SkColorGetG(toColor), fraction);
-    U8CPU blue = lerp(SkColorGetB(fromColor), SkColorGetB(toColor), fraction);
-    return SkColorSetARGB(alpha, red, green, blue);
-}
-
 void FullPathColorPropertyValuesHolder::setFraction(float fraction) {
-    SkColor animatedValue = interpolateColors(mStartValue, mEndValue, fraction);
+    SkColor animatedValue = calculateAnimatedValue(fraction);
     mFullPath->mutateProperties()->setColorPropertyValue(mPropertyId, animatedValue);
 }
 
 void FullPathPropertyValuesHolder::setFraction(float fraction) {
-    float animatedValue;
-    if (mDataSource.size() > 0) {
-        animatedValue = getValueFromData(fraction);
-    } else {
-        animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction;
-    }
+    float animatedValue = calculateAnimatedValue(fraction);
     mFullPath->mutateProperties()->setPropertyValue(mPropertyId, animatedValue);
 }
 
 void PathDataPropertyValuesHolder::setFraction(float fraction) {
-    VectorDrawableUtils::interpolatePaths(&mPathData, mStartValue, mEndValue, fraction);
+    mEvaluator->evaluate(&mPathData, mStartValue, mEndValue, fraction);
     mPath->mutateProperties()->setData(mPathData);
 }
 
 void RootAlphaPropertyValuesHolder::setFraction(float fraction) {
-    float animatedValue = mStartValue * (1 - fraction) + mEndValue * fraction;
+    float animatedValue = calculateAnimatedValue(fraction);
     mTree->mutateProperties()->setRootAlpha(animatedValue);
 }
 
diff --git a/libs/hwui/PropertyValuesHolder.h b/libs/hwui/PropertyValuesHolder.h
index b905fae..432f8ba 100644
--- a/libs/hwui/PropertyValuesHolder.h
+++ b/libs/hwui/PropertyValuesHolder.h
@@ -31,91 +31,130 @@
 class ANDROID_API PropertyValuesHolder {
 public:
     virtual void setFraction(float fraction) = 0;
-    void setPropertyDataSource(float* dataSource, int length) {
-        mDataSource.insert(mDataSource.begin(), dataSource, dataSource + length);
-    }
-   float getValueFromData(float fraction);
-   virtual ~PropertyValuesHolder() {}
-protected:
-   std::vector<float> mDataSource;
+    virtual ~PropertyValuesHolder() {}
 };
 
-class ANDROID_API GroupPropertyValuesHolder : public PropertyValuesHolder {
+template <typename T>
+class Evaluator {
+public:
+    virtual void evaluate(T* out, const T& from, const T& to, float fraction) const {};
+    virtual ~Evaluator() {}
+};
+
+class FloatEvaluator : public Evaluator<float> {
+public:
+    virtual void evaluate(float* out, const float& from, const float& to, float fraction)
+            const override {
+        *out = from * (1 - fraction) + to * fraction;
+    }
+};
+
+class ANDROID_API ColorEvaluator : public Evaluator<SkColor> {
+public:
+    virtual void evaluate(SkColor* outColor, const SkColor& from, const SkColor& to,
+            float fraction) const override;
+};
+
+class ANDROID_API PathEvaluator : public Evaluator<PathData> {
+    virtual void evaluate(PathData* out, const PathData& from, const PathData& to, float fraction)
+            const override;
+};
+
+template <typename T>
+class ANDROID_API PropertyValuesHolderImpl : public PropertyValuesHolder {
+public:
+    PropertyValuesHolderImpl(const T& startValue, const T& endValue)
+            : mStartValue(startValue)
+            , mEndValue(endValue) {}
+    void setPropertyDataSource(T* dataSource, int length) {
+        mDataSource.insert(mDataSource.begin(), dataSource, dataSource + length);
+    }
+    // Calculate the animated value from the data source.
+    const T getValueFromData(float fraction) const;
+    // Convenient method to favor getting animated value from data source. If no data source is set
+    // fall back to linear interpolation.
+    const T calculateAnimatedValue(float fraction) const;
+protected:
+   std::unique_ptr<Evaluator<T>> mEvaluator = nullptr;
+   // This contains uniformly sampled data throughout the animation duration. The first element
+   // should be the start value and the last should be the end value of the animation. When the
+   // data source is set, we'll favor data source over the linear interpolation of start/end value
+   // for calculation of animated value.
+   std::vector<T> mDataSource;
+   T mStartValue;
+   T mEndValue;
+};
+
+class ANDROID_API GroupPropertyValuesHolder : public PropertyValuesHolderImpl<float> {
 public:
     GroupPropertyValuesHolder(VectorDrawable::Group* ptr, int propertyId, float startValue,
             float endValue)
-            : mGroup(ptr)
-            , mPropertyId(propertyId)
-            , mStartValue(startValue)
-            , mEndValue(endValue){
+            : PropertyValuesHolderImpl(startValue, endValue)
+            , mGroup(ptr)
+            , mPropertyId(propertyId) {
+        mEvaluator.reset(new FloatEvaluator());
     }
     void setFraction(float fraction) override;
 private:
     VectorDrawable::Group* mGroup;
     int mPropertyId;
-    float mStartValue;
-    float mEndValue;
 };
 
-class ANDROID_API FullPathColorPropertyValuesHolder : public PropertyValuesHolder {
+class ANDROID_API FullPathColorPropertyValuesHolder : public PropertyValuesHolderImpl<SkColor> {
 public:
-    FullPathColorPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId, int32_t startValue,
-            int32_t endValue)
-            : mFullPath(ptr)
-            , mPropertyId(propertyId)
-            , mStartValue(startValue)
-            , mEndValue(endValue) {};
+    FullPathColorPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId,
+            SkColor startValue, SkColor endValue)
+            : PropertyValuesHolderImpl(startValue, endValue)
+            , mFullPath(ptr)
+            , mPropertyId(propertyId) {
+        mEvaluator.reset(new ColorEvaluator());
+    }
     void setFraction(float fraction) override;
     static SkColor interpolateColors(SkColor fromColor, SkColor toColor, float fraction);
 private:
     VectorDrawable::FullPath* mFullPath;
     int mPropertyId;
-    int32_t mStartValue;
-    int32_t mEndValue;
 };
 
-class ANDROID_API FullPathPropertyValuesHolder : public PropertyValuesHolder {
+class ANDROID_API FullPathPropertyValuesHolder : public PropertyValuesHolderImpl<float> {
 public:
     FullPathPropertyValuesHolder(VectorDrawable::FullPath* ptr, int propertyId, float startValue,
             float endValue)
-            : mFullPath(ptr)
-            , mPropertyId(propertyId)
-            , mStartValue(startValue)
-            , mEndValue(endValue) {};
+            : PropertyValuesHolderImpl(startValue, endValue)
+            , mFullPath(ptr)
+            , mPropertyId(propertyId) {
+        mEvaluator.reset(new FloatEvaluator());
+    };
     void setFraction(float fraction) override;
 private:
     VectorDrawable::FullPath* mFullPath;
     int mPropertyId;
-    float mStartValue;
-    float mEndValue;
 };
 
-class ANDROID_API PathDataPropertyValuesHolder : public PropertyValuesHolder {
+class ANDROID_API PathDataPropertyValuesHolder : public PropertyValuesHolderImpl<PathData> {
 public:
     PathDataPropertyValuesHolder(VectorDrawable::Path* ptr, PathData* startValue,
             PathData* endValue)
-            : mPath(ptr)
-            , mStartValue(*startValue)
-            , mEndValue(*endValue) {};
+            : PropertyValuesHolderImpl(*startValue, *endValue)
+            , mPath(ptr) {
+        mEvaluator.reset(new PathEvaluator());
+    };
     void setFraction(float fraction) override;
 private:
     VectorDrawable::Path* mPath;
     PathData mPathData;
-    PathData mStartValue;
-    PathData mEndValue;
 };
 
-class ANDROID_API RootAlphaPropertyValuesHolder : public PropertyValuesHolder {
+class ANDROID_API RootAlphaPropertyValuesHolder : public PropertyValuesHolderImpl<float> {
 public:
     RootAlphaPropertyValuesHolder(VectorDrawable::Tree* tree, float startValue, float endValue)
-            : mTree(tree)
-            , mStartValue(startValue)
-            , mEndValue(endValue) {}
+            : PropertyValuesHolderImpl(startValue, endValue)
+            , mTree(tree) {
+        mEvaluator.reset(new FloatEvaluator());
+    }
     void setFraction(float fraction) override;
 private:
     VectorDrawable::Tree* mTree;
-    float mStartValue;
-    float mEndValue;
 };
 }
 }
diff --git a/libs/hwui/Readback.cpp b/libs/hwui/Readback.cpp
index 55f823d..0ab247d 100644
--- a/libs/hwui/Readback.cpp
+++ b/libs/hwui/Readback.cpp
@@ -136,7 +136,7 @@
             EGL_NATIVE_BUFFER_ANDROID, clientBuffer, attrs);
 
     if (sourceImage == EGL_NO_IMAGE_KHR) {
-        ALOGW("Error creating image (%#x)", eglGetError());
+        ALOGW("eglCreateImageKHR failed (%#x)", eglGetError());
         return CopyResult::UnknownError;
     }
     GLuint sourceTexId;
@@ -147,7 +147,8 @@
 
     GLenum status = GL_NO_ERROR;
     while ((status = glGetError()) != GL_NO_ERROR) {
-        ALOGW("Error creating image (%#x)", status);
+        ALOGW("glEGLImageTargetTexture2DOES failed (%#x)", status);
+        eglDestroyImageKHR(display, sourceImage);
         return CopyResult::UnknownError;
     }
 
@@ -183,6 +184,13 @@
     caches.textureState().deleteTexture(texture);
     renderState.deleteFramebuffer(fbo);
 
+    sourceTexture.deleteTexture();
+    // All we're flushing & finishing is the deletion of the texture since
+    // copyTextureInto already did a major flush & finish as an implicit
+    // part of glReadPixels, so this shouldn't pose any major stalls.
+    glFinish();
+    eglDestroyImageKHR(display, sourceImage);
+
     GL_CHECKPOINT(MODERATE);
 
     return CopyResult::Success;
diff --git a/libs/hwui/RecordingCanvas.cpp b/libs/hwui/RecordingCanvas.cpp
index b49f9b5..0c552ba 100644
--- a/libs/hwui/RecordingCanvas.cpp
+++ b/libs/hwui/RecordingCanvas.cpp
@@ -127,7 +127,8 @@
     // operations will be able to store and restore the current clip and transform info, and
     // quick rejection will be correct (for display lists)
 
-    const Rect unmappedBounds(left, top, right, bottom);
+    Rect unmappedBounds(left, top, right, bottom);
+    unmappedBounds.roundOut();
 
     // determine clipped bounds relative to previous viewport.
     Rect visibleBounds = unmappedBounds;
@@ -148,50 +149,53 @@
 
     // Map visible bounds back to layer space, and intersect with parameter bounds
     Rect layerBounds = visibleBounds;
-    Matrix4 inverse;
-    inverse.loadInverse(*previous.transform);
-    inverse.mapRect(layerBounds);
-    layerBounds.doIntersect(unmappedBounds);
+    if (CC_LIKELY(!layerBounds.isEmpty())) {
+        // if non-empty, can safely map by the inverse transform
+        Matrix4 inverse;
+        inverse.loadInverse(*previous.transform);
+        inverse.mapRect(layerBounds);
+        layerBounds.doIntersect(unmappedBounds);
+    }
 
     int saveValue = mState.save((int) flags);
     Snapshot& snapshot = *mState.writableSnapshot();
 
     // layerBounds is in original bounds space, but clipped by current recording clip
-    if (layerBounds.isEmpty() || unmappedBounds.isEmpty()) {
-        // Don't bother recording layer, since it's been rejected
+    if (!layerBounds.isEmpty() && !unmappedBounds.isEmpty()) {
         if (CC_LIKELY(clippedLayer)) {
-            snapshot.resetClip(0, 0, 0, 0);
+            auto previousClip = getRecordedClip(); // capture before new snapshot clip has changed
+            if (addOp(alloc().create_trivial<BeginLayerOp>(
+                    unmappedBounds,
+                    *previous.transform, // transform to *draw* with
+                    previousClip, // clip to *draw* with
+                    refPaint(paint))) >= 0) {
+                snapshot.flags |= Snapshot::kFlagIsLayer | Snapshot::kFlagIsFboLayer;
+                snapshot.initializeViewport(unmappedBounds.getWidth(), unmappedBounds.getHeight());
+                snapshot.transform->loadTranslate(-unmappedBounds.left, -unmappedBounds.top, 0.0f);
+
+                Rect clip = layerBounds;
+                clip.translate(-unmappedBounds.left, -unmappedBounds.top);
+                snapshot.resetClip(clip.left, clip.top, clip.right, clip.bottom);
+                snapshot.roundRectClipState = nullptr;
+                return saveValue;
+            }
+        } else {
+            if (addOp(alloc().create_trivial<BeginUnclippedLayerOp>(
+                    unmappedBounds,
+                    *mState.currentSnapshot()->transform,
+                    getRecordedClip(),
+                    refPaint(paint))) >= 0) {
+                snapshot.flags |= Snapshot::kFlagIsLayer;
+                return saveValue;
+            }
         }
-        return saveValue;
     }
 
+    // Layer not needed, so skip recording it...
     if (CC_LIKELY(clippedLayer)) {
-        auto previousClip = getRecordedClip(); // note: done before new snapshot's clip has changed
-
-        snapshot.flags |= Snapshot::kFlagIsLayer | Snapshot::kFlagIsFboLayer;
-        snapshot.initializeViewport(unmappedBounds.getWidth(), unmappedBounds.getHeight());
-        snapshot.transform->loadTranslate(-unmappedBounds.left, -unmappedBounds.top, 0.0f);
-
-        Rect clip = layerBounds;
-        clip.translate(-unmappedBounds.left, -unmappedBounds.top);
-        snapshot.resetClip(clip.left, clip.top, clip.right, clip.bottom);
-        snapshot.roundRectClipState = nullptr;
-
-        addOp(alloc().create_trivial<BeginLayerOp>(
-                unmappedBounds,
-                *previous.transform, // transform to *draw* with
-                previousClip, // clip to *draw* with
-                refPaint(paint)));
-    } else {
-        snapshot.flags |= Snapshot::kFlagIsLayer;
-
-        addOp(alloc().create_trivial<BeginUnclippedLayerOp>(
-                unmappedBounds,
-                *mState.currentSnapshot()->transform,
-                getRecordedClip(),
-                refPaint(paint)));
+        // ... and set empty clip to reject inner content, if possible
+        snapshot.resetClip(0, 0, 0, 0);
     }
-
     return saveValue;
 }
 
@@ -267,7 +271,7 @@
 
 // Geometry
 void RecordingCanvas::drawPoints(const float* points, int floatCount, const SkPaint& paint) {
-    if (floatCount < 2) return;
+    if (CC_UNLIKELY(floatCount < 2 || PaintUtils::paintWillNotDraw(paint))) return;
     floatCount &= ~0x1; // round down to nearest two
 
     addOp(alloc().create_trivial<PointsOp>(
@@ -278,7 +282,7 @@
 }
 
 void RecordingCanvas::drawLines(const float* points, int floatCount, const SkPaint& paint) {
-    if (floatCount < 4) return;
+    if (CC_UNLIKELY(floatCount < 4 || PaintUtils::paintWillNotDraw(paint))) return;
     floatCount &= ~0x3; // round down to nearest four
 
     addOp(alloc().create_trivial<LinesOp>(
@@ -289,6 +293,8 @@
 }
 
 void RecordingCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
+    if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
+
     addOp(alloc().create_trivial<RectOp>(
             Rect(left, top, right, bottom),
             *(mState.currentSnapshot()->transform),
@@ -330,6 +336,8 @@
 }
 
 void RecordingCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
+    if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
+
     if (paint.getStyle() == SkPaint::kFill_Style
             && (!paint.isAntiAlias() || mState.currentTransform()->isSimple())) {
         int count = 0;
@@ -354,8 +362,11 @@
         }
     }
 }
+
 void RecordingCanvas::drawRoundRect(float left, float top, float right, float bottom,
             float rx, float ry, const SkPaint& paint) {
+    if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
+
     if (CC_LIKELY(MathUtils::isPositive(rx) || MathUtils::isPositive(ry))) {
         addOp(alloc().create_trivial<RoundRectOp>(
                 Rect(left, top, right, bottom),
@@ -390,7 +401,8 @@
 
 void RecordingCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
     // TODO: move to Canvas.h
-    if (radius <= 0) return;
+    if (CC_UNLIKELY(radius <= 0 || PaintUtils::paintWillNotDraw(paint))) return;
+
     drawOval(x - radius, y - radius, x + radius, y + radius, paint);
 }
 
@@ -410,6 +422,8 @@
 }
 
 void RecordingCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
+    if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
+
     addOp(alloc().create_trivial<OvalOp>(
             Rect(left, top, right, bottom),
             *(mState.currentSnapshot()->transform),
@@ -419,6 +433,8 @@
 
 void RecordingCanvas::drawArc(float left, float top, float right, float bottom,
         float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
+    if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
+
     if (fabs(sweepAngle) >= 360.0f) {
         drawOval(left, top, right, bottom, paint);
     } else {
@@ -432,6 +448,8 @@
 }
 
 void RecordingCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
+    if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
+
     addOp(alloc().create_trivial<PathOp>(
             Rect(path.getBounds()),
             *(mState.currentSnapshot()->transform),
@@ -440,8 +458,8 @@
 }
 
 void RecordingCanvas::drawVectorDrawable(VectorDrawableRoot* tree) {
-    mDisplayList->pushStagingFunctors.push_back(tree->getFunctor());
     mDisplayList->ref(tree);
+    mDisplayList->vectorDrawables.push_back(tree);
     addOp(alloc().create_trivial<VectorDrawableOp>(
             tree,
             Rect(tree->stagingProperties()->getBounds()),
@@ -604,7 +622,7 @@
             functor));
 }
 
-size_t RecordingCanvas::addOp(RecordedOp* op) {
+int RecordingCanvas::addOp(RecordedOp* op) {
     // skip op with empty clip
     if (op->localClip && op->localClip->rect.isEmpty()) {
         // NOTE: this rejection happens after op construction/content ref-ing, so content ref'd
diff --git a/libs/hwui/RecordingCanvas.h b/libs/hwui/RecordingCanvas.h
index 372be24..337e97b 100644
--- a/libs/hwui/RecordingCanvas.h
+++ b/libs/hwui/RecordingCanvas.h
@@ -208,7 +208,7 @@
     void drawSimpleRects(const float* rects, int vertexCount, const SkPaint* paint);
 
 
-    size_t addOp(RecordedOp* op);
+    int addOp(RecordedOp* op);
 // ----------------------------------------------------------------------------
 // lazy object copy
 // ----------------------------------------------------------------------------
diff --git a/libs/hwui/Rect.h b/libs/hwui/Rect.h
index dbd188f..eb05e91 100644
--- a/libs/hwui/Rect.h
+++ b/libs/hwui/Rect.h
@@ -73,6 +73,13 @@
             bottom(height) {
     }
 
+    inline Rect(const SkIRect& rect):  // NOLINT, implicit
+            left(rect.fLeft),
+            top(rect.fTop),
+            right(rect.fRight),
+            bottom(rect.fBottom) {
+    }
+
     inline Rect(const SkRect& rect):  // NOLINT, implicit
             left(rect.fLeft),
             top(rect.fTop),
diff --git a/libs/hwui/RenderNode.cpp b/libs/hwui/RenderNode.cpp
index a393625..bdcad79 100644
--- a/libs/hwui/RenderNode.cpp
+++ b/libs/hwui/RenderNode.cpp
@@ -422,6 +422,16 @@
         pushStagingDisplayListChanges(info);
     }
     prepareSubTree(info, childFunctorsNeedLayer, mDisplayList);
+
+    if (mDisplayList) {
+        for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
+            // If any vector drawable in the display list needs update, damage the node.
+            if (vectorDrawable->isDirty()) {
+                damageSelf(info);
+            }
+            vectorDrawable->setPropertyChangeWillBeConsumed(true);
+        }
+    }
     pushLayerUpdate(info);
 
     info.damageAccumulator->popTransform();
@@ -482,8 +492,8 @@
         for (auto& iter : mDisplayList->getFunctors()) {
             (*iter.functor)(DrawGlInfo::kModeSync, nullptr);
         }
-        for (size_t i = 0; i < mDisplayList->getPushStagingFunctors().size(); i++) {
-            (*mDisplayList->getPushStagingFunctors()[i])();
+        for (auto& vectorDrawable : mDisplayList->getVectorDrawables()) {
+            vectorDrawable->syncProperties();
         }
     }
 }
diff --git a/libs/hwui/RenderNode.h b/libs/hwui/RenderNode.h
index 1eaf5d6..f9735a2 100644
--- a/libs/hwui/RenderNode.h
+++ b/libs/hwui/RenderNode.h
@@ -232,7 +232,7 @@
     // the frameNumber to appropriately batch/synchronize these transactions.
     // There is no other filtering/batching to ensure that only the "final"
     // state called once per frame.
-    class ANDROID_API PositionListener {
+    class ANDROID_API PositionListener : public VirtualLightRefBase {
     public:
         virtual ~PositionListener() {}
         // Called when the RenderNode's position changes
@@ -247,7 +247,7 @@
     // before the RenderNode is used for drawing.
     // RenderNode takes ownership of the pointer
     ANDROID_API void setPositionListener(PositionListener* listener) {
-        mPositionListener.reset(listener);
+        mPositionListener = listener;
     }
 
     // This is only modified in MODE_FULL, so it can be safely accessed
@@ -366,7 +366,7 @@
     // mDisplayList, not mStagingDisplayList.
     uint32_t mParentCount;
 
-    std::unique_ptr<PositionListener> mPositionListener;
+    sp<PositionListener> mPositionListener;
 }; // class RenderNode
 
 } /* namespace uirenderer */
diff --git a/libs/hwui/SkiaCanvas.cpp b/libs/hwui/SkiaCanvas.cpp
index 0693804..3b6fae0 100644
--- a/libs/hwui/SkiaCanvas.cpp
+++ b/libs/hwui/SkiaCanvas.cpp
@@ -329,9 +329,10 @@
 static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
     SkCanvas::SaveLayerFlags layerFlags = 0;
 
-    if (!(flags & SaveFlags::HasAlphaLayer)) {
-        layerFlags |= SkCanvas::kIsOpaque_SaveLayerFlag;
-    }
+    // We intentionally ignore the SaveFlags::HasAlphaLayer and
+    // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
+    // and our Android client may use it incorrectly.
+    // In Skia, this flag is purely for performance optimization.
 
     if (!(flags & SaveFlags::ClipToLayer)) {
         layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
diff --git a/libs/hwui/SpotShadow.cpp b/libs/hwui/SpotShadow.cpp
index 760d814..cc96a13 100644
--- a/libs/hwui/SpotShadow.cpp
+++ b/libs/hwui/SpotShadow.cpp
@@ -942,9 +942,13 @@
         AlphaVertex::set(&shadowVertices[vertexBufferIndex++], newPenumbra[i].x,
                 newPenumbra[i].y, PENUMBRA_ALPHA);
     }
+    // Since the umbra can be a faked one when the occluder is too high, the umbra should be lighter
+    // in this case.
+    float scaledUmbraAlpha = UMBRA_ALPHA * shadowStrengthScale;
+
     for (int i = 0; i < umbraLength; i++) {
         AlphaVertex::set(&shadowVertices[vertexBufferIndex++], umbra[i].x, umbra[i].y,
-                UMBRA_ALPHA);
+                scaledUmbraAlpha);
     }
 
     for (int i = 0; i < verticesPairIndex; i++) {
@@ -984,14 +988,14 @@
             indexBuffer[indexBufferIndex++] = newPenumbraLength + i;
             indexBuffer[indexBufferIndex++] = vertexBufferIndex;
             AlphaVertex::set(&shadowVertices[vertexBufferIndex++],
-                    closerVertex.x, closerVertex.y, UMBRA_ALPHA);
+                    closerVertex.x, closerVertex.y, scaledUmbraAlpha);
         }
     } else {
         // If there is no occluded umbra at all, then draw the triangle fan
         // starting from the centroid to all umbra vertices.
         int lastCentroidIndex = vertexBufferIndex;
         AlphaVertex::set(&shadowVertices[vertexBufferIndex++], centroid.x,
-                centroid.y, UMBRA_ALPHA);
+                centroid.y, scaledUmbraAlpha);
         for (int i = 0; i < umbraLength; i++) {
             indexBuffer[indexBufferIndex++] = newPenumbraLength + i;
             indexBuffer[indexBufferIndex++] = lastCentroidIndex;
diff --git a/libs/hwui/TextureCache.cpp b/libs/hwui/TextureCache.cpp
index ade8600..523924a 100644
--- a/libs/hwui/TextureCache.cpp
+++ b/libs/hwui/TextureCache.cpp
@@ -167,6 +167,10 @@
     return texture;
 }
 
+bool TextureCache::prefetch(const SkBitmap* bitmap) {
+    return getCachedTexture(bitmap, AtlasUsageType::Use);
+}
+
 Texture* TextureCache::get(const SkBitmap* bitmap, AtlasUsageType atlasUsageType) {
     Texture* texture = getCachedTexture(bitmap, atlasUsageType);
 
diff --git a/libs/hwui/TextureCache.h b/libs/hwui/TextureCache.h
index a4317ce..0a61b6b 100644
--- a/libs/hwui/TextureCache.h
+++ b/libs/hwui/TextureCache.h
@@ -78,6 +78,13 @@
     bool prefetchAndMarkInUse(void* ownerToken, const SkBitmap* bitmap);
 
     /**
+     * Attempts to precache the SkBitmap. Returns true if a Texture was successfully
+     * acquired for the bitmap, false otherwise. Does not mark the Texture
+     * as in use and won't update currently in-use Textures.
+     */
+    bool prefetch(const SkBitmap* bitmap);
+
+    /**
      * Returns the texture associated with the specified bitmap from either within the cache, or
      * the AssetAtlas. If the texture cannot be found in the cache, a new texture is generated.
      */
diff --git a/libs/hwui/VectorDrawable.h b/libs/hwui/VectorDrawable.h
index 3586d8a..6c1815f 100644
--- a/libs/hwui/VectorDrawable.h
+++ b/libs/hwui/VectorDrawable.h
@@ -673,21 +673,17 @@
     void onPropertyChanged(TreeProperties* prop);
     TreeProperties* mutateStagingProperties() { return &mStagingProperties; }
     const TreeProperties* stagingProperties() const { return &mStagingProperties; }
-    PushStagingFunctor* getFunctor() { return &mFunctor;}
 
     // This should only be called from animations on RT
     TreeProperties* mutateProperties() { return &mProperties; }
 
+    // This should always be called from RT.
+    void markDirty() { mCache.dirty = true; }
+    bool isDirty() const { return mCache.dirty; }
+    bool getPropertyChangeWillBeConsumed() const { return mWillBeConsumed; }
+    void setPropertyChangeWillBeConsumed(bool willBeConsumed) { mWillBeConsumed = willBeConsumed; }
+
 private:
-    class VectorDrawableFunctor : public PushStagingFunctor {
-    public:
-        VectorDrawableFunctor(Tree* tree) : mTree(tree) {}
-        virtual void operator ()() {
-            mTree->syncProperties();
-        }
-    private:
-        Tree* mTree;
-    };
 
     SkPaint* updatePaint(SkPaint* outPaint, TreeProperties* prop);
     bool allocateBitmapIfNeeded(SkBitmap* outCache, int width, int height);
@@ -704,8 +700,6 @@
     TreeProperties mProperties = TreeProperties(this);
     TreeProperties mStagingProperties = TreeProperties(this);
 
-    VectorDrawableFunctor mFunctor = VectorDrawableFunctor(this);
-
     SkPaint mPaint;
     struct Cache {
         SkBitmap bitmap;
@@ -717,6 +711,8 @@
 
     PropertyChangedListener mPropertyChangedListener
             = PropertyChangedListener(&mCache.dirty, &mStagingCache.dirty);
+
+    mutable bool mWillBeConsumed = false;
 };
 
 } // namespace VectorDrawable
diff --git a/libs/hwui/font/CacheTexture.cpp b/libs/hwui/font/CacheTexture.cpp
index fcdde45..49e9f65 100644
--- a/libs/hwui/font/CacheTexture.cpp
+++ b/libs/hwui/font/CacheTexture.cpp
@@ -330,5 +330,17 @@
     return false;
 }
 
+uint32_t CacheTexture::calculateFreeMemory() const {
+    CacheBlock* cacheBlock = mCacheBlocks;
+    uint32_t free = 0;
+    // currently only two formats are supported: GL_ALPHA or GL_RGBA;
+    uint32_t bpp = mFormat == GL_RGBA ? 4 : 1;
+    while (cacheBlock) {
+        free += bpp * cacheBlock->mWidth * cacheBlock->mHeight;
+        cacheBlock = cacheBlock->mNext;
+    }
+    return free;
+}
+
 }; // namespace uirenderer
 }; // namespace android
diff --git a/libs/hwui/font/CacheTexture.h b/libs/hwui/font/CacheTexture.h
index 4dfb41d..6750a8a 100644
--- a/libs/hwui/font/CacheTexture.h
+++ b/libs/hwui/font/CacheTexture.h
@@ -178,6 +178,8 @@
         return mCurrentQuad == mMaxQuadCount;
     }
 
+    uint32_t calculateFreeMemory() const;
+
 private:
     void setDirty(bool dirty);
 
diff --git a/libs/hwui/font/Font.cpp b/libs/hwui/font/Font.cpp
index 8e04c87..a95454a 100644
--- a/libs/hwui/font/Font.cpp
+++ b/libs/hwui/font/Font.cpp
@@ -408,9 +408,15 @@
         if (cachedGlyph->mIsValid && cachedGlyph->mCacheTexture) {
             int penX = x + (int) roundf(positions[(glyphsCount << 1)]);
             int penY = y + (int) roundf(positions[(glyphsCount << 1) + 1]);
-
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+            mState->historyTracker().glyphRendered(cachedGlyph, penX, penY);
+#endif
             (*this.*render)(cachedGlyph, penX, penY,
                     bitmap, bitmapW, bitmapH, bounds, positions);
+        } else {
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+            mState->historyTracker().glyphRendered(cachedGlyph, -1, -1);
+#endif
         }
 
         glyphsCount++;
diff --git a/libs/hwui/font/FontCacheHistoryTracker.cpp b/libs/hwui/font/FontCacheHistoryTracker.cpp
new file mode 100644
index 0000000..a2bfb27
--- /dev/null
+++ b/libs/hwui/font/FontCacheHistoryTracker.cpp
@@ -0,0 +1,100 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "FontCacheHistoryTracker.h"
+
+#include "CachedGlyphInfo.h"
+#include "CacheTexture.h"
+
+namespace android {
+namespace uirenderer {
+
+void FontCacheHistoryTracker::dumpCachedGlyph(String8& log, const CachedGlyph& glyph) {
+    log.appendFormat("glyph (texture %p, position: (%d, %d), size: %dx%d, gen: %d)", glyph.texture,
+            glyph.startX, glyph.startY, glyph.bitmapW, glyph.bitmapH, glyph.generation);
+}
+
+void FontCacheHistoryTracker::dumpRenderEntry(String8& log, const RenderEntry& entry) {
+    if (entry.penX == -1 && entry.penY == -1) {
+        log.appendFormat("      glyph skipped in gen: %d\n", entry.glyph.generation);
+    } else {
+        log.appendFormat("      rendered ");
+        dumpCachedGlyph(log, entry.glyph);
+        log.appendFormat(" at (%d, %d)\n", entry.penX, entry.penY);
+    }
+}
+
+void FontCacheHistoryTracker::dumpUploadEntry(String8& log, const CachedGlyph& glyph) {
+    if (glyph.bitmapW == 0 && glyph.bitmapH == 0) {
+        log.appendFormat("      cleared cachetexture %p in gen %d\n", glyph.texture,
+                glyph.generation);
+    } else {
+        log.appendFormat("      uploaded ");
+        dumpCachedGlyph(log, glyph);
+        log.appendFormat("\n");
+    }
+}
+
+void FontCacheHistoryTracker::dump(String8& log) const {
+    log.appendFormat("FontCacheHistory: \n");
+    log.appendFormat("  Upload history: \n");
+    for (size_t i = 0; i < mUploadHistory.size(); i++) {
+        dumpUploadEntry(log, mUploadHistory[i]);
+    }
+    log.appendFormat("  Render history: \n");
+    for (size_t i = 0; i < mRenderHistory.size(); i++) {
+        dumpRenderEntry(log, mRenderHistory[i]);
+    }
+}
+
+void FontCacheHistoryTracker::glyphRendered(CachedGlyphInfo* glyphInfo, int penX, int penY) {
+    RenderEntry& entry = mRenderHistory.next();
+    entry.glyph.generation = generation;
+    entry.glyph.texture = glyphInfo->mCacheTexture;
+    entry.glyph.startX = glyphInfo->mStartX;
+    entry.glyph.startY = glyphInfo->mStartY;
+    entry.glyph.bitmapW = glyphInfo->mBitmapWidth;
+    entry.glyph.bitmapH = glyphInfo->mBitmapHeight;
+    entry.penX = penX;
+    entry.penY = penY;
+}
+
+void FontCacheHistoryTracker::glyphUploaded(CacheTexture* texture, uint32_t x, uint32_t y,
+        uint16_t glyphW, uint16_t glyphH) {
+    CachedGlyph& glyph = mUploadHistory.next();
+    glyph.generation = generation;
+    glyph.texture = texture;
+    glyph.startX = x;
+    glyph.startY = y;
+    glyph.bitmapW = glyphW;
+    glyph.bitmapH = glyphH;
+}
+
+void FontCacheHistoryTracker::glyphsCleared(CacheTexture* texture) {
+    CachedGlyph& glyph = mUploadHistory.next();
+    glyph.generation = generation;
+    glyph.texture = texture;
+    glyph.startX = 0;
+    glyph.startY = 0;
+    glyph.bitmapW = 0;
+    glyph.bitmapH = 0;
+}
+
+void FontCacheHistoryTracker::frameCompleted() {
+    generation++;
+}
+}; // namespace uirenderer
+}; // namespace android
diff --git a/libs/hwui/font/FontCacheHistoryTracker.h b/libs/hwui/font/FontCacheHistoryTracker.h
new file mode 100644
index 0000000..f1d9b9f
--- /dev/null
+++ b/libs/hwui/font/FontCacheHistoryTracker.h
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include "../utils/RingBuffer.h"
+
+#include <utils/String8.h>
+
+namespace android {
+namespace uirenderer {
+
+class CacheTexture;
+struct CachedGlyphInfo;
+
+// Tracks glyph uploads and recent rendered/skipped glyphs, so it can give an idea
+// what a missing character is: skipped glyph, wrong coordinates in cache texture etc.
+class FontCacheHistoryTracker {
+public:
+    void glyphRendered(CachedGlyphInfo*, int penX, int penY);
+    void glyphUploaded(CacheTexture*, uint32_t x, uint32_t y, uint16_t glyphW, uint16_t glyphH);
+    void glyphsCleared(CacheTexture*);
+    void frameCompleted();
+
+    void dump(String8& log) const;
+private:
+    struct CachedGlyph {
+        void* texture;
+        uint16_t generation;
+        uint16_t startX;
+        uint16_t startY;
+        uint16_t bitmapW;
+        uint16_t bitmapH;
+    };
+
+    struct RenderEntry {
+        CachedGlyph glyph;
+        int penX;
+        int penY;
+    };
+
+    static void dumpCachedGlyph(String8& log, const CachedGlyph& glyph);
+    static void dumpRenderEntry(String8& log, const RenderEntry& entry);
+    static void dumpUploadEntry(String8& log, const CachedGlyph& glyph);
+
+    RingBuffer<RenderEntry, 300> mRenderHistory;
+    RingBuffer<CachedGlyph, 120> mUploadHistory;
+    uint16_t generation = 0;
+};
+
+}; // namespace uirenderer
+}; // namespace android
\ No newline at end of file
diff --git a/libs/hwui/renderthread/CanvasContext.cpp b/libs/hwui/renderthread/CanvasContext.cpp
index c626c54..975ac83 100644
--- a/libs/hwui/renderthread/CanvasContext.cpp
+++ b/libs/hwui/renderthread/CanvasContext.cpp
@@ -67,7 +67,7 @@
         , mEglManager(thread.eglManager())
         , mOpaque(!translucent)
         , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
-        , mJankTracker(thread.timeLord().frameIntervalNanos())
+        , mJankTracker(thread.mainDisplayInfo())
         , mProfiler(mFrames)
         , mContentDrawBounds(0, 0, 0, 0) {
     mRenderNodes.emplace_back(rootRenderNode);
@@ -198,6 +198,48 @@
     return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
 }
 
+bool CanvasContext::isSwapChainStuffed() {
+    static const auto SLOW_THRESHOLD = 6_ms;
+
+    if (mSwapHistory.size() != mSwapHistory.capacity()) {
+        // We want at least 3 frames of history before attempting to
+        // guess if the queue is stuffed
+        return false;
+    }
+    nsecs_t frameInterval = mRenderThread.timeLord().frameIntervalNanos();
+    auto& swapA = mSwapHistory[0];
+
+    // Was there a happy queue & dequeue time? If so, don't
+    // consider it stuffed
+    if (swapA.dequeueDuration < SLOW_THRESHOLD
+            && swapA.queueDuration < SLOW_THRESHOLD) {
+        return false;
+    }
+
+    for (size_t i = 1; i < mSwapHistory.size(); i++) {
+        auto& swapB = mSwapHistory[i];
+
+        // If there's a multi-frameInterval gap we effectively already dropped a frame,
+        // so consider the queue healthy.
+        if (swapA.swapCompletedTime - swapB.swapCompletedTime > frameInterval * 3) {
+            return false;
+        }
+
+        // Was there a happy queue & dequeue time? If so, don't
+        // consider it stuffed
+        if (swapB.dequeueDuration < SLOW_THRESHOLD
+                && swapB.queueDuration < SLOW_THRESHOLD) {
+            return false;
+        }
+
+        swapA = swapB;
+    }
+
+    // All signs point to a stuffed swap chain
+    ATRACE_NAME("swap chain stuffed");
+    return true;
+}
+
 void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
         int64_t syncQueued, RenderNode* target) {
     mRenderThread.removeFrameCallback(this);
@@ -243,7 +285,7 @@
 
     if (CC_LIKELY(mSwapHistory.size())) {
         nsecs_t latestVsync = mRenderThread.timeLord().latestVsync();
-        const SwapHistory& lastSwap = mSwapHistory.back();
+        SwapHistory& lastSwap = mSwapHistory.back();
         nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync);
         // The slight fudge-factor is to deal with cases where
         // the vsync was estimated due to being slow handling the signal.
@@ -253,15 +295,17 @@
             // Already drew for this vsync pulse, UI draw request missed
             // the deadline for RT animations
             info.out.canDrawThisFrame = false;
-        } else if (lastSwap.swapTime < latestVsync) {
+        } else if (vsyncDelta >= mRenderThread.timeLord().frameIntervalNanos() * 3
+                || (latestVsync - mLastDropVsync) < 500_ms) {
+            // It's been several frame intervals, assume the buffer queue is fine
+            // or the last drop was too recent
             info.out.canDrawThisFrame = true;
         } else {
-            // We're maybe behind? Find out for sure
-            int runningBehind = 0;
-            // TODO: Have this method be on Surface, too, not just ANativeWindow...
-            ANativeWindow* window = mNativeSurface.get();
-            window->query(window, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
-            info.out.canDrawThisFrame = !runningBehind;
+            info.out.canDrawThisFrame = !isSwapChainStuffed();
+            if (!info.out.canDrawThisFrame) {
+                // dropping frame
+                mLastDropVsync = mRenderThread.timeLord().latestVsync();
+            }
         }
     } else {
         info.out.canDrawThisFrame = true;
@@ -282,6 +326,7 @@
 
 void CanvasContext::stopDrawing() {
     mRenderThread.removeFrameCallback(this);
+    mAnimationContext->pauseAnimators();
 }
 
 void CanvasContext::notifyFramePending() {
@@ -515,10 +560,27 @@
         }
         SwapHistory& swap = mSwapHistory.next();
         swap.damage = screenDirty;
-        swap.swapTime = systemTime(CLOCK_MONOTONIC);
+        swap.swapCompletedTime = systemTime(CLOCK_MONOTONIC);
         swap.vsyncTime = mRenderThread.timeLord().latestVsync();
+        if (mNativeSurface.get()) {
+            int durationUs;
+            mNativeSurface->query(NATIVE_WINDOW_LAST_DEQUEUE_DURATION, &durationUs);
+            swap.dequeueDuration = us2ns(durationUs);
+            mNativeSurface->query(NATIVE_WINDOW_LAST_QUEUE_DURATION, &durationUs);
+            swap.queueDuration = us2ns(durationUs);
+        } else {
+            swap.dequeueDuration = 0;
+            swap.queueDuration = 0;
+        }
+        mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration)
+                = swap.dequeueDuration;
+        mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration)
+                = swap.queueDuration;
         mHaveNewSurface = false;
         mFrameNumber = -1;
+    } else {
+        mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = 0;
+        mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = 0;
     }
 
     // TODO: Use a fence for real completion?
@@ -546,6 +608,10 @@
     }
 
     GpuMemoryTracker::onFrameCompleted();
+#ifdef BUGREPORT_FONT_CACHE_USAGE
+    caches.fontRenderer.getFontRenderer().historyTracker().frameCompleted();
+#endif
+
 }
 
 // Called by choreographer to do an RT-driven animation
@@ -571,6 +637,9 @@
     prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node);
     if (info.out.canDrawThisFrame) {
         draw();
+    } else {
+        // wait on fences so tasks don't overlap next frame
+        waitOnFences();
     }
 }
 
@@ -783,6 +852,7 @@
     }
     sp<FuncTask> task(new FuncTask());
     task->func = func;
+    mFrameFences.push_back(task);
     mFrameWorkProcessor->add(task);
 }
 
diff --git a/libs/hwui/renderthread/CanvasContext.h b/libs/hwui/renderthread/CanvasContext.h
index a6eb7ad..e182175 100644
--- a/libs/hwui/renderthread/CanvasContext.h
+++ b/libs/hwui/renderthread/CanvasContext.h
@@ -168,6 +168,8 @@
 
     ANDROID_API int64_t getFrameNumber();
 
+    void waitOnFences();
+
 private:
     friend class RegisterFrameCallbackTask;
     // TODO: Replace with something better for layer & other GL object
@@ -178,7 +180,7 @@
 
     void freePrefetchedLayers(TreeObserver* observer);
 
-    void waitOnFences();
+    bool isSwapChainStuffed();
 
     EGLint mLastFrameWidth = 0;
     EGLint mLastFrameHeight = 0;
@@ -198,12 +200,17 @@
     struct SwapHistory {
         SkRect damage;
         nsecs_t vsyncTime;
-        nsecs_t swapTime;
+        nsecs_t swapCompletedTime;
+        nsecs_t dequeueDuration;
+        nsecs_t queueDuration;
     };
 
     RingBuffer<SwapHistory, 3> mSwapHistory;
     int64_t mFrameNumber = -1;
 
+    // last vsync for a dropped frame due to stuffed queue
+    nsecs_t mLastDropVsync = 0;
+
     bool mOpaque;
 #if HWUI_NEW_OPS
     BakedOpRenderer::LightInfo mLightInfo;
diff --git a/libs/hwui/renderthread/DrawFrameTask.cpp b/libs/hwui/renderthread/DrawFrameTask.cpp
index c9c07b3..e3b6dc6 100644
--- a/libs/hwui/renderthread/DrawFrameTask.cpp
+++ b/libs/hwui/renderthread/DrawFrameTask.cpp
@@ -104,6 +104,9 @@
 
     if (CC_LIKELY(canDrawThisFrame)) {
         context->draw();
+    } else {
+        // wait on fences so tasks don't overlap next frame
+        context->waitOnFences();
     }
 
     if (!canUnblockUiThread) {
diff --git a/libs/hwui/renderthread/RenderProxy.cpp b/libs/hwui/renderthread/RenderProxy.cpp
index 54af282..a734401 100644
--- a/libs/hwui/renderthread/RenderProxy.cpp
+++ b/libs/hwui/renderthread/RenderProxy.cpp
@@ -22,9 +22,11 @@
 #include "Readback.h"
 #include "Rect.h"
 #include "renderthread/CanvasContext.h"
+#include "renderthread/EglManager.h"
 #include "renderthread/RenderTask.h"
 #include "renderthread/RenderThread.h"
 #include "utils/Macros.h"
+#include "utils/TimeUtils.h"
 
 namespace android {
 namespace uirenderer {
@@ -44,6 +46,8 @@
     typedef struct { \
         a1; a2; a3; a4; a5; a6; a7; a8; \
     } ARGS(name); \
+    static_assert(std::is_trivially_destructible<ARGS(name)>::value, \
+            "Error, ARGS must be trivially destructible!"); \
     static void* Bridge_ ## name(ARGS(name)* args)
 
 #define SETUP_TASK(method) \
@@ -154,7 +158,7 @@
     SETUP_TASK(updateSurface);
     args->context = mContext;
     args->surface = surface.get();
-    postAndWait(task);
+    post(task);
 }
 
 CREATE_BRIDGE2(pauseSurface, CanvasContext* context, Surface* surface) {
@@ -514,6 +518,10 @@
     post(task);
 }
 
+int RenderProxy::getRenderThreadTid() {
+    return mRenderThread.getTid();
+}
+
 CREATE_BRIDGE3(addRenderNode, CanvasContext* context, RenderNode* node, bool placeFront) {
     args->context->addRenderNode(args->node, args->placeFront);
     return nullptr;
@@ -632,6 +640,41 @@
             reinterpret_cast<intptr_t>( staticPostAndWait(task) ));
 }
 
+CREATE_BRIDGE2(prepareToDraw, RenderThread* thread, SkBitmap* bitmap) {
+    if (Caches::hasInstance() && args->thread->eglManager().hasEglContext()) {
+        ATRACE_NAME("Bitmap#prepareToDraw task");
+        Caches::getInstance().textureCache.prefetch(args->bitmap);
+    }
+    delete args->bitmap;
+    args->bitmap = nullptr;
+    return nullptr;
+}
+
+void RenderProxy::prepareToDraw(const SkBitmap& bitmap) {
+    // If we haven't spun up a hardware accelerated window yet, there's no
+    // point in precaching these bitmaps as it can't impact jank.
+    // We also don't know if we even will spin up a hardware-accelerated
+    // window or not.
+    if (!RenderThread::hasInstance()) return;
+    RenderThread* renderThread = &RenderThread::getInstance();
+    SETUP_TASK(prepareToDraw);
+    args->thread = renderThread;
+    args->bitmap = new SkBitmap(bitmap);
+    nsecs_t lastVsync = renderThread->timeLord().latestVsync();
+    nsecs_t estimatedNextVsync = lastVsync + renderThread->timeLord().frameIntervalNanos();
+    nsecs_t timeToNextVsync = estimatedNextVsync - systemTime(CLOCK_MONOTONIC);
+    // We expect the UI thread to take 4ms and for RT to be active from VSYNC+4ms to
+    // VSYNC+12ms or so, so aim for the gap during which RT is expected to
+    // be idle
+    // TODO: Make this concept a first-class supported thing? RT could use
+    // knowledge of pending draws to better schedule this task
+    if (timeToNextVsync > -6_ms && timeToNextVsync < 1_ms) {
+        renderThread->queueAt(task, estimatedNextVsync + 8_ms);
+    } else {
+        renderThread->queue(task);
+    }
+}
+
 void RenderProxy::post(RenderTask* task) {
     mRenderThread.queue(task);
 }
diff --git a/libs/hwui/renderthread/RenderProxy.h b/libs/hwui/renderthread/RenderProxy.h
index 898b314..bb111bd 100644
--- a/libs/hwui/renderthread/RenderProxy.h
+++ b/libs/hwui/renderthread/RenderProxy.h
@@ -115,6 +115,7 @@
 
     ANDROID_API void setTextureAtlas(const sp<GraphicBuffer>& buffer, int64_t* map, size_t size);
     ANDROID_API void setProcessStatsBuffer(int fd);
+    ANDROID_API int getRenderThreadTid();
 
     ANDROID_API void serializeDisplayListTree();
 
@@ -128,6 +129,7 @@
     ANDROID_API long getDroppedFrameReportCount();
 
     ANDROID_API static int copySurfaceInto(sp<Surface>& surface, SkBitmap* bitmap);
+    ANDROID_API static void prepareToDraw(const SkBitmap& bitmap);
 
 private:
     RenderThread& mRenderThread;
diff --git a/libs/hwui/renderthread/RenderThread.cpp b/libs/hwui/renderthread/RenderThread.cpp
index 3c1c0bc..9688340 100644
--- a/libs/hwui/renderthread/RenderThread.cpp
+++ b/libs/hwui/renderthread/RenderThread.cpp
@@ -190,7 +190,7 @@
     initializeDisplayEventReceiver();
     mEglManager = new EglManager(*this);
     mRenderState = new RenderState(*this);
-    mJankTracker = new JankTracker(frameIntervalNanos);
+    mJankTracker = new JankTracker(mDisplayInfo);
 }
 
 int RenderThread::displayEventReceiverCallback(int fd, int events, void* data) {
diff --git a/libs/hwui/tests/common/TestContext.cpp b/libs/hwui/tests/common/TestContext.cpp
index 146e735..9956975 100644
--- a/libs/hwui/tests/common/TestContext.cpp
+++ b/libs/hwui/tests/common/TestContext.cpp
@@ -33,7 +33,6 @@
     false, // secure?
     0, // appVsyncOffset
     0, // presentationDeadline
-    0, // colorTransform
 };
 
 DisplayInfo getBuiltInDisplay() {
diff --git a/libs/hwui/tests/unit/ClipAreaTests.cpp b/libs/hwui/tests/unit/ClipAreaTests.cpp
index 54ca68d..d4d7919 100644
--- a/libs/hwui/tests/unit/ClipAreaTests.cpp
+++ b/libs/hwui/tests/unit/ClipAreaTests.cpp
@@ -275,5 +275,73 @@
     }
 }
 
+TEST(ClipArea, serializeIntersectedClip_scale) {
+    ClipArea area(createClipArea());
+    area.setClip(0, 0, 400, 400);
+    LinearAllocator allocator;
+
+    SkPath circlePath;
+    circlePath.addCircle(50, 50, 50);
+
+    ClipRegion recordedClip;
+    recordedClip.region.setPath(circlePath, SkRegion(SkIRect::MakeWH(100, 100)));
+    recordedClip.rect = Rect(100, 100);
+
+    Matrix4 translateScale;
+    translateScale.loadTranslate(100, 100, 0);
+    translateScale.scale(2, 2, 1);
+    auto resolvedClip = area.serializeIntersectedClip(allocator, &recordedClip, translateScale);
+
+    ASSERT_NE(nullptr, resolvedClip);
+    EXPECT_EQ(ClipMode::Region, resolvedClip->mode);
+    EXPECT_EQ(Rect(100, 100, 300, 300), resolvedClip->rect);
+    auto clipRegion = reinterpret_cast<const ClipRegion*>(resolvedClip);
+    EXPECT_EQ(SkIRect::MakeLTRB(100, 100, 300, 300), clipRegion->region.getBounds());
+}
+
+TEST(ClipArea, applyTransformToRegion_identity) {
+    SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
+    ClipArea::applyTransformToRegion(Matrix4::identity(), &region);
+    EXPECT_TRUE(region.isRect());
+    EXPECT_EQ(SkIRect::MakeLTRB(1, 2, 3, 4), region.getBounds());
+}
+
+TEST(ClipArea, applyTransformToRegion_translate) {
+    SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
+    Matrix4 transform;
+    transform.loadTranslate(10, 20, 0);
+    ClipArea::applyTransformToRegion(transform, &region);
+    EXPECT_TRUE(region.isRect());
+    EXPECT_EQ(SkIRect::MakeLTRB(11, 22, 13, 24), region.getBounds());
+}
+
+TEST(ClipArea, applyTransformToRegion_scale) {
+    SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
+    Matrix4 transform;
+    transform.loadScale(2, 3, 1);
+    ClipArea::applyTransformToRegion(transform, &region);
+    EXPECT_TRUE(region.isRect());
+    EXPECT_EQ(SkIRect::MakeLTRB(2, 6, 6, 12), region.getBounds());
+}
+
+TEST(ClipArea, applyTransformToRegion_translateScale) {
+    SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
+    Matrix4 transform;
+    transform.translate(10, 20);
+    transform.scale(2, 3, 1);
+    ClipArea::applyTransformToRegion(transform, &region);
+    EXPECT_TRUE(region.isRect());
+    EXPECT_EQ(SkIRect::MakeLTRB(12, 26, 16, 32), region.getBounds());
+}
+
+TEST(ClipArea, applyTransformToRegion_rotate90) {
+    SkRegion region(SkIRect::MakeLTRB(1, 2, 3, 4));
+    Matrix4 transform;
+    transform.loadRotate(90);
+    ClipArea::applyTransformToRegion(transform, &region);
+    EXPECT_TRUE(region.isRect());
+    EXPECT_EQ(SkIRect::MakeLTRB(-4, 1, -2, 3), region.getBounds());
+}
+
 } // namespace uirenderer
 } // namespace android
diff --git a/libs/hwui/tests/unit/FrameBuilderTests.cpp b/libs/hwui/tests/unit/FrameBuilderTests.cpp
index af54e07..53dbede 100644
--- a/libs/hwui/tests/unit/FrameBuilderTests.cpp
+++ b/libs/hwui/tests/unit/FrameBuilderTests.cpp
@@ -1459,7 +1459,8 @@
 
 static void drawOrderedRect(RecordingCanvas* canvas, uint8_t expectedDrawOrder) {
     SkPaint paint;
-    paint.setColor(SkColorSetARGB(256, 0, 0, expectedDrawOrder)); // order put in blue channel
+    // order put in blue channel, transparent so overlapped content doesn't get rejected
+    paint.setColor(SkColorSetARGB(1, 0, 0, expectedDrawOrder));
     canvas->drawRect(0, 0, 100, 100, paint);
 }
 static void drawOrderedNode(RecordingCanvas* canvas, uint8_t expectedDrawOrder, float z) {
diff --git a/libs/hwui/tests/unit/RecordingCanvasTests.cpp b/libs/hwui/tests/unit/RecordingCanvasTests.cpp
index 18171de..c072d0b 100644
--- a/libs/hwui/tests/unit/RecordingCanvasTests.cpp
+++ b/libs/hwui/tests/unit/RecordingCanvasTests.cpp
@@ -81,6 +81,27 @@
     ASSERT_EQ(0u, dl->getOps().size()) << "Must be zero ops. Rect should be rejected.";
 }
 
+TEST(RecordingCanvas, emptyPaintRejection) {
+    auto dl = TestUtils::createDisplayList<RecordingCanvas>(200, 200, [](RecordingCanvas& canvas) {
+        SkPaint emptyPaint;
+        emptyPaint.setColor(Color::Transparent);
+
+        float points[] = {0, 0, 200, 200};
+        canvas.drawPoints(points, 4, emptyPaint);
+        canvas.drawLines(points, 4, emptyPaint);
+        canvas.drawRect(0, 0, 200, 200, emptyPaint);
+        canvas.drawRegion(SkRegion(SkIRect::MakeWH(200, 200)), emptyPaint);
+        canvas.drawRoundRect(0, 0, 200, 200, 10, 10, emptyPaint);
+        canvas.drawCircle(100, 100, 100, emptyPaint);
+        canvas.drawOval(0, 0, 200, 200, emptyPaint);
+        canvas.drawArc(0, 0, 200, 200, 0, 360, true, emptyPaint);
+        SkPath path;
+        path.addRect(0, 0, 200, 200);
+        canvas.drawPath(path, emptyPaint);
+    });
+    EXPECT_EQ(0u, dl->getOps().size()) << "Op should be rejected";
+}
+
 TEST(RecordingCanvas, drawArc) {
     auto dl = TestUtils::createDisplayList<RecordingCanvas>(200, 200, [](RecordingCanvas& canvas) {
         canvas.drawArc(0, 0, 200, 200, 0, 180, true, SkPaint());
@@ -340,6 +361,36 @@
     EXPECT_EQ(3, count);
 }
 
+TEST(RecordingCanvas, saveLayer_rounding) {
+    auto dl = TestUtils::createDisplayList<RecordingCanvas>(100, 100, [](RecordingCanvas& canvas) {
+            canvas.saveLayerAlpha(10.25f, 10.75f, 89.25f, 89.75f, 128, SaveFlags::ClipToLayer);
+            canvas.drawRect(20, 20, 80, 80, SkPaint());
+            canvas.restore();
+        });
+        int count = 0;
+        playbackOps(*dl, [&count](const RecordedOp& op) {
+            Matrix4 expectedMatrix;
+            switch(count++) {
+            case 0:
+                EXPECT_EQ(RecordedOpId::BeginLayerOp, op.opId);
+                EXPECT_EQ(Rect(10, 10, 90, 90), op.unmappedBounds) << "Expect bounds rounded out";
+                break;
+            case 1:
+                EXPECT_EQ(RecordedOpId::RectOp, op.opId);
+                expectedMatrix.loadTranslate(-10, -10, 0);
+                EXPECT_MATRIX_APPROX_EQ(expectedMatrix, op.localMatrix) << "Expect rounded offset";
+                break;
+            case 2:
+                EXPECT_EQ(RecordedOpId::EndLayerOp, op.opId);
+                // Don't bother asserting recording state data - it's not used
+                break;
+            default:
+                ADD_FAILURE();
+            }
+        });
+        EXPECT_EQ(3, count);
+}
+
 TEST(RecordingCanvas, saveLayer_missingRestore) {
     auto dl = TestUtils::createDisplayList<RecordingCanvas>(200, 200, [](RecordingCanvas& canvas) {
         canvas.saveLayerAlpha(0, 0, 200, 200, 128, SaveFlags::ClipToLayer);
@@ -494,6 +545,21 @@
     EXPECT_EQ(3, count);
 }
 
+TEST(RecordingCanvas, saveLayer_rejectBegin) {
+    auto dl = TestUtils::createDisplayList<RecordingCanvas>(200, 200, [](RecordingCanvas& canvas) {
+        canvas.save(SaveFlags::MatrixClip);
+        canvas.translate(0, -20); // avoid identity case
+        // empty clip rect should force layer + contents to be rejected
+        canvas.clipRect(0, -20, 200, -20, SkRegion::kIntersect_Op);
+        canvas.saveLayerAlpha(0, 0, 200, 200, 128, SaveFlags::ClipToLayer);
+        canvas.drawRect(0, 0, 200, 200, SkPaint());
+        canvas.restore();
+        canvas.restore();
+    });
+
+    ASSERT_EQ(0u, dl->getOps().size()) << "Begin/Rect/End should all be rejected.";
+}
+
 TEST(RecordingCanvas, drawRenderNode_rejection) {
     auto child = TestUtils::createNode(50, 50, 150, 150,
             [](RenderProperties& props, RecordingCanvas& canvas) {
diff --git a/libs/hwui/tests/unit/RenderNodeTests.cpp b/libs/hwui/tests/unit/RenderNodeTests.cpp
index b2997df..cf76a86 100644
--- a/libs/hwui/tests/unit/RenderNodeTests.cpp
+++ b/libs/hwui/tests/unit/RenderNodeTests.cpp
@@ -16,13 +16,26 @@
 
 #include <gtest/gtest.h>
 
+#include "AnimationContext.h"
+#include "DamageAccumulator.h"
+#include "IContextFactory.h"
 #include "RenderNode.h"
 #include "TreeInfo.h"
+#include "renderthread/CanvasContext.h"
 #include "tests/common/TestUtils.h"
 #include "utils/Color.h"
 
 using namespace android;
 using namespace android::uirenderer;
+using namespace android::uirenderer::renderthread;
+
+class ContextFactory : public android::uirenderer::IContextFactory {
+public:
+    android::uirenderer::AnimationContext* createAnimationContext
+        (android::uirenderer::renderthread::TimeLord& clock) override {
+        return new android::uirenderer::AnimationContext(clock);
+    }
+};
 
 TEST(RenderNode, hasParents) {
     auto child = TestUtils::createNode(0, 0, 200, 400,
@@ -89,3 +102,31 @@
     TestUtils::syncHierarchyPropertiesAndDisplayList(node);
     EXPECT_EQ(0, refcnt);
 }
+
+RENDERTHREAD_TEST(RenderNode, prepareTree_nullableDisplayList) {
+    ContextFactory contextFactory;
+    CanvasContext canvasContext(renderThread, false, nullptr, &contextFactory);
+    TreeInfo info(TreeInfo::MODE_RT_ONLY, canvasContext);
+    DamageAccumulator damageAccumulator;
+    info.damageAccumulator = &damageAccumulator;
+    info.observer = nullptr;
+
+    {
+        auto nonNullDLNode = TestUtils::createNode(0, 0, 200, 400,
+                [](RenderProperties& props, TestCanvas& canvas) {
+            canvas.drawColor(Color::Red_500, SkXfermode::kSrcOver_Mode);
+        });
+        TestUtils::syncHierarchyPropertiesAndDisplayList(nonNullDLNode);
+        EXPECT_TRUE(nonNullDLNode->getDisplayList());
+        nonNullDLNode->prepareTree(info);
+    }
+
+    {
+        auto nullDLNode = TestUtils::createNode(0, 0, 200, 400, nullptr);
+        TestUtils::syncHierarchyPropertiesAndDisplayList(nullDLNode);
+        EXPECT_FALSE(nullDLNode->getDisplayList());
+        nullDLNode->prepareTree(info);
+    }
+
+    canvasContext.destroy(nullptr);
+}
diff --git a/libs/hwui/utils/TimeUtils.h b/libs/hwui/utils/TimeUtils.h
index 8d42d7e..ce181b7 100644
--- a/libs/hwui/utils/TimeUtils.h
+++ b/libs/hwui/utils/TimeUtils.h
@@ -21,10 +21,18 @@
 namespace android {
 namespace uirenderer {
 
+constexpr nsecs_t operator"" _s (unsigned long long s) {
+    return seconds_to_nanoseconds(s);
+}
+
 constexpr nsecs_t operator"" _ms (unsigned long long ms) {
     return milliseconds_to_nanoseconds(ms);
 }
 
+constexpr nsecs_t operator"" _us (unsigned long long us) {
+    return microseconds_to_nanoseconds(us);
+}
+
 } /* namespace uirenderer */
 } /* namespace android */
 
diff --git a/libs/input/PointerController.cpp b/libs/input/PointerController.cpp
index 27193b7..abef66f 100644
--- a/libs/input/PointerController.cpp
+++ b/libs/input/PointerController.cpp
@@ -36,6 +36,29 @@
 
 namespace android {
 
+// --- WeakLooperCallback ---
+
+class WeakLooperCallback: public LooperCallback {
+protected:
+    virtual ~WeakLooperCallback() { }
+
+public:
+    WeakLooperCallback(const wp<LooperCallback>& callback) :
+        mCallback(callback) {
+    }
+
+    virtual int handleEvent(int fd, int events, void* data) {
+        sp<LooperCallback> callback = mCallback.promote();
+        if (callback != NULL) {
+            return callback->handleEvent(fd, events, data);
+        }
+        return 0; // the client is gone, remove the callback
+    }
+
+private:
+    wp<LooperCallback> mCallback;
+};
+
 // --- PointerController ---
 
 // Time to wait before starting the fade when the pointer is inactive.
@@ -57,10 +80,11 @@
         const sp<Looper>& looper, const sp<SpriteController>& spriteController) :
         mPolicy(policy), mLooper(looper), mSpriteController(spriteController) {
     mHandler = new WeakMessageHandler(this);
+    mCallback = new WeakLooperCallback(this);
 
     if (mDisplayEventReceiver.initCheck() == NO_ERROR) {
         mLooper->addFd(mDisplayEventReceiver.getFd(), Looper::POLL_CALLBACK,
-                       Looper::EVENT_INPUT, this, nullptr);
+                       Looper::EVENT_INPUT, mCallback, nullptr);
     } else {
         ALOGE("Failed to initialize DisplayEventReceiver.");
     }
diff --git a/libs/input/PointerController.h b/libs/input/PointerController.h
index 99292d7..4794f3d 100644
--- a/libs/input/PointerController.h
+++ b/libs/input/PointerController.h
@@ -144,6 +144,7 @@
     sp<Looper> mLooper;
     sp<SpriteController> mSpriteController;
     sp<WeakMessageHandler> mHandler;
+    sp<LooperCallback> mCallback;
 
     DisplayEventReceiver mDisplayEventReceiver;
 
diff --git a/location/java/android/location/GnssMeasurementsEvent.java b/location/java/android/location/GnssMeasurementsEvent.java
index 3151694..7db0466 100644
--- a/location/java/android/location/GnssMeasurementsEvent.java
+++ b/location/java/android/location/GnssMeasurementsEvent.java
@@ -98,13 +98,13 @@
             throw new InvalidParameterException("Parameter 'clock' must not be null.");
         }
         if (measurements == null || measurements.length == 0) {
-            throw new InvalidParameterException(
-                    "Parameter 'measurements' must not be null or empty.");
+            mReadOnlyMeasurements = Collections.emptyList();
+        } else {
+            Collection<GnssMeasurement> measurementCollection = Arrays.asList(measurements);
+            mReadOnlyMeasurements = Collections.unmodifiableCollection(measurementCollection);
         }
 
         mClock = clock;
-        Collection<GnssMeasurement> measurementCollection = Arrays.asList(measurements);
-        mReadOnlyMeasurements = Collections.unmodifiableCollection(measurementCollection);
     }
 
     /**
diff --git a/media/java/android/media/AudioAttributes.java b/media/java/android/media/AudioAttributes.java
index 5286f8f..89709ee 100644
--- a/media/java/android/media/AudioAttributes.java
+++ b/media/java/android/media/AudioAttributes.java
@@ -24,6 +24,7 @@
 import android.os.Parcelable;
 import android.text.TextUtils;
 import android.util.Log;
+import android.util.SparseIntArray;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
@@ -170,6 +171,66 @@
     public final static int USAGE_VIRTUAL_SOURCE = 15;
 
     /**
+     * IMPORTANT: when adding new usage types, add them to SDK_USAGES and update SUPPRESSIBLE_USAGES
+     *            if applicable.
+     */
+
+    /**
+     * @hide
+     * Denotes a usage for notifications that do not expect immediate intervention from the user,
+     * will be muted when the Zen mode disables notifications
+     * @see #SUPPRESSIBLE_USAGES
+     */
+    public final static int SUPPRESSIBLE_NOTIFICATION = 1;
+    /**
+     * @hide
+     * Denotes a usage for notifications that do expect immediate intervention from the user,
+     * will be muted when the Zen mode disables calls
+     * @see #SUPPRESSIBLE_USAGES
+     */
+    public final static int SUPPRESSIBLE_CALL = 2;
+
+    /**
+     * @hide
+     * Array of all usage types for calls and notifications to assign the suppression behavior,
+     * used by the Zen mode restrictions.
+     * @see com.android.server.notification.ZenModeHelper
+     */
+    public static final SparseIntArray SUPPRESSIBLE_USAGES;
+
+    static {
+        SUPPRESSIBLE_USAGES = new SparseIntArray();
+        SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION,                      SUPPRESSIBLE_NOTIFICATION);
+        SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_RINGTONE,             SUPPRESSIBLE_CALL);
+        SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_REQUEST,SUPPRESSIBLE_CALL);
+        SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_INSTANT,SUPPRESSIBLE_NOTIFICATION);
+        SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_COMMUNICATION_DELAYED,SUPPRESSIBLE_NOTIFICATION);
+        SUPPRESSIBLE_USAGES.put(USAGE_NOTIFICATION_EVENT,                SUPPRESSIBLE_NOTIFICATION);
+    }
+
+    /**
+     * @hide
+     * Array of all usage types exposed in the SDK that applications can use.
+     */
+    public final static int[] SDK_USAGES = {
+            USAGE_UNKNOWN,
+            USAGE_MEDIA,
+            USAGE_VOICE_COMMUNICATION,
+            USAGE_VOICE_COMMUNICATION_SIGNALLING,
+            USAGE_ALARM,
+            USAGE_NOTIFICATION,
+            USAGE_NOTIFICATION_RINGTONE,
+            USAGE_NOTIFICATION_COMMUNICATION_REQUEST,
+            USAGE_NOTIFICATION_COMMUNICATION_INSTANT,
+            USAGE_NOTIFICATION_COMMUNICATION_DELAYED,
+            USAGE_NOTIFICATION_EVENT,
+            USAGE_ASSISTANCE_ACCESSIBILITY,
+            USAGE_ASSISTANCE_NAVIGATION_GUIDANCE,
+            USAGE_ASSISTANCE_SONIFICATION,
+            USAGE_GAME
+    };
+
+    /**
      * Flag defining a behavior where the audibility of the sound will be ensured by the system.
      */
     public final static int FLAG_AUDIBILITY_ENFORCED = 0x1 << 0;
diff --git a/media/java/android/media/AudioFormat.java b/media/java/android/media/AudioFormat.java
index a4484e7..81cc93d 100644
--- a/media/java/android/media/AudioFormat.java
+++ b/media/java/android/media/AudioFormat.java
@@ -263,6 +263,9 @@
      * on some platforms when converting to short internally.
      */
     public static final int ENCODING_IEC61937 = 13;
+    /** Audio data format: DOLBY TRUEHD compressed
+     **/
+    public static final int ENCODING_DOLBY_TRUEHD = 14;
 
     /** Invalid audio channel configuration */
     /** @deprecated Use {@link #CHANNEL_INVALID} instead.  */
diff --git a/media/java/android/media/AudioManager.java b/media/java/android/media/AudioManager.java
index f19a262..33c1c3f 100644
--- a/media/java/android/media/AudioManager.java
+++ b/media/java/android/media/AudioManager.java
@@ -3289,7 +3289,10 @@
 
     /**
      * Used as a key for {@link #getProperty} to request the native or optimal output sample rate
-     * for this device's primary output stream, in decimal Hz.
+     * for this device's low latency output stream, in decimal Hz.  Latency-sensitive apps
+     * should use this value as a default, and offer the user the option to override it.
+     * The low latency output stream is typically either the device's primary output stream,
+     * or another output stream with smaller buffers.
      */
     // FIXME Deprecate
     public static final String PROPERTY_OUTPUT_SAMPLE_RATE =
@@ -3297,7 +3300,10 @@
 
     /**
      * Used as a key for {@link #getProperty} to request the native or optimal output buffer size
-     * for this device's primary output stream, in decimal PCM frames.
+     * for this device's low latency output stream, in decimal PCM frames.  Latency-sensitive apps
+     * should use this value as a minimum, and offer the user the option to override it.
+     * The low latency output stream is typically either the device's primary output stream,
+     * or another output stream with smaller buffers.
      */
     // FIXME Deprecate
     public static final String PROPERTY_OUTPUT_FRAMES_PER_BUFFER =
diff --git a/media/java/android/media/AudioRecordingConfiguration.java b/media/java/android/media/AudioRecordingConfiguration.java
index 354339c..50dbd03 100644
--- a/media/java/android/media/AudioRecordingConfiguration.java
+++ b/media/java/android/media/AudioRecordingConfiguration.java
@@ -57,12 +57,12 @@
     /**
      * @hide
      */
-    public AudioRecordingConfiguration(int session, int source, AudioFormat devFormat,
-            AudioFormat clientFormat, int patchHandle) {
+    public AudioRecordingConfiguration(int session, int source, AudioFormat clientFormat,
+            AudioFormat devFormat, int patchHandle) {
         mSessionId = session;
         mClientSource = source;
-        mDeviceFormat = devFormat;
         mClientFormat = clientFormat;
+        mDeviceFormat = devFormat;
         mPatchHandle = patchHandle;
     }
 
diff --git a/media/java/android/media/ImageReader.java b/media/java/android/media/ImageReader.java
index ec2d4bc..ed71849 100644
--- a/media/java/android/media/ImageReader.java
+++ b/media/java/android/media/ImageReader.java
@@ -241,6 +241,10 @@
      * same {@link Surface} can be reused with a different API once the first source is
      * disconnected from the {@link Surface}.</p>
      *
+     * <p>Please note that holding on to the Surface object returned by this method is not enough
+     * to keep its parent ImageReader from being reclaimed. In that sense, a Surface acts like a
+     * {@link java.lang.ref.WeakReference weak reference} to the ImageReader that provides it.</p>
+     *
      * @return A {@link Surface} to use for a drawing target for various APIs.
      */
     public Surface getSurface() {
@@ -512,11 +516,36 @@
             mAcquiredImages.clear();
 
             nativeClose();
-        }
 
-        if (mEstimatedNativeAllocBytes > 0) {
-            VMRuntime.getRuntime().registerNativeFree(mEstimatedNativeAllocBytes);
-            mEstimatedNativeAllocBytes = 0;
+            if (mEstimatedNativeAllocBytes > 0) {
+                VMRuntime.getRuntime().registerNativeFree(mEstimatedNativeAllocBytes);
+                mEstimatedNativeAllocBytes = 0;
+            }
+        }
+    }
+
+    /**
+     * Discard any free buffers owned by this ImageReader.
+     *
+     * <p>
+     * Generally, the ImageReader caches buffers for reuse once they have been
+     * allocated, for best performance. However, sometimes it may be important to
+     * release all the cached, unused buffers to save on memory.
+     * </p>
+     * <p>
+     * Calling this method will discard all free cached buffers. This does not include any buffers
+     * associated with Images acquired from the ImageReader, any filled buffers waiting to be
+     * acquired, and any buffers currently in use by the source rendering buffers into the
+     * ImageReader's Surface.
+     * <p>
+     * The ImageReader continues to be usable after this call, but may need to reallocate buffers
+     * when more buffers are needed for rendering.
+     * </p>
+     * @hide
+     */
+    public void discardFreeBuffers() {
+        synchronized (mCloseLock) {
+            nativeDiscardFreeBuffers();
         }
     }
 
@@ -872,6 +901,7 @@
     private synchronized native void nativeReleaseImage(Image i);
     private synchronized native Surface nativeGetSurface();
     private synchronized native int nativeDetachImage(Image i);
+    private synchronized native void nativeDiscardFreeBuffers();
 
     /**
      * @return A return code {@code ACQUIRE_*}
diff --git a/media/java/android/media/MediaCodec.java b/media/java/android/media/MediaCodec.java
index 542dced..264944f 100644
--- a/media/java/android/media/MediaCodec.java
+++ b/media/java/android/media/MediaCodec.java
@@ -133,9 +133,10 @@
  <p class=note>
  Note that on some devices the slice-height is advertised as 0. This could mean either that the
  slice-height is the same as the frame height, or that the slice-height is the frame height
- aligned to some value (usually a power of 2). Unfortunately, there is no way to tell the actual
- slice height in this case. Furthermore, the vertical stride of the {@code U} plane in planar
- formats is also not specified or defined, though usually it is half of the slice height.
+ aligned to some value (usually a power of 2). Unfortunately, there is no standard and simple way
+ to tell the actual slice height in this case. Furthermore, the vertical stride of the {@code U}
+ plane in planar formats is also not specified or defined, though usually it is half of the slice
+ height.
  <p>
  The {@link MediaFormat#KEY_WIDTH} and {@link MediaFormat#KEY_HEIGHT} keys specify the size of the
  video frames; however, for most encondings the video (picture) only occupies a portion of the
@@ -620,8 +621,9 @@
  mode} will be automatically applied with one exception:
  <p class=note>
  Prior to the {@link android.os.Build.VERSION_CODES#M} release, software decoders may not
- have applied the rotation when being rendered onto a Surface. Unfortunately, there is no way to
- identify software decoders, or if they apply the rotation other than by trying it out.
+ have applied the rotation when being rendered onto a Surface. Unfortunately, there is no standard
+ and simple way to identify software decoders, or if they apply the rotation other than by trying
+ it out.
  <p>
  There are also some caveats.
  <p class=note>
diff --git a/media/java/android/media/MediaExtractor.java b/media/java/android/media/MediaExtractor.java
index 6f5199b..c8aedd0 100644
--- a/media/java/android/media/MediaExtractor.java
+++ b/media/java/android/media/MediaExtractor.java
@@ -496,6 +496,11 @@
     /**
      * Advance to the next sample. Returns false if no more sample data
      * is available (end of stream).
+     *
+     * When extracting a local file, the behaviors of {@link #advance} and
+     * {@link #readSampleData} are undefined in presence of concurrent
+     * writes to the same local file; more specifically, end of stream
+     * could be signalled earlier than expected.
      */
     public native boolean advance();
 
diff --git a/media/java/android/media/MediaFile.java b/media/java/android/media/MediaFile.java
index f7becf5..da490b9 100644
--- a/media/java/android/media/MediaFile.java
+++ b/media/java/android/media/MediaFile.java
@@ -69,8 +69,9 @@
 
     // More video file types
     public static final int FILE_TYPE_MP2PS   = 200;
+    public static final int FILE_TYPE_QT      = 201;
     private static final int FIRST_VIDEO_FILE_TYPE2 = FILE_TYPE_MP2PS;
-    private static final int LAST_VIDEO_FILE_TYPE2 = FILE_TYPE_MP2PS;
+    private static final int LAST_VIDEO_FILE_TYPE2 = FILE_TYPE_QT;
 
     // Image file types
     public static final int FILE_TYPE_JPEG    = 31;
@@ -211,6 +212,8 @@
         addFileType("MPG", FILE_TYPE_MP4, "video/mpeg", MtpConstants.FORMAT_MPEG);
         addFileType("MP4", FILE_TYPE_MP4, "video/mp4", MtpConstants.FORMAT_MPEG);
         addFileType("M4V", FILE_TYPE_M4V, "video/mp4", MtpConstants.FORMAT_MPEG);
+        addFileType("MOV", FILE_TYPE_QT, "video/quicktime", MtpConstants.FORMAT_MPEG);
+
         addFileType("3GP", FILE_TYPE_3GPP, "video/3gpp",  MtpConstants.FORMAT_3GP_CONTAINER);
         addFileType("3GPP", FILE_TYPE_3GPP, "video/3gpp", MtpConstants.FORMAT_3GP_CONTAINER);
         addFileType("3G2", FILE_TYPE_3GPP2, "video/3gpp2", MtpConstants.FORMAT_3GP_CONTAINER);
diff --git a/media/java/android/media/MediaFormat.java b/media/java/android/media/MediaFormat.java
index d7a18d9..d74aa81 100644
--- a/media/java/android/media/MediaFormat.java
+++ b/media/java/android/media/MediaFormat.java
@@ -49,12 +49,18 @@
  * <tr><td>{@link #KEY_FRAME_RATE}</td><td>Integer or Float</td><td>required for <b>encoders</b>,
  *         optional for <b>decoders</b></td></tr>
  * <tr><td>{@link #KEY_CAPTURE_RATE}</td><td>Integer</td><td></td></tr>
- * <tr><td>{@link #KEY_I_FRAME_INTERVAL}</td><td>Integer</td><td><b>encoder-only</b></td></tr>
+ * <tr><td>{@link #KEY_I_FRAME_INTERVAL}</td><td>Integer (or Float)</td><td><b>encoder-only</b>,
+ *         time-interval between key frames.
+ *         Float support added in {@link android.os.Build.VERSION_CODES#N_MR1}</td></tr>
  * <tr><td>{@link #KEY_INTRA_REFRESH_PERIOD}</td><td>Integer</td><td><b>encoder-only</b>, optional</td></tr>
  * <tr><td>{@link #KEY_MAX_WIDTH}</td><td>Integer</td><td><b>decoder-only</b>, optional, max-resolution width</td></tr>
  * <tr><td>{@link #KEY_MAX_HEIGHT}</td><td>Integer</td><td><b>decoder-only</b>, optional, max-resolution height</td></tr>
- * <tr><td>{@link #KEY_REPEAT_PREVIOUS_FRAME_AFTER}</td><td>Long</td><td><b>video encoder in surface-mode only</b></td></tr>
- * <tr><td>{@link #KEY_PUSH_BLANK_BUFFERS_ON_STOP}</td><td>Integer(1)</td><td><b>video decoder rendering to a surface only</b></td></tr>
+ * <tr><td>{@link #KEY_REPEAT_PREVIOUS_FRAME_AFTER}</td><td>Long</td><td><b>encoder in surface-mode
+ *         only</b>, optional</td></tr>
+ * <tr><td>{@link #KEY_PUSH_BLANK_BUFFERS_ON_STOP}</td><td>Integer(1)</td><td><b>decoder rendering
+ *         to a surface only</b>, optional</td></tr>
+ * <tr><td>{@link #KEY_TEMPORAL_LAYERING}</td><td>String</td><td><b>encoder only</b>, optional,
+ *         temporal-layering schema</td></tr>
  * </table>
  * Specify both {@link #KEY_MAX_WIDTH} and {@link #KEY_MAX_HEIGHT} to enable
  * adaptive playback (seamless resolution change) for a video decoder that
@@ -258,9 +264,20 @@
     public static final String KEY_CAPTURE_RATE = "capture-rate";
 
     /**
-     * A key describing the frequency of I frames expressed in secs
-     * between I frames.
-     * The associated value is an integer.
+     * A key describing the frequency of key frames expressed in seconds between key frames.
+     * <p>
+     * This key is used by video encoders.
+     * A negative value means no key frames are requested after the first frame.
+     * A zero value means a stream containing all key frames is requested.
+     * <p class=note>
+     * Most video encoders will convert this value of the number of non-key-frames between
+     * key-frames, using the {@linkplain #KEY_FRAME_RATE frame rate} information; therefore,
+     * if the actual frame rate differs (e.g. input frames are dropped or the frame rate
+     * changes), the <strong>time interval</strong> between key frames will not be the
+     * configured value.
+     * <p>
+     * The associated value is an integer (or float since
+     * {@link android.os.Build.VERSION_CODES#N_MR1}).
      */
     public static final String KEY_I_FRAME_INTERVAL = "i-frame-interval";
 
@@ -280,12 +297,18 @@
 
    /**
      * A key describing the temporal layering schema.  This is an optional parameter
-     * that applies only to video encoders.  Use {@link MediaCodec#getInputFormat}
+     * that applies only to video encoders.  Use {@link MediaCodec#getOutputFormat}
      * after {@link MediaCodec#configure configure} to query if the encoder supports
-     * the desired schema. Supported values are {@code webrtc.vp8.1-layer},
-     * {@code webrtc.vp8.2-layer}, {@code webrtc.vp8.3-layer}, and {@code none}.
-     * If the encoder does not support temporal layering, the input format will
-     * not have an entry with this key.
+     * the desired schema. Supported values are {@code webrtc.vp8.N-layer},
+     * {@code android.generic.N}, {@code android.generic.N+M} and {@code none}, where
+     * {@code N} denotes the total number of non-bidirectional layers (which must be at least 1)
+     * and {@code M} denotes the total number of bidirectional layers (which must be non-negative).
+     * <p class=note>{@code android.generic.*} schemas have been added in {@link
+     * android.os.Build.VERSION_CODES#N_MR1}.
+     * <p>
+     * The encoder may support fewer temporal layers, in which case the output format
+     * will contain the configured schema. If the encoder does not support temporal
+     * layering, the output format will not have an entry with this key.
      * The associated value is a string.
      */
     public static final String KEY_TEMPORAL_LAYERING = "ts-schema";
diff --git a/media/java/android/media/MediaPlayer.java b/media/java/android/media/MediaPlayer.java
index 92adfd9..949acc0 100644
--- a/media/java/android/media/MediaPlayer.java
+++ b/media/java/android/media/MediaPlayer.java
@@ -1641,7 +1641,8 @@
      * (i.e. reaches the end of the stream).
      * The media framework will attempt to transition from this player to
      * the next as seamlessly as possible. The next player can be set at
-     * any time before completion. The next player must be prepared by the
+     * any time before completion, but shall be after setDataSource has been
+     * called successfully. The next player must be prepared by the
      * app, and the application should not call start() on it.
      * The next MediaPlayer must be different from 'this'. An exception
      * will be thrown if next == this.
@@ -3494,7 +3495,7 @@
          * @param extra an extra code, specific to the info. Typically
          * implementation dependent.
          * @return True if the method handled the info, false if it didn't.
-         * Returning false, or not having an OnErrorListener at all, will
+         * Returning false, or not having an OnInfoListener at all, will
          * cause the info to be discarded.
          */
         boolean onInfo(MediaPlayer mp, int what, int extra);
diff --git a/media/java/android/media/MediaRecorder.java b/media/java/android/media/MediaRecorder.java
index 73485af..94e894f 100644
--- a/media/java/android/media/MediaRecorder.java
+++ b/media/java/android/media/MediaRecorder.java
@@ -746,6 +746,27 @@
     }
 
     /**
+     * Sets the video encoding profile for recording. Call this method before prepare().
+     * Prepare() may perform additional checks on the parameter to make sure whether the
+     * specified profile and level are applicable, and sometimes the passed profile or
+     * level will be discarded due to codec capablity or to ensure the video recording
+     * can proceed smoothly based on the capabilities of the platform.
+     * @hide
+     * @param profile declared in {@link MediaCodecInfo.CodecProfileLevel}.
+     * @param level declared in {@link MediaCodecInfo.CodecProfileLevel}.
+     */
+    public void setVideoEncodingProfileLevel(int profile, int level) {
+        if (profile <= 0)  {
+            throw new IllegalArgumentException("Video encoding profile is not positive");
+        }
+        if (level <= 0)  {
+            throw new IllegalArgumentException("Video encoding level is not positive");
+        }
+        setParameter("video-param-encoder-profile=" + profile);
+        setParameter("video-param-encoder-level=" + level);
+    }
+
+    /**
      * Currently not implemented. It does nothing.
      * @deprecated Time lapse mode video recording using camera still image capture
      * is not desirable, and will not be supported.
diff --git a/media/java/android/media/MediaRouter.java b/media/java/android/media/MediaRouter.java
index 101facd..79195d6 100644
--- a/media/java/android/media/MediaRouter.java
+++ b/media/java/android/media/MediaRouter.java
@@ -220,8 +220,7 @@
 
             if (mBluetoothA2dpRoute != null) {
                 final boolean a2dpEnabled = isBluetoothA2dpOn();
-                if (mainType != AudioRoutesInfo.MAIN_SPEAKER &&
-                        mSelectedRoute == mBluetoothA2dpRoute && !a2dpEnabled) {
+                if (mSelectedRoute == mBluetoothA2dpRoute && !a2dpEnabled) {
                     selectRouteStatic(ROUTE_TYPE_LIVE_AUDIO, mDefaultAudioVideo, false);
                 } else if ((mSelectedRoute == mDefaultAudioVideo || mSelectedRoute == null) &&
                         a2dpEnabled) {
diff --git a/media/java/android/media/MediaScanner.java b/media/java/android/media/MediaScanner.java
index 8b6c38b..ddfa025 100644
--- a/media/java/android/media/MediaScanner.java
+++ b/media/java/android/media/MediaScanner.java
@@ -21,12 +21,14 @@
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
+import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.database.SQLException;
 import android.drm.DrmManagerClient;
 import android.graphics.BitmapFactory;
 import android.mtp.MtpConstants;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Environment;
 import android.os.RemoteException;
 import android.os.SystemProperties;
@@ -60,11 +62,14 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.text.SimpleDateFormat;
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Locale;
+import java.util.TimeZone;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
@@ -150,6 +155,11 @@
     private static final String MUSIC_DIR = "/music/";
     private static final String PODCAST_DIR = "/podcasts/";
 
+    public static final String SCANNED_BUILD_PREFS_NAME = "MediaScanBuild";
+    public static final String LAST_INTERNAL_SCAN_FINGERPRINT = "lastScanFingerprint";
+    private static final String SYSTEM_SOUNDS_DIR = "/system/media/audio";
+    private static String sLastInternalScanFingerprint;
+
     private static final String[] ID3_GENRES = {
         // ID3v1 Genres
         "Blues",
@@ -399,6 +409,13 @@
         mMediaProvider = mContext.getContentResolver()
                 .acquireContentProviderClient(MediaStore.AUTHORITY);
 
+        if (sLastInternalScanFingerprint == null) {
+            final SharedPreferences scanSettings =
+                    mContext.getSharedPreferences(SCANNED_BUILD_PREFS_NAME, Context.MODE_PRIVATE);
+            sLastInternalScanFingerprint =
+                    scanSettings.getString(LAST_INTERNAL_SCAN_FINGERPRINT, new String());
+        }
+
         mAudioUri = Audio.Media.getContentUri(volumeName);
         mVideoUri = Video.Media.getContentUri(volumeName);
         mImagesUri = Images.Media.getContentUri(volumeName);
@@ -451,6 +468,8 @@
 
     private class MyMediaScannerClient implements MediaScannerClient {
 
+        private final SimpleDateFormat mDateFormatter;
+
         private String mArtist;
         private String mAlbumArtist;    // use this if mArtist is missing
         private String mAlbum;
@@ -463,6 +482,7 @@
         private int mYear;
         private int mDuration;
         private String mPath;
+        private long mDate;
         private long mLastModified;
         private long mFileSize;
         private String mWriter;
@@ -472,6 +492,11 @@
         private int mWidth;
         private int mHeight;
 
+        public MyMediaScannerClient() {
+            mDateFormatter = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
+            mDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
+        }
+
         public FileEntry beginFile(String path, String mimeType, long lastModified,
                 long fileSize, boolean isDirectory, boolean noMedia) {
             mMimeType = mimeType;
@@ -537,6 +562,7 @@
             mYear = 0;
             mDuration = 0;
             mPath = path;
+            mDate = 0;
             mLastModified = lastModified;
             mWriter = null;
             mCompilation = 0;
@@ -573,16 +599,24 @@
                     entry.mRowId = 0;
                 }
 
-                if (entry.mPath != null &&
-                        ((!mDefaultNotificationSet &&
+                if (entry.mPath != null) {
+                    if (((!mDefaultNotificationSet &&
                                 doesPathHaveFilename(entry.mPath, mDefaultNotificationFilename))
                         || (!mDefaultRingtoneSet &&
                                 doesPathHaveFilename(entry.mPath, mDefaultRingtoneFilename))
                         || (!mDefaultAlarmSet &&
                                 doesPathHaveFilename(entry.mPath, mDefaultAlarmAlertFilename)))) {
-                    Log.w(TAG, "forcing rescan of " + entry.mPath +
-                            "since ringtone setting didn't finish");
-                    scanAlways = true;
+                        Log.w(TAG, "forcing rescan of " + entry.mPath +
+                                "since ringtone setting didn't finish");
+                        scanAlways = true;
+                    } else if (isSystemSoundWithMetadata(entry.mPath)
+                            && !Build.FINGERPRINT.equals(sLastInternalScanFingerprint)) {
+                        // file is located on the system partition where the date cannot be trusted:
+                        // rescan if the build fingerprint has changed since the last scan.
+                        Log.i(TAG, "forcing rescan of " + entry.mPath
+                                + " since build fingerprint changed");
+                        scanAlways = true;
+                    }
                 }
 
                 // rescan for metadata if file was modified since last scan
@@ -627,6 +661,14 @@
             return result;
         }
 
+        private long parseDate(String date) {
+            try {
+              return mDateFormatter.parse(date).getTime();
+            } catch (ParseException e) {
+              return 0;
+            }
+        }
+
         private int parseSubstring(String s, int start, int defaultValue) {
             int length = s.length();
             if (start == length) return defaultValue;
@@ -684,6 +726,8 @@
                 mCompilation = parseSubstring(value, 0, 0);
             } else if (name.equalsIgnoreCase("isdrm")) {
                 mIsDrm = (parseSubstring(value, 0, 0) == 1);
+            } else if (name.equalsIgnoreCase("date")) {
+                mDate = parseDate(value);
             } else if (name.equalsIgnoreCase("width")) {
                 mWidth = parseSubstring(value, 0, 0);
             } else if (name.equalsIgnoreCase("height")) {
@@ -830,6 +874,9 @@
                     if (resolution != null) {
                         map.put(Video.Media.RESOLUTION, resolution);
                     }
+                    if (mDate > 0) {
+                        map.put(Video.Media.DATE_TAKEN, mDate);
+                    }
                 } else if (MediaFile.isImageFileType(mFileType)) {
                     // FIXME - add DESCRIPTION
                 } else if (MediaFile.isAudioFileType(mFileType)) {
@@ -1103,6 +1150,15 @@
 
     }; // end of anonymous MediaScannerClient instance
 
+    private static boolean isSystemSoundWithMetadata(String path) {
+        if (path.startsWith(SYSTEM_SOUNDS_DIR + ALARMS_DIR)
+                || path.startsWith(SYSTEM_SOUNDS_DIR + RINGTONES_DIR)
+                || path.startsWith(SYSTEM_SOUNDS_DIR + NOTIFICATIONS_DIR)) {
+            return true;
+        }
+        return false;
+    }
+
     private String settingSetIndicatorName(String base) {
         return base + "_set";
     }
@@ -1227,16 +1283,6 @@
         }
     }
 
-    private boolean inScanDirectory(String path, String[] directories) {
-        for (int i = 0; i < directories.length; i++) {
-            String directory = directories[i];
-            if (path.startsWith(directory)) {
-                return true;
-            }
-        }
-        return false;
-    }
-
     private void pruneDeadThumbnailFiles() {
         HashSet<String> existingFiles = new HashSet<String>();
         String directory = "/sdcard/DCIM/.thumbnails";
diff --git a/media/java/android/media/PlayerBase.java b/media/java/android/media/PlayerBase.java
index 0f7dc9a..b262d97 100644
--- a/media/java/android/media/PlayerBase.java
+++ b/media/java/android/media/PlayerBase.java
@@ -181,10 +181,15 @@
      * @return
      */
     boolean isRestricted_sync() {
+        // check app ops
+        if (mHasAppOpsPlayAudio) {
+            return false;
+        }
+        // check bypass flag
         if ((mAttributes.getAllFlags() & AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY) != 0) {
             return false;
         }
-        return !mHasAppOpsPlayAudio;
+        return true;
     }
 
     // Abstract methods a subclass needs to implement
diff --git a/media/java/android/media/SoundPool.java b/media/java/android/media/SoundPool.java
index 5ede1d5..9fafda4 100644
--- a/media/java/android/media/SoundPool.java
+++ b/media/java/android/media/SoundPool.java
@@ -505,27 +505,31 @@
     }
 
     private boolean isRestricted() {
-        IAudioService service = getService();
-        boolean cameraSoundForced = false;
-
-        try {
-            cameraSoundForced = service.isCameraSoundForced();
-        } catch (RemoteException e) {
-            Log.e(TAG, "Cannot access AudioService in isRestricted()");
-        }
-
-        if (cameraSoundForced &&
-                ((mAttributes.getAllFlags() & AudioAttributes.FLAG_AUDIBILITY_ENFORCED) != 0)
-// FIXME: should also check usage when set properly by camera app
-//                && (mAttributes.getUsage() == AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
-                ) {
+        // check app ops
+        if (mHasAppOpsPlayAudio) {
             return false;
         }
-
+        // check bypass flag
         if ((mAttributes.getAllFlags() & AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY) != 0) {
             return false;
         }
-        return !mHasAppOpsPlayAudio;
+        // check force audibility flag and camera restriction
+        if ((mAttributes.getAllFlags() & AudioAttributes.FLAG_AUDIBILITY_ENFORCED) != 0) {
+// FIXME: should also check usage when set properly by camera app
+//          && (mAttributes.getUsage() == AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
+            boolean cameraSoundForced = false;
+            try {
+                cameraSoundForced = getService().isCameraSoundForced();
+            } catch (RemoteException e) {
+                Log.e(TAG, "Cannot access AudioService in isRestricted()");
+            } catch (NullPointerException e) {
+                Log.e(TAG, "Null AudioService in isRestricted()");
+            }
+            if (cameraSoundForced) {
+                return false;
+            }
+        }
+        return true;
     }
 
     private void updateAppOpsPlayAudio() {
diff --git a/media/java/android/media/soundtrigger/SoundTriggerDetector.java b/media/java/android/media/soundtrigger/SoundTriggerDetector.java
index df0961b..d5296ae 100644
--- a/media/java/android/media/soundtrigger/SoundTriggerDetector.java
+++ b/media/java/android/media/soundtrigger/SoundTriggerDetector.java
@@ -15,6 +15,7 @@
  */
 
 package android.media.soundtrigger;
+import static android.hardware.soundtrigger.SoundTrigger.STATUS_OK;
 
 import android.annotation.IntDef;
 import android.annotation.NonNull;
@@ -243,27 +244,29 @@
 
         boolean allowMultipleTriggers =
                 (recognitionFlags & RECOGNITION_FLAG_ALLOW_MULTIPLE_TRIGGERS) != 0;
+        int status = STATUS_OK;
         try {
-            mSoundTriggerService.startRecognition(new ParcelUuid(mSoundModelId),
+            status = mSoundTriggerService.startRecognition(new ParcelUuid(mSoundModelId),
                     mRecognitionCallback, new RecognitionConfig(captureTriggerAudio,
                         allowMultipleTriggers, null, null));
         } catch (RemoteException e) {
             return false;
         }
-        return true;
+        return status == STATUS_OK;
     }
 
     /**
      * Stops recognition for the associated model.
      */
     public boolean stopRecognition() {
+        int status = STATUS_OK;
         try {
-            mSoundTriggerService.stopRecognition(new ParcelUuid(mSoundModelId),
+            status = mSoundTriggerService.stopRecognition(new ParcelUuid(mSoundModelId),
                     mRecognitionCallback);
         } catch (RemoteException e) {
             return false;
         }
-        return true;
+        return status == STATUS_OK;
     }
 
     /**
diff --git a/media/java/android/mtp/MtpDevice.java b/media/java/android/mtp/MtpDevice.java
index 4082778..e7ea1a5 100644
--- a/media/java/android/mtp/MtpDevice.java
+++ b/media/java/android/mtp/MtpDevice.java
@@ -18,11 +18,13 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.content.Context;
 import android.hardware.usb.UsbDevice;
 import android.hardware.usb.UsbDeviceConnection;
 import android.os.CancellationSignal;
 import android.os.ParcelFileDescriptor;
 
+import android.os.UserManager;
 import com.android.internal.util.Preconditions;
 
 import java.io.IOException;
@@ -62,7 +64,17 @@
      * @return true if the device was successfully opened.
      */
     public boolean open(UsbDeviceConnection connection) {
-        boolean result = native_open(mDevice.getDeviceName(), connection.getFileDescriptor());
+        boolean result = false;
+
+        Context context = connection.getContext();
+        if (context != null) {
+            UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
+
+            if (!userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
+                result = native_open(mDevice.getDeviceName(), connection.getFileDescriptor());
+            }
+        }
+
         if (!result) {
             connection.close();
         }
diff --git a/media/jni/android_media_ImageReader.cpp b/media/jni/android_media_ImageReader.cpp
index c3993ae..724fc02 100644
--- a/media/jni/android_media_ImageReader.cpp
+++ b/media/jni/android_media_ImageReader.cpp
@@ -611,6 +611,23 @@
     return OK;
 }
 
+static void ImageReader_discardFreeBuffers(JNIEnv* env, jobject thiz) {
+    ALOGV("%s:", __FUNCTION__);
+    JNIImageReaderContext* ctx = ImageReader_getContext(env, thiz);
+    if (ctx == NULL) {
+        jniThrowException(env, "java/lang/IllegalStateException", "ImageReader was already closed");
+        return;
+    }
+
+    BufferItemConsumer* bufferConsumer = ctx->getBufferConsumer();
+    status_t res = bufferConsumer->discardFreeBuffers();
+    if (res != OK) {
+        ALOGE("Buffer discard failed: %s (%d)", strerror(-res), res);
+        jniThrowRuntimeException(env,
+                "nativeDicardFreebuffers failed");
+    }
+}
+
 static jobject ImageReader_getSurface(JNIEnv* env, jobject thiz)
 {
     ALOGV("%s: ", __FUNCTION__);
@@ -773,6 +790,7 @@
     {"nativeImageSetup",       "(Landroid/media/Image;)I",   (void*)ImageReader_imageSetup },
     {"nativeGetSurface",       "()Landroid/view/Surface;",   (void*)ImageReader_getSurface },
     {"nativeDetachImage",      "(Landroid/media/Image;)I",   (void*)ImageReader_detachImage },
+    {"nativeDiscardFreeBuffers", "()V",                      (void*)ImageReader_discardFreeBuffers }
 };
 
 static const JNINativeMethod gImageMethods[] = {
diff --git a/media/jni/audioeffect/android_media_Visualizer.cpp b/media/jni/audioeffect/android_media_Visualizer.cpp
index 75a3b6f..0645543 100644
--- a/media/jni/audioeffect/android_media_Visualizer.cpp
+++ b/media/jni/audioeffect/android_media_Visualizer.cpp
@@ -435,14 +435,12 @@
 
 // ----------------------------------------------------------------------------
 static void android_media_visualizer_native_release(JNIEnv *env,  jobject thiz) {
-    // ensure that lpVisualizer is deleted before lpJniStorage
-    {
+    { //limit scope so that lpVisualizer is deleted before JNI storage data.
         sp<Visualizer> lpVisualizer = setVisualizer(env, thiz, 0);
         if (lpVisualizer == 0) {
             return;
         }
     }
-
     // delete the JNI data
     VisualizerJniStorage* lpJniStorage =
         (VisualizerJniStorage *)env->GetLongField(thiz, fields.fidJniData);
diff --git a/opengl/java/android/opengl/GLSurfaceView.java b/opengl/java/android/opengl/GLSurfaceView.java
index 38ed932..0f5dd3a 100644
--- a/opengl/java/android/opengl/GLSurfaceView.java
+++ b/opengl/java/android/opengl/GLSurfaceView.java
@@ -16,6 +16,13 @@
 
 package android.opengl;
 
+import android.content.Context;
+import android.os.Trace;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+
 import java.io.Writer;
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
@@ -29,15 +36,6 @@
 import javax.microedition.khronos.opengles.GL;
 import javax.microedition.khronos.opengles.GL10;
 
-import android.content.Context;
-import android.content.pm.ConfigurationInfo;
-import android.os.SystemProperties;
-import android.os.Trace;
-import android.util.AttributeSet;
-import android.util.Log;
-import android.view.SurfaceHolder;
-import android.view.SurfaceView;
-
 /**
  * An implementation of SurfaceView that uses the dedicated surface for
  * displaying OpenGL rendering.
@@ -119,9 +117,9 @@
  * {@link #setRenderMode}. The default is continuous rendering.
  * <p>
  * <h3>Activity Life-cycle</h3>
- * A GLSurfaceView must be notified when the activity is paused and resumed. GLSurfaceView clients
- * are required to call {@link #onPause()} when the activity pauses and
- * {@link #onResume()} when the activity resumes. These calls allow GLSurfaceView to
+ * A GLSurfaceView must be notified when to pause and resume rendering. GLSurfaceView clients
+ * are required to call {@link #onPause()} when the activity stops and
+ * {@link #onResume()} when the activity starts. These calls allow GLSurfaceView to
  * pause and resume the rendering thread, and also allow GLSurfaceView to release and recreate
  * the OpenGL display.
  * <p>
@@ -294,10 +292,12 @@
      * resumed.
      * <p>
      * If set to true, then the EGL context may be preserved when the GLSurfaceView is paused.
-     * Whether the EGL context is actually preserved or not depends upon whether the
-     * Android device that the program is running on can support an arbitrary number of EGL
-     * contexts or not. Devices that can only support a limited number of EGL contexts must
-     * release the  EGL context in order to allow multiple applications to share the GPU.
+     * <p>
+     * Prior to API level 11, whether the EGL context is actually preserved or not
+     * depends upon whether the Android device can support an arbitrary number of
+     * EGL contexts or not. Devices that can only support a limited number of EGL
+     * contexts must release the EGL context in order to allow multiple applications
+     * to share the GPU.
      * <p>
      * If set to false, the EGL context will be released when the GLSurfaceView is paused,
      * and recreated when the GLSurfaceView is resumed.
@@ -554,9 +554,13 @@
 
 
     /**
-     * Inform the view that the activity is paused. The owner of this view must
-     * call this method when the activity is paused. Calling this method will
-     * pause the rendering thread.
+     * Pause the rendering thread, optionally tearing down the EGL context
+     * depending upon the value of {@link #setPreserveEGLContextOnPause(boolean)}.
+     *
+     * This method should be called when it is no longer desirable for the
+     * GLSurfaceView to continue rendering, such as in response to
+     * {@link android.app.Activity#onStop Activity.onStop}.
+     *
      * Must not be called before a renderer has been set.
      */
     public void onPause() {
@@ -564,10 +568,12 @@
     }
 
     /**
-     * Inform the view that the activity is resumed. The owner of this view must
-     * call this method when the activity is resumed. Calling this method will
-     * recreate the OpenGL display and resume the rendering
-     * thread.
+     * Resumes the rendering thread, re-creating the OpenGL context if necessary. It
+     * is the counterpart to {@link #onPause()}.
+     *
+     * This method should typically be called in
+     * {@link android.app.Activity#onStart Activity.onStart}.
+     *
      * Must not be called before a renderer has been set.
      */
     public void onResume() {
@@ -1354,7 +1360,7 @@
                                 GLSurfaceView view = mGLSurfaceViewWeakRef.get();
                                 boolean preserveEglContextOnPause = view == null ?
                                         false : view.mPreserveEGLContextOnPause;
-                                if (!preserveEglContextOnPause || sGLThreadManager.shouldReleaseEGLContextWhenPausing()) {
+                                if (!preserveEglContextOnPause) {
                                     stopEglContextLocked();
                                     if (LOG_SURFACE) {
                                         Log.i("GLThread", "releasing EGL context because paused tid=" + getId());
@@ -1362,16 +1368,6 @@
                                 }
                             }
 
-                            // When pausing, optionally terminate EGL:
-                            if (pausing) {
-                                if (sGLThreadManager.shouldTerminateEGLWhenPausing()) {
-                                    mEglHelper.finish();
-                                    if (LOG_SURFACE) {
-                                        Log.i("GLThread", "terminating EGL because paused tid=" + getId());
-                                    }
-                                }
-                            }
-
                             // Have we lost the SurfaceView surface?
                             if ((! mHasSurface) && (! mWaitingForSurface)) {
                                 if (LOG_SURFACE) {
@@ -1411,7 +1407,7 @@
                                 if (! mHaveEglContext) {
                                     if (askedToReleaseEglContext) {
                                         askedToReleaseEglContext = false;
-                                    } else if (sGLThreadManager.tryAcquireEglContextLocked(this)) {
+                                    } else {
                                         try {
                                             mEglHelper.start();
                                         } catch (RuntimeException t) {
@@ -1506,7 +1502,6 @@
                     if (createGlInterface) {
                         gl = (GL10) mEglHelper.createGL();
 
-                        sGLThreadManager.checkGLDriver(gl);
                         createGlInterface = false;
                     }
 
@@ -1888,111 +1883,16 @@
                 Log.i("GLThread", "exiting tid=" +  thread.getId());
             }
             thread.mExited = true;
-            if (mEglOwner == thread) {
-                mEglOwner = null;
-            }
             notifyAll();
         }
 
         /*
-         * Tries once to acquire the right to use an EGL
-         * context. Does not block. Requires that we are already
-         * in the sGLThreadManager monitor when this is called.
-         *
-         * @return true if the right to use an EGL context was acquired.
-         */
-        public boolean tryAcquireEglContextLocked(GLThread thread) {
-            if (mEglOwner == thread || mEglOwner == null) {
-                mEglOwner = thread;
-                notifyAll();
-                return true;
-            }
-            checkGLESVersion();
-            if (mMultipleGLESContextsAllowed) {
-                return true;
-            }
-            // Notify the owning thread that it should release the context.
-            // TODO: implement a fairness policy. Currently
-            // if the owning thread is drawing continuously it will just
-            // reacquire the EGL context.
-            if (mEglOwner != null) {
-                mEglOwner.requestReleaseEglContextLocked();
-            }
-            return false;
-        }
-
-        /*
          * Releases the EGL context. Requires that we are already in the
          * sGLThreadManager monitor when this is called.
          */
         public void releaseEglContextLocked(GLThread thread) {
-            if (mEglOwner == thread) {
-                mEglOwner = null;
-            }
             notifyAll();
         }
-
-        public synchronized boolean shouldReleaseEGLContextWhenPausing() {
-            // Release the EGL context when pausing even if
-            // the hardware supports multiple EGL contexts.
-            // Otherwise the device could run out of EGL contexts.
-            return mLimitedGLESContexts;
-        }
-
-        public synchronized boolean shouldTerminateEGLWhenPausing() {
-            checkGLESVersion();
-            return !mMultipleGLESContextsAllowed;
-        }
-
-        public synchronized void checkGLDriver(GL10 gl) {
-            if (! mGLESDriverCheckComplete) {
-                checkGLESVersion();
-                String renderer = gl.glGetString(GL10.GL_RENDERER);
-                if (mGLESVersion < kGLES_20) {
-                    mMultipleGLESContextsAllowed =
-                        ! renderer.startsWith(kMSM7K_RENDERER_PREFIX);
-                    notifyAll();
-                }
-                mLimitedGLESContexts = !mMultipleGLESContextsAllowed;
-                if (LOG_SURFACE) {
-                    Log.w(TAG, "checkGLDriver renderer = \"" + renderer + "\" multipleContextsAllowed = "
-                        + mMultipleGLESContextsAllowed
-                        + " mLimitedGLESContexts = " + mLimitedGLESContexts);
-                }
-                mGLESDriverCheckComplete = true;
-            }
-        }
-
-        private void checkGLESVersion() {
-            if (! mGLESVersionCheckComplete) {
-                mGLESVersion = SystemProperties.getInt(
-                        "ro.opengles.version",
-                        ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
-                if (mGLESVersion >= kGLES_20) {
-                    mMultipleGLESContextsAllowed = true;
-                }
-                if (LOG_SURFACE) {
-                    Log.w(TAG, "checkGLESVersion mGLESVersion =" +
-                            " " + mGLESVersion + " mMultipleGLESContextsAllowed = " + mMultipleGLESContextsAllowed);
-                }
-                mGLESVersionCheckComplete = true;
-            }
-        }
-
-        /**
-         * This check was required for some pre-Android-3.0 hardware. Android 3.0 provides
-         * support for hardware-accelerated views, therefore multiple EGL contexts are
-         * supported on all Android 3.0+ EGL drivers.
-         */
-        private boolean mGLESVersionCheckComplete;
-        private int mGLESVersion;
-        private boolean mGLESDriverCheckComplete;
-        private boolean mMultipleGLESContextsAllowed;
-        private boolean mLimitedGLESContexts;
-        private static final int kGLES_20 = 0x20000;
-        private static final String kMSM7K_RENDERER_PREFIX =
-            "Q3Dimension MSM7500 ";
-        private GLThread mEglOwner;
     }
 
     private static final GLThreadManager sGLThreadManager = new GLThreadManager();
diff --git a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
index 6fb8b51..b58c87a 100644
--- a/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
+++ b/packages/CaptivePortalLogin/src/com/android/captiveportallogin/CaptivePortalLoginActivity.java
@@ -96,7 +96,7 @@
         // Exit app if Network disappears.
         final NetworkCapabilities networkCapabilities = mCm.getNetworkCapabilities(mNetwork);
         if (networkCapabilities == null) {
-            finish();
+            finishAndRemoveTask();
             return;
         }
         mNetworkCallback = new NetworkCallback() {
@@ -163,7 +163,7 @@
                 mCaptivePortal.useNetwork();
                 break;
         }
-        finish();
+        finishAndRemoveTask();
     }
 
     @Override
diff --git a/packages/DocumentsUI/AndroidManifest.xml b/packages/DocumentsUI/AndroidManifest.xml
index 69912ab..a3edae3 100644
--- a/packages/DocumentsUI/AndroidManifest.xml
+++ b/packages/DocumentsUI/AndroidManifest.xml
@@ -11,6 +11,7 @@
     <application
         android:name=".DocumentsApplication"
         android:label="@string/app_label"
+        android:icon="@mipmap/ic_app_icon"
         android:supportsRtl="true">
 
         <activity
diff --git a/packages/DocumentsUI/res/drawable/ic_check_circle.xml b/packages/DocumentsUI/res/drawable/ic_check_circle.xml
index d49ba6a..62a4e34 100644
--- a/packages/DocumentsUI/res/drawable/ic_check_circle.xml
+++ b/packages/DocumentsUI/res/drawable/ic_check_circle.xml
@@ -19,6 +19,6 @@
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#FF009688"
+        android:fillColor="?android:attr/colorAccent"
         android:pathData="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10,-4.48 10,-10S17.52 2 12 2zm-2 15l-5,-5 1.41,-1.41L10 14.17l7.59,-7.59L19 8l-9 9z"/>
 </vector>
diff --git a/packages/DocumentsUI/res/mipmap-hdpi/ic_app_icon.png b/packages/DocumentsUI/res/mipmap-hdpi/ic_app_icon.png
new file mode 100644
index 0000000..cd3a037
--- /dev/null
+++ b/packages/DocumentsUI/res/mipmap-hdpi/ic_app_icon.png
Binary files differ
diff --git a/packages/DocumentsUI/res/mipmap-mdpi/ic_app_icon.png b/packages/DocumentsUI/res/mipmap-mdpi/ic_app_icon.png
new file mode 100644
index 0000000..8d08e9b
--- /dev/null
+++ b/packages/DocumentsUI/res/mipmap-mdpi/ic_app_icon.png
Binary files differ
diff --git a/packages/DocumentsUI/res/mipmap-xhdpi/ic_app_icon.png b/packages/DocumentsUI/res/mipmap-xhdpi/ic_app_icon.png
new file mode 100644
index 0000000..f3bacb7
--- /dev/null
+++ b/packages/DocumentsUI/res/mipmap-xhdpi/ic_app_icon.png
Binary files differ
diff --git a/packages/DocumentsUI/res/mipmap-xxhdpi/ic_app_icon.png b/packages/DocumentsUI/res/mipmap-xxhdpi/ic_app_icon.png
new file mode 100644
index 0000000..5156171
--- /dev/null
+++ b/packages/DocumentsUI/res/mipmap-xxhdpi/ic_app_icon.png
Binary files differ
diff --git a/packages/DocumentsUI/res/mipmap-xxxhdpi/ic_app_icon.png b/packages/DocumentsUI/res/mipmap-xxxhdpi/ic_app_icon.png
new file mode 100644
index 0000000..6dc2f76
--- /dev/null
+++ b/packages/DocumentsUI/res/mipmap-xxxhdpi/ic_app_icon.png
Binary files differ
diff --git a/packages/DocumentsUI/res/values/colors.xml b/packages/DocumentsUI/res/values/colors.xml
index cf0643d..366a8a4 100644
--- a/packages/DocumentsUI/res/values/colors.xml
+++ b/packages/DocumentsUI/res/values/colors.xml
@@ -15,9 +15,6 @@
 -->
 
 <resources>
-    <color name="material_grey_400">#ffbdbdbd</color>
-    <color name="material_teal_700">#ff00796b</color>
-
     <!-- This is the window background, but also the background for anything
          else that needs to manually declare a background matching the "default"
          app background (e.g. the drawer overlay). -->
@@ -26,22 +23,19 @@
     <color name="directory_background">#fff7f7f7</color>
     <color name="menu_search_background">@android:color/transparent</color>
 
-    <color name="primary_dark">@*android:color/primary_dark_material_dark</color>
-    <color name="primary">@*android:color/material_blue_grey_900</color>
-    <color name="accent">@*android:color/accent_material_light</color>
-    <color name="accent_dark">@*android:color/accent_material_dark</color>
-    <color name="action_mode">@color/material_grey_400</color>
-    <color name="status_bar_color">@*android:color/material_blue_grey_950</color>
+    <color name="primary_dark">@*android:color/primary_dark_device_default_settings</color>
+    <color name="primary">@*android:color/primary_device_default_settings</color>
+    <color name="accent">@*android:color/accent_device_default_light</color>
+    <color name="accent_dark">@*android:color/accent_device_default_dark</color>
 
     <color name="band_select_background">#88ffffff</color>
     <color name="band_select_border">#44000000</color>
 
-    <color name="item_doc_background_disabled">#fff4f4f4</color>
-
-    <color name="root_activated_color">@color/material_teal_700</color>
+    <color name="root_activated_color">@*android:color/accent_device_default_700</color>
 
     <!-- TODO: Would be nice to move this to a color-set, but not sure how to support animation -->
     <color name="item_doc_background">#fffafafa</color>
-    <color name="item_doc_background_selected">#ffe0f2f1</color>
+    <color name="item_doc_background_disabled">#fff4f4f4</color>
+    <color name="item_doc_background_selected">@*android:color/accent_device_default_50</color>
 
 </resources>
diff --git a/packages/DocumentsUI/res/values/strings.xml b/packages/DocumentsUI/res/values/strings.xml
index e67cc8a..c2d4d04 100644
--- a/packages/DocumentsUI/res/values/strings.xml
+++ b/packages/DocumentsUI/res/values/strings.xml
@@ -16,7 +16,7 @@
 
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <!-- Title of the documents application [CHAR LIMIT=32] -->
-    <string name="app_label">Documents</string>
+    <string name="app_label">Files</string>
 
     <!-- Title of the standalone downloads activity. [CHAR LIMIT=32] -->
     <string name="downloads_label">Downloads</string>
diff --git a/packages/DocumentsUI/res/values/styles.xml b/packages/DocumentsUI/res/values/styles.xml
index 9f09ebc..b5e32d4 100644
--- a/packages/DocumentsUI/res/values/styles.xml
+++ b/packages/DocumentsUI/res/values/styles.xml
@@ -28,9 +28,8 @@
         <item name="android:colorPrimaryDark">@color/primary_dark</item>
         <item name="android:colorPrimary">@color/primary</item>
         <item name="android:colorAccent">@color/accent</item>
-        <item name="colorActionMode">@color/action_mode</item>
+        <item name="android:colorControlActivated">?android:attr/colorAccent</item>
         <item name="android:queryBackground">@color/menu_search_background</item>
-        <item name="android:statusBarColor">@color/status_bar_color</item>
 
         <item name="android:listDivider">@*android:drawable/list_divider_material</item>
 
diff --git a/packages/DocumentsUI/src/com/android/documentsui/Events.java b/packages/DocumentsUI/src/com/android/documentsui/Events.java
index 14d4e2d..02a9127 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/Events.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/Events.java
@@ -53,7 +53,8 @@
      */
     public static boolean isTouchType(int toolType) {
         return toolType == MotionEvent.TOOL_TYPE_FINGER
-                || toolType == MotionEvent.TOOL_TYPE_STYLUS;
+                || toolType == MotionEvent.TOOL_TYPE_STYLUS
+                || toolType == MotionEvent.TOOL_TYPE_UNKNOWN;
     }
 
     /**
diff --git a/packages/DocumentsUI/src/com/android/documentsui/FilesActivity.java b/packages/DocumentsUI/src/com/android/documentsui/FilesActivity.java
index b82f8dd..7186339 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/FilesActivity.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/FilesActivity.java
@@ -341,8 +341,18 @@
 
         // Fall back to traditional VIEW action...
         intent = new Intent(Intent.ACTION_VIEW);
-        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
-        intent.setData(doc.derivedUri);
+        intent.setDataAndType(doc.derivedUri, doc.mimeType);
+
+        // Downloads has traditionally added the WRITE permission
+        // in the TrampolineActivity. Since this behavior is long
+        // established, we set the same permission for non-managed files
+        // This ensures consistent behavior between the Downloads root
+        // and other roots.
+        int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
+        if (doc.isWriteSupported()) {
+            flags |= Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
+        }
+        intent.setFlags(flags);
 
         if (DEBUG && intent.getClipData() != null) {
             Log.d(TAG, "Starting intent w/ clip data: " + intent.getClipData());
diff --git a/packages/DocumentsUI/src/com/android/documentsui/IconUtils.java b/packages/DocumentsUI/src/com/android/documentsui/IconUtils.java
index c28fae8..177ba0d 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/IconUtils.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/IconUtils.java
@@ -23,181 +23,7 @@
 import android.provider.DocumentsContract.Document;
 import android.util.TypedValue;
 
-import java.util.HashMap;
-
 public class IconUtils {
-
-    private static HashMap<String, Integer> sMimeIcons = new HashMap<>();
-
-    private static void add(String mimeType, int resId) {
-        if (sMimeIcons.put(mimeType, resId) != null) {
-            throw new RuntimeException(mimeType + " already registered!");
-        }
-    }
-
-    static {
-        int icon;
-
-        // Package
-        icon = R.drawable.ic_doc_apk;
-        add("application/vnd.android.package-archive", icon);
-
-        // Audio
-        icon = R.drawable.ic_doc_audio;
-        add("application/ogg", icon);
-        add("application/x-flac", icon);
-
-        // Certificate
-        icon = R.drawable.ic_doc_certificate;
-        add("application/pgp-keys", icon);
-        add("application/pgp-signature", icon);
-        add("application/x-pkcs12", icon);
-        add("application/x-pkcs7-certreqresp", icon);
-        add("application/x-pkcs7-crl", icon);
-        add("application/x-x509-ca-cert", icon);
-        add("application/x-x509-user-cert", icon);
-        add("application/x-pkcs7-certificates", icon);
-        add("application/x-pkcs7-mime", icon);
-        add("application/x-pkcs7-signature", icon);
-
-        // Source code
-        icon = R.drawable.ic_doc_codes;
-        add("application/rdf+xml", icon);
-        add("application/rss+xml", icon);
-        add("application/x-object", icon);
-        add("application/xhtml+xml", icon);
-        add("text/css", icon);
-        add("text/html", icon);
-        add("text/xml", icon);
-        add("text/x-c++hdr", icon);
-        add("text/x-c++src", icon);
-        add("text/x-chdr", icon);
-        add("text/x-csrc", icon);
-        add("text/x-dsrc", icon);
-        add("text/x-csh", icon);
-        add("text/x-haskell", icon);
-        add("text/x-java", icon);
-        add("text/x-literate-haskell", icon);
-        add("text/x-pascal", icon);
-        add("text/x-tcl", icon);
-        add("text/x-tex", icon);
-        add("application/x-latex", icon);
-        add("application/x-texinfo", icon);
-        add("application/atom+xml", icon);
-        add("application/ecmascript", icon);
-        add("application/json", icon);
-        add("application/javascript", icon);
-        add("application/xml", icon);
-        add("text/javascript", icon);
-        add("application/x-javascript", icon);
-
-        // Compressed
-        icon = R.drawable.ic_doc_compressed;
-        add("application/mac-binhex40", icon);
-        add("application/rar", icon);
-        add("application/zip", icon);
-        add("application/x-apple-diskimage", icon);
-        add("application/x-debian-package", icon);
-        add("application/x-gtar", icon);
-        add("application/x-iso9660-image", icon);
-        add("application/x-lha", icon);
-        add("application/x-lzh", icon);
-        add("application/x-lzx", icon);
-        add("application/x-stuffit", icon);
-        add("application/x-tar", icon);
-        add("application/x-webarchive", icon);
-        add("application/x-webarchive-xml", icon);
-        add("application/gzip", icon);
-        add("application/x-7z-compressed", icon);
-        add("application/x-deb", icon);
-        add("application/x-rar-compressed", icon);
-
-        // Contact
-        icon = R.drawable.ic_doc_contact;
-        add("text/x-vcard", icon);
-        add("text/vcard", icon);
-
-        // Event
-        icon = R.drawable.ic_doc_event;
-        add("text/calendar", icon);
-        add("text/x-vcalendar", icon);
-
-        // Font
-        icon = R.drawable.ic_doc_font;
-        add("application/x-font", icon);
-        add("application/font-woff", icon);
-        add("application/x-font-woff", icon);
-        add("application/x-font-ttf", icon);
-
-        // Image
-        icon = R.drawable.ic_doc_image;
-        add("application/vnd.oasis.opendocument.graphics", icon);
-        add("application/vnd.oasis.opendocument.graphics-template", icon);
-        add("application/vnd.oasis.opendocument.image", icon);
-        add("application/vnd.stardivision.draw", icon);
-        add("application/vnd.sun.xml.draw", icon);
-        add("application/vnd.sun.xml.draw.template", icon);
-
-        // PDF
-        icon = R.drawable.ic_doc_pdf;
-        add("application/pdf", icon);
-
-        // Presentation
-        icon = R.drawable.ic_doc_presentation;
-        add("application/vnd.stardivision.impress", icon);
-        add("application/vnd.sun.xml.impress", icon);
-        add("application/vnd.sun.xml.impress.template", icon);
-        add("application/x-kpresenter", icon);
-        add("application/vnd.oasis.opendocument.presentation", icon);
-
-        // Spreadsheet
-        icon = R.drawable.ic_doc_spreadsheet;
-        add("application/vnd.oasis.opendocument.spreadsheet", icon);
-        add("application/vnd.oasis.opendocument.spreadsheet-template", icon);
-        add("application/vnd.stardivision.calc", icon);
-        add("application/vnd.sun.xml.calc", icon);
-        add("application/vnd.sun.xml.calc.template", icon);
-        add("application/x-kspread", icon);
-
-        // Document
-        icon = R.drawable.ic_doc_document;
-        add("application/vnd.oasis.opendocument.text", icon);
-        add("application/vnd.oasis.opendocument.text-master", icon);
-        add("application/vnd.oasis.opendocument.text-template", icon);
-        add("application/vnd.oasis.opendocument.text-web", icon);
-        add("application/vnd.stardivision.writer", icon);
-        add("application/vnd.stardivision.writer-global", icon);
-        add("application/vnd.sun.xml.writer", icon);
-        add("application/vnd.sun.xml.writer.global", icon);
-        add("application/vnd.sun.xml.writer.template", icon);
-        add("application/x-abiword", icon);
-        add("application/x-kword", icon);
-
-        // Video
-        icon = R.drawable.ic_doc_video;
-        add("application/x-quicktimeplayer", icon);
-        add("application/x-shockwave-flash", icon);
-
-        // Word
-        icon = R.drawable.ic_doc_word;
-        add("application/msword", icon);
-        add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", icon);
-        add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", icon);
-
-        // Excel
-        icon = R.drawable.ic_doc_excel;
-        add("application/vnd.ms-excel", icon);
-        add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", icon);
-        add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", icon);
-
-        // Powerpoint
-        icon = R.drawable.ic_doc_powerpoint;
-        add("application/vnd.ms-powerpoint", icon);
-        add("application/vnd.openxmlformats-officedocument.presentationml.presentation", icon);
-        add("application/vnd.openxmlformats-officedocument.presentationml.template", icon);
-        add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", icon);
-    }
-
     public static Drawable loadPackageIcon(Context context, String authority, int icon) {
         if (icon != 0) {
             if (authority != null) {
@@ -225,7 +51,7 @@
             if (mode == State.MODE_GRID) {
                 return context.getDrawable(R.drawable.ic_grid_folder);
             } else {
-                return context.getDrawable(R.drawable.ic_doc_folder);
+                return context.getDrawable(com.android.internal.R.drawable.ic_doc_folder);
             }
         }
 
@@ -233,34 +59,7 @@
     }
 
     public static Drawable loadMimeIcon(Context context, String mimeType) {
-        if (Document.MIME_TYPE_DIR.equals(mimeType)) {
-            return context.getDrawable(R.drawable.ic_doc_folder);
-        }
-
-        // Look for exact match first
-        Integer resId = sMimeIcons.get(mimeType);
-        if (resId != null) {
-            return context.getDrawable(resId);
-        }
-
-        if (mimeType == null) {
-            // TODO: generic icon?
-            return null;
-        }
-
-        // Otherwise look for partial match
-        final String typeOnly = mimeType.split("/")[0];
-        if ("audio".equals(typeOnly)) {
-            return context.getDrawable(R.drawable.ic_doc_audio);
-        } else if ("image".equals(typeOnly)) {
-            return context.getDrawable(R.drawable.ic_doc_image);
-        } else if ("text".equals(typeOnly)) {
-            return context.getDrawable(R.drawable.ic_doc_text);
-        } else if ("video".equals(typeOnly)) {
-            return context.getDrawable(R.drawable.ic_doc_video);
-        } else {
-            return context.getDrawable(R.drawable.ic_doc_generic);
-        }
+        return context.getContentResolver().getTypeDrawable(mimeType);
     }
 
     public static Drawable applyTintColor(Context context, int drawableId, int tintColorId) {
diff --git a/packages/DocumentsUI/src/com/android/documentsui/QuickViewIntentBuilder.java b/packages/DocumentsUI/src/com/android/documentsui/QuickViewIntentBuilder.java
index af6aee7..5e3bbbb 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/QuickViewIntentBuilder.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/QuickViewIntentBuilder.java
@@ -28,6 +28,7 @@
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.Build;
 import android.provider.DocumentsContract;
 import android.provider.DocumentsContract.Document;
 import android.support.annotation.Nullable;
@@ -73,7 +74,7 @@
     @Nullable Intent build() {
         if (DEBUG) Log.d(TAG, "Preparing intent for doc:" + mDocument.documentId);
 
-        String trustedPkg = mResources.getString(R.string.trusted_quick_viewer_package);
+        String trustedPkg = getQuickViewPackage();
 
         if (!TextUtils.isEmpty(trustedPkg)) {
             Intent intent = new Intent(Intent.ACTION_QUICK_VIEW);
@@ -116,6 +117,16 @@
         return null;
     }
 
+    private String getQuickViewPackage() {
+        String resValue = mResources.getString(R.string.trusted_quick_viewer_package);
+        if (Build.IS_DEBUGGABLE ) {
+            // Allow users of debug devices to override default quick viewer
+            // for the purposes of testing.
+            return android.os.SystemProperties.get("debug.quick_viewer", resValue);
+        }
+        return resValue;
+    }
+
     private int collectViewableUris(ArrayList<Uri> uris) {
         final String[] siblingIds = mModel.getModelIds();
         uris.ensureCapacity(siblingIds.length);
diff --git a/packages/DocumentsUI/src/com/android/documentsui/SaveFragment.java b/packages/DocumentsUI/src/com/android/documentsui/SaveFragment.java
index d830c61..a37590d 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/SaveFragment.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/SaveFragment.java
@@ -22,6 +22,7 @@
 import android.content.Context;
 import android.os.Bundle;
 import android.text.Editable;
+import android.text.TextUtils;
 import android.text.TextWatcher;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
@@ -91,6 +92,14 @@
                             return false;
                         }
 
+                        // Returning false in this method will bubble the event up to
+                        // {@link BaseActivity#onKeyDown}. In order to prevent backspace popping
+                        // documents once the textView is empty, we are going to trap it here.
+                        if (keyCode == KeyEvent.KEYCODE_DEL
+                                && TextUtils.isEmpty(mDisplayName.getText())) {
+                            return true;
+                        }
+
                         if (keyCode == KeyEvent.KEYCODE_ENTER && mSave.isEnabled()) {
                             performSave();
                             return true;
diff --git a/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java b/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java
index 870c343..8a6723f 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/dirlist/DirectoryFragment.java
@@ -53,10 +53,13 @@
 import android.support.annotation.Nullable;
 import android.support.design.widget.Snackbar;
 import android.support.v13.view.DragStartHelper;
+import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
+import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat.AccessibilityActionCompat;
 import android.support.v7.widget.GridLayoutManager;
 import android.support.v7.widget.GridLayoutManager.SpanSizeLookup;
 import android.support.v7.widget.RecyclerView;
 import android.support.v7.widget.RecyclerView.OnItemTouchListener;
+import android.support.v7.widget.RecyclerView.Recycler;
 import android.support.v7.widget.RecyclerView.RecyclerListener;
 import android.support.v7.widget.RecyclerView.ViewHolder;
 import android.text.BidiFormatter;
@@ -243,7 +246,40 @@
 
         mRecView.setAdapter(mAdapter);
 
-        mLayout = new GridLayoutManager(getContext(), mColumnCount);
+        // Switch Access Accessibility API needs an {@link AccessibilityDelegate} to know the proper
+        // route when user selects an UI element. It usually guesses this if the element has an
+        // {@link OnClickListener}, but since we do not have one for itemView, we will need to
+        // manually route it to the right behavior. RecyclerView has its own AccessibilityDelegate,
+        // and routes it to its LayoutManager; so we must override the LayoutManager's accessibility
+        // methods to route clicks correctly.
+        mLayout = new GridLayoutManager(getContext(), mColumnCount) {
+            @Override
+            public void onInitializeAccessibilityNodeInfoForItem(
+                    RecyclerView.Recycler recycler, RecyclerView.State state,
+                    View host, AccessibilityNodeInfoCompat info) {
+                super.onInitializeAccessibilityNodeInfoForItem(recycler, state, host, info);
+                info.addAction(AccessibilityActionCompat.ACTION_CLICK);
+            }
+
+            @Override
+            public boolean performAccessibilityActionForItem(
+                    RecyclerView.Recycler recycler, RecyclerView.State state, View view,
+                    int action, Bundle args) {
+                // We are only handling click events; route all other to default implementation
+                if (action == AccessibilityNodeInfoCompat.ACTION_CLICK) {
+                    RecyclerView.ViewHolder vh = mRecView.getChildViewHolder(view);
+                    if (vh instanceof DocumentHolder) {
+                        DocumentHolder dh = (DocumentHolder) vh;
+                        if (dh.mEventListener != null) {
+                            dh.mEventListener.onActivate(dh);
+                            return true;
+                        }
+                    }
+                }
+                return super.performAccessibilityActionForItem(recycler, state, view, action,
+                        args);
+            }
+        };
         SpanSizeLookup lookup = mAdapter.createSpanSizeLookup();
         if (lookup != null) {
             mLayout.setSpanSizeLookup(lookup);
@@ -664,10 +700,10 @@
                     return true;
 
                 case R.id.menu_copy_to:
-                    transferDocuments(selection, FileOperationService.OPERATION_COPY);
                     // TODO: Only finish selection mode if copy-to is not canceled.
                     // Need to plum down into handling the way we do with deleteDocuments.
                     mode.finish();
+                    transferDocuments(selection, FileOperationService.OPERATION_COPY);
                     return true;
 
                 case R.id.menu_move_to:
@@ -717,63 +753,57 @@
     private void openDocuments(final Selection selected) {
         Metrics.logUserAction(getContext(), Metrics.USER_ACTION_OPEN);
 
-        new GetDocumentsTask() {
-            @Override
-            void onDocumentsReady(List<DocumentInfo> docs) {
-                // TODO: Implement support in Files activity for opening multiple docs.
-                BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
-            }
-        }.execute(selected);
+        // Model must be accessed in UI thread, since underlying cursor is not threadsafe.
+        List<DocumentInfo> docs = mModel.getDocuments(selected);
+        // TODO: Implement support in Files activity for opening multiple docs.
+        BaseActivity.get(DirectoryFragment.this).onDocumentsPicked(docs);
     }
 
     private void shareDocuments(final Selection selected) {
         Metrics.logUserAction(getContext(), Metrics.USER_ACTION_SHARE);
 
-        new GetDocumentsTask() {
-            @Override
-            void onDocumentsReady(List<DocumentInfo> docs) {
-                Intent intent;
+        // Model must be accessed in UI thread, since underlying cursor is not threadsafe.
+        List<DocumentInfo> docs = mModel.getDocuments(selected);
+        Intent intent;
 
-                // Filter out directories and virtual files - those can't be shared.
-                List<DocumentInfo> docsForSend = new ArrayList<>();
-                for (DocumentInfo doc: docs) {
-                    if (!doc.isDirectory() && !doc.isVirtualDocument()) {
-                        docsForSend.add(doc);
-                    }
-                }
-
-                if (docsForSend.size() == 1) {
-                    final DocumentInfo doc = docsForSend.get(0);
-
-                    intent = new Intent(Intent.ACTION_SEND);
-                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
-                    intent.addCategory(Intent.CATEGORY_DEFAULT);
-                    intent.setType(doc.mimeType);
-                    intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
-
-                } else if (docsForSend.size() > 1) {
-                    intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
-                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
-                    intent.addCategory(Intent.CATEGORY_DEFAULT);
-
-                    final ArrayList<String> mimeTypes = new ArrayList<>();
-                    final ArrayList<Uri> uris = new ArrayList<>();
-                    for (DocumentInfo doc : docsForSend) {
-                        mimeTypes.add(doc.mimeType);
-                        uris.add(doc.derivedUri);
-                    }
-
-                    intent.setType(findCommonMimeType(mimeTypes));
-                    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
-
-                } else {
-                    return;
-                }
-
-                intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
-                startActivity(intent);
+        // Filter out directories and virtual files - those can't be shared.
+        List<DocumentInfo> docsForSend = new ArrayList<>();
+        for (DocumentInfo doc: docs) {
+            if (!doc.isDirectory() && !doc.isVirtualDocument()) {
+                docsForSend.add(doc);
             }
-        }.execute(selected);
+        }
+
+        if (docsForSend.size() == 1) {
+            final DocumentInfo doc = docsForSend.get(0);
+
+            intent = new Intent(Intent.ACTION_SEND);
+            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+            intent.addCategory(Intent.CATEGORY_DEFAULT);
+            intent.setType(doc.mimeType);
+            intent.putExtra(Intent.EXTRA_STREAM, doc.derivedUri);
+
+        } else if (docsForSend.size() > 1) {
+            intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
+            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
+            intent.addCategory(Intent.CATEGORY_DEFAULT);
+
+            final ArrayList<String> mimeTypes = new ArrayList<>();
+            final ArrayList<Uri> uris = new ArrayList<>();
+            for (DocumentInfo doc : docsForSend) {
+                mimeTypes.add(doc.mimeType);
+                uris.add(doc.derivedUri);
+            }
+
+            intent.setType(findCommonMimeType(mimeTypes));
+            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
+
+        } else {
+            return;
+        }
+
+        intent = Intent.createChooser(intent, getActivity().getText(R.string.share_via));
+        startActivity(intent);
     }
 
     private String generateDeleteMessage(final List<DocumentInfo> docs) {
@@ -819,52 +849,51 @@
         assert(!selected.isEmpty());
 
         final DocumentInfo srcParent = getDisplayState().stack.peek();
-        new GetDocumentsTask() {
-            @Override
-            void onDocumentsReady(final List<DocumentInfo> docs) {
 
-                TextView message =
-                        (TextView) mInflater.inflate(R.layout.dialog_delete_confirmation, null);
-                message.setText(generateDeleteMessage(docs));
+        // Model must be accessed in UI thread, since underlying cursor is not threadsafe.
+        List<DocumentInfo> docs = mModel.getDocuments(selected);
 
-                // This "insta-hides" files that are being deleted, because
-                // the delete operation may be not execute immediately (it
-                // may be queued up on the FileOperationService.)
-                // To hide the files locally, we call the hide method on the adapter
-                // ...which a live object...cannot be parceled.
-                // For that reason, for now, we implement this dialog NOT
-                // as a fragment (which can survive rotation and have its own state),
-                // but as a simple runtime dialog. So rotating a device with an
-                // active delete dialog...results in that dialog disappearing.
-                // We can do better, but don't have cycles for it now.
-                new AlertDialog.Builder(getActivity())
-                    .setView(message)
-                    .setPositiveButton(
-                         android.R.string.yes,
-                         new DialogInterface.OnClickListener() {
-                            public void onClick(DialogInterface dialog, int id) {
-                                // Finish selection mode first which clears selection so we
-                                // don't end up trying to deselect deleted documents.
-                                // This is done here, rather in the onActionItemClicked
-                                // so we can avoid de-selecting items in the case where
-                                // the user cancels the delete.
-                                if (mActionMode != null) {
-                                    mActionMode.finish();
-                                } else {
-                                    Log.w(TAG, "Action mode is null before deleting documents.");
-                                }
-                                // Hide the files in the UI...since the operation
-                                // might be queued up on FileOperationService.
-                                // We're walking a line here.
-                                mAdapter.hide(selected.getAll());
-                                FileOperations.delete(
-                                        getActivity(), docs, srcParent, getDisplayState().stack);
-                            }
-                        })
-                    .setNegativeButton(android.R.string.no, null)
-                    .show();
-            }
-        }.execute(selected);
+        TextView message =
+                (TextView) mInflater.inflate(R.layout.dialog_delete_confirmation, null);
+        message.setText(generateDeleteMessage(docs));
+
+        // This "insta-hides" files that are being deleted, because
+        // the delete operation may be not execute immediately (it
+        // may be queued up on the FileOperationService.)
+        // To hide the files locally, we call the hide method on the adapter
+        // ...which a live object...cannot be parceled.
+        // For that reason, for now, we implement this dialog NOT
+        // as a fragment (which can survive rotation and have its own state),
+        // but as a simple runtime dialog. So rotating a device with an
+        // active delete dialog...results in that dialog disappearing.
+        // We can do better, but don't have cycles for it now.
+        new AlertDialog.Builder(getActivity())
+            .setView(message)
+            .setPositiveButton(
+                 android.R.string.yes,
+                 new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int id) {
+                        // Finish selection mode first which clears selection so we
+                        // don't end up trying to deselect deleted documents.
+                        // This is done here, rather in the onActionItemClicked
+                        // so we can avoid de-selecting items in the case where
+                        // the user cancels the delete.
+                        if (mActionMode != null) {
+                            mActionMode.finish();
+                        } else {
+                            Log.w(TAG, "Action mode is null before deleting documents.");
+                        }
+                        // Hide the files in the UI...since the operation
+                        // might be queued up on FileOperationService.
+                        // We're walking a line here.
+                        mAdapter.hide(selected.getAll());
+                        FileOperations.delete(
+                                getActivity(), docs, srcParent, getDisplayState().stack);
+                    }
+                })
+            .setNegativeButton(android.R.string.no, null)
+            .show();
     }
 
     private void transferDocuments(final Selection selected, final @OpType int mode) {
@@ -898,25 +927,21 @@
                 ? R.string.menu_move : R.string.menu_copy;
         intent.putExtra(DocumentsContract.EXTRA_PROMPT, getResources().getString(drawerTitleId));
 
-        new GetDocumentsTask() {
-            @Override
-            void onDocumentsReady(List<DocumentInfo> docs) {
-                // TODO: Can this move to Fragment bundle state?
-                getDisplayState().selectedDocumentsForCopy = docs;
+        // Model must be accessed in UI thread, since underlying cursor is not threadsafe.
+        List<DocumentInfo> docs = mModel.getDocuments(selected);
+        // TODO: Can this move to Fragment bundle state?
+        getDisplayState().selectedDocumentsForCopy = docs;
 
-                // Determine if there is a directory in the set of documents
-                // to be copied? Why? Directory creation isn't supported by some roots
-                // (like Downloads). This informs DocumentsActivity (the "picker")
-                // to restrict available roots to just those with support.
-                intent.putExtra(Shared.EXTRA_DIRECTORY_COPY, hasDirectory(docs));
-                intent.putExtra(FileOperationService.EXTRA_OPERATION, mode);
+        // Determine if there is a directory in the set of documents
+        // to be copied? Why? Directory creation isn't supported by some roots
+        // (like Downloads). This informs DocumentsActivity (the "picker")
+        // to restrict available roots to just those with support.
+        intent.putExtra(Shared.EXTRA_DIRECTORY_COPY, hasDirectory(docs));
+        intent.putExtra(FileOperationService.EXTRA_OPERATION, mode);
 
-                // This just identifies the type of request...we'll check it
-                // when we reveive a response.
-                startActivityForResult(intent, REQUEST_COPY_DESTINATION);
-            }
-
-        }.execute(selected);
+        // This just identifies the type of request...we'll check it
+        // when we reveive a response.
+        startActivityForResult(intent, REQUEST_COPY_DESTINATION);
     }
 
     private static boolean hasDirectory(List<DocumentInfo> docs) {
@@ -935,12 +960,9 @@
         // Rename option is only available in menu when 1 document selected
         assert(selected.size() == 1);
 
-        new GetDocumentsTask() {
-            @Override
-            void onDocumentsReady(List<DocumentInfo> docs) {
-                RenameDocumentFragment.show(getFragmentManager(), docs.get(0));
-            }
-        }.execute(selected);
+        // Model must be accessed in UI thread, since underlying cursor is not threadsafe.
+        List<DocumentInfo> docs = mModel.getDocuments(selected);
+        RenameDocumentFragment.show(getFragmentManager(), docs.get(0));
     }
 
     @Override
@@ -1099,19 +1121,17 @@
         }
     }
 
-    void copySelectionToClipboard(Selection selection) {
-        assert(!selection.isEmpty());
-        new GetDocumentsTask() {
-            @Override
-            void onDocumentsReady(List<DocumentInfo> docs) {
-                mClipper.clipDocuments(docs);
-                Activity activity = getActivity();
-                Snackbars.makeSnackbar(activity,
-                        activity.getResources().getQuantityString(
-                                R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
-                        Snackbar.LENGTH_SHORT).show();
-            }
-        }.execute(selection);
+    void copySelectionToClipboard(Selection selected) {
+        assert(!selected.isEmpty());
+
+        // Model must be accessed in UI thread, since underlying cursor is not threadsafe.
+        List<DocumentInfo> docs = mModel.getDocuments(selected);
+        mClipper.clipDocuments(docs);
+        Activity activity = getActivity();
+        Snackbars.makeSnackbar(activity,
+                activity.getResources().getQuantityString(
+                        R.plurals.clipboard_files_clipped, docs.size(), docs.size()),
+                Snackbar.LENGTH_SHORT).show();
     }
 
     public void pasteFromClipboard() {
@@ -1391,7 +1411,7 @@
                 return mIconHelper.getDocumentIcon(mContext, doc.authority, doc.documentId,
                         doc.mimeType, doc.icon);
             }
-            return mContext.getDrawable(R.drawable.ic_doc_generic);
+            return mContext.getDrawable(com.android.internal.R.drawable.ic_doc_generic);
         }
 
         private String getTitle(List<DocumentInfo> docs) {
@@ -1420,25 +1440,6 @@
             mShadowView.draw(canvas);
         }
     }
-    /**
-     * Abstract task providing support for loading documents *off*
-     * the main thread. And if it isn't obvious, creating a list
-     * of documents (especially large lists) can be pretty expensive.
-     */
-    private abstract class GetDocumentsTask
-            extends AsyncTask<Selection, Void, List<DocumentInfo>> {
-        @Override
-        protected final List<DocumentInfo> doInBackground(Selection... selected) {
-            return mModel.getDocuments(selected[0]);
-        }
-
-        @Override
-        protected final void onPostExecute(List<DocumentInfo> docs) {
-            onDocumentsReady(docs);
-        }
-
-        abstract void onDocumentsReady(List<DocumentInfo> docs);
-    }
 
     @Override
     public boolean isSelected(String modelId) {
diff --git a/packages/DocumentsUI/src/com/android/documentsui/model/DocumentInfo.java b/packages/DocumentsUI/src/com/android/documentsui/model/DocumentInfo.java
index 3a86a51..63f66de 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/model/DocumentInfo.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/model/DocumentInfo.java
@@ -235,6 +235,10 @@
         return (flags & Document.FLAG_DIR_PREFERS_GRID) != 0;
     }
 
+    public boolean isWriteSupported() {
+        return (flags & Document.FLAG_SUPPORTS_WRITE) != 0;
+    }
+
     public boolean isDeleteSupported() {
         return (flags & Document.FLAG_SUPPORTS_DELETE) != 0;
     }
diff --git a/packages/DocumentsUI/src/com/android/documentsui/model/RootInfo.java b/packages/DocumentsUI/src/com/android/documentsui/model/RootInfo.java
index d54bdfd..649dde0 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/model/RootInfo.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/model/RootInfo.java
@@ -213,13 +213,13 @@
             derivedIcon = R.drawable.ic_root_download;
         } else if (isImages()) {
             derivedType = TYPE_IMAGES;
-            derivedIcon = R.drawable.ic_doc_image;
+            derivedIcon = com.android.internal.R.drawable.ic_doc_image;
         } else if (isVideos()) {
             derivedType = TYPE_VIDEO;
-            derivedIcon = R.drawable.ic_doc_video;
+            derivedIcon = com.android.internal.R.drawable.ic_doc_video;
         } else if (isAudio()) {
             derivedType = TYPE_AUDIO;
-            derivedIcon = R.drawable.ic_doc_audio;
+            derivedIcon = com.android.internal.R.drawable.ic_doc_audio;
         } else if (isRecents()) {
             derivedType = TYPE_RECENTS;
         } else {
diff --git a/packages/DocumentsUI/src/com/android/documentsui/services/MoveJob.java b/packages/DocumentsUI/src/com/android/documentsui/services/MoveJob.java
index 1118171..1de3bbc 100644
--- a/packages/DocumentsUI/src/com/android/documentsui/services/MoveJob.java
+++ b/packages/DocumentsUI/src/com/android/documentsui/services/MoveJob.java
@@ -117,7 +117,9 @@
         byteCopyDocument(src, dest);
 
         // Remove the source document.
-        deleteDocument(src, srcParent);
+        if(!isCanceled()) {
+            deleteDocument(src, srcParent);
+        }
     }
 
     @Override
diff --git a/packages/EasterEgg/AndroidManifest.xml b/packages/EasterEgg/AndroidManifest.xml
index 50e8b5c..fbc2386 100644
--- a/packages/EasterEgg/AndroidManifest.xml
+++ b/packages/EasterEgg/AndroidManifest.xml
@@ -31,6 +31,7 @@
                   android:label="@string/app_name">
             <intent-filter>
                 <action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
+                <action android:name="android.intent.action.MAIN" />
             </intent-filter>
         </activity>
 
diff --git a/packages/EasterEgg/res/drawable/collar.xml b/packages/EasterEgg/res/drawable/collar.xml
index 6c0d90a..5e4d0fd 100644
--- a/packages/EasterEgg/res/drawable/collar.xml
+++ b/packages/EasterEgg/res/drawable/collar.xml
@@ -18,5 +18,5 @@
         android:height="48dp"
         android:viewportWidth="48.0"
         android:viewportHeight="48.0">
-    <path android:name="collar" android:fillColor="#FF000000" android:pathData="M9,18.4h30v1.6h-30z"/>
+    <path android:name="collar" android:fillColor="#FF000000" android:pathData="M9,18.4h30v1.7h-30z"/>
 </vector>
diff --git a/packages/EasterEgg/res/drawable/leg1.xml b/packages/EasterEgg/res/drawable/leg1.xml
index 6257333..d72c746 100644
--- a/packages/EasterEgg/res/drawable/leg1.xml
+++ b/packages/EasterEgg/res/drawable/leg1.xml
@@ -18,5 +18,5 @@
         android:height="48dp"
         android:viewportWidth="48.0"
         android:viewportHeight="48.0">
-    <path android:name="leg1" android:fillColor="#FF000000" android:pathData="M9,38h5v5h-5z"/>
+    <path android:name="leg1" android:fillColor="#FF000000" android:pathData="M9,37h5v6h-5z"/>
 </vector>
diff --git a/packages/EasterEgg/res/drawable/leg2.xml b/packages/EasterEgg/res/drawable/leg2.xml
index 73352f6..a772a87 100644
--- a/packages/EasterEgg/res/drawable/leg2.xml
+++ b/packages/EasterEgg/res/drawable/leg2.xml
@@ -18,5 +18,5 @@
         android:height="48dp"
         android:viewportWidth="48.0"
         android:viewportHeight="48.0">
-    <path android:name="leg2" android:fillColor="#FF000000" android:pathData="M16,38h5v5h-5z"/>
+    <path android:name="leg2" android:fillColor="#FF000000" android:pathData="M16,37h5v6h-5z"/>
 </vector>
diff --git a/packages/EasterEgg/res/drawable/leg2_shadow.xml b/packages/EasterEgg/res/drawable/leg2_shadow.xml
index 77f4893..b01bd69 100644
--- a/packages/EasterEgg/res/drawable/leg2_shadow.xml
+++ b/packages/EasterEgg/res/drawable/leg2_shadow.xml
@@ -18,5 +18,5 @@
         android:height="48dp"
         android:viewportWidth="48.0"
         android:viewportHeight="48.0">
-    <path android:name="leg2_shadow" android:fillColor="#FF000000" android:pathData="M16,38h5v1h-5z"/>
+    <path android:name="leg2_shadow" android:fillColor="#FF000000" android:pathData="M16,37h5v3h-5z"/>
 </vector>
diff --git a/packages/EasterEgg/res/drawable/leg3.xml b/packages/EasterEgg/res/drawable/leg3.xml
index 53dea5c..d471236 100644
--- a/packages/EasterEgg/res/drawable/leg3.xml
+++ b/packages/EasterEgg/res/drawable/leg3.xml
@@ -18,5 +18,5 @@
         android:height="48dp"
         android:viewportWidth="48.0"
         android:viewportHeight="48.0">
-    <path android:name="leg3" android:fillColor="#FF000000" android:pathData="M27,38h5v5h-5z"/>
+    <path android:name="leg3" android:fillColor="#FF000000" android:pathData="M27,37h5v6h-5z"/>
 </vector>
diff --git a/packages/EasterEgg/res/drawable/leg4.xml b/packages/EasterEgg/res/drawable/leg4.xml
index f2ce73e..e5868eb 100644
--- a/packages/EasterEgg/res/drawable/leg4.xml
+++ b/packages/EasterEgg/res/drawable/leg4.xml
@@ -18,5 +18,5 @@
         android:height="48dp"
         android:viewportWidth="48.0"
         android:viewportHeight="48.0">
-    <path android:name="leg4" android:fillColor="#FF000000" android:pathData="M34,38h5v5h-5z"/>
+    <path android:name="leg4" android:fillColor="#FF000000" android:pathData="M34,37h5v6h-5z"/>
 </vector>
diff --git a/packages/EasterEgg/res/layout/cat_view.xml b/packages/EasterEgg/res/layout/cat_view.xml
index 82ced2f2..85b494d 100644
--- a/packages/EasterEgg/res/layout/cat_view.xml
+++ b/packages/EasterEgg/res/layout/cat_view.xml
@@ -20,12 +20,14 @@
     android:minHeight="?android:attr/listPreferredItemHeightSmall"
     android:paddingStart="?android:attr/listPreferredItemPaddingStart"
     android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
+    android:paddingTop="8dp"
+    android:paddingBottom="8dp"
     android:background="?android:attr/selectableItemBackgroundBorderless"
     android:gravity="center_horizontal"
     android:clipToPadding="false">
 
     <FrameLayout
-        android:layout_width="wrap_content"
+        android:layout_width="96dp"
         android:layout_height="wrap_content">
 
         <ImageView
@@ -33,6 +35,7 @@
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:padding="10dp"
+            android:layout_gravity="center"
             android:scaleType="fitCenter" />
 
         <LinearLayout
diff --git a/packages/EasterEgg/res/values/dimens.xml b/packages/EasterEgg/res/values/dimens.xml
new file mode 100644
index 0000000..e9dcebd
--- /dev/null
+++ b/packages/EasterEgg/res/values/dimens.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+Copyright (C) 2016 The Android Open Source Project
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<resources xmlns:android="http://schemas.android.com/apk/res/android">
+    <dimen name="neko_display_size">64dp</dimen>
+</resources>
diff --git a/packages/EasterEgg/res/values/strings.xml b/packages/EasterEgg/res/values/strings.xml
index a2440c7b..8478a43 100644
--- a/packages/EasterEgg/res/values/strings.xml
+++ b/packages/EasterEgg/res/values/strings.xml
@@ -21,6 +21,7 @@
     <string name="notification_title" translatable="false">A cat is here.</string>
     <string name="default_cat_name" translatable="false">Cat #%s</string>
     <string name="directory_name" translatable="false">Cats</string>
+    <string name="confirm_delete" translatable="false">Forget %s?</string>
     <string-array name="food_names" translatable="false">
         <item>Empty dish</item>
         <item>Bits</item>
diff --git a/packages/EasterEgg/src/com/android/egg/neko/Cat.java b/packages/EasterEgg/src/com/android/egg/neko/Cat.java
index 864b20c..a4df372 100644
--- a/packages/EasterEgg/src/com/android/egg/neko/Cat.java
+++ b/packages/EasterEgg/src/com/android/egg/neko/Cat.java
@@ -24,10 +24,12 @@
 import android.graphics.drawable.Icon;
 import android.os.Bundle;
 
+import java.io.ByteArrayOutputStream;
 import java.util.Random;
 import java.util.concurrent.ThreadLocalRandom;
 
 import com.android.egg.R;
+import com.android.internal.logging.MetricsLogger;
 
 public class Cat extends Drawable {
     public static final long[] PURR = {0, 40, 20, 40, 20, 40, 20, 40, 20, 40, 20, 40};
@@ -37,6 +39,8 @@
     private long mSeed;
     private String mName;
     private int mBodyColor;
+    private int mFootType;
+    private boolean mBowTie;
 
     private synchronized Random notSoRandom(long seed) {
         if (mNotSoRandom == null) {
@@ -66,6 +70,15 @@
         return a[i+1];
     }
 
+    public static final int getColorIndex(int q, int[] a) {
+        for(int i = 1; i < a.length; i+=2) {
+            if (a[i] == q) {
+                return i/2;
+            }
+        }
+        return -1;
+    }
+
     public static final int[] P_BODY_COLORS = {
             180, 0xFF212121, // black
             180, 0xFFFFFFFF, // white
@@ -130,7 +143,7 @@
         mSeed = seed;
 
         setName(context.getString(R.string.default_cat_name,
-                String.valueOf(mSeed).substring(0, 3)));
+                String.valueOf(mSeed % 1000)));
 
         final Random nsr = notSoRandom(seed);
 
@@ -155,14 +168,19 @@
             tint(0xFF000000, D.mouth, D.nose);
         }
 
+        mFootType = 0;
         if (nsr.nextFloat() < 0.25f) {
+            mFootType = 4;
             tint(0xFFFFFFFF, D.foot1, D.foot2, D.foot3, D.foot4);
         } else {
             if (nsr.nextFloat() < 0.25f) {
-                tint(0xFFFFFFFF, D.foot1, D.foot2);
+                mFootType = 2;
+                tint(0xFFFFFFFF, D.foot1, D.foot3);
             } else if (nsr.nextFloat() < 0.25f) {
-                tint(0xFFFFFFFF, D.foot3, D.foot4);
+                mFootType = 3; // maybe -2 would be better? meh.
+                tint(0xFFFFFFFF, D.foot2, D.foot4);
             } else if (nsr.nextFloat() < 0.1f) {
+                mFootType = 1;
                 tint(0xFFFFFFFF, (Drawable) choose(nsr, D.foot1, D.foot2, D.foot3, D.foot4));
             }
         }
@@ -175,7 +193,8 @@
 
         final int collarColor = chooseP(nsr, P_COLLAR_COLORS);
         tint(collarColor, D.collar);
-        tint((nsr.nextFloat() < 0.1f) ? collarColor : 0, D.bowtie);
+        mBowTie = nsr.nextFloat() < 0.1f;
+        tint(mBowTie ? collarColor : 0, D.bowtie);
     }
 
     public static Cat create(Context context) {
@@ -190,7 +209,7 @@
                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         return new Notification.Builder(context)
                 .setSmallIcon(Icon.createWithResource(context, R.drawable.stat_icon))
-                .setLargeIcon(createLargeIcon(context))
+                .setLargeIcon(createNotificationLargeIcon(context))
                 .setColor(getBodyColor())
                 .setPriority(Notification.PRIORITY_LOW)
                 .setContentTitle(context.getString(R.string.notification_title))
@@ -240,11 +259,24 @@
         return result;
     }
 
-    public Icon createLargeIcon(Context context) {
-        final Resources res = context.getResources();
-        final int w = res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
-        final int h = res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
+    public static Icon recompressIcon(Icon bitmapIcon) {
+        if (bitmapIcon.getType() != Icon.TYPE_BITMAP) return bitmapIcon;
+        final Bitmap bits = bitmapIcon.getBitmap();
+        final ByteArrayOutputStream ostream = new ByteArrayOutputStream(
+                bits.getWidth() * bits.getHeight() * 2); // guess 50% compression
+        final boolean ok = bits.compress(Bitmap.CompressFormat.PNG, 100, ostream);
+        if (!ok) return null;
+        return Icon.createWithData(ostream.toByteArray(), 0, ostream.size());
+    }
 
+    public Icon createNotificationLargeIcon(Context context) {
+        final Resources res = context.getResources();
+        final int w = 2*res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
+        final int h = 2*res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
+        return recompressIcon(createIcon(context, w, h));
+    }
+
+    public Icon createIcon(Context context, int w, int h) {
         Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
         final Canvas canvas = new Canvas(result);
         final Paint pt = new Paint();
@@ -290,6 +322,30 @@
         return mBodyColor;
     }
 
+    public void logAdd(Context context) {
+        logCatAction(context, "egg_neko_add");
+    }
+
+    public void logRename(Context context) {
+        logCatAction(context, "egg_neko_rename");
+    }
+
+    public void logRemove(Context context) {
+        logCatAction(context, "egg_neko_remove");
+    }
+
+    public void logShare(Context context) {
+        logCatAction(context, "egg_neko_share");
+    }
+
+    private void logCatAction(Context context, String prefix) {
+        MetricsLogger.count(context, prefix, 1);
+        MetricsLogger.histogram(context, prefix +"_color",
+                getColorIndex(mBodyColor, P_BODY_COLORS));
+        MetricsLogger.histogram(context, prefix + "_bowtie", mBowTie ? 1 : 0);
+        MetricsLogger.histogram(context, prefix + "_feet", mFootType);
+    }
+
     public static class CatParts {
         public Drawable leftEar;
         public Drawable rightEar;
diff --git a/packages/EasterEgg/src/com/android/egg/neko/NekoActivationActivity.java b/packages/EasterEgg/src/com/android/egg/neko/NekoActivationActivity.java
index 88a7968..c0b725c 100644
--- a/packages/EasterEgg/src/com/android/egg/neko/NekoActivationActivity.java
+++ b/packages/EasterEgg/src/com/android/egg/neko/NekoActivationActivity.java
@@ -20,6 +20,8 @@
 import android.util.Log;
 import android.widget.Toast;
 
+import com.android.internal.logging.MetricsLogger;
+
 public class NekoActivationActivity extends Activity {
     private void toastUp(String s) {
         Toast toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
@@ -39,6 +41,7 @@
             }
             pm.setComponentEnabledSetting(cn, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                     PackageManager.DONT_KILL_APP);
+            MetricsLogger.histogram(this, "egg_neko_enable", 0);
             toastUp("\uD83D\uDEAB");
         } else {
             if (NekoLand.DEBUG) {
@@ -46,6 +49,7 @@
             }
             pm.setComponentEnabledSetting(cn, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                     PackageManager.DONT_KILL_APP);
+            MetricsLogger.histogram(this, "egg_neko_enable", 1);
             toastUp("\uD83D\uDC31");
         }
         finish();
diff --git a/packages/EasterEgg/src/com/android/egg/neko/NekoDialog.java b/packages/EasterEgg/src/com/android/egg/neko/NekoDialog.java
index a2ffd3e..2d2fbe8 100644
--- a/packages/EasterEgg/src/com/android/egg/neko/NekoDialog.java
+++ b/packages/EasterEgg/src/com/android/egg/neko/NekoDialog.java
@@ -26,6 +26,7 @@
 import android.widget.TextView;
 
 import com.android.egg.R;
+import com.android.internal.logging.MetricsLogger;
 
 import java.util.ArrayList;
 
@@ -51,6 +52,7 @@
         if (currentState == 0 && food.getType() != 0) {
             NekoService.registerJob(getContext(), food.getInterval(getContext()));
         }
+        MetricsLogger.histogram(getContext(), "egg_neko_offered_food", food.getType());
         prefs.setFoodState(food.getType());
         dismiss();
     }
diff --git a/packages/EasterEgg/src/com/android/egg/neko/NekoLand.java b/packages/EasterEgg/src/com/android/egg/neko/NekoLand.java
index e6a4177..f59f0d9 100644
--- a/packages/EasterEgg/src/com/android/egg/neko/NekoLand.java
+++ b/packages/EasterEgg/src/com/android/egg/neko/NekoLand.java
@@ -24,6 +24,7 @@
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.graphics.Bitmap;
+import android.graphics.Color;
 import android.graphics.drawable.Drawable;
 import android.media.MediaScannerConnection;
 import android.net.Uri;
@@ -44,16 +45,22 @@
 
 import com.android.egg.R;
 import com.android.egg.neko.PrefState.PrefsListener;
+import com.android.internal.logging.MetricsLogger;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
 
 public class NekoLand extends Activity implements PrefsListener {
     public static boolean DEBUG = false;
     public static boolean DEBUG_NOTIFICATIONS = false;
 
+    private static final int EXPORT_BITMAP_SIZE = 600;
+
     private static final int STORAGE_PERM_REQUEST = 123;
 
     private static boolean CAT_GEN = false;
@@ -79,7 +86,8 @@
         mAdapter = new CatAdapter();
         recyclerView.setAdapter(mAdapter);
         recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
-        updateCats();
+        int numCats = updateCats();
+        MetricsLogger.histogram(this, "egg_neko_visit_gallery", numCats);
     }
 
     @Override
@@ -88,7 +96,7 @@
         mPrefs.setListener(null);
     }
 
-    private void updateCats() {
+    private int updateCats() {
         Cat[] cats;
         if (CAT_GEN) {
             cats = new Cat[50];
@@ -96,9 +104,22 @@
                 cats[i] = Cat.create(this);
             }
         } else {
-            cats = mPrefs.getCats().toArray(new Cat[0]);
+            final float[] hsv = new float[3];
+            List<Cat> list = mPrefs.getCats();
+            Collections.sort(list, new Comparator<Cat>() {
+                @Override
+                public int compare(Cat cat, Cat cat2) {
+                    Color.colorToHSV(cat.getBodyColor(), hsv);
+                    float bodyH1 = hsv[0];
+                    Color.colorToHSV(cat2.getBodyColor(), hsv);
+                    float bodyH2 = hsv[0];
+                    return Float.compare(bodyH1, bodyH2);
+                }
+            });
+            cats = list.toArray(new Cat[0]);
         }
         mAdapter.setCats(cats);
+        return cats.length;
     }
 
     private void onCatClick(Cat cat) {
@@ -115,18 +136,21 @@
     }
 
     private void onCatRemove(Cat cat) {
+        cat.logRemove(this);
         mPrefs.removeCat(cat);
     }
 
     private void showNameDialog(final Cat cat) {
-        Context context = new ContextThemeWrapper(this,
+        final Context context = new ContextThemeWrapper(this,
                 android.R.style.Theme_Material_Light_Dialog_NoActionBar);
         // TODO: Move to XML, add correct margins.
         View view = LayoutInflater.from(context).inflate(R.layout.edit_text, null);
         final EditText text = (EditText) view.findViewById(android.R.id.edit);
         text.setText(cat.getName());
         text.setSelection(cat.getName().length());
-        Drawable catIcon = cat.createLargeIcon(this).loadDrawable(this);
+        final int size = context.getResources()
+                .getDimensionPixelSize(android.R.dimen.app_icon_size);
+        Drawable catIcon = cat.createIcon(this, size, size).loadDrawable(this);
         new AlertDialog.Builder(context)
                 .setTitle(" ")
                 .setIcon(catIcon)
@@ -134,6 +158,7 @@
                 .setPositiveButton(android.R.string.ok, new OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
+                        cat.logRename(context);
                         cat.setName(text.getText().toString().trim());
                         mPrefs.addCat(cat);
                     }
@@ -160,10 +185,36 @@
                     .inflate(R.layout.cat_view, parent, false));
         }
 
+        private void setContextGroupVisible(final CatHolder holder, boolean vis) {
+            final View group = holder.contextGroup;
+            if (vis && group.getVisibility() != View.VISIBLE) {
+                group.setAlpha(0);
+                group.setVisibility(View.VISIBLE);
+                group.animate().alpha(1.0f).setDuration(333);
+                Runnable hideAction = new Runnable() {
+                    @Override
+                    public void run() {
+                        setContextGroupVisible(holder, false);
+                    }
+                };
+                group.setTag(hideAction);
+                group.postDelayed(hideAction, 5000);
+            } else if (!vis && group.getVisibility() == View.VISIBLE) {
+                group.removeCallbacks((Runnable) group.getTag());
+                group.animate().alpha(0f).setDuration(250).withEndAction(new Runnable() {
+                    @Override
+                    public void run() {
+                        group.setVisibility(View.INVISIBLE);
+                    }
+                });
+            }
+        }
+
         @Override
         public void onBindViewHolder(final CatHolder holder, int position) {
             Context context = holder.itemView.getContext();
-            holder.imageView.setImageIcon(mCats[position].createLargeIcon(context));
+            final int size = context.getResources().getDimensionPixelSize(R.dimen.neko_display_size);
+            holder.imageView.setImageIcon(mCats[position].createIcon(context, size, size));
             holder.textView.setText(mCats[position].getName());
             holder.itemView.setOnClickListener(new View.OnClickListener() {
                 @Override
@@ -174,30 +225,30 @@
             holder.itemView.setOnLongClickListener(new OnLongClickListener() {
                 @Override
                 public boolean onLongClick(View v) {
-                    holder.contextGroup.removeCallbacks((Runnable) holder.contextGroup.getTag());
-                    holder.contextGroup.setVisibility(View.VISIBLE);
-                    Runnable hideAction = new Runnable() {
-                        @Override
-                        public void run() {
-                            holder.contextGroup.setVisibility(View.INVISIBLE);
-                        }
-                    };
-                    holder.contextGroup.setTag(hideAction);
-                    holder.contextGroup.postDelayed(hideAction, 5000);
+                    setContextGroupVisible(holder, true);
                     return true;
                 }
             });
             holder.delete.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
-                    holder.contextGroup.setVisibility(View.INVISIBLE);
-                    holder.contextGroup.removeCallbacks((Runnable) holder.contextGroup.getTag());
-                    onCatRemove(mCats[holder.getAdapterPosition()]);
+                    setContextGroupVisible(holder, false);
+                    new AlertDialog.Builder(NekoLand.this)
+                        .setTitle(getString(R.string.confirm_delete, mCats[position].getName()))
+                        .setNegativeButton(android.R.string.cancel, null)
+                        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                            @Override
+                            public void onClick(DialogInterface dialog, int which) {
+                                onCatRemove(mCats[holder.getAdapterPosition()]);
+                            }
+                        })
+                        .show();
                 }
             });
             holder.share.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
+                    setContextGroupVisible(holder, false);
                     Cat cat = mCats[holder.getAdapterPosition()];
                     if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                             != PackageManager.PERMISSION_GRANTED) {
@@ -227,7 +278,7 @@
             return;
         }
         final File png = new File(dir, cat.getName().replaceAll("[/ #:]+", "_") + ".png");
-        Bitmap bitmap = cat.createBitmap(512, 512);
+        Bitmap bitmap = cat.createBitmap(EXPORT_BITMAP_SIZE, EXPORT_BITMAP_SIZE);
         if (bitmap != null) {
             try {
                 OutputStream os = new FileOutputStream(png);
@@ -244,6 +295,7 @@
                 intent.putExtra(Intent.EXTRA_SUBJECT, cat.getName());
                 intent.setType("image/png");
                 startActivity(Intent.createChooser(intent, null));
+                cat.logShare(this);
             } catch (IOException e) {
                 Log.e("NekoLand", "save: error: " + e);
             }
diff --git a/packages/EasterEgg/src/com/android/egg/neko/NekoService.java b/packages/EasterEgg/src/com/android/egg/neko/NekoService.java
index 1ee3851..808ec36 100644
--- a/packages/EasterEgg/src/com/android/egg/neko/NekoService.java
+++ b/packages/EasterEgg/src/com/android/egg/neko/NekoService.java
@@ -82,6 +82,7 @@
                 if (cats.size() == 0 || rng.nextFloat() <= new_cat_prob) {
                     cat = Cat.create(this);
                     prefs.addCat(cat);
+                    cat.logAdd(this);
                     Log.v(TAG, "A new cat is here: " + cat.getName());
                 } else {
                     cat = cats.get(rng.nextInt(cats.size()));
@@ -101,6 +102,14 @@
         return false;
     }
 
+    public static void registerJobIfNeeded(Context context, long intervalMinutes) {
+        JobScheduler jss = context.getSystemService(JobScheduler.class);
+        JobInfo info = jss.getPendingJob(JOB_ID);
+        if (info == null) {
+            registerJob(context, intervalMinutes);
+        }
+    }
+
     public static void registerJob(Context context, long intervalMinutes) {
         JobScheduler jss = context.getSystemService(JobScheduler.class);
         jss.cancel(JOB_ID);
diff --git a/packages/EasterEgg/src/com/android/egg/neko/NekoTile.java b/packages/EasterEgg/src/com/android/egg/neko/NekoTile.java
index d5e143c..159b40a 100644
--- a/packages/EasterEgg/src/com/android/egg/neko/NekoTile.java
+++ b/packages/EasterEgg/src/com/android/egg/neko/NekoTile.java
@@ -20,6 +20,7 @@
 import android.util.Log;
 
 import com.android.egg.neko.PrefState.PrefsListener;
+import com.android.internal.logging.MetricsLogger;
 
 public class NekoTile extends TileService implements PrefsListener {
 
@@ -47,6 +48,18 @@
     }
 
     @Override
+    public void onTileAdded() {
+        super.onTileAdded();
+        MetricsLogger.count(this, "egg_neko_tile_added", 1);
+    }
+
+    @Override
+    public void onTileRemoved() {
+        super.onTileRemoved();
+        MetricsLogger.count(this, "egg_neko_tile_removed", 1);
+    }
+
+    @Override
     public void onPrefsChanged() {
         updateState();
     }
@@ -55,6 +68,9 @@
         Tile tile = getQsTile();
         int foodState = mPrefs.getFoodState();
         Food food = new Food(foodState);
+        if (foodState != 0) {
+            NekoService.registerJobIfNeeded(this, food.getInterval(this));
+        }
         tile.setIcon(food.getIcon(this));
         tile.setLabel(food.getName(this));
         tile.setState(foodState != 0 ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
@@ -65,6 +81,7 @@
     public void onClick() {
         if (mPrefs.getFoodState() != 0) {
             // there's already food loaded, let's empty it
+            MetricsLogger.count(this, "egg_neko_empty_food", 1);
             mPrefs.setFoodState(0);
             NekoService.cancelJob(this);
         } else {
@@ -91,6 +108,7 @@
 
     private void showNekoDialog() {
         Log.d(TAG, "showNekoDialog");
+        MetricsLogger.count(this, "egg_neko_select_food", 1);
         showDialog(new NekoDialog(this));
     }
 }
diff --git a/packages/EasterEgg/src/com/android/egg/neko/PrefState.java b/packages/EasterEgg/src/com/android/egg/neko/PrefState.java
index 5f54180..bf71b19 100644
--- a/packages/EasterEgg/src/com/android/egg/neko/PrefState.java
+++ b/packages/EasterEgg/src/com/android/egg/neko/PrefState.java
@@ -43,13 +43,11 @@
     public void addCat(Cat cat) {
         mPrefs.edit()
               .putString(CAT_KEY_PREFIX + String.valueOf(cat.getSeed()), cat.getName())
-              .commit();
+              .apply();
     }
 
     public void removeCat(Cat cat) {
-        mPrefs.edit()
-                .remove(CAT_KEY_PREFIX + String.valueOf(cat.getSeed()))
-                .commit();
+        mPrefs.edit().remove(CAT_KEY_PREFIX + String.valueOf(cat.getSeed())).apply();
     }
 
     public List<Cat> getCats() {
@@ -71,7 +69,7 @@
     }
 
     public void setFoodState(int foodState) {
-        mPrefs.edit().putInt(FOOD_STATE, foodState).commit();
+        mPrefs.edit().putInt(FOOD_STATE, foodState).apply();
     }
 
     public void setListener(PrefsListener listener) {
diff --git a/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java b/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
index 62f33bf..3ef9b8e 100644
--- a/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
+++ b/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
@@ -466,10 +466,7 @@
         displayName = FileUtils.buildValidFatFilename(displayName);
 
         final File before = getFileForDocId(docId);
-        final File after = new File(before.getParentFile(), displayName);
-        if (after.exists()) {
-            throw new IllegalStateException("Already exists " + after);
-        }
+        final File after = FileUtils.buildUniqueFile(before.getParentFile(), displayName);
         if (!before.renameTo(after)) {
             throw new IllegalStateException("Failed to rename to " + after);
         }
@@ -484,6 +481,8 @@
     @Override
     public void deleteDocument(String docId) throws FileNotFoundException {
         final File file = getFileForDocId(docId);
+        final File visibleFile = getFileForDocId(docId, true);
+
         final boolean isDirectory = file.isDirectory();
         if (isDirectory) {
             FileUtils.deleteContents(file);
@@ -492,23 +491,25 @@
             throw new IllegalStateException("Failed to delete " + file);
         }
 
-        final ContentResolver resolver = getContext().getContentResolver();
-        final Uri externalUri = MediaStore.Files.getContentUri("external");
+        if (visibleFile != null) {
+            final ContentResolver resolver = getContext().getContentResolver();
+            final Uri externalUri = MediaStore.Files.getContentUri("external");
 
-        // Remove media store entries for any files inside this directory, using
-        // path prefix match. Logic borrowed from MtpDatabase.
-        if (isDirectory) {
-            final String path = file.getAbsolutePath() + "/";
+            // Remove media store entries for any files inside this directory, using
+            // path prefix match. Logic borrowed from MtpDatabase.
+            if (isDirectory) {
+                final String path = visibleFile.getAbsolutePath() + "/";
+                resolver.delete(externalUri,
+                        "_data LIKE ?1 AND lower(substr(_data,1,?2))=lower(?3)",
+                        new String[] { path + "%", Integer.toString(path.length()), path });
+            }
+
+            // Remove media store entry for this exact file.
+            final String path = visibleFile.getAbsolutePath();
             resolver.delete(externalUri,
-                    "_data LIKE ?1 AND lower(substr(_data,1,?2))=lower(?3)",
-                    new String[] { path + "%", Integer.toString(path.length()), path });
+                    "_data LIKE ?1 AND lower(_data)=lower(?2)",
+                    new String[] { path, path });
         }
-
-        // Remove media store entry for this exact file.
-        final String path = file.getAbsolutePath();
-        resolver.delete(externalUri,
-                "_data LIKE ?1 AND lower(_data)=lower(?2)",
-                new String[] { path, path });
     }
 
     @Override
@@ -562,6 +563,7 @@
             throws FileNotFoundException {
         final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection));
 
+        query = query.toLowerCase();
         final File parent;
         synchronized (mRootsLock) {
             parent = mRoots.get(rootId).path;
diff --git a/packages/Keyguard/res/layout/keyguard_password_view.xml b/packages/Keyguard/res/layout/keyguard_password_view.xml
index 7d45017..29c93d5 100644
--- a/packages/Keyguard/res/layout/keyguard_password_view.xml
+++ b/packages/Keyguard/res/layout/keyguard_password_view.xml
@@ -26,7 +26,6 @@
     androidprv:layout_maxWidth="@dimen/keyguard_security_width"
     androidprv:layout_maxHeight="@dimen/keyguard_security_height"
     android:gravity="bottom"
-    android:contentDescription="@string/keyguard_accessibility_password_unlock"
     >
 
     <Space
diff --git a/packages/Keyguard/res/layout/keyguard_pin_view.xml b/packages/Keyguard/res/layout/keyguard_pin_view.xml
index d3fb982..e75f3c15 100644
--- a/packages/Keyguard/res/layout/keyguard_pin_view.xml
+++ b/packages/Keyguard/res/layout/keyguard_pin_view.xml
@@ -26,7 +26,6 @@
         androidprv:layout_maxWidth="@dimen/keyguard_security_width"
         androidprv:layout_maxHeight="@dimen/keyguard_security_max_height"
         android:orientation="vertical"
-        android:contentDescription="@string/keyguard_accessibility_pin_unlock"
         >
     <include layout="@layout/keyguard_message_area"
              android:layout_width="match_parent"
diff --git a/packages/SystemUI/res/drawable/notification_header_bg.xml b/packages/Keyguard/res/values-my-rMM/dimens.xml
similarity index 70%
rename from packages/SystemUI/res/drawable/notification_header_bg.xml
rename to packages/Keyguard/res/values-my-rMM/dimens.xml
index 1f46502..21b2a46 100644
--- a/packages/SystemUI/res/drawable/notification_header_bg.xml
+++ b/packages/Keyguard/res/values-my-rMM/dimens.xml
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <!--
-  ~ Copyright (C) 2014 The Android Open Source Project
+  ~ Copyright (C) 2016 The Android Open Source Project
   ~
   ~ Licensed under the Apache License, Version 2.0 (the "License");
   ~ you may not use this file except in compliance with the License.
@@ -14,8 +14,6 @@
   ~ See the License for the specific language governing permissions and
   ~ limitations under the License
   -->
-
-<ripple xmlns:android="http://schemas.android.com/apk/res/android"
-    android:color="?android:attr/colorControlHighlight" >
-    <item android:drawable="@color/system_secondary_color"/>
-</ripple>
\ No newline at end of file
+<resources>
+    <dimen name="bottom_text_spacing_digital">4dp</dimen>
+</resources>
diff --git a/packages/Keyguard/res/values/strings.xml b/packages/Keyguard/res/values/strings.xml
index 09fec81..ff689aa 100644
--- a/packages/Keyguard/res/values/strings.xml
+++ b/packages/Keyguard/res/values/strings.xml
@@ -103,15 +103,6 @@
     <!-- Time format strings for fall-back clock widget -->
     <string name="keyguard_widget_24_hours_format" translatable="false">kk\uee01mm</string>
 
-    <string name="keyguard_accessibility_pattern_unlock">Pattern unlock.</string>
-    <!-- Accessibility description of the pin lock. [CHAR_LIMIT=none] -->
-    <string name="keyguard_accessibility_pin_unlock">Pin unlock.</string>
-    <!-- Accessibility description of the password lock. [CHAR_LIMIT=none] -->
-    <string name="keyguard_accessibility_password_unlock">Password unlock.</string>
-    <!-- Accessibility description of the unlock pattern area. [CHAR_LIMIT=none] -->
-    <string name="keyguard_accessibility_pattern_area" msgid="7679891324509597904">Pattern area.</string>
-    <!-- Accessibility description of the unlock slide area. [CHAR_LIMIT=none] -->
-    <string name="keyguard_accessibility_slide_area">Slide area.</string>
     <!-- Accessibility description of the PIN password view. [CHAR_LIMIT=none] -->
     <string name="keyguard_accessibility_pin_area">PIN area</string>
     <!-- Accessibility description of the SIM PIN password view. [CHAR_LIMIT=none] -->
diff --git a/packages/Keyguard/src/com/android/keyguard/EmergencyButton.java b/packages/Keyguard/src/com/android/keyguard/EmergencyButton.java
index 8d41145..0474df7 100644
--- a/packages/Keyguard/src/com/android/keyguard/EmergencyButton.java
+++ b/packages/Keyguard/src/com/android/keyguard/EmergencyButton.java
@@ -28,13 +28,16 @@
 import android.telecom.TelecomManager;
 import android.util.AttributeSet;
 import android.util.Slog;
+import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewConfiguration;
 import android.widget.Button;
 
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.internal.telephony.IccCardConstants.State;
 import com.android.internal.widget.LockPatternUtils;
+import com.android.internal.policy.EmergencyAffordanceManager;
 
 /**
  * This class implements a smart emergency button that updates itself based
@@ -51,7 +54,10 @@
                     | Intent.FLAG_ACTIVITY_CLEAR_TOP);
 
     private static final String LOG_TAG = "EmergencyButton";
+    private final EmergencyAffordanceManager mEmergencyAffordanceManager;
 
+    private int mDownX;
+    private int mDownY;
     KeyguardUpdateMonitorCallback mInfoCallback = new KeyguardUpdateMonitorCallback() {
 
         @Override
@@ -64,6 +70,7 @@
             updateEmergencyCallButton();
         }
     };
+    private boolean mLongPressWasDragged;
 
     public interface EmergencyButtonCallback {
         public void onEmergencyButtonClickedWhenInCall();
@@ -86,6 +93,7 @@
                 com.android.internal.R.bool.config_voice_capable);
         mEnableEmergencyCallWhileSimLocked = mContext.getResources().getBoolean(
                 com.android.internal.R.bool.config_enable_emergency_call_while_sim_locked);
+        mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
     }
 
     @Override
@@ -110,10 +118,45 @@
                 takeEmergencyCallAction();
             }
         });
+        setOnLongClickListener(new OnLongClickListener() {
+            @Override
+            public boolean onLongClick(View v) {
+                if (!mLongPressWasDragged
+                        && mEmergencyAffordanceManager.needsEmergencyAffordance()) {
+                    mEmergencyAffordanceManager.performEmergencyCall();
+                    return true;
+                }
+                return false;
+            }
+        });
         updateEmergencyCallButton();
     }
 
     @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        final int x = (int) event.getX();
+        final int y = (int) event.getY();
+        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
+            mDownX = x;
+            mDownY = y;
+            mLongPressWasDragged = false;
+        } else {
+            final int xDiff = Math.abs(x - mDownX);
+            final int yDiff = Math.abs(y - mDownY);
+            int touchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
+            if (Math.abs(yDiff) > touchSlop || Math.abs(xDiff) > touchSlop) {
+                mLongPressWasDragged = true;
+            }
+        }
+        return super.onTouchEvent(event);
+    }
+
+    @Override
+    public boolean performLongClick() {
+        return super.performLongClick();
+    }
+
+    @Override
     protected void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
         updateEmergencyCallButton();
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardAbsKeyInputView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardAbsKeyInputView.java
index 60eaad2..038e08d 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardAbsKeyInputView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardAbsKeyInputView.java
@@ -137,12 +137,21 @@
                 entry,
                 userId,
                 new LockPatternChecker.OnCheckCallback() {
+
+                    @Override
+                    public void onEarlyMatched() {
+                        onPasswordChecked(userId, true /* matched */, 0 /* timeoutMs */,
+                                true /* isValidPassword */);
+                    }
+
                     @Override
                     public void onChecked(boolean matched, int timeoutMs) {
                         setPasswordEntryInputEnabled(true);
                         mPendingLockCheck = null;
-                        onPasswordChecked(userId, matched, timeoutMs,
-                                true /* isValidPassword */);
+                        if (!matched) {
+                            onPasswordChecked(userId, false /* matched */, timeoutMs,
+                                    true /* isValidPassword */);
+                        }
                     }
                 });
     }
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardPINView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardPINView.java
index 7ea767c..285b1ae 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardPINView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardPINView.java
@@ -16,12 +16,8 @@
 
 package com.android.keyguard;
 
-import android.animation.Animator;
-import android.animation.ObjectAnimator;
 import android.content.Context;
 import android.util.AttributeSet;
-import android.view.RenderNode;
-import android.view.RenderNodeAnimator;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.animation.AnimationUtils;
@@ -36,6 +32,7 @@
 
     private final AppearAnimationUtils mAppearAnimationUtils;
     private final DisappearAnimationUtils mDisappearAnimationUtils;
+    private final DisappearAnimationUtils mDisappearAnimationUtilsLocked;
     private ViewGroup mContainer;
     private ViewGroup mRow0;
     private ViewGroup mRow1;
@@ -44,6 +41,7 @@
     private View mDivider;
     private int mDisappearYTranslation;
     private View[][] mViews;
+    private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
 
     public KeyguardPINView(Context context) {
         this(context, null);
@@ -56,8 +54,14 @@
                 125, 0.6f /* translationScale */,
                 0.45f /* delayScale */, AnimationUtils.loadInterpolator(
                         mContext, android.R.interpolator.fast_out_linear_in));
+        mDisappearAnimationUtilsLocked = new DisappearAnimationUtils(context,
+                (long) (125 * KeyguardPatternView.DISAPPEAR_MULTIPLIER_LOCKED),
+                0.6f /* translationScale */,
+                0.45f /* delayScale */, AnimationUtils.loadInterpolator(
+                        mContext, android.R.interpolator.fast_out_linear_in));
         mDisappearYTranslation = getResources().getDimensionPixelSize(
                 R.dimen.disappear_y_translation);
+        mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(context);
     }
 
     @Override
@@ -136,7 +140,11 @@
         setTranslationY(0);
         AppearAnimationUtils.startTranslationYAnimation(this, 0 /* delay */, 280 /* duration */,
                 mDisappearYTranslation, mDisappearAnimationUtils.getInterpolator());
-        mDisappearAnimationUtils.startAnimation2d(mViews,
+        DisappearAnimationUtils disappearAnimationUtils = mKeyguardUpdateMonitor
+                .needsSlowUnlockTransition()
+                        ? mDisappearAnimationUtilsLocked
+                        : mDisappearAnimationUtils;
+        disappearAnimationUtils.startAnimation2d(mViews,
                 new Runnable() {
                     @Override
                     public void run() {
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardPasswordView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardPasswordView.java
index b75f529..ddccc14 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardPasswordView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardPasswordView.java
@@ -42,16 +42,21 @@
  * Displays an alphanumeric (latin-1) key entry for the user to enter
  * an unlock password
  */
-
 public class KeyguardPasswordView extends KeyguardAbsKeyInputView
         implements KeyguardSecurityView, OnEditorActionListener, TextWatcher {
 
     private final boolean mShowImeAtScreenOn;
     private final int mDisappearYTranslation;
 
+    // A delay constant to be used in a workaround for the situation where InputMethodManagerService
+    // is not switched to the new user yet.
+    // TODO: Remove this by ensuring such a race condition never happens.
+    private static final int DELAY_MILLIS_TO_REEVALUATE_IME_SWITCH_ICON = 500;  // 500ms
+
     InputMethodManager mImm;
     private TextView mPasswordEntry;
     private TextViewInputDisabler mPasswordEntryDisabler;
+    private View mSwitchImeButton;
 
     private Interpolator mLinearOutSlowInInterpolator;
     private Interpolator mFastOutLinearInInterpolator;
@@ -141,12 +146,31 @@
         mPasswordEntry.requestFocus();
     }
 
+    private void updateSwitchImeButton() {
+        // If there's more than one IME, enable the IME switcher button
+        final boolean wasVisible = mSwitchImeButton.getVisibility() == View.VISIBLE;
+        final boolean shouldBeVisible = hasMultipleEnabledIMEsOrSubtypes(mImm, false);
+        if (wasVisible != shouldBeVisible) {
+            mSwitchImeButton.setVisibility(shouldBeVisible ? View.VISIBLE : View.GONE);
+        }
+
+        // TODO: Check if we still need this hack.
+        // If no icon is visible, reset the start margin on the password field so the text is
+        // still centered.
+        if (mSwitchImeButton.getVisibility() != View.VISIBLE) {
+            android.view.ViewGroup.LayoutParams params = mPasswordEntry.getLayoutParams();
+            if (params instanceof MarginLayoutParams) {
+                final MarginLayoutParams mlp = (MarginLayoutParams) params;
+                mlp.setMarginStart(0);
+                mPasswordEntry.setLayoutParams(params);
+            }
+        }
+    }
+
     @Override
     protected void onFinishInflate() {
         super.onFinishInflate();
 
-        boolean imeOrDeleteButtonVisible = false;
-
         mImm = (InputMethodManager) getContext().getSystemService(
                 Context.INPUT_METHOD_SERVICE);
 
@@ -171,31 +195,29 @@
 
         mPasswordEntry.requestFocus();
 
-        // If there's more than one IME, enable the IME switcher button
-        View switchImeButton = findViewById(R.id.switch_ime_button);
-        if (switchImeButton != null && hasMultipleEnabledIMEsOrSubtypes(mImm, false)) {
-            switchImeButton.setVisibility(View.VISIBLE);
-            imeOrDeleteButtonVisible = true;
-            switchImeButton.setOnClickListener(new OnClickListener() {
-                @Override
-                public void onClick(View v) {
-                    mCallback.userActivity(); // Leave the screen on a bit longer
-                    // Do not show auxiliary subtypes in password lock screen.
-                    mImm.showInputMethodPicker(false /* showAuxiliarySubtypes */);
-                }
-            });
-        }
-
-        // If no icon is visible, reset the start margin on the password field so the text is
-        // still centered.
-        if (!imeOrDeleteButtonVisible) {
-            android.view.ViewGroup.LayoutParams params = mPasswordEntry.getLayoutParams();
-            if (params instanceof MarginLayoutParams) {
-                final MarginLayoutParams mlp = (MarginLayoutParams) params;
-                mlp.setMarginStart(0);
-                mPasswordEntry.setLayoutParams(params);
+        mSwitchImeButton = findViewById(R.id.switch_ime_button);
+        mSwitchImeButton.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                mCallback.userActivity(); // Leave the screen on a bit longer
+                // Do not show auxiliary subtypes in password lock screen.
+                mImm.showInputMethodPicker(false /* showAuxiliarySubtypes */);
             }
-        }
+        });
+
+        // If there's more than one IME, enable the IME switcher button
+        updateSwitchImeButton();
+
+        // When we the current user is switching, InputMethodManagerService sometimes has not
+        // switched internal state yet here. As a quick workaround, we check the keyboard state
+        // again.
+        // TODO: Remove this workaround by ensuring such a race condition never happens.
+        postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                updateSwitchImeButton();
+            }
+        }, DELAY_MILLIS_TO_REEVALUATE_IME_SWITCH_ICON);
     }
 
     @Override
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardPatternView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardPatternView.java
index e070492..506f77d 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardPatternView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardPatternView.java
@@ -55,9 +55,13 @@
     // how many cells the user has to cross before we poke the wakelock
     private static final int MIN_PATTERN_BEFORE_POKE_WAKELOCK = 2;
 
+    // How much we scale up the duration of the disappear animation when the current user is locked
+    public static final float DISAPPEAR_MULTIPLIER_LOCKED = 1.5f;
+
     private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
     private final AppearAnimationUtils mAppearAnimationUtils;
     private final DisappearAnimationUtils mDisappearAnimationUtils;
+    private final DisappearAnimationUtils mDisappearAnimationUtilsLocked;
 
     private CountDownTimer mCountdownTimer = null;
     private LockPatternUtils mLockPatternUtils;
@@ -109,6 +113,10 @@
                 125, 1.2f /* translationScale */,
                 0.6f /* delayScale */, AnimationUtils.loadInterpolator(
                         mContext, android.R.interpolator.fast_out_linear_in));
+        mDisappearAnimationUtilsLocked = new DisappearAnimationUtils(context,
+                (long) (125 * DISAPPEAR_MULTIPLIER_LOCKED), 1.2f /* translationScale */,
+                0.6f /* delayScale */, AnimationUtils.loadInterpolator(
+                mContext, android.R.interpolator.fast_out_linear_in));
         mDisappearYTranslation = getResources().getDimensionPixelSize(
                 R.dimen.disappear_y_translation);
     }
@@ -239,11 +247,21 @@
                     pattern,
                     userId,
                     new LockPatternChecker.OnCheckCallback() {
+
+                        @Override
+                        public void onEarlyMatched() {
+                            onPatternChecked(userId, true /* matched */, 0 /* timeoutMs */,
+                                    true /* isValidPattern */);
+                        }
+
                         @Override
                         public void onChecked(boolean matched, int timeoutMs) {
                             mLockPatternView.enableInput();
                             mPendingLockCheck = null;
-                            onPatternChecked(userId, matched, timeoutMs, true);
+                            if (!matched) {
+                                onPatternChecked(userId, false /* matched */, timeoutMs,
+                                        true /* isValidPattern */);
+                            }
                         }
                     });
             if (pattern.size() > MIN_PATTERN_BEFORE_POKE_WAKELOCK) {
@@ -390,25 +408,31 @@
 
     @Override
     public boolean startDisappearAnimation(final Runnable finishRunnable) {
+        float durationMultiplier = mKeyguardUpdateMonitor.needsSlowUnlockTransition()
+                ? DISAPPEAR_MULTIPLIER_LOCKED
+                : 1f;
         mLockPatternView.clearPattern();
         enableClipping(false);
         setTranslationY(0);
-        AppearAnimationUtils.startTranslationYAnimation(this, 0 /* delay */, 300 /* duration */,
+        AppearAnimationUtils.startTranslationYAnimation(this, 0 /* delay */,
+                (long) (300 * durationMultiplier),
                 -mDisappearAnimationUtils.getStartTranslation(),
                 mDisappearAnimationUtils.getInterpolator());
-        mDisappearAnimationUtils.startAnimation2d(mLockPatternView.getCellStates(),
-                new Runnable() {
-                    @Override
-                    public void run() {
-                        enableClipping(true);
-                        if (finishRunnable != null) {
-                            finishRunnable.run();
-                        }
+
+        DisappearAnimationUtils disappearAnimationUtils = mKeyguardUpdateMonitor
+                .needsSlowUnlockTransition()
+                        ? mDisappearAnimationUtilsLocked
+                        : mDisappearAnimationUtils;
+        disappearAnimationUtils.startAnimation2d(mLockPatternView.getCellStates(),
+                () -> {
+                    enableClipping(true);
+                    if (finishRunnable != null) {
+                        finishRunnable.run();
                     }
                 }, KeyguardPatternView.this);
         if (!TextUtils.isEmpty(mSecurityMessageDisplay.getText())) {
             mDisappearAnimationUtils.createAnimation(mSecurityMessageDisplay, 0,
-                    200,
+                    (long) (200 * durationMultiplier),
                     - mDisappearAnimationUtils.getStartTranslation() * 3,
                     false /* appearing */,
                     mDisappearAnimationUtils.getInterpolator(),
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardPinBasedInputView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardPinBasedInputView.java
index 8d6e07e..108b466 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardPinBasedInputView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardPinBasedInputView.java
@@ -20,13 +20,14 @@
 import android.graphics.Rect;
 import android.util.AttributeSet;
 import android.view.KeyEvent;
+import android.view.MotionEvent;
 import android.view.View;
 
 /**
  * A Pin based Keyguard input view
  */
 public abstract class KeyguardPinBasedInputView extends KeyguardAbsKeyInputView
-        implements View.OnKeyListener {
+        implements View.OnKeyListener, View.OnTouchListener {
 
     protected PasswordTextView mPasswordEntry;
     private View mOkButton;
@@ -185,10 +186,10 @@
 
         mOkButton = findViewById(R.id.key_enter);
         if (mOkButton != null) {
+            mOkButton.setOnTouchListener(this);
             mOkButton.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
-                    doHapticKeyClick();
                     if (mPasswordEntry.isEnabled()) {
                         verifyPasswordAndUnlock();
                     }
@@ -199,6 +200,7 @@
 
         mDeleteButton = findViewById(R.id.delete_button);
         mDeleteButton.setVisibility(View.VISIBLE);
+        mDeleteButton.setOnTouchListener(this);
         mDeleteButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
@@ -206,7 +208,6 @@
                 if (mPasswordEntry.isEnabled()) {
                     mPasswordEntry.deleteLastChar();
                 }
-                doHapticKeyClick();
             }
         });
         mDeleteButton.setOnLongClickListener(new View.OnLongClickListener() {
@@ -237,6 +238,14 @@
     }
 
     @Override
+    public boolean onTouch(View v, MotionEvent event) {
+        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
+            doHapticKeyClick();
+        }
+        return false;
+    }
+
+    @Override
     public boolean onKey(View v, int keyCode, KeyEvent event) {
         if (event.getAction() == KeyEvent.ACTION_DOWN) {
             return onKeyDown(keyCode, event);
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardSecurityView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardSecurityView.java
index aa74940..8290842 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardSecurityView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardSecurityView.java
@@ -49,11 +49,6 @@
     int PROMPT_REASON_AFTER_LOCKOUT = 5;
 
     /**
-     * Some auth is required because a single wrong credential has been tried.
-     */
-    int PROMPT_REASON_WRONG_CREDENTIAL = 6;
-
-    /**
      * Interface back to keyguard to tell it when security
      * @param callback
      */
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardStatusView.java b/packages/Keyguard/src/com/android/keyguard/KeyguardStatusView.java
index 9d1df26..e1657c7 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardStatusView.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardStatusView.java
@@ -127,6 +127,11 @@
         super.onConfigurationChanged(newConfig);
         mClockView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                 getResources().getDimensionPixelSize(R.dimen.widget_big_font_size));
+        // Some layouts like burmese have a different margin for the clock
+        MarginLayoutParams layoutParams = (MarginLayoutParams) mClockView.getLayoutParams();
+        layoutParams.bottomMargin = getResources().getDimensionPixelSize(
+                R.dimen.bottom_text_spacing_digital);
+        mClockView.setLayoutParams(layoutParams);
         mDateView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                 getResources().getDimensionPixelSize(R.dimen.widget_label_font_size));
         mOwnerInfo.setTextSize(TypedValue.COMPLEX_UNIT_PX,
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java b/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
index a8419bf..a9f6dc9 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
@@ -16,6 +16,17 @@
 
 package com.android.keyguard;
 
+import static android.content.Intent.ACTION_USER_UNLOCKED;
+import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
+import static android.os.BatteryManager.BATTERY_STATUS_FULL;
+import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
+import static android.os.BatteryManager.EXTRA_HEALTH;
+import static android.os.BatteryManager.EXTRA_LEVEL;
+import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT;
+import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE;
+import static android.os.BatteryManager.EXTRA_PLUGGED;
+import static android.os.BatteryManager.EXTRA_STATUS;
+
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
 import android.app.AlarmManager;
@@ -24,12 +35,14 @@
 import android.app.admin.DevicePolicyManager;
 import android.app.trust.TrustManager;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.database.ContentObserver;
 import android.graphics.Bitmap;
-import android.hardware.fingerprint.Fingerprint;
 import android.hardware.fingerprint.FingerprintManager;
 import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback;
 import android.hardware.fingerprint.FingerprintManager.AuthenticationResult;
@@ -41,7 +54,9 @@
 import android.os.Message;
 import android.os.RemoteException;
 import android.os.SystemClock;
+import android.os.Trace;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.provider.Settings;
 import android.telephony.ServiceState;
 import android.telephony.SubscriptionInfo;
@@ -69,16 +84,6 @@
 import java.util.List;
 import java.util.Map.Entry;
 
-import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN;
-import static android.os.BatteryManager.BATTERY_STATUS_FULL;
-import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN;
-import static android.os.BatteryManager.EXTRA_HEALTH;
-import static android.os.BatteryManager.EXTRA_LEVEL;
-import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT;
-import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE;
-import static android.os.BatteryManager.EXTRA_PLUGGED;
-import static android.os.BatteryManager.EXTRA_STATUS;
-
 /**
  * Watches for updates that may be interesting to the keyguard, and provides
  * the up to date information as well as a registration for callbacks that care
@@ -107,12 +112,6 @@
 
     private static final String PERMISSION_SELF = "com.android.systemui.permission.SELF";
 
-    /**
-     * Milliseconds after unlocking with fingerprint times out, i.e. the user has to use a
-     * strong auth method like password, PIN or pattern.
-     */
-    private static final long FINGERPRINT_UNLOCK_TIMEOUT_MS = 72 * 60 * 60 * 1000;
-
     // Callback messages
     private static final int MSG_TIME_UPDATE = 301;
     private static final int MSG_BATTERY_UPDATE = 302;
@@ -137,6 +136,8 @@
     private static final int MSG_SERVICE_STATE_CHANGE = 330;
     private static final int MSG_SCREEN_TURNED_ON = 331;
     private static final int MSG_SCREEN_TURNED_OFF = 332;
+    private static final int MSG_DREAMING_STATE_CHANGED = 333;
+    private static final int MSG_USER_UNLOCKED = 334;
 
     /** Fingerprint state: Not listening to fingerprint. */
     private static final int FINGERPRINT_STATE_STOPPED = 0;
@@ -158,6 +159,9 @@
 
     private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000;
 
+    private static final ComponentName FALLBACK_HOME_COMPONENT = new ComponentName(
+            "com.android.settings", "com.android.settings.FallbackHome");
+
     private static KeyguardUpdateMonitor sInstance;
 
     private final Context mContext;
@@ -176,6 +180,8 @@
     private boolean mGoingToSleep;
     private boolean mBouncer;
     private boolean mBootCompleted;
+    private boolean mNeedsSlowUnlockTransition;
+    private boolean mHasLockscreenWallpaper;
 
     // Device provisioning state
     private boolean mDeviceProvisioned;
@@ -202,6 +208,7 @@
     private AlarmManager mAlarmManager;
     private List<SubscriptionInfo> mSubscriptionInfo;
     private TrustManager mTrustManager;
+    private UserManager mUserManager;
     private int mFingerprintRunningState = FINGERPRINT_STATE_STOPPED;
 
     private final Handler mHandler = new Handler() {
@@ -257,10 +264,14 @@
                     handleFinishedGoingToSleep(msg.arg1);
                     break;
                 case MSG_STARTED_WAKING_UP:
+                    Trace.beginSection("KeyguardUpdateMonitor#handler MSG_STARTED_WAKING_UP");
                     handleStartedWakingUp();
+                    Trace.endSection();
                     break;
                 case MSG_FACE_UNLOCK_STATE_CHANGED:
+                    Trace.beginSection("KeyguardUpdateMonitor#handler MSG_FACE_UNLOCK_STATE_CHANGED");
                     handleFaceUnlockStateChanged(msg.arg1 != 0, msg.arg2);
+                    Trace.endSection();
                     break;
                 case MSG_SIM_SUBSCRIPTION_INFO_CHANGED:
                     handleSimSubscriptionInfoChanged();
@@ -275,7 +286,15 @@
                     handleScreenTurnedOn();
                     break;
                 case MSG_SCREEN_TURNED_OFF:
+                    Trace.beginSection("KeyguardUpdateMonitor#handler MSG_SCREEN_TURNED_ON");
                     handleScreenTurnedOff();
+                    Trace.endSection();
+                    break;
+                case MSG_DREAMING_STATE_CHANGED:
+                    handleDreamingStateChanged(msg.arg1);
+                    break;
+                case MSG_USER_UNLOCKED:
+                    handleUserUnlocked();
                     break;
             }
         }
@@ -370,7 +389,7 @@
     }
 
     /** @return List of SubscriptionInfo records, maybe empty but never null */
-    List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
+    public List<SubscriptionInfo> getSubscriptionInfo(boolean forceReload) {
         List<SubscriptionInfo> sil = mSubscriptionInfo;
         if (sil == null || forceReload) {
             sil = mSubscriptionManager.getActiveSubscriptionInfoList();
@@ -397,6 +416,7 @@
     }
 
     private void onFingerprintAuthenticated(int userId) {
+        Trace.beginSection("KeyGuardUpdateMonitor#onFingerPrintAuthenticated");
         mUserFingerprintAuthenticated.put(userId, true);
 
         // If fingerprint unlocking is allowed, this event will lead to a Keyguard dismiss or to a
@@ -409,6 +429,7 @@
                 cb.onFingerprintAuthenticated(userId);
             }
         }
+        Trace.endSection();
     }
 
     private void handleFingerprintAuthFailed() {
@@ -433,7 +454,8 @@
         }
     }
 
-    private void handleFingerprintAuthenticated() {
+    private void handleFingerprintAuthenticated(int authUserId) {
+        Trace.beginSection("KeyGuardUpdateMonitor#handlerFingerPrintAuthenticated");
         try {
             final int userId;
             try {
@@ -442,6 +464,10 @@
                 Log.e(TAG, "Failed to get current user id: ", e);
                 return;
             }
+            if (userId != authUserId) {
+                Log.d(TAG, "Fingerprint authenticated for wrong user: " + authUserId);
+                return;
+            }
             if (isFingerprintDisabled(userId)) {
                 Log.d(TAG, "Fingerprint disabled by DPM for userId: " + userId);
                 return;
@@ -450,6 +476,7 @@
         } finally {
             setFingerprintRunningState(FINGERPRINT_STATE_STOPPED);
         }
+        Trace.endSection();
     }
 
     private void handleFingerprintHelp(int msgId, String helpString) {
@@ -554,6 +581,10 @@
                 && !hasFingerprintUnlockTimedOut(sCurrentUser);
     }
 
+    public boolean needsSlowUnlockTransition() {
+        return mNeedsSlowUnlockTransition;
+    }
+
     public StrongAuthTracker getStrongAuthTracker() {
         return mStrongAuthTracker;
     }
@@ -576,7 +607,10 @@
     }
 
     private void scheduleStrongAuthTimeout() {
-        long when = SystemClock.elapsedRealtime() + FINGERPRINT_UNLOCK_TIMEOUT_MS;
+        final DevicePolicyManager dpm =
+                (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
+        long when = SystemClock.elapsedRealtime() + dpm.getRequiredStrongAuthTimeout(null,
+                sCurrentUser);
         Intent intent = new Intent(ACTION_STRONG_AUTH_TIMEOUT);
         intent.putExtra(USER_ID, sCurrentUser);
         PendingIntent sender = PendingIntent.getBroadcast(mContext,
@@ -684,14 +718,18 @@
                 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_INFO_CHANGED,
                         intent.getIntExtra(Intent.EXTRA_USER_HANDLE, getSendingUserId()), 0));
             } else if (ACTION_FACE_UNLOCK_STARTED.equals(action)) {
+                Trace.beginSection("KeyguardUpdateMonitor.mBroadcastAllReceiver#onReceive ACTION_FACE_UNLOCK_STARTED");
                 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 1,
                         getSendingUserId()));
+                Trace.endSection();
             } else if (ACTION_FACE_UNLOCK_STOPPED.equals(action)) {
                 mHandler.sendMessage(mHandler.obtainMessage(MSG_FACE_UNLOCK_STATE_CHANGED, 0,
                         getSendingUserId()));
             } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
                     .equals(action)) {
                 mHandler.sendEmptyMessage(MSG_DPM_STATE_CHANGED);
+            } else if (ACTION_USER_UNLOCKED.equals(action)) {
+                mHandler.sendEmptyMessage(MSG_USER_UNLOCKED);
             }
         }
     };
@@ -725,7 +763,9 @@
 
         @Override
         public void onAuthenticationSucceeded(AuthenticationResult result) {
-            handleFingerprintAuthenticated();
+            Trace.beginSection("KeyguardUpdateMonitor#onAuthenticationSucceeded");
+            handleFingerprintAuthenticated(result.getUserId());
+            Trace.endSection();
         }
 
         @Override
@@ -900,6 +940,7 @@
     }
 
     protected void handleStartedWakingUp() {
+        Trace.beginSection("KeyguardUpdateMonitor#handleStartedWakingUp");
         updateFingerprintListeningState();
         final int count = mCallbacks.size();
         for (int i = 0; i < count; i++) {
@@ -908,6 +949,7 @@
                 cb.onStartedWakingUp();
             }
         }
+        Trace.endSection();
     }
 
     protected void handleStartedGoingToSleep(int arg1) {
@@ -956,6 +998,17 @@
         }
     }
 
+    private void handleDreamingStateChanged(int dreamStart) {
+        final int count = mCallbacks.size();
+        boolean showingDream = dreamStart == 1;
+        for (int i = 0; i < count; i++) {
+            KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
+            if (cb != null) {
+                cb.onDreamingStateChanged(showingDream);
+            }
+        }
+    }
+
     /**
      * IMPORTANT: Must be called from UI thread.
      */
@@ -979,6 +1032,16 @@
         }
     }
 
+    private void handleUserUnlocked() {
+        mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
+        for (int i = 0; i < mCallbacks.size(); i++) {
+            KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
+            if (cb != null) {
+                cb.onUserUnlocked();
+            }
+        }
+    }
+
     private KeyguardUpdateMonitor(Context context) {
         mContext = context;
         mSubscriptionManager = SubscriptionManager.from(context);
@@ -1019,6 +1082,7 @@
         allUserFilter.addAction(ACTION_FACE_UNLOCK_STARTED);
         allUserFilter.addAction(ACTION_FACE_UNLOCK_STOPPED);
         allUserFilter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
+        allUserFilter.addAction(ACTION_USER_UNLOCKED);
         context.registerReceiverAsUser(mBroadcastAllReceiver, UserHandle.ALL, allUserFilter,
                 null, null);
 
@@ -1040,10 +1104,9 @@
                         public void onForegroundProfileSwitch(int newProfileId) {
                             // Ignore.
                         }
-                    });
+                    }, TAG);
         } catch (RemoteException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+            e.rethrowAsRuntimeException();
         }
 
         IntentFilter strongAuthTimeoutFilter = new IntentFilter();
@@ -1059,6 +1122,8 @@
         if (mFpm != null) {
             mFpm.addLockoutResetCallback(mLockoutResetCallback);
         }
+
+        mUserManager = context.getSystemService(UserManager.class);
     }
 
     private void updateFingerprintListeningState() {
@@ -1145,6 +1210,30 @@
     }
 
     /**
+     * Update the state whether Keyguard currently has a lockscreen wallpaper.
+     *
+     * @param hasLockscreenWallpaper Whether Keyguard has a lockscreen wallpaper.
+     */
+    public void setHasLockscreenWallpaper(boolean hasLockscreenWallpaper) {
+        if (hasLockscreenWallpaper != mHasLockscreenWallpaper) {
+            mHasLockscreenWallpaper = hasLockscreenWallpaper;
+            for (int i = mCallbacks.size() - 1; i >= 0; i--) {
+                KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get();
+                if (cb != null) {
+                    cb.onHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
+                }
+            }
+        }
+    }
+
+    /**
+     * @return Whether Keyguard has a lockscreen wallpaper.
+     */
+    public boolean hasLockscreenWallpaper() {
+        return mHasLockscreenWallpaper;
+    }
+
+    /**
      * Handle {@link #MSG_DPM_STATE_CHANGED}
      */
     protected void handleDevicePolicyManagerStateChanged() {
@@ -1391,6 +1480,18 @@
     private void handleKeyguardReset() {
         if (DEBUG) Log.d(TAG, "handleKeyguardReset");
         updateFingerprintListeningState();
+        mNeedsSlowUnlockTransition = resolveNeedsSlowUnlockTransition();
+    }
+
+    private boolean resolveNeedsSlowUnlockTransition() {
+        if (mUserManager.isUserUnlocked(getCurrentUser())) {
+            return false;
+        }
+        Intent homeIntent = new Intent(Intent.ACTION_MAIN)
+                .addCategory(Intent.CATEGORY_HOME);
+        ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivity(homeIntent,
+                0 /* flags */);
+        return FALLBACK_HOME_COMPONENT.equals(resolveInfo.getComponentInfo().getComponentName());
     }
 
     /**
@@ -1665,6 +1766,14 @@
         mHandler.sendEmptyMessage(MSG_SCREEN_TURNED_OFF);
     }
 
+    public void dispatchDreamingStarted() {
+        mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 1, 0));
+    }
+
+    public void dispatchDreamingStopped() {
+        mHandler.sendMessage(mHandler.obtainMessage(MSG_DREAMING_STATE_CHANGED, 0, 0));
+    }
+
     public boolean isDeviceInteractive() {
         return mDeviceInteractive;
     }
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitorCallback.java b/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitorCallback.java
index bd6c51c..14d6b59 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitorCallback.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitorCallback.java
@@ -128,6 +128,11 @@
     public void onUserInfoChanged(int userId) { }
 
     /**
+     * Called when a user got unlocked.
+     */
+    public void onUserUnlocked() { }
+
+    /**
      * Called when boot completed.
      *
      * Note, this callback will only be received if boot complete occurs after registering with
@@ -240,4 +245,15 @@
      * has changed.
      */
     public void onStrongAuthStateChanged(int userId) { }
+
+    /**
+     * Called when the state whether we have a lockscreen wallpaper has changed.
+     */
+    public void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) { }
+
+    /**
+     * Called when the dream's window state is changed.
+     * @param dreaming true if the dream's window has been created and is visible
+     */
+    public void onDreamingStateChanged(boolean dreaming) { }
 }
diff --git a/packages/Keyguard/src/com/android/keyguard/NumPadKey.java b/packages/Keyguard/src/com/android/keyguard/NumPadKey.java
index 2ff7e12..1518bdc 100644
--- a/packages/Keyguard/src/com/android/keyguard/NumPadKey.java
+++ b/packages/Keyguard/src/com/android/keyguard/NumPadKey.java
@@ -23,6 +23,7 @@
 import android.util.AttributeSet;
 import android.view.HapticFeedbackConstants;
 import android.view.LayoutInflater;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
@@ -53,8 +54,7 @@
             if (mTextView != null && mTextView.isEnabled()) {
                 mTextView.append(Character.forDigit(mDigit, 10));
             }
-            userActivity();
-            doHapticKeyClick();
+            userActivity();;
         }
     };
 
@@ -126,6 +126,14 @@
     }
 
     @Override
+    public boolean onTouchEvent(MotionEvent event) {
+        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
+            doHapticKeyClick();
+        }
+        return super.onTouchEvent(event);
+    }
+
+    @Override
     public void onDetachedFromWindow() {
         super.onDetachedFromWindow();
 
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java
index cce619e..4950af3 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDatabase.java
@@ -873,7 +873,7 @@
     }
 
     private static int getRootFlags(int[] operationsSupported) {
-        int rootFlag = Root.FLAG_SUPPORTS_IS_CHILD;
+        int rootFlag = Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_LOCAL_ONLY;
         if (MtpDeviceRecord.isWritingSupported(operationsSupported)) {
             rootFlag |= Root.FLAG_SUPPORTS_CREATE;
         }
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDatabaseTest.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDatabaseTest.java
index 404047b..8c13c81 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDatabaseTest.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDatabaseTest.java
@@ -128,7 +128,7 @@
             cursor.moveToNext();
             assertEquals(1, getInt(cursor, Root.COLUMN_ROOT_ID));
             assertEquals(
-                    Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE,
+                    Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE | Root.FLAG_LOCAL_ONLY,
                     getInt(cursor, Root.COLUMN_FLAGS));
             assertEquals(R.drawable.ic_root_mtp, getInt(cursor, Root.COLUMN_ICON));
             assertEquals("Device Storage", getString(cursor, Root.COLUMN_TITLE));
diff --git a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDocumentsProviderTest.java b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDocumentsProviderTest.java
index 9ed15c8..d19b460 100644
--- a/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDocumentsProviderTest.java
+++ b/packages/MtpDocumentsProvider/tests/src/com/android/mtp/MtpDocumentsProviderTest.java
@@ -210,7 +210,11 @@
             assertEquals(2, cursor.getCount());
             cursor.moveToNext();
             assertEquals("1", cursor.getString(0));
-            assertEquals(Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE, cursor.getInt(1));
+            assertEquals(
+                    Root.FLAG_SUPPORTS_IS_CHILD |
+                    Root.FLAG_SUPPORTS_CREATE |
+                    Root.FLAG_LOCAL_ONLY,
+                    cursor.getInt(1));
             assertEquals(R.drawable.ic_root_mtp, cursor.getInt(2));
             assertEquals("Device A Storage A", cursor.getString(3));
             assertEquals("1", cursor.getString(4));
@@ -225,7 +229,8 @@
             cursor.moveToNext();
             cursor.moveToNext();
             assertEquals("2", cursor.getString(0));
-            assertEquals(Root.FLAG_SUPPORTS_IS_CHILD, cursor.getInt(1));
+            assertEquals(
+                    Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_LOCAL_ONLY, cursor.getInt(1));
             assertEquals(R.drawable.ic_root_mtp, cursor.getInt(2));
             assertEquals("Device B Storage B", cursor.getString(3));
             assertEquals("2", cursor.getString(4));
@@ -271,7 +276,9 @@
 
             cursor.moveToNext();
             assertEquals("1", cursor.getString(0));
-            assertEquals(Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE, cursor.getInt(1));
+            assertEquals(
+                    Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE | Root.FLAG_LOCAL_ONLY,
+                    cursor.getInt(1));
             assertEquals(R.drawable.ic_root_mtp, cursor.getInt(2));
             assertEquals("Device A", cursor.getString(3));
             assertEquals("1", cursor.getString(4));
@@ -279,7 +286,9 @@
 
             cursor.moveToNext();
             assertEquals("2", cursor.getString(0));
-            assertEquals(Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE, cursor.getInt(1));
+            assertEquals(
+                    Root.FLAG_SUPPORTS_IS_CHILD | Root.FLAG_SUPPORTS_CREATE | Root.FLAG_LOCAL_ONLY,
+                    cursor.getInt(1));
             assertEquals(R.drawable.ic_root_mtp, cursor.getInt(2));
             assertEquals("Device B Storage B", cursor.getString(3));
             assertEquals("2", cursor.getString(4));
diff --git a/packages/PrintSpooler/res/drawable/print_button.xml b/packages/PrintSpooler/res/drawable/print_button.xml
index b59afba..0114103 100644
--- a/packages/PrintSpooler/res/drawable/print_button.xml
+++ b/packages/PrintSpooler/res/drawable/print_button.xml
@@ -16,7 +16,7 @@
 -->
 
 <ripple xmlns:android="http://schemas.android.com/apk/res/android"
-    android:color="@color/print_button_tint_color">
+    android:color="?android:attr/colorControlHighlight">
     <item
         android:drawable="@drawable/print_button_background">
     </item>
diff --git a/packages/PrintSpooler/res/layout/print_activity.xml b/packages/PrintSpooler/res/layout/print_activity.xml
index 2db6fb0..31a776c 100644
--- a/packages/PrintSpooler/res/layout/print_activity.xml
+++ b/packages/PrintSpooler/res/layout/print_activity.xml
@@ -16,7 +16,6 @@
 
 <com.android.printspooler.widget.PrintContentView
         xmlns:android="http://schemas.android.com/apk/res/android"
-        xmlns:printspooler="http://schemas.android.com/apk/res/com.android.printspooler"
     android:id="@+id/options_content"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
@@ -28,12 +27,14 @@
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:paddingStart="8dip"
+        android:layout_marginEnd="16dp"
         android:elevation="@dimen/preview_controls_elevation"
         android:background="?android:attr/colorPrimary">
 
         <Spinner
             android:id="@+id/destination_spinner"
-            android:layout_width="@dimen/preview_destination_spinner_width"
+            android:layout_width="wrap_content"
+            android:minWidth="@dimen/preview_destination_spinner_width"
             android:layout_height="wrap_content"
             android:layout_marginTop="4dip"
             android:dropDownWidth="wrap_content"
diff --git a/packages/PrintSpooler/res/layout/printer_dropdown_item.xml b/packages/PrintSpooler/res/layout/printer_dropdown_item.xml
index 103c157..0d8a90a 100644
--- a/packages/PrintSpooler/res/layout/printer_dropdown_item.xml
+++ b/packages/PrintSpooler/res/layout/printer_dropdown_item.xml
@@ -16,7 +16,8 @@
 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent"
-      android:layout_height="?android:attr/listPreferredItemHeightSmall"
+      android:layout_height="wrap_content"
+      android:minHeight="?android:attr/listPreferredItemHeightSmall"
       style="?android:attr/spinnerItemStyle"
       android:orientation="horizontal"
       android:gravity="start|center_vertical">
diff --git a/packages/PrintSpooler/res/values/colors.xml b/packages/PrintSpooler/res/values/colors.xml
index 47e616e..9464c67 100644
--- a/packages/PrintSpooler/res/values/colors.xml
+++ b/packages/PrintSpooler/res/values/colors.xml
@@ -16,8 +16,6 @@
 
 <resources>
 
-    <color name="print_button_tint_color">#EEFF41</color>
-
     <color name="print_preview_scrim_color">#99000000</color>
 
     <color name="print_preview_background_color">#F2F1F2</color>
diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java b/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java
index 999d82d..6140428 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/model/PageContentRepository.java
@@ -838,9 +838,15 @@
 
                     try (ParcelFileDescriptor source = pipe[0]) {
                         try (ParcelFileDescriptor destination = pipe[1]) {
-
-                            mRenderer.renderPage(mPageIndex, bitmap.getWidth(), bitmap.getHeight(),
-                                    mRenderSpec.printAttributes, destination);
+                            synchronized (mLock) {
+                                if (mRenderer != null) {
+                                    mRenderer.renderPage(mPageIndex, bitmap.getWidth(),
+                                            bitmap.getHeight(), mRenderSpec.printAttributes,
+                                            destination);
+                                } else {
+                                    throw new IllegalStateException("Renderer is disconnected");
+                                }
+                            }
                         }
 
                         BitmapSerializeUtils.readBitmapPixels(bitmap, source);
diff --git a/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java b/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java
index 99145b7b..eea92b5 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/model/RemotePrintDocument.java
@@ -94,6 +94,7 @@
                     // but the content has changed.
                     if (mNextCommand == null) {
                         if (mUpdateSpec.pages != null && (mDocumentInfo.changed
+                                || mDocumentInfo.writtenPages == null
                                 || (mDocumentInfo.info.getPageCount()
                                         != PrintDocumentInfo.PAGE_COUNT_UNKNOWN
                                 && !PageRangeUtils.contains(mDocumentInfo.writtenPages,
diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java
index 2f58de5..c06e849 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/ui/AddPrinterActivity.java
@@ -737,9 +737,11 @@
         public void updateInstalledServices(List<PrintServiceInfo> services) {
             mInstalledServices.clear();
 
-            final int numServices = services.size();
-            for (int i = 0; i < numServices; i++) {
-                mInstalledServices.add(services.get(i).getComponentName().getPackageName());
+            if (services != null) {
+                final int numServices = services.size();
+                for (int i = 0; i < numServices; i++) {
+                    mInstalledServices.add(services.get(i).getComponentName().getPackageName());
+                }
             }
 
             filterRecommendations();
diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
index c318275..3b25edb 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java
@@ -244,6 +244,8 @@
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        setTitle(R.string.print_dialog);
+
         Bundle extras = getIntent().getExtras();
 
         mPrintJob = extras.getParcelable(PrintManager.EXTRA_PRINT_JOB);
@@ -298,7 +300,6 @@
         // Now that we are bound to the local print spooler service
         // and the printer registry loaded the historical printers
         // we can show the UI without flickering.
-        setTitle(R.string.print_dialog);
         setContentView(R.layout.print_activity);
 
         try {
@@ -526,6 +527,14 @@
                     .setContentType(info.getContentType())
                     .setPageCount(pageCount)
                     .build();
+
+            File file = mFileProvider.acquireFile(null);
+            try {
+                adjustedInfo.setDataSize(file.length());
+            } finally {
+                mFileProvider.releaseFile();
+            }
+
             mPrintJob.setDocumentInfo(adjustedInfo);
             mPrintJob.setPages(document.printedPages);
         }
@@ -2060,6 +2069,7 @@
 
         if (mPrinterRegistry != null) {
             mPrinterRegistry.setTrackedPrinter(null);
+            mPrinterRegistry.setOnPrintersChangeListener(null);
         }
 
         if (mPrintersObserver != null) {
@@ -3076,6 +3086,14 @@
                     .setContentType(oldDocInfo.getContentType())
                     .setPageCount(newPageCount)
                     .build();
+
+            File file = mFileProvider.acquireFile(null);
+            try {
+                newDocInfo.setDataSize(file.length());
+            } finally {
+                mFileProvider.releaseFile();
+            }
+
             mPrintJob.setDocumentInfo(newDocInfo);
         }
 
diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrinterRegistry.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrinterRegistry.java
index 86366dd..9fca959 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrinterRegistry.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrinterRegistry.java
@@ -120,13 +120,11 @@
         @Override
         public void onLoaderReset(Loader<List<PrinterInfo>> loader) {
             mPrinters.clear();
-            if (mOnPrintersChangeListener != null) {
-                // Post a message as we are in onLoadFinished and certain operations
-                // are not allowed in this callback, such as fragment transactions.
-                // Clients should not handle this explicitly.
-                mHandler.obtainMessage(MyHandler.MSG_PRINTERS_INVALID,
-                        mOnPrintersChangeListener).sendToTarget();
-            }
+
+            // Post a message as we are in onLoadFinished and certain operations
+            // are not allowed in this callback, such as fragment transactions.
+            // Clients should not handle this explicitly.
+            mHandler.obtainMessage(MyHandler.MSG_PRINTERS_INVALID).sendToTarget();
         }
 
         // LoaderCallbacks#onLoadFinished
@@ -134,15 +132,12 @@
         public void onLoadFinished(Loader<List<PrinterInfo>> loader, List<PrinterInfo> printers) {
             mPrinters.clear();
             mPrinters.addAll(printers);
-            if (mOnPrintersChangeListener != null) {
-                // Post a message as we are in onLoadFinished and certain operations
-                // are not allowed in this callback, such as fragment transactions.
-                // Clients should not handle this explicitly.
-                SomeArgs args = SomeArgs.obtain();
-                args.arg1 = mOnPrintersChangeListener;
-                args.arg2 = printers;
-                mHandler.obtainMessage(MyHandler.MSG_PRINTERS_CHANGED, args).sendToTarget();
-            }
+
+            // Post a message as we are in onLoadFinished and certain operations
+            // are not allowed in this callback, such as fragment transactions.
+            // Clients should not handle this explicitly.
+            mHandler.obtainMessage(MyHandler.MSG_PRINTERS_CHANGED, printers).sendToTarget();
+
             if (!mReady) {
                 mReady = true;
                 if (mReadyCallback != null) {
@@ -158,7 +153,7 @@
         }
     };
 
-    private static final class MyHandler extends Handler {
+    private final class MyHandler extends Handler {
         public static final int MSG_PRINTERS_CHANGED = 0;
         public static final int MSG_PRINTERS_INVALID = 1;
 
@@ -171,16 +166,17 @@
         public void handleMessage(Message message) {
             switch (message.what) {
                 case MSG_PRINTERS_CHANGED: {
-                    SomeArgs args = (SomeArgs) message.obj;
-                    OnPrintersChangeListener callback = (OnPrintersChangeListener) args.arg1;
-                    List<PrinterInfo> printers = (List<PrinterInfo>) args.arg2;
-                    args.recycle();
-                    callback.onPrintersChanged(printers);
+                    List<PrinterInfo> printers = (List<PrinterInfo>) message.obj;
+
+                    if (mOnPrintersChangeListener != null) {
+                        mOnPrintersChangeListener.onPrintersChanged(printers);
+                    }
                 } break;
 
                 case MSG_PRINTERS_INVALID: {
-                    OnPrintersChangeListener callback = (OnPrintersChangeListener) message.obj;
-                    callback.onPrintersInvalid();
+                    if (mOnPrintersChangeListener != null) {
+                        mOnPrintersChangeListener.onPrintersInvalid();
+                    }
                 } break;
             }
         }
diff --git a/packages/SettingsLib/res/drawable-hdpi/ic_bt_cellphone.png b/packages/SettingsLib/res/drawable-hdpi/ic_bt_cellphone.png
deleted file mode 100644
index 6e29d23..0000000
--- a/packages/SettingsLib/res/drawable-hdpi/ic_bt_cellphone.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-hdpi/ic_bt_headphones_a2dp.png b/packages/SettingsLib/res/drawable-hdpi/ic_bt_headphones_a2dp.png
deleted file mode 100644
index 6110e9e..0000000
--- a/packages/SettingsLib/res/drawable-hdpi/ic_bt_headphones_a2dp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-hdpi/ic_bt_headset_hfp.png b/packages/SettingsLib/res/drawable-hdpi/ic_bt_headset_hfp.png
deleted file mode 100644
index 6cca225..0000000
--- a/packages/SettingsLib/res/drawable-hdpi/ic_bt_headset_hfp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-hdpi/ic_bt_misc_hid.png b/packages/SettingsLib/res/drawable-hdpi/ic_bt_misc_hid.png
deleted file mode 100644
index 6445f2a..0000000
--- a/packages/SettingsLib/res/drawable-hdpi/ic_bt_misc_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-hdpi/ic_bt_network_pan.png b/packages/SettingsLib/res/drawable-hdpi/ic_bt_network_pan.png
deleted file mode 100644
index 78c0294..0000000
--- a/packages/SettingsLib/res/drawable-hdpi/ic_bt_network_pan.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-hdpi/ic_bt_pointing_hid.png b/packages/SettingsLib/res/drawable-hdpi/ic_bt_pointing_hid.png
deleted file mode 100644
index 2fcc3b0..0000000
--- a/packages/SettingsLib/res/drawable-hdpi/ic_bt_pointing_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-hdpi/ic_lockscreen_ime.png b/packages/SettingsLib/res/drawable-hdpi/ic_lockscreen_ime.png
deleted file mode 100644
index 70d35bf..0000000
--- a/packages/SettingsLib/res/drawable-hdpi/ic_lockscreen_ime.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-ldrtl-hdpi/ic_bt_cellphone.png b/packages/SettingsLib/res/drawable-ldrtl-hdpi/ic_bt_cellphone.png
deleted file mode 100644
index 2d9b75e..0000000
--- a/packages/SettingsLib/res/drawable-ldrtl-hdpi/ic_bt_cellphone.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-ldrtl-mdpi/ic_bt_cellphone.png b/packages/SettingsLib/res/drawable-ldrtl-mdpi/ic_bt_cellphone.png
deleted file mode 100644
index b6ebe34..0000000
--- a/packages/SettingsLib/res/drawable-ldrtl-mdpi/ic_bt_cellphone.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-ldrtl-xhdpi/ic_bt_cellphone.png b/packages/SettingsLib/res/drawable-ldrtl-xhdpi/ic_bt_cellphone.png
deleted file mode 100644
index 8b67b91..0000000
--- a/packages/SettingsLib/res/drawable-ldrtl-xhdpi/ic_bt_cellphone.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-mdpi/ic_bt_cellphone.png b/packages/SettingsLib/res/drawable-mdpi/ic_bt_cellphone.png
deleted file mode 100644
index 1fa0a3d..0000000
--- a/packages/SettingsLib/res/drawable-mdpi/ic_bt_cellphone.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-mdpi/ic_bt_headphones_a2dp.png b/packages/SettingsLib/res/drawable-mdpi/ic_bt_headphones_a2dp.png
deleted file mode 100644
index 175bd78..0000000
--- a/packages/SettingsLib/res/drawable-mdpi/ic_bt_headphones_a2dp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-mdpi/ic_bt_headset_hfp.png b/packages/SettingsLib/res/drawable-mdpi/ic_bt_headset_hfp.png
deleted file mode 100644
index 05b27e8..0000000
--- a/packages/SettingsLib/res/drawable-mdpi/ic_bt_headset_hfp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-mdpi/ic_bt_misc_hid.png b/packages/SettingsLib/res/drawable-mdpi/ic_bt_misc_hid.png
deleted file mode 100644
index 6e9f8ae..0000000
--- a/packages/SettingsLib/res/drawable-mdpi/ic_bt_misc_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-mdpi/ic_bt_network_pan.png b/packages/SettingsLib/res/drawable-mdpi/ic_bt_network_pan.png
deleted file mode 100644
index 5f3371f..0000000
--- a/packages/SettingsLib/res/drawable-mdpi/ic_bt_network_pan.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-mdpi/ic_bt_pointing_hid.png b/packages/SettingsLib/res/drawable-mdpi/ic_bt_pointing_hid.png
deleted file mode 100644
index 539d77f..0000000
--- a/packages/SettingsLib/res/drawable-mdpi/ic_bt_pointing_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-mdpi/ic_lockscreen_ime.png b/packages/SettingsLib/res/drawable-mdpi/ic_lockscreen_ime.png
deleted file mode 100644
index 3216776..0000000
--- a/packages/SettingsLib/res/drawable-mdpi/ic_lockscreen_ime.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_cellphone.png b/packages/SettingsLib/res/drawable-xhdpi/ic_bt_cellphone.png
deleted file mode 100644
index 4f381ba..0000000
--- a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_cellphone.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_headphones_a2dp.png b/packages/SettingsLib/res/drawable-xhdpi/ic_bt_headphones_a2dp.png
deleted file mode 100644
index c67127d..0000000
--- a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_headphones_a2dp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_headset_hfp.png b/packages/SettingsLib/res/drawable-xhdpi/ic_bt_headset_hfp.png
deleted file mode 100644
index d3b356b..0000000
--- a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_headset_hfp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_misc_hid.png b/packages/SettingsLib/res/drawable-xhdpi/ic_bt_misc_hid.png
deleted file mode 100644
index 2d38129..0000000
--- a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_misc_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_network_pan.png b/packages/SettingsLib/res/drawable-xhdpi/ic_bt_network_pan.png
deleted file mode 100644
index fb76575..0000000
--- a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_network_pan.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_pointing_hid.png b/packages/SettingsLib/res/drawable-xhdpi/ic_bt_pointing_hid.png
deleted file mode 100644
index d8b68eb..0000000
--- a/packages/SettingsLib/res/drawable-xhdpi/ic_bt_pointing_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xhdpi/ic_lockscreen_ime.png b/packages/SettingsLib/res/drawable-xhdpi/ic_lockscreen_ime.png
deleted file mode 100644
index 02cc3af..0000000
--- a/packages/SettingsLib/res/drawable-xhdpi/ic_lockscreen_ime.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_cellphone.png b/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_cellphone.png
deleted file mode 100644
index 7805b7a..0000000
--- a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_cellphone.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_headphones_a2dp.png b/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_headphones_a2dp.png
deleted file mode 100644
index 8127774..0000000
--- a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_headphones_a2dp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_headset_hfp.png b/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_headset_hfp.png
deleted file mode 100644
index 84b8085..0000000
--- a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_headset_hfp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_misc_hid.png b/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_misc_hid.png
deleted file mode 100644
index 289d6ac..0000000
--- a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_misc_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_network_pan.png b/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_network_pan.png
deleted file mode 100644
index 72bc804..0000000
--- a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_network_pan.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_pointing_hid.png b/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_pointing_hid.png
deleted file mode 100644
index e31ce2b..0000000
--- a/packages/SettingsLib/res/drawable-xxhdpi/ic_bt_pointing_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxhdpi/ic_lockscreen_ime.png b/packages/SettingsLib/res/drawable-xxhdpi/ic_lockscreen_ime.png
deleted file mode 100644
index f23b0e7..0000000
--- a/packages/SettingsLib/res/drawable-xxhdpi/ic_lockscreen_ime.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_cellphone.png b/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_cellphone.png
deleted file mode 100644
index 1e12f96..0000000
--- a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_cellphone.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_headphones_a2dp.png b/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_headphones_a2dp.png
deleted file mode 100644
index 8b547d9..0000000
--- a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_headphones_a2dp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_headset_hfp.png b/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_headset_hfp.png
deleted file mode 100644
index 03c5033..0000000
--- a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_headset_hfp.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_misc_hid.png b/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_misc_hid.png
deleted file mode 100644
index b9a9923..0000000
--- a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_misc_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_network_pan.png b/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_network_pan.png
deleted file mode 100644
index 989e1ab..0000000
--- a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_network_pan.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_pointing_hid.png b/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_pointing_hid.png
deleted file mode 100644
index de8c389..0000000
--- a/packages/SettingsLib/res/drawable-xxxhdpi/ic_bt_pointing_hid.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable-xxxhdpi/ic_lockscreen_ime.png b/packages/SettingsLib/res/drawable-xxxhdpi/ic_lockscreen_ime.png
deleted file mode 100644
index 2eb8a92..0000000
--- a/packages/SettingsLib/res/drawable-xxxhdpi/ic_lockscreen_ime.png
+++ /dev/null
Binary files differ
diff --git a/packages/SettingsLib/res/drawable/ic_bt_cellphone.xml b/packages/SettingsLib/res/drawable/ic_bt_cellphone.xml
new file mode 100644
index 0000000..cc9b732
--- /dev/null
+++ b/packages/SettingsLib/res/drawable/ic_bt_cellphone.xml
@@ -0,0 +1,29 @@
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<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/colorAccent"
+    android:autoMirrored="true">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M6.62,10.79c1.44,2.83 3.76,5.14 6.59,6.59l2.2,-2.2c0.27,-0.27 0.67,-0.36
+            1.02,-0.24 1.12,0.37 2.33,0.57 3.57,0.57 0.55,0 1,0.45 1,1V20c0,0.55 -0.45,1 -1,1
+            -9.39,0 -17,-7.61 -17,-17 0,-0.55 0.45,-1 1,-1h3.5c0.55,0 1,0.45 1,1 0,1.25 0.2,2.45
+            0.57,3.57 0.11,0.35 0.03,0.74 -0.25,1.02l-2.2,2.2z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/SettingsLib/res/drawable/ic_bt_headphones_a2dp.xml b/packages/SettingsLib/res/drawable/ic_bt_headphones_a2dp.xml
new file mode 100644
index 0000000..bbe3914
--- /dev/null
+++ b/packages/SettingsLib/res/drawable/ic_bt_headphones_a2dp.xml
@@ -0,0 +1,26 @@
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<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/colorAccent">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M12,1c-4.97,0 -9,4.03 -9,9v7c0,1.66 1.34,3 3,3h3v-8H5v-2c0,-3.87
+            3.13,-7 7,-7s7,3.13 7,7v2h-4v8h3c1.66,0 3,-1.34 3,-3v-7c0,-4.97 -4.03,-9 -9,-9z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/SettingsLib/res/drawable/ic_bt_headset_hfp.xml b/packages/SettingsLib/res/drawable/ic_bt_headset_hfp.xml
new file mode 100644
index 0000000..ceeef19
--- /dev/null
+++ b/packages/SettingsLib/res/drawable/ic_bt_headset_hfp.xml
@@ -0,0 +1,27 @@
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<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/colorAccent">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M12,1c-4.97,0 -9,4.03 -9,9v7c0,1.66 1.34,3 3,3h3v-8H5v-2c0,-3.87
+            3.13,-7 7,-7s7,3.13 7,7v2h-4v8h4v1h-7v2h6c1.66,0 3,-1.34 3,-3V10c0,-4.97 -4.03,-9
+            -9,-9z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/SettingsLib/res/drawable/ic_bt_misc_hid.xml b/packages/SettingsLib/res/drawable/ic_bt_misc_hid.xml
new file mode 100644
index 0000000..67b42ae
--- /dev/null
+++ b/packages/SettingsLib/res/drawable/ic_bt_misc_hid.xml
@@ -0,0 +1,26 @@
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<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/colorAccent">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M15,7.5V2H9v5.5l3,3 3,-3zM7.5,9H2v6h5.5l3,-3 -3,-3zM9,16.5V22h6v-5.5l-3,-3
+            -3,3zM16.5,9l-3,3 3,3H22V9h-5.5z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/SettingsLib/res/drawable/ic_bt_network_pan.xml b/packages/SettingsLib/res/drawable/ic_bt_network_pan.xml
new file mode 100644
index 0000000..c5ab01c
--- /dev/null
+++ b/packages/SettingsLib/res/drawable/ic_bt_network_pan.xml
@@ -0,0 +1,30 @@
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<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/colorAccent">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M14.24,12.01l2.32,2.32c0.28,-0.72 0.44,-1.51 0.44,-2.33 0,-0.82
+            -0.16,-1.59 -0.43,-2.31l-2.33,2.32zM19.53,6.71l-1.26,1.26c0.63,1.21 0.98,2.57
+            0.98,4.02s-0.36,2.82 -0.98,4.02l1.2,1.2c0.97,-1.54 1.54,-3.36 1.54,-5.31 -0.01,-1.89
+            -0.55,-3.67 -1.48,-5.19zM15.71,7.71L10,2L9,2v7.59L4.41,5 3,6.41 8.59,12 3,17.59 4.41,19
+            9,14.41L9,22h1l5.71,-5.71 -4.3,-4.29
+            4.3,-4.29zM11,5.83l1.88,1.88L11,9.59L11,5.83zM12.88,16.29L11,18.17v-3.76l1.88,1.88z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/SettingsLib/res/drawable/ic_bt_pointing_hid.xml b/packages/SettingsLib/res/drawable/ic_bt_pointing_hid.xml
new file mode 100644
index 0000000..821618d
--- /dev/null
+++ b/packages/SettingsLib/res/drawable/ic_bt_pointing_hid.xml
@@ -0,0 +1,26 @@
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<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/colorAccent">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M13,1.07L13,9h7c0,-4.08 -3.05,-7.44 -7,-7.93zM4,15c0,4.42
+            3.58,8 8,8s8,-3.58 8,-8v-4L4,11v4zM11,1.07C7.05,1.56 4,4.92 4,9h7L11,1.07z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/SettingsLib/res/drawable/ic_lockscreen_ime.xml b/packages/SettingsLib/res/drawable/ic_lockscreen_ime.xml
new file mode 100644
index 0000000..4aa8569
--- /dev/null
+++ b/packages/SettingsLib/res/drawable/ic_lockscreen_ime.xml
@@ -0,0 +1,26 @@
+<!--
+     Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<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/colorAccent">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M20,5L4,5c-1.1,0 -1.99,0.9 -1.99,2L2,17c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9
+            2,-2L22,7c0,-1.1 -0.9,-2 -2,-2zM11,8h2v2h-2L11,8zM11,11h2v2h-2v-2zM8,8h2v2L8,10L8,8zM8,11h2v2L8,13v-2zM7,13L5,13v-2h2v2zM7,10L5,10L5,8h2v2zM16,17L8,17v-2h8v2zM16,13h-2v-2h2v2zM16,10h-2L14,8h2v2zM19,13h-2v-2h2v2zM19,10h-2L17,8h2v2z"/>
+</vector>
\ No newline at end of file
diff --git a/packages/SettingsLib/res/layout/settings_with_drawer.xml b/packages/SettingsLib/res/layout/settings_with_drawer.xml
index 67296a6..a68a44e 100644
--- a/packages/SettingsLib/res/layout/settings_with_drawer.xml
+++ b/packages/SettingsLib/res/layout/settings_with_drawer.xml
@@ -41,6 +41,11 @@
                 android:background="?android:attr/colorPrimary" />
         </FrameLayout>
         <FrameLayout
+            android:id="@+id/content_header_container"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            style="?android:attr/actionBarStyle" />
+        <FrameLayout
             android:id="@+id/content_frame"
             android:layout_width="match_parent"
             android:layout_height="fill_parent"
diff --git a/packages/SettingsLib/res/layout/usage_view.xml b/packages/SettingsLib/res/layout/usage_view.xml
index aa1a046..1d56668 100644
--- a/packages/SettingsLib/res/layout/usage_view.xml
+++ b/packages/SettingsLib/res/layout/usage_view.xml
@@ -71,9 +71,11 @@
         android:id="@+id/bottom_label_group"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:paddingStart="@dimen/usage_graph_labels_width"
         android:orientation="horizontal">
-
+        <Space
+            android:id="@+id/bottom_label_space"
+            android:layout_width="@dimen/usage_graph_labels_width"
+            android:layout_height="wrap_content"/>
         <include android:id="@+id/label_start"
             layout="@layout/usage_side_label" />
 
diff --git a/packages/SettingsLib/res/values/arrays.xml b/packages/SettingsLib/res/values/arrays.xml
index a49fe69..5b11303 100644
--- a/packages/SettingsLib/res/values/arrays.xml
+++ b/packages/SettingsLib/res/values/arrays.xml
@@ -386,4 +386,12 @@
         <item>3</item>
     </string-array>
 
+    <!-- IDs for each color mode. The values must match the corresponding constants in
+         android.view.Display -->
+    <integer-array name="color_mode_ids">
+        <item>0</item>
+        <item>-1</item>
+        <item>7</item>
+    </integer-array>
+
 </resources>
diff --git a/packages/SettingsLib/res/values/colors.xml b/packages/SettingsLib/res/values/colors.xml
index 796273d..02b7ea6 100644
--- a/packages/SettingsLib/res/values/colors.xml
+++ b/packages/SettingsLib/res/values/colors.xml
@@ -17,5 +17,5 @@
 <resources>
     <color name="disabled_text_color">#66000000</color> <!-- 38% black -->
 
-    <color name="usage_graph_dots">#455A64</color>
+    <color name="usage_graph_dots">@*android:color/tertiary_device_default_settings</color>
 </resources>
diff --git a/packages/SettingsLib/res/values/config.xml b/packages/SettingsLib/res/values/config.xml
index 299a5b7..e2e721c 100755
--- a/packages/SettingsLib/res/values/config.xml
+++ b/packages/SettingsLib/res/values/config.xml
@@ -19,4 +19,25 @@
 <resources>
     <!-- Configuration for automotive -->
     <bool name="enable_pbap_pce_profile">false</bool>
-</resources>
+
+    <!-- Default data warning level in mb -->
+    <integer name="default_data_warning_level_mb">2048</integer>
+
+    <!-- Whether to send a custom package name with the PSD. translatable="false"-->
+    <bool name="config_sendPackageName">true</bool>
+
+    <!-- Name for the set of keys associating package names -->
+    <string name="config_helpPackageNameKey" translatable="false"></string>
+
+    <!-- Name for the set of values of package names -->
+    <string name="config_helpPackageNameValue" translatable="false"></string>
+
+    <!-- Intent key for the package name keys -->
+    <string name="config_helpIntentExtraKey" translatable="false"></string>
+
+    <!-- Intent key for package name values -->
+    <string name="config_helpIntentNameKey" translatable="false"></string>
+
+    <!-- The apps that need to be hided when they are disabled -->
+    <string-array name="config_hideWhenDisabled_packageNames"></string-array>
+</resources>
\ No newline at end of file
diff --git a/packages/SettingsLib/res/values/strings.xml b/packages/SettingsLib/res/values/strings.xml
index 6cc5beb..7d9cc53 100644
--- a/packages/SettingsLib/res/values/strings.xml
+++ b/packages/SettingsLib/res/values/strings.xml
@@ -64,8 +64,10 @@
     <string name="wifi_disabled_password_failure">Authentication problem</string>
     <!-- Summary for the remembered network but currently not in range. -->
     <string name="wifi_not_in_range">Not in range</string>
+    <!-- Summary for the network but no internet connection was detected. -->
+    <string name="wifi_no_internet_no_reconnect">No Internet Access Detected, won\'t automatically reconnect.</string>
     <!-- Summary for the remembered network but no internet connection was detected. -->
-    <string name="wifi_no_internet">No Internet Access Detected, won\'t automatically reconnect.</string>
+    <string name="wifi_no_internet">No Internet Access.</string>
     <!-- Summary for saved networks -->
     <string name="saved_network">Saved by <xliff:g id="name">%1$s</xliff:g></string>
 
@@ -456,8 +458,6 @@
     <string name="allow_mock_location_summary">Allow mock locations</string>
     <!-- Setting Checkbox title whether to enable view attribute inspection -->
     <string name="debug_view_attributes">Enable view attribute inspection</string>
-    <!-- Setting Checkbox summary whether to use DHCP client from Lollipop (Android 5.0) [CHAR LIMIT=130] -->
-    <string name="legacy_dhcp_client_summary">Use the DHCP client from Lollipop instead of the new Android DHCP client.</string>
     <string name="mobile_data_always_on_summary">Always keep mobile data active, even when Wi\u2011Fi is active (for fast network switching).</string>
     <!-- Title of warning dialog about the implications of enabling USB debugging -->
     <string name="adb_warning_title">Allow USB debugging?</string>
@@ -865,4 +865,7 @@
     <!-- Label for Help and feedback menu item -->
     <string name="help_feedback_label">Help &amp; feedback</string>
 
+    <!-- Content description for drawer menu button [CHAR_LIMIT=30]-->
+    <string name="content_description_menu_button">Menu</string>
+
 </resources>
diff --git a/packages/SettingsLib/src/com/android/settingslib/DeviceInfoUtils.java b/packages/SettingsLib/src/com/android/settingslib/DeviceInfoUtils.java
index ff1c866..b04948b 100644
--- a/packages/SettingsLib/src/com/android/settingslib/DeviceInfoUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/DeviceInfoUtils.java
@@ -22,6 +22,9 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.os.Build;
+import android.telephony.PhoneNumberUtils;
+import android.telephony.SubscriptionInfo;
+import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.text.format.DateFormat;
 import android.util.Log;
@@ -37,6 +40,8 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import static android.content.Context.TELEPHONY_SERVICE;
+
 public class DeviceInfoUtils {
     private static final String TAG = "DeviceInfoUtils";
 
@@ -169,4 +174,40 @@
         }
     }
 
+    public static String getFormattedPhoneNumber(Context context, SubscriptionInfo subscriptionInfo) {
+        String formattedNumber = null;
+        if (subscriptionInfo != null) {
+            final TelephonyManager telephonyManager =
+                    (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
+            final String rawNumber =
+                    telephonyManager.getLine1Number(subscriptionInfo.getSubscriptionId());
+            if (!TextUtils.isEmpty(rawNumber)) {
+                formattedNumber = PhoneNumberUtils.formatNumber(rawNumber);
+            }
+
+        }
+        return formattedNumber;
+    }
+
+    public static String getFormattedPhoneNumbers(Context context,
+            List<SubscriptionInfo> subscriptionInfo) {
+        StringBuilder sb = new StringBuilder();
+        if (subscriptionInfo != null) {
+            final TelephonyManager telephonyManager =
+                    (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
+            final int count = subscriptionInfo.size();
+            for (int i = 0; i < count; i++) {
+                final String rawNumber = telephonyManager.getLine1Number(
+                        subscriptionInfo.get(i).getSubscriptionId());
+                if (!TextUtils.isEmpty(rawNumber)) {
+                    sb.append(PhoneNumberUtils.formatNumber(rawNumber));
+                    if (i < count - 1) {
+                        sb.append("\n");
+                    }
+                }
+            }
+        }
+        return sb.toString();
+    }
+
 }
diff --git a/packages/SettingsLib/src/com/android/settingslib/HelpUtils.java b/packages/SettingsLib/src/com/android/settingslib/HelpUtils.java
index 83a2123..381f903 100644
--- a/packages/SettingsLib/src/com/android/settingslib/HelpUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/HelpUtils.java
@@ -23,7 +23,9 @@
 import android.content.Intent;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.Resources;
 import android.content.res.Resources.Theme;
+import android.content.res.TypedArray;
 import android.net.Uri;
 import android.provider.Settings.Global;
 import android.text.TextUtils;
@@ -32,6 +34,8 @@
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.MenuItem.OnMenuItemClickListener;
+import com.android.internal.logging.MetricsLogger;
+import com.android.internal.logging.MetricsProto.MetricsEvent;
 
 import java.net.URISyntaxException;
 import java.util.Locale;
@@ -110,6 +114,9 @@
                 helpMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                     @Override
                     public boolean onMenuItemClick(MenuItem item) {
+                        MetricsLogger.action(activity,
+                            MetricsEvent.ACTION_SETTING_HELP_AND_FEEDBACK,
+                            intent.getStringExtra(EXTRA_CONTEXT));
                         try {
                             activity.startActivityForResult(intent, 0);
                         } catch (ActivityNotFoundException exc) {
@@ -139,7 +146,7 @@
         try {
             Intent intent = Intent.parseUri(helpUriString,
                     Intent.URI_ANDROID_APP_SCHEME | Intent.URI_INTENT_SCHEME);
-            addIntentParameters(context, intent, backupContext);
+            addIntentParameters(context, intent, backupContext, true /* sendPackageName */);
             ComponentName component = intent.resolveActivity(context.getPackageManager());
             if (component != null) {
                 return intent;
@@ -163,16 +170,32 @@
         return intent;
     }
 
-    private static void addIntentParameters(Context context, Intent intent, String backupContext) {
+    public static void addIntentParameters(Context context, Intent intent, String backupContext,
+            boolean sendPackageName) {
         if (!intent.hasExtra(EXTRA_CONTEXT)) {
             // Insert some context if none exists.
             intent.putExtra(EXTRA_CONTEXT, backupContext);
         }
+
+        Resources resources = context.getResources();
+        boolean includePackageName = resources.getBoolean(R.bool.config_sendPackageName);
+
+        if (sendPackageName && includePackageName) {
+            String[] packageNameKey =
+                    {resources.getString(R.string.config_helpPackageNameKey)};
+            String[] packageNameValue =
+                    {resources.getString(R.string.config_helpPackageNameValue)};
+            String intentExtraKey =
+                    resources.getString(R.string.config_helpIntentExtraKey);
+            String intentNameKey =
+                    resources.getString(R.string.config_helpIntentNameKey);
+            intent.putExtra(intentExtraKey, packageNameKey);
+            intent.putExtra(intentNameKey, packageNameValue);
+        }
         intent.putExtra(EXTRA_THEME, 1 /* Light, dark action bar */);
-        Theme theme = context.getTheme();
-        TypedValue typedValue = new TypedValue();
-        theme.resolveAttribute(android.R.attr.colorPrimary, typedValue, true);
-        intent.putExtra(EXTRA_PRIMARY_COLOR, context.getColor(typedValue.resourceId));
+        TypedArray array = context.obtainStyledAttributes(new int[]{android.R.attr.colorPrimary});
+        intent.putExtra(EXTRA_PRIMARY_COLOR, array.getColor(0, 0));
+        array.recycle();
     }
 
     /**
diff --git a/packages/SettingsLib/src/com/android/settingslib/Utils.java b/packages/SettingsLib/src/com/android/settingslib/Utils.java
index d98f1a4..e049079 100644
--- a/packages/SettingsLib/src/com/android/settingslib/Utils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/Utils.java
@@ -1,5 +1,6 @@
 package com.android.settingslib;
 
+import android.annotation.ColorInt;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageInfo;
@@ -8,12 +9,14 @@
 import android.content.pm.UserInfo;
 import android.content.pm.Signature;
 import android.content.res.Resources;
+import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.drawable.Drawable;
 import android.net.ConnectivityManager;
 import android.os.BatteryManager;
 import android.os.UserManager;
+import android.print.PrintManager;
 import com.android.internal.util.UserIcons;
 import com.android.settingslib.drawable.UserIconDrawable;
 
@@ -154,11 +157,19 @@
         return statusString;
     }
 
+    @ColorInt
+    public static int getColorAccent(Context context) {
+        TypedArray ta = context.obtainStyledAttributes(new int[]{android.R.attr.colorAccent});
+        @ColorInt int colorAccent = ta.getColor(0, 0);
+        ta.recycle();
+        return colorAccent;
+    }
+
     /**
      * Determine whether a package is a "system package", in which case certain things (like
      * disabling notifications or disabling the package altogether) should be disallowed.
      */
-    public static boolean isSystemPackage(PackageManager pm, PackageInfo pkg) {
+    public static boolean isSystemPackage(Resources resources, PackageManager pm, PackageInfo pkg) {
         if (sSystemSignature == null) {
             sSystemSignature = new Signature[]{ getSystemSignature(pm) };
         }
@@ -175,7 +186,9 @@
                         && sSystemSignature[0].equals(getFirstSignature(pkg)))
                 || pkg.packageName.equals(sPermissionControllerPackageName)
                 || pkg.packageName.equals(sServicesSystemSharedLibPackageName)
-                || pkg.packageName.equals(sSharedSystemSharedLibPackageName);
+                || pkg.packageName.equals(sSharedSystemSharedLibPackageName)
+                || pkg.packageName.equals(PrintManager.PRINT_SPOOLER_PACKAGE_NAME)
+                || isDeviceProvisioningPackage(resources, pkg.packageName);
     }
 
     private static Signature getFirstSignature(PackageInfo pkg) {
@@ -193,4 +206,14 @@
         }
         return null;
     }
+
+    /**
+     * Returns {@code true} if the supplied package is the device provisioning app. Otherwise,
+     * returns {@code false}.
+     */
+    public static boolean isDeviceProvisioningPackage(Resources resources, String packageName) {
+        String deviceProvisioningPackage = resources.getString(
+                com.android.internal.R.string.config_deviceProvisioningPackage);
+        return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName);
+    }
 }
diff --git a/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java b/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java
index 7392453..f0ec107 100644
--- a/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java
+++ b/packages/SettingsLib/src/com/android/settingslib/applications/ApplicationsState.java
@@ -47,6 +47,7 @@
 import android.util.SparseArray;
 
 import com.android.internal.util.ArrayUtils;
+import com.android.settingslib.R;
 
 import java.io.File;
 import java.text.Collator;
@@ -582,10 +583,10 @@
         public ArrayList<AppEntry> rebuild(AppFilter filter, Comparator<AppEntry> comparator,
                 boolean foreground) {
             synchronized (mRebuildSync) {
-                synchronized (mEntriesMap) {
+                synchronized (mRebuildingSessions) {
                     mRebuildingSessions.add(this);
                     mRebuildRequested = true;
-                    mRebuildAsync = false;
+                    mRebuildAsync = true;
                     mRebuildFilter = filter;
                     mRebuildComparator = comparator;
                     mRebuildForeground = foreground;
@@ -597,23 +598,7 @@
                     }
                 }
 
-                // We will wait for .25s for the list to be built.
-                long waitend = SystemClock.uptimeMillis()+250;
-
-                while (mRebuildResult == null) {
-                    long now = SystemClock.uptimeMillis();
-                    if (now >= waitend) {
-                        break;
-                    }
-                    try {
-                        mRebuildSync.wait(waitend - now);
-                    } catch (InterruptedException e) {
-                    }
-                }
-
-                mRebuildAsync = true;
-
-                return mRebuildResult;
+                return null;
             }
         }
 
@@ -637,7 +622,7 @@
             }
 
             if (filter != null) {
-                filter.init();
+                filter.init(mContext);
             }
 
             List<AppEntry> apps;
@@ -765,6 +750,7 @@
         static final int MSG_LOAD_ICONS = 3;
         static final int MSG_LOAD_SIZES = 4;
         static final int MSG_LOAD_LAUNCHER = 5;
+        static final int MSG_LOAD_HOME_APP = 6;
 
         boolean mRunning;
 
@@ -776,7 +762,7 @@
         public void handleMessage(Message msg) {
             // Always try rebuilding list first thing, if needed.
             ArrayList<Session> rebuildingSessions = null;
-            synchronized (mEntriesMap) {
+            synchronized (mRebuildingSessions) {
                 if (mRebuildingSessions.size() > 0) {
                     rebuildingSessions = new ArrayList<Session>(mRebuildingSessions);
                     mRebuildingSessions.clear();
@@ -833,13 +819,33 @@
                         if (!mMainHandler.hasMessages(MainHandler.MSG_LOAD_ENTRIES_COMPLETE)) {
                             mMainHandler.sendEmptyMessage(MainHandler.MSG_LOAD_ENTRIES_COMPLETE);
                         }
-                        sendEmptyMessage(MSG_LOAD_LAUNCHER);
+                        sendEmptyMessage(MSG_LOAD_HOME_APP);
                     }
                 } break;
+                case MSG_LOAD_HOME_APP: {
+                    final List<ResolveInfo> homeActivities = new ArrayList<>();
+                    mPm.getHomeActivities(homeActivities);
+                    synchronized (mEntriesMap) {
+                        final int entryCount = mEntriesMap.size();
+                        for (int i = 0; i < entryCount; i++) {
+                            if (DEBUG_LOCKING) Log.v(TAG, "MSG_LOAD_HOME_APP acquired lock");
+                            final HashMap<String, AppEntry> userEntries = mEntriesMap.valueAt(i);
+                            for (ResolveInfo activity : homeActivities) {
+                                String packageName = activity.activityInfo.packageName;
+                                AppEntry entry = userEntries.get(packageName);
+                                if (entry != null) {
+                                    entry.isHomeApp = true;
+                                }
+                            }
+                            if (DEBUG_LOCKING) Log.v(TAG, "MSG_LOAD_HOME_APP releasing lock");
+                        }
+                    }
+                    sendEmptyMessage(MSG_LOAD_LAUNCHER);
+                }
+                break;
                 case MSG_LOAD_LAUNCHER: {
                     Intent launchIntent = new Intent(Intent.ACTION_MAIN, null)
                             .addCategory(Intent.CATEGORY_LAUNCHER);
-
                     for (int i = 0; i < mEntriesMap.size(); i++) {
                         int userId = mEntriesMap.keyAt(i);
                         // If we do not specify MATCH_DIRECT_BOOT_AWARE or
@@ -1135,6 +1141,11 @@
          */
         public boolean hasLauncherEntry;
 
+        /**
+         * Whether or not it's a Home app.
+         */
+        public boolean isHomeApp;
+
         public String getNormalizedLabel() {
             if (normalizedLabel != null) {
                 return normalizedLabel;
@@ -1270,6 +1281,9 @@
 
     public interface AppFilter {
         void init();
+        default void init(Context context) {
+            init();
+        }
         boolean filterApp(AppEntry info);
     }
 
@@ -1326,6 +1340,8 @@
                 return true;
             } else if (entry.hasLauncherEntry) {
                 return true;
+            } else if ((entry.info.flags & ApplicationInfo.FLAG_SYSTEM) != 0 && entry.isHomeApp) {
+                return true;
             }
             return false;
         }
@@ -1386,6 +1402,33 @@
         }
     };
 
+    public static final AppFilter FILTER_NOT_HIDE = new AppFilter() {
+        private String[] mHidePackageNames;
+
+        public void init(Context context) {
+            mHidePackageNames = context.getResources()
+                .getStringArray(R.array.config_hideWhenDisabled_packageNames);
+        }
+
+        @Override
+        public void init() {
+        }
+
+        @Override
+        public boolean filterApp(AppEntry entry) {
+            if (ArrayUtils.contains(mHidePackageNames, entry.info.packageName)) {
+                if (!entry.info.enabled) {
+                    return false;
+                } else if (entry.info.enabledSetting ==
+                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
+                    return false;
+                }
+            }
+
+            return true;
+        }
+    };
+
     public static class VolumeFilter implements AppFilter {
         private final String mVolumeUuid;
 
@@ -1413,6 +1456,12 @@
         }
 
         @Override
+        public void init(Context context) {
+            mFirstFilter.init(context);
+            mSecondFilter.init(context);
+        }
+
+        @Override
         public void init() {
             mFirstFilter.init();
             mSecondFilter.init();
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
index 281f1db..52e686c9b 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/CachedBluetoothDevice.java
@@ -803,7 +803,9 @@
             // No separate prompt is displayed after pairing.
             if (getPhonebookPermissionChoice() == CachedBluetoothDevice.ACCESS_UNKNOWN) {
                 if (mDevice.getBluetoothClass().getDeviceClass()
-                        == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE) {
+                        == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
+                    mDevice.getBluetoothClass().getDeviceClass()
+                        == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
                     setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_ALLOWED);
                 } else {
                     setPhonebookPermissionChoice(CachedBluetoothDevice.ACCESS_REJECTED);
diff --git a/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageMeasurement.java b/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageMeasurement.java
index 0e3e0d5..5c577f8 100644
--- a/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageMeasurement.java
+++ b/packages/SettingsLib/src/com/android/settingslib/deviceinfo/StorageMeasurement.java
@@ -126,6 +126,13 @@
          * internal storage. Key is {@link UserHandle}.
          */
         public SparseLongArray usersSize = new SparseLongArray();
+
+        @Override
+        public String toString() {
+            return "MeasurementDetails: [totalSize: " + totalSize + " availSize: " + availSize
+                    + " cacheSize: " + cacheSize + " mediaSize: " + mediaSize
+                    + " miscSize: " + miscSize + "usersSize: " + usersSize + "]";
+        }
     }
 
     public interface MeasurementReceiver {
@@ -435,7 +442,7 @@
     private static long getDirectorySize(IMediaContainerService imcs, File path) {
         try {
             final long size = imcs.calculateDirectorySize(path.toString());
-            Log.d(TAG, "getDirectorySize(" + path + ") returned " + size);
+            if (LOGV) Log.v(TAG, "getDirectorySize(" + path + ") returned " + size);
             return size;
         } catch (Exception e) {
             Log.w(TAG, "Could not read memory from default container service for " + path, e);
diff --git a/packages/SettingsLib/src/com/android/settingslib/display/DisplayDensityUtils.java b/packages/SettingsLib/src/com/android/settingslib/display/DisplayDensityUtils.java
index 78d7c56..af8fd4c 100644
--- a/packages/SettingsLib/src/com/android/settingslib/display/DisplayDensityUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/display/DisplayDensityUtils.java
@@ -20,8 +20,10 @@
 
 import android.content.Context;
 import android.content.res.Resources;
+import android.hardware.display.DisplayManager;
 import android.os.AsyncTask;
 import android.os.RemoteException;
+import android.os.UserHandle;
 import android.util.DisplayMetrics;
 import android.util.Log;
 import android.util.MathUtils;
@@ -95,7 +97,9 @@
         }
 
         final Resources res = context.getResources();
-        final DisplayMetrics metrics = res.getDisplayMetrics();
+        final DisplayMetrics metrics = new DisplayMetrics();
+        context.getDisplay().getRealMetrics(metrics);
+
         final int currentDensity = metrics.densityDpi;
         int currentDensityIndex = -1;
 
@@ -204,39 +208,41 @@
 
     /**
      * Asynchronously applies display density changes to the specified display.
+     * <p>
+     * The change will be applied to the user specified by the value of
+     * {@link UserHandle#myUserId()} at the time the method is called.
      *
      * @param displayId the identifier of the display to modify
      */
     public static void clearForcedDisplayDensity(final int displayId) {
-        AsyncTask.execute(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
-                    wm.clearForcedDisplayDensity(displayId);
-                } catch (RemoteException exc) {
-                    Log.w(LOG_TAG, "Unable to clear forced display density setting");
-                }
+        final int userId = UserHandle.myUserId();
+        AsyncTask.execute(() -> {
+            try {
+                final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
+                wm.clearForcedDisplayDensityForUser(displayId, userId);
+            } catch (RemoteException exc) {
+                Log.w(LOG_TAG, "Unable to clear forced display density setting");
             }
         });
     }
 
     /**
      * Asynchronously applies display density changes to the specified display.
+     * <p>
+     * The change will be applied to the user specified by the value of
+     * {@link UserHandle#myUserId()} at the time the method is called.
      *
      * @param displayId the identifier of the display to modify
      * @param density the density to force for the specified display
      */
     public static void setForcedDisplayDensity(final int displayId, final int density) {
-        AsyncTask.execute(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
-                    wm.setForcedDisplayDensity(displayId, density);
-                } catch (RemoteException exc) {
-                    Log.w(LOG_TAG, "Unable to save forced display density setting");
-                }
+        final int userId = UserHandle.myUserId();
+        AsyncTask.execute(() -> {
+            try {
+                final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
+                wm.setForcedDisplayDensityForUser(displayId, density, userId);
+            } catch (RemoteException exc) {
+                Log.w(LOG_TAG, "Unable to save forced display density setting");
             }
         });
     }
diff --git a/packages/SettingsLib/src/com/android/settingslib/drawer/SettingsDrawerActivity.java b/packages/SettingsLib/src/com/android/settingslib/drawer/SettingsDrawerActivity.java
index 37e3c53..e6e0243 100644
--- a/packages/SettingsLib/src/com/android/settingslib/drawer/SettingsDrawerActivity.java
+++ b/packages/SettingsLib/src/com/android/settingslib/drawer/SettingsDrawerActivity.java
@@ -28,8 +28,11 @@
 import android.content.res.TypedArray;
 import android.os.AsyncTask;
 import android.os.Bundle;
+import android.os.UserHandle;
+import android.os.UserManager;
 import android.provider.Settings;
 import android.support.v4.widget.DrawerLayout;
+import android.text.TextUtils;
 import android.util.ArraySet;
 import android.util.Log;
 import android.util.Pair;
@@ -41,8 +44,10 @@
 import android.view.Window;
 import android.view.WindowManager.LayoutParams;
 import android.widget.AdapterView;
+import android.widget.FrameLayout;
 import android.widget.ListView;
 import android.widget.Toolbar;
+
 import com.android.settingslib.R;
 import com.android.settingslib.applications.InterestingConfigChanges;
 
@@ -54,6 +59,7 @@
 
     protected static final boolean DEBUG_TIMING = false;
     private static final String TAG = "SettingsDrawerActivity";
+    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
 
     public static final String EXTRA_SHOW_MENU = "show_drawer_menu";
 
@@ -68,8 +74,10 @@
     private final List<CategoryListener> mCategoryListeners = new ArrayList<>();
 
     private SettingsDrawerAdapter mDrawerAdapter;
+    private FrameLayout mContentHeaderContainer;
     private DrawerLayout mDrawerLayout;
     private boolean mShowingMenu;
+    private UserManager mUserManager;
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
@@ -84,6 +92,7 @@
             requestWindowFeature(Window.FEATURE_NO_TITLE);
         }
         super.setContentView(R.layout.settings_with_drawer);
+        mContentHeaderContainer = (FrameLayout) findViewById(R.id.content_header_container);
         mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
         if (mDrawerLayout == null) {
             return;
@@ -104,8 +113,10 @@
             public void onItemClick(android.widget.AdapterView<?> parent, View view, int position,
                     long id) {
                 onTileClicked(mDrawerAdapter.getTile(position));
-            };
+            }
         });
+
+        mUserManager = UserManager.get(this);
         if (DEBUG_TIMING) Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime)
                 + " ms");
     }
@@ -134,8 +145,16 @@
 
             new CategoriesUpdater().execute();
         }
-        if (getIntent() != null && getIntent().getBooleanExtra(EXTRA_SHOW_MENU, false)) {
-            showMenuIcon();
+        final Intent intent = getIntent();
+        if (intent != null) {
+            if (intent.hasExtra(EXTRA_SHOW_MENU)) {
+                if (intent.getBooleanExtra(EXTRA_SHOW_MENU, false)) {
+                    // Intent explicitly set to show menu.
+                    showMenuIcon();
+                }
+            } else if (isTopLevelTile(intent)) {
+                showMenuIcon();
+            }
         }
     }
 
@@ -148,6 +167,30 @@
         super.onPause();
     }
 
+    private boolean isTopLevelTile(Intent intent) {
+        final ComponentName componentName = intent.getComponent();
+        if (componentName == null) {
+            return false;
+        }
+        // Look for a tile that has the same component as incoming intent
+        final List<DashboardCategory> categories = getDashboardCategories();
+        for (DashboardCategory category : categories) {
+            for (Tile tile : category.tiles) {
+                if (TextUtils.equals(tile.intent.getComponent().getClassName(),
+                        componentName.getClassName())) {
+                    if (DEBUG) {
+                        Log.d(TAG, "intent is for top level tile: " + tile.title);
+                    }
+                    return true;
+                }
+            }
+        }
+        if (DEBUG) {
+            Log.d(TAG, "Intent is not for top level settings " + intent);
+        }
+        return false;
+    }
+
     public void addCategoryListener(CategoryListener listener) {
         mCategoryListeners.add(listener);
     }
@@ -180,6 +223,13 @@
         }
     }
 
+    public void setContentHeaderView(View headerView) {
+        mContentHeaderContainer.removeAllViews();
+        if (headerView != null) {
+            mContentHeaderContainer.addView(headerView);
+        }
+    }
+
     @Override
     public void setContentView(@LayoutRes int layoutResID) {
         final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame);
@@ -215,6 +265,7 @@
     public void showMenuIcon() {
         mShowingMenu = true;
         getActionBar().setHomeAsUpIndicator(R.drawable.ic_menu);
+        getActionBar().setHomeActionContentDescription(R.string.content_description_menu_button);
         getActionBar().setDisplayHomeAsUpEnabled(true);
     }
 
@@ -245,6 +296,7 @@
             return true;
         }
         try {
+            updateUserHandlesIfNeeded(tile);
             int numUserHandles = tile.userHandle.size();
             if (numUserHandles > 1) {
                 ProfileSelectDialog.show(getFragmentManager(), tile);
@@ -266,12 +318,32 @@
         return true;
     }
 
+    private void updateUserHandlesIfNeeded(Tile tile) {
+        List<UserHandle> userHandles = tile.userHandle;
+
+        for (int i = userHandles.size() - 1; i >= 0; i--) {
+            if (mUserManager.getUserInfo(userHandles.get(i).getIdentifier()) == null) {
+                if (DEBUG) {
+                    Log.d(TAG, "Delete the user: " + userHandles.get(i).getIdentifier());
+                }
+                userHandles.remove(i);
+            }
+        }
+    }
+
     protected void onTileClicked(Tile tile) {
         if (openTile(tile)) {
             finish();
         }
     }
 
+    public HashMap<Pair<String, String>, Tile> getTileCache() {
+        if (sTileCache == null) {
+            getDashboardCategories();
+        }
+        return sTileCache;
+    }
+
     public void onProfileTileOpen() {
         finish();
     }
diff --git a/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java b/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java
index 1664c89..e70cc29 100644
--- a/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java
@@ -307,7 +307,7 @@
         return false;
     }
 
-    private static final Comparator<Tile> TILE_COMPARATOR =
+    public static final Comparator<Tile> TILE_COMPARATOR =
             new Comparator<Tile>() {
         @Override
         public int compare(Tile lhs, Tile rhs) {
diff --git a/packages/SettingsLib/src/com/android/settingslib/graph/UsageView.java b/packages/SettingsLib/src/com/android/settingslib/graph/UsageView.java
index ee1821d..c6a45bc 100644
--- a/packages/SettingsLib/src/com/android/settingslib/graph/UsageView.java
+++ b/packages/SettingsLib/src/com/android/settingslib/graph/UsageView.java
@@ -71,10 +71,11 @@
                 layout.addView(labels);
                 // Set gravity.
                 labels.setGravity(Gravity.END);
-                // Swap the bottom label padding
+                // Swap the bottom space order.
                 LinearLayout bottomLabels = (LinearLayout) findViewById(R.id.bottom_label_group);
-                bottomLabels.setPadding(bottomLabels.getPaddingRight(), bottomLabels.getPaddingTop(),
-                        bottomLabels.getPaddingLeft(), bottomLabels.getPaddingBottom());
+                View bottomSpace = bottomLabels.findViewById(R.id.bottom_label_space);
+                bottomLabels.removeView(bottomSpace);
+                bottomLabels.addView(bottomSpace);
             } else if (gravity != Gravity.START) {
                 throw new IllegalArgumentException("Unsupported gravity " + gravity);
             }
diff --git a/packages/SettingsLib/src/com/android/settingslib/net/DataUsageController.java b/packages/SettingsLib/src/com/android/settingslib/net/DataUsageController.java
index e53dd2f..994ea88 100644
--- a/packages/SettingsLib/src/com/android/settingslib/net/DataUsageController.java
+++ b/packages/SettingsLib/src/com/android/settingslib/net/DataUsageController.java
@@ -32,6 +32,8 @@
 import android.text.format.Time;
 import android.util.Log;
 
+import com.android.settingslib.R;
+
 import java.util.Date;
 import java.util.Locale;
 
@@ -41,12 +43,12 @@
 import static android.telephony.TelephonyManager.SIM_STATE_READY;
 import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
 import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
+import static android.net.TrafficStats.MB_IN_BYTES;
 
 public class DataUsageController {
+
     private static final String TAG = "DataUsageController";
     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
-
-    public static final long DEFAULT_WARNING_LEVEL = 2L * 1024 * 1024 * 1024;
     private static final int FIELDS = FIELD_RX_BYTES | FIELD_TX_BYTES;
     private static final StringBuilder PERIOD_BUILDER = new StringBuilder(50);
     private static final java.util.Formatter PERIOD_FORMATTER = new java.util.Formatter(
@@ -75,6 +77,14 @@
         mNetworkController = networkController;
     }
 
+    /**
+     * Returns the default warning level in bytes.
+     */
+    public long getDefaultWarningLevel() {
+        return MB_IN_BYTES
+                * mContext.getResources().getInteger(R.integer.default_data_warning_level_mb);
+    }
+
     private INetworkStatsSession getSession() {
         if (mSession == null) {
             try {
@@ -169,7 +179,7 @@
                 usage.limitLevel = policy.limitBytes > 0 ? policy.limitBytes : 0;
                 usage.warningLevel = policy.warningBytes > 0 ? policy.warningBytes : 0;
             } else {
-                usage.warningLevel = DEFAULT_WARNING_LEVEL;
+                usage.warningLevel = getDefaultWarningLevel();
             }
             if (usage != null && mNetworkController != null) {
                 usage.carrier = mNetworkController.getMobileDataNetworkName();
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
index 202e066..89d32f0 100644
--- a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
+++ b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPoint.java
@@ -426,7 +426,10 @@
             String format = mContext.getString(R.string.available_via_passpoint);
             summary.append(String.format(format, config.providerFriendlyName));
         } else if (config != null && config.hasNoInternetAccess()) {
-            summary.append(mContext.getString(R.string.wifi_no_internet));
+            int messageID = config.getNetworkSelectionStatus().isNetworkPermanentlyDisabled()
+                    ? R.string.wifi_no_internet_no_reconnect
+                    : R.string.wifi_no_internet;
+            summary.append(mContext.getString(messageID));
         } else if (config != null && !config.getNetworkSelectionStatus().isNetworkEnabled()) {
             WifiConfiguration.NetworkSelectionStatus networkStatus =
                     config.getNetworkSelectionStatus();
diff --git a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPointPreference.java b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPointPreference.java
index 284827b..aae9cf6 100644
--- a/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPointPreference.java
+++ b/packages/SettingsLib/src/com/android/settingslib/wifi/AccessPointPreference.java
@@ -29,7 +29,6 @@
 import android.util.AttributeSet;
 import android.util.SparseArray;
 import android.widget.TextView;
-
 import com.android.settingslib.R;
 
 public class AccessPointPreference extends Preference {
@@ -44,13 +43,14 @@
     private final StateListDrawable mWifiSld;
     private final int mBadgePadding;
     private final UserBadgeCache mBadgeCache;
-
     private TextView mTitleView;
+
     private boolean mForSavedNetworks = false;
     private AccessPoint mAccessPoint;
     private Drawable mBadge;
     private int mLevel;
     private CharSequence mContentDescription;
+    private int mDefaultIconResId;
 
     static final int[] WIFI_CONNECTION_STRENGTH = {
             R.string.accessibility_wifi_one_bar,
@@ -85,6 +85,24 @@
         refresh();
     }
 
+    public AccessPointPreference(AccessPoint accessPoint, Context context, UserBadgeCache cache,
+            int iconResId, boolean forSavedNetworks) {
+        super(context);
+        mBadgeCache = cache;
+        mAccessPoint = accessPoint;
+        mForSavedNetworks = forSavedNetworks;
+        mAccessPoint.setTag(this);
+        mLevel = -1;
+        mDefaultIconResId = iconResId;
+
+        mWifiSld = (StateListDrawable) context.getTheme()
+                .obtainStyledAttributes(wifi_signal_attributes).getDrawable(0);
+
+        // Distance from the end of the title at which this AP's user badge should sit.
+        mBadgePadding = context.getResources()
+                .getDimensionPixelSize(R.dimen.wifi_preference_badge_padding);
+    }
+
     public AccessPoint getAccessPoint() {
         return mAccessPoint;
     }
@@ -112,7 +130,7 @@
 
     protected void updateIcon(int level, Context context) {
         if (level == -1) {
-            setIcon(null);
+            safeSetDefaultIcon();
         } else {
             if (getIcon() == null) {
                 // To avoid a drawing race condition, we first set the state (SECURE/NONE) and then
@@ -124,16 +142,24 @@
                             ? STATE_SECURED
                             : STATE_NONE);
                     Drawable drawable = mWifiSld.getCurrent();
-                    if (!mForSavedNetworks) {
+                    if (!mForSavedNetworks && drawable != null) {
                         setIcon(drawable);
-                    } else {
-                        setIcon(null);
+                        return;
                     }
                 }
+                safeSetDefaultIcon();
             }
         }
     }
 
+    private void safeSetDefaultIcon() {
+        if (mDefaultIconResId != 0) {
+            setIcon(mDefaultIconResId);
+        } else {
+            setIcon(null);
+        }
+    }
+
     protected void updateBadge(Context context) {
         WifiConfiguration config = mAccessPoint.getConfig();
         if (config != null) {
diff --git a/packages/SettingsProvider/res/values/defaults.xml b/packages/SettingsProvider/res/values/defaults.xml
index 978ca94..08bf3a7 100644
--- a/packages/SettingsProvider/res/values/defaults.xml
+++ b/packages/SettingsProvider/res/values/defaults.xml
@@ -36,7 +36,7 @@
     <fraction name="def_window_transition_scale">100%</fraction>
     <bool name="def_haptic_feedback">true</bool>
 
-    <bool name="def_bluetooth_on">false</bool>
+    <bool name="def_bluetooth_on">true</bool>
     <bool name="def_wifi_display_on">false</bool>
     <bool name="def_install_non_market_apps">false</bool>
     <bool name="def_package_verifier_enable">true</bool>
@@ -153,7 +153,10 @@
     <integer name="def_download_manager_recommended_max_bytes_over_mobile">-1</integer>
 
     <!-- Default for Settings.Secure.LONG_PRESS_TIMEOUT_MILLIS -->
-    <integer name="def_long_press_timeout_millis">500</integer>
+    <integer name="def_long_press_timeout_millis">400</integer>
+
+    <!-- Default for Settings.Secure.MULTI_PRESS_TIMEOUT -->
+    <integer name="def_multi_press_timeout_millis">300</integer>
 
     <!-- Default for Settings.Secure.SHOW_IME_WITH_HARD_KEYBOARD -->
     <bool name="def_show_ime_with_hard_keyboard">false</bool>
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/EventLogTags.logtags b/packages/SettingsProvider/src/com/android/providers/settings/EventLogTags.logtags
index 298d776..7eff16b 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/EventLogTags.logtags
+++ b/packages/SettingsProvider/src/com/android/providers/settings/EventLogTags.logtags
@@ -1,5 +1,6 @@
-# See system/core/logcat/e for a description of the format of this file.
+# See system/core/logcat/event.logtags for a description of the format of this file.
 
 option java_package com.android.providers.settings;
 
 52100 unsupported_settings_query (uri|3),(selection|3),(whereArgs|3)
+52101 persist_setting_error (message|3)
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
index b79ce80..bf48e5d 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
@@ -247,7 +247,6 @@
                 return Settings.Secure.getInt(mContext.getContentResolver(), name, 0) != 0;
             case Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES:
             case Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES:
-            case Settings.Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX:
             case Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER:
             case Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE:
                 return !TextUtils.isEmpty(Settings.Secure.getString(
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index 90eade4..e66e963 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -114,7 +114,7 @@
 public class SettingsProvider extends ContentProvider {
     private static final boolean DEBUG = false;
 
-    private static final boolean DROP_DATABASE_ON_MIGRATION = !Build.IS_DEBUGGABLE;
+    private static final boolean DROP_DATABASE_ON_MIGRATION = true;
 
     private static final String LOG_TAG = "SettingsProvider";
 
@@ -544,7 +544,7 @@
                 final int userCount = users.size();
                 for (int i = 0; i < userCount; i++) {
                     UserInfo user = users.get(i);
-                    dumpForUser(user.id, pw);
+                    dumpForUserLocked(user.id, pw);
                 }
             } finally {
                 Binder.restoreCallingIdentity(identity);
@@ -552,12 +552,16 @@
         }
     }
 
-    private void dumpForUser(int userId, PrintWriter pw) {
+    private void dumpForUserLocked(int userId, PrintWriter pw) {
         if (userId == UserHandle.USER_SYSTEM) {
             pw.println("GLOBAL SETTINGS (user " + userId + ")");
             Cursor globalCursor = getAllGlobalSettings(ALL_COLUMNS);
             dumpSettings(globalCursor, pw);
             pw.println();
+
+            SettingsState globalSettings = mSettingsRegistry.getSettingsLocked(
+                    SETTINGS_TYPE_GLOBAL, UserHandle.USER_SYSTEM);
+            globalSettings.dumpHistoricalOperations(pw);
         }
 
         pw.println("SECURE SETTINGS (user " + userId + ")");
@@ -565,10 +569,18 @@
         dumpSettings(secureCursor, pw);
         pw.println();
 
+        SettingsState secureSettings = mSettingsRegistry.getSettingsLocked(
+                SETTINGS_TYPE_SECURE, userId);
+        secureSettings.dumpHistoricalOperations(pw);
+
         pw.println("SYSTEM SETTINGS (user " + userId + ")");
         Cursor systemCursor = getAllSystemSettings(userId, ALL_COLUMNS);
         dumpSettings(systemCursor, pw);
         pw.println();
+
+        SettingsState systemSettings = mSettingsRegistry.getSettingsLocked(
+                SETTINGS_TYPE_SYSTEM, userId);
+        systemSettings.dumpHistoricalOperations(pw);
     }
 
     private void dumpSettings(Cursor cursor, PrintWriter pw) {
@@ -2090,7 +2102,7 @@
         }
 
         private final class UpgradeController {
-            private static final int SETTINGS_VERSION = 127;
+            private static final int SETTINGS_VERSION = 131;
 
             private final int mUserId;
 
@@ -2130,6 +2142,12 @@
 
                     // Now upgrade should work fine.
                     onUpgradeLocked(mUserId, oldVersion, newVersion);
+
+                    // Make a note what happened, so we don't wonder why data was lost
+                    String reason = "Settings rebuilt! Current version: "
+                            + curVersion + " while expected: " + newVersion;
+                    getGlobalSettingsLocked().insertSettingLocked(
+                            Settings.Global.DATABASE_DOWNGRADE_REASON, reason, "android");
                 }
 
                 // Set the global settings version if owner.
@@ -2345,6 +2363,84 @@
                     currentVersion = 127;
                 }
 
+                if (currentVersion == 127) {
+                    // version 127 is no longer used.
+                    currentVersion = 128;
+                }
+
+                if (currentVersion == 128) {
+                    // Version 128: Allow OEMs to grant DND access to default apps. Note that
+                    // the new apps are appended to the list of already approved apps.
+                    final SettingsState systemSecureSettings =
+                            getSecureSettingsLocked(userId);
+
+                    final Setting policyAccess = systemSecureSettings.getSettingLocked(
+                            Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES);
+                    String defaultPolicyAccess = getContext().getResources().getString(
+                            com.android.internal.R.string.config_defaultDndAccessPackages);
+                    if (!TextUtils.isEmpty(defaultPolicyAccess)) {
+                        if (policyAccess.isNull()) {
+                            systemSecureSettings.insertSettingLocked(
+                                    Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
+                                    defaultPolicyAccess,
+                                    SettingsState.SYSTEM_PACKAGE_NAME);
+                        } else {
+                            StringBuilder currentSetting =
+                                    new StringBuilder(policyAccess.getValue());
+                            currentSetting.append(":");
+                            currentSetting.append(defaultPolicyAccess);
+                            systemSecureSettings.updateSettingLocked(
+                                    Settings.Secure.ENABLED_NOTIFICATION_POLICY_ACCESS_PACKAGES,
+                                    currentSetting.toString(),
+                                    SettingsState.SYSTEM_PACKAGE_NAME);
+                        }
+                    }
+
+                    currentVersion = 129;
+                }
+
+                if (currentVersion == 129) {
+                    // default longpress timeout changed from 500 to 400. If unchanged from the old
+                    // default, update to the new default.
+                    final SettingsState systemSecureSettings =
+                            getSecureSettingsLocked(userId);
+                    final String oldValue = systemSecureSettings.getSettingLocked(
+                            Settings.Secure.LONG_PRESS_TIMEOUT).getValue();
+                    if (TextUtils.equals("500", oldValue)) {
+                        systemSecureSettings.insertSettingLocked(
+                                Settings.Secure.LONG_PRESS_TIMEOUT,
+                                String.valueOf(getContext().getResources().getInteger(
+                                        R.integer.def_long_press_timeout_millis)),
+                                SettingsState.SYSTEM_PACKAGE_NAME);
+                    }
+                    currentVersion = 130;
+                }
+
+                if (currentVersion == 130) {
+                    // Initialize new multi-press timeout to default value
+                    final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
+                    final String oldValue = systemSecureSettings.getSettingLocked(
+                            Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
+                    if (TextUtils.equals(null, oldValue)) {
+                        systemSecureSettings.insertSettingLocked(
+                                Settings.Secure.MULTI_PRESS_TIMEOUT,
+                                String.valueOf(getContext().getResources().getInteger(
+                                        R.integer.def_multi_press_timeout_millis)),
+                                SettingsState.SYSTEM_PACKAGE_NAME);
+                    }
+
+                    currentVersion = 131;
+                }
+
+                if (currentVersion != newVersion) {
+                    Slog.wtf("SettingsProvider", "warning: upgrading settings database to version "
+                            + newVersion + " left it at "
+                            + currentVersion + " instead; this is probably a bug", new Throwable());
+                    if (DEBUG) {
+                        throw new RuntimeException("db upgrade error");
+                    }
+                }
+
                 // vXXX: Add new settings above this point.
 
                 // Return the current version.
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsState.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsState.java
index 2167b76..832c9d9 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsState.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsState.java
@@ -16,6 +16,7 @@
 
 package com.android.providers.settings;
 
+import android.os.Build;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -26,6 +27,7 @@
 import android.util.AtomicFile;
 import android.util.Base64;
 import android.util.Slog;
+import android.util.TimeUtils;
 import android.util.Xml;
 import com.android.internal.annotations.GuardedBy;
 import libcore.io.IoUtils;
@@ -39,6 +41,7 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
@@ -60,7 +63,7 @@
 
     private static final String LOG_TAG = "SettingsState";
 
-    static final int SETTINGS_VERSOIN_NEW_ENCODING = 121;
+    static final int SETTINGS_VERSION_NEW_ENCODING = 121;
 
     private static final long WRITE_SETTINGS_DELAY_MILLIS = 200;
     private static final long MAX_WRITE_SETTINGS_DELAY_MILLIS = 2000;
@@ -93,6 +96,12 @@
     // This was used in version 120 and before.
     private static final String NULL_VALUE_OLD_STYLE = "null";
 
+    private static final int HISTORICAL_OPERATION_COUNT = 20;
+    private static final String HISTORICAL_OPERATION_UPDATE = "update";
+    private static final String HISTORICAL_OPERATION_DELETE = "delete";
+    private static final String HISTORICAL_OPERATION_PERSIST = "persist";
+    private static final String HISTORICAL_OPERATION_INITIALIZE = "initialize";
+
     private final Object mLock;
 
     private final Handler mHandler;
@@ -117,6 +126,9 @@
     };
 
     @GuardedBy("mLock")
+    private final List<HistoricalOperation> mHistoricalOperations;
+
+    @GuardedBy("mLock")
     public final int mKey;
 
     @GuardedBy("mLock")
@@ -134,6 +146,9 @@
     @GuardedBy("mLock")
     private long mNextId;
 
+    @GuardedBy("mLock")
+    private int mNextHistoricalOpIdx;
+
     public SettingsState(Object lock, File file, int key, int maxBytesPerAppPackage,
             Looper looper) {
         // It is important that we use the same lock as the settings provider
@@ -150,6 +165,10 @@
             mMaxBytesPerAppPackage = maxBytesPerAppPackage;
             mPackageToMemoryUsage = null;
         }
+
+        mHistoricalOperations = Build.IS_DEBUGGABLE
+                ? new ArrayList<>(HISTORICAL_OPERATION_COUNT) : null;
+
         synchronized (mLock) {
             readStateSyncLocked();
         }
@@ -238,16 +257,20 @@
 
         Setting oldState = mSettings.get(name);
         String oldValue = (oldState != null) ? oldState.value : null;
+        Setting newState;
 
         if (oldState != null) {
             if (!oldState.update(value, packageName)) {
                 return false;
             }
+            newState = oldState;
         } else {
-            Setting state = new Setting(name, value, packageName);
-            mSettings.put(name, state);
+            newState = new Setting(name, value, packageName);
+            mSettings.put(name, newState);
         }
 
+        addHistoricalOperationLocked(HISTORICAL_OPERATION_UPDATE, newState);
+
         updateMemoryUsagePerPackageLocked(packageName, oldValue, value);
 
         scheduleWriteIfNeededLocked();
@@ -271,6 +294,8 @@
 
         updateMemoryUsagePerPackageLocked(oldState.packageName, oldState.value, null);
 
+        addHistoricalOperationLocked(HISTORICAL_OPERATION_DELETE, oldState);
+
         scheduleWriteIfNeededLocked();
 
         return true;
@@ -290,6 +315,51 @@
         }
     }
 
+    private void addHistoricalOperationLocked(String type, Setting setting) {
+        if (mHistoricalOperations == null) {
+            return;
+        }
+        HistoricalOperation operation = new HistoricalOperation(
+                SystemClock.elapsedRealtime(), type,
+                setting != null ? new Setting(setting) : null);
+        if (mNextHistoricalOpIdx >= mHistoricalOperations.size()) {
+            mHistoricalOperations.add(operation);
+        } else {
+            mHistoricalOperations.set(mNextHistoricalOpIdx, operation);
+        }
+        mNextHistoricalOpIdx++;
+        if (mNextHistoricalOpIdx >= HISTORICAL_OPERATION_COUNT) {
+            mNextHistoricalOpIdx = 0;
+        }
+    }
+
+    public void dumpHistoricalOperations(PrintWriter pw) {
+        synchronized (mLock) {
+            if (mHistoricalOperations == null) {
+                return;
+            }
+            pw.println("Historical operations");
+            final int operationCount = mHistoricalOperations.size();
+            for (int i = 0; i < operationCount; i++) {
+                int index = mNextHistoricalOpIdx - 1 - i;
+                if (index < 0) {
+                    index = operationCount + index;
+                }
+                HistoricalOperation operation = mHistoricalOperations.get(index);
+                pw.print(TimeUtils.formatForLogging(operation.mTimestamp));
+                pw.print(" ");
+                pw.print(operation.mOperation);
+                if (operation.mSetting != null) {
+                    pw.print("  ");
+                    pw.print(operation.mSetting);
+                }
+                pw.println();
+            }
+            pw.println();
+            pw.println();
+        }
+    }
+
     private void updateMemoryUsagePerPackageLocked(String packageName, String oldValue,
             String newValue) {
         if (mMaxBytesPerAppPackage == MAX_BYTES_PER_APP_PACKAGE_UNLIMITED) {
@@ -407,6 +477,10 @@
             serializer.endDocument();
             destination.finishWrite(out);
 
+            synchronized (mLock) {
+                addHistoricalOperationLocked(HISTORICAL_OPERATION_PERSIST, null);
+            }
+
             if (DEBUG_PERSISTENCE) {
                 Slog.i(LOG_TAG, "[PERSIST END]");
             }
@@ -435,7 +509,7 @@
 
     static void setValueAttribute(int version, XmlSerializer serializer, String value)
             throws IOException {
-        if (version >= SETTINGS_VERSOIN_NEW_ENCODING) {
+        if (version >= SETTINGS_VERSION_NEW_ENCODING) {
             if (value == null) {
                 // Null value -> No ATTR_VALUE nor ATTR_VALUE_BASE64.
             } else if (isBinary(value)) {
@@ -454,7 +528,7 @@
     }
 
     private String getValueAttribute(XmlPullParser parser) {
-        if (mVersion >= SETTINGS_VERSOIN_NEW_ENCODING) {
+        if (mVersion >= SETTINGS_VERSION_NEW_ENCODING) {
             final String value = parser.getAttributeValue(null, ATTR_VALUE);
             if (value != null) {
                 return value;
@@ -479,22 +553,26 @@
     private void readStateSyncLocked() {
         FileInputStream in;
         if (!mStatePersistFile.exists()) {
+            Slog.i(LOG_TAG, "No settings state " + mStatePersistFile);
+            addHistoricalOperationLocked(HISTORICAL_OPERATION_INITIALIZE, null);
             return;
         }
         try {
             in = new AtomicFile(mStatePersistFile).openRead();
         } catch (FileNotFoundException fnfe) {
-            Slog.i(LOG_TAG, "No settings state");
+            String message = "No settings state " + mStatePersistFile;
+            Slog.wtf(LOG_TAG, message);
+            Slog.i(LOG_TAG, message);
             return;
         }
         try {
             XmlPullParser parser = Xml.newPullParser();
             parser.setInput(in, StandardCharsets.UTF_8.name());
             parseStateLocked(parser);
-
         } catch (XmlPullParserException | IOException e) {
-            throw new IllegalStateException("Failed parsing settings file: "
-                    + mStatePersistFile , e);
+            String message = "Failed parsing settings file: " + mStatePersistFile;
+            Slog.wtf(LOG_TAG, message);
+            throw new IllegalStateException(message , e);
         } finally {
             IoUtils.closeQuietly(in);
         }
@@ -567,6 +645,19 @@
         }
     }
 
+    private class HistoricalOperation {
+        final long mTimestamp;
+        final String mOperation;
+        final Setting mSetting;
+
+        public HistoricalOperation(long timestamp,
+                String operation, Setting setting) {
+            mTimestamp = timestamp;
+            mOperation = operation;
+            mSetting = setting;
+        }
+    }
+
     class Setting {
         private String name;
         private String value;
@@ -629,6 +720,10 @@
             this.id = String.valueOf(mNextId++);
             return true;
         }
+
+        public String toString() {
+            return "Setting{name=" + value + " from " + packageName + "}";
+        }
     }
 
     /**
diff --git a/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsStateTest.java b/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsStateTest.java
index b5bd8ad..9964467 100644
--- a/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsStateTest.java
+++ b/packages/SettingsProvider/test/src/com/android/providers/settings/SettingsStateTest.java
@@ -15,7 +15,6 @@
  */
 package com.android.providers.settings;
 
-import android.os.Handler;
 import android.os.Looper;
 import android.test.AndroidTestCase;
 import android.util.Xml;
@@ -99,10 +98,10 @@
         checkWriteSingleSetting(serializer, null, null);
         checkWriteSingleSetting(serializer, CRAZY_STRING, null);
         SettingsState.writeSingleSetting(
-                SettingsState.SETTINGS_VERSOIN_NEW_ENCODING,
+                SettingsState.SETTINGS_VERSION_NEW_ENCODING,
                 serializer, null, "k", "v", "package");
         SettingsState.writeSingleSetting(
-                SettingsState.SETTINGS_VERSOIN_NEW_ENCODING,
+                SettingsState.SETTINGS_VERSION_NEW_ENCODING,
                 serializer, "1", "k", "v", null);
     }
 
@@ -115,7 +114,7 @@
             String key, String value) throws Exception {
         // Make sure the XML serializer won't crash.
         SettingsState.writeSingleSetting(
-                SettingsState.SETTINGS_VERSOIN_NEW_ENCODING,
+                SettingsState.SETTINGS_VERSION_NEW_ENCODING,
                 serializer, "1", key, value, "package");
     }
 
@@ -129,7 +128,7 @@
 
         final SettingsState ssWriter = new SettingsState(lock, file, 1,
                 SettingsState.MAX_BYTES_PER_APP_PACKAGE_UNLIMITED, Looper.getMainLooper());
-        ssWriter.setVersionLocked(SettingsState.SETTINGS_VERSOIN_NEW_ENCODING);
+        ssWriter.setVersionLocked(SettingsState.SETTINGS_VERSION_NEW_ENCODING);
 
         ssWriter.insertSettingLocked("k1", "\u0000", "package");
         ssWriter.insertSettingLocked("k2", "abc", "p2");
diff --git a/packages/Shell/Android.mk b/packages/Shell/Android.mk
index 73a0449..81ab2ff 100644
--- a/packages/Shell/Android.mk
+++ b/packages/Shell/Android.mk
@@ -12,6 +12,8 @@
 LOCAL_CERTIFICATE := platform
 LOCAL_PRIVILEGED_MODULE := true
 
+LOCAL_JACK_COVERAGE_INCLUDE_FILTER := com.android.shell.*
+
 include $(BUILD_PACKAGE)
 
 include $(LOCAL_PATH)/tests/Android.mk
diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml
index 2efefb3..227d0e9 100644
--- a/packages/Shell/AndroidManifest.xml
+++ b/packages/Shell/AndroidManifest.xml
@@ -114,6 +114,8 @@
     <uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE" />
     <!-- 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()) -->
+    <uses-permission android:name="android.permission.WAKE_LOCK" />
 
     <application android:label="@string/app_label"
                  android:defaultToDeviceProtectedStorage="true"
@@ -142,7 +144,7 @@
 
         <activity
             android:name=".BugreportWarningActivity"
-            android:theme="@android:style/Theme.Material.Light.Dialog.Alert"
+            android:theme="@android:style/Theme.DeviceDefault.Light.Dialog.Alert"
             android:finishOnCloseSystemDialogs="true"
             android:excludeFromRecents="true"
             android:exported="false" />
diff --git a/packages/Shell/res/layout/confirm_repeat.xml b/packages/Shell/res/layout/confirm_repeat.xml
index ad90af1..9f1d53e 100644
--- a/packages/Shell/res/layout/confirm_repeat.xml
+++ b/packages/Shell/res/layout/confirm_repeat.xml
@@ -18,10 +18,10 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:paddingStart="16dip"
-    android:paddingEnd="16dip"
-    android:paddingTop="8dip"
-    android:paddingBottom="16dip"
+    android:paddingStart="24dip"
+    android:paddingEnd="24dip"
+    android:paddingTop="24dip"
+    android:paddingBottom="8dip"
     android:orientation="vertical"
     android:keepScreenOn="true">
     <ScrollView
diff --git a/packages/Shell/res/values/strings.xml b/packages/Shell/res/values/strings.xml
index 3d6643d..5dfac95 100644
--- a/packages/Shell/res/values/strings.xml
+++ b/packages/Shell/res/values/strings.xml
@@ -25,8 +25,8 @@
     <!-- Content notification indicating a bugreport is being updated before it can be shared, asking the user to wait [CHAR LIMIT=50] -->
     <string name="bugreport_updating_wait">Please wait\u2026</string>
 
-    <!-- Text of notification indicating that swipe left will share the captured bugreport. [CHAR LIMIT=100] -->
-    <string name="bugreport_finished_text" product="watch">Swipe left to share your bug report</string>
+    <!-- Text of notification indicating that bugreport will appear on the phone. [CHAR LIMIT=100] -->
+    <string name="bugreport_finished_text" product="watch">The bug report will appear on the phone shortly</string>
     <!-- Text of notification indicating that tapping will share the captured bugreport. [CHAR LIMIT=100] -->
     <string name="bugreport_finished_text" product="default">Tap to share your bug report</string>
     <!-- Text of notification indicating that swipe left will share the captured bugreport, but giving user the option to wait for the screenshot. [CHAR LIMIT=100] -->
diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java
index 6bc4df7..5a75f40 100644
--- a/packages/Shell/src/com/android/shell/BugreportProgressService.java
+++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java
@@ -60,6 +60,8 @@
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.res.Configuration;
+import android.graphics.Bitmap;
+import android.hardware.display.DisplayManagerGlobal;
 import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Bundle;
@@ -78,6 +80,8 @@
 import android.util.Log;
 import android.util.Patterns;
 import android.util.SparseArray;
+import android.view.Display;
+import android.view.KeyEvent;
 import android.view.View;
 import android.view.WindowManager;
 import android.view.View.OnFocusChangeListener;
@@ -212,6 +216,8 @@
 
     private static final Bundle sNotificationBundle = new Bundle();
 
+    private boolean mIsWatch;
+
     @Override
     public void onCreate() {
         mContext = getApplicationContext();
@@ -225,6 +231,9 @@
                 Log.w(TAG, "Could not create directory " + mScreenshotsDir);
             }
         }
+        final Configuration conf = mContext.getResources().getConfiguration();
+        mIsWatch = (conf.uiMode & Configuration.UI_MODE_TYPE_MASK) ==
+                Configuration.UI_MODE_TYPE_WATCH;
     }
 
     @Override
@@ -439,56 +448,68 @@
             return;
         }
 
-        final NumberFormat nf = NumberFormat.getPercentInstance();
-        nf.setMinimumFractionDigits(2);
-        nf.setMaximumFractionDigits(2);
-        final String percentageText = nf.format((double) info.progress / info.max);
-        final Action cancelAction = new Action.Builder(null, mContext.getString(
-                com.android.internal.R.string.cancel), newCancelIntent(mContext, info)).build();
-        final Intent infoIntent = new Intent(mContext, BugreportProgressService.class);
-        infoIntent.setAction(INTENT_BUGREPORT_INFO_LAUNCH);
-        infoIntent.putExtra(EXTRA_ID, info.id);
-        final PendingIntent infoPendingIntent =
-                PendingIntent.getService(mContext, info.id, infoIntent,
-                PendingIntent.FLAG_UPDATE_CURRENT);
-        final Action infoAction = new Action.Builder(null,
-                mContext.getString(R.string.bugreport_info_action),
-                infoPendingIntent).build();
-        final Intent screenshotIntent = new Intent(mContext, BugreportProgressService.class);
-        screenshotIntent.setAction(INTENT_BUGREPORT_SCREENSHOT);
-        screenshotIntent.putExtra(EXTRA_ID, info.id);
-        PendingIntent screenshotPendingIntent = mTakingScreenshot ? null : PendingIntent
-                .getService(mContext, info.id, screenshotIntent,
-                        PendingIntent.FLAG_UPDATE_CURRENT);
-        final Action screenshotAction = new Action.Builder(null,
-                mContext.getString(R.string.bugreport_screenshot_action),
-                screenshotPendingIntent).build();
-
-        final String title = mContext.getString(R.string.bugreport_in_progress_title, info.id);
-
-        final String name =
-                info.name != null ? info.name : mContext.getString(R.string.bugreport_unnamed);
-
-        final Notification notification = newBaseNotification(mContext)
-                .setContentTitle(title)
-                .setTicker(title)
-                .setContentText(name)
-                .setProgress(info.max, info.progress, false)
-                .setOngoing(true)
-                .setContentIntent(infoPendingIntent)
-                .setActions(infoAction, screenshotAction, cancelAction)
-                .build();
-
         if (info.finished) {
             Log.w(TAG, "Not sending progress notification because bugreport has finished already ("
                     + info + ")");
             return;
         }
+
+        final NumberFormat nf = NumberFormat.getPercentInstance();
+        nf.setMinimumFractionDigits(2);
+        nf.setMaximumFractionDigits(2);
+        final String percentageText = nf.format((double) info.progress / info.max);
+
+        String title = mContext.getString(R.string.bugreport_in_progress_title, info.id);
+
+        // TODO: Remove this workaround when notification progress is implemented on Wear.
+        if (mIsWatch) {
+            nf.setMinimumFractionDigits(0);
+            nf.setMaximumFractionDigits(0);
+            final String watchPercentageText = nf.format((double) info.progress / info.max);
+            title = title + "\n" + watchPercentageText;
+        }
+
+        final String name =
+                info.name != null ? info.name : mContext.getString(R.string.bugreport_unnamed);
+
+        final Notification.Builder builder = newBaseNotification(mContext)
+                .setContentTitle(title)
+                .setTicker(title)
+                .setContentText(name)
+                .setProgress(info.max, info.progress, false)
+                .setOngoing(true);
+
+        // Wear bugreport doesn't need the bug info dialog, screenshot and cancel action.
+        if (!mIsWatch) {
+            final Action cancelAction = new Action.Builder(null, mContext.getString(
+                    com.android.internal.R.string.cancel), newCancelIntent(mContext, info)).build();
+            final Intent infoIntent = new Intent(mContext, BugreportProgressService.class);
+            infoIntent.setAction(INTENT_BUGREPORT_INFO_LAUNCH);
+            infoIntent.putExtra(EXTRA_ID, info.id);
+            final PendingIntent infoPendingIntent =
+                    PendingIntent.getService(mContext, info.id, infoIntent,
+                    PendingIntent.FLAG_UPDATE_CURRENT);
+            final Action infoAction = new Action.Builder(null,
+                    mContext.getString(R.string.bugreport_info_action),
+                    infoPendingIntent).build();
+            final Intent screenshotIntent = new Intent(mContext, BugreportProgressService.class);
+            screenshotIntent.setAction(INTENT_BUGREPORT_SCREENSHOT);
+            screenshotIntent.putExtra(EXTRA_ID, info.id);
+            PendingIntent screenshotPendingIntent = mTakingScreenshot ? null : PendingIntent
+                    .getService(mContext, info.id, screenshotIntent,
+                            PendingIntent.FLAG_UPDATE_CURRENT);
+            final Action screenshotAction = new Action.Builder(null,
+                    mContext.getString(R.string.bugreport_screenshot_action),
+                    screenshotPendingIntent).build();
+            builder.setContentIntent(infoPendingIntent)
+                .setActions(infoAction, screenshotAction, cancelAction);
+        }
+
         if (DEBUG) {
             Log.d(TAG, "Sending 'Progress' notification for id " + info.id + " (pid " + info.pid
                     + "): " + percentageText);
         }
-        sendForegroundabledNotification(info.id, notification);
+        sendForegroundabledNotification(info.id, builder.build());
     }
 
     private void sendForegroundabledNotification(int id, Notification notification) {
@@ -754,7 +775,6 @@
             }
             msg = mContext.getString(R.string.bugreport_screenshot_taken);
         } else {
-            // TODO: try again using Framework APIs instead of relying on screencap.
             msg = mContext.getString(R.string.bugreport_screenshot_failed);
             Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
         }
@@ -854,10 +874,7 @@
         // Stop running on foreground, otherwise share notification cannot be dismissed.
         stopForegroundWhenDone(id);
 
-        final Configuration conf = mContext.getResources().getConfiguration();
-        if ((conf.uiMode & Configuration.UI_MODE_TYPE_MASK) != Configuration.UI_MODE_TYPE_WATCH) {
-            triggerLocalNotification(mContext, info);
-        }
+        triggerLocalNotification(mContext, info);
     }
 
     /**
@@ -1336,22 +1353,24 @@
     /**
      * Takes a screenshot and save it to the given location.
      */
-    private static boolean takeScreenshot(Context context, String screenshotFile) {
-        final ProcessBuilder screencap = new ProcessBuilder()
-                .command("/system/bin/screencap", "-p", screenshotFile);
-        Log.d(TAG, "Taking screenshot using " + screencap.command());
-        try {
-            final int exitValue = screencap.start().waitFor();
-            if (exitValue == 0) {
+    private static boolean takeScreenshot(Context context, String path) {
+        final Bitmap bitmap = Screenshooter.takeScreenshot();
+        if (bitmap == null) {
+            return false;
+        }
+        boolean status;
+        try (final FileOutputStream fos = new FileOutputStream(path)) {
+            if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)) {
                 ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(150);
                 return true;
+            } else {
+                Log.e(TAG, "Failed to save screenshot on " + path);
             }
-            Log.e(TAG, "screencap (" + screencap.command() + ") failed: " + exitValue);
-        } catch (IOException e) {
-            Log.e(TAG, "screencap (" + screencap.command() + ") failed", e);
-        } catch (InterruptedException e) {
-            Log.w(TAG, "Thread interrupted while screencap still running");
-            Thread.currentThread().interrupt();
+        } catch (IOException e ) {
+            Log.e(TAG, "Failed to save screenshot on " + path, e);
+            return false;
+        } finally {
+            bitmap.recycle();
         }
         return false;
     }
diff --git a/packages/Shell/src/com/android/shell/BugreportStorageProvider.java b/packages/Shell/src/com/android/shell/BugreportStorageProvider.java
index 8b07599..9fd80d3 100644
--- a/packages/Shell/src/com/android/shell/BugreportStorageProvider.java
+++ b/packages/Shell/src/com/android/shell/BugreportStorageProvider.java
@@ -132,6 +132,7 @@
         if (!getFileForDocId(documentId).delete()) {
             throw new FileNotFoundException("Failed to delete: " + documentId);
         }
+        getContext().getContentResolver().notifyChange(getNotificationUri(), null);
     }
 
     // This is used by BugreportProgressService so that the notification uri shared by
diff --git a/packages/Shell/src/com/android/shell/Screenshooter.java b/packages/Shell/src/com/android/shell/Screenshooter.java
new file mode 100644
index 0000000..ce0fbbc
--- /dev/null
+++ b/packages/Shell/src/com/android/shell/Screenshooter.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.shell;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Point;
+import android.hardware.display.DisplayManagerGlobal;
+import android.os.Binder;
+import android.os.RemoteException;
+import android.util.Log;
+import android.view.Display;
+import android.view.Surface;
+import android.view.SurfaceControl;
+
+/**
+ * Helper class used to take screenshots.
+ *
+ * TODO: logic below was copied and pasted from UiAutomation; it should be refactored into a common
+ * component that could be used by both (Shell and UiAutomation).
+ */
+final class Screenshooter {
+
+    private static final String TAG = "Screenshooter";
+
+    /** Rotation constant: Freeze rotation to 0 degrees (natural orientation) */
+    public static final int ROTATION_FREEZE_0 = Surface.ROTATION_0;
+
+    /** Rotation constant: Freeze rotation to 90 degrees . */
+    public static final int ROTATION_FREEZE_90 = Surface.ROTATION_90;
+
+    /** Rotation constant: Freeze rotation to 180 degrees . */
+    public static final int ROTATION_FREEZE_180 = Surface.ROTATION_180;
+
+    /** Rotation constant: Freeze rotation to 270 degrees . */
+    public static final int ROTATION_FREEZE_270 = Surface.ROTATION_270;
+
+    /**
+     * Takes a screenshot.
+     *
+     * @return The screenshot bitmap on success, null otherwise.
+     */
+    static Bitmap takeScreenshot() {
+        Display display = DisplayManagerGlobal.getInstance()
+                .getRealDisplay(Display.DEFAULT_DISPLAY);
+        Point displaySize = new Point();
+        display.getRealSize(displaySize);
+        final int displayWidth = displaySize.x;
+        final int displayHeight = displaySize.y;
+
+        final float screenshotWidth;
+        final float screenshotHeight;
+
+        final int rotation = display.getRotation();
+        switch (rotation) {
+            case ROTATION_FREEZE_0: {
+                screenshotWidth = displayWidth;
+                screenshotHeight = displayHeight;
+            } break;
+            case ROTATION_FREEZE_90: {
+                screenshotWidth = displayHeight;
+                screenshotHeight = displayWidth;
+            } break;
+            case ROTATION_FREEZE_180: {
+                screenshotWidth = displayWidth;
+                screenshotHeight = displayHeight;
+            } break;
+            case ROTATION_FREEZE_270: {
+                screenshotWidth = displayHeight;
+                screenshotHeight = displayWidth;
+            } break;
+            default: {
+                throw new IllegalArgumentException("Invalid rotation: "
+                        + rotation);
+            }
+        }
+
+        Log.d(TAG, "Taking screenshot of dimensions " + displayWidth + " x " + displayHeight);
+        // Take the screenshot
+        Bitmap screenShot =
+                SurfaceControl.screenshot((int) screenshotWidth, (int) screenshotHeight);
+        if (screenShot == null) {
+            Log.e(TAG, "Failed to take screenshot of dimensions " + screenshotWidth + " x "
+                    + screenshotHeight);
+            return null;
+        }
+
+        // Rotate the screenshot to the current orientation
+        if (rotation != ROTATION_FREEZE_0) {
+            Bitmap unrotatedScreenShot = Bitmap.createBitmap(displayWidth, displayHeight,
+                    Bitmap.Config.ARGB_8888);
+            Canvas canvas = new Canvas(unrotatedScreenShot);
+            canvas.translate(unrotatedScreenShot.getWidth() / 2,
+                    unrotatedScreenShot.getHeight() / 2);
+            canvas.rotate(getDegreesForRotation(rotation));
+            canvas.translate(- screenshotWidth / 2, - screenshotHeight / 2);
+            canvas.drawBitmap(screenShot, 0, 0, null);
+            canvas.setBitmap(null);
+            screenShot.recycle();
+            screenShot = unrotatedScreenShot;
+        }
+
+        // Optimization
+        screenShot.setHasAlpha(false);
+
+        return screenShot;
+    }
+
+    private static float getDegreesForRotation(int value) {
+        switch (value) {
+            case Surface.ROTATION_90: {
+                return 360f - 90f;
+            }
+            case Surface.ROTATION_180: {
+                return 360f - 180f;
+            }
+            case Surface.ROTATION_270: {
+                return 360f - 270f;
+            } default: {
+                return 0;
+            }
+        }
+    }
+
+}
diff --git a/packages/SystemUI/AndroidManifest.xml b/packages/SystemUI/AndroidManifest.xml
index eadb4bf..de25115 100644
--- a/packages/SystemUI/AndroidManifest.xml
+++ b/packages/SystemUI/AndroidManifest.xml
@@ -150,9 +150,6 @@
     <!-- DevicePolicyManager get user restrictions -->
     <uses-permission android:name="android.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS" />
 
-    <!-- Needed for passing extras with intent ACTION_SHOW_ADMIN_SUPPORT_DETAILS -->
-    <uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
-
     <!-- TV picture-in-picture -->
     <uses-permission android:name="android.permission.RECEIVE_MEDIA_RESOURCE_USAGE" />
 
@@ -165,6 +162,9 @@
     <!-- the ability to rename notifications posted by other apps -->
     <uses-permission android:name="android.permission.SUBSTITUTE_NOTIFICATION_APP_NAME" />
 
+    <!-- shortcut manager -->
+    <uses-permission android:name="android.permission.RESET_SHORTCUT_MANAGER_THROTTLING" />
+
     <application
         android:name=".SystemUIApplication"
         android:persistent="true"
@@ -385,7 +385,7 @@
             android:theme="@style/PipTheme"
             android:launchMode="singleTop"
             android:taskAffinity=""
-            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
+            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|locale|layoutDirection"
             android:resizeableActivity="true"
             android:supportsPictureInPicture="true"
             androidprv:alwaysFocusable="true"
@@ -395,7 +395,7 @@
             android:exported="true"
             android:theme="@style/PipTheme"
             android:taskAffinity=""
-            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
+            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|locale|layoutDirection"
             android:resizeableActivity="true"
             android:supportsPictureInPicture="true"
             android:excludeFromRecents="true" />
diff --git a/packages/SystemUI/res/color/notification_guts_buttons.xml b/packages/SystemUI/res/color/notification_guts_buttons.xml
index c211e43..3b8d59b 100644
--- a/packages/SystemUI/res/color/notification_guts_buttons.xml
+++ b/packages/SystemUI/res/color/notification_guts_buttons.xml
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:state_checked="true"
-          android:color="@*android:color/material_deep_teal_500" />
+          android:color="?android:attr/colorAccent" />
     <item android:color="@android:color/black"
           android:alpha=".54" />
 </selector>
\ No newline at end of file
diff --git a/packages/SystemUI/res/color/qs_detail_empty.xml b/packages/SystemUI/res/color/qs_detail_empty.xml
new file mode 100644
index 0000000..4be39c7
--- /dev/null
+++ b/packages/SystemUI/res/color/qs_detail_empty.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:alpha="0.14" android:color="@*android:color/quaternary_device_default_settings" />
+</selector>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/packages/SystemUI/res/color/qs_detail_progress_track.xml
similarity index 67%
copy from packages/SystemUI/res/layout/night_mode_settings.xml
copy to packages/SystemUI/res/color/qs_detail_progress_track.xml
index 3725e78..c56382e 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/packages/SystemUI/res/color/qs_detail_progress_track.xml
@@ -13,12 +13,8 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-
-    <include layout="@layout/switch_bar" />
-
-</LinearLayout>
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- I really don't want to define this, but the View that uses this asset uses both the
+         light and dark accent colors. -->
+    <item android:alpha="0.6" android:drawable="@*android:color/accent_device_default_light" />
+</selector>
diff --git a/packages/SystemUI/res/color/qs_user_detail_avatar_frame.xml b/packages/SystemUI/res/color/qs_user_detail_avatar_frame.xml
index 20251c2..344859c 100644
--- a/packages/SystemUI/res/color/qs_user_detail_avatar_frame.xml
+++ b/packages/SystemUI/res/color/qs_user_detail_avatar_frame.xml
@@ -17,6 +17,6 @@
   -->
 
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:state_activated="true" android:color="@color/current_user_border_color" />
+    <item android:state_activated="true" android:color="?android:attr/colorAccent" />
     <item android:color="@android:color/transparent" />
 </selector>
\ No newline at end of file
diff --git a/packages/SystemUI/res/color/qs_user_detail_name.xml b/packages/SystemUI/res/color/qs_user_detail_name.xml
index 35c7a4f..e262209 100644
--- a/packages/SystemUI/res/color/qs_user_detail_name.xml
+++ b/packages/SystemUI/res/color/qs_user_detail_name.xml
@@ -17,7 +17,7 @@
   -->
 
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
-    <item android:state_activated="true" android:color="@color/current_user_border_color" />
+    <item android:state_activated="true" android:color="?android:attr/colorAccent" />
     <item android:state_enabled="false" android:color="@color/qs_tile_disabled_color" />
     <item android:color="#66ffffff" /> <!-- 40% white -->
 </selector>
\ No newline at end of file
diff --git a/packages/DocumentsUI/res/values/attrs.xml b/packages/SystemUI/res/color/screen_pinning_nav_icon_highlight_outer.xml
similarity index 73%
copy from packages/DocumentsUI/res/values/attrs.xml
copy to packages/SystemUI/res/color/screen_pinning_nav_icon_highlight_outer.xml
index 9e13001..5370f7a 100644
--- a/packages/DocumentsUI/res/values/attrs.xml
+++ b/packages/SystemUI/res/color/screen_pinning_nav_icon_highlight_outer.xml
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 The Android Open Source Project
+<!-- Copyright (C) 2016 The Android Open Source Project
 
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
@@ -13,8 +13,6 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<resources>
-    <declare-styleable name="DocumentsTheme">
-        <attr name="colorActionMode" format="color"/>
-    </declare-styleable>
-</resources>
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:alpha="0.25" android:color="?android:attr/colorAccent" />
+</selector>
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_carmode.png
index 6242084..95e5778 100644
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_ime_carmode.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_ime_carmode.png
index 1b37a47..6421146 100644
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_ime_carmode.png
+++ b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_back_ime_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_home_carmode.png b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_home_carmode.png
index 9e05758..151d5fe 100644
--- a/packages/SystemUI/res/drawable-hdpi/ic_sysbar_home_carmode.png
+++ b/packages/SystemUI/res/drawable-hdpi/ic_sysbar_home_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-ldrtl-hdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-ldrtl-hdpi/ic_sysbar_back_carmode.png
index 2fcfdde..b954aa7 100644
--- a/packages/SystemUI/res/drawable-ldrtl-hdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-ldrtl-hdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-ldrtl-mdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-ldrtl-mdpi/ic_sysbar_back_carmode.png
index 48708a5..61d5db6 100644
--- a/packages/SystemUI/res/drawable-ldrtl-mdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-ldrtl-mdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-ldrtl-xhdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-ldrtl-xhdpi/ic_sysbar_back_carmode.png
index 3d73184..7b98c1f 100644
--- a/packages/SystemUI/res/drawable-ldrtl-xhdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-ldrtl-xhdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-ldrtl-xxhdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-ldrtl-xxhdpi/ic_sysbar_back_carmode.png
index 786935d..aad1320 100644
--- a/packages/SystemUI/res/drawable-ldrtl-xxhdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-ldrtl-xxhdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_carmode.png
index e4bd4bc..754b2d9 100644
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_ime_carmode.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_ime_carmode.png
index 94ccf79..873ed7f 100644
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_ime_carmode.png
+++ b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_back_ime_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_home_carmode.png b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_home_carmode.png
index 980bbbc..7696d87 100644
--- a/packages/SystemUI/res/drawable-mdpi/ic_sysbar_home_carmode.png
+++ b/packages/SystemUI/res/drawable-mdpi/ic_sysbar_home_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-nodpi/play.xml b/packages/SystemUI/res/drawable-nodpi/play.xml
index 747e60b..7720230 100644
--- a/packages/SystemUI/res/drawable-nodpi/play.xml
+++ b/packages/SystemUI/res/drawable-nodpi/play.xml
@@ -21,7 +21,4 @@
     <path
         android:fillColor="#FF000000"
         android:pathData="M8.0,5.0l0.0,14.0l11.0,-7.0z"/>
-    <path
-        android:pathData="M0 0h24v24H0z"
-        android:fillColor="#00000000"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_carmode.png
index 201be3b..c98f55e 100644
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_ime_carmode.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_ime_carmode.png
index 2770d62..187a566 100644
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_ime_carmode.png
+++ b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_back_ime_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_home_carmode.png b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_home_carmode.png
index 8ac6493..c66f8be 100644
--- a/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_home_carmode.png
+++ b/packages/SystemUI/res/drawable-xhdpi/ic_sysbar_home_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_back_carmode.png
index 8e3678b..3a3a119 100644
--- a/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_back_ime_carmode.png b/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_back_ime_carmode.png
index 6c7cb05..7198c82 100644
--- a/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_back_ime_carmode.png
+++ b/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_back_ime_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_home_carmode.png b/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_home_carmode.png
index ea2b108..b1fc02e 100644
--- a/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_home_carmode.png
+++ b/packages/SystemUI/res/drawable-xxhdpi/ic_sysbar_home_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_back_carmode.png b/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_back_carmode.png
index 3e11023..c06bfda 100644
--- a/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_back_carmode.png
+++ b/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_back_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_back_ime_carmode.png b/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_back_ime_carmode.png
index fe8213d..a8c76bf 100644
--- a/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_back_ime_carmode.png
+++ b/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_back_ime_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_home_carmode.png b/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_home_carmode.png
index c117efd..b798e3d 100644
--- a/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_home_carmode.png
+++ b/packages/SystemUI/res/drawable-xxxhdpi/ic_sysbar_home_carmode.png
Binary files differ
diff --git a/packages/SystemUI/res/drawable/brightness_mirror_background.xml b/packages/SystemUI/res/drawable/brightness_mirror_background.xml
index e901e40..0c69d89 100644
--- a/packages/SystemUI/res/drawable/brightness_mirror_background.xml
+++ b/packages/SystemUI/res/drawable/brightness_mirror_background.xml
@@ -15,5 +15,5 @@
   ~ limitations under the License
   -->
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
-    <solid android:color="@color/system_primary_color" />
+    <solid android:color="?android:attr/colorPrimary" />
 </shape>
diff --git a/packages/SystemUI/res/drawable/car_ic_arrow.xml b/packages/SystemUI/res/drawable/car_ic_arrow.xml
index 9d292cc..2c5ad27 100644
--- a/packages/SystemUI/res/drawable/car_ic_arrow.xml
+++ b/packages/SystemUI/res/drawable/car_ic_arrow.xml
@@ -21,7 +21,4 @@
     <path
         android:fillColor="#FFFFFFFF"
         android:pathData="M14.0,20.0l10.0,10.0 10.0,-10.0z"/>
-    <path
-        android:pathData="M0 0h48v48H0z"
-        android:fillColor="#00000000"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/fab_background.xml b/packages/SystemUI/res/drawable/fab_background.xml
deleted file mode 100644
index 7f23f2b..0000000
--- a/packages/SystemUI/res/drawable/fab_background.xml
+++ /dev/null
@@ -1,23 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2015 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.
--->
-<ripple xmlns:android="http://schemas.android.com/apk/res/android"
-        android:color="@color/fab_ripple">
-    <item>
-        <shape>
-            <solid android:color="@color/fab_shape" />
-        </shape>
-    </item>
-</ripple>
diff --git a/packages/SystemUI/res/drawable/ic_brightness_thumb.xml b/packages/SystemUI/res/drawable/ic_brightness_thumb.xml
index dc978fe..24ac018 100644
--- a/packages/SystemUI/res/drawable/ic_brightness_thumb.xml
+++ b/packages/SystemUI/res/drawable/ic_brightness_thumb.xml
@@ -20,7 +20,7 @@
         android:viewportHeight="24.0">
     <path
         android:pathData="m18.250000,12.000000a6.250000,6.250000 0.000000,1.000000 1.000000,-12.500000 0.000000,6.250000 6.250000,0.000000 1.000000,1.000000 12.500000,0.000000z"
-        android:fillColor="@color/system_primary_color" />
+        android:fillColor="?android:attr/colorPrimary" />
     <path
         android:fillColor="#FFFFFFFF"
         android:pathData="M20.000000,8.700000L20.000000,4.000000L15.300000,4.000000L12.000000,0.700000 8.700000,4.000000L4.000000,4.000000L4.000000,8.700000L0.700000,12.000000 4.000000,15.300000L4.000000,20.000000L8.700000,20.000000L12.000000,23.299999 15.300000,20.000000L20.000000,20.000000L20.000000,15.300000L23.299999,12.000000 20.000000,8.700000zM12.000000,18.000000C8.700000,18.000000 6.000000,15.300000 6.000000,12.000000 6.000000,8.700000 8.700000,6.000000 12.000000,6.000000c3.300000,0.000000 6.000000,2.700000 6.000000,6.000000 0.000000,3.300000 -2.700000,6.000000 -6.000000,6.000000zM12.000000,8.000000c-2.200000,0.000000 -4.000000,1.800000 -4.000000,4.000000 0.000000,2.200000 1.800000,4.000000 4.000000,4.000000 2.200000,0.000000 4.000000,-1.800000 4.000000,-4.000000 0.000000,-2.200000 -1.800000,-4.000000 -4.000000,-4.000000z"/>
diff --git a/packages/SystemUI/res/drawable/ic_fullscreen_white_24dp.xml b/packages/SystemUI/res/drawable/ic_fullscreen_white_24dp.xml
index 7ddb40c..314a25a 100644
--- a/packages/SystemUI/res/drawable/ic_fullscreen_white_24dp.xml
+++ b/packages/SystemUI/res/drawable/ic_fullscreen_white_24dp.xml
@@ -18,9 +18,6 @@
     android:height="24dp"
     android:viewportWidth="24"
     android:viewportHeight="24">
-
-    <path
-        android:pathData="M0 0h24v24H0z" />
     <path
         android:fillColor="#FFFFFF"
         android:pathData="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z" />
diff --git a/packages/SystemUI/res/drawable/ic_ksh_key_backspace.xml b/packages/SystemUI/res/drawable/ic_ksh_key_backspace.xml
index 6519673..1183203 100644
--- a/packages/SystemUI/res/drawable/ic_ksh_key_backspace.xml
+++ b/packages/SystemUI/res/drawable/ic_ksh_key_backspace.xml
@@ -19,7 +19,6 @@
         android:height="24dp"
         android:viewportWidth="24"
         android:viewportHeight="24">
-    <path android:pathData="M0 0h24v24H0z" />
     <path android:fillColor="@color/ksh_key_item_color"
             android:pathData="M22 3H7c-.69 0-1.23 .35 -1.59 .88 L0 12l5.41 8.11c.36 .53 .9 .89
 1.59 .89 h15c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-3 12.59L17.59 17 14 13.41 10.41 17 9 15.59
diff --git a/packages/SystemUI/res/drawable/ic_ksh_key_enter.xml b/packages/SystemUI/res/drawable/ic_ksh_key_enter.xml
index 599f350..66b1307 100644
--- a/packages/SystemUI/res/drawable/ic_ksh_key_enter.xml
+++ b/packages/SystemUI/res/drawable/ic_ksh_key_enter.xml
@@ -19,7 +19,6 @@
         android:height="24dp"
         android:viewportWidth="24"
         android:viewportHeight="24">
-    <path android:pathData="M0 0h24v24H0z" />
    <path android:fillColor="@color/ksh_key_item_color"
             android:pathData="M19 7v4H5.83l3.58-3.59L8 6l-6 6 6 6 1.41-1.41L5.83 13H21V7z" />
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_ksh_key_left.xml b/packages/SystemUI/res/drawable/ic_ksh_key_left.xml
index 038187e..57d0423e9c 100644
--- a/packages/SystemUI/res/drawable/ic_ksh_key_left.xml
+++ b/packages/SystemUI/res/drawable/ic_ksh_key_left.xml
@@ -21,5 +21,4 @@
         android:viewportHeight="24">
     <path android:fillColor="@color/ksh_key_item_color"
             android:pathData="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z" />
-    <path android:pathData="M0 0h24v24H0z" />
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_ksh_key_meta.xml b/packages/SystemUI/res/drawable/ic_ksh_key_meta.xml
index 1e2195e..be8fe8c 100644
--- a/packages/SystemUI/res/drawable/ic_ksh_key_meta.xml
+++ b/packages/SystemUI/res/drawable/ic_ksh_key_meta.xml
@@ -24,5 +24,4 @@
 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27 .28 v.79l5 4.99L20.49
 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5
 14z" />
-    <path android:pathData="M0 0h24v24H0z" />
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_ksh_key_right.xml b/packages/SystemUI/res/drawable/ic_ksh_key_right.xml
index f2d7315..720d4e4 100644
--- a/packages/SystemUI/res/drawable/ic_ksh_key_right.xml
+++ b/packages/SystemUI/res/drawable/ic_ksh_key_right.xml
@@ -21,5 +21,4 @@
         android:viewportHeight="24">
     <path android:fillColor="@color/ksh_key_item_color"
             android:pathData="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z" />
-    <path android:pathData="M0 0h24v24H0z" />
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_ksh_key_up.xml b/packages/SystemUI/res/drawable/ic_ksh_key_up.xml
index 36a83b1..350c994 100644
--- a/packages/SystemUI/res/drawable/ic_ksh_key_up.xml
+++ b/packages/SystemUI/res/drawable/ic_ksh_key_up.xml
@@ -21,5 +21,4 @@
         android:viewportHeight="24">
     <path android:fillColor="@color/ksh_key_item_color"
             android:pathData="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z" />
-    <path android:pathData="M0 0h24v24H0z" />
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_night_mode_disabled.xml b/packages/SystemUI/res/drawable/ic_mode_edit.xml
similarity index 66%
rename from packages/SystemUI/res/drawable/ic_night_mode_disabled.xml
rename to packages/SystemUI/res/drawable/ic_mode_edit.xml
index 010815a..8a73686 100644
--- a/packages/SystemUI/res/drawable/ic_night_mode_disabled.xml
+++ b/packages/SystemUI/res/drawable/ic_mode_edit.xml
@@ -14,11 +14,11 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="24.0dp"
-        android:height="24.0dp"
+        android:width="20.0dp"
+        android:height="20.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M20.71,5.63l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0.0l-3.12,3.12 -1.93,-1.91 -1.41,1.41 1.42,1.42L3.0,16.25L3.0,21.0l4.75,0.0l8.92,-8.92 1.42,1.42 1.41,-1.41 -1.92,-1.92 3.12,-3.12c0.4,0.0 0.4,-1.0 0.01,-1.42zM6.92,19.0L5.0,17.08l8.06,-8.06 1.92,1.92L6.92,19.0z"/>
+        android:fillColor="#FFFFFF"
+        android:pathData="M3.0,17.25L3.0,21.0l3.75,0.0L17.81,9.94l-3.75,-3.75L3.0,17.25zM20.71,7.04c0.39,-0.3 0.39,-1.02 0.0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0.0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_night_mode.xml b/packages/SystemUI/res/drawable/ic_night_mode.xml
deleted file mode 100644
index caa7a47..0000000
--- a/packages/SystemUI/res/drawable/ic_night_mode.xml
+++ /dev/null
@@ -1,24 +0,0 @@
-<!--
-    Copyright (C) 2016 The Android Open Source Project
-
-    Licensed under the Apache License, Version 2.0 (the "License");
-    you may not use this file except in compliance with the License.
-    You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-    Unless required by applicable law or agreed to in writing, software
-    distributed under the License is distributed on an "AS IS" BASIS,
-    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-    See the License for the specific language governing permissions and
-    limitations under the License.
--->
-<vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="24.0dp"
-        android:height="24.0dp"
-        android:viewportWidth="24.0"
-        android:viewportHeight="24.0">
-    <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M20.71,5.63l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0.0l-3.12,3.12 -1.93,-1.91 -1.41,1.41 1.42,1.42L3.0,16.25L3.0,21.0l4.75,0.0l8.92,-8.92 1.42,1.42 1.41,-1.41 -1.92,-1.92 3.12,-3.12c0.4,0.0 0.4,-1.0 0.01,-1.42zM6.92,19.0L5.0,17.08l8.06,-8.06 1.92,1.92L6.92,19.0z"/>
-</vector>
diff --git a/packages/SystemUI/res/drawable/ic_pause_white_24dp.xml b/packages/SystemUI/res/drawable/ic_pause_white_24dp.xml
index d9a4f7b..5b65f10 100644
--- a/packages/SystemUI/res/drawable/ic_pause_white_24dp.xml
+++ b/packages/SystemUI/res/drawable/ic_pause_white_24dp.xml
@@ -22,6 +22,4 @@
     <path
         android:fillColor="#FFFFFF"
         android:pathData="M6 19h4V5H6v14zm8-14v14h4V5h-4z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_play_arrow_white_24dp.xml b/packages/SystemUI/res/drawable/ic_play_arrow_white_24dp.xml
index b8fa99e..ddc9e8d 100644
--- a/packages/SystemUI/res/drawable/ic_play_arrow_white_24dp.xml
+++ b/packages/SystemUI/res/drawable/ic_play_arrow_white_24dp.xml
@@ -22,6 +22,4 @@
     <path
         android:fillColor="#FFFFFF"
         android:pathData="M8 5v14l11-7z" />
-    <path
-        android:pathData="M0 0h24v24H0z" />
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_branded_vpn.xml b/packages/SystemUI/res/drawable/ic_qs_branded_vpn.xml
new file mode 100644
index 0000000..736a04a
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_qs_branded_vpn.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="12.0dp"
+        android:height="12.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#4DFFFFFF"
+        android:pathData="M12.700000,10.000000c-0.800000,-2.300000 -3.000000,-4.000000 -5.700000,-4.000000c-3.300000,0.000000 -6.000000,2.700000 -6.000000,6.000000s2.700000,6.000000 6.000000,6.000000c2.600000,0.000000 4.800000,-1.700000 5.700000,-4.000000L17.000000,14.000000l0.000000,4.000000l4.000000,0.000000l0.000000,-4.000000l2.000000,0.000000l0.000000,-4.000000L12.700000,10.000000zM7.000000,14.000000c-1.100000,0.000000 -2.000000,-0.900000 -2.000000,-2.000000c0.000000,-1.100000 0.900000,-2.000000 2.000000,-2.000000s2.000000,0.900000 2.000000,2.000000C9.000000,13.100000 8.100000,14.000000 7.000000,14.000000z"/>
+</vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_data_disabled.xml b/packages/SystemUI/res/drawable/ic_qs_data_disabled.xml
index b4144a3..d11b6f4 100644
--- a/packages/SystemUI/res/drawable/ic_qs_data_disabled.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_data_disabled.xml
@@ -14,8 +14,9 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="24.0dp"
-        android:height="24.0dp"
+        android:autoMirrored="true"
+        android:width="32.0dp"
+        android:height="32.0dp"
         android:viewportWidth="40.0"
         android:viewportHeight="40.0">
     <path
diff --git a/packages/SystemUI/res/drawable/ic_night_mode_disabled.xml b/packages/SystemUI/res/drawable/ic_qs_night_display_off.xml
similarity index 60%
copy from packages/SystemUI/res/drawable/ic_night_mode_disabled.xml
copy to packages/SystemUI/res/drawable/ic_qs_night_display_off.xml
index 010815a..778ccbc 100644
--- a/packages/SystemUI/res/drawable/ic_night_mode_disabled.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_night_display_off.xml
@@ -14,11 +14,14 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="24.0dp"
-        android:height="24.0dp"
-        android:viewportWidth="24.0"
-        android:viewportHeight="24.0">
+    android:width="64dp"
+    android:height="64dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24"
+    android:alpha="0.3">
+
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M20.71,5.63l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0.0l-3.12,3.12 -1.93,-1.91 -1.41,1.41 1.42,1.42L3.0,16.25L3.0,21.0l4.75,0.0l8.92,-8.92 1.42,1.42 1.41,-1.41 -1.92,-1.92 3.12,-3.12c0.4,0.0 0.4,-1.0 0.01,-1.42zM6.92,19.0L5.0,17.08l8.06,-8.06 1.92,1.92L6.92,19.0z"/>
+        android:fillColor="#FFF"
+        android:pathData="M6,12c0,5.5,4.5,10,10,10c1,0,2-0.2,3-0.5c-4.1-1.3-7-5.1-7-9.5s2.9-8.3,7-9.5C18.1,2.2,17.1,2,16,2C10.5,2,6,6.5,6,12z" />
+
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_night_mode_disabled.xml b/packages/SystemUI/res/drawable/ic_qs_night_display_on.xml
similarity index 60%
copy from packages/SystemUI/res/drawable/ic_night_mode_disabled.xml
copy to packages/SystemUI/res/drawable/ic_qs_night_display_on.xml
index 010815a..aaca663 100644
--- a/packages/SystemUI/res/drawable/ic_night_mode_disabled.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_night_display_on.xml
@@ -14,11 +14,13 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="24.0dp"
-        android:height="24.0dp"
-        android:viewportWidth="24.0"
-        android:viewportHeight="24.0">
+    android:width="64dp"
+    android:height="64dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M20.71,5.63l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0.0l-3.12,3.12 -1.93,-1.91 -1.41,1.41 1.42,1.42L3.0,16.25L3.0,21.0l4.75,0.0l8.92,-8.92 1.42,1.42 1.41,-1.41 -1.92,-1.92 3.12,-3.12c0.4,0.0 0.4,-1.0 0.01,-1.42zM6.92,19.0L5.0,17.08l8.06,-8.06 1.92,1.92L6.92,19.0z"/>
+        android:fillColor="#FFF"
+        android:pathData="M6,12c0,5.5,4.5,10,10,10c1,0,2-0.2,3-0.5c-4.1-1.3-7-5.1-7-9.5s2.9-8.3,7-9.5C18.1,2.2,17.1,2,16,2C10.5,2,6,6.5,6,12z" />
+
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_signal_0.xml b/packages/SystemUI/res/drawable/ic_qs_signal_0.xml
index f63dfb12..b78d3bf 100644
--- a/packages/SystemUI/res/drawable/ic_qs_signal_0.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_signal_0.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,17 +15,15 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="32dp"
-        android:height="32dp"
+        android:width="32.0dp"
+        android:height="32.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.700001,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
+        android:fillAlpha="0.3"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.700001,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
-    <path
-        android:pathData="M17.700001,8.000000l4.299999,0.000000 0.000000,-6.000000 -20.000000,20.000000 15.700001,0.000000z"
-        android:fillColor="#4DFFFFFF"/>
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_signal_1.xml b/packages/SystemUI/res/drawable/ic_qs_signal_1.xml
index 7fb423e..e055de7 100644
--- a/packages/SystemUI/res/drawable/ic_qs_signal_1.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_signal_1.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,20 +15,18 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="32dp"
-        android:height="32dp"
+        android:width="32.0dp"
+        android:height="32.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.7,20.0l2.0,0.0l0.0,2.0l-2.0,0.0z"/>
+        android:pathData="M10.0,14.6l-8.0,8.0l8.0,0.0l0,-8z"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.7,10.0l2.0,0.0l0.0,8.1l-2.0,0.0z"/>
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
+        android:fillAlpha="0.3"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M17.7,8.0l4.299999,0.0 0.0,-6.0 -20.0,20.0 15.700001,0.0z"/>
-    <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M10.1,13.9l-8.1,8.1 8.1,0.0z"/>
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_signal_2.xml b/packages/SystemUI/res/drawable/ic_qs_signal_2.xml
index 3358d65..8a48817 100644
--- a/packages/SystemUI/res/drawable/ic_qs_signal_2.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_signal_2.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,20 +15,18 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="32dp"
-        android:height="32dp"
+        android:width="32.0dp"
+        android:height="32.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.700001,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
+        android:pathData="M14.0,10.6l-12.0,12.0l12.0,0.0L14.0,10.6z"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.700001,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
+        android:fillAlpha="0.3"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M13.900000,10.000000l-11.900000,12.000000 11.900000,0.000000z"/>
-    <path
-        android:pathData="M17.700001,8.000000l4.299999,0.000000 0.000000,-6.000000 -20.000000,20.000000 15.700001,0.000000z"
-        android:fillColor="#4DFFFFFF"/>
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_signal_3.xml b/packages/SystemUI/res/drawable/ic_qs_signal_3.xml
index 63838a9..39cc94c 100644
--- a/packages/SystemUI/res/drawable/ic_qs_signal_3.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_signal_3.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,20 +15,18 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="32dp"
-        android:height="32dp"
+        android:width="32.0dp"
+        android:height="32.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.700001,19.900000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
+        android:fillAlpha="0.3"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.700001,9.900000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M16.700001,7.200000l-14.700001,14.700000 14.700001,0.000000z"/>
-    <path
-        android:pathData="M17.700001,7.900000l4.299999,0.000000 0.000000,-6.000000 -20.000000,20.000000 15.700001,0.000000z"
-        android:fillColor="#4DFFFFFF"/>
+        android:pathData="M14.1,14.1l2.9,0.0 0.0,-6.5 -15.0,15.0 12.1,0.0z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_signal_4.xml b/packages/SystemUI/res/drawable/ic_qs_signal_4.xml
index 76690cc..012e95e 100644
--- a/packages/SystemUI/res/drawable/ic_qs_signal_4.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_signal_4.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,17 +15,14 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="32dp"
-        android:height="32dp"
+        android:width="32.0dp"
+        android:height="32.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.700001,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.700001,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M2.000000,22.000000l15.700001,0.000000 0.000000,-14.000000 4.299999,0.000000 0.000000,-6.000000z"/>
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_signal_4g_plus.xml b/packages/SystemUI/res/drawable/ic_qs_signal_4g_plus.xml
new file mode 100644
index 0000000..4d7f325
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_qs_signal_4g_plus.xml
@@ -0,0 +1,30 @@
+<!--
+    Copyright (C) 2016 The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M4.6,7.8l0.7,0.0l0.0,1.3L4.6,9.1L4.6,11.0L3.0,11.0L3.0,9.2L0.1,9.2L0.0,8.2l3.0,-5.7l1.7,0.0L4.6,7.8L4.6,7.8zM1.7,7.8L3.0,7.8l0.0,-3.0L2.9,5.0L1.7,7.8z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M11.9,9.9c-0.2,0.4 -0.6,0.7 -1.0,0.9s-1.0,0.4 -1.8,0.4c-0.9,0.0 -1.7,-0.3 -2.2,-0.8S6.1,9.0 6.1,7.9L6.1,5.6c0.0,-1.1 0.3,-1.9 0.8,-2.4S8.2,2.4 9.0,2.4c1.0,0.0 1.7,0.2 2.1,0.7s0.7,1.2 0.7,2.1l-1.6,0.0c0.0,-0.5 -0.1,-0.9 -0.2,-1.1S9.5,3.7 9.0,3.7c-0.4,0.0 -0.7,0.2 -0.9,0.5S7.8,5.0 7.8,5.6l0.0,2.3c0.0,0.7 0.1,1.1 0.3,1.4c0.2,0.3 0.6,0.5 1.0,0.5c0.3,0.0 0.6,0.0 0.7,-0.1s0.3,-0.2 0.4,-0.3L10.2,7.8L9.0,7.8L9.0,6.6l2.9,0.0L11.9,9.9z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M17.7,4.4l-1.900001,0.0 0.0,-1.9 -1.2,0.0 0.0,1.9 -1.900001,0.0 0.0,1.2 1.900001,0.0 0.0,1.9 1.2,0.0 0.0,-1.9 1.900001,0.0z"/>
+</vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_signal_lte_plus.xml b/packages/SystemUI/res/drawable/ic_qs_signal_lte_plus.xml
new file mode 100644
index 0000000..3af0629
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_qs_signal_lte_plus.xml
@@ -0,0 +1,33 @@
+<!--
+    Copyright (C) 2016 The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M2.0,9.7l2.0,0.0L4.0,11.0L0.4,11.0L0.4,2.5L2.0,2.5L2.0,9.7z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M8.3,3.8L7.0,3.8L7.0,11.0L5.4,11.0L5.4,3.8L4.0,3.8L4.0,2.5l4.3,0.0L8.3,3.8z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M12.4,7.3l-1.7,0.0l0.0,2.4l2.1,0.0L12.799999,11.0L9.0,11.0L9.0,2.5l3.7,0.0l0.0,1.3l-2.1,0.0l0.0,2.1l1.7,0.0L12.4,7.3L12.4,7.3z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M18.4,4.4l-1.9,0.0 0.0,-1.9 -1.2,0.0 0.0,1.9 -1.900001,0.0 0.0,1.2 1.900001,0.0 0.0,1.9 1.2,0.0 0.0,-1.9 1.9,0.0z"/>
+</vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_wifi_0.xml b/packages/SystemUI/res/drawable/ic_qs_wifi_0.xml
index 50c427e..e6f9292 100644
--- a/packages/SystemUI/res/drawable/ic_qs_wifi_0.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_wifi_0.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -14,17 +14,15 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="32.0dp"
-        android:height="29.5dp"
-        android:viewportWidth="26.0"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.200000,-1.500000C25.100000,6.100000 20.299999,2.100000 13.000000,2.100000S0.900000,6.100000 0.400000,6.500000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillAlpha="0.3"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.9,1.9 -1.9,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.799999,-1.800001z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_wifi_1.xml b/packages/SystemUI/res/drawable/ic_qs_wifi_1.xml
index a2d11a0..d423ccb 100644
--- a/packages/SystemUI/res/drawable/ic_qs_wifi_1.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_wifi_1.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -14,20 +14,18 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="32.0dp"
-        android:height="29.5dp"
-        android:viewportWidth="26.0"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.300000,-1.600000C25.100000,6.000000 20.299999,2.000000 13.000000,2.000000S0.900000,6.000000 0.400000,6.400000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,13.2c-0.1,0.0 -0.3,-0.1 -0.4,-0.1c-0.1,0.0 -0.3,0.0 -0.4,-0.1c-0.3,0.0 -0.6,-0.1 -0.9,-0.1c0.0,0.0 0.0,0.0 -0.1,0.0c0.0,0.0 0.0,0.0 0.0,0.0s0.0,0.0 0.0,0.0c0.0,0.0 0.0,0.0 -0.1,0.0c-0.3,0.0 -0.6,0.0 -0.9,0.1c-0.1,0.0 -0.3,0.0 -0.4,0.1c-0.2,0.0 -0.3,0.1 -0.5,0.1c-0.2,0.0 -0.3,0.1 -0.5,0.1c-0.1,0.0 -0.1,0.0 -0.2,0.1c-1.6,0.5 -2.7,1.3 -2.8,1.5l5.3,6.6l0.0,0.0l0.0,0.0l0.0,0.0l0.0,0.0l1.8,-2.2L13.700002,13.2z"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M13.000000,22.000000l5.500000,-6.800000c-0.200000,-0.200000 -2.300000,-1.900000 -5.500000,-1.900000s-5.300000,1.800000 -5.500000,1.900000L13.000000,22.000000L13.000000,22.000000L13.000000,22.000000L13.000000,22.000000L13.000000,22.000000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillAlpha="0.3"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.9,1.9 -1.9,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.799999,-1.800001z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_wifi_2.xml b/packages/SystemUI/res/drawable/ic_qs_wifi_2.xml
index f2043fc..1982130 100644
--- a/packages/SystemUI/res/drawable/ic_qs_wifi_2.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_wifi_2.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -14,20 +14,18 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="32.0dp"
-        android:height="29.5dp"
-        android:viewportWidth="26.0"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.300000,-1.600000C25.100000,6.000000 20.299999,2.000000 13.000000,2.000000S0.900000,6.000000 0.400000,6.400000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,12.2l4.9,0.0c-1.0,-0.7 -3.4,-2.2 -6.7,-2.2c-4.1,0.0 -6.9,2.2 -7.2,2.5l7.2,9.0l0.0,0.0l0.0,0.0l1.8,-2.2L13.800001,12.2z"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.000000,11.600000c-1.300000,-0.700000 -3.400000,-1.600000 -6.000000,-1.600000c-4.400000,0.000000 -7.300000,2.400000 -7.600000,2.700000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,11.600000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillAlpha="0.3"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.800001,1.9 -1.800001,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.9,-1.800001z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_wifi_3.xml b/packages/SystemUI/res/drawable/ic_qs_wifi_3.xml
index b7a4f4c..b350111 100644
--- a/packages/SystemUI/res/drawable/ic_qs_wifi_3.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_wifi_3.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -14,20 +14,18 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="32.0dp"
-        android:height="29.5dp"
-        android:viewportWidth="26.0"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#4DFFFFFF"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.300000,-1.600000C25.100000,6.000000 20.299999,2.000000 13.000000,2.000000S0.900000,6.000000 0.400000,6.400000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0l1.0,-1.2C20.0,10.6 16.8,8.0 12.0,8.0s-8.0,2.6 -8.5,3.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.000000,8.600000c-1.600000,-0.700000 -3.600000,-1.300000 -6.000000,-1.300000c-5.300000,0.000000 -8.900000,3.000000 -9.200000,3.200000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.600000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillAlpha="0.3"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.9,1.9 -1.9,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.9,-1.800001z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_wifi_4.xml b/packages/SystemUI/res/drawable/ic_qs_wifi_4.xml
index 35a9138..136a004 100644
--- a/packages/SystemUI/res/drawable/ic_qs_wifi_4.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_wifi_4.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -14,17 +14,14 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="32.0dp"
-        android:height="29.5dp"
-        android:viewportWidth="26.0"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.300000,-1.600000C25.100000,6.000000 20.299999,2.000000 13.000000,2.000000S0.900000,6.000000 0.400000,6.400000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillColor="#FFFFFF"/>
     <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="#FFFFFFFF"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.9,1.9 -1.9,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.9,-1.800001z"
+        android:fillColor="#FFFFFF"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_qs_wifi_disconnected.xml b/packages/SystemUI/res/drawable/ic_qs_wifi_disconnected.xml
index 4d2a35e..2dcdb71 100644
--- a/packages/SystemUI/res/drawable/ic_qs_wifi_disconnected.xml
+++ b/packages/SystemUI/res/drawable/ic_qs_wifi_disconnected.xml
@@ -19,9 +19,6 @@
         android:viewportWidth="26.0"
         android:viewportHeight="24.0">
     <path
-        android:pathData="M0 0h26v24H0z"
-        android:fillColor="#00000000"/>
-    <path
         android:fillColor="#FFFFFFFF"
         android:pathData="M21.0,8.5
         c0.85,0.0 1.6,0.23 2.3,0.62l2.24,-2.79
diff --git a/packages/SystemUI/res/drawable/ic_send.xml b/packages/SystemUI/res/drawable/ic_send.xml
index 6ce3672..7cac2a4 100644
--- a/packages/SystemUI/res/drawable/ic_send.xml
+++ b/packages/SystemUI/res/drawable/ic_send.xml
@@ -22,7 +22,4 @@
     <path
         android:fillColor="#FF000000"
         android:pathData="M4.02,42.0L46.0,24.0 4.02,6.0 4.0,20.0l30.0,4.0 -30.0,4.0z"/>
-    <path
-        android:pathData="M0 0h48v48H0z"
-        android:fillColor="#00000000"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/ic_settings_20dp.xml b/packages/SystemUI/res/drawable/ic_settings_20dp.xml
new file mode 100644
index 0000000..3170f86
--- /dev/null
+++ b/packages/SystemUI/res/drawable/ic_settings_20dp.xml
@@ -0,0 +1,24 @@
+<!--
+    Copyright (C) 2016 The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="20dp"
+    android:height="20dp"
+    android:viewportWidth="24.0"
+    android:viewportHeight="24.0">
+    <path
+        android:pathData="M19.4,13.0c0.0,-0.3 0.1,-0.6 0.1,-1.0s0.0,-0.7 -0.1,-1.0l2.1,-1.7c0.2,-0.2 0.2,-0.4 0.1,-0.6l-2.0,-3.5C19.5,5.1 19.3,5.0 19.0,5.1l-2.5,1.0c-0.5,-0.4 -1.1,-0.7 -1.7,-1.0l-0.4,-2.6C14.5,2.2 14.2,2.0 14.0,2.0l-4.0,0.0C9.8,2.0 9.5,2.2 9.5,2.4L9.1,5.1C8.5,5.3 8.0,5.7 7.4,6.1L5.0,5.1C4.7,5.0 4.5,5.1 4.3,5.3l-2.0,3.5C2.2,8.9 2.3,9.2 2.5,9.4L4.6,11.0c0.0,0.3 -0.1,0.6 -0.1,1.0s0.0,0.7 0.1,1.0l-2.1,1.7c-0.2,0.2 -0.2,0.4 -0.1,0.6l2.0,3.5C4.5,18.9 4.7,19.0 5.0,18.9l2.5,-1.0c0.5,0.4 1.1,0.7 1.7,1.0l0.4,2.6c0.0,0.2 0.2,0.4 0.5,0.4l4.0,0.0c0.2,0.0 0.5,-0.2 0.5,-0.4l0.4,-2.6c0.6,-0.3 1.2,-0.6 1.7,-1.0l2.5,1.0c0.2,0.1 0.5,0.0 0.6,-0.2l2.0,-3.5c0.1,-0.2 0.1,-0.5 -0.1,-0.6L19.4,13.0zM12.0,15.5c-1.9,0.0 -3.5,-1.6 -3.5,-3.5s1.6,-3.5 3.5,-3.5s3.5,1.6 3.5,3.5S13.9,15.5 12.0,15.5z"
+        android:fillColor="#ffffffff" />
+</vector>
diff --git a/packages/SystemUI/res/drawable/lockscreen_fingerprint_error_state_to_fp.xml b/packages/SystemUI/res/drawable/lockscreen_fingerprint_error_state_to_fp.xml
index 67e6a01..e207cb3 100644
--- a/packages/SystemUI/res/drawable/lockscreen_fingerprint_error_state_to_fp.xml
+++ b/packages/SystemUI/res/drawable/lockscreen_fingerprint_error_state_to_fp.xml
@@ -98,11 +98,11 @@
             <path
                 android:name="path_2"
                 android:pathData="M 1.35900878906,6.76104736328 c 0.0,0.0 -2.69998168945,0.0 -2.69998168945,0.0 c 0.0,0.0 0.0,-2.69995117188 0.0,-2.69995117188 c 0.0,0.0 2.69998168945,0.0 2.69998168945,0.0 c 0.0,0.0 0.0,2.69995117188 0.0,2.69995117188 Z"
-                android:fillColor="#FFF2501D" />
+                android:fillColor="@*android:color/system_error" />
             <path
                 android:name="path_1"
                 android:pathData="M 1.35363769531,1.36633300781 c 0.0,0.0 -2.69998168945,0.0 -2.69998168945,0.0 c 0.0,0.0 0.0,-8.09997558594 0.0,-8.09997558594 c 0.0,0.0 2.69998168945,0.0 2.69998168945,0.0 c 0.0,0.0 0.0,8.09997558594 0.0,8.09997558594 Z"
-                android:fillColor="#FFF2501D" />
+                android:fillColor="@*android:color/system_error" />
         </group>
     </group>
     <group
@@ -117,7 +117,7 @@
             <path
                 android:name="path_3"
                 android:pathData="M 0.0101470947266,10.8087768555 c -5.96701049805,0.0 -10.8000183105,-4.8330078125 -10.8000183105,-10.8000488281 c 0.0,-5.96691894531 4.8330078125,-10.7999267578 10.8000183105,-10.7999267578 c 5.96697998047,0.0 10.799987793,4.8330078125 10.799987793,10.7999267578 c 0.0,5.96704101562 -4.8330078125,10.8000488281 -10.799987793,10.8000488281 Z"
-                android:strokeColor="#FFF2501D"
+                android:strokeColor="@*android:color/system_error"
                 android:strokeWidth="2"
                 android:trimPathStart="0"
                 android:trimPathEnd="1" />
diff --git a/packages/SystemUI/res/drawable/lockscreen_fingerprint_fp_to_error_state.xml b/packages/SystemUI/res/drawable/lockscreen_fingerprint_fp_to_error_state.xml
index bbadec1..2b4babc 100644
--- a/packages/SystemUI/res/drawable/lockscreen_fingerprint_fp_to_error_state.xml
+++ b/packages/SystemUI/res/drawable/lockscreen_fingerprint_fp_to_error_state.xml
@@ -96,7 +96,7 @@
                 <path
                     android:name="ridge_5_path_0"
                     android:pathData="M -25.3591003418,-24.4138946533 c -0.569000244141,0.106399536133 -1.12660217285,0.140594482422 -1.45460510254,0.140594482422 c -1.29689025879,0.0 -2.53239440918,-0.343307495117 -3.62019348145,-1.12400817871 c -1.67700195312,-1.20349121094 -2.76950073242,-3.17008972168 -2.76950073242,-5.39189147949"
-                    android:strokeColor="#FFF2501D"
+                    android:strokeColor="@*android:color/system_error"
                     android:strokeWidth="1.45"
                     android:strokeLineCap="round"
                     android:trimPathEnd="0" />
@@ -106,7 +106,7 @@
                 <path
                     android:name="ridge_7_path_0"
                     android:pathData="M -36.1409912109,-21.7843475342 c -1.00540161133,-1.19300842285 -1.57499694824,-1.9181060791 -2.36520385742,-3.50170898438 c -0.827560424805,-1.65869140625 -1.31352233887,-3.49159240723 -1.31352233887,-5.48489379883 c 0.0,-3.66279602051 2.96932983398,-6.63220214844 6.63221740723,-6.63220214844 c 3.6628112793,0.0 6.63220214844,2.96940612793 6.63220214844,6.63220214844"
-                    android:strokeColor="#FFF2501D"
+                    android:strokeColor="@*android:color/system_error"
                     android:strokeWidth="1.45"
                     android:strokeLineCap="round"
                     android:trimPathEnd="0" />
@@ -116,7 +116,7 @@
                 <path
                     android:name="ridge_6_path_0"
                     android:pathData="M -42.1907958984,-25.6756896973 c -0.758117675781,-2.14370727539 -0.896545410156,-3.86891174316 -0.896545410156,-5.12921142578 c 0.0,-1.46069335938 0.249176025391,-2.84799194336 0.814682006836,-4.09748840332 c 1.56153869629,-3.45030212402 5.03434753418,-5.85076904297 9.0679473877,-5.85076904297 c 5.49430847168,0.0 9.94830322266,4.4539642334 9.94830322266,9.94825744629 c 0.0,1.83151245117 -1.48460388184,3.31610107422 -3.31610107422,3.31610107422 c -1.83149719238,0.0 -3.31610107422,-1.48469543457 -3.31610107422,-3.31610107422 c 0.0,-1.83139038086 -1.48458862305,-3.31610107422 -3.31610107422,-3.31610107422 c -1.83149719238,0.0 -3.31610107422,1.48471069336 -3.31610107422,3.31610107422 c 0.0,2.57020568848 0.989517211914,4.88710021973 2.60510253906,6.5865020752 c 1.22210693359,1.28550720215 2.43139648438,2.09950256348 4.47590637207,2.69030761719"
-                    android:strokeColor="#FFF2501D"
+                    android:strokeColor="@*android:color/system_error"
                     android:strokeWidth="1.45"
                     android:strokeLineCap="round"
                     android:trimPathEnd="0" />
@@ -126,7 +126,7 @@
                 <path
                     android:name="ridge_2_path_0"
                     android:pathData="M -44.0646514893,-38.1672973633 c 1.19026184082,-1.77430725098 2.67503356934,-3.24531555176 4.55902099609,-4.27278137207 c 1.88395690918,-1.0274810791 4.04466247559,-1.61137390137 6.34175109863,-1.61137390137 c 2.28761291504,0.0 4.43991088867,0.579071044922 6.31831359863,1.59861755371 c 1.8784942627,1.01954650879 3.36059570312,2.4796295166 4.55279541016,4.24153137207"
-                    android:strokeColor="#FFF2501D"
+                    android:strokeColor="@*android:color/system_error"
                     android:strokeWidth="1.45"
                     android:strokeLineCap="round"
                     android:trimPathStart="1" />
@@ -138,7 +138,7 @@
                 <path
                     android:name="ridge_1_path_0"
                     android:pathData="M 71.7812347412,97.0507202148 c -2.27149963379,-1.31344604492 -4.71360778809,-2.07006835938 -7.56221008301,-2.07006835938 c -2.84869384766,0.0 -5.23320007324,0.779556274414 -7.34411621094,2.07006835938"
-                    android:strokeColor="#FFF2501D"
+                    android:strokeColor="@*android:color/system_error"
                     android:strokeWidth="1.45"
                     android:strokeLineCap="round"
                     android:trimPathEnd="0" />
@@ -157,11 +157,11 @@
             <path
                 android:name="path_2"
                 android:pathData="M -1.3705291748,4.06730651855 c 0.0,0.0 0.036376953125,-0.0102386474609 0.036376953125,-0.0102386474609 c 0.0,0.0 -0.00682067871094,0.0040283203125 -0.00682067871093,0.0040283203125 c 0.0,0.0 -0.0161437988281,0.00479125976562 -0.0161437988281,0.00479125976563 c 0.0,0.0 -0.0134124755859,0.00141906738281 -0.0134124755859,0.00141906738281 Z"
-                android:fillColor="#FFF2501D" />
+                android:fillColor="@*android:color/system_error" />
             <path
                 android:name="path_1"
                 android:pathData="M -1.3737487793,-6.77532958984 c 0.0,0.0 0.00604248046875,0.0166625976562 0.00604248046876,0.0166625976562 c 0.0,0.0 0.0213623046875,0.0250244140625 0.0213623046875,0.0250244140625 c 0.0,0.0 -0.00604248046875,-0.0166625976562 -0.00604248046876,-0.0166625976562 c 0.0,0.0 -0.0213623046875,-0.0250244140625 -0.0213623046875,-0.0250244140625 Z"
-                android:fillColor="#FFF2501D" />
+                android:fillColor="@*android:color/system_error" />
         </group>
     </group>
     <group
@@ -176,7 +176,7 @@
             <path
                 android:name="path_3"
                 android:pathData="M 0.0101470947266,10.8087768555 c -5.96701049805,0.0 -10.8000183105,-4.8330078125 -10.8000183105,-10.8000488281 c 0.0,-5.96691894531 4.8330078125,-10.7999267578 10.8000183105,-10.7999267578 c 5.96697998047,0.0 10.799987793,4.8330078125 10.799987793,10.7999267578 c 0.0,5.96704101562 -4.8330078125,10.8000488281 -10.799987793,10.8000488281 Z"
-                android:strokeColor="#FFF2501D"
+                android:strokeColor="@*android:color/system_error"
                 android:strokeWidth="2"
                 android:trimPathStart="1" />
         </group>
diff --git a/packages/SystemUI/res/drawable/qs_background_primary.xml b/packages/SystemUI/res/drawable/qs_background_primary.xml
index 1bf7d4c..8ea9e06 100644
--- a/packages/SystemUI/res/drawable/qs_background_primary.xml
+++ b/packages/SystemUI/res/drawable/qs_background_primary.xml
@@ -15,6 +15,6 @@
 -->
 <inset xmlns:android="http://schemas.android.com/apk/res/android">
     <shape>
-        <solid android:color="@color/system_primary_color"/>
+        <solid android:color="?android:attr/colorPrimary"/>
     </shape>
 </inset>
diff --git a/packages/SystemUI/res/drawable/qs_customizer_background.xml b/packages/SystemUI/res/drawable/qs_customizer_background.xml
index d90f820..12d8016 100644
--- a/packages/SystemUI/res/drawable/qs_customizer_background.xml
+++ b/packages/SystemUI/res/drawable/qs_customizer_background.xml
@@ -15,5 +15,5 @@
 -->
 <transition xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@color/qs_detail_transition" />
-    <item android:drawable="@color/system_primary_color" />
+    <item android:drawable="?android:attr/colorPrimary" />
 </transition>
diff --git a/packages/SystemUI/res/drawable/qs_detail_background.xml b/packages/SystemUI/res/drawable/qs_detail_background.xml
index 692cd44..84c793f 100644
--- a/packages/SystemUI/res/drawable/qs_detail_background.xml
+++ b/packages/SystemUI/res/drawable/qs_detail_background.xml
@@ -15,5 +15,5 @@
 -->
 <transition xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:drawable="@color/qs_detail_transition" />
-    <item android:drawable="@color/system_primary_color" />
+    <item android:drawable="?android:attr/colorPrimary" />
 </transition>
\ No newline at end of file
diff --git a/packages/SystemUI/res/drawable/quick_header_bg.xml b/packages/SystemUI/res/drawable/quick_header_bg.xml
index d45d673..920e6f5 100644
--- a/packages/SystemUI/res/drawable/quick_header_bg.xml
+++ b/packages/SystemUI/res/drawable/quick_header_bg.xml
@@ -17,5 +17,5 @@
 
 <ripple xmlns:android="http://schemas.android.com/apk/res/android"
     android:color="?android:attr/colorControlHighlight" >
-    <item android:drawable="@color/system_primary_color"/>
+    <item android:drawable="?android:attr/colorPrimary"/>
 </ripple>
diff --git a/packages/SystemUI/res/drawable/recents_stack_action_background.xml b/packages/SystemUI/res/drawable/recents_stack_action_background.xml
new file mode 100644
index 0000000..2a40dd0
--- /dev/null
+++ b/packages/SystemUI/res/drawable/recents_stack_action_background.xml
@@ -0,0 +1,25 @@
+<!--
+Copyright (C) 2016 The Android Open Source Project
+
+   Licensed under the Apache License, Version 2 (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.
+-->
+
+<ripple xmlns:android="http://schemas.android.com/apk/res/android"
+        android:color="?android:attr/colorControlHighlight">
+    <item android:id="@android:id/mask">
+        <shape>
+            <corners android:radius="@dimen/recents_task_view_rounded_corners_radius" />
+            <solid android:color="@android:color/white" />
+        </shape>
+    </item>
+</ripple>
\ No newline at end of file
diff --git a/packages/SystemUI/res/drawable/screen_pinning_bg_circ.xml b/packages/SystemUI/res/drawable/screen_pinning_bg_circ.xml
index 354b8bd..1303bdb 100644
--- a/packages/SystemUI/res/drawable/screen_pinning_bg_circ.xml
+++ b/packages/SystemUI/res/drawable/screen_pinning_bg_circ.xml
@@ -19,7 +19,7 @@
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
     android:shape="oval" >
 
-    <solid android:color="@color/system_accent_color" />
+    <solid android:color="?android:attr/colorAccent" />
 
     <size
         android:height="@dimen/screen_pinning_nav_highlight_size"
diff --git a/packages/SystemUI/res/drawable/stat_sys_branded_vpn.xml b/packages/SystemUI/res/drawable/stat_sys_branded_vpn.xml
new file mode 100644
index 0000000..a86e5b9
--- /dev/null
+++ b/packages/SystemUI/res/drawable/stat_sys_branded_vpn.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="17.0dp"
+        android:height="17.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M12.700000,10.000000c-0.800000,-2.300000 -3.000000,-4.000000 -5.700000,-4.000000c-3.300000,0.000000 -6.000000,2.700000 -6.000000,6.000000s2.700000,6.000000 6.000000,6.000000c2.600000,0.000000 4.800000,-1.700000 5.700000,-4.000000L17.000000,14.000000l0.000000,4.000000l4.000000,0.000000l0.000000,-4.000000l2.000000,0.000000l0.000000,-4.000000L12.700000,10.000000zM7.000000,14.000000c-1.100000,0.000000 -2.000000,-0.900000 -2.000000,-2.000000c0.000000,-1.100000 0.900000,-2.000000 2.000000,-2.000000s2.000000,0.900000 2.000000,2.000000C9.000000,13.100000 8.100000,14.000000 7.000000,14.000000z"/>
+</vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_data_disabled.xml b/packages/SystemUI/res/drawable/stat_sys_data_disabled.xml
index 4e2a024..694019e9 100644
--- a/packages/SystemUI/res/drawable/stat_sys_data_disabled.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_data_disabled.xml
@@ -14,9 +14,9 @@
     limitations under the License.
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="17.0dp"
+        android:width="8.5dp"
         android:height="17.0dp"
-        android:viewportWidth="40.0"
+        android:viewportWidth="20.0"
         android:viewportHeight="40.0">
     <path
         android:fillColor="#FFFFFFFF"
diff --git a/packages/SystemUI/res/drawable/stat_sys_data_fully_connected_4g_plus.xml b/packages/SystemUI/res/drawable/stat_sys_data_fully_connected_4g_plus.xml
new file mode 100644
index 0000000..3cdd3e1
--- /dev/null
+++ b/packages/SystemUI/res/drawable/stat_sys_data_fully_connected_4g_plus.xml
@@ -0,0 +1,30 @@
+<!--
+    Copyright (C) 2016 The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="17.0dp"
+        android:height="17.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M4.6,7.8l0.7,0.0l0.0,1.3L4.6,9.1L4.6,11.0L3.0,11.0L3.0,9.2L0.1,9.2L0.0,8.2l3.0,-5.7l1.7,0.0L4.6,7.8L4.6,7.8zM1.7,7.8L3.0,7.8l0.0,-3.0L2.9,5.0L1.7,7.8z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M11.9,9.9c-0.2,0.4 -0.6,0.7 -1.0,0.9s-1.0,0.4 -1.8,0.4c-0.9,0.0 -1.7,-0.3 -2.2,-0.8S6.1,9.0 6.1,7.9L6.1,5.6c0.0,-1.1 0.3,-1.9 0.8,-2.4S8.2,2.4 9.0,2.4c1.0,0.0 1.7,0.2 2.1,0.7s0.7,1.2 0.7,2.1l-1.6,0.0c0.0,-0.5 -0.1,-0.9 -0.2,-1.1S9.5,3.7 9.0,3.7c-0.4,0.0 -0.7,0.2 -0.9,0.5S7.8,5.0 7.8,5.6l0.0,2.3c0.0,0.7 0.1,1.1 0.3,1.4c0.2,0.3 0.6,0.5 1.0,0.5c0.3,0.0 0.6,0.0 0.7,-0.1s0.3,-0.2 0.4,-0.3L10.2,7.8L9.0,7.8L9.0,6.6l2.9,0.0L11.9,9.9z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M17.7,4.4l-1.900001,0.0 0.0,-1.9 -1.2,0.0 0.0,1.9 -1.900001,0.0 0.0,1.2 1.900001,0.0 0.0,1.9 1.2,0.0 0.0,-1.9 1.900001,0.0z"/>
+</vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_data_fully_connected_lte_plus.xml b/packages/SystemUI/res/drawable/stat_sys_data_fully_connected_lte_plus.xml
new file mode 100644
index 0000000..db18fad
--- /dev/null
+++ b/packages/SystemUI/res/drawable/stat_sys_data_fully_connected_lte_plus.xml
@@ -0,0 +1,33 @@
+<!--
+    Copyright (C) 2016 The Android Open Source Project
+
+    Licensed under the Apache License, Version 2.0 (the "License");
+    you may not use this file except in compliance with the License.
+    You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="17.0dp"
+        android:height="17.0dp"
+        android:viewportWidth="24.0"
+        android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M2.0,9.7l2.0,0.0L4.0,11.0L0.4,11.0L0.4,2.5L2.0,2.5L2.0,9.7z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M8.3,3.8L7.0,3.8L7.0,11.0L5.4,11.0L5.4,3.8L4.0,3.8L4.0,2.5l4.3,0.0L8.3,3.8z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M12.4,7.3l-1.7,0.0l0.0,2.4l2.1,0.0L12.799999,11.0L9.0,11.0L9.0,2.5l3.7,0.0l0.0,1.3l-2.1,0.0l0.0,2.1l1.7,0.0L12.4,7.3L12.4,7.3z"/>
+    <path
+        android:fillColor="#FFFFFFFF"
+        android:pathData="M18.4,4.4l-1.9,0.0 0.0,-1.9 -1.2,0.0 0.0,1.9 -1.900001,0.0 0.0,1.2 1.900001,0.0 0.0,1.9 1.2,0.0 0.0,-1.9 1.9,0.0z"/>
+</vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_signal_0.xml b/packages/SystemUI/res/drawable/stat_sys_signal_0.xml
index 643c4f9..8bc872a 100644
--- a/packages/SystemUI/res/drawable/stat_sys_signal_0.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_signal_0.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,17 +15,14 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="17dp"
-        android:height="17dp"
+        android:width="17.0dp"
+        android:height="17.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.700001,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.700001,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
-    <path
-        android:pathData="M17.700001,8.000000l4.299999,0.000000 0.000000,-6.000000 -20.000000,20.000000 15.700001,0.000000z"
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
         android:fillColor="?attr/backgroundColor"/>
+    <path
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_signal_1.xml b/packages/SystemUI/res/drawable/stat_sys_signal_1.xml
index 64781c3..8fa7630 100644
--- a/packages/SystemUI/res/drawable/stat_sys_signal_1.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_signal_1.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,20 +15,17 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="17dp"
-        android:height="17dp"
+        android:width="17.0dp"
+        android:height="17.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.7,20.0l2.0,0.0l0.0,2.0l-2.0,0.0z"/>
+        android:pathData="M10.0,14.6l-8.0,8.0l8.0,0.0l0,-8z"
+        android:fillColor="?attr/fillColor"/>
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.7,10.0l2.0,0.0l0.0,8.1l-2.0,0.0z"/>
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
+        android:fillColor="?attr/backgroundColor"/>
     <path
-        android:fillColor="?attr/backgroundColor"
-        android:pathData="M17.7,8.0l4.299999,0.0 0.0,-6.0 -20.0,20.0 15.700001,0.0z"/>
-    <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M10.1,13.9l-8.1,8.1 8.1,0.0z"/>
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_signal_2.xml b/packages/SystemUI/res/drawable/stat_sys_signal_2.xml
index eb2be08..2a660a3 100644
--- a/packages/SystemUI/res/drawable/stat_sys_signal_2.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_signal_2.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,20 +15,17 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="17dp"
-        android:height="17dp"
+        android:width="17.0dp"
+        android:height="17.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.700001,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
+        android:pathData="M14.0,10.6l-12.0,12.0l12.0,0.0L14.0,10.6z"
+        android:fillColor="?attr/fillColor"/>
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.700001,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M13.900000,10.000000l-11.900000,12.000000 11.900000,0.000000z"/>
-    <path
-        android:pathData="M17.700001,8.000000l4.299999,0.000000 0.000000,-6.000000 -20.000000,20.000000 15.700001,0.000000z"
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
         android:fillColor="?attr/backgroundColor"/>
+    <path
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_signal_3.xml b/packages/SystemUI/res/drawable/stat_sys_signal_3.xml
index 22afad0..9e0a433 100644
--- a/packages/SystemUI/res/drawable/stat_sys_signal_3.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_signal_3.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,20 +15,17 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="17dp"
-        android:height="17dp"
+        android:width="17.0dp"
+        android:height="17.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.700001,19.900000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.700001,9.900000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M16.700001,7.200000l-14.700001,14.700000 14.700001,0.000000z"/>
-    <path
-        android:pathData="M17.700001,7.900000l4.299999,0.000000 0.000000,-6.000000 -20.000000,20.000000 15.700001,0.000000z"
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
         android:fillColor="?attr/backgroundColor"/>
+    <path
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="?attr/fillColor"/>
+    <path
+        android:pathData="M14.1,14.1l2.9,0.0 0.0,-6.5 -15.0,15.0 12.1,0.0z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_signal_4.xml b/packages/SystemUI/res/drawable/stat_sys_signal_4.xml
index d1e866d..01f6703 100644
--- a/packages/SystemUI/res/drawable/stat_sys_signal_4.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_signal_4.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,18 +15,14 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:autoMirrored="true"
-        android:width="17dp"
-        android:height="17dp"
+        android:width="17.0dp"
+        android:height="17.0dp"
         android:viewportWidth="24.0"
         android:viewportHeight="24.0">
-
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.700001,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
+        android:pathData="M14.1,14.1l7.9,0.0 0.0,-11.5 -20.0,20.0 12.1,0.0z"
+        android:fillColor="?attr/fillColor"/>
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.700001,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M2.000000,22.000000l15.700001,0.000000 0.000000,-14.000000 4.299999,0.000000 0.000000,-6.000000z"/>
+        android:pathData="M21.9,17.0l-1.1,-1.1 -1.9,1.9 -1.9,-1.9 -1.1,1.1 1.9,1.9 -1.9,1.9 1.1,1.1 1.9,-1.9 1.9,1.9 1.1,-1.1 -1.9,-1.9z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_0.xml b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_0.xml
index 7f1b715e..2de2e36 100644
--- a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_0.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_0.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,16 +15,13 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:width="18.41dp"
-        android:height="17dp"
-        android:viewportWidth="26.0"
+        android:height="18.41dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/backgroundColor"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.200000,-1.500000C25.100000,6.100000 20.299999,2.100000 13.000000,2.100000S0.900000,6.100000 0.400000,6.500000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillColor="?attr/backgroundColor"/>
     <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.9,1.9 -1.9,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.799999,-1.800001z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_1.xml b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_1.xml
index acd89be..144a7c1 100644
--- a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_1.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_1.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,19 +15,16 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:width="18.41dp"
-        android:height="17dp"
-        android:viewportWidth="26.0"
+        android:height="18.41dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/backgroundColor"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.300000,-1.600000C25.100000,6.000000 20.299999,2.000000 13.000000,2.000000S0.900000,6.000000 0.400000,6.400000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,13.2c-0.1,0.0 -0.3,-0.1 -0.4,-0.1c-0.1,0.0 -0.3,0.0 -0.4,-0.1c-0.3,0.0 -0.6,-0.1 -0.9,-0.1c0.0,0.0 0.0,0.0 -0.1,0.0c0.0,0.0 0.0,0.0 0.0,0.0s0.0,0.0 0.0,0.0c0.0,0.0 0.0,0.0 -0.1,0.0c-0.3,0.0 -0.6,0.0 -0.9,0.1c-0.1,0.0 -0.3,0.0 -0.4,0.1c-0.2,0.0 -0.3,0.1 -0.5,0.1c-0.2,0.0 -0.3,0.1 -0.5,0.1c-0.1,0.0 -0.1,0.0 -0.2,0.1c-1.6,0.5 -2.7,1.3 -2.8,1.5l5.3,6.6l0.0,0.0l0.0,0.0l0.0,0.0l0.0,0.0l1.8,-2.2L13.700002,13.2z"
+        android:fillColor="?attr/fillColor"/>
     <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M13.000000,22.000000l5.500000,-6.800000c-0.200000,-0.200000 -2.300000,-1.900000 -5.500000,-1.900000s-5.300000,1.800000 -5.500000,1.900000L13.000000,22.000000L13.000000,22.000000L13.000000,22.000000L13.000000,22.000000L13.000000,22.000000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillColor="?attr/backgroundColor"/>
     <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.9,1.9 -1.9,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.799999,-1.800001z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_2.xml b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_2.xml
index f33b25c..6b7f712 100644
--- a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_2.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_2.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,19 +15,16 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:width="18.41dp"
-        android:height="17dp"
-        android:viewportWidth="26.0"
+        android:height="18.41dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/backgroundColor"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.300000,-1.600000C25.100000,6.000000 20.299999,2.000000 13.000000,2.000000S0.900000,6.000000 0.400000,6.400000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,12.2l4.9,0.0c-1.0,-0.7 -3.4,-2.2 -6.7,-2.2c-4.1,0.0 -6.9,2.2 -7.2,2.5l7.2,9.0l0.0,0.0l0.0,0.0l1.8,-2.2L13.800001,12.2z"
+        android:fillColor="?attr/fillColor"/>
     <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M19.000000,11.600000c-1.300000,-0.700000 -3.400000,-1.600000 -6.000000,-1.600000c-4.400000,0.000000 -7.300000,2.400000 -7.600000,2.700000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,11.600000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillColor="?attr/backgroundColor"/>
     <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.800001,1.9 -1.800001,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.9,-1.800001z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_3.xml b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_3.xml
index 09d2e50..d34b4de 100644
--- a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_3.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_3.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,19 +15,16 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:width="18.41dp"
-        android:height="17dp"
-        android:viewportWidth="26.0"
+        android:height="18.41dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/backgroundColor"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.300000,-1.600000C25.100000,6.000000 20.299999,2.000000 13.000000,2.000000S0.900000,6.000000 0.400000,6.400000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0l1.0,-1.2C20.0,10.6 16.8,8.0 12.0,8.0s-8.0,2.6 -8.5,3.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillColor="?attr/fillColor"/>
     <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M19.000000,8.600000c-1.600000,-0.700000 -3.600000,-1.300000 -6.000000,-1.300000c-5.300000,0.000000 -8.900000,3.000000 -9.200000,3.200000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.600000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillColor="?attr/backgroundColor"/>
     <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/fillColor"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.9,1.9 -1.9,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.9,-1.800001z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_4.xml b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_4.xml
index fb1f584..5701356 100644
--- a/packages/SystemUI/res/drawable/stat_sys_wifi_signal_4.xml
+++ b/packages/SystemUI/res/drawable/stat_sys_wifi_signal_4.xml
@@ -1,7 +1,7 @@
 <!--
-Copyright (C) 2014 The Android Open Source Project
+    Copyright (C) 2016 The Android Open Source Project
 
-   Licensed under the Apache License, Version 2.0 (the "License");
+    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
 
@@ -15,16 +15,13 @@
 -->
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
         android:width="18.41dp"
-        android:height="17dp"
-        android:viewportWidth="26.0"
+        android:height="18.41dp"
+        android:viewportWidth="24.0"
         android:viewportHeight="24.0">
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M19.000000,8.000000l5.300000,0.000000l1.300000,-1.600000C25.100000,6.000000 20.299999,2.000000 13.000000,2.000000S0.900000,6.000000 0.400000,6.400000L13.000000,22.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l0.000000,0.000000l6.000000,-7.400000L19.000000,8.000000z"/>
+        android:pathData="M13.8,12.2l5.7,0.0L23.6,7.0C23.2,6.7 18.7,3.0 12.0,3.0C5.3,3.0 0.8,6.7 0.4,7.0L12.0,21.5l0.0,0.0l0.0,0.0l1.8,-2.2L13.8,12.2z"
+        android:fillColor="?attr/fillColor"/>
     <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M21.000000,20.000000l2.000000,0.000000l0.000000,2.000000l-2.000000,0.000000z"/>
-    <path
-        android:fillColor="?attr/singleToneColor"
-        android:pathData="M21.000000,10.000000l2.000000,0.000000l0.000000,8.100000l-2.000000,0.000000z"/>
+        android:pathData="M21.9,15.4l-1.1,-1.2 -1.9,1.900001 -1.9,-1.900001 -1.1,1.2 1.9,1.9 -1.9,1.800001 1.1,1.199999 1.9,-1.9 1.9,1.9 1.1,-1.199999 -1.9,-1.800001z"
+        android:fillColor="?attr/fillColor"/>
 </vector>
diff --git a/packages/SystemUI/res/drawable/switchbar_background.xml b/packages/SystemUI/res/drawable/switchbar_background.xml
index 8d97c46..fb59633 100644
--- a/packages/SystemUI/res/drawable/switchbar_background.xml
+++ b/packages/SystemUI/res/drawable/switchbar_background.xml
@@ -16,6 +16,6 @@
 
 <ripple xmlns:android="http://schemas.android.com/apk/res/android"
     android:color="?android:attr/colorControlHighlight">
-    <item android:drawable="@color/switch_bar_background" />
+    <item android:drawable="?android:attr/colorSecondary" />
 </ripple>
 
diff --git a/packages/SystemUI/res/drawable/volume_dialog_background.xml b/packages/SystemUI/res/drawable/volume_dialog_background.xml
index e98bfb8..d6adea9 100644
--- a/packages/SystemUI/res/drawable/volume_dialog_background.xml
+++ b/packages/SystemUI/res/drawable/volume_dialog_background.xml
@@ -14,5 +14,5 @@
      limitations under the License.
 -->
 <shape xmlns:android="http://schemas.android.com/apk/res/android" >
-    <solid android:color="@color/system_primary_color" />
+    <solid android:color="?android:attr/colorPrimary" />
 </shape>
\ No newline at end of file
diff --git a/packages/SystemUI/res/drawable/zen_introduction_message_background.xml b/packages/SystemUI/res/drawable/zen_introduction_message_background.xml
index 352fb57..e5b41a3 100644
--- a/packages/SystemUI/res/drawable/zen_introduction_message_background.xml
+++ b/packages/SystemUI/res/drawable/zen_introduction_message_background.xml
@@ -17,6 +17,6 @@
 
       <corners android:radius="@dimen/borderless_button_radius" />
 
-      <solid android:color="@color/zen_introduction_message_background" />
+      <solid android:color="?android:attr/colorAccent" />
 
 </shape>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout-sw410dp/status_bar_alarm_group.xml b/packages/SystemUI/res/layout-sw410dp/status_bar_alarm_group.xml
index ba5c0aa..a726161 100644
--- a/packages/SystemUI/res/layout-sw410dp/status_bar_alarm_group.xml
+++ b/packages/SystemUI/res/layout-sw410dp/status_bar_alarm_group.xml
@@ -20,7 +20,7 @@
     android:id="@+id/date_time_alarm_group"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:layout_marginTop="8dp"
+    android:layout_marginTop="16dp"
     android:layout_marginStart="16dp"
     android:gravity="start"
     android:orientation="vertical">
@@ -62,7 +62,8 @@
     <com.android.systemui.statusbar.AlphaOptimizedButton
         android:id="@+id/alarm_status"
         android:layout_width="wrap_content"
-        android:layout_height="20dp"
+        android:layout_height="wrap_content"
+        android:minHeight="20dp"
         android:paddingTop="3dp"
         android:drawablePadding="8dp"
         android:drawableStart="@drawable/ic_access_alarms_small"
diff --git a/packages/SystemUI/res/layout/battery_detail.xml b/packages/SystemUI/res/layout/battery_detail.xml
index 1f24ab0..8abfcf6 100644
--- a/packages/SystemUI/res/layout/battery_detail.xml
+++ b/packages/SystemUI/res/layout/battery_detail.xml
@@ -25,7 +25,7 @@
         android:id="@+id/charge_and_estimation"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:paddingStart="72dp"
+        android:paddingStart="16dp"
         android:textAppearance="?android:attr/textAppearanceSmall"
         android:textColor="?android:attr/colorAccent" />
 
diff --git a/packages/SystemUI/res/layout/calibrate_sliders.xml b/packages/SystemUI/res/layout/calibrate_sliders.xml
deleted file mode 100644
index 0dec8a1..0000000
--- a/packages/SystemUI/res/layout/calibrate_sliders.xml
+++ /dev/null
@@ -1,97 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-     Copyright (C) 2015 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.
--->
-
-<LinearLayout
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
-    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
-    android:paddingTop="8dp"
-    android:paddingBottom="8dp"
-    android:orientation="vertical">
-
-    <LinearLayout
-        android:id="@+id/r_group"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:paddingTop="4dp"
-        android:paddingBottom="4dp"
-        android:orientation="horizontal">
-
-        <TextView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:text="@string/color_modification_r"
-            android:gravity="center"
-            android:textAppearance="?android:attr/textAppearanceMedium" />
-
-        <SeekBar android:id="@*android:id/seekbar"
-            android:layout_marginStart="16dp"
-            android:layout_gravity="center_vertical"
-            android:layout_width="0dp"
-            android:layout_height="wrap_content"
-            android:layout_weight="1" />
-    </LinearLayout>
-
-    <LinearLayout
-        android:id="@+id/g_group"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:paddingTop="4dp"
-        android:paddingBottom="4dp"
-        android:orientation="horizontal">
-
-        <TextView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:text="@string/color_modification_g"
-            android:gravity="center"
-            android:textAppearance="?android:attr/textAppearanceMedium" />
-
-        <SeekBar android:id="@*android:id/seekbar"
-            android:layout_marginStart="16dp"
-            android:layout_gravity="center_vertical"
-            android:layout_width="0dp"
-            android:layout_height="wrap_content"
-            android:layout_weight="1" />
-    </LinearLayout>
-
-    <LinearLayout
-        android:id="@+id/b_group"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:paddingTop="4dp"
-        android:paddingBottom="4dp"
-        android:orientation="horizontal">
-
-        <TextView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:text="@string/color_modification_b"
-            android:gravity="center"
-            android:textAppearance="?android:attr/textAppearanceMedium" />
-
-        <SeekBar android:id="@*android:id/seekbar"
-            android:layout_marginStart="16dp"
-            android:layout_gravity="center_vertical"
-            android:layout_width="0dp"
-            android:layout_height="wrap_content"
-            android:layout_weight="1" />
-    </LinearLayout>
-
-</LinearLayout>
diff --git a/packages/SystemUI/res/layout/emergency_cryptkeeper_text.xml b/packages/SystemUI/res/layout/emergency_cryptkeeper_text.xml
new file mode 100644
index 0000000..0a1730a
--- /dev/null
+++ b/packages/SystemUI/res/layout/emergency_cryptkeeper_text.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2016 The Android Open Source Project
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~      http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License
+  -->
+
+<com.android.systemui.statusbar.policy.EmergencyCryptkeeperText
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/emergency_cryptkeeper_text"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:textAppearance="@style/TextAppearance.StatusBar.Clock"
+        android:paddingStart="6dp"
+        android:singleLine="true"
+        android:ellipsize="marquee"
+        android:gravity="center_vertical|start"
+        />
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/keyboard_shortcut_app_item.xml b/packages/SystemUI/res/layout/keyboard_shortcut_app_item.xml
index 52cab72..69e52c5 100644
--- a/packages/SystemUI/res/layout/keyboard_shortcut_app_item.xml
+++ b/packages/SystemUI/res/layout/keyboard_shortcut_app_item.xml
@@ -53,5 +53,6 @@
             android:layout_alignParentEnd="true"
             android:textSize="14sp"
             android:scrollHorizontally="false"
-            android:layout_centerVertical="true"/>
+            android:layout_centerVertical="true"
+            android:focusable="true"/>
 </RelativeLayout>
diff --git a/packages/SystemUI/res/layout/keyboard_shortcuts_key_icon_view.xml b/packages/SystemUI/res/layout/keyboard_shortcuts_key_icon_view.xml
index 5db6789..a3901d0 100644
--- a/packages/SystemUI/res/layout/keyboard_shortcuts_key_icon_view.xml
+++ b/packages/SystemUI/res/layout/keyboard_shortcuts_key_icon_view.xml
@@ -21,4 +21,4 @@
         android:padding="@dimen/ksh_item_padding"
         android:layout_marginStart="@dimen/ksh_item_margin_start"
         android:scaleType="fitXY"
-        android:background="@drawable/ksh_key_item_background"/>
+        android:background="@drawable/ksh_key_item_background" />
diff --git a/packages/SystemUI/res/layout/keyboard_shortcuts_key_view.xml b/packages/SystemUI/res/layout/keyboard_shortcuts_key_view.xml
index 31a8773..b06f7fc 100644
--- a/packages/SystemUI/res/layout/keyboard_shortcuts_key_view.xml
+++ b/packages/SystemUI/res/layout/keyboard_shortcuts_key_view.xml
@@ -23,4 +23,5 @@
           android:textColor="@color/ksh_key_item_color"
           android:singleLine="true"
           android:gravity="center"
-          android:textSize="@dimen/ksh_item_text_size"/>
+          android:textSize="@dimen/ksh_item_text_size"
+          android:textAllCaps="true"/>
diff --git a/packages/SystemUI/res/layout/notification_guts.xml b/packages/SystemUI/res/layout/notification_guts.xml
index d09c42b..e84ed23 100644
--- a/packages/SystemUI/res/layout/notification_guts.xml
+++ b/packages/SystemUI/res/layout/notification_guts.xml
@@ -26,7 +26,8 @@
         android:orientation="vertical"
         android:paddingStart="@*android:dimen/notification_content_margin_start"
         android:paddingEnd="8dp"
-        android:background="@color/notification_guts_bg_color">
+        android:background="@color/notification_guts_bg_color"
+        android:theme="@*android:style/Theme.DeviceDefault.Light">
 
     <!-- header -->
     <LinearLayout
@@ -145,8 +146,8 @@
                     android:focusable="true"
                     android:background="#00ffffff"
                     android:progressBackgroundTint="@color/notification_guts_secondary_slider_color"
-                    android:thumbTint="@color/notification_guts_slider_color"
-                    android:progressTint="@color/notification_guts_slider_color"
+                    android:thumbTint="?android:attr/colorAccent"
+                    android:progressTint="?android:attr/colorAccent"
                     style="@android:style/Widget.Material.SeekBar.Discrete"
                     android:tickMarkTint="@android:color/black" />
 
diff --git a/packages/SystemUI/res/layout/qs_detail.xml b/packages/SystemUI/res/layout/qs_detail.xml
index 63390e2..967db26 100644
--- a/packages/SystemUI/res/layout/qs_detail.xml
+++ b/packages/SystemUI/res/layout/qs_detail.xml
@@ -47,11 +47,12 @@
     <com.android.systemui.qs.NonInterceptingScrollView
         android:layout_width="match_parent"
         android:layout_height="0dp"
-        android:layout_weight="1">
+        android:layout_weight="1"
+        android:fillViewport="true">
         <FrameLayout
             android:id="@android:id/content"
             android:layout_width="match_parent"
-            android:layout_height="wrap_content" />
+            android:layout_height="match_parent"/>
     </com.android.systemui.qs.NonInterceptingScrollView>
 
     <include layout="@layout/qs_detail_buttons" />
diff --git a/packages/SystemUI/res/layout/qs_detail_header.xml b/packages/SystemUI/res/layout/qs_detail_header.xml
index c062b6d..f6ca862 100644
--- a/packages/SystemUI/res/layout/qs_detail_header.xml
+++ b/packages/SystemUI/res/layout/qs_detail_header.xml
@@ -22,19 +22,9 @@
     android:background="@drawable/btn_borderless_rect"
     android:gravity="center">
 
-    <ImageView
-        android:id="@*android:id/up"
-        android:layout_width="56dp"
-        android:layout_height="56dp"
-        android:layout_marginEnd="16dp"
-        android:padding="16dp"
-        android:clickable="true"
-        android:background="?android:attr/selectableItemBackground"
-        android:contentDescription="@*android:string/action_bar_up_description"
-        android:src="?android:attr/homeAsUpIndicator" />
-
     <TextView
         android:id="@android:id/title"
+        android:paddingStart="16dp"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="1"
diff --git a/packages/SystemUI/res/layout/qs_detail_items.xml b/packages/SystemUI/res/layout/qs_detail_items.xml
index f1a8d63..c94a972 100644
--- a/packages/SystemUI/res/layout/qs_detail_items.xml
+++ b/packages/SystemUI/res/layout/qs_detail_items.xml
@@ -15,7 +15,8 @@
      limitations under the License.
 -->
 <!-- extends FrameLayout -->
-<com.android.systemui.qs.QSDetailItems xmlns:android="http://schemas.android.com/apk/res/android"
+<com.android.systemui.qs.QSDetailItems
+    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:sysui="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
@@ -28,26 +29,26 @@
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical"
-        sysui:itemHeight="@dimen/qs_detail_item_height" />
+        sysui:itemHeight="@dimen/qs_detail_item_height"/>
 
     <LinearLayout
         android:id="@android:id/empty"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
         android:layout_gravity="center"
-        android:gravity="center_horizontal"
-        android:orientation="vertical" >
+        android:gravity="center"
+        android:orientation="vertical">
 
         <ImageView
             android:id="@android:id/icon"
             android:layout_width="56dp"
-            android:layout_height="56dp" />
+            android:layout_height="56dp"/>
 
         <TextView
             android:id="@android:id/title"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_marginTop="20dp"
-            android:textAppearance="@style/TextAppearance.QS.DetailEmpty" />
+            android:textAppearance="@style/TextAppearance.QS.DetailEmpty"/>
     </LinearLayout>
 </com.android.systemui.qs.QSDetailItems>
diff --git a/packages/SystemUI/res/layout/qs_paged_tile_layout.xml b/packages/SystemUI/res/layout/qs_paged_tile_layout.xml
index ee55ec2..8b6060f 100644
--- a/packages/SystemUI/res/layout/qs_paged_tile_layout.xml
+++ b/packages/SystemUI/res/layout/qs_paged_tile_layout.xml
@@ -37,19 +37,6 @@
             android:importantForAccessibility="yes"
             android:focusable="true" />
 
-        <TextView
-            android:id="@android:id/edit"
-            style="@style/QSBorderlessButton"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_gravity="end"
-            android:minWidth="88dp"
-            android:textAppearance="@style/TextAppearance.QS.DetailButton"
-            android:textColor="#64FFFFFF"
-            android:focusable="true"
-            android:text="@string/qs_edit"
-            android:contentDescription="@string/accessibility_quick_settings_edit"/>
-
     </FrameLayout>
 
 </com.android.systemui.qs.PagedTileLayout>
diff --git a/packages/SystemUI/res/layout/qs_panel.xml b/packages/SystemUI/res/layout/qs_panel.xml
index 751f181..26c7339 100644
--- a/packages/SystemUI/res/layout/qs_panel.xml
+++ b/packages/SystemUI/res/layout/qs_panel.xml
@@ -26,7 +26,7 @@
     <com.android.systemui.qs.QSPanel
             android:id="@+id/quick_settings_panel"
             android:background="#0000"
-            android:layout_marginTop="@dimen/status_bar_header_height"
+            android:layout_marginTop="80dp"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:paddingBottom="8dp" />
diff --git a/packages/SystemUI/res/layout/qs_user_detail_item.xml b/packages/SystemUI/res/layout/qs_user_detail_item.xml
index 58fc069..8c6c7cf 100644
--- a/packages/SystemUI/res/layout/qs_user_detail_item.xml
+++ b/packages/SystemUI/res/layout/qs_user_detail_item.xml
@@ -28,6 +28,7 @@
         android:minHeight="112dp"
         android:clipChildren="false"
         android:clipToPadding="false"
+        android:focusable="true"
         android:background="@drawable/ripple_drawable"
         systemui:activatedFontFamily="sans-serif-medium">
 
@@ -65,4 +66,4 @@
                 android:visibility="gone" />
     </LinearLayout>
 
-</com.android.systemui.qs.tiles.UserDetailItemView>
\ No newline at end of file
+</com.android.systemui.qs.tiles.UserDetailItemView>
diff --git a/packages/SystemUI/res/layout/quick_status_bar_expanded_header.xml b/packages/SystemUI/res/layout/quick_status_bar_expanded_header.xml
index f65a667..6673d6e 100644
--- a/packages/SystemUI/res/layout/quick_status_bar_expanded_header.xml
+++ b/packages/SystemUI/res/layout/quick_status_bar_expanded_header.xml
@@ -37,13 +37,14 @@
         android:clipToPadding="false"
         android:orientation="horizontal"
         android:layout_alignParentEnd="true"
-        android:layout_marginTop="28dp"
+        android:layout_marginTop="4dp"
         android:layout_marginEnd="12dp">
 
         <com.android.systemui.statusbar.phone.MultiUserSwitch android:id="@+id/multi_user_switch"
             android:layout_width="48dp"
             android:layout_height="48dp"
             android:layout_alignParentEnd="true"
+            android:focusable="true"
             android:background="@drawable/ripple_drawable" >
             <ImageView android:id="@+id/multi_user_avatar"
                 android:layout_width="@dimen/multi_user_avatar_expanded_size"
@@ -52,6 +53,18 @@
                 android:scaleType="centerInside"/>
         </com.android.systemui.statusbar.phone.MultiUserSwitch>
 
+        <com.android.systemui.statusbar.AlphaOptimizedImageView
+            android:id="@android:id/edit"
+            android:layout_width="48dp"
+            android:layout_height="48dp"
+            android:clipToPadding="false"
+            android:clickable="true"
+            android:focusable="true"
+            android:src="@drawable/ic_mode_edit"
+            android:background="?android:attr/selectableItemBackgroundBorderless"
+            android:contentDescription="@string/accessibility_quick_settings_edit"
+            android:padding="14dp" />
+
         <com.android.systemui.statusbar.AlphaOptimizedFrameLayout
             android:id="@+id/settings_button_container"
             android:layout_width="48dp"
@@ -64,7 +77,7 @@
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:background="@drawable/ripple_drawable"
-                android:src="@drawable/ic_settings"
+                android:src="@drawable/ic_settings_20dp"
                 android:contentDescription="@string/accessibility_quick_settings_settings" />
             <com.android.systemui.statusbar.AlphaOptimizedImageView android:id="@+id/tuner_icon"
                 android:layout_width="match_parent"
@@ -98,7 +111,7 @@
         android:layout_alignParentTop="true"
         android:paddingStart="16dp"
         android:paddingEnd="16dp"
-        android:paddingTop="8dp"
+        android:paddingTop="6dp"
         android:visibility="gone"
         android:textAppearance="@style/TextAppearance.StatusBar.Expanded.EmergencyCallsOnly"
         android:text="@*android:string/emergency_calls_only"
@@ -116,7 +129,7 @@
         android:id="@+id/quick_qs_panel"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:layout_marginTop="28dp"
+        android:layout_marginTop="52dp"
         android:layout_marginStart="12dp"
         android:layout_marginEnd="12dp"
         android:layout_alignParentEnd="true"
diff --git a/packages/SystemUI/res/layout/recents_stack_action_button.xml b/packages/SystemUI/res/layout/recents_stack_action_button.xml
index 43b3de1..541000b 100644
--- a/packages/SystemUI/res/layout/recents_stack_action_button.xml
+++ b/packages/SystemUI/res/layout/recents_stack_action_button.xml
@@ -31,6 +31,6 @@
     android:shadowDy="2"
     android:shadowRadius="5"
     android:fontFamily="sans-serif-medium"
-    android:background="?android:selectableItemBackground"
+    android:background="@drawable/recents_stack_action_background"
     android:visibility="invisible"
     android:forceHasOverlappingRendering="false" />
diff --git a/packages/SystemUI/res/layout/recents_task_view.xml b/packages/SystemUI/res/layout/recents_task_view.xml
index 1978a93..c8e5b61 100644
--- a/packages/SystemUI/res/layout/recents_task_view.xml
+++ b/packages/SystemUI/res/layout/recents_task_view.xml
@@ -30,8 +30,8 @@
         android:id="@+id/lock_to_app_fab"
         android:layout_width="@dimen/recents_lock_to_app_size"
         android:layout_height="@dimen/recents_lock_to_app_size"
-        android:layout_gravity="bottom|right"
-        android:layout_marginRight="15dp"
+        android:layout_gravity="bottom|end"
+        android:layout_marginEnd="15dp"
         android:layout_marginBottom="15dp"
         android:translationZ="4dp"
         android:contentDescription="@string/recents_lock_to_app_button_label"
diff --git a/packages/SystemUI/res/layout/remote_input.xml b/packages/SystemUI/res/layout/remote_input.xml
index 0c8cc9b..73f26e2 100644
--- a/packages/SystemUI/res/layout/remote_input.xml
+++ b/packages/SystemUI/res/layout/remote_input.xml
@@ -42,8 +42,7 @@
             android:singleLine="true"
             android:ellipsize="start"
             android:inputType="textShortMessage|textAutoCorrect|textCapSentences"
-            android:textIsSelectable="true"
-            android:imeOptions="actionNone|flagNoExtractUi" />
+            android:imeOptions="actionNone|flagNoExtractUi|flagNoFullscreen" />
 
     <FrameLayout
             android:layout_width="wrap_content"
diff --git a/packages/SystemUI/res/layout/screen_pinning_request.xml b/packages/SystemUI/res/layout/screen_pinning_request.xml
index fea45cc..d55ba94 100644
--- a/packages/SystemUI/res/layout/screen_pinning_request.xml
+++ b/packages/SystemUI/res/layout/screen_pinning_request.xml
@@ -21,7 +21,8 @@
     android:layout_height="wrap_content"
     android:gravity="bottom|center_horizontal"
     android:layoutDirection="ltr"
-    android:orientation="vertical" >
+    android:orientation="vertical"
+    android:theme="@android:style/Theme.DeviceDefault.Light">
 
     <include
         android:layout_width="@dimen/screen_pinning_request_width"
@@ -32,7 +33,7 @@
         android:id="@+id/spacer"
         android:layout_width="@dimen/screen_pinning_request_width"
         android:layout_height="18dp"
-        android:background="@color/screen_pinning_request_bg" />
+        android:background="?android:attr/colorAccent" />
 
     <include
         android:layout_width="@dimen/screen_pinning_request_width"
diff --git a/packages/SystemUI/res/layout/screen_pinning_request_buttons.xml b/packages/SystemUI/res/layout/screen_pinning_request_buttons.xml
index 60112be..e3662f17 100644
--- a/packages/SystemUI/res/layout/screen_pinning_request_buttons.xml
+++ b/packages/SystemUI/res/layout/screen_pinning_request_buttons.xml
@@ -28,7 +28,7 @@
     android:id="@+id/screen_pinning_buttons"
     android:layout_width="match_parent"
     android:layout_height="@dimen/screen_pinning_request_button_height"
-    android:background="@color/screen_pinning_request_bg" >
+    android:background="?android:attr/colorAccent">
 
     <View
         android:layout_width="@dimen/screen_pinning_request_side_width"
@@ -42,7 +42,8 @@
         android:layout_height="@dimen/screen_pinning_request_button_height"
         android:layout_weight="0"
         android:paddingStart="@dimen/screen_pinning_request_frame_padding"
-        android:paddingEnd="@dimen/screen_pinning_request_frame_padding" >
+        android:paddingEnd="@dimen/screen_pinning_request_frame_padding"
+        android:theme="@*android:style/ThemeOverlay.DeviceDefault.Accent">
 
         <ImageView
             android:id="@+id/screen_pinning_back_bg_light"
diff --git a/packages/SystemUI/res/layout/screen_pinning_request_buttons_land.xml b/packages/SystemUI/res/layout/screen_pinning_request_buttons_land.xml
index ebad7a4..3649ffb 100644
--- a/packages/SystemUI/res/layout/screen_pinning_request_buttons_land.xml
+++ b/packages/SystemUI/res/layout/screen_pinning_request_buttons_land.xml
@@ -25,8 +25,8 @@
     android:id="@+id/screen_pinning_buttons"
     android:layout_height="match_parent"
     android:layout_width="@dimen/screen_pinning_request_button_height"
-    android:background="@color/screen_pinning_request_bg"
-    android:orientation="vertical" >
+    android:background="?android:attr/colorAccent"
+    android:orientation="vertical">
 
     <View
         android:layout_height="@dimen/screen_pinning_request_side_width"
@@ -82,7 +82,8 @@
         android:id="@+id/screen_pinning_back_group"
         android:layout_height="@dimen/screen_pinning_request_button_width"
         android:layout_width="@dimen/screen_pinning_request_button_height"
-        android:layout_weight="0" >
+        android:layout_weight="0"
+        android:theme="@*android:style/ThemeOverlay.DeviceDefault.Accent">
 
         <ImageView
             android:id="@+id/screen_pinning_back_bg_light"
diff --git a/packages/SystemUI/res/layout/screen_pinning_request_land_phone.xml b/packages/SystemUI/res/layout/screen_pinning_request_land_phone.xml
index e6c22d4..ec16991 100644
--- a/packages/SystemUI/res/layout/screen_pinning_request_land_phone.xml
+++ b/packages/SystemUI/res/layout/screen_pinning_request_land_phone.xml
@@ -20,7 +20,8 @@
     android:layout_height="@dimen/screen_pinning_request_width"
     android:layout_width="wrap_content"
     android:gravity="right|center_vertical"
-    android:orientation="horizontal" >
+    android:orientation="horizontal"
+    android:theme="@android:style/Theme.DeviceDefault.Light">
 
     <include
         android:layout_width="360dp"
diff --git a/packages/SystemUI/res/layout/screen_pinning_request_text_area.xml b/packages/SystemUI/res/layout/screen_pinning_request_text_area.xml
index df957f4..cdad94b 100644
--- a/packages/SystemUI/res/layout/screen_pinning_request_text_area.xml
+++ b/packages/SystemUI/res/layout/screen_pinning_request_text_area.xml
@@ -20,8 +20,8 @@
     android:id="@+id/screen_pinning_text_area"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:background="@color/screen_pinning_request_bg"
-    android:gravity="center_vertical" >
+    android:background="?android:attr/colorAccent"
+    android:gravity="center_vertical">
 
     <TextView
         android:id="@+id/screen_pinning_title"
diff --git a/packages/SystemUI/res/layout/status_bar.xml b/packages/SystemUI/res/layout/status_bar.xml
index 39c16d7..63af3e0 100644
--- a/packages/SystemUI/res/layout/status_bar.xml
+++ b/packages/SystemUI/res/layout/status_bar.xml
@@ -77,4 +77,11 @@
         </com.android.keyguard.AlphaOptimizedLinearLayout>
     </LinearLayout>
 
+    <ViewStub
+        android:id="@+id/emergency_cryptkeeper_text"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:layout="@layout/emergency_cryptkeeper_text"
+    />
+
 </com.android.systemui.statusbar.phone.PhoneStatusBarView>
diff --git a/packages/SystemUI/res/layout/status_bar_alarm_group.xml b/packages/SystemUI/res/layout/status_bar_alarm_group.xml
index f94b727..701b621 100644
--- a/packages/SystemUI/res/layout/status_bar_alarm_group.xml
+++ b/packages/SystemUI/res/layout/status_bar_alarm_group.xml
@@ -20,7 +20,7 @@
     android:id="@+id/date_time_alarm_group"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:layout_marginTop="8dp"
+    android:layout_marginTop="16dp"
     android:layout_marginStart="16dp"
     android:gravity="start"
     android:orientation="vertical">
@@ -60,7 +60,8 @@
     <com.android.systemui.statusbar.AlphaOptimizedButton
         android:id="@+id/alarm_status"
         android:layout_width="wrap_content"
-        android:layout_height="20dp"
+        android:layout_height="wrap_content"
+        android:minHeight="20dp"
         android:paddingTop="3dp"
         android:drawablePadding="8dp"
         android:drawableStart="@drawable/ic_access_alarms_small"
diff --git a/packages/SystemUI/res/layout/status_bar_expanded.xml b/packages/SystemUI/res/layout/status_bar_expanded.xml
index 2c8a559..3d70969 100644
--- a/packages/SystemUI/res/layout/status_bar_expanded.xml
+++ b/packages/SystemUI/res/layout/status_bar_expanded.xml
@@ -65,6 +65,14 @@
             layout="@layout/keyguard_status_bar"
             android:visibility="invisible" />
 
+        <Button
+            android:id="@+id/report_rejected_touch"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_marginTop="@dimen/status_bar_header_height_keyguard"
+            android:text="@string/report_rejected_touch"
+            android:visibility="gone" />
+
     </com.android.systemui.statusbar.phone.NotificationsQuickSettingsContainer>
 
     <include
diff --git a/packages/SystemUI/res/layout/status_bar_expanded_header.xml b/packages/SystemUI/res/layout/status_bar_expanded_header.xml
deleted file mode 100644
index c5cd65e..0000000
--- a/packages/SystemUI/res/layout/status_bar_expanded_header.xml
+++ /dev/null
@@ -1,195 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-** Copyright 2012, 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.
--->
-
-<!-- Extends RelativeLayout -->
-<com.android.systemui.statusbar.phone.StatusBarHeaderView
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:systemui="http://schemas.android.com/apk/res-auto"
-    android:id="@+id/header"
-    android:layout_width="@dimen/notification_panel_width"
-    android:layout_height="@dimen/status_bar_header_height"
-    android:layout_gravity="@integer/notification_panel_layout_gravity"
-    android:baselineAligned="false"
-    android:elevation="4dp"
-    android:background="@drawable/notification_header_bg"
-    android:clickable="true"
-    android:focusable="true"
-    >
-
-    <com.android.systemui.statusbar.phone.MultiUserSwitch android:id="@+id/multi_user_switch"
-        android:layout_width="@dimen/multi_user_switch_width_collapsed"
-        android:layout_height="@dimen/status_bar_header_height"
-        android:layout_alignParentEnd="true"
-        android:background="@drawable/ripple_drawable" >
-        <ImageView android:id="@+id/multi_user_avatar"
-            android:layout_width="@dimen/multi_user_avatar_expanded_size"
-            android:layout_height="@dimen/multi_user_avatar_expanded_size"
-            android:layout_gravity="center"
-            android:scaleType="centerInside"/>
-    </com.android.systemui.statusbar.phone.MultiUserSwitch>
-
-    <com.android.systemui.statusbar.AlphaOptimizedFrameLayout
-        android:id="@+id/settings_button_container"
-        android:layout_width="48dp"
-        android:layout_height="@dimen/status_bar_header_height"
-        android:clipChildren="false"
-        android:clipToPadding="false"
-        android:layout_toStartOf="@id/multi_user_switch">
-
-        <com.android.systemui.statusbar.phone.SettingsButton android:id="@+id/settings_button"
-            style="@android:style/Widget.Material.Button.Borderless"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:background="@drawable/ripple_drawable"
-            android:src="@drawable/ic_settings"
-            android:contentDescription="@string/accessibility_desc_settings" />
-        <com.android.systemui.statusbar.AlphaOptimizedImageView android:id="@+id/tuner_icon"
-            android:layout_width="match_parent"
-            android:layout_height="match_parent"
-            android:paddingStart="36dp"
-            android:tint="#4DFFFFFF"
-            android:tintMode="src_in"
-            android:visibility="invisible"
-            android:src="@drawable/tuner" />
-
-    </com.android.systemui.statusbar.AlphaOptimizedFrameLayout>
-
-    <LinearLayout android:id="@+id/system_icons_super_container"
-        android:layout_width="wrap_content"
-        android:layout_height="@dimen/status_bar_header_height"
-        android:layout_toStartOf="@id/multi_user_switch"
-        android:layout_alignWithParentIfMissing="true"
-        android:layout_marginStart="16dp"
-        android:background="@drawable/ripple_drawable"
-        android:paddingEnd="4dp" >
-        <FrameLayout android:id="@+id/system_icons_container"
-            android:layout_width="wrap_content"
-            android:layout_height="@dimen/status_bar_height"
-            android:layout_gravity="center_vertical"
-            >
-            <include layout="@layout/system_icons" />
-        </FrameLayout>
-        <TextView android:id="@+id/battery_level"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_gravity="center_vertical"
-            android:layout_marginStart="@dimen/header_battery_margin_expanded"
-            android:paddingEnd="@dimen/battery_level_padding_end"
-            android:textColor="#ffffff"
-            android:textSize="@dimen/battery_level_text_size"
-            android:importantForAccessibility="noHideDescendants"/>
-    </LinearLayout>
-
-    <TextView
-        android:id="@+id/header_emergency_calls_only"
-        android:layout_height="@dimen/status_bar_header_height"
-        android:layout_width="wrap_content"
-        android:layout_alignParentStart="true"
-        android:layout_toStartOf="@id/system_icons_super_container"
-        android:paddingStart="16dp"
-        android:paddingEnd="16dp"
-        android:visibility="gone"
-        android:textAppearance="@style/TextAppearance.StatusBar.Expanded.EmergencyCallsOnly"
-        android:text="@*android:string/emergency_calls_only"
-        android:singleLine="true"
-        android:gravity="center_vertical" />
-
-    <FrameLayout
-        android:id="@+id/date_group"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginBottom="@dimen/clock_collapsed_bottom_margin"
-        android:layout_alignParentBottom="true">
-        <com.android.systemui.statusbar.policy.DateView android:id="@+id/date_collapsed"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_marginStart="16dp"
-            android:singleLine="true"
-            android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Date"
-            android:layout_below="@id/clock"
-            systemui:datePattern="@string/abbrev_wday_month_day_no_year_alarm"
-            />
-
-        <com.android.systemui.statusbar.policy.DateView android:id="@+id/date_expanded"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_marginStart="16dp"
-            android:singleLine="true"
-            android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Date"
-            android:layout_below="@id/clock"
-            systemui:datePattern="eeeeMMMMd"
-            />
-    </FrameLayout>
-
-    <include layout="@layout/split_clock_view"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_marginStart="16dp"
-        android:layout_above="@id/date_group"
-        android:id="@+id/clock"
-        />
-
-    <com.android.systemui.statusbar.AlphaOptimizedButton android:id="@+id/alarm_status"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_alignParentBottom="true"
-        android:layout_toEndOf="@id/date_group"
-        android:layout_marginBottom="4dp"
-        android:drawablePadding="6dp"
-        android:drawableStart="@drawable/ic_access_alarms_small"
-        android:textColor="#64ffffff"
-        android:textAppearance="@style/TextAppearance.StatusBar.Expanded.Date"
-        android:paddingEnd="6dp"
-        android:paddingStart="6dp"
-        android:paddingTop="16dp"
-        android:paddingBottom="16dp"
-        android:background="?android:attr/selectableItemBackground"
-        android:visibility="gone"
-        />
-
-    <include
-        android:id="@+id/qs_detail_header"
-        layout="@layout/qs_detail_header"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_alignParentBottom="true"
-        />
-
-    <com.android.systemui.statusbar.AlphaOptimizedImageView
-        android:id="@+id/qs_detail_header_progress"
-        android:src="@drawable/indeterminate_anim"
-        android:alpha="0"
-        android:background="@color/qs_detail_progress_track"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:layout_alignParentBottom="true"
-        />
-
-    <TextView
-        android:id="@+id/header_debug_info"
-        android:visibility="invisible"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_gravity="center_vertical"
-        android:fontFamily="sans-serif-condensed"
-        android:textSize="11dp"
-        android:textStyle="bold"
-        android:textColor="#00A040"
-        android:padding="2dp"
-        />
-
-</com.android.systemui.statusbar.phone.StatusBarHeaderView>
diff --git a/packages/SystemUI/res/layout/switch_bar.xml b/packages/SystemUI/res/layout/switch_bar.xml
index f98de96..41cdb78 100644
--- a/packages/SystemUI/res/layout/switch_bar.xml
+++ b/packages/SystemUI/res/layout/switch_bar.xml
@@ -22,7 +22,8 @@
     android:paddingStart="16dp"
     android:paddingEnd="16dp"
     android:clickable="true"
-    android:gravity="center">
+    android:gravity="center"
+    android:theme="@*android:style/ThemeOverlay.DeviceDefault.Accent">
 
     <TextView android:id="@+id/switch_text"
         android:layout_height="wrap_content"
@@ -42,7 +43,6 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center_vertical"
-        android:background="@null"
-        android:theme="@style/ThemeOverlay.SwitchBar" />
+        android:background="@null" />
 
 </LinearLayout>
diff --git a/packages/SystemUI/res/layout/tile_listing.xml b/packages/SystemUI/res/layout/tile_listing.xml
deleted file mode 100644
index 9ab62ca..0000000
--- a/packages/SystemUI/res/layout/tile_listing.xml
+++ /dev/null
@@ -1,55 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
- ** Copyright 2015, 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.
- -->
-
- <LinearLayout
-     xmlns:android="http://schemas.android.com/apk/res/android"
-     android:layout_width="match_parent"
-     android:layout_height="wrap_content"
-     android:background="@drawable/qs_background_primary"
-     android:paddingBottom="20dp"
-     android:orientation="vertical">
-
-     <LinearLayout
-         android:layout_width="match_parent"
-         android:layout_height="wrap_content"
-         android:background="@drawable/notification_header_bg"
-         android:paddingStart="?android:attr/listPreferredItemPaddingStart"
-         android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
-         android:paddingTop="10dp"
-         android:paddingBottom="10dp"
-         android:orientation="horizontal">
-         <ImageView
-             android:id="@android:id/icon"
-             android:layout_width="36dp"
-             android:layout_height="36dp" />
-         <TextView
-             android:id="@android:id/title"
-             android:paddingStart="10dp"
-             android:textColor="@android:color/white"
-             android:textAppearance="?android:attr/textAppearanceSmall"
-             android:layout_width="wrap_content"
-             android:layout_height="wrap_content"
-             android:layout_gravity="center_vertical" />
-     </LinearLayout>
-
-     <GridLayout
-         android:id="@+id/tile_grid"
-         android:layout_width="match_parent"
-         android:layout_height="wrap_content"
-         android:columnCount="4" />
-
- </LinearLayout>
diff --git a/packages/SystemUI/res/layout/tuner_qs.xml b/packages/SystemUI/res/layout/tuner_qs.xml
deleted file mode 100644
index 9a51e0c..0000000
--- a/packages/SystemUI/res/layout/tuner_qs.xml
+++ /dev/null
@@ -1,47 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-     Copyright (C) 2015 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-
-<com.android.systemui.tuner.AutoScrollView
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:background="@color/system_secondary_color" >
-    <LinearLayout
-        android:id="@+id/all_details"
-        android:layout_width="match_parent"
-        android:layout_height="wrap_content"
-        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
-        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
-        android:gravity="center"
-        android:orientation="vertical">
-
-        <View
-            android:background="@color/qs_tile_divider"
-            android:layout_width="match_parent"
-            android:layout_height="2dp" />
-
-        <FrameLayout
-            android:id="@+id/remove_target"
-            android:layout_width="105dp"
-            android:layout_height="@dimen/qs_tile_height" />
-
-        <FrameLayout
-            android:id="@+id/add_target"
-            android:layout_width="105dp"
-            android:layout_height="@dimen/qs_tile_height" />
-    </LinearLayout>
-</com.android.systemui.tuner.AutoScrollView>
diff --git a/packages/SystemUI/res/layout/tv_pip_controls.xml b/packages/SystemUI/res/layout/tv_pip_controls.xml
index 0a2f320..49119fb 100644
--- a/packages/SystemUI/res/layout/tv_pip_controls.xml
+++ b/packages/SystemUI/res/layout/tv_pip_controls.xml
@@ -41,5 +41,6 @@
         android:layout_height="wrap_content"
         android:layout_marginStart="-50dp"
         android:src="@drawable/ic_pause_white_24dp"
-        android:text="@string/pip_pause" />
+        android:text="@string/pip_pause"
+        android:visibility="gone" />
 </merge>
diff --git a/packages/SystemUI/res/layout/volume_dialog.xml b/packages/SystemUI/res/layout/volume_dialog.xml
index d3f2a25..b3ff5d6 100644
--- a/packages/SystemUI/res/layout/volume_dialog.xml
+++ b/packages/SystemUI/res/layout/volume_dialog.xml
@@ -20,19 +20,27 @@
     android:layout_height="wrap_content"
     android:layout_marginBottom="@dimen/volume_dialog_margin_bottom"
     android:background="@drawable/volume_dialog_background"
-    android:translationZ="4dp"
-    android:paddingTop="8dp">
+    android:translationZ="4dp" >
 
     <LinearLayout
         android:id="@+id/volume_dialog_content"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:orientation="vertical"
-        android:paddingBottom="8dp"
-        android:paddingStart="8dp"
-        android:animateLayoutChanges="true" >
+        android:orientation="vertical" >
 
-        <!-- volume rows added and removed here! :-) -->
+        <LinearLayout
+                android:id="@+id/volume_dialog_rows"
+                android:layout_width="match_parent"
+                android:layout_height="wrap_content"
+                android:paddingEnd="@dimen/volume_button_size"
+                android:paddingTop="@dimen/volume_dialog_collapsed_padding_top"
+                android:orientation="vertical" >
+            <View android:id="@+id/spacer"
+                  android:layout_width="match_parent"
+                  android:layout_height="@dimen/volume_dialog_expanded_spacer"
+                  android:visibility="gone"/>
+            <!-- volume rows added and removed here! :-) -->
+        </LinearLayout>
 
         <include layout="@layout/volume_zen_footer" />
 
@@ -50,8 +58,11 @@
             android:clickable="true"
             android:soundEffectsEnabled="false"
             android:src="@drawable/ic_volume_collapse_animation"
+            android:background="@drawable/ripple_drawable"
             tools:ignore="RtlHardcoded"
             android:layout_alignParentEnd="true"
-            android:layout_alignParentTop="true"/>
+            android:layout_alignParentTop="true"
+            android:layout_marginTop="@dimen/volume_expander_margin_top"
+            android:layout_marginEnd="@dimen/volume_expander_margin_end"/>
 
 </RelativeLayout>
diff --git a/packages/SystemUI/res/layout/volume_dialog_row.xml b/packages/SystemUI/res/layout/volume_dialog_row.xml
index be05a3a..7328d05 100644
--- a/packages/SystemUI/res/layout/volume_dialog_row.xml
+++ b/packages/SystemUI/res/layout/volume_dialog_row.xml
@@ -13,14 +13,14 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-<RelativeLayout
+<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="wrap_content"
+    android:layout_height="@dimen/volume_row_height"
     android:clipChildren="false"
     android:clipToPadding="false"
-    android:id="@+id/volume_dialog_row"
-    android:paddingEnd="@dimen/volume_button_size" >
+    android:orientation="vertical"
+    android:paddingBottom="@dimen/volume_row_padding_bottom" >
 
     <TextView
         android:id="@+id/volume_row_header"
@@ -29,32 +29,28 @@
         android:ellipsize="end"
         android:maxLines="1"
         android:textAppearance="@style/TextAppearance.Volume.Header"
-        android:paddingBottom="0dp"
-        android:paddingEnd="12dp"
-        android:paddingStart="12dp"
-        android:paddingTop="4dp"
-        android:visibility="gone" />
+        android:paddingStart="@dimen/volume_row_header_padding_start" />
 
-    <com.android.keyguard.AlphaOptimizedImageButton
-        android:id="@+id/volume_row_icon"
-        style="@style/VolumeButtons"
-        android:layout_width="@dimen/volume_button_size"
-        android:layout_height="@dimen/volume_button_size"
-        android:layout_below="@id/volume_row_header"
-        android:soundEffectsEnabled="false" />
+    <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="@dimen/volume_row_slider_height"
+            android:orientation="horizontal"
+            android:paddingStart="@dimen/volume_row_padding_start" >
+        <com.android.keyguard.AlphaOptimizedImageButton
+                android:id="@+id/volume_row_icon"
+                style="@style/VolumeButtons"
+                android:layout_width="@dimen/volume_button_size"
+                android:layout_height="@dimen/volume_button_size"
+                android:soundEffectsEnabled="false" />
 
-    <SeekBar
-        android:id="@+id/volume_row_slider"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:layout_alignBottom="@+id/volume_row_icon"
-        android:layout_alignWithParentIfMissing="true"
-        android:layout_below="@id/volume_row_header"
-        android:layout_toEndOf="@id/volume_row_icon"
-        android:layout_toStartOf="@+id/volume_settings_button"
-        android:focusable="true"
-        android:focusableInTouchMode="true"
-        android:paddingEnd="8dp"
-        android:paddingStart="8dp" />
+        <SeekBar
+                android:id="@+id/volume_row_slider"
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:layout_alignWithParentIfMissing="true"
+                android:focusable="true"
+                android:focusableInTouchMode="true"
+                android:paddingStart="@dimen/volume_row_slider_padding_start"/>
+    </LinearLayout>
 
-</RelativeLayout>
\ No newline at end of file
+</LinearLayout>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/volume_zen_footer.xml b/packages/SystemUI/res/layout/volume_zen_footer.xml
index f30023d..775b157 100644
--- a/packages/SystemUI/res/layout/volume_zen_footer.xml
+++ b/packages/SystemUI/res/layout/volume_zen_footer.xml
@@ -18,7 +18,8 @@
     android:id="@+id/volume_zen_footer"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
-    android:orientation="vertical" > <!-- extends LinearLayout -->
+    android:orientation="vertical"
+    android:paddingBottom="8dp" > <!-- extends LinearLayout -->
 
     <View
         android:id="@+id/zen_embedded_divider"
@@ -78,7 +79,7 @@
         android:paddingStart="15dp"
         android:paddingEnd="15dp"
         android:text="@string/volume_zen_end_now"
-        android:textColor="@color/system_accent_color"
+        android:textColor="?android:attr/colorAccent"
         android:textAppearance="@style/TextAppearance.QS.DetailButton" />
 
 </com.android.systemui.volume.ZenFooter>
\ No newline at end of file
diff --git a/packages/SystemUI/res/layout/zen_mode_panel.xml b/packages/SystemUI/res/layout/zen_mode_panel.xml
index 906b867..c0be676 100644
--- a/packages/SystemUI/res/layout/zen_mode_panel.xml
+++ b/packages/SystemUI/res/layout/zen_mode_panel.xml
@@ -40,7 +40,8 @@
         android:layout_marginEnd="16dp"
         android:paddingTop="8dp"
         android:paddingBottom="8dp"
-        android:background="@drawable/zen_introduction_message_background" >
+        android:background="@drawable/zen_introduction_message_background"
+        android:theme="@*android:style/ThemeOverlay.DeviceDefault.Accent.Light">
 
         <ImageView
             android:id="@+id/zen_introduction_confirm"
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/packages/SystemUI/res/values-ldrtl/config.xml
similarity index 71%
rename from packages/SystemUI/res/layout/night_mode_settings.xml
rename to packages/SystemUI/res/values-ldrtl/config.xml
index 3725e78..40604c1 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/packages/SystemUI/res/values-ldrtl/config.xml
@@ -14,11 +14,8 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-
-    <include layout="@layout/switch_bar" />
-
-</LinearLayout>
+<resources>
+    <!-- Bounds [left top right bottom] on screen for picture-in-picture (PIP) windows,
+         when the PIP menu is shown with settings. -->
+    <string translatable="false" name="pip_settings_bounds">"778 54 1258 324"</string>
+</resources>
diff --git a/packages/SystemUI/res/values-sw600dp/dimens.xml b/packages/SystemUI/res/values-sw600dp/dimens.xml
index 6c5a313..7e63cbf 100644
--- a/packages/SystemUI/res/values-sw600dp/dimens.xml
+++ b/packages/SystemUI/res/values-sw600dp/dimens.xml
@@ -94,8 +94,6 @@
     <dimen name="navigation_key_width">128dp</dimen>
     <dimen name="navigation_key_padding">25dp</dimen>
 
-    <dimen name="qs_expand_margin">0dp</dimen>
-
     <!-- Keyboard shortcuts helper -->
     <dimen name="ksh_layout_width">488dp</dimen>
 
diff --git a/packages/SystemUI/res/values-w550dp-land/dimens.xml b/packages/SystemUI/res/values-w550dp-land/dimens.xml
index 4160c83..eaca9d7 100644
--- a/packages/SystemUI/res/values-w550dp-land/dimens.xml
+++ b/packages/SystemUI/res/values-w550dp-land/dimens.xml
@@ -18,6 +18,4 @@
 <resources>
     <!-- Standard notification width + gravity -->
     <dimen name="notification_panel_width">544dp</dimen>
-
-    <dimen name="qs_expand_margin">32dp</dimen>
 </resources>
diff --git a/packages/SystemUI/res/values/colors.xml b/packages/SystemUI/res/values/colors.xml
index cb51649..52565ba 100644
--- a/packages/SystemUI/res/values/colors.xml
+++ b/packages/SystemUI/res/values/colors.xml
@@ -30,18 +30,13 @@
     <color name="batterymeter_charge_color">#FFFFFFFF</color>
     <color name="batterymeter_bolt_color">#FFFFFFFF</color>
     <color name="qs_batterymeter_frame_color">#FF404040</color>
-    <color name="system_primary_color">#ff263238</color><!-- blue grey 900 -->
-    <color name="system_secondary_color">#ff37474F</color><!-- blue grey 800 -->
-    <color name="system_accent_color">#ff80CBC4</color><!-- deep teal 200 -->
-    <color name="system_warning_color">#fff4511e</color><!-- deep orange 600 -->
+    <color name="system_warning_color">@*android:color/system_error</color>
     <color name="qs_text">#FFFFFFFF</color>
     <color name="qs_tile_divider">#29ffffff</color><!-- 16% white -->
     <color name="qs_subhead">#99FFFFFF</color><!-- 60% white -->
-    <color name="qs_detail_empty">#24B0BEC5</color><!-- 14% blue grey 200 -->
-    <color name="qs_detail_button">#FFB0BEC5</color><!-- 100% blue grey 200 -->
+    <color name="qs_detail_button">@*android:color/quaternary_device_default_settings</color>
     <color name="qs_detail_button_white">#B3FFFFFF</color><!-- 70% white -->
     <color name="qs_detail_transition">#66FFFFFF</color>
-    <color name="qs_detail_progress_track">#99009688</color><!-- 60% deep teal 500 -->
     <color name="data_usage_secondary">#99FFFFFF</color><!-- 60% white -->
     <color name="data_usage_graph_track">#33FFFFFF</color><!-- 20% white -->
     <color name="data_usage_graph_warning">#FFFFFFFF</color>
@@ -99,15 +94,11 @@
     <!-- The color of the ripples on the tinted notifications -->
     <color name="notification_ripple_tinted_color">#30ffffff</color>
 
-    <!-- The color of the circle around the primary user in the user switcher -->
-    <color name="current_user_border_color">@color/system_accent_color</color>
-
     <!-- The color of the gear shown behind a notification -->
     <color name="notification_gear_color">#ff757575</color>
 
     <!-- The "inside" of a notification, reached via longpress -->
     <color name="notification_guts_bg_color">#eeeeee</color>
-    <color name="notification_guts_slider_color">@*android:color/material_deep_teal_500</color>
     <color name="notification_guts_disabled_slider_color">@*android:color/material_grey_300</color>
     <color name="notification_guts_secondary_slider_color">#858383</color>
     <color name="notification_guts_icon_tint">#8a000000</color>
@@ -126,13 +117,11 @@
     <!-- Shadow color for the furthest pixels around the fake shadow for recents. -->
     <color name="fake_shadow_end_color">#03000000</color>
 
-    <color name="screen_pinning_nav_icon_highlight_outer">#4080cbc4</color><!-- 25% deep teal 200 -->
-    <color name="screen_pinning_request_bg">#ff009688</color><!-- deep teal 500 -->
     <color name="screen_pinning_request_window_bg">#80000000</color>
 
     <color name="segmented_buttons_background">#14FFFFFF</color><!-- 8% white -->
     <color name="segmented_button_selected">#FFFFFFFF</color>
-    <color name="segmented_button_unselected">#FFB0BEC5</color><!-- blue grey 200 -->
+    <color name="segmented_button_unselected">@*android:color/quaternary_device_default_settings</color>
 
     <color name="dark_mode_icon_color_single_tone">#99000000</color>
     <color name="dark_mode_icon_color_dual_tone_background">#3d000000</color>
@@ -142,13 +131,9 @@
     <color name="light_mode_icon_color_dual_tone_background">#4dffffff</color>
     <color name="light_mode_icon_color_dual_tone_fill">#ffffff</color>
 
-    <color name="zen_introduction_message_background">#ff009688</color><!-- deep teal 500 -->
     <color name="volume_icon_color">#ffffffff</color>
     <color name="volume_settings_icon_color">#7fffffff</color>
-    <color name="volume_slider_inactive">#FFB0BEC5</color><!-- blue grey 200 -->
-
-    <color name="fab_ripple">#1fffffff</color><!-- 12% white -->
-    <color name="fab_shape">#ff009688</color><!-- Teal 500 -->
+    <color name="volume_slider_inactive">@*android:color/quaternary_device_default_settings</color>
 
     <color name="docked_divider_background">#ff000000</color>
     <color name="docked_divider_handle">#ffffff</color>
@@ -166,16 +151,10 @@
     <color name="qs_tile_tint_inactive">#4dffffff</color>
     <color name="qs_tile_tint_active">#ffffffff</color>
 
-    <color name="switch_bar_background">#ff37474f</color>
-    <color name="switch_accent_color">#ff7fcac3</color>
-
     <!-- Keyboard shortcuts colors -->
-    <color name="ksh_system_group_color">@color/material_deep_teal_500</color>
     <color name="ksh_application_group_color">#fff44336</color>
     <color name="ksh_keyword_color">#d9000000</color>
     <color name="ksh_key_item_color">@color/material_grey_600</color>
     <color name="ksh_key_item_background">@color/material_grey_100</color>
 
-    <!-- Background color of edit overflow -->
-    <color name="qs_edit_overflow_bg">#455A64</color>
 </resources>
diff --git a/packages/SystemUI/res/values/config.xml b/packages/SystemUI/res/values/config.xml
index 02b1b50..eb1a1eb 100644
--- a/packages/SystemUI/res/values/config.xml
+++ b/packages/SystemUI/res/values/config.xml
@@ -100,12 +100,22 @@
 
     <!-- The default tiles to display in QuickSettings -->
     <string name="quick_settings_tiles_default" translatable="false">
-        wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location
+        wifi,cell,battery,dnd,flashlight,rotation,bt,airplane
+    </string>
+
+    <!-- Tiles native to System UI. Order should match "quick_settings_tiles_default" -->
+    <string name="quick_settings_tiles_stock" translatable="false">
+        wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,work,cast,night
     </string>
 
     <!-- The tiles to display in QuickSettings -->
     <string name="quick_settings_tiles" translatable="false">default</string>
 
+    <!-- The tiles to display in QuickSettings in retail mode -->
+    <string name="quick_settings_tiles_retail_mode" translatable="false">
+        cell,battery,dnd,flashlight,rotation,location
+    </string>
+
     <!-- Whether or not the RSSI tile is capitalized or not. -->
     <bool name="quick_settings_rssi_tile_capitalization">true</bool>
 
@@ -116,6 +126,9 @@
     <!-- Should "4G" be shown instead of "LTE" when the network is NETWORK_TYPE_LTE? -->
     <bool name="config_show4GForLTE">true</bool>
 
+    <!-- Should "LTE"/"4G" be shown instead of "LTE+"/"4G+" when on NETWORK_TYPE_LTE_CA? -->
+    <bool name="config_hideLtePlus">false</bool>
+
     <!-- milliseconds before the heads up notification auto-dismisses. -->
     <integer name="heads_up_notification_decay">5000</integer>
 
@@ -205,26 +218,39 @@
     <!-- Doze: should notifications be used as a pulse signal? -->
     <bool name="doze_pulse_on_notifications">true</bool>
 
-    <!-- Doze: when to pulse after a buzzworthy notification arrives -->
-    <string name="doze_pulse_schedule" translatable="false">10s,30s,60s</string>
-
-    <!-- Doze: maximum number of times the notification pulse schedule can be reset -->
-    <integer name="doze_pulse_schedule_resets">2</integer>
-
     <!-- Doze: duration to avoid false pickup gestures triggered by notification vibrations -->
     <integer name="doze_pickup_vibration_threshold">2000</integer>
 
-    <!-- Doze: can we assume the pickup sensor includes a proximity check? -->
+    <!-- Doze: can we assume the pickup sensor includes a proximity check?
+         This is ignored if doze_pickup_subtype_performs_proximity_check is not empty.
+         @deprecated: use doze_pickup_subtype_performs_proximity_check instead.-->
     <bool name="doze_pickup_performs_proximity_check">false</bool>
 
+    <!-- Doze: a list of pickup sensor subtypes that perform a proximity check before they trigger.
+               If not empty, either * or !* must appear to specify the default.
+               If empty, falls back to doze_pickup_performs_proximity_check.
+
+               Examples: 1,2,3,!* -> subtypes 1,2 and 3 perform the check, all others don't.
+                         !1,!2,*  -> subtypes 1 and 2 don't perform the check, all others do.
+                         !8,*     -> subtype 8 does not perform the check, all others do
+                         1,1,*    -> illegal, every item may only appear once
+                         1,!1,*   -> illegal, no contradictions allowed
+                         1,2      -> illegal, need either * or !*
+                         1,,4a3   -> illegal, no empty or non-numeric terms allowed
+    -->
+    <string name="doze_pickup_subtype_performs_proximity_check"></string>
+
+    <!-- Type of the double tap sensor. Empty if double tap is not supported. -->
+    <string name="doze_double_tap_sensor_type" translatable="false"></string>
+
     <!-- Doze: pulse parameter - how long does it take to fade in? -->
     <integer name="doze_pulse_duration_in">900</integer>
 
     <!-- Doze: pulse parameter - how long does it take to fade in after a pickup? -->
-    <integer name="doze_pulse_duration_in_pickup">300</integer>
+    <integer name="doze_pulse_duration_in_pickup">130</integer>
 
     <!-- Doze: pulse parameter - once faded in, how long does it stay visible? -->
-    <integer name="doze_pulse_duration_visible">3000</integer>
+    <integer name="doze_pulse_duration_visible">6000</integer>
 
     <!-- Doze: pulse parameter - how long does it take to fade out? -->
     <integer name="doze_pulse_duration_out">600</integer>
@@ -265,4 +291,3 @@
     <bool name="quick_settings_show_full_alarm">false</bool>
 
 </resources>
-
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index 2a4752a..fe67808 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -57,6 +57,9 @@
     <!-- The amount to scale each of the status bar icons by. A value of 1 means no scaling. -->
     <item name="status_bar_icon_scale_factor" format="float" type="dimen">1.0</item>
 
+    <!-- max height of a notification such that the content can still fade out when closing -->
+    <dimen name="max_notification_fadeout_height">100dp</dimen>
+
     <!-- Height of a small notification in the status bar-->
     <dimen name="notification_min_height">92dp</dimen>
 
@@ -130,7 +133,7 @@
     <dimen name="close_handle_underlap">32dp</dimen>
 
     <!-- Height of the status bar header bar -->
-    <dimen name="status_bar_header_height">80dp</dimen>
+    <dimen name="status_bar_header_height">104dp</dimen>
 
     <!-- Height of the status bar header bar when expanded -->
     <dimen name="status_bar_header_height_expanded">116dp</dimen>
@@ -141,6 +144,9 @@
     <!-- Margin start of the system icons super container -->
     <dimen name="system_icons_super_container_margin_start">16dp</dimen>
 
+    <!-- Margin end of the system icons super container when the avatar is missing. -->
+    <dimen name="system_icons_super_container_avatarless_margin_end">6dp</dimen>
+
     <!-- Width for the notification panel and related windows -->
     <dimen name="match_parent">-1px</dimen>
     <dimen name="standard_notification_panel_width">416dp</dimen>
@@ -172,10 +178,6 @@
     <dimen name="qs_tile_margin_top">16dp</dimen>
     <dimen name="qs_quick_tile_size">48dp</dimen>
     <dimen name="qs_quick_tile_padding">12dp</dimen>
-    <dimen name="qs_date_anim_translation">32dp</dimen>
-    <dimen name="qs_date_alarm_anim_translation">22dp</dimen>
-    <dimen name="qs_date_collapsed_text_size">14sp</dimen>
-    <dimen name="qs_date_text_size">16sp</dimen>
     <dimen name="qs_header_gear_translation">16dp</dimen>
     <dimen name="qs_page_indicator_width">16dp</dimen>
     <dimen name="qs_page_indicator_height">8dp</dimen>
@@ -202,7 +204,6 @@
     <dimen name="qs_detail_margin_top">28dp</dimen>
     <dimen name="qs_data_usage_text_size">14sp</dimen>
     <dimen name="qs_data_usage_usage_text_size">36sp</dimen>
-    <dimen name="qs_expand_margin">0dp</dimen>
     <dimen name="qs_battery_padding">2dp</dimen>
     <dimen name="qs_detail_items_padding_top">4dp</dimen>
 
@@ -377,6 +378,8 @@
 
     <!-- The font size of the date in QS -->
     <dimen name="qs_date_collapsed_size">14sp</dimen>
+    <!-- Amount the date/time move when emergency calls only is present -->
+    <dimen name="qs_date_time_translation">8dp</dimen>
 
     <!-- Battery level text padding end when in expanded QS and on Keyguard -->
     <dimen name="battery_level_padding_end">2dp</dimen>
@@ -538,6 +541,19 @@
 
     <!-- Volume dialog root view bottom margin, at rest -->
     <dimen name="volume_dialog_margin_bottom">4dp</dimen>
+    <dimen name="volume_dialog_collapsed_padding_top">8dp</dimen>
+    <dimen name="volume_dialog_expanded_spacer">14dp</dimen>
+    <dimen name="volume_dialog_padding_end">40dp</dimen>
+
+    <dimen name="volume_row_padding_bottom">9.4dp</dimen>
+    <dimen name="volume_row_padding_start">4dp</dimen>
+    <dimen name="volume_row_header_padding_start">16dp</dimen>
+    <dimen name="volume_row_height">64dp</dimen>
+    <dimen name="volume_row_slider_height">48dp</dimen>
+    <dimen name="volume_row_slider_padding_start">12dp</dimen>
+
+    <dimen name="volume_expander_margin_end">2dp</dimen>
+    <dimen name="volume_expander_margin_top">6dp</dimen>
 
     <!-- Padding between icon and text for managed profile toast -->
     <dimen name="managed_profile_toast_padding">4dp</dimen>
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index f7a169c..d13039e 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -348,9 +348,15 @@
     <!-- Content description of the data connection type 4G for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->
     <string name="accessibility_data_connection_4g">4G</string>
 
+    <!-- Content description of the data connection type 4G for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->
+    <string name="accessibility_data_connection_4g_plus">4G+</string>
+
     <!-- Content description of the data connection type LTE for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->
     <string name="accessibility_data_connection_lte">LTE</string>
 
+    <!-- Content description of the data connection type LTE+ for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->
+    <string name="accessibility_data_connection_lte_plus">LTE+</string>
+
     <!-- Content description of the data connection type CDMA for accessibility (not shown on the screen). [CHAR LIMIT=NONE] -->
     <string name="accessibility_data_connection_cdma">CDMA</string>
 
@@ -563,7 +569,7 @@
     <!-- Title of dialog shown when data usage has exceeded limit and has been disabled. [CHAR LIMIT=48] -->
     <string name="data_usage_disabled_dialog_title">Data is paused</string>
     <!-- Body of dialog shown when data usage has exceeded limit and has been disabled. [CHAR LIMIT=NONE] -->
-    <string name="data_usage_disabled_dialog">Because your set data limit was reached, the device has paused data usage for the remainder of this cycle.\n\nResuming may lead to charges from your carrier.</string>
+    <string name="data_usage_disabled_dialog">The data limit you set has been reached. You are no longer using cellular data.\n\nIf you resume, charges may apply for data usage.</string>
     <!-- Dialog button indicating that data connection should be re-enabled. [CHAR LIMIT=28] -->
     <string name="data_usage_disabled_dialog_enable">Resume</string>
 
@@ -749,6 +755,12 @@
     <string name="quick_settings_cellular_detail_data_warning"><xliff:g id="data_limit" example="2.0 GB">%s</xliff:g> warning</string>
     <!-- QuickSettings: Work mode [CHAR LIMIT=NONE] -->
     <string name="quick_settings_work_mode_label">Work mode</string>
+    <!-- QuickSettings: Label for the toggle to activate Night display (renamed "Night Light" with title caps). [CHAR LIMIT=20] -->
+    <string name="quick_settings_night_display_label">Night Light</string>
+    <!-- QuickSettings: Summary for the toggle to deactivate Night display when it's on (renamed "Night Light" with title caps). [CHAR LIMIT=NONE] -->
+    <string name="quick_settings_night_display_summary_on">Night Light on, tap to turn off</string>
+    <!-- QuickSettings: Label for the toggle to activate Night display when it's off (renamed "Night Light" with title caps). [CHAR LIMIT=NONE] -->
+    <string name="quick_settings_night_display_summary_off">Night Light off, tap to turn on</string>
 
     <!-- Recents: The empty recents string. [CHAR LIMIT=NONE] -->
     <string name="recents_empty_message">No recent items</string>
@@ -778,6 +790,10 @@
     <!-- Recents: MultiStack add stack split custom radio button. [CHAR LIMIT=NONE] -->
     <string name="recents_multistack_add_stack_dialog_split_custom">Split Custom</string>
 
+    <!-- Fully qualified activity class names to be blacklisted in Recents, add package names into overlay as needed -->
+    <string-array name="recents_blacklist_array">
+    </string-array>
+
     <!-- Expanded Status Bar Header: Battery Charged [CHAR LIMIT=40] -->
     <string name="expanded_header_battery_charged">Charged</string>
 
@@ -985,6 +1001,9 @@
     <!-- Footer vpn present text [CHAR LIMIT=50] -->
     <string name="vpn_footer">Network may be monitored</string>
 
+    <!-- Footer vpn present text [CHAR LIMIT=50] -->
+    <string name="branded_vpn_footer">Network may be monitored</string>
+
     <!-- Monitoring dialog title for device owned devices [CHAR LIMIT=35] -->
     <string name="monitoring_title_device_owned">Device monitoring</string>
 
@@ -1021,6 +1040,9 @@
     <!-- Monitoring dialog text for single app (inside personal profile) [CHAR LIMIT=400] -->
     <string name="monitoring_description_app_personal">You\'re connected to <xliff:g id="application">%1$s</xliff:g>, which can monitor your personal network activity, including emails, apps, and websites.</string>
 
+    <!-- Monitoring dialog text for single app (inside personal profile) [CHAR LIMIT=400] -->
+    <string name="branded_monitoring_description_app_personal">You\'re connected to <xliff:g id="application">%1$s</xliff:g>, which can monitor your personal network activity, including emails, apps, and websites.</string>
+
     <!-- Monitoring dialog text for single app (inside work profile) [CHAR LIMIT=400] -->
     <string name="monitoring_description_app_work">Your work profile is managed by <xliff:g id="organization">%1$s</xliff:g>. It is connected to <xliff:g id="application">%2$s</xliff:g>, which can monitor your work network activity, including emails, apps, and websites.\n\nFor more information, contact your administrator.</string>
 
@@ -1093,14 +1115,14 @@
     <!-- Toast shown when user unlocks screen and managed profile activity is in the foreground -->
     <string name="managed_profile_foreground_toast">You\'re using your work profile</string>
 
-    <string-array name="volume_stream_titles" translatable="false">
-        <item>Voice calls</item> <!-- STREAM_VOICE_CALL -->
+    <string-array name="volume_stream_titles">
+        <item>Call</item> <!-- STREAM_VOICE_CALL -->
         <item>System</item> <!-- STREAM_SYSTEM -->
-        <item>Notifications</item> <!-- STREAM_RING -->
+        <item>Ring</item> <!-- STREAM_RING -->
         <item>Media</item> <!-- STREAM_MUSIC -->
-        <item>Alarms</item> <!-- STREAM_ALARM -->
+        <item>Alarm</item> <!-- STREAM_ALARM -->
         <item></item> <!-- STREAM_NOTIFICATION -->
-        <item>Bluetooth calls</item> <!-- STREAM_BLUETOOTH_SCO -->
+        <item>Bluetooth</item> <!-- STREAM_BLUETOOTH_SCO -->
         <item></item> <!-- STREAM_SYSTEM_ENFORCED -->
         <item></item> <!-- STREAM_DTMF -->
         <item></item> <!-- STREAM_TTS -->
@@ -1227,11 +1249,6 @@
     <!-- Option to use new paging layout in quick settings [CHAR LIMIT=60] -->
     <string name="qs_paging" translatable="false">Use the new Quick Settings</string>
 
-    <!-- Toggle to enable the gesture to enter split-screen by swiping up from the Overview button. [CHAR LIMIT=60]-->
-    <string name="overview_nav_bar_gesture">Enable split-screen swipe-up gesture</string>
-    <!-- Description for the toggle to enable the gesture to enter split-screen by swiping up from the Overview button. [CHAR LIMIT=NONE]-->
-    <string name="overview_nav_bar_gesture_desc">Enable gesture to enter split-screen by swiping up from the Overview button</string>
-
     <!-- Category in the System UI Tuner settings, where new/experimental
          settings are -->
     <string name="experimental">Experimental</string>
@@ -1333,59 +1350,6 @@
     <!-- Notification: Gear: Content description for the gear. [CHAR LIMIT=NONE] -->
     <string name="notification_gear_accessibility"><xliff:g id="app_name" example="YouTube">%1$s</xliff:g> notification controls</string>
 
-    <!-- SysUI Tuner: Color and appearance screen title [CHAR LIMIT=50] -->
-    <string name="color_and_appearance">Color and appearance</string>
-
-    <!-- SysUI Tuner: Name of the night mode feature [CHAR LIMIT=30] -->
-    <string name="night_mode">Night mode</string>
-
-    <!-- SysUI Tuner: Name of calibrate display dialog [CHAR LIMIT=30] -->
-    <string name="calibrate_display">Calibrate display</string>
-
-    <!-- SysUI Tuner: Summary of night mode when its on [CHAR LIMIT=NONE] -->
-    <string name="night_mode_on">On</string>
-
-    <!-- SysUI Tuner: Summary of night mode when its off [CHAR LIMIT=NONE] -->
-    <string name="night_mode_off">Off</string>
-
-    <!-- SysUI Tuner: Label for switch to turn on night mode automatically [CHAR LIMIT=50] -->
-    <string name="turn_on_automatically">Turn on automatically</string>
-
-    <!-- SysUI Tuner: Summary for switch to turn on night mode automatically [CHAR LIMIT=NONE] -->
-    <string name="turn_on_auto_summary">Switch into Night Mode as appropriate for location and time of day</string>
-
-    <!-- SysUI Tuner: Label for section controlling what night mode does [CHAR LIMIT=60] -->
-    <string name="when_night_mode_on">When Night Mode is on</string>
-
-    <!-- SysUI Tuner: Switch controlling whether dark theme is turned on with night mode [CHAR LIMIT=45] -->
-    <string name="use_dark_theme">Use dark theme for Android OS</string>
-
-    <!-- SysUI Tuner: Switch controlling whether tint is changed with night mode [CHAR LIMIT=45] -->
-    <string name="adjust_tint">Adjust tint</string>
-
-    <!-- SysUI Tuner: Switch controlling whether brightness is changed with night mode [CHAR LIMIT=45] -->
-    <string name="adjust_brightness">Adjust brightness</string>
-
-    <!-- SysUI Tuner: Disclaimer about using dark theme with night mode [CHAR LIMIT=NONE] -->
-    <string name="night_mode_disclaimer">The dark theme is applied to
-        core areas of Android OS that are normally displayed in a light theme,
-        such as Settings.</string>
-
-    <!-- Button to apply settings [CHAR LIMIT=30] -->
-    <string name="color_apply">Apply</string>
-
-    <!-- Title of warning dialog about bad color settings. [CHAR LIMIT=30] -->
-    <string name="color_revert_title">Confirm settings</string>
-
-    <!-- Message warning user about custom color settings [CHAR LIMIT=NONE] -->
-    <string name="color_revert_message">Some color settings can make this
-        device unusable. Click OK to confirm these color settings,
-        otherwise these settings will reset after 10 seconds.</string>
-
-    <string name="color_modification_r" translatable="false">R</string>
-    <string name="color_modification_g" translatable="false">G</string>
-    <string name="color_modification_b" translatable="false">B</string>
-
     <!-- Title of the battery settings detail panel [CHAR LIMIT=20] -->
     <string name="battery_panel_title">Battery usage</string>
 
@@ -1666,6 +1630,9 @@
     <!-- Accessibility label for the notification icons in the collapsed status bar. Not shown on screen [CHAR LIMIT=NONE] -->
     <string name="accessibility_desc_notification_icon"><xliff:g name="app_name" example="Gmail">%1$s</xliff:g> notification: <xliff:g name="notification_text" example="5 new messages">%2$s</xliff:g></string>
 
+    <!-- Label for button that reports a touch that was wrongly rejected by the lockscreen. For debugging only. [CHAR LIMIT=NONE] -->
+    <string name="report_rejected_touch" translatable="false">Report rejected touch</string>
+
     <!-- Multi-Window strings -->
     <!-- Text that gets shown on top of current activity to inform the user that the system force-resized the current activity and that things might crash/not work properly [CHAR LIMIT=NONE] -->
     <string name="dock_forced_resizable">App may not work with split-screen.</string>
diff --git a/packages/SystemUI/res/values/styles.xml b/packages/SystemUI/res/values/styles.xml
index aeb484f..c659acf 100644
--- a/packages/SystemUI/res/values/styles.xml
+++ b/packages/SystemUI/res/values/styles.xml
@@ -42,7 +42,7 @@
     </style>
 
     <!-- Theme used for the activity that shows when the system forced an app to be resizable -->
-    <style name="ForcedResizableTheme" parent="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
+    <style name="ForcedResizableTheme" parent="@android:style/Theme.Translucent.NoTitleBar">
         <item name="android:windowBackground">@drawable/forced_resizable_background</item>
         <item name="android:statusBarColor">@color/transparent</item>
         <item name="android:windowAnimationStyle">@style/Animation.ForcedResizable</item>
@@ -132,7 +132,7 @@
 
     <style name="TextAppearance.QS.DetailItemSecondary">
         <item name="android:textSize">@dimen/qs_detail_item_secondary_text_size</item>
-        <item name="android:textColor">@color/system_accent_color</item>
+        <item name="android:textColor">?android:attr/colorAccent</item>
     </style>
 
     <style name="TextAppearance.QS.Introduction">
@@ -177,7 +177,7 @@
 
     <style name="TextAppearance.QS.DataUsage.Usage">
         <item name="android:textSize">@dimen/qs_data_usage_usage_text_size</item>
-        <item name="android:textColor">@color/system_accent_color</item>
+        <item name="android:textColor">?android:attr/colorAccent</item>
     </style>
 
     <style name="TextAppearance.QS.DataUsage.Secondary">
@@ -217,19 +217,13 @@
     <style name="Animation.StatusBar">
     </style>
 
-    <style name="systemui_theme" parent="@android:style/Theme.DeviceDefault">
-        <item name="android:colorPrimary">@color/system_primary_color</item>
-        <item name="android:colorControlActivated">@color/system_accent_color</item>
-    </style>
+    <style name="systemui_theme" parent="@*android:style/Theme.DeviceDefault.Settings.Dark" />
 
     <style name="systemui_theme_remote_input" parent="@android:style/Theme.DeviceDefault.Light">
         <item name="android:colorAccent">@color/remote_input_accent</item>
     </style>
 
-    <style name="Theme.SystemUI.Dialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog">
-        <item name="android:colorPrimary">@color/system_primary_color</item>
-        <item name="android:colorControlActivated">@color/system_accent_color</item>
-    </style>
+    <style name="Theme.SystemUI.Dialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog" />
 
     <style name="Theme.SystemUI.Dialog.Alert" parent="@*android:style/Theme.DeviceDefault.Light.Dialog.Alert" />
 
@@ -296,7 +290,7 @@
     <style name="TextAppearance.Volume.ZenDetail">
         <item name="android:textSize">14sp</item>
         <item name="android:fontFamily">sans-serif</item>
-        <item name="android:textColor">#ffb0b3c5</item>
+        <item name="android:textColor">@*android:color/quaternary_device_default_settings</item>
     </style>
 
     <style name="VolumeButtons" parent="@android:style/Widget.Material.Button.Borderless">
@@ -320,12 +314,12 @@
         <item name="android:layout_height">48dp</item>
     </style>
 
-    <style name="TunerSettings" parent="@android:style/Theme.Material.Settings">
+    <style name="TunerSettings" parent="@android:style/Theme.DeviceDefault.Settings">
         <item name="android:windowActionBar">false</item>
         <item name="preferenceTheme">@style/TunerPreferenceTheme</item>
     </style>
 
-    <style name="TunerPreferenceTheme" parent="@android:style/Theme.Material.Settings">
+    <style name="TunerPreferenceTheme" parent="@android:style/Theme.DeviceDefault.Settings">
         <item name="dropdownPreferenceStyle">@style/Preference.DropDown.Material</item>
     </style>
 
@@ -341,16 +335,16 @@
     </style>
 
     <style name="TextAppearance.NotificationGuts.Secondary">
-        <item name="android:alpha">.54</item>
+        <item name="android:textColor">?android:attr/textColorSecondary</item>
     </style>
 
     <style name="TextAppearance.NotificationGuts.Primary">
-        <item name="android:alpha">.87</item>
+        <item name="android:textColor">?android:attr/textColorPrimary</item>
         <item name="android:textSize">16sp</item>
     </style>
 
     <style name="TextAppearance.NotificationGuts.Radio">
-        <item name="android:alpha">.87</item>
+        <item name="android:textColor">?android:attr/textColorPrimary</item>
     </style>
 
     <style name="TextAppearance.NotificationGuts.Button">
@@ -358,15 +352,11 @@
         <item name="android:textAllCaps">true</item>
         <item name="android:fontFamily">sans-serif-medium</item>
         <item name="android:gravity">center</item>
-        <item name="android:textColor">@*android:color/material_deep_teal_500</item>
+        <item name="android:textColor">?android:attr/colorAccent</item>
     </style>
 
-    <style name="ThemeOverlay.SwitchBar" parent="@android:style/ThemeOverlay">
-        <item name="android:colorAccent">@color/switch_accent_color</item>
-    </style>
-
-    <style name="edit_theme" parent="@android:style/Theme.Material">
-        <item name="android:colorBackground">@color/qs_edit_overflow_bg</item>
+    <style name="edit_theme" parent="@*android:style/Theme.DeviceDefault.Settings.Dark">
+        <item name="android:colorBackground">?android:attr/colorSecondary</item>
     </style>
 
 </resources>
diff --git a/packages/SystemUI/res/xml/color_and_appearance.xml b/packages/SystemUI/res/xml/color_and_appearance.xml
deleted file mode 100644
index 21f890e..0000000
--- a/packages/SystemUI/res/xml/color_and_appearance.xml
+++ /dev/null
@@ -1,30 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2016 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:sysui="http://schemas.android.com/apk/res-auto"
-    android:title="@string/color_and_appearance">
-
-    <Preference
-        android:key="night_mode"
-        android:title="@string/night_mode"
-        android:fragment="com.android.systemui.tuner.NightModeFragment" />
-
-    <com.android.systemui.tuner.CalibratePreference
-        android:key="calibrate"
-        android:title="@string/calibrate_display" />
-
-</PreferenceScreen>
diff --git a/packages/SystemUI/res/xml/night_mode.xml b/packages/SystemUI/res/xml/night_mode.xml
deleted file mode 100644
index 34af820..0000000
--- a/packages/SystemUI/res/xml/night_mode.xml
+++ /dev/null
@@ -1,39 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!-- Copyright (C) 2016 The Android Open Source Project
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
-
-<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:sysui="http://schemas.android.com/apk/res-auto"
-    android:title="@string/night_mode">
-
-    <SwitchPreference
-        android:key="auto"
-        android:title="@string/turn_on_automatically"
-        android:summary="@string/turn_on_auto_summary" />
-
-    <PreferenceCategory
-        android:title="@string/when_night_mode_on">
-
-        <SwitchPreference
-            android:key="adjust_tint"
-            android:title="@string/adjust_tint" />
-
-        <SwitchPreference
-            android:key="adjust_brightness"
-            android:title="@string/adjust_brightness" />
-
-    </PreferenceCategory>
-
-</PreferenceScreen>
diff --git a/packages/SystemUI/res/xml/other_settings.xml b/packages/SystemUI/res/xml/other_settings.xml
index 3c872fa..ce636cd 100644
--- a/packages/SystemUI/res/xml/other_settings.xml
+++ b/packages/SystemUI/res/xml/other_settings.xml
@@ -18,11 +18,6 @@
                   xmlns:sysui="http://schemas.android.com/apk/res-auto"
                   android:title="@string/other">
 
-    <com.android.systemui.tuner.TunerSwitch
-            android:key="overview_nav_bar_gesture"
-            android:title="@string/overview_nav_bar_gesture"
-            android:summary="@string/overview_nav_bar_gesture_desc" />
-
     <!-- importance -->
     <Preference
             android:key="power_notification_controls"
diff --git a/packages/SystemUI/res/xml/tuner_prefs.xml b/packages/SystemUI/res/xml/tuner_prefs.xml
index 116bc69..b46e862 100644
--- a/packages/SystemUI/res/xml/tuner_prefs.xml
+++ b/packages/SystemUI/res/xml/tuner_prefs.xml
@@ -100,13 +100,6 @@
 
     </PreferenceScreen>
 
-    <!--
-    <Preference
-        android:key="color_transform"
-        android:title="@string/color_and_appearance"
-        android:fragment="com.android.systemui.tuner.ColorAndAppearanceFragment" />
-    -->
-
     <PreferenceScreen
         android:key="volume_and_do_not_disturb"
         android:title="@string/volume_and_do_not_disturb">
diff --git a/packages/SystemUI/src/com/android/systemui/Interpolators.java b/packages/SystemUI/src/com/android/systemui/Interpolators.java
index 17f4dab..fd3bd92 100644
--- a/packages/SystemUI/src/com/android/systemui/Interpolators.java
+++ b/packages/SystemUI/src/com/android/systemui/Interpolators.java
@@ -36,6 +36,7 @@
     public static final Interpolator ACCELERATE = new AccelerateInterpolator();
     public static final Interpolator ACCELERATE_DECELERATE = new AccelerateDecelerateInterpolator();
     public static final Interpolator DECELERATE_QUINT = new DecelerateInterpolator(2.5f);
+    public static final Interpolator CUSTOM_40_40 = new PathInterpolator(0.4f, 0f, 0.6f, 1f);
 
     /**
      * Interpolator to be used when animating a move based on a click. Pair with enough duration.
diff --git a/packages/SystemUI/src/com/android/systemui/Prefs.java b/packages/SystemUI/src/com/android/systemui/Prefs.java
index 907616c..19ae295 100644
--- a/packages/SystemUI/src/com/android/systemui/Prefs.java
+++ b/packages/SystemUI/src/com/android/systemui/Prefs.java
@@ -47,7 +47,6 @@
         Key.QS_DATA_SAVER_DIALOG_SHOWN,
         Key.QS_INVERT_COLORS_ADDED,
         Key.QS_WORK_ADDED,
-        Key.QS_NIGHT_ADDED,
     })
     public @interface Key {
         String OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME = "OverviewLastStackTaskActiveTime";
@@ -67,7 +66,6 @@
         String QS_DATA_SAVER_DIALOG_SHOWN = "QsDataSaverDialogShown";
         String QS_INVERT_COLORS_ADDED = "QsInvertColorsAdded";
         String QS_WORK_ADDED = "QsWorkAdded";
-        String QS_NIGHT_ADDED = "QsNightAdded";
     }
 
     public static boolean getBoolean(Context context, @Key String key, boolean defaultValue) {
diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
index 9df5690..4b9ae2a 100644
--- a/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
+++ b/packages/SystemUI/src/com/android/systemui/SystemUIApplication.java
@@ -57,7 +57,8 @@
             com.android.systemui.media.RingtonePlayer.class,
             com.android.systemui.keyboard.KeyboardUI.class,
             com.android.systemui.tv.pip.PipUI.class,
-            com.android.systemui.shortcut.ShortcutKeyDispatcher.class
+            com.android.systemui.shortcut.ShortcutKeyDispatcher.class,
+            com.android.systemui.VendorServices.class
     };
 
     /**
diff --git a/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java b/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java
index 53c2233..06ce88cc 100644
--- a/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java
+++ b/packages/SystemUI/src/com/android/systemui/SystemUIFactory.java
@@ -23,8 +23,12 @@
 
 import com.android.internal.widget.LockPatternUtils;
 import com.android.keyguard.ViewMediatorCallback;
+import com.android.systemui.R;
+import com.android.systemui.assist.AssistManager;
+import com.android.systemui.statusbar.BaseStatusBar;
 import com.android.systemui.statusbar.ScrimView;
 import com.android.systemui.statusbar.phone.KeyguardBouncer;
+import com.android.systemui.statusbar.phone.LockscreenWallpaper;
 import com.android.systemui.statusbar.phone.NotificationIconAreaController;
 import com.android.systemui.statusbar.phone.PhoneStatusBar;
 import com.android.systemui.statusbar.phone.QSTileHost;
@@ -89,7 +93,7 @@
     }
 
     public ScrimController createScrimController(ScrimView scrimBehind, ScrimView scrimInFront,
-            View headsUpScrim) {
+            View headsUpScrim, LockscreenWallpaper lockscreenWallpaper) {
         return new ScrimController(scrimBehind, scrimInFront, headsUpScrim);
     }
 
@@ -115,4 +119,8 @@
     public <T> T createInstance(Class<T> classType) {
         return null;
     }
+
+    public AssistManager createAssistManager(BaseStatusBar bar, Context context) {
+        return new AssistManager(bar, context);
+    }
 }
diff --git a/core/java/com/android/internal/app/IEphemeralResolver.aidl b/packages/SystemUI/src/com/android/systemui/VendorServices.java
similarity index 61%
copy from core/java/com/android/internal/app/IEphemeralResolver.aidl
copy to packages/SystemUI/src/com/android/systemui/VendorServices.java
index 40429ee..0be6b12 100644
--- a/core/java/com/android/internal/app/IEphemeralResolver.aidl
+++ b/packages/SystemUI/src/com/android/systemui/VendorServices.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -11,14 +11,19 @@
  * 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.
+ * limitations under the License
  */
 
-package com.android.internal.app;
+package com.android.systemui;
 
-import android.content.Intent;
-import android.os.IRemoteCallback;
+/**
+ * Placeholder for any vendor-specific services.
+ */
+public class VendorServices extends SystemUI {
 
-oneway interface IEphemeralResolver {
-    void getEphemeralResolveInfoList(IRemoteCallback callback, int digestPrefix, int sequence);
+    @Override
+    public void start() {
+        // no-op
+    }
+
 }
diff --git a/packages/SystemUI/src/com/android/systemui/analytics/DataCollector.java b/packages/SystemUI/src/com/android/systemui/analytics/DataCollector.java
index 91f6520..b1f454e 100644
--- a/packages/SystemUI/src/com/android/systemui/analytics/DataCollector.java
+++ b/packages/SystemUI/src/com/android/systemui/analytics/DataCollector.java
@@ -21,6 +21,7 @@
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
+import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Build;
 import android.os.Handler;
@@ -28,6 +29,7 @@
 import android.provider.Settings;
 import android.util.Log;
 import android.view.MotionEvent;
+import android.widget.Toast;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -48,6 +50,8 @@
     private static final String TAG = "DataCollector";
     private static final String COLLECTOR_ENABLE = "data_collector_enable";
     private static final String COLLECT_BAD_TOUCHES = "data_collector_collect_bad_touches";
+    private static final String ALLOW_REJECTED_TOUCH_REPORTS =
+            "data_collector_allow_rejected_touch_reports";
 
     private static final long TIMEOUT_MILLIS = 11000; // 11 seconds.
     public static final boolean DEBUG = false;
@@ -64,6 +68,7 @@
     private boolean mCollectBadTouches = false;
     private boolean mCornerSwiping = false;
     private boolean mTrackingStarted = false;
+    private boolean mAllowReportRejectedTouch = false;
 
     private static DataCollector sInstance = null;
 
@@ -87,6 +92,11 @@
                 mSettingsObserver,
                 UserHandle.USER_ALL);
 
+        mContext.getContentResolver().registerContentObserver(
+                Settings.Secure.getUriFor(ALLOW_REJECTED_TOUCH_REPORTS), false,
+                mSettingsObserver,
+                UserHandle.USER_ALL);
+
         updateConfiguration();
     }
 
@@ -104,10 +114,13 @@
         mCollectBadTouches = mEnableCollector && 0 != Settings.Secure.getInt(
                 mContext.getContentResolver(),
                 COLLECT_BAD_TOUCHES, 0);
+        mAllowReportRejectedTouch = Build.IS_DEBUGGABLE && 0 != Settings.Secure.getInt(
+                mContext.getContentResolver(),
+                ALLOW_REJECTED_TOUCH_REPORTS, 0);
     }
 
     private boolean sessionEntrypoint() {
-        if (mEnableCollector && mCurrentSession == null) {
+        if (isEnabled() && mCurrentSession == null) {
             onSessionStart();
             return true;
         }
@@ -115,7 +128,7 @@
     }
 
     private void sessionExitpoint(int result) {
-        if (mEnableCollector && mCurrentSession != null) {
+        if (mCurrentSession != null) {
             onSessionEnd(result);
         }
     }
@@ -130,8 +143,36 @@
         SensorLoggerSession session = mCurrentSession;
         mCurrentSession = null;
 
-        session.end(System.currentTimeMillis(), result);
-        queueSession(session);
+        if (mEnableCollector) {
+            session.end(System.currentTimeMillis(), result);
+            queueSession(session);
+        }
+    }
+
+    public Uri reportRejectedTouch() {
+        if (mCurrentSession == null) {
+            Toast.makeText(mContext, "Generating rejected touch report failed: session timed out.",
+                    Toast.LENGTH_LONG).show();
+            return null;
+        }
+        SensorLoggerSession currentSession = mCurrentSession;
+
+        currentSession.setType(Session.REJECTED_TOUCH_REPORT);
+        currentSession.end(System.currentTimeMillis(), Session.SUCCESS);
+        Session proto = currentSession.toProto();
+
+        byte[] b = Session.toByteArray(proto);
+        File dir = new File(mContext.getExternalCacheDir(), "rejected_touch_reports");
+        dir.mkdir();
+        File touch = new File(dir, "rejected_touch_report_" + System.currentTimeMillis());
+
+        try {
+            new FileOutputStream(touch).write(b);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+
+        return Uri.fromFile(touch);
     }
 
     private void queueSession(final SensorLoggerSession currentSession) {
@@ -164,7 +205,7 @@
 
     @Override
     public synchronized void onSensorChanged(SensorEvent event) {
-        if (mEnableCollector && mCurrentSession != null) {
+        if (isEnabled() && mCurrentSession != null) {
             mCurrentSession.addSensorEvent(event, System.nanoTime());
             enforceTimeout();
         }
@@ -186,7 +227,19 @@
     public void onAccuracyChanged(Sensor sensor, int accuracy) {
     }
 
+    /**
+     * @return true if data is being collected - either for data gathering or creating a
+     *         rejected touch report.
+     */
     public boolean isEnabled() {
+        return mEnableCollector || mAllowReportRejectedTouch;
+    }
+
+    /**
+     * @return true if the full data set for data gathering should be collected - including
+     *         extensive sensor data, which is is not normally included with rejected touch reports.
+     */
+    public boolean isEnabledFull() {
         return mEnableCollector;
     }
 
@@ -401,8 +454,12 @@
     }
 
     private void addEvent(int eventType) {
-        if (mEnableCollector && mCurrentSession != null) {
+        if (isEnabled() && mCurrentSession != null) {
             mCurrentSession.addPhoneEvent(eventType, System.nanoTime());
         }
     }
+
+    public boolean isReportingEnabled() {
+        return mAllowReportRejectedTouch;
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/analytics/SensorLoggerSession.java b/packages/SystemUI/src/com/android/systemui/analytics/SensorLoggerSession.java
index faaad2b..b39803a 100644
--- a/packages/SystemUI/src/com/android/systemui/analytics/SensorLoggerSession.java
+++ b/packages/SystemUI/src/com/android/systemui/analytics/SensorLoggerSession.java
@@ -53,6 +53,10 @@
         mType = Session.REAL;
     }
 
+    public void setType(int type) {
+        mType = type;
+    }
+
     public void end(long endTimestampMillis, int result) {
         mResult = result;
         mEndTimestampMillis = endTimestampMillis;
diff --git a/packages/SystemUI/src/com/android/systemui/assist/AssistDisclosure.java b/packages/SystemUI/src/com/android/systemui/assist/AssistDisclosure.java
index 5878219..8bd38db 100644
--- a/packages/SystemUI/src/com/android/systemui/assist/AssistDisclosure.java
+++ b/packages/SystemUI/src/com/android/systemui/assist/AssistDisclosure.java
@@ -95,39 +95,34 @@
     private class AssistDisclosureView extends View
             implements ValueAnimator.AnimatorUpdateListener {
 
-        public static final int TRACING_ANIMATION_DURATION = 600;
-        public static final int ALPHA_IN_ANIMATION_DURATION = 450;
-        public static final int ALPHA_OUT_ANIMATION_DURATION = 400;
+        static final int FULL_ALPHA = 222; // 87%
+        static final int ALPHA_IN_ANIMATION_DURATION = 400;
+        static final int ALPHA_OUT_ANIMATION_DURATION = 300;
+
 
         private float mThickness;
         private float mShadowThickness;
         private final Paint mPaint = new Paint();
         private final Paint mShadowPaint = new Paint();
 
-        private final ValueAnimator mTracingAnimator;
         private final ValueAnimator mAlphaOutAnimator;
         private final ValueAnimator mAlphaInAnimator;
         private final AnimatorSet mAnimator;
 
-        private float mTracingProgress = 0;
         private int mAlpha = 0;
 
         public AssistDisclosureView(Context context) {
             super(context);
 
-            mTracingAnimator = ValueAnimator.ofFloat(0, 1).setDuration(TRACING_ANIMATION_DURATION);
-            mTracingAnimator.addUpdateListener(this);
-            mTracingAnimator.setInterpolator(AnimationUtils.loadInterpolator(mContext,
-                    R.interpolator.assist_disclosure_trace));
-            mAlphaInAnimator = ValueAnimator.ofInt(0, 255).setDuration(ALPHA_IN_ANIMATION_DURATION);
+            mAlphaInAnimator = ValueAnimator.ofInt(0, FULL_ALPHA)
+                    .setDuration(ALPHA_IN_ANIMATION_DURATION);
             mAlphaInAnimator.addUpdateListener(this);
-            mAlphaInAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
-            mAlphaOutAnimator = ValueAnimator.ofInt(255, 0).setDuration(
+            mAlphaInAnimator.setInterpolator(Interpolators.CUSTOM_40_40);
+            mAlphaOutAnimator = ValueAnimator.ofInt(FULL_ALPHA, 0).setDuration(
                     ALPHA_OUT_ANIMATION_DURATION);
             mAlphaOutAnimator.addUpdateListener(this);
-            mAlphaOutAnimator.setInterpolator(Interpolators.FAST_OUT_LINEAR_IN);
+            mAlphaOutAnimator.setInterpolator(Interpolators.CUSTOM_40_40);
             mAnimator = new AnimatorSet();
-            mAnimator.play(mAlphaInAnimator).with(mTracingAnimator);
             mAnimator.play(mAlphaInAnimator).before(mAlphaOutAnimator);
             mAnimator.addListener(new AnimatorListenerAdapter() {
                 boolean mCancelled;
@@ -174,8 +169,6 @@
             super.onDetachedFromWindow();
 
             mAnimator.cancel();
-
-            mTracingProgress = 0;
             mAlpha = 0;
         }
 
@@ -197,45 +190,32 @@
             final int width = getWidth();
             final int height = getHeight();
             float thickness = mThickness;
-            final float pixelProgress = mTracingProgress * (width + height - 2 * thickness);
 
-            float bottomProgress = Math.min(pixelProgress, width / 2f);
-            if (bottomProgress > 0) {
-                drawBeam(canvas,
-                        width / 2f - bottomProgress,
-                        height - thickness,
-                        width / 2f + bottomProgress,
-                        height, paint, padding);
-            }
+            // bottom
+            drawBeam(canvas,
+                    0,
+                    height - thickness,
+                    width,
+                    height, paint, padding);
 
-            float sideProgress = Math.min(pixelProgress - bottomProgress, height - thickness);
-            if (sideProgress > 0) {
-                drawBeam(canvas,
-                        0,
-                        (height - thickness) - sideProgress,
-                        thickness,
-                        height - thickness, paint, padding);
-                drawBeam(canvas,
-                        width - thickness,
-                        (height - thickness) - sideProgress,
-                        width,
-                        height - thickness, paint, padding);
-            }
+            // sides
+            drawBeam(canvas,
+                    0,
+                    0,
+                    thickness,
+                    height - thickness, paint, padding);
+            drawBeam(canvas,
+                    width - thickness,
+                    0,
+                    width,
+                    height - thickness, paint, padding);
 
-            float topProgress = Math.min(pixelProgress - bottomProgress - sideProgress,
-                    width / 2 - thickness);
-            if (sideProgress > 0 && topProgress > 0) {
-                drawBeam(canvas,
-                        thickness,
-                        0,
-                        thickness + topProgress,
-                        thickness, paint, padding);
-                drawBeam(canvas,
-                        (width - thickness) - topProgress,
-                        0,
-                        width - thickness,
-                        thickness, paint, padding);
-            }
+            // top
+            drawBeam(canvas,
+                    thickness,
+                    0,
+                    width - thickness,
+                    thickness, paint, padding);
         }
 
         private void drawBeam(Canvas canvas, float left, float top, float right, float bottom,
@@ -253,8 +233,6 @@
                 mAlpha = (int) mAlphaOutAnimator.getAnimatedValue();
             } else if (animation == mAlphaInAnimator) {
                 mAlpha = (int) mAlphaInAnimator.getAnimatedValue();
-            } else if (animation == mTracingAnimator) {
-                mTracingProgress = (float) mTracingAnimator.getAnimatedValue();
             }
             invalidate();
         }
diff --git a/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java b/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java
index a0aeb2f..9eceeac 100644
--- a/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java
+++ b/packages/SystemUI/src/com/android/systemui/assist/AssistManager.java
@@ -15,6 +15,7 @@
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.provider.Settings;
@@ -28,6 +29,7 @@
 import android.widget.ImageView;
 
 import com.android.internal.app.AssistUtils;
+import com.android.internal.app.IVoiceInteractionSessionListener;
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
 import com.android.systemui.R;
 import com.android.systemui.statusbar.BaseStatusBar;
@@ -45,13 +47,13 @@
     private static final long TIMEOUT_SERVICE = 2500;
     private static final long TIMEOUT_ACTIVITY = 1000;
 
-    private final Context mContext;
+    protected final Context mContext;
     private final WindowManager mWindowManager;
     private final AssistDisclosure mAssistDisclosure;
 
     private AssistOrbContainer mView;
     private final BaseStatusBar mBar;
-    private final AssistUtils mAssistUtils;
+    protected final AssistUtils mAssistUtils;
 
     private IVoiceInteractionSessionShowCallback mShowCallback =
             new IVoiceInteractionSessionShowCallback.Stub() {
@@ -81,6 +83,23 @@
         mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
         mAssistUtils = new AssistUtils(context);
         mAssistDisclosure = new AssistDisclosure(context, new Handler());
+
+        registerVoiceInteractionSessionListener();
+    }
+
+    protected void registerVoiceInteractionSessionListener() {
+        mAssistUtils.registerVoiceInteractionSessionListener(
+                new IVoiceInteractionSessionListener.Stub() {
+            @Override
+            public void onVoiceSessionShown() throws RemoteException {
+                Log.v(TAG, "Voice open");
+            }
+
+            @Override
+            public void onVoiceSessionHidden() throws RemoteException {
+                Log.v(TAG, "Voice closed");
+            }
+        });
     }
 
     public void onConfigurationChanged() {
@@ -103,6 +122,10 @@
         }
     }
 
+    protected boolean shouldShowOrb() {
+        return true;
+    }
+
     public void startAssist(Bundle args) {
         final ComponentName assistComponent = getAssistInfo();
         if (assistComponent == null) {
@@ -110,7 +133,7 @@
         }
 
         final boolean isService = assistComponent.equals(getVoiceInteractorComponentName());
-        if (!isService || !isVoiceSessionRunning()) {
+        if (!isService || (!isVoiceSessionRunning() && shouldShowOrb())) {
             showOrb(assistComponent, isService);
             mView.postDelayed(mHideRunnable, isService
                     ? TIMEOUT_SERVICE
diff --git a/packages/SystemUI/src/com/android/systemui/classifier/AccelerationClassifier.java b/packages/SystemUI/src/com/android/systemui/classifier/AccelerationClassifier.java
index bad739fd..4585fe9 100644
--- a/packages/SystemUI/src/com/android/systemui/classifier/AccelerationClassifier.java
+++ b/packages/SystemUI/src/com/android/systemui/classifier/AccelerationClassifier.java
@@ -62,36 +62,36 @@
     @Override
     public float getFalseTouchEvaluation(int type, Stroke stroke) {
         Data data = mStrokeMap.get(stroke);
-        return SpeedRatioEvaluator.evaluate(data.maxSpeedRatio)
-                + DistanceRatioEvaluator.evaluate(data.maxDistanceRatio);
+        return 2 * SpeedRatioEvaluator.evaluate(data.maxSpeedRatio);
     }
 
     private static class Data {
-        public Point previousPoint;
-        public float previousSpeed;
-        public float previousDistance;
-        public float maxSpeedRatio;
-        public float maxDistanceRatio;
+
+        static final float MILLIS_TO_NANOS = 1e6f;
+
+        Point previousPoint;
+        float previousSpeed = 0;
+        float maxSpeedRatio = 0;
 
         public Data(Point point) {
             previousPoint = point;
-            previousSpeed = previousDistance = 0.0f;
-            maxDistanceRatio = maxSpeedRatio = 0.0f;
         }
 
         public void addPoint(Point point) {
             float distance = previousPoint.dist(point);
             float duration = (float) (point.timeOffsetNano - previousPoint.timeOffsetNano + 1);
             float speed = distance / duration;
-            if (previousDistance != 0.0f) {
-                maxDistanceRatio = Math.max(maxDistanceRatio, distance / previousDistance);
-            }
 
+            if (duration > 20 * MILLIS_TO_NANOS || duration < 5 * MILLIS_TO_NANOS) {
+                // reject this segment and ensure we won't use data about it in the next round.
+                previousSpeed = 0;
+                previousPoint = point;
+                return;
+            }
             if (previousSpeed != 0.0f) {
                 maxSpeedRatio = Math.max(maxSpeedRatio, speed / previousSpeed);
             }
 
-            previousDistance = distance;
             previousSpeed = speed;
             previousPoint = point;
         }
diff --git a/packages/SystemUI/src/com/android/systemui/classifier/DistanceRatioEvaluator.java b/packages/SystemUI/src/com/android/systemui/classifier/DistanceRatioEvaluator.java
deleted file mode 100644
index 8acb009..0000000
--- a/packages/SystemUI/src/com/android/systemui/classifier/DistanceRatioEvaluator.java
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Copyright (C) 2015 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.classifier;
-
-public class DistanceRatioEvaluator {
-    public static float evaluate(float value) {
-        float evaluation = 0.0f;
-        if (value <= 1.0) evaluation++;
-        if (value <= 0.5) evaluation++;
-        if (value > 4.0) evaluation++;
-        if (value > 7.0) evaluation++;
-        if (value > 14.0) evaluation++;
-        return evaluation;
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/classifier/FalsingManager.java b/packages/SystemUI/src/com/android/systemui/classifier/FalsingManager.java
index 1ac5992..664e886 100644
--- a/packages/SystemUI/src/com/android/systemui/classifier/FalsingManager.java
+++ b/packages/SystemUI/src/com/android/systemui/classifier/FalsingManager.java
@@ -22,6 +22,7 @@
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
 import android.hardware.SensorManager;
+import android.net.Uri;
 import android.os.Handler;
 import android.os.PowerManager;
 import android.os.UserHandle;
@@ -142,7 +143,7 @@
         if (mHumanInteractionClassifier.isEnabled()) {
             registerSensors(CLASSIFIER_SENSORS);
         }
-        if (mDataCollector.isEnabled()) {
+        if (mDataCollector.isEnabledFull()) {
             registerSensors(COLLECTOR_SENSORS);
         }
     }
@@ -400,4 +401,15 @@
         pw.print("mScreenOn="); pw.println(mScreenOn ? 1 : 0);
         pw.println();
     }
+
+    public Uri reportRejectedTouch() {
+        if (mDataCollector.isEnabled()) {
+            return mDataCollector.reportRejectedTouch();
+        }
+        return null;
+    }
+
+    public boolean isReportingEnabled() {
+        return mDataCollector.isReportingEnabled();
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/classifier/HistoryEvaluator.java b/packages/SystemUI/src/com/android/systemui/classifier/HistoryEvaluator.java
index 85a9bee..4c64711 100644
--- a/packages/SystemUI/src/com/android/systemui/classifier/HistoryEvaluator.java
+++ b/packages/SystemUI/src/com/android/systemui/classifier/HistoryEvaluator.java
@@ -16,6 +16,8 @@
 
 package com.android.systemui.classifier;
 
+import android.os.SystemClock;
+
 import java.util.ArrayList;
 
 /**
@@ -31,7 +33,7 @@
     private long mLastUpdate;
 
     public HistoryEvaluator() {
-        mLastUpdate = System.currentTimeMillis();
+        mLastUpdate = SystemClock.elapsedRealtime();
     }
 
     public void addStroke(float evaluation) {
@@ -69,15 +71,18 @@
     }
 
     private void decayValue() {
-        long currentTimeMillis = System.currentTimeMillis();
+        long time = SystemClock.elapsedRealtime();
+
+        if (time <= mLastUpdate) {
+            return;
+        }
 
         // All weights are multiplied by HISTORY_FACTOR after each INTERVAL milliseconds.
-        float factor = (float) Math.pow(HISTORY_FACTOR,
-                (float) (currentTimeMillis - mLastUpdate) / INTERVAL);
+        float factor = (float) Math.pow(HISTORY_FACTOR, (time - mLastUpdate) / INTERVAL);
 
         decayValue(mStrokes, factor);
         decayValue(mGestureWeights, factor);
-        mLastUpdate = currentTimeMillis;
+        mLastUpdate = time;
     }
 
     private void decayValue(ArrayList<Data> list, float factor) {
diff --git a/packages/SystemUI/src/com/android/systemui/classifier/SpeedRatioEvaluator.java b/packages/SystemUI/src/com/android/systemui/classifier/SpeedRatioEvaluator.java
index 4c6cea0..e34f222 100644
--- a/packages/SystemUI/src/com/android/systemui/classifier/SpeedRatioEvaluator.java
+++ b/packages/SystemUI/src/com/android/systemui/classifier/SpeedRatioEvaluator.java
@@ -19,6 +19,7 @@
 public class SpeedRatioEvaluator {
     public static float evaluate(float value) {
         float evaluation = 0.0f;
+        if (value == 0) return 0;
         if (value <= 1.0) evaluation++;
         if (value <= 0.5) evaluation++;
         if (value > 9.0) evaluation++;
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java b/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
index 9eb768c..b3038b9 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeLog.java
@@ -33,14 +33,15 @@
     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
     private static final boolean ENABLED = true;
     private static final int SIZE = Build.IS_DEBUGGABLE ? 400 : 50;
-    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
+    static final SimpleDateFormat FORMAT = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
 
-    private static final int PULSE_REASONS = 4;
+    private static final int PULSE_REASONS = 5;
 
     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;
 
     private static long[] sTimes;
     private static String[] sMessages;
@@ -57,8 +58,9 @@
     private static SummaryStats sEmergencyCallStats;
     private static SummaryStats[][] sProxStats; // [reason][near/far]
 
-    public static void tracePickupPulse(boolean withinVibrationThreshold) {
+    public static void tracePickupPulse(Context context, boolean withinVibrationThreshold) {
         if (!ENABLED) return;
+        init(context);
         log("pickupPulse withinVibrationThreshold=" + withinVibrationThreshold);
         (withinVibrationThreshold ? sPickupPulseNearVibrationStats
                 : sPickupPulseNotNearVibrationStats).append();
@@ -76,8 +78,9 @@
         log("pulseFinish");
     }
 
-    public static void traceNotificationPulse(long instance) {
+    public static void traceNotificationPulse(Context context, long instance) {
         if (!ENABLED) return;
+        init(context);
         log("notificationPulse instance=" + instance);
         sNotificationPulseStats.append();
     }
@@ -153,9 +156,9 @@
     public static void traceProximityResult(Context context, boolean near, long millis,
             int pulseReason) {
         if (!ENABLED) return;
+        init(context);
         log("proximityResult reason=" + pulseReasonToString(pulseReason) + " near=" + near
                 + " millis=" + millis);
-        init(context);
         sProxStats[pulseReason][near ? 0 : 1].append();
     }
 
@@ -165,6 +168,7 @@
             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";
             default: throw new IllegalArgumentException("bad reason: " + pulseReason);
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
index 3370091..56f6b8d 100644
--- a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
+++ b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java
@@ -16,14 +16,14 @@
 
 package com.android.systemui.doze;
 
-import android.app.AlarmManager;
-import android.app.PendingIntent;
+import android.app.ActivityManager;
 import android.app.UiModeManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.res.Configuration;
+import android.database.ContentObserver;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
@@ -31,21 +31,27 @@
 import android.hardware.TriggerEvent;
 import android.hardware.TriggerEventListener;
 import android.media.AudioAttributes;
+import android.net.Uri;
 import android.os.Handler;
 import android.os.PowerManager;
 import android.os.SystemClock;
+import android.os.UserHandle;
 import android.os.Vibrator;
+import android.provider.Settings;
 import android.service.dreams.DreamService;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.Display;
 
+import com.android.internal.logging.MetricsLogger;
+import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.systemui.SystemUIApplication;
 import com.android.systemui.statusbar.phone.DozeParameters;
-import com.android.systemui.statusbar.phone.DozeParameters.PulseSchedule;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.Date;
+import java.util.List;
 
 public class DozeService extends DreamService {
     private static final String TAG = "DozeService";
@@ -53,19 +59,11 @@
 
     private static final String ACTION_BASE = "com.android.systemui.doze";
     private static final String PULSE_ACTION = ACTION_BASE + ".pulse";
-    private static final String NOTIFICATION_PULSE_ACTION = ACTION_BASE + ".notification_pulse";
-    private static final String EXTRA_INSTANCE = "instance";
 
     /**
-     * Earliest time we pulse due to a notification light after the service started.
-     *
-     * <p>Incoming notification light events during the blackout period are
-     * delayed to the earliest time defined by this constant.</p>
-     *
-     * <p>This delay avoids a pulse immediately after screen off, at which
-     * point the notification light is re-enabled again by NoMan.</p>
+     * If true, reregisters all trigger sensors when the screen turns off.
      */
-    private static final int EARLIEST_LIGHT_PULSE_AFTER_START_MS = 10 * 1000;
+    private static final boolean REREGISTER_ALL_SENSORS_ON_SCREEN_OFF = true;
 
     private final String mTag = String.format(TAG + ".%08x", hashCode());
     private final Context mContext = this;
@@ -73,24 +71,19 @@
     private final Handler mHandler = new Handler();
 
     private DozeHost mHost;
-    private SensorManager mSensors;
-    private TriggerSensor mSigMotionSensor;
+    private SensorManager mSensorManager;
+    private TriggerSensor[] mSensors;
     private TriggerSensor mPickupSensor;
     private PowerManager mPowerManager;
     private PowerManager.WakeLock mWakeLock;
-    private AlarmManager mAlarmManager;
     private UiModeManager mUiModeManager;
     private boolean mDreaming;
     private boolean mPulsing;
     private boolean mBroadcastReceiverRegistered;
     private boolean mDisplayStateSupported;
-    private boolean mNotificationLightOn;
     private boolean mPowerSaveActive;
     private boolean mCarMode;
     private long mNotificationPulseTime;
-    private long mLastScheduleResetTime;
-    private long mEarliestPulseDueToLight;
-    private int mScheduleResetsRemaining;
 
     public DozeService() {
         if (DEBUG) Log.d(mTag, "new DozeService()");
@@ -105,14 +98,16 @@
         pw.print("  mWakeLock: held="); pw.println(mWakeLock.isHeld());
         pw.print("  mHost: "); pw.println(mHost);
         pw.print("  mBroadcastReceiverRegistered: "); pw.println(mBroadcastReceiverRegistered);
-        pw.print("  mSigMotionSensor: "); pw.println(mSigMotionSensor);
-        pw.print("  mPickupSensor:"); pw.println(mPickupSensor);
+        for (TriggerSensor s : mSensors) {
+            pw.print("  sensor: ");
+            pw.println(s);
+        }
         pw.print("  mDisplayStateSupported: "); pw.println(mDisplayStateSupported);
-        pw.print("  mNotificationLightOn: "); pw.println(mNotificationLightOn);
         pw.print("  mPowerSaveActive: "); pw.println(mPowerSaveActive);
         pw.print("  mCarMode: "); pw.println(mCarMode);
-        pw.print("  mNotificationPulseTime: "); pw.println(mNotificationPulseTime);
-        pw.print("  mScheduleResetsRemaining: "); pw.println(mScheduleResetsRemaining);
+        pw.print("  mNotificationPulseTime: "); pw.println(
+                DozeLog.FORMAT.format(new Date(mNotificationPulseTime
+                        - SystemClock.elapsedRealtime() + System.currentTimeMillis())));
         mDozeParameters.dump(pw);
     }
 
@@ -129,17 +124,28 @@
 
         setWindowless(true);
 
-        mSensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
-        mSigMotionSensor = new TriggerSensor(Sensor.TYPE_SIGNIFICANT_MOTION,
-                mDozeParameters.getPulseOnSigMotion(), mDozeParameters.getVibrateOnSigMotion(),
-                DozeLog.PULSE_REASON_SENSOR_SIGMOTION);
-        mPickupSensor = new TriggerSensor(Sensor.TYPE_PICK_UP_GESTURE,
-                mDozeParameters.getPulseOnPickup(), mDozeParameters.getVibrateOnPickup(),
-                DozeLog.PULSE_REASON_SENSOR_PICKUP);
+        mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
+        mSensors = new TriggerSensor[] {
+                new TriggerSensor(
+                        mSensorManager.getDefaultSensor(Sensor.TYPE_SIGNIFICANT_MOTION),
+                        null /* setting */,
+                        mDozeParameters.getPulseOnSigMotion(),
+                        mDozeParameters.getVibrateOnSigMotion(),
+                        DozeLog.PULSE_REASON_SENSOR_SIGMOTION),
+                mPickupSensor = new TriggerSensor(
+                        mSensorManager.getDefaultSensor(Sensor.TYPE_PICK_UP_GESTURE),
+                        Settings.Secure.DOZE_PULSE_ON_PICK_UP,
+                        mDozeParameters.getPulseOnPickup(), mDozeParameters.getVibrateOnPickup(),
+                        DozeLog.PULSE_REASON_SENSOR_PICKUP),
+                new TriggerSensor(
+                        findSensorWithType(mDozeParameters.getDoubleTapSensorType()),
+                        Settings.Secure.DOZE_PULSE_ON_DOUBLE_TAP,
+                        mDozeParameters.getPulseOnPickup(), mDozeParameters.getVibrateOnPickup(),
+                        DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP)
+        };
         mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
         mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
         mWakeLock.setReferenceCounted(true);
-        mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
         mDisplayStateSupported = mDozeParameters.getDisplayStateSupported();
         mUiModeManager = (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
         turnDisplayOff();
@@ -174,25 +180,20 @@
         }
 
         mDreaming = true;
-        rescheduleNotificationPulse(false /*predicate*/);  // cancel any pending pulse alarms
-        mEarliestPulseDueToLight = System.currentTimeMillis() + EARLIEST_LIGHT_PULSE_AFTER_START_MS;
         listenForPulseSignals(true);
 
         // Ask the host to get things ready to start dozing.
         // Once ready, we call startDozing() at which point the CPU may suspend
         // and we will need to acquire a wakelock to do work.
-        mHost.startDozing(new Runnable() {
-            @Override
-            public void run() {
-                if (mDreaming) {
-                    startDozing();
+        mHost.startDozing(mWakeLock.wrap(() -> {
+            if (mDreaming) {
+                startDozing();
 
-                    // From this point until onDreamingStopped we will need to hold a
-                    // wakelock whenever we are doing work.  Note that we never call
-                    // stopDozing because can we just keep dozing until the bitter end.
-                }
+                // From this point until onDreamingStopped we will need to hold a
+                // wakelock whenever we are doing work.  Note that we never call
+                // stopDozing because can we just keep dozing until the bitter end.
             }
-        });
+        }));
     }
 
     @Override
@@ -212,6 +213,10 @@
     }
 
     private void requestPulse(final int reason) {
+        requestPulse(reason, false /* performedProxCheck */);
+    }
+
+    private void requestPulse(final int reason, boolean performedProxCheck) {
         if (mHost != null && mDreaming && !mPulsing) {
             // Let the host know we want to pulse.  Wait for it to be ready, then
             // turn the screen on.  When finished, turn the screen off again.
@@ -224,10 +229,9 @@
                 return;
             }
             final long start = SystemClock.uptimeMillis();
-            final boolean nonBlocking = reason == DozeLog.PULSE_REASON_SENSOR_PICKUP
-                    && mDozeParameters.getPickupPerformsProxCheck();
-            if (nonBlocking) {
-                // proximity check is only done to capture statistics, continue pulsing
+            if (performedProxCheck) {
+                // the caller already performed a successful proximity check; we'll only do one to
+                // capture statistics, continue pulsing immediately.
                 continuePulsing(reason);
             }
             // perform a proximity check
@@ -237,7 +241,7 @@
                     final boolean isNear = result == RESULT_NEAR;
                     final long end = SystemClock.uptimeMillis();
                     DozeLog.traceProximityResult(mContext, isNear, end - start, reason);
-                    if (nonBlocking) {
+                    if (performedProxCheck) {
                         // we already continued
                         return;
                     }
@@ -273,6 +277,9 @@
             public void onPulseFinished() {
                 if (mPulsing && mDreaming) {
                     mPulsing = false;
+                    if (REREGISTER_ALL_SENSORS_ON_SCREEN_OFF) {
+                        reregisterAllSensors();
+                    }
                     turnDisplayOff();
                 }
                 mWakeLock.release(); // needs to be unconditional to balance acquire
@@ -302,22 +309,41 @@
 
     private void listenForPulseSignals(boolean listen) {
         if (DEBUG) Log.d(mTag, "listenForPulseSignals: " + listen);
-        mSigMotionSensor.setListening(listen);
-        mPickupSensor.setListening(listen);
+        for (TriggerSensor s : mSensors) {
+            s.setListening(listen);
+        }
         listenForBroadcasts(listen);
         listenForNotifications(listen);
     }
 
+    private void reregisterAllSensors() {
+        for (TriggerSensor s : mSensors) {
+            s.setListening(false);
+        }
+        for (TriggerSensor s : mSensors) {
+            s.setListening(true);
+        }
+    }
+
     private void listenForBroadcasts(boolean listen) {
         if (listen) {
             final IntentFilter filter = new IntentFilter(PULSE_ACTION);
-            filter.addAction(NOTIFICATION_PULSE_ACTION);
             filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE);
+            filter.addAction(Intent.ACTION_USER_SWITCHED);
             mContext.registerReceiver(mBroadcastReceiver, filter);
+
+            for (TriggerSensor s : mSensors) {
+                if (s.mConfigured && !TextUtils.isEmpty(s.mSetting)) {
+                    mContext.getContentResolver().registerContentObserver(
+                            Settings.Secure.getUriFor(s.mSetting), false /* descendants */,
+                            mSettingsObserver, UserHandle.USER_ALL);
+                }
+            }
             mBroadcastReceiverRegistered = true;
         } else {
             if (mBroadcastReceiverRegistered) {
                 mContext.unregisterReceiver(mBroadcastReceiver);
+                mContext.getContentResolver().unregisterContentObserver(mSettingsObserver);
             }
             mBroadcastReceiverRegistered = false;
         }
@@ -325,93 +351,17 @@
 
     private void listenForNotifications(boolean listen) {
         if (listen) {
-            resetNotificationResets();
             mHost.addCallback(mHostCallback);
-
-            // Continue to pulse for existing LEDs.
-            mNotificationLightOn = mHost.isNotificationLightOn();
-            if (mNotificationLightOn) {
-                updateNotificationPulseDueToLight();
-            }
         } else {
             mHost.removeCallback(mHostCallback);
         }
     }
 
-    private void resetNotificationResets() {
-        if (DEBUG) Log.d(mTag, "resetNotificationResets");
-        mScheduleResetsRemaining = mDozeParameters.getPulseScheduleResets();
-    }
-
-    private void updateNotificationPulseDueToLight() {
-        long timeMs = System.currentTimeMillis();
-        timeMs = Math.max(timeMs, mEarliestPulseDueToLight);
-        updateNotificationPulse(timeMs);
-    }
-
-    private void updateNotificationPulse(long notificationTimeMs) {
-        if (DEBUG) Log.d(mTag, "updateNotificationPulse notificationTimeMs=" + notificationTimeMs);
+    private void requestNotificationPulse() {
+        if (DEBUG) Log.d(mTag, "requestNotificationPulse");
         if (!mDozeParameters.getPulseOnNotifications()) return;
-        if (mScheduleResetsRemaining <= 0) {
-            if (DEBUG) Log.d(mTag, "No more schedule resets remaining");
-            return;
-        }
-        final long pulseDuration = mDozeParameters.getPulseDuration(false /*pickup*/);
-        boolean pulseImmediately = System.currentTimeMillis() >= notificationTimeMs;
-        if ((notificationTimeMs - mLastScheduleResetTime) >= pulseDuration) {
-            mScheduleResetsRemaining--;
-            mLastScheduleResetTime = notificationTimeMs;
-        } else if (!pulseImmediately){
-            if (DEBUG) Log.d(mTag, "Recently updated, not resetting schedule");
-            return;
-        }
-        if (DEBUG) Log.d(mTag, "mScheduleResetsRemaining = " + mScheduleResetsRemaining);
-        mNotificationPulseTime = notificationTimeMs;
-        if (pulseImmediately) {
-            DozeLog.traceNotificationPulse(0);
-            requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
-        }
-        // schedule the rest of the pulses
-        rescheduleNotificationPulse(true /*predicate*/);
-    }
-
-    private PendingIntent notificationPulseIntent(long instance) {
-        return PendingIntent.getBroadcast(mContext, 0,
-                new Intent(NOTIFICATION_PULSE_ACTION)
-                        .setPackage(getPackageName())
-                        .putExtra(EXTRA_INSTANCE, instance)
-                        .setFlags(Intent.FLAG_RECEIVER_FOREGROUND),
-                PendingIntent.FLAG_UPDATE_CURRENT);
-    }
-
-    private void rescheduleNotificationPulse(boolean predicate) {
-        if (DEBUG) Log.d(mTag, "rescheduleNotificationPulse predicate=" + predicate);
-        final PendingIntent notificationPulseIntent = notificationPulseIntent(0);
-        mAlarmManager.cancel(notificationPulseIntent);
-        if (!predicate) {
-            if (DEBUG) Log.d(mTag, "  don't reschedule: predicate is false");
-            return;
-        }
-        final PulseSchedule schedule = mDozeParameters.getPulseSchedule();
-        if (schedule == null) {
-            if (DEBUG) Log.d(mTag, "  don't reschedule: schedule is null");
-            return;
-        }
-        final long now = System.currentTimeMillis();
-        final long time = schedule.getNextTime(now, mNotificationPulseTime);
-        if (time <= 0) {
-            if (DEBUG) Log.d(mTag, "  don't reschedule: time is " + time);
-            return;
-        }
-        final long delta = time - now;
-        if (delta <= 0) {
-            if (DEBUG) Log.d(mTag, "  don't reschedule: delta is " + delta);
-            return;
-        }
-        final long instance = time - mNotificationPulseTime;
-        if (DEBUG) Log.d(mTag, "Scheduling pulse " + instance + " in " + delta + "ms for "
-                + new Date(time));
-        mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, notificationPulseIntent(instance));
+        mNotificationPulseTime = SystemClock.elapsedRealtime();
+        requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
     }
 
     private static String triggerEventToString(TriggerEvent event) {
@@ -434,19 +384,29 @@
                 if (DEBUG) Log.d(mTag, "Received pulse intent");
                 requestPulse(DozeLog.PULSE_REASON_INTENT);
             }
-            if (NOTIFICATION_PULSE_ACTION.equals(intent.getAction())) {
-                final long instance = intent.getLongExtra(EXTRA_INSTANCE, -1);
-                if (DEBUG) Log.d(mTag, "Received notification pulse intent instance=" + instance);
-                DozeLog.traceNotificationPulse(instance);
-                requestPulse(DozeLog.PULSE_REASON_NOTIFICATION);
-                rescheduleNotificationPulse(mNotificationLightOn);
-            }
             if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) {
                 mCarMode = true;
                 if (mCarMode && mDreaming) {
                     finishForCarMode();
                 }
             }
+            if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
+                for (TriggerSensor s : mSensors) {
+                    s.updateListener();
+                }
+            }
+        }
+    };
+
+    private final ContentObserver mSettingsObserver = new ContentObserver(mHandler) {
+        @Override
+        public void onChange(boolean selfChange, Uri uri, int userId) {
+            if (userId != ActivityManager.getCurrentUser()) {
+                return;
+            }
+            for (TriggerSensor s : mSensors) {
+                s.updateListener();
+            }
         }
     };
 
@@ -460,17 +420,13 @@
         @Override
         public void onBuzzBeepBlinked() {
             if (DEBUG) Log.d(mTag, "onBuzzBeepBlinked");
-            updateNotificationPulse(System.currentTimeMillis());
+            requestNotificationPulse();
         }
 
         @Override
         public void onNotificationLight(boolean on) {
-            if (DEBUG) Log.d(mTag, "onNotificationLight on=" + on);
-            if (mNotificationLightOn == on) return;
-            mNotificationLightOn = on;
-            if (mNotificationLightOn) {
-                updateNotificationPulseDueToLight();
-            }
+            if (DEBUG) Log.d(mTag, "onNotificationLight (noop) on=" + on);
+            // noop for now
         }
 
         @Override
@@ -482,18 +438,34 @@
         }
     };
 
+    private Sensor findSensorWithType(String type) {
+        if (TextUtils.isEmpty(type)) {
+            return null;
+        }
+        List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
+        for (Sensor s : sensorList) {
+            if (type.equals(s.getStringType())) {
+                return s;
+            }
+        }
+        return null;
+    }
+
     private class TriggerSensor extends TriggerEventListener {
-        private final Sensor mSensor;
-        private final boolean mConfigured;
-        private final boolean mDebugVibrate;
-        private final int mPulseReason;
+        final Sensor mSensor;
+        final boolean mConfigured;
+        final boolean mDebugVibrate;
+        final int mPulseReason;
+        final String mSetting;
 
         private boolean mRequested;
         private boolean mRegistered;
         private boolean mDisabled;
 
-        public TriggerSensor(int type, boolean configured, boolean debugVibrate, int pulseReason) {
-            mSensor = mSensors.getDefaultSensor(type);
+        public TriggerSensor(Sensor sensor, String setting, boolean configured,
+                boolean debugVibrate, int pulseReason) {
+            mSensor = sensor;
+            mSetting = setting;
             mConfigured = configured;
             mDebugVibrate = debugVibrate;
             mPulseReason = pulseReason;
@@ -511,18 +483,26 @@
             updateListener();
         }
 
-        private void updateListener() {
+        public void updateListener() {
             if (!mConfigured || mSensor == null) return;
-            if (mRequested && !mDisabled && !mRegistered) {
-                mRegistered = mSensors.requestTriggerSensor(this, mSensor);
+            if (mRequested && !mDisabled && enabledBySetting() && !mRegistered) {
+                mRegistered = mSensorManager.requestTriggerSensor(this, mSensor);
                 if (DEBUG) Log.d(mTag, "requestTriggerSensor " + mRegistered);
             } else if (mRegistered) {
-                final boolean rt = mSensors.cancelTriggerSensor(this, mSensor);
+                final boolean rt = mSensorManager.cancelTriggerSensor(this, mSensor);
                 if (DEBUG) Log.d(mTag, "cancelTriggerSensor " + rt);
                 mRegistered = false;
             }
         }
 
+        private boolean enabledBySetting() {
+            if (TextUtils.isEmpty(mSetting)) {
+                return true;
+            }
+            return Settings.Secure.getIntForUser(mContext.getContentResolver(), mSetting, 1,
+                    UserHandle.USER_CURRENT) != 0;
+        }
+
         @Override
         public String toString() {
             return new StringBuilder("{mRegistered=").append(mRegistered)
@@ -538,6 +518,13 @@
             mWakeLock.acquire();
             try {
                 if (DEBUG) Log.d(mTag, "onTrigger: " + triggerEventToString(event));
+                boolean sensorPerformsProxCheck = false;
+                if (mSensor.getType() == Sensor.TYPE_PICK_UP_GESTURE) {
+                    int subType = (int) event.values[0];
+                    MetricsLogger.action(mContext, MetricsEvent.ACTION_AMBIENT_GESTURE, subType);
+                    sensorPerformsProxCheck = mDozeParameters.getPickupSubtypePerformsProxCheck(
+                            subType);
+                }
                 if (mDebugVibrate) {
                     final Vibrator v = (Vibrator) mContext.getSystemService(
                             Context.VIBRATOR_SERVICE);
@@ -549,22 +536,17 @@
                 }
 
                 mRegistered = false;
-                requestPulse(mPulseReason);
+                requestPulse(mPulseReason, sensorPerformsProxCheck);
                 updateListener();  // reregister, this sensor only fires once
 
-                // reset the notification pulse schedule, but only if we think we were not triggered
-                // by a notification-related vibration
-                final long timeSinceNotification = System.currentTimeMillis()
+                // record pickup gesture, also keep track of whether we might have been triggered
+                // by recent vibration.
+                final long timeSinceNotification = SystemClock.elapsedRealtime()
                         - mNotificationPulseTime;
                 final boolean withinVibrationThreshold =
                         timeSinceNotification < mDozeParameters.getPickupVibrationThreshold();
-                if (withinVibrationThreshold) {
-                   if (DEBUG) Log.d(mTag, "Not resetting schedule, recent notification");
-                } else {
-                    resetNotificationResets();
-                }
                 if (mSensor.getType() == Sensor.TYPE_PICK_UP_GESTURE) {
-                    DozeLog.tracePickupPulse(withinVibrationThreshold);
+                    DozeLog.tracePickupPulse(mContext, withinVibrationThreshold);
                 }
             } finally {
                 mWakeLock.release();
@@ -589,7 +571,7 @@
 
         public void check() {
             if (mFinished || mRegistered) return;
-            final Sensor sensor = mSensors.getDefaultSensor(Sensor.TYPE_PROXIMITY);
+            final Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
             if (sensor == null) {
                 if (DEBUG) Log.d(mTag, "No sensor found");
                 finishWithResult(RESULT_UNKNOWN);
@@ -599,7 +581,8 @@
             mPickupSensor.setDisabled(true);
 
             mMaxRange = sensor.getMaximumRange();
-            mSensors.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL, 0, mHandler);
+            mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL, 0,
+                    mHandler);
             mHandler.postDelayed(this, TIMEOUT_DELAY_MS);
             mRegistered = true;
         }
@@ -626,7 +609,7 @@
             if (mFinished) return;
             if (mRegistered) {
                 mHandler.removeCallbacks(this);
-                mSensors.unregisterListener(this);
+                mSensorManager.unregisterListener(this);
                 // we're done - reenable the pickup sensor
                 mPickupSensor.setDisabled(false);
                 mRegistered = false;
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java
index 84d3599..b393cf7 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardService.java
@@ -23,6 +23,7 @@
 import android.os.Debug;
 import android.os.IBinder;
 import android.os.Process;
+import android.os.Trace;
 import android.util.Log;
 
 import com.android.internal.policy.IKeyguardDrawnCallback;
@@ -73,27 +74,33 @@
 
         @Override // Binder interface
         public void verifyUnlock(IKeyguardExitCallback callback) {
+            Trace.beginSection("KeyguardService.mBinder#verifyUnlock");
             checkPermission();
             mKeyguardViewMediator.verifyUnlock(callback);
+            Trace.endSection();
         }
 
         @Override // Binder interface
         public void keyguardDone(boolean authenticated, boolean wakeup) {
+            Trace.beginSection("KeyguardService.mBinder#keyguardDone");
             checkPermission();
             // TODO: Remove wakeup
             mKeyguardViewMediator.keyguardDone(authenticated);
+            Trace.endSection();
         }
 
         @Override // Binder interface
-        public void setOccluded(boolean isOccluded) {
+        public void setOccluded(boolean isOccluded, boolean animate) {
+            Trace.beginSection("KeyguardService.mBinder#setOccluded");
             checkPermission();
-            mKeyguardViewMediator.setOccluded(isOccluded);
+            mKeyguardViewMediator.setOccluded(isOccluded, animate);
+            Trace.endSection();
         }
 
         @Override // Binder interface
-        public void dismiss() {
+        public void dismiss(boolean allowWhileOccluded) {
             checkPermission();
-            mKeyguardViewMediator.dismiss();
+            mKeyguardViewMediator.dismiss(allowWhileOccluded);
         }
 
         @Override // Binder interface
@@ -122,20 +129,26 @@
 
         @Override // Binder interface
         public void onStartedWakingUp() {
+            Trace.beginSection("KeyguardService.mBinder#onStartedWakingUp");
             checkPermission();
             mKeyguardViewMediator.onStartedWakingUp();
+            Trace.endSection();
         }
 
         @Override // Binder interface
         public void onScreenTurningOn(IKeyguardDrawnCallback callback) {
+            Trace.beginSection("KeyguardService.mBinder#onScreenTurningOn");
             checkPermission();
             mKeyguardViewMediator.onScreenTurningOn(callback);
+            Trace.endSection();
         }
 
         @Override // Binder interface
         public void onScreenTurnedOn() {
+            Trace.beginSection("KeyguardService.mBinder#onScreenTurningOn");
             checkPermission();
             mKeyguardViewMediator.onScreenTurnedOn();
+            Trace.endSection();
         }
 
         @Override // Binder interface
@@ -152,8 +165,10 @@
 
         @Override // Binder interface
         public void onSystemReady() {
+            Trace.beginSection("KeyguardService.mBinder#onSystemReady");
             checkPermission();
             mKeyguardViewMediator.onSystemReady();
+            Trace.endSection();
         }
 
         @Override // Binder interface
@@ -176,8 +191,10 @@
 
         @Override
         public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
+            Trace.beginSection("KeyguardService.mBinder#startKeyguardExitAnimation");
             checkPermission();
             mKeyguardViewMediator.startKeyguardExitAnimation(startTime, fadeoutDuration);
+            Trace.endSection();
         }
 
         @Override
diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
index 6759e6b..6103355 100644
--- a/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
+++ b/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java
@@ -16,7 +16,11 @@
 
 package com.android.systemui.keyguard;
 
-import android.annotation.UserIdInt;
+import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
+import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
+import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW;
+import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT;
+
 import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
@@ -42,6 +46,7 @@
 import android.os.RemoteException;
 import android.os.SystemClock;
 import android.os.SystemProperties;
+import android.os.Trace;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.storage.StorageManager;
@@ -81,13 +86,6 @@
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
-import java.util.List;
-
-import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
-import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
-import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL;
-import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_DPM_LOCK_NOW;
-import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_LOCKOUT;
 
 /**
  * Mediates requests related to the keyguard.  This includes queries about the
@@ -360,9 +358,9 @@
             mSwitchingUser = false;
             if (userId != UserHandle.USER_SYSTEM) {
                 UserInfo info = UserManager.get(mContext).getUserInfo(userId);
-                if (info != null && info.isGuest()) {
+                if (info != null && (info.isGuest() || info.isDemo())) {
                     // If we just switched to a guest, try to dismiss keyguard.
-                    dismiss();
+                    dismiss(false /* allowWhileOccluded */);
                 }
             }
         }
@@ -399,8 +397,7 @@
             sendUserPresentBroadcast();
             synchronized (KeyguardViewMediator.this) {
                 // If system user is provisioned, we might want to lock now to avoid showing launcher
-                if (UserManager.isSplitSystemUser()
-                        && KeyguardUpdateMonitor.getCurrentUser() == UserHandle.USER_SYSTEM) {
+                if (mustNotUnlockCurrentUser()) {
                     doKeyguardLocked(null);
                 }
             }
@@ -500,6 +497,22 @@
                         userId);
             }
         }
+
+        @Override
+        public void onTrustChanged(int userId) {
+            if (userId == KeyguardUpdateMonitor.getCurrentUser()) {
+                synchronized (KeyguardViewMediator.this) {
+                    notifyTrustedChangedLocked(mUpdateMonitor.getUserHasTrust(userId));
+                }
+            }
+        }
+
+        @Override
+        public void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) {
+            synchronized (KeyguardViewMediator.this) {
+                notifyHasLockscreenWallpaperChanged(hasLockscreenWallpaper);
+            }
+        }
     };
 
     ViewMediatorCallback mViewMediatorCallback = new ViewMediatorCallback() {
@@ -521,7 +534,9 @@
 
         @Override
         public void keyguardDoneDrawing() {
+            Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardDoneDrawing");
             mHandler.sendEmptyMessage(KEYGUARD_DONE_DRAWING);
+            Trace.endSection();
         }
 
         @Override
@@ -531,6 +546,7 @@
 
         @Override
         public void keyguardDonePending(boolean strongAuth) {
+            Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardDonePending");
             mKeyguardDonePending = true;
             mHideAnimationRun = true;
             mStatusBarKeyguardViewManager.startPreHideAnimation(null /* finishRunnable */);
@@ -539,20 +555,25 @@
             if (strongAuth) {
                 mUpdateMonitor.reportSuccessfulStrongAuthUnlockAttempt();
             }
+            Trace.endSection();
         }
 
         @Override
         public void keyguardGone() {
+            Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#keyguardGone");
             mKeyguardDisplayManager.hide();
+            Trace.endSection();
         }
 
         @Override
         public void readyForKeyguardDone() {
+            Trace.beginSection("KeyguardViewMediator.mViewMediatorCallback#readyForKeyguardDone");
             if (mKeyguardDonePending) {
                 // Somebody has called keyguardDonePending before, which means that we are
                 // authenticated
                 KeyguardViewMediator.this.keyguardDone(true /* authenticated */);
             }
+            Trace.endSection();
         }
 
         @Override
@@ -595,11 +616,7 @@
                 return KeyguardSecurityView.PROMPT_REASON_USER_REQUEST;
             } else if (any && (strongAuth & STRONG_AUTH_REQUIRED_AFTER_LOCKOUT) != 0) {
                 return KeyguardSecurityView.PROMPT_REASON_AFTER_LOCKOUT;
-            } else if (trust && (strongAuth & SOME_AUTH_REQUIRED_AFTER_WRONG_CREDENTIAL) != 0) {
-                return KeyguardSecurityView.PROMPT_REASON_WRONG_CREDENTIAL;
             }
-
-
             return KeyguardSecurityView.PROMPT_REASON_NONE;
         }
     };
@@ -608,6 +625,11 @@
         mPM.userActivity(SystemClock.uptimeMillis(), false);
     }
 
+    boolean mustNotUnlockCurrentUser() {
+        return (UserManager.isSplitSystemUser() || UserManager.isDeviceInDemoMode(mContext))
+                && KeyguardUpdateMonitor.getCurrentUser() == UserHandle.USER_SYSTEM;
+    }
+
     private void setupLocked() {
         mPM = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
         mWM = WindowManagerGlobal.getWindowManagerService();
@@ -889,6 +911,7 @@
      * Let's us know when the device is waking up.
      */
     public void onStartedWakingUp() {
+        Trace.beginSection("KeyguardViewMediator#onStartedWakingUp");
 
         // TODO: Rename all screen off/on references to interactive/sleeping
         synchronized (this) {
@@ -900,15 +923,20 @@
         }
         KeyguardUpdateMonitor.getInstance(mContext).dispatchStartedWakingUp();
         maybeSendUserPresentBroadcast();
+        Trace.endSection();
     }
 
     public void onScreenTurningOn(IKeyguardDrawnCallback callback) {
+        Trace.beginSection("KeyguardViewMediator#onScreenTurningOn");
         notifyScreenOn(callback);
+        Trace.endSection();
     }
 
     public void onScreenTurnedOn() {
+        Trace.beginSection("KeyguardViewMediator#onScreenTurnedOn");
         notifyScreenTurnedOn();
         mUpdateMonitor.dispatchScreenTurnedOn();
+        Trace.endSection();
     }
 
     public void onScreenTurnedOff() {
@@ -936,6 +964,7 @@
      * if there is a secure lock pattern.
      */
     public void onDreamingStarted() {
+        KeyguardUpdateMonitor.getInstance(mContext).dispatchDreamingStarted();
         synchronized (this) {
             if (mDeviceInteractive
                     && mLockPatternUtils.isSecure(KeyguardUpdateMonitor.getCurrentUser())) {
@@ -948,6 +977,7 @@
      * A dream stopped.
      */
     public void onDreamingStopped() {
+        KeyguardUpdateMonitor.getInstance(mContext).dispatchDreamingStopped();
         synchronized (this) {
             if (mDeviceInteractive) {
                 cancelDoKeyguardLaterLocked();
@@ -1022,6 +1052,7 @@
      * @see android.app.KeyguardManager#exitKeyguardSecurely
      */
     public void verifyUnlock(IKeyguardExitCallback callback) {
+        Trace.beginSection("KeyguardViewMediator#verifyUnlock");
         synchronized (this) {
             if (DEBUG) Log.d(TAG, "verifyUnlock");
             if (shouldWaitForProvisioning()) {
@@ -1072,6 +1103,7 @@
                 }
             }
         }
+        Trace.endSection();
     }
 
     /**
@@ -1084,17 +1116,20 @@
     /**
      * Notify us when the keyguard is occluded by another window
      */
-    public void setOccluded(boolean isOccluded) {
+    public void setOccluded(boolean isOccluded, boolean animate) {
+        Trace.beginSection("KeyguardViewMediator#setOccluded");
         if (DEBUG) Log.d(TAG, "setOccluded " + isOccluded);
         mHandler.removeMessages(SET_OCCLUDED);
-        Message msg = mHandler.obtainMessage(SET_OCCLUDED, (isOccluded ? 1 : 0), 0);
+        Message msg = mHandler.obtainMessage(SET_OCCLUDED, isOccluded ? 1 : 0, animate ? 1 : 0);
         mHandler.sendMessage(msg);
+        Trace.endSection();
     }
 
     /**
      * Handles SET_OCCLUDED message sent by setOccluded()
      */
-    private void handleSetOccluded(boolean isOccluded) {
+    private void handleSetOccluded(boolean isOccluded, boolean animate) {
+        Trace.beginSection("KeyguardViewMediator#handleSetOccluded");
         synchronized (KeyguardViewMediator.this) {
             if (mHiding && isOccluded) {
                 // We're in the process of going away but WindowManager wants to show a
@@ -1104,11 +1139,12 @@
 
             if (mOccluded != isOccluded) {
                 mOccluded = isOccluded;
-                mStatusBarKeyguardViewManager.setOccluded(isOccluded);
+                mStatusBarKeyguardViewManager.setOccluded(isOccluded, animate);
                 updateActivityLockScreenState();
                 adjustStatusBarLocked();
             }
         }
+        Trace.endSection();
     }
 
     /**
@@ -1181,8 +1217,7 @@
         }
 
         // In split system user mode, we never unlock system user.
-        if (!UserManager.isSplitSystemUser()
-                || KeyguardUpdateMonitor.getCurrentUser() != UserHandle.USER_SYSTEM
+        if (!mustNotUnlockCurrentUser()
                 || !mUpdateMonitor.isDeviceProvisioned()) {
 
             // if the setup wizard hasn't run yet, don't show
@@ -1230,15 +1265,16 @@
 
     /**
      * Dismiss the keyguard through the security layers.
+     * @param allowWhileOccluded if true, dismiss the keyguard even if it's currently occluded.
      */
-    public void handleDismiss() {
-        if (mShowing && !mOccluded) {
+    public void handleDismiss(boolean allowWhileOccluded) {
+        if (mShowing && (allowWhileOccluded || !mOccluded)) {
             mStatusBarKeyguardViewManager.dismiss();
         }
     }
 
-    public void dismiss() {
-        mHandler.sendEmptyMessage(DISMISS);
+    public void dismiss(boolean allowWhileOccluded) {
+        mHandler.obtainMessage(DISMISS, allowWhileOccluded ? 1 : 0, 0).sendToTarget();
     }
 
     /**
@@ -1298,11 +1334,13 @@
      * @see #handleShow
      */
     private void showLocked(Bundle options) {
+        Trace.beginSection("KeyguardViewMediator#showLocked aqcuiring mShowKeyguardWakeLock");
         if (DEBUG) Log.d(TAG, "showLocked");
         // ensure we stay awake until we are finished displaying the keyguard
         mShowKeyguardWakeLock.acquire();
         Message msg = mHandler.obtainMessage(SHOW, options);
         mHandler.sendMessage(msg);
+        Trace.endSection();
     }
 
     /**
@@ -1310,9 +1348,11 @@
      * @see #handleHide()
      */
     private void hideLocked() {
+        Trace.beginSection("KeyguardViewMediator#hideLocked");
         if (DEBUG) Log.d(TAG, "hideLocked");
         Message msg = mHandler.obtainMessage(HIDE);
         mHandler.sendMessage(msg);
+        Trace.endSection();
     }
 
     public boolean isSecure() {
@@ -1328,6 +1368,9 @@
      */
     public void setCurrentUser(int newUserId) {
         KeyguardUpdateMonitor.setCurrentUser(newUserId);
+        synchronized (this) {
+            notifyTrustedChangedLocked(mUpdateMonitor.getUserHasTrust(newUserId));
+        }
     }
 
     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@@ -1357,10 +1400,13 @@
     };
 
     public void keyguardDone(boolean authenticated) {
+        Trace.beginSection("KeyguardViewMediator#keyguardDone");
         if (DEBUG) Log.d(TAG, "keyguardDone(" + authenticated +")");
+        userActivity();
         EventLog.writeEvent(70000, 2);
         Message msg = mHandler.obtainMessage(KEYGUARD_DONE, authenticated ? 1 : 0);
         mHandler.sendMessage(msg);
+        Trace.endSection();
     }
 
     /**
@@ -1384,7 +1430,9 @@
                     handleReset();
                     break;
                 case VERIFY_UNLOCK:
+                    Trace.beginSection("KeyguardViewMediator#handleMessage VERIFY_UNLOCK");
                     handleVerifyUnlock();
+                    Trace.endSection();
                     break;
                 case NOTIFY_STARTED_GOING_TO_SLEEP:
                     handleNotifyStartedGoingToSleep();
@@ -1393,25 +1441,37 @@
                     handleNotifyFinishedGoingToSleep();
                     break;
                 case NOTIFY_SCREEN_TURNING_ON:
+                    Trace.beginSection("KeyguardViewMediator#handleMessage NOTIFY_SCREEN_TURNING_ON");
                     handleNotifyScreenTurningOn((IKeyguardDrawnCallback) msg.obj);
+                    Trace.endSection();
                     break;
                 case NOTIFY_SCREEN_TURNED_ON:
+                    Trace.beginSection("KeyguardViewMediator#handleMessage NOTIFY_SCREEN_TURNED_ON");
                     handleNotifyScreenTurnedOn();
+                    Trace.endSection();
                     break;
                 case NOTIFY_SCREEN_TURNED_OFF:
                     handleNotifyScreenTurnedOff();
                     break;
                 case NOTIFY_STARTED_WAKING_UP:
+                    Trace.beginSection("KeyguardViewMediator#handleMessage NOTIFY_STARTED_WAKING_UP");
                     handleNotifyStartedWakingUp();
+                    Trace.endSection();
                     break;
                 case KEYGUARD_DONE:
+                    Trace.beginSection("KeyguardViewMediator#handleMessage KEYGUARD_DONE");
                     handleKeyguardDone(msg.arg1 != 0);
+                    Trace.endSection();
                     break;
                 case KEYGUARD_DONE_DRAWING:
+                    Trace.beginSection("KeyguardViewMediator#handleMessage KEYGUARD_DONE_DRAWING");
                     handleKeyguardDoneDrawing();
+                    Trace.endSection();
                     break;
                 case SET_OCCLUDED:
-                    handleSetOccluded(msg.arg1 != 0);
+                    Trace.beginSection("KeyguardViewMediator#handleMessage SET_OCCLUDED");
+                    handleSetOccluded(msg.arg1 != 0, msg.arg2 != 0);
+                    Trace.endSection();
                     break;
                 case KEYGUARD_TIMEOUT:
                     synchronized (KeyguardViewMediator.this) {
@@ -1419,15 +1479,19 @@
                     }
                     break;
                 case DISMISS:
-                    handleDismiss();
+                    handleDismiss(msg.arg1 == 1 ? true : false /* allowWhileOccluded */);
                     break;
                 case START_KEYGUARD_EXIT_ANIM:
+                    Trace.beginSection("KeyguardViewMediator#handleMessage START_KEYGUARD_EXIT_ANIM");
                     StartKeyguardExitAnimParams params = (StartKeyguardExitAnimParams) msg.obj;
                     handleStartKeyguardExitAnimation(params.startTime, params.fadeoutDuration);
                     FalsingManager.getInstance(mContext).onSucccessfulUnlock();
+                    Trace.endSection();
                     break;
                 case KEYGUARD_DONE_PENDING_TIMEOUT:
+                    Trace.beginSection("KeyguardViewMediator#handleMessage KEYGUARD_DONE_PENDING_TIMEOUT");
                     Log.w(TAG, "Timeout while waiting for activity drawn!");
+                    Trace.endSection();
                     // Fall through.
                 case ON_ACTIVITY_DRAWN:
                     handleOnActivityDrawn();
@@ -1441,6 +1505,7 @@
      * @see #KEYGUARD_DONE
      */
     private void handleKeyguardDone(boolean authenticated) {
+        Trace.beginSection("KeyguardViewMediator#handleKeyguardDone");
         final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
         if (mLockPatternUtils.isSecure(currentUser)) {
             mLockPatternUtils.getDevicePolicyManager().reportKeyguardDismissed(currentUser);
@@ -1478,6 +1543,7 @@
         }
 
         handleHide();
+        Trace.endSection();
     }
 
     private void sendUserPresentBroadcast() {
@@ -1502,6 +1568,7 @@
      * @see #KEYGUARD_DONE_DRAWING
      */
     private void handleKeyguardDoneDrawing() {
+        Trace.beginSection("KeyguardViewMediator#handleKeyguardDoneDrawing");
         synchronized(this) {
             if (DEBUG) Log.d(TAG, "handleKeyguardDoneDrawing");
             if (mWaitingUntilKeyguardVisible) {
@@ -1515,6 +1582,7 @@
                 mHandler.removeMessages(KEYGUARD_DONE_DRAWING);
             }
         }
+        Trace.endSection();
     }
 
     private void playSounds(boolean locked) {
@@ -1546,10 +1614,12 @@
     }
 
     private void updateActivityLockScreenState() {
+        Trace.beginSection("KeyguardViewMediator#updateActivityLockScreenState");
         try {
             ActivityManagerNative.getDefault().setLockScreenShown(mShowing, mOccluded);
         } catch (RemoteException e) {
         }
+        Trace.endSection();
     }
 
     /**
@@ -1557,6 +1627,7 @@
      * @see #SHOW
      */
     private void handleShow(Bundle options) {
+        Trace.beginSection("KeyguardViewMediator#handleShow");
         final int currentUser = KeyguardUpdateMonitor.getCurrentUser();
         if (mLockPatternUtils.isSecure(currentUser)) {
             mLockPatternUtils.getDevicePolicyManager().reportKeyguardSecured(currentUser);
@@ -1582,11 +1653,14 @@
             mShowKeyguardWakeLock.release();
         }
         mKeyguardDisplayManager.show();
+        Trace.endSection();
     }
 
     private final Runnable mKeyguardGoingAwayRunnable = new Runnable() {
         @Override
         public void run() {
+            Trace.beginSection("KeyguardViewMediator.mKeyGuardGoingAwayRunnable");
+            if (DEBUG) Log.d(TAG, "keyguardGoingAway");
             try {
                 mStatusBarKeyguardViewManager.keyguardGoingAway();
 
@@ -1609,6 +1683,7 @@
             } catch (RemoteException e) {
                 Log.e(TAG, "Error while calling WindowManager", e);
             }
+            Trace.endSection();
         }
     };
 
@@ -1617,11 +1692,11 @@
      * @see #HIDE
      */
     private void handleHide() {
+        Trace.beginSection("KeyguardViewMediator#handleHide");
         synchronized (KeyguardViewMediator.this) {
             if (DEBUG) Log.d(TAG, "handleHide");
 
-            if (UserManager.isSplitSystemUser()
-                    && KeyguardUpdateMonitor.getCurrentUser() == UserHandle.USER_SYSTEM) {
+            if (mustNotUnlockCurrentUser()) {
                 // In split system user mode, we never unlock system user. The end user has to
                 // switch to another user.
                 // TODO: We should stop it early by disabling the swipe up flow. Right now swipe up
@@ -1645,6 +1720,7 @@
                         mHideAnimation.getDuration());
             }
         }
+        Trace.endSection();
     }
 
     private void handleOnActivityDrawn() {
@@ -1655,6 +1731,9 @@
     }
 
     private void handleStartKeyguardExitAnimation(long startTime, long fadeoutDuration) {
+        Trace.beginSection("KeyguardViewMediator#handleStartKeyguardExitAnimation");
+        if (DEBUG) Log.d(TAG, "handleStartKeyguardExitAnimation startTime=" + startTime
+                + " fadeoutDuration=" + fadeoutDuration);
         synchronized (KeyguardViewMediator.this) {
 
             if (!mHiding) {
@@ -1669,6 +1748,7 @@
                 // this to our ViewRootImpl.
                 mStatusBarKeyguardViewManager.getViewRootImpl().setReportNextDraw();
                 notifyDrawn(mDrawnCallback);
+                mDrawnCallback = null;
             }
 
             // only play "unlock" noises if not on a call (since the incall UI
@@ -1677,6 +1757,7 @@
                 playSounds(false);
             }
 
+            mWakeAndUnlocking = false;
             setShowingLocked(false);
             mStatusBarKeyguardViewManager.hide(startTime, fadeoutDuration);
             resetKeyguardDonePendingLocked();
@@ -1685,6 +1766,7 @@
             adjustStatusBarLocked();
             sendUserPresentBroadcast();
         }
+        Trace.endSection();
     }
 
     private void adjustStatusBarLocked() {
@@ -1736,12 +1818,14 @@
      * @see #VERIFY_UNLOCK
      */
     private void handleVerifyUnlock() {
+        Trace.beginSection("KeyguardViewMediator#handleVerifyUnlock");
         synchronized (KeyguardViewMediator.this) {
             if (DEBUG) Log.d(TAG, "handleVerifyUnlock");
             setShowingLocked(true);
             mStatusBarKeyguardViewManager.verifyUnlock();
             updateActivityLockScreenState();
         }
+        Trace.endSection();
     }
 
     private void handleNotifyStartedGoingToSleep() {
@@ -1763,13 +1847,16 @@
     }
 
     private void handleNotifyStartedWakingUp() {
+        Trace.beginSection("KeyguardViewMediator#handleMotifyStartedWakingUp");
         synchronized (KeyguardViewMediator.this) {
             if (DEBUG) Log.d(TAG, "handleNotifyWakingUp");
             mStatusBarKeyguardViewManager.onStartedWakingUp();
         }
+        Trace.endSection();
     }
 
     private void handleNotifyScreenTurningOn(IKeyguardDrawnCallback callback) {
+        Trace.beginSection("KeyguardViewMediator#handleNotifyScreenTurningOn");
         synchronized (KeyguardViewMediator.this) {
             if (DEBUG) Log.d(TAG, "handleNotifyScreenTurningOn");
             mStatusBarKeyguardViewManager.onScreenTurningOn();
@@ -1781,29 +1868,35 @@
                 }
             }
         }
+        Trace.endSection();
     }
 
     private void handleNotifyScreenTurnedOn() {
+        Trace.beginSection("KeyguardViewMediator#handleNotifyScreenTurnedOn");
         synchronized (this) {
             if (DEBUG) Log.d(TAG, "handleNotifyScreenTurnedOn");
             mStatusBarKeyguardViewManager.onScreenTurnedOn();
         }
+        Trace.endSection();
     }
 
     private void handleNotifyScreenTurnedOff() {
         synchronized (this) {
             if (DEBUG) Log.d(TAG, "handleNotifyScreenTurnedOff");
             mStatusBarKeyguardViewManager.onScreenTurnedOff();
+            mDrawnCallback = null;
             mWakeAndUnlocking = false;
         }
     }
 
     private void notifyDrawn(final IKeyguardDrawnCallback callback) {
+        Trace.beginSection("KeyguardViewMediator#notifyDrawn");
         try {
             callback.onDrawn();
         } catch (RemoteException e) {
             Slog.w(TAG, "Exception calling onDrawn():", e);
         }
+        Trace.endSection();
     }
 
     private void resetKeyguardDonePendingLocked() {
@@ -1823,8 +1916,10 @@
     }
 
     public void onWakeAndUnlocking() {
+        Trace.beginSection("KeyguardViewMediator#onWakeAndUnlocking");
         mWakeAndUnlocking = true;
         keyguardDone(true /* authenticated */);
+        Trace.endSection();
     }
 
     public StatusBarKeyguardViewManager registerStatusBar(PhoneStatusBar phoneStatusBar,
@@ -1837,9 +1932,11 @@
     }
 
     public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) {
+        Trace.beginSection("KeyguardViewMediator#startKeyguardExitAnimation");
         Message msg = mHandler.obtainMessage(START_KEYGUARD_EXIT_ANIM,
                 new StartKeyguardExitAnimParams(startTime, fadeoutDuration));
         mHandler.sendMessage(msg);
+        Trace.endSection();
     }
 
     public void onActivityDrawn() {
@@ -1908,6 +2005,35 @@
         }
     }
 
+    private void notifyTrustedChangedLocked(boolean trusted) {
+        int size = mKeyguardStateCallbacks.size();
+        for (int i = size - 1; i >= 0; i--) {
+            try {
+                mKeyguardStateCallbacks.get(i).onTrustedChanged(trusted);
+            } catch (RemoteException e) {
+                Slog.w(TAG, "Failed to call notifyTrustedChangedLocked", e);
+                if (e instanceof DeadObjectException) {
+                    mKeyguardStateCallbacks.remove(i);
+                }
+            }
+        }
+    }
+
+    private void notifyHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) {
+        int size = mKeyguardStateCallbacks.size();
+        for (int i = size - 1; i >= 0; i--) {
+            try {
+                mKeyguardStateCallbacks.get(i).onHasLockscreenWallpaperChanged(
+                        hasLockscreenWallpaper);
+            } catch (RemoteException e) {
+                Slog.w(TAG, "Failed to call onHasLockscreenWallpaperChanged", e);
+                if (e instanceof DeadObjectException) {
+                    mKeyguardStateCallbacks.remove(i);
+                }
+            }
+        }
+    }
+
     public void addStateMonitorCallback(IKeyguardStateCallback callback) {
         synchronized (this) {
             mKeyguardStateCallbacks.add(callback);
@@ -1915,8 +2041,11 @@
                 callback.onSimSecureStateChanged(mUpdateMonitor.isSimPinSecure());
                 callback.onShowingStateChanged(mShowing);
                 callback.onInputRestrictedStateChanged(mInputRestricted);
+                callback.onTrustedChanged(mUpdateMonitor.getUserHasTrust(
+                        KeyguardUpdateMonitor.getCurrentUser()));
+                callback.onHasLockscreenWallpaperChanged(mUpdateMonitor.hasLockscreenWallpaper());
             } catch (RemoteException e) {
-                Slog.w(TAG, "Failed to call onShowingStateChanged or onSimSecureStateChanged or onInputRestrictedStateChanged", e);
+                Slog.w(TAG, "Failed to call to IKeyguardStateCallback", e);
             }
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/DataUsageGraph.java b/packages/SystemUI/src/com/android/systemui/qs/DataUsageGraph.java
index aff5d2b..5f23a40 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/DataUsageGraph.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/DataUsageGraph.java
@@ -24,6 +24,7 @@
 import android.util.AttributeSet;
 import android.view.View;
 
+import com.android.settingslib.Utils;
 import com.android.systemui.R;
 
 public class DataUsageGraph extends View {
@@ -45,7 +46,7 @@
         super(context, attrs);
         final Resources res = context.getResources();
         mTrackColor = context.getColor(R.color.data_usage_graph_track);
-        mUsageColor = context.getColor(R.color.system_accent_color);
+        mUsageColor = Utils.getColorAccent(context);
         mOverlimitColor = context.getColor(R.color.system_warning_color);
         mWarningColor = context.getColor(R.color.data_usage_graph_warning);
         mMarkerWidth = res.getDimensionPixelSize(R.dimen.data_usage_graph_marker_width);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java
index 71bd798..1c242e9 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/PagedTileLayout.java
@@ -10,6 +10,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+
 import com.android.systemui.R;
 import com.android.systemui.qs.QSPanel.QSTileLayout;
 import com.android.systemui.qs.QSPanel.TileRecord;
@@ -87,9 +88,9 @@
         if (mListening == listening) return;
         mListening = listening;
         if (mListening) {
-            mPages.get(mPosition).setListening(listening);
+            setPageListening(mPosition, true);
             if (mOffPage) {
-                mPages.get(mPosition + 1).setListening(listening);
+                setPageListening(mPosition + 1, true);
             }
         } else {
             // Make sure no pages are listening.
@@ -130,6 +131,9 @@
 
     private void setPageListening(int position, boolean listening) {
         if (position >= mPages.size()) return;
+        if (isLayoutRtl()) {
+            position = mPages.size() - 1 - position;
+        }
         mPages.get(position).setListening(listening);
     }
 
@@ -207,6 +211,7 @@
             }
             if (DEBUG) Log.d(TAG, "Size: " + mNumPages);
             mPageIndicator.setNumPages(mNumPages);
+            mDecorGroup.setVisibility(mNumPages > 1 ? View.VISIBLE : View.GONE);
             setAdapter(mAdapter);
             mAdapter.notifyDataSetChanged();
             setCurrentItem(0, false);
@@ -238,7 +243,8 @@
                 maxHeight = height;
             }
         }
-        setMeasuredDimension(getMeasuredWidth(), maxHeight + mDecorGroup.getMeasuredHeight());
+        setMeasuredDimension(getMeasuredWidth(), maxHeight
+                + (mDecorGroup.getVisibility() != View.GONE ? mDecorGroup.getMeasuredHeight() : 0));
     }
 
     private final Runnable mDistribute = new Runnable() {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java b/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
index 2dcb5f4..5c8a6e2 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSAnimator.java
@@ -41,8 +41,7 @@
     private static final String ALLOW_FANCY_ANIMATION = "sysui_qs_fancy_anim";
     private static final String MOVE_FULL_ROWS = "sysui_qs_move_whole_rows";
 
-    public static final float EXPANDED_TILE_DELAY = .7f;
-    private static final float LAST_ROW_EXPANDED_DELAY = .86f;
+    public static final float EXPANDED_TILE_DELAY = .86f;
 
     private final ArrayList<View> mAllViews = new ArrayList<>();
     private final ArrayList<View> mTopFiveQs = new ArrayList<>();
@@ -58,7 +57,7 @@
     private TouchAnimator mTranslationXAnimator;
     private TouchAnimator mTranslationYAnimator;
     private TouchAnimator mNonfirstPageAnimator;
-    private TouchAnimator mLastRowAnimator;
+    private TouchAnimator mBrightnessAnimator;
 
     private boolean mOnKeyguard;
 
@@ -144,7 +143,6 @@
         TouchAnimator.Builder firstPageBuilder = new Builder();
         TouchAnimator.Builder translationXBuilder = new Builder();
         TouchAnimator.Builder translationYBuilder = new Builder();
-        TouchAnimator.Builder lastRowBuilder = new Builder();
 
         if (mQsPanel.getHost() == null) return;
         Collection<QSTile<?>> tiles = mQsPanel.getHost().getTiles();
@@ -152,7 +150,6 @@
         int[] loc1 = new int[2];
         int[] loc2 = new int[2];
         int lastXDiff = 0;
-        int lastYDiff = 0;
         int lastX = 0;
 
         clearAnimationState();
@@ -163,6 +160,10 @@
 
         for (QSTile<?> tile : tiles) {
             QSTileBaseView tileView = mQsPanel.getTileView(tile);
+            if (tileView == null) {
+                Log.e(TAG, "tileView is null " + tile.getTileSpec());
+                continue;
+            }
             final TextView label = ((QSTileView) tileView).getLabel();
             final View tileIcon = tileView.getIcon().getIconView();
             if (count < mNumQuickTiles && mAllowFancy) {
@@ -175,7 +176,6 @@
                 final int xDiff = loc2[0] - loc1[0];
                 final int yDiff = loc2[1] - loc1[1];
                 lastXDiff = loc1[0] - lastX;
-                lastYDiff = yDiff;
                 // Move the quick tile right from its location to the new one.
                 translationXBuilder.addFloat(quickTileView, "translationX", 0, xDiff);
                 translationYBuilder.addFloat(quickTileView, "translationY", 0, yDiff);
@@ -209,27 +209,42 @@
 
                 mAllViews.add(tileIcon);
             } else {
-                lastRowBuilder.addFloat(tileView, "alpha", 0, 1);
+                firstPageBuilder.addFloat(tileView, "alpha", 0, 1);
             }
             mAllViews.add(tileView);
             mAllViews.add(label);
             count++;
         }
         if (mAllowFancy) {
+            // Make brightness appear static position and alpha in through second half.
+            View brightness = mQsPanel.getBrightnessView();
+            if (brightness != null) {
+                firstPageBuilder.addFloat(brightness, "translationY", mQsPanel.getHeight(), 0);
+                mBrightnessAnimator = new TouchAnimator.Builder()
+                        .addFloat(brightness, "alpha", 0, 1)
+                        .setStartDelay(.5f)
+                        .build();
+                mAllViews.add(brightness);
+            } else {
+                mBrightnessAnimator = null;
+            }
             mFirstPageAnimator = firstPageBuilder
                     .setListener(this)
                     .build();
             // Fade in the tiles/labels as we reach the final position.
             mFirstPageDelayedAnimator = new TouchAnimator.Builder()
                     .setStartDelay(EXPANDED_TILE_DELAY)
-                    .addFloat(mQsPanel.getTileLayout(), "alpha", 0, 1).build();
-            mLastRowAnimator = lastRowBuilder
-                    .setStartDelay(LAST_ROW_EXPANDED_DELAY)
-                    .build();
-            Path path = new Path();
-            path.moveTo(0, 0);
-            path.cubicTo(0, 0, 0, 1, 1, 1);
-            PathInterpolatorBuilder interpolatorBuilder = new PathInterpolatorBuilder(0, 0, 0, 1);
+                    .addFloat(mQsPanel.getTileLayout(), "alpha", 0, 1)
+                    .addFloat(mQsPanel.getFooter().getView(), "alpha", 0, 1).build();
+            mAllViews.add(mQsPanel.getFooter().getView());
+            float px = 0;
+            float py = 1;
+            if (tiles.size() <= 3) {
+                px = 1;
+            } else if (tiles.size() <= 6) {
+                px = .4f;
+            }
+            PathInterpolatorBuilder interpolatorBuilder = new PathInterpolatorBuilder(0, 0, px, py);
             translationXBuilder.setInterpolator(interpolatorBuilder.getXInterpolator());
             translationYBuilder.setInterpolator(interpolatorBuilder.getYInterpolator());
             mTranslationXAnimator = translationXBuilder.build();
@@ -279,7 +294,9 @@
             mFirstPageDelayedAnimator.setPosition(position);
             mTranslationXAnimator.setPosition(position);
             mTranslationYAnimator.setPosition(position);
-            mLastRowAnimator.setPosition(position);
+            if (mBrightnessAnimator != null) {
+                mBrightnessAnimator.setPosition(position);
+            }
         } else {
             mNonfirstPageAnimator.setPosition(position);
         }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSContainer.java b/packages/SystemUI/src/com/android/systemui/qs/QSContainer.java
index d5fb8f2..19a5d52 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSContainer.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSContainer.java
@@ -146,7 +146,9 @@
             return getHeight();
         }
         if (mQSDetail.isClosingDetail()) {
-            return mQSPanel.getGridHeight() + mHeader.getCollapsedHeight() + getPaddingBottom();
+            int panelHeight = ((LayoutParams) mQSPanel.getLayoutParams()).topMargin
+                    + mQSPanel.getMeasuredHeight();
+            return panelHeight + getPaddingBottom();
         } else {
             return getMeasuredHeight();
         }
@@ -235,6 +237,10 @@
         mQSPanel.setListening(mListening && mQsExpanded);
     }
 
+    public void setHeaderListening(boolean listening) {
+        mHeader.setListening(listening);
+    }
+
     public void setQsExpansion(float expansion, float headerTranslation) {
         if (DEBUG) Log.d(TAG, "setQSExpansion " + expansion + " " + headerTranslation);
         mQsExpansion = expansion;
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSDetail.java b/packages/SystemUI/src/com/android/systemui/qs/QSDetail.java
index a40e5b7..662b6a7 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSDetail.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSDetail.java
@@ -63,11 +63,12 @@
     private boolean mScanState;
     private boolean mClosingDetail;
     private boolean mFullyExpanded;
-    private View mQsDetailHeaderBack;
     private BaseStatusBarHeader mHeader;
     private boolean mTriggeredExpand;
     private int mOpenX;
     private int mOpenY;
+    private boolean mAnimatingOpen;
+    private boolean mSwitchState;
 
     public QSDetail(Context context, @Nullable AttributeSet attrs) {
         super(context, attrs);
@@ -92,7 +93,6 @@
         mDetailDoneButton = (TextView) findViewById(android.R.id.button1);
 
         mQsDetailHeader = findViewById(R.id.qs_detail_header);
-        mQsDetailHeaderBack = mQsDetailHeader.findViewById(com.android.internal.R.id.up);
         mQsDetailHeaderTitle = (TextView) mQsDetailHeader.findViewById(android.R.id.title);
         mQsDetailHeaderSwitch = (Switch) mQsDetailHeader.findViewById(android.R.id.toggle);
         mQsDetailHeaderProgress = (ImageView) findViewById(R.id.qs_detail_header_progress);
@@ -109,7 +109,6 @@
                 mQsPanel.closeDetail();
             }
         };
-        mQsDetailHeaderBack.setOnClickListener(doneListener);
         mDetailDoneButton.setOnClickListener(doneListener);
     }
 
@@ -161,7 +160,7 @@
                 mQsDetailHeader.setClickable(false);
             } else {
                 mQsDetailHeaderSwitch.setVisibility(VISIBLE);
-                mQsDetailHeaderSwitch.setChecked(toggleState);
+                handleToggleStateChanged(toggleState, adapter.getToggleEnabled());
                 mQsDetailHeader.setClickable(true);
                 mQsDetailHeader.setOnClickListener(new OnClickListener() {
                     @Override
@@ -231,6 +230,7 @@
         }
         sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
         if (visibleDiff) {
+            mAnimatingOpen = adapter != null;
             if (mFullyExpanded || mDetailAdapter != null) {
                 setAlpha(1);
                 mClipper.animateCircularClip(x, y, mDetailAdapter != null, listener);
@@ -243,8 +243,14 @@
         }
     }
 
-    private void handleToggleStateChanged(boolean state) {
+    private void handleToggleStateChanged(boolean state, boolean toggleEnabled) {
+        mSwitchState = state;
+        if (mAnimatingOpen) {
+            return;
+        }
         mQsDetailHeaderSwitch.setChecked(state);
+        mQsDetailHeader.setEnabled(toggleEnabled);
+        mQsDetailHeaderSwitch.setEnabled(toggleEnabled);
     }
 
     private void handleScanStateChanged(boolean state) {
@@ -260,13 +266,19 @@
         }
     }
 
+    private void checkPendingAnimations() {
+        handleToggleStateChanged(mSwitchState,
+                            mDetailAdapter != null && mDetailAdapter.getToggleEnabled());
+    }
+
     private final QSPanel.Callback mQsPanelCallback = new QSPanel.Callback() {
         @Override
         public void onToggleStateChanged(final boolean state) {
             post(new Runnable() {
                 @Override
                 public void run() {
-                    handleToggleStateChanged(state);
+                    handleToggleStateChanged(state,
+                            mDetailAdapter != null && mDetailAdapter.getToggleEnabled());
                 }
             });
         }
@@ -297,6 +309,8 @@
             // If we have been cancelled, remove the listener so that onAnimationEnd doesn't get
             // called, this will avoid accidentally turning off the grid when we don't want to.
             animation.removeListener(this);
+            mAnimatingOpen = false;
+            checkPendingAnimations();
         };
 
         @Override
@@ -306,6 +320,8 @@
                 mQsPanel.setGridContentVisibility(false);
                 mHeader.setVisibility(View.INVISIBLE);
             }
+            mAnimatingOpen = false;
+            checkPendingAnimations();
         }
     };
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSFooter.java b/packages/SystemUI/src/com/android/systemui/qs/QSFooter.java
index 932b4f5..4ac629d 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSFooter.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSFooter.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.graphics.drawable.Drawable;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -57,6 +58,7 @@
     private boolean mIsVisible;
     private boolean mIsIconVisible;
     private int mFooterTextId;
+    private int mFooterIconId;
 
     public QSFooter(QSPanel qsPanel, Context context) {
         mRootView = LayoutInflater.from(context)
@@ -64,6 +66,7 @@
         mRootView.setOnClickListener(this);
         mFooterText = (TextView) mRootView.findViewById(R.id.footer_text);
         mFooterIcon = (ImageView) mRootView.findViewById(R.id.footer_icon);
+        mFooterIconId = R.drawable.ic_qs_vpn;
         mContext = context;
         mMainHandler = new Handler();
     }
@@ -117,7 +120,14 @@
             mFooterTextId = R.string.device_owned_footer;
             mIsVisible = true;
         } else {
-            mFooterTextId = R.string.vpn_footer;
+            boolean isBranded = mSecurityController.isVpnBranded();
+            mFooterTextId = isBranded ? R.string.branded_vpn_footer : R.string.vpn_footer;
+            // Update the VPN footer icon, if needed.
+            int footerIconId = isBranded ? R.drawable.ic_qs_branded_vpn : R.drawable.ic_qs_vpn;
+            if (mFooterIconId != footerIconId) {
+                mFooterIconId = footerIconId;
+                mMainHandler.post(mUpdateIcon);
+            }
             mIsVisible = mIsIconVisible;
         }
         mMainHandler.post(mUpdateDisplayState);
@@ -137,11 +147,15 @@
         String primaryVpn = mSecurityController.getPrimaryVpnName();
         String profileVpn = mSecurityController.getProfileVpnName();
         boolean managed = mSecurityController.hasProfileOwner();
+        boolean isBranded = deviceOwner == null && mSecurityController.isVpnBranded();
 
         mDialog = new SystemUIDialog(mContext);
-        mDialog.setTitle(getTitle(deviceOwner));
-        mDialog.setMessage(getMessage(deviceOwner, profileOwner, primaryVpn, profileVpn, managed));
-        mDialog.setButton(DialogInterface.BUTTON_POSITIVE, getPositiveButton(), this);
+        if (!isBranded) {
+            mDialog.setTitle(getTitle(deviceOwner));
+        }
+        mDialog.setMessage(getMessage(deviceOwner, profileOwner, primaryVpn, profileVpn, managed,
+                isBranded));
+        mDialog.setButton(DialogInterface.BUTTON_POSITIVE, getPositiveButton(isBranded), this);
         if (mSecurityController.isVpnEnabled() && !mSecurityController.isVpnRestricted()) {
             mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getSettingsButton(), this);
         }
@@ -152,12 +166,12 @@
         return mContext.getString(R.string.status_bar_settings_settings_button);
     }
 
-    private String getPositiveButton() {
-        return mContext.getString(R.string.quick_settings_done);
+    private String getPositiveButton(boolean isBranded) {
+        return mContext.getString(isBranded ? android.R.string.ok : R.string.quick_settings_done);
     }
 
     private String getMessage(String deviceOwner, String profileOwner, String primaryVpn,
-            String profileVpn, boolean primaryUserIsManaged) {
+            String profileVpn, boolean primaryUserIsManaged, boolean isBranded) {
         // Show a special warning when the device has device owner, but --
         // TODO See b/25779452 -- device owner doesn't actually have monitoring power.
         if (deviceOwner != null) {
@@ -173,8 +187,12 @@
                 return mContext.getString(R.string.monitoring_description_app_personal_work,
                         profileOwner, profileVpn, primaryVpn);
             } else {
-                return mContext.getString(R.string.monitoring_description_app_personal,
-                        primaryVpn);
+                if (isBranded) {
+                    return mContext.getString(R.string.branded_monitoring_description_app_personal);
+                } else {
+                    return mContext.getString(R.string.monitoring_description_app_personal,
+                            primaryVpn);
+                }
             }
         } else if (profileVpn != null) {
             return mContext.getString(R.string.monitoring_description_app_work,
@@ -196,6 +214,13 @@
         }
     }
 
+    private final Runnable mUpdateIcon = new Runnable() {
+        @Override
+        public void run() {
+            mFooterIcon.setImageResource(mFooterIconId);
+        }
+    };
+
     private final Runnable mUpdateDisplayState = new Runnable() {
         @Override
         public void run() {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
index 636a9a50..fef8930 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSPanel.java
@@ -23,7 +23,6 @@
 import android.os.Handler;
 import android.os.Message;
 import android.util.AttributeSet;
-import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.ImageView;
@@ -106,8 +105,6 @@
                 R.layout.qs_paged_tile_layout, this, false);
         mTileLayout.setListening(mListening);
         addView((View) mTileLayout);
-        findViewById(android.R.id.edit).setOnClickListener(view ->
-                mHost.startRunnableDismissingKeyguard(() -> showEdit(view)));
     }
 
     public boolean isShowingCustomize() {
@@ -168,6 +165,10 @@
         brightnessSlider.setMirrorController(c);
     }
 
+    View getBrightnessView() {
+        return mBrightnessView;
+    }
+
     public void setCallback(Callback callback) {
         mCallback = callback;
     }
@@ -181,6 +182,7 @@
         if (mCustomizePanel != null) {
             mCustomizePanel.setHost(mHost);
         }
+        mBrightnessController.setBackgroundLooper(host.getLooper());
     }
 
     public QSTileHost getHost() {
@@ -244,10 +246,12 @@
         if (mListening) {
             refreshAllTiles();
         }
-        if (listening) {
-            mBrightnessController.registerCallbacks();
-        } else {
-            mBrightnessController.unregisterCallbacks();
+        if (mBrightnessView.getVisibility() == View.VISIBLE) {
+            if (listening) {
+                mBrightnessController.registerCallbacks();
+            } else {
+                mBrightnessController.unregisterCallbacks();
+            }
         }
     }
 
@@ -369,7 +373,7 @@
     }
 
 
-    private void showEdit(final View v) {
+    public void showEdit(final View v) {
         v.post(new Runnable() {
             @Override
             public void run() {
@@ -377,8 +381,8 @@
                     if (!mCustomizePanel.isCustomizing()) {
                         int[] loc = new int[2];
                         v.getLocationInWindow(loc);
-                        int x = loc[0];
-                        int y = loc[1];
+                        int x = loc[0] + v.getWidth() / 2;
+                        int y = loc[1] + v.getHeight() / 2;
                         mCustomizePanel.show(x, y);
                     }
                 }
@@ -503,6 +507,10 @@
         return null;
     }
 
+    public QSFooter getFooter() {
+        return mFooter;
+    }
+
     private class H extends Handler {
         private static final int SHOW_DETAIL = 1;
         private static final int SET_TILE_VISIBILITY = 2;
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
index 27b079a..2fda6ea 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSTile.java
@@ -39,7 +39,6 @@
 import com.android.systemui.statusbar.policy.BatteryController;
 import com.android.systemui.statusbar.policy.BluetoothController;
 import com.android.systemui.statusbar.policy.CastController;
-import com.android.systemui.statusbar.policy.NightModeController;
 import com.android.systemui.statusbar.policy.FlashlightController;
 import com.android.systemui.statusbar.policy.HotspotController;
 import com.android.systemui.statusbar.policy.KeyguardMonitor;
@@ -149,6 +148,9 @@
     public interface DetailAdapter {
         CharSequence getTitle();
         Boolean getToggleState();
+        default boolean getToggleEnabled() {
+            return true;
+        }
         View createDetailView(Context context, View convertView, ViewGroup parent);
         Intent getSettingsIntent();
         void setToggleState(boolean state);
@@ -447,7 +449,6 @@
         UserInfoController getUserInfoController();
         BatteryController getBatteryController();
         TileServices getTileServices();
-        NightModeController getNightModeController();
         void removeTile(String tileSpec);
         ManagedProfileController getManagedProfileController();
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QSTileBaseView.java b/packages/SystemUI/src/com/android/systemui/qs/QSTileBaseView.java
index feacaa0..5ed19ef 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QSTileBaseView.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QSTileBaseView.java
@@ -63,6 +63,7 @@
         setClipChildren(false);
         setClipToPadding(false);
         mCollapsedView = collapsedView;
+        setFocusable(true);
     }
 
     private Drawable newTileBackground() {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/QuickQSPanel.java b/packages/SystemUI/src/com/android/systemui/qs/QuickQSPanel.java
index f287f1b..f09275d 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/QuickQSPanel.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/QuickQSPanel.java
@@ -146,12 +146,11 @@
     };
 
     public int getNumQuickTiles(Context context) {
-        return TunerService.get(context).getValue(NUM_QUICK_TILES, 5);
+        return TunerService.get(context).getValue(NUM_QUICK_TILES, 6);
     }
 
     private static class HeaderTileLayout extends LinearLayout implements QSTileLayout {
 
-        private final Space mEndSpacer;
         protected final ArrayList<TileRecord> mRecords = new ArrayList<>();
         private boolean mListening;
 
@@ -161,25 +160,6 @@
             setClipToPadding(false);
             setGravity(Gravity.CENTER_VERTICAL);
             setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
-
-            mEndSpacer = new Space(context);
-            mEndSpacer.setLayoutParams(generateLayoutParams());
-            updateDownArrowMargin();
-            addView(mEndSpacer);
-            setOrientation(LinearLayout.HORIZONTAL);
-        }
-
-        @Override
-        protected void onConfigurationChanged(Configuration newConfig) {
-            super.onConfigurationChanged(newConfig);
-            updateDownArrowMargin();
-        }
-
-        private void updateDownArrowMargin() {
-            LayoutParams params = (LayoutParams) mEndSpacer.getLayoutParams();
-            params.setMarginStart(mContext.getResources().getDimensionPixelSize(
-                    R.dimen.qs_expand_margin));
-            mEndSpacer.setLayoutParams(params);
         }
 
         @Override
@@ -193,11 +173,11 @@
 
         @Override
         public void addTile(TileRecord tile) {
-            addView(tile.tileView, getChildCount() - 1 /* Leave icon at end */,
-                    generateLayoutParams());
-            // Add a spacer.
-            addView(new Space(mContext), getChildCount() - 1 /* Leave icon at end */,
-                    generateSpaceParams());
+            if (getChildCount() != 0) {
+                // Add a spacer.
+                addView(new Space(mContext), getChildCount(), generateSpaceParams());
+            }
+            addView(tile.tileView, getChildCount(), generateLayoutParams());
             mRecords.add(tile);
             tile.tile.setListening(this, mListening);
         }
@@ -222,8 +202,10 @@
             int childIndex = getChildIndex(tile.tileView);
             // Remove the tile.
             removeViewAt(childIndex);
-            // Remove its spacer as well.
-            removeViewAt(childIndex);
+            if (getChildCount() != 0) {
+                // Remove its spacer as well.
+                removeViewAt(childIndex);
+            }
             mRecords.remove(tile);
             tile.tile.setListening(this, false);
         }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
index 8e4ed91..8d7f6ee 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/customize/TileAdapter.java
@@ -19,6 +19,7 @@
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.DialogInterface;
+import android.content.res.TypedArray;
 import android.graphics.Canvas;
 import android.graphics.drawable.ColorDrawable;
 import android.os.Handler;
@@ -71,6 +72,7 @@
     private final Handler mHandler = new Handler();
     private final List<TileInfo> mTiles = new ArrayList<>();
     private final ItemTouchHelper mItemTouchHelper;
+    private final ItemDecoration mDecoration;
     private final AccessibilityManager mAccessibilityManager;
     private int mEditIndex;
     private int mTileDividerIndex;
@@ -88,6 +90,7 @@
         mContext = context;
         mAccessibilityManager = context.getSystemService(AccessibilityManager.class);
         mItemTouchHelper = new ItemTouchHelper(mCallbacks);
+        mDecoration = new TileItemDecoration(context);
     }
 
     public void setHost(QSTileHost host) {
@@ -294,6 +297,9 @@
         mAccessibilityMoving = false;
         mTiles.remove(mEditIndex--);
         notifyItemRemoved(mEditIndex - 1);
+        // Don't remove items when the last position is selected.
+        if (position == mEditIndex) position--;
+
         move(mAccessibilityFromIndex, position, v);
         notifyDataSetChanged();
     }
@@ -459,9 +465,16 @@
         }
     };
 
-    private final ItemDecoration mDecoration = new ItemDecoration() {
-        // TODO: Move this to resource.
-        private final ColorDrawable mDrawable = new ColorDrawable(0xff384248);
+    private class TileItemDecoration extends ItemDecoration {
+        private final ColorDrawable mDrawable;
+
+        private TileItemDecoration(Context context) {
+            TypedArray ta =
+                    context.obtainStyledAttributes(new int[]{android.R.attr.colorSecondary});
+            mDrawable = new ColorDrawable(ta.getColor(0, 0));
+            ta.recycle();
+        }
+
 
         @Override
         public void onDraw(Canvas c, RecyclerView parent, State state) {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/customize/TileQueryHelper.java b/packages/SystemUI/src/com/android/systemui/qs/customize/TileQueryHelper.java
index 1431b22..40ef6eb 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/customize/TileQueryHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/customize/TileQueryHelper.java
@@ -57,8 +57,7 @@
     }
 
     private void addSystemTiles(final QSTileHost host) {
-        String possible = mContext.getString(R.string.quick_settings_tiles_default)
-                + ",hotspot,inversion,saver,work,cast,night";
+        String possible = mContext.getString(R.string.quick_settings_tiles_stock);
         String[] possibleTiles = possible.split(",");
         final Handler qsHandler = new Handler(host.getLooper());
         final Handler mainHandler = new Handler(Looper.getMainLooper());
@@ -145,9 +144,16 @@
             PackageManager pm = mContext.getPackageManager();
             List<ResolveInfo> services = pm.queryIntentServicesAsUser(
                     new Intent(TileService.ACTION_QS_TILE), 0, ActivityManager.getCurrentUser());
+            String stockTiles = mContext.getString(R.string.quick_settings_tiles_stock);
             for (ResolveInfo info : services) {
                 String packageName = info.serviceInfo.packageName;
                 ComponentName componentName = new ComponentName(packageName, info.serviceInfo.name);
+
+                // Don't include apps that are a part of the default tile set.
+                if (stockTiles.contains(componentName.flattenToString())) {
+                    continue;
+                }
+
                 final CharSequence appLabel = info.serviceInfo.applicationInfo.loadLabel(pm);
                 String spec = CustomTile.toSpec(componentName);
                 State state = getState(params[0], spec);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java b/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
index d3f5d26..b36221d 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/CustomTile.java
@@ -71,7 +71,7 @@
         super(host);
         mWindowManager = WindowManagerGlobal.getWindowManagerService();
         mComponent = ComponentName.unflattenFromString(action);
-        mTile = new Tile(mComponent);
+        mTile = new Tile();
         setTileIcon();
         mServiceManager = host.getTileServices().getTileWrapper(this);
         mService = mServiceManager.getTileService();
@@ -82,8 +82,11 @@
     private void setTileIcon() {
         try {
             PackageManager pm = mContext.getPackageManager();
-            ServiceInfo info = pm.getServiceInfo(mComponent,
-                    PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE);
+            int flags = PackageManager.MATCH_ENCRYPTION_AWARE_AND_UNAWARE;
+            if (isSystemApp(pm)) {
+                flags |= PackageManager.MATCH_DISABLED_COMPONENTS;
+            }
+            ServiceInfo info = pm.getServiceInfo(mComponent, flags);
             int icon = info.icon != 0 ? info.icon
                     : info.applicationInfo.icon;
             // Update the icon if its not set or is the default icon.
@@ -103,6 +106,10 @@
         }
     }
 
+    private boolean isSystemApp(PackageManager pm) throws PackageManager.NameNotFoundException {
+        return pm.getApplicationInfo(mComponent.getPackageName(), 0).isSystemApp();
+    }
+
     /**
      * Compare two icons, only works for resources.
      */
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/QSTileServiceWrapper.java b/packages/SystemUI/src/com/android/systemui/qs/external/QSTileServiceWrapper.java
index 407453c..451e1f6 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/QSTileServiceWrapper.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/QSTileServiceWrapper.java
@@ -94,4 +94,8 @@
             return false;
         }
     }
+
+    public IQSTileService getService() {
+        return mService;
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java b/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java
index 11bf0c5..17ce0cb 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/TileLifecycleManager.java
@@ -26,6 +26,7 @@
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.ServiceInfo;
 import android.net.Uri;
+import android.os.Binder;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.RemoteException;
@@ -37,6 +38,7 @@
 import android.support.annotation.VisibleForTesting;
 import android.util.ArraySet;
 import android.util.Log;
+
 import libcore.util.Objects;
 
 import java.util.Set;
@@ -70,6 +72,7 @@
     private final Handler mHandler;
     private final Intent mIntent;
     private final UserHandle mUser;
+    private final IBinder mToken = new Binder();
 
     private Set<Integer> mQueuedMessages = new ArraySet<>();
     private QSTileServiceWrapper mWrapper;
@@ -91,7 +94,7 @@
         mHandler = handler;
         mIntent = intent;
         mIntent.putExtra(TileService.EXTRA_SERVICE, service.asBinder());
-        mIntent.putExtra(TileService.EXTRA_COMPONENT, intent.getComponent());
+        mIntent.putExtra(TileService.EXTRA_TOKEN, mToken);
         mUser = user;
         if (DEBUG) Log.d(TAG, "Creating " + mIntent + " " + mUser);
     }
@@ -405,6 +408,10 @@
         handleDeath();
     }
 
+    public IBinder getToken() {
+        return mToken;
+    }
+
     public interface TileChangeListener {
         void onTileChanged(ComponentName tile);
     }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/TileServiceManager.java b/packages/SystemUI/src/com/android/systemui/qs/external/TileServiceManager.java
index da2c5245..91a0eb0 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/TileServiceManager.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/TileServiceManager.java
@@ -25,6 +25,7 @@
 import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.Handler;
+import android.os.IBinder;
 import android.os.UserHandle;
 import android.service.quicksettings.IQSTileService;
 import android.service.quicksettings.Tile;
@@ -32,6 +33,7 @@
 import android.support.annotation.VisibleForTesting;
 import android.util.Log;
 
+import com.android.systemui.qs.customize.TileQueryHelper.TileStateListener;
 import com.android.systemui.qs.external.TileLifecycleManager.TileChangeListener;
 
 import java.util.List;
@@ -113,6 +115,10 @@
         return mStateManager;
     }
 
+    public IBinder getToken() {
+        return mStateManager.getToken();
+    }
+
     public void setBindRequested(boolean bindRequested) {
         if (mBindRequested == bindRequested) return;
         mBindRequested = bindRequested;
diff --git a/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java b/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java
index 6f0bed2..6bc94b2 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/external/TileServices.java
@@ -25,10 +25,12 @@
 import android.graphics.drawable.Icon;
 import android.os.Binder;
 import android.os.Handler;
+import android.os.IBinder;
 import android.os.Looper;
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.service.quicksettings.IQSService;
+import android.service.quicksettings.IQSTileService;
 import android.service.quicksettings.Tile;
 import android.service.quicksettings.TileService;
 import android.util.ArrayMap;
@@ -52,6 +54,7 @@
 
     private final ArrayMap<CustomTile, TileServiceManager> mServices = new ArrayMap<>();
     private final ArrayMap<ComponentName, CustomTile> mTiles = new ArrayMap<>();
+    private final ArrayMap<IBinder, CustomTile> mTokenMap = new ArrayMap<>();
     private final Context mContext;
     private final Handler mHandler;
     private final Handler mMainHandler;
@@ -82,6 +85,7 @@
         synchronized (mServices) {
             mServices.put(tile, service);
             mTiles.put(component, tile);
+            mTokenMap.put(service.getToken(), tile);
         }
         return service;
     }
@@ -95,6 +99,7 @@
             service.setBindAllowed(false);
             service.handleDestroy();
             mServices.remove(tile);
+            mTokenMap.remove(service.getToken());
             mTiles.remove(tile.getComponent());
             final String slot = tile.getComponent().getClassName();
             mMainHandler.post(new Runnable() {
@@ -138,8 +143,9 @@
         }
     }
 
-    private void verifyCaller(String packageName) {
+    private void verifyCaller(CustomTile tile) {
         try {
+            String packageName = tile.getComponent().getPackageName();
             int uid = mContext.getPackageManager().getPackageUidAsUser(packageName,
                     Binder.getCallingUserHandle().getIdentifier());
             if (Binder.getCallingUid() != uid) {
@@ -170,11 +176,10 @@
     }
 
     @Override
-    public void updateQsTile(Tile tile) {
-        ComponentName componentName = tile.getComponentName();
-        verifyCaller(componentName.getPackageName());
-        CustomTile customTile = getTileForComponent(componentName);
+    public void updateQsTile(Tile tile, IBinder token) {
+        CustomTile customTile = getTileForToken(token);
         if (customTile != null) {
+            verifyCaller(customTile);
             synchronized (mServices) {
                 final TileServiceManager tileServiceManager = mServices.get(customTile);
                 tileServiceManager.clearPendingBind();
@@ -186,11 +191,10 @@
     }
 
     @Override
-    public void onStartSuccessful(Tile tile) {
-        ComponentName componentName = tile.getComponentName();
-        verifyCaller(componentName.getPackageName());
-        CustomTile customTile = getTileForComponent(componentName);
+    public void onStartSuccessful(IBinder token) {
+        CustomTile customTile = getTileForToken(token);
         if (customTile != null) {
+            verifyCaller(customTile);
             synchronized (mServices) {
                 final TileServiceManager tileServiceManager = mServices.get(customTile);
                 tileServiceManager.clearPendingBind();
@@ -200,11 +204,10 @@
     }
 
     @Override
-    public void onShowDialog(Tile tile) {
-        ComponentName componentName = tile.getComponentName();
-        verifyCaller(componentName.getPackageName());
-        CustomTile customTile = getTileForComponent(componentName);
+    public void onShowDialog(IBinder token) {
+        CustomTile customTile = getTileForToken(token);
         if (customTile != null) {
+            verifyCaller(customTile);
             customTile.onDialogShown();
             mHost.collapsePanels();
             mServices.get(customTile).setShowingDialog(true);
@@ -212,34 +215,32 @@
     }
 
     @Override
-    public void onDialogHidden(Tile tile) {
-        ComponentName componentName = tile.getComponentName();
-        verifyCaller(componentName.getPackageName());
-        CustomTile customTile = getTileForComponent(componentName);
+    public void onDialogHidden(IBinder token) {
+        CustomTile customTile = getTileForToken(token);
         if (customTile != null) {
+            verifyCaller(customTile);
             mServices.get(customTile).setShowingDialog(false);
             customTile.onDialogHidden();
         }
     }
 
     @Override
-    public void onStartActivity(Tile tile) {
-        ComponentName componentName = tile.getComponentName();
-        verifyCaller(componentName.getPackageName());
-        CustomTile customTile = getTileForComponent(componentName);
+    public void onStartActivity(IBinder token) {
+        CustomTile customTile = getTileForToken(token);
         if (customTile != null) {
+            verifyCaller(customTile);
             mHost.collapsePanels();
         }
     }
 
     @Override
-    public void updateStatusIcon(Tile tile, Icon icon, String contentDescription) {
-        final ComponentName componentName = tile.getComponentName();
-        String packageName = componentName.getPackageName();
-        verifyCaller(packageName);
-        CustomTile customTile = getTileForComponent(componentName);
+    public void updateStatusIcon(IBinder token, Icon icon, String contentDescription) {
+        CustomTile customTile = getTileForToken(token);
         if (customTile != null) {
+            verifyCaller(customTile);
             try {
+                ComponentName componentName = customTile.getComponent();
+                String packageName = componentName.getPackageName();
                 UserHandle userHandle = getCallingUserHandle();
                 PackageInfo info = mContext.getPackageManager().getPackageInfoAsUser(packageName, 0,
                         userHandle.getIdentifier());
@@ -263,21 +264,20 @@
     }
 
     @Override
-    public Tile getTile(ComponentName componentName) {
-        verifyCaller(componentName.getPackageName());
-        CustomTile customTile = getTileForComponent(componentName);
+    public Tile getTile(IBinder token) {
+        CustomTile customTile = getTileForToken(token);
         if (customTile != null) {
+            verifyCaller(customTile);
             return customTile.getQsTile();
         }
         return null;
     }
 
     @Override
-    public void startUnlockAndRun(Tile tile) {
-        ComponentName componentName = tile.getComponentName();
-        verifyCaller(componentName.getPackageName());
-        CustomTile customTile = getTileForComponent(componentName);
+    public void startUnlockAndRun(IBinder token) {
+        CustomTile customTile = getTileForToken(token);
         if (customTile != null) {
+            verifyCaller(customTile);
             customTile.startUnlockAndRun();
         }
     }
@@ -294,6 +294,12 @@
         return keyguardMonitor.isSecure() && keyguardMonitor.isShowing();
     }
 
+    private CustomTile getTileForToken(IBinder token) {
+        synchronized (mServices) {
+            return mTokenMap.get(token);
+        }
+    }
+
     private CustomTile getTileForComponent(ComponentName component) {
         synchronized (mServices) {
             return mTiles.get(component);
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java
index 7a23910..f1e8749 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/BluetoothTile.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.qs.tiles;
 
+import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothProfile;
 import android.content.Context;
@@ -90,11 +91,11 @@
             mHost.startActivityDismissingKeyguard(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS));
             return;
         }
+        showDetail(true);
         if (!mState.value) {
             mState.value = true;
             mController.setBluetoothEnabled(true);
         }
-        showDetail(true);
     }
 
     @Override
@@ -208,6 +209,12 @@
         }
 
         @Override
+        public boolean getToggleEnabled() {
+            return mController.getBluetoothState() == BluetoothAdapter.STATE_OFF
+                    || mController.getBluetoothState() == BluetoothAdapter.STATE_ON;
+        }
+
+        @Override
         public Intent getSettingsIntent() {
             return BLUETOOTH_SETTINGS;
         }
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/DataUsageDetailView.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/DataUsageDetailView.java
index bad4e79..c7b6aea 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/DataUsageDetailView.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/DataUsageDetailView.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.qs.tiles;
 
+import android.annotation.ColorInt;
 import android.content.Context;
 import android.content.res.Configuration;
 import android.content.res.Resources;
@@ -24,6 +25,7 @@
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
+import com.android.settingslib.Utils;
 import com.android.settingslib.net.DataUsageController;
 import com.android.systemui.FontSizeUtils;
 import com.android.systemui.R;
@@ -64,7 +66,7 @@
         final Resources res = mContext.getResources();
         final int titleId;
         final long bytes;
-        int usageColor = R.color.system_accent_color;
+        @ColorInt int usageColor = 0;
         final String top;
         String bottom = null;
         if (info.usageLevel < info.warningLevel || info.limitLevel <= 0) {
@@ -89,14 +91,18 @@
                     formatBytes(info.usageLevel));
             bottom = res.getString(R.string.quick_settings_cellular_detail_data_limit,
                     formatBytes(info.limitLevel));
-            usageColor = R.color.system_warning_color;
+            usageColor = mContext.getColor(R.color.system_warning_color);
+        }
+
+        if (usageColor == 0) {
+            usageColor = Utils.getColorAccent(mContext);
         }
 
         final TextView title = (TextView) findViewById(android.R.id.title);
         title.setText(titleId);
         final TextView usage = (TextView) findViewById(R.id.usage_text);
         usage.setText(formatBytes(bytes));
-        usage.setTextColor(mContext.getColor(usageColor));
+        usage.setTextColor(usageColor);
         final DataUsageGraph graph = (DataUsageGraph) findViewById(R.id.usage_graph);
         graph.setLevels(info.limitLevel, info.warningLevel, info.usageLevel);
         final TextView carrier = (TextView) findViewById(R.id.usage_carrier_text);
@@ -109,6 +115,12 @@
         final TextView infoBottom = (TextView) findViewById(R.id.usage_info_bottom_text);
         infoBottom.setVisibility(bottom != null ? View.VISIBLE : View.GONE);
         infoBottom.setText(bottom);
+        boolean showLevel = info.warningLevel > 0 || info.limitLevel > 0;
+        graph.setVisibility(showLevel ? View.VISIBLE : View.GONE);
+        if (!showLevel) {
+            infoTop.setVisibility(View.GONE);
+        }
+
     }
 
     private String formatBytes(long bytes) {
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java
index 04cb553..91821ba 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/DndTile.java
@@ -122,9 +122,9 @@
         if (mState.value) {
             mController.setZen(Global.ZEN_MODE_OFF, null, TAG);
         } else {
+            showDetail(true);
             int zen = Prefs.getInt(mContext, Prefs.Key.DND_FAVORITE_ZEN, Global.ZEN_MODE_ALARMS);
             mController.setZen(zen, null, TAG);
-            showDetail(true);
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/NightDisplayTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/NightDisplayTile.java
new file mode 100644
index 0000000..9415b27
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/NightDisplayTile.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2016, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.qs.tiles;
+
+import android.app.ActivityManager;
+import android.content.Intent;
+import android.provider.Settings;
+import android.widget.Switch;
+
+import com.android.internal.app.NightDisplayController;
+import com.android.internal.logging.MetricsLogger;
+import com.android.internal.logging.MetricsProto.MetricsEvent;
+import com.android.systemui.R;
+import com.android.systemui.qs.QSTile;
+
+public class NightDisplayTile extends QSTile<QSTile.BooleanState>
+        implements NightDisplayController.Callback {
+
+    private NightDisplayController mController;
+    private boolean mIsListening;
+
+    public NightDisplayTile(Host host) {
+        super(host);
+        mController = new NightDisplayController(mContext, ActivityManager.getCurrentUser());
+    }
+
+    @Override
+    public boolean isAvailable() {
+        return NightDisplayController.isAvailable(mContext);
+    }
+
+    @Override
+    public BooleanState newTileState() {
+        return new BooleanState();
+    }
+
+    @Override
+    protected void handleClick() {
+        final boolean activated = !mState.value;
+        MetricsLogger.action(mContext, getMetricsCategory(), activated);
+        mController.setActivated(activated);
+    }
+
+    @Override
+    protected void handleUserSwitch(int newUserId) {
+        // Stop listening to the old controller.
+        if (mIsListening) {
+            mController.setListener(null);
+        }
+
+        // Make a new controller for the new user.
+        mController = new NightDisplayController(mContext, newUserId);
+        if (mIsListening) {
+            mController.setListener(this);
+        }
+
+        super.handleUserSwitch(newUserId);
+    }
+
+    @Override
+    protected void handleUpdateState(BooleanState state, Object arg) {
+        final boolean isActivated = mController.isActivated();
+        state.value = isActivated;
+        state.label = mContext.getString(R.string.quick_settings_night_display_label);
+        state.icon = ResourceIcon.get(isActivated ? R.drawable.ic_qs_night_display_on
+                : R.drawable.ic_qs_night_display_off);
+        state.contentDescription = mContext.getString(isActivated
+                ? R.string.quick_settings_night_display_summary_on
+                : R.string.quick_settings_night_display_summary_off);
+        state.minimalAccessibilityClassName = state.expandedAccessibilityClassName
+                = Switch.class.getName();
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return MetricsEvent.QS_NIGHT_DISPLAY;
+    }
+
+    @Override
+    public Intent getLongClickIntent() {
+        return new Intent(Settings.ACTION_NIGHT_DISPLAY_SETTINGS);
+    }
+
+    @Override
+    protected void setListening(boolean listening) {
+        mIsListening = listening;
+        if (listening) {
+            mController.setListener(this);
+            refreshState();
+        } else {
+            mController.setListener(null);
+        }
+    }
+
+    @Override
+    public CharSequence getTileLabel() {
+        return mContext.getString(R.string.quick_settings_night_display_label);
+    }
+
+    @Override
+    public void onActivated(boolean activated) {
+        refreshState();
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java
index 661212c..9ce1f31 100644
--- a/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java
+++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/WifiTile.java
@@ -113,11 +113,11 @@
             mHost.startActivityDismissingKeyguard(new Intent(Settings.ACTION_WIFI_SETTINGS));
             return;
         }
+        showDetail(true);
         if (!mState.value) {
             mController.setWifiEnabled(true);
             mState.value = true;
         }
-        showDetail(true);
     }
 
     @Override
@@ -368,5 +368,5 @@
             }
             mItems.setItems(items);
         }
-    };
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/IRecentsNonSystemUserCallbacks.aidl b/packages/SystemUI/src/com/android/systemui/recents/IRecentsNonSystemUserCallbacks.aidl
index 9403664..9214eef 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/IRecentsNonSystemUserCallbacks.aidl
+++ b/packages/SystemUI/src/com/android/systemui/recents/IRecentsNonSystemUserCallbacks.aidl
@@ -35,4 +35,5 @@
             in Rect initialBounds);
     void onDraggingInRecents(float distanceFromTop);
     void onDraggingInRecentsEnded(float velocity);
+    void showCurrentUserToast(int msgResId, int msgLength);
 }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/Recents.java b/packages/SystemUI/src/com/android/systemui/recents/Recents.java
index e117bfe..5b25f052 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/Recents.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/Recents.java
@@ -53,6 +53,7 @@
 import com.android.systemui.recents.events.activity.RecentsActivityStartingEvent;
 import com.android.systemui.recents.events.component.RecentsVisibilityChangedEvent;
 import com.android.systemui.recents.events.component.ScreenPinningRequestEvent;
+import com.android.systemui.recents.events.component.ShowUserToastEvent;
 import com.android.systemui.recents.events.ui.RecentsDrawnEvent;
 import com.android.systemui.recents.misc.SystemServicesProxy;
 import com.android.systemui.recents.model.RecentsTaskLoader;
@@ -455,8 +456,8 @@
                 mDraggingInRecentsCurrentUser = currentUser;
                 return true;
             } else {
-                Toast.makeText(mContext, R.string.recents_incompatible_app_message,
-                        Toast.LENGTH_SHORT).show();
+                EventBus.getDefault().send(new ShowUserToastEvent(
+                        R.string.recents_incompatible_app_message, Toast.LENGTH_SHORT));
                 return false;
             }
         } else {
@@ -674,6 +675,27 @@
         mImpl.onConfigurationChanged();
     }
 
+    public final void onBusEvent(ShowUserToastEvent event) {
+        int currentUser = sSystemServicesProxy.getCurrentUser();
+        if (sSystemServicesProxy.isSystemUser(currentUser)) {
+            mImpl.onShowCurrentUserToast(event.msgResId, event.msgLength);
+        } else {
+            if (mSystemToUserCallbacks != null) {
+                IRecentsNonSystemUserCallbacks callbacks =
+                        mSystemToUserCallbacks.getNonSystemUserRecentsForUser(currentUser);
+                if (callbacks != null) {
+                    try {
+                        callbacks.showCurrentUserToast(event.msgResId, event.msgLength);
+                    } catch (RemoteException e) {
+                        Log.e(TAG, "Callback failed", e);
+                    }
+                } else {
+                    Log.e(TAG, "No SystemUI callbacks found for user: " + currentUser);
+                }
+            }
+        }
+    }
+
     /**
      * Attempts to register with the system user.
      */
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
index 0f356e0..70642ed 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java
@@ -17,6 +17,7 @@
 package com.android.systemui.recents;
 
 import android.app.Activity;
+import android.app.ActivityManager;
 import android.app.ActivityOptions;
 import android.app.TaskStackBuilder;
 import android.content.BroadcastReceiver;
@@ -87,6 +88,7 @@
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.util.List;
 
 /**
  * The main Recents activity that is started from RecentsComponent.
@@ -165,18 +167,39 @@
      */
     final BroadcastReceiver mSystemBroadcastReceiver = new BroadcastReceiver() {
         @Override
-        public void onReceive(Context context, Intent intent) {
+        public void onReceive(Context ctx, Intent intent) {
             String action = intent.getAction();
             if (action.equals(Intent.ACTION_SCREEN_OFF)) {
                 // When the screen turns off, dismiss Recents to Home
                 dismissRecentsToHomeIfVisible(false);
             } else if (action.equals(Intent.ACTION_TIME_CHANGED)) {
-                // For the time being, if the time changes, then invalidate the
-                // last-stack-active-time, this ensures that we will just show the last N tasks
-                // the next time that Recents loads, but prevents really old tasks from showing
-                // up if the task time is set forward.
-                Prefs.putLong(RecentsActivity.this, Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME,
-                        0);
+                // If the time shifts but the currentTime >= lastStackActiveTime, then that boundary
+                // is still valid.  Otherwise, we need to reset the lastStackactiveTime to the
+                // currentTime and remove the old tasks in between which would not be previously
+                // visible, but currently would be in the new currentTime
+                long oldLastStackActiveTime = Prefs.getLong(RecentsActivity.this,
+                        Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME, -1);
+                if (oldLastStackActiveTime != -1) {
+                    long currentTime = System.currentTimeMillis();
+                    if (currentTime < oldLastStackActiveTime) {
+                        // We are only removing tasks that are between the new current time
+                        // and the old last stack active time, they were not visible and in the
+                        // TaskStack so we don't need to remove any associated TaskViews but we do
+                        // need to load the task id's from the system
+                        RecentsTaskLoadPlan loadPlan = Recents.getTaskLoader().createLoadPlan(ctx);
+                        loadPlan.preloadRawTasks(false /* includeFrontMostExcludedTask */);
+                        List<ActivityManager.RecentTaskInfo> tasks = loadPlan.getRawTasks();
+                        for (int i = tasks.size() - 1; i >= 0; i--) {
+                            ActivityManager.RecentTaskInfo task = tasks.get(i);
+                            if (currentTime <= task.lastActiveTime && task.lastActiveTime <
+                                    oldLastStackActiveTime) {
+                                Recents.getSystemServices().removeTask(task.persistentId);
+                            }
+                        }
+                        Prefs.putLong(RecentsActivity.this,
+                                Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME, currentTime);
+                    }
+                }
             }
         }
     };
@@ -801,9 +824,13 @@
         Recents.getTaskLoader().dump(prefix, writer);
 
         String id = Integer.toHexString(System.identityHashCode(this));
+        long lastStackActiveTime = Prefs.getLong(this,
+                Prefs.Key.OVERVIEW_LAST_STACK_TASK_ACTIVE_TIME, -1);
 
         writer.print(prefix); writer.print(TAG);
         writer.print(" visible="); writer.print(mIsVisible ? "Y" : "N");
+        writer.print(" lastStackTaskActiveTime="); writer.print(lastStackActiveTime);
+        writer.print(" currentTime="); writer.print(System.currentTimeMillis());
         writer.print(" [0x"); writer.print(id); writer.print("]");
         writer.println();
 
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivityLaunchState.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivityLaunchState.java
index 7161053..914035b 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivityLaunchState.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivityLaunchState.java
@@ -29,6 +29,7 @@
 
     public boolean launchedWithAltTab;
     public boolean launchedFromApp;
+    public boolean launchedFromBlacklistedApp;
     public boolean launchedFromHome;
     public boolean launchedViaDragGesture;
     public boolean launchedViaDockGesture;
@@ -39,6 +40,7 @@
     public void reset() {
         launchedFromHome = false;
         launchedFromApp = false;
+        launchedFromBlacklistedApp = false;
         launchedToTaskId = -1;
         launchedWithAltTab = false;
         launchedViaDragGesture = false;
@@ -53,8 +55,14 @@
         RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
         if (launchedFromApp) {
             if (!launchState.launchedWithAltTab && debugFlags.isFastToggleRecentsEnabled()) {
-                // If fast toggling, focus the front most task so that the next tap will focus the
-                // N-1 task
+                // If fast toggling, focus the front most task so that the next tap will launch the
+                // task
+                return numTasks - 1;
+            }
+
+            if (launchState.launchedFromBlacklistedApp) {
+                // If we are launching from a blacklisted app, focus the front most task so that the
+                // next tap will launch the task
                 return numTasks - 1;
             }
 
@@ -67,7 +75,7 @@
                 return -1;
             }
 
-            // If coming from home, focus the first task
+            // If coming from home, focus the front most task
             return numTasks - 1;
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsImpl.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsImpl.java
index 7e1deec..42d1b61 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsImpl.java
@@ -40,6 +40,7 @@
 import android.view.ViewConfiguration;
 import android.view.WindowManager;
 
+import android.widget.Toast;
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.policy.DockedDividerUtils;
 import com.android.systemui.R;
@@ -200,9 +201,17 @@
     }
 
     public void onConfigurationChanged() {
+        Resources res = mContext.getResources();
         reloadResources();
         mDummyStackView.reloadOnConfigurationChange();
+        // Update the header bar direction directly as it is not attached to anything and does not
+        // layout except in updateHeaderBarLayout()
+        mHeaderBar.setLayoutDirection(res.getConfiguration().getLayoutDirection());
         mHeaderBar.onConfigurationChanged();
+        mHeaderBar.forceLayout();
+        mHeaderBar.measure(
+                MeasureSpec.makeMeasureSpec(mHeaderBar.getMeasuredWidth(), MeasureSpec.EXACTLY),
+                MeasureSpec.makeMeasureSpec(mHeaderBar.getMeasuredHeight(), MeasureSpec.EXACTLY));
     }
 
     /**
@@ -211,11 +220,7 @@
      * {@link Recents#onBusEvent(RecentsVisibilityChangedEvent)}.
      */
     public void onVisibilityChanged(Context context, boolean visible) {
-        SystemUIApplication app = (SystemUIApplication) context;
-        PhoneStatusBar statusBar = app.getComponent(PhoneStatusBar.class);
-        if (statusBar != null) {
-            statusBar.updateRecentsVisibility(visible);
-        }
+        Recents.getSystemServices().setRecentsVisibility(visible);
     }
 
     /**
@@ -392,6 +397,10 @@
         EventBus.getDefault().sendOntoMainThread(new DraggingInRecentsEndedEvent(velocity));
     }
 
+    public void onShowCurrentUserToast(int msgResId, int msgLength) {
+        Toast.makeText(mContext, msgResId, msgLength).show();
+    }
+
     /**
      * Transitions to the next recent task in the stack.
      */
@@ -609,7 +618,7 @@
         stackLayout.setSystemInsets(systemInsets);
         if (stack != null) {
             stackLayout.getTaskStackBounds(displayRect, windowRect, systemInsets.top,
-                    systemInsets.right, mTaskStackBounds);
+                    systemInsets.left, systemInsets.right, mTaskStackBounds);
             stackLayout.reset();
             stackLayout.initialize(displayRect, windowRect, mTaskStackBounds,
                     TaskStackLayoutAlgorithm.StackState.getStackStateForStack(stack));
@@ -621,6 +630,7 @@
                 synchronized (mHeaderBarLock) {
                     if (mHeaderBar.getMeasuredWidth() != taskViewWidth ||
                             mHeaderBar.getMeasuredHeight() != mTaskBarHeight) {
+                        mHeaderBar.forceLayout();
                         mHeaderBar.measure(
                                 MeasureSpec.makeMeasureSpec(taskViewWidth, MeasureSpec.EXACTLY),
                                 MeasureSpec.makeMeasureSpec(mTaskBarHeight, MeasureSpec.EXACTLY));
@@ -807,15 +817,19 @@
             boolean isHomeStackVisible, boolean animate, int growTarget) {
         RecentsTaskLoader loader = Recents.getTaskLoader();
         RecentsActivityLaunchState launchState = Recents.getConfiguration().getLaunchState();
+        SystemServicesProxy ssp = Recents.getSystemServices();
+        boolean isBlacklisted = (runningTask != null)
+                ? ssp.isBlackListedActivity(runningTask.baseActivity.getClassName())
+                : false;
 
-        int runningTaskId = !mLaunchedWhileDocking && (runningTask != null)
+        int runningTaskId = !mLaunchedWhileDocking && !isBlacklisted && (runningTask != null)
                 ? runningTask.id
                 : -1;
 
         // In the case where alt-tab is triggered, we never get a preloadRecents() call, so we
         // should always preload the tasks now. If we are dragging in recents, reload them as
         // the stacks might have changed.
-        if (mLaunchedWhileDocking || mTriggeredFromAltTab ||sInstanceLoadPlan == null) {
+        if (mLaunchedWhileDocking || mTriggeredFromAltTab || sInstanceLoadPlan == null) {
             // Create a new load plan if preloadRecents() was never triggered
             sInstanceLoadPlan = loader.createLoadPlan(mContext);
         }
@@ -825,11 +839,13 @@
 
         TaskStack stack = sInstanceLoadPlan.getTaskStack();
         boolean hasRecentTasks = stack.getTaskCount() > 0;
-        boolean useThumbnailTransition = (runningTask != null) && !isHomeStackVisible && hasRecentTasks;
+        boolean useThumbnailTransition = (runningTask != null) && !isHomeStackVisible &&
+                hasRecentTasks;
 
         // Update the launch state that we need in updateHeaderBarLayout()
         launchState.launchedFromHome = !useThumbnailTransition && !mLaunchedWhileDocking;
         launchState.launchedFromApp = useThumbnailTransition || mLaunchedWhileDocking;
+        launchState.launchedFromBlacklistedApp = launchState.launchedFromApp && isBlacklisted;
         launchState.launchedViaDockGesture = mLaunchedWhileDocking;
         launchState.launchedViaDragGesture = mDraggingInRecents;
         launchState.launchedToTaskId = runningTaskId;
@@ -857,7 +873,9 @@
         }
 
         ActivityOptions opts;
-        if (useThumbnailTransition) {
+        if (isBlacklisted) {
+            opts = getUnknownTransitionActivityOptions();
+        } else if (useThumbnailTransition) {
             // Try starting with a thumbnail transition
             opts = getThumbnailTransitionActivityOptions(runningTask, mDummyStackView,
                     windowOverrideRect);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsImplProxy.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsImplProxy.java
index 60bf760..ff9e89e9 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/RecentsImplProxy.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsImplProxy.java
@@ -38,6 +38,7 @@
     private static final int MSG_DOCK_TOP_TASK = 7;
     private static final int MSG_ON_DRAGGING_IN_RECENTS = 8;
     private static final int MSG_ON_DRAGGING_IN_RECENTS_ENDED = 9;
+    private static final int MSG_SHOW_USER_TOAST = 10;
 
     private RecentsImpl mImpl;
 
@@ -109,6 +110,11 @@
         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DRAGGING_IN_RECENTS_ENDED, velocity));
     }
 
+    @Override
+    public void showCurrentUserToast(int msgResId, int msgLength) {
+        mHandler.sendMessage(mHandler.obtainMessage(MSG_SHOW_USER_TOAST, msgResId, msgLength));
+    }
+
     private final Handler mHandler = new Handler() {
 
         @Override
@@ -147,6 +153,9 @@
                 case MSG_ON_DRAGGING_IN_RECENTS_ENDED:
                     mImpl.onDraggingInRecentsEnded((Float) msg.obj);
                     break;
+                case MSG_SHOW_USER_TOAST:
+                    mImpl.onShowCurrentUserToast(msg.arg1, msg.arg2);
+                    break;
                 default:
                     super.handleMessage(msg);
             }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/events/component/ShowUserToastEvent.java b/packages/SystemUI/src/com/android/systemui/recents/events/component/ShowUserToastEvent.java
new file mode 100644
index 0000000..e2b39c3
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/recents/events/component/ShowUserToastEvent.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.recents.events.component;
+
+import com.android.systemui.recents.events.EventBus;
+
+/**
+ * This is sent when we want to show a toast for the current user.
+ */
+public class ShowUserToastEvent extends EventBus.Event {
+
+    public final int msgResId;
+    public final int msgLength;
+
+    public ShowUserToastEvent(int msgResId, int msgLength) {
+        this.msgResId = msgResId;
+        this.msgLength = msgLength;
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java b/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java
index 37a4948..930ed79 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/misc/SystemServicesProxy.java
@@ -69,6 +69,7 @@
 import android.view.Display;
 import android.view.IAppTransitionAnimationSpecsFuture;
 import android.view.IDockedStackListener;
+import android.view.IWindowManager;
 import android.view.WindowManager;
 import android.view.WindowManager.KeyboardShortcutsReceiver;
 import android.view.WindowManagerGlobal;
@@ -120,6 +121,7 @@
     IPackageManager mIpm;
     AssistUtils mAssistUtils;
     WindowManager mWm;
+    IWindowManager mIwm;
     UserManager mUm;
     Display mDisplay;
     String mRecentsPackage;
@@ -207,6 +209,7 @@
         mIpm = AppGlobals.getPackageManager();
         mAssistUtils = new AssistUtils(context);
         mWm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
+        mIwm = WindowManagerGlobal.getWindowManagerService();
         mUm = UserManager.get(context);
         mDisplay = mWm.getDefaultDisplay();
         mRecentsPackage = context.getPackageName();
@@ -243,6 +246,9 @@
         if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
             Collections.addAll(sRecentsBlacklist,
                     res.getStringArray(R.array.recents_tv_blacklist_array));
+        } else {
+            Collections.addAll(sRecentsBlacklist,
+                    res.getStringArray(R.array.recents_blacklist_array));
         }
     }
 
@@ -261,6 +267,13 @@
     }
 
     /**
+     * @return whether the provided {@param className} is blacklisted
+     */
+    public boolean isBlackListedActivity(String className) {
+        return sRecentsBlacklist.contains(className);
+    }
+
+    /**
      * Returns a list of the recents tasks.
      *
      * @param includeFrontMostExcludedTask if set, will ensure that the front most excluded task
@@ -1081,6 +1094,28 @@
         }
     }
 
+    /**
+     * Updates the visibility of recents.
+     */
+    public void setRecentsVisibility(boolean visible) {
+        try {
+            mIwm.setRecentsVisibility(visible);
+        } catch (RemoteException e) {
+            Log.e(TAG, "Unable to reach window manager", e);
+        }
+    }
+
+    /**
+     * Updates the visibility of the picture-in-picture.
+     */
+    public void setTvPipVisibility(boolean visible) {
+        try {
+            mIwm.setTvPipVisibility(visible);
+        } catch (RemoteException e) {
+            Log.e(TAG, "Unable to reach window manager", e);
+        }
+    }
+
     private final class H extends Handler {
         private static final int ON_TASK_STACK_CHANGED = 1;
         private static final int ON_ACTIVITY_PINNED = 2;
diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoadPlan.java b/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoadPlan.java
index 1278b73..9b48e4d 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoadPlan.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoadPlan.java
@@ -248,6 +248,13 @@
         return mStack;
     }
 
+    /**
+     * Returns the raw list of recent tasks.
+     */
+    public List<ActivityManager.RecentTaskInfo> getRawTasks() {
+        return mRawTasks;
+    }
+
     /** Returns whether there are any tasks in any stacks. */
     public boolean hasTasks() {
         if (mStack != null) {
diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoader.java b/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoader.java
index ca59831..ba31e3e 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoader.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/model/RecentsTaskLoader.java
@@ -200,6 +200,10 @@
 
                             if (cachedThumbnailData.thumbnail == null) {
                                 cachedThumbnailData.thumbnail = mDefaultThumbnail;
+                            } else {
+                                // Kick off an early upload of the bitmap to GL so
+                                // that this won't jank the first frame it's drawn in.
+                                cachedThumbnailData.thumbnail.prepareToDraw();
                             }
 
                             // When svelte, we trim the memory to just the visible thumbnails when
diff --git a/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java b/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java
index 26200d0..745f5a5 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/model/TaskStack.java
@@ -275,10 +275,16 @@
                 new RectF(0, 0.5f, 1, 1));
 
         @Override
-        public boolean acceptsDrop(int x, int y, int width, int height, boolean isCurrentTarget) {
-            return isCurrentTarget
-                    ? areaContainsPoint(expandedTouchDockArea, width, height, x, y)
-                    : areaContainsPoint(touchArea, width, height, x, y);
+        public boolean acceptsDrop(int x, int y, int width, int height, Rect insets,
+                boolean isCurrentTarget) {
+            if (isCurrentTarget) {
+                getMappedRect(expandedTouchDockArea, width, height, mTmpRect);
+                return mTmpRect.contains(x, y);
+            } else {
+                getMappedRect(touchArea, width, height, mTmpRect);
+                updateBoundsWithSystemInsets(mTmpRect, insets);
+                return mTmpRect.contains(x, y);
+            }
         }
 
         // Represents the view state of this dock state
@@ -423,6 +429,7 @@
         private final RectF touchArea;
         private final RectF dockArea;
         private final RectF expandedTouchDockArea;
+        private static final Rect mTmpRect = new Rect();
 
         /**
          * @param createMode used to pass to ActivityManager to dock the task
@@ -452,23 +459,11 @@
         }
 
         /**
-         * Returns whether {@param x} and {@param y} are contained in the area scaled to the
-         * given {@param width} and {@param height}.
-         */
-        public boolean areaContainsPoint(RectF area, int width, int height, float x, float y) {
-            int left = (int) (area.left * width);
-            int top = (int) (area.top * height);
-            int right = (int) (area.right * width);
-            int bottom = (int) (area.bottom * height);
-            return x >= left && y >= top && x <= right && y <= bottom;
-        }
-
-        /**
          * Returns the docked task bounds with the given {@param width} and {@param height}.
          */
-        public Rect getPreDockedBounds(int width, int height) {
-            return new Rect((int) (dockArea.left * width), (int) (dockArea.top * height),
-                    (int) (dockArea.right * width), (int) (dockArea.bottom * height));
+        public Rect getPreDockedBounds(int width, int height, Rect insets) {
+            getMappedRect(dockArea, width, height, mTmpRect);
+            return updateBoundsWithSystemInsets(mTmpRect, insets);
         }
 
         /**
@@ -511,10 +506,34 @@
             int top = dockArea.bottom < 1f
                     ? 0
                     : insets.top;
-            layoutAlgorithm.getTaskStackBounds(displayRect, windowRectOut, top, insets.right,
+            // For now, ignore the left insets since we always dock on the left and show Recents
+            // on the right
+            layoutAlgorithm.getTaskStackBounds(displayRect, windowRectOut, top, 0, insets.right,
                     taskStackBounds);
             return taskStackBounds;
         }
+
+        /**
+         * Returns the expanded bounds in certain dock sides such that the bounds account for the
+         * system insets (namely the vertical nav bar).  This call modifies and returns the given
+         * {@param bounds}.
+         */
+        private Rect updateBoundsWithSystemInsets(Rect bounds, Rect insets) {
+            if (dockSide == DOCKED_LEFT) {
+                bounds.right += insets.left;
+            } else if (dockSide == DOCKED_RIGHT) {
+                bounds.left -= insets.right;
+            }
+            return bounds;
+        }
+
+        /**
+         * Returns the mapped rect to the given dimensions.
+         */
+        private void getMappedRect(RectF bounds, int width, int height, Rect out) {
+            out.set((int) (bounds.left * width), (int) (bounds.top * height),
+                    (int) (bounds.right * width), (int) (bounds.bottom * height));
+        }
     }
 
     // A comparator that sorts tasks by their freeform state
diff --git a/packages/SystemUI/src/com/android/systemui/recents/tv/RecentsTvImpl.java b/packages/SystemUI/src/com/android/systemui/recents/tv/RecentsTvImpl.java
index fca8d2d..ef9de53 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/tv/RecentsTvImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/tv/RecentsTvImpl.java
@@ -140,10 +140,6 @@
 
     @Override
     public void onVisibilityChanged(Context context, boolean visible) {
-        SystemUIApplication app = (SystemUIApplication) context;
-        TvStatusBar statusBar = app.getComponent(TvStatusBar.class);
-        if (statusBar != null) {
-            statusBar.updateRecentsVisibility(visible);
-        }
+        Recents.getSystemServices().setRecentsVisibility(visible);
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/tv/animations/HomeRecentsEnterExitAnimationHolder.java b/packages/SystemUI/src/com/android/systemui/recents/tv/animations/HomeRecentsEnterExitAnimationHolder.java
index 9faaa4b..a673c8c 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/tv/animations/HomeRecentsEnterExitAnimationHolder.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/tv/animations/HomeRecentsEnterExitAnimationHolder.java
@@ -47,12 +47,13 @@
     public void startEnterAnimation(boolean isPipShown) {
         for(int i = 0; i < mGridView.getChildCount(); i++) {
             TaskCardView view = (TaskCardView) mGridView.getChildAt(i);
+            long delay = Math.max(mDelay * i, 0);
             view.setTranslationX(-mTranslationX);
             view.animate()
                     .alpha(isPipShown ? mDimAlpha : 1.0f)
                     .translationX(0)
                     .setDuration(mDuration)
-                    .setStartDelay(mDelay * i)
+                    .setStartDelay(delay)
                     .setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
         }
     }
@@ -60,11 +61,12 @@
     public void startExitAnimation(DismissRecentsToHomeAnimationStarted dismissEvent) {
         for(int i = mGridView.getChildCount() - 1; i >= 0; i--) {
             TaskCardView view = (TaskCardView) mGridView.getChildAt(i);
+            long delay = Math.max(mDelay * (mGridView.getChildCount() - 1 - i), 0);
             view.animate()
                     .alpha(0.0f)
                     .translationXBy(-mTranslationX)
                     .setDuration(mDuration)
-                    .setStartDelay(mDelay * (mGridView.getChildCount() - 1 - i))
+                    .setStartDelay(delay)
                     .setInterpolator(Interpolators.FAST_OUT_SLOW_IN);
             if(i == 0) {
                 view.animate().setListener(dismissEvent.getAnimationTrigger()
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/DropTarget.java b/packages/SystemUI/src/com/android/systemui/recents/views/DropTarget.java
index 3ad368c..f2a6310 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/DropTarget.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/DropTarget.java
@@ -16,6 +16,8 @@
 
 package com.android.systemui.recents.views;
 
+import android.graphics.Rect;
+
 /**
  * Represents a drop target for a drag gesture.
  */
@@ -25,5 +27,5 @@
      * Returns whether this target can accept this drop.  The x,y are relative to the top level
      * RecentsView, and the width/height are of the RecentsView.
      */
-    boolean acceptsDrop(int x, int y, int width, int height, boolean isCurrentTarget);
+    boolean acceptsDrop(int x, int y, int width, int height, Rect insets, boolean isCurrentTarget);
 }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
index a893910..24ef433 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsView.java
@@ -104,7 +104,7 @@
     private boolean mLastTaskLaunchedWasFreeform;
 
     @ViewDebug.ExportedProperty(category="recents")
-    private Rect mSystemInsets = new Rect();
+    Rect mSystemInsets = new Rect();
     private int mDividerSize;
 
     private Drawable mBackgroundScrim = new ColorDrawable(
@@ -140,8 +140,6 @@
 
         LayoutInflater inflater = LayoutInflater.from(context);
         if (RecentsDebugFlags.Static.EnableStackActionButton) {
-            float cornerRadius = context.getResources().getDimensionPixelSize(
-                    R.dimen.recents_task_view_rounded_corners_radius);
             mStackActionButton = (TextView) inflater.inflate(R.layout.recents_stack_action_button,
                     this, false);
             mStackActionButton.setOnClickListener(new View.OnClickListener() {
@@ -151,13 +149,6 @@
                 }
             });
             addView(mStackActionButton);
-            mStackActionButton.setClipToOutline(true);
-            mStackActionButton.setOutlineProvider(new ViewOutlineProvider() {
-                @Override
-                public void getOutline(View view, Outline outline) {
-                    outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), cornerRadius);
-                }
-            });
         }
         mEmptyView = (TextView) inflater.inflate(R.layout.recents_empty, this, false);
         addView(mEmptyView);
@@ -748,9 +739,10 @@
                         ? overrideHintAlpha
                         : viewState.hintTextAlpha;
                 Rect bounds = isDefaultDockState
-                        ? dockState.getPreDockedBounds(getMeasuredWidth(), getMeasuredHeight())
+                        ? dockState.getPreDockedBounds(getMeasuredWidth(), getMeasuredHeight(),
+                                mSystemInsets)
                         : dockState.getDockedBounds(getMeasuredWidth(), getMeasuredHeight(),
-                        mDividerSize, mSystemInsets, getResources());
+                                mDividerSize, mSystemInsets, getResources());
                 if (viewState.dockAreaOverlay.getCallback() != this) {
                     viewState.dockAreaOverlay.setCallback(this);
                     viewState.dockAreaOverlay.setBounds(bounds);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsViewTouchHandler.java b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsViewTouchHandler.java
index c692a16..636d4d6 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/RecentsViewTouchHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/RecentsViewTouchHandler.java
@@ -46,7 +46,7 @@
  */
 class DockRegion {
     public static TaskStack.DockState[] PHONE_LANDSCAPE = {
-            // We only allow docking to the left for now on small devices
+            // We only allow docking to the left in landscape for now on small devices
             TaskStack.DockState.LEFT
     };
     public static TaskStack.DockState[] PHONE_PORTRAIT = {
@@ -113,10 +113,11 @@
         boolean isLandscape = mRv.getResources().getConfiguration().orientation ==
                 Configuration.ORIENTATION_LANDSCAPE;
         RecentsConfiguration config = Recents.getConfiguration();
-        TaskStack.DockState[] dockStates = isLandscape ?
-                (config.isLargeScreen ? DockRegion.TABLET_LANDSCAPE : DockRegion.PHONE_LANDSCAPE) :
-                (config.isLargeScreen ? DockRegion.TABLET_PORTRAIT : DockRegion.PHONE_PORTRAIT);
-        return dockStates;
+        if (config.isLargeScreen) {
+            return isLandscape ? DockRegion.TABLET_LANDSCAPE : DockRegion.TABLET_PORTRAIT;
+        } else {
+            return isLandscape ? DockRegion.PHONE_LANDSCAPE : DockRegion.PHONE_PORTRAIT;
+        }
     }
 
     /**
@@ -226,7 +227,7 @@
                         // Give priority to the current drop target to retain the touch handling
                         if (mLastDropTarget != null) {
                             if (mLastDropTarget.acceptsDrop((int) evX, (int) evY, width, height,
-                                    true /* isCurrentTarget */)) {
+                                    mRv.mSystemInsets, true /* isCurrentTarget */)) {
                                 currentDropTarget = mLastDropTarget;
                             }
                         }
@@ -235,7 +236,7 @@
                         if (currentDropTarget == null) {
                             for (DropTarget target : mDropTargets) {
                                 if (target.acceptsDrop((int) evX, (int) evY, width, height,
-                                        false /* isCurrentTarget */)) {
+                                        mRv.mSystemInsets, false /* isCurrentTarget */)) {
                                     currentDropTarget = target;
                                     break;
                                 }
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackLayoutAlgorithm.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackLayoutAlgorithm.java
index e3fe1ab..fce7f9d 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackLayoutAlgorithm.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackLayoutAlgorithm.java
@@ -556,7 +556,9 @@
                     Math.max(0, mUnfocusedRange.getAbsoluteX(maxBottomNormX)));
             boolean scrollToFront = launchState.launchedFromHome ||
                     launchState.launchedViaDockGesture;
-            if (launchState.launchedWithAltTab) {
+            if (launchState.launchedFromBlacklistedApp) {
+                mInitialScrollP = mMaxScrollP;
+            } else if (launchState.launchedWithAltTab) {
                 mInitialScrollP = Utilities.clamp(launchTaskIndex, mMinScrollP, mMaxScrollP);
             } else if (scrollToFront) {
                 mInitialScrollP = Utilities.clamp(launchTaskIndex, mMinScrollP, mMaxScrollP);
@@ -579,6 +581,7 @@
         mTaskIndexOverrideMap.clear();
 
         boolean scrollToFront = launchState.launchedFromHome ||
+                launchState.launchedFromBlacklistedApp ||
                 launchState.launchedViaDockGesture;
         if (getInitialFocusState() == STATE_UNFOCUSED && mNumStackTasks > 1) {
             if (ignoreScrollToFront || (!launchState.launchedWithAltTab && !scrollToFront)) {
@@ -1058,9 +1061,9 @@
      * top and right system insets (but not the bottom inset) and left/right paddings, but _not_
      * the top/bottom padding or insets.
      */
-    public void getTaskStackBounds(Rect displayRect, Rect windowRect, int topInset, int rightInset,
-            Rect taskStackBounds) {
-        taskStackBounds.set(windowRect.left, windowRect.top + topInset,
+    public void getTaskStackBounds(Rect displayRect, Rect windowRect, int topInset, int leftInset,
+            int rightInset, Rect taskStackBounds) {
+        taskStackBounds.set(windowRect.left + leftInset, windowRect.top + topInset,
                 windowRect.right - rightInset, windowRect.bottom);
 
         // Ensure that the new width is at most the smaller display edge size
@@ -1255,7 +1258,7 @@
         String innerPrefix = prefix + "  ";
 
         writer.print(prefix); writer.print(TAG);
-        writer.write(" numStackTasks="); writer.write(mNumStackTasks);
+        writer.write(" numStackTasks="); writer.print(mNumStackTasks);
         writer.println();
 
         writer.print(innerPrefix);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
index 586a8bc..2715971 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackView.java
@@ -164,6 +164,8 @@
     @ViewDebug.ExportedProperty(category="recents")
     private boolean mAwaitingFirstLayout = true;
     @ViewDebug.ExportedProperty(category="recents")
+    private boolean mLaunchNextAfterFirstMeasure = false;
+    @ViewDebug.ExportedProperty(category="recents")
     @InitialStateAction
     private int mInitialState = INITIAL_STATE_UPDATE_ALL;
     @ViewDebug.ExportedProperty(category="recents")
@@ -218,7 +220,8 @@
     // The drop targets for a task drag
     private DropTarget mFreeformWorkspaceDropTarget = new DropTarget() {
         @Override
-        public boolean acceptsDrop(int x, int y, int width, int height, boolean isCurrentTarget) {
+        public boolean acceptsDrop(int x, int y, int width, int height, Rect insets,
+                boolean isCurrentTarget) {
             // This drop target has a fixed bounds and should be checked last, so just fall through
             // if it is the current target
             if (!isCurrentTarget) {
@@ -230,7 +233,8 @@
 
     private DropTarget mStackDropTarget = new DropTarget() {
         @Override
-        public boolean acceptsDrop(int x, int y, int width, int height, boolean isCurrentTarget) {
+        public boolean acceptsDrop(int x, int y, int width, int height, Rect insets,
+                boolean isCurrentTarget) {
             // This drop target has a fixed bounds and should be checked last, so just fall through
             // if it is the current target
             if (!isCurrentTarget) {
@@ -334,6 +338,7 @@
         // Since we always animate to the same place in (the initial state), always reset the stack
         // to the initial state when resuming
         mAwaitingFirstLayout = true;
+        mLaunchNextAfterFirstMeasure = false;
         mInitialState = INITIAL_STATE_UPDATE_ALL;
         requestLayout();
     }
@@ -1191,7 +1196,8 @@
         // bounds have changed.  This is because we may get spurious measures while dragging where
         // our current stack bounds reflect the target drop region.
         mLayoutAlgorithm.getTaskStackBounds(mDisplayRect, new Rect(0, 0, width, height),
-                mLayoutAlgorithm.mSystemInsets.top, mLayoutAlgorithm.mSystemInsets.right, mTmpRect);
+                mLayoutAlgorithm.mSystemInsets.top, mLayoutAlgorithm.mSystemInsets.left,
+                mLayoutAlgorithm.mSystemInsets.right, mTmpRect);
         if (!mTmpRect.equals(mStableStackBounds)) {
             mStableStackBounds.set(mTmpRect);
             mStackBounds.set(mTmpRect);
@@ -1220,6 +1226,12 @@
                 mInitialState = INITIAL_STATE_UPDATE_NONE;
             }
         }
+        // If we got the launch-next event before the first layout pass, then re-send it after the
+        // initial state has been updated
+        if (mLaunchNextAfterFirstMeasure) {
+            mLaunchNextAfterFirstMeasure = false;
+            EventBus.getDefault().post(new LaunchNextTaskRequestEvent());
+        }
 
         // Rebind all the views, including the ignore ones
         bindVisibleTaskViews(mStackScroller.getStackScroll(), false /* ignoreTaskOverrides */);
@@ -1662,6 +1674,11 @@
     }
 
     public final void onBusEvent(LaunchNextTaskRequestEvent event) {
+        if (mAwaitingFirstLayout) {
+            mLaunchNextAfterFirstMeasure = true;
+            return;
+        }
+
         int launchTaskIndex = mStack.indexOfStackTask(mStack.getLaunchTarget());
         if (launchTaskIndex != -1) {
             launchTaskIndex = Math.max(0, launchTaskIndex - 1);
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackViewTouchHandler.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackViewTouchHandler.java
index 67a2595..d44aa84 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackViewTouchHandler.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskStackViewTouchHandler.java
@@ -634,6 +634,12 @@
             }
 
             int taskIndex = mCurrentTasks.indexOf(task);
+            if (taskIndex == -1) {
+                // If a task was added to the stack view after the start of the dismiss gesture,
+                // just ignore it
+                continue;
+            }
+
             TaskViewTransform fromTransform = mCurrentTaskTransforms.get(taskIndex);
             TaskViewTransform toTransform = mFinalTaskTransforms.get(taskIndex);
 
diff --git a/packages/SystemUI/src/com/android/systemui/recents/views/TaskViewThumbnail.java b/packages/SystemUI/src/com/android/systemui/recents/views/TaskViewThumbnail.java
index 3193759..c46adf1 100644
--- a/packages/SystemUI/src/com/android/systemui/recents/views/TaskViewThumbnail.java
+++ b/packages/SystemUI/src/com/android/systemui/recents/views/TaskViewThumbnail.java
@@ -164,6 +164,7 @@
     /** Sets the thumbnail to a given bitmap. */
     void setThumbnail(Bitmap bm, ActivityManager.TaskThumbnailInfo thumbnailInfo) {
         if (bm != null) {
+            bm.prepareToDraw();
             mBitmapShader = new BitmapShader(bm, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
             mDrawPaint.setShader(mBitmapShader);
             mThumbnailRect.set(0, 0, bm.getWidth(), bm.getHeight());
diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/TakeScreenshotService.java b/packages/SystemUI/src/com/android/systemui/screenshot/TakeScreenshotService.java
index 4badc42..f3bae20 100644
--- a/packages/SystemUI/src/com/android/systemui/screenshot/TakeScreenshotService.java
+++ b/packages/SystemUI/src/com/android/systemui/screenshot/TakeScreenshotService.java
@@ -23,6 +23,8 @@
 import android.os.Message;
 import android.os.Messenger;
 import android.os.RemoteException;
+import android.os.UserManager;
+import android.util.Log;
 import android.view.WindowManager;
 
 public class TakeScreenshotService extends Service {
@@ -44,6 +46,16 @@
                     }
                 }
             };
+
+            // If the storage for this user is locked, we have no place to store
+            // the screenshot, so skip taking it instead of showing a misleading
+            // animation and error notification.
+            if (!getSystemService(UserManager.class).isUserUnlocked()) {
+                Log.w(TAG, "Skipping screenshot because storage is locked!");
+                post(finisher);
+                return;
+            }
+
             if (mScreenshot == null) {
                 mScreenshot = new GlobalScreenshot(TakeScreenshotService.this);
             }
diff --git a/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
index d5131be..1c6fffb 100644
--- a/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
+++ b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
@@ -23,6 +23,8 @@
 import android.os.AsyncTask;
 import android.os.Handler;
 import android.os.IPowerManager;
+import android.os.Looper;
+import android.os.Message;
 import android.os.PowerManager;
 import android.os.RemoteException;
 import android.os.ServiceManager;
@@ -45,6 +47,12 @@
      */
     private static final float BRIGHTNESS_ADJ_RESOLUTION = 2048;
 
+    private static final int MSG_UPDATE_ICON = 0;
+    private static final int MSG_UPDATE_SLIDER = 1;
+    private static final int MSG_SET_CHECKED = 2;
+    private static final int MSG_ATTACH_LISTENER = 3;
+    private static final int MSG_DETACH_LISTENER = 4;
+
     private final int mMinimumBacklight;
     private final int mMaximumBacklight;
 
@@ -54,13 +62,14 @@
     private final boolean mAutomaticAvailable;
     private final IPowerManager mPower;
     private final CurrentUserTracker mUserTracker;
-    private final Handler mHandler;
+
+    private Handler mBackgroundHandler;
     private final BrightnessObserver mBrightnessObserver;
 
     private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks =
             new ArrayList<BrightnessStateChangeCallback>();
 
-    private boolean mAutomatic;
+    private volatile boolean mAutomatic;
     private boolean mListening;
     private boolean mExternalChange;
 
@@ -90,24 +99,20 @@
         @Override
         public void onChange(boolean selfChange, Uri uri) {
             if (selfChange) return;
-            try {
-                mExternalChange = true;
-                if (BRIGHTNESS_MODE_URI.equals(uri)) {
-                    updateMode();
-                    updateSlider();
-                } else if (BRIGHTNESS_URI.equals(uri) && !mAutomatic) {
-                    updateSlider();
-                } else if (BRIGHTNESS_ADJ_URI.equals(uri) && mAutomatic) {
-                    updateSlider();
-                } else {
-                    updateMode();
-                    updateSlider();
-                }
-                for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
-                    cb.onBrightnessLevelChanged();
-                }
-            } finally {
-                mExternalChange = false;
+
+            if (BRIGHTNESS_MODE_URI.equals(uri)) {
+                mBackgroundHandler.post(mUpdateModeRunnable);
+                mBackgroundHandler.post(mUpdateSliderRunnable);
+            } else if (BRIGHTNESS_URI.equals(uri) && !mAutomatic) {
+                mBackgroundHandler.post(mUpdateSliderRunnable);
+            } else if (BRIGHTNESS_ADJ_URI.equals(uri) && mAutomatic) {
+                mBackgroundHandler.post(mUpdateSliderRunnable);
+            } else {
+                mBackgroundHandler.post(mUpdateModeRunnable);
+                mBackgroundHandler.post(mUpdateSliderRunnable);
+            }
+            for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
+                cb.onBrightnessLevelChanged();
             }
         }
 
@@ -132,16 +137,117 @@
 
     }
 
+    private final Runnable mStartListeningRunnable = new Runnable() {
+        @Override
+        public void run() {
+            mBrightnessObserver.startObserving();
+            mUserTracker.startTracking();
+
+            // Update the slider and mode before attaching the listener so we don't
+            // receive the onChanged notifications for the initial values.
+            mUpdateModeRunnable.run();
+            mUpdateSliderRunnable.run();
+
+            mHandler.sendEmptyMessage(MSG_ATTACH_LISTENER);
+        }
+    };
+
+    private final Runnable mStopListeningRunnable = new Runnable() {
+        @Override
+        public void run() {
+            mBrightnessObserver.stopObserving();
+            mUserTracker.stopTracking();
+
+            mHandler.sendEmptyMessage(MSG_DETACH_LISTENER);
+        }
+    };
+
+    /**
+     * Fetch the brightness mode from the system settings and update the icon. Should be called from
+     * background thread.
+     */
+    private final Runnable mUpdateModeRunnable = new Runnable() {
+        @Override
+        public void run() {
+            if (mAutomaticAvailable) {
+                int automatic;
+                automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
+                        Settings.System.SCREEN_BRIGHTNESS_MODE,
+                        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
+                        UserHandle.USER_CURRENT);
+                mAutomatic = automatic != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
+                mHandler.obtainMessage(MSG_UPDATE_ICON, mAutomatic ? 1 : 0).sendToTarget();
+            } else {
+                mHandler.obtainMessage(MSG_SET_CHECKED, 0).sendToTarget();
+                mHandler.obtainMessage(MSG_UPDATE_ICON, 0 /* automatic */).sendToTarget();
+            }
+        }
+    };
+
+    /**
+     * Fetch the brightness from the system settings and update the slider. Should be called from
+     * background thread.
+     */
+    private final Runnable mUpdateSliderRunnable = new Runnable() {
+        @Override
+        public void run() {
+            if (mAutomatic) {
+                float value = Settings.System.getFloatForUser(mContext.getContentResolver(),
+                        Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0,
+                        UserHandle.USER_CURRENT);
+                mHandler.obtainMessage(MSG_UPDATE_SLIDER, (int) BRIGHTNESS_ADJ_RESOLUTION,
+                        (int) ((value + 1) * BRIGHTNESS_ADJ_RESOLUTION / 2f)).sendToTarget();
+            } else {
+                int value;
+                value = Settings.System.getIntForUser(mContext.getContentResolver(),
+                        Settings.System.SCREEN_BRIGHTNESS, mMaximumBacklight,
+                        UserHandle.USER_CURRENT);
+                mHandler.obtainMessage(MSG_UPDATE_SLIDER, mMaximumBacklight - mMinimumBacklight,
+                        value - mMinimumBacklight).sendToTarget();
+            }
+        }
+    };
+
+    private final Handler mHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            mExternalChange = true;
+            try {
+                switch (msg.what) {
+                    case MSG_UPDATE_ICON:
+                        updateIcon(msg.arg1 != 0);
+                        break;
+                    case MSG_UPDATE_SLIDER:
+                        mControl.setMax(msg.arg1);
+                        mControl.setValue(msg.arg2);
+                        break;
+                    case MSG_SET_CHECKED:
+                        mControl.setChecked(msg.arg1 != 0);
+                        break;
+                    case MSG_ATTACH_LISTENER:
+                        mControl.setOnChangedListener(BrightnessController.this);
+                        break;
+                    case MSG_DETACH_LISTENER:
+                        mControl.setOnChangedListener(null);
+                    default:
+                        super.handleMessage(msg);
+                }
+            } finally {
+                mExternalChange = false;
+            }
+        }
+    };
+
     public BrightnessController(Context context, ImageView icon, ToggleSlider control) {
         mContext = context;
         mIcon = icon;
         mControl = control;
-        mHandler = new Handler();
+        mBackgroundHandler = new Handler(Looper.getMainLooper());
         mUserTracker = new CurrentUserTracker(mContext) {
             @Override
             public void onUserSwitched(int newUserId) {
-                updateMode();
-                updateSlider();
+                mBackgroundHandler.post(mUpdateModeRunnable);
+                mBackgroundHandler.post(mUpdateSliderRunnable);
             }
         };
         mBrightnessObserver = new BrightnessObserver(mHandler);
@@ -155,6 +261,10 @@
         mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
     }
 
+    public void setBackgroundLooper(Looper backgroundLooper) {
+        mBackgroundHandler = new Handler(backgroundLooper);
+    }
+
     public void addStateChangedCallback(BrightnessStateChangeCallback cb) {
         mChangeCallbacks.add(cb);
     }
@@ -173,15 +283,7 @@
             return;
         }
 
-        mBrightnessObserver.startObserving();
-        mUserTracker.startTracking();
-
-        // Update the slider and mode before attaching the listener so we don't
-        // receive the onChanged notifications for the initial values.
-        updateMode();
-        updateSlider();
-
-        mControl.setOnChangedListener(this);
+        mBackgroundHandler.post(mStartListeningRunnable);
         mListening = true;
     }
 
@@ -191,9 +293,7 @@
             return;
         }
 
-        mBrightnessObserver.stopObserving();
-        mUserTracker.stopTracking();
-        mControl.setOnChangedListener(null);
+        mBackgroundHandler.post(mStopListeningRunnable);
         mListening = false;
     }
 
@@ -267,39 +367,4 @@
                     com.android.systemui.R.drawable.ic_qs_brightness_auto_off);
         }
     }
-
-    /** Fetch the brightness mode from the system settings and update the icon */
-    private void updateMode() {
-        if (mAutomaticAvailable) {
-            int automatic;
-            automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
-                    Settings.System.SCREEN_BRIGHTNESS_MODE,
-                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
-                    UserHandle.USER_CURRENT);
-            mAutomatic = automatic != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
-            updateIcon(mAutomatic);
-        } else {
-            mControl.setChecked(false);
-            updateIcon(false /*automatic*/);
-        }
-    }
-
-    /** Fetch the brightness from the system settings and update the slider */
-    private void updateSlider() {
-        if (mAutomatic) {
-            float value = Settings.System.getFloatForUser(mContext.getContentResolver(),
-                    Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0,
-                    UserHandle.USER_CURRENT);
-            mControl.setMax((int) BRIGHTNESS_ADJ_RESOLUTION);
-            mControl.setValue((int) ((value + 1) * BRIGHTNESS_ADJ_RESOLUTION / 2f));
-        } else {
-            int value;
-            value = Settings.System.getIntForUser(mContext.getContentResolver(),
-                    Settings.System.SCREEN_BRIGHTNESS, mMaximumBacklight,
-                    UserHandle.USER_CURRENT);
-            mControl.setMax(mMaximumBacklight - mMinimumBacklight);
-            mControl.setValue(value - mMinimumBacklight);
-        }
-    }
-
 }
diff --git a/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java b/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java
index 998f50f..cb77d7b 100644
--- a/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java
+++ b/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java
@@ -31,6 +31,7 @@
 import android.hardware.display.DisplayManager;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.Message;
 import android.util.AttributeSet;
 import android.view.Display;
 import android.view.DisplayInfo;
@@ -106,6 +107,11 @@
     private static final Interpolator IME_ADJUST_INTERPOLATOR =
             new PathInterpolator(0.2f, 0f, 0.1f, 1f);
 
+    private static final long ONE_MS_IN_NS = 1000000;
+    private static final long ONE_S_IN_NS = ONE_MS_IN_NS * 1000;
+
+    private static final int MSG_RESIZE_STACK = 0;
+
     private DividerHandleView mHandle;
     private View mBackground;
     private MinimizedDockShadow mMinimizedShadow;
@@ -150,7 +156,25 @@
     private boolean mDockedStackMinimized;
     private boolean mAdjustedForIme;
     private DividerState mState;
-    private final Handler mHandler = new Handler();
+
+    /**
+     * The offset between vsync-app and vsync-surfaceflinger. See
+     * {@link #calculateAppSurfaceFlingerVsyncOffsetMs} why this is necessary.
+     */
+    private long mSurfaceFlingerOffsetMs;
+
+    private final Handler mHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_RESIZE_STACK:
+                    resizeStack(msg.arg1, msg.arg2, (SnapTarget) msg.obj);
+                    break;
+                default:
+                    super.handleMessage(msg);
+            }
+        }
+    };
 
     private final AccessibilityDelegate mHandleDelegate = new AccessibilityDelegate() {
         @Override
@@ -290,6 +314,7 @@
     protected void onAttachedToWindow() {
         super.onAttachedToWindow();
         EventBus.getDefault().register(this);
+        mSurfaceFlingerOffsetMs = calculateAppSurfaceFlingerVsyncOffsetMs();
     }
 
     @Override
@@ -298,6 +323,25 @@
         EventBus.getDefault().unregister(this);
     }
 
+    /**
+     * This method calculates the offset between vsync-surfaceflinger and vsync-app. If vsync-app
+     * is a couple of milliseconds before vsync-sf, a touch or animation event that causes the
+     * stacks to be resized are sometimes processed before the vsync-sf tick, and sometimes after,
+     * which leads to jank. Figure out this difference here and then post all the touch/animation
+     * events to start being processed at vsync-sf.
+     *
+     * @return The offset between vsync-app and vsync-sf, or 0 if vsync app happens after vsync-sf.
+     */
+    private long calculateAppSurfaceFlingerVsyncOffsetMs() {
+        Display display = getDisplay();
+
+        // Calculate vsync offset from SurfaceFlinger.
+        // See frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp:getDisplayConfigs
+        long vsyncPeriod = (long) (ONE_S_IN_NS / display.getRefreshRate());
+        long sfVsyncOffset = vsyncPeriod - (display.getPresentationDeadlineNanos() - ONE_MS_IN_NS);
+        return Math.max(0, (sfVsyncOffset - display.getAppVsyncOffsetNanos()) / ONE_MS_IN_NS);
+    }
+
     @Override
     public WindowInsets onApplyWindowInsets(WindowInsets insets) {
         if (mStableInsets.left != insets.getStableInsetLeft()
@@ -453,7 +497,7 @@
                 if (mMoving && mDockSide != WindowManager.DOCKED_INVALID) {
                     SnapTarget snapTarget = mSnapAlgorithm.calculateSnapTarget(
                             mStartPosition, 0 /* velocity */, false /* hardDismiss */);
-                    resizeStack(calculatePosition(x, y), mStartPosition, snapTarget);
+                    resizeStackDelayed(calculatePosition(x, y), mStartPosition, snapTarget);
                 }
                 break;
             case MotionEvent.ACTION_UP:
@@ -532,10 +576,11 @@
             final long endDelay) {
         final boolean taskPositionSameAtEnd = snapTarget.flag == SnapTarget.FLAG_NONE;
         ValueAnimator anim = ValueAnimator.ofInt(position, snapTarget.position);
-        anim.addUpdateListener(animation -> resizeStack((Integer) animation.getAnimatedValue(),
+        anim.addUpdateListener(animation -> resizeStackDelayed((int) animation.getAnimatedValue(),
                 taskPositionSameAtEnd && animation.getAnimatedFraction() == 1f
                         ? TASK_POSITION_SAME
-                        : snapTarget.taskPosition, snapTarget));
+                        : snapTarget.taskPosition,
+                snapTarget));
         Runnable endAction = () -> {
             commitSnapFlags(snapTarget);
             mWindowManagerProxy.setResizing(false);
@@ -551,15 +596,24 @@
 
             @Override
             public void onAnimationCancel(Animator animation) {
+                mHandler.removeMessages(MSG_RESIZE_STACK);
                 mCancelled = true;
             }
 
             @Override
             public void onAnimationEnd(Animator animation) {
-                if (endDelay == 0 || mCancelled) {
+                long delay = 0;
+                if (endDelay != 0) {
+                    delay = endDelay;
+                } else if (mCancelled) {
+                    delay = 0;
+                } else if (mSurfaceFlingerOffsetMs != 0) {
+                    delay = mSurfaceFlingerOffsetMs;
+                }
+                if (delay == 0) {
                     endAction.run();
                 } else {
-                    mHandler.postDelayed(endAction, endDelay);
+                    mHandler.postDelayed(endAction, delay);
                 }
             }
         });
@@ -793,6 +847,17 @@
                 mDisplayHeight, mDividerSize);
     }
 
+    public void resizeStackDelayed(int position, int taskPosition, SnapTarget taskSnapTarget) {
+        if (mSurfaceFlingerOffsetMs != 0) {
+            Message message = mHandler.obtainMessage(MSG_RESIZE_STACK, position, taskPosition,
+                    taskSnapTarget);
+            message.setAsynchronous(true);
+            mHandler.sendMessageDelayed(message, mSurfaceFlingerOffsetMs);
+        } else {
+            resizeStack(position, taskPosition, taskSnapTarget);
+        }
+    }
+
     public void resizeStack(int position, int taskPosition, SnapTarget taskSnapTarget) {
         calculateBoundsForPosition(position, mDockSide, mDockedRect);
 
diff --git a/packages/SystemUI/src/com/android/systemui/stackdivider/ForcedResizableInfoActivityController.java b/packages/SystemUI/src/com/android/systemui/stackdivider/ForcedResizableInfoActivityController.java
index 5f083d5..5920f46 100644
--- a/packages/SystemUI/src/com/android/systemui/stackdivider/ForcedResizableInfoActivityController.java
+++ b/packages/SystemUI/src/com/android/systemui/stackdivider/ForcedResizableInfoActivityController.java
@@ -20,12 +20,14 @@
 import android.content.Context;
 import android.content.Intent;
 import android.os.Handler;
+import android.os.UserHandle;
 import android.util.ArraySet;
 import android.widget.Toast;
 
 import com.android.systemui.R;
 import com.android.systemui.recents.events.EventBus;
 import com.android.systemui.recents.events.activity.AppTransitionFinishedEvent;
+import com.android.systemui.recents.events.component.ShowUserToastEvent;
 import com.android.systemui.recents.misc.SystemServicesProxy;
 import com.android.systemui.recents.misc.SystemServicesProxy.TaskStackListener;
 import com.android.systemui.stackdivider.events.StartedDragingEvent;
@@ -100,9 +102,8 @@
     }
 
     private void activityDismissingDockedStack() {
-        Toast toast = Toast.makeText(mContext, R.string.dock_non_resizeble_failed_to_dock_text,
-                Toast.LENGTH_SHORT);
-        toast.show();
+        EventBus.getDefault().send(new ShowUserToastEvent(
+                R.string.dock_non_resizeble_failed_to_dock_text, Toast.LENGTH_SHORT));
     }
 
     private void showPending() {
@@ -112,7 +113,7 @@
             ActivityOptions options = ActivityOptions.makeBasic();
             options.setLaunchTaskId(mPendingTaskIds.valueAt(i));
             options.setTaskOverlay(true);
-            mContext.startActivity(intent, options.toBundle());
+            mContext.startActivityAsUser(intent, options.toBundle(), UserHandle.CURRENT);
         }
         mPendingTaskIds.clear();
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ActivatableNotificationView.java b/packages/SystemUI/src/com/android/systemui/statusbar/ActivatableNotificationView.java
index 42c9a126..bc46548 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/ActivatableNotificationView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/ActivatableNotificationView.java
@@ -137,11 +137,13 @@
 
     private float mNormalBackgroundVisibilityAmount;
     private ValueAnimator mFadeInFromDarkAnimator;
+    private float mDimmedBackgroundFadeInAmount = -1;
     private ValueAnimator.AnimatorUpdateListener mBackgroundVisibilityUpdater
             = new ValueAnimator.AnimatorUpdateListener() {
         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
             setNormalBackgroundVisibilityAmount(mBackgroundNormal.getAlpha());
+            mDimmedBackgroundFadeInAmount = mBackgroundDimmed.getAlpha();
         }
     };
     private AnimatorListenerAdapter mFadeInEndListener = new AnimatorListenerAdapter() {
@@ -149,6 +151,7 @@
         public void onAnimationEnd(Animator animation) {
             super.onAnimationEnd(animation);
             mFadeInFromDarkAnimator = null;
+            mDimmedBackgroundFadeInAmount = -1;
             updateBackground();
         }
     };
@@ -590,6 +593,9 @@
             public void onAnimationEnd(Animator animation) {
                 updateBackground();
                 mBackgroundAnimator = null;
+                if (mFadeInFromDarkAnimator == null) {
+                    mDimmedBackgroundFadeInAmount = -1;
+                }
             }
         });
         mBackgroundAnimator.addUpdateListener(mBackgroundVisibilityUpdater);
@@ -597,7 +603,10 @@
     }
 
     protected void updateBackgroundAlpha(float transformationAmount) {
-        mBgAlpha = isChildInGroup() && mDimmed ? transformationAmount : 1f;
+        mBgAlpha =  isChildInGroup() && mDimmed ? transformationAmount : 1f;
+        if (mDimmedBackgroundFadeInAmount != -1) {
+            mBgAlpha *= mDimmedBackgroundFadeInAmount;
+        }
         mBackgroundDimmed.setAlpha(mBgAlpha);
     }
 
@@ -738,6 +747,7 @@
                 }
                 if (!mWasCancelled) {
                     enableAppearDrawing(false);
+                    onAppearAnimationFinished(isAppearing);
                 }
             }
 
@@ -754,6 +764,9 @@
         mAppearAnimator.start();
     }
 
+    protected void onAppearAnimationFinished(boolean wasAppearing) {
+    }
+
     private void cancelAppearAnimation() {
         if (mAppearAnimator != null) {
             mAppearAnimator.cancel();
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
index 5d6b415..54347b4 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/BaseStatusBar.java
@@ -113,8 +113,11 @@
 import com.android.systemui.statusbar.stack.StackStateAnimator;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
+import java.util.Set;
 
 import static android.service.notification.NotificationListenerService.Ranking.IMPORTANCE_HIGH;
 
@@ -268,6 +271,8 @@
 
     protected boolean mVrMode;
 
+    private Set<String> mNonBlockablePkgs;
+
     @Override  // NotificationData.Environment
     public boolean isDeviceProvisioned() {
         return mDeviceProvisioned;
@@ -471,20 +476,28 @@
                 }
             }
 
-            riv.setVisibility(View.VISIBLE);
-            int cx = view.getLeft() + view.getWidth() / 2;
+            int width = view.getWidth();
+            if (view instanceof TextView) {
+                // Center the reveal on the text which might be off-center from the TextView
+                TextView tv = (TextView) view;
+                if (tv.getLayout() != null) {
+                    int innerWidth = (int) tv.getLayout().getLineWidth(0);
+                    innerWidth += tv.getCompoundPaddingLeft() + tv.getCompoundPaddingRight();
+                    width = Math.min(width, innerWidth);
+                }
+            }
+            int cx = view.getLeft() + width / 2;
             int cy = view.getTop() + view.getHeight() / 2;
             int w = riv.getWidth();
             int h = riv.getHeight();
             int r = Math.max(
                     Math.max(cx + cy, cx + (h - cy)),
                     Math.max((w - cx) + cy, (w - cx) + (h - cy)));
-            ViewAnimationUtils.createCircularReveal(riv, cx, cy, 0, r)
-                    .start();
 
+            riv.setRevealParameters(cx, cy, r);
             riv.setPendingIntent(pendingIntent);
             riv.setRemoteInput(inputs, input);
-            riv.focus();
+            riv.focusAnimated();
 
             return true;
         }
@@ -820,6 +833,9 @@
             Slog.e(TAG, "Failed to register VR mode state listener: " + e);
         }
 
+        mNonBlockablePkgs = new HashSet<String>();
+        Collections.addAll(mNonBlockablePkgs, mContext.getResources().getStringArray(
+                com.android.internal.R.array.config_nonBlockableNotificationPackages));
     }
 
     protected void notifyUserAboutHiddenNotifications() {
@@ -957,7 +973,6 @@
                 mNotificationGutsExposed = entry.row.getGuts();
                 bindGuts(entry.row);
             }
-            entry.cacheContentViews(mContext, null /* updatedNotification */);
             inflateViews(entry, mStackScroller);
         }
     }
@@ -1101,7 +1116,8 @@
             settingsButton.setVisibility(View.GONE);
         }
 
-        guts.bindImportance(pmUser, sbn, mNotificationData.getImportance(sbn.getKey()));
+        guts.bindImportance(pmUser, sbn, mNonBlockablePkgs,
+                mNotificationData.getImportance(sbn.getKey()));
 
         final TextView doneButton = (TextView) guts.findViewById(R.id.done);
         doneButton.setText(R.string.notification_done);
@@ -1581,7 +1597,12 @@
                 entry.notification.getUser().getIdentifier());
 
         final StatusBarNotification sbn = entry.notification;
-        entry.cacheContentViews(mContext, null);
+        try {
+            entry.cacheContentViews(mContext, null);
+        } catch (RuntimeException e) {
+            Log.e(TAG, "Unable to get notification remote views", e);
+            return false;
+        }
 
         final RemoteViews contentView = entry.cachedContentView;
         final RemoteViews bigContentView = entry.cachedBigContentView;
@@ -1936,9 +1957,18 @@
                                             .getIdentifier();
                                     if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)
                                             && mKeyguardManager.isDeviceLocked(userId)) {
-                                        if (startWorkChallengeIfNecessary(userId,
-                                                intent.getIntentSender(), notificationKey)) {
-                                            // Show work challenge, do not run pendingintent and
+                                        boolean canBypass = false;
+                                        try {
+                                            canBypass = ActivityManagerNative.getDefault()
+                                                    .canBypassWorkChallenge(intent);
+                                        } catch (RemoteException e) {
+                                        }
+                                        // For direct-boot aware activities, they can be shown when
+                                        // the device is still locked without triggering the work
+                                        // challenge.
+                                        if ((!canBypass) && startWorkChallengeIfNecessary(userId,
+                                                    intent.getIntentSender(), notificationKey)) {
+                                            // Show work challenge, do not run PendingIntent and
                                             // remove notification
                                             return;
                                         }
@@ -2350,7 +2380,13 @@
         Notification n = notification.getNotification();
         mNotificationData.updateRanking(ranking);
 
-        boolean applyInPlace = entry.cacheContentViews(mContext, notification.getNotification());
+        boolean applyInPlace;
+        try {
+            applyInPlace = entry.cacheContentViews(mContext, notification.getNotification());
+        } catch (RuntimeException e) {
+            Log.e(TAG, "Unable to get notification remote views", e);
+            applyInPlace = false;
+        }
         boolean shouldPeek = shouldPeek(entry, notification);
         boolean alertAgain = alertAgain(entry, n);
         if (DEBUG) {
@@ -2402,7 +2438,10 @@
                     StatusBarIconView.contentDescForNotification(mContext, n));
             entry.icon.setNotification(n);
             entry.icon.set(ic);
-            inflateViews(entry, mStackScroller);
+            if (!inflateViews(entry, mStackScroller)) {
+                handleNotificationError(notification, "Couldn't update remote views for: "
+                        + notification);
+            }
         }
         updateHeadsUp(key, entry, shouldPeek, alertAgain);
         updateNotifications();
@@ -2500,8 +2539,7 @@
 
         boolean inUse = mPowerManager.isScreenOn()
                 && (!mStatusBarKeyguardViewManager.isShowing()
-                || mStatusBarKeyguardViewManager.isOccluded())
-                && !mStatusBarKeyguardViewManager.isInputRestricted();
+                || mStatusBarKeyguardViewManager.isOccluded());
         try {
             inUse = inUse && !mDreamManager.isDreaming();
         } catch (RemoteException e) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java b/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
index 7b23c80..419c91b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/CommandQueue.java
@@ -23,6 +23,7 @@
 import android.os.IBinder;
 import android.os.Message;
 import android.util.Pair;
+import android.view.KeyEvent;
 
 import com.android.internal.os.SomeArgs;
 import com.android.internal.statusbar.IStatusBar;
@@ -75,6 +76,7 @@
     private static final int MSG_TOGGLE_APP_SPLIT_SCREEN       = 30 << MSG_SHIFT;
     private static final int MSG_APP_TRANSITION_FINISHED       = 31 << MSG_SHIFT;
     private static final int MSG_DISMISS_KEYBOARD_SHORTCUTS    = 32 << MSG_SHIFT;
+    private static final int MSG_HANDLE_SYSNAV_KEY             = 33 << MSG_SHIFT;
 
     public static final int FLAG_EXCLUDE_NONE = 0;
     public static final int FLAG_EXCLUDE_SEARCH_PANEL = 1 << 0;
@@ -129,6 +131,8 @@
         void addQsTile(ComponentName tile);
         void remQsTile(ComponentName tile);
         void clickTile(ComponentName tile);
+
+        void handleSystemNavigationKey(int arg1);
     }
 
     public CommandQueue(Callbacks callbacks) {
@@ -388,6 +392,13 @@
         }
     }
 
+    @Override
+    public void handleSystemNavigationKey(int key) {
+        synchronized (mLock) {
+            mHandler.obtainMessage(MSG_HANDLE_SYSNAV_KEY, key, 0).sendToTarget();
+        }
+    }
+
     private final class H extends Handler {
         public void handleMessage(Message msg) {
             final int what = msg.what & MSG_MASK;
@@ -503,6 +514,9 @@
                 case MSG_TOGGLE_APP_SPLIT_SCREEN:
                     mCallbacks.toggleSplitScreen();
                     break;
+                case MSG_HANDLE_SYSNAV_KEY:
+                    mCallbacks.handleSystemNavigationKey(msg.arg1);
+                    break;
             }
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/CrossFadeHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/CrossFadeHelper.java
index 123dc69..e8f0925 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/CrossFadeHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/CrossFadeHelper.java
@@ -48,13 +48,30 @@
     }
 
     public static void fadeOut(View view, float fadeOutAmount) {
+        fadeOut(view, fadeOutAmount, true /* remap */);
+    }
+
+    /**
+     * Fade out a view by a given progress amount
+     * @param view the view to fade out
+     * @param fadeOutAmount how much the view is faded out. 0 means not at all and 1 means fully
+     *                      faded out
+     * @param remap whether the fade amount should be remapped to the shorter duration
+     * {@link #ANIMATION_DURATION_LENGTH} from the normal fade duration
+     * {@link StackStateAnimator#ANIMATION_DURATION_STANDARD} in order to have a faster fading.
+     *
+     * @see #fadeIn(View, float, boolean)
+     */
+    public static void fadeOut(View view, float fadeOutAmount, boolean remap) {
         view.animate().cancel();
         if (fadeOutAmount == 1.0f) {
             view.setVisibility(View.INVISIBLE);
         } else if (view.getVisibility() == View.INVISIBLE) {
             view.setVisibility(View.VISIBLE);
         }
-        fadeOutAmount = mapToFadeDuration(fadeOutAmount);
+        if (remap) {
+            fadeOutAmount = mapToFadeDuration(fadeOutAmount);
+        }
         float alpha = Interpolators.ALPHA_OUT.getInterpolation(1.0f - fadeOutAmount);
         view.setAlpha(alpha);
         updateLayerType(view, alpha);
@@ -92,11 +109,28 @@
     }
 
     public static void fadeIn(View view, float fadeInAmount) {
+        fadeIn(view, fadeInAmount, true /* remap */);
+    }
+
+    /**
+     * Fade in a view by a given progress amount
+     * @param view the view to fade in
+     * @param fadeInAmount how much the view is faded in. 0 means not at all and 1 means fully
+     *                     faded in.
+     * @param remap whether the fade amount should be remapped to the shorter duration
+     * {@link #ANIMATION_DURATION_LENGTH} from the normal fade duration
+     * {@link StackStateAnimator#ANIMATION_DURATION_STANDARD} in order to have a faster fading.
+     *
+     * @see #fadeOut(View, float, boolean)
+     */
+    public static void fadeIn(View view, float fadeInAmount, boolean remap) {
         view.animate().cancel();
         if (view.getVisibility() == View.INVISIBLE) {
             view.setVisibility(View.VISIBLE);
         }
-        fadeInAmount = mapToFadeDuration(fadeInAmount);
+        if (remap) {
+            fadeInAmount = mapToFadeDuration(fadeInAmount);
+        }
         float alpha = Interpolators.ALPHA_IN.getInterpolation(fadeInAmount);
         view.setAlpha(alpha);
         updateLayerType(view, alpha);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/DismissView.java b/packages/SystemUI/src/com/android/systemui/statusbar/DismissView.java
index 2045ec8..1d7bede 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/DismissView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/DismissView.java
@@ -56,6 +56,8 @@
     protected void onConfigurationChanged(Configuration newConfig) {
         super.onConfigurationChanged(newConfig);
         mDismissButton.setText(R.string.clear_all_notifications_text);
+        mDismissButton.setContentDescription(
+                mContext.getString(R.string.accessibility_clear_all));
     }
 
     public boolean isButtonVisible() {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java b/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
index e400506..caf5447 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/ExpandableNotificationRow.java
@@ -507,7 +507,7 @@
         int intrinsicHeight = getIntrinsicHeight();
         mIsPinned = pinned;
         if (intrinsicHeight != getIntrinsicHeight()) {
-            notifyHeightChanged(false);
+            notifyHeightChanged(false /* needsAnimation */);
         }
         if (pinned) {
             setIconAnimationRunning(true);
@@ -598,7 +598,7 @@
     }
 
     private NotificationHeaderView getVisibleNotificationHeader() {
-        if (mIsSummaryWithChildren) {
+        if (mIsSummaryWithChildren && !mShowingPublic) {
             return mChildrenContainer.getHeaderView();
         }
         return getShowingLayout().getVisibleNotificationHeader();
@@ -840,8 +840,6 @@
     }
 
     public void resetHeight() {
-        mMaxExpandHeight = 0;
-        mHeadsUpHeight = 0;
         onHeightReset();
         requestLayout();
     }
@@ -907,6 +905,9 @@
     }
 
     public void resetTranslation() {
+        if (mTranslateAnim != null) {
+            mTranslateAnim.cancel();
+        }
         if (mTranslateableViews != null) {
             for (int i = 0; i < mTranslateableViews.size(); i++) {
                 mTranslateableViews.get(i).setTranslationX(0);
@@ -1122,7 +1123,7 @@
         mPrivateLayout.setUserExpanding(userLocked);
         if (mIsSummaryWithChildren) {
             mChildrenContainer.setUserLocked(userLocked);
-            if (userLocked || (!userLocked && !isGroupExpanded())) {
+            if (userLocked || !isGroupExpanded()) {
                 updateBackgroundForGroupState();
             }
         }
@@ -1175,7 +1176,20 @@
      * @see #canViewBeDismissed()
      */
     public boolean isClearable() {
-        return mStatusBarNotification != null && mStatusBarNotification.isClearable();
+        if (mStatusBarNotification == null || !mStatusBarNotification.isClearable()) {
+            return false;
+        }
+        if (mIsSummaryWithChildren) {
+            List<ExpandableNotificationRow> notificationChildren =
+                    mChildrenContainer.getNotificationChildren();
+            for (int i = 0; i < notificationChildren.size(); i++) {
+                ExpandableNotificationRow child = notificationChildren.get(i);
+                if (!child.isClearable()) {
+                    return false;
+                }
+            }
+        }
+        return true;
     }
 
     @Override
@@ -1277,7 +1291,7 @@
         }
         mHeadsUpHeight = headsUpChild.getHeight();
         if (intrinsicBefore != getIntrinsicHeight()) {
-            notifyHeightChanged(false  /* needsAnimation */);
+            notifyHeightChanged(true  /* needsAnimation */);
         }
     }
 
@@ -1385,7 +1399,7 @@
         if (isChildInGroup()) {
             mGroupManager.setGroupExpanded(mStatusBarNotification, true);
         }
-        notifyHeightChanged(false);
+        notifyHeightChanged(false /* needsAnimation */);
     }
 
     public void setChildrenExpanded(boolean expanded, boolean animate) {
@@ -1429,13 +1443,30 @@
 
     @Override
     protected View getContentView() {
-        if (mIsSummaryWithChildren) {
+        if (mIsSummaryWithChildren && !mShowingPublic) {
             return mChildrenContainer;
         }
         return getShowingLayout();
     }
 
     @Override
+    protected void onAppearAnimationFinished(boolean wasAppearing) {
+        super.onAppearAnimationFinished(wasAppearing);
+        if (wasAppearing) {
+            // During the animation the visible view might have changed, so let's make sure all
+            // alphas are reset
+            if (mChildrenContainer != null) {
+                mChildrenContainer.setAlpha(1.0f);
+                mChildrenContainer.setLayerType(LAYER_TYPE_NONE, null);
+            }
+            mPrivateLayout.setAlpha(1.0f);
+            mPrivateLayout.setLayerType(LAYER_TYPE_NONE, null);
+            mPublicLayout.setAlpha(1.0f);
+            mPublicLayout.setLayerType(LAYER_TYPE_NONE, null);
+        }
+    }
+
+    @Override
     public int getExtraBottomPadding() {
         if (mIsSummaryWithChildren && isGroupExpanded()) {
             return mIncreasedPaddingBetweenElements;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/KeyboardShortcuts.java b/packages/SystemUI/src/com/android/systemui/statusbar/KeyboardShortcuts.java
index 79e06c6..016e1f2 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/KeyboardShortcuts.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/KeyboardShortcuts.java
@@ -16,6 +16,8 @@
 
 package com.android.systemui.statusbar;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.app.AlertDialog;
 import android.app.AppGlobals;
 import android.app.Dialog;
@@ -45,15 +47,18 @@
 import android.view.KeyboardShortcutInfo;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.View.AccessibilityDelegate;
 import android.view.ViewGroup;
 import android.view.Window;
 import android.view.WindowManager.KeyboardShortcutsReceiver;
+import android.view.accessibility.AccessibilityNodeInfo;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
 
 import com.android.internal.app.AssistUtils;
+import com.android.settingslib.Utils;
 import com.android.systemui.R;
 import com.android.systemui.recents.Recents;
 
@@ -63,6 +68,7 @@
 import java.util.List;
 
 import static android.content.Context.LAYOUT_INFLATER_SERVICE;
+import static android.view.View.IMPORTANT_FOR_ACCESSIBILITY_YES;
 import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
 
 /**
@@ -72,7 +78,6 @@
     private static final String TAG = KeyboardShortcuts.class.getSimpleName();
     private static final Object sLock = new Object();
     private static KeyboardShortcuts sInstance;
-    private static boolean sIsShowing;
 
     private final SparseArray<String> mSpecialCharacterNames = new SparseArray<>();
     private final SparseArray<String> mModifierNames = new SparseArray<>();
@@ -113,7 +118,7 @@
     private KeyCharacterMap mKeyCharacterMap;
 
     private KeyboardShortcuts(Context context) {
-        this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_Material_Light);
+        this.mContext = new ContextThemeWrapper(context, android.R.style.Theme_DeviceDefault_Light);
         this.mPackageManager = AppGlobals.getPackageManager();
         loadResources(context);
     }
@@ -131,13 +136,12 @@
                 dismiss();
             }
             getInstance(context).showKeyboardShortcuts(deviceId);
-            sIsShowing = true;
         }
     }
 
     public static void toggle(Context context, int deviceId) {
         synchronized (sLock) {
-            if (sIsShowing) {
+            if (isShowing()) {
                 dismiss();
             } else {
                 show(context, deviceId);
@@ -151,10 +155,14 @@
                 sInstance.dismissKeyboardShortcuts();
                 sInstance = null;
             }
-            sIsShowing = false;
         }
     }
 
+    private static boolean isShowing() {
+        return sInstance != null && sInstance.mKeyboardShortcutsDialog != null
+                && sInstance.mKeyboardShortcutsDialog.isShowing();
+    }
+
     private void loadResources(Context context) {
         mSpecialCharacterNames.put(
                 KeyEvent.KEYCODE_HOME, context.getString(R.string.keyboard_key_home));
@@ -580,7 +588,7 @@
                     R.layout.keyboard_shortcuts_category_title, keyboardShortcutsLayout, false);
             categoryTitle.setText(group.getLabel());
             categoryTitle.setTextColor(group.isSystemGroup()
-                    ? mContext.getColor(R.color.ksh_system_group_color)
+                    ? Utils.getColorAccent(mContext)
                     : mContext.getColor(R.color.ksh_application_group_color));
             keyboardShortcutsLayout.addView(categoryTitle);
 
@@ -589,7 +597,7 @@
             final int itemsSize = group.getItems().size();
             for (int j = 0; j < itemsSize; j++) {
                 KeyboardShortcutInfo info = group.getItems().get(j);
-                List<StringOrDrawable> shortcutKeys = getHumanReadableShortcutKeys(info);
+                List<StringDrawableContainer> shortcutKeys = getHumanReadableShortcutKeys(info);
                 if (shortcutKeys == null) {
                     // Ignore shortcuts we can't display keys for.
                     Log.w(TAG, "Keyboard Shortcut contains unsupported keys, skipping.");
@@ -619,25 +627,33 @@
                         .findViewById(R.id.keyboard_shortcuts_item_container);
                 final int shortcutKeysSize = shortcutKeys.size();
                 for (int k = 0; k < shortcutKeysSize; k++) {
-                    StringOrDrawable shortcutRepresentation = shortcutKeys.get(k);
-                    if (shortcutRepresentation.drawable != null) {
+                    StringDrawableContainer shortcutRepresentation = shortcutKeys.get(k);
+                    if (shortcutRepresentation.mDrawable != null) {
                         ImageView shortcutKeyIconView = (ImageView) inflater.inflate(
                                 R.layout.keyboard_shortcuts_key_icon_view, shortcutItemsContainer,
                                 false);
                         Bitmap bitmap = Bitmap.createBitmap(shortcutKeyIconItemHeightWidth,
                                 shortcutKeyIconItemHeightWidth, Bitmap.Config.ARGB_8888);
                         Canvas canvas = new Canvas(bitmap);
-                        shortcutRepresentation.drawable.setBounds(0, 0, canvas.getWidth(),
+                        shortcutRepresentation.mDrawable.setBounds(0, 0, canvas.getWidth(),
                                 canvas.getHeight());
-                        shortcutRepresentation.drawable.draw(canvas);
+                        shortcutRepresentation.mDrawable.draw(canvas);
                         shortcutKeyIconView.setImageBitmap(bitmap);
+                        shortcutKeyIconView.setImportantForAccessibility(
+                                IMPORTANT_FOR_ACCESSIBILITY_YES);
+                        shortcutKeyIconView.setAccessibilityDelegate(
+                                new ShortcutKeyAccessibilityDelegate(
+                                        shortcutRepresentation.mString));
                         shortcutItemsContainer.addView(shortcutKeyIconView);
-                    } else if (shortcutRepresentation.string != null) {
+                    } else if (shortcutRepresentation.mString != null) {
                         TextView shortcutKeyTextView = (TextView) inflater.inflate(
                                 R.layout.keyboard_shortcuts_key_view, shortcutItemsContainer,
                                 false);
                         shortcutKeyTextView.setMinimumWidth(shortcutKeyTextItemMinWidth);
-                        shortcutKeyTextView.setText(shortcutRepresentation.string);
+                        shortcutKeyTextView.setText(shortcutRepresentation.mString);
+                        shortcutKeyTextView.setAccessibilityDelegate(
+                                new ShortcutKeyAccessibilityDelegate(
+                                        shortcutRepresentation.mString));
                         shortcutItemsContainer.addView(shortcutKeyTextView);
                     }
                 }
@@ -653,19 +669,20 @@
         }
     }
 
-    private List<StringOrDrawable> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
-        List<StringOrDrawable> shortcutKeys = getHumanReadableModifiers(info);
+    private List<StringDrawableContainer> getHumanReadableShortcutKeys(KeyboardShortcutInfo info) {
+        List<StringDrawableContainer> shortcutKeys = getHumanReadableModifiers(info);
         if (shortcutKeys == null) {
             return null;
         }
-        String displayLabelString = null;
-        Drawable displayLabelDrawable = null;
+        String shortcutKeyString = null;
+        Drawable shortcutKeyDrawable = null;
         if (info.getBaseCharacter() > Character.MIN_VALUE) {
-            displayLabelString = String.valueOf(info.getBaseCharacter());
+            shortcutKeyString = String.valueOf(info.getBaseCharacter());
         } else if (mSpecialCharacterDrawables.get(info.getKeycode()) != null) {
-            displayLabelDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
+            shortcutKeyDrawable = mSpecialCharacterDrawables.get(info.getKeycode());
+            shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
         } else if (mSpecialCharacterNames.get(info.getKeycode()) != null) {
-            displayLabelString = mSpecialCharacterNames.get(info.getKeycode());
+            shortcutKeyString = mSpecialCharacterNames.get(info.getKeycode());
         } else {
             // Special case for shortcuts with no base key or keycode.
             if (info.getKeycode() == KeyEvent.KEYCODE_UNKNOWN) {
@@ -673,22 +690,23 @@
             }
             char displayLabel = mKeyCharacterMap.getDisplayLabel(info.getKeycode());
             if (displayLabel != 0) {
-                displayLabelString = String.valueOf(displayLabel);
+                shortcutKeyString = String.valueOf(displayLabel);
             } else {
                 return null;
             }
         }
 
-        if (displayLabelDrawable != null) {
-            shortcutKeys.add(new StringOrDrawable(displayLabelDrawable));
-        } else if (displayLabelString != null) {
-            shortcutKeys.add(new StringOrDrawable(displayLabelString.toUpperCase()));
+        if (shortcutKeyString != null) {
+            shortcutKeys.add(new StringDrawableContainer(shortcutKeyString, shortcutKeyDrawable));
+        } else {
+            Log.w(TAG, "Keyboard Shortcut does not have a text representation, skipping.");
         }
+
         return shortcutKeys;
     }
 
-    private List<StringOrDrawable> getHumanReadableModifiers(KeyboardShortcutInfo info) {
-        final List<StringOrDrawable> shortcutKeys = new ArrayList<>();
+    private List<StringDrawableContainer> getHumanReadableModifiers(KeyboardShortcutInfo info) {
+        final List<StringDrawableContainer> shortcutKeys = new ArrayList<>();
         int modifiers = info.getModifiers();
         if (modifiers == 0) {
             return shortcutKeys;
@@ -696,13 +714,9 @@
         for(int i = 0; i < mModifierNames.size(); ++i) {
             final int supportedModifier = mModifierNames.keyAt(i);
             if ((modifiers & supportedModifier) != 0) {
-                if (mModifierDrawables.get(supportedModifier) != null) {
-                    shortcutKeys.add(new StringOrDrawable(
-                            mModifierDrawables.get(supportedModifier)));
-                } else {
-                    shortcutKeys.add(new StringOrDrawable(
-                            mModifierNames.get(supportedModifier).toUpperCase()));
-                }
+                shortcutKeys.add(new StringDrawableContainer(
+                        mModifierNames.get(supportedModifier),
+                        mModifierDrawables.get(supportedModifier)));
                 modifiers &= ~supportedModifier;
             }
         }
@@ -713,16 +727,31 @@
         return shortcutKeys;
     }
 
-    private static final class StringOrDrawable {
-        public String string;
-        public Drawable drawable;
+    private final class ShortcutKeyAccessibilityDelegate extends AccessibilityDelegate {
+        private String mContentDescription;
 
-        public StringOrDrawable(String string) {
-            this.string = string;
+        ShortcutKeyAccessibilityDelegate(String contentDescription) {
+            mContentDescription = contentDescription;
         }
 
-        public StringOrDrawable(Drawable drawable) {
-            this.drawable = drawable;
+        @Override
+        public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfo info) {
+            super.onInitializeAccessibilityNodeInfo(host, info);
+            if (mContentDescription != null) {
+                info.setContentDescription(mContentDescription.toLowerCase());
+            }
+        }
+    }
+
+    private static final class StringDrawableContainer {
+        @NonNull
+        public String mString;
+        @Nullable
+        public Drawable mDrawable;
+
+        StringDrawableContainer(String string, Drawable drawable) {
+            mString = string;
+            mDrawable = drawable;
         }
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardAffordanceView.java b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardAffordanceView.java
index 0b1984d..8b4225a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardAffordanceView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardAffordanceView.java
@@ -147,7 +147,7 @@
 
     @Override
     protected void onDraw(Canvas canvas) {
-        mSupportHardware = false;//canvas.isHardwareAccelerated();
+        mSupportHardware = canvas.isHardwareAccelerated();
         drawBackgroundCircle(canvas);
         canvas.save();
         canvas.scale(mImageScale, mImageScale, getWidth() / 2, getHeight() / 2);
@@ -174,7 +174,10 @@
 
     private void drawBackgroundCircle(Canvas canvas) {
         if (mCircleRadius > 0 || mFinishing) {
-            if (mFinishing && mSupportHardware) {
+            if (mFinishing && mSupportHardware && mHwCenterX != null) {
+                // Our hardware drawing proparties can be null if the finishing started but we have
+                // never drawn before. In that case we are not doing a render thread animation
+                // anyway, so we need to use the normal drawing.
                 DisplayListCanvas displayListCanvas = (DisplayListCanvas) canvas;
                 displayListCanvas.drawCircle(mHwCenterX, mHwCenterY, mHwCircleRadius,
                         mHwCirclePaint);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
index 71904fa..0ef97152 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.statusbar;
 
+import android.app.ActivityManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -30,6 +31,7 @@
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.text.TextUtils;
 import android.text.format.Formatter;
 import android.util.Log;
@@ -57,6 +59,7 @@
 
     private final Context mContext;
     private final KeyguardIndicationTextView mTextView;
+    private final UserManager mUserManager;
     private final IBatteryStats mBatteryInfo;
 
     private final int mSlowThreshold;
@@ -85,11 +88,12 @@
         mSlowThreshold = res.getInteger(R.integer.config_chargingSlowlyThreshold);
         mFastThreshold = res.getInteger(R.integer.config_chargingFastThreshold);
 
-
+        mUserManager = context.getSystemService(UserManager.class);
         mBatteryInfo = IBatteryStats.Stub.asInterface(
                 ServiceManager.getService(BatteryStats.SERVICE_NAME));
+
         KeyguardUpdateMonitor.getInstance(context).registerCallback(mUpdateMonitor);
-        context.registerReceiverAsUser(mReceiver, UserHandle.SYSTEM,
+        context.registerReceiverAsUser(mTickReceiver, UserHandle.SYSTEM,
                 new IntentFilter(Intent.ACTION_TIME_TICK), null, null);
     }
 
@@ -155,30 +159,29 @@
 
     private void updateIndication() {
         if (mVisible) {
-            mTextView.switchIndication(computeIndication());
-            mTextView.setTextColor(computeColor());
-        }
-    }
+            // Walk down a precedence-ordered list of what should indication
+            // should be shown based on user or device state
+            if (!mUserManager.isUserUnlocked(ActivityManager.getCurrentUser())) {
+                mTextView.switchIndication(com.android.internal.R.string.lockscreen_storage_locked);
+                mTextView.setTextColor(Color.WHITE);
 
-    private int computeColor() {
-        if (!TextUtils.isEmpty(mTransientIndication)) {
-            return mTransientTextColor;
-        }
-        return Color.WHITE;
-    }
+            } else if (!TextUtils.isEmpty(mTransientIndication)) {
+                mTextView.switchIndication(mTransientIndication);
+                mTextView.setTextColor(mTransientTextColor);
 
-    private String computeIndication() {
-        if (!TextUtils.isEmpty(mTransientIndication)) {
-            return mTransientIndication;
-        }
-        if (mPowerPluggedIn) {
-            String indication = computePowerIndication();
-            if (DEBUG_CHARGING_SPEED) {
-                indication += ",  " + (mChargingWattage / 1000) + " mW";
+            } else if (mPowerPluggedIn) {
+                String indication = computePowerIndication();
+                if (DEBUG_CHARGING_SPEED) {
+                    indication += ",  " + (mChargingWattage / 1000) + " mW";
+                }
+                mTextView.switchIndication(indication);
+                mTextView.setTextColor(Color.WHITE);
+
+            } else {
+                mTextView.switchIndication(mRestingIndication);
+                mTextView.setTextColor(Color.WHITE);
             }
-            return indication;
         }
-        return mRestingIndication;
     }
 
     private String computePowerIndication() {
@@ -225,6 +228,8 @@
     }
 
     KeyguardUpdateMonitorCallback mUpdateMonitor = new KeyguardUpdateMonitorCallback() {
+        public int mLastSuccessiveErrorMessage = -1;
+
         @Override
         public void onRefreshBatteryInfo(KeyguardUpdateMonitor.BatteryStatus status) {
             boolean isChargingOrFull = status.status == BatteryManager.BATTERY_STATUS_CHARGING
@@ -252,6 +257,9 @@
                 mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_CLEAR_FP_MSG),
                         TRANSIENT_FP_ERROR_TIMEOUT);
             }
+            // Help messages indicate that there was actually a try since the last error, so those
+            // are not two successive error messages anymore.
+            mLastSuccessiveErrorMessage = -1;
         }
 
         @Override
@@ -263,15 +271,22 @@
             }
             int errorColor = mContext.getResources().getColor(R.color.system_warning_color, null);
             if (mStatusBarKeyguardViewManager.isBouncerShowing()) {
-                mStatusBarKeyguardViewManager.showBouncerMessage(errString, errorColor);
+                // When swiping up right after receiving a fingerprint error, the bouncer calls
+                // authenticate leading to the same message being shown again on the bouncer.
+                // We want to avoid this, as it may confuse the user when the message is too
+                // generic.
+                if (mLastSuccessiveErrorMessage != msgId) {
+                    mStatusBarKeyguardViewManager.showBouncerMessage(errString, errorColor);
+                }
             } else if (updateMonitor.isDeviceInteractive()) {
-                    showTransientIndication(errString, errorColor);
-                    // We want to keep this message around in case the screen was off
-                    mHandler.removeMessages(MSG_HIDE_TRANSIENT);
-                    hideTransientIndicationDelayed(5000);
-             } else {
-                    mMessageToShowOnScreenOn = errString;
+                showTransientIndication(errString, errorColor);
+                // We want to keep this message around in case the screen was off
+                mHandler.removeMessages(MSG_HIDE_TRANSIENT);
+                hideTransientIndicationDelayed(5000);
+            } else {
+                mMessageToShowOnScreenOn = errString;
             }
+            mLastSuccessiveErrorMessage = msgId;
         }
 
         @Override
@@ -293,9 +308,28 @@
                 mMessageToShowOnScreenOn = null;
             }
         }
+
+        @Override
+        public void onFingerprintAuthenticated(int userId) {
+            super.onFingerprintAuthenticated(userId);
+            mLastSuccessiveErrorMessage = -1;
+        }
+
+        @Override
+        public void onFingerprintAuthFailed() {
+            super.onFingerprintAuthFailed();
+            mLastSuccessiveErrorMessage = -1;
+        }
+
+        @Override
+        public void onUserUnlocked() {
+            if (mVisible) {
+                updateIndication();
+            }
+        }
     };
 
-    BroadcastReceiver mReceiver = new BroadcastReceiver() {
+    BroadcastReceiver mTickReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
             if (mVisible) {
@@ -304,6 +338,7 @@
         }
     };
 
+
     private final Handler mHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationContentView.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationContentView.java
index 9fd09d9..58d57f6 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationContentView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationContentView.java
@@ -116,6 +116,8 @@
     private boolean mForceSelectNextLayout = true;
     private PendingIntent mPreviousExpandedRemoteInputIntent;
     private PendingIntent mPreviousHeadsUpRemoteInputIntent;
+    private RemoteInputView mCachedExpandedRemoteInput;
+    private RemoteInputView mCachedHeadsUpRemoteInput;
 
     private int mContentHeightAtAnimationStart = UNDEFINED;
     private boolean mFocusOnVisibilityChange;
@@ -205,7 +207,7 @@
                     && MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED) {
                 singleLineWidthSpec = MeasureSpec.makeMeasureSpec(
                         width - mSingleLineWidthIndention + mSingleLineView.getPaddingEnd(),
-                        MeasureSpec.AT_MOST);
+                        MeasureSpec.EXACTLY);
             }
             mSingleLineView.measure(singleLineWidthSpec,
                     MeasureSpec.makeMeasureSpec(maxSize, MeasureSpec.AT_MOST));
@@ -298,6 +300,9 @@
             mExpandedRemoteInput.onNotificationUpdateOrReset();
             if (mExpandedRemoteInput.isActive()) {
                 mPreviousExpandedRemoteInputIntent = mExpandedRemoteInput.getPendingIntent();
+                mCachedExpandedRemoteInput = mExpandedRemoteInput;
+                mExpandedRemoteInput.dispatchStartTemporaryDetach();
+                ((ViewGroup)mExpandedRemoteInput.getParent()).removeView(mExpandedRemoteInput);
             }
         }
         if (mExpandedChild != null) {
@@ -310,6 +315,9 @@
             mHeadsUpRemoteInput.onNotificationUpdateOrReset();
             if (mHeadsUpRemoteInput.isActive()) {
                 mPreviousHeadsUpRemoteInputIntent = mHeadsUpRemoteInput.getPendingIntent();
+                mCachedHeadsUpRemoteInput = mHeadsUpRemoteInput;
+                mHeadsUpRemoteInput.dispatchStartTemporaryDetach();
+                ((ViewGroup)mHeadsUpRemoteInput.getParent()).removeView(mHeadsUpRemoteInput);
             }
         }
         if (mHeadsUpChild != null) {
@@ -963,22 +971,35 @@
         View bigContentView = mExpandedChild;
         if (bigContentView != null) {
             mExpandedRemoteInput = applyRemoteInput(bigContentView, entry, hasRemoteInput,
-                    mPreviousExpandedRemoteInputIntent);
+                    mPreviousExpandedRemoteInputIntent, mCachedExpandedRemoteInput);
         } else {
             mExpandedRemoteInput = null;
         }
+        if (mCachedExpandedRemoteInput != null
+                && mCachedExpandedRemoteInput != mExpandedRemoteInput) {
+            // We had a cached remote input but didn't reuse it. Clean up required.
+            mCachedExpandedRemoteInput.dispatchFinishTemporaryDetach();
+        }
+        mCachedExpandedRemoteInput = null;
 
         View headsUpContentView = mHeadsUpChild;
         if (headsUpContentView != null) {
             mHeadsUpRemoteInput = applyRemoteInput(headsUpContentView, entry, hasRemoteInput,
-                    mPreviousHeadsUpRemoteInputIntent);
+                    mPreviousHeadsUpRemoteInputIntent, mCachedHeadsUpRemoteInput);
         } else {
             mHeadsUpRemoteInput = null;
         }
+        if (mCachedHeadsUpRemoteInput != null
+                && mCachedHeadsUpRemoteInput != mHeadsUpRemoteInput) {
+            // We had a cached remote input but didn't reuse it. Clean up required.
+            mCachedHeadsUpRemoteInput.dispatchFinishTemporaryDetach();
+        }
+        mCachedHeadsUpRemoteInput = null;
     }
 
     private RemoteInputView applyRemoteInput(View view, NotificationData.Entry entry,
-            boolean hasRemoteInput, PendingIntent existingPendingIntent) {
+            boolean hasRemoteInput, PendingIntent existingPendingIntent,
+            RemoteInputView cachedView) {
         View actionContainerCandidate = view.findViewById(
                 com.android.internal.R.id.actions_container);
         if (actionContainerCandidate instanceof FrameLayout) {
@@ -991,15 +1012,22 @@
 
             if (existing == null && hasRemoteInput) {
                 ViewGroup actionContainer = (FrameLayout) actionContainerCandidate;
-                RemoteInputView riv = RemoteInputView.inflate(
-                        mContext, actionContainer, entry, mRemoteInputController);
+                if (cachedView == null) {
+                    RemoteInputView riv = RemoteInputView.inflate(
+                            mContext, actionContainer, entry, mRemoteInputController);
 
-                riv.setVisibility(View.INVISIBLE);
-                actionContainer.addView(riv, new LayoutParams(
-                        ViewGroup.LayoutParams.MATCH_PARENT,
-                        ViewGroup.LayoutParams.MATCH_PARENT)
-                );
-                existing = riv;
+                    riv.setVisibility(View.INVISIBLE);
+                    actionContainer.addView(riv, new LayoutParams(
+                            ViewGroup.LayoutParams.MATCH_PARENT,
+                            ViewGroup.LayoutParams.MATCH_PARENT)
+                    );
+                    existing = riv;
+                } else {
+                    actionContainer.addView(cachedView);
+                    cachedView.dispatchFinishTemporaryDetach();
+                    cachedView.requestFocus();
+                    existing = cachedView;
+                }
             }
             if (hasRemoteInput) {
                 int color = entry.notification.getNotification().color;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationData.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationData.java
index 6570221..05a9fc7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationData.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationData.java
@@ -34,7 +34,6 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.Map;
 import java.util.Objects;
 
 /**
@@ -343,7 +342,6 @@
                         entry.notification.setOverrideGroupKey(overrideGroupKey);
                         mGroupManager.onEntryUpdated(entry, oldSbn);
                     }
-                    //mGroupManager.onEntryBundlingUpdated(entry, getOverrideGroupKey(entry.key));
                 }
             }
         }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationGuts.java b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationGuts.java
index 870e7f0..62d730a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationGuts.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationGuts.java
@@ -50,6 +50,8 @@
 import com.android.systemui.statusbar.stack.StackStateAnimator;
 import com.android.systemui.tuner.TunerService;
 
+import java.util.Set;
+
 /**
  * The guts of a notification revealed when performing a long press.
  */
@@ -173,7 +175,7 @@
     }
 
     void bindImportance(final PackageManager pm, final StatusBarNotification sbn,
-            final int importance) {
+            final Set<String> nonBlockablePkgs, final int importance) {
         mINotificationManager = INotificationManager.Stub.asInterface(
                 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
         mStartingUserImportance = NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED;
@@ -182,24 +184,26 @@
                     mINotificationManager.getImportance(sbn.getPackageName(), sbn.getUid());
         } catch (RemoteException e) {}
         mNotificationImportance = importance;
-        boolean systemApp = false;
+        boolean nonBlockable = false;
         try {
             final PackageInfo info =
                     pm.getPackageInfo(sbn.getPackageName(), PackageManager.GET_SIGNATURES);
-            systemApp = Utils.isSystemPackage(pm, info);
+            nonBlockable = Utils.isSystemPackage(getResources(), pm, info);
         } catch (PackageManager.NameNotFoundException e) {
             // unlikely.
         }
+        if (nonBlockablePkgs != null) {
+            nonBlockable |= nonBlockablePkgs.contains(sbn.getPackageName());
+        }
 
         final View importanceSlider = findViewById(R.id.importance_slider);
         final View importanceButtons = findViewById(R.id.importance_buttons);
         if (mShowSlider) {
-            bindSlider(importanceSlider, systemApp);
+            bindSlider(importanceSlider, nonBlockable);
             importanceSlider.setVisibility(View.VISIBLE);
             importanceButtons.setVisibility(View.GONE);
         } else {
-
-            bindToggles(importanceButtons, mStartingUserImportance, systemApp);
+            bindToggles(importanceButtons, mStartingUserImportance, nonBlockable);
             importanceButtons.setVisibility(View.VISIBLE);
             importanceSlider.setVisibility(View.GONE);
         }
@@ -239,7 +243,7 @@
     }
 
     private void bindToggles(final View importanceButtons, final int importance,
-            final boolean systemApp) {
+            final boolean nonBlockable) {
         ((RadioGroup) importanceButtons).setOnCheckedChangeListener(
                 new RadioGroup.OnCheckedChangeListener() {
                     @Override
@@ -250,7 +254,7 @@
         mBlock = (RadioButton) importanceButtons.findViewById(R.id.block_importance);
         mSilent = (RadioButton) importanceButtons.findViewById(R.id.silent_importance);
         mReset = (RadioButton) importanceButtons.findViewById(R.id.reset_importance);
-        if (systemApp) {
+        if (nonBlockable) {
             mBlock.setVisibility(View.GONE);
             mReset.setText(mContext.getString(R.string.do_not_silence));
         } else {
@@ -265,15 +269,15 @@
         }
     }
 
-    private void bindSlider(final View importanceSlider, final boolean systemApp) {
-        mActiveSliderTint = loadColorStateList(R.color.notification_guts_slider_color);
+    private void bindSlider(final View importanceSlider, final boolean nonBlockable) {
+        mActiveSliderTint = ColorStateList.valueOf(Utils.getColorAccent(mContext));
         mInactiveSliderTint = loadColorStateList(R.color.notification_guts_disabled_slider_color);
 
         mImportanceSummary = ((TextView) importanceSlider.findViewById(R.id.summary));
         mImportanceTitle = ((TextView) importanceSlider.findViewById(R.id.title));
         mSeekBar = (SeekBar) importanceSlider.findViewById(R.id.seekbar);
 
-        final int minProgress = systemApp ?
+        final int minProgress = nonBlockable ?
                 NotificationListenerService.Ranking.IMPORTANCE_MIN
                 : NotificationListenerService.Ranking.IMPORTANCE_NONE;
         mSeekBar.setMax(NotificationListenerService.Ranking.IMPORTANCE_MAX);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/RemoteInputController.java b/packages/SystemUI/src/com/android/systemui/statusbar/RemoteInputController.java
index 6cbacea..66cc15d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/RemoteInputController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/RemoteInputController.java
@@ -21,7 +21,9 @@
 import com.android.systemui.statusbar.policy.HeadsUpManager;
 import com.android.systemui.statusbar.policy.RemoteInputView;
 
+import android.util.ArrayMap;
 import android.util.ArraySet;
+import android.util.Pair;
 
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
@@ -31,8 +33,9 @@
  */
 public class RemoteInputController {
 
-    private final ArrayList<WeakReference<NotificationData.Entry>> mOpen = new ArrayList<>();
-    private final ArraySet<String> mSpinning = new ArraySet<>();
+    private final ArrayList<Pair<WeakReference<NotificationData.Entry>, Object>> mOpen
+            = new ArrayList<>();
+    private final ArrayMap<String, Object> mSpinning = new ArrayMap<>();
     private final ArrayList<Callback> mCallbacks = new ArrayList<>(3);
     private final HeadsUpManager mHeadsUpManager;
 
@@ -41,36 +44,72 @@
         mHeadsUpManager = headsUpManager;
     }
 
-    public void addRemoteInput(NotificationData.Entry entry) {
+    /**
+     * Adds a currently active remote input.
+     *
+     * @param entry the entry for which a remote input is now active.
+     * @param token a token identifying the view that is managing the remote input
+     */
+    public void addRemoteInput(NotificationData.Entry entry, Object token) {
         Preconditions.checkNotNull(entry);
+        Preconditions.checkNotNull(token);
 
         boolean found = pruneWeakThenRemoveAndContains(
-                entry /* contains */, null /* remove */);
+                entry /* contains */, null /* remove */, token /* removeToken */);
         if (!found) {
-            mOpen.add(new WeakReference<>(entry));
+            mOpen.add(new Pair<>(new WeakReference<>(entry), token));
         }
 
         apply(entry);
     }
 
-    public void removeRemoteInput(NotificationData.Entry entry) {
+    /**
+     * Removes a currently active remote input.
+     *
+     * @param entry the entry for which a remote input should be removed.
+     * @param token a token identifying the view that is requesting the removal. If non-null,
+     *              the entry is only removed if the token matches the last added token for this
+     *              entry. If null, the entry is removed regardless.
+     */
+    public void removeRemoteInput(NotificationData.Entry entry, Object token) {
         Preconditions.checkNotNull(entry);
 
-        pruneWeakThenRemoveAndContains(null /* contains */, entry /* remove */);
+        pruneWeakThenRemoveAndContains(null /* contains */, entry /* remove */, token);
 
         apply(entry);
     }
 
-    public void addSpinning(String key) {
-        mSpinning.add(key);
+    /**
+     * Adds a currently spinning (i.e. sending) remote input.
+     *
+     * @param key the key of the entry that's spinning.
+     * @param token the token of the view managing the remote input.
+     */
+    public void addSpinning(String key, Object token) {
+        Preconditions.checkNotNull(key);
+        Preconditions.checkNotNull(token);
+
+        mSpinning.put(key, token);
     }
 
-    public void removeSpinning(String key) {
-        mSpinning.remove(key);
+    /**
+     * Removes a currently spinning remote input.
+     *
+     * @param key the key of the entry for which a remote input should be removed.
+     * @param token a token identifying the view that is requesting the removal. If non-null,
+     *              the entry is only removed if the token matches the last added token for this
+     *              entry. If null, the entry is removed regardless.
+     */
+    public void removeSpinning(String key, Object token) {
+        Preconditions.checkNotNull(key);
+
+        if (token == null || mSpinning.get(key) == token) {
+            mSpinning.remove(key);
+        }
     }
 
     public boolean isSpinning(String key) {
-        return mSpinning.contains(key);
+        return mSpinning.containsKey(key);
     }
 
     private void apply(NotificationData.Entry entry) {
@@ -86,14 +125,16 @@
      * @return true if {@param entry} has an active RemoteInput
      */
     public boolean isRemoteInputActive(NotificationData.Entry entry) {
-        return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */);
+        return pruneWeakThenRemoveAndContains(entry /* contains */, null /* remove */,
+                null /* removeToken */);
     }
 
     /**
      * @return true if any entry has an active RemoteInput
      */
     public boolean isRemoteInputActive() {
-        pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */);
+        pruneWeakThenRemoveAndContains(null /* contains */, null /* remove */,
+                null /* removeToken */);
         return !mOpen.isEmpty();
     }
 
@@ -101,17 +142,27 @@
      * Prunes dangling weak references, removes entries referring to {@param remove} and returns
      * whether {@param contains} is part of the array in a single loop.
      * @param remove if non-null, removes this entry from the active remote inputs
+     * @param removeToken if non-null, only removes an entry if this matches the token when the
+     *                    entry was added.
      * @return true if {@param contains} is in the set of active remote inputs
      */
     private boolean pruneWeakThenRemoveAndContains(
-            NotificationData.Entry contains, NotificationData.Entry remove) {
+            NotificationData.Entry contains, NotificationData.Entry remove, Object removeToken) {
         boolean found = false;
         for (int i = mOpen.size() - 1; i >= 0; i--) {
-            NotificationData.Entry item = mOpen.get(i).get();
-            if (item == null || item == remove) {
+            NotificationData.Entry item = mOpen.get(i).first.get();
+            Object itemToken = mOpen.get(i).second;
+            boolean removeTokenMatches = (removeToken == null || itemToken == removeToken);
+
+            if (item == null || (item == remove && removeTokenMatches)) {
                 mOpen.remove(i);
             } else if (item == contains) {
-                found = true;
+                if (removeToken != null && removeToken != itemToken) {
+                    // We need to update the token. Remove here and let caller reinsert it.
+                    mOpen.remove(i);
+                } else {
+                    found = true;
+                }
             }
         }
         return found;
@@ -138,7 +189,7 @@
         // Make a copy because closing the remote inputs will modify mOpen.
         ArrayList<NotificationData.Entry> list = new ArrayList<>(mOpen.size());
         for (int i = mOpen.size() - 1; i >= 0; i--) {
-            NotificationData.Entry item = mOpen.get(i).get();
+            NotificationData.Entry item = mOpen.get(i).first.get();
             if (item != null && item.row != null) {
                 list.add(item);
             }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java b/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java
index dba7130..bfa43fd 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/ScrimView.java
@@ -42,6 +42,7 @@
     private float mViewAlpha = 1.0f;
     private ValueAnimator mAlphaAnimator;
     private Rect mExcludedRect = new Rect();
+    private int mLeftInset = 0;
     private boolean mHasExcludedArea;
     private ValueAnimator.AnimatorUpdateListener mAlphaUpdateListener
             = new ValueAnimator.AnimatorUpdateListener() {
@@ -87,12 +88,12 @@
                 if (mExcludedRect.top > 0) {
                     canvas.drawRect(0, 0, getWidth(), mExcludedRect.top, mPaint);
                 }
-                if (mExcludedRect.left > 0) {
-                    canvas.drawRect(0,  mExcludedRect.top, mExcludedRect.left, mExcludedRect.bottom,
-                            mPaint);
+                if (mExcludedRect.left + mLeftInset > 0) {
+                    canvas.drawRect(0,  mExcludedRect.top, mExcludedRect.left + mLeftInset,
+                            mExcludedRect.bottom, mPaint);
                 }
-                if (mExcludedRect.right < getWidth()) {
-                    canvas.drawRect(mExcludedRect.right,
+                if (mExcludedRect.right + mLeftInset < getWidth()) {
+                    canvas.drawRect(mExcludedRect.right + mLeftInset,
                             mExcludedRect.top,
                             getWidth(),
                             mExcludedRect.bottom,
@@ -183,4 +184,14 @@
     public void setChangeRunnable(Runnable changeRunnable) {
         mChangeRunnable = changeRunnable;
     }
+
+    public void setLeftInset(int leftInset) {
+        if (mLeftInset != leftInset) {
+            mLeftInset = leftInset;
+
+            if (mHasExcludedArea) {
+                invalidate();
+            }
+        }
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java b/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java
index 599e575a..74caa53 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/SignalClusterView.java
@@ -67,6 +67,8 @@
 
     private boolean mNoSimsVisible = false;
     private boolean mVpnVisible = false;
+    private int mVpnIconId = 0;
+    private int mLastVpnIconId = -1;
     private boolean mEthernetVisible = false;
     private int mEthernetIconId = 0;
     private int mLastEthernetIconId = -1;
@@ -164,6 +166,7 @@
         mSC = sc;
         mSC.addCallback(this);
         mVpnVisible = mSC.isVpnEnabled();
+        mVpnIconId = currentVpnIconId(mSC.isVpnBranded());
     }
 
     @Override
@@ -249,6 +252,7 @@
             @Override
             public void run() {
                 mVpnVisible = mSC.isVpnEnabled();
+                mVpnIconId = currentVpnIconId(mSC.isVpnBranded());
                 apply();
             }
         });
@@ -436,6 +440,15 @@
         if (mWifiGroup == null) return;
 
         mVpn.setVisibility(mVpnVisible ? View.VISIBLE : View.GONE);
+        if (mVpnVisible) {
+            if (mLastVpnIconId != mVpnIconId) {
+                setIconForView(mVpn, mVpnIconId);
+                mLastVpnIconId = mVpnIconId;
+            }
+            mVpn.setVisibility(View.VISIBLE);
+        } else {
+            mVpn.setVisibility(View.GONE);
+        }
         if (DEBUG) Log.d(TAG, String.format("vpn: %s", mVpnVisible ? "VISIBLE" : "GONE"));
 
         if (mEthernetVisible) {
@@ -564,6 +577,10 @@
         v.setImageTintList(ColorStateList.valueOf(tint));
     }
 
+    private int currentVpnIconId(boolean isBranded) {
+        return isBranded ? R.drawable.stat_sys_branded_vpn : R.drawable.stat_sys_vpn_ic;
+    }
+
     private class PhoneState {
         private final int mSubId;
         private boolean mMobileVisible = false;
@@ -685,4 +702,3 @@
         }
     }
 }
-
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/ViewTransformationHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/ViewTransformationHelper.java
index 1ff2b13..cd6c31f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/ViewTransformationHelper.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/ViewTransformationHelper.java
@@ -116,8 +116,7 @@
                     ownState.transformViewTo(otherState, transformationAmount);
                     otherState.recycle();
                 } else {
-                    // there's no other view available
-                    CrossFadeHelper.fadeOut(mTransformedViews.get(viewType), transformationAmount);
+                    ownState.disappear(transformationAmount, notification);
                 }
                 ownState.recycle();
             }
@@ -174,13 +173,7 @@
                     ownState.transformViewFrom(otherState, transformationAmount);
                     otherState.recycle();
                 } else {
-                    // There's no other view, lets fade us in
-                    // Certain views need to prepare the fade in and make sure its children are
-                    // completely visible. An example is the notification header.
-                    if (transformationAmount == 0.0f) {
-                        ownState.prepareFadeIn();
-                    }
-                    CrossFadeHelper.fadeIn(mTransformedViews.get(viewType), transformationAmount);
+                    ownState.appear(transformationAmount, notification);
                 }
                 ownState.recycle();
             }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/ImageTransformState.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/ImageTransformState.java
index 45027c0..e6b3fb8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/ImageTransformState.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/ImageTransformState.java
@@ -21,12 +21,17 @@
 import android.view.View;
 import android.widget.ImageView;
 
+import com.android.systemui.Interpolators;
 import com.android.systemui.R;
+import com.android.systemui.statusbar.CrossFadeHelper;
+import com.android.systemui.statusbar.TransformableView;
+import com.android.systemui.statusbar.stack.StackStateAnimator;
 
 /**
  * A transform state of a image view.
 */
 public class ImageTransformState extends TransformState {
+    public static final long ANIMATION_DURATION_LENGTH = 210;
 
     public static final int ICON_TAG = R.id.image_icon_tag;
     private static Pools.SimplePool<ImageTransformState> sInstancePool
@@ -49,6 +54,52 @@
         return super.sameAs(otherState);
     }
 
+    @Override
+    public void appear(float transformationAmount, TransformableView otherView) {
+        if (otherView instanceof HybridNotificationView) {
+            if (transformationAmount == 0.0f) {
+                mTransformedView.setPivotY(0);
+                mTransformedView.setPivotX(mTransformedView.getWidth() / 2);
+                prepareFadeIn();
+            }
+            transformationAmount = mapToDuration(transformationAmount);
+            CrossFadeHelper.fadeIn(mTransformedView, transformationAmount, false /* remap */);
+            transformationAmount = Interpolators.LINEAR_OUT_SLOW_IN.getInterpolation(
+                    transformationAmount);
+            mTransformedView.setScaleX(transformationAmount);
+            mTransformedView.setScaleY(transformationAmount);
+        } else {
+            super.appear(transformationAmount, otherView);
+        }
+    }
+
+    @Override
+    public void disappear(float transformationAmount, TransformableView otherView) {
+        if (otherView instanceof HybridNotificationView) {
+            if (transformationAmount == 0.0f) {
+                mTransformedView.setPivotY(0);
+                mTransformedView.setPivotX(mTransformedView.getWidth() / 2);
+            }
+            transformationAmount = mapToDuration(1.0f - transformationAmount);
+            CrossFadeHelper.fadeOut(mTransformedView, 1.0f - transformationAmount,
+                    false /* remap */);
+            transformationAmount = Interpolators.LINEAR_OUT_SLOW_IN.getInterpolation(
+                    transformationAmount);
+            mTransformedView.setScaleX(transformationAmount);
+            mTransformedView.setScaleY(transformationAmount);
+        } else {
+            super.disappear(transformationAmount, otherView);
+        }
+    }
+
+    private static float mapToDuration(float scaleAmount) {
+        // Assuming a linear interpolator, we can easily map it to our new duration
+        scaleAmount = (scaleAmount * StackStateAnimator.ANIMATION_DURATION_STANDARD
+                - (StackStateAnimator.ANIMATION_DURATION_STANDARD - ANIMATION_DURATION_LENGTH))
+                        / ANIMATION_DURATION_LENGTH;
+        return Math.max(Math.min(scaleAmount, 1.0f), 0.0f);
+    }
+
     public Icon getIcon() {
         return mIcon;
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationHeaderViewWrapper.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationHeaderViewWrapper.java
index 7794d5b..3e4c758 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationHeaderViewWrapper.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationHeaderViewWrapper.java
@@ -240,7 +240,11 @@
         // Also, the notification might have been modified during the animation, so background
         // might be null here.
         if (iconDrawable != null) {
-            iconDrawable.mutate().setColorFilter(mIconColorFilter);
+            Drawable d = iconDrawable.mutate();
+            // DrawableContainer ignores the color filter if it's already set, so clear it first to
+            // get it set and invalidated properly.
+            d.setColorFilter(null);
+            d.setColorFilter(mIconColorFilter);
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/TransformState.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/TransformState.java
index f0f5c8d..770ec95 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/TransformState.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/TransformState.java
@@ -30,6 +30,7 @@
 import com.android.systemui.R;
 import com.android.systemui.statusbar.CrossFadeHelper;
 import com.android.systemui.statusbar.ExpandableNotificationRow;
+import com.android.systemui.statusbar.TransformableView;
 import com.android.systemui.statusbar.ViewTransformationHelper;
 
 /**
@@ -357,6 +358,7 @@
 
     public int[] getLaidOutLocationOnScreen() {
         int[] location = getLocationOnScreen();
+        // remove translation
         location[0] -= mTransformedView.getTranslationX();
         location[1] -= mTransformedView.getTranslationY();
         return location;
@@ -364,6 +366,10 @@
 
     public int[] getLocationOnScreen() {
         mTransformedView.getLocationOnScreen(mOwnPosition);
+
+        // remove scale
+        mOwnPosition[0] -= (1.0f - mTransformedView.getScaleX()) * mTransformedView.getPivotX();
+        mOwnPosition[1] -= (1.0f - mTransformedView.getScaleY()) * mTransformedView.getPivotY();
         return mOwnPosition;
     }
 
@@ -371,6 +377,20 @@
         return false;
     }
 
+    public void appear(float transformationAmount, TransformableView otherView) {
+        // There's no other view, lets fade us in
+        // Certain views need to prepare the fade in and make sure its children are
+        // completely visible. An example is the notification header.
+        if (transformationAmount == 0.0f) {
+            prepareFadeIn();
+        }
+        CrossFadeHelper.fadeIn(mTransformedView, transformationAmount);
+    }
+
+    public void disappear(float transformationAmount, TransformableView otherView) {
+        CrossFadeHelper.fadeOut(mTransformedView, transformationAmount);
+    }
+
     public static TransformState createFrom(View view) {
         if (view instanceof TextView) {
             TextViewTransformState result = TextViewTransformState.obtain();
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/AutoTileManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/AutoTileManager.java
index 58fbd4c..b742479 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/AutoTileManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/AutoTileManager.java
@@ -24,7 +24,6 @@
 import com.android.systemui.statusbar.policy.DataSaverController.Listener;
 import com.android.systemui.statusbar.policy.HotspotController;
 import com.android.systemui.statusbar.policy.HotspotController.Callback;
-import com.android.systemui.statusbar.policy.NightModeController;
 
 /**
  * Manages which tiles should be automatically added to QS.
@@ -67,35 +66,12 @@
         if (!Prefs.getBoolean(context, Key.QS_WORK_ADDED, false)) {
             host.getManagedProfileController().addCallback(mProfileCallback);
         }
-        if (!Prefs.getBoolean(context, Key.QS_NIGHT_ADDED, false)) {
-            host.getNightModeController().addListener(mNightModeListener);
-        }
     }
 
     public void destroy() {
         // TODO: Remove any registered listeners.
     }
 
-    private final NightModeController.Listener mNightModeListener =
-            new NightModeController.Listener() {
-        @Override
-        public void onNightModeChanged() {
-            if (mHost.getNightModeController().isEnabled()) {
-                mHost.addTile("night");
-                Prefs.putBoolean(mContext, Key.QS_NIGHT_ADDED, true);
-                mHandler.post(new Runnable() {
-                    @Override
-                    public void run() {
-                        mHost.getNightModeController().removeListener(mNightModeListener);
-                    }
-                });
-            }
-        }
-
-        @Override
-        public void onTwilightAutoChanged() { }
-    };
-
     private final ManagedProfileController.Callback mProfileCallback =
             new ManagedProfileController.Callback() {
                 @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ButtonDispatcher.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ButtonDispatcher.java
index 30d24ff..95cb672 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ButtonDispatcher.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ButtonDispatcher.java
@@ -14,11 +14,10 @@
 
 package com.android.systemui.statusbar.phone;
 
+import android.annotation.DrawableRes;
+import android.annotation.Nullable;
 import android.graphics.drawable.Drawable;
 import android.view.View;
-import android.widget.ImageView;
-
-import com.android.systemui.statusbar.policy.KeyButtonView;
 
 import java.util.ArrayList;
 
@@ -50,6 +49,13 @@
         mViews.clear();
     }
 
+    void addView(View view, boolean landscape) {
+        addView(view);
+        if (view instanceof ButtonInterface) {
+            ((ButtonInterface) view).setLandscape(landscape);
+        }
+    }
+
     void addView(View view) {
         mViews.add(view);
         view.setOnClickListener(mClickListener);
@@ -65,9 +71,9 @@
             view.setVisibility(mVisibility);
         }
         if (mImageResource > 0) {
-            ((ImageView) view).setImageResource(mImageResource);
+            ((ButtonInterface) view).setImageResource(mImageResource);
         } else if (mImageDrawable != null) {
-            ((ImageView) view).setImageDrawable(mImageDrawable);
+            ((ButtonInterface) view).setImageDrawable(mImageDrawable);
         }
     }
 
@@ -88,7 +94,7 @@
         mImageResource = -1;
         final int N = mViews.size();
         for (int i = 0; i < N; i++) {
-            ((ImageView) mViews.get(i)).setImageDrawable(mImageDrawable);
+            ((ButtonInterface) mViews.get(i)).setImageDrawable(mImageDrawable);
         }
     }
 
@@ -97,7 +103,7 @@
         mImageDrawable = null;
         final int N = mViews.size();
         for (int i = 0; i < N; i++) {
-            ((ImageView) mViews.get(i)).setImageResource(mImageResource);
+            ((ButtonInterface) mViews.get(i)).setImageResource(mImageResource);
         }
     }
 
@@ -114,7 +120,7 @@
         // This seems to be an instantaneous thing, so not going to persist it.
         final int N = mViews.size();
         for (int i = 0; i < N; i++) {
-            ((KeyButtonView) mViews.get(i)).abortCurrentGesture();
+            ((ButtonInterface) mViews.get(i)).abortCurrentGesture();
         }
     }
 
@@ -158,6 +164,10 @@
         }
     }
 
+    public ArrayList<View> getViews() {
+        return mViews;
+    }
+
     public View getCurrentView() {
         return mCurrentView;
     }
@@ -165,4 +175,29 @@
     public void setCurrentView(View currentView) {
         mCurrentView = currentView.findViewById(mId);
     }
+
+    public void setCarMode(boolean carMode) {
+        final int N = mViews.size();
+        for (int i = 0; i < N; i++) {
+            final View view = mViews.get(i);
+            if (view instanceof ButtonInterface) {
+                ((ButtonInterface) view).setCarMode(carMode);
+            }
+        }
+    }
+
+    /**
+     * Interface for button actions.
+     */
+    public interface ButtonInterface {
+        void setImageResource(@DrawableRes int resId);
+
+        void setImageDrawable(@Nullable Drawable drawable);
+
+        void abortCurrentGesture();
+
+        void setLandscape(boolean landscape);
+
+        void setCarMode(boolean carMode);
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java
index 1d890d0..d5bf499 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeParameters.java
@@ -21,6 +21,7 @@
 import android.text.TextUtils;
 import android.util.Log;
 import android.util.MathUtils;
+import android.util.SparseBooleanArray;
 
 import com.android.systemui.R;
 
@@ -37,7 +38,7 @@
 
     private final Context mContext;
 
-    private static PulseSchedule sPulseSchedule;
+    private static IntInOutMatcher sPickupSubtypePerformsProxMatcher;
 
     public DozeParameters(Context context) {
         mContext = context;
@@ -58,10 +59,21 @@
         pw.print("    getVibrateOnPickup(): "); pw.println(getVibrateOnPickup());
         pw.print("    getProxCheckBeforePulse(): "); pw.println(getProxCheckBeforePulse());
         pw.print("    getPulseOnNotifications(): "); pw.println(getPulseOnNotifications());
-        pw.print("    getPulseSchedule(): "); pw.println(getPulseSchedule());
-        pw.print("    getPulseScheduleResets(): "); pw.println(getPulseScheduleResets());
         pw.print("    getPickupVibrationThreshold(): "); pw.println(getPickupVibrationThreshold());
-        pw.print("    getPickupPerformsProxCheck(): "); pw.println(getPickupPerformsProxCheck());
+        pw.print("    getPickupSubtypePerformsProxCheck(): ");pw.println(
+                dumpPickupSubtypePerformsProxCheck());
+    }
+
+    private String dumpPickupSubtypePerformsProxCheck() {
+        // Refresh sPickupSubtypePerformsProxMatcher
+        getPickupSubtypePerformsProxCheck(0);
+
+        if (sPickupSubtypePerformsProxMatcher == null) {
+            return "fallback: " + mContext.getResources().getBoolean(
+                    R.bool.doze_pickup_performs_proximity_check);
+        } else {
+            return "spec: " + sPickupSubtypePerformsProxMatcher.mSpec;
+        }
     }
 
     public boolean getDisplayStateSupported() {
@@ -72,8 +84,8 @@
         return getPulseInDuration(pickup) + getPulseVisibleDuration() + getPulseOutDuration();
     }
 
-    public int getPulseInDuration(boolean pickup) {
-        return pickup
+    public int getPulseInDuration(boolean pickupOrDoubleTap) {
+        return pickupOrDoubleTap
                 ? getInt("doze.pulse.duration.in.pickup", R.integer.doze_pulse_duration_in_pickup)
                 : getInt("doze.pulse.duration.in", R.integer.doze_pulse_duration_in);
     }
@@ -102,30 +114,18 @@
         return SystemProperties.getBoolean("doze.vibrate.pickup", false);
     }
 
+    public String getDoubleTapSensorType() {
+        return mContext.getString(R.string.doze_double_tap_sensor_type);
+    }
+
     public boolean getProxCheckBeforePulse() {
         return getBoolean("doze.pulse.proxcheck", R.bool.doze_proximity_check_before_pulse);
     }
 
-    public boolean getPickupPerformsProxCheck() {
-        return getBoolean("doze.pickup.proxcheck", R.bool.doze_pickup_performs_proximity_check);
-    }
-
     public boolean getPulseOnNotifications() {
         return getBoolean("doze.pulse.notifications", R.bool.doze_pulse_on_notifications);
     }
 
-    public PulseSchedule getPulseSchedule() {
-        final String spec = getString("doze.pulse.schedule", R.string.doze_pulse_schedule);
-        if (sPulseSchedule == null || !sPulseSchedule.mSpec.equals(spec)) {
-            sPulseSchedule = PulseSchedule.parse(spec);
-        }
-        return sPulseSchedule;
-    }
-
-    public int getPulseScheduleResets() {
-        return getInt("doze.pulse.schedule.resets", R.integer.doze_pulse_schedule_resets);
-    }
-
     public int getPickupVibrationThreshold() {
         return getInt("doze.pickup.vibration.threshold", R.integer.doze_pickup_vibration_threshold);
     }
@@ -143,43 +143,98 @@
         return SystemProperties.get(propName, mContext.getString(resId));
     }
 
-    public static class PulseSchedule {
-        private static final Pattern PATTERN = Pattern.compile("(\\d+?)s", 0);
+    public boolean getPickupSubtypePerformsProxCheck(int subType) {
+        String spec = getString("doze.pickup.proxcheck",
+                R.string.doze_pickup_subtype_performs_proximity_check);
 
-        private String mSpec;
-        private int[] mSchedule;
+        if (TextUtils.isEmpty(spec)) {
+            // Fall back to non-subtype based property.
+            return mContext.getResources().getBoolean(R.bool.doze_pickup_performs_proximity_check);
+        }
 
-        public static PulseSchedule parse(String spec) {
-            if (TextUtils.isEmpty(spec)) return null;
-            try {
-                final PulseSchedule rt = new PulseSchedule();
-                rt.mSpec = spec;
-                final String[] tokens = spec.split(",");
-                rt.mSchedule = new int[tokens.length];
-                for (int i = 0; i < tokens.length; i++) {
-                    final Matcher m = PATTERN.matcher(tokens[i]);
-                    if (!m.matches()) throw new IllegalArgumentException("Bad token: " + tokens[i]);
-                    rt.mSchedule[i] = Integer.parseInt(m.group(1));
+        if (sPickupSubtypePerformsProxMatcher == null
+                || !TextUtils.equals(spec, sPickupSubtypePerformsProxMatcher.mSpec)) {
+            sPickupSubtypePerformsProxMatcher = new IntInOutMatcher(spec);
+        }
+
+        return sPickupSubtypePerformsProxMatcher.isIn(subType);
+    }
+
+
+    /**
+     * Parses a spec of the form `1,2,3,!5,*`. The resulting object will match numbers that are
+     * listed, will not match numbers that are listed with a ! prefix, and will match / not match
+     * unlisted numbers depending on whether * or !* is present.
+     *
+     * *  -> match any numbers that are not explicitly listed
+     * !* -> don't match any numbers that are not explicitly listed
+     * 2  -> match 2
+     * !3 -> don't match 3
+     *
+     * It is illegal to specify:
+     * - an empty spec
+     * - a spec containing that are empty, or a lone !
+     * - a spec for anything other than numbers or *
+     * - multiple terms for the same number / multiple *s
+     */
+    public static class IntInOutMatcher {
+        private static final String WILDCARD = "*";
+        private static final char OUT_PREFIX = '!';
+
+        private final SparseBooleanArray mIsIn;
+        private final boolean mDefaultIsIn;
+        final String mSpec;
+
+        public IntInOutMatcher(String spec) {
+            if (TextUtils.isEmpty(spec)) {
+                throw new IllegalArgumentException("Spec must not be empty");
+            }
+
+            boolean defaultIsIn = false;
+            boolean foundWildcard = false;
+
+            mSpec = spec;
+            mIsIn = new SparseBooleanArray();
+
+            for (String itemPrefixed : spec.split(",", -1)) {
+                if (itemPrefixed.length() == 0) {
+                    throw new IllegalArgumentException(
+                            "Illegal spec, must not have zero-length items: `" + spec + "`");
                 }
-                if (DEBUG) Log.d(TAG, "Parsed spec [" + spec + "] as: " + rt);
-                return rt;
-            } catch (RuntimeException e) {
-                Log.w(TAG, "Error parsing spec: " + spec, e);
-                return null;
+                boolean isIn = itemPrefixed.charAt(0) != OUT_PREFIX;
+                String item = isIn ? itemPrefixed : itemPrefixed.substring(1);
+
+                if (itemPrefixed.length() == 0) {
+                    throw new IllegalArgumentException(
+                            "Illegal spec, must not have zero-length items: `" + spec + "`");
+                }
+
+                if (WILDCARD.equals(item)) {
+                    if (foundWildcard) {
+                        throw new IllegalArgumentException("Illegal spec, `" + WILDCARD +
+                                "` must not appear multiple times in `" + spec + "`");
+                    }
+                    defaultIsIn = isIn;
+                    foundWildcard = true;
+                } else {
+                    int key = Integer.parseInt(item);
+                    if (mIsIn.indexOfKey(key) >= 0) {
+                        throw new IllegalArgumentException("Illegal spec, `" + key +
+                                "` must not appear multiple times in `" + spec + "`");
+                    }
+                    mIsIn.put(key, isIn);
+                }
             }
+
+            if (!foundWildcard) {
+                throw new IllegalArgumentException("Illegal spec, must specify either * or !*");
+            }
+
+            mDefaultIsIn = defaultIsIn;
         }
 
-        @Override
-        public String toString() {
-            return Arrays.toString(mSchedule);
-        }
-
-        public long getNextTime(long now, long notificationTime) {
-            for (int i = 0; i < mSchedule.length; i++) {
-                final long time = notificationTime + mSchedule[i] * 1000;
-                if (time > now) return time;
-            }
-            return 0;
+        public boolean isIn(int value) {
+            return (mIsIn.get(value, mDefaultIsIn));
         }
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeScrimController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeScrimController.java
index 7d4515e..b44f5f7 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeScrimController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/DozeScrimController.java
@@ -109,10 +109,11 @@
 
     public void onScreenTurnedOn() {
         if (isPulsing()) {
-            final boolean pickup = mPulseReason == DozeLog.PULSE_REASON_SENSOR_PICKUP;
+            final boolean pickupOrDoubleTap = mPulseReason == DozeLog.PULSE_REASON_SENSOR_PICKUP
+                    || mPulseReason == DozeLog.PULSE_REASON_SENSOR_DOUBLE_TAP;
             startScrimAnimation(true /* inFront */, 0f,
-                    mDozeParameters.getPulseInDuration(pickup),
-                    pickup ? Interpolators.LINEAR_OUT_SLOW_IN : Interpolators.ALPHA_OUT,
+                    mDozeParameters.getPulseInDuration(pickupOrDoubleTap),
+                    pickupOrDoubleTap ? Interpolators.LINEAR_OUT_SLOW_IN : Interpolators.ALPHA_OUT,
                     mPulseInFinished);
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/FingerprintUnlockController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/FingerprintUnlockController.java
index 1c9d937..2c3e805 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/FingerprintUnlockController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/FingerprintUnlockController.java
@@ -20,6 +20,7 @@
 import android.os.Handler;
 import android.os.PowerManager;
 import android.os.SystemClock;
+import android.os.Trace;
 import android.util.Log;
 
 import com.android.keyguard.KeyguardConstants;
@@ -140,11 +141,14 @@
 
     @Override
     public void onFingerprintAcquired() {
+        Trace.beginSection("FingerprintUnlockController#onFingerprintAcquired");
         releaseFingerprintWakeLock();
         if (!mUpdateMonitor.isDeviceInteractive()) {
             mWakeLock = mPowerManager.newWakeLock(
                     PowerManager.PARTIAL_WAKE_LOCK, FINGERPRINT_WAKE_LOCK_NAME);
+            Trace.beginSection("acquiring wake-and-unlock");
             mWakeLock.acquire();
+            Trace.endSection();
             if (DEBUG_FP_WAKELOCK) {
                 Log.i(TAG, "fingerprint acquired, grabbing fp wakelock");
             }
@@ -159,12 +163,15 @@
                 mStatusBarWindowManager.setForceDozeBrightness(true);
             }
         }
+        Trace.endSection();
     }
 
     @Override
     public void onFingerprintAuthenticated(int userId) {
+        Trace.beginSection("FingerprintUnlockController#onFingerprintAuthenticated");
         if (mUpdateMonitor.isGoingToSleep()) {
             mPendingAuthenticatedUserId = userId;
+            Trace.endSection();
             return;
         }
         boolean wasDeviceInteractive = mUpdateMonitor.isDeviceInteractive();
@@ -175,25 +182,34 @@
             }
             mPowerManager.wakeUp(SystemClock.uptimeMillis(), "android.policy:FINGERPRINT");
         }
+        Trace.beginSection("release wake-and-unlock");
         releaseFingerprintWakeLock();
+        Trace.endSection();
         switch (mMode) {
             case MODE_DISMISS_BOUNCER:
+                Trace.beginSection("MODE_DISMISS");
                 mStatusBarKeyguardViewManager.notifyKeyguardAuthenticated(
                         false /* strongAuth */);
+                Trace.endSection();
                 break;
             case MODE_UNLOCK:
             case MODE_SHOW_BOUNCER:
+                Trace.beginSection("MODE_UNLOCK or MODE_SHOW_BOUNCER");
                 if (!wasDeviceInteractive) {
                     mStatusBarKeyguardViewManager.notifyDeviceWakeUpRequested();
                 }
                 mStatusBarKeyguardViewManager.animateCollapsePanels(
                         FINGERPRINT_COLLAPSE_SPEEDUP_FACTOR);
+                Trace.endSection();
                 break;
             case MODE_WAKE_AND_UNLOCK_PULSING:
+                Trace.beginSection("MODE_WAKE_AND_UNLOCK_PULSING");
                 mPhoneStatusBar.updateMediaMetaData(false /* metaDataChanged */, 
                         true /* allowEnterAnimation */);
                 // Fall through.
+                Trace.endSection();
             case MODE_WAKE_AND_UNLOCK:
+                Trace.beginSection("MODE_WAKE_AND_UNLOCK");
                 mStatusBarWindowManager.setStatusBarFocusable(false);
                 mDozeScrimController.abortPulsing();
                 mKeyguardViewMediator.onWakeAndUnlocking();
@@ -201,6 +217,7 @@
                 if (mPhoneStatusBar.getNavigationBarView() != null) {
                     mPhoneStatusBar.getNavigationBarView().setWakeAndUnlocking(true);
                 }
+                Trace.endSection();
                 break;
             case MODE_ONLY_WAKE:
             case MODE_NONE:
@@ -210,6 +227,7 @@
             mStatusBarWindowManager.setForceDozeBrightness(false);
         }
         mPhoneStatusBar.notifyFpAuthModeChanged();
+        Trace.endSection();
     }
 
     @Override
@@ -219,6 +237,7 @@
 
     @Override
     public void onFinishedGoingToSleep(int why) {
+        Trace.beginSection("FingerprintUnlockController#onFinishedGoingToSleep");
         if (mPendingAuthenticatedUserId != -1) {
 
             // Post this to make sure it's executed after the device is fully locked.
@@ -230,6 +249,7 @@
             });
         }
         mPendingAuthenticatedUserId = -1;
+        Trace.endSection();
     }
 
     public int getMode() {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java
index 772c766..4d0e5d3 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBottomAreaView.java
@@ -16,8 +16,12 @@
 
 package com.android.systemui.statusbar.phone;
 
+import static android.view.accessibility.AccessibilityNodeInfo.ACTION_CLICK;
+import static android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
+
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
+import android.app.ActivityOptions;
 import android.app.admin.DevicePolicyManager;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
@@ -44,6 +48,7 @@
 import android.util.TypedValue;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.WindowManager;
 import android.view.accessibility.AccessibilityNodeInfo;
 import android.widget.FrameLayout;
 import android.widget.TextView;
@@ -63,9 +68,6 @@
 import com.android.systemui.statusbar.policy.FlashlightController;
 import com.android.systemui.statusbar.policy.PreviewInflater;
 
-import static android.view.accessibility.AccessibilityNodeInfo.ACTION_CLICK;
-import static android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
-
 /**
  * Implementation for the bottom area of the Keyguard, including camera/phone affordance and status
  * text.
@@ -109,6 +111,7 @@
     private KeyguardIndicationController mIndicationController;
     private AccessibilityController mAccessibilityController;
     private PhoneStatusBar mPhoneStatusBar;
+    private KeyguardAffordanceHelper mAffordanceHelper;
 
     private boolean mUserSetupComplete;
     private boolean mPrewarmBound;
@@ -269,6 +272,10 @@
         updateCameraVisibility(); // in case onFinishInflate() was called too early
     }
 
+    public void setAffordanceHelper(KeyguardAffordanceHelper affordanceHelper) {
+        mAffordanceHelper = affordanceHelper;
+    }
+
     public void setUserSetupComplete(boolean userSetupComplete) {
         mUserSetupComplete = userSetupComplete;
         updateCameraVisibility();
@@ -401,7 +408,7 @@
     public void bindCameraPrewarmService() {
         Intent intent = getCameraIntent();
         ActivityInfo targetInfo = PreviewInflater.getTargetActivityInfo(mContext, intent,
-                KeyguardUpdateMonitor.getCurrentUser());
+                KeyguardUpdateMonitor.getCurrentUser(), true /* onlyDirectBootAware */);
         if (targetInfo != null && targetInfo.metaData != null) {
             String clazz = targetInfo.metaData.getString(
                     MediaStore.META_DATA_STILL_IMAGE_CAMERA_PREWARM_SERVICE);
@@ -448,12 +455,24 @@
                 @Override
                 public void run() {
                     int result = ActivityManager.START_CANCELED;
+
+                    // Normally an activity will set it's requested rotation
+                    // animation on its window. However when launching an activity
+                    // causes the orientation to change this is too late. In these cases
+                    // the default animation is used. This doesn't look good for
+                    // the camera (as it rotates the camera contents out of sync
+                    // with physical reality). So, we ask the WindowManager to
+                    // force the crossfade animation if an orientation change
+                    // happens to occur during the launch.
+                    ActivityOptions o = ActivityOptions.makeBasic();
+                    o.setRotationAnimationHint(
+                            WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS);
                     try {
                         result = ActivityManagerNative.getDefault().startActivityAsUser(
                                 null, getContext().getBasePackageName(),
                                 intent,
                                 intent.resolveTypeIfNeeded(getContext().getContentResolver()),
-                                null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, null,
+                                null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null, o.toBundle(),
                                 UserHandle.CURRENT.getIdentifier());
                     } catch (RemoteException e) {
                         Log.w(TAG, "Unable to start camera activity", e);
@@ -576,10 +595,19 @@
     }
 
     private void inflateCameraPreview() {
+        View previewBefore = mCameraPreview;
+        boolean visibleBefore = false;
+        if (previewBefore != null) {
+            mPreviewContainer.removeView(previewBefore);
+            visibleBefore = previewBefore.getVisibility() == View.VISIBLE;
+        }
         mCameraPreview = mPreviewInflater.inflatePreview(getCameraIntent());
         if (mCameraPreview != null) {
             mPreviewContainer.addView(mCameraPreview);
-            mCameraPreview.setVisibility(View.INVISIBLE);
+            mCameraPreview.setVisibility(visibleBefore ? View.VISIBLE : View.INVISIBLE);
+        }
+        if (mAffordanceHelper != null) {
+            mAffordanceHelper.updatePreviews();
         }
     }
 
@@ -598,6 +626,9 @@
             mPreviewContainer.addView(mLeftPreview);
             mLeftPreview.setVisibility(View.INVISIBLE);
         }
+        if (mAffordanceHelper != null) {
+            mAffordanceHelper.updatePreviews();
+        }
     }
 
     public void startFinishDozeAnimation() {
@@ -682,6 +713,13 @@
         public void onStrongAuthStateChanged(int userId) {
             mLockIcon.update();
         }
+
+        @Override
+        public void onUserUnlocked() {
+            inflateCameraPreview();
+            updateCameraVisibility();
+            updateLeftAffordance();
+        }
     };
 
     public void setKeyguardIndicationController(
@@ -698,4 +736,9 @@
         updateLeftAffordanceIcon();
         updateLeftPreview();
     }
+
+    public void onKeyguardShowingChanged() {
+        updateLeftAffordance();
+        inflateCameraPreview();
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java
index ee88b00..b3e86b5 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardBouncer.java
@@ -234,7 +234,6 @@
         mKeyguardView.setViewMediatorCallback(mCallback);
         mContainer.addView(mRoot, mContainer.getChildCount());
         mRoot.setVisibility(View.INVISIBLE);
-        mRoot.setSystemUiVisibility(View.STATUS_BAR_DISABLE_HOME);
     }
 
     protected void removeView() {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java
index 7db2870..41b0bb2 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarView.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.content.res.Configuration;
+import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.util.TypedValue;
@@ -56,8 +57,10 @@
 
     private BatteryController mBatteryController;
     private KeyguardUserSwitcher mKeyguardUserSwitcher;
+    private UserSwitcherController mUserSwitcherController;
 
     private int mSystemIconsSwitcherHiddenExpandedMargin;
+    private int mSystemIconsBaseMargin;
     private View mSystemIconsContainer;
 
     public KeyguardStatusBarView(Context context, AttributeSet attrs) {
@@ -136,8 +139,11 @@
     }
 
     private void loadDimens() {
-        mSystemIconsSwitcherHiddenExpandedMargin = getResources().getDimensionPixelSize(
+        Resources res = getResources();
+        mSystemIconsSwitcherHiddenExpandedMargin = res.getDimensionPixelSize(
                 R.dimen.system_icons_switcher_hidden_expanded_margin);
+        mSystemIconsBaseMargin = res.getDimensionPixelSize(
+                R.dimen.system_icons_super_container_avatarless_margin_end);
     }
 
     private void updateVisibilities() {
@@ -149,13 +155,29 @@
         } else if (mMultiUserSwitch.getParent() == this && mKeyguardUserSwitcherShowing) {
             removeView(mMultiUserSwitch);
         }
+        if (mKeyguardUserSwitcher == null) {
+            // If we have no keyguard switcher, the screen width is under 600dp. In this case,
+            // we don't show the multi-user avatar unless there is more than 1 user on the device.
+            if (mUserSwitcherController != null
+                    && mUserSwitcherController.getSwitchableUserCount() > 1) {
+                mMultiUserSwitch.setVisibility(View.VISIBLE);
+            } else {
+                mMultiUserSwitch.setVisibility(View.GONE);
+            }
+        }
         mBatteryLevel.setVisibility(mBatteryCharging ? View.VISIBLE : View.GONE);
     }
 
     private void updateSystemIconsLayoutParams() {
         RelativeLayout.LayoutParams lp =
                 (LayoutParams) mSystemIconsSuperContainer.getLayoutParams();
-        int marginEnd = mKeyguardUserSwitcherShowing ? mSystemIconsSwitcherHiddenExpandedMargin : 0;
+        // If the avatar icon is gone, we need to have some end margin to display the system icons
+        // correctly.
+        int baseMarginEnd = mMultiUserSwitch.getVisibility() == View.GONE
+                ? mSystemIconsBaseMargin
+                : 0;
+        int marginEnd = mKeyguardUserSwitcherShowing ? mSystemIconsSwitcherHiddenExpandedMargin :
+                baseMarginEnd;
         if (marginEnd != lp.getMarginEnd()) {
             lp.setMarginEnd(marginEnd);
             mSystemIconsSuperContainer.setLayoutParams(lp);
@@ -187,6 +209,7 @@
     }
 
     public void setUserSwitcherController(UserSwitcherController controller) {
+        mUserSwitcherController = controller;
         mMultiUserSwitch.setUserSwitcherController(controller);
     }
 
@@ -285,8 +308,12 @@
         super.setVisibility(visibility);
         if (visibility != View.VISIBLE) {
             mSystemIconsSuperContainer.animate().cancel();
+            mSystemIconsSuperContainer.setTranslationX(0);
             mMultiUserSwitch.animate().cancel();
             mMultiUserSwitch.setAlpha(1f);
+        } else {
+            updateVisibilities();
+            updateSystemIconsLayoutParams();
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/LightStatusBarController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/LightStatusBarController.java
index f98b9e5..df4566b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/LightStatusBarController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/LightStatusBarController.java
@@ -27,7 +27,7 @@
 /**
  * Controls how light status bar flag applies to the icons.
  */
-public class LightStatusBarController {
+public class LightStatusBarController implements BatteryController.BatteryStateChangeCallback {
 
     private final StatusBarIconController mIconController;
     private final BatteryController mBatteryController;
@@ -37,6 +37,7 @@
     private int mDockedStackVisibility;
     private boolean mFullscreenLight;
     private boolean mDockedLight;
+    private int mLastStatusBarMode;
 
     private final Rect mLastFullscreenBounds = new Rect();
     private final Rect mLastDockedBounds = new Rect();
@@ -45,6 +46,7 @@
             BatteryController batteryController) {
         mIconController = iconController;
         mBatteryController = batteryController;
+        batteryController.addStateChangedCallback(this);
     }
 
     public void setFingerprintUnlockController(
@@ -73,6 +75,7 @@
         }
         mFullscreenStackVisibility = newFullscreen;
         mDockedStackVisibility = newDocked;
+        mLastStatusBarMode = statusBarMode;
         mLastFullscreenBounds.set(fullscreenStackBounds);
         mLastDockedBounds.set(dockedStackBounds);
     }
@@ -123,4 +126,16 @@
             mIconController.setIconsDark(true, animateChange());
         }
     }
+
+    @Override
+    public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
+
+    }
+
+    @Override
+    public void onPowerSaveChanged(boolean isPowerSave) {
+        onSystemUiVisibilityChanged(mFullscreenStackVisibility, mDockedStackVisibility,
+                0 /* mask */, mLastFullscreenBounds, mLastDockedBounds, true /* sbModeChange*/,
+                mLastStatusBarMode);
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockIcon.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockIcon.java
index ab5ee93..9bb4936 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockIcon.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockIcon.java
@@ -168,6 +168,7 @@
             setContentDescription(contentDescription);
             mHasFingerPrintIcon = anyFingerprintIcon;
             if (animation != null && isAnim) {
+                animation.forceAnimationOnUI();
                 animation.start();
             }
             mLastState = state;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockscreenWallpaper.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockscreenWallpaper.java
index ef19d95..2be47e4 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockscreenWallpaper.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/LockscreenWallpaper.java
@@ -26,6 +26,7 @@
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Rect;
+import android.graphics.Xfermode;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.DrawableWrapper;
@@ -37,6 +38,8 @@
 import android.os.UserHandle;
 import android.util.Log;
 
+import com.android.keyguard.KeyguardUpdateMonitor;
+
 import libcore.io.IoUtils;
 
 import java.util.Objects;
@@ -51,6 +54,7 @@
     private final PhoneStatusBar mBar;
     private final WallpaperManager mWallpaperManager;
     private final Handler mH;
+    private final KeyguardUpdateMonitor mUpdateMonitor;
 
     private boolean mCached;
     private Bitmap mCache;
@@ -65,6 +69,7 @@
         mH = h;
         mWallpaperManager = (WallpaperManager) ctx.getSystemService(Context.WALLPAPER_SERVICE);
         mCurrentUserId = ActivityManager.getCurrentUser();
+        mUpdateMonitor = KeyguardUpdateMonitor.getInstance(ctx);
 
         IWallpaperManager service = IWallpaperManager.Stub.asInterface(
                 ServiceManager.getService(Context.WALLPAPER_SERVICE));
@@ -88,6 +93,7 @@
         LoaderResult result = loadBitmap(mCurrentUserId, mSelectedUser);
         if (result.success) {
             mCached = true;
+            mUpdateMonitor.setHasLockscreenWallpaper(result.bitmap != null);
             mCache = result.bitmap;
         }
         return mCache;
@@ -180,6 +186,7 @@
                 if (result.success) {
                     mCached = true;
                     mCache = result.bitmap;
+                    mUpdateMonitor.setHasLockscreenWallpaper(result.bitmap != null);
                     mBar.updateMediaMetaData(
                             true /* metaDataChanged */, true /* allowEnterAnimation */);
                 }
@@ -224,6 +231,12 @@
         }
 
         @Override
+        public void setXfermode(@Nullable Xfermode mode) {
+            // DrawableWrapper does not call this for us.
+            getDrawable().setXfermode(mode);
+        }
+
+        @Override
         public int getIntrinsicWidth() {
             return -1;
         }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java
index 2bee816..06c8b68 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarInflaterView.java
@@ -26,6 +26,7 @@
 import android.widget.FrameLayout;
 import android.widget.LinearLayout;
 import android.widget.Space;
+
 import com.android.systemui.R;
 import com.android.systemui.statusbar.policy.KeyButtonView;
 import com.android.systemui.tuner.TunerService;
@@ -69,6 +70,8 @@
     private View mLastRot0;
     private View mLastRot90;
 
+    private boolean mAlternativeOrder;
+
     public NavigationBarInflaterView(Context context, AttributeSet attrs) {
         super(context, attrs);
         mDensity = context.getResources().getConfiguration().densityDpi;
@@ -112,6 +115,7 @@
                 false);
         mRot90.setId(R.id.rot90);
         addView(mRot90);
+        updateAlternativeOrder();
         if (getParent() instanceof NavigationBarView) {
             ((NavigationBarView) getParent()).updateRotatedViews();
         }
@@ -150,6 +154,26 @@
         }
     }
 
+    public void setAlternativeOrder(boolean alternativeOrder) {
+        if (alternativeOrder != mAlternativeOrder) {
+            mAlternativeOrder = alternativeOrder;
+            updateAlternativeOrder();
+        }
+    }
+
+    private void updateAlternativeOrder() {
+        updateAlternativeOrder(mRot0.findViewById(R.id.ends_group));
+        updateAlternativeOrder(mRot0.findViewById(R.id.center_group));
+        updateAlternativeOrder(mRot90.findViewById(R.id.ends_group));
+        updateAlternativeOrder(mRot90.findViewById(R.id.center_group));
+    }
+
+    private void updateAlternativeOrder(View v) {
+        if (v instanceof ReverseLinearLayout) {
+            ((ReverseLinearLayout) v).setAlternativeOrder(mAlternativeOrder);
+        }
+    }
+
     private void initiallyFill(ButtonDispatcher buttonDispatcher) {
         addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.ends_group));
         addAll(buttonDispatcher, (ViewGroup) mRot0.findViewById(R.id.center_group));
@@ -256,7 +280,7 @@
             params.width = (int) (params.width * size);
         }
         parent.addView(v);
-        addToDispatchers(v);
+        addToDispatchers(v, landscape);
         View lastView = landscape ? mLastRot90 : mLastRot0;
         if (lastView != null) {
             v.setAccessibilityTraversalAfter(lastView.getId());
@@ -303,16 +327,16 @@
         return buttonSpec.substring(0, buttonSpec.indexOf(SIZE_MOD_START));
     }
 
-    private void addToDispatchers(View v) {
+    private void addToDispatchers(View v, boolean landscape) {
         if (mButtonDispatchers != null) {
             final int indexOfKey = mButtonDispatchers.indexOfKey(v.getId());
             if (indexOfKey >= 0) {
-                mButtonDispatchers.valueAt(indexOfKey).addView(v);
+                mButtonDispatchers.valueAt(indexOfKey).addView(v, landscape);
             } else if (v instanceof ViewGroup) {
                 final ViewGroup viewGroup = (ViewGroup)v;
                 final int N = viewGroup.getChildCount();
                 for (int i = 0; i < N; i++) {
-                    addToDispatchers(viewGroup.getChildAt(i));
+                    addToDispatchers(viewGroup.getChildAt(i), landscape);
                 }
             }
         }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
index 5fab796..0bc70b5 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -54,17 +54,20 @@
 
 public class NavigationBarView extends LinearLayout {
     final static boolean DEBUG = false;
-    final static String TAG = "PhoneStatusBar/NavigationBarView";
+    final static String TAG = "StatusBar/NavBarView";
 
     // slippery nav bar when everything is disabled, e.g. during setup
     final static boolean SLIPPERY_WHEN_DISABLED = true;
 
+    final static boolean ALTERNATE_CAR_MODE_UI = false;
+
     final Display mDisplay;
     View mCurrentView = null;
     View[] mRotatedViews = new View[4];
 
     boolean mVertical;
     boolean mScreenOn;
+    private int mCurrentRotation = -1;
 
     boolean mShowMenu;
     int mDisabledFlags = 0;
@@ -93,12 +96,15 @@
     private OnVerticalChangedListener mOnVerticalChangedListener;
     private boolean mLayoutTransitionsEnabled = true;
     private boolean mWakeAndUnlocking;
-    private boolean mCarMode = false;
+    private boolean mUseCarModeUi = false;
+    private boolean mInCarMode = false;
     private boolean mDockedStackExists;
 
     private final SparseArray<ButtonDispatcher> mButtonDisatchers = new SparseArray<>();
     private Configuration mConfiguration;
 
+    private NavigationBarInflaterView mNavigationInflaterView;
+
     private class NavTransitionListener implements TransitionListener {
         private boolean mBackTransitioning;
         private boolean mHomeAppearing;
@@ -287,7 +293,9 @@
             mMenuIcon = ctx.getDrawable(R.drawable.ic_sysbar_menu);
             mImeIcon = ctx.getDrawable(R.drawable.ic_ime_switcher_default);
 
-            updateCarModeIcons(ctx);
+            if (ALTERNATE_CAR_MODE_UI) {
+                updateCarModeIcons(ctx);
+            }
         }
     }
 
@@ -338,14 +346,14 @@
         // carmode, respectively. Recents are not available in CarMode in nav bar so change
         // to recent icon is not required.
         Drawable backIcon = (backAlt)
-                ? getBackIconWithAlt(mCarMode, mVertical)
-                : getBackIcon(mCarMode, mVertical);
+                ? getBackIconWithAlt(mUseCarModeUi, mVertical)
+                : getBackIcon(mUseCarModeUi, mVertical);
 
         getBackButton().setImageDrawable(backIcon);
 
         updateRecentsIcon();
 
-        if (mCarMode) {
+        if (mUseCarModeUi) {
             getHomeButton().setImageDrawable(mHomeCarModeIcon);
         } else {
             getHomeButton().setImageDrawable(mHomeDefaultIcon);
@@ -373,17 +381,13 @@
 
         final boolean disableHome = ((disabledFlags & View.STATUS_BAR_DISABLE_HOME) != 0);
 
-        // Disable recents always in car mode.
-        boolean disableRecent = (
-                mCarMode || (disabledFlags & View.STATUS_BAR_DISABLE_RECENT) != 0);
+        // Always disable recents when alternate car mode UI is active.
+        boolean disableRecent = mUseCarModeUi
+                        || ((disabledFlags & View.STATUS_BAR_DISABLE_RECENT) != 0);
         final boolean disableBack = ((disabledFlags & View.STATUS_BAR_DISABLE_BACK) != 0)
                 && ((mNavigationIconHints & StatusBarManager.NAVIGATION_HINT_BACK_ALT) == 0);
         final boolean disableSearch = ((disabledFlags & View.STATUS_BAR_DISABLE_SEARCH) != 0);
 
-        if (SLIPPERY_WHEN_DISABLED) {
-            setSlippery(disableHome && disableRecent && disableBack && disableSearch);
-        }
-
         ViewGroup navButtons = (ViewGroup) getCurrentView().findViewById(R.id.nav_buttons);
         if (navButtons != null) {
             LayoutTransition lt = navButtons.getLayoutTransition();
@@ -458,22 +462,6 @@
         }
     }
 
-    public void setSlippery(boolean newSlippery) {
-        WindowManager.LayoutParams lp = (WindowManager.LayoutParams) getLayoutParams();
-        if (lp != null) {
-            boolean oldSlippery = (lp.flags & WindowManager.LayoutParams.FLAG_SLIPPERY) != 0;
-            if (!oldSlippery && newSlippery) {
-                lp.flags |= WindowManager.LayoutParams.FLAG_SLIPPERY;
-            } else if (oldSlippery && !newSlippery) {
-                lp.flags &= ~WindowManager.LayoutParams.FLAG_SLIPPERY;
-            } else {
-                return;
-            }
-            WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
-            wm.updateViewLayout(this, lp);
-        }
-    }
-
     public void setMenuVisibility(final boolean show) {
         setMenuVisibility(show, false);
     }
@@ -492,9 +480,10 @@
 
     @Override
     public void onFinishInflate() {
+        mNavigationInflaterView = (NavigationBarInflaterView) findViewById(
+                R.id.navigation_inflater);
         updateRotatedViews();
-        ((NavigationBarInflaterView) findViewById(R.id.navigation_inflater)).setButtonDispatchers(
-                mButtonDisatchers);
+        mNavigationInflaterView.setButtonDispatchers(mButtonDisatchers);
 
         getImeSwitchButton().setOnClickListener(mImeSwitcherClickListener);
 
@@ -543,6 +532,10 @@
         updateCurrentView();
     }
 
+    public boolean needsReorient(int rotation) {
+        return mCurrentRotation != rotation;
+    }
+
     private void updateCurrentView() {
         final int rot = mDisplay.getRotation();
         for (int i=0; i<4; i++) {
@@ -550,10 +543,12 @@
         }
         mCurrentView = mRotatedViews[rot];
         mCurrentView.setVisibility(View.VISIBLE);
+        mNavigationInflaterView.setAlternativeOrder(rot == Surface.ROTATION_90);
         for (int i = 0; i < mButtonDisatchers.size(); i++) {
             mButtonDisatchers.valueAt(i).setCurrentView(mCurrentView);
         }
         updateLayoutTransitionsEnabled();
+        mCurrentRotation = rot;
     }
 
     private void updateRecentsIcon() {
@@ -577,7 +572,7 @@
         setMenuVisibility(mShowMenu, true /* force */);
 
         if (DEBUG) {
-            Log.d(TAG, "reorient(): rot=" + mDisplay.getRotation());
+            Log.d(TAG, "reorient(): rot=" + mCurrentRotation);
         }
 
         updateTaskSwitchHelper();
@@ -633,12 +628,19 @@
         boolean uiCarModeChanged = false;
         if (newConfig != null) {
             int uiMode = newConfig.uiMode & Configuration.UI_MODE_TYPE_MASK;
-            if (mCarMode && uiMode != Configuration.UI_MODE_TYPE_CAR) {
-                mCarMode = false;
-                uiCarModeChanged = true;
-            } else if (uiMode == Configuration.UI_MODE_TYPE_CAR) {
-                mCarMode = true;
-                uiCarModeChanged = true;
+            final boolean isCarMode = (uiMode == Configuration.UI_MODE_TYPE_CAR);
+
+            if (isCarMode != mInCarMode) {
+                mInCarMode = isCarMode;
+                getHomeButton().setCarMode(isCarMode);
+
+                if (ALTERNATE_CAR_MODE_UI) {
+                    mUseCarModeUi = isCarMode;
+                    uiCarModeChanged = true;
+                } else {
+                    // Don't use car mode behavior if ALTERNATE_CAR_MODE_UI not set.
+                    mUseCarModeUi = false;
+                }
             }
         }
         return uiCarModeChanged;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java
index 204ab7e..2c8339a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationGroupManager.java
@@ -31,7 +31,6 @@
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Objects;
 
 /**
  * A class to handle notifications and their corresponding groups.
@@ -43,6 +42,7 @@
     private int mBarState = -1;
     private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
     private HeadsUpManager mHeadsUpManager;
+    private boolean mIsUpdatingUnchangedGroup;
 
     public void setOnGroupChangeListener(OnGroupChangeListener listener) {
         mListener = listener;
@@ -140,15 +140,6 @@
         }
     }
 
-    public void onEntryBundlingUpdated(final NotificationData.Entry updated,
-            final String overrideGroupKey) {
-        final StatusBarNotification oldSbn = updated.notification.clone();
-        if (!Objects.equals(oldSbn.getOverrideGroupKey(), overrideGroupKey)) {
-            updated.notification.setOverrideGroupKey(overrideGroupKey);
-            onEntryUpdated(updated, oldSbn);
-        }
-    }
-
     private void updateSuppression(NotificationGroup group) {
         if (group == null) {
             return;
@@ -163,7 +154,9 @@
             if (group.suppressed) {
                 handleSuppressedSummaryHeadsUpped(group.summary);
             }
-            mListener.onGroupsChanged();
+            if (!mIsUpdatingUnchangedGroup) {
+                mListener.onGroupsChanged();
+            }
         }
     }
 
@@ -192,19 +185,24 @@
 
     public void onEntryUpdated(NotificationData.Entry entry,
             StatusBarNotification oldNotification) {
+        String oldKey = oldNotification.getGroupKey();
+        String newKey = entry.notification.getGroupKey();
+        boolean groupKeysChanged = !oldKey.equals(newKey);
+        boolean wasGroupChild = isGroupChild(oldNotification);
+        boolean isGroupChild = isGroupChild(entry.notification);
+        mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
         if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
             onEntryRemovedInternal(entry, oldNotification);
         }
         onEntryAdded(entry);
+        mIsUpdatingUnchangedGroup = false;
         if (isIsolated(entry.notification)) {
             mIsolatedEntries.put(entry.key, entry.notification);
-            String oldKey = oldNotification.getGroupKey();
-            String newKey = entry.notification.getGroupKey();
-            if (!oldKey.equals(newKey)) {
+            if (groupKeysChanged) {
                 updateSuppression(mGroupMap.get(oldKey));
                 updateSuppression(mGroupMap.get(newKey));
             }
-        } else if (!isGroupChild(oldNotification) && isGroupChild(entry.notification)) {
+        } else if (!wasGroupChild && isGroupChild) {
             onEntryBecomingChild(entry);
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
index 5064d8e..1cf7a23 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
@@ -58,6 +58,7 @@
 import com.android.systemui.statusbar.KeyguardAffordanceView;
 import com.android.systemui.statusbar.NotificationData;
 import com.android.systemui.statusbar.StatusBarState;
+import com.android.systemui.statusbar.notification.NotificationUtils;
 import com.android.systemui.statusbar.policy.HeadsUpManager;
 import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
 import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
@@ -78,11 +79,10 @@
     private static final int CAP_HEIGHT = 1456;
     private static final int FONT_HEIGHT = 2163;
 
-    private static final float HEADER_RUBBERBAND_FACTOR = 2.05f;
     private static final float LOCK_ICON_ACTIVE_SCALE = 1.2f;
 
-    private static final String COUNTER_PANEL_OPEN = "panel_open";
-    private static final String COUNTER_PANEL_OPEN_QS = "panel_open_qs";
+    static final String COUNTER_PANEL_OPEN = "panel_open";
+    static final String COUNTER_PANEL_OPEN_QS = "panel_open_qs";
     private static final String COUNTER_PANEL_OPEN_PEEK = "panel_open_peek";
 
     private static final Rect mDummyDirtyRect = new Rect(0, 0, 1, 1);
@@ -189,6 +189,7 @@
     private boolean mExpandingFromHeadsUp;
     private boolean mCollapsedOnDown;
     private int mPositionMinSideMargin;
+    private int mMaxFadeoutHeight;
     private int mLastOrientation = -1;
     private boolean mClosingWithAlphaFadeOut;
     private boolean mHeadsUpAnimatingAway;
@@ -232,6 +233,7 @@
         mKeyguardBottomArea = (KeyguardBottomAreaView) findViewById(R.id.keyguard_bottom_area);
         mQsNavbarScrim = findViewById(R.id.qs_navbar_scrim);
         mAfforanceHelper = new KeyguardAffordanceHelper(this, getContext());
+        mKeyguardBottomArea.setAffordanceHelper(mAfforanceHelper);
         mLastOrientation = getResources().getConfiguration().orientation;
 
         mQsAutoReinflateContainer =
@@ -278,6 +280,8 @@
                 R.dimen.qs_falsing_threshold);
         mPositionMinSideMargin = getResources().getDimensionPixelSize(
                 R.dimen.notification_panel_min_side_margin);
+        mMaxFadeoutHeight = getResources().getDimensionPixelSize(
+                R.dimen.max_notification_fadeout_height);
     }
 
     public void updateResources() {
@@ -325,7 +329,7 @@
         } else if (!mQsExpanded) {
             setQsExpansion(mQsMinExpansionHeight + mLastOverscroll);
         }
-        updateStackHeight(getExpandedHeight());
+        updateExpandedHeight(getExpandedHeight());
         updateHeader();
 
         // If we are running a size change animation, the animation takes care of the height of
@@ -373,10 +377,7 @@
         boolean animate = mNotificationStackScroller.isAddOrRemoveAnimationPending();
         int stackScrollerPadding;
         if (mStatusBarState != StatusBarState.KEYGUARD) {
-            int bottom = mQsContainer.getHeader().getHeight();
-            stackScrollerPadding = mStatusBarState == StatusBarState.SHADE
-                    ? bottom + mQsPeekHeight
-                    : mKeyguardStatusBar.getHeight();
+            stackScrollerPadding = mQsContainer.getHeader().getHeight() + mQsPeekHeight;
             mTopPaddingAdjustment = 0;
         } else {
             mClockPositionAlgorithm.setup(
@@ -552,7 +553,9 @@
     protected void flingToHeight(float vel, boolean expand, float target,
             float collapseSpeedUpFactor, boolean expandBecauseOfFalsing) {
         mHeadsUpTouchHelper.notifyFling(!expand);
-        setClosingWithAlphaFadeout(!expand && getFadeoutAlpha() == 1.0f);
+        setClosingWithAlphaFadeout(!expand
+                && mNotificationStackScroller.getFirstChildIntrinsicHeight() <= mMaxFadeoutHeight
+                && getFadeoutAlpha() == 1.0f);
         super.flingToHeight(vel, expand, target, collapseSpeedUpFactor, expandBecauseOfFalsing);
     }
 
@@ -1013,8 +1016,7 @@
             mKeyguardStatusBar.setAlpha(1f);
             mKeyguardStatusBar.setVisibility(keyguardShowing ? View.VISIBLE : View.INVISIBLE);
             if (keyguardShowing && oldState != mStatusBarState) {
-                mKeyguardBottomArea.updateLeftAffordance();
-                mAfforanceHelper.updatePreviews();
+                mKeyguardBottomArea.onKeyguardShowingChanged();
             }
         }
         if (keyguardShowing) {
@@ -1161,6 +1163,7 @@
 
     private void updateQsState() {
         mQsContainer.setExpanded(mQsExpanded);
+        mNotificationStackScroller.setQsExpanded(mQsExpanded);
         mNotificationStackScroller.setScrollingEnabled(
                 mStatusBarState != StatusBarState.KEYGUARD && (!mQsExpanded
                         || mQsExpansionFromOverscroll));
@@ -1296,7 +1299,7 @@
         }
     }
 
-    private void flingSettings(float vel, boolean expand) {
+    public void flingSettings(float vel, boolean expand) {
         flingSettings(vel, expand, null, false /* isClick */);
     }
 
@@ -1376,8 +1379,7 @@
         int min = mStatusBarMinHeight;
         if (mStatusBar.getBarState() != StatusBarState.KEYGUARD
                 && mNotificationStackScroller.getNotGoneChildCount() == 0) {
-            int minHeight = (int) ((mQsMinExpansionHeight + getOverExpansionAmount())
-                    * HEADER_RUBBERBAND_FACTOR);
+            int minHeight = (int) (mQsMinExpansionHeight + getOverExpansionAmount());
             min = Math.max(min, minHeight);
         }
         int maxHeight;
@@ -1390,10 +1392,14 @@
         return maxHeight;
     }
 
-    private boolean isInSettings() {
+    public boolean isInSettings() {
         return mQsExpanded;
     }
 
+    public boolean isExpanding() {
+        return mIsExpanding;
+    }
+
     @Override
     protected void onHeightUpdated(float expandedHeight) {
         if (!mQsExpanded || mQsExpandImmediate || mIsExpanding && mQsExpandedWhenExpandingStarted) {
@@ -1419,7 +1425,7 @@
             setQsExpansion(mQsMinExpansionHeight
                     + t * (getTempQsMaxExpansion() - mQsMinExpansionHeight));
         }
-        updateStackHeight(expandedHeight);
+        updateExpandedHeight(expandedHeight);
         updateHeader();
         updateUnlockIcon();
         updateNotificationTranslucency();
@@ -1479,7 +1485,7 @@
                 maxQsHeight, mStatusBarState == StatusBarState.KEYGUARD
                         ? mClockPositionResult.stackScrollerPadding - mTopPaddingAdjustment
                         : 0)
-                + notificationHeight;
+                + notificationHeight + mNotificationStackScroller.getTopPaddingOverflow();
         if (totalHeight > mNotificationStackScroller.getHeight()) {
             float fullyCollapsedHeight = maxQsHeight
                     + mNotificationStackScroller.getLayoutMinHeight();
@@ -1548,15 +1554,8 @@
         if (mStatusBar.getBarState() == StatusBarState.KEYGUARD) {
             return 0;
         }
-        if (mNotificationStackScroller.getNotGoneChildCount() == 0) {
-            return Math.min(0, mExpandedHeight / HEADER_RUBBERBAND_FACTOR - mQsMinExpansionHeight);
-        }
-        float stackTranslation = mNotificationStackScroller.getStackTranslation();
-        float translation = stackTranslation / HEADER_RUBBERBAND_FACTOR;
-        if (mHeadsUpManager.hasPinnedHeadsUp() || mIsExpansionFromHeadsUp) {
-            translation = mNotificationStackScroller.getTopPadding() + stackTranslation
-                    - mQsMinExpansionHeight;
-        }
+        float translation = NotificationUtils.interpolate(-mQsMinExpansionHeight, 0,
+                mNotificationStackScroller.getAppearFraction(mExpandedHeight));
         return Math.min(0, translation);
     }
 
@@ -1616,6 +1615,9 @@
         if (mQsExpanded) {
             onQsExpansionStarted();
         }
+        // Since there are QS tiles in the header now, we need to make sure we start listening
+        // immediately so they can be up to date.
+        mQsContainer.setHeaderListening(true);
     }
 
     @Override
@@ -1726,6 +1728,14 @@
         if (view == null && mQsExpanded) {
             return;
         }
+        ExpandableView firstChildNotGone = mNotificationStackScroller.getFirstChildNotGone();
+        ExpandableNotificationRow firstRow = firstChildNotGone instanceof ExpandableNotificationRow
+                ? (ExpandableNotificationRow) firstChildNotGone
+                : null;
+        if (firstRow != null
+                && (view == firstRow || (firstRow.getNotificationParent() == firstRow))) {
+            requestScrollerTopPaddingUpdate(false);
+        }
         requestPanelHeightUpdate();
     }
 
@@ -1964,7 +1974,7 @@
         if (mNotificationStackScroller.getNotGoneChildCount() > 0) {
             return mNotificationStackScroller.getPeekHeight();
         } else {
-            return mQsMinExpansionHeight * HEADER_RUBBERBAND_FACTOR;
+            return mQsMinExpansionHeight;
         }
     }
 
@@ -2108,6 +2118,7 @@
         onEmptySpaceClick(x);
     }
 
+    @Override
     protected boolean onMiddleClicked() {
         switch (mStatusBar.getBarState()) {
             case StatusBarState.KEYGUARD:
@@ -2244,8 +2255,8 @@
         mQsAutoReinflateContainer.setTranslationX(translation);
     }
 
-    protected void updateStackHeight(float stackHeight) {
-        mNotificationStackScroller.setStackHeight(stackHeight);
+    protected void updateExpandedHeight(float expandedHeight) {
+        mNotificationStackScroller.setExpandedHeight(expandedHeight);
         updateKeyguardBottomAreaAlpha();
     }
 
@@ -2257,6 +2268,7 @@
         mStatusBar.clearNotificationEffects();
     }
 
+    @Override
     protected boolean isPanelVisibleBecauseOfHeadsUp() {
         return mHeadsUpManager.hasPinnedHeadsUp() || mHeadsUpAnimatingAway;
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
index e4aa103..a6a5742 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PanelView.java
@@ -92,6 +92,7 @@
      * Whether an instant expand request is currently pending and we are just waiting for layout.
      */
     private boolean mInstantExpanding;
+    private boolean mAnimateAfterExpanding;
 
     PanelBar mBar;
 
@@ -209,6 +210,9 @@
 
     public void setTouchDisabled(boolean disabled) {
         mTouchDisabled = disabled;
+        if (mTouchDisabled && mTracking) {
+            onTrackingStopped(true /* expanded */);
+        }
     }
 
     @Override
@@ -653,7 +657,7 @@
                 vel = 0;
             }
             mFlingAnimationUtils.apply(animator, mExpandedHeight, target, vel, getHeight());
-            if (expandBecauseOfFalsing) {
+            if (vel == 0) {
                 animator.setDuration(350);
             }
         } else {
@@ -867,6 +871,7 @@
         }
 
         mInstantExpanding = true;
+        mAnimateAfterExpanding = animate;
         mUpdateFlingOnLayout = false;
         abortAnimations();
         cancelPeek();
@@ -891,7 +896,7 @@
                         if (mStatusBar.getStatusBarWindow().getHeight()
                                 != mStatusBar.getStatusBarHeight()) {
                             getViewTreeObserver().removeOnGlobalLayoutListener(this);
-                            if (animate) {
+                            if (mAnimateAfterExpanding) {
                                 notifyExpandingStarted();
                                 fling(0, true /* expand */);
                             } else {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
index 5f6fb86..f6447f6 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBar.java
@@ -17,11 +17,25 @@
 package com.android.systemui.statusbar.phone;
 
 
+import static android.app.StatusBarManager.NAVIGATION_HINT_BACK_ALT;
+import static android.app.StatusBarManager.NAVIGATION_HINT_IME_SHOWN;
+import static android.app.StatusBarManager.WINDOW_STATE_HIDDEN;
+import static android.app.StatusBarManager.WINDOW_STATE_SHOWING;
+import static android.app.StatusBarManager.windowStateToString;
+import static com.android.systemui.statusbar.phone.BarTransitions.MODE_LIGHTS_OUT;
+import static com.android.systemui.statusbar.phone.BarTransitions.MODE_LIGHTS_OUT_TRANSPARENT;
+import static com.android.systemui.statusbar.phone.BarTransitions.MODE_OPAQUE;
+import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
+import static com.android.systemui.statusbar.phone.BarTransitions.MODE_TRANSLUCENT;
+import static com.android.systemui.statusbar.phone.BarTransitions.MODE_TRANSPARENT;
+import static com.android.systemui.statusbar.phone.BarTransitions.MODE_WARNING;
+
 import android.animation.Animator;
 import android.animation.AnimatorListenerAdapter;
 import android.annotation.NonNull;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
+import android.app.ActivityOptions;
 import android.app.IActivityManager;
 import android.app.Notification;
 import android.app.PendingIntent;
@@ -58,6 +72,7 @@
 import android.media.session.MediaSession;
 import android.media.session.MediaSessionManager;
 import android.media.session.PlaybackState;
+import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Handler;
@@ -69,6 +84,8 @@
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemClock;
+import android.os.Trace;
+import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.os.Vibrator;
@@ -76,11 +93,13 @@
 import android.service.notification.NotificationListenerService;
 import android.service.notification.NotificationListenerService.RankingMap;
 import android.service.notification.StatusBarNotification;
+import android.telecom.TelecomManager;
 import android.util.ArraySet;
 import android.util.DisplayMetrics;
 import android.util.EventLog;
 import android.util.Log;
 import android.view.Display;
+import android.view.IRotationWatcher;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
@@ -97,6 +116,7 @@
 import android.view.animation.Interpolator;
 import android.widget.ImageView;
 import android.widget.TextView;
+
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.internal.statusbar.NotificationVisibility;
@@ -115,7 +135,6 @@
 import com.android.systemui.Prefs;
 import com.android.systemui.R;
 import com.android.systemui.SystemUIFactory;
-import com.android.systemui.assist.AssistManager;
 import com.android.systemui.classifier.FalsingLog;
 import com.android.systemui.classifier.FalsingManager;
 import com.android.systemui.doze.DozeHost;
@@ -155,12 +174,14 @@
 import com.android.systemui.statusbar.policy.BluetoothControllerImpl;
 import com.android.systemui.statusbar.policy.BrightnessMirrorController;
 import com.android.systemui.statusbar.policy.CastControllerImpl;
+import com.android.systemui.statusbar.policy.EncryptionHelper;
 import com.android.systemui.statusbar.policy.FlashlightController;
 import com.android.systemui.statusbar.policy.HeadsUpManager;
 import com.android.systemui.statusbar.policy.HotspotControllerImpl;
 import com.android.systemui.statusbar.policy.KeyguardMonitor;
 import com.android.systemui.statusbar.policy.KeyguardUserSwitcher;
 import com.android.systemui.statusbar.policy.LocationControllerImpl;
+import com.android.systemui.statusbar.policy.NetworkController;
 import com.android.systemui.statusbar.policy.NetworkControllerImpl;
 import com.android.systemui.statusbar.policy.NextAlarmController;
 import com.android.systemui.statusbar.policy.PreviewInflater;
@@ -170,13 +191,15 @@
 import com.android.systemui.statusbar.policy.UserSwitcherController;
 import com.android.systemui.statusbar.policy.ZenModeController;
 import com.android.systemui.statusbar.stack.NotificationStackScrollLayout;
-import com.android.systemui.statusbar.stack.NotificationStackScrollLayout.OnChildLocationsChangedListener;
+import com.android.systemui.statusbar.stack.NotificationStackScrollLayout
+        .OnChildLocationsChangedListener;
 import com.android.systemui.statusbar.stack.StackStateAnimator;
 import com.android.systemui.statusbar.stack.StackViewState;
 import com.android.systemui.volume.VolumeComponent;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -184,19 +207,6 @@
 import java.util.List;
 import java.util.Map;
 
-import static android.app.StatusBarManager.NAVIGATION_HINT_BACK_ALT;
-import static android.app.StatusBarManager.NAVIGATION_HINT_IME_SHOWN;
-import static android.app.StatusBarManager.WINDOW_STATE_HIDDEN;
-import static android.app.StatusBarManager.WINDOW_STATE_SHOWING;
-import static android.app.StatusBarManager.windowStateToString;
-import static com.android.systemui.statusbar.phone.BarTransitions.MODE_LIGHTS_OUT;
-import static com.android.systemui.statusbar.phone.BarTransitions.MODE_LIGHTS_OUT_TRANSPARENT;
-import static com.android.systemui.statusbar.phone.BarTransitions.MODE_OPAQUE;
-import static com.android.systemui.statusbar.phone.BarTransitions.MODE_SEMI_TRANSPARENT;
-import static com.android.systemui.statusbar.phone.BarTransitions.MODE_TRANSLUCENT;
-import static com.android.systemui.statusbar.phone.BarTransitions.MODE_TRANSPARENT;
-import static com.android.systemui.statusbar.phone.BarTransitions.MODE_WARNING;
-
 public class PhoneStatusBar extends BaseStatusBar implements DemoMode,
         DragDownHelper.DragDownCallback, ActivityStarter, OnUnlockMethodChangedListener,
         HeadsUpManager.OnHeadsUpChangedListener {
@@ -271,6 +281,14 @@
      */
     private static final int REMOTE_INPUT_KEPT_ENTRY_AUTO_CANCEL_DELAY = 200;
 
+    /**
+     * Never let the alpha become zero for surfaces that draw with SRC - otherwise the RenderNode
+     * won't draw anything and uninitialized memory will show through
+     * if mScrimSrcModeEnabled. Note that 0.001 is rounded down to 0 in
+     * libhwui.
+     */
+    private static final float SRC_MIN_ALPHA = 0.002f;
+
     static {
         boolean onlyCoreApps;
         boolean freeformWindowManagement;
@@ -360,6 +378,8 @@
     private View mPendingRemoteInputView;
     private View mPendingWorkRemoteInputView;
 
+    private View mReportRejectedTouch;
+
     int mMaxAllowedKeyguardNotifications;
 
     boolean mExpandedVisible;
@@ -646,6 +666,15 @@
     private boolean mNoAnimationOnNextBarModeChange;
     private FalsingManager mFalsingManager;
 
+    private KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
+        @Override
+        public void onDreamingStateChanged(boolean dreaming) {
+            if (dreaming) {
+                maybeEscalateHeadsUp();
+            }
+        }
+    };
+
     @Override
     public void start() {
         mDisplay = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
@@ -683,8 +712,8 @@
         mUnlockMethodCache.addListener(this);
         startKeyguard();
 
+        KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateCallback);
         mDozeServiceHost = new DozeServiceHost();
-        KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mDozeServiceHost);
         putComponent(DozeHost.class, mDozeServiceHost);
         putComponent(PhoneStatusBar.class, this);
 
@@ -765,7 +794,7 @@
             // no window manager? good luck with that
         }
 
-        mAssistManager = new AssistManager(this, context);
+        mAssistManager = SystemUIFactory.getInstance().createAssistManager(this, context);
 
         // figure out which pixel-format to use for the status bar.
         mPixelFormat = PixelFormat.OPAQUE;
@@ -787,11 +816,15 @@
         mBackdropFront = (ImageView) mBackdrop.findViewById(R.id.backdrop_front);
         mBackdropBack = (ImageView) mBackdrop.findViewById(R.id.backdrop_back);
 
+        if (ENABLE_LOCKSCREEN_WALLPAPER) {
+            mLockscreenWallpaper = new LockscreenWallpaper(mContext, this, mHandler);
+        }
+
         ScrimView scrimBehind = (ScrimView) mStatusBarWindow.findViewById(R.id.scrim_behind);
         ScrimView scrimInFront = (ScrimView) mStatusBarWindow.findViewById(R.id.scrim_in_front);
         View headsUpScrim = mStatusBarWindow.findViewById(R.id.heads_up_scrim);
         mScrimController = SystemUIFactory.getInstance().createScrimController(
-                scrimBehind, scrimInFront, headsUpScrim);
+                scrimBehind, scrimInFront, headsUpScrim, mLockscreenWallpaper);
         if (mScrimSrcModeEnabled) {
             Runnable runnable = new Runnable() {
                 @Override
@@ -821,10 +854,6 @@
                 mKeyguardBottomArea.getLockIcon());
         mKeyguardBottomArea.setKeyguardIndicationController(mKeyguardIndicationController);
 
-        if (ENABLE_LOCKSCREEN_WALLPAPER) {
-            mLockscreenWallpaper = new LockscreenWallpaper(mContext, this, mHandler);
-        }
-
         // set the initial view visibility
         setAreThereNotifications();
 
@@ -868,6 +897,7 @@
 
         initSignalCluster(mStatusBarView);
         initSignalCluster(mKeyguardStatusBar);
+        initEmergencyCryptkeeperText();
 
         mFlashlightController = new FlashlightController(mContext);
         mKeyguardBottomArea.setFlashlightController(mFlashlightController);
@@ -879,9 +909,9 @@
         mLightStatusBarController = new LightStatusBarController(mIconController,
                 mBatteryController);
         mKeyguardMonitor = new KeyguardMonitor(mContext);
+        mUserSwitcherController = new UserSwitcherController(mContext, mKeyguardMonitor,
+                mHandler, this);
         if (UserManager.get(mContext).isUserSwitcherEnabled()) {
-            mUserSwitcherController = new UserSwitcherController(mContext, mKeyguardMonitor,
-                    mHandler, this);
             createUserSwitcher();
         }
 
@@ -922,6 +952,36 @@
                 mBatteryController);
         mKeyguardStatusBar.setBatteryController(mBatteryController);
 
+        mReportRejectedTouch = mStatusBarWindow.findViewById(R.id.report_rejected_touch);
+        if (mReportRejectedTouch != null) {
+            updateReportRejectedTouchVisibility();
+            mReportRejectedTouch.setOnClickListener(v -> {
+                Uri session = mFalsingManager.reportRejectedTouch();
+                if (session == null) { return; }
+
+                StringWriter message = new StringWriter();
+                message.write("Build info: ");
+                message.write(SystemProperties.get("ro.build.description"));
+                message.write("\nSerial number: ");
+                message.write(SystemProperties.get("ro.serialno"));
+                message.write("\n");
+
+                PrintWriter falsingPw = new PrintWriter(message);
+                FalsingLog.dump(falsingPw);
+                falsingPw.flush();
+
+                startActivityDismissingKeyguard(Intent.createChooser(new Intent(Intent.ACTION_SEND)
+                                .setType("*/*")
+                                .putExtra(Intent.EXTRA_SUBJECT, "Rejected touch report")
+                                .putExtra(Intent.EXTRA_STREAM, session)
+                                .putExtra(Intent.EXTRA_TEXT, message.toString()),
+                        "Share rejected touch report")
+                                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
+                        true /* onlyProvisioned */, true /* dismissShade */);
+            });
+        }
+
+
         PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
         mBroadcastReceiver.onReceive(mContext,
                 new Intent(pm.isScreenOn() ? Intent.ACTION_SCREEN_ON : Intent.ACTION_SCREEN_OFF));
@@ -956,6 +1016,24 @@
         return mStatusBarView;
     }
 
+    private void initEmergencyCryptkeeperText() {
+        View emergencyViewStub = mStatusBarWindow.findViewById(R.id.emergency_cryptkeeper_text);
+        if (mNetworkController.hasEmergencyCryptKeeperText()) {
+            if (emergencyViewStub != null) {
+                ((ViewStub) emergencyViewStub).inflate();
+            }
+            mNetworkController.addSignalCallback(new NetworkController.SignalCallback() {
+                @Override
+                public void setIsAirplaneMode(NetworkController.IconState icon) {
+                    recomputeDisableFlags(true /* animate */);
+                }
+            });
+        } else if (emergencyViewStub != null) {
+            ViewGroup parent = (ViewGroup) emergencyViewStub.getParent();
+            parent.removeView(emergencyViewStub);
+        }
+    }
+
     protected BatteryController createBatteryController() {
         return new BatteryControllerImpl(mContext);
     }
@@ -1176,6 +1254,7 @@
     }
 
     protected void startKeyguard() {
+        Trace.beginSection("PhoneStatusBar#startKeyguard");
         KeyguardViewMediator keyguardViewMediator = getComponent(KeyguardViewMediator.class);
         mFingerprintUnlockController = new FingerprintUnlockController(mContext,
                 mStatusBarWindowManager, mDozeScrimController, keyguardViewMediator,
@@ -1210,6 +1289,7 @@
 
         mKeyguardViewMediatorCallback = keyguardViewMediator.getViewMediatorCallback();
         mLightStatusBarController.setFingerprintUnlockController(mFingerprintUnlockController);
+        Trace.endSection();
     }
 
     @Override
@@ -1235,6 +1315,7 @@
     }
 
     private View.OnClickListener mRecentsClickListener = new View.OnClickListener() {
+        @Override
         public void onClick(View v) {
             awakenDreams();
             toggleRecentApps();
@@ -1299,8 +1380,29 @@
     };
 
     private final View.OnTouchListener mHomeActionListener = new View.OnTouchListener() {
+        public boolean mBlockedThisTouch;
+
+        @Override
         public boolean onTouch(View v, MotionEvent event) {
+            if (mBlockedThisTouch && event.getActionMasked() != MotionEvent.ACTION_DOWN) {
+                return true;
+            }
+            // If an incoming call is ringing, HOME is totally disabled.
+            // (The user is already on the InCallUI at this point,
+            // and his ONLY options are to answer or reject the call.)
             switch (event.getAction()) {
+                case MotionEvent.ACTION_DOWN:
+                    mBlockedThisTouch = false;
+                    TelecomManager telecomManager = mContext.getSystemService(TelecomManager.class);
+                    if (telecomManager != null && telecomManager.isRinging()) {
+                        if (mStatusBarKeyguardViewManager.isShowing()) {
+                            Log.i(TAG, "Ignoring HOME; there's a ringing incoming call. " +
+                                    "No heads up");
+                            mBlockedThisTouch = true;
+                            return true;
+                        }
+                    }
+                    break;
                 case MotionEvent.ACTION_UP:
                 case MotionEvent.ACTION_CANCEL:
                     awakenDreams();
@@ -1345,6 +1447,27 @@
         if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + mNavigationBarView);
         if (mNavigationBarView == null) return;
 
+        try {
+            WindowManagerGlobal.getWindowManagerService()
+                    .watchRotation(new IRotationWatcher.Stub() {
+                @Override
+                public void onRotationChanged(int rotation) throws RemoteException {
+                    // We need this to be scheduled as early as possible to beat the redrawing of
+                    // window in response to the orientation change.
+                    Message msg = Message.obtain(mHandler, () -> {
+                        if (mNavigationBarView != null
+                                && mNavigationBarView.needsReorient(rotation)) {
+                            repositionNavigationBar();
+                        }
+                    });
+                    msg.setAsynchronous(true);
+                    mHandler.sendMessageAtFrontOfQueue(msg);
+                }
+            });
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        }
+
         prepareNavigationBarView();
 
         mWindowManager.addView(mNavigationBarView, getNavigationBarLayoutParams());
@@ -1355,7 +1478,7 @@
 
         prepareNavigationBarView();
 
-        mWindowManager.updateViewLayout(mNavigationBarView, getNavigationBarLayoutParams());
+        mWindowManager.updateViewLayout(mNavigationBarView, mNavigationBarView.getLayoutParams());
     }
 
     private void notifyNavigationBarScreenOn(boolean screenOn) {
@@ -1372,7 +1495,8 @@
                     | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                     | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                     | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
-                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH,
+                    | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
+                    | WindowManager.LayoutParams.FLAG_SLIPPERY,
                 PixelFormat.TRANSLUCENT);
         // this will allow the navbar to run in an overlay on devices that support this
         if (ActivityManager.isHighEndGfx()) {
@@ -1524,7 +1648,8 @@
         }
         Entry entry = mNotificationData.get(key);
 
-        if (entry != null && mRemoteInputController.isRemoteInputActive(entry)) {
+        if (entry != null && mRemoteInputController.isRemoteInputActive(entry)
+                && (entry.row != null && !entry.row.isDismissed())) {
             mLatestRankingMap = ranking;
             mRemoteInputEntriesToRemoveOnCollapse.add(entry);
             return;
@@ -1543,7 +1668,7 @@
                     && !mNotificationPanel.isTracking() && !mNotificationPanel.isQsExpanded()) {
                 if (mState == StatusBarState.SHADE) {
                     animateCollapsePanels();
-                } else if (mState == StatusBarState.SHADE_LOCKED) {
+                } else if (mState == StatusBarState.SHADE_LOCKED && !isCollapsing()) {
                     goToKeyguard();
                 }
             }
@@ -1594,7 +1719,7 @@
     protected void performRemoveNotification(StatusBarNotification n, boolean removeView) {
         Entry entry = mNotificationData.get(n.getKey());
         if (mRemoteInputController.isRemoteInputActive(entry)) {
-            mRemoteInputController.removeRemoteInput(entry);
+            mRemoteInputController.removeRemoteInput(entry, null);
         }
         super.performRemoveNotification(n, removeView);
     }
@@ -2059,12 +2184,20 @@
      * Refresh or remove lockscreen artwork from media metadata or the lockscreen wallpaper.
      */
     public void updateMediaMetaData(boolean metaDataChanged, boolean allowEnterAnimation) {
-        if (!SHOW_LOCKSCREEN_MEDIA_ARTWORK) return;
+        Trace.beginSection("PhoneStatusBar#updateMediaMetaData");
+        if (!SHOW_LOCKSCREEN_MEDIA_ARTWORK) {
+            Trace.endSection();
+            return;
+        }
 
-        if (mBackdrop == null) return; // called too early
+        if (mBackdrop == null) {
+            Trace.endSection();
+            return; // called too early
+        }
 
         if (mLaunchTransitionFadingAway) {
             mBackdrop.setVisibility(View.INVISIBLE);
+            Trace.endSection();
             return;
         }
 
@@ -2114,17 +2247,13 @@
             if (mBackdrop.getVisibility() != View.VISIBLE) {
                 mBackdrop.setVisibility(View.VISIBLE);
                 if (allowEnterAnimation) {
-                    mBackdrop.animate().alpha(1f).withEndAction(new Runnable() {
-                        @Override
-                        public void run() {
-                            mStatusBarWindowManager.setBackdropShowing(true);
-                        }
-                    });
+                    mBackdrop.setAlpha(SRC_MIN_ALPHA);
+                    mBackdrop.animate().alpha(1f);
                 } else {
                     mBackdrop.animate().cancel();
                     mBackdrop.setAlpha(1f);
-                    mStatusBarWindowManager.setBackdropShowing(true);
                 }
+                mStatusBarWindowManager.setBackdropShowing(true);
                 metaDataChanged = true;
                 if (DEBUG_MEDIA) {
                     Log.v(TAG, "DEBUG_MEDIA: Fading in album artwork");
@@ -2187,11 +2316,7 @@
                 } else {
                     mStatusBarWindowManager.setBackdropShowing(false);
                     mBackdrop.animate()
-                            // Never let the alpha become zero - otherwise the RenderNode
-                            // won't draw anything and uninitialized memory will show through
-                            // if mScrimSrcModeEnabled. Note that 0.001 is rounded down to 0 in
-                            // libhwui.
-                            .alpha(0.002f)
+                            .alpha(SRC_MIN_ALPHA)
                             .setInterpolator(Interpolators.ACCELERATE_DECELERATE)
                             .setDuration(300)
                             .setStartDelay(0)
@@ -2206,7 +2331,6 @@
                             });
                     if (mKeyguardFadingAway) {
                         mBackdrop.animate()
-
                                 // Make it disappear faster, as the focus should be on the activity
                                 // behind.
                                 .setDuration(mKeyguardFadingAwayDuration / 2)
@@ -2217,6 +2341,15 @@
                 }
             }
         }
+        Trace.endSection();
+    }
+
+    private void updateReportRejectedTouchVisibility() {
+        if (mReportRejectedTouch == null) {
+            return;
+        }
+        mReportRejectedTouch.setVisibility(mState == StatusBarState.KEYGUARD
+                && mFalsingManager.isReportingEnabled() ? View.VISIBLE : View.INVISIBLE);
     }
 
     protected int adjustDisableFlags(int state) {
@@ -2225,12 +2358,21 @@
             state |= StatusBarManager.DISABLE_NOTIFICATION_ICONS;
             state |= StatusBarManager.DISABLE_SYSTEM_INFO;
         }
+        if (mNetworkController != null && EncryptionHelper.IS_DATA_ENCRYPTED) {
+            if (mNetworkController.hasEmergencyCryptKeeperText()) {
+                state |= StatusBarManager.DISABLE_NOTIFICATION_ICONS;
+            }
+            if (!mNetworkController.isRadioOn()) {
+                state |= StatusBarManager.DISABLE_SYSTEM_INFO;
+            }
+        }
         return state;
     }
 
     /**
      * State is one or more of the DISABLE constants from StatusBarManager.
      */
+    @Override
     public void disable(int state1, int state2, boolean animate) {
         animate &= mStatusBarWindowState != WINDOW_STATE_HIDDEN;
         mDisabledUnmodified1 = state1;
@@ -2328,6 +2470,15 @@
         }
     }
 
+    /**
+     * Reapplies the disable flags as last requested by StatusBarManager.
+     *
+     * This needs to be called if state used by {@link #adjustDisableFlags} changes.
+     */
+    private void recomputeDisableFlags(boolean animate) {
+        disable(mDisabledUnmodified1, mDisabledUnmodified2, animate);
+    }
+
     @Override
     protected BaseStatusBar.H createHandler() {
         return new PhoneStatusBar.H();
@@ -2431,6 +2582,7 @@
                             mStatusBarWindowManager.setHeadsUpShowing(false);
                             mHeadsUpManager.setHeadsUpGoingAway(false);
                         }
+                        removeRemoteInputEntriesKeptUntilCollapsed();
                     }
                 });
             }
@@ -2460,6 +2612,7 @@
 
     }
 
+    @Override
     protected void updateHeadsUp(String key, Entry entry, boolean shouldPeek,
             boolean alertAgain) {
         final boolean wasHeadsUp = isHeadsUp(key);
@@ -2476,6 +2629,7 @@
         }
     }
 
+    @Override
     protected void setHeadsUpUser(int newUserId) {
         if (mHeadsUpManager != null) {
             mHeadsUpManager.setUser(newUserId);
@@ -2486,6 +2640,7 @@
         return mHeadsUpManager.isHeadsUp(key);
     }
 
+    @Override
     protected boolean isSnoozedPackage(StatusBarNotification sbn) {
         return mHeadsUpManager.isSnoozed(sbn.getPackageName());
     }
@@ -2512,7 +2667,7 @@
     private void removeRemoteInputEntriesKeptUntilCollapsed() {
         for (int i = 0; i < mRemoteInputEntriesToRemoveOnCollapse.size(); i++) {
             Entry entry = mRemoteInputEntriesToRemoveOnCollapse.valueAt(i);
-            mRemoteInputController.removeRemoteInput(entry);
+            mRemoteInputController.removeRemoteInput(entry, null);
             removeNotification(entry.key, mLatestRankingMap);
         }
         mRemoteInputEntriesToRemoveOnCollapse.clear();
@@ -2526,6 +2681,7 @@
      * All changes to the status bar and notifications funnel through here and are batched.
      */
     private class H extends BaseStatusBar.H {
+        @Override
         public void handleMessage(Message m) {
             super.handleMessage(m);
             switch (m.what) {
@@ -2567,6 +2723,37 @@
         mHeadsUpManager.releaseAllImmediately();
     }
 
+    /**
+     * Called for system navigation gestures. First action opens the panel, second opens
+     * settings. Down action closes the entire panel.
+     */
+    @Override
+    public void handleSystemNavigationKey(int key) {
+        if (SPEW) Log.d(TAG, "handleSystemNavigationKey: " + key);
+        if (!panelsEnabled() || !mKeyguardMonitor.isDeviceInteractive()
+                || mKeyguardMonitor.isShowing() && !mKeyguardMonitor.isOccluded()) {
+            return;
+        }
+
+        // Panels are not available in setup
+        if (!mUserSetup) return;
+
+        if (KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP == key) {
+            MetricsLogger.action(mContext, MetricsEvent.ACTION_SYSTEM_NAVIGATION_KEY_UP);
+            mNotificationPanel.collapse(false /* delayed */, 1.0f /* speedUpFactor */);
+        } else if (KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN == key) {
+            MetricsLogger.action(mContext, MetricsEvent.ACTION_SYSTEM_NAVIGATION_KEY_DOWN);
+            if (mNotificationPanel.isFullyCollapsed()) {
+                mNotificationPanel.expand(true /* animate */);
+                MetricsLogger.count(mContext, NotificationPanelView.COUNTER_PANEL_OPEN, 1);
+            } else if (!mNotificationPanel.isInSettings() && !mNotificationPanel.isExpanding()){
+                mNotificationPanel.flingSettings(0 /* velocity */, true /* expand */);
+                MetricsLogger.count(mContext, NotificationPanelView.COUNTER_PANEL_OPEN_QS, 1);
+            }
+        }
+
+    }
+
     boolean panelsEnabled() {
         return (mDisabled1 & StatusBarManager.DISABLE_EXPAND) == 0 && !ONLY_CORE_APPS;
     }
@@ -2578,8 +2765,6 @@
         }
 
         mExpandedVisible = true;
-        if (mNavigationBarView != null)
-            mNavigationBarView.setSlippery(true);
 
         // Expand the window to encompass the full screen in anticipation of the drag.
         // This is only possible to do atomically because the status bar is at the top of the screen!
@@ -2587,7 +2772,7 @@
 
         visibilityChanged(true);
         mWaitingForKeyguardExit = false;
-        disable(mDisabledUnmodified1, mDisabledUnmodified2, !force /* animate */);
+        recomputeDisableFlags(!force /* animate */);
         setInteracting(StatusBarManager.WINDOW_STATUS_BAR, true);
     }
 
@@ -2610,15 +2795,18 @@
         mHandler.sendEmptyMessage(MSG_OPEN_SETTINGS_PANEL);
     }
 
+    @Override
     public void animateCollapsePanels(int flags) {
         animateCollapsePanels(flags, false /* force */, false /* delayed */,
                 1.0f /* speedUpFactor */);
     }
 
+    @Override
     public void animateCollapsePanels(int flags, boolean force) {
         animateCollapsePanels(flags, force, false /* delayed */, 1.0f /* speedUpFactor */);
     }
 
+    @Override
     public void animateCollapsePanels(int flags, boolean force, boolean delayed) {
         animateCollapsePanels(flags, force, delayed, 1.0f /* speedUpFactor */);
     }
@@ -2713,8 +2901,7 @@
         mNotificationPanel.closeQs();
 
         mExpandedVisible = false;
-        if (mNavigationBarView != null)
-            mNavigationBarView.setSlippery(false);
+
         visibilityChanged(false);
 
         // Shrink the window to the size of the status bar only
@@ -2727,7 +2914,7 @@
         runPostCollapseRunnables();
         setInteracting(StatusBarManager.WINDOW_STATUS_BAR, false);
         showBouncer();
-        disable(mDisabledUnmodified1, mDisabledUnmodified2, true /* animate */);
+        recomputeDisableFlags(true /* animate */);
 
         // Trimming will happen later if Keyguard is showing - doing it here might cause a jank in
         // the bouncer appear animation.
@@ -2845,10 +3032,6 @@
                 Integer.toHexString(diff)));
         boolean sbModeChanged = false;
         if (diff != 0) {
-            // we never set the recents bit via this method, so save the prior state to prevent
-            // clobbering the bit below
-            final boolean wasRecentsVisible = (mSystemUiVisibility & View.RECENT_APPS_VISIBLE) > 0;
-
             mSystemUiVisibility = newVal;
 
             // update low profile
@@ -2899,11 +3082,6 @@
                 mSystemUiVisibility &= ~View.NAVIGATION_BAR_UNHIDE;
             }
 
-            // restore the recents bit
-            if (wasRecentsVisible) {
-                mSystemUiVisibility |= View.RECENT_APPS_VISIBLE;
-            }
-
             // send updated sysui visibility to window manager
             notifyUiVisibilityChanged(mSystemUiVisibility);
         }
@@ -3056,6 +3234,7 @@
         }
     }
 
+    @Override
     public void topAppWindowChanged(boolean showMenu) {
         if (SPEW) {
             Log.d(TAG, (showMenu?"showing":"hiding") + " the MENU button");
@@ -3092,6 +3271,7 @@
                 + ") " + v.getWidth() + "x" + v.getHeight() + "]";
     }
 
+    @Override
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         synchronized (mQueueLock) {
             pw.println("Current Status Bar state:");
@@ -3169,6 +3349,7 @@
                 pw.println("see the logcat for a dump of the views we have created.");
                 // must happen on ui thread
                 mHandler.post(new Runnable() {
+                        @Override
                         public void run() {
                             mStatusBarView.getLocationOnScreen(mAbsPos);
                             Log.d(TAG, "mStatusBarView: ----- (" + mAbsPos[0] + "," + mAbsPos[1]
@@ -3224,6 +3405,9 @@
         if (KeyguardUpdateMonitor.getInstance(mContext) != null) {
             KeyguardUpdateMonitor.getInstance(mContext).dump(fd, pw, args);
         }
+        if (mFlashlightController != null) {
+            mFlashlightController.dump(fd, pw, args);
+        }
 
         FalsingManager.getInstance(mContext).dump(pw);
         FalsingLog.dump(pw);
@@ -3279,18 +3463,32 @@
                 mContext, intent, mCurrentUserId);
         final boolean keyguardShowing = mStatusBarKeyguardViewManager.isShowing();
         Runnable runnable = new Runnable() {
+            @Override
             public void run() {
                 mAssistManager.hideAssist();
                 intent.setFlags(
                         Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                 int result = ActivityManager.START_CANCELED;
+                ActivityOptions options = new ActivityOptions(getActivityOptions());
+                if (intent == KeyguardBottomAreaView.INSECURE_CAMERA_INTENT) {
+                    // Normally an activity will set it's requested rotation
+                    // animation on its window. However when launching an activity
+                    // causes the orientation to change this is too late. In these cases
+                    // the default animation is used. This doesn't look good for
+                    // the camera (as it rotates the camera contents out of sync
+                    // with physical reality). So, we ask the WindowManager to
+                    // force the crossfade animation if an orientation change
+                    // happens to occur during the launch.
+                    options.setRotationAnimationHint(
+                            WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS);
+                }
                 try {
                     result = ActivityManagerNative.getDefault().startActivityAsUser(
                             null, mContext.getBasePackageName(),
                             intent,
                             intent.resolveTypeIfNeeded(mContext.getContentResolver()),
                             null, null, 0, Intent.FLAG_ACTIVITY_NEW_TASK, null,
-                            getActivityOptions(), UserHandle.CURRENT.getIdentifier());
+                            options.toBundle(), UserHandle.CURRENT.getIdentifier());
                 } catch (RemoteException e) {
                     Log.w(TAG, "Unable to start activity", e);
                 }
@@ -3323,6 +3521,7 @@
             @Override
             public boolean onDismiss() {
                 AsyncTask.execute(new Runnable() {
+                    @Override
                     public void run() {
                         try {
                             if (keyguardShowing && !afterKeyguardGone) {
@@ -3346,6 +3545,7 @@
     }
 
     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+        @Override
         public void onReceive(Context context, Intent intent) {
             if (DEBUG) Log.v(TAG, "onReceive: " + intent);
             String action = intent.getAction();
@@ -3376,6 +3576,7 @@
     };
 
     private BroadcastReceiver mDemoReceiver = new BroadcastReceiver() {
+        @Override
         public void onReceive(Context context, Intent intent) {
             if (DEBUG) Log.v(TAG, "onReceive: " + intent);
             String action = intent.getAction();
@@ -3457,6 +3658,7 @@
         setControllerUsers();
         clearCurrentMediaNotification();
         mLockscreenWallpaper.setCurrentUser(newUserId);
+        mScrimController.setCurrentUser(newUserId);
         updateMediaMetaData(true, false);
     }
 
@@ -3634,6 +3836,7 @@
     }
 
     Runnable mStartTracing = new Runnable() {
+        @Override
         public void run() {
             vibrate();
             SystemClock.sleep(250);
@@ -3644,6 +3847,7 @@
     };
 
     Runnable mStopTracing = new Runnable() {
+        @Override
         public void run() {
             android.os.Debug.stopMethodTracing();
             Log.d(TAG, "stopTracing");
@@ -3956,6 +4160,15 @@
     }
 
     /**
+     * Plays the animation when an activity that was occluding Keyguard goes away.
+     */
+    public void animateKeyguardUnoccluding() {
+        mScrimController.animateKeyguardUnoccluding(500);
+        mNotificationPanel.setExpandedFraction(0f);
+        animateExpandNotificationsPanel();
+    }
+
+    /**
      * Starts the timeout when we try to start the affordances on Keyguard. We usually rely that
      * Keyguard goes away via fadeKeyguardAfterLaunchTransition, however, that might not happen
      * because the launched app crashed or something else went wrong.
@@ -3987,6 +4200,7 @@
      * @return true if we would like to stay in the shade, false if it should go away entirely
      */
     public boolean hideKeyguard() {
+        Trace.beginSection("PhoneStatusBar#hideKeyguard");
         boolean staying = mLeaveOpenOnKeyguardHide;
         setBarState(StatusBarState.SHADE);
         View viewToClick = null;
@@ -4031,6 +4245,7 @@
         mNotificationPanel.onAffordanceLaunchEnded();
         mNotificationPanel.animate().cancel();
         mNotificationPanel.setAlpha(1f);
+        Trace.endSection();
         return staying;
     }
 
@@ -4071,7 +4286,7 @@
                 startTime + fadeoutDuration
                         - StatusBarIconController.DEFAULT_TINT_ANIMATION_DURATION,
                 StatusBarIconController.DEFAULT_TINT_ANIMATION_DURATION);
-        disable(mDisabledUnmodified1, mDisabledUnmodified2, fadeoutDuration > 0 /* animate */);
+        recomputeDisableFlags(fadeoutDuration > 0 /* animate */);
     }
 
     public boolean isKeyguardFadingAway() {
@@ -4105,6 +4320,7 @@
     }
 
     protected void updateKeyguardState(boolean goingToFullShade, boolean fromShadeLocked) {
+        Trace.beginSection("PhoneStatusBar#updateKeyguardState");
         if (mState == StatusBarState.KEYGUARD) {
             mKeyguardIndicationController.setVisible(true);
             mNotificationPanel.resetViews();
@@ -4135,10 +4351,13 @@
         checkBarModes();
         updateMediaMetaData(false, mState != StatusBarState.KEYGUARD);
         mKeyguardMonitor.notifyKeyguardState(mStatusBarKeyguardViewManager.isShowing(),
-                mStatusBarKeyguardViewManager.isSecure());
+                mStatusBarKeyguardViewManager.isSecure(),
+                mStatusBarKeyguardViewManager.isOccluded());
+        Trace.endSection();
     }
 
     private void updateDozingState() {
+        Trace.beginSection("PhoneStatusBar#updateDozingState");
         boolean animate = !mDozing && mDozeScrimController.isPulsing();
         mNotificationPanel.setDozing(mDozing, animate);
         mStackScroller.setDark(mDozing, animate, mWakeUpTouchLocation);
@@ -4149,6 +4368,7 @@
         mDozeScrimController.setDozing(mDozing &&
                 mFingerprintUnlockController.getMode()
                         != FingerprintUnlockController.MODE_WAKE_AND_UNLOCK_PULSING, animate);
+        Trace.endSection();
     }
 
     public void updateStackScrollerState(boolean goingToFullShade, boolean fromShadeLocked) {
@@ -4249,6 +4469,10 @@
         mStackScroller.setActivatedChild(view);
     }
 
+    public ButtonDispatcher getHomeButton() {
+        return mNavigationBarView.getHomeButton();
+    }
+
     /**
      * @param state The {@link StatusBarState} to set.
      */
@@ -4263,11 +4487,13 @@
         }
         if (state == StatusBarState.KEYGUARD) {
             removeRemoteInputEntriesKeptUntilCollapsed();
+            maybeEscalateHeadsUp();
         }
         mState = state;
         mGroupManager.setStatusBarState(state);
         mFalsingManager.setStatusBarState(state);
         mStatusBarWindowManager.setStatusBarState(state);
+        updateReportRejectedTouchVisibility();
         updateDozing();
     }
 
@@ -4546,7 +4772,7 @@
     public void setBouncerShowing(boolean bouncerShowing) {
         super.setBouncerShowing(bouncerShowing);
         mStatusBarView.setBouncerShowing(bouncerShowing);
-        disable(mDisabledUnmodified1, mDisabledUnmodified2, true /* animate */);
+        recomputeDisableFlags(true /* animate */);
     }
 
     public void onStartedGoingToSleep() {
@@ -4596,7 +4822,7 @@
 
     private void vibrateForCameraGesture() {
         // Make sure to pass -1 for repeat so VibratorService doesn't stop us when going to sleep.
-        mVibrator.vibrate(new long[]{0, 750L}, -1 /* repeat */);
+        mVibrator.vibrate(new long[]{0, 400}, -1 /* repeat */);
     }
 
     public void onScreenTurnedOn() {
@@ -4623,16 +4849,6 @@
         return false;
     }
 
-    public void updateRecentsVisibility(boolean visible) {
-        // Update the recents visibility flag
-        if (visible) {
-            mSystemUiVisibility |= View.RECENT_APPS_VISIBLE;
-        } else {
-            mSystemUiVisibility &= ~View.RECENT_APPS_VISIBLE;
-        }
-        notifyUiVisibilityChanged(mSystemUiVisibility);
-    }
-
     @Override
     public void showScreenPinningRequest(int taskId) {
         if (mKeyguardMonitor.isShowing()) {
@@ -4746,11 +4962,13 @@
     }
 
     private void updateDozing() {
+        Trace.beginSection("PhoneStatusBar#updateDozing");
         // When in wake-and-unlock while pulsing, keep dozing state until fully unlocked.
         mDozing = mDozingRequested && mState == StatusBarState.KEYGUARD
                 || mFingerprintUnlockController.getMode()
                         == FingerprintUnlockController.MODE_WAKE_AND_UNLOCK_PULSING;
         updateDozingState();
+        Trace.endSection();
     }
 
     private final class ShadeUpdates {
@@ -4779,7 +4997,7 @@
         }
     }
 
-    private final class DozeServiceHost extends KeyguardUpdateMonitorCallback implements DozeHost  {
+    private final class DozeServiceHost implements DozeHost {
         // Amount of time to allow to update the time shown on the screen before releasing
         // the wakelock.  This timeout is design to compensate for the fact that we don't
         // currently have a way to know when time display contents have actually been
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
index 1fd3bdf..7187ec2 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java
@@ -147,7 +147,7 @@
 
         // listen for user / profile change.
         try {
-            ActivityManagerNative.getDefault().registerUserSwitchObserver(mUserSwitchListener);
+            ActivityManagerNative.getDefault().registerUserSwitchObserver(mUserSwitchListener, TAG);
         } catch (RemoteException e) {
             // Ignore
         }
@@ -221,6 +221,8 @@
             mSimState = IccCardConstants.State.ABSENT;
         } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
             mSimState = IccCardConstants.State.CARD_IO_ERROR;
+        } else if (IccCardConstants.INTENT_VALUE_ICC_CARD_RESTRICTED.equals(stateExtra)) {
+            mSimState = IccCardConstants.State.CARD_RESTRICTED;
         } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
             mSimState = IccCardConstants.State.READY;
         } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
index 4002473..da698d8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
@@ -27,6 +27,7 @@
 import android.os.Looper;
 import android.os.Process;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.provider.Settings;
 import android.provider.Settings.Secure;
 import android.service.quicksettings.Tile;
@@ -51,6 +52,7 @@
 import com.android.systemui.qs.tiles.HotspotTile;
 import com.android.systemui.qs.tiles.IntentTile;
 import com.android.systemui.qs.tiles.LocationTile;
+import com.android.systemui.qs.tiles.NightDisplayTile;
 import com.android.systemui.qs.tiles.RotationLockTile;
 import com.android.systemui.qs.tiles.UserTile;
 import com.android.systemui.qs.tiles.WifiTile;
@@ -59,7 +61,6 @@
 import com.android.systemui.statusbar.policy.BluetoothController;
 import com.android.systemui.statusbar.policy.CastController;
 import com.android.systemui.statusbar.policy.NextAlarmController;
-import com.android.systemui.statusbar.policy.NightModeController;
 import com.android.systemui.statusbar.policy.FlashlightController;
 import com.android.systemui.statusbar.policy.HotspotController;
 import com.android.systemui.statusbar.policy.KeyguardMonitor;
@@ -70,7 +71,6 @@
 import com.android.systemui.statusbar.policy.UserInfoController;
 import com.android.systemui.statusbar.policy.UserSwitcherController;
 import com.android.systemui.statusbar.policy.ZenModeController;
-import com.android.systemui.tuner.NightModeTile;
 import com.android.systemui.tuner.TunerService;
 import com.android.systemui.tuner.TunerService.Tunable;
 
@@ -110,7 +110,6 @@
     private final TileServices mServices;
 
     private final List<Callback> mCallbacks = new ArrayList<>();
-    private final NightModeController mNightModeController;
     private final AutoTileManager mAutoTiles;
     private final ManagedProfileController mProfileController;
     private final NextAlarmController mNextAlarmController;
@@ -143,7 +142,6 @@
         mBattery = battery;
         mIconController = iconController;
         mNextAlarmController = nextAlarmController;
-        mNightModeController = new NightModeController(mContext, true);
         mProfileController = new ManagedProfileController(this);
 
         final HandlerThread ht = new HandlerThread(QSTileHost.class.getSimpleName(),
@@ -307,10 +305,6 @@
         return mIconController;
     }
 
-    public NightModeController getNightModeController() {
-        return mNightModeController;
-    }
-
     public ManagedProfileController getManagedProfileController() {
         return mProfileController;
     }
@@ -321,6 +315,9 @@
             return;
         }
         if (DEBUG) Log.d(TAG, "Recreating tiles");
+        if (newValue == null && UserManager.isDeviceInDemoMode(mContext)) {
+            newValue = mContext.getResources().getString(R.string.quick_settings_tiles_retail_mode);
+        }
         final List<String> tileSpecs = loadTileSpecs(mContext, newValue);
         int currentUser = ActivityManager.getCurrentUser();
         if (tileSpecs.equals(mTileSpecs) && currentUser == mCurrentUser) return;
@@ -337,6 +334,9 @@
                     || ((CustomTile) tile).getUser() == currentUser)) {
                 if (DEBUG) Log.d(TAG, "Adding " + tile);
                 tile.removeCallbacks();
+                if (!(tile instanceof CustomTile) && mCurrentUser != currentUser) {
+                    tile.userSwitch(currentUser);
+                }
                 newTiles.put(tileSpec, tile);
             } else {
                 if (DEBUG) Log.d(TAG, "Creating tile: " + tileSpec);
@@ -403,7 +403,7 @@
                 ComponentName component = CustomTile.getComponentFromSpec(tileSpec);
                 Intent intent = new Intent().setComponent(component);
                 TileLifecycleManager lifecycleManager = new TileLifecycleManager(new Handler(),
-                        mContext, mServices, new Tile(component), intent,
+                        mContext, mServices, new Tile(), intent,
                         new UserHandle(ActivityManager.getCurrentUser()));
                 lifecycleManager.onStopListening();
                 lifecycleManager.onTileRemoved();
@@ -432,8 +432,7 @@
         else if (tileSpec.equals("user")) return new UserTile(this);
         else if (tileSpec.equals("battery")) return new BatteryTile(this);
         else if (tileSpec.equals("saver")) return new DataSaverTile(this);
-        else if (tileSpec.equals(NightModeTile.NIGHT_MODE_SPEC))
-            return new NightModeTile(this);
+        else if (tileSpec.equals("night")) return new NightDisplayTile(this);
         // Intent tiles.
         else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(this,tileSpec);
         else if (tileSpec.startsWith(CustomTile.PREFIX)) return CustomTile.create(this,tileSpec);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickStatusBarHeader.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickStatusBarHeader.java
index 2de8329..7a2ae22 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickStatusBarHeader.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickStatusBarHeader.java
@@ -23,6 +23,7 @@
 import android.content.res.Configuration;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.RippleDrawable;
+import android.os.UserManager;
 import android.util.AttributeSet;
 import android.view.View;
 import android.view.View.OnClickListener;
@@ -36,11 +37,11 @@
 import com.android.keyguard.KeyguardStatusView;
 import com.android.systemui.FontSizeUtils;
 import com.android.systemui.R;
-import com.android.systemui.qs.QSAnimator;
 import com.android.systemui.qs.QSPanel;
 import com.android.systemui.qs.QSPanel.Callback;
 import com.android.systemui.qs.QuickQSPanel;
 import com.android.systemui.qs.TouchAnimator;
+import com.android.systemui.qs.TouchAnimator.Builder;
 import com.android.systemui.statusbar.policy.BatteryController;
 import com.android.systemui.statusbar.policy.NextAlarmController;
 import com.android.systemui.statusbar.policy.NextAlarmController.NextAlarmChangeCallback;
@@ -82,19 +83,14 @@
     protected MultiUserSwitch mMultiUserSwitch;
     private ImageView mMultiUserAvatar;
 
-    private float mDateTimeTranslation;
-    private float mDateTimeAlarmTranslation;
-    private float mDateScaleFactor;
-    protected float mGearTranslation;
 
-    private TouchAnimator mSecondHalfAnimator;
-    private TouchAnimator mFirstHalfAnimator;
-    private TouchAnimator mDateSizeAnimator;
-    private TouchAnimator mAlarmTranslation;
+    private TouchAnimator mAnimator;
     protected TouchAnimator mSettingsAlpha;
     private float mExpansionAmount;
     private QSTileHost mHost;
+    private View mEdit;
     private boolean mShowFullAlarm;
+    private float mDateTimeTranslation;
 
     public QuickStatusBarHeader(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -106,11 +102,16 @@
 
         mEmergencyOnly = (TextView) findViewById(R.id.header_emergency_calls_only);
 
+        mEdit = findViewById(android.R.id.edit);
+        findViewById(android.R.id.edit).setOnClickListener(view ->
+                mHost.startRunnableDismissingKeyguard(() -> mQsPanel.showEdit(view)));
+
         mDateTimeAlarmGroup = (ViewGroup) findViewById(R.id.date_time_alarm_group);
         mDateTimeAlarmGroup.findViewById(R.id.empty_time_view).setVisibility(View.GONE);
         mDateTimeGroup = (ViewGroup) findViewById(R.id.date_time_group);
         mDateTimeGroup.setPivotX(0);
         mDateTimeGroup.setPivotY(0);
+        mDateTimeTranslation = getResources().getDimension(R.dimen.qs_date_time_translation);
         mShowFullAlarm = getResources().getBoolean(R.bool.quick_settings_show_full_alarm);
 
         mExpandIndicator = (ExpandableIndicator) findViewById(R.id.expand_indicator);
@@ -152,47 +153,21 @@
         FontSizeUtils.updateFontSize(mAlarmStatus, R.dimen.qs_date_collapsed_size);
         FontSizeUtils.updateFontSize(mEmergencyOnly, R.dimen.qs_emergency_calls_only_text_size);
 
-        mGearTranslation = mContext.getResources().getDimension(R.dimen.qs_header_gear_translation);
-
-        mDateTimeTranslation = mContext.getResources().getDimension(
-                R.dimen.qs_date_anim_translation);
-        mDateTimeAlarmTranslation = mContext.getResources().getDimension(
-                R.dimen.qs_date_alarm_anim_translation);
-        float dateCollapsedSize = mContext.getResources().getDimension(
-                R.dimen.qs_date_collapsed_text_size);
-        float dateExpandedSize = mContext.getResources().getDimension(
-                R.dimen.qs_date_text_size);
-        mDateScaleFactor = dateExpandedSize / dateCollapsedSize;
-        updateDateTimePosition();
-
-        mSecondHalfAnimator = new TouchAnimator.Builder()
+        Builder builder = new Builder()
                 .addFloat(mShowFullAlarm ? mAlarmStatus : findViewById(R.id.date), "alpha", 0, 1)
-                .addFloat(mEmergencyOnly, "alpha", 0, 1)
-                .setStartDelay(.5f)
-                .build();
+                .addFloat(mEmergencyOnly, "alpha", 0, 1);
         if (mShowFullAlarm) {
-            mFirstHalfAnimator = new TouchAnimator.Builder()
-                    .addFloat(mAlarmStatusCollapsed, "alpha", 1, 0)
-                    .setEndDelay(.5f)
-                    .build();
+            builder.addFloat(mAlarmStatusCollapsed, "alpha", 1, 0);
         }
-        mDateSizeAnimator = new TouchAnimator.Builder()
-                .addFloat(mDateTimeGroup, "scaleX", 1, mDateScaleFactor)
-                .addFloat(mDateTimeGroup, "scaleY", 1, mDateScaleFactor)
-                .setStartDelay(.36f)
-                .build();
+        mAnimator = builder.build();
 
         updateSettingsAnimator();
     }
 
     protected void updateSettingsAnimator() {
         mSettingsAlpha = new TouchAnimator.Builder()
-                .addFloat(mSettingsContainer, "translationY", -mGearTranslation, 0)
-                .addFloat(mMultiUserSwitch, "translationY", -mGearTranslation, 0)
-                .addFloat(mSettingsButton, "rotation", -90, 0)
-                .addFloat(mSettingsContainer, "alpha", 0, 1)
+                .addFloat(mEdit, "alpha", 0, 1)
                 .addFloat(mMultiUserSwitch, "alpha", 0, 1)
-                .setStartDelay(QSAnimator.EXPANDED_TILE_DELAY)
                 .build();
 
         final boolean isRtl = isLayoutRtl();
@@ -222,6 +197,7 @@
 
     @Override
     public void setExpanded(boolean expanded) {
+        if (mExpanded == expanded) return;
         mExpanded = expanded;
         mHeaderQsPanel.setExpanded(expanded);
         updateEverything();
@@ -247,12 +223,8 @@
     @Override
     public void setExpansion(float headerExpansionFraction) {
         mExpansionAmount = headerExpansionFraction;
-        mSecondHalfAnimator.setPosition(headerExpansionFraction);
-        if (mShowFullAlarm) {
-            mFirstHalfAnimator.setPosition(headerExpansionFraction);
-        }
-        mDateSizeAnimator.setPosition(headerExpansionFraction);
-        mAlarmTranslation.setPosition(headerExpansionFraction);
+        updateDateTimePosition();
+        mAnimator.setPosition(headerExpansionFraction);
         mSettingsAlpha.setPosition(headerExpansionFraction);
 
         updateAlarmVisibilities();
@@ -273,15 +245,6 @@
         mAlarmStatusCollapsed.setVisibility(mAlarmShowing ? View.VISIBLE : View.INVISIBLE);
     }
 
-    private void updateDateTimePosition() {
-        // This one has its own because we have to rebuild it every time the alarm state changes.
-        mAlarmTranslation = new TouchAnimator.Builder()
-                .addFloat(mDateTimeAlarmGroup, "translationY", 0, mAlarmShowing
-                        ? mDateTimeAlarmTranslation : mDateTimeTranslation)
-                .build();
-        mAlarmTranslation.setPosition(mExpansionAmount);
-    }
-
     public void setListening(boolean listening) {
         if (listening == mListening) {
             return;
@@ -293,20 +256,28 @@
 
     @Override
     public void updateEverything() {
-        updateDateTimePosition();
-        updateVisibilities();
-        setClickable(false);
+        post(() -> {
+            updateVisibilities();
+            setClickable(false);
+        });
     }
 
     protected void updateVisibilities() {
         updateAlarmVisibilities();
+        updateDateTimePosition();
         mEmergencyOnly.setVisibility(mExpanded && mShowEmergencyCallsOnly
                 ? View.VISIBLE : View.INVISIBLE);
-        mSettingsContainer.setVisibility(mExpanded ? View.VISIBLE : View.INVISIBLE);
         mSettingsContainer.findViewById(R.id.tuner_icon).setVisibility(
                 TunerService.isTunerEnabled(mContext) ? View.VISIBLE : View.INVISIBLE);
-        mMultiUserSwitch.setVisibility(mExpanded && mMultiUserSwitch.hasMultipleUsers()
+        final boolean isDemo = UserManager.isDeviceInDemoMode(mContext);
+        mMultiUserSwitch.setVisibility(mExpanded && mMultiUserSwitch.hasMultipleUsers() && !isDemo
                 ? View.VISIBLE : View.INVISIBLE);
+        mEdit.setVisibility(isDemo || !mExpanded ? View.INVISIBLE : View.VISIBLE);
+    }
+
+    private void updateDateTimePosition() {
+        mDateTimeAlarmGroup.setTranslationY(mShowEmergencyCallsOnly
+                ? mExpansionAmount * mDateTimeTranslation : 0);
     }
 
     private void updateListeners() {
@@ -350,7 +321,8 @@
     public void onClick(View v) {
         if (v == mSettingsButton) {
             MetricsLogger.action(mContext,
-                    MetricsProto.MetricsEvent.ACTION_QS_EXPANDED_SETTINGS_LAUNCH);
+                    mExpanded ? MetricsProto.MetricsEvent.ACTION_QS_EXPANDED_SETTINGS_LAUNCH
+                            : MetricsProto.MetricsEvent.ACTION_QS_COLLAPSED_SETTINGS_LAUNCH);
             if (mSettingsButton.isTunerClick()) {
                 mHost.startRunnableDismissingKeyguard(() -> post(() -> {
                     if (TunerService.isTunerEnabled(mContext)) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ReverseLinearLayout.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ReverseLinearLayout.java
index 3682aa1..f45967a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ReverseLinearLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ReverseLinearLayout.java
@@ -30,7 +30,11 @@
  */
 public class ReverseLinearLayout extends LinearLayout {
 
-    private boolean mIsLayoutRtl;
+    /** If true, the layout is reversed vs. a regular linear layout */
+    private boolean mIsLayoutReverse;
+
+    /** If true, the layout is opposite to it's natural reversity from the layout direction */
+    private boolean mIsAlternativeOrder;
 
     public ReverseLinearLayout(Context context, @Nullable AttributeSet attrs) {
         super(context, attrs);
@@ -39,45 +43,50 @@
     @Override
     protected void onFinishInflate() {
         super.onFinishInflate();
-        mIsLayoutRtl = getResources().getConfiguration()
-                .getLayoutDirection() == LAYOUT_DIRECTION_RTL;
+        updateOrder();
     }
 
     @Override
     public void addView(View child) {
         reversParams(child.getLayoutParams());
-        if (mIsLayoutRtl) {
-            super.addView(child);
-        } else {
+        if (mIsLayoutReverse) {
             super.addView(child, 0);
+        } else {
+            super.addView(child);
         }
     }
 
     @Override
     public void addView(View child, ViewGroup.LayoutParams params) {
         reversParams(params);
-        if (mIsLayoutRtl) {
-            super.addView(child, params);
-        } else {
+        if (mIsLayoutReverse) {
             super.addView(child, 0, params);
+        } else {
+            super.addView(child, params);
         }
     }
 
     @Override
-    protected void onConfigurationChanged(Configuration newConfig) {
-        super.onConfigurationChanged(newConfig);
-        updateRTLOrder();
+    public void onRtlPropertiesChanged(int layoutDirection) {
+        super.onRtlPropertiesChanged(layoutDirection);
+        updateOrder();
+    }
+
+    public void setAlternativeOrder(boolean alternative) {
+        mIsAlternativeOrder = alternative;
+        updateOrder();
     }
 
     /**
      * In landscape, the LinearLayout is not auto mirrored since it is vertical. Therefore we
      * have to do it manually
      */
-    private void updateRTLOrder() {
-        boolean isLayoutRtl = getResources().getConfiguration()
-                .getLayoutDirection() == LAYOUT_DIRECTION_RTL;
-        if (mIsLayoutRtl != isLayoutRtl) {
-            // RTL changed, swap the order of all views.
+    private void updateOrder() {
+        boolean isLayoutRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
+        boolean isLayoutReverse = isLayoutRtl ^ mIsAlternativeOrder;
+
+        if (mIsLayoutReverse != isLayoutReverse) {
+            // reversity changed, swap the order of all views.
             int childCount = getChildCount();
             ArrayList<View> childList = new ArrayList<>(childCount);
             for (int i = 0; i < childCount; i++) {
@@ -87,7 +96,7 @@
             for (int i = childCount - 1; i >= 0; i--) {
                 super.addView(childList.get(i));
             }
-            mIsLayoutRtl = isLayoutRtl;
+            mIsLayoutReverse = isLayoutReverse;
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
index 9c4480e..35e084d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/ScrimController.java
@@ -30,6 +30,7 @@
 import android.view.animation.Interpolator;
 import android.view.animation.PathInterpolator;
 
+import com.android.keyguard.KeyguardUpdateMonitor;
 import com.android.systemui.R;
 import com.android.systemui.statusbar.ExpandableNotificationRow;
 import com.android.systemui.statusbar.NotificationData;
@@ -46,10 +47,13 @@
     public static final long ANIMATION_DURATION = 220;
     public static final Interpolator KEYGUARD_FADE_OUT_INTERPOLATOR
             = new PathInterpolator(0f, 0, 0.7f, 1f);
+    public static final Interpolator KEYGUARD_FADE_OUT_INTERPOLATOR_LOCKED
+            = new PathInterpolator(0.3f, 0f, 0.8f, 1f);
     private static final float SCRIM_BEHIND_ALPHA = 0.62f;
-    private static final float SCRIM_BEHIND_ALPHA_KEYGUARD = 0.45f;
-    private static final float SCRIM_BEHIND_ALPHA_UNLOCKING = 0.2f;
+    protected static final float SCRIM_BEHIND_ALPHA_KEYGUARD = 0.45f;
+    protected static final float SCRIM_BEHIND_ALPHA_UNLOCKING = 0.2f;
     private static final float SCRIM_IN_FRONT_ALPHA = 0.75f;
+    private static final float SCRIM_IN_FRONT_ALPHA_LOCKED = 0.85f;
     private static final int TAG_KEY_ANIM = R.id.scrim;
     private static final int TAG_KEY_ANIM_TARGET = R.id.scrim_target;
     private static final int TAG_START_ALPHA = R.id.scrim_alpha_start;
@@ -59,6 +63,7 @@
     private final ScrimView mScrimInFront;
     private final UnlockMethodCache mUnlockMethodCache;
     private final View mHeadsUpScrim;
+    private final KeyguardUpdateMonitor mKeyguardUpdateMonitor;
 
     private float mScrimBehindAlpha = SCRIM_BEHIND_ALPHA;
     private float mScrimBehindAlphaKeyguard = SCRIM_BEHIND_ALPHA_KEYGUARD;
@@ -99,6 +104,7 @@
         mHeadsUpScrim = headsUpScrim;
         final Context context = scrimBehind.getContext();
         mUnlockMethodCache = UnlockMethodCache.getInstance(context);
+        mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(context);
         updateHeadsUpScrim(false);
     }
 
@@ -120,6 +126,13 @@
         scheduleUpdate();
     }
 
+    protected void setScrimBehindValues(float scrimBehindAlphaKeyguard,
+            float scrimBehindAlphaUnlocking) {
+        mScrimBehindAlphaKeyguard = scrimBehindAlphaKeyguard;
+        mScrimBehindAlphaUnlocking = scrimBehindAlphaUnlocking;
+        scheduleUpdate();
+    }
+
     public void onTrackingStarted() {
         mExpanding = true;
         mDarkenWhileDragging = !mUnlockMethodCache.canSkipBouncer();
@@ -162,11 +175,19 @@
         mAnimateChange = true;
         mSkipFirstFrame = skipFirstFrame;
         mOnAnimationFinished = onAnimationFinished;
-        scheduleUpdate();
 
-        // No need to wait for the next frame to be drawn for this case - onPreDraw will execute
-        // the changes we just scheduled.
-        onPreDraw();
+        if (!mKeyguardUpdateMonitor.needsSlowUnlockTransition()) {
+            scheduleUpdate();
+
+            // No need to wait for the next frame to be drawn for this case - onPreDraw will execute
+            // the changes we just scheduled.
+            onPreDraw();
+        } else {
+
+            // In case the user isn't unlocked, make sure to delay a bit because the system is hosed
+            // with too many things in this case, in order to not skip the initial frames.
+            mScrimInFront.postOnAnimationDelayed(this::scheduleUpdate, 16);
+        }
     }
 
     public void abortKeyguardFadingOut() {
@@ -175,6 +196,14 @@
         }
     }
 
+    public void animateKeyguardUnoccluding(long duration) {
+        mAnimateChange = false;
+        setScrimBehindColor(0f);
+        mAnimateChange = true;
+        scheduleUpdate();
+        mDurationOverride = duration;
+    }
+
     public void animateGoingToFullShade(long delay, long duration) {
         mDurationOverride = duration;
         mAnimationDelay = delay;
@@ -211,6 +240,11 @@
         return mDozeInFrontAlpha;
     }
 
+    private float getScrimInFrontAlpha() {
+        return mKeyguardUpdateMonitor.needsSlowUnlockTransition()
+                ? SCRIM_IN_FRONT_ALPHA_LOCKED
+                : SCRIM_IN_FRONT_ALPHA;
+    }
     private void scheduleUpdate() {
         if (mUpdatePending) return;
 
@@ -250,10 +284,10 @@
             float fraction = 1 - behindFraction;
             fraction = (float) Math.pow(fraction, 0.8f);
             behindFraction = (float) Math.pow(behindFraction, 0.8f);
-            setScrimInFrontColor(fraction * SCRIM_IN_FRONT_ALPHA);
+            setScrimInFrontColor(fraction * getScrimInFrontAlpha());
             setScrimBehindColor(behindFraction * mScrimBehindAlphaKeyguard);
         } else if (mBouncerShowing) {
-            setScrimInFrontColor(SCRIM_IN_FRONT_ALPHA);
+            setScrimInFrontColor(getScrimInFrontAlpha());
             setScrimBehindColor(0f);
         } else {
             float fraction = Math.max(0, Math.min(mFraction, 1));
@@ -371,7 +405,13 @@
     }
 
     private Interpolator getInterpolator() {
-        return mAnimateKeyguardFadingOut ? KEYGUARD_FADE_OUT_INTERPOLATOR : mInterpolator;
+        if (mAnimateKeyguardFadingOut && mKeyguardUpdateMonitor.needsSlowUnlockTransition()) {
+            return KEYGUARD_FADE_OUT_INTERPOLATOR_LOCKED;
+        } else if (mAnimateKeyguardFadingOut) {
+            return KEYGUARD_FADE_OUT_INTERPOLATOR;
+        } else {
+            return mInterpolator;
+        }
     }
 
     @Override
@@ -524,6 +564,10 @@
         mScrimBehind.setExcludedArea(area);
     }
 
+    public void setLeftInset(int inset) {
+        mScrimBehind.setLeftInset(inset);
+    }
+
     public int getScrimBehindColor() {
         return mScrimBehind.getScrimColorWithAlpha();
     }
@@ -538,4 +582,8 @@
                 R.dimen.heads_up_scrim_height);
         mHeadsUpScrim.setLayoutParams(layoutParams);
     }
+
+    public void setCurrentUser(int currentUser) {
+        // Don't care in the base class.
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarHeaderView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarHeaderView.java
deleted file mode 100644
index 72eafd8..0000000
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarHeaderView.java
+++ /dev/null
@@ -1,842 +0,0 @@
-/*
- * Copyright (C) 2014 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 android.app.AlarmManager;
-import android.app.PendingIntent;
-import android.content.Context;
-import android.content.Intent;
-import android.content.res.Configuration;
-import android.content.res.Resources;
-import android.graphics.Outline;
-import android.graphics.Rect;
-import android.graphics.drawable.Animatable;
-import android.graphics.drawable.Drawable;
-import android.graphics.drawable.RippleDrawable;
-import android.util.AttributeSet;
-import android.util.MathUtils;
-import android.util.TypedValue;
-import android.view.View;
-import android.view.ViewGroup;
-import android.view.ViewOutlineProvider;
-import android.widget.ImageView;
-import android.widget.LinearLayout;
-import android.widget.RelativeLayout;
-import android.widget.Switch;
-import android.widget.TextView;
-import android.widget.Toast;
-import com.android.keyguard.KeyguardStatusView;
-import com.android.systemui.BatteryMeterView;
-import com.android.systemui.FontSizeUtils;
-import com.android.systemui.R;
-import com.android.systemui.qs.QSPanel;
-import com.android.systemui.qs.QSPanel.Callback;
-import com.android.systemui.qs.QSTile;
-import com.android.systemui.qs.QSTile.DetailAdapter;
-import com.android.systemui.statusbar.policy.BatteryController;
-import com.android.systemui.statusbar.policy.NetworkController.EmergencyListener;
-import com.android.systemui.statusbar.policy.NextAlarmController;
-import com.android.systemui.statusbar.policy.UserInfoController;
-import com.android.systemui.tuner.TunerService;
-
-import java.text.NumberFormat;
-
-/**
- * The view to manage the header area in the expanded status bar.
- */
-public class StatusBarHeaderView extends BaseStatusBarHeader implements View.OnClickListener,
-        BatteryController.BatteryStateChangeCallback, NextAlarmController.NextAlarmChangeCallback,
-        EmergencyListener {
-
-    private boolean mExpanded;
-    private boolean mListening;
-
-    private ViewGroup mSystemIconsContainer;
-    private View mSystemIconsSuperContainer;
-    private View mDateGroup;
-    private View mClock;
-    private TextView mTime;
-    private TextView mAmPm;
-    private MultiUserSwitch mMultiUserSwitch;
-    private ImageView mMultiUserAvatar;
-    private TextView mDateCollapsed;
-    private TextView mDateExpanded;
-    private LinearLayout mSystemIcons;
-    private View mSignalCluster;
-    private SettingsButton mSettingsButton;
-    private View mSettingsContainer;
-    private View mQsDetailHeader;
-    private TextView mQsDetailHeaderTitle;
-    private Switch mQsDetailHeaderSwitch;
-    private ImageView mQsDetailHeaderProgress;
-    private TextView mEmergencyCallsOnly;
-    private TextView mBatteryLevel;
-    private TextView mAlarmStatus;
-
-    private boolean mShowEmergencyCallsOnly;
-    private boolean mAlarmShowing;
-    private AlarmManager.AlarmClockInfo mNextAlarm;
-
-    private int mCollapsedHeight;
-    private int mExpandedHeight;
-
-    private int mMultiUserExpandedMargin;
-    private int mMultiUserCollapsedMargin;
-
-    private int mClockMarginBottomExpanded;
-    private int mClockMarginBottomCollapsed;
-    private int mMultiUserSwitchWidthCollapsed;
-    private int mMultiUserSwitchWidthExpanded;
-
-    private int mClockCollapsedSize;
-    private int mClockExpandedSize;
-
-    /**
-     * In collapsed QS, the clock and avatar are scaled down a bit post-layout to allow for a nice
-     * transition. These values determine that factor.
-     */
-    private float mClockCollapsedScaleFactor;
-    private float mAvatarCollapsedScaleFactor;
-
-    private ActivityStarter mActivityStarter;
-    private BatteryController mBatteryController;
-    private NextAlarmController mNextAlarmController;
-    private QSPanel mQSPanel;
-
-    private final Rect mClipBounds = new Rect();
-
-    private boolean mCaptureValues;
-    private boolean mSignalClusterDetached;
-    private final LayoutValues mCollapsedValues = new LayoutValues();
-    private final LayoutValues mExpandedValues = new LayoutValues();
-    private final LayoutValues mCurrentValues = new LayoutValues();
-
-    private float mCurrentT;
-    private boolean mShowingDetail;
-    private boolean mDetailTransitioning;
-
-    private boolean mAllowExpand = true;
-
-    public StatusBarHeaderView(Context context, AttributeSet attrs) {
-        super(context, attrs);
-    }
-
-    @Override
-    protected void onFinishInflate() {
-        super.onFinishInflate();
-        mSystemIconsSuperContainer = findViewById(R.id.system_icons_super_container);
-        mSystemIconsContainer = (ViewGroup) findViewById(R.id.system_icons_container);
-        mSystemIconsSuperContainer.setOnClickListener(this);
-        mDateGroup = findViewById(R.id.date_group);
-        mClock = findViewById(R.id.clock);
-        mTime = (TextView) findViewById(R.id.time_view);
-        mAmPm = (TextView) findViewById(R.id.am_pm_view);
-        mMultiUserSwitch = (MultiUserSwitch) findViewById(R.id.multi_user_switch);
-        mMultiUserAvatar = (ImageView) findViewById(R.id.multi_user_avatar);
-        mDateCollapsed = (TextView) findViewById(R.id.date_collapsed);
-        mDateExpanded = (TextView) findViewById(R.id.date_expanded);
-        mSettingsButton = (SettingsButton) findViewById(R.id.settings_button);
-        mSettingsContainer = findViewById(R.id.settings_button_container);
-        mSettingsButton.setOnClickListener(this);
-        mQsDetailHeader = findViewById(R.id.qs_detail_header);
-        mQsDetailHeader.setAlpha(0);
-        mQsDetailHeaderTitle = (TextView) mQsDetailHeader.findViewById(android.R.id.title);
-        mQsDetailHeaderSwitch = (Switch) mQsDetailHeader.findViewById(android.R.id.toggle);
-        mQsDetailHeaderProgress = (ImageView) findViewById(R.id.qs_detail_header_progress);
-        mEmergencyCallsOnly = (TextView) findViewById(R.id.header_emergency_calls_only);
-        mBatteryLevel = (TextView) findViewById(R.id.battery_level);
-        mAlarmStatus = (TextView) findViewById(R.id.alarm_status);
-        mAlarmStatus.setOnClickListener(this);
-        mSignalCluster = findViewById(R.id.signal_cluster);
-        mSystemIcons = (LinearLayout) findViewById(R.id.system_icons);
-        loadDimens();
-        updateVisibilities();
-        updateClockScale();
-        updateAvatarScale();
-        addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
-            @Override
-            public void onLayoutChange(View v, int left, int top, int right,
-                    int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
-                if ((right - left) != (oldRight - oldLeft)) {
-                    // width changed, update clipping
-                    setClipping(getHeight());
-                }
-                boolean rtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
-                mTime.setPivotX(rtl ? mTime.getWidth() : 0);
-                mTime.setPivotY(mTime.getBaseline());
-                updateAmPmTranslation();
-            }
-        });
-        setOutlineProvider(new ViewOutlineProvider() {
-            @Override
-            public void getOutline(View view, Outline outline) {
-                outline.setRect(mClipBounds);
-            }
-        });
-        requestCaptureValues();
-
-        // RenderThread is doing more harm than good when touching the header (to expand quick
-        // settings), so disable it for this view
-        ((RippleDrawable) getBackground()).setForceSoftware(true);
-        ((RippleDrawable) mSettingsButton.getBackground()).setForceSoftware(true);
-        ((RippleDrawable) mSystemIconsSuperContainer.getBackground()).setForceSoftware(true);
-    }
-
-    @Override
-    protected void onLayout(boolean changed, int l, int t, int r, int b) {
-        super.onLayout(changed, l, t, r, b);
-        if (mCaptureValues) {
-            if (mExpanded) {
-                captureLayoutValues(mExpandedValues);
-            } else {
-                captureLayoutValues(mCollapsedValues);
-            }
-            mCaptureValues = false;
-            updateLayoutValues(mCurrentT);
-        }
-        mAlarmStatus.setX(mDateGroup.getLeft() + mDateCollapsed.getRight());
-    }
-
-    @Override
-    protected void onConfigurationChanged(Configuration newConfig) {
-        super.onConfigurationChanged(newConfig);
-        FontSizeUtils.updateFontSize(mBatteryLevel, R.dimen.battery_level_text_size);
-        FontSizeUtils.updateFontSize(mEmergencyCallsOnly,
-                R.dimen.qs_emergency_calls_only_text_size);
-        FontSizeUtils.updateFontSize(mDateCollapsed, R.dimen.qs_date_collapsed_size);
-        FontSizeUtils.updateFontSize(mDateExpanded, R.dimen.qs_date_collapsed_size);
-        FontSizeUtils.updateFontSize(mAlarmStatus, R.dimen.qs_date_collapsed_size);
-        FontSizeUtils.updateFontSize(this, android.R.id.title, R.dimen.qs_detail_header_text_size);
-        FontSizeUtils.updateFontSize(this, android.R.id.toggle, R.dimen.qs_detail_header_text_size);
-        FontSizeUtils.updateFontSize(mAmPm, R.dimen.qs_time_collapsed_size);
-        FontSizeUtils.updateFontSize(this, R.id.empty_time_view, R.dimen.qs_time_expanded_size);
-
-        mEmergencyCallsOnly.setText(com.android.internal.R.string.emergency_calls_only);
-
-        mClockCollapsedSize = getResources().getDimensionPixelSize(R.dimen.qs_time_collapsed_size);
-        mClockExpandedSize = getResources().getDimensionPixelSize(R.dimen.qs_time_expanded_size);
-        mClockCollapsedScaleFactor = (float) mClockCollapsedSize / (float) mClockExpandedSize;
-
-        updateClockScale();
-        updateClockCollapsedMargin();
-    }
-
-    private void updateClockCollapsedMargin() {
-        Resources res = getResources();
-        int padding = res.getDimensionPixelSize(R.dimen.clock_collapsed_bottom_margin);
-        int largePadding = res.getDimensionPixelSize(
-                R.dimen.clock_collapsed_bottom_margin_large_text);
-        float largeFactor = (MathUtils.constrain(getResources().getConfiguration().fontScale, 1.0f,
-                FontSizeUtils.LARGE_TEXT_SCALE) - 1f) / (FontSizeUtils.LARGE_TEXT_SCALE - 1f);
-        mClockMarginBottomCollapsed = Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
-        requestLayout();
-    }
-
-    private void requestCaptureValues() {
-        mCaptureValues = true;
-        requestLayout();
-    }
-
-    private void loadDimens() {
-        mCollapsedHeight = getResources().getDimensionPixelSize(R.dimen.status_bar_header_height);
-        mExpandedHeight = getResources().getDimensionPixelSize(
-                R.dimen.status_bar_header_height_expanded);
-        mMultiUserExpandedMargin =
-                getResources().getDimensionPixelSize(R.dimen.multi_user_switch_expanded_margin);
-        mMultiUserCollapsedMargin =
-                getResources().getDimensionPixelSize(R.dimen.multi_user_switch_collapsed_margin);
-        mClockMarginBottomExpanded =
-                getResources().getDimensionPixelSize(R.dimen.clock_expanded_bottom_margin);
-        updateClockCollapsedMargin();
-        mMultiUserSwitchWidthCollapsed =
-                getResources().getDimensionPixelSize(R.dimen.multi_user_switch_width_collapsed);
-        mMultiUserSwitchWidthExpanded =
-                getResources().getDimensionPixelSize(R.dimen.multi_user_switch_width_expanded);
-        mAvatarCollapsedScaleFactor =
-                getResources().getDimensionPixelSize(R.dimen.multi_user_avatar_collapsed_size)
-                / (float) mMultiUserAvatar.getLayoutParams().width;
-        mClockCollapsedSize = getResources().getDimensionPixelSize(R.dimen.qs_time_collapsed_size);
-        mClockExpandedSize = getResources().getDimensionPixelSize(R.dimen.qs_time_expanded_size);
-        mClockCollapsedScaleFactor = (float) mClockCollapsedSize / (float) mClockExpandedSize;
-
-    }
-
-    public void setActivityStarter(ActivityStarter activityStarter) {
-        mActivityStarter = activityStarter;
-    }
-
-    public void setBatteryController(BatteryController batteryController) {
-        mBatteryController = batteryController;
-        ((BatteryMeterView) findViewById(R.id.battery)).setBatteryController(batteryController);
-    }
-
-    public void setNextAlarmController(NextAlarmController nextAlarmController) {
-        mNextAlarmController = nextAlarmController;
-    }
-
-    public int getCollapsedHeight() {
-        return mCollapsedHeight;
-    }
-
-    public int getExpandedHeight() {
-        return mAllowExpand ? mExpandedHeight : mCollapsedHeight;
-    }
-
-    public void setListening(boolean listening) {
-        if (listening == mListening) {
-            return;
-        }
-        mListening = listening;
-        updateListeners();
-    }
-
-    public void setExpanded(boolean expanded) {
-        if (!mAllowExpand) {
-            expanded = false;
-        }
-        boolean changed = expanded != mExpanded;
-        mExpanded = expanded;
-        if (changed) {
-            updateEverything();
-        }
-    }
-
-    public void updateEverything() {
-        updateHeights();
-        updateVisibilities();
-        updateSystemIconsLayoutParams();
-        updateClickTargets();
-        updateMultiUserSwitch();
-        updateClockScale();
-        updateAvatarScale();
-        updateClockLp();
-        requestCaptureValues();
-    }
-
-    private void updateHeights() {
-        int height = mExpanded ? mExpandedHeight : mCollapsedHeight;
-        ViewGroup.LayoutParams lp = getLayoutParams();
-        if (lp.height != height) {
-            lp.height = height;
-            setLayoutParams(lp);
-        }
-    }
-
-    private void updateVisibilities() {
-        mDateCollapsed.setVisibility(mExpanded && mAlarmShowing ? View.VISIBLE : View.INVISIBLE);
-        mDateExpanded.setVisibility(mExpanded && mAlarmShowing ? View.INVISIBLE : View.VISIBLE);
-        mAlarmStatus.setVisibility(mExpanded && mAlarmShowing ? View.VISIBLE : View.INVISIBLE);
-        mSettingsContainer.setVisibility(mExpanded ? View.VISIBLE : View.INVISIBLE);
-        mQsDetailHeader.setVisibility(mExpanded && mShowingDetail? View.VISIBLE : View.INVISIBLE);
-        if (mSignalCluster != null) {
-            updateSignalClusterDetachment();
-        }
-        mEmergencyCallsOnly.setVisibility(mExpanded && mShowEmergencyCallsOnly ? VISIBLE : GONE);
-        mBatteryLevel.setVisibility(mExpanded ? View.VISIBLE : View.GONE);
-        mSettingsContainer.findViewById(R.id.tuner_icon).setVisibility(
-                TunerService.isTunerEnabled(mContext) ? View.VISIBLE : View.INVISIBLE);
-    }
-
-    private void updateSignalClusterDetachment() {
-        boolean detached = mExpanded;
-        if (detached != mSignalClusterDetached) {
-            if (detached) {
-                getOverlay().add(mSignalCluster);
-            } else {
-                reattachSignalCluster();
-            }
-        }
-        mSignalClusterDetached = detached;
-    }
-
-    private void reattachSignalCluster() {
-        getOverlay().remove(mSignalCluster);
-        mSystemIcons.addView(mSignalCluster, 1);
-    }
-
-    private void updateSystemIconsLayoutParams() {
-        RelativeLayout.LayoutParams lp = (LayoutParams) mSystemIconsSuperContainer.getLayoutParams();
-        int rule = mExpanded
-                ? mSettingsContainer.getId()
-                : mMultiUserSwitch.getId();
-        if (rule != lp.getRules()[RelativeLayout.START_OF]) {
-            lp.addRule(RelativeLayout.START_OF, rule);
-            mSystemIconsSuperContainer.setLayoutParams(lp);
-        }
-    }
-
-    private void updateListeners() {
-        if (mListening) {
-            mBatteryController.addStateChangedCallback(this);
-            mNextAlarmController.addStateChangedCallback(this);
-        } else {
-            mBatteryController.removeStateChangedCallback(this);
-            mNextAlarmController.removeStateChangedCallback(this);
-        }
-    }
-
-    private void updateAvatarScale() {
-        if (mExpanded) {
-            mMultiUserAvatar.setScaleX(1f);
-            mMultiUserAvatar.setScaleY(1f);
-        } else {
-            mMultiUserAvatar.setScaleX(mAvatarCollapsedScaleFactor);
-            mMultiUserAvatar.setScaleY(mAvatarCollapsedScaleFactor);
-        }
-    }
-
-    private void updateClockScale() {
-        mTime.setTextSize(TypedValue.COMPLEX_UNIT_PX, mExpanded
-                ? mClockExpandedSize
-                : mClockCollapsedSize);
-        mTime.setScaleX(1f);
-        mTime.setScaleY(1f);
-        updateAmPmTranslation();
-    }
-
-    private void updateAmPmTranslation() {
-        boolean rtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
-        mAmPm.setTranslationX((rtl ? 1 : -1) * mTime.getWidth() * (1 - mTime.getScaleX()));
-    }
-
-    @Override
-    public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
-        String percentage = NumberFormat.getPercentInstance().format((double) level / 100.0);
-        mBatteryLevel.setText(percentage);
-    }
-
-    @Override
-    public void onPowerSaveChanged(boolean isPowerSave) {
-        // could not care less
-    }
-
-    @Override
-    public void onNextAlarmChanged(AlarmManager.AlarmClockInfo nextAlarm) {
-        mNextAlarm = nextAlarm;
-        if (nextAlarm != null) {
-            mAlarmStatus.setText(KeyguardStatusView.formatNextAlarm(getContext(), nextAlarm));
-        }
-        mAlarmShowing = nextAlarm != null;
-        updateEverything();
-        requestCaptureValues();
-    }
-
-    private void updateClickTargets() {
-        mMultiUserSwitch.setClickable(mExpanded);
-        mMultiUserSwitch.setFocusable(mExpanded);
-        mSystemIconsSuperContainer.setClickable(mExpanded);
-        mSystemIconsSuperContainer.setFocusable(mExpanded);
-        mAlarmStatus.setClickable(mNextAlarm != null && mNextAlarm.getShowIntent() != null);
-    }
-
-    private void updateClockLp() {
-        int marginBottom = mExpanded
-                ? mClockMarginBottomExpanded
-                : mClockMarginBottomCollapsed;
-        LayoutParams lp = (LayoutParams) mDateGroup.getLayoutParams();
-        if (marginBottom != lp.bottomMargin) {
-            lp.bottomMargin = marginBottom;
-            mDateGroup.setLayoutParams(lp);
-        }
-    }
-
-    private void updateMultiUserSwitch() {
-        int marginEnd;
-        int width;
-        if (mExpanded) {
-            marginEnd = mMultiUserExpandedMargin;
-            width = mMultiUserSwitchWidthExpanded;
-        } else {
-            marginEnd = mMultiUserCollapsedMargin;
-            width = mMultiUserSwitchWidthCollapsed;
-        }
-        MarginLayoutParams lp = (MarginLayoutParams) mMultiUserSwitch.getLayoutParams();
-        if (marginEnd != lp.getMarginEnd() || lp.width != width) {
-            lp.setMarginEnd(marginEnd);
-            lp.width = width;
-            mMultiUserSwitch.setLayoutParams(lp);
-        }
-    }
-
-    public void setExpansion(float t) {
-        if (!mExpanded) {
-            t = 0f;
-        }
-        mCurrentT = t;
-        float height = mCollapsedHeight + t * (mExpandedHeight - mCollapsedHeight);
-        if (height < mCollapsedHeight) {
-            height = mCollapsedHeight;
-        }
-        if (height > mExpandedHeight) {
-            height = mExpandedHeight;
-        }
-        setClipping(height);
-        updateLayoutValues(t);
-    }
-
-    private void updateLayoutValues(float t) {
-        if (mCaptureValues) {
-            return;
-        }
-        mCurrentValues.interpoloate(mCollapsedValues, mExpandedValues, t);
-        applyLayoutValues(mCurrentValues);
-    }
-
-    private void setClipping(float height) {
-        mClipBounds.set(getPaddingLeft(), 0, getWidth() - getPaddingRight(), (int) height);
-        setClipBounds(mClipBounds);
-        invalidateOutline();
-    }
-
-    public void setUserInfoController(UserInfoController userInfoController) {
-        userInfoController.addListener(new UserInfoController.OnUserInfoChangedListener() {
-            @Override
-            public void onUserInfoChanged(String name, Drawable picture) {
-                mMultiUserAvatar.setImageDrawable(picture);
-            }
-        });
-    }
-
-    @Override
-    public void setCallback(Callback qsPanelCallback) {
-    }
-
-    @Override
-    public void onClick(View v) {
-        if (v == mSettingsButton) {
-            if (mSettingsButton.isTunerClick()) {
-                if (TunerService.isTunerEnabled(mContext)) {
-                    TunerService.showResetRequest(mContext, new Runnable() {
-                        @Override
-                        public void run() {
-                            // Relaunch settings so that the tuner disappears.
-                            startSettingsActivity();
-                        }
-                    });
-                } else {
-                    Toast.makeText(getContext(), R.string.tuner_toast, Toast.LENGTH_LONG).show();
-                    TunerService.setTunerEnabled(mContext, true);
-                }
-            }
-            startSettingsActivity();
-        } else if (v == mSystemIconsSuperContainer) {
-            startBatteryActivity();
-        } else if (v == mAlarmStatus && mNextAlarm != null) {
-            PendingIntent showIntent = mNextAlarm.getShowIntent();
-            if (showIntent != null) {
-                mActivityStarter.startPendingIntentDismissingKeyguard(showIntent);
-            }
-        }
-    }
-
-    private void startSettingsActivity() {
-        mActivityStarter.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS),
-                true /* dismissShade */);
-    }
-
-    private void startBatteryActivity() {
-        mActivityStarter.startActivity(new Intent(Intent.ACTION_POWER_USAGE_SUMMARY),
-                true /* dismissShade */);
-    }
-
-    public void setQSPanel(QSPanel qsp) {
-        mQSPanel = qsp;
-        if (mQSPanel != null) {
-            mQSPanel.setCallback(mQsPanelCallback);
-        }
-        mMultiUserSwitch.setQsPanel(qsp);
-    }
-
-    @Override
-    public boolean shouldDelayChildPressedState() {
-        return true;
-    }
-
-    @Override
-    public void setEmergencyCallsOnly(boolean show) {
-        boolean changed = show != mShowEmergencyCallsOnly;
-        if (changed) {
-            mShowEmergencyCallsOnly = show;
-            if (mExpanded) {
-                updateEverything();
-                requestCaptureValues();
-            }
-        }
-    }
-
-    @Override
-    protected void dispatchSetPressed(boolean pressed) {
-        // We don't want that everything lights up when we click on the header, so block the request
-        // here.
-    }
-
-    private void captureLayoutValues(LayoutValues target) {
-        target.timeScale = mExpanded ? 1f : mClockCollapsedScaleFactor;
-        target.clockY = mClock.getBottom();
-        target.dateY = mDateGroup.getTop();
-        target.emergencyCallsOnlyAlpha = getAlphaForVisibility(mEmergencyCallsOnly);
-        target.alarmStatusAlpha = getAlphaForVisibility(mAlarmStatus);
-        target.dateCollapsedAlpha = getAlphaForVisibility(mDateCollapsed);
-        target.dateExpandedAlpha = getAlphaForVisibility(mDateExpanded);
-        target.avatarScale = mMultiUserAvatar.getScaleX();
-        target.avatarX = mMultiUserSwitch.getLeft() + mMultiUserAvatar.getLeft();
-        target.avatarY = mMultiUserSwitch.getTop() + mMultiUserAvatar.getTop();
-        if (getLayoutDirection() == LAYOUT_DIRECTION_LTR) {
-            target.batteryX = mSystemIconsSuperContainer.getLeft()
-                    + mSystemIconsContainer.getRight();
-        } else {
-            target.batteryX = mSystemIconsSuperContainer.getLeft()
-                    + mSystemIconsContainer.getLeft();
-        }
-        target.batteryY = mSystemIconsSuperContainer.getTop() + mSystemIconsContainer.getTop();
-        target.batteryLevelAlpha = getAlphaForVisibility(mBatteryLevel);
-        target.settingsAlpha = getAlphaForVisibility(mSettingsContainer);
-        target.settingsTranslation = mExpanded
-                ? 0
-                : mMultiUserSwitch.getLeft() - mSettingsContainer.getLeft();
-        target.signalClusterAlpha = mSignalClusterDetached ? 0f : 1f;
-        target.settingsRotation = !mExpanded ? 90f : 0f;
-    }
-
-    private float getAlphaForVisibility(View v) {
-        return v == null || v.getVisibility() == View.VISIBLE ? 1f : 0f;
-    }
-
-    private void applyAlpha(View v, float alpha) {
-        if (v == null || v.getVisibility() == View.GONE) {
-            return;
-        }
-        if (alpha == 0f) {
-            v.setVisibility(View.INVISIBLE);
-        } else {
-            v.setVisibility(View.VISIBLE);
-            v.setAlpha(alpha);
-        }
-    }
-
-    private void applyLayoutValues(LayoutValues values) {
-        mTime.setScaleX(values.timeScale);
-        mTime.setScaleY(values.timeScale);
-        mClock.setY(values.clockY - mClock.getHeight());
-        mDateGroup.setY(values.dateY);
-        mAlarmStatus.setY(values.dateY - mAlarmStatus.getPaddingTop());
-        mMultiUserAvatar.setScaleX(values.avatarScale);
-        mMultiUserAvatar.setScaleY(values.avatarScale);
-        mMultiUserAvatar.setX(values.avatarX - mMultiUserSwitch.getLeft());
-        mMultiUserAvatar.setY(values.avatarY - mMultiUserSwitch.getTop());
-        if (getLayoutDirection() == LAYOUT_DIRECTION_LTR) {
-            mSystemIconsSuperContainer.setX(values.batteryX - mSystemIconsContainer.getRight());
-        } else {
-            mSystemIconsSuperContainer.setX(values.batteryX - mSystemIconsContainer.getLeft());
-        }
-        mSystemIconsSuperContainer.setY(values.batteryY - mSystemIconsContainer.getTop());
-        if (mSignalCluster != null && mExpanded) {
-            if (getLayoutDirection() == LAYOUT_DIRECTION_LTR) {
-                mSignalCluster.setX(mSystemIconsSuperContainer.getX()
-                        - mSignalCluster.getWidth());
-            } else {
-                mSignalCluster.setX(mSystemIconsSuperContainer.getX()
-                        + mSystemIconsSuperContainer.getWidth());
-            }
-            mSignalCluster.setY(
-                    mSystemIconsSuperContainer.getY() + mSystemIconsSuperContainer.getHeight()/2
-                            - mSignalCluster.getHeight()/2);
-        } else if (mSignalCluster != null) {
-            mSignalCluster.setTranslationX(0f);
-            mSignalCluster.setTranslationY(0f);
-        }
-        if (!mSettingsButton.isAnimating()) {
-            mSettingsContainer.setTranslationY(mSystemIconsSuperContainer.getTranslationY());
-            mSettingsContainer.setTranslationX(values.settingsTranslation);
-            mSettingsButton.setRotation(values.settingsRotation);
-        }
-        applyAlpha(mEmergencyCallsOnly, values.emergencyCallsOnlyAlpha);
-        if (!mShowingDetail && !mDetailTransitioning) {
-            // Otherwise it needs to stay invisible
-            applyAlpha(mAlarmStatus, values.alarmStatusAlpha);
-        }
-        applyAlpha(mDateCollapsed, values.dateCollapsedAlpha);
-        applyAlpha(mDateExpanded, values.dateExpandedAlpha);
-        applyAlpha(mBatteryLevel, values.batteryLevelAlpha);
-        applyAlpha(mSettingsContainer, values.settingsAlpha);
-        applyAlpha(mSignalCluster, values.signalClusterAlpha);
-        if (!mExpanded) {
-            mTime.setScaleX(1f);
-            mTime.setScaleY(1f);
-        }
-        updateAmPmTranslation();
-    }
-
-    /**
-     * Captures all layout values (position, visibility) for a certain state. This is used for
-     * animations.
-     */
-    private static final class LayoutValues {
-
-        float dateExpandedAlpha;
-        float dateCollapsedAlpha;
-        float emergencyCallsOnlyAlpha;
-        float alarmStatusAlpha;
-        float timeScale = 1f;
-        float clockY;
-        float dateY;
-        float avatarScale;
-        float avatarX;
-        float avatarY;
-        float batteryX;
-        float batteryY;
-        float batteryLevelAlpha;
-        float settingsAlpha;
-        float settingsTranslation;
-        float signalClusterAlpha;
-        float settingsRotation;
-
-        public void interpoloate(LayoutValues v1, LayoutValues v2, float t) {
-            timeScale = v1.timeScale * (1 - t) + v2.timeScale * t;
-            clockY = v1.clockY * (1 - t) + v2.clockY * t;
-            dateY = v1.dateY * (1 - t) + v2.dateY * t;
-            avatarScale = v1.avatarScale * (1 - t) + v2.avatarScale * t;
-            avatarX = v1.avatarX * (1 - t) + v2.avatarX * t;
-            avatarY = v1.avatarY * (1 - t) + v2.avatarY * t;
-            batteryX = v1.batteryX * (1 - t) + v2.batteryX * t;
-            batteryY = v1.batteryY * (1 - t) + v2.batteryY * t;
-            settingsTranslation = v1.settingsTranslation * (1 - t) + v2.settingsTranslation * t;
-
-            float t1 = Math.max(0, t - 0.5f) * 2;
-            settingsRotation = v1.settingsRotation * (1 - t1) + v2.settingsRotation * t1;
-            emergencyCallsOnlyAlpha =
-                    v1.emergencyCallsOnlyAlpha * (1 - t1) + v2.emergencyCallsOnlyAlpha * t1;
-
-            float t2 = Math.min(1, 2 * t);
-            signalClusterAlpha = v1.signalClusterAlpha * (1 - t2) + v2.signalClusterAlpha * t2;
-
-            float t3 = Math.max(0, t - 0.7f) / 0.3f;
-            batteryLevelAlpha = v1.batteryLevelAlpha * (1 - t3) + v2.batteryLevelAlpha * t3;
-            settingsAlpha = v1.settingsAlpha * (1 - t3) + v2.settingsAlpha * t3;
-            dateExpandedAlpha = v1.dateExpandedAlpha * (1 - t3) + v2.dateExpandedAlpha * t3;
-            dateCollapsedAlpha = v1.dateCollapsedAlpha * (1 - t3) + v2.dateCollapsedAlpha * t3;
-            alarmStatusAlpha = v1.alarmStatusAlpha * (1 - t3) + v2.alarmStatusAlpha * t3;
-        }
-    }
-
-    private final QSPanel.Callback mQsPanelCallback = new QSPanel.Callback() {
-        private boolean mScanState;
-
-        @Override
-        public void onToggleStateChanged(final boolean state) {
-            post(new Runnable() {
-                @Override
-                public void run() {
-                    handleToggleStateChanged(state);
-                }
-            });
-        }
-
-        @Override
-        public void onShowingDetail(final DetailAdapter detail, int x, int y) {
-            mDetailTransitioning = true;
-            post(new Runnable() {
-                @Override
-                public void run() {
-                    handleShowingDetail(detail);
-                }
-            });
-        }
-
-        @Override
-        public void onScanStateChanged(final boolean state) {
-            post(new Runnable() {
-                @Override
-                public void run() {
-                    handleScanStateChanged(state);
-                }
-            });
-        }
-
-        private void handleToggleStateChanged(boolean state) {
-            mQsDetailHeaderSwitch.setChecked(state);
-        }
-
-        private void handleScanStateChanged(boolean state) {
-            if (mScanState == state) return;
-            mScanState = state;
-            final Animatable anim = (Animatable) mQsDetailHeaderProgress.getDrawable();
-            if (state) {
-                mQsDetailHeaderProgress.animate().alpha(1f);
-                anim.start();
-            } else {
-                mQsDetailHeaderProgress.animate().alpha(0f);
-                anim.stop();
-            }
-        }
-
-        private void handleShowingDetail(final QSTile.DetailAdapter detail) {
-            final boolean showingDetail = detail != null;
-            transition(mClock, !showingDetail);
-            transition(mDateGroup, !showingDetail);
-            if (mAlarmShowing) {
-                transition(mAlarmStatus, !showingDetail);
-            }
-            transition(mQsDetailHeader, showingDetail);
-            mShowingDetail = showingDetail;
-            if (showingDetail) {
-                mQsDetailHeaderTitle.setText(detail.getTitle());
-                final Boolean toggleState = detail.getToggleState();
-                if (toggleState == null) {
-                    mQsDetailHeaderSwitch.setVisibility(INVISIBLE);
-                    mQsDetailHeader.setClickable(false);
-                } else {
-                    mQsDetailHeaderSwitch.setVisibility(VISIBLE);
-                    mQsDetailHeaderSwitch.setChecked(toggleState);
-                    mQsDetailHeader.setClickable(true);
-                    mQsDetailHeader.setOnClickListener(new OnClickListener() {
-                        @Override
-                        public void onClick(View v) {
-                            boolean checked = !mQsDetailHeaderSwitch.isChecked();
-                            mQsDetailHeaderSwitch.setChecked(checked);
-                            detail.setToggleState(checked);
-                        }
-                    });
-                }
-            } else {
-                mQsDetailHeader.setClickable(false);
-            }
-        }
-
-        private void transition(final View v, final boolean in) {
-            if (in) {
-                v.bringToFront();
-                v.setVisibility(VISIBLE);
-            }
-            if (v.hasOverlappingRendering()) {
-                v.animate().withLayer();
-            }
-            v.animate()
-                    .alpha(in ? 1 : 0)
-                    .withEndAction(new Runnable() {
-                        @Override
-                        public void run() {
-                            if (!in) {
-                                v.setVisibility(INVISIBLE);
-                            }
-                            mDetailTransitioning = false;
-                        }
-                    })
-                    .start();
-        }
-    };
-}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconController.java
index 716a44e..3326736 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconController.java
@@ -334,11 +334,12 @@
 
     public void dump(PrintWriter pw) {
         int N = mStatusIcons.getChildCount();
-        pw.println("  system icons: " + N);
+        pw.println("  icon views: " + N);
         for (int i=0; i<N; i++) {
             StatusBarIconView ic = (StatusBarIconView) mStatusIcons.getChildAt(i);
             pw.println("    [" + i + "] icon=" + ic);
         }
+        super.dump(pw);
     }
 
     public void dispatchDemoCommand(String command, Bundle args) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconList.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconList.java
index 94adea0..660672d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconList.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarIconList.java
@@ -78,9 +78,9 @@
 
     public void dump(PrintWriter pw) {
         final int N = mSlots.size();
-        pw.println("Icon list:");
+        pw.println("  icon slots: " + N);
         for (int i=0; i<N; i++) {
-            pw.printf("  %2d: (%s) %s\n", i, mSlots.get(i), mIcons.get(i));
+            pw.printf("    %2d: (%s) %s\n", i, mSlots.get(i), mIcons.get(i));
         }
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java
index 8d0d9cb..664e103 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarKeyguardViewManager.java
@@ -53,6 +53,11 @@
 
     private static final long WAKE_AND_UNLOCK_SCRIM_FADEOUT_DURATION_MS = 200;
 
+    // Duration of the Keyguard dismissal animation in case the user is currently locked. This is to
+    // make everything a bit slower to bridge a gap until the user is unlocked and home screen has
+    // dranw its first frame.
+    private static final long KEYGUARD_DISMISS_DURATION_LOCKED = 2000;
+
     private static String TAG = "StatusBarKeyguardViewManager";
 
     protected final Context mContext;
@@ -180,13 +185,17 @@
     }
 
     public void onStartedWakingUp() {
+        Trace.beginSection("StatusBarKeyguardViewManager#onStartedWakingUp");
         mDeviceInteractive = true;
         mDeviceWillWakeUp = false;
         mPhoneStatusBar.onStartedWakingUp();
+        Trace.endSection();
     }
 
     public void onScreenTurningOn() {
+        Trace.beginSection("StatusBarKeyguardViewManager#onScreenTurningOn");
         mPhoneStatusBar.onScreenTurningOn();
+        Trace.endSection();
     }
 
     public boolean isScreenTurnedOn() {
@@ -194,6 +203,7 @@
     }
 
     public void onScreenTurnedOn() {
+        Trace.beginSection("StatusBarKeyguardViewManager#onScreenTurnedOn");
         mScreenTurnedOn = true;
         if (mDeferScrimFadeOut) {
             mDeferScrimFadeOut = false;
@@ -202,6 +212,7 @@
             updateStates();
         }
         mPhoneStatusBar.onScreenTurnedOn();
+        Trace.endSection();
     }
 
     @Override
@@ -231,7 +242,7 @@
         return mStatusBarWindowManager.isShowingWallpaper();
     }
 
-    public void setOccluded(boolean occluded) {
+    public void setOccluded(boolean occluded, boolean animate) {
         if (occluded && !mOccluded && mShowing) {
             if (mPhoneStatusBar.isInLaunchTransition()) {
                 mOccluded = true;
@@ -247,9 +258,12 @@
             }
         }
         mOccluded = occluded;
-        mPhoneStatusBar.updateMediaMetaData(false, false);
+        mPhoneStatusBar.updateMediaMetaData(false, animate && !occluded);
         mStatusBarWindowManager.setKeyguardOccluded(occluded);
         reset();
+        if (animate && !occluded) {
+            mPhoneStatusBar.animateKeyguardUnoccluding();
+        }
     }
 
     public boolean isOccluded() {
@@ -274,9 +288,12 @@
     /**
      * Hides the keyguard view
      */
-    public void hide(long startTime, final long fadeoutDuration) {
+    public void hide(long startTime, long fadeoutDuration) {
         mShowing = false;
 
+        if (KeyguardUpdateMonitor.getInstance(mContext).needsSlowUnlockTransition()) {
+            fadeoutDuration = KEYGUARD_DISMISS_DURATION_LOCKED;
+        }
         long uptimeMillis = SystemClock.uptimeMillis();
         long delay = Math.max(0, startTime + HIDE_TIMING_CORRECTION_MS - uptimeMillis);
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java
index 888e19c..a7a792b 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowManager.java
@@ -16,11 +16,16 @@
 
 package com.android.systemui.statusbar.phone;
 
+import android.app.ActivityManagerNative;
+import android.app.IActivityManager;
 import android.content.Context;
 import android.content.pm.ActivityInfo;
 import android.content.res.Resources;
 import android.graphics.PixelFormat;
+import android.os.RemoteException;
 import android.os.SystemProperties;
+import android.os.Trace;
+import android.util.Log;
 import android.view.Gravity;
 import android.view.View;
 import android.view.ViewGroup;
@@ -41,11 +46,16 @@
  */
 public class StatusBarWindowManager implements RemoteInputController.Callback {
 
+    private static final String TAG = "StatusBarWindowManager";
+
     private final Context mContext;
     private final WindowManager mWindowManager;
+    private final IActivityManager mActivityManager;
     private View mStatusBarView;
     private WindowManager.LayoutParams mLp;
     private WindowManager.LayoutParams mLpChanged;
+    private boolean mHasTopUi;
+    private boolean mHasTopUiChanged;
     private int mBarHeight;
     private final boolean mKeyguardScreenRotation;
     private final float mScreenBrightnessDoze;
@@ -54,6 +64,7 @@
     public StatusBarWindowManager(Context context) {
         mContext = context;
         mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
+        mActivityManager = ActivityManagerNative.getDefault();
         mKeyguardScreenRotation = shouldEnableKeyguardScreenRotation();
         mScreenBrightnessDoze = mContext.getResources().getInteger(
                 com.android.internal.R.integer.config_screenBrightnessDoze) / 255f;
@@ -157,7 +168,11 @@
     }
 
     private void applyFitsSystemWindows(State state) {
-        mStatusBarView.setFitsSystemWindows(!state.isKeyguardShowingAndNotOccluded());
+        boolean fitsSystemWindows = !state.isKeyguardShowingAndNotOccluded();
+        if (mStatusBarView.getFitsSystemWindows() != fitsSystemWindows) {
+            mStatusBarView.setFitsSystemWindows(fitsSystemWindows);
+            mStatusBarView.requestApplyInsets();
+        }
     }
 
     private void applyUserActivityTimeout(State state) {
@@ -193,9 +208,18 @@
         applyFitsSystemWindows(state);
         applyModalFlag(state);
         applyBrightness(state);
+        applyHasTopUi(state);
         if (mLp.copyFrom(mLpChanged) != 0) {
             mWindowManager.updateViewLayout(mStatusBarView, mLp);
         }
+        if (mHasTopUi != mHasTopUiChanged) {
+            try {
+                mActivityManager.setHasTopUi(mHasTopUiChanged);
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to call setHasTopUi", e);
+            }
+            mHasTopUi = mHasTopUiChanged;
+        }
     }
 
     private void applyForceStatusBarVisibleFlag(State state) {
@@ -224,6 +248,10 @@
         }
     }
 
+    private void applyHasTopUi(State state) {
+        mHasTopUiChanged = isExpanded(state);
+    }
+
     public void setKeyguardShowing(boolean showing) {
         mCurrentState.keyguardShowing = showing;
         apply(mCurrentState);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java
index ebfa018..47ea59e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarWindowView.java
@@ -70,6 +70,7 @@
     private View mBrightnessMirror;
 
     private int mRightInset = 0;
+    private int mLeftInset = 0;
 
     private PhoneStatusBar mService;
     private final Paint mTransparentSrcPaint = new Paint();
@@ -93,25 +94,26 @@
     @Override
     protected boolean fitSystemWindows(Rect insets) {
         if (getFitsSystemWindows()) {
-            boolean paddingChanged = insets.left != getPaddingLeft()
-                    || insets.top != getPaddingTop()
+            boolean paddingChanged = insets.top != getPaddingTop()
                     || insets.bottom != getPaddingBottom();
 
             // Super-special right inset handling, because scrims and backdrop need to ignore it.
-            if (insets.right != mRightInset) {
+            if (insets.right != mRightInset || insets.left != mLeftInset) {
                 mRightInset = insets.right;
+                mLeftInset = insets.left;
                 applyMargins();
             }
-            // Drop top inset, apply left inset and pass through bottom inset.
+            // Drop top inset, and pass through bottom inset.
             if (paddingChanged) {
-                setPadding(insets.left, 0, 0, 0);
+                setPadding(0, 0, 0, 0);
             }
             insets.left = 0;
             insets.top = 0;
             insets.right = 0;
         } else {
-            if (mRightInset != 0) {
+            if (mRightInset != 0 || mLeftInset != 0) {
                 mRightInset = 0;
+                mLeftInset = 0;
                 applyMargins();
             }
             boolean changed = getPaddingLeft() != 0
@@ -127,13 +129,16 @@
     }
 
     private void applyMargins() {
+        mService.mScrimController.setLeftInset(mLeftInset);
         final int N = getChildCount();
         for (int i = 0; i < N; i++) {
             View child = getChildAt(i);
             if (child.getLayoutParams() instanceof LayoutParams) {
                 LayoutParams lp = (LayoutParams) child.getLayoutParams();
-                if (!lp.ignoreRightInset && lp.rightMargin != mRightInset) {
+                if (!lp.ignoreRightInset
+                        && (lp.rightMargin != mRightInset || lp.leftMargin != mLeftInset)) {
                     lp.rightMargin = mRightInset;
+                    lp.leftMargin = mLeftInset;
                     child.requestLayout();
                 }
             }
@@ -185,6 +190,12 @@
 
     @Override
     public boolean dispatchKeyEvent(KeyEvent event) {
+        if (mService.interceptMediaKey(event)) {
+            return true;
+        }
+        if (super.dispatchKeyEvent(event)) {
+            return true;
+        }
         boolean down = event.getAction() == KeyEvent.ACTION_DOWN;
         switch (event.getKeyCode()) {
             case KeyEvent.KEYCODE_BACK:
@@ -209,10 +220,7 @@
                 }
                 break;
         }
-        if (mService.interceptMediaKey(event)) {
-            return true;
-        }
-        return super.dispatchKeyEvent(event);
+        return false;
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/UnlockMethodCache.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/UnlockMethodCache.java
index a91cd51..7e92edf 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/UnlockMethodCache.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/UnlockMethodCache.java
@@ -17,6 +17,7 @@
 package com.android.systemui.statusbar.phone;
 
 import android.content.Context;
+import android.os.Trace;
 
 import com.android.internal.widget.LockPatternUtils;
 import com.android.keyguard.KeyguardUpdateMonitor;
@@ -85,6 +86,7 @@
     }
 
     private void update(boolean updateAlways) {
+        Trace.beginSection("UnlockMethodCache#update");
         int user = KeyguardUpdateMonitor.getCurrentUser();
         boolean secure = mLockPatternUtils.isSecure(user);
         boolean canSkipBouncer = !secure ||  mKeyguardUpdateMonitor.getUserCanSkipBouncer(user);
@@ -102,6 +104,7 @@
             mFaceUnlockRunning = faceUnlockRunning;
             notifyListeners();
         }
+        Trace.endSection();
     }
 
     private void notifyListeners() {
@@ -133,10 +136,13 @@
 
         @Override
         public void onFingerprintAuthenticated(int userId) {
+            Trace.beginSection("KeyguardUpdateMonitorCallback#onFingerprintAuthenticated");
             if (!mKeyguardUpdateMonitor.isUnlockingWithFingerprintAllowed()) {
+                Trace.endSection();
                 return;
             }
             update(false /* updateAlways */);
+            Trace.endSection();
         }
 
         @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/touch_analytics.proto b/packages/SystemUI/src/com/android/systemui/statusbar/phone/touch_analytics.proto
index afc8f77..50fd52a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/touch_analytics.proto
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/touch_analytics.proto
@@ -120,6 +120,7 @@
         RESERVED_2 = 1;
         RANDOM_WAKEUP = 2;
         REAL = 3;
+        REJECTED_TOUCH_REPORT = 4;
     }
 
     optional uint64 startTimestampMillis = 1;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
index 5d734c6..6726c92 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryControllerImpl.java
@@ -53,6 +53,7 @@
     protected boolean mCharged;
     protected boolean mPowerSave;
     private boolean mTestmode = false;
+    private boolean mHasReceivedBattery = false;
 
     public BatteryControllerImpl(Context context) {
         mContext = context;
@@ -89,14 +90,19 @@
 
     @Override
     public void addStateChangedCallback(BatteryController.BatteryStateChangeCallback cb) {
-        mChangeCallbacks.add(cb);
+        synchronized (mChangeCallbacks) {
+            mChangeCallbacks.add(cb);
+        }
+        if (!mHasReceivedBattery) return;
         cb.onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
         cb.onPowerSaveChanged(mPowerSave);
     }
 
     @Override
     public void removeStateChangedCallback(BatteryController.BatteryStateChangeCallback cb) {
-        mChangeCallbacks.remove(cb);
+        synchronized (mChangeCallbacks) {
+            mChangeCallbacks.remove(cb);
+        }
     }
 
     @Override
@@ -104,6 +110,7 @@
         final String action = intent.getAction();
         if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
             if (mTestmode && !intent.getBooleanExtra("testmode", false)) return;
+            mHasReceivedBattery = true;
             mLevel = (int)(100f
                     * intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
                     / intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100));
@@ -171,16 +178,20 @@
     }
 
     protected void fireBatteryLevelChanged() {
-        final int N = mChangeCallbacks.size();
-        for (int i = 0; i < N; i++) {
-            mChangeCallbacks.get(i).onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
+        synchronized (mChangeCallbacks) {
+            final int N = mChangeCallbacks.size();
+            for (int i = 0; i < N; i++) {
+                mChangeCallbacks.get(i).onBatteryLevelChanged(mLevel, mPluggedIn, mCharging);
+            }
         }
     }
 
     private void firePowerSaveChanged() {
-        final int N = mChangeCallbacks.size();
-        for (int i = 0; i < N; i++) {
-            mChangeCallbacks.get(i).onPowerSaveChanged(mPowerSave);
+        synchronized (mChangeCallbacks) {
+            final int N = mChangeCallbacks.size();
+            for (int i = 0; i < N; i++) {
+                mChangeCallbacks.get(i).onPowerSaveChanged(mPowerSave);
+            }
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothController.java
index e7e2ac2..08675c4 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothController.java
@@ -26,6 +26,9 @@
 
     boolean isBluetoothSupported();
     boolean isBluetoothEnabled();
+
+    int getBluetoothState();
+
     boolean isBluetoothConnected();
     boolean isBluetoothConnecting();
     String getLastDeviceName();
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
index 014cc49..4f880b4 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/BluetoothControllerImpl.java
@@ -49,6 +49,7 @@
     private CachedBluetoothDevice mLastDevice;
 
     private final H mHandler = new H();
+    private int mState;
 
     public BluetoothControllerImpl(Context context, Looper bgLooper) {
         mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, null);
@@ -120,6 +121,11 @@
     }
 
     @Override
+    public int getBluetoothState() {
+        return mState;
+    }
+
+    @Override
     public boolean isBluetoothConnected() {
         return mConnectionState == BluetoothAdapter.STATE_CONNECTED;
     }
@@ -192,7 +198,9 @@
 
     @Override
     public void onBluetoothStateChanged(int bluetoothState) {
-        mEnabled = bluetoothState == BluetoothAdapter.STATE_ON;
+        mEnabled = bluetoothState == BluetoothAdapter.STATE_ON
+                || bluetoothState == BluetoothAdapter.STATE_TURNING_ON;
+        mState = bluetoothState;
         mHandler.sendEmptyMessage(H.MSG_STATE_CHANGED);
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/EmergencyCryptkeeperText.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/EmergencyCryptkeeperText.java
new file mode 100644
index 0000000..8abfb89
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/EmergencyCryptkeeperText.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.systemui.statusbar.policy;
+
+import android.annotation.Nullable;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.provider.Settings;
+import android.telephony.ServiceState;
+import android.telephony.SubscriptionInfo;
+import android.text.TextUtils;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.view.ViewGroup;
+import android.view.ViewParent;
+import android.widget.TextView;
+
+import com.android.internal.telephony.IccCardConstants;
+import com.android.internal.telephony.TelephonyIntents;
+import com.android.keyguard.KeyguardUpdateMonitor;
+import com.android.keyguard.KeyguardUpdateMonitorCallback;
+
+import java.util.List;
+
+public class EmergencyCryptkeeperText extends TextView {
+
+    private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
+    private KeyguardUpdateMonitorCallback mCallback = new KeyguardUpdateMonitorCallback() {
+        @Override
+        public void onPhoneStateChanged(int phoneState) {
+            update();
+        }
+    };
+
+    public EmergencyCryptkeeperText(Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+        setVisibility(GONE);
+    }
+
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        mKeyguardUpdateMonitor = KeyguardUpdateMonitor.getInstance(mContext);
+        mKeyguardUpdateMonitor.registerCallback(mCallback);
+        update();
+    }
+
+    @Override
+    protected void onDetachedFromWindow() {
+        super.onDetachedFromWindow();
+        if (mKeyguardUpdateMonitor != null) {
+            mKeyguardUpdateMonitor.removeCallback(mCallback);
+        }
+    }
+
+    public void update() {
+        boolean hasMobile = ConnectivityManager.from(mContext)
+                .isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
+        boolean airplaneMode = (Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.AIRPLANE_MODE_ON, 0) == 1);
+
+        if (!hasMobile || airplaneMode) {
+            setText(null);
+            setVisibility(GONE);
+            return;
+        }
+
+        boolean allSimsMissing = true;
+        CharSequence displayText = null;
+
+        List<SubscriptionInfo> subs = mKeyguardUpdateMonitor.getSubscriptionInfo(false);
+        final int N = subs.size();
+        for (int i = 0; i < N; i++) {
+            int subId = subs.get(i).getSubscriptionId();
+            IccCardConstants.State simState = mKeyguardUpdateMonitor.getSimState(subId);
+            CharSequence carrierName = subs.get(i).getCarrierName();
+            if (simState.iccCardExist() && !TextUtils.isEmpty(carrierName)) {
+                allSimsMissing = false;
+                displayText = carrierName;
+            }
+        }
+        if (allSimsMissing) {
+            if (N != 0) {
+                // Shows "Emergency calls only" on devices that are voice-capable.
+                // This depends on mPlmn containing the text "Emergency calls only" when the radio
+                // has some connectivity. Otherwise it should show "No service"
+                // Grab the first subscription, because they all should contain the emergency text,
+                // described above.
+                displayText = subs.get(0).getCarrierName();
+            } else {
+                // We don't have a SubscriptionInfo to get the emergency calls only from.
+                // Grab it from the old sticky broadcast if possible instead. We can use it
+                // here because no subscriptions are active, so we don't have
+                // to worry about MSIM clashing.
+                displayText = getContext().getText(
+                        com.android.internal.R.string.emergency_calls_only);
+                Intent i = getContext().registerReceiver(null,
+                        new IntentFilter(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION));
+                if (i != null) {
+                    displayText = i.getStringExtra(TelephonyIntents.EXTRA_PLMN);
+                }
+            }
+        }
+
+        setText(displayText);
+        setVisibility(TextUtils.isEmpty(displayText) ? GONE : VISIBLE);
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/EncryptionHelper.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/EncryptionHelper.java
new file mode 100644
index 0000000..639e50c
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/EncryptionHelper.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.systemui.statusbar.policy;
+
+import android.os.SystemProperties;
+
+/**
+ * Helper for determining whether the phone is decrypted yet.
+ */
+public class EncryptionHelper {
+
+    public static final boolean IS_DATA_ENCRYPTED = isDataEncrypted();
+
+    private static boolean isDataEncrypted() {
+        String voldState = SystemProperties.get("vold.decrypt");
+        return "1".equals(voldState) || "trigger_restart_min_framework".equals(voldState);
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java
index 9a21a1e..4e9fc76 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/FlashlightController.java
@@ -17,6 +17,7 @@
 package com.android.systemui.statusbar.policy;
 
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.hardware.camera2.CameraAccessException;
 import android.hardware.camera2.CameraCharacteristics;
 import android.hardware.camera2.CameraManager;
@@ -26,6 +27,8 @@
 import android.text.TextUtils;
 import android.util.Log;
 
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 
@@ -42,6 +45,7 @@
     private static final int DISPATCH_AVAILABILITY_CHANGED = 2;
 
     private final CameraManager mCameraManager;
+    private final Context mContext;
     /** Call {@link #ensureHandler()} before using */
     private Handler mHandler;
 
@@ -51,20 +55,22 @@
     /** Lock on {@code this} when accessing */
     private boolean mFlashlightEnabled;
 
-    private final String mCameraId;
+    private String mCameraId;
     private boolean mTorchAvailable;
 
-    public FlashlightController(Context mContext) {
+    public FlashlightController(Context context) {
+        mContext = context;
         mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
 
-        String cameraId = null;
+        tryInitCamera();
+    }
+
+    private void tryInitCamera() {
         try {
-            cameraId = getCameraId();
+            mCameraId = getCameraId();
         } catch (Throwable e) {
             Log.e(TAG, "Couldn't initialize.", e);
             return;
-        } finally {
-            mCameraId = cameraId;
         }
 
         if (mCameraId != null) {
@@ -76,6 +82,7 @@
     public void setFlashlight(boolean enabled) {
         boolean pendingError = false;
         synchronized (this) {
+            if (mCameraId == null) return;
             if (mFlashlightEnabled != enabled) {
                 mFlashlightEnabled = enabled;
                 try {
@@ -94,7 +101,7 @@
     }
 
     public boolean hasFlashlight() {
-        return mCameraId != null;
+        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
     }
 
     public synchronized boolean isEnabled() {
@@ -107,6 +114,9 @@
 
     public void addListener(FlashlightListener l) {
         synchronized (mListeners) {
+            if (mCameraId == null) {
+                tryInitCamera();
+            }
             cleanUpListenersLocked(l);
             mListeners.add(new WeakReference<>(l));
         }
@@ -228,6 +238,17 @@
         }
     };
 
+    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.println("FlashlightController state:");
+
+        pw.print("  mCameraId=");
+        pw.println(mCameraId);
+        pw.print("  mFlashlightEnabled=");
+        pw.println(mFlashlightEnabled);
+        pw.print("  mTorchAvailable=");
+        pw.println(mTorchAvailable);
+    }
+
     public interface FlashlightListener {
 
         /**
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManager.java
index d3ae549..f6c0942 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManager.java
@@ -381,7 +381,7 @@
     }
 
     public void onComputeInternalInsets(ViewTreeObserver.InternalInsetsInfo info) {
-        if (mIsExpanded) {
+        if (mIsExpanded || mBar.isBouncerShowing()) {
             // The touchable region is always the full area when expanded
             return;
         }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
index c8c824a..e6066aa 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyButtonView.java
@@ -16,6 +16,8 @@
 
 package com.android.systemui.statusbar.policy;
 
+import android.annotation.DrawableRes;
+import android.annotation.Nullable;
 import android.app.ActivityManager;
 import android.content.Context;
 import android.content.res.Configuration;
@@ -42,11 +44,12 @@
 import android.widget.ImageView;
 
 import com.android.systemui.R;
+import com.android.systemui.statusbar.phone.ButtonDispatcher;
 
 import static android.view.accessibility.AccessibilityNodeInfo.ACTION_CLICK;
 import static android.view.accessibility.AccessibilityNodeInfo.ACTION_LONG_CLICK;
 
-public class KeyButtonView extends ImageView {
+public class KeyButtonView extends ImageView implements ButtonDispatcher.ButtonInterface {
 
     private int mContentDescriptionRes;
     private long mDownTime;
@@ -56,6 +59,7 @@
     private AudioManager mAudioManager;
     private boolean mGestureAborted;
     private boolean mLongClicked;
+    private OnClickListener mOnClickListener;
 
     private final Runnable mCheckLongPress = new Runnable() {
         public void run() {
@@ -106,6 +110,12 @@
         mCode = code;
     }
 
+    @Override
+    public void setOnClickListener(OnClickListener onClickListener) {
+        super.setOnClickListener(onClickListener);
+        mOnClickListener = onClickListener;
+    }
+
     public void loadAsync(String uri) {
         new AsyncTask<String, Void, Drawable>() {
             @Override
@@ -187,6 +197,7 @@
                     // Provide the same haptic feedback that the system offers for virtual keys.
                     performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
                 }
+                playSoundEffect(SoundEffectConstants.CLICK);
                 removeCallbacks(mCheckLongPress);
                 postDelayed(mCheckLongPress, ViewConfiguration.getLongPressTimeout());
                 break;
@@ -212,14 +223,14 @@
                     if (doIt) {
                         sendEvent(KeyEvent.ACTION_UP, 0);
                         sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
-                        playSoundEffect(SoundEffectConstants.CLICK);
                     } else {
                         sendEvent(KeyEvent.ACTION_UP, KeyEvent.FLAG_CANCELED);
                     }
                 } else {
                     // no key code, just a regular ImageView
-                    if (doIt) {
-                        performClick();
+                    if (doIt && mOnClickListener != null) {
+                        mOnClickListener.onClick(this);
+                        sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
                     }
                 }
                 removeCallbacks(mCheckLongPress);
@@ -247,10 +258,31 @@
                 InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);
     }
 
+    @Override
     public void abortCurrentGesture() {
         setPressed(false);
         mGestureAborted = true;
     }
+
+    @Override
+    public void setImageResource(@DrawableRes int resId) {
+        super.setImageResource(resId);
+    }
+
+    @Override
+    public void setImageDrawable(@Nullable Drawable drawable) {
+        super.setImageDrawable(drawable);
+    }
+
+    @Override
+    public void setLandscape(boolean landscape) {
+        //no op
+    }
+
+    @Override
+    public void setCarMode(boolean carMode) {
+        // no op
+    }
 }
 
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardMonitor.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardMonitor.java
index 970fed0..44816f9 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardMonitor.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/KeyguardMonitor.java
@@ -38,6 +38,7 @@
     private int mCurrentUser;
     private boolean mShowing;
     private boolean mSecure;
+    private boolean mOccluded;
     private boolean mCanSkipBouncer;
 
     private boolean mListening;
@@ -81,6 +82,10 @@
         return mSecure;
     }
 
+    public boolean isOccluded() {
+        return mOccluded;
+    }
+
     public boolean canSkipBouncer() {
         return mCanSkipBouncer;
     }
@@ -99,10 +104,11 @@
         }
     }
 
-    public void notifyKeyguardState(boolean showing, boolean secure) {
-        if (mShowing == showing && mSecure == secure) return;
+    public void notifyKeyguardState(boolean showing, boolean secure, boolean occluded) {
+        if (mShowing == showing && mSecure == secure && mOccluded == occluded) return;
         mShowing = showing;
         mSecure = secure;
+        mOccluded = occluded;
         notifyKeyguardChanged();
     }
 
@@ -112,6 +118,10 @@
         notifyKeyguardChanged();
     }
 
+    public boolean isDeviceInteractive() {
+        return mKeyguardUpdateMonitor.isDeviceInteractive();
+    }
+
     private void updateCanSkipBouncerState() {
         mCanSkipBouncer = mKeyguardUpdateMonitor.getUserCanSkipBouncer(mCurrentUser);
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
index 83e25eb..83463e2 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/MobileSignalController.java
@@ -197,8 +197,22 @@
 
         if (mConfig.show4gForLte) {
             mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE, TelephonyIcons.FOUR_G);
+            if (mConfig.hideLtePlus) {
+                mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE_CA,
+                        TelephonyIcons.FOUR_G);
+            } else {
+                mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE_CA,
+                        TelephonyIcons.FOUR_G_PLUS);
+            }
         } else {
             mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE, TelephonyIcons.LTE);
+            if (mConfig.hideLtePlus) {
+                mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE_CA,
+                        TelephonyIcons.LTE);
+            } else {
+                mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_LTE_CA,
+                        TelephonyIcons.LTE_PLUS);
+            }
         }
         mNetworkToIconLookup.put(TelephonyManager.NETWORK_TYPE_IWLAN, TelephonyIcons.WFC);
     }
@@ -457,6 +471,10 @@
             }
             mServiceState = state;
             mDataNetType = state.getDataNetworkType();
+            if (mDataNetType == TelephonyManager.NETWORK_TYPE_LTE && mServiceState != null &&
+                    mServiceState.isUsingCarrierAggregation()) {
+                mDataNetType = TelephonyManager.NETWORK_TYPE_LTE_CA;
+            }
             updateTelephony();
         }
 
@@ -468,6 +486,10 @@
             }
             mDataState = state;
             mDataNetType = networkType;
+            if (mDataNetType == TelephonyManager.NETWORK_TYPE_LTE && mServiceState != null &&
+                    mServiceState.isUsingCarrierAggregation()) {
+                mDataNetType = TelephonyManager.NETWORK_TYPE_LTE_CA;
+            }
             updateTelephony();
         }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java
index 348e0b0..5f1b871 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkController.java
@@ -41,20 +41,20 @@
     void removeEmergencyListener(EmergencyListener listener);
 
     public interface SignalCallback {
-        void setWifiIndicators(boolean enabled, IconState statusIcon, IconState qsIcon,
-                boolean activityIn, boolean activityOut, String description);
+        default void setWifiIndicators(boolean enabled, IconState statusIcon, IconState qsIcon,
+                boolean activityIn, boolean activityOut, String description) {}
 
-        void setMobileDataIndicators(IconState statusIcon, IconState qsIcon, int statusType,
+        default void setMobileDataIndicators(IconState statusIcon, IconState qsIcon, int statusType,
                 int qsType, boolean activityIn, boolean activityOut, String typeContentDescription,
-                String description, boolean isWide, int subId);
-        void setSubs(List<SubscriptionInfo> subs);
-        void setNoSims(boolean show);
+                String description, boolean isWide, int subId) {}
+        default void setSubs(List<SubscriptionInfo> subs) {}
+        default void setNoSims(boolean show) {}
 
-        void setEthernetIndicators(IconState icon);
+        default void setEthernetIndicators(IconState icon) {}
 
-        void setIsAirplaneMode(IconState icon);
+        default void setIsAirplaneMode(IconState icon) {}
 
-        void setMobileDataEnabled(boolean enabled);
+        default void setMobileDataEnabled(boolean enabled) {}
     }
 
     public interface EmergencyListener {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
index a633241..37e6a2a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NetworkControllerImpl.java
@@ -781,10 +781,12 @@
                             datatype.equals("1x") ? TelephonyIcons.ONE_X :
                             datatype.equals("3g") ? TelephonyIcons.THREE_G :
                             datatype.equals("4g") ? TelephonyIcons.FOUR_G :
+                            datatype.equals("4g+") ? TelephonyIcons.FOUR_G_PLUS :
                             datatype.equals("e") ? TelephonyIcons.E :
                             datatype.equals("g") ? TelephonyIcons.G :
                             datatype.equals("h") ? TelephonyIcons.H :
                             datatype.equals("lte") ? TelephonyIcons.LTE :
+                            datatype.equals("lte+") ? TelephonyIcons.LTE_PLUS :
                             datatype.equals("roam") ? TelephonyIcons.ROAMING :
                             TelephonyIcons.UNKNOWN;
                 }
@@ -810,13 +812,21 @@
 
     private SubscriptionInfo addSignalController(int id, int simSlotIndex) {
         SubscriptionInfo info = new SubscriptionInfo(id, "", simSlotIndex, "", "", 0, 0, "", 0,
-                null, 0, 0, "", SubscriptionManager.SIM_PROVISIONED);
+                null, 0, 0, "");
         mMobileSignalControllers.put(id, new MobileSignalController(mContext,
                 mConfig, mHasMobileDataFeature, mPhone, mCallbackHandler, this, info,
                 mSubDefaults, mReceiverHandler.getLooper()));
         return info;
     }
 
+    public boolean hasEmergencyCryptKeeperText() {
+        return EncryptionHelper.IS_DATA_ENCRYPTED;
+    }
+
+    public boolean isRadioOn() {
+        return !mAirplaneMode;
+    }
+
     private class SubListener extends OnSubscriptionsChangedListener {
         @Override
         public void onSubscriptionsChanged() {
@@ -850,6 +860,7 @@
         boolean showAtLeast3G = false;
         boolean alwaysShowCdmaRssi = false;
         boolean show4gForLte = false;
+        boolean hideLtePlus = false;
         boolean hspaDataDistinguishable;
 
         static Config readConfig(Context context) {
@@ -862,6 +873,7 @@
             config.show4gForLte = res.getBoolean(R.bool.config_show4GForLTE);
             config.hspaDataDistinguishable =
                     res.getBoolean(R.bool.config_hspa_data_distinguishable);
+            config.hideLtePlus = res.getBoolean(R.bool.config_hideLtePlus);
             return config;
         }
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NightModeController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/NightModeController.java
deleted file mode 100644
index 4611ef9..0000000
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/NightModeController.java
+++ /dev/null
@@ -1,246 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- * except in compliance with the License. You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the
- * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
-
-package com.android.systemui.statusbar.policy;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.opengl.Matrix;
-import android.provider.Settings.Secure;
-import android.util.MathUtils;
-import com.android.systemui.tuner.TunerService;
-
-import java.util.ArrayList;
-
-/**
- * Listens for changes to twilight from the TwilightService.
- *
- * Also pushes the current matrix to accessibility based on the current twilight
- * and various tuner settings.
- */
-public class NightModeController implements TunerService.Tunable {
-
-    public static final String NIGHT_MODE_ADJUST_TINT = "tuner_night_mode_adjust_tint";
-    private static final String COLOR_MATRIX_CUSTOM_VALUES = "tuner_color_custom_values";
-
-    private static final String ACTION_TWILIGHT_CHANGED = "android.intent.action.TWILIGHT_CHANGED";
-
-    private static final String EXTRA_IS_NIGHT = "isNight";
-    private static final String EXTRA_AMOUNT = "amount";
-
-    // Night mode ~= 3400 K
-    private static final float[] NIGHT_VALUES = new float[] {
-        1, 0,     0,     0,
-        0, .754f, 0,     0,
-        0, 0,     .516f, 0,
-        0, 0,     0,     1,
-    };
-    public static final float[] IDENTITY_MATRIX = new float[] {
-        1, 0, 0, 0,
-        0, 1, 0, 0,
-        0, 0, 1, 0,
-        0, 0, 0, 1,
-    };
-
-    private final ArrayList<Listener> mListeners = new ArrayList<>();
-
-    private final Context mContext;
-
-    // This is whether or not this is the main NightMode controller in SysUI that should be
-    // updating relevant color matrixes or if its in the tuner process getting current state
-    // for UI.
-    private final boolean mUpdateMatrix;
-
-    private float[] mCustomMatrix;
-    private boolean mListening;
-    private boolean mAdjustTint;
-
-    private boolean mIsNight;
-    private float mAmount;
-    private boolean mIsAuto;
-
-    public NightModeController(Context context) {
-        this(context, false);
-    }
-
-    public NightModeController(Context context, boolean updateMatrix) {
-        mContext = context;
-        mUpdateMatrix = updateMatrix;
-        TunerService.get(mContext).addTunable(this, NIGHT_MODE_ADJUST_TINT,
-                COLOR_MATRIX_CUSTOM_VALUES, Secure.TWILIGHT_MODE);
-    }
-
-    public void setNightMode(boolean isNight) {
-        if (mIsAuto) {
-            if (mIsNight != isNight) {
-                TunerService.get(mContext).setValue(Secure.TWILIGHT_MODE, isNight
-                        ? Secure.TWILIGHT_MODE_AUTO_OVERRIDE_ON
-                        : Secure.TWILIGHT_MODE_AUTO_OVERRIDE_OFF);
-            } else {
-                TunerService.get(mContext).setValue(Secure.TWILIGHT_MODE,
-                        Secure.TWILIGHT_MODE_AUTO);
-            }
-        } else {
-            TunerService.get(mContext).setValue(Secure.TWILIGHT_MODE, isNight
-                    ? Secure.TWILIGHT_MODE_LOCKED_ON : Secure.TWILIGHT_MODE_LOCKED_OFF);
-        }
-    }
-
-    public void setAuto(boolean auto) {
-        mIsAuto = auto;
-        if (auto) {
-            TunerService.get(mContext).setValue(Secure.TWILIGHT_MODE, Secure.TWILIGHT_MODE_AUTO);
-        } else {
-            // Lock into the current state
-            TunerService.get(mContext).setValue(Secure.TWILIGHT_MODE, mIsNight
-                    ? Secure.TWILIGHT_MODE_LOCKED_ON : Secure.TWILIGHT_MODE_LOCKED_OFF);
-        }
-    }
-
-    public boolean isAuto() {
-        return mIsAuto;
-    }
-
-    public void setAdjustTint(Boolean newValue) {
-        TunerService.get(mContext).setValue(NIGHT_MODE_ADJUST_TINT, ((Boolean) newValue) ? 1 : 0);
-    }
-
-    public void addListener(Listener listener) {
-        mListeners.add(listener);
-        listener.onNightModeChanged();
-        updateListening();
-    }
-
-    public void removeListener(Listener listener) {
-        mListeners.remove(listener);
-        updateListening();
-    }
-
-    private void updateListening() {
-        boolean shouldListen = mListeners.size() != 0 || (mUpdateMatrix && mAdjustTint);
-        if (shouldListen == mListening) return;
-        mListening = shouldListen;
-        if (mListening) {
-            mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_TWILIGHT_CHANGED));
-        } else {
-            mContext.unregisterReceiver(mReceiver);
-        }
-    }
-
-    public boolean isEnabled() {
-        if (!mListening) {
-            updateNightMode(mContext.registerReceiver(null,
-                    new IntentFilter(ACTION_TWILIGHT_CHANGED)));
-        }
-        return mIsNight;
-    }
-
-    public String getCustomValues() {
-        return TunerService.get(mContext).getValue(COLOR_MATRIX_CUSTOM_VALUES);
-    }
-
-    public void setCustomValues(String values) {
-        TunerService.get(mContext).setValue(COLOR_MATRIX_CUSTOM_VALUES, values);
-    }
-
-    @Override
-    public void onTuningChanged(String key, String newValue) {
-        if (COLOR_MATRIX_CUSTOM_VALUES.equals(key)) {
-            mCustomMatrix = newValue != null ? toValues(newValue) : null;
-            updateCurrentMatrix();
-        } else if (NIGHT_MODE_ADJUST_TINT.equals(key)) {
-            mAdjustTint = newValue == null || Integer.parseInt(newValue) != 0;
-            updateListening();
-            updateCurrentMatrix();
-        } else if (Secure.TWILIGHT_MODE.equals(key)) {
-            mIsAuto = newValue != null && Integer.parseInt(newValue) >= Secure.TWILIGHT_MODE_AUTO;
-        }
-    }
-
-    private void updateCurrentMatrix() {
-        if (!mUpdateMatrix) return;
-        if ((!mAdjustTint || mAmount == 0) && mCustomMatrix == null) {
-            TunerService.get(mContext).setValue(Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX, null);
-            return;
-        }
-        float[] values = scaleValues(IDENTITY_MATRIX, NIGHT_VALUES, mAdjustTint ? mAmount : 0);
-        if (mCustomMatrix != null) {
-            values = multiply(values, mCustomMatrix);
-        }
-        TunerService.get(mContext).setValue(Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX,
-                toString(values));
-    }
-
-    private void updateNightMode(Intent intent) {
-        mIsNight = intent != null && intent.getBooleanExtra(EXTRA_IS_NIGHT, false);
-        mAmount = intent != null ? intent.getFloatExtra(EXTRA_AMOUNT, 0) : 0;
-    }
-
-    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            if (ACTION_TWILIGHT_CHANGED.equals(intent.getAction())) {
-                updateNightMode(intent);
-                updateCurrentMatrix();
-                for (int i = 0; i < mListeners.size(); i++) {
-                    mListeners.get(i).onNightModeChanged();
-                }
-            }
-        }
-    };
-
-    public interface Listener {
-        void onNightModeChanged();
-        void onTwilightAutoChanged();
-    }
-
-    private static float[] multiply(float[] matrix, float[] other) {
-        if (matrix == null) {
-            return other;
-        }
-        float[] result = new float[16];
-        Matrix.multiplyMM(result, 0, matrix, 0, other, 0);
-        return result;
-    }
-
-    private float[] scaleValues(float[] identityMatrix, float[] nightValues, float amount) {
-        float[] values = new float[identityMatrix.length];
-        for (int i = 0; i < values.length; i++) {
-            values[i] = MathUtils.lerp(identityMatrix[i], nightValues[i], amount);
-        }
-        return values;
-    }
-
-    public static String toString(float[] values) {
-        StringBuilder builder = new StringBuilder();
-        for (int i = 0; i < values.length; i++) {
-            if (builder.length() != 0) {
-                builder.append(',');
-            }
-            builder.append(values[i]);
-        }
-        return builder.toString();
-    }
-
-    public static float[] toValues(String customValues) {
-        String[] strValues = customValues.split(",");
-        float[] values = new float[strValues.length];
-        for (int i = 0; i < values.length; i++) {
-            values[i] = Float.parseFloat(strValues[i]);
-        }
-        return values;
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/PreviewInflater.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/PreviewInflater.java
index 93d0ec3..687b83a 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/PreviewInflater.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/PreviewInflater.java
@@ -119,13 +119,16 @@
 
     private WidgetInfo getWidgetInfo(Intent intent) {
         PackageManager packageManager = mContext.getPackageManager();
+        int flags = PackageManager.MATCH_DEFAULT_ONLY
+                | PackageManager.MATCH_DIRECT_BOOT_AWARE
+                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
         final List<ResolveInfo> appList = packageManager.queryIntentActivitiesAsUser(
-                intent, PackageManager.MATCH_DEFAULT_ONLY, KeyguardUpdateMonitor.getCurrentUser());
+                intent, flags, KeyguardUpdateMonitor.getCurrentUser());
         if (appList.size() == 0) {
             return null;
         }
         ResolveInfo resolved = packageManager.resolveActivityAsUser(intent,
-                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA,
+                flags | PackageManager.GET_META_DATA,
                 KeyguardUpdateMonitor.getCurrentUser());
         if (wouldLaunchResolverActivity(resolved, appList)) {
             return null;
@@ -139,23 +142,32 @@
 
     public static boolean wouldLaunchResolverActivity(Context ctx, Intent intent,
             int currentUserId) {
-        return getTargetActivityInfo(ctx, intent, currentUserId) == null;
+        return getTargetActivityInfo(ctx, intent, currentUserId, false /* onlyDirectBootAware */)
+                == null;
     }
 
     /**
+     * @param onlyDirectBootAware a boolean indicating whether the matched activity packages must
+     *                            be direct boot aware when in direct boot mode if false, all
+     *                            packages are considered a match even if they are not aware.
      * @return the target activity info of the intent it resolves to a specific package or
      *         {@code null} if it resolved to the resolver activity
      */
     public static ActivityInfo getTargetActivityInfo(Context ctx, Intent intent,
-            int currentUserId) {
+            int currentUserId, boolean onlyDirectBootAware) {
         PackageManager packageManager = ctx.getPackageManager();
+        int flags = PackageManager.MATCH_DEFAULT_ONLY;
+        if (!onlyDirectBootAware) {
+            flags |=  PackageManager.MATCH_DIRECT_BOOT_AWARE
+                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
+        }
         final List<ResolveInfo> appList = packageManager.queryIntentActivitiesAsUser(
-                intent, PackageManager.MATCH_DEFAULT_ONLY, currentUserId);
+                intent, flags, currentUserId);
         if (appList.size() == 0) {
             return null;
         }
         ResolveInfo resolved = packageManager.resolveActivityAsUser(intent,
-                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA, currentUserId);
+                flags | PackageManager.GET_META_DATA, currentUserId);
         if (resolved == null || wouldLaunchResolverActivity(resolved, appList)) {
             return null;
         } else {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
index 7704a07..7b1f707 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/RemoteInputView.java
@@ -16,11 +16,14 @@
 
 package com.android.systemui.statusbar.policy;
 
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
 import android.app.Notification;
 import android.app.PendingIntent;
 import android.app.RemoteInput;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.ShortcutManager;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
@@ -32,8 +35,10 @@
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewAnimationUtils;
 import android.view.ViewGroup;
 import android.view.ViewParent;
+import android.view.accessibility.AccessibilityEvent;
 import android.view.inputmethod.CompletionInfo;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputConnection;
@@ -46,11 +51,13 @@
 
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.MetricsProto;
+import com.android.systemui.Interpolators;
 import com.android.systemui.R;
 import com.android.systemui.statusbar.ExpandableView;
 import com.android.systemui.statusbar.NotificationData;
 import com.android.systemui.statusbar.RemoteInputController;
 import com.android.systemui.statusbar.stack.ScrollContainer;
+import com.android.systemui.statusbar.stack.StackStateAnimator;
 
 /**
  * Host for the remote input.
@@ -62,6 +69,8 @@
     // A marker object that let's us easily find views of this class.
     public static final Object VIEW_TAG = new Object();
 
+    public final Object mToken = new Object();
+
     private RemoteEditText mEditText;
     private ImageButton mSendButton;
     private ProgressBar mProgressBar;
@@ -76,6 +85,12 @@
     private View mScrollContainerChild;
     private boolean mRemoved;
 
+    private int mRevealCx;
+    private int mRevealCy;
+    private int mRevealR;
+
+    private boolean mResetting;
+
     public RemoteInputView(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
@@ -127,11 +142,20 @@
         mSendButton.setVisibility(INVISIBLE);
         mProgressBar.setVisibility(VISIBLE);
         mEntry.remoteInputText = mEditText.getText();
-        mController.addSpinning(mEntry.key);
-        mController.removeRemoteInput(mEntry);
+        mController.addSpinning(mEntry.key, mToken);
+        mController.removeRemoteInput(mEntry, mToken);
         mEditText.mShowImeOnInputConnection = false;
         mController.remoteInputSent(mEntry);
 
+        // Tell ShortcutManager that this package has been "activated".  ShortcutManager
+        // will reset the throttling for this package.
+        // Strictly speaking, the intent receiver may be different from the notification publisher,
+        // but that's an edge case, and also because we can't always know which package will receive
+        // an intent, so we just reset for the publisher.
+        getContext().getSystemService(ShortcutManager.class).onApplicationActive(
+                mEntry.notification.getPackageName(),
+                mEntry.notification.getUser().getIdentifier());
+
         MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_REMOTE_INPUT_SEND,
                 mEntry.notification.getPackageName());
         try {
@@ -170,14 +194,28 @@
         return true;
     }
 
-    public void onDefocus() {
-        mController.removeRemoteInput(mEntry);
+    private void onDefocus(boolean animate) {
+        mController.removeRemoteInput(mEntry, mToken);
         mEntry.remoteInputText = mEditText.getText();
 
         // During removal, we get reattached and lose focus. Not hiding in that
         // case to prevent flicker.
         if (!mRemoved) {
-            setVisibility(INVISIBLE);
+            if (animate && mRevealR > 0) {
+                Animator reveal = ViewAnimationUtils.createCircularReveal(
+                        this, mRevealCx, mRevealCy, mRevealR, 0);
+                reveal.setInterpolator(Interpolators.FAST_OUT_LINEAR_IN);
+                reveal.setDuration(StackStateAnimator.ANIMATION_DURATION_CLOSE_REMOTE_INPUT);
+                reveal.addListener(new AnimatorListenerAdapter() {
+                    @Override
+                    public void onAnimationEnd(Animator animation) {
+                        setVisibility(INVISIBLE);
+                    }
+                });
+                reveal.start();
+            } else {
+                setVisibility(INVISIBLE);
+            }
         }
         MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_REMOTE_INPUT_CLOSE,
                 mEntry.notification.getPackageName());
@@ -196,11 +234,11 @@
     @Override
     protected void onDetachedFromWindow() {
         super.onDetachedFromWindow();
-        if (mEntry.row.isChangingPosition()) {
+        if (mEntry.row.isChangingPosition() || isTemporarilyDetached()) {
             return;
         }
-        mController.removeRemoteInput(mEntry);
-        mController.removeSpinning(mEntry.key);
+        mController.removeRemoteInput(mEntry, mToken);
+        mController.removeSpinning(mEntry.key, mToken);
     }
 
     public void setPendingIntent(PendingIntent pendingIntent) {
@@ -213,12 +251,23 @@
         mEditText.setHint(mRemoteInput.getLabel());
     }
 
+    public void focusAnimated() {
+        if (getVisibility() != VISIBLE) {
+            Animator animator = ViewAnimationUtils.createCircularReveal(
+                    this, mRevealCx, mRevealCy, 0, mRevealR);
+            animator.setDuration(StackStateAnimator.ANIMATION_DURATION_STANDARD);
+            animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
+            animator.start();
+        }
+        focus();
+    }
+
     public void focus() {
         MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_REMOTE_INPUT_OPEN,
                 mEntry.notification.getPackageName());
 
         setVisibility(VISIBLE);
-        mController.addRemoteInput(mEntry);
+        mController.addRemoteInput(mEntry, mToken);
         mEditText.setInnerFocusable(true);
         mEditText.mShowImeOnInputConnection = true;
         mEditText.setText(mEntry.remoteInputText);
@@ -237,13 +286,28 @@
     }
 
     private void reset() {
+        mResetting = true;
+
         mEditText.getText().clear();
         mEditText.setEnabled(true);
         mSendButton.setVisibility(VISIBLE);
         mProgressBar.setVisibility(INVISIBLE);
-        mController.removeSpinning(mEntry.key);
+        mController.removeSpinning(mEntry.key, mToken);
         updateSendButton();
-        onDefocus();
+        onDefocus(false /* animate */);
+
+        mResetting = false;
+    }
+
+    @Override
+    public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
+        if (mResetting && child == mEditText) {
+            // Suppress text events if it happens during resetting. Ideally this would be
+            // suppressed by the text view not being shown, but that doesn't work here because it
+            // needs to stay visible for the animation.
+            return false;
+        }
+        return super.onRequestSendAccessibilityEvent(child, event);
     }
 
     private void updateSendButton() {
@@ -262,7 +326,7 @@
     }
 
     public void close() {
-        mEditText.defocusIfNeeded();
+        mEditText.defocusIfNeeded(false /* animated */);
     }
 
     @Override
@@ -304,13 +368,14 @@
     }
 
     public boolean isActive() {
-        return mEditText.isFocused();
+        return mEditText.isFocused() && mEditText.isEnabled();
     }
 
     public void stealFocusFrom(RemoteInputView other) {
         other.close();
         setPendingIntent(other.mPendingIntent);
         setRemoteInput(other.mRemoteInputs, other.mRemoteInput);
+        setRevealParameters(other.mRevealCx, other.mRevealCy, other.mRevealR);
         focus();
     }
 
@@ -321,7 +386,6 @@
      * @return true if a matching action was found, false otherwise
      */
     public boolean updatePendingIntentFromActions(Notification.Action[] actions) {
-        boolean found = false;
         if (mPendingIntent == null || actions == null) {
             return false;
         }
@@ -364,6 +428,30 @@
         mRemoved = true;
     }
 
+    public void setRevealParameters(int cx, int cy, int r) {
+        mRevealCx = cx;
+        mRevealCy = cy;
+        mRevealR = r;
+    }
+
+    @Override
+    public void dispatchStartTemporaryDetach() {
+        super.dispatchStartTemporaryDetach();
+        // Detach the EditText temporarily such that it doesn't get onDetachedFromWindow and
+        // won't lose IME focus.
+        detachViewFromParent(mEditText);
+    }
+
+    @Override
+    public void dispatchFinishTemporaryDetach() {
+        if (isAttachedToWindow()) {
+            attachViewToParent(mEditText, 0, mEditText.getLayoutParams());
+        } else {
+            removeDetachedView(mEditText, false /* animate */);
+        }
+        super.dispatchFinishTemporaryDetach();
+    }
+
     /**
      * An EditText that changes appearance based on whether it's focusable and becomes
      * un-focusable whenever the user navigates away from it or it becomes invisible.
@@ -379,14 +467,22 @@
             mBackground = getBackground();
         }
 
-        private void defocusIfNeeded() {
-            if (mRemoteInputView != null && mRemoteInputView.mEntry.row.isChangingPosition()) {
+        private void defocusIfNeeded(boolean animate) {
+            if (mRemoteInputView != null && mRemoteInputView.mEntry.row.isChangingPosition()
+                    || isTemporarilyDetached()) {
+                if (isTemporarilyDetached()) {
+                    // We might get reattached but then the other one of HUN / expanded might steal
+                    // our focus, so we'll need to save our text here.
+                    if (mRemoteInputView != null) {
+                        mRemoteInputView.mEntry.remoteInputText = getText();
+                    }
+                }
                 return;
             }
             if (isFocusable() && isEnabled()) {
                 setInnerFocusable(false);
                 if (mRemoteInputView != null) {
-                    mRemoteInputView.onDefocus();
+                    mRemoteInputView.onDefocus(animate);
                 }
                 mShowImeOnInputConnection = false;
             }
@@ -397,7 +493,7 @@
             super.onVisibilityChanged(changedView, visibility);
 
             if (!isShown()) {
-                defocusIfNeeded();
+                defocusIfNeeded(false /* animate */);
             }
         }
 
@@ -405,7 +501,7 @@
         protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
             super.onFocusChanged(focused, direction, previouslyFocusedRect);
             if (!focused) {
-                defocusIfNeeded();
+                defocusIfNeeded(true /* animate */);
             }
         }
 
@@ -422,14 +518,21 @@
         }
 
         @Override
-        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
-            if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
-                defocusIfNeeded();
-                final InputMethodManager imm = InputMethodManager.getInstance();
-                imm.hideSoftInputFromWindow(getWindowToken(), 0);
+        public boolean onKeyDown(int keyCode, KeyEvent event) {
+            if (keyCode == KeyEvent.KEYCODE_BACK) {
+                // Eat the DOWN event here to prevent any default behavior.
                 return true;
             }
-            return super.onKeyPreIme(keyCode, event);
+            return super.onKeyDown(keyCode, event);
+        }
+
+        @Override
+        public boolean onKeyUp(int keyCode, KeyEvent event) {
+            if (keyCode == KeyEvent.KEYCODE_BACK) {
+                defocusIfNeeded(true /* animate */);
+                return true;
+            }
+            return super.onKeyUp(keyCode, event);
         }
 
         @Override
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityController.java
index a22f988..014afae 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityController.java
@@ -23,6 +23,8 @@
     String getProfileOwnerName();
     boolean isVpnEnabled();
     boolean isVpnRestricted();
+    /** Whether the VPN app should use branded VPN iconography.  */
+    boolean isVpnBranded();
     String getPrimaryVpnName();
     String getProfileVpnName();
     void onUserSwitched(int newUserId);
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityControllerImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityControllerImpl.java
index 5046456..07d3b59 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityControllerImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/SecurityControllerImpl.java
@@ -18,6 +18,8 @@
 import android.app.ActivityManager;
 import android.app.admin.DevicePolicyManager;
 import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.UserInfo;
 import android.net.ConnectivityManager;
@@ -54,10 +56,13 @@
             .build();
     private static final int NO_NETWORK = -1;
 
+    private static final String VPN_BRANDED_META_DATA = "com.android.systemui.IS_BRANDED";
+
     private final Context mContext;
     private final ConnectivityManager mConnectivityManager;
     private final IConnectivityManager mConnectivityManagerService;
     private final DevicePolicyManager mDevicePolicyManager;
+    private final PackageManager mPackageManager;
     private final UserManager mUserManager;
 
     @GuardedBy("mCallbacks")
@@ -75,6 +80,7 @@
                 context.getSystemService(Context.CONNECTIVITY_SERVICE);
         mConnectivityManagerService = IConnectivityManager.Stub.asInterface(
                 ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
+        mPackageManager = context.getPackageManager();
         mUserManager = (UserManager)
                 context.getSystemService(Context.USER_SERVICE);
 
@@ -165,6 +171,21 @@
     }
 
     @Override
+    public boolean isVpnBranded() {
+        VpnConfig cfg = mCurrentVpns.get(mVpnUserId);
+        if (cfg == null) {
+            return false;
+        }
+
+        String packageName = getPackageNameForVpnConfig(cfg);
+        if (packageName == null) {
+            return false;
+        }
+
+        return isVpnPackageBranded(packageName);
+    }
+
+    @Override
     public void removeCallback(SecurityControllerCallback callback) {
         synchronized (mCallbacks) {
             if (callback == null) return;
@@ -245,6 +266,28 @@
         mCurrentVpns = vpns;
     }
 
+    private String getPackageNameForVpnConfig(VpnConfig cfg) {
+        if (cfg.legacy) {
+            return null;
+        }
+        return cfg.user;
+    }
+
+    private boolean isVpnPackageBranded(String packageName) {
+        boolean isBranded;
+        try {
+            ApplicationInfo info = mPackageManager.getApplicationInfo(packageName,
+                PackageManager.GET_META_DATA);
+            if (info == null || info.metaData == null || !info.isSystemApp()) {
+                return false;
+            }
+            isBranded = info.metaData.getBoolean(VPN_BRANDED_META_DATA, false);
+        } catch (NameNotFoundException e) {
+            return false;
+        }
+        return isBranded;
+    }
+
     private final NetworkCallback mNetworkCallback = new NetworkCallback() {
         @Override
         public void onAvailable(Network network) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/TelephonyIcons.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/TelephonyIcons.java
index 6ff8f77..ed8c7ff 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/TelephonyIcons.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/TelephonyIcons.java
@@ -182,6 +182,19 @@
 
     static final int QS_DATA_4G = R.drawable.ic_qs_signal_4g;
 
+    static final int[][] DATA_4G_PLUS = {
+            { R.drawable.stat_sys_data_fully_connected_4g_plus,
+              R.drawable.stat_sys_data_fully_connected_4g_plus,
+              R.drawable.stat_sys_data_fully_connected_4g_plus,
+              R.drawable.stat_sys_data_fully_connected_4g_plus },
+            { R.drawable.stat_sys_data_fully_connected_4g_plus,
+              R.drawable.stat_sys_data_fully_connected_4g_plus,
+              R.drawable.stat_sys_data_fully_connected_4g_plus,
+              R.drawable.stat_sys_data_fully_connected_4g_plus }
+    };
+
+    static final int QS_DATA_4G_PLUS = R.drawable.ic_qs_signal_4g_plus;
+
     // LTE branded "LTE"
     static final int[][] DATA_LTE = {
             { R.drawable.stat_sys_data_fully_connected_lte,
@@ -195,15 +208,18 @@
     };
 
     static final int QS_DATA_LTE = R.drawable.ic_qs_signal_lte;
+    static final int QS_DATA_LTE_PLUS = R.drawable.ic_qs_signal_lte_plus;
 
     static final int FLIGHT_MODE_ICON = R.drawable.stat_sys_airplane_mode;
     static final int ROAMING_ICON = R.drawable.stat_sys_data_fully_connected_roam;
     static final int ICON_LTE = R.drawable.stat_sys_data_fully_connected_lte;
+    static final int ICON_LTE_PLUS = R.drawable.stat_sys_data_fully_connected_lte_plus;
     static final int ICON_G = R.drawable.stat_sys_data_fully_connected_g;
     static final int ICON_E = R.drawable.stat_sys_data_fully_connected_e;
     static final int ICON_H = R.drawable.stat_sys_data_fully_connected_h;
     static final int ICON_3G = R.drawable.stat_sys_data_fully_connected_3g;
     static final int ICON_4G = R.drawable.stat_sys_data_fully_connected_4g;
+    static final int ICON_4G_PLUS = R.drawable.stat_sys_data_fully_connected_4g_plus;
     static final int ICON_1X = R.drawable.stat_sys_data_fully_connected_1x;
     static final int ICON_CARRIER_NETWORK_CHANGE =
             R.drawable.stat_sys_signal_carrier_network_change_animation;
@@ -213,6 +229,7 @@
     static final int QS_ICON_LTE = R.drawable.ic_qs_signal_lte;
     static final int QS_ICON_3G = R.drawable.ic_qs_signal_3g;
     static final int QS_ICON_4G = R.drawable.ic_qs_signal_4g;
+    static final int QS_ICON_4G_PLUS = R.drawable.ic_qs_signal_4g_plus;
     static final int QS_ICON_1X = R.drawable.ic_qs_signal_1x;
     static final int QS_ICON_CARRIER_NETWORK_CHANGE =
             R.drawable.ic_qs_signal_carrier_network_change_animation;
@@ -348,6 +365,21 @@
             TelephonyIcons.QS_DATA_4G
             );
 
+    static final MobileIconGroup FOUR_G_PLUS = new MobileIconGroup(
+            "4G+",
+            TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH,
+            TelephonyIcons.QS_TELEPHONY_SIGNAL_STRENGTH,
+            AccessibilityContentDescriptions.PHONE_SIGNAL_STRENGTH,
+            0,0,
+            TelephonyIcons.TELEPHONY_NO_NETWORK,
+            TelephonyIcons.QS_TELEPHONY_NO_NETWORK,
+            AccessibilityContentDescriptions.PHONE_SIGNAL_STRENGTH[0],
+            R.string.accessibility_data_connection_4g_plus,
+            TelephonyIcons.ICON_4G_PLUS,
+            true,
+            TelephonyIcons.QS_DATA_4G_PLUS
+            );
+
     static final MobileIconGroup LTE = new MobileIconGroup(
             "LTE",
             TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH,
@@ -363,6 +395,21 @@
             TelephonyIcons.QS_DATA_LTE
             );
 
+    static final MobileIconGroup LTE_PLUS = new MobileIconGroup(
+            "LTE+",
+            TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH,
+            TelephonyIcons.QS_TELEPHONY_SIGNAL_STRENGTH,
+            AccessibilityContentDescriptions.PHONE_SIGNAL_STRENGTH,
+            0, 0,
+            TelephonyIcons.TELEPHONY_NO_NETWORK,
+            TelephonyIcons.QS_TELEPHONY_NO_NETWORK,
+            AccessibilityContentDescriptions.PHONE_SIGNAL_STRENGTH[0],
+            R.string.accessibility_data_connection_lte_plus,
+            TelephonyIcons.ICON_LTE_PLUS,
+            true,
+            TelephonyIcons.QS_DATA_LTE_PLUS
+            );
+
     static final MobileIconGroup ROAMING = new MobileIconGroup(
             "Roaming",
             TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH_ROAMING,
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
index 89d2712..27ba003 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
@@ -381,6 +381,18 @@
         Log.e(TAG, "Couldn't switch to user, id=" + userId);
     }
 
+    public int getSwitchableUserCount() {
+        int count = 0;
+        final int N = mUsers.size();
+        for (int i = 0; i < N; ++i) {
+            UserRecord record = mUsers.get(i);
+            if (record.info != null && record.info.supportsSwitchTo()) {
+                count++;
+            }
+        }
+        return count;
+    }
+
     private void switchToUserId(int id) {
         try {
             pauseRefreshUsers();
@@ -419,23 +431,27 @@
     }
 
     private void listenForCallState() {
-        TelephonyManager.from(mContext).listen(new PhoneStateListener() {
-            private int mCallState;
-            @Override
-            public void onCallStateChanged(int state, String incomingNumber) {
-                if (mCallState == state) return;
-                if (DEBUG) Log.v(TAG, "Call state changed: " + state);
-                mCallState = state;
-                int currentUserId = ActivityManager.getCurrentUser();
-                UserInfo userInfo = mUserManager.getUserInfo(currentUserId);
-                if (userInfo != null && userInfo.isGuest()) {
-                    showGuestNotification(currentUserId);
-                }
-                refreshUsers(UserHandle.USER_NULL);
-            }
-        }, PhoneStateListener.LISTEN_CALL_STATE);
+        TelephonyManager.from(mContext).listen(mPhoneStateListener,
+                PhoneStateListener.LISTEN_CALL_STATE);
     }
 
+    private final PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
+        private int mCallState;
+
+        @Override
+        public void onCallStateChanged(int state, String incomingNumber) {
+            if (mCallState == state) return;
+            if (DEBUG) Log.v(TAG, "Call state changed: " + state);
+            mCallState = state;
+            int currentUserId = ActivityManager.getCurrentUser();
+            UserInfo userInfo = mUserManager.getUserInfo(currentUserId);
+            if (userInfo != null && userInfo.isGuest()) {
+                showGuestNotification(currentUserId);
+            }
+            refreshUsers(UserHandle.USER_NULL);
+        }
+    };
+
     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/stack/AmbientState.java b/packages/SystemUI/src/com/android/systemui/statusbar/stack/AmbientState.java
index 50e5b88..81da672 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/stack/AmbientState.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/stack/AmbientState.java
@@ -43,6 +43,7 @@
     private boolean mShadeExpanded;
     private float mMaxHeadsUpTranslation;
     private boolean mDismissAllInProgress;
+    private int mLayoutMinHeight;
 
     public int getScrollY() {
         return mScrollY;
@@ -137,10 +138,6 @@
         mStackTranslation = stackTranslation;
     }
 
-    public int getLayoutHeight() {
-        return mLayoutHeight;
-    }
-
     public void setLayoutHeight(int layoutHeight) {
         mLayoutHeight = layoutHeight;
     }
@@ -154,7 +151,7 @@
     }
 
     public int getInnerHeight() {
-        return mLayoutHeight - mTopPadding;
+        return Math.max(mLayoutHeight - mTopPadding, mLayoutMinHeight);
     }
 
     public boolean isShadeExpanded() {
@@ -180,4 +177,8 @@
     public boolean isDismissAllInProgress() {
         return mDismissAllInProgress;
     }
+
+    public void setLayoutMinHeight(int layoutMinHeight) {
+        mLayoutMinHeight = layoutMinHeight;
+    }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationChildrenContainer.java b/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationChildrenContainer.java
index 3c9373b..d7920a9 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationChildrenContainer.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationChildrenContainer.java
@@ -123,8 +123,10 @@
             mDividers.get(i).layout(0, 0, getWidth(), mDividerHeight);
         }
         if (mOverflowNumber != null) {
-            mOverflowNumber.layout(getWidth() - mOverflowNumber.getMeasuredWidth(), 0, getWidth(),
-                    mOverflowNumber.getMeasuredHeight());
+            boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
+            int left = (isRtl ? 0 : getWidth() - mOverflowNumber.getMeasuredWidth());
+            int right = left + mOverflowNumber.getMeasuredWidth();
+            mOverflowNumber.layout(left, 0, right, mOverflowNumber.getMeasuredHeight());
         }
         if (mNotificationHeader != null) {
             mNotificationHeader.layout(0, 0, mNotificationHeader.getMeasuredWidth(),
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java b/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java
index 1e5654e..90e12ec 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/stack/NotificationStackScrollLayout.java
@@ -111,11 +111,7 @@
     private int mCurrentStackHeight = Integer.MAX_VALUE;
     private final Paint mBackgroundPaint = new Paint();
 
-    /**
-     * mCurrentStackHeight is the actual stack height, mLastSetStackHeight is the stack height set
-     * externally from {@link #setStackHeight}
-     */
-    private float mLastSetStackHeight;
+    private float mExpandedHeight;
     private int mOwnScrollY;
     private int mMaxLayoutHeight;
 
@@ -130,7 +126,7 @@
     private boolean mIsBeingDragged;
     private int mLastMotionY;
     private int mDownX;
-    private int mActivePointerId;
+    private int mActivePointerId = INVALID_POINTER;
     private boolean mTouchIsClick;
     private float mInitialTouchX;
     private float mInitialTouchY;
@@ -216,7 +212,6 @@
     private float mTopPaddingOverflow;
     private boolean mDontReportNextOverScroll;
     private boolean mDontClampNextScroll;
-    private boolean mRequestViewResizeAnimationOnLayout;
     private boolean mNeedViewResizeAnimation;
     private View mExpandedGroupView;
     private boolean mEverythingNeedsAnimation;
@@ -355,6 +350,9 @@
                     return object.getBackgroundFadeAmount();
                 }
             };
+    private boolean mQsExpanded;
+    private boolean mForwardScrollable;
+    private boolean mBackwardScrollable;
 
     public NotificationStackScrollLayout(Context context) {
         this(context, null);
@@ -412,7 +410,10 @@
 
     @Override
     protected void onDraw(Canvas canvas) {
-        canvas.drawRect(0, mCurrentBounds.top, getWidth(), mCurrentBounds.bottom, mBackgroundPaint);
+        if (mCurrentBounds.top < mCurrentBounds.bottom) {
+            canvas.drawRect(0, mCurrentBounds.top, getWidth(), mCurrentBounds.bottom,
+                    mBackgroundPaint);
+        }
         if (DEBUG) {
             int y = mTopPadding;
             canvas.drawLine(0, y, getWidth(), y, mDebugPaint);
@@ -515,12 +516,9 @@
         setMaxLayoutHeight(getHeight());
         updateContentHeight();
         clampScrollPosition();
-        if (mRequestViewResizeAnimationOnLayout) {
-            requestAnimationOnViewResize(null);
-            mRequestViewResizeAnimationOnLayout = false;
-        }
         requestChildrenUpdate();
         updateFirstAndLastBackgroundViews();
+        updateAlgorithmLayoutMinHeight();
     }
 
     private void requestAnimationOnViewResize(ExpandableNotificationRow row) {
@@ -562,9 +560,14 @@
 
     private void updateAlgorithmHeightAndPadding() {
         mAmbientState.setLayoutHeight(getLayoutHeight());
+        updateAlgorithmLayoutMinHeight();
         mAmbientState.setTopPadding(mTopPadding);
     }
 
+    private void updateAlgorithmLayoutMinHeight() {
+        mAmbientState.setLayoutMinHeight(mQsExpanded && !onKeyguard() ? getLayoutMinHeight() : 0);
+    }
+
     /**
      * Updates the children views according to the stack scroll algorithm. Call this whenever
      * modifications to {@link #mOwnScrollY} are performed to reflect it in the view layout.
@@ -595,7 +598,7 @@
                 if (startingPosition < mOwnScrollY) {
                     // This child starts off screen, so let's keep it offscreen to keep the others visible
 
-                    mOwnScrollY += childHeight;
+                    setOwnScrollY(mOwnScrollY + childHeight);
                 }
             }
         }
@@ -618,7 +621,7 @@
             // Only apply the scroll if we're scrolling the view upwards, or the view is so far up
             // that it is not visible anymore.
             if (mOwnScrollY < targetScroll || outOfViewScroll < mOwnScrollY) {
-                mOwnScrollY = targetScroll;
+                setOwnScrollY(targetScroll);
             }
         }
     }
@@ -638,7 +641,7 @@
     private void clampScrollPosition() {
         int scrollRange = getScrollRange();
         if (scrollRange < mOwnScrollY) {
-            mOwnScrollY = scrollRange;
+            setOwnScrollY(scrollRange);
         }
     }
 
@@ -661,37 +664,85 @@
     }
 
     /**
-     * Update the height of the stack to a new height.
+     * Update the height of the panel.
      *
-     * @param height the new height of the stack
+     * @param height the expanded height of the panel
      */
-    public void setStackHeight(float height) {
-        mLastSetStackHeight = height;
+    public void setExpandedHeight(float height) {
+        mExpandedHeight = height;
         setIsExpanded(height > 0.0f);
-        int newStackHeight = (int) height;
-        int minStackHeight = getLayoutMinHeight();
         int stackHeight;
-        float paddingOffset;
-        boolean trackingHeadsUp = mTrackingHeadsUp || mHeadsUpManager.hasPinnedHeadsUp();
-        int normalUnfoldPositionStart = trackingHeadsUp
-                ? mHeadsUpManager.getTopHeadsUpPinnedHeight()
-                : minStackHeight;
-        if (newStackHeight - mTopPadding - mTopPaddingOverflow >= normalUnfoldPositionStart
-                || getNotGoneChildCount() == 0) {
-            paddingOffset = mTopPaddingOverflow;
-            stackHeight = newStackHeight;
+        float translationY;
+        float appearEndPosition = getAppearEndPosition();
+        float appearStartPosition = getAppearStartPosition();
+        if (height >= appearEndPosition) {
+            translationY = 0;
+            stackHeight = (int) height;
         } else {
-            int translationY;
-            translationY = newStackHeight - normalUnfoldPositionStart;
-            paddingOffset = translationY - mTopPadding;
-            stackHeight = (int) (height - (translationY - mTopPadding));
+            float appearFraction = getAppearFraction(height);
+            if (appearFraction >= 0) {
+                translationY = NotificationUtils.interpolate(getExpandTranslationStart(), 0,
+                        appearFraction);
+            } else {
+                // This may happen when pushing up a heads up. We linearly push it up from the
+                // start
+                translationY = height - appearStartPosition + getExpandTranslationStart();
+            }
+            stackHeight = (int) (height - translationY);
         }
         if (stackHeight != mCurrentStackHeight) {
             mCurrentStackHeight = stackHeight;
             updateAlgorithmHeightAndPadding();
             requestChildrenUpdate();
         }
-        setStackTranslation(paddingOffset);
+        setStackTranslation(translationY);
+    }
+
+    /**
+     * @return The translation at the beginning when expanding.
+     *         Measured relative to the resting position.
+     */
+    private float getExpandTranslationStart() {
+        int startPosition = 0;
+        if (!mTrackingHeadsUp && !mHeadsUpManager.hasPinnedHeadsUp()) {
+            startPosition = - Math.min(getFirstChildIntrinsicHeight(),
+                    mMaxLayoutHeight - mIntrinsicPadding - mBottomStackSlowDownHeight
+                            - mBottomStackPeekSize);
+        }
+        return startPosition - mTopPadding;
+    }
+
+    /**
+     * @return the position from where the appear transition starts when expanding.
+     *         Measured in absolute height.
+     */
+    private float getAppearStartPosition() {
+        return mTrackingHeadsUp
+                ? mHeadsUpManager.getTopHeadsUpPinnedHeight()
+                : 0;
+    }
+
+    /**
+     * @return the position from where the appear transition ends when expanding.
+     *         Measured in absolute height.
+     */
+    private float getAppearEndPosition() {
+        int firstItemHeight = mTrackingHeadsUp || mHeadsUpManager.hasPinnedHeadsUp()
+                ? mHeadsUpManager.getTopHeadsUpPinnedHeight() + mBottomStackPeekSize
+                        + mBottomStackSlowDownHeight
+                : getLayoutMinHeight();
+        return firstItemHeight + (onKeyguard() ? mTopPadding : mIntrinsicPadding);
+    }
+
+    /**
+     * @param height the height of the panel
+     * @return the fraction of the appear animation that has been performed
+     */
+    public float getAppearFraction(float height) {
+        float appearEndPosition = getAppearEndPosition();
+        float appearStartPosition = getAppearStartPosition();
+        return (height - appearStartPosition)
+                / (appearEndPosition - appearStartPosition);
     }
 
     public float getStackTranslation() {
@@ -1110,6 +1161,10 @@
 
     @Override
     public boolean isAntiFalsingNeeded() {
+        return onKeyguard();
+    }
+
+    private boolean onKeyguard() {
         return mPhoneStatusBar.getBarState() == StatusBarState.KEYGUARD;
     }
 
@@ -1186,7 +1241,7 @@
         if (!isScrollingEnabled()) {
             return false;
         }
-        if (ev.getY() < mQsContainer.getBottom()) {
+        if (ev.getY() < mQsContainer.getBottom() && !mIsBeingDragged) {
             return false;
         }
         mForcedScroll = null;
@@ -1353,7 +1408,7 @@
                         false /* onTop */,
                         false /* animate */);
             }
-            mOwnScrollY = range;
+            setOwnScrollY(range);
             scrollAmount = 0.0f;
         }
         return scrollAmount;
@@ -1384,7 +1439,7 @@
             setOverScrolledPixels(currentTopPixels - newScrollY,
                     true /* onTop */,
                     false /* animate */);
-            mOwnScrollY = 0;
+            setOwnScrollY(0);
             scrollAmount = 0.0f;
         }
         return scrollAmount;
@@ -1599,7 +1654,7 @@
     }
 
     private void customScrollTo(int y) {
-        mOwnScrollY = y;
+        setOwnScrollY(y);
         updateChildren();
     }
 
@@ -1610,7 +1665,7 @@
             final int oldX = mScrollX;
             final int oldY = mOwnScrollY;
             mScrollX = scrollX;
-            mOwnScrollY = scrollY;
+            setOwnScrollY(scrollY);
             if (clampedY) {
                 springBack();
             } else {
@@ -1640,12 +1695,12 @@
             if (overScrolledTop) {
                 onTop = true;
                 newAmount = -mOwnScrollY;
-                mOwnScrollY = 0;
+                setOwnScrollY(0);
                 mDontReportNextOverScroll = true;
             } else {
                 onTop = false;
                 newAmount = mOwnScrollY - scrollRange;
-                mOwnScrollY = scrollRange;
+                setOwnScrollY(scrollRange);
             }
             setOverScrollAmount(newAmount, onTop, false);
             setOverScrollAmount(0.0f, onTop, true);
@@ -1773,6 +1828,19 @@
         if (scrollable != mScrollable) {
             mScrollable = scrollable;
             setFocusable(scrollable);
+            updateForwardAndBackwardScrollability();
+        }
+    }
+
+    private void updateForwardAndBackwardScrollability() {
+        boolean forwardScrollable = mScrollable && mOwnScrollY < getScrollRange();
+        boolean backwardsScrollable = mScrollable && mOwnScrollY > 0;
+        boolean changed = forwardScrollable != mForwardScrollable
+                || backwardsScrollable != mBackwardScrollable;
+        mForwardScrollable = forwardScrollable;
+        mBackwardScrollable = backwardsScrollable;
+        if (changed) {
+            sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
         }
     }
 
@@ -1919,9 +1987,9 @@
     }
 
     private void applyCurrentBackgroundBounds() {
-        if (!mFadingOut) {
-            mScrimController.setExcludedBackgroundArea(mCurrentBounds);
-        }
+        mScrimController.setExcludedBackgroundArea(
+                mFadingOut || mParentFadingOut || mAmbientState.isDark() ? null
+                        : mCurrentBounds);
         invalidate();
     }
 
@@ -1965,15 +2033,16 @@
                 bottom = Math.min(bottom, getHeight());
             }
         } else {
-            top = (int) (mTopPadding + mStackTranslation);
+            top = mTopPadding;
             bottom = top;
         }
         if (mPhoneStatusBar.getBarState() != StatusBarState.KEYGUARD) {
-            mBackgroundBounds.top = (int) Math.max(mTopPadding + mStackTranslation, top);
+            top = (int) Math.max(mTopPadding + mStackTranslation, top);
         } else {
             // otherwise the animation from the shade to the keyguard will jump as it's maxed
-            mBackgroundBounds.top = Math.max(0, top);
+            top = Math.max(0, top);
         }
+        mBackgroundBounds.top = top;
         mBackgroundBounds.bottom = Math.min(getHeight(), Math.max(bottom, top));
     }
 
@@ -2030,13 +2099,13 @@
             float topAmount = getCurrentOverScrollAmount(true);
             float bottomAmount = getCurrentOverScrollAmount(false);
             if (velocityY < 0 && topAmount > 0) {
-                mOwnScrollY -= (int) topAmount;
+                setOwnScrollY(mOwnScrollY - (int) topAmount);
                 mDontReportNextOverScroll = true;
                 setOverScrollAmount(0, true, false);
                 mMaxOverScroll = Math.abs(velocityY) / 1000f * getRubberBandFactor(true /* onTop */)
                         * mOverflingDistance + topAmount;
             } else if (velocityY > 0 && bottomAmount > 0) {
-                mOwnScrollY += bottomAmount;
+                setOwnScrollY((int) (mOwnScrollY + bottomAmount));
                 setOverScrollAmount(0, false, false);
                 mMaxOverScroll = Math.abs(velocityY) / 1000f
                         * getRubberBandFactor(false /* onTop */) * mOverflingDistance
@@ -2079,34 +2148,35 @@
      */
     public void updateTopPadding(float qsHeight, boolean animate,
             boolean ignoreIntrinsicPadding) {
-        float start = qsHeight;
-        float stackHeight = getHeight() - start;
+        int topPadding = (int) qsHeight;
         int minStackHeight = getLayoutMinHeight();
-        if (stackHeight <= minStackHeight) {
-            float overflow = minStackHeight - stackHeight;
-            stackHeight = minStackHeight;
-            start = getHeight() - stackHeight;
-            mTopPaddingOverflow = overflow;
+        if (topPadding + minStackHeight > getHeight()) {
+            mTopPaddingOverflow = topPadding + minStackHeight - getHeight();
         } else {
             mTopPaddingOverflow = 0;
         }
-        setTopPadding(ignoreIntrinsicPadding ? (int) start : clampPadding((int) start),
+        setTopPadding(ignoreIntrinsicPadding ? topPadding : clampPadding(topPadding),
                 animate);
-        setStackHeight(mLastSetStackHeight);
+        setExpandedHeight(mExpandedHeight);
     }
 
     public int getLayoutMinHeight() {
+        int firstChildMinHeight = getFirstChildIntrinsicHeight();
+        return Math.min(firstChildMinHeight + mBottomStackPeekSize + mBottomStackSlowDownHeight,
+                mMaxLayoutHeight - mIntrinsicPadding);
+    }
+
+    public int getFirstChildIntrinsicHeight() {
         final ExpandableView firstChild = getFirstChildNotGone();
         int firstChildMinHeight = firstChild != null
                 ? firstChild.getIntrinsicHeight()
                 : mEmptyShadeView != null
-                        ? mEmptyShadeView.getMinHeight()
+                        ? mEmptyShadeView.getIntrinsicHeight()
                         : mCollapsedSize;
         if (mOwnScrollY > 0) {
             firstChildMinHeight = Math.max(firstChildMinHeight - mOwnScrollY, mCollapsedSize);
         }
-        return Math.min(firstChildMinHeight + mBottomStackPeekSize + mBottomStackSlowDownHeight,
-                mMaxLayoutHeight - mTopPadding);
+        return firstChildMinHeight;
     }
 
     public float getTopPaddingOverflow() {
@@ -2386,11 +2456,11 @@
         if (endPosition <= mOwnScrollY) {
             // This child is fully scrolled of the top, so we have to deduct its height from the
             // scrollPosition
-            mOwnScrollY -= childHeight;
+            setOwnScrollY(mOwnScrollY - childHeight);
         } else if (startingPosition < mOwnScrollY) {
             // This child is currently being scrolled into, set the scroll position to the start of
             // this child
-            mOwnScrollY = startingPosition;
+            setOwnScrollY(startingPosition);
         }
     }
 
@@ -2971,7 +3041,7 @@
     public void onExpansionStopped() {
         mIsExpansionChanging = false;
         if (!mIsExpanded) {
-            mOwnScrollY = 0;
+            setOwnScrollY(0);
             mPhoneStatusBar.resetUserExpandedStates();
 
             // lets make sure nothing is in the overlay / transient anymore
@@ -3004,7 +3074,7 @@
 
     public void resetScrollPosition() {
         mScroller.abortAnimation();
-        mOwnScrollY = 0;
+        setOwnScrollY(0);
     }
 
     private void setIsExpanded(boolean isExpanded) {
@@ -3040,10 +3110,14 @@
         updateScrollPositionOnExpandInBottom(view);
         clampScrollPosition();
         notifyHeightChangeListener(view);
+        ExpandableNotificationRow row = view instanceof ExpandableNotificationRow
+                ? (ExpandableNotificationRow) view
+                : null;
+        if (row != null && (row == mFirstVisibleBackgroundChild
+                || row.getNotificationParent() == mFirstVisibleBackgroundChild)) {
+            updateAlgorithmLayoutMinHeight();
+        }
         if (needsAnimation) {
-            ExpandableNotificationRow row = view instanceof ExpandableNotificationRow
-                    ? (ExpandableNotificationRow) view
-                    : null;
             requestAnimationOnViewResize(row);
         }
         requestChildrenUpdate();
@@ -3051,9 +3125,6 @@
 
     @Override
     public void onReset(ExpandableView view) {
-        if (mIsExpanded && mAnimationsEnabled) {
-            mRequestViewResizeAnimationOnLayout = true;
-        }
         updateAnimationState(view);
         updateChronometerForChild(view);
     }
@@ -3072,7 +3143,7 @@
                 }
                 int stackEnd = getStackEndPosition();
                 if (endPosition > stackEnd) {
-                    mOwnScrollY += endPosition - stackEnd;
+                    setOwnScrollY((int) (mOwnScrollY + endPosition - stackEnd));
                     mDisallowScrollingInThisMotion = true;
                 }
             }
@@ -3329,7 +3400,7 @@
     }
 
     private int findDarkAnimationOriginIndex(@Nullable PointF screenLocation) {
-        if (screenLocation == null || screenLocation.y < mTopPadding + mTopPaddingOverflow) {
+        if (screenLocation == null || screenLocation.y < mTopPadding) {
             return AnimationEvent.DARK_ANIMATION_ORIGIN_INDEX_ABOVE;
         }
         if (screenLocation.y > getBottomMostNotificationBottom()) {
@@ -3402,7 +3473,7 @@
                         notifyHeightChangeListener(mEmptyShadeView);
                     }
                 };
-                if (mAnimationsEnabled) {
+                if (mAnimationsEnabled && mIsExpanded) {
                     mEmptyShadeView.setWillBeGone(true);
                     mEmptyShadeView.performVisibilityAnimation(false, onFinishedRunnable);
                 } else {
@@ -3634,15 +3705,14 @@
     @Override
     public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
         super.onInitializeAccessibilityNodeInfoInternal(info);
-        final int scrollRange = getScrollRange();
-        if (scrollRange > 0) {
+        if (mScrollable) {
             info.setScrollable(true);
-            if (mScrollY > 0) {
+            if (mBackwardScrollable) {
                 info.addAction(
                         AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
                 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_UP);
             }
-            if (mScrollY < scrollRange) {
+            if (mForwardScrollable) {
                 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
                 info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_DOWN);
             }
@@ -3794,11 +3864,7 @@
     }
 
     private void updateFadingState() {
-        if (mFadingOut || mParentFadingOut || mAmbientState.isDark()) {
-            mScrimController.setExcludedBackgroundArea(null);
-        } else {
-            applyCurrentBackgroundBounds();
-        }
+        applyCurrentBackgroundBounds();
         updateSrcDrawing();
     }
 
@@ -3817,6 +3883,18 @@
         mCurrentStackScrollState.removeViewStateForView(view);
     }
 
+    public void setQsExpanded(boolean qsExpanded) {
+        mQsExpanded = qsExpanded;
+        updateAlgorithmLayoutMinHeight();
+    }
+
+    public void setOwnScrollY(int ownScrollY) {
+        if (ownScrollY != mOwnScrollY) {
+            mOwnScrollY = ownScrollY;
+            updateForwardAndBackwardScrollability();
+        }
+    }
+
     /**
      * A listener that is notified when some child locations might have changed.
      */
@@ -3859,6 +3937,7 @@
     private class NotificationSwipeHelper extends SwipeHelper {
         private static final long SHOW_GEAR_DELAY = 60;
         private static final long COVER_GEAR_DELAY = 4000;
+        private static final long SWIPE_GEAR_TIMING = 200;
         private CheckForDrag mCheckForDrag;
         private Runnable mFalsingCheck;
         private Handler mHandler;
@@ -3975,6 +4054,9 @@
 
             boolean gestureTowardsGear = isTowardsGear(velocity, mCurrIconRow.isIconOnLeft());
             boolean gestureFastEnough = Math.abs(velocity) > getEscapeVelocity();
+            final double timeForGesture = ev.getEventTime() - ev.getDownTime();
+            final boolean showGearForSlowOnGoing = !canChildBeDismissed(animView)
+                && timeForGesture >= SWIPE_GEAR_TIMING;
 
             if (mGearSnappedTo && mCurrIconRow.isVisible()) {
                 if (mGearSnappedOnLeft == mCurrIconRow.isIconOnLeft()) {
@@ -3999,7 +4081,8 @@
                 } else {
                     dismissOrSnapBack(animView, velocity, ev);
                 }
-            } else if ((!gestureFastEnough && swipedEnoughToShowGear(animView))
+            } else if (((!gestureFastEnough || showGearForSlowOnGoing)
+                    && swipedEnoughToShowGear(animView))
                     || gestureTowardsGear) {
                 // Gear has not been snapped to previously and this is gear revealing gesture
                 snapToGear(animView, velocity);
@@ -4035,7 +4118,7 @@
             onDragCancelled(animView);
 
             // If we're on the lockscreen we want to false this.
-            if (mPhoneStatusBar.getBarState() == StatusBarState.KEYGUARD) {
+            if (isAntiFalsingNeeded()) {
                 mHandler.removeCallbacks(mFalsingCheck);
                 mHandler.postDelayed(mFalsingCheck, COVER_GEAR_DELAY);
             }
@@ -4051,13 +4134,9 @@
             final float multiplier = canChildBeDismissed(animView) ? 0.4f : 0.2f;
             final float snapBackThreshold = getSpaceForGear(animView) * multiplier;
             final float translation = getTranslation(animView);
-            final boolean fromLeft = translation > 0;
-            final float absTrans = Math.abs(translation);
-            final float notiThreshold = getSize(mTranslatingParentView) * 0.4f;
-
-            return mCurrIconRow.isVisible() && (mCurrIconRow.isIconOnLeft()
-                    ? (translation > snapBackThreshold && translation <= notiThreshold)
-                    : (translation < -snapBackThreshold && translation >= -notiThreshold));
+            return !swipedFarEnough() && mCurrIconRow.isVisible() && (mCurrIconRow.isIconOnLeft()
+                    ? translation > snapBackThreshold
+                    : translation < -snapBackThreshold);
         }
 
         @Override
@@ -4098,13 +4177,11 @@
                 final int rx = (int) ev.getRawX();
                 final int ry = (int) ev.getRawY();
 
-                getLocationOnScreen(mTempInt2);
-                int[] location = new int[2];
-                view.getLocationOnScreen(location);
-                final int x = location[0] - mTempInt2[0];
-                final int y = location[1] - mTempInt2[1];
+                view.getLocationOnScreen(mTempInt2);
+                final int x = mTempInt2[0];
+                final int y = mTempInt2[1];
                 Rect rect = new Rect(x, y, x + view.getWidth(), y + height);
-                if (!rect.contains((int) rx, (int) ry)) {
+                if (!rect.contains(rx, ry)) {
                     // Touch was outside visible guts / gear notification, close what's visible
                     mPhoneStatusBar.dismissPopups(-1, -1, true /* resetGear */, true /* animate */);
                 }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackStateAnimator.java b/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackStateAnimator.java
index 659eaf7..3804b42 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackStateAnimator.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackStateAnimator.java
@@ -44,6 +44,7 @@
     public static final int ANIMATION_DURATION_GO_TO_FULL_SHADE = 448;
     public static final int ANIMATION_DURATION_APPEAR_DISAPPEAR = 464;
     public static final int ANIMATION_DURATION_DIMMED_ACTIVATED = 220;
+    public static final int ANIMATION_DURATION_CLOSE_REMOTE_INPUT = 150;
     public static final int ANIMATION_DURATION_HEADS_UP_APPEAR = 650;
     public static final int ANIMATION_DURATION_HEADS_UP_DISAPPEAR = 230;
     public static final int ANIMATION_DELAY_PER_ELEMENT_INTERRUPTING = 80;
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java
index 27726af..3c83921 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/tv/TvStatusBar.java
@@ -19,7 +19,6 @@
 import android.content.ComponentName;
 import android.graphics.Rect;
 import android.os.IBinder;
-import android.os.RemoteException;
 import android.service.notification.NotificationListenerService.RankingMap;
 import android.service.notification.StatusBarNotification;
 import android.view.View;
@@ -36,16 +35,6 @@
 
 public class TvStatusBar extends BaseStatusBar {
 
-    /**
-     * Tracking calls to View.setSystemUiVisibility().
-     */
-    int mSystemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE;
-
-    /**
-     * Last value sent to window manager.
-     */
-    private int mLastDispatchedSystemUiVisibility = ~View.SYSTEM_UI_FLAG_VISIBLE;
-
     @Override
     public void setIcon(String slot, StatusBarIcon icon) {
     }
@@ -224,37 +213,8 @@
         putComponent(TvStatusBar.class, this);
     }
 
-    /**
-     * Updates the visibility of the picture-in-picture.
-     */
-    public void updatePipVisibility(boolean visible) {
-        if (visible) {
-            mSystemUiVisibility |= View.TV_PICTURE_IN_PICTURE_VISIBLE;
-        } else {
-            mSystemUiVisibility &= ~View.TV_PICTURE_IN_PICTURE_VISIBLE;
-        }
-        notifyUiVisibilityChanged(mSystemUiVisibility);
-    }
-
-    /**
-     * Updates the visibility of the Recents
-     */
-    public void updateRecentsVisibility(boolean visible) {
-        if (visible) {
-            mSystemUiVisibility |= View.RECENT_APPS_VISIBLE;
-        } else {
-            mSystemUiVisibility &= ~View.RECENT_APPS_VISIBLE;
-        }
-        notifyUiVisibilityChanged(mSystemUiVisibility);
-    }
-
-    private void notifyUiVisibilityChanged(int vis) {
-        try {
-            if (mLastDispatchedSystemUiVisibility != vis) {
-                mWindowManagerService.statusBarVisibilityChanged(vis);
-                mLastDispatchedSystemUiVisibility = vis;
-            }
-        } catch (RemoteException ex) {
-        }
+    @Override
+    public void handleSystemNavigationKey(int arg1) {
+        // Not implemented
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/BatteryPreference.java b/packages/SystemUI/src/com/android/systemui/tuner/BatteryPreference.java
index 8881c79..6e08139 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/BatteryPreference.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/BatteryPreference.java
@@ -47,9 +47,9 @@
     @Override
     public void onAttached() {
         super.onAttached();
-        TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
         mHasPercentage = Settings.System.getInt(getContext().getContentResolver(),
                 SHOW_PERCENT_SETTING, 0) != 0;
+        TunerService.get(getContext()).addTunable(this, StatusBarIconController.ICON_BLACKLIST);
     }
 
     @Override
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/CalibratePreference.java b/packages/SystemUI/src/com/android/systemui/tuner/CalibratePreference.java
deleted file mode 100644
index ff7be13..0000000
--- a/packages/SystemUI/src/com/android/systemui/tuner/CalibratePreference.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- * except in compliance with the License. You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the
- * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
-
-package com.android.systemui.tuner;
-
-import android.content.Context;
-import android.support.v7.preference.DialogPreference;
-import android.util.AttributeSet;
-
-public class CalibratePreference extends DialogPreference {
-    public CalibratePreference(Context context, AttributeSet attrs) {
-        super(context, attrs);
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/ClockPreference.java b/packages/SystemUI/src/com/android/systemui/tuner/ClockPreference.java
index ea92443..caa0527 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/ClockPreference.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/ClockPreference.java
@@ -32,6 +32,8 @@
     private boolean mHasSeconds;
     private ArraySet<String> mBlacklist;
     private boolean mHasSetValue;
+    private boolean mReceivedSeconds;
+    private boolean mReceivedClock;
 
     public ClockPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
@@ -55,12 +57,14 @@
     @Override
     public void onTuningChanged(String key, String newValue) {
         if (StatusBarIconController.ICON_BLACKLIST.equals(key)) {
+            mReceivedClock = true;
             mBlacklist = StatusBarIconController.getIconBlacklist(newValue);
             mClockEnabled = !mBlacklist.contains(mClock);
         } else if (Clock.CLOCK_SECONDS.equals(key)) {
+            mReceivedSeconds = true;
             mHasSeconds = newValue != null && Integer.parseInt(newValue) != 0;
         }
-        if (!mHasSetValue) {
+        if (!mHasSetValue && mReceivedClock && mReceivedSeconds) {
             // Because of the complicated tri-state it can end up looping and setting state back to
             // what the user didn't choose.  To avoid this, just set the state once and rely on the
             // preference to handle updates.
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/ColorAndAppearanceFragment.java b/packages/SystemUI/src/com/android/systemui/tuner/ColorAndAppearanceFragment.java
deleted file mode 100644
index af95cf9..0000000
--- a/packages/SystemUI/src/com/android/systemui/tuner/ColorAndAppearanceFragment.java
+++ /dev/null
@@ -1,219 +0,0 @@
-/*
- * Copyright (C) 2016 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- * except in compliance with the License. You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the
- * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
-
-package com.android.systemui.tuner;
-
-
-import android.app.AlertDialog;
-import android.app.Dialog;
-import android.app.DialogFragment;
-import android.content.DialogInterface;
-import android.os.Bundle;
-import android.provider.Settings;
-import android.provider.Settings.Secure;
-import android.support.v14.preference.PreferenceFragment;
-import android.support.v7.preference.Preference;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.widget.SeekBar;
-
-import com.android.internal.logging.MetricsLogger;
-import com.android.internal.logging.MetricsProto.MetricsEvent;
-import com.android.systemui.R;
-import com.android.systemui.statusbar.policy.NightModeController;
-
-public class ColorAndAppearanceFragment extends PreferenceFragment {
-
-    private static final String KEY_CALIBRATE = "calibrate";
-
-    private static final long RESET_DELAY = 10000;
-    private static final CharSequence KEY_NIGHT_MODE = "night_mode";
-
-    private NightModeController mNightModeController;
-
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        mNightModeController = new NightModeController(getContext());
-    }
-
-    @Override
-    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
-        addPreferencesFromResource(R.xml.color_and_appearance);
-    }
-
-    @Override
-    public void onResume() {
-        super.onResume();
-        MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_COLOR_AND_APPEARANCE, true);
-        // TODO: Figure out better title model for Tuner, to avoid any more of this.
-        getActivity().setTitle(R.string.color_and_appearance);
-
-        Preference nightMode = findPreference(KEY_NIGHT_MODE);
-        nightMode.setSummary(mNightModeController.isEnabled()
-                ? R.string.night_mode_on : R.string.night_mode_off);
-    }
-
-    @Override
-    public void onPause() {
-        super.onPause();
-        MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_COLOR_AND_APPEARANCE, false);
-    }
-
-    @Override
-    public void onDisplayPreferenceDialog(Preference preference) {
-        if (preference instanceof CalibratePreference) {
-            CalibrateDialog.show(this);
-        } else {
-            super.onDisplayPreferenceDialog(preference);
-        }
-    }
-
-    private void startRevertTimer() {
-        getView().postDelayed(mResetColorMatrix, RESET_DELAY);
-    }
-
-    private void onApply() {
-        MetricsLogger.action(getContext(), MetricsEvent.ACTION_TUNER_CALIBRATE_DISPLAY_CHANGED);
-        mNightModeController.setCustomValues(Settings.Secure.getString(
-                getContext().getContentResolver(), Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX));
-        getView().removeCallbacks(mResetColorMatrix);
-    }
-
-    private void onRevert() {
-        getView().removeCallbacks(mResetColorMatrix);
-        mResetColorMatrix.run();
-    }
-
-    private final Runnable mResetColorMatrix = new Runnable() {
-        @Override
-        public void run() {
-            ((DialogFragment) getFragmentManager().findFragmentByTag("RevertWarning")).dismiss();
-            Settings.Secure.putString(getContext().getContentResolver(),
-                    Settings.Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX, null);
-        }
-    };
-
-    public static class CalibrateDialog extends DialogFragment implements
-            DialogInterface.OnClickListener {
-        private float[] mValues;
-        private NightModeController mNightModeController;
-
-        public static void show(ColorAndAppearanceFragment fragment) {
-            CalibrateDialog dialog = new CalibrateDialog();
-            dialog.setTargetFragment(fragment, 0);
-            dialog.show(fragment.getFragmentManager(), "Calibrate");
-        }
-
-        @Override
-        public void onCreate(Bundle savedInstanceState) {
-            super.onCreate(savedInstanceState);
-            mNightModeController = new NightModeController(getContext());
-            String customValues = mNightModeController.getCustomValues();
-            if (customValues == null) {
-                // Generate this as a string because its the easiest way to generate a copy of the
-                // identity.
-                customValues = NightModeController.toString(NightModeController.IDENTITY_MATRIX);
-            }
-            mValues = NightModeController.toValues(customValues);
-        }
-
-        @Override
-        public Dialog onCreateDialog(Bundle savedInstanceState) {
-            View v = LayoutInflater.from(getContext()).inflate(R.layout.calibrate_sliders, null);
-            bindView(v.findViewById(R.id.r_group), 0);
-            bindView(v.findViewById(R.id.g_group), 5);
-            bindView(v.findViewById(R.id.b_group), 10);
-            MetricsLogger.visible(getContext(), MetricsEvent.TUNER_CALIBRATE_DISPLAY);
-            return new AlertDialog.Builder(getContext())
-                    .setTitle(R.string.calibrate_display)
-                    .setView(v)
-                    .setPositiveButton(R.string.color_apply, this)
-                    .setNegativeButton(android.R.string.cancel, null)
-                    .create();
-        }
-
-        @Override
-        public void onDismiss(DialogInterface dialog) {
-            super.onDismiss(dialog);
-            MetricsLogger.hidden(getContext(), MetricsEvent.TUNER_CALIBRATE_DISPLAY);
-        }
-
-        private void bindView(View view, final int index) {
-            SeekBar seekBar = (SeekBar) view.findViewById(com.android.internal.R.id.seekbar);
-            seekBar.setMax(1000);
-            seekBar.setProgress((int) (1000 * mValues[index]));
-            seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
-                @Override
-                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
-                    mValues[index] = progress / 1000f;
-                }
-
-                @Override
-                public void onStartTrackingTouch(SeekBar seekBar) {
-                }
-
-                @Override
-                public void onStopTrackingTouch(SeekBar seekBar) {
-                }
-            });
-        }
-
-        @Override
-        public void onClick(DialogInterface dialog, int which) {
-            if (mValues[0] == 1 && mValues[5] == 1 && mValues[10] == 1) {
-                // Allow removal of matrix by all values set to highest.
-                mNightModeController.setCustomValues(null);
-                return;
-            }
-            ((ColorAndAppearanceFragment) getTargetFragment()).startRevertTimer();
-            Settings.Secure.putString(getContext().getContentResolver(),
-                    Settings.Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX,
-                    NightModeController.toString(mValues));
-            RevertWarning.show((ColorAndAppearanceFragment) getTargetFragment());
-        }
-    }
-
-    public static class RevertWarning extends DialogFragment
-            implements DialogInterface.OnClickListener {
-
-        public static void show(ColorAndAppearanceFragment fragment) {
-            RevertWarning warning = new RevertWarning();
-            warning.setTargetFragment(fragment, 0);
-            warning.show(fragment.getFragmentManager(), "RevertWarning");
-        }
-
-        @Override
-        public Dialog onCreateDialog(Bundle savedInstanceState) {
-            AlertDialog alertDialog = new AlertDialog.Builder(getContext())
-                    .setTitle(R.string.color_revert_title)
-                    .setMessage(R.string.color_revert_message)
-                    .setPositiveButton(R.string.ok, this)
-                    .create();
-            alertDialog.setCanceledOnTouchOutside(true);
-            return alertDialog;
-        }
-
-        @Override
-        public void onCancel(DialogInterface dialog) {
-            super.onCancel(dialog);
-            ((ColorAndAppearanceFragment) getTargetFragment()).onRevert();
-        }
-
-        @Override
-        public void onClick(DialogInterface dialog, int which) {
-            ((ColorAndAppearanceFragment) getTargetFragment()).onApply();
-        }
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/NightModeFragment.java b/packages/SystemUI/src/com/android/systemui/tuner/NightModeFragment.java
deleted file mode 100644
index ae2856c..0000000
--- a/packages/SystemUI/src/com/android/systemui/tuner/NightModeFragment.java
+++ /dev/null
@@ -1,188 +0,0 @@
-/**
- * Copyright (c) 2016, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.systemui.tuner;
-
-import android.annotation.Nullable;
-import android.app.UiModeManager;
-import android.content.Context;
-import android.os.Bundle;
-import android.provider.Settings.Secure;
-import android.support.v14.preference.PreferenceFragment;
-import android.support.v14.preference.SwitchPreference;
-import android.support.v7.preference.Preference;
-import android.support.v7.preference.Preference.OnPreferenceChangeListener;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.Switch;
-import com.android.internal.logging.MetricsLogger;
-import com.android.internal.logging.MetricsProto.MetricsEvent;
-import com.android.systemui.R;
-import com.android.systemui.statusbar.policy.NightModeController;
-import com.android.systemui.statusbar.policy.NightModeController.Listener;
-import com.android.systemui.tuner.TunerService.Tunable;
-
-public class NightModeFragment extends PreferenceFragment implements Tunable,
-        Listener, OnPreferenceChangeListener {
-
-    private static final String TAG = "NightModeFragment";
-
-    public static final String EXTRA_SHOW_NIGHT_MODE = "show_night_mode";
-
-    private static final CharSequence KEY_AUTO = "auto";
-    private static final CharSequence KEY_ADJUST_TINT = "adjust_tint";
-    private static final CharSequence KEY_ADJUST_BRIGHTNESS = "adjust_brightness";
-
-    private Switch mSwitch;
-
-    private NightModeController mNightModeController;
-    private SwitchPreference mAutoSwitch;
-    private SwitchPreference mAdjustTint;
-    private SwitchPreference mAdjustBrightness;
-    private UiModeManager mUiModeManager;
-
-    @Override
-    public void onCreate(@Nullable Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        mNightModeController = new NightModeController(getContext());
-        mUiModeManager = getContext().getSystemService(UiModeManager.class);
-    }
-
-    @Override
-    public View onCreateView(LayoutInflater inflater, ViewGroup container,
-            Bundle savedInstanceState) {
-        final View view = LayoutInflater.from(getContext()).inflate(
-                R.layout.night_mode_settings, container, false);
-        ((ViewGroup) view).addView(super.onCreateView(inflater, container, savedInstanceState));
-        return view;
-    }
-
-    @Override
-    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
-        final Context context = getPreferenceManager().getContext();
-
-        addPreferencesFromResource(R.xml.night_mode);
-        mAutoSwitch = (SwitchPreference) findPreference(KEY_AUTO);
-        mAutoSwitch.setOnPreferenceChangeListener(this);
-        mAdjustTint = (SwitchPreference) findPreference(KEY_ADJUST_TINT);
-        mAdjustTint.setOnPreferenceChangeListener(this);
-        mAdjustBrightness = (SwitchPreference) findPreference(KEY_ADJUST_BRIGHTNESS);
-        mAdjustBrightness.setOnPreferenceChangeListener(this);
-    }
-
-    @Override
-    public void onViewCreated(View view, Bundle savedInstanceState) {
-        super.onViewCreated(view, savedInstanceState);
-        View switchBar = view.findViewById(R.id.switch_bar);
-        mSwitch = (Switch) switchBar.findViewById(android.R.id.switch_widget);
-        mSwitch.setChecked(mNightModeController.isEnabled());
-        switchBar.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                boolean newState = !mNightModeController.isEnabled();
-                MetricsLogger.action(getContext(), MetricsEvent.ACTION_TUNER_NIGHT_MODE, newState);
-                mNightModeController.setNightMode(newState);
-                mSwitch.setChecked(newState);
-            }
-        });
-    }
-
-    @Override
-    public void onResume() {
-        super.onResume();
-        MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_NIGHT_MODE, true);
-        mNightModeController.addListener(this);
-        TunerService.get(getContext()).addTunable(this, Secure.BRIGHTNESS_USE_TWILIGHT,
-                NightModeController.NIGHT_MODE_ADJUST_TINT);
-        calculateDisabled();
-    }
-
-    @Override
-    public void onPause() {
-        super.onPause();
-        MetricsLogger.visibility(getContext(), MetricsEvent.TUNER_NIGHT_MODE, false);
-        mNightModeController.removeListener(this);
-        TunerService.get(getContext()).removeTunable(this);
-    }
-
-    @Override
-    public boolean onPreferenceChange(Preference preference, Object newValue) {
-        final Boolean value = (Boolean) newValue;
-        if (mAutoSwitch == preference) {
-            MetricsLogger.action(getContext(), MetricsEvent.ACTION_TUNER_NIGHT_MODE_AUTO, value);
-            mNightModeController.setAuto(value);
-        } else if (mAdjustTint == preference) {
-            MetricsLogger.action(getContext(),
-                    MetricsEvent.ACTION_TUNER_NIGHT_MODE_ADJUST_TINT, value);
-            mNightModeController.setAdjustTint(value);
-            postCalculateDisabled();
-        } else if (mAdjustBrightness == preference) {
-            MetricsLogger.action(getContext(),
-                    MetricsEvent.ACTION_TUNER_NIGHT_MODE_ADJUST_BRIGHTNESS, value);
-            TunerService.get(getContext()).setValue(Secure.BRIGHTNESS_USE_TWILIGHT,
-                    value ? 1 : 0);
-            postCalculateDisabled();
-        } else {
-            return false;
-        }
-        return true;
-    }
-
-    private void postCalculateDisabled() {
-        // Post this because its the easiest way to wait for all state to be calculated.
-        getView().post(new Runnable() {
-            @Override
-            public void run() {
-                calculateDisabled();
-            }
-        });
-    }
-
-    private void calculateDisabled() {
-        int enabledCount = (mAdjustTint.isChecked() ? 1 : 0)
-                + (mAdjustBrightness.isChecked() ? 1 : 0);
-        if (enabledCount == 1) {
-            if (mAdjustTint.isChecked()) {
-                mAdjustTint.setEnabled(false);
-            } else {
-                mAdjustBrightness.setEnabled(false);
-            }
-        } else {
-            mAdjustTint.setEnabled(true);
-            mAdjustBrightness.setEnabled(true);
-        }
-    }
-
-    @Override
-    public void onTuningChanged(String key, String newValue) {
-        if (Secure.BRIGHTNESS_USE_TWILIGHT.equals(key)) {
-            mAdjustBrightness.setChecked(newValue != null && Integer.parseInt(newValue) != 0);
-        } else if (NightModeController.NIGHT_MODE_ADJUST_TINT.equals(key)) {
-            // Default on.
-            mAdjustTint.setChecked(newValue == null || Integer.parseInt(newValue) != 0);
-        }
-    }
-
-    @Override
-    public void onNightModeChanged() {
-        mSwitch.setChecked(mNightModeController.isEnabled());
-    }
-
-    @Override
-    public void onTwilightAutoChanged() {
-        mAutoSwitch.setChecked(mNightModeController.isAuto());
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/NightModeTile.java b/packages/SystemUI/src/com/android/systemui/tuner/NightModeTile.java
deleted file mode 100644
index fe44502..0000000
--- a/packages/SystemUI/src/com/android/systemui/tuner/NightModeTile.java
+++ /dev/null
@@ -1,107 +0,0 @@
-/**
- * Copyright (c) 2016, The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.systemui.tuner;
-
-import android.content.Intent;
-import com.android.internal.logging.MetricsProto.MetricsEvent;
-import com.android.systemui.Prefs;
-import com.android.systemui.Prefs.Key;
-import com.android.systemui.R;
-import com.android.systemui.qs.QSTile;
-import com.android.systemui.statusbar.policy.NightModeController;
-
-
-public class NightModeTile extends QSTile<QSTile.State> implements NightModeController.Listener {
-
-    public static final String NIGHT_MODE_SPEC = "night";
-
-    private final NightModeController mNightModeController;
-
-    private int mIndex;
-    private String mCurrentValue;
-
-    private boolean mCustomEnabled;
-    private String[] mValues;
-    private CharSequence[] mValueTitles;
-
-    public NightModeTile(Host host) {
-        super(host);
-        mNightModeController = host.getNightModeController();
-    }
-
-    @Override
-    public boolean isAvailable() {
-        return Prefs.getBoolean(mContext, Key.QS_NIGHT_ADDED, false)
-                && TunerService.isTunerEnabled(mContext);
-    }
-
-    @Override
-    public void setListening(boolean listening) {
-        if (listening) {
-            mNightModeController.addListener(this);
-            refreshState();
-        } else {
-            mNightModeController.removeListener(this);
-        }
-    }
-
-    @Override
-    public State newTileState() {
-        return new State();
-    }
-
-    @Override
-    public Intent getLongClickIntent() {
-        return new Intent(mContext, TunerActivity.class)
-                .putExtra(NightModeFragment.EXTRA_SHOW_NIGHT_MODE, true);
-    }
-
-    @Override
-    protected void handleClick() {
-        mNightModeController.setNightMode(!mNightModeController.isEnabled());
-        refreshState();
-    }
-
-    @Override
-    public CharSequence getTileLabel() {
-        return mContext.getString(R.string.night_mode);
-    }
-
-    @Override
-    protected void handleUpdateState(State state, Object arg) {
-        // TODO: Right now this is just a dropper, needs an actual night icon.
-        boolean enabled = mNightModeController.isEnabled();
-        state.icon = ResourceIcon.get(enabled ? R.drawable.ic_night_mode
-                : R.drawable.ic_night_mode_disabled);
-        state.label = mContext.getString(R.string.night_mode);
-        state.contentDescription = mContext.getString(R.string.night_mode);
-    }
-
-    @Override
-    public void onNightModeChanged() {
-        refreshState();
-    }
-
-    @Override
-    public void onTwilightAutoChanged() {
-        // Don't care.
-    }
-
-    @Override
-    public int getMetricsCategory() {
-        return MetricsEvent.QS_COLOR_MATRIX;
-    }
-}
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/TunerActivity.java b/packages/SystemUI/src/com/android/systemui/tuner/TunerActivity.java
index 5e5da74..5fe9296 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/TunerActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/TunerActivity.java
@@ -39,10 +39,7 @@
             final String action = getIntent().getAction();
             boolean showDemoMode = action != null && action.equals(
                     "com.android.settings.action.DEMO_MODE");
-            boolean showNightMode = getIntent().getBooleanExtra(
-                    NightModeFragment.EXTRA_SHOW_NIGHT_MODE, false);
-            final PreferenceFragment fragment = showNightMode ? new NightModeFragment()
-                    : showDemoMode ? new DemoModeFragment()
+            final PreferenceFragment fragment = showDemoMode ? new DemoModeFragment()
                     : new TunerFragment();
             getFragmentManager().beginTransaction().replace(R.id.content_frame,
                     fragment, TAG_TUNER).commit();
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/TunerZenModePanel.java b/packages/SystemUI/src/com/android/systemui/tuner/TunerZenModePanel.java
index cc0ffb0..1ea23bb 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/TunerZenModePanel.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/TunerZenModePanel.java
@@ -54,7 +54,6 @@
         mHeaderSwitch = findViewById(R.id.tuner_zen_switch);
         mHeaderSwitch.setVisibility(View.VISIBLE);
         mHeaderSwitch.setOnClickListener(this);
-        mHeaderSwitch.findViewById(com.android.internal.R.id.up).setVisibility(View.GONE);
         ((TextView) mHeaderSwitch.findViewById(android.R.id.title)).setText(
                 R.string.quick_settings_dnd_label);
         mZenModePanel = (ZenModePanel) findViewById(R.id.zen_mode_panel);
diff --git a/packages/SystemUI/src/com/android/systemui/tv/pip/PipManager.java b/packages/SystemUI/src/com/android/systemui/tv/pip/PipManager.java
index 30622d2..085e003 100644
--- a/packages/SystemUI/src/com/android/systemui/tv/pip/PipManager.java
+++ b/packages/SystemUI/src/com/android/systemui/tv/pip/PipManager.java
@@ -78,6 +78,18 @@
         sSettingsPackageAndClassNamePairList.add(new Pair<String, String>(
                 "com.google.android.leanbacklauncher",
                 "com.google.android.leanbacklauncher.settings.HomeScreenSettingsActivity"));
+        sSettingsPackageAndClassNamePairList.add(new Pair<String, String>(
+                "com.google.android.apps.mediashell",
+                "com.google.android.apps.mediashell.settings.CastSettingsActivity"));
+        sSettingsPackageAndClassNamePairList.add(new Pair<String, String>(
+                "com.google.android.katniss",
+                "com.google.android.katniss.setting.SpeechSettingsActivity"));
+        sSettingsPackageAndClassNamePairList.add(new Pair<String, String>(
+                "com.google.android.katniss",
+                "com.google.android.katniss.setting.SearchSettingsActivity"));
+        sSettingsPackageAndClassNamePairList.add(new Pair<String, String>(
+                "com.google.android.gsf.notouch",
+                "com.google.android.gsf.notouch.UsageDiagnosticsSettingActivity"));
     }
 
     /**
@@ -196,7 +208,23 @@
         }
         mInitialized = true;
         mContext = context;
-        Resources res = context.getResources();
+
+        mActivityManager = ActivityManagerNative.getDefault();
+        SystemServicesProxy.getInstance(context).registerTaskStackListener(mTaskStackListener);
+        IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(Intent.ACTION_MEDIA_RESOURCE_GRANTED);
+        mContext.registerReceiver(mBroadcastReceiver, intentFilter);
+        mOnboardingShown = Prefs.getBoolean(
+                mContext, TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN, false);
+
+        loadConfigurationsAndApply();
+        mPipRecentsOverlayManager = new PipRecentsOverlayManager(context);
+        mMediaSessionManager =
+                (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
+    }
+
+    private void loadConfigurationsAndApply() {
+        Resources res = mContext.getResources();
         mDefaultPipBounds = Rect.unflattenFromString(res.getString(
                 com.android.internal.R.string.config_defaultPictureInPictureBounds));
         mSettingsPipBounds = Rect.unflattenFromString(res.getString(
@@ -209,25 +237,19 @@
                 R.string.pip_recents_focused_bounds));
         mRecentsFocusChangedAnimationDurationMs = res.getInteger(
                 R.integer.recents_tv_pip_focus_anim_duration);
-        mPipBounds = mDefaultPipBounds;
 
-        mActivityManager = ActivityManagerNative.getDefault();
-        SystemServicesProxy.getInstance(context).registerTaskStackListener(mTaskStackListener);
-        IntentFilter intentFilter = new IntentFilter();
-        intentFilter.addAction(Intent.ACTION_MEDIA_RESOURCE_GRANTED);
-        mContext.registerReceiver(mBroadcastReceiver, intentFilter);
-        mOnboardingShown = Prefs.getBoolean(
-                mContext, TV_PICTURE_IN_PICTURE_ONBOARDING_SHOWN, false);
-
-        mPipRecentsOverlayManager = new PipRecentsOverlayManager(context);
-        mMediaSessionManager =
-                (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
+        // Reset the PIP bounds and apply. PIP bounds can be changed by two reasons.
+        //   1. Configuration changed due to the language change (RTL <-> RTL)
+        //   2. SystemUI restarts after the crash
+        mPipBounds = isSettingsShown() ? mSettingsPipBounds : mDefaultPipBounds;
+        resizePinnedStack(getPinnedStackInfo() == null ? STATE_NO_PIP : STATE_PIP_OVERLAY);
     }
 
     /**
      * Updates the PIP per configuration changed.
      */
     void onConfigurationChanged() {
+        loadConfigurationsAndApply();
         mPipRecentsOverlayManager.onConfigurationChanged(mContext);
     }
 
@@ -443,6 +465,16 @@
         return mState != STATE_NO_PIP;
     }
 
+    private StackInfo getPinnedStackInfo() {
+        StackInfo stackInfo = null;
+        try {
+            stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID);
+        } catch (RemoteException e) {
+            Log.e(TAG, "getStackInfo failed", e);
+        }
+        return stackInfo;
+    }
+
     private void handleMediaResourceGranted(String[] packageNames) {
         if (mState == STATE_NO_PIP) {
             mLastPackagesResourceGranted = packageNames;
@@ -525,10 +557,21 @@
         return PLAYBACK_STATE_UNAVAILABLE;
     }
 
-    private static boolean isSettingsShown(ComponentName topActivity) {
+    private boolean isSettingsShown() {
+        List<RunningTaskInfo> runningTasks;
+        try {
+            runningTasks = mActivityManager.getTasks(1, 0);
+            if (runningTasks == null || runningTasks.size() == 0) {
+                return false;
+            }
+        } catch (RemoteException e) {
+            Log.d(TAG, "Failed to detect top activity", e);
+            return false;
+        }
+        ComponentName topActivity = runningTasks.get(0).topActivity;
         for (Pair<String, String> componentName : sSettingsPackageAndClassNamePairList) {
             String packageName = componentName.first;
-            if (topActivity.getPackageName().equals(componentName.first)) {
+            if (topActivity.getPackageName().equals(packageName)) {
                 String className = componentName.second;
                 if (className == null || topActivity.getClassName().equals(className)) {
                     return true;
@@ -544,16 +587,10 @@
             if (mState != STATE_NO_PIP) {
                 boolean hasPip = false;
 
-                StackInfo stackInfo = null;
-                try {
-                    stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID);
-                    if (stackInfo == null) {
-                        Log.w(TAG, "There is no pinned stack");
-                        closePipInternal(false);
-                        return;
-                    }
-                } catch (RemoteException e) {
-                    Log.e(TAG, "getStackInfo failed", e);
+                StackInfo stackInfo = getPinnedStackInfo();
+                if (stackInfo == null || stackInfo.taskIds == null) {
+                    Log.w(TAG, "There is nothing in pinned stack");
+                    closePipInternal(false);
                     return;
                 }
                 for (int i = stackInfo.taskIds.length - 1; i >= 0; --i) {
@@ -570,20 +607,10 @@
                 }
             }
             if (mState == STATE_PIP_OVERLAY) {
-                try {
-                    List<RunningTaskInfo> runningTasks = mActivityManager.getTasks(1, 0);
-                    if (runningTasks == null || runningTasks.size() == 0) {
-                        return;
-                    }
-                    RunningTaskInfo topTask = runningTasks.get(0);
-                    Rect bounds = isSettingsShown(topTask.topActivity)
-                          ? mSettingsPipBounds : mDefaultPipBounds;
-                    if (mPipBounds != bounds) {
-                        mPipBounds = bounds;
-                        resizePinnedStack(STATE_PIP_OVERLAY);
-                    }
-                } catch (RemoteException e) {
-                    Log.d(TAG, "Failed to detect top activity", e);
+                Rect bounds = isSettingsShown() ? mSettingsPipBounds : mDefaultPipBounds;
+                if (mPipBounds != bounds) {
+                    mPipBounds = bounds;
+                    resizePinnedStack(STATE_PIP_OVERLAY);
                 }
             }
         }
@@ -591,15 +618,9 @@
         @Override
         public void onActivityPinned() {
             if (DEBUG) Log.d(TAG, "onActivityPinned()");
-            StackInfo stackInfo = null;
-            try {
-                stackInfo = mActivityManager.getStackInfo(PINNED_STACK_ID);
-                if (stackInfo == null) {
-                    Log.w(TAG, "Cannot find pinned stack");
-                    return;
-                }
-            } catch (RemoteException e) {
-                Log.e(TAG, "getStackInfo failed", e);
+            StackInfo stackInfo = getPinnedStackInfo();
+            if (stackInfo == null) {
+                Log.w(TAG, "Cannot find pinned stack");
                 return;
             }
             if (DEBUG) Log.d(TAG, "PINNED_STACK:" + stackInfo);
@@ -702,10 +723,7 @@
         return mPipRecentsOverlayManager;
     }
 
-    private void updatePipVisibility(boolean visible) {
-        TvStatusBar statusBar = ((SystemUIApplication) mContext).getComponent(TvStatusBar.class);
-        if (statusBar != null) {
-            statusBar.updatePipVisibility(visible);
-        }
+    private void updatePipVisibility(final boolean visible) {
+        SystemServicesProxy.getInstance(mContext).setTvPipVisibility(visible);
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialog.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialog.java
index 1973e05..4aa14e2 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialog.java
@@ -17,9 +17,7 @@
 package com.android.systemui.volume;
 
 import android.accessibilityservice.AccessibilityServiceInfo;
-import android.animation.LayoutTransition;
 import android.animation.ObjectAnimator;
-import android.animation.ValueAnimator;
 import android.annotation.NonNull;
 import android.annotation.SuppressLint;
 import android.app.Dialog;
@@ -43,9 +41,11 @@
 import android.os.Message;
 import android.os.SystemClock;
 import android.provider.Settings.Global;
+import android.transition.AutoTransition;
+import android.transition.Transition;
+import android.transition.TransitionManager;
 import android.util.DisplayMetrics;
 import android.util.Log;
-import android.util.Slog;
 import android.util.SparseBooleanArray;
 import android.view.Gravity;
 import android.view.MotionEvent;
@@ -63,11 +63,12 @@
 import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener;
 import android.view.animation.DecelerateInterpolator;
 import android.widget.ImageButton;
-import android.widget.LinearLayout;
 import android.widget.SeekBar;
 import android.widget.SeekBar.OnSeekBarChangeListener;
 import android.widget.TextView;
 
+import com.android.settingslib.Utils;
+import com.android.systemui.Interpolators;
 import com.android.systemui.R;
 import com.android.systemui.statusbar.policy.ZenModeController;
 import com.android.systemui.tuner.TunerService;
@@ -101,8 +102,10 @@
     private final H mHandler = new H();
     private final VolumeDialogController mController;
 
+    private Window mWindow;
     private CustomDialog mDialog;
     private ViewGroup mDialogView;
+    private ViewGroup mDialogRowsView;
     private ViewGroup mDialogContentView;
     private ImageButton mExpandButton;
     private final List<VolumeRow> mRows = new ArrayList<>();
@@ -113,7 +116,6 @@
     private final AccessibilityManager mAccessibilityMgr;
     private int mExpandButtonAnimationDuration;
     private ZenFooter mZenFooter;
-    private LayoutTransition mLayoutTransition;
     private final Object mSafetyWarningLock = new Object();
     private final Accessibility mAccessibility = new Accessibility();
     private final ColorStateList mActiveSliderTint;
@@ -151,8 +153,9 @@
         mZenModeController = zenModeController;
         mKeyguard = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
-        mAccessibilityMgr = (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
-        mActiveSliderTint = loadColorStateList(R.color.system_accent_color);
+        mAccessibilityMgr =
+                (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
+        mActiveSliderTint = ColorStateList.valueOf(Utils.getColorAccent(mContext));
         mInactiveSliderTint = loadColorStateList(R.color.volume_slider_inactive);
 
         initDialog();
@@ -171,15 +174,13 @@
         mDialog = new CustomDialog(mContext);
 
         mSpTexts = new SpTexts(mContext);
-        mLayoutTransition = new LayoutTransition();
-        mLayoutTransition.setDuration(new ValueAnimator().getDuration() / 2);
         mHovering = false;
         mShowing = false;
-        final Window window = mDialog.getWindow();
-        window.requestFeature(Window.FEATURE_NO_TITLE);
-        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
-        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
-        window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
+        mWindow = mDialog.getWindow();
+        mWindow.requestFeature(Window.FEATURE_NO_TITLE);
+        mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
+        mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
+        mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
@@ -187,7 +188,7 @@
                 | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
         mDialog.setCanceledOnTouchOutside(true);
         final Resources res = mContext.getResources();
-        final WindowManager.LayoutParams lp = window.getAttributes();
+        final WindowManager.LayoutParams lp = mWindow.getAttributes();
         lp.type = mWindowType;
         lp.format = PixelFormat.TRANSLUCENT;
         lp.setTitle(VolumeDialog.class.getSimpleName());
@@ -195,9 +196,8 @@
         lp.y = res.getDimensionPixelSize(R.dimen.volume_offset_top);
         lp.gravity = Gravity.TOP;
         lp.windowAnimations = -1;
-        window.setAttributes(lp);
-        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
-
+        mWindow.setAttributes(lp);
+        mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
 
         mDialog.setContentView(R.layout.volume_dialog);
         mDialogView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog);
@@ -212,13 +212,13 @@
             }
         });
         mDialogContentView = (ViewGroup) mDialog.findViewById(R.id.volume_dialog_content);
+        mDialogRowsView = (ViewGroup) mDialogContentView.findViewById(R.id.volume_dialog_rows);
         mExpanded = false;
         mExpandButton = (ImageButton) mDialogView.findViewById(R.id.volume_expand_button);
         mExpandButton.setOnClickListener(mClickExpand);
         updateWindowWidthH();
         updateExpandButtonH();
 
-        mDialogContentView.setLayoutTransition(mLayoutTransition);
         mMotion = new VolumeDialogMotion(mDialog, mDialogView, mDialogContentView, mExpandButton,
                 new VolumeDialogMotion.Callback() {
                     @Override
@@ -309,10 +309,7 @@
     private void addRow(int stream, int iconRes, int iconMuteRes, boolean important) {
         VolumeRow row = new VolumeRow();
         initRow(row, stream, iconRes, iconMuteRes, important);
-        if (!mRows.isEmpty()) {
-            addSpacer(row);
-        }
-        mDialogContentView.addView(row.view, mDialogContentView.getChildCount() - 2);
+        mDialogRowsView.addView(row.view);
         mRows.add(row);
     }
 
@@ -321,23 +318,10 @@
         for (int i = 0; i < N; i++) {
             final VolumeRow row = mRows.get(i);
             initRow(row, row.stream, row.iconRes, row.iconMuteRes, row.important);
-            if (i > 0) {
-                addSpacer(row);
-            }
-            mDialogContentView.addView(row.view, mDialogContentView.getChildCount() - 2);
+            mDialogRowsView.addView(row.view);
         }
     }
 
-    private void addSpacer(VolumeRow row) {
-        final View v = new View(mContext);
-        v.setId(android.R.id.background);
-        final int h = mContext.getResources()
-                .getDimensionPixelSize(R.dimen.volume_slider_interspacing);
-        final LinearLayout.LayoutParams lp =
-                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, h);
-        mDialogContentView.addView(v, mDialogContentView.getChildCount() - 2, lp);
-        row.space = v;
-    }
 
     private boolean isAttached() {
         return mDialogContentView != null && mDialogContentView.isAttachedToWindow();
@@ -391,12 +375,15 @@
         row.iconMuteRes = iconMuteRes;
         row.important = important;
         row.view = mDialog.getLayoutInflater().inflate(R.layout.volume_dialog_row, null);
+        row.view.setId(row.stream);
         row.view.setTag(row);
         row.header = (TextView) row.view.findViewById(R.id.volume_row_header);
+        row.header.setId(20 * row.stream);
         mSpTexts.add(row.header);
         row.slider = (SeekBar) row.view.findViewById(R.id.volume_row_slider);
         row.slider.setOnSeekBarChangeListener(new VolumeSeekBarChangeListener(row));
         row.anim = null;
+        row.cachedShowHeaders = VolumePrefs.DEFAULT_SHOW_HEADERS;
 
         // forward events above the slider into the slider
         row.view.setOnTouchListener(new OnTouchListener() {
@@ -508,7 +495,7 @@
         mMotion.startDismiss(new Runnable() {
             @Override
             public void run() {
-                setExpandedH(false);
+                updateExpandedH(false /* expanding */, true /* dismissing */);
             }
         });
         if (mAccessibilityMgr.isEnabled()) {
@@ -554,15 +541,67 @@
         mHandler.sendEmptyMessageDelayed(H.UPDATE_BOTTOM_MARGIN, getConservativeCollapseDuration());
     }
 
-    private void setExpandedH(boolean expanded) {
+    private void updateExpandedH(final boolean expanded, final boolean dismissing) {
         if (mExpanded == expanded) return;
         mExpanded = expanded;
         mExpandButtonAnimationRunning = isAttached();
-        if (D.BUG) Log.d(TAG, "setExpandedH " + expanded);
-        if (!mExpanded && mExpandButtonAnimationRunning) {
-            prepareForCollapse();
+        if (D.BUG) Log.d(TAG, "updateExpandedH " + expanded);
+        updateExpandButtonH();
+        updateFooterH();
+        TransitionManager.endTransitions(mDialogView);
+        final VolumeRow activeRow = getActiveRow();
+        if (!dismissing) {
+            mWindow.setLayout(mWindow.getAttributes().width, ViewGroup.LayoutParams.MATCH_PARENT);
+            AutoTransition transition = new AutoTransition();
+            transition.setDuration(mExpandButtonAnimationDuration);
+            transition.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN);
+            transition.addListener(new Transition.TransitionListener() {
+                @Override
+                public void onTransitionStart(Transition transition) {
+                }
+
+                @Override
+                public void onTransitionEnd(Transition transition) {
+                    mWindow.setLayout(
+                            mWindow.getAttributes().width, ViewGroup.LayoutParams.WRAP_CONTENT);
+                }
+
+                @Override
+                public void onTransitionCancel(Transition transition) {
+                }
+
+                @Override
+                public void onTransitionPause(Transition transition) {
+                    mWindow.setLayout(
+                            mWindow.getAttributes().width, ViewGroup.LayoutParams.WRAP_CONTENT);
+                }
+
+                @Override
+                public void onTransitionResume(Transition transition) {
+                }
+            });
+            TransitionManager.beginDelayedTransition(mDialogView, transition);
         }
-        updateRowsH();
+        updateRowsH(activeRow);
+        rescheduleTimeoutH();
+    }
+
+    private void updateExpandButtonH() {
+        if (D.BUG) Log.d(TAG, "updateExpandButtonH");
+        mExpandButton.setClickable(!mExpandButtonAnimationRunning);
+        if (!(mExpandButtonAnimationRunning && isAttached())) {
+            final int res = mExpanded ? R.drawable.ic_volume_collapse_animation
+                    : R.drawable.ic_volume_expand_animation;
+            if (hasTouchFeature()) {
+                mExpandButton.setImageResource(res);
+            } else {
+                // if there is no touch feature, show the volume ringer instead
+                mExpandButton.setImageResource(R.drawable.ic_volume_ringer);
+                mExpandButton.setBackgroundResource(0);  // remove gray background emphasis
+            }
+            mExpandButton.setContentDescription(mContext.getString(mExpanded ?
+                    R.string.accessibility_volume_collapse : R.string.accessibility_volume_expand));
+        }
         if (mExpandButtonAnimationRunning) {
             final Drawable d = mExpandButton.getDrawable();
             if (d instanceof AnimatedVectorDrawable) {
@@ -581,61 +620,41 @@
                 }, mExpandButtonAnimationDuration);
             }
         }
-        rescheduleTimeoutH();
     }
 
-    private void updateExpandButtonH() {
-        if (D.BUG) Log.d(TAG, "updateExpandButtonH");
-        mExpandButton.setClickable(!mExpandButtonAnimationRunning);
-        if (mExpandButtonAnimationRunning && isAttached()) return;
-        final int res = mExpanded ? R.drawable.ic_volume_collapse_animation
-                : R.drawable.ic_volume_expand_animation;
-        if (hasTouchFeature()) {
-            mExpandButton.setImageResource(res);
-        } else {
-            // if there is no touch feature, show the volume ringer instead
-            mExpandButton.setImageResource(R.drawable.ic_volume_ringer);
-            mExpandButton.setBackgroundResource(0);  // remove gray background emphasis
-        }
-        mExpandButton.setContentDescription(mContext.getString(mExpanded ?
-                R.string.accessibility_volume_collapse : R.string.accessibility_volume_expand));
-    }
-
-    private boolean isVisibleH(VolumeRow row, boolean isActive) {
+    private boolean shouldBeVisibleH(VolumeRow row, boolean isActive) {
         return mExpanded && row.view.getVisibility() == View.VISIBLE
                 || (mExpanded && (row.important || isActive))
                 || !mExpanded && isActive;
     }
 
-    private void updateRowsH() {
+    private void updateRowsH(final VolumeRow activeRow) {
         if (D.BUG) Log.d(TAG, "updateRowsH");
-        final VolumeRow activeRow = getActiveRow();
-        updateFooterH();
-        updateExpandButtonH();
         if (!mShowing) {
             trimObsoleteH();
         }
+        Util.setVisOrGone(mDialogRowsView.findViewById(R.id.spacer), mExpanded);
         // apply changes to all rows
-        for (VolumeRow row : mRows) {
+        for (final VolumeRow row : mRows) {
             final boolean isActive = row == activeRow;
-            final boolean visible = isVisibleH(row, isActive);
-            Util.setVisOrGone(row.view, visible);
-            Util.setVisOrGone(row.space, visible && mExpanded);
-            updateVolumeRowHeaderVisibleH(row);
-            row.header.setAlpha(mExpanded && isActive ? 1 : 0.5f);
-            updateVolumeRowSliderTintH(row, isActive);
+            final boolean shouldBeVisible = shouldBeVisibleH(row, isActive);
+            Util.setVisOrGone(row.view, shouldBeVisible);
+            if (row.view.isShown()) {
+                updateVolumeRowHeaderVisibleH(row);
+                updateVolumeRowSliderTintH(row, isActive);
+            }
         }
+
     }
 
     private void trimObsoleteH() {
         if (D.BUG) Log.d(TAG, "trimObsoleteH");
-        for (int i = mRows.size() -1; i >= 0; i--) {
+        for (int i = mRows.size() - 1; i >= 0; i--) {
             final VolumeRow row = mRows.get(i);
             if (row.ss == null || !row.ss.dynamic) continue;
             if (!mDynamic.get(row.stream)) {
                 mRows.remove(i);
-                mDialogContentView.removeView(row.view);
-                mDialogContentView.removeView(row.space);
+                mDialogRowsView.removeView(row.view);
             }
         }
     }
@@ -662,7 +681,7 @@
 
         if (mActiveStream != state.activeStream) {
             mActiveStream = state.activeStream;
-            updateRowsH();
+            updateRowsH(getActiveRow());
             rescheduleTimeoutH();
         }
         for (VolumeRow row : mRows) {
@@ -723,9 +742,6 @@
                 && mState.ringerModeInternal == AudioManager.RINGER_MODE_SILENT;
         final boolean isZenAlarms = mState.zenMode == Global.ZEN_MODE_ALARMS;
         final boolean isZenNone = mState.zenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
-        final boolean isZenPriority = mState.zenMode == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
-        final boolean isRingZenNone = (isRingStream || isSystemStream) && isZenNone;
-        final boolean isRingLimited = isRingStream && isZenPriority;
         final boolean zenMuted = isZenAlarms ? (isRingStream || isSystemStream)
                 : isZenNone ? (isRingStream || isSystemStream || isAlarmStream || isMusicStream)
                 : false;
@@ -740,21 +756,7 @@
         updateVolumeRowHeaderVisibleH(row);
 
         // update header text
-        String text = ss.name;
-        if (mShowHeaders) {
-            if (isRingZenNone) {
-                text = mContext.getString(R.string.volume_stream_muted_dnd, ss.name);
-            } else if (isRingVibrate && isRingLimited) {
-                text = mContext.getString(R.string.volume_stream_vibrate_dnd, ss.name);
-            } else if (isRingVibrate) {
-                text = mContext.getString(R.string.volume_stream_vibrate, ss.name);
-            } else if (ss.muted || mAutomute && ss.level == 0) {
-                text = mContext.getString(R.string.volume_stream_muted, ss.name);
-            } else if (isRingLimited) {
-                text = mContext.getString(R.string.volume_stream_limited_dnd, ss.name);
-            }
-        }
-        Util.setText(row.header, text);
+        Util.setText(row.header, ss.name);
 
         // update icon
         final boolean iconEnabled = (mAutomute || ss.muteSupported) && !zenMuted;
@@ -823,7 +825,7 @@
 
     private void updateVolumeRowHeaderVisibleH(VolumeRow row) {
         final boolean dynamic = row.ss != null && row.ss.dynamic;
-        final boolean showHeaders = mShowHeaders || mExpanded && dynamic;
+        final boolean showHeaders = mExpanded && (mShowHeaders || dynamic);
         if (row.cachedShowHeaders != showHeaders) {
             row.cachedShowHeaders = showHeaders;
             Util.setVisOrGone(row.header, showHeaders);
@@ -979,6 +981,7 @@
                 mDialog.dismiss();
                 mZenFooter.cleanup();
                 initDialog();
+                mDensity = density;
             }
             updateWindowWidthH();
             mSpTexts.update();
@@ -1028,7 +1031,7 @@
             if (mExpandButtonAnimationRunning) return;
             final boolean newExpand = !mExpanded;
             Events.writeEvent(mContext, Events.EVENT_EXPAND, newExpand);
-            setExpandedH(newExpand);
+            updateExpandedH(newExpand, false /* dismissing */);
         }
     };
 
@@ -1225,7 +1228,6 @@
 
     private static class VolumeRow {
         private View view;
-        private View space;
         private TextView header;
         private ImageButton icon;
         private SeekBar slider;
diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogComponent.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogComponent.java
index 3d33809..44a435e 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogComponent.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogComponent.java
@@ -122,7 +122,6 @@
     private void applyConfiguration() {
         mDialog.setStreamImportant(AudioManager.STREAM_ALARM, true);
         mDialog.setStreamImportant(AudioManager.STREAM_SYSTEM, false);
-        mDialog.setShowHeaders(false);
         mDialog.setAutomute(true);
         mDialog.setSilentMode(false);
         mController.setVolumePolicy(mVolumePolicy);
diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumePrefs.java b/packages/SystemUI/src/com/android/systemui/volume/VolumePrefs.java
index bbb70ed..04339eb 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/VolumePrefs.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/VolumePrefs.java
@@ -43,7 +43,7 @@
     public static final String PREF_ADJUST_ALARMS = "pref_adjust_alarms";
     public static final String PREF_ADJUST_NOTIFICATION = "pref_adjust_notification";
 
-    public static final boolean DEFAULT_SHOW_HEADERS = false;
+    public static final boolean DEFAULT_SHOW_HEADERS = true;
     public static final boolean DEFAULT_ENABLE_AUTOMUTE = true;
     public static final boolean DEFAULT_ENABLE_SILENT_MODE = true;
 
diff --git a/packages/SystemUI/src/com/android/systemui/volume/ZenFooter.java b/packages/SystemUI/src/com/android/systemui/volume/ZenFooter.java
index f01e95f..995ecae 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/ZenFooter.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/ZenFooter.java
@@ -124,12 +124,8 @@
                 : null;
         Util.setText(mSummaryLine1, line1);
 
-        final boolean isForever = mConfig != null && mConfig.manualRule != null
-                && mConfig.manualRule.conditionId == null;
-        final CharSequence line2 =
-                isForever ? mContext.getString(com.android.internal.R.string.zen_mode_forever_dnd)
-                : ZenModeConfig.getConditionSummary(mContext, mConfig, mController.getCurrentUser(),
-                        true /*shortVersion*/);
+        final CharSequence line2 = ZenModeConfig.getConditionSummary(mContext, mConfig,
+                                mController.getCurrentUser(), true /*shortVersion*/);
         Util.setText(mSummaryLine2, line2);
     }
 
diff --git a/packages/SystemUI/tests/Android.mk b/packages/SystemUI/tests/Android.mk
index c9c5805..d122ccc 100644
--- a/packages/SystemUI/tests/Android.mk
+++ b/packages/SystemUI/tests/Android.mk
@@ -58,6 +58,42 @@
 # UI it doesn't own. This is necessary to allow screenshots to be taken
 LOCAL_CERTIFICATE := platform
 
+# Provide jack a list of classes to exclude from code coverage.
+# This is needed because the SystemUITests compile SystemUI source directly, rather than using
+# LOCAL_INSTRUMENTATION_FOR := SystemUI.
+#
+# We want to exclude the test classes from code coverage measurements, but they share the same
+# package as the rest of SystemUI so they can't be easily filtered by package name.
+#
+# Generate a comma separated list of patterns based on the test source files under src/
+# SystemUI classes are in ../src/ so they won't be excluded.
+# Example:
+#   Input files: src/com/android/systemui/Test.java src/com/android/systemui/AnotherTest.java
+#   Generated exclude list: com.android.systemui.Test*,com.android.systemui.AnotherTest*
+
+# Filter all src files under src/ to just java files
+local_java_files := $(filter %.java,$(call all-java-files-under, src))
+# Transform java file names into full class names.
+# This only works if the class name matches the file name and the directory structure
+# matches the package.
+local_classes := $(subst /,.,$(patsubst src/%.java,%,$(local_java_files)))
+local_comma := ,
+local_empty :=
+local_space := $(local_empty) $(local_empty)
+# Convert class name list to jacoco exclude list
+# This appends a * to all classes and replace the space separators with commas.
+jacoco_exclude := $(subst $(space),$(comma),$(patsubst %,%*,$(local_classes)))
+
+LOCAL_JACK_COVERAGE_INCLUDE_FILTER := com.android.systemui.*
+LOCAL_JACK_COVERAGE_EXCLUDE_FILTER := com.android.systemui.tests.*,$(jacoco_exclude)
+
 include frameworks/base/packages/SettingsLib/common.mk
 
 include $(BUILD_PACKAGE)
+
+# Reset variables
+local_java_files :=
+local_classes :=
+local_comma :=
+local_space :=
+jacoco_exclude :=
diff --git a/packages/SystemUI/tests/src/com/android/systemui/phone/DozeParametersTests.java b/packages/SystemUI/tests/src/com/android/systemui/phone/DozeParametersTests.java
new file mode 100644
index 0000000..07334f3
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/phone/DozeParametersTests.java
@@ -0,0 +1,166 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.systemui.phone;
+
+import com.android.systemui.statusbar.phone.DozeParameters.IntInOutMatcher;
+
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+@SmallTest
+public class DozeParametersTests extends AndroidTestCase {
+
+    public void test_inOutMatcher_defaultIn() {
+        IntInOutMatcher intInOutMatcher = new IntInOutMatcher("*");
+
+        assertTrue(intInOutMatcher.isIn(1));
+        assertTrue(intInOutMatcher.isIn(-1));
+        assertTrue(intInOutMatcher.isIn(0));
+    }
+
+    public void test_inOutMatcher_defaultOut() {
+        IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!*");
+
+        assertFalse(intInOutMatcher.isIn(1));
+        assertFalse(intInOutMatcher.isIn(-1));
+        assertFalse(intInOutMatcher.isIn(0));
+    }
+
+    public void test_inOutMatcher_someIn() {
+        IntInOutMatcher intInOutMatcher = new IntInOutMatcher("1,2,3,!*");
+
+        assertTrue(intInOutMatcher.isIn(1));
+        assertTrue(intInOutMatcher.isIn(2));
+        assertTrue(intInOutMatcher.isIn(3));
+
+        assertFalse(intInOutMatcher.isIn(0));
+        assertFalse(intInOutMatcher.isIn(4));
+    }
+
+    public void test_inOutMatcher_someOut() {
+        IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!1,!2,!3,*");
+
+        assertFalse(intInOutMatcher.isIn(1));
+        assertFalse(intInOutMatcher.isIn(2));
+        assertFalse(intInOutMatcher.isIn(3));
+
+        assertTrue(intInOutMatcher.isIn(0));
+        assertTrue(intInOutMatcher.isIn(4));
+    }
+
+    public void test_inOutMatcher_mixed() {
+        IntInOutMatcher intInOutMatcher = new IntInOutMatcher("!1,2,!3,*");
+
+        assertFalse(intInOutMatcher.isIn(1));
+        assertTrue(intInOutMatcher.isIn(2));
+        assertFalse(intInOutMatcher.isIn(3));
+
+        assertTrue(intInOutMatcher.isIn(0));
+        assertTrue(intInOutMatcher.isIn(4));
+    }
+
+    public void test_inOutMatcher_failEmpty() {
+        try {
+            new IntInOutMatcher("");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failNull() {
+        try {
+            new IntInOutMatcher(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failEmptyClause() {
+        try {
+            new IntInOutMatcher("!1,*,");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failDuplicate() {
+        try {
+            new IntInOutMatcher("!1,*,!1");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failDuplicateDefault() {
+        try {
+            new IntInOutMatcher("!1,*,*");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failMalformedNot() {
+        try {
+            new IntInOutMatcher("!,*");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failText() {
+        try {
+            new IntInOutMatcher("!abc,*");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failContradiction() {
+        try {
+            new IntInOutMatcher("1,!1,*");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failContradictionDefault() {
+        try {
+            new IntInOutMatcher("1,*,!*");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+    public void test_inOutMatcher_failMissingDefault() {
+        try {
+            new IntInOutMatcher("1");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/external/TileLifecycleManagerTests.java b/packages/SystemUI/tests/src/com/android/systemui/qs/external/TileLifecycleManagerTests.java
index c93377a..7703c58 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/qs/external/TileLifecycleManagerTests.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/qs/external/TileLifecycleManagerTests.java
@@ -58,7 +58,7 @@
         mHandler = new Handler(mThread.getLooper());
         ComponentName component = new ComponentName(mContext, FakeTileService.class);
         mStateManager = new TileLifecycleManager(mHandler, getContext(),
-                Mockito.mock(IQSService.class), new Tile(component),
+                Mockito.mock(IQSService.class), new Tile(),
                 new Intent().setComponent(component),
                 new UserHandle(UserHandle.myUserId()));
         mCallbacks.clear();
diff --git a/packages/WallpaperBackup/AndroidManifest.xml b/packages/WallpaperBackup/AndroidManifest.xml
index b8cea20..c548101 100644
--- a/packages/WallpaperBackup/AndroidManifest.xml
+++ b/packages/WallpaperBackup/AndroidManifest.xml
@@ -24,6 +24,7 @@
                  android:process="system"
                  android:killAfterRestore="false"
                  android:allowBackup="true"
+                 android:backupInForeground="true"
                  android:backupAgent=".WallpaperBackupAgent"
                  android:fullBackupOnly="true" >
     </application>
diff --git a/packages/WallpaperBackup/src/com/android/wallpaperbackup/WallpaperBackupAgent.java b/packages/WallpaperBackup/src/com/android/wallpaperbackup/WallpaperBackupAgent.java
index 47d1493..ff934ef 100644
--- a/packages/WallpaperBackup/src/com/android/wallpaperbackup/WallpaperBackupAgent.java
+++ b/packages/WallpaperBackup/src/com/android/wallpaperbackup/WallpaperBackupAgent.java
@@ -16,20 +16,31 @@
 
 package com.android.wallpaperbackup;
 
+import static android.app.WallpaperManager.FLAG_LOCK;
+import static android.app.WallpaperManager.FLAG_SYSTEM;
+
+import android.app.AppGlobals;
 import android.app.WallpaperManager;
 import android.app.backup.BackupAgent;
 import android.app.backup.BackupDataInput;
 import android.app.backup.BackupDataOutput;
 import android.app.backup.FullBackupDataOutput;
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.SharedPreferences;
+import android.content.pm.IPackageManager;
+import android.content.pm.PackageInfo;
 import android.graphics.Rect;
 import android.os.Environment;
+import android.os.FileUtils;
 import android.os.ParcelFileDescriptor;
+import android.os.RemoteException;
 import android.os.UserHandle;
-import android.system.Os;
 import android.util.Slog;
 import android.util.Xml;
 
+import libcore.io.IoUtils;
+
 import org.xmlpull.v1.XmlPullParser;
 
 import java.io.File;
@@ -47,15 +58,28 @@
 
     // Target filenames within the system's wallpaper directory
     static final String WALLPAPER = "wallpaper_orig";
+    static final String WALLPAPER_LOCK = "wallpaper_lock_orig";
     static final String WALLPAPER_INFO = "wallpaper_info.xml";
 
     // Names of our local-data stage files/links
     static final String IMAGE_STAGE = "wallpaper-stage";
+    static final String LOCK_IMAGE_STAGE = "wallpaper-lock-stage";
     static final String INFO_STAGE = "wallpaper-info-stage";
     static final String EMPTY_SENTINEL = "empty";
+    static final String QUOTA_SENTINEL = "quota";
 
-    private File mWallpaperInfo;    // wallpaper metadata file
-    private File mWallpaperFile;    // primary wallpaper image file
+    // Not-for-backup bookkeeping
+    static final String PREFS_NAME = "wbprefs.xml";
+    static final String SYSTEM_GENERATION = "system_gen";
+    static final String LOCK_GENERATION = "lock_gen";
+
+    private File mWallpaperInfo;        // wallpaper metadata file
+    private File mWallpaperFile;        // primary wallpaper image file
+    private File mLockWallpaperFile;    // lock wallpaper image file
+
+    // If this file exists, it means we exceeded our quota last time
+    private File mQuotaFile;
+    private boolean mQuotaExceeded;
 
     private WallpaperManager mWm;
 
@@ -68,7 +92,14 @@
         File wallpaperDir = Environment.getUserSystemDirectory(UserHandle.USER_SYSTEM);
         mWallpaperInfo = new File(wallpaperDir, WALLPAPER_INFO);
         mWallpaperFile = new File(wallpaperDir, WALLPAPER);
+        mLockWallpaperFile = new File(wallpaperDir, WALLPAPER_LOCK);
         mWm = (WallpaperManager) getSystemService(Context.WALLPAPER_SERVICE);
+
+        mQuotaFile = new File(getFilesDir(), QUOTA_SENTINEL);
+        mQuotaExceeded = mQuotaFile.exists();
+        if (DEBUG) {
+            Slog.v(TAG, "quota file " + mQuotaFile.getPath() + " exists=" + mQuotaExceeded);
+        }
     }
 
     @Override
@@ -77,6 +108,7 @@
         final File filesDir = getFilesDir();
         final File infoStage = new File(filesDir, INFO_STAGE);
         final File imageStage = new File (filesDir, IMAGE_STAGE);
+        final File lockImageStage = new File (filesDir, LOCK_IMAGE_STAGE);
         final File empty = new File (filesDir, EMPTY_SENTINEL);
 
         try {
@@ -87,33 +119,83 @@
             touch.close();
             fullBackupFile(empty, data);
 
-            // only back up the wallpaper if we've been told it's allowed
-            if (mWm.isWallpaperBackupEligible()) {
-                if (DEBUG) {
-                    Slog.v(TAG, "Wallpaper is backup-eligible; linking & writing");
+            SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+            final int lastSysGeneration = prefs.getInt(SYSTEM_GENERATION, -1);
+            final int lastLockGeneration = prefs.getInt(LOCK_GENERATION, -1);
+
+            final int sysGeneration =
+                    mWm.getWallpaperIdForUser(FLAG_SYSTEM, UserHandle.USER_SYSTEM);
+            final int lockGeneration =
+                    mWm.getWallpaperIdForUser(FLAG_LOCK, UserHandle.USER_SYSTEM);
+            final boolean sysChanged = (sysGeneration != lastSysGeneration);
+            final boolean lockChanged = (lockGeneration != lastLockGeneration);
+
+            final boolean sysEligible = mWm.isWallpaperBackupEligible(FLAG_SYSTEM);
+            final boolean lockEligible = mWm.isWallpaperBackupEligible(FLAG_LOCK);
+
+                // There might be a latent lock wallpaper file present but unused: don't
+                // include it in the backup if that's the case.
+                ParcelFileDescriptor lockFd = mWm.getWallpaperFile(FLAG_LOCK, UserHandle.USER_SYSTEM);
+                final boolean hasLockWallpaper = (lockFd != null);
+                IoUtils.closeQuietly(lockFd);
+
+            if (DEBUG) {
+                Slog.v(TAG, "sysGen=" + sysGeneration + " : sysChanged=" + sysChanged);
+                Slog.v(TAG, "lockGen=" + lockGeneration + " : lockChanged=" + lockChanged);
+                Slog.v(TAG, "sysEligble=" + sysEligible);
+                Slog.v(TAG, "lockEligible=" + lockEligible);
+            }
+
+            // only back up the wallpapers if we've been told they're eligible
+            if (mWallpaperInfo.exists()) {
+                if (sysChanged || lockChanged || !infoStage.exists()) {
+                    if (DEBUG) Slog.v(TAG, "New wallpaper configuration; copying");
+                    FileUtils.copyFileOrThrow(mWallpaperInfo, infoStage);
                 }
-
-                // In case of prior muddled state
-                infoStage.delete();
-                imageStage.delete();
-
-                Os.link(mWallpaperInfo.getCanonicalPath(), infoStage.getCanonicalPath());
+                if (DEBUG) Slog.v(TAG, "Storing wallpaper metadata");
                 fullBackupFile(infoStage, data);
-                Os.link(mWallpaperFile.getCanonicalPath(), imageStage.getCanonicalPath());
-                fullBackupFile(imageStage, data);
-            } else {
-                if (DEBUG) {
-                    Slog.v(TAG, "Wallpaper not backup-eligible; writing no data");
+            }
+            if (sysEligible && mWallpaperFile.exists()) {
+                if (sysChanged || !imageStage.exists()) {
+                    if (DEBUG) Slog.v(TAG, "New system wallpaper; copying");
+                    FileUtils.copyFileOrThrow(mWallpaperFile, imageStage);
                 }
+                if (DEBUG) Slog.v(TAG, "Storing system wallpaper image");
+                fullBackupFile(imageStage, data);
+                prefs.edit().putInt(SYSTEM_GENERATION, sysGeneration).apply();
+            }
+
+            // Don't try to store the lock image if we overran our quota last time
+            if (lockEligible && hasLockWallpaper && mLockWallpaperFile.exists() && !mQuotaExceeded) {
+                if (lockChanged || !lockImageStage.exists()) {
+                    if (DEBUG) Slog.v(TAG, "New lock wallpaper; copying");
+                    FileUtils.copyFileOrThrow(mLockWallpaperFile, lockImageStage);
+                }
+                if (DEBUG) Slog.v(TAG, "Storing lock wallpaper image");
+                fullBackupFile(lockImageStage, data);
+                prefs.edit().putInt(LOCK_GENERATION, lockGeneration).apply();
             }
         } catch (Exception e) {
-            Slog.e(TAG, "Unable to back up wallpaper: " + e.getMessage());
+            Slog.e(TAG, "Unable to back up wallpaper", e);
         } finally {
-            if (DEBUG) {
-                Slog.v(TAG, "Removing backup stage links");
-            }
-            infoStage.delete();
-            imageStage.delete();
+            // Even if this time we had to back off on attempting to store the lock image
+            // due to exceeding the data quota, try again next time.  This will alternate
+            // between "try both" and "only store the primary image" until either there
+            // is no lock image to store, or the quota is raised, or both fit under the
+            // quota.
+            mQuotaFile.delete();
+        }
+    }
+
+    @Override
+    public void onQuotaExceeded(long backupDataBytes, long quotaBytes) {
+        if (DEBUG) {
+            Slog.i(TAG, "Quota exceeded (" + backupDataBytes + " vs " + quotaBytes + ')');
+        }
+        try (FileOutputStream f = new FileOutputStream(mQuotaFile)) {
+            f.write(0);
+        } catch (Exception e) {
+            Slog.w(TAG, "Unable to record quota-exceeded: " + e.getMessage());
         }
     }
 
@@ -124,42 +206,75 @@
         if (DEBUG) {
             Slog.v(TAG, "onRestoreFinished()");
         }
-        final File infoStage = new File(getFilesDir(), INFO_STAGE);
-        final File imageStage = new File (getFilesDir(), IMAGE_STAGE);
+        final File filesDir = getFilesDir();
+        final File infoStage = new File(filesDir, INFO_STAGE);
+        final File imageStage = new File (filesDir, IMAGE_STAGE);
+        final File lockImageStage = new File (filesDir, LOCK_IMAGE_STAGE);
+
+        // If we restored separate lock imagery, the system wallpaper should be
+        // applied as system-only; but if there's no separate lock image, make
+        // sure to apply the restored system wallpaper as both.
+        final int sysWhich = FLAG_SYSTEM | (lockImageStage.exists() ? 0 : FLAG_LOCK);
 
         try {
-            // It is valid for the imagery to be absent; it means that we were not permitted
-            // to back up the original image on the source device.
-            if (imageStage.exists()) {
-                if (DEBUG) {
-                    Slog.v(TAG, "Got restored wallpaper; applying");
-                }
+            // First off, revert to the factory state
+            mWm.clear(FLAG_SYSTEM | FLAG_LOCK);
 
-                // Parse the restored info file to find the crop hint.  Note that this currently
-                // relies on a priori knowledge of the wallpaper info file schema.
-                Rect cropHint = parseCropHint(infoStage);
-                if (cropHint != null) {
-                    if (DEBUG) {
-                        Slog.v(TAG, "Restored crop hint " + cropHint + "; now writing data");
-                    }
-                    WallpaperManager wm = getSystemService(WallpaperManager.class);
-                    try (FileInputStream in = new FileInputStream(imageStage)) {
-                        wm.setStream(in, cropHint, true, WallpaperManager.FLAG_SYSTEM);
-                    } finally {} // auto-closes 'in'
+            // It is valid for the imagery to be absent; it means that we were not permitted
+            // to back up the original image on the source device, or there was no user-supplied
+            // wallpaper image present.
+            restoreFromStage(imageStage, infoStage, "wp", sysWhich);
+            restoreFromStage(lockImageStage, infoStage, "kwp", FLAG_LOCK);
+
+            // And reset to the wallpaper service we should be using
+            ComponentName wpService = parseWallpaperComponent(infoStage, "wp");
+            if (servicePackageExists(wpService)) {
+                if (DEBUG) {
+                    Slog.i(TAG, "Using wallpaper service " + wpService);
+                }
+                mWm.setWallpaperComponent(wpService, UserHandle.USER_SYSTEM);
+            } else {
+                if (DEBUG) {
+                    Slog.v(TAG, "Can't use wallpaper service " + wpService);
                 }
             }
         } catch (Exception e) {
             Slog.e(TAG, "Unable to restore wallpaper: " + e.getMessage());
         } finally {
             if (DEBUG) {
-                Slog.v(TAG, "Removing restore stage files");
+                Slog.v(TAG, "Restore finished; clearing backup bookkeeping");
             }
             infoStage.delete();
             imageStage.delete();
+            lockImageStage.delete();
+
+            SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
+            prefs.edit()
+                    .putInt(SYSTEM_GENERATION, -1)
+                    .putInt(LOCK_GENERATION, -1)
+                    .commit();
         }
     }
 
-    private Rect parseCropHint(File wallpaperInfo) {
+    private void restoreFromStage(File stage, File info, String hintTag, int which)
+            throws IOException {
+        if (stage.exists()) {
+            // Parse the restored info file to find the crop hint.  Note that this currently
+            // relies on a priori knowledge of the wallpaper info file schema.
+            Rect cropHint = parseCropHint(info, hintTag);
+            if (cropHint != null) {
+                Slog.i(TAG, "Got restored wallpaper; applying which=" + which);
+                if (DEBUG) {
+                    Slog.v(TAG, "Restored crop hint " + cropHint);
+                }
+                try (FileInputStream in = new FileInputStream(stage)) {
+                    mWm.setStream(in, cropHint.isEmpty() ? null : cropHint, true, which);
+                } finally {} // auto-closes 'in'
+            }
+        }
+    }
+
+    private Rect parseCropHint(File wallpaperInfo, String sectionTag) {
         Rect cropHint = new Rect();
         try (FileInputStream stream = new FileInputStream(wallpaperInfo)) {
             XmlPullParser parser = Xml.newPullParser();
@@ -170,7 +285,7 @@
                 type = parser.next();
                 if (type == XmlPullParser.START_TAG) {
                     String tag = parser.getName();
-                    if ("wp".equals(tag)) {
+                    if (sectionTag.equals(tag)) {
                         cropHint.left = getAttributeInt(parser, "cropLeft", 0);
                         cropHint.top = getAttributeInt(parser, "cropTop", 0);
                         cropHint.right = getAttributeInt(parser, "cropRight", 0);
@@ -180,18 +295,60 @@
             } while (type != XmlPullParser.END_DOCUMENT);
         } catch (Exception e) {
             // Whoops; can't process the info file at all.  Report failure.
-            Slog.w(TAG, "Failed to parse restored metadata: " + e.getMessage());
+            Slog.w(TAG, "Failed to parse restored crop: " + e.getMessage());
             return null;
         }
 
         return cropHint;
     }
 
+    private ComponentName parseWallpaperComponent(File wallpaperInfo, String sectionTag) {
+        ComponentName name = null;
+        try (FileInputStream stream = new FileInputStream(wallpaperInfo)) {
+            final XmlPullParser parser = Xml.newPullParser();
+            parser.setInput(stream, StandardCharsets.UTF_8.name());
+
+            int type;
+            do {
+                type = parser.next();
+                if (type == XmlPullParser.START_TAG) {
+                    String tag = parser.getName();
+                    if (sectionTag.equals(tag)) {
+                        final String parsedName = parser.getAttributeValue(null, "component");
+                        name = (parsedName != null)
+                                ? ComponentName.unflattenFromString(parsedName)
+                                : null;
+                        break;
+                    }
+                }
+            } while (type != XmlPullParser.END_DOCUMENT);
+        } catch (Exception e) {
+            // Whoops; can't process the info file at all.  Report failure.
+            Slog.w(TAG, "Failed to parse restored component: " + e.getMessage());
+            return null;
+        }
+        return name;
+    }
+
     private int getAttributeInt(XmlPullParser parser, String name, int defValue) {
         final String value = parser.getAttributeValue(null, name);
         return (value == null) ? defValue : Integer.parseInt(value);
     }
 
+    private boolean servicePackageExists(ComponentName comp) {
+        try {
+            if (comp != null) {
+                final IPackageManager pm = AppGlobals.getPackageManager();
+                final PackageInfo info = pm.getPackageInfo(comp.getPackageName(),
+                        0, UserHandle.USER_SYSTEM);
+                return (info != null);
+            }
+        } catch (RemoteException e) {
+            Slog.e(TAG, "Unable to contact package manager");
+        }
+        return false;
+    }
+
     //
     // Key/value API: abstract, therefore required; but not used
     //
diff --git a/proto/src/metrics_constants.proto b/proto/src/metrics_constants.proto
index a1487e3..8a0dfe5 100644
--- a/proto/src/metrics_constants.proto
+++ b/proto/src/metrics_constants.proto
@@ -33,73 +33,61 @@
     // OPEN: Settings > Accessibility
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCESSIBILITY = 2;
 
     // OPEN: Settings > Accessibility > Captions
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCESSIBILITY_CAPTION_PROPERTIES = 3;
 
     // OPEN: Settings > Accessibility > [Service]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCESSIBILITY_SERVICE = 4;
 
     // OPEN: Settings > Accessibility > Color correction
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCESSIBILITY_TOGGLE_DALTONIZER = 5;
 
     // OPEN: Settings > Accessibility > Accessibility shortcut
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCESSIBILITY_TOGGLE_GLOBAL_GESTURE = 6;
 
     // OPEN: Settings > Accessibility > Magnification gestures
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCESSIBILITY_TOGGLE_SCREEN_MAGNIFICATION = 7;
 
     // OPEN: Settings > Accounts
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCOUNT = 8;
 
     // OPEN: Settings > Accounts > [Single Account Sync Settings]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCOUNTS_ACCOUNT_SYNC = 9;
 
     // OPEN: Settings > Accounts > Add an account
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCOUNTS_CHOOSE_ACCOUNT_ACTIVITY = 10;
 
     // OPEN: Settings > Accounts > [List of accounts when more than one]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACCOUNTS_MANAGE_ACCOUNTS = 11;
 
     // OPEN: Settings > Cellular network settings > APNs
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APN = 12;
 
     // OPEN: Settings > More > Cellular network settings > APNs > [Edit APN]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APN_EDITOR = 13;
 
     // OBSOLETE
@@ -114,7 +102,6 @@
     // OPEN: Settings > Apps > Configure apps > App links > [App]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_APP_LAUNCH = 17;
 
     // OBSOLETE
@@ -123,19 +110,16 @@
     // OPEN: Settings > Internal storage > Apps storage > [App]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_APP_STORAGE = 19;
 
     // OPEN: Settings > Apps > [App info]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_INSTALLED_APP_DETAILS = 20;
 
     // OPEN: Settings > Memory > App usage > [App Memory usage]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_PROCESS_STATS_DETAIL = 21;
 
     // OBSOLETE
@@ -144,19 +128,16 @@
     // OPEN: Settings > Memory > App usage
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_PROCESS_STATS_UI = 23;
 
     // OPEN: Settings > Bluetooth
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     BLUETOOTH = 24;
 
     // OPEN: Choose Bluetooth device (ex: when sharing)
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     BLUETOOTH_DEVICE_PICKER = 25;
 
     // OBSOLETE
@@ -165,55 +146,46 @@
     // OPEN: Settings > Security > Choose screen lock
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     CHOOSE_LOCK_GENERIC = 27;
 
     // OPEN: Settings > Security > Choose screen lock > Choose your password
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     CHOOSE_LOCK_PASSWORD = 28;
 
     // OPEN: Settings > Security > Choose screen lock > Choose your pattern
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     CHOOSE_LOCK_PATTERN = 29;
 
     // OPEN: Settings > Security > Choose screen lock > Confirm your password
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     CONFIRM_LOCK_PASSWORD = 30;
 
     // OPEN: Settings > Security > Choose screen lock > Confirm your pattern
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     CONFIRM_LOCK_PATTERN = 31;
 
     // OPEN: Settings > Security > Encrypt phone
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     CRYPT_KEEPER = 32;
 
     // OPEN: Settings > Security > Encrypt phone > Confirm
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     CRYPT_KEEPER_CONFIRM = 33;
 
     // OPEN: Settings > Search results
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DASHBOARD_SEARCH_RESULTS = 34;
 
     // OPEN: Settings (Root page)
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DASHBOARD_SUMMARY = 35;
 
     // OBSOLETE
@@ -222,49 +194,41 @@
     // OPEN: Settings > Data usage
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DATA_USAGE_SUMMARY = 37;
 
     // OPEN: Settings > Date & time
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DATE_TIME = 38;
 
     // OPEN: Settings > Developer options
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DEVELOPMENT = 39;
 
     // OPEN: Settings > About phone
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DEVICEINFO = 40;
 
     // OPEN: Settings > About phone > Status > IMEI information
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DEVICEINFO_IMEI_INFORMATION = 41;
 
     // OPEN: Settings > Internal storage
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DEVICEINFO_STORAGE = 42;
 
     // OPEN: Settings > About phone > Status > SIM status
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DEVICEINFO_SIM_STATUS = 43;
 
     // OPEN: Settings > About phone > Status
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DEVICEINFO_STATUS = 44;
 
     // OBSOLETE
@@ -273,25 +237,21 @@
     // OPEN: Settings > Display
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DISPLAY = 46;
 
     // OPEN: Settings > Display > Daydream
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     DREAM = 47;
 
     // OPEN: Settings > Security > Screen lock > Secure start-up
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ENCRYPTION = 48;
 
     // OPEN: Settings > Security > Nexus Imprint
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     FINGERPRINT = 49;
 
     // OBSOLETE
@@ -300,55 +260,46 @@
     // OPEN: Settings > Battery > History details
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     FUELGAUGE_BATTERY_HISTORY_DETAIL = 51;
 
     // OPEN: Settings > Battery > Battery saver
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     FUELGAUGE_BATTERY_SAVER = 52;
 
     // OPEN: Settings > Battery > [App Use details]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     FUELGAUGE_POWER_USAGE_DETAIL = 53;
 
     // OPEN: Settings > Battery
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     FUELGAUGE_POWER_USAGE_SUMMARY = 54;
 
     // OPEN: Settings > Home
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     HOME = 55;
 
     // OPEN: Settings > Security > SIM card lock settings
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ICC_LOCK = 56;
 
     // OPEN: Settings > Language & input
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     INPUTMETHOD_LANGUAGE = 57;
 
     // OPEN: Settings > Language & input > Physical keyboard
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     INPUTMETHOD_KEYBOARD = 58;
 
     // OPEN: Settings > Language & input > Spell checker
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     INPUTMETHOD_SPELL_CHECKERS = 59;
 
     // OBSOLETE
@@ -357,79 +308,66 @@
     // OPEN: Settings > Language & input > Personal dictionary
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     INPUTMETHOD_USER_DICTIONARY = 61;
 
     // OPEN: Settings > Language & input > Add word
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     INPUTMETHOD_USER_DICTIONARY_ADD_WORD = 62;
 
     // OPEN: Settings > Location
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     LOCATION = 63;
 
     // OPEN: Settings > Location > Location mode
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     LOCATION_MODE = 64;
 
     // OPEN: Settings > Apps
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     MANAGE_APPLICATIONS = 65;
 
     // OPEN: Settings > Backup & reset > Factory data reset
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     MASTER_CLEAR = 66;
 
     // OPEN: Settings > Backup & reset > Factory data reset > Confirm
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     MASTER_CLEAR_CONFIRM = 67;
 
     // OPEN: Settings > Data usage > Network restrictions
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NET_DATA_USAGE_METERED = 68;
 
     // OPEN: Settings > More > Android Beam
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NFC_BEAM = 69;
 
     // OPEN: Settings > Tap & pay
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NFC_PAYMENT = 70;
 
     // OPEN: Settings > Sound & notification
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION = 71;
 
     // OPEN: Settings > Sound & notification > App notifications > [App]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_APP_NOTIFICATION = 72;
 
     // OPEN: Settings > Sound & notification > Other sounds
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_OTHER_SOUND = 73;
 
     // OBSOLETE
@@ -438,13 +376,11 @@
     // OPEN: Settings Widget > Notification log
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_STATION = 75;
 
     // OPEN: Settings > Sound & notification > Do not disturb
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_ZEN_MODE = 76;
 
     // OPEN: OBSOLETE
@@ -453,25 +389,21 @@
     // OPEN: Print job notification > Print job settings
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     PRINT_JOB_SETTINGS = 78;
 
     // OPEN: Settings > Printing > [Print Service]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     PRINT_SERVICE_SETTINGS = 79;
 
     // OPEN: Settings > Printing
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     PRINT_SETTINGS = 80;
 
     // OPEN: Settings > Backup & reset
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     PRIVACY = 81;
 
     //OBSOLETE
@@ -480,37 +412,31 @@
     // OPEN: Settings > Backup & reset > Network settings reset
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     RESET_NETWORK = 83;
 
     // OPEN: Settings > Backup & reset > Network settings reset > Confirm
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     RESET_NETWORK_CONFIRM = 84;
 
     // OPEN: Settings > Developer Options > Running Services
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     RUNNING_SERVICE_DETAILS = 85;
 
     // OPEN: Settings > Security > Screen pinning
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     SCREEN_PINNING = 86;
 
     // OPEN: Settings > Security
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     SECURITY = 87;
 
     // OPEN: Settings > SIM cards
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     SIM = 88;
 
     // OBSOLETE
@@ -519,55 +445,46 @@
     // OPEN: Settings > More > Tethering & portable hotspot
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TETHER = 90;
 
     // OPEN: Settings > Security > Trust agents
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TRUST_AGENT = 91;
 
     // OPEN: Settings > Security > Trusted credentials
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TRUSTED_CREDENTIALS = 92;
 
     // OPEN: Settings > Language & input > TTS output > [Engine] > Settings
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TTS_ENGINE_SETTINGS = 93;
 
     // OPEN: Settings > Language & input > Text-to-speech output
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TTS_TEXT_TO_SPEECH = 94;
 
     // OPEN: Settings > Security > Apps with usage access
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     USAGE_ACCESS = 95;
 
     // OPEN: Settings > Users
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     USER = 96;
 
     // OPEN: Settings > Users > [Restricted profile app & content access]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     USERS_APP_RESTRICTIONS = 97;
 
     // OPEN: Settings > Users > [User settings]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     USER_DETAILS = 98;
 
     // OBSOLETE
@@ -576,43 +493,36 @@
     // OPEN: Settings > More > VPN
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     VPN = 100;
 
     // OPEN: Settings > Display > Choose wallpaper from
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     WALLPAPER_TYPE = 101;
 
     // OPEN: Settings > Display > Cast
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     WFD_WIFI_DISPLAY = 102;
 
     // OPEN: Settings > Wi-Fi
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     WIFI = 103;
 
     // OPEN: Settings > Wi-Fi > Advanced Wi-Fi
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     WIFI_ADVANCED = 104;
 
     // OPEN: Settings > More > Wi-Fi Calling
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     WIFI_CALLING = 105;
 
     // OPEN: Settings > Wi-Fi > Saved networks
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     WIFI_SAVED_ACCESS_POINTS = 106;
 
     // OBSOLETE
@@ -624,19 +534,16 @@
     // OPEN: Settings > Wi-Fi > Advanced Wi-Fi > Wi-Fi Direct
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     WIFI_P2P = 109;
 
     // OPEN: Settings > More
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     WIRELESS = 110;
 
     // OPEN: Quick Settings Panel
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_PANEL = 111;
 
     // OPEN: QS Airplane mode tile shown
@@ -644,7 +551,6 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_AIRPLANEMODE = 112;
 
     // OPEN: QS Bluetooth tile shown
@@ -652,21 +558,18 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_BLUETOOTH = 113;
 
     // OPEN: QS Cast tile shown
     // ACTION: QS Cast tile tapped
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_CAST = 114;
 
     // OPEN: QS Cellular tile shown
     // ACTION: QS Cellular tile tapped
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_CELLULAR = 115;
 
     // OPEN: QS Color inversion tile shown
@@ -674,13 +577,11 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_COLORINVERSION = 116;
 
     // OPEN: QS Cellular tile > Cellular detail panel
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_DATAUSAGEDETAIL = 117;
 
     // OPEN: QS Do not disturb tile shown
@@ -688,7 +589,6 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_DND = 118;
 
     // OPEN: QS Flashlight tile shown
@@ -696,7 +596,6 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_FLASHLIGHT = 119;
 
     // OPEN: QS Hotspot tile shown
@@ -704,14 +603,12 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_HOTSPOT = 120;
 
     // OPEN: QS 3P tile shown
     // ACTION: QS 3P tile tapped
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_INTENT = 121;
 
     // OPEN: QS Location tile shown
@@ -719,7 +616,6 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_LOCATION = 122;
 
     // OPEN: QS Rotation tile shown
@@ -727,7 +623,6 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_ROTATIONLOCK = 123;
 
     // OBSOLETE
@@ -736,7 +631,6 @@
     // OPEN: QS User list panel
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_USERDETAIL = 125;
 
     // OPEN: QS WiFi tile shown
@@ -744,13 +638,11 @@
     //  SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.46
     QS_WIFI = 126;
 
     // OPEN: Notification Panel (including lockscreen)
     // CATEGORY: NOTIFICATION
     // OS: 5.1.1
-    // GMS: 7.5.26
     NOTIFICATION_PANEL = 127;
 
     // OPEN: Notification in panel became visible.
@@ -764,7 +656,6 @@
     //   SUBTYPE: Dismiss reason from NotificationManagerService.java
     // CATEGORY: NOTIFICATION
     // OS: 5.1.1
-    // GMS: 7.5.26
     NOTIFICATION_ITEM = 128;
 
     // ACTION: User tapped notification action
@@ -772,19 +663,16 @@
     //   SUBTYPE: Index of action on notification
     // CATEGORY: NOTIFICATION
     // OS: 5.0
-    // GMS: 7.5.26
     NOTIFICATION_ITEM_ACTION = 129;
 
     // OPEN: Settings > Apps > Configure apps > App permissions
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_ADVANCED = 130;
 
     // OPEN: Settings > Location > Scanning
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     LOCATION_SCANNING = 131;
 
     // OBSOLETE
@@ -793,43 +681,36 @@
     // OPEN: Settings > Sound & notification > App notifications
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     MANAGE_APPLICATIONS_NOTIFICATIONS = 133;
 
     // ACTION: Settings > Wi-Fi > Overflow > Add Network
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_WIFI_ADD_NETWORK = 134;
 
     // ACTION: Settings > Wi-Fi > [Long press network] > Connect to network
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_WIFI_CONNECT = 135;
 
     // ACTION: Settings > Wi-Fi > Overflow > Refresh
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_WIFI_FORCE_SCAN = 136;
 
     // ACTION: Settings > Wi-Fi > [Long press network] > Forget network
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_WIFI_FORGET = 137;
 
     // ACTION: Settings > Wi-Fi > Toggle off
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_WIFI_OFF = 138;
 
     // ACTION: Settings > Wi-Fi > Toggle on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_WIFI_ON = 139;
 
     // OBSOLETE
@@ -838,280 +719,236 @@
     // OPEN: Settings > Sound & notification > DND > Priority only allows
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_ZEN_MODE_PRIORITY = 141;
 
     // OPEN: Settings > Sound & notification > DND > Automatic rules
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_ZEN_MODE_AUTOMATION = 142;
 
     // OPEN: Settings > Apps > Configure apps > App links
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     MANAGE_DOMAIN_URLS = 143;
 
     // OPEN: Settings > Sound & notification > DND > [Time based rule]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_ZEN_MODE_SCHEDULE_RULE = 144;
 
     // OPEN: Settings > Sound & notification > DND > [External rule]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_ZEN_MODE_EXTERNAL_RULE = 145;
 
     // OPEN: Settings > Sound & notification > DND > [Event rule]
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_ZEN_MODE_EVENT_RULE = 146;
 
     // ACTION: App notification settings > Block Notifications
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_BAN_APP_NOTES = 147;
 
     // ACTION: Notification shade > Dismiss all button
     // CATEGORY: NOTIFICATION
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_DISMISS_ALL_NOTES = 148;
 
     // OPEN: QS Do Not Disturb detail panel
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_DND_DETAILS = 149;
 
     // OPEN: QS Bluetooth detail panel
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_BLUETOOTH_DETAILS = 150;
 
     // OPEN: QS Cast detail panel
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_CAST_DETAILS = 151;
 
     // OPEN: QS Wi-Fi detail panel
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_WIFI_DETAILS = 152;
 
     // ACTION: QS Wi-Fi detail panel > Wi-Fi toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_WIFI_TOGGLE = 153;
 
     // ACTION: QS Bluetooth detail panel > Bluetooth toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_BLUETOOTH_TOGGLE = 154;
 
     // ACTION: QS Cellular detail panel > Cellular toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_CELLULAR_TOGGLE = 155;
 
     // ACTION: QS User list panel > Select different user
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_SWITCH_USER = 156;
 
     // ACTION: QS Cast detail panel > Select cast device
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_CAST_SELECT = 157;
 
     // ACTION: QS Cast detail panel > Disconnect cast device
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_CAST_DISCONNECT = 158;
 
     // ACTION: Settings > Bluetooth > Toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_BLUETOOTH_TOGGLE = 159;
 
     // ACTION: Settings > Bluetooth > Overflow > Refresh
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_BLUETOOTH_SCAN = 160;
 
     // ACTION: Settings > Bluetooth > Overflow > Rename this device
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_BLUETOOTH_RENAME = 161;
 
     // ACTION: Settings > Bluetooth > Overflow > Show received files
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_BLUETOOTH_FILES = 162;
 
     // ACTION: QS DND details panel > Increase / Decrease exit time
     //   SUBTYPE: true is increase, false is decrease
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_DND_TIME = 163;
 
     // ACTION: QS DND details panel > [Exit condition]
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_DND_CONDITION_SELECT = 164;
 
     // ACTION: QS DND details panel > [DND mode]
     //  SUBTYPE: 1 is priority, 2 is silence, 3 is alarms only
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_DND_ZEN_SELECT = 165;
 
     // ACTION: QS DND detail panel > DND toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     QS_DND_TOGGLE = 166;
 
     // ACTION: DND Settings > Priority only allows > Reminder toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_ALLOW_REMINDERS = 167;
 
     // ACTION: DND Settings > Priority only allows > Event toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_ALLOW_EVENTS = 168;
 
     // ACTION: DND Settings > Priority only allows > Messages
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_ALLOW_MESSAGES = 169;
 
     // ACTION: DND Settings > Priority only allows > Calls
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_ALLOW_CALLS = 170;
 
     // ACTION: DND Settings > Priority only allows > Repeat callers toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_ALLOW_REPEAT_CALLS = 171;
 
     // ACTION: DND Settings > Automatic rules > Add rule
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_ADD_RULE = 172;
 
     // ACTION: DND Settings > Automatic rules > Add rule > OK
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_ADD_RULE_OK = 173;
 
     // ACTION: DND Settings > Automatic rules > [Rule] > Delete rule
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_DELETE_RULE = 174;
 
     // ACTION: DND Settings > Automatic rules > [Rule] > Delete rule > Delete
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_DELETE_RULE_OK = 175;
 
     // ACTION: DND Settings > Automatic rules > [Rule] > Toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ZEN_ENABLE_RULE = 176;
 
     // ACTION: Settings > More > Airplane mode toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_AIRPLANE_TOGGLE = 177;
 
     // ACTION: Settings > Data usage > Cellular data toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_CELL_DATA_TOGGLE = 178;
 
     // OPEN: Settings > Sound & notification > Notification access
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_ACCESS = 179;
 
     // OPEN: Settings > Sound & notification > Do Not Disturb access
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     NOTIFICATION_ZEN_MODE_ACCESS = 180;
 
     // OPEN: Settings > Apps > Configure apps > Default Apps
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_DEFAULT_APPS = 181;
 
     // OPEN: Settings > Internal storage > Apps storage
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_STORAGE_APPS = 182;
 
     // OPEN: Settings > Security > Usage access
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_USAGE_ACCESS_DETAIL = 183;
 
     // OPEN: Settings > Battery > Battery optimization
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_HIGH_POWER_APPS = 184;
 
     // OBSOLETE
@@ -1120,448 +957,377 @@
     // ACTION: Lockscreen > Unlock gesture
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     ACTION_LS_UNLOCK = 186;
 
     // ACTION: Lockscreen > Pull shade open
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     ACTION_LS_SHADE = 187;
 
     // ACTION: Lockscreen > Tap on lock, shows hint
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     ACTION_LS_HINT = 188;
 
     // ACTION: Lockscreen > Camera
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     ACTION_LS_CAMERA = 189;
 
     // ACTION: Lockscreen > Dialer
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     ACTION_LS_DIALER = 190;
 
     // ACTION: Lockscreen > Tap on lock, locks phone
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     ACTION_LS_LOCK = 191;
 
     // ACTION: Lockscreen > Tap on notification, false touch rejection
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     ACTION_LS_NOTE = 192;
 
     // ACTION: Lockscreen > Swipe down to open quick settings
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.8.22
     ACTION_LS_QS = 193;
 
     // ACTION: Swipe down to open quick settings when unlocked
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.8.22
     ACTION_SHADE_QS_PULL = 194;
 
     // ACTION: Notification shade > Tap to open quick settings
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.8.22
     ACTION_SHADE_QS_TAP = 195;
 
     // OPEN: Lockscreen
     //   SUBTYPE: 0 is unsecure, 1 is secured by password / pattern / PIN
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     LOCKSCREEN = 196;
 
     // OPEN: Lockscreen > Screen to enter password / pattern / PIN
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     BOUNCER = 197;
 
     // OPEN: Screen turned on
     //   SUBTYPE: 2 is user action
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.8.22
     SCREEN = 198;
 
     // OPEN: Notification caused sound, vibration, and/or LED blink
     //   SUBTYPE: 1 is buzz, 2 is beep, blink is 4, or'd together
     // CATEGORY: NOTIFICATION
     // OS: 5.1.1
-    // GMS: 7.8.53
     NOTIFICATION_ALERT = 199;
 
     // ACTION: Lockscreen > Emergency Call button
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 5.1.1
-    // GMS: 7.5.26
     ACTION_EMERGENCY_CALL = 200;
 
     // OPEN: Settings > Apps > Configure > Default apps > Assist & voice input
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     APPLICATIONS_MANAGE_ASSIST = 201;
 
     // OPEN: Settings > Memory
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     PROCESS_STATS_SUMMARY = 202;
 
     // ACTION: Settings > Display > When device is rotated
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ROTATION_LOCK = 203;
 
     // ACTION: Long press on notification to view controls
     // CATEGORY: NOTIFICATION
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_NOTE_CONTROLS = 204;
 
     // ACTION: Notificatoin controls > Info button
     // CATEGORY: NOTIFICATION
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_NOTE_INFO = 205;
 
     // ACTION: Notification controls > Settings button
     // CATEGORY: NOTIFICATION
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_APP_NOTE_SETTINGS = 206;
 
     // OPEN: Volume Dialog (with hardware buttons)
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     VOLUME_DIALOG = 207;
 
     // OPEN: Volume dialog > Expanded volume dialog (multiple sliders)
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     VOLUME_DIALOG_DETAILS = 208;
 
     // ACTION: Volume dialog > Adjust volume slider
     //   SUBTYPE: volume level (0-7)
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_VOLUME_SLIDER = 209;
 
     // ACTION: Volume dialog > Select non-active stream
     //   SUBTYPE: stream (defined in AudioSystem.java)
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_VOLUME_STREAM = 210;
 
     // ACTION: Adjust volume with hardware key
     //   SUBTYPE: volume level (0-7)
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_VOLUME_KEY = 211;
 
     // ACTION: Volume dialog > Mute a stream by tapping icon
     //   SUBTYPE: mute is 1, audible is 2
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_VOLUME_ICON = 212;
 
     // ACTION: Volume dialog > Change ringer mode by tapping icon
     //   SUBTYPE: 2 is audible, 3 is vibrate
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_RINGER_MODE = 213;
 
     // ACTION: Chooser shown (share target, file open, etc.)
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ACTIVITY_CHOOSER_SHOWN = 214;
 
     // ACTION: Chooser > User taps an app target
     //   SUBTYPE: Index of target
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ACTIVITY_CHOOSER_PICKED_APP_TARGET = 215;
 
     // ACTION: Chooser > User taps a service target
     //   SUBTYPE: Index of target
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ACTIVITY_CHOOSER_PICKED_SERVICE_TARGET = 216;
 
     // ACTION: Chooser > User taps a standard target
     //   SUBTYPE: Index of target
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ACTIVITY_CHOOSER_PICKED_STANDARD_TARGET = 217;
 
     // ACTION: QS Brightness Slider (with auto brightness disabled)
     //   SUBTYPE: slider value
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_BRIGHTNESS = 218;
 
     // ACTION: QS Brightness Slider (with auto brightness enabled)
     //   SUBTYPE: slider value
     // CATEGORY: QUICK_SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_BRIGHTNESS_AUTO = 219;
 
     // OPEN: Settings > Display > Brightness Slider
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     BRIGHTNESS_DIALOG = 220;
 
     // OPEN: Settings > Apps > Configure Apps > Draw over other apps
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     SYSTEM_ALERT_WINDOW_APPS = 221;
 
     // OPEN: Display has entered dream mode
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     DREAMING = 222;
 
     // OPEN: Display has entered ambient notification mode
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     DOZING = 223;
 
     // OPEN: Overview
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     OVERVIEW_ACTIVITY = 224;
 
     // OPEN: Settings > About phone > Legal information
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ABOUT_LEGAL_SETTINGS = 225;
 
     // OPEN: Settings > Search > Perform search
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_SEARCH_RESULTS = 226;
 
     // OPEN: Settings > System UI Tuner
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER = 227;
 
     // OPEN: Settings > System UI Tuner > Quick Settings
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_QS = 228;
 
     // OPEN: Settings > System UI Tuner > Demo mode
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_DEMO_MODE = 229;
 
     // ACTION: Settings > System UI Tuner > Quick Settings > Move tile
     //   PACKAGE: Tile
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_QS_REORDER = 230;
 
     // ACTION: Settings > System UI Tuner > Quick Settings > Add tile
     //   PACKAGE: Tile
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_QS_ADD = 231;
 
     // ACTION: Settings > System UI Tuner > Quick Settings > Remove tile
     //   PACKAGE: Tile
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_QS_REMOVE = 232;
 
     // ACTION: Settings > System UI Tuner > Status bar > Enable icon
     //   PACKAGE: Icon
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_STATUS_BAR_ENABLE = 233;
 
     // ACTION: Settings > System UI Tuner > Status bar > Disable icon
     //   PACKAGE: Icon
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_STATUS_BAR_DISABLE = 234;
 
     // ACTION: Settings > System UI Tuner > Demo mode > Enable demo mode
     //   SUBTYPE: false is disabled, true is enabled
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_DEMO_MODE_ENABLED = 235;
 
     // ACTION: Settings > System UI Tuner > Demo mode > Show demo mode
     //   SUBTYPE: false is disabled, true is enabled
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_DEMO_MODE_ON = 236;
 
     // ACTION: Settings > System UI Tuner > Show embedded battery percentage
     //   SUBTYPE: 0 is disabled, 1 is enabled
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     TUNER_BATTERY_PERCENTAGE = 237;
 
     // OPEN: Settings > Developer options > Inactive apps
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.5.26
     FUELGAUGE_INACTIVE_APPS = 238;
 
     // ACTION: Long press home to bring up assistant
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.5.26
     ACTION_ASSIST_LONG_PRESS = 239;
 
     // OPEN: Settings > Security > Nexus Imprint > Add Fingerprint
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLLING = 240;
 
     // OPEN: Fingerprint Enroll > Find Sensor
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_FIND_SENSOR = 241;
 
     // OPEN: Fingerprint Enroll > Fingerprint Enrolled!
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLL_FINISH = 242;
 
     // OPEN: Fingerprint Enroll introduction
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLL_INTRO = 243;
 
     // OPEN: Fingerprint Enroll onboarding
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLL_ONBOARD = 244;
 
     // OPEN: Fingerprint Enroll > Let's Start!
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLL_SIDECAR = 245;
 
     // OPEN: Fingerprint Enroll SUW > Let's Start!
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLLING_SETUP = 246;
 
     // OPEN: Fingerprint Enroll SUW > Find Sensor
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_FIND_SENSOR_SETUP = 247;
 
     // OPEN: Fingerprint Enroll SUW > Fingerprint Enrolled!
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLL_FINISH_SETUP = 248;
 
     // OPEN: Fingerprint Enroll SUW introduction
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLL_INTRO_SETUP = 249;
 
     // OPEN: Fingerprint Enroll SUW onboarding
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     FINGERPRINT_ENROLL_ONBOARD_SETUP = 250;
 
     // ACTION: Add fingerprint > Enroll fingerprint
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     ACTION_FINGERPRINT_ENROLL = 251;
 
     // ACTION: Authenticate using fingerprint
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     ACTION_FINGERPRINT_AUTH = 252;
 
     // ACTION: Settings > Security > Nexus Imprint > [Fingerprint] > Delete
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     ACTION_FINGERPRINT_DELETE = 253;
 
     // ACTION: Settings > Security > Nexus Imprint > [Fingerprint] > Rename
     // CATEGORY: SETTINGS
     // OS: 6.0
-    // GMS: 7.8.99
     ACTION_FINGERPRINT_RENAME = 254;
 
     // ACTION: Double tap camera shortcut
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.8.99
     ACTION_DOUBLE_TAP_POWER_CAMERA_GESTURE = 255;
 
     // ACTION: Double twist camera shortcut
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: 6.0
-    // GMS: 7.8.99
     ACTION_WIGGLE_CAMERA_GESTURE = 256;
 
     // OPEN: QS Work Mode tile shown
@@ -1569,13 +1335,11 @@
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: N
-    // GMS: 7.8.99
     QS_WORKMODE = 257;
 
     // OPEN: Settings > Developer Options > Background Check
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     BACKGROUND_CHECK_SUMMARY = 258;
 
     // OPEN: QS Lock tile shown
@@ -1583,52 +1347,44 @@
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: N
-    // GMS: 7.8.99
     QS_LOCK_TILE = 259;
 
     // OPEN: QS User Tile shown
     // CATEGORY: QUICK_SETTINGS
     // OS: N
-    // GMS: 7.8.99
     QS_USER_TILE = 260;
 
     // OPEN: QS Battery tile shown
     // CATEGORY: QUICK_SETTINGS
     // OS: N
-    // GMS: 7.8.99
     QS_BATTERY_TILE = 261;
 
     // OPEN: Settings > Sound > Do not disturb > Visual interruptions
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     NOTIFICATION_ZEN_MODE_VISUAL_INTERRUPTIONS = 262;
 
     // ACTION: Visual interruptions > No screen interuptions toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_ZEN_ALLOW_WHEN_SCREEN_OFF = 263;
 
     // ACTION: Visual interruptions > No notification light toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_ZEN_ALLOW_LIGHTS = 264;
 
     // OPEN: Settings > Notifications > [App] > Topic Notifications
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     NOTIFICATION_TOPIC_NOTIFICATION = 265;
 
     // ACTION: Settings > Apps > Default Apps > Select different SMS app
     //   PACKAGE: Selected SMS app
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_DEFAULT_SMS_APP_CHANGED = 266;
 
     // OPEN: QS Color modification tile shown
@@ -1636,105 +1392,88 @@
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: QUICK_SETTINGS
     // OS: N
-    // GMS: 7.8.99
     QS_COLOR_MATRIX = 267;
 
     // OPEN: QS Custom tile shown
     // ACTION: QS Work Mode tile tapped
     // CATEGORY: QUICK_SETTINGS
     // OS: N
-    // GMS: 7.8.99
     QS_CUSTOM = 268;
 
     // ACTION: Visual interruptions > Never turn off the screen toggle
     //   SUBTYPE: 0 is off, 1 is on
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_ZEN_ALLOW_WHEN_SCREEN_ON = 269;
 
     // ACTION: Overview > Long-press task, drag to enter split-screen
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_WINDOW_DOCK_DRAG_DROP = 270;
 
     // ACTION: In App > Long-press Overview button to enter split-screen
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_WINDOW_DOCK_LONGPRESS = 271;
 
     // ACTION: In App > Swipe Overview button to enter split-screen
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_WINDOW_DOCK_SWIPE = 272;
 
     // ACTION: Launch profile-specific app > Confirm credentials
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     PROFILE_CHALLENGE = 273;
 
     // OPEN: QS Battery detail panel
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     QS_BATTERY_DETAIL = 274;
 
     // OPEN: Overview > History
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     OVERVIEW_HISTORY = 275;
 
     // ACTION: Overview > Page by tapping Overview button
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_OVERVIEW_PAGE = 276;
 
     // ACTION: Overview > Select app
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_OVERVIEW_SELECT = 277;
 
     // ACTION: View emergency info
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_VIEW_EMERGENCY_INFO = 278;
 
     // ACTION: Edit emergency info activity
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_EDIT_EMERGENCY_INFO = 279;
 
     // ACTION: Edit emergency info field
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_EDIT_EMERGENCY_INFO_FIELD = 280;
 
     // ACTION: Add emergency contact
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_ADD_EMERGENCY_CONTACT = 281;
 
     // ACTION: Delete emergency contact
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_DELETE_EMERGENCY_CONTACT = 282;
 
     // ACTION: Call emergency contact
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     ACTION_CALL_EMERGENCY_CONTACT = 283;
 
     // OPEN: QS Data Saver tile shown
@@ -1745,13 +1484,11 @@
     // OPEN: Settings > Security > User credentials
     // CATEGORY: Settings
     // OS: N
-    // GMS: 7.8.99
     USER_CREDENTIALS = 285;
 
     // ACTION: In App (splitscreen) > Long-press Overview to exit split-screen
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_WINDOW_UNDOCK_LONGPRESS = 286;
 
     // Logged when the user scrolls through overview manually
@@ -1773,81 +1510,68 @@
     // ACTION: Long-press power button, then tap "Take bug report" option.
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE = 292;
 
     // ACTION: Long-press power button, then long-press "Take bug report" option.
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_FROM_POWER_MENU_FULL = 293;
 
     // ACTION: Settings -> Developer Options -> Take bug report -> Interactive report
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     // Interactive bug report initiated from Settings.
     ACTION_BUGREPORT_FROM_SETTINGS_INTERACTIVE = 294;
 
     // ACTION: Settings -> Developer Options -> Take bug report -> Full report
     // CATEGORY: SETTINGS
     // OS: N
-    // GMS: 7.8.99
     // Interactive bug report initiated from Settings.
     ACTION_BUGREPORT_FROM_SETTINGS_FULL = 295;
 
     // ACTION: User tapped notification action to cancel a bug report
     // CATEGORY: NOTIFICATION
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_NOTIFICATION_ACTION_CANCEL = 296;
 
     // ACTION: User tapped notification action to launch bug report details screen
     // CATEGORY: NOTIFICATION
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_NOTIFICATION_ACTION_DETAILS = 297;
 
     // ACTION: User tapped notification action to take adition screenshot on bug report
     // CATEGORY: NOTIFICATION
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_NOTIFICATION_ACTION_SCREENSHOT = 298;
 
     // ACTION: User tapped notification to share bug report
     // CATEGORY: NOTIFICATION
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_NOTIFICATION_ACTION_SHARE = 299;
 
     // ACTION: User changed bug report name using the details screen
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_DETAILS_NAME_CHANGED = 300;
 
     // ACTION: User changed bug report title using the details screen
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_DETAILS_TITLE_CHANGED = 301;
 
     // ACTION: User changed bug report description using the details screen
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_DETAILS_DESCRIPTION_CHANGED = 302;
 
     // ACTION: User tapped Save in the bug report details screen.
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_DETAILS_SAVED = 303;
 
     // ACTION: User tapped Cancel in the bug report details screen.
     // CATEGORY: GLOBAL_SYSTEM_UI
     // OS: N
-    // GMS: 7.8.99
     ACTION_BUGREPORT_DETAILS_CANCELED = 304;
 
     // Tuner: Open/close calibrate dialog.
@@ -1920,79 +1644,140 @@
     // the transition was executed.
     APP_TRANSITION_DEVICE_UPTIME_SECONDS = 325;
 
-    // User granted access to the request folder; action takes an integer
-    // representing the folder's index on Environment.STANDARD_DIRECTORIES
-    // (or -2 for root access, or -1 or unknown directory).
+    // ACTION: app requested access to a scoped directory, user granted it.
+    //   SUBTYPE: directory's index on Environment.STANDARD_DIRECTORIES
+    // CATEGORY: GLOBAL_SYSTEM_UI
+    // OS: N
     ACTION_SCOPED_DIRECTORY_ACCESS_GRANTED_BY_FOLDER = 326;
 
-    // User denied access to the request folder; action takes an integer
-    // representing the folder's index on Environment.STANDARD_DIRECTORIES
-    // (or -2 for root access, or -1 or unknown directory).
+    // ACTION: app requested access to a scoped directory, user denied it.
+    //   SUBTYPE: directory's index on Environment.STANDARD_DIRECTORIES
+    // CATEGORY: GLOBAL_SYSTEM_UI
+    // OS: N
     ACTION_SCOPED_DIRECTORY_ACCESS_DENIED_BY_FOLDER = 327;
 
-    // User granted access to the request folder; action pass package name
-    // of calling package.
+    // ACTION: app requested access to a scoped directory, user granted it.
+    //   PACKAGE: app that requested access
+    // CATEGORY: GLOBAL_SYSTEM_UI
+    // OS: N
     ACTION_SCOPED_DIRECTORY_ACCESS_GRANTED_BY_PACKAGE = 328;
 
-    // User denied access to the request folder; action pass package name
-    // of calling package.
+    // ACTION: app requested access to a scoped directory, user denied it.
+    //   PACKAGE: app that requested access.
+    // CATEGORY: GLOBAL_SYSTEM_UI
+    // OS: N
     ACTION_SCOPED_DIRECTORY_ACCESS_DENIED_BY_PACKAGE = 329;
 
-    // App requested access to a directory it has already been granted
-    // access before; action takes an integer representing the folder's
-    // index on Environment.STANDARD_DIRECTORIES
-    // (or -2 for root access, or -1 or unknown directory).
+    // ACTION: app requested access to a directory user has already been granted
+    // access before.
+    //   SUBTYPE: directory's index on Environment.STANDARD_DIRECTORIES.
+    // CATEGORY: GLOBAL_SYSTEM_UI
+    // OS: N
     ACTION_SCOPED_DIRECTORY_ACCESS_ALREADY_GRANTED_BY_FOLDER = 330;
 
-    // App requested access to a directory it has already been granted
-    // access before; action pass package name of calling package.
+    // ACTION: app requested access to a directory user has already been granted
+    // access before.
+    //   PACKAGE: app that requested access.
+    // CATEGORY: GLOBAL_SYSTEM_UI
+    // OS: N
     ACTION_SCOPED_DIRECTORY_ACCESS_ALREADY_GRANTED_BY_PACKAGE = 331;
 
-    // Logged when the user slides a notification and
-    // reveals the gear beneath it.
+    // ACTION: Logged when the user slides a notification and reveals the gear
+    // beneath it.
+    // CATEGORY: NOTIFICATION
+    // OS: N
     ACTION_REVEAL_GEAR = 332;
 
-    // Logged when the user taps on the gear beneath
-    // a notification.
+    // ACTION: Logged when the user taps on the gear beneath a notification.
+    // CATEGORY: NOTIFICATION
+    // OS: N
     ACTION_TOUCH_GEAR = 333;
 
     // Logs that the user has edited the enabled VR listeners.
+    // CATEGORY: SETTINGS
+    // OS: N
     VR_MANAGE_LISTENERS = 334;
 
     // Settings -> Accessibility -> Click after pointer stops moving
+    // CATEGORY: SETTINGS
+    // OS: N
     ACCESSIBILITY_TOGGLE_AUTOCLICK = 335;
+
     // Settings -> Sound
+    // CATEGORY: SETTINGS
+    // OS: N
     SOUND = 336;
+
     // Settings -> Notifications -> Gear
+    // CATEGORY: SETTINGS
+    // OS: N
     CONFIGURE_NOTIFICATION = 337;
+
     // Settings -> Wi-Fi -> Gear
+    // CATEGORY: SETTINGS
+    // OS: N
     CONFIGURE_WIFI = 338;
+
     // Settings -> Display -> Display size
+    // OS: N
     DISPLAY_SCREEN_ZOOM = 339;
+
     // Settings -> Display -> Font size
+    // CATEGORY: SETTINGS
+    // OS: N
     ACCESSIBILITY_FONT_SIZE = 340;
+
     // Settings -> Data usage -> Cellular/Wi-Fi data usage
+    // CATEGORY: SETTINGS
+    // OS: N
     DATA_USAGE_LIST = 341;
+
     // Settings -> Data usage -> Billing cycle or DATA_USAGE_LIST -> Gear
+    // CATEGORY: SETTINGS
+    // OS: N
     BILLING_CYCLE = 342;
+
     // DATA_USAGE_LIST -> Any item or App info -> Data usage
+    // CATEGORY: SETTINGS
+    // OS: N
     APP_DATA_USAGE = 343;
+
     // Settings -> Language & input -> Language
+    // CATEGORY: SETTINGS
+    // OS: N
     USER_LOCALE_LIST = 344;
+
     // Settings -> Language & input -> Virtual keyboard
+    // CATEGORY: SETTINGS
+    // OS: N
     VIRTUAL_KEYBOARDS = 345;
+
     // Settings -> Language & input -> Physical keyboard
+    // CATEGORY: SETTINGS
+    // OS: N
     PHYSICAL_KEYBOARDS = 346;
+
     // Settings -> Language & input -> Virtual keyboard -> Add a virtual keyboard
+    // CATEGORY: SETTINGS
+    // OS: N
     ENABLE_VIRTUAL_KEYBOARDS = 347;
+
     // Settings -> Data usage -> Data Saver
+    // CATEGORY: SETTINGS
+    // OS: N
     DATA_SAVER_SUMMARY = 348;
+
     // Settings -> Data usage -> Data Saver -> Unrestricted data access
+    // CATEGORY: SETTINGS
+    // OS: N
     DATA_USAGE_UNRESTRICTED_ACCESS = 349;
 
     // Used for generic logging of Settings Preference Persistence, should not be used
     // outside SharedPreferencesLogger.
+    // CATEGORY: SETTINGS
+    // OS: N
     ACTION_GENERIC_PACKAGE = 350;
+
     // Settings -> Apps -> Gear -> Special access
     SPECIAL_ACCESS = 351;
 
@@ -2158,15 +1943,28 @@
     // System UI Tuner > Other > Power notification controls > Toggle on/off
     ACTION_TUNER_POWER_NOTIFICATION_CONTROLS = 393;
 
-    // Action: user enable / disabled data saver using Settings. Arguments:
-    // 0: Data Saver mode is disabled.
-    // 1: Data Saver mode is enabled.
+    // Action: user enable / disabled data saver using Settings
+    // OPEN: Settings -> Data Usage -> Data saver -> On/off toggle
+    // VALUE: 1 for enabled, 0 for disabled
+    // CATEGORY: SETTINGS
+    // OS: N
     ACTION_DATA_SAVER_MODE = 394;
 
-    // User whitelisted an app for Data Saver mode; action pass package name of app.
+    // User whitelisted an app for Data Saver mode; action pass package name of app
+    // Action: user enable / disabled data saver using Settings
+    // OPEN: Settings -> Data Usage -> Data saver -> Unrestricted data access > APP toggle turned on
+    //       or
+    //       Settings -> Apps -> APP -> Data usage -> Unrestricted data usage toggle turned on
+    // VALUE: package name of APP
+    // CATEGORY: SETTINGS
+    // OS: N
     ACTION_DATA_SAVER_WHITELIST = 395;
 
-    // User blacklisted an app for Data Saver mode; action pass package name of app.
+    // User blacklisted an app for Data Saver mode; action pass package name of app
+    // OPEN: Settings -> Apps -> APP -> Data usage -> Background data toggle turned off
+    // VALUE: package name of APP
+    // CATEGORY: SETTINGS
+    // OS: N
     ACTION_DATA_SAVER_BLACKLIST = 396;
 
     // User opened a remote input view associated with a notification. Passes package name of app
@@ -2212,12 +2010,209 @@
     // Notification group expansion state toggled by the expand affordance.
     ACTION_NOTIFICATION_GROUP_EXPANDER = 408;
 
+
     // Notification expansion state toggled by the expand gesture.
     ACTION_NOTIFICATION_GESTURE_EXPANDER = 409;
 
     // Notification group expansion state toggled by the expand gesture.
     ACTION_NOTIFICATION_GROUP_GESTURE_EXPANDER = 410;
 
+    // User performs gesture that activates the ambient display
+    // 1: Gesture performed is Nudge
+    // 2: Gesture performed is Pickup
+    // 4: Gesture performed is Double Tap
+    ACTION_AMBIENT_GESTURE = 411;
+
+    // ---- End N Constants, all N constants go above this line ----
+
+    // ------- Begin N App Disambig Shade -----
+    // Application disambig shade opened or closed with a featured app.
+    // These are actually visibility events, but visible/hidden doesn't
+    // take a package, so these are being logged as actions.
+    // Package: Calling app on open, called app on close
+    ACTION_SHOW_APP_DISAMBIG_APP_FEATURED = 451;
+    ACTION_HIDE_APP_DISAMBIG_APP_FEATURED = 452;
+
+    // Application disambig shade opened or closed without a featured app.
+    // These are actually visibility events, but visible/hidden doesn't
+    // take a package, so these are being logged as actions.
+    // Package: Calling app on open, called app on close
+    ACTION_SHOW_APP_DISAMBIG_NONE_FEATURED = 453;
+    ACTION_HIDE_APP_DISAMBIG_NONE_FEATURED = 454;
+
+    // User opens in an app by pressing “Always” in the application disambig shade.
+    // Subtype: Index of selection
+    ACTION_APP_DISAMBIG_ALWAYS = 455;
+
+    // User opens in an app by pressing “Just Once” in the application disambig shade.
+    // Subtype: Index of selection
+    ACTION_APP_DISAMBIG_JUST_ONCE = 456;
+
+    // User opens in an app by tapping on its name in the application disambig shade.
+    // Subtype: Index of selection
+    ACTION_APP_DISAMBIG_TAP = 457;
+
+    // OPEN: Settings > Internal storage > Storage manager
+    // CATEGORY: SETTINGS
+    STORAGE_MANAGER_SETTINGS = 458;
+
+    // OPEN: Settings -> Gestures
+    // CATEGORY: SETTINGS
+    SETTINGS_GESTURES = 459;
+
+    // ------ Begin Deletion Helper ------
+    // ACTION: Settings > Storage > Free Up Space > Photos & Videos > Toggle
+    //   SUBTYPE: false is off, true is on
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_SELECTION_PHOTOS = 460;
+
+    // ACTION: Settings > Storage > Free Up Space > Apps > Toggle
+    //   SUBTYPE: false is off, true is on
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_SELECTION_ALL_APPS = 461;
+
+    // ACTION: Settings > Storage > Free Up Space > Apps > Click an unchecked app
+    // CATEGORY: SETTINGS
+    //   PACKAGE: Unchecked app
+    ACTION_DELETION_SELECTION_APP_ON = 462;
+
+    // ACTION: Settings > Storage > Free Up Space > Apps > Click a checked app
+    // CATEGORY: SETTINGS
+    //   PACKAGE: Checked app
+    ACTION_DELETION_SELECTION_APP_OFF = 463;
+
+    // ACTION: Settings > Storage > Free Up Space > Apps > Click category
+    //   SUBTYPE: false is expanded, true is collapsed
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_APPS_COLLAPSED = 464;
+
+    // ACTION: Settings > Storage > Free Up Space > Downloads > Check On
+    //   SUBTYPE: false is off, true is on
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_SELECTION_DOWNLOADS = 465;
+
+    // ACTION: Settings > Storage > Free Up Space > Downloads > Click category
+    //   SUBTYPE: false is expanded, true is collapsed
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_DOWNLOADS_COLLAPSED = 466;
+
+    // ACTION: Settings > Storage > Free Up Space > Free up ... GB
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_HELPER_CLEAR = 467;
+
+    // ACTION: Settings > Storage > Free Up Space > Cancel
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_HELPER_CANCEL = 468;
+
+    // ACTION: Settings > Storage > Free Up Space > Free up ... GB > Remove
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_HELPER_REMOVE_CONFIRM = 469;
+
+    // ACTION: Settings > Storage > Free Up Space > Free up ... GB > Cancel
+    // CATEGORY: SETTINGS
+    ACTION_DELETION_HELPER_REMOVE_CANCEL = 470;
+
+    // Deletion helper encountered an error during package deletion.
+    ACTION_DELETION_HELPER_APPS_DELETION_FAIL = 471;
+
+    // Deletion helper encountered an error during downloads folder deletion.
+    ACTION_DELETION_HELPER_DOWNLOADS_DELETION_FAIL = 472;
+
+    // Deletion helper encountered an error during photo and video deletion.
+    ACTION_DELETION_HELPER_PHOTOS_VIDEOS_DELETION_FAIL = 473;
+
+    // OPEN: Settings (root page if there are multiple tabs)
+    // CATEGORY: SETTINGS
+    DASHBOARD_CONTAINER = 474;
+
+    // OPEN: Settings -> SUPPORT TAB
+    // CATEGORY: SETTINGS
+    SUPPORT_FRAGMENT = 475;
+
+    // ACTION: Settings -> Select summary tab.
+    // CATEGORY: SETTINGS
+    ACTION_SELECT_SUMMARY=476;
+
+    // ACTION: Settings -> Select support tab.
+    // CATEGORY: SETTINGS
+    ACTION_SELECT_SUPPORT_FRAGMENT = 477;
+
+    // ACTION: Settings -> Support -> Tips & tricks
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_TIPS_AND_TRICKS = 478;
+
+    // ACTION: Settings -> Support -> Help & feedback
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_HELP_AND_FEEDBACK = 479;
+
+    // ACTION: Settings -> Support -> Sign in
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_SIGN_IN = 480;
+
+    // ACTION: Settings -> Support -> Phone
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_PHONE = 481;
+
+    // ACTION: Settings -> Support -> Chat
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_CHAT = 482;
+
+    // ACTION: Settings -> Support -> Phone/Chat -> Disclaimer Cancel
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_DISCLAIMER_CANCEL = 483;
+
+    // ACTION: Settings -> Support -> Phone/Chat -> Disclaimer OK
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_DISCLAIMER_OK = 484;
+
+    // ACTION: Settings -> Support -> Toll-Free Phone
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_DAIL_TOLLFREE = 485;
+
+    // ACTION: Settings -> Support -> "Travel Abroad" Button
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_VIEW_TRAVEL_ABROAD_DIALOG = 486;
+
+    // ACTION: Settings -> Support -> "Travel Abroad" Button -> Tolled Phone
+    // CATEGORY: SETTINGS
+    ACTION_SUPPORT_DIAL_TOLLED = 487;
+
+    // OPEN: Settings > Display > Night Light
+    // CATEGORY: SETTINGS
+    NIGHT_DISPLAY_SETTINGS = 488;
+
+    // ACTION: Settings -> Storage -> Manage storage -> Click Storage Manager
+        //   SUBTYPE: false is off, true is on
+    ACTION_TOGGLE_STORAGE_MANAGER = 489;
+
+    // Settings launched from collapsed quick settings.
+    ACTION_QS_COLLAPSED_SETTINGS_LAUNCH = 490;
+
+    // OPEN: QS Night Light tile shown
+    // ACTION: QS Night Light tile tapped
+    //   SUBTYPE: 0 is off, 1 is on
+    // CATEGORY: QUICK_SETTINGS
+    QS_NIGHT_DISPLAY = 491;
+
+    // Night Light on
+    SETTINGS_CONDITION_NIGHT_DISPLAY = 492;
+
+    // System navigation key up.
+    ACTION_SYSTEM_NAVIGATION_KEY_UP = 493;
+
+    // System navigation key down.
+    ACTION_SYSTEM_NAVIGATION_KEY_DOWN = 494;
+
+    // OPEN: Settings > Display -> Ambient Display
+    // CATEGORY: SETTINGS
+    ACTION_AMBIENT_DISPLAY = 495;
+
+    // ACTION: Settings -> [sub settings activity] -> Options menu -> Help & Support
+    //   SUBTYPE: sub settings classname
+    ACTION_SETTING_HELP_AND_FEEDBACK = 496;
+
+    // ---- End N-MR1 Constants, all N-MR1 constants go above this line ----
+
     // Add new aosp constants above this line.
     // END OF AOSP CONSTANTS
   }
diff --git a/services/Android.mk b/services/Android.mk
index 1918db5..3385bed 100644
--- a/services/Android.mk
+++ b/services/Android.mk
@@ -28,6 +28,7 @@
     net \
     print \
     restrictions \
+    retaildemo \
     usage \
     usb \
     voiceinteraction
diff --git a/services/accessibility/java/com/android/server/accessibility/AccessibilityGestureDetector.java b/services/accessibility/java/com/android/server/accessibility/AccessibilityGestureDetector.java
index 562d950..582b19b 100644
--- a/services/accessibility/java/com/android/server/accessibility/AccessibilityGestureDetector.java
+++ b/services/accessibility/java/com/android/server/accessibility/AccessibilityGestureDetector.java
@@ -341,6 +341,8 @@
         mDoubleTapDetected = false;
         mSecondFingerDoubleTap = false;
         mGestureStarted = false;
+        mGestureDetector.onTouchEvent(MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_CANCEL,
+                0.0f, 0.0f, 0));
         cancelGesture();
     }
 
diff --git a/services/accessibility/java/com/android/server/accessibility/AccessibilityInputFilter.java b/services/accessibility/java/com/android/server/accessibility/AccessibilityInputFilter.java
index 2a7d945..fd93865 100644
--- a/services/accessibility/java/com/android/server/accessibility/AccessibilityInputFilter.java
+++ b/services/accessibility/java/com/android/server/accessibility/AccessibilityInputFilter.java
@@ -77,6 +77,9 @@
      */
     static final int FLAG_FEATURE_INJECT_MOTION_EVENTS = 0x00000010;
 
+    static final int FEATURES_AFFECTING_MOTION_EVENTS = FLAG_FEATURE_INJECT_MOTION_EVENTS
+            | FLAG_FEATURE_AUTOCLICK | FLAG_FEATURE_TOUCH_EXPLORATION
+            | FLAG_FEATURE_SCREEN_MAGNIFIER;
     /**
      * Flag for enabling the feature to control the screen magnifier. If
      * {@link #FLAG_FEATURE_SCREEN_MAGNIFIER} is set this flag is ignored
@@ -203,8 +206,13 @@
         }
 
         if (event instanceof MotionEvent) {
-            MotionEvent motionEvent = (MotionEvent) event;
-            processMotionEvent(state, motionEvent, policyFlags);
+            if ((mEnabledFeatures & FEATURES_AFFECTING_MOTION_EVENTS) != 0) {
+                MotionEvent motionEvent = (MotionEvent) event;
+                processMotionEvent(state, motionEvent, policyFlags);
+                return;
+            } else {
+                super.onInputEvent(event, policyFlags);
+            }
         } else if (event instanceof KeyEvent) {
             KeyEvent keyEvent = (KeyEvent) event;
             processKeyEvent(state, keyEvent, policyFlags);
diff --git a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
index f039e1e..e00178f 100644
--- a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
+++ b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
@@ -279,6 +279,31 @@
             }
 
             @Override
+            public void onPackageUpdateFinished(String packageName, int uid) {
+                // Unbind all services from this package, and then update the user state to
+                // re-bind new versions of them.
+                synchronized (mLock) {
+                    final int userId = getChangingUserId();
+                    if (userId != mCurrentUserId) {
+                        return;
+                    }
+                    UserState userState = getUserStateLocked(userId);
+                    boolean unboundAService = false;
+                    for (int i = userState.mBoundServices.size() - 1; i >= 0; i--) {
+                        Service boundService = userState.mBoundServices.get(i);
+                        String servicePkg = boundService.mComponentName.getPackageName();
+                        if (servicePkg.equals(packageName)) {
+                            boundService.unbindLocked();
+                            unboundAService = true;
+                        }
+                    }
+                    if (unboundAService) {
+                        onUserStateChangedLocked(userState);
+                    }
+                }
+            }
+
+            @Override
             public void onPackageRemoved(String packageName, int uid) {
                 synchronized (mLock) {
                     final int userId = getChangingUserId();
@@ -1112,9 +1137,11 @@
 
     private void addServiceLocked(Service service, UserState userState) {
         try {
-            service.onAdded();
-            userState.mBoundServices.add(service);
-            userState.mComponentNameToServiceMap.put(service.mComponentName, service);
+            if (!userState.mBoundServices.contains(service)) {
+                service.onAdded();
+                userState.mBoundServices.add(service);
+                userState.mComponentNameToServiceMap.put(service.mComponentName, service);
+            }
         } catch (RemoteException re) {
             /* do nothing */
         }
@@ -1127,8 +1154,14 @@
      */
     private void removeServiceLocked(Service service, UserState userState) {
         userState.mBoundServices.remove(service);
-        userState.mComponentNameToServiceMap.remove(service.mComponentName);
         service.onRemoved();
+        // It may be possible to bind a service twice, which confuses the map. Rebuild the map
+        // to make sure we can still reach a service
+        userState.mComponentNameToServiceMap.clear();
+        for (int i = 0; i < userState.mBoundServices.size(); i++) {
+            Service boundService = userState.mBoundServices.get(i);
+            userState.mComponentNameToServiceMap.put(boundService.mComponentName, boundService);
+        }
     }
 
     /**
@@ -1423,7 +1456,8 @@
         updateTouchExplorationLocked(userState);
         updatePerformGesturesLocked(userState);
         updateEnhancedWebAccessibilityLocked(userState);
-        updateDisplayColorAdjustmentSettingsLocked(userState);
+        updateDisplayDaltonizerLocked(userState);
+        updateDisplayInversionLocked(userState);
         updateMagnificationLocked(userState);
         updateSoftKeyboardShowModeLocked(userState);
         scheduleUpdateInputFilter(userState);
@@ -1540,7 +1574,6 @@
         somethingChanged |= readEnhancedWebAccessibilityEnabledChangedLocked(userState);
         somethingChanged |= readDisplayMagnificationEnabledSettingLocked(userState);
         somethingChanged |= readAutoclickEnabledSettingLocked(userState);
-        somethingChanged |= readDisplayColorAdjustmentSettingsLocked(userState);
 
         return somethingChanged;
     }
@@ -1603,18 +1636,6 @@
          return false;
     }
 
-    private boolean readDisplayColorAdjustmentSettingsLocked(UserState userState) {
-        final boolean displayAdjustmentsEnabled = DisplayAdjustmentUtils.hasAdjustments(mContext,
-                userState.mUserId);
-        if (displayAdjustmentsEnabled != userState.mHasDisplayColorAdjustment) {
-            userState.mHasDisplayColorAdjustment = displayAdjustmentsEnabled;
-            return true;
-        }
-        // If display adjustment is enabled, always assume there was a change in
-        // the adjustment settings.
-        return displayAdjustmentsEnabled;
-    }
-
     private boolean readHighTextContrastEnabledSettingLocked(UserState userState) {
         final boolean highTextContrastEnabled = Settings.Secure.getIntForUser(
                 mContext.getContentResolver(),
@@ -1731,8 +1752,12 @@
         return false;
     }
 
-    private void updateDisplayColorAdjustmentSettingsLocked(UserState userState) {
-        DisplayAdjustmentUtils.applyAdjustments(mContext, userState.mUserId);
+    private void updateDisplayDaltonizerLocked(UserState userState) {
+        DisplayAdjustmentUtils.applyDaltonizerSetting(mContext, userState.mUserId);
+    }
+
+    private void updateDisplayInversionLocked(UserState userState) {
+        DisplayAdjustmentUtils.applyInversionSetting(mContext, userState.mUserId);
     }
 
     private void updateMagnificationLocked(UserState userState) {
@@ -2183,6 +2208,8 @@
 
         AccessibilityServiceInfo mAccessibilityServiceInfo;
 
+        // The service that's bound to this instance. Whenever this value is non-null, this
+        // object is registered as a death recipient
         IBinder mService;
 
         IAccessibilityServiceClient mServiceInterface;
@@ -2317,14 +2344,14 @@
                 }
             } else {
                 userState.mBindingServices.add(mComponentName);
-                mService = userState.mUiAutomationServiceClient.asBinder();
                 mMainHandler.post(new Runnable() {
                     @Override
                     public void run() {
                         // Simulate asynchronous connection since in onServiceConnected
                         // we may modify the state data in case of an error but bind is
                         // called while iterating over the data and bad things can happen.
-                        onServiceConnected(mComponentName, mService);
+                        onServiceConnected(mComponentName,
+                                userState.mUiAutomationServiceClient.asBinder());
                     }
                 });
                 userState.mUiAutomationService = this;
@@ -2333,15 +2360,12 @@
         }
 
         /**
-         * Unbinds form the accessibility service and removes it from the data
+         * Unbinds from the accessibility service and removes it from the data
          * structures for service management.
          *
          * @return True if unbinding is successful.
          */
         public boolean unbindLocked() {
-            if (mService == null) {
-                return false;
-            }
             UserState userState = getUserStateLocked(mUserId);
             getKeyEventDispatcher().flush(this);
             if (!mIsAutomation) {
@@ -2419,7 +2443,19 @@
         @Override
         public void onServiceConnected(ComponentName componentName, IBinder service) {
             synchronized (mLock) {
-                mService = service;
+                if (mService != service) {
+                    if (mService != null) {
+                        mService.unlinkToDeath(this, 0);
+                    }
+                    mService = service;
+                    try {
+                        mService.linkToDeath(this, 0);
+                    } catch (RemoteException re) {
+                        Slog.e(LOG_TAG, "Failed registering death link");
+                        binderDied();
+                        return;
+                    }
+                }
                 mServiceInterface = IAccessibilityServiceClient.Stub.asInterface(service);
                 UserState userState = getUserStateLocked(mUserId);
                 addServiceLocked(this, userState);
@@ -3049,11 +3085,10 @@
 
         @Override
         public void onServiceDisconnected(ComponentName componentName) {
-            /* do nothing - #binderDied takes care */
+            binderDied();
         }
 
         public void onAdded() throws RemoteException {
-            linkToOwnDeathLocked();
             final long identity = Binder.clearCallingIdentity();
             try {
                 mWindowManagerService.addWindowToken(mOverlayWindowToken,
@@ -3070,26 +3105,22 @@
             } finally {
                 Binder.restoreCallingIdentity(identity);
             }
-            unlinkToOwnDeathLocked();
-        }
-
-        public void linkToOwnDeathLocked() throws RemoteException {
-            mService.linkToDeath(this, 0);
-        }
-
-        public void unlinkToOwnDeathLocked() {
-            mService.unlinkToDeath(this, 0);
         }
 
         public void resetLocked() {
             try {
                 // Clear the proxy in the other process so this
                 // IAccessibilityServiceConnection can be garbage collected.
-                mServiceInterface.init(null, mId, null);
+                if (mServiceInterface != null) {
+                    mServiceInterface.init(null, mId, null);
+                }
             } catch (RemoteException re) {
                 /* ignore */
             }
-            mService = null;
+            if (mService != null) {
+                mService.unlinkToDeath(this, 0);
+                mService = null;
+            }
             mServiceInterface = null;
         }
 
@@ -3109,10 +3140,10 @@
                 mWasConnectedAndDied = true;
                 getKeyEventDispatcher().flush(this);
                 UserState userState = getUserStateLocked(mUserId);
-                // The death recipient is unregistered in removeServiceLocked
-                removeServiceLocked(this, userState);
                 resetLocked();
                 if (mIsAutomation) {
+                    // This is typically done when unbinding, but UiAutomation isn't bound.
+                    removeServiceLocked(this, userState);
                     // We no longer have an automation service, so restore
                     // the state based on values in the settings database.
                     userState.mInstalledServices.remove(mAccessibilityServiceInfo);
@@ -3567,6 +3598,7 @@
                 case WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL:
                 case WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL:
                 case WindowManager.LayoutParams.TYPE_BASE_APPLICATION:
+                case WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION:
                 case WindowManager.LayoutParams.TYPE_PHONE:
                 case WindowManager.LayoutParams.TYPE_PRIORITY_PHONE:
                 case WindowManager.LayoutParams.TYPE_TOAST:
@@ -3651,13 +3683,6 @@
                 Rect boundsInScreen = mTempRect;
                 focus.getBoundsInScreen(boundsInScreen);
 
-                // Clip to the window bounds.
-                Rect windowBounds = mTempRect1;
-                getWindowBounds(focus.getWindowId(), windowBounds);
-                if (!boundsInScreen.intersect(windowBounds)) {
-                    return false;
-                }
-
                 // Apply magnification if needed.
                 MagnificationSpec spec = getCompatibleMagnificationSpecLocked(focus.getWindowId());
                 if (spec != null && !spec.isNop()) {
@@ -3665,6 +3690,13 @@
                     boundsInScreen.scale(1 / spec.scale);
                 }
 
+                // Clip to the window bounds.
+                Rect windowBounds = mTempRect1;
+                getWindowBounds(focus.getWindowId(), windowBounds);
+                if (!boundsInScreen.intersect(windowBounds)) {
+                    return false;
+                }
+
                 // Clip to the screen bounds.
                 Point screenSize = mTempPoint;
                 mDefaultDisplay.getRealSize(screenSize);
@@ -4192,7 +4224,6 @@
         public boolean mIsAutoclickEnabled;
         public boolean mIsPerformGesturesEnabled;
         public boolean mIsFilterKeyEventsEnabled;
-        public boolean mHasDisplayColorAdjustment;
         public boolean mAccessibilityFocusOnlyInActiveWindow;
 
         private Service mUiAutomationService;
@@ -4308,9 +4339,6 @@
         private final Uri mDisplayDaltonizerUri = Settings.Secure.getUriFor(
                 Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER);
 
-        private final Uri mDisplayColorMatrixUri = Settings.Secure.getUriFor(
-                Settings.Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX);
-
         private final Uri mHighTextContrastUri = Settings.Secure.getUriFor(
                 Settings.Secure.ACCESSIBILITY_HIGH_TEXT_CONTRAST_ENABLED);
 
@@ -4342,8 +4370,6 @@
             contentResolver.registerContentObserver(
                     mDisplayDaltonizerUri, false, this, UserHandle.USER_ALL);
             contentResolver.registerContentObserver(
-                    mDisplayColorMatrixUri, false, this, UserHandle.USER_ALL);
-            contentResolver.registerContentObserver(
                     mHighTextContrastUri, false, this, UserHandle.USER_ALL);
             contentResolver.registerContentObserver(
                     mAccessibilitySoftKeyboardModeUri, false, this, UserHandle.USER_ALL);
@@ -4385,14 +4411,11 @@
                     if (readEnhancedWebAccessibilityEnabledChangedLocked(userState)) {
                         onUserStateChangedLocked(userState);
                     }
-                } else if (mDisplayInversionEnabledUri.equals(uri)
-                        || mDisplayDaltonizerEnabledUri.equals(uri)
+                } else if (mDisplayDaltonizerEnabledUri.equals(uri)
                         || mDisplayDaltonizerUri.equals(uri)) {
-                    if (readDisplayColorAdjustmentSettingsLocked(userState)) {
-                        updateDisplayColorAdjustmentSettingsLocked(userState);
-                    }
-                } else if (mDisplayColorMatrixUri.equals(uri)) {
-                    updateDisplayColorAdjustmentSettingsLocked(userState);
+                    updateDisplayDaltonizerLocked(userState);
+                } else if (mDisplayInversionEnabledUri.equals(uri)) {
+                    updateDisplayInversionLocked(userState);
                 } else if (mHighTextContrastUri.equals(uri)) {
                     if (readHighTextContrastEnabledSettingLocked(userState)) {
                         onUserStateChangedLocked(userState);
diff --git a/services/accessibility/java/com/android/server/accessibility/DisplayAdjustmentUtils.java b/services/accessibility/java/com/android/server/accessibility/DisplayAdjustmentUtils.java
index e1f3cd8..1532946 100644
--- a/services/accessibility/java/com/android/server/accessibility/DisplayAdjustmentUtils.java
+++ b/services/accessibility/java/com/android/server/accessibility/DisplayAdjustmentUtils.java
@@ -18,23 +18,23 @@
 
 import android.content.ContentResolver;
 import android.content.Context;
-import android.opengl.Matrix;
-import android.os.IBinder;
-import android.os.Parcel;
-import android.os.RemoteException;
-import android.os.ServiceManager;
-import android.provider.Settings;
-import android.util.Slog;
+import android.provider.Settings.Secure;
 import android.view.accessibility.AccessibilityManager;
 
+import com.android.server.LocalServices;
+import com.android.server.display.DisplayTransformManager;
+
 /**
  * Utility methods for performing accessibility display adjustments.
  */
 class DisplayAdjustmentUtils {
-    private static final String LOG_TAG = DisplayAdjustmentUtils.class.getSimpleName();
+
+    /** Default inversion mode for display color correction. */
+    private static final int DEFAULT_DISPLAY_DALTONIZER =
+            AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY;
 
     /** Matrix and offset used for converting color to gray-scale. */
-    private static final float[] GRAYSCALE_MATRIX = new float[] {
+    private static final float[] MATRIX_GRAYSCALE = new float[] {
         .2126f, .2126f, .2126f, 0,
         .7152f, .7152f, .7152f, 0,
         .0722f, .0722f, .0722f, 0,
@@ -48,150 +48,44 @@
      * represents a non-multiplied addition, see surfaceflinger's ProgramCache
      * for full implementation details.
      */
-    private static final float[] INVERSION_MATRIX_VALUE_ONLY = new float[] {
+    private static final float[] MATRIX_INVERT_COLOR = new float[] {
         0.402f, -0.598f, -0.599f, 0,
        -1.174f, -0.174f, -1.175f, 0,
        -0.228f, -0.228f,  0.772f, 0,
              1,       1,       1, 1
     };
 
-    /** Default inversion mode for display color correction. */
-    private static final int DEFAULT_DISPLAY_DALTONIZER =
-            AccessibilityManager.DALTONIZER_CORRECT_DEUTERANOMALY;
-
-    /**
-     * Returns whether the specified user with has any display color
-     * adjustments.
-     */
-    public static boolean hasAdjustments(Context context, int userId) {
+    public static void applyDaltonizerSetting(Context context, int userId) {
         final ContentResolver cr = context.getContentResolver();
+        final DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
 
-        if (Settings.Secure.getIntForUser(cr,
-                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0, userId) != 0) {
-            return true;
+        int daltonizerMode = AccessibilityManager.DALTONIZER_DISABLED;
+        if (Secure.getIntForUser(cr,
+                Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0, userId) != 0) {
+            daltonizerMode = Secure.getIntForUser(cr,
+                    Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, DEFAULT_DISPLAY_DALTONIZER, userId);
         }
 
-        if (Settings.Secure.getIntForUser(cr,
-                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0, userId) != 0) {
-            return true;
+        float[] grayscaleMatrix = null;
+        if (daltonizerMode == AccessibilityManager.DALTONIZER_SIMULATE_MONOCHROMACY) {
+            // Monochromacy isn't supported by the native Daltonizer.
+            grayscaleMatrix = MATRIX_GRAYSCALE;
+            daltonizerMode = AccessibilityManager.DALTONIZER_DISABLED;
         }
-
-        return false;
+        dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_GRAYSCALE, grayscaleMatrix);
+        dtm.setDaltonizerMode(daltonizerMode);
     }
 
     /**
      * Applies the specified user's display color adjustments.
      */
-    public static void applyAdjustments(Context context, int userId) {
+    public static void applyInversionSetting(Context context, int userId) {
         final ContentResolver cr = context.getContentResolver();
-        float[] colorMatrix = null;
+        final DisplayTransformManager dtm = LocalServices.getService(DisplayTransformManager.class);
 
-        if (Settings.Secure.getIntForUser(cr,
-                Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0, userId) != 0) {
-            colorMatrix = multiply(colorMatrix, INVERSION_MATRIX_VALUE_ONLY);
-        }
-
-        if (Settings.Secure.getIntForUser(cr,
-                Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0, userId) != 0) {
-            final int daltonizerMode = Settings.Secure.getIntForUser(cr,
-                    Settings.Secure.ACCESSIBILITY_DISPLAY_DALTONIZER, DEFAULT_DISPLAY_DALTONIZER,
-                    userId);
-            // Monochromacy isn't supported by the native Daltonizer.
-            if (daltonizerMode == AccessibilityManager.DALTONIZER_SIMULATE_MONOCHROMACY) {
-                colorMatrix = multiply(colorMatrix, GRAYSCALE_MATRIX);
-                setDaltonizerMode(AccessibilityManager.DALTONIZER_DISABLED);
-            } else {
-                setDaltonizerMode(daltonizerMode);
-            }
-        } else {
-            setDaltonizerMode(AccessibilityManager.DALTONIZER_DISABLED);
-        }
-
-        String matrix = Settings.Secure.getStringForUser(cr,
-                Settings.Secure.ACCESSIBILITY_DISPLAY_COLOR_MATRIX, userId);
-        if (matrix != null) {
-            final float[] userMatrix = get4x4Matrix(matrix);
-            if (userMatrix != null) {
-                colorMatrix = multiply(colorMatrix, userMatrix);
-            }
-        }
-
-        setColorTransform(colorMatrix);
+        final boolean invertColors = Secure.getIntForUser(cr,
+                Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0, userId) != 0;
+        dtm.setColorMatrix(DisplayTransformManager.LEVEL_COLOR_MATRIX_INVERT_COLOR,
+                invertColors ? MATRIX_INVERT_COLOR : null);
     }
-
-    private static float[] get4x4Matrix(String matrix) {
-        String[] strValues = matrix.split(",");
-        if (strValues.length != 16) {
-            return null;
-        }
-        float[] values = new float[strValues.length];
-        try {
-            for (int i = 0; i < values.length; i++) {
-                values[i] = Float.parseFloat(strValues[i]);
-            }
-        } catch (java.lang.NumberFormatException ex) {
-            return null;
-        }
-        return values;
-    }
-
-    private static float[] multiply(float[] matrix, float[] other) {
-        if (matrix == null) {
-            return other;
-        }
-        float[] result = new float[16];
-        Matrix.multiplyMM(result, 0, matrix, 0, other, 0);
-        return result;
-    }
-
-    /**
-     * Sets the surface flinger's Daltonization mode. This adjusts the color
-     * space to correct for or simulate various types of color blindness.
-     *
-     * @param mode new Daltonization mode
-     */
-    private static void setDaltonizerMode(int mode) {
-        try {
-            final IBinder flinger = ServiceManager.getService("SurfaceFlinger");
-            if (flinger != null) {
-                final Parcel data = Parcel.obtain();
-                data.writeInterfaceToken("android.ui.ISurfaceComposer");
-                data.writeInt(mode);
-                flinger.transact(1014, data, null, 0);
-                data.recycle();
-            }
-        } catch (RemoteException ex) {
-            Slog.e(LOG_TAG, "Failed to set Daltonizer mode", ex);
-        }
-    }
-
-    /**
-     * Sets the surface flinger's color transformation as a 4x4 matrix. If the
-     * matrix is null, color transformations are disabled.
-     *
-     * @param m the float array that holds the transformation matrix, or null to
-     *            disable transformation
-     */
-    private static void setColorTransform(float[] m) {
-        try {
-            final IBinder flinger = ServiceManager.getService("SurfaceFlinger");
-            if (flinger != null) {
-                final Parcel data = Parcel.obtain();
-                data.writeInterfaceToken("android.ui.ISurfaceComposer");
-                if (m != null) {
-                    data.writeInt(1);
-                    for (int i = 0; i < 16; i++) {
-                        data.writeFloat(m[i]);
-                    }
-                } else {
-                    data.writeInt(0);
-                }
-                flinger.transact(1015, data, null, 0);
-                data.recycle();
-            }
-        } catch (RemoteException ex) {
-            Slog.e(LOG_TAG, "Failed to set color transform", ex);
-        }
-    }
-
 }
diff --git a/services/accessibility/java/com/android/server/accessibility/KeyEventDispatcher.java b/services/accessibility/java/com/android/server/accessibility/KeyEventDispatcher.java
index e03c16e..e8f93b8 100644
--- a/services/accessibility/java/com/android/server/accessibility/KeyEventDispatcher.java
+++ b/services/accessibility/java/com/android/server/accessibility/KeyEventDispatcher.java
@@ -121,7 +121,7 @@
             Service service = boundServices.get(i);
             // Key events are handled only by services that declared
             // this capability and requested to filter key events.
-            if (!service.mRequestFilterKeyEvents) {
+            if (!service.mRequestFilterKeyEvents || (service.mServiceInterface == null)) {
                 continue;
             }
             int filterKeyEventBit = service.mAccessibilityServiceInfo.getCapabilities()
diff --git a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java
index 2a30229..b5fcb5c 100644
--- a/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java
+++ b/services/appwidget/java/com/android/server/appwidget/AppWidgetServiceImpl.java
@@ -30,6 +30,7 @@
 import android.app.admin.DevicePolicyManagerInternal.OnCrossProfileWidgetProvidersChangeListener;
 import android.appwidget.AppWidgetManager;
 import android.appwidget.AppWidgetProviderInfo;
+import android.appwidget.PendingHostUpdate;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
@@ -72,10 +73,12 @@
 import android.util.ArraySet;
 import android.util.AtomicFile;
 import android.util.AttributeSet;
+import android.util.LongSparseArray;
 import android.util.Pair;
 import android.util.Slog;
 import android.util.SparseArray;
 import android.util.SparseIntArray;
+import android.util.SparseLongArray;
 import android.util.TypedValue;
 import android.util.Xml;
 import android.view.Display;
@@ -738,8 +741,8 @@
     }
 
     @Override
-    public ParceledListSlice<RemoteViews> startListening(IAppWidgetHost callbacks,
-            String callingPackage, int hostId, int[] appWidgetIds, int[] updatedIds) {
+    public ParceledListSlice<PendingHostUpdate> startListening(IAppWidgetHost callbacks,
+            String callingPackage, int hostId, int[] appWidgetIds) {
         final int userId = UserHandle.getCallingUserId();
 
         if (DEBUG) {
@@ -759,18 +762,19 @@
             host.callbacks = callbacks;
 
             int N = appWidgetIds.length;
-            ArrayList<RemoteViews> outViews = new ArrayList<>(N);
-            RemoteViews rv;
-            int added = 0;
+            ArrayList<PendingHostUpdate> outUpdates = new ArrayList<>(N);
+
+            LongSparseArray<PendingHostUpdate> updatesMap = new LongSparseArray<>();
             for (int i = 0; i < N; i++) {
-                rv = host.getPendingViewsForId(appWidgetIds[i]);
-                if (rv != null) {
-                    updatedIds[added] = appWidgetIds[i];
-                    outViews.add(rv);
-                    added++;
+                if (host.getPendingUpdatesForId(appWidgetIds[i], updatesMap)) {
+                    // We key the updates based on time, so that the values are sorted by time.
+                    int M = updatesMap.size();
+                    for (int j = 0; j < M; j++) {
+                        outUpdates.add(updatesMap.valueAt(j));
+                    }
                 }
             }
-            return new ParceledListSlice<>(outViews);
+            return new ParceledListSlice<>(outUpdates);
         }
     }
 
@@ -1810,6 +1814,15 @@
     }
 
     private void scheduleNotifyAppWidgetViewDataChanged(Widget widget, int viewId) {
+        if (viewId == ID_VIEWS_UPDATE || viewId == ID_PROVIDER_CHANGED) {
+            // A view id should never collide with these constants but a developer can call this
+            // method with a wrong id. In that case, ignore the call.
+            return;
+        }
+        long requestTime = SystemClock.uptimeMillis();
+        if (widget != null) {
+            widget.updateTimes.put(viewId, requestTime);
+        }
         if (widget == null || widget.host == null || widget.host.zombie
                 || widget.host.callbacks == null || widget.provider == null
                 || widget.provider.zombie) {
@@ -1819,6 +1832,7 @@
         SomeArgs args = SomeArgs.obtain();
         args.arg1 = widget.host;
         args.arg2 = widget.host.callbacks;
+        args.arg3 = requestTime;
         args.argi1 = widget.appWidgetId;
         args.argi2 = viewId;
 
@@ -1829,9 +1843,10 @@
 
 
     private void handleNotifyAppWidgetViewDataChanged(Host host, IAppWidgetHost callbacks,
-            int appWidgetId, int viewId) {
+            int appWidgetId, int viewId, long requestTime) {
         try {
             callbacks.viewDataChanged(appWidgetId, viewId);
+            host.lastWidgetUpdateTime = requestTime;
         } catch (RemoteException re) {
             // It failed; remove the callback. No need to prune because
             // we know that this host is still referenced by this instance.
@@ -1880,7 +1895,7 @@
     private void scheduleNotifyUpdateAppWidgetLocked(Widget widget, RemoteViews updateViews) {
         long requestTime = SystemClock.uptimeMillis();
         if (widget != null) {
-            widget.lastUpdateTime = requestTime;
+            widget.updateTimes.put(ID_VIEWS_UPDATE, requestTime);
         }
         if (widget == null || widget.provider == null || widget.provider.zombie
                 || widget.host.callbacks == null || widget.host.zombie) {
@@ -1890,7 +1905,7 @@
         SomeArgs args = SomeArgs.obtain();
         args.arg1 = widget.host;
         args.arg2 = widget.host.callbacks;
-        args.arg3 = updateViews;
+        args.arg3 = (updateViews != null) ? updateViews.clone() : null;
         args.arg4 = requestTime;
         args.argi1 = widget.appWidgetId;
 
@@ -1913,6 +1928,12 @@
     }
 
     private void scheduleNotifyProviderChangedLocked(Widget widget) {
+        long requestTime = SystemClock.uptimeMillis();
+        if (widget != null) {
+            // When the provider changes, reset everything else.
+            widget.updateTimes.clear();
+            widget.updateTimes.append(ID_PROVIDER_CHANGED, requestTime);
+        }
         if (widget == null || widget.provider == null || widget.provider.zombie
                 || widget.host.callbacks == null || widget.host.zombie) {
             return;
@@ -1922,6 +1943,7 @@
         args.arg1 = widget.host;
         args.arg2 = widget.host.callbacks;
         args.arg3 = widget.provider.info;
+        args.arg4 = requestTime;
         args.argi1 = widget.appWidgetId;
 
         mCallbackHandler.obtainMessage(
@@ -1930,9 +1952,10 @@
     }
 
     private void handleNotifyProviderChanged(Host host, IAppWidgetHost callbacks,
-            int appWidgetId, AppWidgetProviderInfo info) {
+            int appWidgetId, AppWidgetProviderInfo info, long requestTime) {
         try {
             callbacks.providerChanged(appWidgetId, info);
+            host.lastWidgetUpdateTime = requestTime;
         } catch (RemoteException re) {
             synchronized (mLock){
                 Slog.e(TAG, "Widget host dead: " + host.id, re);
@@ -2243,6 +2266,7 @@
         pw.print(info.updatePeriodMillis);
         pw.print(" resizeMode=");
         pw.print(info.resizeMode);
+        pw.print(" widgetCategory=");
         pw.print(info.widgetCategory);
         pw.print(" autoAdvanceViewId=");
         pw.print(info.autoAdvanceViewId);
@@ -3416,10 +3440,11 @@
                     Host host = (Host) args.arg1;
                     IAppWidgetHost callbacks = (IAppWidgetHost) args.arg2;
                     AppWidgetProviderInfo info = (AppWidgetProviderInfo)args.arg3;
+                    long requestTime = (Long) args.arg4;
                     final int appWidgetId = args.argi1;
                     args.recycle();
 
-                    handleNotifyProviderChanged(host, callbacks, appWidgetId, info);
+                    handleNotifyProviderChanged(host, callbacks, appWidgetId, info, requestTime);
                 } break;
 
                 case MSG_NOTIFY_PROVIDERS_CHANGED: {
@@ -3435,11 +3460,13 @@
                     SomeArgs args = (SomeArgs) message.obj;
                     Host host = (Host) args.arg1;
                     IAppWidgetHost callbacks = (IAppWidgetHost) args.arg2;
+                    long requestTime = (Long) args.arg3;
                     final int appWidgetId = args.argi1;
                     final int viewId = args.argi2;
                     args.recycle();
 
-                    handleNotifyAppWidgetViewDataChanged(host, callbacks, appWidgetId, viewId);
+                    handleNotifyAppWidgetViewDataChanged(host, callbacks, appWidgetId, viewId,
+                            requestTime);
                 } break;
             }
         }
@@ -3778,20 +3805,41 @@
         }
 
         /**
-         * Returns the RemoveViews for the provided widget id if an update is pending
-         * for that widget.
+         * Adds all pending updates in {@param outUpdates} keys by the update time.
          */
-        public RemoteViews getPendingViewsForId(int appWidgetId) {
+        public boolean getPendingUpdatesForId(int appWidgetId,
+                LongSparseArray<PendingHostUpdate> outUpdates) {
             long updateTime = lastWidgetUpdateTime;
             int N = widgets.size();
             for (int i = 0; i < N; i++) {
                 Widget widget = widgets.get(i);
-                if (widget.appWidgetId == appWidgetId
-                        && widget.lastUpdateTime > updateTime) {
-                    return cloneIfLocalBinder(widget.getEffectiveViewsLocked());
+                if (widget.appWidgetId == appWidgetId) {
+                    outUpdates.clear();
+                    for (int j = widget.updateTimes.size() - 1; j >= 0; j--) {
+                        long time = widget.updateTimes.valueAt(j);
+                        if (time <= updateTime) {
+                            continue;
+                        }
+                        int id = widget.updateTimes.keyAt(j);
+                        final PendingHostUpdate update;
+                        switch (id) {
+                            case ID_PROVIDER_CHANGED:
+                                update = PendingHostUpdate.providerChanged(
+                                        appWidgetId, widget.provider.info);
+                                break;
+                            case ID_VIEWS_UPDATE:
+                                update = PendingHostUpdate.updateAppWidget(appWidgetId,
+                                        cloneIfLocalBinder(widget.getEffectiveViewsLocked()));
+                                break;
+                            default:
+                                update = PendingHostUpdate.viewDataChanged(appWidgetId, id);
+                        }
+                        outUpdates.put(time, update);
+                    }
+                    return true;
                 }
             }
-            return null;
+            return false;
         }
 
         @Override
@@ -3856,6 +3904,10 @@
         }
     }
 
+    // These can be any constants that would not collide with a resource id.
+    private static final int ID_VIEWS_UPDATE = 0;
+    private static final int ID_PROVIDER_CHANGED = 1;
+
     private static final class Widget {
         int appWidgetId;
         int restoredId;  // tracking & remapping any restored state
@@ -3864,7 +3916,8 @@
         RemoteViews maskedViews;
         Bundle options;
         Host host;
-        long lastUpdateTime;
+        // timestamps for various operations
+        SparseLongArray updateTimes = new SparseLongArray(2);
 
         @Override
         public String toString() {
diff --git a/services/backup/java/com/android/server/backup/BackupManagerService.java b/services/backup/java/com/android/server/backup/BackupManagerService.java
index e6f99c1..497eac9 100644
--- a/services/backup/java/com/android/server/backup/BackupManagerService.java
+++ b/services/backup/java/com/android/server/backup/BackupManagerService.java
@@ -234,6 +234,7 @@
     private static final int MSG_WIDGET_BROADCAST = 13;
     private static final int MSG_RUN_FULL_TRANSPORT_BACKUP = 14;
     private static final int MSG_REQUEST_BACKUP = 15;
+    private static final int MSG_SCHEDULE_BACKUP_PACKAGE = 16;
 
     // backup task state machine tick
     static final int MSG_BACKUP_RESTORE_STEP = 20;
@@ -796,7 +797,7 @@
                                 queue, oldJournal, null, null, false);
                         Message pbtMessage = obtainMessage(MSG_BACKUP_RESTORE_STEP, pbt);
                         sendMessage(pbtMessage);
-                    } catch (RemoteException e) {
+                    } catch (Exception e) {
                         // unable to ask the transport its dir name -- transient failure, since
                         // the above check succeeded.  Try again next time.
                         Slog.e(TAG, "Transport became unavailable attempting backup");
@@ -939,7 +940,7 @@
                     }
                     if (sets == null) EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
                 } catch (Exception e) {
-                    Slog.e(TAG, "Error from transport getting set list");
+                    Slog.e(TAG, "Error from transport getting set list: " + e.getMessage());
                 } finally {
                     if (params.observer != null) {
                         try {
@@ -947,7 +948,7 @@
                         } catch (RemoteException re) {
                             Slog.e(TAG, "Unable to report listing to observer");
                         } catch (Exception e) {
-                            Slog.e(TAG, "Restore observer threw", e);
+                            Slog.e(TAG, "Restore observer threw: " + e.getMessage());
                         }
                     }
 
@@ -1037,6 +1038,16 @@
                 sendMessage(pbtMessage);
                 break;
             }
+
+            case MSG_SCHEDULE_BACKUP_PACKAGE:
+            {
+                String pkgName = (String)msg.obj;
+                if (MORE_DEBUG) {
+                    Slog.d(TAG, "MSG_SCHEDULE_BACKUP_PACKAGE " + pkgName);
+                }
+                dataChangedImpl(pkgName);
+                break;
+            }
             }
         }
     }
@@ -1216,7 +1227,7 @@
 
         // Now that we know about valid backup participants, parse any
         // leftover journal files into the pending backup set
-        parseLeftoverJournals();
+        mBackupHandler.post(() -> parseLeftoverJournals());
 
         // Power management
         mWakelock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*backup*");
@@ -1759,8 +1770,10 @@
                 }
                 return; // done; don't fall through to the error case
             }
-        } catch (RemoteException e) {
+        } catch (Exception e) {
             // transport threw when asked its name; fall through to the lookup-failed case
+            Slog.e(TAG, "Transport " + transportName + " failed to report name: "
+                    + e.getMessage());
         }
 
         // The named transport doesn't exist or threw.  This operation is
@@ -1848,7 +1861,7 @@
                             System.currentTimeMillis() + delay, mRunInitIntent);
                 }
             }
-        } catch (RemoteException e) {
+        } catch (Exception e) {
             // the transport threw when asked its file naming prefs; declare it invalid
             Slog.e(TAG, "Unable to register transport as " + name);
             mTransportNames.remove(component);
@@ -2054,8 +2067,9 @@
                 IBackupTransport transport = IBackupTransport.Stub.asInterface(service);
                 registerTransport(transport.name(), name, transport);
                 EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, name, 1);
-            } catch (RemoteException e) {
-                Slog.e(TAG, "Unable to register transport " + component);
+            } catch (Exception e) {
+                Slog.e(TAG, "Unable to register transport " + component
+                        + ": " + e.getMessage());
                 EventLog.writeEvent(EventLogTags.BACKUP_TRANSPORT_LIFECYCLE, name, 0);
             }
         }
@@ -2161,7 +2175,7 @@
                 int uid = pkg.applicationInfo.uid;
                 HashSet<String> set = mBackupParticipants.get(uid);
                 if (set == null) {
-                    set = new HashSet<String>();
+                    set = new HashSet<>();
                     mBackupParticipants.put(uid, set);
                 }
                 set.add(pkg.packageName);
@@ -2169,7 +2183,9 @@
 
                 // Schedule a backup for it on general principles
                 if (MORE_DEBUG) Slog.i(TAG, "Scheduling backup for new app " + pkg.packageName);
-                dataChangedImpl(pkg.packageName);
+                Message msg = mBackupHandler
+                        .obtainMessage(MSG_SCHEDULE_BACKUP_PACKAGE, pkg.packageName);
+                mBackupHandler.sendMessage(msg);
             }
         }
     }
@@ -2516,8 +2532,8 @@
         String dirName;
         try {
             dirName = transport.transportDirName();
-        } catch (RemoteException e) {
-            Slog.e(TAG, "Transport became unavailable while attempting backup");
+        } catch (Exception e) {
+            Slog.e(TAG, "Transport unavailable while attempting backup: " + e.getMessage());
             sendBackupFinished(observer, BackupManager.ERROR_TRANSPORT_ABORTED);
             return BackupManager.ERROR_TRANSPORT_ABORTED;
         }
@@ -2961,9 +2977,10 @@
                 try {
                     mCurrentToken = mTransport.getCurrentRestoreSet();
                     writeRestoreTokens();
-                } catch (RemoteException e) {
+                } catch (Exception e) {
                     // nothing for it at this point, unfortunately, but this will be
                     // recorded the next time we fully succeed.
+                    Slog.e(TAG, "Transport threw reporting restore set: " + e.getMessage());
                     addBackupTrace("transport threw returning token");
                 }
             }
@@ -2988,7 +3005,7 @@
                             }
                         }
                     } catch (Exception e) {
-                        Slog.w(TAG, "Failed to query transport name heading for init", e);
+                        Slog.w(TAG, "Failed to query transport name for init: " + e.getMessage());
                         // swallow it and proceed; we don't rely on this
                     }
                     clearMetadata();
@@ -3354,8 +3371,8 @@
                     try {
                         long quota = mTransport.getBackupQuota(mCurrentPackage.packageName, false);
                         mAgentBinder.doQuotaExceeded(size, quota);
-                    } catch (RemoteException e) {
-                        Slog.e(TAG, "Unable to contact backup agent for quota exceeded");
+                    } catch (Exception e) {
+                        Slog.e(TAG, "Unable to notify about quota exceeded: " + e.getMessage());
                     }
                 }
                 nextState = (mQueue.isEmpty()) ? BackupState.FINAL : BackupState.RUNNING_QUEUE;
@@ -3393,7 +3410,7 @@
             try {
                 delay = mTransport.requestBackupTime();
             } catch (Exception e) {
-                Slog.w(TAG, "Unable to contact transport for recommended backoff");
+                Slog.w(TAG, "Unable to contact transport for recommended backoff: " + e.getMessage());
                 delay = 0;  // use the scheduler's default
             }
             KeyValueBackupJob.schedule(mContext, delay);
@@ -4311,7 +4328,10 @@
                 Slog.e(TAG, "Internal exception during full backup", e);
             } finally {
                 try {
-                    if (out != null) out.close();
+                    if (out != null) {
+                        out.flush();
+                        out.close();
+                    }
                     mOutputFile.close();
                 } catch (IOException e) {
                     /* nothing we can do about this */
@@ -4649,6 +4669,13 @@
                     }
                     cleanUpPipes(transportPipes);
                     cleanUpPipes(enginePipes);
+                    if (currentPackage.applicationInfo != null) {
+                        Slog.i(TAG, "Unbinding agent in " + packageName);
+                        addBackupTrace("unbinding " + packageName);
+                        try {
+                            mActivityManager.unbindBackupAgent(currentPackage.applicationInfo);
+                        } catch (RemoteException e) { /* can't happen; activity manager is local */ }
+                    }
                 }
             } catch (Exception e) {
                 backupRunStatus = BackupManager.ERROR_TRANSPORT_ABORTED;
@@ -4984,7 +5011,7 @@
                 return false;
             }
         } catch (Exception e) {
-            Slog.w(TAG, "Unable to contact transport");
+            Slog.w(TAG, "Unable to get transport name: " + e.getMessage());
             return false;
         }
 
@@ -5474,11 +5501,11 @@
 
                         // If the policy is satisfied, go ahead and set up to pipe the
                         // data to the agent.
-                        if (DEBUG && okay && mAgent != null) {
+                        if (MORE_DEBUG && okay && mAgent != null) {
                             Slog.i(TAG, "Reusing existing agent instance");
                         }
                         if (okay && mAgent == null) {
-                            if (DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg);
+                            if (MORE_DEBUG) Slog.d(TAG, "Need to launch agent for " + pkg);
 
                             try {
                                 mTargetApp = mPackageManager.getApplicationInfo(pkg, 0);
@@ -5680,16 +5707,21 @@
         }
 
         void tearDownPipes() {
-            if (mPipes != null) {
-                try {
-                    mPipes[0].close();
-                    mPipes[0] = null;
-                    mPipes[1].close();
-                    mPipes[1] = null;
-                } catch (IOException e) {
-                    Slog.w(TAG, "Couldn't close agent pipes", e);
+            // Teardown might arise from the inline restore processing or from the asynchronous
+            // timeout mechanism, and these might race.  Make sure we don't try to close and
+            // null out the pipes twice.
+            synchronized (this) {
+                if (mPipes != null) {
+                    try {
+                        mPipes[0].close();
+                        mPipes[0] = null;
+                        mPipes[1].close();
+                        mPipes[1] = null;
+                    } catch (IOException e) {
+                        Slog.w(TAG, "Couldn't close agent pipes", e);
+                    }
+                    mPipes = null;
                 }
-                mPipes = null;
             }
         }
 
@@ -8208,9 +8240,9 @@
                 // Success; cache the metadata and continue as expected with the
                 // next state already enqueued
 
-            } catch (RemoteException e) {
+            } catch (Exception e) {
                 // If we lost the transport at any time, halt
-                Slog.e(TAG, "Unable to contact transport for restore");
+                Slog.e(TAG, "Unable to contact transport for restore: " + e.getMessage());
                 mStatus = BackupTransport.TRANSPORT_ERROR;
                 mBackupHandler.removeMessages(MSG_BACKUP_RESTORE_STEP, this);
                 executeNextState(UnifiedRestoreState.FINAL);
@@ -8307,8 +8339,9 @@
                     nextState = UnifiedRestoreState.RUNNING_QUEUE;
                     return;
                 }
-            } catch (RemoteException e) {
-                Slog.e(TAG, "Can't get next target from transport; ending restore");
+            } catch (Exception e) {
+                Slog.e(TAG, "Can't get next restore target from transport; halting: "
+                        + e.getMessage());
                 EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
                 nextState = UnifiedRestoreState.FINAL;
                 return;
@@ -8618,11 +8651,11 @@
                     EventLog.writeEvent(EventLogTags.RESTORE_AGENT_FAILURE,
                             mCurrentPackage.packageName, "I/O error on pipes");
                     status = BackupTransport.AGENT_ERROR;
-                } catch (RemoteException e) {
-                    // The transport went away; terminate the whole operation.  Closing
+                } catch (Exception e) {
+                    // The transport threw; terminate the whole operation.  Closing
                     // the sockets will wake up the engine and it will then tidy up the
                     // remote end.
-                    Slog.e(TAG, "Transport failed during restore");
+                    Slog.e(TAG, "Transport failed during restore: " + e.getMessage());
                     EventLog.writeEvent(EventLogTags.RESTORE_TRANSPORT_FAILURE);
                     status = BackupTransport.TRANSPORT_ERROR;
                 } finally {
@@ -8660,9 +8693,10 @@
                         // level is immaterial; we need to tell the transport to bail
                         try {
                             mTransport.abortFullRestore();
-                        } catch (RemoteException e) {
+                        } catch (Exception e) {
                             // transport itself is dead; make sure we handle this as a
                             // fatal error
+                            Slog.e(TAG, "Transport threw from abortFullRestore: " + e.getMessage());
                             status = BackupTransport.TRANSPORT_ERROR;
                         }
 
@@ -9010,16 +9044,15 @@
                 // Tell the transport to remove all the persistent storage for the app
                 // TODO - need to handle failures
                 mTransport.clearBackupData(mPackage);
-            } catch (RemoteException e) {
-                // can't happen; the transport is local
             } catch (Exception e) {
-                Slog.e(TAG, "Transport threw attempting to clear data for " + mPackage);
+                Slog.e(TAG, "Transport threw clearing data for " + mPackage + ": " + e.getMessage());
             } finally {
                 try {
                     // TODO - need to handle failures
                     mTransport.finishBackup();
-                } catch (RemoteException e) {
-                    // can't happen; the transport is local
+                } catch (Exception e) {
+                    // Nothing we can do here, alas
+                    Slog.e(TAG, "Unable to mark clear operation finished: " + e.getMessage());
                 }
 
                 // Last but not least, release the cpu
@@ -9078,8 +9111,6 @@
                                 System.currentTimeMillis() + delay, mRunInitIntent);
                     }
                 }
-            } catch (RemoteException e) {
-                // can't happen; the transports are local
             } catch (Exception e) {
                 Slog.e(TAG, "Unexpected error performing init", e);
             } finally {
@@ -9767,8 +9798,9 @@
                     if (MORE_DEBUG) Slog.d(TAG, "getConfigurationIntent() returning config intent "
                             + intent);
                     return intent;
-                } catch (RemoteException e) {
+                } catch (Exception e) {
                     /* fall through to return null */
+                    Slog.e(TAG, "Unable to get configuration intent from transport: " + e.getMessage());
                 }
             }
         }
@@ -9792,8 +9824,9 @@
                     final String text = transport.currentDestinationString();
                     if (MORE_DEBUG) Slog.d(TAG, "getDestinationString() returning " + text);
                     return text;
-                } catch (RemoteException e) {
+                } catch (Exception e) {
                     /* fall through to return null */
+                    Slog.e(TAG, "Unable to get string from transport: " + e.getMessage());
                 }
             }
         }
@@ -9814,8 +9847,9 @@
                     if (MORE_DEBUG) Slog.d(TAG, "getDataManagementIntent() returning intent "
                             + intent);
                     return intent;
-                } catch (RemoteException e) {
+                } catch (Exception e) {
                     /* fall through to return null */
+                    Slog.e(TAG, "Unable to get management intent from transport: " + e.getMessage());
                 }
             }
         }
@@ -9836,8 +9870,9 @@
                     final String text = transport.dataManagementLabel();
                     if (MORE_DEBUG) Slog.d(TAG, "getDataManagementLabel() returning " + text);
                     return text;
-                } catch (RemoteException e) {
+                } catch (Exception e) {
                     /* fall through to return null */
+                    Slog.e(TAG, "Unable to get management label from transport: " + e.getMessage());
                 }
             }
         }
@@ -9930,9 +9965,9 @@
                 msg.obj = new RestoreParams(transport, dirName, null,
                         restoreSet, packageName, token);
                 mBackupHandler.sendMessage(msg);
-            } catch (RemoteException e) {
-                // Binding to the transport broke; back off and proceed with the installation.
-                Slog.e(TAG, "Unable to contact transport");
+            } catch (Exception e) {
+                // Calling into the transport broke; back off and proceed with the installation.
+                Slog.e(TAG, "Unable to contact transport: " + e.getMessage());
                 skip = true;
             }
         }
@@ -10053,8 +10088,8 @@
                 try {
                     return transport.isAppEligibleForBackup(packageInfo,
                         appGetsFullBackup(packageInfo));
-                } catch (RemoteException e) {
-                    Slog.e(TAG, "Unable to contact transport");
+                } catch (Exception e) {
+                    Slog.e(TAG, "Unable to ask about eligibility: " + e.getMessage());
                 }
             }
             // If transport is not present we couldn't tell that the package is not eligible.
@@ -10156,9 +10191,9 @@
             String dirName;
             try {
                 dirName = mRestoreTransport.transportDirName();
-            } catch (RemoteException e) {
+            } catch (Exception e) {
                 // Transport went AWOL; fail.
-                Slog.e(TAG, "Unable to contact transport for restore");
+                Slog.e(TAG, "Unable to get transport dir for restore: " + e.getMessage());
                 return -1;
             }
 
@@ -10238,9 +10273,9 @@
             String dirName;
             try {
                 dirName = mRestoreTransport.transportDirName();
-            } catch (RemoteException e) {
+            } catch (Exception e) {
                 // Transport went AWOL; fail.
-                Slog.e(TAG, "Unable to contact transport for restore");
+                Slog.e(TAG, "Unable to get transport name for restoreSome: " + e.getMessage());
                 return -1;
             }
 
@@ -10328,9 +10363,9 @@
                 String dirName;
                 try {
                     dirName = mRestoreTransport.transportDirName();
-                } catch (RemoteException e) {
+                } catch (Exception e) {
                     // Transport went AWOL; fail.
-                    Slog.e(TAG, "Unable to contact transport for restore");
+                    Slog.e(TAG, "Unable to get transport dir for restorePackage: " + e.getMessage());
                     return -1;
                 }
 
diff --git a/services/core/Android.mk b/services/core/Android.mk
index a5b1069..58f2074 100644
--- a/services/core/Android.mk
+++ b/services/core/Android.mk
@@ -8,6 +8,7 @@
 
 LOCAL_SRC_FILES += \
     $(call all-java-files-under,java) \
+    $(call all-proto-files-under, proto) \
     java/com/android/server/EventLogTags.logtags \
     java/com/android/server/am/EventLogTags.logtags \
     ../../../../system/netd/server/binder/android/net/INetd.aidl \
@@ -18,6 +19,7 @@
 
 LOCAL_JAVA_LIBRARIES := services.net telephony-common
 LOCAL_STATIC_JAVA_LIBRARIES := tzdata_update
+LOCAL_PROTOC_OPTIMIZE_TYPE := nano
 
 ifneq ($(INCREMENTAL_BUILDS),)
     LOCAL_PROGUARD_ENABLED := disabled
diff --git a/services/core/java/com/android/server/AlarmManagerService.java b/services/core/java/com/android/server/AlarmManagerService.java
index 3f59b02..20cca16 100644
--- a/services/core/java/com/android/server/AlarmManagerService.java
+++ b/services/core/java/com/android/server/AlarmManagerService.java
@@ -2472,8 +2472,10 @@
                             Slog.v(TAG, "Time changed notification from kernel; rebatching");
                         }
                         removeImpl(mTimeTickSender);
+                        removeImpl(mDateChangeSender);
                         rebatchAllAlarms();
                         mClockReceiver.scheduleTimeTickEvent();
+                        mClockReceiver.scheduleDateChangedEvent();
                         synchronized (mLock) {
                             mNumTimeChanged++;
                             mLastTimeChangeClockTime = nowRTC;
@@ -2552,7 +2554,9 @@
                 } else {
                     // Just in case -- even though no wakeup flag was set, make sure
                     // we have updated the kernel to the next alarm time.
-                    rescheduleKernelAlarmsLocked();
+                    synchronized (mLock) {
+                        rescheduleKernelAlarmsLocked();
+                    }
                 }
             }
         }
@@ -2695,7 +2699,7 @@
         public void scheduleDateChangedEvent() {
             Calendar calendar = Calendar.getInstance();
             calendar.setTimeInMillis(System.currentTimeMillis());
-            calendar.set(Calendar.HOUR, 0);
+            calendar.set(Calendar.HOUR_OF_DAY, 0);
             calendar.set(Calendar.MINUTE, 0);
             calendar.set(Calendar.SECOND, 0);
             calendar.set(Calendar.MILLISECOND, 0);
diff --git a/services/core/java/com/android/server/AnyMotionDetector.java b/services/core/java/com/android/server/AnyMotionDetector.java
index a8ae914d..d564925 100644
--- a/services/core/java/com/android/server/AnyMotionDetector.java
+++ b/services/core/java/com/android/server/AnyMotionDetector.java
@@ -70,6 +70,9 @@
     /** The interval between accelerometer orientation measurements. */
     private static final long ORIENTATION_MEASUREMENT_INTERVAL_MILLIS = 5000;
 
+    /** The maximum duration we will hold a wakelock to determine stationary status. */
+    private static final long WAKELOCK_TIMEOUT_MILLIS = 30000;
+
     /**
      * The duration in milliseconds after which an orientation measurement is considered
      * too stale to be used.
@@ -94,6 +97,15 @@
     /** True if an orientation measurement is in progress. */
     private boolean mMeasurementInProgress;
 
+    /** True if sendMessageDelayed() for the mMeasurementTimeout callback has been scheduled */
+    private boolean mMeasurementTimeoutIsActive;
+
+    /** True if sendMessageDelayed() for the mWakelockTimeout callback has been scheduled */
+    private boolean mWakelockTimeoutIsActive;
+
+    /** True if sendMessageDelayed() for the mSensorRestart callback has been scheduled */
+    private boolean mSensorRestartIsActive;
+
     /** The most recent gravity vector. */
     private Vector3 mCurrentGravityVector = null;
 
@@ -115,6 +127,9 @@
             mSensorManager = sm;
             mAccelSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
             mMeasurementInProgress = false;
+            mMeasurementTimeoutIsActive = false;
+            mWakelockTimeoutIsActive = false;
+            mSensorRestartIsActive = false;
             mState = STATE_INACTIVE;
             mCallback = callback;
             mThresholdAngle = thresholdAngle;
@@ -141,24 +156,33 @@
                 mCurrentGravityVector = null;
                 mPreviousGravityVector = null;
                 mWakeLock.acquire();
+                Message wakelockTimeoutMsg = Message.obtain(mHandler, mWakelockTimeout);
+                mHandler.sendMessageDelayed(wakelockTimeoutMsg, WAKELOCK_TIMEOUT_MILLIS);
+                mWakelockTimeoutIsActive = true;
                 startOrientationMeasurementLocked();
             }
         }
     }
 
     public void stop() {
-        if (mState == STATE_ACTIVE) {
-            synchronized (mLock) {
+        synchronized (mLock) {
+            if (mState == STATE_ACTIVE) {
                 mState = STATE_INACTIVE;
                 if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE.");
-                if (mMeasurementInProgress) {
-                    mMeasurementInProgress = false;
-                    mSensorManager.unregisterListener(mListener);
-                }
-                mHandler.removeCallbacks(mMeasurementTimeout);
-                mHandler.removeCallbacks(mSensorRestart);
-                mCurrentGravityVector = null;
-                mPreviousGravityVector = null;
+            }
+            mHandler.removeCallbacks(mMeasurementTimeout);
+            mHandler.removeCallbacks(mSensorRestart);
+            mMeasurementTimeoutIsActive = false;
+            mSensorRestartIsActive = false;
+            if (mMeasurementInProgress) {
+                mMeasurementInProgress = false;
+                mSensorManager.unregisterListener(mListener);
+            }
+            mCurrentGravityVector = null;
+            mPreviousGravityVector = null;
+            if (mWakeLock.isHeld()) {
+                mHandler.removeCallbacks(mWakelockTimeout);
+                mWakelockTimeoutIsActive = false;
                 mWakeLock.release();
             }
         }
@@ -173,9 +197,9 @@
                 mMeasurementInProgress = true;
                 mRunningStats.reset();
             }
-            Message msg = Message.obtain(mHandler, mMeasurementTimeout);
-            msg.setAsynchronous(true);
-            mHandler.sendMessageDelayed(msg, ACCELEROMETER_DATA_TIMEOUT_MILLIS);
+            Message measurementTimeoutMsg = Message.obtain(mHandler, mMeasurementTimeout);
+            mHandler.sendMessageDelayed(measurementTimeoutMsg, ACCELEROMETER_DATA_TIMEOUT_MILLIS);
+            mMeasurementTimeoutIsActive = true;
         }
     }
 
@@ -184,12 +208,15 @@
                 mMeasurementInProgress);
         int status = RESULT_UNKNOWN;
         if (mMeasurementInProgress) {
-            mSensorManager.unregisterListener(mListener);
             mHandler.removeCallbacks(mMeasurementTimeout);
-            long detectionEndTime = SystemClock.elapsedRealtime();
+            mMeasurementTimeoutIsActive = false;
+            mSensorManager.unregisterListener(mListener);
             mMeasurementInProgress = false;
             mPreviousGravityVector = mCurrentGravityVector;
             mCurrentGravityVector = mRunningStats.getRunningAverage();
+            if (mRunningStats.getSampleCount() == 0) {
+                Slog.w(TAG, "No accelerometer data acquired for orientation measurement.");
+            }
             if (DEBUG) {
                 Slog.d(TAG, "mRunningStats = " + mRunningStats.toString());
                 String currentGravityVectorString = (mCurrentGravityVector == null) ?
@@ -203,7 +230,11 @@
             status = getStationaryStatus();
             if (DEBUG) Slog.d(TAG, "getStationaryStatus() returned " + status);
             if (status != RESULT_UNKNOWN) {
-                mWakeLock.release();
+                if (mWakeLock.isHeld()) {
+                    mHandler.removeCallbacks(mWakelockTimeout);
+                    mWakelockTimeoutIsActive = false;
+                    mWakeLock.release();
+                }
                 if (DEBUG) {
                     Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE. status = " + status);
                 }
@@ -217,8 +248,8 @@
                         " scheduled in " + ORIENTATION_MEASUREMENT_INTERVAL_MILLIS +
                         " milliseconds.");
                 Message msg = Message.obtain(mHandler, mSensorRestart);
-                msg.setAsynchronous(true);
                 mHandler.sendMessageDelayed(msg, ORIENTATION_MEASUREMENT_INTERVAL_MILLIS);
+                mSensorRestartIsActive = true;
             }
         }
         return status;
@@ -271,6 +302,8 @@
                 }
             }
             if (status != RESULT_UNKNOWN) {
+                mHandler.removeCallbacks(mWakelockTimeout);
+                mWakelockTimeoutIsActive = false;
                 mCallback.onAnyMotionResult(status);
             }
         }
@@ -284,26 +317,46 @@
         @Override
         public void run() {
             synchronized (mLock) {
-                startOrientationMeasurementLocked();
+                if (mSensorRestartIsActive == true) {
+                    mSensorRestartIsActive = false;
+                    startOrientationMeasurementLocked();
+                }
             }
         }
     };
 
     private final Runnable mMeasurementTimeout = new Runnable() {
-      @Override
-      public void run() {
-          int status = RESULT_UNKNOWN;
-          synchronized (mLock) {
-              if (DEBUG) Slog.i(TAG, "mMeasurementTimeout. Failed to collect sufficient accel " +
-                      "data within " + ACCELEROMETER_DATA_TIMEOUT_MILLIS + " ms. Stopping " +
-                      "orientation measurement.");
-              status = stopOrientationMeasurementLocked();
-          }
-          if (status != RESULT_UNKNOWN) {
-              mCallback.onAnyMotionResult(status);
-          }
-      }
-  };
+        @Override
+        public void run() {
+            int status = RESULT_UNKNOWN;
+            synchronized (mLock) {
+                if (mMeasurementTimeoutIsActive == true) {
+                    mMeasurementTimeoutIsActive = false;
+                    if (DEBUG) Slog.i(TAG, "mMeasurementTimeout. Failed to collect sufficient accel " +
+                          "data within " + ACCELEROMETER_DATA_TIMEOUT_MILLIS + " ms. Stopping " +
+                          "orientation measurement.");
+                    status = stopOrientationMeasurementLocked();
+                    if (status != RESULT_UNKNOWN) {
+                        mHandler.removeCallbacks(mWakelockTimeout);
+                        mWakelockTimeoutIsActive = false;
+                        mCallback.onAnyMotionResult(status);
+                    }
+                }
+            }
+        }
+    };
+
+    private final Runnable mWakelockTimeout = new Runnable() {
+        @Override
+        public void run() {
+            synchronized (mLock) {
+                if (mWakelockTimeoutIsActive == true) {
+                    mWakelockTimeoutIsActive = false;
+                    stop();
+                }
+            }
+        }
+    };
 
     /**
      * A timestamped three dimensional vector and some vector operations.
diff --git a/services/core/java/com/android/server/AppOpsService.java b/services/core/java/com/android/server/AppOpsService.java
index b5b0cd8..4caeba8 100644
--- a/services/core/java/com/android/server/AppOpsService.java
+++ b/services/core/java/com/android/server/AppOpsService.java
@@ -875,7 +875,7 @@
             return AppOpsManager.MODE_IGNORED;
         }
         synchronized (this) {
-            if (isOpRestricted(uid, code, resolvedPackageName)) {
+            if (isOpRestrictedLocked(uid, code, resolvedPackageName)) {
                 return AppOpsManager.MODE_IGNORED;
             }
             code = AppOpsManager.opToSwitch(code);
@@ -1024,7 +1024,7 @@
                 return AppOpsManager.MODE_ERRORED;
             }
             Op op = getOpLocked(ops, code, true);
-            if (isOpRestricted(uid, code, packageName)) {
+            if (isOpRestrictedLocked(uid, code, packageName)) {
                 return AppOpsManager.MODE_IGNORED;
             }
             if (op.duration == -1) {
@@ -1082,7 +1082,7 @@
                 return AppOpsManager.MODE_ERRORED;
             }
             Op op = getOpLocked(ops, code, true);
-            if (isOpRestricted(uid, code, resolvedPackageName)) {
+            if (isOpRestrictedLocked(uid, code, resolvedPackageName)) {
                 return AppOpsManager.MODE_IGNORED;
             }
             final int switchCode = AppOpsManager.opToSwitch(code);
@@ -1308,7 +1308,7 @@
         return op;
     }
 
-    private boolean isOpRestricted(int uid, int code, String packageName) {
+    private boolean isOpRestrictedLocked(int uid, int code, String packageName) {
         int userHandle = UserHandle.getUserId(uid);
         final int restrictionSetCount = mOpUserRestrictions.size();
 
@@ -2210,25 +2210,33 @@
 
     private void setUserRestrictionNoCheck(int code, boolean restricted, IBinder token,
             int userHandle, String[] exceptionPackages) {
-        ClientRestrictionState restrictionState = mOpUserRestrictions.get(token);
+        boolean notifyChange = false;
 
-        if (restrictionState == null) {
-            try {
-                restrictionState = new ClientRestrictionState(token);
-            } catch (RemoteException e) {
-                return;
+        synchronized (AppOpsService.this) {
+            ClientRestrictionState restrictionState = mOpUserRestrictions.get(token);
+
+            if (restrictionState == null) {
+                try {
+                    restrictionState = new ClientRestrictionState(token);
+                } catch (RemoteException e) {
+                    return;
+                }
+                mOpUserRestrictions.put(token, restrictionState);
             }
-            mOpUserRestrictions.put(token, restrictionState);
+
+            if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) {
+                notifyChange = true;
+            }
+
+            if (restrictionState.isDefault()) {
+                mOpUserRestrictions.remove(token);
+                restrictionState.destroy();
+            }
         }
 
-        if (restrictionState.setRestriction(code, restricted, exceptionPackages, userHandle)) {
+        if (notifyChange) {
             notifyWatchersOfChange(code);
         }
-
-        if (restrictionState.isDefault()) {
-            mOpUserRestrictions.remove(token);
-            restrictionState.destroy();
-        }
     }
 
     private void notifyWatchersOfChange(int code) {
@@ -2263,10 +2271,12 @@
     @Override
     public void removeUser(int userHandle) throws RemoteException {
         checkSystemUid("removeUser");
-        final int tokenCount = mOpUserRestrictions.size();
-        for (int i = tokenCount - 1; i >= 0; i--) {
-            ClientRestrictionState opRestrictions = mOpUserRestrictions.valueAt(i);
-            opRestrictions.removeUser(userHandle);
+        synchronized (AppOpsService.this) {
+            final int tokenCount = mOpUserRestrictions.size();
+            for (int i = tokenCount - 1; i >= 0; i--) {
+                ClientRestrictionState opRestrictions = mOpUserRestrictions.valueAt(i);
+                opRestrictions.removeUser(userHandle);
+            }
         }
     }
 
diff --git a/services/core/java/com/android/server/AttributeCache.java b/services/core/java/com/android/server/AttributeCache.java
index 57f18c0..58ec836 100644
--- a/services/core/java/com/android/server/AttributeCache.java
+++ b/services/core/java/com/android/server/AttributeCache.java
@@ -25,23 +25,24 @@
 import android.content.res.TypedArray;
 import android.os.UserHandle;
 import android.util.ArrayMap;
+import android.util.LruCache;
 import android.util.SparseArray;
 
 import com.android.internal.annotations.GuardedBy;
 
-import java.lang.ref.WeakReference;
-
 /**
  * TODO: This should be better integrated into the system so it doesn't need
  * special calls from the activity manager to clear it.
  */
 public final class AttributeCache {
+    private static final int CACHE_SIZE = 4;
     private static AttributeCache sInstance = null;
 
     private final Context mContext;
 
     @GuardedBy("this")
-    private final ArrayMap<String, WeakReference<Package>> mPackages = new ArrayMap<>();
+    private final LruCache<String, Package> mPackages = new LruCache<>(CACHE_SIZE);
+
     @GuardedBy("this")
     private final Configuration mConfiguration = new Configuration();
 
@@ -86,15 +87,12 @@
 
     public void removePackage(String packageName) {
         synchronized (this) {
-            final WeakReference<Package> ref = mPackages.remove(packageName);
-            final Package pkg = (ref != null) ? ref.get() : null;
+            final Package pkg = mPackages.remove(packageName);
             if (pkg != null) {
-                if (pkg.mMap != null) {
-                    for (int i = 0; i < pkg.mMap.size(); i++) {
-                        final ArrayMap<int[], Entry> map = pkg.mMap.valueAt(i);
-                        for (int j = 0; j < map.size(); j++) {
-                            map.valueAt(j).recycle();
-                        }
+                for (int i = 0; i < pkg.mMap.size(); i++) {
+                    final ArrayMap<int[], Entry> map = pkg.mMap.valueAt(i);
+                    for (int j = 0; j < map.size(); j++) {
+                        map.valueAt(j).recycle();
                     }
                 }
 
@@ -113,15 +111,14 @@
                 // The configurations being masked out are ones that commonly
                 // change so we don't want flushing the cache... all others
                 // will flush the cache.
-                mPackages.clear();
+                mPackages.evictAll();
             }
         }
     }
     
     public Entry get(String packageName, int resId, int[] styleable, int userId) {
         synchronized (this) {
-            WeakReference<Package> ref = mPackages.get(packageName);
-            Package pkg = (ref != null) ? ref.get() : null;
+            Package pkg = mPackages.get(packageName);
             ArrayMap<int[], Entry> map = null;
             Entry ent = null;
             if (pkg != null) {
@@ -144,7 +141,7 @@
                     return null;
                 }
                 pkg = new Package(context);
-                mPackages.put(packageName, new WeakReference<>(pkg));
+                mPackages.put(packageName, pkg);
             }
             
             if (map == null) {
diff --git a/services/core/java/com/android/server/BluetoothManagerService.java b/services/core/java/com/android/server/BluetoothManagerService.java
index 6575a2a..0f8db2e 100644
--- a/services/core/java/com/android/server/BluetoothManagerService.java
+++ b/services/core/java/com/android/server/BluetoothManagerService.java
@@ -293,7 +293,7 @@
      */
     private final boolean isBluetoothPersistedStateOn() {
         return Settings.Global.getInt(mContentResolver,
-                Settings.Global.BLUETOOTH_ON, 0) != BLUETOOTH_OFF;
+                Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) != BLUETOOTH_OFF;
     }
 
     /**
@@ -301,7 +301,7 @@
      */
     private final boolean isBluetoothPersistedStateOnBluetooth() {
         return Settings.Global.getInt(mContentResolver,
-                Settings.Global.BLUETOOTH_ON, 0) == BLUETOOTH_ON_BLUETOOTH;
+                Settings.Global.BLUETOOTH_ON, BLUETOOTH_ON_BLUETOOTH) == BLUETOOTH_ON_BLUETOOTH;
     }
 
     /**
@@ -425,6 +425,24 @@
         return false;
     }
 
+    public int getState() {
+        if ((Binder.getCallingUid() != Process.SYSTEM_UID) &&
+                (!checkIfCallerIsForegroundUser())) {
+            Slog.w(TAG, "getState(): not allowed for non-active and non system user");
+            return BluetoothAdapter.STATE_OFF;
+        }
+
+        try {
+            mBluetoothLock.readLock().lock();
+            if (mBluetooth != null) return mBluetooth.getState();
+        } catch (RemoteException e) {
+            Slog.e(TAG, "getState()", e);
+        } finally {
+            mBluetoothLock.readLock().unlock();
+        }
+        return BluetoothAdapter.STATE_OFF;
+    }
+
     class ClientDeathRecipient implements IBinder.DeathRecipient {
         public void binderDied() {
             if (DBG) Slog.d(TAG, "Binder is dead -  unregister Ble App");
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index c3e9c3e..5f59e32 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -24,6 +24,7 @@
 import static android.net.ConnectivityManager.getNetworkTypeName;
 import static android.net.ConnectivityManager.isNetworkTypeValid;
 import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL;
+import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND;
 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
@@ -38,7 +39,6 @@
 
 import android.annotation.Nullable;
 import android.app.BroadcastOptions;
-import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
@@ -76,7 +76,9 @@
 import android.net.UidRange;
 import android.net.Uri;
 import android.net.metrics.DefaultNetworkEvent;
+import android.net.metrics.IpConnectivityLog;
 import android.net.metrics.NetworkEvent;
+import android.net.util.AvoidBadWifiTracker;
 import android.os.Binder;
 import android.os.Build;
 import android.os.Bundle;
@@ -93,6 +95,7 @@
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.ResultReceiver;
+import android.os.SystemClock;
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -124,14 +127,18 @@
 import com.android.internal.util.AsyncChannel;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.internal.util.MessageUtils;
+import com.android.internal.util.WakeupMessage;
 import com.android.internal.util.XmlUtils;
 import com.android.server.am.BatteryStatsService;
 import com.android.server.connectivity.DataConnectionStats;
 import com.android.server.connectivity.KeepaliveTracker;
 import com.android.server.connectivity.Nat464Xlat;
+import com.android.server.connectivity.LingerMonitor;
 import com.android.server.connectivity.NetworkAgentInfo;
 import com.android.server.connectivity.NetworkDiagnostics;
 import com.android.server.connectivity.NetworkMonitor;
+import com.android.server.connectivity.NetworkNotificationManager;
+import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
 import com.android.server.connectivity.PacManager;
 import com.android.server.connectivity.PermissionMonitor;
 import com.android.server.connectivity.Tethering;
@@ -170,7 +177,7 @@
  */
 public class ConnectivityService extends IConnectivityManager.Stub
         implements PendingIntent.OnFinished {
-    private static final String TAG = "ConnectivityService";
+    private static final String TAG = ConnectivityService.class.getSimpleName();
 
     private static final boolean DBG = true;
     private static final boolean VDBG = false;
@@ -190,6 +197,12 @@
     // connect anyway?" dialog after the user selects a network that doesn't validate.
     private static final int PROMPT_UNVALIDATED_DELAY_MS = 8 * 1000;
 
+    // Default to 30s linger time-out. Modifiable only for testing.
+    private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger";
+    private static final int DEFAULT_LINGER_DELAY_MS = 30_000;
+    @VisibleForTesting
+    protected int mLingerDelayMs;  // Can't be final, or test subclass constructors can't change it.
+
     // How long to delay to removal of a pending intent based request.
     // See Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS
     private final int mReleasePendingIntentDelayMs;
@@ -238,7 +251,8 @@
     private static final int DISABLED = 0;
 
     private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
-            new Class[] { AsyncChannel.class, ConnectivityService.class, NetworkAgent.class });
+            new Class[] { AsyncChannel.class, ConnectivityService.class, NetworkAgent.class,
+                    NetworkAgentInfo.class });
 
     private enum ReapUnvalidatedNetworks {
         // Tear down networks that have no chance (e.g. even if validated) of becoming
@@ -250,6 +264,11 @@
         DONT_REAP
     };
 
+    private enum UnneededFor {
+        LINGER,    // Determine whether this network is unneeded and should be lingered.
+        TEARDOWN,  // Determine whether this network is unneeded and should be torn down.
+    }
+
     /**
      * used internally to change our mobile data enabled flag
      */
@@ -293,7 +312,7 @@
 
     /**
      * indicates a timeout period is over - check if we had a network yet or not
-     * and if not, call the timeout calback (but leave the request live until they
+     * and if not, call the timeout callback (but leave the request live until they
      * cancel it.
      * includes a NetworkRequestInfo
      */
@@ -354,6 +373,11 @@
     private static final int EVENT_SET_ACCEPT_UNVALIDATED = 28;
 
     /**
+     * used to specify whether a network should not be penalized when it becomes unvalidated.
+     */
+    private static final int EVENT_SET_AVOID_UNVALIDATED = 35;
+
+    /**
      * used to ask the user to confirm a connection to an unvalidated network.
      * obj  = network
      */
@@ -370,6 +394,16 @@
      */
     private static final int EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT = 31;
 
+    /**
+     * Indicates a caller has requested to have its callback invoked with
+     * the latest LinkProperties or NetworkCapabilities.
+     *
+     * arg1 = UID of caller
+     * obj  = NetworkRequest
+     */
+    private static final int EVENT_REQUEST_LINKPROPERTIES  = 32;
+    private static final int EVENT_REQUEST_NETCAPABILITIES = 33;
+
     /** Handler thread used for both of the handlers below. */
     @VisibleForTesting
     protected final HandlerThread mHandlerThread;
@@ -416,6 +450,8 @@
     TelephonyManager mTelephonyManager;
 
     private KeepaliveTracker mKeepaliveTracker;
+    private NetworkNotificationManager mNotifier;
+    private LingerMonitor mLingerMonitor;
 
     // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp
     private final static int MIN_NET_ID = 100; // some reserved marks
@@ -429,6 +465,11 @@
     private static final int MAX_NETWORK_REQUEST_LOGS = 20;
     private final LocalLog mNetworkRequestInfoLogs = new LocalLog(MAX_NETWORK_REQUEST_LOGS);
 
+    // NetworkInfo blocked and unblocked String log entries
+    // TODO: consider reducing memory usage. Each log line is ~40 2B chars, for a total of ~8kB.
+    private static final int MAX_NETWORK_INFO_LOGS = 100;
+    private final LocalLog mNetworkInfoBlockingLogs = new LocalLog(MAX_NETWORK_INFO_LOGS);
+
     // Array of <Network,ReadOnlyLocalLogs> tracking network validation and results
     private static final int MAX_VALIDATION_LOGS = 10;
     private static class ValidationLog {
@@ -454,6 +495,11 @@
         }
     }
 
+    private final IpConnectivityLog mMetricsLog;
+
+    @VisibleForTesting
+    final AvoidBadWifiTracker mAvoidBadWifiTracker;
+
     /**
      * Implements support for the legacy "one network per network type" model.
      *
@@ -650,16 +696,23 @@
 
     public ConnectivityService(Context context, INetworkManagementService netManager,
             INetworkStatsService statsService, INetworkPolicyManager policyManager) {
+        this(context, netManager, statsService, policyManager, new IpConnectivityLog());
+    }
+
+    @VisibleForTesting
+    protected ConnectivityService(Context context, INetworkManagementService netManager,
+            INetworkStatsService statsService, INetworkPolicyManager policyManager,
+            IpConnectivityLog logger) {
         if (DBG) log("ConnectivityService starting up");
 
-        mDefaultRequest = createInternetRequestForTransport(-1);
-        NetworkRequestInfo defaultNRI = new NetworkRequestInfo(null, mDefaultRequest,
-                new Binder(), NetworkRequestType.REQUEST);
+        mMetricsLog = logger;
+        mDefaultRequest = createInternetRequestForTransport(-1, NetworkRequest.Type.REQUEST);
+        NetworkRequestInfo defaultNRI = new NetworkRequestInfo(null, mDefaultRequest, new Binder());
         mNetworkRequests.put(mDefaultRequest, defaultNRI);
         mNetworkRequestInfoLogs.log("REGISTER " + defaultNRI);
 
         mDefaultMobileDataRequest = createInternetRequestForTransport(
-                NetworkCapabilities.TRANSPORT_CELLULAR);
+                NetworkCapabilities.TRANSPORT_CELLULAR, NetworkRequest.Type.BACKGROUND_REQUEST);
 
         mHandlerThread = createHandlerThread();
         mHandlerThread.start();
@@ -679,6 +732,8 @@
         mReleasePendingIntentDelayMs = Settings.Secure.getInt(context.getContentResolver(),
                 Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS, 5_000);
 
+        mLingerDelayMs = SystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS);
+
         mContext = checkNotNull(context, "missing Context");
         mNetd = checkNotNull(netManager, "missing INetworkManagementService");
         mStatsService = checkNotNull(statsService, "missing INetworkStatsService");
@@ -796,16 +851,41 @@
         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
 
         mKeepaliveTracker = new KeepaliveTracker(mHandler);
+        mNotifier = new NetworkNotificationManager(mContext, mTelephonyManager,
+                mContext.getSystemService(NotificationManager.class));
+
+        final int dailyLimit = Settings.Global.getInt(mContext.getContentResolver(),
+                Settings.Global.NETWORK_SWITCH_NOTIFICATION_DAILY_LIMIT,
+                LingerMonitor.DEFAULT_NOTIFICATION_DAILY_LIMIT);
+        final long rateLimit = Settings.Global.getLong(mContext.getContentResolver(),
+                Settings.Global.NETWORK_SWITCH_NOTIFICATION_RATE_LIMIT_MILLIS,
+                LingerMonitor.DEFAULT_NOTIFICATION_RATE_LIMIT_MILLIS);
+        mLingerMonitor = new LingerMonitor(mContext, mNotifier, dailyLimit, rateLimit);
+
+        mAvoidBadWifiTracker = createAvoidBadWifiTracker(
+                mContext, mHandler, () -> rematchForAvoidBadWifiUpdate());
     }
 
-    private NetworkRequest createInternetRequestForTransport(int transportType) {
+    private NetworkRequest createInternetRequestForTransport(
+            int transportType, NetworkRequest.Type type) {
         NetworkCapabilities netCap = new NetworkCapabilities();
         netCap.addCapability(NET_CAPABILITY_INTERNET);
         netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED);
         if (transportType > -1) {
             netCap.addTransportType(transportType);
         }
-        return new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId());
+        return new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId(), type);
+    }
+
+    // Used only for testing.
+    // TODO: Delete this and either:
+    // 1. Give Fake SettingsProvider the ability to send settings change notifications (requires
+    //    changing ContentResolver to make registerContentObserver non-final).
+    // 2. Give FakeSettingsProvider an alternative notification mechanism and have the test use it
+    //    by subclassing SettingsObserver.
+    @VisibleForTesting
+    void updateMobileDataAlwaysOn() {
+        mHandler.sendEmptyMessage(EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON);
     }
 
     private void handleMobileDataAlwaysOn() {
@@ -818,7 +898,7 @@
 
         if (enable) {
             handleRegisterNetworkRequest(new NetworkRequestInfo(
-                    null, mDefaultMobileDataRequest, new Binder(), NetworkRequestType.REQUEST));
+                    null, mDefaultMobileDataRequest, new Binder()));
         } else {
             handleReleaseNetworkRequest(mDefaultMobileDataRequest, Process.SYSTEM_UID);
         }
@@ -978,7 +1058,9 @@
     }
 
     private void maybeLogBlockedNetworkInfo(NetworkInfo ni, int uid) {
-        if (ni == null || !LOGD_BLOCKED_NETWORKINFO) return;
+        if (ni == null || !LOGD_BLOCKED_NETWORKINFO) {
+            return;
+        }
         boolean removed = false;
         boolean added = false;
         synchronized (mBlockedAppUids) {
@@ -988,8 +1070,13 @@
                 removed = true;
             }
         }
-        if (added) log("Returning blocked NetworkInfo to uid=" + uid);
-        else if (removed) log("Returning unblocked NetworkInfo to uid=" + uid);
+        if (added) {
+            log("Returning blocked NetworkInfo to uid=" + uid);
+            mNetworkInfoBlockingLogs.log("BLOCKED " + uid);
+        } else if (removed) {
+            log("Returning unblocked NetworkInfo to uid=" + uid);
+            mNetworkInfoBlockingLogs.log("UNBLOCKED " + uid);
+        }
     }
 
     /**
@@ -1069,6 +1156,7 @@
         return nai != null ? nai.network : null;
     }
 
+    // Public because it's used by mLockdownTracker.
     public NetworkInfo getActiveNetworkInfoUnfiltered() {
         enforceAccessPermission();
         final int uid = Binder.getCallingUid();
@@ -1324,6 +1412,7 @@
      * desired
      * @return {@code true} on success, {@code false} on failure
      */
+    @Override
     public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) {
         enforceChangePermission();
         if (mProtectedNetworks.contains(networkType)) {
@@ -1528,10 +1617,21 @@
                 "ConnectivityService");
     }
 
+    private void enforceConnectivityRestrictedNetworksPermission() {
+        try {
+            mContext.enforceCallingOrSelfPermission(
+                    android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS,
+                    "ConnectivityService");
+            return;
+        } catch (SecurityException e) { /* fallback to ConnectivityInternalPermission */ }
+        enforceConnectivityInternalPermission();
+    }
+
     private void enforceKeepalivePermission() {
         mContext.enforceCallingOrSelfPermission(KeepaliveTracker.PERMISSION, "ConnectivityService");
     }
 
+    // Public because it's used by mLockdownTracker.
     public void sendConnectedBroadcast(NetworkInfo info) {
         enforceConnectivityInternalPermission();
         sendGeneralBroadcast(info, CONNECTIVITY_ACTION);
@@ -1888,15 +1988,20 @@
         for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
             pw.println(nai.toString());
             pw.increaseIndent();
-            pw.println("Requests:");
+            pw.println(String.format(
+                    "Requests: REQUEST:%d LISTEN:%d BACKGROUND_REQUEST:%d total:%d",
+                    nai.numForegroundNetworkRequests(),
+                    nai.numNetworkRequests() - nai.numRequestNetworkRequests(),
+                    nai.numBackgroundNetworkRequests(),
+                    nai.numNetworkRequests()));
             pw.increaseIndent();
-            for (int i = 0; i < nai.networkRequests.size(); i++) {
-                pw.println(nai.networkRequests.valueAt(i).toString());
+            for (int i = 0; i < nai.numNetworkRequests(); i++) {
+                pw.println(nai.requestAt(i).toString());
             }
             pw.decreaseIndent();
             pw.println("Lingered:");
             pw.increaseIndent();
-            for (NetworkRequest nr : nai.networkLingered) pw.println(nr.toString());
+            nai.dumpLingerTimers(pw);
             pw.decreaseIndent();
             pw.decreaseIndent();
         }
@@ -1957,7 +2062,9 @@
         mKeepaliveTracker.dump(pw);
 
         pw.println();
+        dumpAvoidBadWifiSettings(pw);
 
+        pw.println();
         if (mInetLog != null && mInetLog.size() > 0) {
             pw.println();
             pw.println("Inet condition reports:");
@@ -1985,6 +2092,12 @@
             pw.increaseIndent();
             mNetworkRequestInfoLogs.reverseDump(fd, pw, args);
             pw.decreaseIndent();
+
+            pw.println();
+            pw.println("mNetworkInfoBlockingLogs (most recent first):");
+            pw.increaseIndent();
+            mNetworkInfoBlockingLogs.reverseDump(fd, pw, args);
+            pw.decreaseIndent();
         }
     }
 
@@ -2000,10 +2113,6 @@
         return false;
     }
 
-    private boolean isRequest(NetworkRequest request) {
-        return mNetworkRequests.get(request).isRequest();
-    }
-
     // must be stateless - things change under us.
     private class NetworkStateTrackerHandler extends Handler {
         public NetworkStateTrackerHandler(Looper looper) {
@@ -2045,15 +2154,11 @@
                 case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: {
                     final NetworkCapabilities networkCapabilities = (NetworkCapabilities) msg.obj;
                     if (networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL) ||
-                            networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED)) {
+                            networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED) ||
+                            networkCapabilities.hasCapability(NET_CAPABILITY_FOREGROUND)) {
                         Slog.wtf(TAG, "BUG: " + nai + " has CS-managed capability.");
                     }
-                    if (nai.everConnected && !nai.networkCapabilities.equalImmutableCapabilities(
-                            networkCapabilities)) {
-                        Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: "
-                                + nai.networkCapabilities + " -> " + networkCapabilities);
-                    }
-                    updateCapabilities(nai, networkCapabilities);
+                    updateCapabilities(nai.getCurrentScore(), nai, networkCapabilities);
                     break;
                 }
                 case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: {
@@ -2124,13 +2229,14 @@
                     if (nai != null) {
                         final boolean valid =
                                 (msg.arg1 == NetworkMonitor.NETWORK_TEST_RESULT_VALID);
+                        final boolean wasValidated = nai.lastValidated;
                         if (DBG) log(nai.name() + " validation " + (valid ? "passed" : "failed") +
                                 (msg.obj == null ? "" : " with redirect to " + (String)msg.obj));
                         if (valid != nai.lastValidated) {
                             final int oldScore = nai.getCurrentScore();
                             nai.lastValidated = valid;
                             nai.everValidated |= valid;
-                            updateCapabilities(nai, nai.networkCapabilities);
+                            updateCapabilities(oldScore, nai, nai.networkCapabilities);
                             // If score has changed, rebroadcast to NetworkFactories. b/17726566
                             if (oldScore != nai.getCurrentScore()) sendUpdatedScoreToFactories(nai);
                         }
@@ -2142,13 +2248,9 @@
                                 NetworkAgent.CMD_REPORT_NETWORK_STATUS,
                                 (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK),
                                 0, redirectUrlBundle);
-                    }
-                    break;
-                }
-                case NetworkMonitor.EVENT_NETWORK_LINGER_COMPLETE: {
-                    NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj;
-                    if (isLiveNetworkAgent(nai, msg.what)) {
-                        handleLingerComplete(nai);
+                         if (wasValidated && !nai.lastValidated) {
+                             handleNetworkUnvalidated(nai);
+                         }
                     }
                     break;
                 }
@@ -2161,20 +2263,37 @@
                     }
                     // If captive portal status has changed, update capabilities.
                     if (nai != null && (visible != nai.lastCaptivePortalDetected)) {
+                        final int oldScore = nai.getCurrentScore();
                         nai.lastCaptivePortalDetected = visible;
                         nai.everCaptivePortalDetected |= visible;
-                        updateCapabilities(nai, nai.networkCapabilities);
+                        updateCapabilities(oldScore, nai, nai.networkCapabilities);
                     }
                     if (!visible) {
-                        setProvNotificationVisibleIntent(false, netId, null, 0, null, null, false);
+                        mNotifier.clearNotification(netId);
                     } else {
                         if (nai == null) {
                             loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor");
                             break;
                         }
-                        setProvNotificationVisibleIntent(true, netId, NotificationType.SIGN_IN,
-                                nai.networkInfo.getType(), nai.networkInfo.getExtraInfo(),
-                                (PendingIntent)msg.obj, nai.networkMisc.explicitlySelected);
+                        if (!nai.networkMisc.provisioningNotificationDisabled) {
+                            mNotifier.showNotification(netId, NotificationType.SIGN_IN, nai, null,
+                                    (PendingIntent) msg.obj, nai.networkMisc.explicitlySelected);
+                        }
+                    }
+                    break;
+                }
+            }
+            return true;
+        }
+
+        private boolean maybeHandleNetworkAgentInfoMessage(Message msg) {
+            switch (msg.what) {
+                default:
+                    return false;
+                case NetworkAgentInfo.EVENT_NETWORK_LINGER_COMPLETE: {
+                    NetworkAgentInfo nai = (NetworkAgentInfo) msg.obj;
+                    if (nai != null && isLiveNetworkAgent(nai, msg.what)) {
+                        handleLingerComplete(nai);
                     }
                     break;
                 }
@@ -2184,31 +2303,31 @@
 
         @Override
         public void handleMessage(Message msg) {
-            if (!maybeHandleAsyncChannelMessage(msg) && !maybeHandleNetworkMonitorMessage(msg)) {
+            if (!maybeHandleAsyncChannelMessage(msg) &&
+                    !maybeHandleNetworkMonitorMessage(msg) &&
+                    !maybeHandleNetworkAgentInfoMessage(msg)) {
                 maybeHandleNetworkAgentMessage(msg);
             }
         }
     }
 
-    private void linger(NetworkAgentInfo nai) {
-        nai.lingering = true;
-        NetworkEvent.logEvent(nai.network.netId, NetworkEvent.NETWORK_LINGER);
-        nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER);
-        notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING);
-    }
-
-    // Cancel any lingering so the linger timeout doesn't teardown a network.
-    // This should be called when a network begins satisfying a NetworkRequest.
-    // Note: depending on what state the NetworkMonitor is in (e.g.,
-    // if it's awaiting captive portal login, or if validation failed), this
-    // may trigger a re-evaluation of the network.
-    private void unlinger(NetworkAgentInfo nai) {
-        nai.networkLingered.clear();
-        if (!nai.lingering) return;
-        nai.lingering = false;
-        NetworkEvent.logEvent(nai.network.netId, NetworkEvent.NETWORK_UNLINGER);
-        if (VDBG) log("Canceling linger of " + nai.name());
-        nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED);
+    private void updateLingerState(NetworkAgentInfo nai, long now) {
+        // 1. Update the linger timer. If it's changed, reschedule or cancel the alarm.
+        // 2. If the network was lingering and there are now requests, unlinger it.
+        // 3. If this network is unneeded (which implies it is not lingering), and there is at least
+        //    one lingered request, start lingering.
+        nai.updateLingerTimer();
+        if (nai.isLingering() && nai.numForegroundNetworkRequests() > 0) {
+            if (DBG) log("Unlingering " + nai.name());
+            nai.unlinger();
+            logNetworkEvent(nai, NetworkEvent.NETWORK_UNLINGER);
+        } else if (unneeded(nai, UnneededFor.LINGER) && nai.getLingerExpiry() > 0) {
+            int lingerTime = (int) (nai.getLingerExpiry() - now);
+            if (DBG) log("Lingering " + nai.name() + " for " + lingerTime + "ms");
+            nai.linger();
+            logNetworkEvent(nai, NetworkEvent.NETWORK_LINGER);
+            notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING, lingerTime);
+        }
     }
 
     private void handleAsyncChannelHalfConnect(Message msg) {
@@ -2218,7 +2337,7 @@
                 if (VDBG) log("NetworkFactory connected");
                 // A network factory has connected.  Send it all current NetworkRequests.
                 for (NetworkRequestInfo nri : mNetworkRequests.values()) {
-                    if (!nri.isRequest()) continue;
+                    if (nri.request.isListen()) continue;
                     NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId);
                     ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK,
                             (nai != null ? nai.getCurrentScore() : 0), 0, nri.request);
@@ -2253,7 +2372,7 @@
         NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo);
         if (nai != null) {
             if (DBG) {
-                log(nai.name() + " got DISCONNECTED, was satisfying " + nai.networkRequests.size());
+                log(nai.name() + " got DISCONNECTED, was satisfying " + nai.numNetworkRequests());
             }
             // A network agent has disconnected.
             // TODO - if we move the logic to the network agent (have them disconnect
@@ -2290,21 +2409,23 @@
                 mNetworkForNetId.remove(nai.network.netId);
             }
             // Remove all previously satisfied requests.
-            for (int i = 0; i < nai.networkRequests.size(); i++) {
-                NetworkRequest request = nai.networkRequests.valueAt(i);
+            for (int i = 0; i < nai.numNetworkRequests(); i++) {
+                NetworkRequest request = nai.requestAt(i);
                 NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId);
                 if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) {
                     mNetworkForRequestId.remove(request.requestId);
                     sendUpdatedScoreToFactories(request, 0);
                 }
             }
-            if (nai.networkRequests.get(mDefaultRequest.requestId) != null) {
+            nai.clearLingerState();
+            if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) {
                 removeDataActivityTracking(nai);
                 notifyLockdownVpn(nai);
                 requestNetworkTransitionWakelock(nai.name());
             }
             mLegacyTypeTracker.remove(nai, wasDefault);
             rematchAllNetworksAndRequests(null, 0);
+            mLingerMonitor.noteDisconnect(nai);
             if (nai.created) {
                 // Tell netd to clean up the configuration for this network
                 // (routing rules, DNS, etc).
@@ -2359,7 +2480,7 @@
     private void handleRegisterNetworkRequest(NetworkRequestInfo nri) {
         mNetworkRequests.put(nri.request, nri);
         mNetworkRequestInfoLogs.log("REGISTER " + nri);
-        if (!nri.isRequest()) {
+        if (nri.request.isListen()) {
             for (NetworkAgentInfo network : mNetworkAgentInfos.values()) {
                 if (nri.request.networkCapabilities.hasSignalStrength() &&
                         network.satisfiesImmutableCapabilitiesOf(nri.request)) {
@@ -2368,7 +2489,7 @@
             }
         }
         rematchAllNetworksAndRequests(null, 0);
-        if (nri.isRequest() && mNetworkForRequestId.get(nri.request.requestId) == null) {
+        if (nri.request.isRequest() && mNetworkForRequestId.get(nri.request.requestId) == null) {
             sendUpdatedScoreToFactories(nri.request, 0);
         }
     }
@@ -2381,16 +2502,41 @@
         }
     }
 
-    // Is nai unneeded by all NetworkRequests (and should be disconnected)?
-    // This is whether it is satisfying any NetworkRequests or were it to become validated,
-    // would it have a chance of satisfying any NetworkRequests.
-    private boolean unneeded(NetworkAgentInfo nai) {
-        if (!nai.everConnected || nai.isVPN() || nai.lingering) return false;
+    // Determines whether the network is the best (or could become the best, if it validated), for
+    // none of a particular type of NetworkRequests. The type of NetworkRequests considered depends
+    // on the value of reason:
+    //
+    // - UnneededFor.TEARDOWN: non-listen NetworkRequests. If a network is unneeded for this reason,
+    //   then it should be torn down.
+    // - UnneededFor.LINGER: foreground NetworkRequests. If a network is unneeded for this reason,
+    //   then it should be lingered.
+    private boolean unneeded(NetworkAgentInfo nai, UnneededFor reason) {
+        final int numRequests;
+        switch (reason) {
+            case TEARDOWN:
+                numRequests = nai.numRequestNetworkRequests();
+                break;
+            case LINGER:
+                numRequests = nai.numForegroundNetworkRequests();
+                break;
+            default:
+                Slog.wtf(TAG, "Invalid reason. Cannot happen.");
+                return true;
+        }
+
+        if (!nai.everConnected || nai.isVPN() || nai.isLingering() || numRequests > 0) {
+            return false;
+        }
         for (NetworkRequestInfo nri : mNetworkRequests.values()) {
+            if (reason == UnneededFor.LINGER && nri.request.isBackgroundRequest()) {
+                // Background requests don't affect lingering.
+                continue;
+            }
+
             // If this Network is already the highest scoring Network for a request, or if
             // there is hope for it to become one if it validated, then it is needed.
-            if (nri.isRequest() && nai.satisfies(nri.request) &&
-                    (nai.networkRequests.get(nri.request.requestId) != null ||
+            if (nri.request.isRequest() && nai.satisfies(nri.request) &&
+                    (nai.isSatisfyingRequest(nri.request.requestId) ||
                     // Note that this catches two important cases:
                     // 1. Unvalidated cellular will not be reaped when unvalidated WiFi
                     //    is currently satisfying the request.  This is desirable when
@@ -2406,108 +2552,166 @@
         return true;
     }
 
-    private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) {
-        NetworkRequestInfo nri = mNetworkRequests.get(request);
+    private NetworkRequestInfo getNriForAppRequest(
+            NetworkRequest request, int callingUid, String requestedOperation) {
+        final NetworkRequestInfo nri = mNetworkRequests.get(request);
+
         if (nri != null) {
             if (Process.SYSTEM_UID != callingUid && nri.mUid != callingUid) {
-                if (DBG) log("Attempt to release unowned NetworkRequest " + request);
-                return;
+                log(String.format("UID %d attempted to %s for unowned request %s",
+                        callingUid, requestedOperation, nri));
+                return null;
             }
-            if (VDBG || (DBG && nri.isRequest())) log("releasing NetworkRequest " + request);
-            nri.unlinkDeathRecipient();
-            mNetworkRequests.remove(request);
-            synchronized (mUidToNetworkRequestCount) {
-                int requests = mUidToNetworkRequestCount.get(nri.mUid, 0);
-                if (requests < 1) {
-                    Slog.wtf(TAG, "BUG: too small request count " + requests + " for UID " +
-                            nri.mUid);
-                } else if (requests == 1) {
-                    mUidToNetworkRequestCount.removeAt(
-                            mUidToNetworkRequestCount.indexOfKey(nri.mUid));
-                } else {
-                    mUidToNetworkRequestCount.put(nri.mUid, requests - 1);
-                }
-            }
-            mNetworkRequestInfoLogs.log("RELEASE " + nri);
-            if (nri.isRequest()) {
-                // Find all networks that are satisfying this request and remove the request
-                // from their request lists.
-                // TODO - it's my understanding that for a request there is only a single
-                // network satisfying it, so this loop is wasteful
-                boolean wasKept = false;
-                for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
-                    if (nai.networkRequests.get(nri.request.requestId) != null) {
-                        nai.networkRequests.remove(nri.request.requestId);
-                        if (VDBG) {
-                            log(" Removing from current network " + nai.name() +
-                                    ", leaving " + nai.networkRequests.size() +
-                                    " requests.");
-                        }
-                        if (unneeded(nai)) {
-                            if (DBG) log("no live requests for " + nai.name() + "; disconnecting");
-                            teardownUnneededNetwork(nai);
-                        } else {
-                            // suspect there should only be one pass through here
-                            // but if any were kept do the check below
-                            wasKept |= true;
-                        }
-                    }
-                }
-
-                NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId);
-                if (nai != null) {
-                    mNetworkForRequestId.remove(nri.request.requestId);
-                }
-                // Maintain the illusion.  When this request arrived, we might have pretended
-                // that a network connected to serve it, even though the network was already
-                // connected.  Now that this request has gone away, we might have to pretend
-                // that the network disconnected.  LegacyTypeTracker will generate that
-                // phantom disconnect for this type.
-                if (nri.request.legacyType != TYPE_NONE && nai != null) {
-                    boolean doRemove = true;
-                    if (wasKept) {
-                        // check if any of the remaining requests for this network are for the
-                        // same legacy type - if so, don't remove the nai
-                        for (int i = 0; i < nai.networkRequests.size(); i++) {
-                            NetworkRequest otherRequest = nai.networkRequests.valueAt(i);
-                            if (otherRequest.legacyType == nri.request.legacyType &&
-                                    isRequest(otherRequest)) {
-                                if (DBG) log(" still have other legacy request - leaving");
-                                doRemove = false;
-                            }
-                        }
-                    }
-
-                    if (doRemove) {
-                        mLegacyTypeTracker.remove(nri.request.legacyType, nai, false);
-                    }
-                }
-
-                for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) {
-                    nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST,
-                            nri.request);
-                }
-            } else {
-                // listens don't have a singular affectedNetwork.  Check all networks to see
-                // if this listen request applies and remove it.
-                for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
-                    nai.networkRequests.remove(nri.request.requestId);
-                    if (nri.request.networkCapabilities.hasSignalStrength() &&
-                            nai.satisfiesImmutableCapabilitiesOf(nri.request)) {
-                        updateSignalStrengthThresholds(nai, "RELEASE", nri.request);
-                    }
-                }
-            }
-            callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED);
         }
+
+        return nri;
     }
 
+    private void handleRequestCallbackUpdate(NetworkRequest request, int callingUid,
+            String description, int callbackType) {
+        final NetworkRequestInfo nri = getNriForAppRequest(request, callingUid, description);
+        if (nri == null) return;
+
+        final NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId);
+        // The network that is satisfying this request may have changed since
+        // the application requested the update.
+        //
+        // - If the request is no longer satisfied, don't send any updates.
+        // - If the request is satisfied by a different network, it is the
+        //   caller's responsibility to check that the Network object in the
+        //   callback matches the network that was returned in the last
+        //   onAvailable() callback for this request.
+        if (nai == null) return;
+        callCallbackForRequest(nri, nai, callbackType, 0);
+    }
+
+    private void handleRequestLinkProperties(NetworkRequest request, int callingUid) {
+        handleRequestCallbackUpdate(request, callingUid,
+                "request LinkProperties", ConnectivityManager.CALLBACK_IP_CHANGED);
+    }
+
+    private void handleRequestNetworkCapabilities(NetworkRequest request, int callingUid) {
+        handleRequestCallbackUpdate(request, callingUid,
+                "request NetworkCapabilities", ConnectivityManager.CALLBACK_CAP_CHANGED);
+    }
+
+    private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) {
+        final NetworkRequestInfo nri = getNriForAppRequest(
+                request, callingUid, "release NetworkRequest");
+        if (nri == null) return;
+
+        if (VDBG || (DBG && nri.request.isRequest())) log("releasing " + request);
+        nri.unlinkDeathRecipient();
+        mNetworkRequests.remove(request);
+        synchronized (mUidToNetworkRequestCount) {
+            int requests = mUidToNetworkRequestCount.get(nri.mUid, 0);
+            if (requests < 1) {
+                Slog.wtf(TAG, "BUG: too small request count " + requests + " for UID " +
+                        nri.mUid);
+            } else if (requests == 1) {
+                mUidToNetworkRequestCount.removeAt(
+                        mUidToNetworkRequestCount.indexOfKey(nri.mUid));
+            } else {
+                mUidToNetworkRequestCount.put(nri.mUid, requests - 1);
+            }
+        }
+        mNetworkRequestInfoLogs.log("RELEASE " + nri);
+        if (nri.request.isRequest()) {
+            boolean wasKept = false;
+            NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId);
+            if (nai != null) {
+                boolean wasBackgroundNetwork = nai.isBackgroundNetwork();
+                nai.removeRequest(nri.request.requestId);
+                if (VDBG) {
+                    log(" Removing from current network " + nai.name() +
+                            ", leaving " + nai.numNetworkRequests() + " requests.");
+                }
+                // If there are still lingered requests on this network, don't tear it down,
+                // but resume lingering instead.
+                updateLingerState(nai, SystemClock.elapsedRealtime());
+                if (unneeded(nai, UnneededFor.TEARDOWN)) {
+                    if (DBG) log("no live requests for " + nai.name() + "; disconnecting");
+                    teardownUnneededNetwork(nai);
+                } else {
+                    wasKept = true;
+                }
+                mNetworkForRequestId.remove(nri.request.requestId);
+                if (!wasBackgroundNetwork && nai.isBackgroundNetwork()) {
+                    // Went from foreground to background.
+                    updateCapabilities(nai.getCurrentScore(), nai, nai.networkCapabilities);
+                }
+            }
+
+            // TODO: remove this code once we know that the Slog.wtf is never hit.
+            //
+            // Find all networks that are satisfying this request and remove the request
+            // from their request lists.
+            // TODO - it's my understanding that for a request there is only a single
+            // network satisfying it, so this loop is wasteful
+            for (NetworkAgentInfo otherNai : mNetworkAgentInfos.values()) {
+                if (otherNai.isSatisfyingRequest(nri.request.requestId) && otherNai != nai) {
+                    Slog.wtf(TAG, "Request " + nri.request + " satisfied by " +
+                            otherNai.name() + ", but mNetworkAgentInfos says " +
+                            (nai != null ? nai.name() : "null"));
+                }
+            }
+
+            // Maintain the illusion.  When this request arrived, we might have pretended
+            // that a network connected to serve it, even though the network was already
+            // connected.  Now that this request has gone away, we might have to pretend
+            // that the network disconnected.  LegacyTypeTracker will generate that
+            // phantom disconnect for this type.
+            if (nri.request.legacyType != TYPE_NONE && nai != null) {
+                boolean doRemove = true;
+                if (wasKept) {
+                    // check if any of the remaining requests for this network are for the
+                    // same legacy type - if so, don't remove the nai
+                    for (int i = 0; i < nai.numNetworkRequests(); i++) {
+                        NetworkRequest otherRequest = nai.requestAt(i);
+                        if (otherRequest.legacyType == nri.request.legacyType &&
+                                otherRequest.isRequest()) {
+                            if (DBG) log(" still have other legacy request - leaving");
+                            doRemove = false;
+                        }
+                    }
+                }
+
+                if (doRemove) {
+                    mLegacyTypeTracker.remove(nri.request.legacyType, nai, false);
+                }
+            }
+
+            for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) {
+                nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST,
+                        nri.request);
+            }
+        } else {
+            // listens don't have a singular affectedNetwork.  Check all networks to see
+            // if this listen request applies and remove it.
+            for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
+                nai.removeRequest(nri.request.requestId);
+                if (nri.request.networkCapabilities.hasSignalStrength() &&
+                        nai.satisfiesImmutableCapabilitiesOf(nri.request)) {
+                    updateSignalStrengthThresholds(nai, "RELEASE", nri.request);
+                }
+            }
+        }
+        callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED, 0);
+    }
+
+    @Override
     public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
         enforceConnectivityInternalPermission();
         mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_ACCEPT_UNVALIDATED,
                 accept ? 1 : 0, always ? 1: 0, network));
     }
 
+    @Override
+    public void setAvoidUnvalidated(Network network) {
+        enforceConnectivityInternalPermission();
+        mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_AVOID_UNVALIDATED, network));
+    }
+
     private void handleSetAcceptUnvalidated(Network network, boolean accept, boolean always) {
         if (DBG) log("handleSetAcceptUnvalidated network=" + network +
                 " accept=" + accept + " always=" + always);
@@ -2548,6 +2752,20 @@
 
     }
 
+    private void handleSetAvoidUnvalidated(Network network) {
+        NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
+        if (nai == null || nai.lastValidated) {
+            // Nothing to do. The network either disconnected or revalidated.
+            return;
+        }
+        if (!nai.avoidUnvalidated) {
+            int oldScore = nai.getCurrentScore();
+            nai.avoidUnvalidated = true;
+            rematchAllNetworksAndRequests(nai, oldScore);
+            sendUpdatedScoreToFactories(nai);
+        }
+    }
+
     private void scheduleUnvalidatedPrompt(NetworkAgentInfo nai) {
         if (VDBG) log("scheduleUnvalidatedPrompt " + nai.network);
         mHandler.sendMessageDelayed(
@@ -2555,6 +2773,81 @@
                 PROMPT_UNVALIDATED_DELAY_MS);
     }
 
+    public boolean avoidBadWifi() {
+        return mAvoidBadWifiTracker.currentValue();
+    }
+
+    private void rematchForAvoidBadWifiUpdate() {
+        rematchAllNetworksAndRequests(null, 0);
+        for (NetworkAgentInfo nai: mNetworkAgentInfos.values()) {
+            if (nai.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
+                sendUpdatedScoreToFactories(nai);
+            }
+        }
+    }
+
+    // TODO: Evaluate whether this is of interest to other consumers of
+    // AvoidBadWifiTracker and worth moving out of here.
+    private void dumpAvoidBadWifiSettings(IndentingPrintWriter pw) {
+        final boolean configRestrict = mAvoidBadWifiTracker.configRestrictsAvoidBadWifi();
+        if (!configRestrict) {
+            pw.println("Bad Wi-Fi avoidance: unrestricted");
+            return;
+        }
+
+        pw.println("Bad Wi-Fi avoidance: " + avoidBadWifi());
+        pw.increaseIndent();
+        pw.println("Config restrict:   " + configRestrict);
+
+        final String value = mAvoidBadWifiTracker.getSettingsValue();
+        String description;
+        // Can't use a switch statement because strings are legal case labels, but null is not.
+        if ("0".equals(value)) {
+            description = "get stuck";
+        } else if (value == null) {
+            description = "prompt";
+        } else if ("1".equals(value)) {
+            description = "avoid";
+        } else {
+            description = value + " (?)";
+        }
+        pw.println("User setting:      " + description);
+        pw.println("Network overrides:");
+        pw.increaseIndent();
+        for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
+            if (nai.avoidUnvalidated) {
+                pw.println(nai.name());
+            }
+        }
+        pw.decreaseIndent();
+        pw.decreaseIndent();
+    }
+
+    private void showValidationNotification(NetworkAgentInfo nai, NotificationType type) {
+        final String action;
+        switch (type) {
+            case NO_INTERNET:
+                action = ConnectivityManager.ACTION_PROMPT_UNVALIDATED;
+                break;
+            case LOST_INTERNET:
+                action = ConnectivityManager.ACTION_PROMPT_LOST_VALIDATION;
+                break;
+            default:
+                Slog.wtf(TAG, "Unknown notification type " + type);
+                return;
+        }
+
+        Intent intent = new Intent(action);
+        intent.setData(Uri.fromParts("netId", Integer.toString(nai.network.netId), null));
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        intent.setClassName("com.android.settings",
+                "com.android.settings.wifi.WifiNoInternetDialog");
+
+        PendingIntent pendingIntent = PendingIntent.getActivityAsUser(
+                mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
+        mNotifier.showNotification(nai.network.netId, type, nai, null, pendingIntent, true);
+    }
+
     private void handlePromptUnvalidated(Network network) {
         if (VDBG) log("handlePromptUnvalidated " + network);
         NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
@@ -2566,17 +2859,17 @@
                 !nai.networkMisc.explicitlySelected || nai.networkMisc.acceptUnvalidated) {
             return;
         }
+        showValidationNotification(nai, NotificationType.NO_INTERNET);
+    }
 
-        Intent intent = new Intent(ConnectivityManager.ACTION_PROMPT_UNVALIDATED);
-        intent.setData(Uri.fromParts("netId", Integer.toString(network.netId), null));
-        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        intent.setClassName("com.android.settings",
-                "com.android.settings.wifi.WifiNoInternetDialog");
+    private void handleNetworkUnvalidated(NetworkAgentInfo nai) {
+        NetworkCapabilities nc = nai.networkCapabilities;
+        if (DBG) log("handleNetworkUnvalidated " + nai.name() + " cap=" + nc);
 
-        PendingIntent pendingIntent = PendingIntent.getActivityAsUser(
-                mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
-        setProvNotificationVisibleIntent(true, nai.network.netId, NotificationType.NO_INTERNET,
-                nai.networkInfo.getType(), nai.networkInfo.getExtraInfo(), pendingIntent, true);
+        if (nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) &&
+            mAvoidBadWifiTracker.shouldNotifyWifiUnvalidated()) {
+            showValidationNotification(nai, NotificationType.LOST_INTERNET);
+        }
     }
 
     private class InternalHandler extends Handler {
@@ -2652,6 +2945,10 @@
                     handleSetAcceptUnvalidated((Network) msg.obj, msg.arg1 != 0, msg.arg2 != 0);
                     break;
                 }
+                case EVENT_SET_AVOID_UNVALIDATED: {
+                    handleSetAvoidUnvalidated((Network) msg.obj);
+                    break;
+                }
                 case EVENT_PROMPT_UNVALIDATED: {
                     handlePromptUnvalidated((Network) msg.obj);
                     break;
@@ -2660,6 +2957,12 @@
                     handleMobileDataAlwaysOn();
                     break;
                 }
+                case EVENT_REQUEST_LINKPROPERTIES:
+                    handleRequestLinkProperties((NetworkRequest) msg.obj, msg.arg1);
+                    break;
+                case EVENT_REQUEST_NETCAPABILITIES:
+                    handleRequestNetworkCapabilities((NetworkRequest) msg.obj, msg.arg1);
+                    break;
                 // Sent by KeepaliveTracker to process an app request on the state machine thread.
                 case NetworkAgent.CMD_START_PACKET_KEEPALIVE: {
                     mKeepaliveTracker.handleStartKeepalive(msg);
@@ -2684,6 +2987,7 @@
     }
 
     // javadoc from interface
+    @Override
     public int tether(String iface) {
         ConnectivityManager.enforceTetherChangePermission(mContext);
         if (isTetheringSupported()) {
@@ -2695,6 +2999,7 @@
     }
 
     // javadoc from interface
+    @Override
     public int untether(String iface) {
         ConnectivityManager.enforceTetherChangePermission(mContext);
 
@@ -2707,6 +3012,7 @@
     }
 
     // javadoc from interface
+    @Override
     public int getLastTetherError(String iface) {
         enforceTetherAccessPermission();
 
@@ -2718,6 +3024,7 @@
     }
 
     // TODO - proper iface API for selection by property, inspection, etc
+    @Override
     public String[] getTetherableUsbRegexs() {
         enforceTetherAccessPermission();
         if (isTetheringSupported()) {
@@ -2727,6 +3034,7 @@
         }
     }
 
+    @Override
     public String[] getTetherableWifiRegexs() {
         enforceTetherAccessPermission();
         if (isTetheringSupported()) {
@@ -2736,6 +3044,7 @@
         }
     }
 
+    @Override
     public String[] getTetherableBluetoothRegexs() {
         enforceTetherAccessPermission();
         if (isTetheringSupported()) {
@@ -2745,6 +3054,7 @@
         }
     }
 
+    @Override
     public int setUsbTethering(boolean enable) {
         ConnectivityManager.enforceTetherChangePermission(mContext);
         if (isTetheringSupported()) {
@@ -2756,21 +3066,25 @@
 
     // TODO - move iface listing, queries, etc to new module
     // javadoc from interface
+    @Override
     public String[] getTetherableIfaces() {
         enforceTetherAccessPermission();
         return mTethering.getTetherableIfaces();
     }
 
+    @Override
     public String[] getTetheredIfaces() {
         enforceTetherAccessPermission();
         return mTethering.getTetheredIfaces();
     }
 
+    @Override
     public String[] getTetheringErroredIfaces() {
         enforceTetherAccessPermission();
         return mTethering.getErroredIfaces();
     }
 
+    @Override
     public String[] getTetheredDhcpRanges() {
         enforceConnectivityInternalPermission();
         return mTethering.getTetheredDhcpRanges();
@@ -2829,12 +3143,14 @@
     }
 
     // 100 percent is full good, 0 is full bad.
+    @Override
     public void reportInetCondition(int networkType, int percentage) {
         NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
         if (nai == null) return;
         reportNetworkConnectivity(nai.network, percentage > 50);
     }
 
+    @Override
     public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
         enforceAccessPermission();
         enforceInternetPermission();
@@ -2879,6 +3195,7 @@
         }
     }
 
+    @Override
     public ProxyInfo getProxyForNetwork(Network network) {
         if (network == null) return getDefaultProxy();
         final ProxyInfo globalProxy = getGlobalProxy();
@@ -3450,118 +3767,6 @@
         return -1;
     }
 
-    private static final String NOTIFICATION_ID = "CaptivePortal.Notification";
-    private static enum NotificationType { SIGN_IN, NO_INTERNET; };
-
-    private void setProvNotificationVisible(boolean visible, int networkType, String action) {
-        Intent intent = new Intent(action);
-        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
-        // Concatenate the range of types onto the range of NetIDs.
-        int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE);
-        setProvNotificationVisibleIntent(visible, id, NotificationType.SIGN_IN,
-                networkType, null, pendingIntent, false);
-    }
-
-    /**
-     * Show or hide network provisioning notifications.
-     *
-     * We use notifications for two purposes: to notify that a network requires sign in
-     * (NotificationType.SIGN_IN), or to notify that a network does not have Internet access
-     * (NotificationType.NO_INTERNET). We display at most one notification per ID, so on a
-     * particular network we can display the notification type that was most recently requested.
-     * So for example if a captive portal fails to reply within a few seconds of connecting, we
-     * might first display NO_INTERNET, and then when the captive portal check completes, display
-     * SIGN_IN.
-     *
-     * @param id an identifier that uniquely identifies this notification.  This must match
-     *         between show and hide calls.  We use the NetID value but for legacy callers
-     *         we concatenate the range of types with the range of NetIDs.
-     */
-    private void setProvNotificationVisibleIntent(boolean visible, int id,
-            NotificationType notifyType, int networkType, String extraInfo, PendingIntent intent,
-            boolean highPriority) {
-        if (VDBG || (DBG && visible)) {
-            log("setProvNotificationVisibleIntent " + notifyType + " visible=" + visible
-                    + " networkType=" + getNetworkTypeName(networkType)
-                    + " extraInfo=" + extraInfo + " highPriority=" + highPriority);
-        }
-
-        Resources r = Resources.getSystem();
-        NotificationManager notificationManager = (NotificationManager) mContext
-            .getSystemService(Context.NOTIFICATION_SERVICE);
-
-        if (visible) {
-            CharSequence title;
-            CharSequence details;
-            int icon;
-            if (notifyType == NotificationType.NO_INTERNET &&
-                    networkType == ConnectivityManager.TYPE_WIFI) {
-                title = r.getString(R.string.wifi_no_internet, 0);
-                details = r.getString(R.string.wifi_no_internet_detailed);
-                icon = R.drawable.stat_notify_wifi_in_range;  // TODO: Need new icon.
-            } else if (notifyType == NotificationType.SIGN_IN) {
-                switch (networkType) {
-                    case ConnectivityManager.TYPE_WIFI:
-                        title = r.getString(R.string.wifi_available_sign_in, 0);
-                        details = r.getString(R.string.network_available_sign_in_detailed,
-                                extraInfo);
-                        icon = R.drawable.stat_notify_wifi_in_range;
-                        break;
-                    case ConnectivityManager.TYPE_MOBILE:
-                    case ConnectivityManager.TYPE_MOBILE_HIPRI:
-                        title = r.getString(R.string.network_available_sign_in, 0);
-                        // TODO: Change this to pull from NetworkInfo once a printable
-                        // name has been added to it
-                        details = mTelephonyManager.getNetworkOperatorName();
-                        icon = R.drawable.stat_notify_rssi_in_range;
-                        break;
-                    default:
-                        title = r.getString(R.string.network_available_sign_in, 0);
-                        details = r.getString(R.string.network_available_sign_in_detailed,
-                                extraInfo);
-                        icon = R.drawable.stat_notify_rssi_in_range;
-                        break;
-                }
-            } else {
-                Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network type "
-                        + getNetworkTypeName(networkType));
-                return;
-            }
-
-            Notification notification = new Notification.Builder(mContext)
-                    .setWhen(0)
-                    .setSmallIcon(icon)
-                    .setAutoCancel(true)
-                    .setTicker(title)
-                    .setColor(mContext.getColor(
-                            com.android.internal.R.color.system_notification_accent_color))
-                    .setContentTitle(title)
-                    .setContentText(details)
-                    .setContentIntent(intent)
-                    .setLocalOnly(true)
-                    .setPriority(highPriority ?
-                            Notification.PRIORITY_HIGH :
-                            Notification.PRIORITY_DEFAULT)
-                    .setDefaults(highPriority ? Notification.DEFAULT_ALL : 0)
-                    .setOnlyAlertOnce(true)
-                    .build();
-
-            try {
-                notificationManager.notifyAsUser(NOTIFICATION_ID, id, notification, UserHandle.ALL);
-            } catch (NullPointerException npe) {
-                loge("setNotificationVisible: visible notificationManager npe=" + npe);
-                npe.printStackTrace();
-            }
-        } else {
-            try {
-                notificationManager.cancelAsUser(NOTIFICATION_ID, id, UserHandle.ALL);
-            } catch (NullPointerException npe) {
-                loge("setNotificationVisible: cancel notificationManager npe=" + npe);
-                npe.printStackTrace();
-            }
-        }
-    }
-
     /** Location to an updatable file listing carrier provisioning urls.
      *  An example:
      *
@@ -3665,7 +3870,9 @@
         enforceConnectivityInternalPermission();
         final long ident = Binder.clearCallingIdentity();
         try {
-            setProvNotificationVisible(visible, networkType, action);
+            // Concatenate the range of types onto the range of NetIDs.
+            int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE);
+            mNotifier.setProvNotificationVisible(visible, id, action);
         } finally {
             Binder.restoreCallingIdentity(ident);
         }
@@ -3794,31 +4001,12 @@
         }
     }
 
-    /**
-     * A NetworkRequest as registered by an application can be one of three
-     * types:
-     *
-     *     - "listen", for which the framework will issue callbacks about any
-     *       and all networks that match the specified NetworkCapabilities,
-     *
-     *     - "request", capable of causing a specific network to be created
-     *       first (e.g. a telephony DUN request), the framework will issue
-     *       callbacks about the single, highest scoring current network
-     *       (if any) that matches the specified NetworkCapabilities, or
-     *
-     *     - "track the default network", a hybrid of the two designed such
-     *       that the framework will issue callbacks for the single, highest
-     *       scoring current network (if any) that matches the capabilities of
-     *       the default Internet request (mDefaultRequest), but which cannot
-     *       cause the framework to either create or retain the existence of
-     *       any specific network.
-     *
-     */
-    private static enum NetworkRequestType {
-        LISTEN,
-        TRACK_DEFAULT,
-        REQUEST
-    };
+    private void ensureNetworkRequestHasType(NetworkRequest request) {
+        if (request.type == NetworkRequest.Type.NONE) {
+            throw new IllegalArgumentException(
+                    "All NetworkRequests in ConnectivityService must have a type");
+        }
+    }
 
     /**
      * Tracks info about the requester.
@@ -3832,27 +4020,26 @@
         final int mPid;
         final int mUid;
         final Messenger messenger;
-        private final NetworkRequestType mType;
 
-        NetworkRequestInfo(NetworkRequest r, PendingIntent pi, NetworkRequestType type) {
+        NetworkRequestInfo(NetworkRequest r, PendingIntent pi) {
             request = r;
+            ensureNetworkRequestHasType(request);
             mPendingIntent = pi;
             messenger = null;
             mBinder = null;
             mPid = getCallingPid();
             mUid = getCallingUid();
-            mType = type;
             enforceRequestCountLimit();
         }
 
-        NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder, NetworkRequestType type) {
+        NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder) {
             super();
             messenger = m;
             request = r;
+            ensureNetworkRequestHasType(request);
             mBinder = binder;
             mPid = getCallingPid();
             mUid = getCallingUid();
-            mType = type;
             mPendingIntent = null;
             enforceRequestCountLimit();
 
@@ -3873,16 +4060,6 @@
             }
         }
 
-        private String typeString() {
-            switch (mType) {
-                case LISTEN: return "Listen";
-                case REQUEST: return "Request";
-                case TRACK_DEFAULT: return "Track default";
-                default:
-                    return "unknown type";
-            }
-        }
-
         void unlinkDeathRecipient() {
             if (mBinder != null) {
                 mBinder.unlinkToDeath(this, 0);
@@ -3895,29 +4072,8 @@
             releaseNetworkRequest(request);
         }
 
-        /**
-         * Returns true iff. the contained NetworkRequest is one that:
-         *
-         *     - should be associated with at most one satisfying network
-         *       at a time;
-         *
-         *     - should cause a network to be kept up if it is the only network
-         *       which can satisfy the NetworkReqeust.
-         *
-         * For full detail of how isRequest() is used for pairing Networks with
-         * NetworkRequests read rematchNetworkAndRequests().
-         *
-         * TODO: Rename to something more properly descriptive.
-         */
-        public boolean isRequest() {
-            return (mType == NetworkRequestType.TRACK_DEFAULT) ||
-                   (mType == NetworkRequestType.REQUEST);
-        }
-
         public String toString() {
-            return typeString() +
-                    " from uid/pid:" + mUid + "/" + mPid +
-                    " for " + request +
+            return "uid/pid:" + mUid + "/" + mPid + " " + request +
                     (mPendingIntent == null ? "" : " to trigger " + mPendingIntent);
         }
     }
@@ -3967,18 +4123,21 @@
     @Override
     public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities,
             Messenger messenger, int timeoutMs, IBinder binder, int legacyType) {
-        final NetworkRequestType type = (networkCapabilities == null)
-                ? NetworkRequestType.TRACK_DEFAULT
-                : NetworkRequestType.REQUEST;
+        final NetworkRequest.Type type = (networkCapabilities == null)
+                ? NetworkRequest.Type.TRACK_DEFAULT
+                : NetworkRequest.Type.REQUEST;
         // If the requested networkCapabilities is null, take them instead from
         // the default network request. This allows callers to keep track of
         // the system default network.
-        if (type == NetworkRequestType.TRACK_DEFAULT) {
+        if (type == NetworkRequest.Type.TRACK_DEFAULT) {
             networkCapabilities = new NetworkCapabilities(mDefaultRequest.networkCapabilities);
             enforceAccessPermission();
         } else {
             networkCapabilities = new NetworkCapabilities(networkCapabilities);
             enforceNetworkRequestPermissions(networkCapabilities);
+            // TODO: this is incorrect. We mark the request as metered or not depending on the state
+            // of the app when the request is filed, but we never change the request if the app
+            // changes network state. http://b/29964605
             enforceMeteredApnPolicy(networkCapabilities);
         }
         ensureRequestableCapabilities(networkCapabilities);
@@ -3994,8 +4153,8 @@
         }
 
         NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, legacyType,
-                nextNetworkRequestId());
-        NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, type);
+                nextNetworkRequestId(), type);
+        NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder);
         if (DBG) log("requestNetwork for " + nri);
 
         mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri));
@@ -4008,7 +4167,7 @@
 
     private void enforceNetworkRequestPermissions(NetworkCapabilities networkCapabilities) {
         if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) == false) {
-            enforceConnectivityInternalPermission();
+            enforceConnectivityRestrictedNetworksPermission();
         } else {
             enforceChangePermission();
         }
@@ -4065,9 +4224,8 @@
         ensureRequestableCapabilities(networkCapabilities);
 
         NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE,
-                nextNetworkRequestId());
-        NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation,
-                NetworkRequestType.REQUEST);
+                nextNetworkRequestId(), NetworkRequest.Type.REQUEST);
+        NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation);
         if (DBG) log("pendingRequest for " + nri);
         mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT,
                 nri));
@@ -4116,10 +4274,19 @@
             enforceAccessPermission();
         }
 
-        NetworkRequest networkRequest = new NetworkRequest(
-                new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId());
-        NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder,
-                NetworkRequestType.LISTEN);
+        NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities);
+        if (!ConnectivityManager.checkChangePermission(mContext)) {
+            // Apps without the CHANGE_NETWORK_STATE permission can't use background networks, so
+            // make all their listens include NET_CAPABILITY_FOREGROUND. That way, they will get
+            // onLost and onAvailable callbacks when networks move in and out of the background.
+            // There is no need to do this for requests because an app without CHANGE_NETWORK_STATE
+            // can't request networks.
+            nc.addCapability(NET_CAPABILITY_FOREGROUND);
+        }
+
+        NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(),
+                NetworkRequest.Type.LISTEN);
+        NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder);
         if (VDBG) log("listenForNetwork for " + nri);
 
         mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri));
@@ -4135,18 +4302,35 @@
         }
 
         NetworkRequest networkRequest = new NetworkRequest(
-                new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId());
-        NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation,
-                NetworkRequestType.LISTEN);
+                new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId(),
+                NetworkRequest.Type.LISTEN);
+        NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation);
         if (VDBG) log("pendingListenForNetwork for " + nri);
 
         mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri));
     }
 
     @Override
+    public void requestLinkProperties(NetworkRequest networkRequest) {
+        ensureNetworkRequestHasType(networkRequest);
+        if (networkRequest.type == NetworkRequest.Type.LISTEN) return;
+        mHandler.sendMessage(mHandler.obtainMessage(
+                EVENT_REQUEST_LINKPROPERTIES, getCallingUid(), 0, networkRequest));
+    }
+
+    @Override
+    public void requestNetworkCapabilities(NetworkRequest networkRequest) {
+        ensureNetworkRequestHasType(networkRequest);
+        if (networkRequest.type == NetworkRequest.Type.LISTEN) return;
+        mHandler.sendMessage(mHandler.obtainMessage(
+                EVENT_REQUEST_NETCAPABILITIES, getCallingUid(), 0, networkRequest));
+    }
+
+    @Override
     public void releaseNetworkRequest(NetworkRequest networkRequest) {
-        mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(),
-                0, networkRequest));
+        ensureNetworkRequestHasType(networkRequest);
+        mHandler.sendMessage(mHandler.obtainMessage(
+                EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), 0, networkRequest));
     }
 
     @Override
@@ -4221,6 +4405,10 @@
         return nai == getDefaultNetwork();
     }
 
+    private boolean isDefaultRequest(NetworkRequestInfo nri) {
+        return nri.request.requestId == mDefaultRequest.requestId;
+    }
+
     public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo,
             LinkProperties linkProperties, NetworkCapabilities networkCapabilities,
             int currentScore, NetworkMisc networkMisc) {
@@ -4411,15 +4599,35 @@
         mNumDnsEntries = last;
     }
 
+    private String getNetworkPermission(NetworkCapabilities nc) {
+        // TODO: make these permission strings AIDL constants instead.
+        if (!nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) {
+            return NetworkManagementService.PERMISSION_SYSTEM;
+        }
+        if (!nc.hasCapability(NET_CAPABILITY_FOREGROUND)) {
+            return NetworkManagementService.PERMISSION_NETWORK;
+        }
+        return null;
+    }
+
     /**
      * Update the NetworkCapabilities for {@code networkAgent} to {@code networkCapabilities}
      * augmented with any stateful capabilities implied from {@code networkAgent}
      * (e.g., validated status and captive portal status).
      *
+     * @param oldScore score of the network before any of the changes that prompted us
+     *                 to call this function.
      * @param nai the network having its capabilities updated.
      * @param networkCapabilities the new network capabilities.
      */
-    private void updateCapabilities(NetworkAgentInfo nai, NetworkCapabilities networkCapabilities) {
+    private void updateCapabilities(
+            int oldScore, NetworkAgentInfo nai, NetworkCapabilities networkCapabilities) {
+        if (nai.everConnected && !nai.networkCapabilities.equalImmutableCapabilities(
+                networkCapabilities)) {
+            Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: "
+                    + nai.networkCapabilities + " -> " + networkCapabilities);
+        }
+
         // Don't modify caller's NetworkCapabilities.
         networkCapabilities = new NetworkCapabilities(networkCapabilities);
         if (nai.lastValidated) {
@@ -4432,31 +4640,48 @@
         } else {
             networkCapabilities.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
         }
-        if (!Objects.equals(nai.networkCapabilities, networkCapabilities)) {
-            final int oldScore = nai.getCurrentScore();
-            if (nai.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) !=
-                    networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) {
-                try {
-                    mNetd.setNetworkPermission(nai.network.netId,
-                            networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) ?
-                                    null : NetworkManagementService.PERMISSION_SYSTEM);
-                } catch (RemoteException e) {
-                    loge("Exception in setNetworkPermission: " + e);
-                }
+        if (nai.isBackgroundNetwork()) {
+            networkCapabilities.removeCapability(NET_CAPABILITY_FOREGROUND);
+        } else {
+            networkCapabilities.addCapability(NET_CAPABILITY_FOREGROUND);
+        }
+
+        if (Objects.equals(nai.networkCapabilities, networkCapabilities)) return;
+
+        final String oldPermission = getNetworkPermission(nai.networkCapabilities);
+        final String newPermission = getNetworkPermission(networkCapabilities);
+        if (!Objects.equals(oldPermission, newPermission) && nai.created && !nai.isVPN()) {
+            try {
+                mNetd.setNetworkPermission(nai.network.netId, newPermission);
+            } catch (RemoteException e) {
+                loge("Exception in setNetworkPermission: " + e);
             }
-            synchronized (nai) {
-                nai.networkCapabilities = networkCapabilities;
-            }
+        }
+
+        final NetworkCapabilities prevNc = nai.networkCapabilities;
+        synchronized (nai) {
+            nai.networkCapabilities = networkCapabilities;
+        }
+        if (nai.getCurrentScore() == oldScore &&
+                networkCapabilities.equalRequestableCapabilities(prevNc)) {
+            // If the requestable capabilities haven't changed, and the score hasn't changed, then
+            // the change we're processing can't affect any requests, it can only affect the listens
+            // on this network. We might have been called by rematchNetworkAndRequests when a
+            // network changed foreground state.
+            processListenRequests(nai, true);
+        } else {
+            // If the requestable capabilities have changed or the score changed, we can't have been
+            // called by rematchNetworkAndRequests, so it's safe to start a rematch.
             rematchAllNetworksAndRequests(nai, oldScore);
             notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED);
         }
     }
 
     private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) {
-        for (int i = 0; i < nai.networkRequests.size(); i++) {
-            NetworkRequest nr = nai.networkRequests.valueAt(i);
+        for (int i = 0; i < nai.numNetworkRequests(); i++) {
+            NetworkRequest nr = nai.requestAt(i);
             // Don't send listening requests to factories. b/17393458
-            if (!isRequest(nr)) continue;
+            if (nr.isListen()) continue;
             sendUpdatedScoreToFactories(nr, nai.getCurrentScore());
         }
     }
@@ -4505,7 +4730,7 @@
     }
 
     private void callCallbackForRequest(NetworkRequestInfo nri,
-            NetworkAgentInfo networkAgent, int notificationType) {
+            NetworkAgentInfo networkAgent, int notificationType, int arg1) {
         if (nri.messenger == null) return;  // Default request has no msgr
         Bundle bundle = new Bundle();
         bundle.putParcelable(NetworkRequest.class.getSimpleName(),
@@ -4517,7 +4742,7 @@
         }
         switch (notificationType) {
             case ConnectivityManager.CALLBACK_LOSING: {
-                msg.arg1 = 30 * 1000; // TODO - read this from NetworkMonitor
+                msg.arg1 = arg1;
                 break;
             }
             case ConnectivityManager.CALLBACK_CAP_CHANGED: {
@@ -4546,12 +4771,14 @@
     }
 
     private void teardownUnneededNetwork(NetworkAgentInfo nai) {
-        for (int i = 0; i < nai.networkRequests.size(); i++) {
-            NetworkRequest nr = nai.networkRequests.valueAt(i);
-            // Ignore listening requests.
-            if (!isRequest(nr)) continue;
-            loge("Dead network still had at least " + nr);
-            break;
+        if (nai.numRequestNetworkRequests() != 0) {
+            for (int i = 0; i < nai.numNetworkRequests(); i++) {
+                NetworkRequest nr = nai.requestAt(i);
+                // Ignore listening requests.
+                if (nr.isListen()) continue;
+                loge("Dead network still had at least " + nr);
+                break;
+            }
         }
         nai.asyncChannel.disconnect();
     }
@@ -4562,7 +4789,19 @@
             return;
         }
         if (DBG) log("handleLingerComplete for " + oldNetwork.name());
-        teardownUnneededNetwork(oldNetwork);
+
+        // If we get here it means that the last linger timeout for this network expired. So there
+        // must be no other active linger timers, and we must stop lingering.
+        oldNetwork.clearLingerState();
+
+        if (unneeded(oldNetwork, UnneededFor.TEARDOWN)) {
+            // Tear the network down.
+            teardownUnneededNetwork(oldNetwork);
+        } else {
+            // Put the network in the background.
+            updateCapabilities(oldNetwork.getCurrentScore(), oldNetwork,
+                    oldNetwork.networkCapabilities);
+        }
     }
 
     private void makeDefault(NetworkAgentInfo newNetwork) {
@@ -4579,6 +4818,31 @@
         setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnsServers());
     }
 
+    private void processListenRequests(NetworkAgentInfo nai, boolean capabilitiesChanged) {
+        // For consistency with previous behaviour, send onLost callbacks before onAvailable.
+        for (NetworkRequestInfo nri : mNetworkRequests.values()) {
+            NetworkRequest nr = nri.request;
+            if (!nr.isListen()) continue;
+            if (nai.isSatisfyingRequest(nr.requestId) && !nai.satisfies(nr)) {
+                nai.removeRequest(nri.request.requestId);
+                callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_LOST, 0);
+            }
+        }
+
+        if (capabilitiesChanged) {
+            notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED);
+        }
+
+        for (NetworkRequestInfo nri : mNetworkRequests.values()) {
+            NetworkRequest nr = nri.request;
+            if (!nr.isListen()) continue;
+            if (nai.satisfies(nr) && !nai.isSatisfyingRequest(nr.requestId)) {
+                nai.addRequest(nr);
+                notifyNetworkCallback(nai, nri);
+            }
+        }
+    }
+
     // Handles a network appearing or improving its score.
     //
     // - Evaluates all current NetworkRequests that can be
@@ -4607,18 +4871,30 @@
     //               performed to tear down unvalidated networks that have no chance (i.e. even if
     //               validated) of becoming the highest scoring network.
     private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork,
-            ReapUnvalidatedNetworks reapUnvalidatedNetworks) {
+            ReapUnvalidatedNetworks reapUnvalidatedNetworks, long now) {
         if (!newNetwork.everConnected) return;
         boolean keep = newNetwork.isVPN();
         boolean isNewDefault = false;
         NetworkAgentInfo oldDefaultNetwork = null;
+
+        final boolean wasBackgroundNetwork = newNetwork.isBackgroundNetwork();
+        final int score = newNetwork.getCurrentScore();
+
         if (VDBG) log("rematching " + newNetwork.name());
+
         // Find and migrate to this Network any NetworkRequests for
         // which this network is now the best.
         ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<NetworkAgentInfo>();
         ArrayList<NetworkRequestInfo> addedRequests = new ArrayList<NetworkRequestInfo>();
-        if (VDBG) log(" network has: " + newNetwork.networkCapabilities);
+        NetworkCapabilities nc = newNetwork.networkCapabilities;
+        if (VDBG) log(" network has: " + nc);
         for (NetworkRequestInfo nri : mNetworkRequests.values()) {
+            // Process requests in the first pass and listens in the second pass. This allows us to
+            // change a network's capabilities depending on which requests it has. This is only
+            // correct if the change in capabilities doesn't affect whether the network satisfies
+            // requests or not, and doesn't affect the network's score.
+            if (nri.request.isListen()) continue;
+
             final NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(nri.request.requestId);
             final boolean satisfies = newNetwork.satisfies(nri.request);
             if (newNetwork == currentNetwork && satisfies) {
@@ -4633,32 +4909,24 @@
             // check if it satisfies the NetworkCapabilities
             if (VDBG) log("  checking if request is satisfied: " + nri.request);
             if (satisfies) {
-                if (!nri.isRequest()) {
-                    // This is not a request, it's a callback listener.
-                    // Add it to newNetwork regardless of score.
-                    if (newNetwork.addRequest(nri.request)) addedRequests.add(nri);
-                    continue;
-                }
-
                 // next check if it's better than any current network we're using for
                 // this request
                 if (VDBG) {
                     log("currentScore = " +
                             (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) +
-                            ", newScore = " + newNetwork.getCurrentScore());
+                            ", newScore = " + score);
                 }
-                if (currentNetwork == null ||
-                        currentNetwork.getCurrentScore() < newNetwork.getCurrentScore()) {
+                if (currentNetwork == null || currentNetwork.getCurrentScore() < score) {
                     if (VDBG) log("rematch for " + newNetwork.name());
                     if (currentNetwork != null) {
                         if (VDBG) log("   accepting network in place of " + currentNetwork.name());
-                        currentNetwork.networkRequests.remove(nri.request.requestId);
-                        currentNetwork.networkLingered.add(nri.request);
+                        currentNetwork.removeRequest(nri.request.requestId);
+                        currentNetwork.lingerRequest(nri.request, now, mLingerDelayMs);
                         affectedNetworks.add(currentNetwork);
                     } else {
                         if (VDBG) log("   accepting network in place of null");
                     }
-                    unlinger(newNetwork);
+                    newNetwork.unlingerRequest(nri.request);
                     mNetworkForRequestId.put(nri.request.requestId, newNetwork);
                     if (!newNetwork.addRequest(nri.request)) {
                         Slog.wtf(TAG, "BUG: " + newNetwork.name() + " already has " + nri.request);
@@ -4670,16 +4938,19 @@
                     // TODO - this could get expensive if we have alot of requests for this
                     // network.  Think about if there is a way to reduce this.  Push
                     // netid->request mapping to each factory?
-                    sendUpdatedScoreToFactories(nri.request, newNetwork.getCurrentScore());
-                    if (mDefaultRequest.requestId == nri.request.requestId) {
+                    sendUpdatedScoreToFactories(nri.request, score);
+                    if (isDefaultRequest(nri)) {
                         isNewDefault = true;
                         oldDefaultNetwork = currentNetwork;
+                        if (currentNetwork != null) {
+                            mLingerMonitor.noteLingerDefaultNetwork(currentNetwork, newNetwork);
+                        }
                     }
                 }
-            } else if (newNetwork.networkRequests.get(nri.request.requestId) != null) {
+            } else if (newNetwork.isSatisfyingRequest(nri.request.requestId)) {
                 // If "newNetwork" is listed as satisfying "nri" but no longer satisfies "nri",
                 // mark it as no longer satisfying "nri".  Because networks are processed by
-                // rematchAllNetworkAndRequests() in descending score order, "currentNetwork" will
+                // rematchAllNetworksAndRequests() in descending score order, "currentNetwork" will
                 // match "newNetwork" before this loop will encounter a "currentNetwork" with higher
                 // score than "newNetwork" and where "currentNetwork" no longer satisfies "nri".
                 // This means this code doesn't have to handle the case where "currentNetwork" no
@@ -4688,41 +4959,23 @@
                     log("Network " + newNetwork.name() + " stopped satisfying" +
                             " request " + nri.request.requestId);
                 }
-                newNetwork.networkRequests.remove(nri.request.requestId);
+                newNetwork.removeRequest(nri.request.requestId);
                 if (currentNetwork == newNetwork) {
                     mNetworkForRequestId.remove(nri.request.requestId);
                     sendUpdatedScoreToFactories(nri.request, 0);
                 } else {
-                    if (nri.isRequest()) {
-                        Slog.wtf(TAG, "BUG: Removing request " + nri.request.requestId + " from " +
-                                newNetwork.name() +
-                                " without updating mNetworkForRequestId or factories!");
-                    }
+                    Slog.wtf(TAG, "BUG: Removing request " + nri.request.requestId + " from " +
+                            newNetwork.name() +
+                            " without updating mNetworkForRequestId or factories!");
                 }
-                // TODO: technically, sending CALLBACK_LOST here is
-                // incorrect if nri is a request (not a listen) and there
-                // is a replacement network currently connected that can
-                // satisfy it. However, the only capability that can both
+                // TODO: Technically, sending CALLBACK_LOST here is
+                // incorrect if there is a replacement network currently
+                // connected that can satisfy nri, which is a request
+                // (not a listen). However, the only capability that can both
                 // a) be requested and b) change is NET_CAPABILITY_TRUSTED,
                 // so this code is only incorrect for a network that loses
                 // the TRUSTED capability, which is a rare case.
-                callCallbackForRequest(nri, newNetwork, ConnectivityManager.CALLBACK_LOST);
-            }
-        }
-        // Linger any networks that are no longer needed.
-        for (NetworkAgentInfo nai : affectedNetworks) {
-            if (nai.lingering) {
-                // Already lingered.  Nothing to do.  This can only happen if "nai" is in
-                // "affectedNetworks" twice.  The reasoning being that to get added to
-                // "affectedNetworks", "nai" must have been satisfying a NetworkRequest
-                // (i.e. not lingered) so it could have only been lingered by this loop.
-                // unneeded(nai) will be false and we'll call unlinger() below which would
-                // be bad, so handle it here.
-            } else if (unneeded(nai)) {
-                linger(nai);
-            } else {
-                // Clear nai.networkLingered we might have added above.
-                unlinger(nai);
+                callCallbackForRequest(nri, newNetwork, ConnectivityManager.CALLBACK_LOST, 0);
             }
         }
         if (isNewDefault) {
@@ -4743,10 +4996,41 @@
             }
         }
 
+        if (!newNetwork.networkCapabilities.equalRequestableCapabilities(nc)) {
+            Slog.wtf(TAG, String.format(
+                    "BUG: %s changed requestable capabilities during rematch: %s -> %s",
+                    nc, newNetwork.networkCapabilities));
+        }
+        if (newNetwork.getCurrentScore() != score) {
+            Slog.wtf(TAG, String.format(
+                    "BUG: %s changed score during rematch: %d -> %d",
+                    score, newNetwork.getCurrentScore()));
+        }
+
+        // Second pass: process all listens.
+        if (wasBackgroundNetwork != newNetwork.isBackgroundNetwork()) {
+            // If the network went from background to foreground or vice versa, we need to update
+            // its foreground state. It is safe to do this after rematching the requests because
+            // NET_CAPABILITY_FOREGROUND does not affect requests, as is not a requestable
+            // capability and does not affect the network's score (see the Slog.wtf call above).
+            updateCapabilities(score, newNetwork, newNetwork.networkCapabilities);
+        } else {
+            processListenRequests(newNetwork, false);
+        }
+
         // do this after the default net is switched, but
         // before LegacyTypeTracker sends legacy broadcasts
         for (NetworkRequestInfo nri : addedRequests) notifyNetworkCallback(newNetwork, nri);
 
+        // Linger any networks that are no longer needed. This should be done after sending the
+        // available callback for newNetwork.
+        for (NetworkAgentInfo nai : affectedNetworks) {
+            updateLingerState(nai, now);
+        }
+        // Possibly unlinger newNetwork. Unlingering a network does not send any callbacks so it
+        // does not need to be done in any particular order.
+        updateLingerState(newNetwork, now);
+
         if (isNewDefault) {
             // Maintain the illusion: since the legacy API only
             // understands one network at a time, we must pretend
@@ -4793,9 +5077,9 @@
             // (notification callbacks) and then uses the old api (getNetworkInfo(type))
             // they may get old info.  Reverse this after the old startUsing api is removed.
             // This is on top of the multiple intent sequencing referenced in the todo above.
-            for (int i = 0; i < newNetwork.networkRequests.size(); i++) {
-                NetworkRequest nr = newNetwork.networkRequests.valueAt(i);
-                if (nr.legacyType != TYPE_NONE && isRequest(nr)) {
+            for (int i = 0; i < newNetwork.numNetworkRequests(); i++) {
+                NetworkRequest nr = newNetwork.requestAt(i);
+                if (nr.legacyType != TYPE_NONE && nr.isRequest()) {
                     // legacy type tracker filters out repeat adds
                     mLegacyTypeTracker.add(nr.legacyType, newNetwork);
                 }
@@ -4811,9 +5095,20 @@
         }
         if (reapUnvalidatedNetworks == ReapUnvalidatedNetworks.REAP) {
             for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
-                if (unneeded(nai)) {
-                    if (DBG) log("Reaping " + nai.name());
-                    teardownUnneededNetwork(nai);
+                if (unneeded(nai, UnneededFor.TEARDOWN)) {
+                    if (nai.getLingerExpiry() > 0) {
+                        // This network has active linger timers and no requests, but is not
+                        // lingering. Linger it.
+                        //
+                        // One way (the only way?) this can happen if this network is unvalidated
+                        // and became unneeded due to another network improving its score to the
+                        // point where this network will no longer be able to satisfy any requests
+                        // even if it validates.
+                        updateLingerState(nai, now);
+                    } else {
+                        if (DBG) log("Reaping " + nai.name());
+                        teardownUnneededNetwork(nai);
+                    }
                 }
             }
         }
@@ -4840,8 +5135,9 @@
         // Optimization: Only reprocess "changed" if its score improved.  This is safe because it
         // can only add more NetworkRequests satisfied by "changed", and this is exactly what
         // rematchNetworkAndRequests() handles.
+        final long now = SystemClock.elapsedRealtime();
         if (changed != null && oldScore < changed.getCurrentScore()) {
-            rematchNetworkAndRequests(changed, ReapUnvalidatedNetworks.REAP);
+            rematchNetworkAndRequests(changed, ReapUnvalidatedNetworks.REAP, now);
         } else {
             final NetworkAgentInfo[] nais = mNetworkAgentInfos.values().toArray(
                     new NetworkAgentInfo[mNetworkAgentInfos.size()]);
@@ -4855,7 +5151,8 @@
                         // is complete could incorrectly teardown a network that hasn't yet been
                         // rematched.
                         (nai != nais[nais.length-1]) ? ReapUnvalidatedNetworks.DONT_REAP
-                                : ReapUnvalidatedNetworks.REAP);
+                                : ReapUnvalidatedNetworks.REAP,
+                        now);
             }
         }
     }
@@ -4912,6 +5209,10 @@
         if (!networkAgent.created
                 && (state == NetworkInfo.State.CONNECTED
                 || (state == NetworkInfo.State.CONNECTING && networkAgent.isVPN()))) {
+
+            // A network that has just connected has zero requests and is thus a foreground network.
+            networkAgent.networkCapabilities.addCapability(NET_CAPABILITY_FOREGROUND);
+
             try {
                 // This should never fail.  Specifying an already in use NetID will cause failure.
                 if (networkAgent.isVPN()) {
@@ -4921,9 +5222,7 @@
                                 !networkAgent.networkMisc.allowBypass));
                 } else {
                     mNetd.createPhysicalNetwork(networkAgent.network.netId,
-                            networkAgent.networkCapabilities.hasCapability(
-                                    NET_CAPABILITY_NOT_RESTRICTED) ?
-                                    null : NetworkManagementService.PERMISSION_SYSTEM);
+                            getNetworkPermission(networkAgent.networkCapabilities));
                 }
             } catch (Exception e) {
                 loge("Error creating network " + networkAgent.network.netId + ": "
@@ -4965,7 +5264,8 @@
             updateSignalStrengthThresholds(networkAgent, "CONNECT", null);
 
             // Consider network even though it is not yet validated.
-            rematchNetworkAndRequests(networkAgent, ReapUnvalidatedNetworks.REAP);
+            final long now = SystemClock.elapsedRealtime();
+            rematchNetworkAndRequests(networkAgent, ReapUnvalidatedNetworks.REAP, now);
 
             // This has to happen after matching the requests, because callbacks are just requests.
             notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK);
@@ -5013,14 +5313,8 @@
     // notify only this one new request of the current state
     protected void notifyNetworkCallback(NetworkAgentInfo nai, NetworkRequestInfo nri) {
         int notifyType = ConnectivityManager.CALLBACK_AVAILABLE;
-        // TODO - read state from monitor to decide what to send.
-//        if (nai.networkMonitor.isLingering()) {
-//            notifyType = NetworkCallbacks.LOSING;
-//        } else if (nai.networkMonitor.isEvaluating()) {
-//            notifyType = NetworkCallbacks.callCallbackForRequest(request, nai, notifyType);
-//        }
         if (nri.mPendingIntent == null) {
-            callCallbackForRequest(nri, nai, notifyType);
+            callCallbackForRequest(nri, nai, notifyType, 0);
         } else {
             sendPendingIntentForRequest(nri, nai, notifyType);
         }
@@ -5054,7 +5348,7 @@
                 intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo());
             }
             NetworkAgentInfo newDefaultAgent = null;
-            if (nai.networkRequests.get(mDefaultRequest.requestId) != null) {
+            if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) {
                 newDefaultAgent = getDefaultNetwork();
                 if (newDefaultAgent != null) {
                     intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO,
@@ -5072,20 +5366,26 @@
         }
     }
 
-    protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) {
+    protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType, int arg1) {
         if (VDBG) log("notifyType " + notifyTypeToName(notifyType) + " for " + networkAgent.name());
-        for (int i = 0; i < networkAgent.networkRequests.size(); i++) {
-            NetworkRequest nr = networkAgent.networkRequests.valueAt(i);
+        for (int i = 0; i < networkAgent.numNetworkRequests(); i++) {
+            NetworkRequest nr = networkAgent.requestAt(i);
             NetworkRequestInfo nri = mNetworkRequests.get(nr);
             if (VDBG) log(" sending notification for " + nr);
+            // TODO: if we're in the middle of a rematch, can we send a CAP_CHANGED callback for
+            // a network that no longer satisfies the listen?
             if (nri.mPendingIntent == null) {
-                callCallbackForRequest(nri, networkAgent, notifyType);
+                callCallbackForRequest(nri, networkAgent, notifyType, arg1);
             } else {
                 sendPendingIntentForRequest(nri, networkAgent, notifyType);
             }
         }
     }
 
+    protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) {
+        notifyNetworkCallbacks(networkAgent, notifyType, 0);
+    }
+
     private String notifyTypeToName(int notifyType) {
         switch (notifyType) {
             case ConnectivityManager.CALLBACK_PRECHECK:    return "PRECHECK";
@@ -5145,7 +5445,7 @@
 
     @Override
     public String getCaptivePortalServerUrl() {
-        return NetworkMonitor.getCaptivePortalServerUrl(mContext);
+        return NetworkMonitor.getCaptivePortalServerHttpUrl(mContext);
     }
 
     @Override
@@ -5208,6 +5508,9 @@
                 }
             }
         }
+
+        Settings.Global.putString(mContext.getContentResolver(),
+                Settings.Global.NETWORK_AVOID_BAD_WIFI, null);
     }
 
     @VisibleForTesting
@@ -5216,7 +5519,17 @@
         return new NetworkMonitor(context, handler, nai, defaultRequest);
     }
 
-    private static void logDefaultNetworkEvent(NetworkAgentInfo newNai, NetworkAgentInfo prevNai) {
+    @VisibleForTesting
+    AvoidBadWifiTracker createAvoidBadWifiTracker(Context c, Handler h, Runnable r) {
+        return new AvoidBadWifiTracker(c, h, r);
+    }
+
+    @VisibleForTesting
+    public WakeupMessage makeWakeupMessage(Context c, Handler h, String s, int cmd, Object obj) {
+        return new WakeupMessage(c, h, s, cmd, 0, 0, obj);
+    }
+
+    private void logDefaultNetworkEvent(NetworkAgentInfo newNai, NetworkAgentInfo prevNai) {
         int newNetid = NETID_UNSET;
         int prevNetid = NETID_UNSET;
         int[] transports = new int[0];
@@ -5234,6 +5547,10 @@
             hadIPv6 = lp.hasGlobalIPv6Address() && lp.hasIPv6DefaultRoute();
         }
 
-        DefaultNetworkEvent.logEvent(newNetid, transports, prevNetid, hadIPv4, hadIPv6);
+        mMetricsLog.log(new DefaultNetworkEvent(newNetid, transports, prevNetid, hadIPv4, hadIPv6));
+    }
+
+    private void logNetworkEvent(NetworkAgentInfo nai, int evtype) {
+        mMetricsLog.log(new NetworkEvent(nai.network.netId, evtype));
     }
 }
diff --git a/services/core/java/com/android/server/DeviceIdleController.java b/services/core/java/com/android/server/DeviceIdleController.java
index afed5ef..4405c1b 100644
--- a/services/core/java/com/android/server/DeviceIdleController.java
+++ b/services/core/java/com/android/server/DeviceIdleController.java
@@ -973,13 +973,12 @@
                 cancelSensingTimeoutAlarmLocked();
             }
         }
-        if (result == AnyMotionDetector.RESULT_MOVED) {
-            if (DEBUG) Slog.d(TAG, "RESULT_MOVED received.");
+        if ((result == AnyMotionDetector.RESULT_MOVED) ||
+            (result == AnyMotionDetector.RESULT_UNKNOWN)) {
             synchronized (this) {
-                handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "sense_motion");
+                handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "non_stationary");
             }
         } else if (result == AnyMotionDetector.RESULT_STATIONARY) {
-            if (DEBUG) Slog.d(TAG, "RESULT_STATIONARY received.");
             if (mState == STATE_SENSING) {
                 // If we are currently sensing, it is time to move to locating.
                 synchronized (this) {
@@ -2727,12 +2726,12 @@
                 }
             }
         } else if ("whitelist".equals(cmd)) {
-            long token = Binder.clearCallingIdentity();
-            try {
-                String arg = shell.getNextArg();
-                if (arg != null) {
-                    getContext().enforceCallingOrSelfPermission(
-                            android.Manifest.permission.DEVICE_POWER, null);
+            String arg = shell.getNextArg();
+            if (arg != null) {
+                getContext().enforceCallingOrSelfPermission(
+                        android.Manifest.permission.DEVICE_POWER, null);
+                long token = Binder.clearCallingIdentity();
+                try {
                     do {
                         if (arg.length() < 1 || (arg.charAt(0) != '-'
                                 && arg.charAt(0) != '+' && arg.charAt(0) != '=')) {
@@ -2755,30 +2754,30 @@
                             pw.println(getPowerSaveWhitelistAppInternal(pkg));
                         }
                     } while ((arg=shell.getNextArg()) != null);
-                } else {
-                    synchronized (this) {
-                        for (int j=0; j<mPowerSaveWhitelistAppsExceptIdle.size(); j++) {
-                            pw.print("system-excidle,");
-                            pw.print(mPowerSaveWhitelistAppsExceptIdle.keyAt(j));
-                            pw.print(",");
-                            pw.println(mPowerSaveWhitelistAppsExceptIdle.valueAt(j));
-                        }
-                        for (int j=0; j<mPowerSaveWhitelistApps.size(); j++) {
-                            pw.print("system,");
-                            pw.print(mPowerSaveWhitelistApps.keyAt(j));
-                            pw.print(",");
-                            pw.println(mPowerSaveWhitelistApps.valueAt(j));
-                        }
-                        for (int j=0; j<mPowerSaveWhitelistUserApps.size(); j++) {
-                            pw.print("user,");
-                            pw.print(mPowerSaveWhitelistUserApps.keyAt(j));
-                            pw.print(",");
-                            pw.println(mPowerSaveWhitelistUserApps.valueAt(j));
-                        }
+                } finally {
+                    Binder.restoreCallingIdentity(token);
+                }
+            } else {
+                synchronized (this) {
+                    for (int j=0; j<mPowerSaveWhitelistAppsExceptIdle.size(); j++) {
+                        pw.print("system-excidle,");
+                        pw.print(mPowerSaveWhitelistAppsExceptIdle.keyAt(j));
+                        pw.print(",");
+                        pw.println(mPowerSaveWhitelistAppsExceptIdle.valueAt(j));
+                    }
+                    for (int j=0; j<mPowerSaveWhitelistApps.size(); j++) {
+                        pw.print("system,");
+                        pw.print(mPowerSaveWhitelistApps.keyAt(j));
+                        pw.print(",");
+                        pw.println(mPowerSaveWhitelistApps.valueAt(j));
+                    }
+                    for (int j=0; j<mPowerSaveWhitelistUserApps.size(); j++) {
+                        pw.print("user,");
+                        pw.print(mPowerSaveWhitelistUserApps.keyAt(j));
+                        pw.print(",");
+                        pw.println(mPowerSaveWhitelistUserApps.valueAt(j));
                     }
                 }
-            } finally {
-                Binder.restoreCallingIdentity(token);
             }
         } else if ("tempwhitelist".equals(cmd)) {
             String opt;
diff --git a/services/core/java/com/android/server/GestureLauncherService.java b/services/core/java/com/android/server/GestureLauncherService.java
index 5a90488..553cb07 100644
--- a/services/core/java/com/android/server/GestureLauncherService.java
+++ b/services/core/java/com/android/server/GestureLauncherService.java
@@ -33,6 +33,7 @@
 import android.os.PowerManager.WakeLock;
 import android.os.SystemClock;
 import android.os.SystemProperties;
+import android.os.UserHandle;
 import android.provider.Settings;
 import android.util.MutableBoolean;
 import android.util.Slog;
@@ -284,8 +285,8 @@
      * @return true if camera was launched, false otherwise.
      */
     private boolean handleCameraLaunchGesture(boolean useWakelock, int source) {
-        boolean userSetupComplete = Settings.Secure.getInt(mContext.getContentResolver(),
-                Settings.Secure.USER_SETUP_COMPLETE, 0) != 0;
+        boolean userSetupComplete = Settings.Secure.getIntForUser(mContext.getContentResolver(),
+                Settings.Secure.USER_SETUP_COMPLETE, 0, UserHandle.USER_CURRENT) != 0;
         if (!userSetupComplete) {
             if (DBG) Slog.d(TAG, String.format(
                     "userSetupComplete = %s, ignoring camera launch gesture.",
diff --git a/services/core/java/com/android/server/InputContentUriTokenHandler.java b/services/core/java/com/android/server/InputContentUriTokenHandler.java
new file mode 100644
index 0000000..3f4972b
--- /dev/null
+++ b/services/core/java/com/android/server/InputContentUriTokenHandler.java
@@ -0,0 +1,121 @@
+/*
+** Copyright 2016, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+
+package com.android.server;
+
+import android.annotation.NonNull;
+import android.annotation.UserIdInt;
+import android.app.ActivityManagerNative;
+import android.content.Intent;
+import android.net.Uri;
+import android.os.Binder;
+import android.os.IBinder;
+import android.os.RemoteException;
+
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.inputmethod.IInputContentUriToken;
+
+final class InputContentUriTokenHandler extends IInputContentUriToken.Stub {
+
+    @NonNull
+    private final Uri mUri;
+    private final int mSourceUid;
+    @NonNull
+    private final String mTargetPackage;
+    @UserIdInt
+    private final int mSourceUserId;
+    @UserIdInt
+    private final int mTargetUserId;
+
+    private final Object mLock = new Object();
+
+    @GuardedBy("mLock")
+    private IBinder mPermissionOwnerToken = null;
+
+    InputContentUriTokenHandler(@NonNull Uri contentUri, int sourceUid,
+            @NonNull String targetPackage, @UserIdInt int sourceUserId,
+            @UserIdInt int targetUserId) {
+        mUri = contentUri;
+        mSourceUid = sourceUid;
+        mTargetPackage = targetPackage;
+        mSourceUserId = sourceUserId;
+        mTargetUserId = targetUserId;
+    }
+
+    @Override
+    public void take() {
+        synchronized (mLock) {
+            if (mPermissionOwnerToken != null) {
+                // Permission is already granted.
+                return;
+            }
+
+            try {
+                mPermissionOwnerToken = ActivityManagerNative.getDefault()
+                        .newUriPermissionOwner("InputContentUriTokenHandler");
+            } catch (RemoteException e) {
+                e.rethrowFromSystemServer();
+            }
+
+            doTakeLocked(mPermissionOwnerToken);
+        }
+    }
+
+    private void doTakeLocked(@NonNull IBinder permissionOwner) {
+        long origId = Binder.clearCallingIdentity();
+        try {
+            try {
+                ActivityManagerNative.getDefault().grantUriPermissionFromOwner(
+                        permissionOwner, mSourceUid, mTargetPackage, mUri,
+                        Intent.FLAG_GRANT_READ_URI_PERMISSION, mSourceUserId, mTargetUserId);
+            } catch (RemoteException e) {
+                e.rethrowFromSystemServer();
+            }
+        } finally {
+            Binder.restoreCallingIdentity(origId);
+        }
+    }
+
+    @Override
+    public void release() {
+        synchronized (mLock) {
+            if (mPermissionOwnerToken == null) {
+                return;
+            }
+            try {
+                ActivityManagerNative.getDefault().revokeUriPermissionFromOwner(
+                        mPermissionOwnerToken, mUri,
+                        Intent.FLAG_GRANT_READ_URI_PERMISSION, mSourceUserId);
+            } catch (RemoteException e) {
+                e.rethrowFromSystemServer();
+            } finally {
+                mPermissionOwnerToken = null;
+            }
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    protected void finalize() throws Throwable {
+        try {
+            release();
+        } finally {
+            super.finalize();
+        }
+    }
+}
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index 76b1011..df1b6f5 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -18,6 +18,7 @@
 import static java.lang.annotation.RetentionPolicy.SOURCE;
 
 import com.android.internal.content.PackageMonitor;
+import com.android.internal.inputmethod.IInputContentUriToken;
 import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
 import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
 import com.android.internal.inputmethod.InputMethodUtils;
@@ -137,6 +138,7 @@
 import java.io.PrintWriter;
 import java.lang.annotation.Retention;
 import java.nio.charset.StandardCharsets;
+import java.security.InvalidParameterException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -175,6 +177,8 @@
 
     static final int MSG_HARD_KEYBOARD_SWITCH_CHANGED = 4000;
 
+    static final int MSG_SYSTEM_UNLOCK_USER = 5000;
+
     static final long TIME_TO_RECONNECT = 3 * 1000;
 
     static final int SECURE_SUGGESTION_SPANS_MAX_SIZE = 20;
@@ -798,14 +802,14 @@
 
         @Override
         public void onSwitchUser(@UserIdInt int userHandle) {
-            // Called on the system server's main looper thread.
+            // Called on ActivityManager thread.
             // TODO: Dispatch this to a worker thread as needed.
             mService.onSwitchUser(userHandle);
         }
 
         @Override
         public void onBootPhase(int phase) {
-            // Called on the system server's main looper thread.
+            // Called on ActivityManager thread.
             // TODO: Dispatch this to a worker thread as needed.
             if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
                 StatusBarManagerService statusBarService = (StatusBarManagerService) ServiceManager
@@ -815,10 +819,10 @@
         }
 
         @Override
-        public void onUnlockUser(@UserIdInt int userHandle) {
-            // Called on the system server's main looper thread.
-            // TODO: Dispatch this to a worker thread as needed.
-            mService.onUnlockUser(userHandle);
+        public void onUnlockUser(final @UserIdInt int userHandle) {
+            // Called on ActivityManager thread.
+            mService.mHandler.sendMessage(mService.mHandler.obtainMessage(MSG_SYSTEM_UNLOCK_USER,
+                    userHandle /* arg1 */, 0 /* arg2 */));
         }
     }
 
@@ -2968,6 +2972,10 @@
             case MSG_HARD_KEYBOARD_SWITCH_CHANGED:
                 mHardKeyboardListener.handleHardKeyboardStatusChange(msg.arg1 == 1);
                 return true;
+            case MSG_SYSTEM_UNLOCK_USER:
+                final int userId = msg.arg1;
+                onUnlockUser(userId);
+                return true;
         }
         return false;
     }
@@ -3911,6 +3919,52 @@
     }
 
     @Override
+    public IInputContentUriToken createInputContentUriToken(@Nullable IBinder token,
+            @Nullable Uri contentUri, @Nullable String packageName) {
+        if (!calledFromValidUser()) {
+            return null;
+        }
+
+        if (token == null) {
+            throw new NullPointerException("token");
+        }
+        if (packageName == null) {
+            throw new NullPointerException("packageName");
+        }
+        if (contentUri == null) {
+            throw new NullPointerException("contentUri");
+        }
+        final String contentUriScheme = contentUri.getScheme();
+        if (!"content".equals(contentUriScheme)) {
+            throw new InvalidParameterException("contentUri must have content scheme");
+        }
+
+        synchronized (mMethodMap) {
+            final int uid = Binder.getCallingUid();
+            if (mCurMethodId == null) {
+                return null;
+            }
+            if (mCurToken != token) {
+                Slog.e(TAG, "Ignoring createInputContentUriToken mCurToken=" + mCurToken
+                        + " token=" + token);
+                return null;
+            }
+            // We cannot simply distinguish a bad IME that reports an arbitrary package name from
+            // an unfortunate IME whose internal state is already obsolete due to the asynchronous
+            // nature of our system.  Let's compare it with our internal record.
+            if (!TextUtils.equals(mCurAttribute.packageName, packageName)) {
+                Slog.e(TAG, "Ignoring createInputContentUriToken mCurAttribute.packageName="
+                    + mCurAttribute.packageName + " packageName=" + packageName);
+                return null;
+            }
+            final int imeUserId = UserHandle.getUserId(uid);
+            final int appUserId = UserHandle.getUserId(mCurClient.uid);
+            return new InputContentUriTokenHandler(contentUri, uid, packageName, imeUserId,
+                    appUserId);
+        }
+    }
+
+    @Override
     protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
         if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
                 != PackageManager.PERMISSION_GRANTED) {
diff --git a/services/core/java/com/android/server/IntentResolver.java b/services/core/java/com/android/server/IntentResolver.java
index 3fdcceb..83d374c 100644
--- a/services/core/java/com/android/server/IntentResolver.java
+++ b/services/core/java/com/android/server/IntentResolver.java
@@ -364,6 +364,7 @@
             buildResolveList(intent, categories, debug, defaultOnly,
                     resolvedType, scheme, listCut.get(i), resultList, userId);
         }
+        filterResults(resultList);
         sortResults(resultList);
         return resultList;
     }
@@ -457,6 +458,7 @@
             buildResolveList(intent, categories, debug, defaultOnly,
                     resolvedType, scheme, schemeCut, finalList, userId);
         }
+        filterResults(finalList);
         sortResults(finalList);
 
         if (debug) {
@@ -521,6 +523,12 @@
         Collections.sort(results, mResolvePrioritySorter);
     }
 
+    /**
+     * Apply filtering to the results. This happens before the results are sorted.
+     */
+    protected void filterResults(List<R> results) {
+    }
+
     protected void dumpFilter(PrintWriter out, String prefix, F filter) {
         out.print(prefix); out.println(filter);
     }
diff --git a/services/core/java/com/android/server/LockSettingsService.java b/services/core/java/com/android/server/LockSettingsService.java
index 42e75c6..a91e205 100644
--- a/services/core/java/com/android/server/LockSettingsService.java
+++ b/services/core/java/com/android/server/LockSettingsService.java
@@ -69,6 +69,7 @@
 import android.util.Slog;
 
 import com.android.internal.util.ArrayUtils;
+import com.android.internal.widget.ICheckCredentialProgressCallback;
 import com.android.internal.widget.ILockSettings;
 import com.android.internal.widget.LockPatternUtils;
 import com.android.internal.widget.VerifyCredentialResponse;
@@ -300,9 +301,12 @@
         for (int i = 0; i < users.size(); i++) {
             UserInfo user = users.get(i);
             UserHandle userHandle = user.getUserHandle();
-            if (!mUserManager.isUserUnlockingOrUnlocked(userHandle)) {
+            final boolean isSecure = mStorage.hasPassword(user.id) || mStorage.hasPattern(user.id);
+            if (isSecure && !mUserManager.isUserUnlockingOrUnlocked(userHandle)) {
                 if (!user.isManagedProfile()) {
-                    showEncryptionNotification(userHandle);
+                    // When the user is locked, we communicate it loud-and-clear
+                    // on the lockscreen; we only show a notification below for
+                    // locked managed profiles.
                 } else {
                     UserInfo parent = mUserManager.getProfileParent(user.id);
                     if (parent != null &&
@@ -338,21 +342,6 @@
         showEncryptionNotification(user, title, message, detail, intent);
     }
 
-    private void showEncryptionNotification(UserHandle user) {
-        Resources r = mContext.getResources();
-        CharSequence title = r.getText(
-                com.android.internal.R.string.user_encrypted_title);
-        CharSequence message = r.getText(
-                com.android.internal.R.string.user_encrypted_message);
-        CharSequence detail = r.getText(
-                com.android.internal.R.string.user_encrypted_detail);
-
-        PendingIntent intent = PendingIntent.getBroadcast(mContext, 0, ACTION_NULL,
-                PendingIntent.FLAG_UPDATE_CURRENT);
-
-        showEncryptionNotification(user, title, message, detail, intent);
-    }
-
     private void showEncryptionNotification(UserHandle user, CharSequence title, CharSequence message,
             CharSequence detail, PendingIntent intent) {
         if (DEBUG) Slog.v(TAG, "showing encryption notification, user: " + user.getIdentifier());
@@ -407,7 +396,9 @@
         List<UserInfo> profiles = mUserManager.getProfiles(userId);
         for (int i = 0; i < profiles.size(); i++) {
             UserInfo profile = profiles.get(i);
-            if (profile.isManagedProfile()) {
+            final boolean isSecure =
+                    mStorage.hasPassword(profile.id) || mStorage.hasPattern(profile.id);
+            if (isSecure && profile.isManagedProfile()) {
                 UserHandle userHandle = profile.getUserHandle();
                 if (!mUserManager.isUserUnlockingOrUnlocked(userHandle) &&
                         !mUserManager.isQuietModeEnabled(userHandle)) {
@@ -580,6 +571,18 @@
                         Slog.e(TAG, "Invalid tied profile lock type: " + quality);
                     }
                 }
+                try {
+                    final String alias = LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userInfo.id;
+                    java.security.KeyStore keyStore =
+                            java.security.KeyStore.getInstance("AndroidKeyStore");
+                    keyStore.load(null);
+                    if (keyStore.containsAlias(alias)) {
+                        keyStore.deleteEntry(alias);
+                    }
+                } catch (KeyStoreException | NoSuchAlgorithmException |
+                        CertificateException | IOException e) {
+                    Slog.e(TAG, "Unable to remove tied profile key", e);
+                }
             }
         } catch (RemoteException re) {
             Slog.e(TAG, "Unable to migrate old data", re);
@@ -757,7 +760,7 @@
     private void unlockChildProfile(int profileHandle) throws RemoteException {
         try {
             doVerifyPassword(getDecryptedPasswordForTiedProfile(profileHandle), false,
-                    0 /* no challenge */, profileHandle);
+                    0 /* no challenge */, profileHandle, null /* progressCallback */);
         } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException
                 | NoSuchAlgorithmException | NoSuchPaddingException
                 | InvalidAlgorithmParameterException | IllegalBlockSizeException
@@ -944,7 +947,7 @@
             CredentialHash willStore
                 = new CredentialHash(enrolledHandle, CredentialHash.VERSION_GATEKEEPER);
             setUserKeyProtection(userId, pattern,
-                doVerifyPattern(pattern, willStore, true, 0, userId));
+                doVerifyPattern(pattern, willStore, true, 0, userId, null /* progressCallback */));
             mStorage.writePatternHash(enrolledHandle, userId);
             fixateNewestUserKeyAuth(userId);
             onUserLockChanged(userId);
@@ -1004,7 +1007,8 @@
             CredentialHash willStore
                 = new CredentialHash(enrolledHandle, CredentialHash.VERSION_GATEKEEPER);
             setUserKeyProtection(userId, password,
-                doVerifyPassword(password, willStore, true, 0, userId));
+                doVerifyPassword(password, willStore, true, 0, userId,
+                        null /* progressCallback */));
             mStorage.writePasswordHash(enrolledHandle, userId);
             fixateNewestUserKeyAuth(userId);
             onUserLockChanged(userId);
@@ -1022,37 +1026,38 @@
             KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES);
             keyGenerator.init(new SecureRandom());
             SecretKey secretKey = keyGenerator.generateKey();
-
             java.security.KeyStore keyStore = java.security.KeyStore.getInstance("AndroidKeyStore");
             keyStore.load(null);
-            keyStore.setEntry(
-                    LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userId,
-                    new java.security.KeyStore.SecretKeyEntry(secretKey),
-                    new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT)
-                            .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
-                            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
-                            .build());
-            keyStore.setEntry(
-                    LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId,
-                    new java.security.KeyStore.SecretKeyEntry(secretKey),
-                    new KeyProtection.Builder(KeyProperties.PURPOSE_DECRYPT)
-                            .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
-                            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
-                            .setUserAuthenticationRequired(true)
-                            .setUserAuthenticationValidityDurationSeconds(30)
-                            .build());
-
-            // Key imported, obtain a reference to it.
-            SecretKey keyStoreEncryptionKey = (SecretKey) keyStore.getKey(
-                    LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userId, null);
-            // The original key can now be discarded.
-
-            Cipher cipher = Cipher.getInstance(
-                    KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/"
-                            + KeyProperties.ENCRYPTION_PADDING_NONE);
-            cipher.init(Cipher.ENCRYPT_MODE, keyStoreEncryptionKey);
-            encryptionResult = cipher.doFinal(randomLockSeed);
-            iv = cipher.getIV();
+            try {
+                keyStore.setEntry(
+                        LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userId,
+                        new java.security.KeyStore.SecretKeyEntry(secretKey),
+                        new KeyProtection.Builder(KeyProperties.PURPOSE_ENCRYPT)
+                                .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
+                                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
+                                .build());
+                keyStore.setEntry(
+                        LockPatternUtils.PROFILE_KEY_NAME_DECRYPT + userId,
+                        new java.security.KeyStore.SecretKeyEntry(secretKey),
+                        new KeyProtection.Builder(KeyProperties.PURPOSE_DECRYPT)
+                                .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
+                                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
+                                .setUserAuthenticationRequired(true)
+                                .setUserAuthenticationValidityDurationSeconds(30)
+                                .build());
+                // Key imported, obtain a reference to it.
+                SecretKey keyStoreEncryptionKey = (SecretKey) keyStore.getKey(
+                        LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userId, null);
+                Cipher cipher = Cipher.getInstance(
+                        KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_GCM + "/"
+                                + KeyProperties.ENCRYPTION_PADDING_NONE);
+                cipher.init(Cipher.ENCRYPT_MODE, keyStoreEncryptionKey);
+                encryptionResult = cipher.doFinal(randomLockSeed);
+                iv = cipher.getIV();
+            } finally {
+                // The original key can now be discarded.
+                keyStore.deleteEntry(LockPatternUtils.PROFILE_KEY_NAME_ENCRYPT + userId);
+            }
         } catch (CertificateException | UnrecoverableKeyException
                 | IOException | BadPaddingException | IllegalBlockSizeException | KeyStoreException
                 | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException e) {
@@ -1202,28 +1207,36 @@
     }
 
     @Override
-    public VerifyCredentialResponse checkPattern(String pattern, int userId) throws RemoteException {
-        return doVerifyPattern(pattern, false, 0, userId);
+    public VerifyCredentialResponse checkPattern(String pattern, int userId,
+            ICheckCredentialProgressCallback progressCallback) throws RemoteException {
+        return doVerifyPattern(pattern, false, 0, userId, progressCallback);
     }
 
     @Override
     public VerifyCredentialResponse verifyPattern(String pattern, long challenge, int userId)
             throws RemoteException {
-        return doVerifyPattern(pattern, true, challenge, userId);
+        return doVerifyPattern(pattern, true, challenge, userId, null /* progressCallback */);
     }
 
     private VerifyCredentialResponse doVerifyPattern(String pattern, boolean hasChallenge,
-            long challenge, int userId) throws RemoteException {
+            long challenge, int userId, ICheckCredentialProgressCallback progressCallback)
+            throws RemoteException {
        checkPasswordReadPermission(userId);
        if (TextUtils.isEmpty(pattern)) {
            throw new IllegalArgumentException("Pattern can't be null or empty");
        }
        CredentialHash storedHash = mStorage.readPatternHash(userId);
-       return doVerifyPattern(pattern, storedHash, hasChallenge, challenge, userId);
+       return doVerifyPattern(pattern, storedHash, hasChallenge, challenge, userId,
+               progressCallback);
     }
 
     private VerifyCredentialResponse doVerifyPattern(String pattern, CredentialHash storedHash,
-            boolean hasChallenge, long challenge, int userId) throws RemoteException {
+            boolean hasChallenge, long challenge, int userId,
+            ICheckCredentialProgressCallback progressCallback) throws RemoteException {
+
+       if (TextUtils.isEmpty(pattern)) {
+           throw new IllegalArgumentException("Pattern can't be null or empty");
+       }
        boolean shouldReEnrollBaseZero = storedHash != null && storedHash.isBaseZeroPattern;
 
        String patternToVerify;
@@ -1252,7 +1265,8 @@
                    public String adjustForKeystore(String pattern) {
                        return LockPatternUtils.patternStringToBaseZero(pattern);
                    }
-               }
+               },
+               progressCallback
        );
 
        if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK
@@ -1264,15 +1278,15 @@
     }
 
     @Override
-    public VerifyCredentialResponse checkPassword(String password, int userId)
-            throws RemoteException {
-        return doVerifyPassword(password, false, 0, userId);
+    public VerifyCredentialResponse checkPassword(String password, int userId,
+            ICheckCredentialProgressCallback progressCallback) throws RemoteException {
+        return doVerifyPassword(password, false, 0, userId, progressCallback);
     }
 
     @Override
     public VerifyCredentialResponse verifyPassword(String password, long challenge, int userId)
             throws RemoteException {
-        return doVerifyPassword(password, true, challenge, userId);
+        return doVerifyPassword(password, true, challenge, userId, null /* progressCallback */);
     }
 
     @Override
@@ -1285,8 +1299,10 @@
         final int parentProfileId = mUserManager.getProfileParent(userId).id;
         // Unlock parent by using parent's challenge
         final VerifyCredentialResponse parentResponse = isPattern
-                ? doVerifyPattern(password, true, challenge, parentProfileId)
-                : doVerifyPassword(password, true, challenge, parentProfileId);
+                ? doVerifyPattern(password, true, challenge, parentProfileId,
+                        null /* progressCallback */)
+                : doVerifyPassword(password, true, challenge, parentProfileId,
+                        null /* progressCallback */);
         if (parentResponse.getResponseCode() != VerifyCredentialResponse.RESPONSE_OK) {
             // Failed, just return parent's response
             return parentResponse;
@@ -1296,7 +1312,7 @@
             // Unlock work profile, and work profile with unified lock must use password only
             return doVerifyPassword(getDecryptedPasswordForTiedProfile(userId), true,
                     challenge,
-                    userId);
+                    userId, null /* progressCallback */);
         } catch (UnrecoverableKeyException | InvalidKeyException | KeyStoreException
                 | NoSuchAlgorithmException | NoSuchPaddingException
                 | InvalidAlgorithmParameterException | IllegalBlockSizeException
@@ -1307,17 +1323,23 @@
     }
 
     private VerifyCredentialResponse doVerifyPassword(String password, boolean hasChallenge,
-            long challenge, int userId) throws RemoteException {
+            long challenge, int userId, ICheckCredentialProgressCallback progressCallback)
+            throws RemoteException {
        checkPasswordReadPermission(userId);
        if (TextUtils.isEmpty(password)) {
            throw new IllegalArgumentException("Password can't be null or empty");
        }
        CredentialHash storedHash = mStorage.readPasswordHash(userId);
-       return doVerifyPassword(password, storedHash, hasChallenge, challenge, userId);
+       return doVerifyPassword(password, storedHash, hasChallenge, challenge, userId,
+               progressCallback);
     }
 
     private VerifyCredentialResponse doVerifyPassword(String password, CredentialHash storedHash,
-            boolean hasChallenge, long challenge, int userId) throws RemoteException {
+            boolean hasChallenge, long challenge, int userId,
+            ICheckCredentialProgressCallback progressCallback) throws RemoteException {
+       if (TextUtils.isEmpty(password)) {
+           throw new IllegalArgumentException("Password can't be null or empty");
+       }
        return verifyCredential(userId, storedHash, password, hasChallenge, challenge,
                new CredentialUtil() {
                    @Override
@@ -1335,12 +1357,12 @@
                    public String adjustForKeystore(String password) {
                        return password;
                    }
-               }
-       );
+               }, progressCallback);
     }
 
     private VerifyCredentialResponse verifyCredential(int userId, CredentialHash storedHash,
-            String credential, boolean hasChallenge, long challenge, CredentialUtil credentialUtil)
+            String credential, boolean hasChallenge, long challenge, CredentialUtil credentialUtil,
+            ICheckCredentialProgressCallback progressCallback)
                 throws RemoteException {
         if ((storedHash == null || storedHash.hash.length == 0) && TextUtils.isEmpty(credential)) {
             // don't need to pass empty credentials to GateKeeper
@@ -1397,7 +1419,13 @@
         }
 
         if (response.getResponseCode() == VerifyCredentialResponse.RESPONSE_OK) {
+
+
             // credential has matched
+
+            if (progressCallback != null) {
+                progressCallback.onCredentialVerified();
+            }
             unlockKeystore(credential, userId);
 
             Slog.i(TAG, "Unlocking user " + userId +
@@ -1453,7 +1481,7 @@
 
         try {
             if (mLockPatternUtils.isLockPatternEnabled(userId)) {
-                if (checkPattern(password, userId).getResponseCode()
+                if (checkPattern(password, userId, null /* progressCallback */).getResponseCode()
                         == GateKeeperResponse.RESPONSE_OK) {
                     return true;
                 }
@@ -1463,7 +1491,7 @@
 
         try {
             if (mLockPatternUtils.isLockPasswordEnabled(userId)) {
-                if (checkPassword(password, userId).getResponseCode()
+                if (checkPassword(password, userId, null /* progressCallback */).getResponseCode()
                         == GateKeeperResponse.RESPONSE_OK) {
                     return true;
                 }
diff --git a/services/core/java/com/android/server/MasterClearReceiver.java b/services/core/java/com/android/server/MasterClearReceiver.java
index 1653db9..6f8edec 100644
--- a/services/core/java/com/android/server/MasterClearReceiver.java
+++ b/services/core/java/com/android/server/MasterClearReceiver.java
@@ -47,6 +47,7 @@
         final String reason = intent.getStringExtra(Intent.EXTRA_REASON);
         final boolean wipeExternalStorage = intent.getBooleanExtra(
                 Intent.EXTRA_WIPE_EXTERNAL_STORAGE, false);
+        final boolean forceWipe = intent.getBooleanExtra(Intent.EXTRA_FORCE_MASTER_CLEAR, false);
 
         Slog.w(TAG, "!!! FACTORY RESET !!!");
         // The reboot call is blocking, so we need to do it on another thread.
@@ -54,7 +55,7 @@
             @Override
             public void run() {
                 try {
-                    RecoverySystem.rebootWipeUserData(context, shutdown, reason);
+                    RecoverySystem.rebootWipeUserData(context, shutdown, reason, forceWipe);
                     Log.wtf(TAG, "Still running after master clear?!");
                 } catch (IOException e) {
                     Slog.e(TAG, "Can't perform master clear/factory reset", e);
diff --git a/services/core/java/com/android/server/MountService.java b/services/core/java/com/android/server/MountService.java
index 2cf93ab..b9a4831 100644
--- a/services/core/java/com/android/server/MountService.java
+++ b/services/core/java/com/android/server/MountService.java
@@ -1056,6 +1056,10 @@
                         || mForceAdoptable) {
                     flags |= DiskInfo.FLAG_ADOPTABLE;
                 }
+                // Adoptable storage isn't currently supported on FBE devices
+                if (StorageManager.isFileEncryptedNativeOnly()) {
+                    flags &= ~DiskInfo.FLAG_ADOPTABLE;
+                }
                 mDisks.put(id, new DiskInfo(id, flags));
                 break;
             }
@@ -1433,13 +1437,22 @@
      * Decide if volume is mountable per device policies.
      */
     private boolean isMountDisallowed(VolumeInfo vol) {
-        if (vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_PRIVATE) {
-            final UserManager userManager = mContext.getSystemService(UserManager.class);
-            return userManager.hasUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
+        UserManager userManager = mContext.getSystemService(UserManager.class);
+
+        boolean isUsbRestricted = false;
+        if (vol.disk != null && vol.disk.isUsb()) {
+            isUsbRestricted = userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER,
                     Binder.getCallingUserHandle());
-        } else {
-            return false;
         }
+
+        boolean isTypeRestricted = false;
+        if (vol.type == VolumeInfo.TYPE_PUBLIC || vol.type == VolumeInfo.TYPE_PRIVATE) {
+            isTypeRestricted = userManager
+                    .hasUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA,
+                    Binder.getCallingUserHandle());
+        }
+
+        return isUsbRestricted || isTypeRestricted;
     }
 
     private void enforceAdminUser() {
@@ -1985,6 +1998,11 @@
         }
 
         if ((mask & StorageManager.DEBUG_FORCE_ADOPTABLE) != 0) {
+            if (StorageManager.isFileEncryptedNativeOnly()) {
+                throw new IllegalStateException(
+                        "Adoptable storage not available on device with native FBE");
+            }
+
             synchronized (mLock) {
                 mForceAdoptable = (flags & StorageManager.DEBUG_FORCE_ADOPTABLE) != 0;
 
diff --git a/services/core/java/com/android/server/NativeDaemonConnectorException.java b/services/core/java/com/android/server/NativeDaemonConnectorException.java
index 590bbcc..4d8881c 100644
--- a/services/core/java/com/android/server/NativeDaemonConnectorException.java
+++ b/services/core/java/com/android/server/NativeDaemonConnectorException.java
@@ -41,7 +41,7 @@
     }
 
     public int getCode() {
-        return mEvent.getCode();
+        return mEvent != null ? mEvent.getCode() : -1;
     }
 
     public String getCmd() {
diff --git a/services/core/java/com/android/server/NetworkManagementService.java b/services/core/java/com/android/server/NetworkManagementService.java
index d3d7331..8f16504 100644
--- a/services/core/java/com/android/server/NetworkManagementService.java
+++ b/services/core/java/com/android/server/NetworkManagementService.java
@@ -494,7 +494,7 @@
             if (mLastPowerStateFromWifi != powerState) {
                 mLastPowerStateFromWifi = powerState;
                 try {
-                    getBatteryStats().noteWifiRadioPowerState(powerState, tsNanos);
+                    getBatteryStats().noteWifiRadioPowerState(powerState, tsNanos, uid);
                 } catch (RemoteException e) {
                 }
             }
@@ -971,6 +971,17 @@
     //
     // INetworkManagementService members
     //
+    @Override
+    public INetd getNetdService() throws RemoteException {
+        final CountDownLatch connectedSignal = mConnectedSignal;
+        if (connectedSignal != null) {
+            try {
+                connectedSignal.await();
+            } catch (InterruptedException ignored) {}
+        }
+
+        return mNetdService;
+    }
 
     @Override
     public String[] listInterfaces() {
@@ -1299,8 +1310,9 @@
             mConnector.execute("tether", "interface", "remove", iface);
         } catch (NativeDaemonConnectorException e) {
             throw e.rethrowAsParcelableException();
+        } finally {
+            removeInterfaceFromLocalNetwork(iface);
         }
-        removeInterfaceFromLocalNetwork(iface);
     }
 
     @Override
@@ -2647,4 +2659,19 @@
     public void removeInterfaceFromLocalNetwork(String iface) {
         modifyInterfaceInNetwork("remove", "local", iface);
     }
+
+    @Override
+    public int removeRoutesFromLocalNetwork(List<RouteInfo> routes) {
+        int failures = 0;
+
+        for (RouteInfo route : routes) {
+            try {
+                modifyRoute("remove", "local", route);
+            } catch (IllegalStateException e) {
+                failures++;
+            }
+        }
+
+        return failures;
+    }
 }
diff --git a/services/core/java/com/android/server/PersistentDataBlockService.java b/services/core/java/com/android/server/PersistentDataBlockService.java
index 680547a..080b46c 100644
--- a/services/core/java/com/android/server/PersistentDataBlockService.java
+++ b/services/core/java/com/android/server/PersistentDataBlockService.java
@@ -26,6 +26,7 @@
 import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.provider.Settings;
 import android.service.persistentdata.IPersistentDataBlockService;
 import android.service.persistentdata.PersistentDataBlockManager;
 import android.util.Slog;
@@ -155,6 +156,14 @@
                     "Only the Admin user is allowed to change OEM unlock state");
         }
     }
+
+    private void enforceUserRestriction(String userRestriction) {
+        if (UserManager.get(mContext).hasUserRestriction(userRestriction)) {
+            throw new SecurityException(
+                    "OEM unlock is disallowed by user restriction: " + userRestriction);
+        }
+    }
+
     private int getTotalDataSizeLocked(DataInputStream inputStream) throws IOException {
         // skip over checksum
         inputStream.skipBytes(DIGEST_SIZE_BYTES);
@@ -447,14 +456,20 @@
         }
 
         @Override
-        public void setOemUnlockEnabled(boolean enabled) {
+        public void setOemUnlockEnabled(boolean enabled) throws SecurityException {
             // do not allow monkey to flip the flag
             if (ActivityManager.isUserAMonkey()) {
                 return;
             }
+
             enforceOemUnlockWritePermission();
             enforceIsAdmin();
 
+            if (enabled) {
+                // Do not allow oem unlock to be enabled if it's disallowed by a user restriction.
+                enforceUserRestriction(UserManager.DISALLOW_OEM_UNLOCK);
+                enforceUserRestriction(UserManager.DISALLOW_FACTORY_RESET);
+            }
             synchronized (mLock) {
                 doSetOemUnlockEnabledLocked(enabled);
                 computeAndWriteDigestLocked();
diff --git a/services/core/java/com/android/server/PinnerService.java b/services/core/java/com/android/server/PinnerService.java
index d48aeed..356ccb3 100644
--- a/services/core/java/com/android/server/PinnerService.java
+++ b/services/core/java/com/android/server/PinnerService.java
@@ -17,109 +17,372 @@
 package com.android.server;
 
 import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
-import android.content.res.Resources;
-import android.util.EventLog;
-import android.util.Slog;
+import android.content.pm.ResolveInfo;
 import android.os.Binder;
 import android.os.Build;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.os.UserHandle;
+import android.provider.MediaStore;
 import android.system.ErrnoException;
 import android.system.Os;
 import android.system.OsConstants;
 import android.system.StructStat;
+import android.util.Slog;
 
-import java.util.ArrayList;
+import com.android.internal.app.ResolverActivity;
+import com.android.internal.os.BackgroundThread;
+
+import dalvik.system.DexFile;
+import dalvik.system.VMRuntime;
+
 import java.io.FileDescriptor;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.util.ArrayList;
 
 /**
  * <p>PinnerService pins important files for key processes in memory.</p>
  * <p>Files to pin are specified in the config_defaultPinnerServiceFiles
- * overlay. </p>
+ * overlay.</p>
+ * <p>Pin the default camera application if specified in config_pinnerCameraApp.</p>
  */
 public final class PinnerService extends SystemService {
     private static final boolean DEBUG = false;
     private static final String TAG = "PinnerService";
 
     private final Context mContext;
-    private final ArrayList<String> mPinnedFiles = new ArrayList<String>();
+    private final ArrayList<PinnedFile> mPinnedFiles = new ArrayList<PinnedFile>();
+    private final ArrayList<PinnedFile> mPinnedCameraFiles = new ArrayList<PinnedFile>();
+    private final boolean mShouldPinCamera;
 
     private BinderService mBinderService;
 
+    private final long MAX_CAMERA_PIN_SIZE = 50 * (1 << 20); //50MB max
+
+    private PinnerHandler mPinnerHandler = null;
+
 
     public PinnerService(Context context) {
         super(context);
 
         mContext = context;
-
+        mShouldPinCamera = context.getResources().getBoolean(
+                com.android.internal.R.bool.config_pinnerCameraApp);
+        mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper());
     }
 
     @Override
     public void onStart() {
-        Slog.e(TAG, "Starting PinnerService");
-
+        if (DEBUG) {
+            Slog.i(TAG, "Starting PinnerService");
+        }
         mBinderService = new BinderService();
         publishBinderService("pinner", mBinderService);
 
-        // Files to pin come from the overlay and can be specified per-device config
-        // Continue trying to pin remaining files even if there is a failure
-        String[] filesToPin = mContext.getResources().getStringArray(com.android.internal.R.array.config_defaultPinnerServiceFiles);
-        for (int i = 0; i < filesToPin.length; i++){
-            boolean success = pinFile(filesToPin[i], 0, 0);
-            if (success == true) {
-                mPinnedFiles.add(filesToPin[i]);
-                Slog.i(TAG, "Pinned file = " + filesToPin[i]);
-            } else {
-                Slog.e(TAG, "Failed to pin file = " + filesToPin[i]);
+        mPinnerHandler.obtainMessage(PinnerHandler.PIN_ONSTART_MSG).sendToTarget();
+        mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, UserHandle.USER_SYSTEM, 0)
+                .sendToTarget();
+    }
+
+    /**
+     * Pin camera on user switch.
+     * If more than one user is using the device
+     * each user may set a different preference for the camera app.
+     * Make sure that user's preference is pinned into memory.
+     */
+    @Override
+    public void onSwitchUser(int userHandle) {
+        mPinnerHandler.obtainMessage(PinnerHandler.PIN_CAMERA_MSG, userHandle, 0).sendToTarget();
+    }
+
+    /**
+     * Handler for on start pinning message
+     */
+    private void handlePinOnStart() {
+         // Files to pin come from the overlay and can be specified per-device config
+        String[] filesToPin = mContext.getResources().getStringArray(
+                com.android.internal.R.array.config_defaultPinnerServiceFiles);
+        synchronized(this) {
+            // Continue trying to pin remaining files even if there is a failure
+            for (int i = 0; i < filesToPin.length; i++){
+                PinnedFile pf = pinFile(filesToPin[i], 0, 0, 0);
+                if (pf != null) {
+                    mPinnedFiles.add(pf);
+                    if (DEBUG) {
+                        Slog.i(TAG, "Pinned file = " + pf.mFilename);
+                    }
+                } else {
+                    Slog.e(TAG, "Failed to pin file = " + filesToPin[i]);
+                }
             }
         }
     }
 
-    // mlock length bytes of fileToPin in memory, starting at offset
-    // length == 0 means pin from offset to end of file
-    private boolean pinFile(String fileToPin, long offset, long length) {
+    /**
+     * Handler for camera pinning message
+     */
+    private void handlePinCamera(int userHandle) {
+        if (mShouldPinCamera) {
+            synchronized(this) {
+                boolean success = pinCamera(userHandle);
+                if (!success) {
+                    //this is not necessarily an error
+                    if (DEBUG) {
+                        Slog.v(TAG, "Failed to pin camera.");
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     *  determine if the camera app is already pinned by comparing the
+     *  intent resolution to the pinned files list
+     */
+    private boolean alreadyPinned(int userHandle) {
+        ApplicationInfo cameraInfo = getCameraInfo(userHandle);
+        if (cameraInfo == null ) {
+            return false;
+        }
+        for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
+            if (mPinnedCameraFiles.get(i).mFilename.equals(cameraInfo.sourceDir)) {
+                if (DEBUG) {
+                  Slog.v(TAG, "Camera is already pinned");
+                }
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private void unpinCameraApp() {
+        for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
+            unpinFile(mPinnedCameraFiles.get(i));
+        }
+        mPinnedCameraFiles.clear();
+    }
+
+    private boolean isResolverActivity(ActivityInfo info) {
+        return ResolverActivity.class.getName().equals(info.name);
+    }
+
+    private ApplicationInfo getCameraInfo(int userHandle) {
+        //  find the camera via an intent
+        //  use INTENT_ACTION_STILL_IMAGE_CAMERA instead of _SECURE.  On a
+        //  device without a fbe enabled, the _SECURE intent will never get set.
+        Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
+        PackageManager pm = mContext.getPackageManager();
+        ResolveInfo cameraResolveInfo = pm.resolveActivityAsUser(cameraIntent,
+                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_DIRECT_BOOT_AWARE
+                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
+                userHandle);
+        if (cameraResolveInfo == null ) {
+            //this is not necessarily an error
+            if (DEBUG) {
+              Slog.v(TAG, "Unable to resolve camera intent");
+            }
+            return null;
+        }
+
+        if (isResolverActivity(cameraResolveInfo.activityInfo))
+        {
+            return null;
+        }
+
+        return cameraResolveInfo.activityInfo.applicationInfo;
+    }
+
+    private boolean pinCamera(int userHandle){
+        //we may have already pinned a camera app.  If we've pinned this
+        //camera app, we're done.  otherwise, unpin and pin the new app
+        if (alreadyPinned(userHandle)){
+            return true;
+        }
+
+        ApplicationInfo cameraInfo = getCameraInfo(userHandle);
+        if (cameraInfo == null) {
+            return false;
+        }
+
+        //unpin after checking that the camera intent has resolved
+        //this prevents us from thrashing when switching users with
+        //FBE enabled, because the intent won't resolve until the unlock
+        unpinCameraApp();
+
+        //pin APK
+        String camAPK = cameraInfo.sourceDir;
+        PinnedFile pf = pinFile(camAPK, 0, 0, MAX_CAMERA_PIN_SIZE);
+        if (pf == null) {
+            Slog.e(TAG, "Failed to pin " + camAPK);
+            return false;
+        }
+        if (DEBUG) {
+            Slog.i(TAG, "Pinned " + pf.mFilename);
+        }
+        mPinnedCameraFiles.add(pf);
+
+        // determine the ABI from either ApplicationInfo or Build
+        String arch = "arm";
+        if (cameraInfo.primaryCpuAbi != null
+            && VMRuntime.is64BitAbi(cameraInfo.primaryCpuAbi)) {
+            arch = arch + "64";
+        } else {
+            if (VMRuntime.is64BitAbi(Build.SUPPORTED_ABIS[0])) {
+                arch = arch + "64";
+            }
+        }
+
+        // get the path to the odex or oat file
+        String baseCodePath = cameraInfo.getBaseCodePath();
+        String odex = null;
+        try {
+            odex = DexFile.getDexFileOutputPath(baseCodePath, arch);
+        } catch (IOException ioe) {}
+        if (odex == null) {
+            return true;
+        }
+
+        //not pinning the oat/odex is not a fatal error
+        pf = pinFile(odex, 0, 0, MAX_CAMERA_PIN_SIZE);
+        if (pf != null) {
+            mPinnedCameraFiles.add(pf);
+            if (DEBUG) {
+                Slog.i(TAG, "Pinned " + pf.mFilename);
+            }
+        }
+
+        return true;
+    }
+
+
+    /** mlock length bytes of fileToPin in memory, starting at offset
+     *  length == 0 means pin from offset to end of file
+     *  maxSize == 0 means infinite
+     */
+    private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
         FileDescriptor fd = new FileDescriptor();
         try {
-            fd = Os.open(fileToPin, OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW, OsConstants.O_RDONLY);
+            fd = Os.open(fileToPin,
+                    OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW,
+                    OsConstants.O_RDONLY);
 
             StructStat sb = Os.fstat(fd);
 
             if (offset + length > sb.st_size) {
                 Os.close(fd);
-                return false;
+                Slog.e(TAG, "Failed to pin file " + fileToPin +
+                        ", request extends beyond end of file.  offset + length =  "
+                        + (offset + length) + ", file length = " + sb.st_size);
+                return null;
             }
 
             if (length == 0) {
                 length = sb.st_size - offset;
             }
 
-            long address = Os.mmap(0, length, OsConstants.PROT_READ, OsConstants.MAP_PRIVATE, fd, offset);
+            if (maxSize > 0 && length > maxSize) {
+                Slog.e(TAG, "Could not pin file " + fileToPin +
+                        ", size = " + length + ", maxSize = " + maxSize);
+                Os.close(fd);
+                return null;
+            }
+
+            long address = Os.mmap(0, length, OsConstants.PROT_READ,
+                    OsConstants.MAP_PRIVATE, fd, offset);
             Os.close(fd);
 
             Os.mlock(address, length);
 
-            return true;
+            return new PinnedFile(address, length, fileToPin);
         } catch (ErrnoException e) {
-            Slog.e(TAG, "Failed to pin file " + fileToPin + " with error " + e.getMessage());
+            Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
             if(fd.valid()) {
-                try { Os.close(fd); }
-                catch (ErrnoException eClose) {Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());}
+                try {
+                    Os.close(fd);
+                }
+                catch (ErrnoException eClose) {
+                    Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
+                }
             }
-            return false;
+            return null;
         }
     }
 
+    private static boolean unpinFile(PinnedFile pf) {
+        try {
+            Os.munlock(pf.mAddress, pf.mLength);
+        } catch (ErrnoException e) {
+            Slog.e(TAG, "Failed to unpin file " + pf.mFilename + " with error " + e.getMessage());
+            return false;
+        }
+        if (DEBUG) {
+            Slog.i(TAG, "Unpinned file " + pf.mFilename );
+        }
+        return true;
+    }
 
     private final class BinderService extends Binder {
         @Override
         protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
             mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
             pw.println("Pinned Files:");
-            for (int i = 0; i < mPinnedFiles.size(); i++) {
-                pw.println(mPinnedFiles.get(i));
+            synchronized(this) {
+                for (int i = 0; i < mPinnedFiles.size(); i++) {
+                    pw.println(mPinnedFiles.get(i).mFilename);
+                }
+                for (int i = 0; i < mPinnedCameraFiles.size(); i++) {
+                    pw.println(mPinnedCameraFiles.get(i).mFilename);
+                }
             }
         }
     }
+
+    private static class PinnedFile {
+        long mAddress;
+        long mLength;
+        String mFilename;
+
+        PinnedFile(long address, long length, String filename) {
+             mAddress = address;
+             mLength = length;
+             mFilename = filename;
+        }
+    }
+
+    final class PinnerHandler extends Handler {
+        static final int PIN_CAMERA_MSG  = 4000;
+        static final int PIN_ONSTART_MSG = 4001;
+
+        public PinnerHandler(Looper looper) {
+            super(looper, null, true);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+
+                case PIN_CAMERA_MSG:
+                {
+                    handlePinCamera(msg.arg1);
+                }
+                break;
+
+                case PIN_ONSTART_MSG:
+                {
+                    handlePinOnStart();
+                }
+                break;
+
+                default:
+                    super.handleMessage(msg);
+            }
+        }
+    }
+
 }
diff --git a/services/core/java/com/android/server/ServiceWatcher.java b/services/core/java/com/android/server/ServiceWatcher.java
index 383e25a..2ff036b 100644
--- a/services/core/java/com/android/server/ServiceWatcher.java
+++ b/services/core/java/com/android/server/ServiceWatcher.java
@@ -141,8 +141,12 @@
      * <p>
      * Note that if there are no matching encryption-aware services, we may not
      * bind to a real service until after the current user is unlocked.
+     *
+     * @returns {@code true} if a potential service implementation was found.
      */
     public boolean start() {
+        if (isServiceMissing()) return false;
+
         synchronized (mLock) {
             bindBestPackageLocked(mServicePackageName, false);
         }
@@ -174,6 +178,17 @@
     }
 
     /**
+     * Check if any instance of this service is present on the device,
+     * regardless of it being encryption-aware or not.
+     */
+    private boolean isServiceMissing() {
+        final Intent intent = new Intent(mAction);
+        final int flags = PackageManager.MATCH_DIRECT_BOOT_AWARE
+                | PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
+        return mPm.queryIntentServicesAsUser(intent, flags, mCurrentUserId).isEmpty();
+    }
+
+    /**
      * Searches and binds to the best package, or do nothing if the best package
      * is already bound, unless force rebinding is requested.
      *
@@ -181,7 +196,7 @@
      *            packages if it is {@code null}.
      * @param forceRebind Force a rebinding to the best package if it's already
      *            bound.
-     * @return {@code true} if a valid package was found to bind to.
+     * @returns {@code true} if a valid package was found to bind to.
      */
     private boolean bindBestPackageLocked(String justCheckThisPackage, boolean forceRebind) {
         Intent intent = new Intent(mAction);
diff --git a/services/core/java/com/android/server/SystemServiceManager.java b/services/core/java/com/android/server/SystemServiceManager.java
index ecc69e9..90f507c 100644
--- a/services/core/java/com/android/server/SystemServiceManager.java
+++ b/services/core/java/com/android/server/SystemServiceManager.java
@@ -156,12 +156,15 @@
         final int serviceLen = mServices.size();
         for (int i = 0; i < serviceLen; i++) {
             final SystemService service = mServices.get(i);
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onStartUser "
+                    + service.getClass().getName());
             try {
                 service.onStartUser(userHandle);
             } catch (Exception ex) {
                 Slog.wtf(TAG, "Failure reporting start of user " + userHandle
                         + " to service " + service.getClass().getName(), ex);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
     }
 
@@ -169,12 +172,15 @@
         final int serviceLen = mServices.size();
         for (int i = 0; i < serviceLen; i++) {
             final SystemService service = mServices.get(i);
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onUnlockUser "
+                    + service.getClass().getName());
             try {
                 service.onUnlockUser(userHandle);
             } catch (Exception ex) {
                 Slog.wtf(TAG, "Failure reporting unlock of user " + userHandle
                         + " to service " + service.getClass().getName(), ex);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
     }
 
@@ -182,12 +188,15 @@
         final int serviceLen = mServices.size();
         for (int i = 0; i < serviceLen; i++) {
             final SystemService service = mServices.get(i);
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onSwitchUser "
+                    + service.getClass().getName());
             try {
                 service.onSwitchUser(userHandle);
             } catch (Exception ex) {
                 Slog.wtf(TAG, "Failure reporting switch of user " + userHandle
                         + " to service " + service.getClass().getName(), ex);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
     }
 
@@ -195,12 +204,15 @@
         final int serviceLen = mServices.size();
         for (int i = 0; i < serviceLen; i++) {
             final SystemService service = mServices.get(i);
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onStopUser "
+                    + service.getClass().getName());
             try {
                 service.onStopUser(userHandle);
             } catch (Exception ex) {
                 Slog.wtf(TAG, "Failure reporting stop of user " + userHandle
                         + " to service " + service.getClass().getName(), ex);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
     }
 
@@ -208,12 +220,15 @@
         final int serviceLen = mServices.size();
         for (int i = 0; i < serviceLen; i++) {
             final SystemService service = mServices.get(i);
+            Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "onCleanupUser "
+                    + service.getClass().getName());
             try {
                 service.onCleanupUser(userHandle);
             } catch (Exception ex) {
                 Slog.wtf(TAG, "Failure reporting cleanup of user " + userHandle
                         + " to service " + service.getClass().getName(), ex);
             }
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
         }
     }
 
diff --git a/services/core/java/com/android/server/TextServicesManagerService.java b/services/core/java/com/android/server/TextServicesManagerService.java
index 4b0d4be..4f02a23 100644
--- a/services/core/java/com/android/server/TextServicesManagerService.java
+++ b/services/core/java/com/android/server/TextServicesManagerService.java
@@ -459,71 +459,75 @@
         if (!calledFromValidUser()) {
             return null;
         }
+        final int subtypeHashCode;
+        final SpellCheckerInfo sci;
+        final Locale systemLocale;
         synchronized (mSpellCheckerMap) {
-            final int subtypeHashCode =
+            subtypeHashCode =
                     mSettings.getSelectedSpellCheckerSubtype(SpellCheckerSubtype.SUBTYPE_ID_NONE);
             if (DBG) {
                 Slog.w(TAG, "getCurrentSpellCheckerSubtype: " + subtypeHashCode);
             }
-            final SpellCheckerInfo sci = getCurrentSpellChecker(null);
-            if (sci == null || sci.getSubtypeCount() == 0) {
-                if (DBG) {
-                    Slog.w(TAG, "Subtype not found.");
-                }
-                return null;
-            }
-            if (subtypeHashCode == SpellCheckerSubtype.SUBTYPE_ID_NONE
-                    && !allowImplicitlySelectedSubtype) {
-                return null;
-            }
-            String candidateLocale = null;
-            if (subtypeHashCode == 0) {
-                // Spell checker language settings == "auto"
-                final InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
-                if (imm != null) {
-                    final InputMethodSubtype currentInputMethodSubtype =
-                            imm.getCurrentInputMethodSubtype();
-                    if (currentInputMethodSubtype != null) {
-                        final String localeString = currentInputMethodSubtype.getLocale();
-                        if (!TextUtils.isEmpty(localeString)) {
-                            // 1. Use keyboard locale if available in the spell checker
-                            candidateLocale = localeString;
-                        }
-                    }
-                }
-                if (candidateLocale == null) {
-                    // 2. Use System locale if available in the spell checker
-                    candidateLocale = mContext.getResources().getConfiguration().locale.toString();
-                }
-            }
-            SpellCheckerSubtype candidate = null;
-            for (int i = 0; i < sci.getSubtypeCount(); ++i) {
-                final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
-                if (subtypeHashCode == 0) {
-                    final String scsLocale = scs.getLocale();
-                    if (candidateLocale.equals(scsLocale)) {
-                        return scs;
-                    } else if (candidate == null) {
-                        if (candidateLocale.length() >= 2 && scsLocale.length() >= 2
-                                && candidateLocale.startsWith(scsLocale)) {
-                            // Fall back to the applicable language
-                            candidate = scs;
-                        }
-                    }
-                } else if (scs.hashCode() == subtypeHashCode) {
-                    if (DBG) {
-                        Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
-                                + ", " + scs.getLocale());
-                    }
-                    // 3. Use the user specified spell check language
-                    return scs;
-                }
-            }
-            // 4. Fall back to the applicable language and return it if not null
-            // 5. Simply just return it even if it's null which means we could find no suitable
-            // spell check languages
-            return candidate;
+            sci = getCurrentSpellChecker(null);
+            systemLocale = mContext.getResources().getConfiguration().locale;
         }
+        if (sci == null || sci.getSubtypeCount() == 0) {
+            if (DBG) {
+                Slog.w(TAG, "Subtype not found.");
+            }
+            return null;
+        }
+        if (subtypeHashCode == SpellCheckerSubtype.SUBTYPE_ID_NONE
+                && !allowImplicitlySelectedSubtype) {
+            return null;
+        }
+        String candidateLocale = null;
+        if (subtypeHashCode == 0) {
+            // Spell checker language settings == "auto"
+            final InputMethodManager imm = mContext.getSystemService(InputMethodManager.class);
+            if (imm != null) {
+                final InputMethodSubtype currentInputMethodSubtype =
+                        imm.getCurrentInputMethodSubtype();
+                if (currentInputMethodSubtype != null) {
+                    final String localeString = currentInputMethodSubtype.getLocale();
+                    if (!TextUtils.isEmpty(localeString)) {
+                        // 1. Use keyboard locale if available in the spell checker
+                        candidateLocale = localeString;
+                    }
+                }
+            }
+            if (candidateLocale == null) {
+                // 2. Use System locale if available in the spell checker
+                candidateLocale = systemLocale.toString();
+            }
+        }
+        SpellCheckerSubtype candidate = null;
+        for (int i = 0; i < sci.getSubtypeCount(); ++i) {
+            final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
+            if (subtypeHashCode == 0) {
+                final String scsLocale = scs.getLocale();
+                if (candidateLocale.equals(scsLocale)) {
+                    return scs;
+                } else if (candidate == null) {
+                    if (candidateLocale.length() >= 2 && scsLocale.length() >= 2
+                            && candidateLocale.startsWith(scsLocale)) {
+                        // Fall back to the applicable language
+                        candidate = scs;
+                    }
+                }
+            } else if (scs.hashCode() == subtypeHashCode) {
+                if (DBG) {
+                    Slog.w(TAG, "Return subtype " + scs.hashCode() + ", input= " + locale
+                            + ", " + scs.getLocale());
+                }
+                // 3. Use the user specified spell check language
+                return scs;
+            }
+        }
+        // 4. Fall back to the applicable language and return it if not null
+        // 5. Simply just return it even if it's null which means we could find no suitable
+        // spell check languages
+        return candidate;
     }
 
     @Override
diff --git a/services/core/java/com/android/server/TwilightCalculator.java b/services/core/java/com/android/server/TwilightCalculator.java
deleted file mode 100644
index 5839b16..0000000
--- a/services/core/java/com/android/server/TwilightCalculator.java
+++ /dev/null
@@ -1,122 +0,0 @@
-/*
- * 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.server;
-
-import android.text.format.DateUtils;
-
-/** @hide */
-public class TwilightCalculator {
-
-    /** Value of {@link #mState} if it is currently day */
-    public static final int DAY = 0;
-
-    /** Value of {@link #mState} if it is currently night */
-    public static final int NIGHT = 1;
-
-    private static final float DEGREES_TO_RADIANS = (float) (Math.PI / 180.0f);
-
-    // element for calculating solar transit.
-    private static final float J0 = 0.0009f;
-
-    // correction for civil twilight
-    private static final float ALTIDUTE_CORRECTION_CIVIL_TWILIGHT = -0.104719755f;
-
-    // coefficients for calculating Equation of Center.
-    private static final float C1 = 0.0334196f;
-    private static final float C2 = 0.000349066f;
-    private static final float C3 = 0.000005236f;
-
-    private static final float OBLIQUITY = 0.40927971f;
-
-    // Java time on Jan 1, 2000 12:00 UTC.
-    private static final long UTC_2000 = 946728000000L;
-
-    /**
-     * Time of sunset (civil twilight) in milliseconds or -1 in the case the day
-     * or night never ends.
-     */
-    public long mSunset;
-
-    /**
-     * Time of sunrise (civil twilight) in milliseconds or -1 in the case the
-     * day or night never ends.
-     */
-    public long mSunrise;
-
-    /** Current state */
-    public int mState;
-
-    /**
-     * calculates the civil twilight bases on time and geo-coordinates.
-     *
-     * @param time time in milliseconds.
-     * @param latiude latitude in degrees.
-     * @param longitude latitude in degrees.
-     */
-    public void calculateTwilight(long time, double latiude, double longitude) {
-        final float daysSince2000 = (float) (time - UTC_2000) / DateUtils.DAY_IN_MILLIS;
-
-        // mean anomaly
-        final float meanAnomaly = 6.240059968f + daysSince2000 * 0.01720197f;
-
-        // true anomaly
-        final double trueAnomaly = meanAnomaly + C1 * Math.sin(meanAnomaly) + C2
-                * Math.sin(2 * meanAnomaly) + C3 * Math.sin(3 * meanAnomaly);
-
-        // ecliptic longitude
-        final double solarLng = trueAnomaly + 1.796593063d + Math.PI;
-
-        // solar transit in days since 2000
-        final double arcLongitude = -longitude / 360;
-        float n = Math.round(daysSince2000 - J0 - arcLongitude);
-        double solarTransitJ2000 = n + J0 + arcLongitude + 0.0053d * Math.sin(meanAnomaly)
-                + -0.0069d * Math.sin(2 * solarLng);
-
-        // declination of sun
-        double solarDec = Math.asin(Math.sin(solarLng) * Math.sin(OBLIQUITY));
-
-        final double latRad = latiude * DEGREES_TO_RADIANS;
-
-        double cosHourAngle = (Math.sin(ALTIDUTE_CORRECTION_CIVIL_TWILIGHT) - Math.sin(latRad)
-                * Math.sin(solarDec)) / (Math.cos(latRad) * Math.cos(solarDec));
-        // The day or night never ends for the given date and location, if this value is out of
-        // range.
-        if (cosHourAngle >= 1) {
-            mState = NIGHT;
-            mSunset = -1;
-            mSunrise = -1;
-            return;
-        } else if (cosHourAngle <= -1) {
-            mState = DAY;
-            mSunset = -1;
-            mSunrise = -1;
-            return;
-        }
-
-        float hourAngle = (float) (Math.acos(cosHourAngle) / (2 * Math.PI));
-
-        mSunset = Math.round((solarTransitJ2000 + hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
-        mSunrise = Math.round((solarTransitJ2000 - hourAngle) * DateUtils.DAY_IN_MILLIS) + UTC_2000;
-
-        if (mSunrise < time && mSunset > time) {
-            mState = DAY;
-        } else {
-            mState = NIGHT;
-        }
-    }
-
-}
diff --git a/services/core/java/com/android/server/UiModeManagerService.java b/services/core/java/com/android/server/UiModeManagerService.java
index 6f713cd..bb5f62b 100644
--- a/services/core/java/com/android/server/UiModeManagerService.java
+++ b/services/core/java/com/android/server/UiModeManagerService.java
@@ -16,6 +16,7 @@
 
 package com.android.server;
 
+import android.annotation.Nullable;
 import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
@@ -155,8 +156,13 @@
 
     private final TwilightListener mTwilightListener = new TwilightListener() {
         @Override
-        public void onTwilightStateChanged() {
-            updateTwilight();
+        public void onTwilightStateChanged(@Nullable TwilightState state) {
+            synchronized (mLock) {
+                if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
+                    updateComputedNightModeLocked();
+                    updateLocked(0, 0);
+                }
+            }
         }
     };
 
@@ -344,8 +350,8 @@
                     pw.print(" mSystemReady="); pw.println(mSystemReady);
             if (mTwilightManager != null) {
                 // We may not have a TwilightManager.
-                pw.print("  mTwilightService.getCurrentState()=");
-                pw.println(mTwilightManager.getCurrentState());
+                pw.print("  mTwilightService.getLastTwilightState()=");
+                pw.println(mTwilightManager.getLastTwilightState());
             }
         }
     }
@@ -355,9 +361,6 @@
         if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
             synchronized (mLock) {
                 mTwilightManager = getLocalService(TwilightManager.class);
-                if (mTwilightManager != null) {
-                    mTwilightManager.registerListener(mTwilightListener, mHandler);
-                }
                 mSystemReady = true;
                 mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR;
                 updateComputedNightModeLocked();
@@ -411,10 +414,16 @@
         }
 
         if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
+            if (mTwilightManager != null) {
+                mTwilightManager.registerListener(mTwilightListener, mHandler);
+            }
             updateComputedNightModeLocked();
             uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES
                     : Configuration.UI_MODE_NIGHT_NO;
         } else {
+            if (mTwilightManager != null) {
+                mTwilightManager.unregisterListener(mTwilightListener);
+            }
             uiMode |= mNightMode << 4;
         }
 
@@ -668,18 +677,9 @@
         }
     }
 
-    void updateTwilight() {
-        synchronized (mLock) {
-            if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
-                updateComputedNightModeLocked();
-                updateLocked(0, 0);
-            }
-        }
-    }
-
     private void updateComputedNightModeLocked() {
         if (mTwilightManager != null) {
-            TwilightState state = mTwilightManager.getCurrentState();
+            TwilightState state = mTwilightManager.getLastTwilightState();
             if (state != null) {
                 mComputedNightMode = state.isNight();
             }
diff --git a/services/core/java/com/android/server/UiThread.java b/services/core/java/com/android/server/UiThread.java
index c06afc2..1bc6250 100644
--- a/services/core/java/com/android/server/UiThread.java
+++ b/services/core/java/com/android/server/UiThread.java
@@ -17,6 +17,7 @@
 package com.android.server;
 
 import android.os.Handler;
+import android.os.Process;
 import android.os.Trace;
 
 /**
@@ -29,7 +30,9 @@
     private static Handler sHandler;
 
     private UiThread() {
-        super("android.ui", android.os.Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
+        super("android.ui", Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
+        // Make sure UiThread is in the fg stune boost group
+        Process.setThreadGroup(Process.myTid(), Process.THREAD_GROUP_TOP_APP);
     }
 
     private static void ensureThreadLocked() {
diff --git a/services/core/java/com/android/server/accounts/AccountManagerBackupHelper.java b/services/core/java/com/android/server/accounts/AccountManagerBackupHelper.java
new file mode 100644
index 0000000..1361a31
--- /dev/null
+++ b/services/core/java/com/android/server/accounts/AccountManagerBackupHelper.java
@@ -0,0 +1,315 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.accounts;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.accounts.AccountManagerInternal;
+import android.annotation.IntRange;
+import android.annotation.NonNull;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.os.UserHandle;
+import android.text.TextUtils;
+import android.util.Log;
+import android.util.PackageUtils;
+import android.util.Xml;
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.content.PackageMonitor;
+import com.android.internal.util.FastXmlSerializer;
+import com.android.internal.util.XmlUtils;
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlSerializer;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Helper class for backup and restore of account access grants.
+ */
+public final class AccountManagerBackupHelper {
+    private static final String TAG = "AccountManagerBackupHelper";
+
+    private static final long PENDING_RESTORE_TIMEOUT_MILLIS = 60 * 60 * 1000; // 1 hour
+
+    private static final String TAG_PERMISSIONS = "permissions";
+    private static final String TAG_PERMISSION = "permission";
+    private static final String ATTR_ACCOUNT_SHA_256 = "account-sha-256";
+    private static final String ATTR_PACKAGE = "package";
+    private static final String ATTR_DIGEST = "digest";
+
+    private static final String ACCOUNT_ACCESS_GRANTS = ""
+            + "SELECT " + AccountManagerService.ACCOUNTS_NAME + ", "
+            + AccountManagerService.GRANTS_GRANTEE_UID
+            + " FROM " + AccountManagerService.TABLE_ACCOUNTS
+            + ", " + AccountManagerService.TABLE_GRANTS
+            + " WHERE " + AccountManagerService.GRANTS_ACCOUNTS_ID
+            + "=" + AccountManagerService.ACCOUNTS_ID;
+
+    private final Object mLock = new Object();
+
+    private final AccountManagerService mAccountManagerService;
+    private final AccountManagerInternal mAccountManagerInternal;
+
+    @GuardedBy("mLock")
+    private List<PendingAppPermission> mRestorePendingAppPermissions;
+
+    @GuardedBy("mLock")
+    private RestorePackageMonitor mRestorePackageMonitor;
+
+    @GuardedBy("mLock")
+    private Runnable mRestoreCancelCommand;
+
+    public AccountManagerBackupHelper(AccountManagerService accountManagerService,
+            AccountManagerInternal accountManagerInternal) {
+        mAccountManagerService = accountManagerService;
+        mAccountManagerInternal = accountManagerInternal;
+    }
+
+    private final class PendingAppPermission {
+        private final @NonNull String accountDigest;
+        private final @NonNull String packageName;
+        private final @NonNull String certDigest;
+        private final @IntRange(from = 0) int userId;
+
+        public PendingAppPermission(String accountDigest, String packageName,
+                String certDigest, int userId) {
+            this.accountDigest = accountDigest;
+            this.packageName = packageName;
+            this.certDigest = certDigest;
+            this.userId = userId;
+        }
+
+        public boolean apply(PackageManager packageManager) {
+            Account account = null;
+            AccountManagerService.UserAccounts accounts = mAccountManagerService
+                    .getUserAccounts(userId);
+            synchronized (accounts.cacheLock) {
+                for (Account[] accountsPerType : accounts.accountCache.values()) {
+                    for (Account accountPerType : accountsPerType) {
+                        if (accountDigest.equals(PackageUtils.computeSha256Digest(
+                                accountPerType.name.getBytes()))) {
+                            account = accountPerType;
+                            break;
+                        }
+                    }
+                    if (account != null) {
+                        break;
+                    }
+                }
+            }
+            if (account == null) {
+                return false;
+            }
+            final PackageInfo packageInfo;
+            try {
+                packageInfo = packageManager.getPackageInfoAsUser(packageName,
+                        PackageManager.GET_SIGNATURES, userId);
+            } catch (PackageManager.NameNotFoundException e) {
+                return false;
+            }
+            String currentCertDigest = PackageUtils.computeCertSha256Digest(
+                    packageInfo.signatures[0]);
+            if (!certDigest.equals(currentCertDigest)) {
+                return false;
+            }
+            final int uid = packageInfo.applicationInfo.uid;
+            if (!mAccountManagerInternal.hasAccountAccess(account, uid)) {
+                mAccountManagerService.grantAppPermission(account,
+                        AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid);
+            }
+            return true;
+        }
+    }
+
+    public byte[] backupAccountAccessPermissions(int userId) {
+        final AccountManagerService.UserAccounts accounts = mAccountManagerService
+                .getUserAccounts(userId);
+        synchronized (accounts.cacheLock) {
+            SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
+            try (
+                Cursor cursor = db.rawQuery(ACCOUNT_ACCESS_GRANTS, null);
+            ) {
+                if (cursor == null || !cursor.moveToFirst()) {
+                    return null;
+                }
+
+                final int nameColumnIdx = cursor.getColumnIndex(
+                        AccountManagerService.ACCOUNTS_NAME);
+                final int uidColumnIdx = cursor.getColumnIndex(
+                        AccountManagerService.GRANTS_GRANTEE_UID);
+
+                ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
+                try {
+                    final XmlSerializer serializer = new FastXmlSerializer();
+                    serializer.setOutput(dataStream, StandardCharsets.UTF_8.name());
+                    serializer.startDocument(null, true);
+                    serializer.startTag(null, TAG_PERMISSIONS);
+
+                    PackageManager packageManager = mAccountManagerService.mContext
+                            .getPackageManager();
+
+                    do {
+                        final String accountName = cursor.getString(nameColumnIdx);
+                        final int uid = cursor.getInt(uidColumnIdx);
+
+                        final String[] packageNames = packageManager.getPackagesForUid(uid);
+                        if (packageNames == null) {
+                            continue;
+                        }
+
+                        for (String packageName : packageNames) {
+                            String digest = PackageUtils.computePackageCertSha256Digest(
+                                    packageManager, packageName, userId);
+                            if (digest != null) {
+                                serializer.startTag(null, TAG_PERMISSION);
+                                serializer.attribute(null, ATTR_ACCOUNT_SHA_256,
+                                        PackageUtils.computeSha256Digest(accountName.getBytes()));
+                                serializer.attribute(null, ATTR_PACKAGE, packageName);
+                                serializer.attribute(null, ATTR_DIGEST, digest);
+                                serializer.endTag(null, TAG_PERMISSION);
+                            }
+                        }
+                    } while (cursor.moveToNext());
+
+                    serializer.endTag(null, TAG_PERMISSIONS);
+                    serializer.endDocument();
+                    serializer.flush();
+                } catch (IOException e) {
+                    Log.e(TAG, "Error backing up account access grants", e);
+                    return null;
+                }
+
+                return dataStream.toByteArray();
+            }
+        }
+    }
+
+    public void restoreAccountAccessPermissions(byte[] data, int userId) {
+        try {
+            ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
+            XmlPullParser parser = Xml.newPullParser();
+            parser.setInput(dataStream, StandardCharsets.UTF_8.name());
+            PackageManager packageManager = mAccountManagerService.mContext.getPackageManager();
+
+            final int permissionsOuterDepth = parser.getDepth();
+            while (XmlUtils.nextElementWithin(parser, permissionsOuterDepth)) {
+                if (!TAG_PERMISSIONS.equals(parser.getName())) {
+                    continue;
+                }
+                final int permissionOuterDepth = parser.getDepth();
+                while (XmlUtils.nextElementWithin(parser, permissionOuterDepth)) {
+                    if (!TAG_PERMISSION.equals(parser.getName())) {
+                        continue;
+                    }
+                    String accountDigest = parser.getAttributeValue(null, ATTR_ACCOUNT_SHA_256);
+                    if (TextUtils.isEmpty(accountDigest)) {
+                        XmlUtils.skipCurrentTag(parser);
+                    }
+                    String packageName = parser.getAttributeValue(null, ATTR_PACKAGE);
+                    if (TextUtils.isEmpty(packageName)) {
+                        XmlUtils.skipCurrentTag(parser);
+                    }
+                    String digest =  parser.getAttributeValue(null, ATTR_DIGEST);
+                    if (TextUtils.isEmpty(digest)) {
+                        XmlUtils.skipCurrentTag(parser);
+                    }
+
+                    PendingAppPermission pendingAppPermission = new PendingAppPermission(
+                            accountDigest, packageName, digest, userId);
+
+                    if (!pendingAppPermission.apply(packageManager)) {
+                        synchronized (mLock) {
+                            // Start watching before add pending to avoid a missed signal
+                            if (mRestorePackageMonitor == null) {
+                                mRestorePackageMonitor = new RestorePackageMonitor();
+                                mRestorePackageMonitor.register(mAccountManagerService.mContext,
+                                        mAccountManagerService.mMessageHandler.getLooper(), true);
+                            }
+                            if (mRestorePendingAppPermissions == null) {
+                                mRestorePendingAppPermissions = new ArrayList<>();
+                            }
+                            mRestorePendingAppPermissions.add(pendingAppPermission);
+                        }
+                    }
+                }
+            }
+
+            // Make sure we eventually prune the in-memory pending restores
+            synchronized (mLock) {
+                mRestoreCancelCommand = new CancelRestoreCommand();
+            }
+            mAccountManagerService.mMessageHandler.postDelayed(mRestoreCancelCommand,
+                    PENDING_RESTORE_TIMEOUT_MILLIS);
+        } catch (XmlPullParserException | IOException e) {
+            Log.e(TAG, "Error restoring app permissions", e);
+        }
+    }
+
+    private final class RestorePackageMonitor extends PackageMonitor {
+        @Override
+        public void onPackageAdded(String packageName, int uid) {
+            synchronized (mLock) {
+                // Can happen if restore is cancelled and there is a notification in flight
+                if (mRestorePendingAppPermissions == null) {
+                    return;
+                }
+                if (UserHandle.getUserId(uid) != UserHandle.USER_SYSTEM) {
+                    return;
+                }
+                final int count = mRestorePendingAppPermissions.size();
+                for (int i = count - 1; i >= 0; i--) {
+                    PendingAppPermission pendingAppPermission =
+                            mRestorePendingAppPermissions.get(i);
+                    if (!pendingAppPermission.packageName.equals(packageName)) {
+                        continue;
+                    }
+                    if (pendingAppPermission.apply(
+                            mAccountManagerService.mContext.getPackageManager())) {
+                        mRestorePendingAppPermissions.remove(i);
+                    }
+                }
+                if (mRestorePendingAppPermissions.isEmpty()
+                        && mRestoreCancelCommand != null) {
+                    mAccountManagerService.mMessageHandler.removeCallbacks(mRestoreCancelCommand);
+                    mRestoreCancelCommand.run();
+                    mRestoreCancelCommand = null;
+                }
+            }
+        }
+    }
+
+    private final class CancelRestoreCommand implements Runnable {
+        @Override
+        public void run() {
+            synchronized (mLock) {
+                mRestorePendingAppPermissions = null;
+                if (mRestorePackageMonitor != null) {
+                    mRestorePackageMonitor.unregister();
+                    mRestorePackageMonitor = null;
+                }
+            }
+        }
+    }
+}
diff --git a/services/core/java/com/android/server/accounts/AccountManagerService.java b/services/core/java/com/android/server/accounts/AccountManagerService.java
index cb6cb21..a7a79cd 100644
--- a/services/core/java/com/android/server/accounts/AccountManagerService.java
+++ b/services/core/java/com/android/server/accounts/AccountManagerService.java
@@ -22,6 +22,7 @@
 import android.accounts.AccountAndUser;
 import android.accounts.AccountAuthenticatorResponse;
 import android.accounts.AccountManager;
+import android.accounts.AccountManagerInternal;
 import android.accounts.AuthenticatorDescription;
 import android.accounts.CantAddAccountActivity;
 import android.accounts.GrantCredentialsPermissionActivity;
@@ -29,11 +30,15 @@
 import android.accounts.IAccountAuthenticatorResponse;
 import android.accounts.IAccountManager;
 import android.accounts.IAccountManagerResponse;
+import android.annotation.IntRange;
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
+import android.app.ActivityThread;
 import android.app.AppGlobals;
 import android.app.AppOpsManager;
+import android.app.INotificationManager;
 import android.app.Notification;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
@@ -46,9 +51,11 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.IntentSender;
 import android.content.ServiceConnection;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
@@ -72,7 +79,9 @@
 import android.os.Message;
 import android.os.Parcel;
 import android.os.Process;
+import android.os.RemoteCallback;
 import android.os.RemoteException;
+import android.os.ServiceManager;
 import android.os.SystemClock;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -85,7 +94,9 @@
 import android.util.SparseBooleanArray;
 
 import com.android.internal.R;
+import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.content.PackageMonitor;
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.internal.util.Preconditions;
@@ -115,6 +126,9 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
+import java.util.UUID;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 
@@ -145,13 +159,6 @@
         }
 
         @Override
-        public void onBootPhase(int phase) {
-            if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
-                mService.systemReady();
-            }
-        }
-
-        @Override
         public void onUnlockUser(int userHandle) {
             mService.onUnlockUser(userHandle);
         }
@@ -164,13 +171,13 @@
 
     private static final int MAX_DEBUG_DB_SIZE = 64;
 
-    private final Context mContext;
+    final Context mContext;
 
     private final PackageManager mPackageManager;
     private final AppOpsManager mAppOpsManager;
     private UserManager mUserManager;
 
-    private final MessageHandler mMessageHandler;
+    final MessageHandler mMessageHandler;
 
     // Messages that can be sent on mHandler
     private static final int MESSAGE_TIMED_OUT = 3;
@@ -178,9 +185,9 @@
 
     private final IAccountAuthenticatorCache mAuthenticatorCache;
 
-    private static final String TABLE_ACCOUNTS = "accounts";
-    private static final String ACCOUNTS_ID = "_id";
-    private static final String ACCOUNTS_NAME = "name";
+    static final String TABLE_ACCOUNTS = "accounts";
+    static final String ACCOUNTS_ID = "_id";
+    static final String ACCOUNTS_NAME = "name";
     private static final String ACCOUNTS_TYPE = "type";
     private static final String ACCOUNTS_TYPE_COUNT = "count(type)";
     private static final String ACCOUNTS_PASSWORD = "password";
@@ -194,10 +201,10 @@
     private static final String AUTHTOKENS_TYPE = "type";
     private static final String AUTHTOKENS_AUTHTOKEN = "authtoken";
 
-    private static final String TABLE_GRANTS = "grants";
-    private static final String GRANTS_ACCOUNTS_ID = "accounts_id";
+    static final String TABLE_GRANTS = "grants";
+    static final String GRANTS_ACCOUNTS_ID = "accounts_id";
     private static final String GRANTS_AUTH_TOKEN_TYPE = "auth_token_type";
-    private static final String GRANTS_GRANTEE_UID = "uid";
+    static final String GRANTS_GRANTEE_UID = "uid";
 
     private static final String TABLE_EXTRAS = "extras";
     private static final String EXTRAS_ID = "_id";
@@ -237,6 +244,13 @@
             + " AND " + ACCOUNTS_NAME + "=?"
             + " AND " + ACCOUNTS_TYPE + "=?";
 
+    private static final String COUNT_OF_MATCHING_GRANTS_ANY_TOKEN = ""
+            + "SELECT COUNT(*) FROM " + TABLE_GRANTS + ", " + TABLE_ACCOUNTS
+            + " WHERE " + GRANTS_ACCOUNTS_ID + "=" + ACCOUNTS_ID
+            + " AND " + GRANTS_GRANTEE_UID + "=?"
+            + " AND " + ACCOUNTS_NAME + "=?"
+            + " AND " + ACCOUNTS_TYPE + "=?";
+
     private static final String SELECTION_AUTHTOKENS_BY_ACCOUNT =
             AUTHTOKENS_ACCOUNTS_ID + "=(select _id FROM accounts WHERE name=? AND type=?)";
 
@@ -257,16 +271,16 @@
 
     static class UserAccounts {
         private final int userId;
-        private final DeDatabaseHelper openHelper;
+        final DeDatabaseHelper openHelper;
         private final HashMap<Pair<Pair<Account, String>, Integer>, Integer>
                 credentialsPermissionNotificationIds =
                 new HashMap<Pair<Pair<Account, String>, Integer>, Integer>();
         private final HashMap<Account, Integer> signinRequiredNotificationIds =
                 new HashMap<Account, Integer>();
-        private final Object cacheLock = new Object();
+        final Object cacheLock = new Object();
         /** protected by the {@link #cacheLock} */
-        private final HashMap<String, Account[]> accountCache =
-                new LinkedHashMap<String, Account[]>();
+        final HashMap<String, Account[]> accountCache =
+                new LinkedHashMap<>();
         /** protected by the {@link #cacheLock} */
         private final HashMap<Account, HashMap<String, String>> userDataCache =
                 new HashMap<Account, HashMap<String, String>>();
@@ -305,6 +319,8 @@
 
     private final SparseArray<UserAccounts> mUsers = new SparseArray<>();
     private final SparseBooleanArray mLocalUnlockedUsers = new SparseBooleanArray();
+    private final CopyOnWriteArrayList<AccountManagerInternal.OnAppPermissionChangeListener>
+            mAppPermissionChangeListeners = new CopyOnWriteArrayList<>();
 
     private static AtomicReference<AccountManagerService> sThis = new AtomicReference<>();
     private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[]{};
@@ -376,6 +392,118 @@
                 }
             }
         }, UserHandle.ALL, userFilter, null, null);
+
+        LocalServices.addService(AccountManagerInternal.class, new AccountManagerInternalImpl());
+
+        // Need to cancel account request notifications if the update/install can access the account
+        new PackageMonitor() {
+            @Override
+            public void onPackageAdded(String packageName, int uid) {
+                // Called on a handler, and running as the system
+                cancelAccountAccessRequestNotificationIfNeeded(uid, true);
+            }
+
+            @Override
+            public void onPackageUpdateFinished(String packageName, int uid) {
+                // Called on a handler, and running as the system
+                cancelAccountAccessRequestNotificationIfNeeded(uid, true);
+            }
+        }.register(mContext, mMessageHandler.getLooper(), UserHandle.ALL, true);
+
+        // Cancel account request notification if an app op was preventing the account access
+        mAppOpsManager.startWatchingMode(AppOpsManager.OP_GET_ACCOUNTS, null,
+                new AppOpsManager.OnOpChangedInternalListener() {
+            @Override
+            public void onOpChanged(int op, String packageName) {
+                try {
+                    final int userId = ActivityManager.getCurrentUser();
+                    final int uid = mPackageManager.getPackageUidAsUser(packageName, userId);
+                    final int mode = mAppOpsManager.checkOpNoThrow(
+                            AppOpsManager.OP_GET_ACCOUNTS, uid, packageName);
+                    if (mode == AppOpsManager.MODE_ALLOWED) {
+                        final long identity = Binder.clearCallingIdentity();
+                        try {
+                            cancelAccountAccessRequestNotificationIfNeeded(packageName, uid, true);
+                        } finally {
+                            Binder.restoreCallingIdentity(identity);
+                        }
+                    }
+                } catch (NameNotFoundException e) {
+                    /* ignore */
+                }
+            }
+        });
+
+        // Cancel account request notification if a permission was preventing the account access
+        mPackageManager.addOnPermissionsChangeListener(
+                (int uid) -> {
+            Account[] accounts = null;
+            String[] packageNames = mPackageManager.getPackagesForUid(uid);
+            if (packageNames != null) {
+                final int userId = UserHandle.getUserId(uid);
+                final long identity = Binder.clearCallingIdentity();
+                try {
+                    for (String packageName : packageNames) {
+                        if (mContext.getPackageManager().checkPermission(
+                                Manifest.permission.GET_ACCOUNTS, packageName)
+                                        != PackageManager.PERMISSION_GRANTED) {
+                            continue;
+                        }
+
+                        if (accounts == null) {
+                            accounts = getAccountsAsUser(null, userId, "android");
+                            if (ArrayUtils.isEmpty(accounts)) {
+                                return;
+                            }
+                        }
+
+                        for (Account account : accounts) {
+                            cancelAccountAccessRequestNotificationIfNeeded(
+                                    account, uid, packageName, true);
+                        }
+                    }
+                } finally {
+                    Binder.restoreCallingIdentity(identity);
+                }
+            }
+        });
+    }
+
+    private void cancelAccountAccessRequestNotificationIfNeeded(int uid,
+            boolean checkAccess) {
+        Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android");
+        for (Account account : accounts) {
+            cancelAccountAccessRequestNotificationIfNeeded(account, uid, checkAccess);
+        }
+    }
+
+    private void cancelAccountAccessRequestNotificationIfNeeded(String packageName, int uid,
+            boolean checkAccess) {
+        Account[] accounts = getAccountsAsUser(null, UserHandle.getUserId(uid), "android");
+        for (Account account : accounts) {
+            cancelAccountAccessRequestNotificationIfNeeded(account, uid, packageName, checkAccess);
+        }
+    }
+
+    private void cancelAccountAccessRequestNotificationIfNeeded(Account account, int uid,
+            boolean checkAccess) {
+        String[] packageNames = mPackageManager.getPackagesForUid(uid);
+        if (packageNames != null) {
+            for (String packageName : packageNames) {
+                cancelAccountAccessRequestNotificationIfNeeded(account, uid,
+                        packageName, checkAccess);
+            }
+        }
+    }
+
+    private void cancelAccountAccessRequestNotificationIfNeeded(Account account,
+            int uid, String packageName, boolean checkAccess) {
+        if (!checkAccess || hasAccountAccess(account, packageName,
+                UserHandle.getUserHandleForUid(uid))) {
+            cancelNotification(getCredentialPermissionNotificationId(account,
+                    AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName,
+                    UserHandle.getUserHandleForUid(uid));
+        }
     }
 
     @Override
@@ -393,9 +521,6 @@
         }
     }
 
-    public void systemReady() {
-    }
-
     private UserManager getUserManager() {
         if (mUserManager == null) {
             mUserManager = UserManager.get(mContext);
@@ -566,7 +691,8 @@
                     final ArrayList<String> accountNames = cur.getValue();
                     final Account[] accountsForType = new Account[accountNames.size()];
                     for (int i = 0; i < accountsForType.length; i++) {
-                        accountsForType[i] = new Account(accountNames.get(i), accountType);
+                        accountsForType[i] = new Account(accountNames.get(i), accountType,
+                                UUID.randomUUID().toString());
                     }
                     accounts.accountCache.put(accountType, accountsForType);
                 }
@@ -934,6 +1060,7 @@
      * Should only be called inside of a clearCallingIdentity block.
      */
     private AuthenticatorDescription[] getAuthenticatorTypesInternal(int userId) {
+        mAuthenticatorCache.updateServices(userId);
         Collection<AccountAuthenticatorCache.ServiceInfo<AuthenticatorDescription>>
                 authenticatorCollection = mAuthenticatorCache.getAllServices(userId);
         AuthenticatorDescription[] types =
@@ -947,8 +1074,6 @@
         return types;
     }
 
-
-
     private boolean isCrossUser(int callingUid, int userId) {
         return (userId != UserHandle.getCallingUserId()
                 && callingUid != Process.myUid()
@@ -1225,11 +1350,13 @@
             } finally {
                 db.endTransaction();
             }
-            sendAccountsChangedBroadcast(accounts.userId);
         }
         if (getUserManager().getUserInfo(accounts.userId).canHaveProfile()) {
             addAccountToLinkedRestrictedUsers(account, accounts.userId);
         }
+
+        // Only send LOGIN_ACCOUNTS_CHANGED when the database changed.
+        sendAccountsChangedBroadcast(accounts.userId);
         return true;
     }
 
@@ -1375,6 +1502,8 @@
             Bundle result = new Bundle();
             result.putString(AccountManager.KEY_ACCOUNT_NAME, resultingAccount.name);
             result.putString(AccountManager.KEY_ACCOUNT_TYPE, resultingAccount.type);
+            result.putString(AccountManager.KEY_ACCOUNT_ACCESS_ID,
+                    resultingAccount.getAccessId());
             try {
                 response.onResult(result);
             } catch (RemoteException e) {
@@ -1412,7 +1541,6 @@
         synchronized (accounts.cacheLock) {
             final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked();
             db.beginTransaction();
-            boolean isSuccessful = false;
             Account renamedAccount = new Account(newName, accountToRename.type);
             try {
                 final long accountId = getAccountIdLocked(db, accountToRename);
@@ -1425,54 +1553,53 @@
                     values.put(ACCOUNTS_PREVIOUS_NAME, accountToRename.name);
                     db.update(TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId);
                     db.setTransactionSuccessful();
-                    isSuccessful = true;
                     logRecord(db, DebugDbHelper.ACTION_ACCOUNT_RENAME, TABLE_ACCOUNTS, accountId,
                             accounts);
                 }
             } finally {
                 db.endTransaction();
-                if (isSuccessful) {
-                    /*
-                     * Database transaction was successful. Clean up cached
-                     * data associated with the account in the user profile.
-                     */
-                    insertAccountIntoCacheLocked(accounts, renamedAccount);
-                    /*
-                     * Extract the data and token caches before removing the
-                     * old account to preserve the user data associated with
-                     * the account.
-                     */
-                    HashMap<String, String> tmpData = accounts.userDataCache.get(accountToRename);
-                    HashMap<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename);
-                    removeAccountFromCacheLocked(accounts, accountToRename);
-                    /*
-                     * Update the cached data associated with the renamed
-                     * account.
-                     */
-                    accounts.userDataCache.put(renamedAccount, tmpData);
-                    accounts.authTokenCache.put(renamedAccount, tmpTokens);
-                    accounts.previousNameCache.put(
-                          renamedAccount,
-                          new AtomicReference<String>(accountToRename.name));
-                    resultAccount = renamedAccount;
+            }
+            /*
+             * Database transaction was successful. Clean up cached
+             * data associated with the account in the user profile.
+             * The account is now being tracked for remote access.
+             */
+            renamedAccount = insertAccountIntoCacheLocked(accounts, renamedAccount);
 
-                    int parentUserId = accounts.userId;
-                    if (canHaveProfile(parentUserId)) {
-                        /*
-                         * Owner or system user account was renamed, rename the account for
-                         * those users with which the account was shared.
-                         */
-                        List<UserInfo> users = getUserManager().getUsers(true);
-                        for (UserInfo user : users) {
-                            if (user.isRestricted()
-                                    && (user.restrictedProfileParentId == parentUserId)) {
-                                renameSharedAccountAsUser(accountToRename, newName, user.id);
-                            }
-                        }
+            /*
+             * Extract the data and token caches before removing the
+             * old account to preserve the user data associated with
+             * the account.
+             */
+            HashMap<String, String> tmpData = accounts.userDataCache.get(accountToRename);
+            HashMap<String, String> tmpTokens = accounts.authTokenCache.get(accountToRename);
+            removeAccountFromCacheLocked(accounts, accountToRename);
+            /*
+             * Update the cached data associated with the renamed
+             * account.
+             */
+            accounts.userDataCache.put(renamedAccount, tmpData);
+            accounts.authTokenCache.put(renamedAccount, tmpTokens);
+            accounts.previousNameCache.put(
+                    renamedAccount,
+                    new AtomicReference<String>(accountToRename.name));
+            resultAccount = renamedAccount;
+
+            int parentUserId = accounts.userId;
+            if (canHaveProfile(parentUserId)) {
+                /*
+                 * Owner or system user account was renamed, rename the account for
+                 * those users with which the account was shared.
+                 */
+                List<UserInfo> users = getUserManager().getUsers(true);
+                for (UserInfo user : users) {
+                    if (user.isRestricted()
+                            && (user.restrictedProfileParentId == parentUserId)) {
+                        renameSharedAccountAsUser(accountToRename, newName, user.id);
                     }
-                    sendAccountsChangedBroadcast(accounts.userId);
                 }
             }
+            sendAccountsChangedBroadcast(accounts.userId);
         }
         return resultAccount;
     }
@@ -1667,7 +1794,7 @@
     }
 
     private boolean removeAccountInternal(UserAccounts accounts, Account account, int callingUid) {
-        int deleted;
+        boolean isChanged = false;
         boolean userUnlocked = isLocalUnlockedUser(accounts.userId);
         if (!userUnlocked) {
             Slog.i(TAG, "Removing account " + account + " while user "+ accounts.userId
@@ -1677,25 +1804,38 @@
             final SQLiteDatabase db = userUnlocked
                     ? accounts.openHelper.getWritableDatabaseUserIsUnlocked()
                     : accounts.openHelper.getWritableDatabase();
-            final long accountId = getAccountIdLocked(db, account);
             db.beginTransaction();
+            // Set to a dummy value, this will only be used if the database
+            // transaction succeeds.
+            long accountId = -1;
             try {
-                deleted = db.delete(TABLE_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE
-                                + "=?", new String[]{account.name, account.type});
-                if (userUnlocked) {
-                    // Delete from CE table
-                    deleted = db.delete(CE_TABLE_ACCOUNTS, ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE
-                            + "=?", new String[]{account.name, account.type});
+                accountId = getAccountIdLocked(db, account);
+                if (accountId >= 0) {
+                    db.delete(
+                            TABLE_ACCOUNTS,
+                            ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE + "=?",
+                            new String[]{ account.name, account.type });
+                    if (userUnlocked) {
+                        // Delete from CE table
+                        db.delete(
+                                CE_TABLE_ACCOUNTS,
+                                ACCOUNTS_NAME + "=? AND " + ACCOUNTS_TYPE + "=?",
+                                new String[]{ account.name, account.type });
+                    }
+                    db.setTransactionSuccessful();
+                    isChanged = true;
                 }
-                db.setTransactionSuccessful();
             } finally {
                 db.endTransaction();
             }
-            removeAccountFromCacheLocked(accounts, account);
-            sendAccountsChangedBroadcast(accounts.userId);
-            String action = userUnlocked ? DebugDbHelper.ACTION_ACCOUNT_REMOVE
-                    : DebugDbHelper.ACTION_ACCOUNT_REMOVE_DE;
-            logRecord(db, action, TABLE_ACCOUNTS, accountId, accounts);
+            if (isChanged) {
+                removeAccountFromCacheLocked(accounts, account);
+                // Only broadcast LOGIN_ACCOUNTS_CHANGED if a change occured.
+                sendAccountsChangedBroadcast(accounts.userId);
+                String action = userUnlocked ? DebugDbHelper.ACTION_ACCOUNT_REMOVE
+                        : DebugDbHelper.ACTION_ACCOUNT_REMOVE_DE;
+                logRecord(db, action, TABLE_ACCOUNTS, accountId, accounts);
+            }
         }
         long id = Binder.clearCallingIdentity();
         try {
@@ -1712,7 +1852,22 @@
         } finally {
             Binder.restoreCallingIdentity(id);
         }
-        return (deleted > 0);
+
+        if (isChanged) {
+            synchronized (accounts.credentialsPermissionNotificationIds) {
+                for (Pair<Pair<Account, String>, Integer> key
+                        : accounts.credentialsPermissionNotificationIds.keySet()) {
+                    if (account.equals(key.first.first)
+                            && AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE.equals(key.first.second)) {
+                        final int uid = (Integer) key.second;
+                        mMessageHandler.post(() -> cancelAccountAccessRequestNotificationIfNeeded(
+                                account, uid, false));
+                    }
+                }
+            }
+        }
+
+        return isChanged;
     }
 
     @Override
@@ -1936,6 +2091,7 @@
         if (account == null) {
             return;
         }
+        boolean isChanged = false;
         synchronized (accounts.cacheLock) {
             final SQLiteDatabase db = accounts.openHelper.getWritableDatabaseUserIsUnlocked();
             db.beginTransaction();
@@ -1945,12 +2101,17 @@
                 final long accountId = getAccountIdLocked(db, account);
                 if (accountId >= 0) {
                     final String[] argsAccountId = {String.valueOf(accountId)};
-                    db.update(CE_TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId);
-                    db.delete(CE_TABLE_AUTHTOKENS, AUTHTOKENS_ACCOUNTS_ID + "=?", argsAccountId);
+                    db.update(
+                            CE_TABLE_ACCOUNTS, values, ACCOUNTS_ID + "=?", argsAccountId);
+                    db.delete(
+                            CE_TABLE_AUTHTOKENS, AUTHTOKENS_ACCOUNTS_ID + "=?", argsAccountId);
                     accounts.authTokenCache.remove(account);
                     accounts.accountTokenCaches.remove(account);
                     db.setTransactionSuccessful();
-
+                    // If there is an account whose password will be updated and the database
+                    // transactions succeed, then we say that a change has occured. Even if the
+                    // new password is the same as the old and there were no authtokens to delete.
+                    isChanged = true;
                     String action = (password == null || password.length() == 0) ?
                             DebugDbHelper.ACTION_CLEAR_PASSWORD
                             : DebugDbHelper.ACTION_SET_PASSWORD;
@@ -1958,8 +2119,11 @@
                 }
             } finally {
                 db.endTransaction();
+                if (isChanged) {
+                    // Send LOGIN_ACCOUNTS_CHANGED only if the something changed.
+                    sendAccountsChangedBroadcast(accounts.userId);
+                }
             }
-            sendAccountsChangedBroadcast(accounts.userId);
         }
     }
 
@@ -2099,7 +2263,7 @@
 
         final int callingUid = getCallingUid();
         clearCallingIdentity();
-        if (callingUid != Process.SYSTEM_UID) {
+        if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) {
             throw new SecurityException("can only call from system");
         }
         int userId = UserHandle.getUserId(callingUid);
@@ -2299,9 +2463,11 @@
                         if (result.containsKey(AccountManager.KEY_AUTH_TOKEN_LABEL)) {
                             Intent intent = newGrantCredentialsPermissionIntent(
                                     account,
+                                    null,
                                     callerUid,
                                     new AccountAuthenticatorResponse(this),
-                                    authTokenType);
+                                    authTokenType,
+                                    true);
                             Bundle bundle = new Bundle();
                             bundle.putParcelable(AccountManager.KEY_INTENT, intent);
                             onResult(bundle);
@@ -2352,7 +2518,7 @@
                                     intent);
                             doNotification(mAccounts,
                                     account, result.getString(AccountManager.KEY_AUTH_FAILED_MESSAGE),
-                                    intent, accounts.userId);
+                                    intent, "android", accounts.userId);
                         }
                     }
                     super.onResult(result);
@@ -2383,7 +2549,7 @@
     }
 
     private void createNoCredentialsPermissionNotification(Account account, Intent intent,
-            int userId) {
+            String packageName, int userId) {
         int uid = intent.getIntExtra(
                 GrantCredentialsPermissionActivity.EXTRAS_REQUESTING_UID, -1);
         String authTokenType = intent.getStringExtra(
@@ -2411,20 +2577,23 @@
                         PendingIntent.FLAG_CANCEL_CURRENT, null, user))
                 .build();
         installNotification(getCredentialPermissionNotificationId(
-                account, authTokenType, uid), n, user);
+                account, authTokenType, uid), n, packageName, user.getIdentifier());
     }
 
-    private Intent newGrantCredentialsPermissionIntent(Account account, int uid,
-            AccountAuthenticatorResponse response, String authTokenType) {
+    private Intent newGrantCredentialsPermissionIntent(Account account, String packageName,
+            int uid, AccountAuthenticatorResponse response, String authTokenType,
+            boolean startInNewTask) {
 
         Intent intent = new Intent(mContext, GrantCredentialsPermissionActivity.class);
-        // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag.
-        // Since it was set in Eclair+ we can't change it without breaking apps using
-        // the intent from a non-Activity context.
-        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        intent.addCategory(
-                String.valueOf(getCredentialPermissionNotificationId(account, authTokenType, uid)));
 
+        if (startInNewTask) {
+            // See FLAG_ACTIVITY_NEW_TASK docs for limitations and benefits of the flag.
+            // Since it was set in Eclair+ we can't change it without breaking apps using
+            // the intent from a non-Activity context. This is the default behavior.
+            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        }
+        intent.addCategory(String.valueOf(getCredentialPermissionNotificationId(account,
+                authTokenType, uid) + (packageName != null ? packageName : "")));
         intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_ACCOUNT, account);
         intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_TYPE, authTokenType);
         intent.putExtra(GrantCredentialsPermissionActivity.EXTRAS_RESPONSE, response);
@@ -3275,6 +3444,137 @@
     }
 
     @Override
+    public boolean hasAccountAccess(@NonNull Account account,  @NonNull String packageName,
+            @NonNull UserHandle userHandle) {
+        if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) {
+            throw new SecurityException("Can be called only by system UID");
+        }
+        Preconditions.checkNotNull(account, "account cannot be null");
+        Preconditions.checkNotNull(packageName, "packageName cannot be null");
+        Preconditions.checkNotNull(userHandle, "userHandle cannot be null");
+
+        final int userId = userHandle.getIdentifier();
+
+        Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete");
+
+        try {
+            final int uid = mPackageManager.getPackageUidAsUser(packageName, userId);
+            return hasAccountAccess(account, packageName, uid);
+        } catch (NameNotFoundException e) {
+            return false;
+        }
+    }
+
+    private boolean hasAccountAccess(@NonNull Account account, @Nullable String packageName,
+            int uid) {
+        if (packageName == null) {
+            String[] packageNames = mPackageManager.getPackagesForUid(uid);
+            if (ArrayUtils.isEmpty(packageNames)) {
+                return false;
+            }
+            // For app op checks related to permissions all packages in the UID
+            // have the same app op state, so doesn't matter which one we pick.
+            packageName = packageNames[0];
+        }
+
+        // Use null token which means any token. Having a token means the package
+        // is trusted by the authenticator, hence it is fine to access the account.
+        if (permissionIsGranted(account, null, uid, UserHandle.getUserId(uid))) {
+            return true;
+        }
+        // In addition to the permissions required to get an auth token we also allow
+        // the account to be accessed by holders of the get accounts permissions.
+        return checkUidPermission(Manifest.permission.GET_ACCOUNTS_PRIVILEGED, uid, packageName)
+                || checkUidPermission(Manifest.permission.GET_ACCOUNTS, uid, packageName);
+    }
+
+    private boolean checkUidPermission(String permission, int uid, String opPackageName) {
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            IPackageManager pm = ActivityThread.getPackageManager();
+            if (pm.checkUidPermission(permission, uid) != PackageManager.PERMISSION_GRANTED) {
+                return false;
+            }
+            final int opCode = AppOpsManager.permissionToOpCode(permission);
+            return (opCode == AppOpsManager.OP_NONE || mAppOpsManager.noteOpNoThrow(
+                    opCode, uid, opPackageName) == AppOpsManager.MODE_ALLOWED);
+        } catch (RemoteException e) {
+            /* ignore - local call */
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+        return false;
+    }
+
+    @Override
+    public IntentSender createRequestAccountAccessIntentSenderAsUser(@NonNull Account account,
+            @NonNull String packageName, @NonNull UserHandle userHandle) {
+        if (UserHandle.getAppId(Binder.getCallingUid()) != Process.SYSTEM_UID) {
+            throw new SecurityException("Can be called only by system UID");
+        }
+
+        Preconditions.checkNotNull(account, "account cannot be null");
+        Preconditions.checkNotNull(packageName, "packageName cannot be null");
+        Preconditions.checkNotNull(userHandle, "userHandle cannot be null");
+
+        final int userId = userHandle.getIdentifier();
+
+        Preconditions.checkArgumentInRange(userId, 0, Integer.MAX_VALUE, "user must be concrete");
+
+        final int uid;
+        try {
+            uid = mPackageManager.getPackageUidAsUser(packageName, userId);
+        } catch (NameNotFoundException e) {
+            Slog.e(TAG, "Unknown package " + packageName);
+            return null;
+        }
+
+        Intent intent = newRequestAccountAccessIntent(account, packageName, uid, null);
+
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            return PendingIntent.getActivityAsUser(
+                    mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT
+                            | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE,
+                    null, new UserHandle(userId)).getIntentSender();
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
+    private Intent newRequestAccountAccessIntent(Account account, String packageName,
+            int uid, RemoteCallback callback) {
+        return newGrantCredentialsPermissionIntent(account, packageName, uid,
+                new AccountAuthenticatorResponse(new IAccountAuthenticatorResponse.Stub() {
+            @Override
+            public void onResult(Bundle value) throws RemoteException {
+                handleAuthenticatorResponse(true);
+            }
+
+            @Override
+            public void onRequestContinued() {
+                /* ignore */
+            }
+
+            @Override
+            public void onError(int errorCode, String errorMessage) throws RemoteException {
+                handleAuthenticatorResponse(false);
+            }
+
+            private void handleAuthenticatorResponse(boolean accessGranted) throws RemoteException {
+                cancelNotification(getCredentialPermissionNotificationId(account,
+                        AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, uid), packageName,
+                        UserHandle.getUserHandleForUid(uid));
+                if (callback != null) {
+                    Bundle result = new Bundle();
+                    result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, accessGranted);
+                    callback.sendResult(result);
+                }
+            }
+        }), AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, false);
+    }
+
+    @Override
     public boolean someUserHasAccount(@NonNull final Account account) {
         if (!UserHandle.isSameApp(Process.SYSTEM_UID, Binder.getCallingUid())) {
             throw new SecurityException("Only system can check for accounts across users");
@@ -3779,6 +4079,30 @@
         }
     }
 
+    @Override
+    public void onAccountAccessed(String token) throws RemoteException {
+        final int uid = Binder.getCallingUid();
+        if (UserHandle.getAppId(uid) == Process.SYSTEM_UID) {
+            return;
+        }
+        final int userId = UserHandle.getCallingUserId();
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            for (Account account : getAccounts(userId, mContext.getOpPackageName())) {
+                if (Objects.equals(account.getAccessId(), token)) {
+                    // An app just accessed the account. At this point it knows about
+                    // it and there is not need to hide this account from the app.
+                    if (!hasAccountAccess(account, null, uid)) {
+                        updateAppPermission(account, AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE,
+                                uid, true);
+                    }
+                }
+            }
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
+    }
+
     private abstract class Session extends IAccountAuthenticatorResponse.Stub
             implements IBinder.DeathRecipient, ServiceConnection {
         IAccountManagerResponse mResponse;
@@ -4147,7 +4471,7 @@
         }
     }
 
-    private class MessageHandler extends Handler {
+    class MessageHandler extends Handler {
         MessageHandler(Looper looper) {
             super(looper);
         }
@@ -4914,7 +5238,7 @@
     }
 
     private void doNotification(UserAccounts accounts, Account account, CharSequence message,
-            Intent intent, int userId) {
+            Intent intent, String packageName, final int userId) {
         long identityToken = clearCallingIdentity();
         try {
             if (Log.isLoggable(TAG, Log.VERBOSE)) {
@@ -4924,12 +5248,12 @@
             if (intent.getComponent() != null &&
                     GrantCredentialsPermissionActivity.class.getName().equals(
                             intent.getComponent().getClassName())) {
-                createNoCredentialsPermissionNotification(account, intent, userId);
+                createNoCredentialsPermissionNotification(account, intent, packageName, userId);
             } else {
+                Context contextForUser = getContextForUser(new UserHandle(userId));
                 final Integer notificationId = getSigninRequiredNotificationId(accounts, account);
                 intent.addCategory(String.valueOf(notificationId));
-                UserHandle user = new UserHandle(userId);
-                Context contextForUser = getContextForUser(user);
+
                 final String notificationTitleFormat =
                         contextForUser.getText(R.string.notification_title).toString();
                 Notification n = new Notification.Builder(contextForUser)
@@ -4941,9 +5265,9 @@
                         .setContentText(message)
                         .setContentIntent(PendingIntent.getActivityAsUser(
                                 mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT,
-                                null, user))
+                                null, new UserHandle(userId)))
                         .build();
-                installNotification(notificationId, n, user);
+                installNotification(notificationId, n, packageName, userId);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -4951,18 +5275,40 @@
     }
 
     @VisibleForTesting
-    protected void installNotification(final int notificationId, final Notification n,
+    protected void installNotification(int notificationId, final Notification notification,
             UserHandle user) {
-        ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE))
-                .notifyAsUser(null, notificationId, n, user);
+        installNotification(notificationId, notification, "android", user.getIdentifier());
+    }
+
+    private void installNotification(int notificationId, final Notification notification,
+            String packageName, int userId) {
+        final long token = clearCallingIdentity();
+        try {
+            INotificationManager notificationManager = NotificationManager.getService();
+            try {
+                notificationManager.enqueueNotificationWithTag(packageName, packageName, null,
+                        notificationId, notification, new int[1], userId);
+            } catch (RemoteException e) {
+                /* ignore - local call */
+            }
+        } finally {
+            Binder.restoreCallingIdentity(token);
+        }
     }
 
     @VisibleForTesting
     protected void cancelNotification(int id, UserHandle user) {
+        cancelNotification(id, mContext.getPackageName(), user);
+    }
+
+    protected void cancelNotification(int id, String packageName, UserHandle user) {
         long identityToken = clearCallingIdentity();
         try {
-            ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE))
-                .cancelAsUser(null, id, user);
+            INotificationManager service = INotificationManager.Stub.asInterface(
+                    ServiceManager.getService(Context.NOTIFICATION_SERVICE));
+            service.cancelNotificationWithTag(packageName, null, id, user.getIdentifier());
+        } catch (RemoteException e) {
+            /* ignore - local call */
         } finally {
             restoreCallingIdentity(identityToken);
         }
@@ -5023,18 +5369,40 @@
 
     private boolean permissionIsGranted(
             Account account, String authTokenType, int callerUid, int userId) {
-        final boolean isPrivileged = isPrivileged(callerUid);
-        final boolean fromAuthenticator = account != null
-                && isAccountManagedByCaller(account.type, callerUid, userId);
-        final boolean hasExplicitGrants = account != null
-                && hasExplicitlyGrantedPermission(account, authTokenType, callerUid);
-        if (Log.isLoggable(TAG, Log.VERBOSE)) {
-            Log.v(TAG, "checkGrantsOrCallingUidAgainstAuthenticator: caller uid "
-                    + callerUid + ", " + account
-                    + ": is authenticator? " + fromAuthenticator
-                    + ", has explicit permission? " + hasExplicitGrants);
+        if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) {
+            if (Log.isLoggable(TAG, Log.VERBOSE)) {
+                Log.v(TAG, "Access to " + account + " granted calling uid is system");
+            }
+            return true;
         }
-        return fromAuthenticator || hasExplicitGrants || isPrivileged;
+
+        if (isPrivileged(callerUid)) {
+            if (Log.isLoggable(TAG, Log.VERBOSE)) {
+                Log.v(TAG, "Access to " + account + " granted calling uid "
+                        + callerUid + " privileged");
+            }
+            return true;
+        }
+        if (account != null && isAccountManagedByCaller(account.type, callerUid, userId)) {
+            if (Log.isLoggable(TAG, Log.VERBOSE)) {
+                Log.v(TAG, "Access to " + account + " granted calling uid "
+                        + callerUid + " manages the account");
+            }
+            return true;
+        }
+        if (account != null && hasExplicitlyGrantedPermission(account, authTokenType, callerUid)) {
+            if (Log.isLoggable(TAG, Log.VERBOSE)) {
+                Log.v(TAG, "Access to " + account + " granted calling uid "
+                        + callerUid + " user granted access");
+            }
+            return true;
+        }
+
+        if (Log.isLoggable(TAG, Log.VERBOSE)) {
+            Log.v(TAG, "Access to " + account + " not granted for uid " + callerUid);
+        }
+
+        return false;
     }
 
     private boolean isAccountVisibleToCaller(String accountType, int callingUid, int userId,
@@ -5118,16 +5486,26 @@
 
     private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType,
             int callerUid) {
-        if (callerUid == Process.SYSTEM_UID) {
+        if (UserHandle.getAppId(callerUid) == Process.SYSTEM_UID) {
             return true;
         }
-        UserAccounts accounts = getUserAccountsForCaller();
+        UserAccounts accounts = getUserAccounts(UserHandle.getUserId(callerUid));
         synchronized (accounts.cacheLock) {
             final SQLiteDatabase db = accounts.openHelper.getReadableDatabase();
-            String[] args = { String.valueOf(callerUid), authTokenType,
-                    account.name, account.type};
-            final boolean permissionGranted =
-                    DatabaseUtils.longForQuery(db, COUNT_OF_MATCHING_GRANTS, args) != 0;
+
+            final String query;
+            final String[] args;
+
+            if (authTokenType != null) {
+                query = COUNT_OF_MATCHING_GRANTS;
+                args = new String[] {String.valueOf(callerUid), authTokenType,
+                        account.name, account.type};
+            } else {
+                query = COUNT_OF_MATCHING_GRANTS_ANY_TOKEN;
+                args = new String[] {String.valueOf(callerUid), account.name,
+                        account.type};
+            }
+            final boolean permissionGranted = DatabaseUtils.longForQuery(db, query, args) != 0;
             if (!permissionGranted && ActivityManager.isRunningInTestHarness()) {
                 // TODO: Skip this check when running automated tests. Replace this
                 // with a more general solution.
@@ -5226,7 +5604,7 @@
             throws RemoteException {
         final int callingUid = getCallingUid();
 
-        if (callingUid != Process.SYSTEM_UID) {
+        if (UserHandle.getAppId(callingUid) != Process.SYSTEM_UID) {
             throw new SecurityException();
         }
 
@@ -5244,7 +5622,7 @@
      * which is in the system. This means we don't need to protect it with permissions.
      * @hide
      */
-    private void grantAppPermission(Account account, String authTokenType, int uid) {
+    void grantAppPermission(Account account, String authTokenType, int uid) {
         if (account == null || authTokenType == null) {
             Log.e(TAG, "grantAppPermission: called with invalid arguments", new Exception());
             return;
@@ -5268,6 +5646,14 @@
             }
             cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid),
                     UserHandle.of(accounts.userId));
+
+            cancelAccountAccessRequestNotificationIfNeeded(account, uid, true);
+        }
+
+        // Listeners are a final CopyOnWriteArrayList, hence no lock needed.
+        for (AccountManagerInternal.OnAppPermissionChangeListener listener
+                : mAppPermissionChangeListeners) {
+            mMessageHandler.post(() -> listener.onAppPermissionChanged(account, uid));
         }
     }
 
@@ -5301,9 +5687,16 @@
             } finally {
                 db.endTransaction();
             }
+
             cancelNotification(getCredentialPermissionNotificationId(account, authTokenType, uid),
                     new UserHandle(accounts.userId));
         }
+
+        // Listeners are a final CopyOnWriteArrayList, hence no lock needed.
+        for (AccountManagerInternal.OnAppPermissionChangeListener listener
+                : mAppPermissionChangeListeners) {
+            mMessageHandler.post(() -> listener.onAppPermissionChanged(account, uid));
+        }
     }
 
     static final private String stringArrayToString(String[] value) {
@@ -5334,16 +5727,22 @@
 
     /**
      * This assumes that the caller has already checked that the account is not already present.
+     * IMPORTANT: The account being inserted will begin to be tracked for access in remote
+     * processes and if you will return this account to apps you should return the result.
+     * @return The inserted account which is a new instance that is being tracked.
      */
-    private void insertAccountIntoCacheLocked(UserAccounts accounts, Account account) {
+    private Account insertAccountIntoCacheLocked(UserAccounts accounts, Account account) {
         Account[] accountsForType = accounts.accountCache.get(account.type);
         int oldLength = (accountsForType != null) ? accountsForType.length : 0;
         Account[] newAccountsForType = new Account[oldLength + 1];
         if (accountsForType != null) {
             System.arraycopy(accountsForType, 0, newAccountsForType, 0, oldLength);
         }
-        newAccountsForType[oldLength] = account;
+        String token = account.getAccessId() != null ? account.getAccessId()
+                : UUID.randomUUID().toString();
+        newAccountsForType[oldLength] = new Account(account, token);
         accounts.accountCache.put(account.type, newAccountsForType);
+        return newAccountsForType[oldLength];
     }
 
     private Account[] filterSharedAccounts(UserAccounts userAccounts, Account[] unfiltered,
@@ -5585,4 +5984,88 @@
             }
         }
     }
+
+    private final class AccountManagerInternalImpl extends AccountManagerInternal {
+        private final Object mLock = new Object();
+
+        @GuardedBy("mLock")
+        private AccountManagerBackupHelper mBackupHelper;
+
+        @Override
+        public void requestAccountAccess(@NonNull Account account, @NonNull String packageName,
+                @IntRange(from = 0) int userId, @NonNull RemoteCallback callback) {
+            if (account == null) {
+                Slog.w(TAG, "account cannot be null");
+                return;
+            }
+            if (packageName == null) {
+                Slog.w(TAG, "packageName cannot be null");
+                return;
+            }
+            if (userId < UserHandle.USER_SYSTEM) {
+                Slog.w(TAG, "user id must be concrete");
+                return;
+            }
+            if (callback == null) {
+                Slog.w(TAG, "callback cannot be null");
+                return;
+            }
+
+            if (AccountManagerService.this.hasAccountAccess(account, packageName,
+                    new UserHandle(userId))) {
+                Bundle result = new Bundle();
+                result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
+                callback.sendResult(result);
+                return;
+            }
+
+            final int uid;
+            try {
+                uid = mPackageManager.getPackageUidAsUser(packageName, userId);
+            } catch (NameNotFoundException e) {
+                Slog.e(TAG, "Unknown package " + packageName);
+                return;
+            }
+
+            Intent intent = newRequestAccountAccessIntent(account, packageName, uid, callback);
+            final UserAccounts userAccounts;
+            synchronized (mUsers) {
+                userAccounts = mUsers.get(userId);
+            }
+            doNotification(userAccounts, account, null, intent, packageName, userId);
+        }
+
+        @Override
+        public void addOnAppPermissionChangeListener(OnAppPermissionChangeListener listener) {
+            // Listeners are a final CopyOnWriteArrayList, hence no lock needed.
+            mAppPermissionChangeListeners.add(listener);
+        }
+
+        @Override
+        public boolean hasAccountAccess(@NonNull Account account, @IntRange(from = 0) int uid) {
+            return AccountManagerService.this.hasAccountAccess(account, null, uid);
+        }
+
+        @Override
+        public byte[] backupAccountAccessPermissions(int userId) {
+            synchronized (mLock) {
+                if (mBackupHelper == null) {
+                    mBackupHelper = new AccountManagerBackupHelper(
+                            AccountManagerService.this, this);
+                }
+                return mBackupHelper.backupAccountAccessPermissions(userId);
+            }
+        }
+
+        @Override
+        public void restoreAccountAccessPermissions(byte[] data, int userId) {
+            synchronized (mLock) {
+                if (mBackupHelper == null) {
+                    mBackupHelper = new AccountManagerBackupHelper(
+                            AccountManagerService.this, this);
+                }
+                mBackupHelper.restoreAccountAccessPermissions(data, userId);
+            }
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/accounts/IAccountAuthenticatorCache.java b/services/core/java/com/android/server/accounts/IAccountAuthenticatorCache.java
index bb09687..2c7d921 100644
--- a/services/core/java/com/android/server/accounts/IAccountAuthenticatorCache.java
+++ b/services/core/java/com/android/server/accounts/IAccountAuthenticatorCache.java
@@ -64,4 +64,10 @@
             Handler handler);
 
     void invalidateCache(int userId);
+
+    /**
+     * Request to update services info for which package has been updated, but hasn't been
+     * picked up by the cache.
+     */
+    void updateServices(int userId);
 }
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index ee2fa51..1bcff1a 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -699,7 +699,7 @@
                         throw new IllegalArgumentException("null notification");
                     }
                     if (r.foregroundId != id) {
-                        r.cancelNotification();
+                        cancelForegroudNotificationLocked(r);
                         r.foregroundId = id;
                     }
                     notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
@@ -721,7 +721,7 @@
                         }
                     }
                     if ((flags & Service.STOP_FOREGROUND_REMOVE) != 0) {
-                        r.cancelNotification();
+                        cancelForegroudNotificationLocked(r);
                         r.foregroundId = 0;
                         r.foregroundNoti = null;
                     } else if (r.appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP) {
@@ -738,6 +738,27 @@
         }
     }
 
+    private void cancelForegroudNotificationLocked(ServiceRecord r) {
+        if (r.foregroundId != 0) {
+            // First check to see if this app has any other active foreground services
+            // with the same notification ID.  If so, we shouldn't actually cancel it,
+            // because that would wipe away the notification that still needs to be shown
+            // due the other service.
+            ServiceMap sm = getServiceMap(r.userId);
+            if (sm != null) {
+                for (int i = sm.mServicesByName.size()-1; i >= 0; i--) {
+                    ServiceRecord other = sm.mServicesByName.valueAt(i);
+                    if (other != r && other.foregroundId == r.foregroundId
+                            && other.packageName.equals(r.packageName)) {
+                        // Found one!  Abort the cancel.
+                        return;
+                    }
+                }
+            }
+            r.cancelNotification();
+        }
+    }
+
     private void updateServiceForegroundLocked(ProcessRecord proc, boolean oomAdj) {
         boolean anyForeground = false;
         for (int i=proc.services.size()-1; i>=0; i--) {
@@ -1560,7 +1581,7 @@
             r.makeRestarting(mAm.mProcessStats.getMemFactorLocked(), now);
         }
 
-        r.cancelNotification();
+        cancelForegroudNotificationLocked(r);
 
         mAm.mHandler.removeCallbacks(r.restarter);
         mAm.mHandler.postAtTime(r.restarter, r.nextRestartTime);
@@ -2022,7 +2043,7 @@
             }
         }
 
-        r.cancelNotification();
+        cancelForegroudNotificationLocked(r);
         r.isForeground = false;
         r.foregroundId = 0;
         r.foregroundNoti = null;
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index aa4a130..1800f32 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -63,6 +63,8 @@
 import org.xmlpull.v1.XmlSerializer;
 
 import android.Manifest;
+import android.Manifest.permission;
+import android.annotation.NonNull;
 import android.annotation.UserIdInt;
 import android.app.Activity;
 import android.app.ActivityManager;
@@ -139,7 +141,6 @@
 import android.content.pm.ProviderInfo;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ServiceInfo;
-import android.content.pm.ShortcutServiceInternal;
 import android.content.pm.UserInfo;
 import android.content.res.CompatibilityInfo;
 import android.content.res.Configuration;
@@ -568,6 +569,9 @@
     private boolean mShowDialogs = true;
     private boolean mInVrMode = false;
 
+    // Whether we should use SCHED_FIFO for UI and RenderThreads.
+    private boolean mUseFifoUiScheduling = false;
+
     BroadcastQueue mFgBroadcastQueue;
     BroadcastQueue mBgBroadcastQueue;
     // Convenient for easy iteration over the queues. Foreground is first
@@ -637,7 +641,8 @@
     boolean mDoingSetFocusedActivity;
 
     public boolean canShowErrorDialogs() {
-        return mShowDialogs && !mSleeping && !mShuttingDown;
+        return mShowDialogs && !mSleeping && !mShuttingDown
+                && mLockScreenShown != LOCK_SCREEN_SHOWN;
     }
 
     private static final class PriorityState {
@@ -1083,9 +1088,9 @@
         }
 
         @Override
-        public void onChange(boolean selfChange, Uri uri) {
+        public void onChange(boolean selfChange, Uri uri, @UserIdInt int userId) {
             if (mFontScaleUri.equals(uri)) {
-                updateFontScaleIfNeeded();
+                updateFontScaleIfNeeded(userId);
             }
         }
     }
@@ -1154,6 +1159,7 @@
      * For example, references to the commonly used services.
      */
     HashMap<String, IBinder> mAppBindArgs;
+    HashMap<String, IBinder> mIsolatedAppBindArgs;
 
     /**
      * Temporary to avoid allocations.  Protected by main lock.
@@ -2307,6 +2313,9 @@
             } break;
             case VR_MODE_CHANGE_MSG: {
                 VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class);
+                if (vrService == null) {
+                    break;
+                }
                 final ActivityRecord r = (ActivityRecord) msg.obj;
                 boolean vrMode;
                 ComponentName requestedPackage;
@@ -2320,6 +2329,25 @@
                     if (mInVrMode != vrMode) {
                         mInVrMode = vrMode;
                         mShowDialogs = shouldShowDialogs(mConfiguration, mInVrMode);
+                        if (r.app != null) {
+                            ProcessRecord proc = r.app;
+                            if (proc.vrThreadTid > 0) {
+                                if (proc.curSchedGroup == ProcessList.SCHED_GROUP_TOP_APP) {
+                                    try {
+                                        if (mInVrMode == true) {
+                                            Process.setThreadScheduler(proc.vrThreadTid,
+                                                Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
+                                        } else {
+                                            Process.setThreadScheduler(proc.vrThreadTid,
+                                                Process.SCHED_OTHER, 0);
+                                        }
+                                    } catch (IllegalArgumentException e) {
+                                        Slog.w(TAG, "Failed to set scheduling policy, thread does"
+                                                + " not exist:\n" + e);
+                                    }
+                                }
+                            }
+                        }
                     }
                 }
                 vrService.setVrMode(vrMode, requestedPackage, userId, callingPackage);
@@ -2354,22 +2382,21 @@
                 if (memInfo != null) {
                     updateCpuStatsNow();
                     long nativeTotalPss = 0;
+                    final List<ProcessCpuTracker.Stats> stats;
                     synchronized (mProcessCpuTracker) {
-                        final int N = mProcessCpuTracker.countStats();
-                        for (int j=0; j<N; j++) {
-                            ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(j);
-                            if (st.vsize <= 0 || st.uid >= Process.FIRST_APPLICATION_UID) {
-                                // This is definitely an application process; skip it.
+                        stats = mProcessCpuTracker.getStats( (st)-> {
+                            return st.vsize > 0 && st.uid < Process.FIRST_APPLICATION_UID;
+                        });
+                    }
+                    final int N = stats.size();
+                    for (int j = 0; j < N; j++) {
+                        synchronized (mPidsSelfLocked) {
+                            if (mPidsSelfLocked.indexOfKey(stats.get(j).pid) >= 0) {
+                                // This is one of our own processes; skip it.
                                 continue;
                             }
-                            synchronized (mPidsSelfLocked) {
-                                if (mPidsSelfLocked.indexOfKey(st.pid) >= 0) {
-                                    // This is one of our own processes; skip it.
-                                    continue;
-                                }
-                            }
-                            nativeTotalPss += Debug.getPss(st.pid, null, null);
                         }
+                        nativeTotalPss += Debug.getPss(stats.get(j).pid, null, null);
                     }
                     memInfo.readMemInfo();
                     synchronized (ActivityManagerService.this) {
@@ -2653,6 +2680,10 @@
         GL_ES_VERSION = SystemProperties.getInt("ro.opengles.version",
             ConfigurationInfo.GL_ES_VERSION_UNDEFINED);
 
+        if (SystemProperties.getInt("sys.use_fifo_ui", 0) != 0) {
+            mUseFifoUiScheduling = true;
+        }
+
         mTrackingAssociations = "1".equals(SystemProperties.get("debug.track-associations"));
 
         mConfiguration.setToDefaults();
@@ -2908,18 +2939,24 @@
      * lazily setup to make sure the services are running when they're asked for.
      */
     private HashMap<String, IBinder> getCommonServicesLocked(boolean isolated) {
+        // Isolated processes won't get this optimization, so that we don't
+        // violate the rules about which services they have access to.
+        if (isolated) {
+            if (mIsolatedAppBindArgs == null) {
+                mIsolatedAppBindArgs = new HashMap<>();
+                mIsolatedAppBindArgs.put("package", ServiceManager.getService("package"));
+            }
+            return mIsolatedAppBindArgs;
+        }
+
         if (mAppBindArgs == null) {
             mAppBindArgs = new HashMap<>();
 
-            // Isolated processes won't get this optimization, so that we don't
-            // violate the rules about which services they have access to.
-            if (!isolated) {
-                // Setup the application init args
-                mAppBindArgs.put("package", ServiceManager.getService("package"));
-                mAppBindArgs.put("window", ServiceManager.getService("window"));
-                mAppBindArgs.put(Context.ALARM_SERVICE,
-                        ServiceManager.getService(Context.ALARM_SERVICE));
-            }
+            // Setup the application init args
+            mAppBindArgs.put("package", ServiceManager.getService("package"));
+            mAppBindArgs.put("window", ServiceManager.getService("window"));
+            mAppBindArgs.put(Context.ALARM_SERVICE,
+                    ServiceManager.getService(Context.ALARM_SERVICE));
         }
         return mAppBindArgs;
     }
@@ -3767,9 +3804,6 @@
             checkTime(startTime, "startProcess: returned from zygote!");
             Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
 
-            if (app.isolated) {
-                mBatteryStatsService.addIsolatedUid(app.uid, app.info.uid);
-            }
             mBatteryStatsService.noteProcessStart(app.processName, app.info.uid);
             checkTime(startTime, "startProcess: done updating battery stats");
 
@@ -5455,10 +5489,10 @@
             IPackageManager pm = AppGlobals.getPackageManager();
             int pkgUid = -1;
             synchronized(this) {
-                if (getPackageManagerInternalLocked().canPackageBeWiped(
+                if (getPackageManagerInternalLocked().isPackageDataProtected(
                         userId, packageName)) {
                     throw new SecurityException(
-                            "Cannot clear data for a device owner or a profile owner");
+                            "Cannot clear data for a protected package: " + packageName);
                 }
 
                 try {
@@ -6283,6 +6317,9 @@
             enqueueUidChangeLocked(uidRec, -1, UidRecord.CHANGE_ACTIVE);
         }
         proc.uidRecord = uidRec;
+
+        // Reset render thread tid if it was already set, so new process can set it again.
+        proc.renderThreadTid = 0;
         uidRec.numProcs++;
         mProcessNames.put(proc.processName, proc.uid, proc);
         if (proc.isolated) {
@@ -6683,8 +6720,7 @@
     @Override
     public void showBootMessage(final CharSequence msg, final boolean always) {
         if (Binder.getCallingUid() != Process.myUid()) {
-            // These days only the core system can call this, so apps can't get in
-            // the way of what we show about running them.
+            throw new SecurityException();
         }
         mWindowManager.showBootMessage(msg, always);
     }
@@ -8613,7 +8649,8 @@
             if (uri == null) {
                 owner.removeUriPermissionsLocked(mode);
             } else {
-                owner.removeUriPermissionLocked(new GrantUri(userId, uri, false), mode);
+                final boolean prefix = (mode & Intent.FLAG_GRANT_PREFIX_URI_PERMISSION) != 0;
+                owner.removeUriPermissionLocked(new GrantUri(userId, uri, prefix), mode);
             }
         }
     }
@@ -9284,9 +9321,6 @@
                         // sense, so turn off auto-remove.
                         intent.addFlags(Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
                     }
-                } else if ((intent.getFlags()&Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
-                    // Must be a new task.
-                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 }
                 if (!comp.equals(mLastAddedTaskComponent) || callingUid != mLastAddedTaskUid) {
                     mLastAddedTaskActivity = null;
@@ -11461,6 +11495,14 @@
                 // the uid of the isolated process is specified by the caller.
                 uid = isolatedUid;
             }
+
+            // Register the isolated UID with this application so BatteryStats knows to
+            // attribute resource usage to the application.
+            //
+            // NOTE: This is done here before addProcessNameLocked, which will tell BatteryStats
+            // about the process state of the isolated UID *before* it is registered with the
+            // owning application.
+            mBatteryStatsService.addIsolatedUid(uid, info.uid);
         }
         final ProcessRecord r = new ProcessRecord(stats, info, proc, uid);
         if (!mBooted && !mBooting
@@ -12068,6 +12110,9 @@
             case ActivityManager.BUGREPORT_OPTION_REMOTE:
                 service = "bugreportremote";
                 break;
+            case ActivityManager.BUGREPORT_OPTION_WEAR:
+                service = "bugreportwear";
+                break;
         }
         if (service == null) {
             throw new IllegalArgumentException("Provided bugreport type is not correct, value: "
@@ -12554,6 +12599,91 @@
         }
     }
 
+    public void setVrThread(int tid) {
+        if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_VR_MODE)) {
+            throw new UnsupportedOperationException("VR mode not supported on this device!");
+        }
+
+        synchronized (this) {
+            ProcessRecord proc;
+            synchronized (mPidsSelfLocked) {
+                final int pid = Binder.getCallingPid();
+                proc = mPidsSelfLocked.get(pid);
+
+                if (proc != null && mInVrMode && tid >= 0) {
+                    // ensure the tid belongs to the process
+                    if (!Process.isThreadInProcess(pid, tid)) {
+                        throw new IllegalArgumentException("VR thread does not belong to process");
+                    }
+
+                    // reset existing VR thread to CFS if this thread still exists and belongs to
+                    // the calling process
+                    if (proc.vrThreadTid != 0
+                            && Process.isThreadInProcess(pid, proc.vrThreadTid)) {
+                        try {
+                            Process.setThreadScheduler(proc.vrThreadTid, Process.SCHED_OTHER, 0);
+                        } catch (IllegalArgumentException e) {
+                            // Ignore this.  Only occurs in race condition where previous VR thread
+                            // was destroyed during this method call.
+                        }
+                    }
+
+                    proc.vrThreadTid = tid;
+
+                    // promote to FIFO now if the tid is non-zero
+                    try {
+                        if (proc.curSchedGroup == ProcessList.SCHED_GROUP_TOP_APP &&
+                            proc.vrThreadTid > 0) {
+                            Process.setThreadScheduler(proc.vrThreadTid,
+                                Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
+                        }
+                    } catch (IllegalArgumentException e) {
+                        Slog.e(TAG, "Failed to set scheduling policy, thread does"
+                               + " not exist:\n" + e);
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    public void setRenderThread(int tid) {
+        synchronized (this) {
+            ProcessRecord proc;
+            synchronized (mPidsSelfLocked) {
+                int pid = Binder.getCallingPid();
+                proc = mPidsSelfLocked.get(pid);
+                if (proc != null && proc.renderThreadTid == 0 && tid > 0) {
+                    // ensure the tid belongs to the process
+                    if (!Process.isThreadInProcess(pid, tid)) {
+                        throw new IllegalArgumentException(
+                            "Render thread does not belong to process");
+                    }
+                    proc.renderThreadTid = tid;
+                    if (DEBUG_OOM_ADJ) {
+                        Slog.d("UI_FIFO", "Set RenderThread tid " + tid + " for pid " + pid);
+                    }
+                    // promote to FIFO now
+                    if (proc.curSchedGroup == ProcessList.SCHED_GROUP_TOP_APP) {
+                        if (DEBUG_OOM_ADJ) Slog.d("UI_FIFO", "Promoting " + tid + "out of band");
+                        if (mUseFifoUiScheduling) {
+                            Process.setThreadScheduler(proc.renderThreadTid,
+                                Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
+                        } else {
+                            Process.setThreadPriority(proc.renderThreadTid, -10);
+                        }
+                    }
+                } else {
+                    if (DEBUG_OOM_ADJ) {
+                        Slog.d("UI_FIFO", "Didn't set thread from setRenderThread? " +
+                               "PID: " + pid + ", TID: " + tid + " FIFO: " +
+                               mUseFifoUiScheduling);
+                    }
+                }
+            }
+        }
+    }
+
     @Override
     public int setVrMode(IBinder token, boolean enabled, ComponentName packageName) {
         if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_VR_MODE)) {
@@ -12619,6 +12749,43 @@
         }
     }
 
+    @Override
+    public void setHasTopUi(boolean hasTopUi) throws RemoteException {
+        if (checkCallingPermission(permission.INTERNAL_SYSTEM_WINDOW) != PERMISSION_GRANTED) {
+            String msg = "Permission Denial: setHasTopUi() from pid="
+                    + Binder.getCallingPid()
+                    + ", uid=" + Binder.getCallingUid()
+                    + " requires " + permission.INTERNAL_SYSTEM_WINDOW;
+            Slog.w(TAG, msg);
+            throw new SecurityException(msg);
+        }
+        final int pid = Binder.getCallingPid();
+        final long origId = Binder.clearCallingIdentity();
+        try {
+            synchronized (this) {
+                boolean changed = false;
+                ProcessRecord pr;
+                synchronized (mPidsSelfLocked) {
+                    pr = mPidsSelfLocked.get(pid);
+                    if (pr == null) {
+                        Slog.w(TAG, "setHasTopUi called on unknown pid: " + pid);
+                        return;
+                    }
+                    if (pr.hasTopUi != hasTopUi) {
+                        Slog.i(TAG, "Setting hasTopUi=" + hasTopUi + " for pid=" + pid);
+                        pr.hasTopUi = hasTopUi;
+                        changed = true;
+                    }
+                }
+                if (changed) {
+                    updateOomAdjLocked(pr);
+                }
+            }
+        } finally {
+            Binder.restoreCallingIdentity(origId);
+        }
+    }
+
     public final void enterSafeMode() {
         synchronized(this) {
             // It only makes sense to do this before the system is ready
@@ -13551,7 +13718,7 @@
             sb.append("Process: ").append(processName).append("\n");
             int flags = process.info.flags;
             IPackageManager pm = AppGlobals.getPackageManager();
-            sb.append("Flags: 0x").append(Integer.toString(flags, 16)).append("\n");
+            sb.append("Flags: 0x").append(Integer.toHexString(flags)).append("\n");
             for (int ip=0; ip<process.pkgList.size(); ip++) {
                 String pkg = process.pkgList.keyAt(ip);
                 sb.append("Package: ").append(pkg);
@@ -13679,7 +13846,7 @@
                     try {
                         java.lang.Process logcat = new ProcessBuilder(
                                 "/system/bin/timeout", "-k", "15s", "10s",
-                                "/system/bin/logcat", "-v", "time", "-b", "events", "-b", "system",
+                                "/system/bin/logcat", "-v", "threadtime", "-b", "events", "-b", "system",
                                 "-b", "main", "-b", "crash", "-t", String.valueOf(lines))
                                         .redirectErrorStream(true).start();
 
@@ -16369,21 +16536,24 @@
         }
         updateCpuStatsNow();
         long[] memtrackTmp = new long[1];
+        final List<ProcessCpuTracker.Stats> stats;
+        // Get a list of Stats that have vsize > 0
         synchronized (mProcessCpuTracker) {
-            final int N = mProcessCpuTracker.countStats();
-            for (int i=0; i<N; i++) {
-                ProcessCpuTracker.Stats st = mProcessCpuTracker.getStats(i);
-                if (st.vsize > 0) {
-                    long pss = Debug.getPss(st.pid, null, memtrackTmp);
-                    if (pss > 0) {
-                        if (infoMap.indexOfKey(st.pid) < 0) {
-                            ProcessMemInfo mi = new ProcessMemInfo(st.name, st.pid,
-                                    ProcessList.NATIVE_ADJ, -1, "native", null);
-                            mi.pss = pss;
-                            mi.memtrack = memtrackTmp[0];
-                            memInfos.add(mi);
-                        }
-                    }
+            stats = mProcessCpuTracker.getStats((st) -> {
+                return st.vsize > 0;
+            });
+        }
+        final int statsCount = stats.size();
+        for (int i = 0; i < statsCount; i++) {
+            ProcessCpuTracker.Stats st = stats.get(i);
+            long pss = Debug.getPss(st.pid, null, memtrackTmp);
+            if (pss > 0) {
+                if (infoMap.indexOfKey(st.pid) < 0) {
+                    ProcessMemInfo mi = new ProcessMemInfo(st.name, st.pid,
+                            ProcessList.NATIVE_ADJ, -1, "native", null);
+                    mi.pss = pss;
+                    mi.memtrack = memtrackTmp[0];
+                    memInfos.add(mi);
                 }
             }
         }
@@ -17627,6 +17797,71 @@
         return INTENT_REMOTE_BUGREPORT_FINISHED.equals(intent.getAction());
     }
 
+    private void checkBroadcastFromSystem(Intent intent, ProcessRecord callerApp,
+            String callerPackage, int callingUid, boolean isProtectedBroadcast, List receivers) {
+        final String action = intent.getAction();
+        if (isProtectedBroadcast
+                || Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
+                || Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS.equals(action)
+                || Intent.ACTION_MEDIA_BUTTON.equals(action)
+                || Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)
+                || Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS.equals(action)
+                || Intent.ACTION_MASTER_CLEAR.equals(action)
+                || AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)
+                || AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
+                || LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)
+                || TelephonyIntents.ACTION_REQUEST_OMADM_CONFIGURATION_UPDATE.equals(action)
+                || SuggestionSpan.ACTION_SUGGESTION_PICKED.equals(action)) {
+            // Broadcast is either protected, or it's a public action that
+            // we've relaxed, so it's fine for system internals to send.
+            return;
+        }
+
+        // This broadcast may be a problem...  but there are often system components that
+        // want to send an internal broadcast to themselves, which is annoying to have to
+        // explicitly list each action as a protected broadcast, so we will check for that
+        // one safe case and allow it: an explicit broadcast, only being received by something
+        // that has protected itself.
+        if (receivers != null && receivers.size() > 0
+                && (intent.getPackage() != null || intent.getComponent() != null)) {
+            boolean allProtected = true;
+            for (int i = receivers.size()-1; i >= 0; i--) {
+                Object target = receivers.get(i);
+                if (target instanceof ResolveInfo) {
+                    ResolveInfo ri = (ResolveInfo)target;
+                    if (ri.activityInfo.exported && ri.activityInfo.permission == null) {
+                        allProtected = false;
+                        break;
+                    }
+                } else {
+                    BroadcastFilter bf = (BroadcastFilter)target;
+                    if (bf.requiredPermission == null) {
+                        allProtected = false;
+                        break;
+                    }
+                }
+            }
+            if (allProtected) {
+                // All safe!
+                return;
+            }
+        }
+
+        // The vast majority of broadcasts sent from system internals
+        // should be protected to avoid security holes, so yell loudly
+        // to ensure we examine these cases.
+        if (callerApp != null) {
+            Log.wtf(TAG, "Sending non-protected broadcast " + action
+                            + " from system " + callerApp.toShortString() + " pkg " + callerPackage,
+                    new Throwable());
+        } else {
+            Log.wtf(TAG, "Sending non-protected broadcast " + action
+                            + " from system uid " + UserHandle.formatUid(callingUid)
+                            + " pkg " + callerPackage,
+                    new Throwable());
+        }
+    }
+
     final int broadcastIntentLocked(ProcessRecord callerApp,
             String callerPackage, Intent intent, String resolvedType,
             IIntentReceiver resultTo, int resultCode, String resultData,
@@ -17713,37 +17948,9 @@
                 break;
         }
 
-        if (isCallerSystem) {
-            if (isProtectedBroadcast
-                    || Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
-                    || Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS.equals(action)
-                    || Intent.ACTION_MEDIA_BUTTON.equals(action)
-                    || Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)
-                    || Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS.equals(action)
-                    || AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)
-                    || AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
-                    || LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)
-                    || TelephonyIntents.ACTION_REQUEST_OMADM_CONFIGURATION_UPDATE.equals(action)
-                    || SuggestionSpan.ACTION_SUGGESTION_PICKED.equals(action)) {
-                // Broadcast is either protected, or it's a public action that
-                // we've relaxed, so it's fine for system internals to send.
-            } else {
-                // The vast majority of broadcasts sent from system internals
-                // should be protected to avoid security holes, so yell loudly
-                // to ensure we examine these cases.
-                if (callerApp != null) {
-                    Log.wtf(TAG, "Sending non-protected broadcast " + action
-                            + " from system " + callerApp.toShortString() + " pkg " + callerPackage,
-                            new Throwable());
-                } else {
-                    Log.wtf(TAG, "Sending non-protected broadcast " + action
-                            + " from system uid " + UserHandle.formatUid(callingUid)
-                            + " pkg " + callerPackage,
-                            new Throwable());
-                }
-            }
-
-        } else {
+        // First line security check before anything else: stop non-system apps from
+        // sending protected broadcasts.
+        if (!isCallerSystem) {
             if (isProtectedBroadcast) {
                 String msg = "Permission Denial: not allowed to send broadcast "
                         + action + " from pid="
@@ -18126,6 +18333,10 @@
             // If we are not serializing this broadcast, then send the
             // registered receivers separately so they don't wait for the
             // components to be launched.
+            if (isCallerSystem) {
+                checkBroadcastFromSystem(intent, callerApp, callerPackage, callingUid,
+                        isProtectedBroadcast, registeredReceivers);
+            }
             final BroadcastQueue queue = broadcastQueueForIntent(intent);
             BroadcastRecord r = new BroadcastRecord(queue, intent, callerApp,
                     callerPackage, callingPid, callingUid, resolvedType, requiredPermissions,
@@ -18213,6 +18424,11 @@
             ir++;
         }
 
+        if (isCallerSystem) {
+            checkBroadcastFromSystem(intent, callerApp, callerPackage, callingUid,
+                    isProtectedBroadcast, receivers);
+        }
+
         if ((receivers != null && receivers.size() > 0)
                 || resultTo != null) {
             BroadcastQueue queue = broadcastQueueForIntent(intent);
@@ -18631,23 +18847,28 @@
         int userId = UserHandle.getCallingUserId();
 
         synchronized(this) {
-            final long origId = Binder.clearCallingIdentity();
-            updateConfigurationLocked(values, null, false, true, userId);
+            updatePersistentConfigurationLocked(values, userId);
+        }
+    }
+
+    private void updatePersistentConfigurationLocked(Configuration values, @UserIdInt int userId) {
+        final long origId = Binder.clearCallingIdentity();
+        try {
+            updateConfigurationLocked(values, null, false, true, userId, false /* deferResume */);
+        } finally {
             Binder.restoreCallingIdentity(origId);
         }
     }
 
-    private void updateFontScaleIfNeeded() {
-        final int currentUserId;
-        synchronized(this) {
-            currentUserId = mUserController.getCurrentUserIdLocked();
-        }
+    private void updateFontScaleIfNeeded(@UserIdInt int userId) {
         final float scaleFactor = Settings.System.getFloatForUser(mContext.getContentResolver(),
-                FONT_SCALE, 1.0f, currentUserId);
+                FONT_SCALE, 1.0f, userId);
         if (mConfiguration.fontScale != scaleFactor) {
             final Configuration configuration = mWindowManager.computeNewConfiguration();
             configuration.fontScale = scaleFactor;
-            updatePersistentConfiguration(configuration);
+            synchronized (this) {
+                updatePersistentConfigurationLocked(configuration, userId);
+            }
         }
     }
 
@@ -18700,11 +18921,16 @@
         updateConfigurationLocked(configuration, null, false);
     }
 
-    boolean updateConfigurationLocked(Configuration values,
-            ActivityRecord starting, boolean initLocale) {
+    boolean updateConfigurationLocked(Configuration values, ActivityRecord starting,
+            boolean initLocale) {
+        return updateConfigurationLocked(values, starting, initLocale, false /* deferResume */);
+    }
+
+    boolean updateConfigurationLocked(Configuration values, ActivityRecord starting,
+            boolean initLocale, boolean deferResume) {
         // pass UserHandle.USER_NULL as userId because we don't persist configuration for any user
-        return updateConfigurationLocked(values, starting, initLocale, false,
-                UserHandle.USER_NULL);
+        return updateConfigurationLocked(values, starting, initLocale, false /* persistent */,
+                UserHandle.USER_NULL, deferResume);
     }
 
     // To cache the list of supported system locales
@@ -18720,8 +18946,8 @@
      * @param userId is only used when persistent parameter is set to true to persist configuration
      *               for that particular user
      */
-    private boolean updateConfigurationLocked(Configuration values,
-            ActivityRecord starting, boolean initLocale, boolean persistent, int userId) {
+    private boolean updateConfigurationLocked(Configuration values, ActivityRecord starting,
+            boolean initLocale, boolean persistent, int userId, boolean deferResume) {
         int changes = 0;
 
         if (mWindowManager != null) {
@@ -18820,18 +19046,9 @@
                         null, AppOpsManager.OP_NONE, null, false, false,
                         MY_PID, Process.SYSTEM_UID, UserHandle.USER_ALL);
                 if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
-                    // Tell the shortcut manager that the system locale changed.  It needs to know
-                    // it before any other apps receive ACTION_LOCALE_CHANGED, which is why
-                    // we "push" from here, rather than having the service listen to the broadcast.
-                    final ShortcutServiceInternal shortcutService =
-                            LocalServices.getService(ShortcutServiceInternal.class);
-                    if (shortcutService != null) {
-                        shortcutService.onSystemLocaleChangedNoLock();
-                    }
-
                     intent = new Intent(Intent.ACTION_LOCALE_CHANGED);
                     intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
-                    if (!mProcessesReady) {
+	            if (initLocale || !mProcessesReady) {
                         intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
                     }
                     broadcastIntentLocked(null, null, intent,
@@ -18849,7 +19066,7 @@
                     for (int stackId : resizedStacks) {
                         final Rect newBounds = mWindowManager.getBoundsForNewConfiguration(stackId);
                         mStackSupervisor.resizeStackLocked(
-                                stackId, newBounds, null, null, false, false, !DEFER_RESUME);
+                                stackId, newBounds, null, null, false, false, deferResume);
                     }
                 }
             }
@@ -18882,8 +19099,9 @@
 
     /**
      * Decide based on the configuration whether we should shouw the ANR,
-     * crash, etc dialogs.  The idea is that if there is no affordnace to
-     * press the on-screen buttons, we shouldn't show the dialog.
+     * crash, etc dialogs.  The idea is that if there is no affordence to
+     * press the on-screen buttons, or the user experience would be more
+     * greatly impacted than the crash itself, we shouldn't show the dialog.
      *
      * A thought: SystemUI might also want to get told about this, the Power
      * dialog / global actions also might want different behaviors.
@@ -18892,9 +19110,10 @@
         final boolean inputMethodExists = !(config.keyboard == Configuration.KEYBOARD_NOKEYS
                                    && config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH
                                    && config.navigation == Configuration.NAVIGATION_NONAV);
-        final boolean uiIsNotCarType = !((config.uiMode & Configuration.UI_MODE_TYPE_MASK)
-                                    == Configuration.UI_MODE_TYPE_CAR);
-        return inputMethodExists && uiIsNotCarType && !inVrMode;
+        int modeType = config.uiMode & Configuration.UI_MODE_TYPE_MASK;
+        final boolean uiModeSupportsDialogs = (modeType != Configuration.UI_MODE_TYPE_CAR
+                && !(modeType == Configuration.UI_MODE_TYPE_WATCH && "user".equals(Build.TYPE)));
+        return inputMethodExists && uiModeSupportsDialogs && !inVrMode;
     }
 
     @Override
@@ -19104,6 +19323,10 @@
                 app.systemNoUi = false;
                 app.curSchedGroup = ProcessList.SCHED_GROUP_TOP_APP;
                 app.adjType = "pers-top-activity";
+            } else if (app.hasTopUi) {
+                app.systemNoUi = false;
+                app.curSchedGroup = ProcessList.SCHED_GROUP_TOP_APP;
+                app.adjType = "pers-top-ui";
             } else if (activitiesSize > 0) {
                 for (int j = 0; j < activitiesSize; j++) {
                     final ActivityRecord r = app.activities.get(j);
@@ -19553,7 +19776,7 @@
                             adj = ProcessList.FOREGROUND_APP_ADJ;
                             if ((cr.flags&Context.BIND_NOT_FOREGROUND) == 0) {
                                 if ((cr.flags&Context.BIND_IMPORTANT) != 0) {
-                                    schedGroup = ProcessList.SCHED_GROUP_TOP_APP;
+                                    schedGroup = ProcessList.SCHED_GROUP_TOP_APP_BOUND;
                                 } else {
                                     schedGroup = ProcessList.SCHED_GROUP_DEFAULT;
                                 }
@@ -20154,6 +20377,7 @@
         }
 
         if (app.setSchedGroup != app.curSchedGroup) {
+            int oldSchedGroup = app.setSchedGroup;
             app.setSchedGroup = app.curSchedGroup;
             if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG_OOM_ADJ,
                     "Setting sched group of " + app.processName
@@ -20169,30 +20393,95 @@
                         processGroup = Process.THREAD_GROUP_BG_NONINTERACTIVE;
                         break;
                     case ProcessList.SCHED_GROUP_TOP_APP:
+                    case ProcessList.SCHED_GROUP_TOP_APP_BOUND:
                         processGroup = Process.THREAD_GROUP_TOP_APP;
                         break;
                     default:
                         processGroup = Process.THREAD_GROUP_DEFAULT;
                         break;
                 }
-                if (true) {
-                    long oldId = Binder.clearCallingIdentity();
-                    try {
-                        Process.setProcessGroup(app.pid, processGroup);
-                    } catch (Exception e) {
-                        Slog.w(TAG, "Failed setting process group of " + app.pid
-                                + " to " + app.curSchedGroup);
-                        e.printStackTrace();
-                    } finally {
-                        Binder.restoreCallingIdentity(oldId);
-                    }
-                } else {
-                    if (app.thread != null) {
-                        try {
-                            app.thread.setSchedulingGroup(processGroup);
-                        } catch (RemoteException e) {
+                long oldId = Binder.clearCallingIdentity();
+                try {
+                    Process.setProcessGroup(app.pid, processGroup);
+                    if (app.curSchedGroup == ProcessList.SCHED_GROUP_TOP_APP) {
+                        // do nothing if we already switched to RT
+                        if (oldSchedGroup != ProcessList.SCHED_GROUP_TOP_APP) {
+                            // Switch VR thread for app to SCHED_FIFO
+                            if (mInVrMode && app.vrThreadTid != 0) {
+                                try {
+                                    Process.setThreadScheduler(app.vrThreadTid,
+                                        Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
+                                } catch (IllegalArgumentException e) {
+                                    // thread died, ignore
+                                }
+                            }
+                            if (mUseFifoUiScheduling) {
+                                // Switch UI pipeline for app to SCHED_FIFO
+                                app.savedPriority = Process.getThreadPriority(app.pid);
+                                try {
+                                    Process.setThreadScheduler(app.pid,
+                                        Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
+                                } catch (IllegalArgumentException e) {
+                                    // thread died, ignore
+                                }
+                                if (app.renderThreadTid != 0) {
+                                    try {
+                                        Process.setThreadScheduler(app.renderThreadTid,
+                                            Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK, 1);
+                                    } catch (IllegalArgumentException e) {
+                                        // thread died, ignore
+                                    }
+                                    if (DEBUG_OOM_ADJ) {
+                                        Slog.d("UI_FIFO", "Set RenderThread (TID " +
+                                            app.renderThreadTid + ") to FIFO");
+                                    }
+                                } else {
+                                    if (DEBUG_OOM_ADJ) {
+                                        Slog.d("UI_FIFO", "Not setting RenderThread TID");
+                                    }
+                                }
+                            } else {
+                                // Boost priority for top app UI and render threads
+                                Process.setThreadPriority(app.pid, -10);
+                                if (app.renderThreadTid != 0) {
+                                    try {
+                                        Process.setThreadPriority(app.renderThreadTid, -10);
+                                    } catch (IllegalArgumentException e) {
+                                        // thread died, ignore
+                                    }
+                                }
+                            }
+                        }
+                    } else if (oldSchedGroup == ProcessList.SCHED_GROUP_TOP_APP &&
+                               app.curSchedGroup != ProcessList.SCHED_GROUP_TOP_APP) {
+                        // Reset VR thread to SCHED_OTHER
+                        // Safe to do even if we're not in VR mode
+                        if (app.vrThreadTid != 0) {
+                            Process.setThreadScheduler(app.vrThreadTid, Process.SCHED_OTHER, 0);
+                        }
+                        if (mUseFifoUiScheduling) {
+                            // Reset UI pipeline to SCHED_OTHER
+                            Process.setThreadScheduler(app.pid, Process.SCHED_OTHER, 0);
+                            Process.setThreadPriority(app.pid, app.savedPriority);
+                            if (app.renderThreadTid != 0) {
+                                Process.setThreadScheduler(app.renderThreadTid,
+                                    Process.SCHED_OTHER, 0);
+                                Process.setThreadPriority(app.renderThreadTid, -4);
+                            }
+                        } else {
+                            // Reset priority for top app UI and render threads
+                            Process.setThreadPriority(app.pid, 0);
+                            if (app.renderThreadTid != 0) {
+                                Process.setThreadPriority(app.renderThreadTid, 0);
+                            }
                         }
                     }
+                } catch (Exception e) {
+                    Slog.w(TAG, "Failed setting process group of " + app.pid
+                            + " to " + app.curSchedGroup);
+                    e.printStackTrace();
+                } finally {
+                    Binder.restoreCallingIdentity(oldId);
                 }
             }
         }
@@ -20388,8 +20677,11 @@
             final long now = SystemClock.elapsedRealtime();
             Long lastReported = userState.mProviderLastReportedFg.get(authority);
             if (lastReported == null || lastReported < now - 60 * 1000L) {
-                mUsageStatsService.reportContentProviderUsage(
-                        authority, providerPkgName, app.userId);
+                if (mSystemReady) {
+                    // Cannot touch the user stats if not system ready
+                    mUsageStatsService.reportContentProviderUsage(
+                            authority, providerPkgName, app.userId);
+                }
                 userState.mProviderLastReportedFg.put(authority, now);
             }
         }
@@ -20419,8 +20711,10 @@
                 isInteraction = nowElapsed > app.fgInteractionTime + SERVICE_USAGE_INTERACTION_TIME;
             }
         } else {
-            isInteraction = app.curProcState
-                    <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
+            // If the app was being forced to the foreground, by say a Toast, then
+            // no need to treat it as an interaction
+            isInteraction = app.forcingToForeground == null
+                    && app.curProcState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
             app.fgInteractionTime = 0;
         }
         if (isInteraction && (!app.reportedInteraction
@@ -21355,6 +21649,11 @@
                 Slog.w(TAG, "No user info for user #" + targetUserId);
                 return false;
             }
+            if (!targetUserInfo.isDemo() && UserManager.isDeviceInDemoMode(mContext)) {
+                Slog.w(TAG, "Cannot switch to non-demo user #" + targetUserId
+                        + " when device is in demo mode");
+                return false;
+            }
             if (!targetUserInfo.supportsSwitchTo()) {
                 Slog.w(TAG, "Cannot switch to User #" + targetUserId + ": not supported");
                 return false;
@@ -21390,8 +21689,9 @@
 
     @Override
     public boolean isUserRunning(int userId, int flags) {
-        if (userId != UserHandle.getCallingUserId() && checkCallingPermission(
-                INTERACT_ACROSS_USERS) != PackageManager.PERMISSION_GRANTED) {
+        if (!mUserController.isSameProfileGroup(userId, UserHandle.getCallingUserId())
+                && checkCallingPermission(INTERACT_ACROSS_USERS)
+                    != PackageManager.PERMISSION_GRANTED) {
             String msg = "Permission Denial: isUserRunning() from pid="
                     + Binder.getCallingPid()
                     + ", uid=" + Binder.getCallingUid()
@@ -21421,8 +21721,8 @@
     }
 
     @Override
-    public void registerUserSwitchObserver(IUserSwitchObserver observer) {
-        mUserController.registerUserSwitchObserver(observer);
+    public void registerUserSwitchObserver(IUserSwitchObserver observer, String name) {
+        mUserController.registerUserSwitchObserver(observer, name);
     }
 
     @Override
@@ -21696,6 +21996,48 @@
             }
             ((PendingIntentRecord) target).setWhitelistDuration(duration);
         }
+
+        @Override
+        public void updatePersistentConfigurationForUser(@NonNull Configuration values,
+                int userId) {
+            Preconditions.checkNotNull(values, "Configuration must not be null");
+            Preconditions.checkArgumentNonnegative(userId, "userId " + userId + " not supported");
+            synchronized (ActivityManagerService.this) {
+                updateConfigurationLocked(values, null, false, true, userId,
+                        false /* deferResume */);
+            }
+        }
+
+        @Override
+        public int startActivitiesAsPackage(String packageName, int userId, Intent[] intents,
+                Bundle bOptions) {
+            Preconditions.checkNotNull(intents, "intents");
+            final String[] resolvedTypes = new String[intents.length];
+            for (int i = 0; i < intents.length; i++) {
+                resolvedTypes[i] = intents[i].resolveTypeIfNeeded(mContext.getContentResolver());
+            }
+
+            // UID of the package on user userId.
+            // "= 0" is needed because otherwise catch(RemoteException) would make it look like
+            // packageUid may not be initialized.
+            int packageUid = 0;
+            try {
+                packageUid = AppGlobals.getPackageManager().getPackageUid(
+                        packageName, PackageManager.MATCH_DEBUG_TRIAGED_MISSING, userId);
+            } catch (RemoteException e) {
+                // Shouldn't happen.
+            }
+
+            synchronized (ActivityManagerService.this) {
+                return startActivitiesInPackage(packageUid, packageName, intents, resolvedTypes,
+                        /*resultTo*/ null, bOptions, userId);
+            }
+        }
+
+        @Override
+        public int getUidProcessState(int uid) {
+            return getUidState(uid);
+        }
     }
 
     private final class SleepTokenImpl extends SleepToken {
@@ -21872,4 +22214,22 @@
             Binder.restoreCallingIdentity(callingId);
         }
     }
+
+    @Override
+    public boolean canBypassWorkChallenge(PendingIntent intent) throws RemoteException {
+        final int userId = intent.getCreatorUserHandle().getIdentifier();
+        if (!mUserController.isUserRunningLocked(userId, ActivityManager.FLAG_AND_LOCKED)) {
+            return false;
+        }
+        IIntentSender target = intent.getTarget();
+        if (!(target instanceof PendingIntentRecord)) {
+            return false;
+        }
+        final PendingIntentRecord record = (PendingIntentRecord) target;
+        final ResolveInfo rInfo = mStackSupervisor.resolveIntent(record.key.requestIntent,
+                record.key.requestResolvedType, userId, PackageManager.MATCH_DIRECT_BOOT_AWARE);
+        // For direct boot aware activities, they can be shown without triggering a work challenge
+        // before the profile user is unlocked.
+        return rInfo != null && rInfo.activityInfo != null;
+    }
 }
diff --git a/services/core/java/com/android/server/am/ActivityRecord.java b/services/core/java/com/android/server/am/ActivityRecord.java
index 2325044..67327db 100644
--- a/services/core/java/com/android/server/am/ActivityRecord.java
+++ b/services/core/java/com/android/server/am/ActivityRecord.java
@@ -17,6 +17,7 @@
 package com.android.server.am;
 
 import static android.app.ActivityManager.StackId;
+import static android.app.ActivityManager.StackId.DOCKED_STACK_ID;
 import static android.app.ActivityManager.StackId.FREEFORM_WORKSPACE_STACK_ID;
 import static android.app.ActivityManager.StackId.PINNED_STACK_ID;
 import static android.content.pm.ActivityInfo.RESIZE_MODE_CROP_WINDOWS;
@@ -221,6 +222,13 @@
     boolean pendingVoiceInteractionStart;   // Waiting for activity-invoked voice session
     IVoiceInteractionSession voiceSession;  // Voice interaction session for this activity
 
+    // A hint to override the window specified rotation animation, or -1
+    // to use the window specified value. We use this so that
+    // we can select the right animation in the cases of starting
+    // windows, where the app hasn't had time to set a value
+    // on the window.
+    int mRotationAnimationHint = -1;
+
     private static String startingWindowStateToString(int state) {
         switch (state) {
             case STARTING_WINDOW_NOT_SHOWN:
@@ -635,6 +643,7 @@
         if (options != null) {
             pendingOptions = options;
             mLaunchTaskBehind = pendingOptions.getLaunchTaskBehind();
+            mRotationAnimationHint = pendingOptions.getRotationAnimationHint();
             PendingIntent usageReport = pendingOptions.getUsageTimeReport();
             if (usageReport != null) {
                 appTimeTracker = new AppTimeTracker(usageReport);
@@ -742,6 +751,14 @@
                 && intent.getType() == null;
     }
 
+    static boolean isMainIntent(Intent intent) {
+        return Intent.ACTION_MAIN.equals(intent.getAction())
+                && intent.hasCategory(Intent.CATEGORY_LAUNCHER)
+                && intent.getCategories().size() == 1
+                && intent.getData() == null
+                && intent.getType() == null;
+    }
+
     private boolean canLaunchHomeActivity(int uid, ActivityRecord sourceRecord) {
         if (uid == Process.myUid() || uid == 0) {
             // System process can launch home activity.
@@ -930,21 +947,25 @@
         // The activity now gets access to the data associated with this Intent.
         service.grantUriPermissionFromIntentLocked(callingUid, packageName,
                 intent, getUriPermissionsLocked(), userId);
-        // We want to immediately deliver the intent to the activity if
-        // it is currently the top resumed activity...  however, if the
-        // device is sleeping, then all activities are stopped, so in that
-        // case we will deliver it if this is the current top activity on its
-        // stack.
         final ReferrerIntent rintent = new ReferrerIntent(intent, referrer);
         boolean unsent = true;
-        if ((state == ActivityState.RESUMED
-                || (service.isSleepingLocked() && task.stack != null
-                    && task.stack.topRunningActivityLocked() == this))
-                && app != null && app.thread != null) {
+        final ActivityStack stack = task.stack;
+        final boolean isTopActivityInStack =
+                stack != null && stack.topRunningActivityLocked() == this;
+        final boolean isTopActivityWhileSleeping =
+                service.isSleepingLocked() && isTopActivityInStack;
+
+        // We want to immediately deliver the intent to the activity if:
+        // - It is currently resumed or paused. i.e. it is currently visible to the user and we want
+        //   the user to see the visual effects caused by the intent delivery now.
+        // - The device is sleeping and it is the top activity behind the lock screen (b/6700897).
+        if ((state == ActivityState.RESUMED || state == ActivityState.PAUSED
+                || isTopActivityWhileSleeping) && app != null && app.thread != null) {
             try {
                 ArrayList<ReferrerIntent> ar = new ArrayList<>(1);
                 ar.add(rintent);
-                app.thread.scheduleNewIntent(ar, appToken);
+                app.thread.scheduleNewIntent(
+                        ar, appToken, state == ActivityState.PAUSED /* andPause */);
                 unsent = false;
             } catch (RemoteException e) {
                 Slog.w(TAG, "Exception thrown sending new intent to " + this, e);
@@ -1293,8 +1314,12 @@
                 state == ActivityState.RESUMED;
     }
 
-    public void setSleeping(boolean _sleeping) {
-        if (sleeping == _sleeping) {
+    void setSleeping(boolean _sleeping) {
+        setSleeping(_sleeping, false);
+    }
+
+    void setSleeping(boolean _sleeping, boolean force) {
+        if (!force && sleeping == _sleeping) {
             return;
         }
         if (app != null && app.thread != null) {
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index 5859d34..1b552b5 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -29,6 +29,7 @@
 import static android.content.pm.ActivityInfo.FLAG_RESUME_WHILE_PAUSING;
 import static android.content.pm.ActivityInfo.FLAG_SHOW_FOR_ALL_USERS;
 import static android.content.res.Configuration.SCREENLAYOUT_UNDEFINED;
+import static android.view.Display.DEFAULT_DISPLAY;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ADD_REMOVE;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ALL;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_APP;
@@ -68,6 +69,7 @@
 import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
 import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.am.ActivityManagerService.LOCK_SCREEN_SHOWN;
+import static com.android.server.am.ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS;
 import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
 import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
 import static com.android.server.am.ActivityRecord.STARTING_WINDOW_REMOVED;
@@ -669,7 +671,7 @@
 
     final boolean isOnHomeDisplay() {
         return isAttached() &&
-                mActivityContainer.mActivityDisplay.mDisplayId == Display.DEFAULT_DISPLAY;
+                mActivityContainer.mActivityDisplay.mDisplayId == DEFAULT_DISPLAY;
     }
 
     void moveToFront(String reason) {
@@ -988,7 +990,7 @@
             if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Sleep needs to pause " + mResumedActivity);
             if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
                     "Sleep => pause with userLeaving=false");
-            startPausingLocked(false, true, false, false);
+            startPausingLocked(false, true, null, false);
             return true;
         }
         if (mPausingActivity != null) {
@@ -1041,22 +1043,32 @@
 
         int w = mService.mThumbnailWidth;
         int h = mService.mThumbnailHeight;
-        float scale = 1f;
-        if (w > 0) {
-            if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
 
-            // When this flag is set, we currently take the fullscreen screenshot of the activity
-            // but scaled to half the size.  This gives us a "good-enough" fullscreen thumbnail to
-            // use within SystemUI while keeping memory usage low.
-            if (ActivityManagerService.TAKE_FULLSCREEN_SCREENSHOTS) {
-                w = h = -1;
-                scale = mService.mFullscreenThumbnailScale;
-            }
-            return mWindowManager.screenshotApplications(who.appToken, Display.DEFAULT_DISPLAY,
-                    w, h, scale);
+        if (w <= 0) {
+            Slog.e(TAG, "\tInvalid thumbnail dimensions: " + w + "x" + h);
+            return null;
         }
-        Slog.e(TAG, "Invalid thumbnail dimensions: " + w + "x" + h);
-        return null;
+
+        if (mStackId == DOCKED_STACK_ID && mStackSupervisor.mIsDockMinimized) {
+            // When the docked stack is minimized its app windows are cropped significantly so any
+            // screenshot taken will not display the apps contain. So, we avoid taking a screenshot
+            // in that case.
+            if (DEBUG_SCREENSHOTS) Slog.e(TAG, "\tIn minimized docked stack");
+            return null;
+        }
+
+        float scale = 1f;
+        if (DEBUG_SCREENSHOTS) Slog.d(TAG_SCREENSHOTS, "\tTaking screenshot");
+
+        // When this flag is set, we currently take the fullscreen screenshot of the activity but
+        // scaled to half the size. This gives us a "good-enough" fullscreen thumbnail to use within
+        // SystemUI while keeping memory usage low.
+        if (TAKE_FULLSCREEN_SCREENSHOTS) {
+            w = h = -1;
+            scale = mService.mFullscreenThumbnailScale;
+        }
+
+        return mWindowManager.screenshotApplications(who.appToken, DEFAULT_DISPLAY, w, h, scale);
     }
 
     /**
@@ -1066,15 +1078,16 @@
      * @param userLeaving True if this should result in an onUserLeaving to the current activity.
      * @param uiSleeping True if this is happening with the user interface going to sleep (the
      * screen turning off).
-     * @param resuming True if this is being called as part of resuming the top activity, so
-     * we shouldn't try to instigate a resume here.
+     * @param resuming The activity we are currently trying to resume or null if this is not being
+     *                 called as part of resuming the top activity, so we shouldn't try to instigate
+     *                 a resume here if not null.
      * @param dontWait True if the caller does not want to wait for the pause to complete.  If
      * set to true, we will immediately complete the pause here before returning.
      * @return Returns true if an activity now is in the PAUSING state, and we are waiting for
      * it to tell us when it is done.
      */
-    final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
-            boolean dontWait) {
+    final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
+            ActivityRecord resuming, boolean dontWait) {
         if (mPausingActivity != null) {
             Slog.wtf(TAG, "Going to pause when pause is already pending for " + mPausingActivity
                     + " state=" + mPausingActivity.state);
@@ -1082,12 +1095,12 @@
                 // Avoid recursion among check for sleep and complete pause during sleeping.
                 // Because activity will be paused immediately after resume, just let pause
                 // be completed by the order of activity paused from clients.
-                completePauseLocked(false);
+                completePauseLocked(false, resuming);
             }
         }
         ActivityRecord prev = mResumedActivity;
         if (prev == null) {
-            if (!resuming) {
+            if (resuming == null) {
                 Slog.wtf(TAG, "Trying to pause when nothing is resumed");
                 mStackSupervisor.resumeFocusedStackTopActivityLocked();
             }
@@ -1160,7 +1173,7 @@
             if (dontWait) {
                 // If the caller said they don't want to wait for the pause, then complete
                 // the pause now.
-                completePauseLocked(false);
+                completePauseLocked(false, resuming);
                 return false;
 
             } else {
@@ -1179,7 +1192,7 @@
             // This activity failed to schedule the
             // pause, so just treat it as being paused now.
             if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Activity not running, resuming next.");
-            if (!resuming) {
+            if (resuming == null) {
                 mStackSupervisor.resumeFocusedStackTopActivityLocked();
             }
             return false;
@@ -1196,7 +1209,7 @@
             if (mPausingActivity == r) {
                 if (DEBUG_STATES) Slog.v(TAG_STATES, "Moving to PAUSED: " + r
                         + (timeout ? " (due to timeout)" : " (pause complete)"));
-                completePauseLocked(true);
+                completePauseLocked(true, null);
                 return;
             } else {
                 EventLog.writeEvent(EventLogTags.AM_FAILED_TO_PAUSE,
@@ -1249,7 +1262,7 @@
             r.stopped = true;
             r.state = ActivityState.STOPPED;
 
-            mWindowManager.notifyAppStopped(r.appToken, true);
+            mWindowManager.notifyAppStopped(r.appToken);
 
             if (getVisibleBehindActivity() == r) {
                 mStackSupervisor.requestVisibleBehindLocked(r, false);
@@ -1267,7 +1280,7 @@
         }
     }
 
-    private void completePauseLocked(boolean resumeNext) {
+    private void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {
         ActivityRecord prev = mPausingActivity;
         if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Complete pause: " + prev);
 
@@ -1307,7 +1320,9 @@
             // It is possible the activity was freezing the screen before it was paused.
             // In that case go ahead and remove the freeze this activity has on the screen
             // since it is no longer visible.
-            prev.stopFreezingScreenLocked(true /*force*/);
+            if (prev != null) {
+                prev.stopFreezingScreenLocked(true /*force*/);
+            }
             mPausingActivity = null;
         }
 
@@ -1350,13 +1365,16 @@
             prev.cpuTimeAtResume = 0; // reset it
         }
 
-        // Notify when the task stack has changed, but only if visibilities changed (not just focus)
-        if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause) {
+        // Notify when the task stack has changed, but only if visibilities changed (not just
+        // focus). Also if there is an active pinned stack - we always want to notify it about
+        // task stack changes, because its positioning may depend on it.
+        if (mStackSupervisor.mAppVisibilitiesChangedSinceLastPause
+                || mService.mStackSupervisor.getStack(PINNED_STACK_ID) != null) {
             mService.notifyTaskStackChangedLocked();
             mStackSupervisor.mAppVisibilitiesChangedSinceLastPause = false;
         }
 
-        mStackSupervisor.ensureActivitiesVisibleLocked(null, 0, !PRESERVE_WINDOWS);
+        mStackSupervisor.ensureActivitiesVisibleLocked(resuming, 0, !PRESERVE_WINDOWS);
     }
 
     private void addToStopping(ActivityRecord r, boolean immediate) {
@@ -1587,11 +1605,6 @@
             return STACK_INVISIBLE;
         }
 
-        final boolean isLockscreenShown = mService.mLockScreenShown == LOCK_SCREEN_SHOWN;
-        if (isLockscreenShown && !StackId.isAllowedOverLockscreen(mStackId)) {
-            return STACK_INVISIBLE;
-        }
-
         final ActivityStack focusedStack = mStackSupervisor.getFocusedStack();
         final int focusedStackId = focusedStack.mStackId;
 
@@ -2048,6 +2061,14 @@
         // We don't want to clear starting window for activities that aren't behind fullscreen
         // activities as we need to display their starting window until they are done initializing.
         boolean behindFullscreenActivity = false;
+
+        if (getStackVisibilityLocked(null) == STACK_INVISIBLE) {
+            // The stack is not visible, so no activity in it should be displaying a starting
+            // window. Mark all activities below top and behind fullscreen.
+            aboveTop = false;
+            behindFullscreenActivity = true;
+        }
+
         for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
             final ArrayList<ActivityRecord> activities = mTaskHistory.get(taskNdx).mActivities;
             for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) {
@@ -2245,11 +2266,11 @@
 
         // We need to start pausing the current activity so the top one can be resumed...
         final boolean dontWaitForPause = (next.info.flags & FLAG_RESUME_WHILE_PAUSING) != 0;
-        boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, true, dontWaitForPause);
+        boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, dontWaitForPause);
         if (mResumedActivity != null) {
             if (DEBUG_STATES) Slog.d(TAG_STATES,
                     "resumeTopActivityLocked: Pausing " + mResumedActivity);
-            pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
+            pausing |= startPausingLocked(userLeaving, false, next, dontWaitForPause);
         }
         if (pausing) {
             if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,
@@ -2473,13 +2494,26 @@
                     }
                 }
 
+                boolean allowSavedSurface = true;
                 if (next.newIntents != null) {
-                    next.app.thread.scheduleNewIntent(next.newIntents, next.appToken);
+                    // Restrict saved surface to launcher start, or there is no intent at all
+                    // (eg. task being brought to front). If the intent is something else,
+                    // likely the app is going to show some specific page or view, instead of
+                    // what's left last time.
+                    for (int i = next.newIntents.size() - 1; i >= 0; i--) {
+                        final Intent intent = next.newIntents.get(i);
+                        if (intent != null && !ActivityRecord.isMainIntent(intent)) {
+                            allowSavedSurface = false;
+                            break;
+                        }
+                    }
+                    next.app.thread.scheduleNewIntent(
+                            next.newIntents, next.appToken, false /* andPause */);
                 }
 
                 // Well the app will no longer be stopped.
                 // Clear app token stopped state in window manager if needed.
-                mWindowManager.notifyAppStopped(next.appToken, false);
+                mWindowManager.notifyAppResumed(next.appToken, next.stopped, allowSavedSurface);
 
                 EventLog.writeEvent(EventLogTags.AM_RESUME_ACTIVITY, next.userId,
                         System.identityHashCode(next), next.task.taskId, next.shortComponentName);
@@ -2587,11 +2621,14 @@
     }
 
     private void insertTaskAtTop(TaskRecord task, ActivityRecord newActivity) {
+        boolean isLastTaskOverHome = false;
         // If the moving task is over home stack, transfer its return type to next task
         if (task.isOverHomeStack()) {
             final TaskRecord nextTask = getNextTask(task);
             if (nextTask != null) {
                 nextTask.setTaskToReturnTo(task.getTaskToReturnTo());
+            } else {
+                isLastTaskOverHome = true;
             }
         }
 
@@ -2601,7 +2638,10 @@
             ActivityStack lastStack = mStackSupervisor.getLastStack();
             final boolean fromHome = lastStack.isHomeStack();
             if (!isHomeStack() && (fromHome || topTask() != task)) {
-                int returnToType = APPLICATION_ACTIVITY_TYPE;
+                // If it's a last task over home - we default to keep its return to type not to
+                // make underlying task focused when this one will be finished.
+                int returnToType = isLastTaskOverHome
+                        ? task.getTaskToReturnTo() : APPLICATION_ACTIVITY_TYPE;
                 if (fromHome && StackId.allowTopTaskToReturnHome(mStackId)) {
                     returnToType = lastStack.topTask() == null
                             ? HOME_ACTIVITY_TYPE : lastStack.topTask().taskType;
@@ -3480,7 +3520,7 @@
                 if (DEBUG_PAUSE) Slog.v(TAG_PAUSE, "Finish needs to pause: " + r);
                 if (DEBUG_USER_LEAVING) Slog.v(TAG_USER_LEAVING,
                         "finish() => pause with userLeaving=false");
-                startPausingLocked(false, false, false, false);
+                startPausingLocked(false, false, null, false);
             }
 
             if (endTask) {
@@ -3770,6 +3810,10 @@
         if (getVisibleBehindActivity() == r) {
             mStackSupervisor.requestVisibleBehindLocked(r, false);
         }
+
+        // Clean-up activities are no longer relaunching (e.g. app process died). Notify window
+        // manager so it can update its bookkeeping.
+        mWindowManager.notifyAppRelaunchesCleared(r.appToken);
     }
 
     private void removeTimeoutsForActivityLocked(ActivityRecord r) {
@@ -4483,15 +4527,6 @@
             return true;
         }
 
-        // TODO: We could probably make the condition below just check that the activity state is
-        // stopped, but also checking the sleep state for now to reduce change impact late in
-        // development cycle.
-        if (mService.isSleepingOrShuttingDownLocked() && r.state == ActivityState.STOPPED) {
-            if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
-                    "Skipping config check (stopped while sleeping): " + r);
-            return true;
-        }
-
         if (DEBUG_SWITCH || DEBUG_CONFIGURATION) Slog.v(TAG_CONFIGURATION,
                 "Ensuring correct configuration: " + r);
 
@@ -5205,7 +5240,7 @@
                 (r.info.flags & FLAG_SHOW_FOR_ALL_USERS) != 0, r.userId, r.info.configChanges,
                 task.voiceSession != null, r.mLaunchTaskBehind, bounds, task.mOverrideConfig,
                 task.mResizeMode, r.isAlwaysFocusable(), task.isHomeTask(),
-                r.appInfo.targetSdkVersion);
+                r.appInfo.targetSdkVersion, r.mRotationAnimationHint);
         r.taskConfigOverride = task.mOverrideConfig;
     }
 
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index 4e03ec8..c6ab918 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -802,20 +802,25 @@
         }
     }
 
+    static int nextTaskIdForUser(int taskId, int userId) {
+        int nextTaskId = taskId + 1;
+        if (nextTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
+            // Wrap around as there will be smaller task ids that are available now.
+            nextTaskId -= MAX_TASK_IDS_PER_USER;
+        }
+        return nextTaskId;
+    }
+
     int getNextTaskIdForUserLocked(int userId) {
         final int currentTaskId = mCurTaskIdForUser.get(userId, userId * MAX_TASK_IDS_PER_USER);
         // for a userId u, a taskId can only be in the range
         // [u*MAX_TASK_IDS_PER_USER, (u+1)*MAX_TASK_IDS_PER_USER-1], so if MAX_TASK_IDS_PER_USER
         // was 10, user 0 could only have taskIds 0 to 9, user 1: 10 to 19, user 2: 20 to 29, so on.
-        int candidateTaskId = currentTaskId;
+        int candidateTaskId = nextTaskIdForUser(currentTaskId, userId);
         while (mRecentTasks.taskIdTakenForUserLocked(candidateTaskId, userId)
                 || anyTaskForIdLocked(candidateTaskId, !RESTORE_FROM_RECENTS,
                         INVALID_STACK_ID) != null) {
-            candidateTaskId++;
-            if (candidateTaskId == (userId + 1) * MAX_TASK_IDS_PER_USER) {
-                // Wrap around as there will be smaller task ids that are available now.
-                candidateTaskId -= MAX_TASK_IDS_PER_USER;
-            }
+            candidateTaskId = nextTaskIdForUser(candidateTaskId, userId);
             if (candidateTaskId == currentTaskId) {
                 // Something wrong!
                 // All MAX_TASK_IDS_PER_USER task ids are taken up by running tasks for this user
@@ -892,6 +897,8 @@
                 }
             }
         }
+        // Send launch end powerhint when idle
+        mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
         return true;
     }
 
@@ -937,9 +944,12 @@
     /**
      * Pause all activities in either all of the stacks or just the back stacks.
      * @param userLeaving Passed to pauseActivity() to indicate whether to call onUserLeaving().
+     * @param resuming The resuming activity.
+     * @param dontWait The resuming activity isn't going to wait for all activities to be paused
+     *                 before resuming.
      * @return true if any activity was paused as a result of this call.
      */
-    boolean pauseBackStacks(boolean userLeaving, boolean resuming, boolean dontWait) {
+    boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming, boolean dontWait) {
         boolean someActivityPaused = false;
         for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
             ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
@@ -978,7 +988,7 @@
     }
 
     void pauseChildStacks(ActivityRecord parent, boolean userLeaving, boolean uiSleeping,
-            boolean resuming, boolean dontWait) {
+            ActivityRecord resuming, boolean dontWait) {
         // TODO: Put all stacks in supervisor and iterate through them instead.
         for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
             ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
@@ -1208,7 +1218,10 @@
             Configuration config = mWindowManager.updateOrientationFromAppTokens(
                     mService.mConfiguration,
                     r.mayFreezeScreenLocked(app) ? r.appToken : null);
-            mService.updateConfigurationLocked(config, r, false);
+            // Deferring resume here because we're going to launch new activity shortly.
+            // We don't want to perform a redundant launch of the same record while ensuring
+            // configurations and trying to resume top activity of focused stack.
+            mService.updateConfigurationLocked(config, r, false, true /* deferResume */);
         }
 
         r.app = app;
@@ -2023,7 +2036,7 @@
             boolean preserveWindows, boolean allowResizeInDockedMode, boolean deferResume) {
         if (stackId == DOCKED_STACK_ID) {
             resizeDockedStackLocked(bounds, tempTaskBounds, tempTaskInsetBounds, null, null,
-                    preserveWindows);
+                    preserveWindows, deferResume);
             return;
         }
         final ActivityStack stack = getStack(stackId);
@@ -2199,8 +2212,16 @@
     }
 
     void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
-            Rect tempDockedTaskInsetBounds,
-            Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds, boolean preserveWindows) {
+            Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
+            boolean preserveWindows) {
+        resizeDockedStackLocked(dockedBounds, tempDockedTaskBounds, tempDockedTaskInsetBounds,
+                tempOtherTaskBounds, tempOtherTaskInsetBounds, preserveWindows,
+                false /* deferResume */);
+    }
+
+    void resizeDockedStackLocked(Rect dockedBounds, Rect tempDockedTaskBounds,
+            Rect tempDockedTaskInsetBounds, Rect tempOtherTaskBounds, Rect tempOtherTaskInsetBounds,
+            boolean preserveWindows, boolean deferResume) {
 
         if (!mAllowDockedStackResize) {
             // Docked stack resize currently disabled.
@@ -2243,11 +2264,13 @@
                     if (StackId.isResizeableByDockedStack(i) && getStack(i) != null) {
                         resizeStackLocked(i, tempRect, tempOtherTaskBounds,
                                 tempOtherTaskInsetBounds, preserveWindows,
-                                true /* allowResizeInDockedMode */, !DEFER_RESUME);
+                                true /* allowResizeInDockedMode */, deferResume);
                     }
                 }
             }
-            stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
+            if (!deferResume) {
+                stack.ensureVisibleActivitiesConfigurationLocked(r, preserveWindows);
+            }
         } finally {
             mAllowDockedStackResize = true;
             mWindowManager.continueSurfaceLayout();
@@ -2821,6 +2844,9 @@
             }
         }
 
+        // Send launch end powerhint before going sleep
+        mService.mActivityStarter.sendPowerHintForLaunchEndIfNeeded();
+
         for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
             final ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
             for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
@@ -3113,11 +3139,14 @@
         final boolean nowVisible = allResumedActivitiesVisible();
         for (int activityNdx = mStoppingActivities.size() - 1; activityNdx >= 0; --activityNdx) {
             ActivityRecord s = mStoppingActivities.get(activityNdx);
-            final boolean waitingVisible = mWaitingVisibleActivities.contains(s);
+            // TODO: Remove mWaitingVisibleActivities list and just remove activity from
+            // mStoppingActivities when something else comes up.
+            boolean waitingVisible = mWaitingVisibleActivities.contains(s);
             if (DEBUG_STATES) Slog.v(TAG, "Stopping " + s + ": nowVisible=" + nowVisible
                     + " waitingVisible=" + waitingVisible + " finishing=" + s.finishing);
             if (waitingVisible && nowVisible) {
                 mWaitingVisibleActivities.remove(s);
+                waitingVisible = false;
                 if (s.finishing) {
                     // If this activity is finishing, it is sitting on top of
                     // everyone else but we now know it is no longer needed...
@@ -3738,6 +3767,12 @@
 
     void activityRelaunchedLocked(IBinder token) {
         mWindowManager.notifyAppRelaunchingFinished(token);
+        if (mService.isSleepingOrShuttingDownLocked()) {
+            final ActivityRecord r = ActivityRecord.isInStackLocked(token);
+            if (r != null) {
+                r.setSleeping(true, true);
+            }
+        }
     }
 
     void activityRelaunchingLocked(ActivityRecord r) {
@@ -4233,7 +4268,7 @@
                 mContainerState = CONTAINER_STATE_NO_SURFACE;
                 ((VirtualActivityDisplay) mActivityDisplay).setSurface(null);
                 if (mStack.mPausingActivity == null && mStack.mResumedActivity != null) {
-                    mStack.startPausingLocked(false, true, false, false);
+                    mStack.startPausingLocked(false, true, null, false);
                 }
             }
 
@@ -4489,6 +4524,7 @@
         // Work Challenge is present) let startActivityInPackage handle the intercepting.
         if (!mService.mUserController.shouldConfirmCredentials(task.userId)
                 && task.getRootActivity() != null) {
+            mService.mActivityStarter.sendPowerHintForLaunchStartIfNeeded(true /* forceSend */);
             mActivityMetricsLogger.notifyActivityLaunching();
             mService.moveTaskToFrontLocked(task.taskId, 0, bOptions);
             mActivityMetricsLogger.notifyActivityLaunched(ActivityManager.START_TASK_TO_FRONT,
diff --git a/services/core/java/com/android/server/am/ActivityStartInterceptor.java b/services/core/java/com/android/server/am/ActivityStartInterceptor.java
index f26e47e..d719c58 100644
--- a/services/core/java/com/android/server/am/ActivityStartInterceptor.java
+++ b/services/core/java/com/android/server/am/ActivityStartInterceptor.java
@@ -29,6 +29,7 @@
 import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
 import static android.content.pm.ApplicationInfo.FLAG_SUSPENDED;
 
+import android.app.ActivityManager;
 import android.app.ActivityOptions;
 import android.app.KeyguardManager;
 import android.app.admin.DevicePolicyManagerInternal;
@@ -210,6 +211,11 @@
         if (!mService.mUserController.shouldConfirmCredentials(userId)) {
             return null;
         }
+        // Allow direct boot aware activity to be displayed before the user is unlocked.
+        if (aInfo.directBootAware && mService.mUserController.isUserRunningLocked(userId,
+                ActivityManager.FLAG_AND_LOCKED)) {
+            return null;
+        }
         final IIntentSender target = mService.getIntentSenderLocked(
                 INTENT_SENDER_ACTIVITY, callingPackage,
                 Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
diff --git a/services/core/java/com/android/server/am/ActivityStarter.java b/services/core/java/com/android/server/am/ActivityStarter.java
index 2b08ba9..85ab4926 100644
--- a/services/core/java/com/android/server/am/ActivityStarter.java
+++ b/services/core/java/com/android/server/am/ActivityStarter.java
@@ -102,6 +102,8 @@
 import android.os.Build;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.os.PowerManagerInternal;
+import android.os.Process;
 import android.os.RemoteException;
 import android.os.SystemClock;
 import android.os.UserHandle;
@@ -131,6 +133,9 @@
     private static final String TAG_CONFIGURATION = TAG + POSTFIX_CONFIGURATION;
     private static final String TAG_USER_LEAVING = TAG + POSTFIX_USER_LEAVING;
 
+    // TODO b/30204367 remove when the platform fully supports ephemeral applications
+    private static final boolean USE_DEFAULT_EPHEMERAL_LAUNCHER = false;
+
     private final ActivityManagerService mService;
     private final ActivityStackSupervisor mSupervisor;
     private ActivityStartInterceptor mInterceptor;
@@ -173,6 +178,7 @@
     private boolean mNoAnimation;
     private boolean mKeepCurTransition;
     private boolean mAvoidMoveToFront;
+    private boolean mPowerHintSent;
 
     private IVoiceInteractionSession mVoiceSession;
     private IVoiceInteractor mVoiceInteractor;
@@ -454,39 +460,13 @@
         // starts either the intent we resolved here [on install error] or the ephemeral
         // app [on install success].
         if (rInfo != null && rInfo.ephemeralResolveInfo != null) {
-            // Create a pending intent to start the intent resolved here.
-            final IIntentSender failureTarget = mService.getIntentSenderLocked(
-                    ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
-                    Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ intent },
-                    new String[]{ resolvedType },
-                    PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
-                            | PendingIntent.FLAG_IMMUTABLE, null);
-
-            // Create a pending intent to start the ephemeral application; force it to be
-            // directed to the ephemeral package.
-            ephemeralIntent.setPackage(rInfo.ephemeralResolveInfo.getPackageName());
-            final IIntentSender ephemeralTarget = mService.getIntentSenderLocked(
-                    ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
-                    Binder.getCallingUid(), userId, null, null, 0, new Intent[]{ ephemeralIntent },
-                    new String[]{ resolvedType },
-                    PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
-                            | PendingIntent.FLAG_IMMUTABLE, null);
-
-            int flags = intent.getFlags();
-            intent = new Intent();
-            intent.setFlags(flags
-                    | Intent.FLAG_ACTIVITY_NEW_TASK
-                    | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
-            intent.putExtra(Intent.EXTRA_PACKAGE_NAME,
-                    rInfo.ephemeralResolveInfo.getPackageName());
-            intent.putExtra(Intent.EXTRA_EPHEMERAL_FAILURE, new IntentSender(failureTarget));
-            intent.putExtra(Intent.EXTRA_EPHEMERAL_SUCCESS, new IntentSender(ephemeralTarget));
-
+            intent = buildEphemeralInstallerIntent(intent, ephemeralIntent,
+                    rInfo.ephemeralResolveInfo.getPackageName(), callingPackage, resolvedType,
+                    userId);
             resolvedType = null;
             callingUid = realCallingUid;
             callingPid = realCallingPid;
 
-            rInfo = rInfo.ephemeralInstaller;
             aInfo = mSupervisor.resolveActivity(intent, rInfo, startFlags, null /*profilerInfo*/);
         }
 
@@ -541,6 +521,58 @@
         return err;
     }
 
+    /**
+     * Builds and returns an intent to launch the ephemeral installer.
+     */
+    private Intent buildEphemeralInstallerIntent(Intent launchIntent, Intent origIntent,
+            String ephemeralPackage, String callingPackage, String resolvedType, int userId) {
+        final Intent nonEphemeralIntent = new Intent(origIntent);
+        nonEphemeralIntent.setFlags(nonEphemeralIntent.getFlags() | Intent.FLAG_IGNORE_EPHEMERAL);
+        // Intent that is launched if the ephemeral package couldn't be installed
+        // for any reason.
+        final IIntentSender failureIntentTarget = mService.getIntentSenderLocked(
+                ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
+                Binder.getCallingUid(), userId, null /*token*/, null /*resultWho*/, 1,
+                new Intent[]{ nonEphemeralIntent }, new String[]{ resolvedType },
+                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
+                | PendingIntent.FLAG_IMMUTABLE, null /*bOptions*/);
+
+        final Intent ephemeralIntent;
+        if (USE_DEFAULT_EPHEMERAL_LAUNCHER) {
+            // Force the intent to be directed to the ephemeral package
+            ephemeralIntent = new Intent(origIntent);
+            ephemeralIntent.setPackage(ephemeralPackage);
+        } else {
+            // Success intent goes back to the installer
+            ephemeralIntent = new Intent(launchIntent);
+        }
+
+        // Intent that is eventually launched if the ephemeral package was
+        // installed successfully. This will actually be launched by a platform
+        // broadcast receiver.
+        final IIntentSender successIntentTarget = mService.getIntentSenderLocked(
+                ActivityManager.INTENT_SENDER_ACTIVITY, callingPackage,
+                Binder.getCallingUid(), userId, null /*token*/, null /*resultWho*/, 0,
+                new Intent[]{ ephemeralIntent }, new String[]{ resolvedType },
+                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT
+                | PendingIntent.FLAG_IMMUTABLE, null /*bOptions*/);
+
+        // Finally build the actual intent to launch the ephemeral installer
+        int flags = launchIntent.getFlags();
+        final Intent intent = new Intent();
+        intent.setFlags(flags
+                | Intent.FLAG_ACTIVITY_NEW_TASK
+                | Intent.FLAG_ACTIVITY_CLEAR_TASK
+                | Intent.FLAG_ACTIVITY_NO_HISTORY
+                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
+        intent.putExtra(Intent.EXTRA_PACKAGE_NAME, ephemeralPackage);
+        intent.putExtra(Intent.EXTRA_EPHEMERAL_FAILURE, new IntentSender(failureIntentTarget));
+        intent.putExtra(Intent.EXTRA_EPHEMERAL_SUCCESS, new IntentSender(successIntentTarget));
+        // TODO: Remove when the platform has fully implemented ephemeral apps
+        intent.setData(origIntent.getData().buildUpon().clearQuery().build());
+        return intent;
+    }
+
     void postStartActivityUncheckedProcessing(
             ActivityRecord r, int result, int prevFocusedStackId, ActivityRecord sourceRecord,
             ActivityStack targetStack) {
@@ -570,6 +602,9 @@
         // If we launched the activity from a no display activity that was launched from the home
         // screen, we also need to start recents to un-minimize the docked stack, since the
         // noDisplay activity will be finished shortly after.
+        // Note that some apps have trampoline activities without noDisplay being set. In that case,
+        // we have another heuristic in DockedStackDividerController.notifyAppTransitionStarting
+        // that tries to detect that case.
         // TODO: We should prevent noDisplay activities from affecting task/stack ordering and
         // visibility instead of using this flag.
         final boolean noDisplayActivityOverHome = sourceRecord != null
@@ -898,13 +933,15 @@
             throw new IllegalArgumentException("intents are length different than resolvedTypes");
         }
 
+        final int realCallingPid = Binder.getCallingPid();
+        final int realCallingUid = Binder.getCallingUid();
 
         int callingPid;
         if (callingUid >= 0) {
             callingPid = -1;
         } else if (caller == null) {
-            callingPid = Binder.getCallingPid();
-            callingUid = Binder.getCallingUid();
+            callingPid = realCallingPid;
+            callingUid = realCallingUid;
         } else {
             callingPid = callingUid = -1;
         }
@@ -945,7 +982,8 @@
                             i == intents.length - 1 ? bOptions : null);
                     int res = startActivityLocked(caller, intent, null /*ephemeralIntent*/,
                             resolvedTypes[i], aInfo, null /*rInfo*/, null, null, resultTo, null, -1,
-                            callingPid, callingUid, callingPackage, callingPid, callingUid, 0,
+                            callingPid, callingUid, callingPackage,
+                            realCallingPid, realCallingUid, 0,
                             options, false, componentSpecified, outActivity, null, null);
                     if (res < 0) {
                         return res;
@@ -961,6 +999,28 @@
         return START_SUCCESS;
     }
 
+    void sendPowerHintForLaunchStartIfNeeded(boolean forceSend) {
+        // Trigger launch power hint if activity being launched is not in the current task
+        final ActivityStack focusStack = mSupervisor.getFocusedStack();
+        final ActivityRecord curTop = (focusStack == null)
+            ? null : focusStack.topRunningNonDelayedActivityLocked(mNotTop);
+        if ((forceSend || (!mPowerHintSent && curTop != null &&
+                curTop.task != null && mStartActivity != null &&
+                curTop.task != mStartActivity.task )) &&
+                mService.mLocalPowerManager != null) {
+            mService.mLocalPowerManager.powerHint(PowerManagerInternal.POWER_HINT_LAUNCH, 1);
+            mPowerHintSent = true;
+        }
+    }
+
+    void sendPowerHintForLaunchEndIfNeeded() {
+        // Trigger launch power hint if activity is launched
+        if (mPowerHintSent && mService.mLocalPowerManager != null) {
+            mService.mLocalPowerManager.powerHint(PowerManagerInternal.POWER_HINT_LAUNCH, 0);
+            mPowerHintSent = false;
+        }
+    }
+
     private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
             IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
             int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask) {
@@ -1022,6 +1082,8 @@
                 }
             }
 
+            sendPowerHintForLaunchStartIfNeeded(false /* forceSend */);
+
             mReusedActivity = setTargetStackAndMoveToFrontIfNeeded(mReusedActivity);
 
             if ((mStartFlags & START_FLAG_ONLY_IF_NEEDED) != 0) {
@@ -1100,7 +1162,11 @@
                 return START_RETURN_LOCK_TASK_MODE_VIOLATION;
             }
             if (!mMovedOtherTask) {
-                updateTaskReturnToType(mStartActivity.task, mLaunchFlags, topStack);
+                // If stack id is specified in activity options, usually it means that activity is
+                // launched not from currently focused stack (e.g. from SysUI or from shell) - in
+                // that case we check the target stack.
+                updateTaskReturnToType(mStartActivity.task, mLaunchFlags,
+                        preferredLaunchStackId != INVALID_STACK_ID ? mTargetStack : topStack);
             }
         } else if (mSourceRecord != null) {
             if (mSupervisor.isLockTaskModeViolation(mSourceRecord.task)) {
@@ -1143,6 +1209,9 @@
         ActivityStack.logStartActivity(
                 EventLogTags.AM_CREATE_ACTIVITY, mStartActivity, mStartActivity.task);
         mTargetStack.mLastPausedActivity = null;
+
+        sendPowerHintForLaunchStartIfNeeded(false /* forceSend */);
+
         mTargetStack.startActivityLocked(mStartActivity, newTask, mKeepCurTransition, mOptions);
         if (mDoResume) {
             if (!mLaunchTaskBehind) {
diff --git a/services/core/java/com/android/server/am/AppErrorDialog.java b/services/core/java/com/android/server/am/AppErrorDialog.java
index e37feb2..646f6ce 100644
--- a/services/core/java/com/android/server/am/AppErrorDialog.java
+++ b/services/core/java/com/android/server/am/AppErrorDialog.java
@@ -16,29 +16,22 @@
 
 package com.android.server.am;
 
-import android.app.ActivityManagerInternal;
-import android.app.ActivityOptions;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.pm.IPackageDataObserver;
-import android.content.pm.PackageManager;
 import android.content.res.Resources;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.provider.Settings;
 import android.text.BidiFormatter;
-import android.util.Slog;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.WindowManager;
 import android.widget.FrameLayout;
 import android.widget.TextView;
 
-import java.util.List;
-
 import static com.android.server.am.ActivityManagerService.IS_USER_BUILD;
 
 final class AppErrorDialog extends BaseErrorDialog implements View.OnClickListener {
@@ -47,7 +40,7 @@
     private final AppErrorResult mResult;
     private final ProcessRecord mProc;
     private final boolean mRepeating;
-    private final boolean mForeground;
+    private final boolean mIsRestartable;
 
     private CharSequence mName;
 
@@ -74,7 +67,7 @@
         mProc = data.proc;
         mResult = data.result;
         mRepeating = data.repeating;
-        mForeground = data.task != null;
+        mIsRestartable = data.task != null || data.isRestartableForService;
         BidiFormatter bidi = BidiFormatter.getInstance();
 
         if ((mProc.pkgList.size() == 1) &&
@@ -118,7 +111,7 @@
         LayoutInflater.from(context).inflate(
                 com.android.internal.R.layout.app_error_dialog, frame, true);
 
-        boolean hasRestart = !mRepeating && mForeground;
+        boolean hasRestart = !mRepeating && mIsRestartable;
         final boolean hasReceiver = mProc.errorReportReceiver != null;
 
         final TextView restart = (TextView) findViewById(com.android.internal.R.id.aerr_restart);
@@ -214,5 +207,6 @@
         TaskRecord task;
         boolean repeating;
         ProcessRecord proc;
+        boolean isRestartableForService;
     }
 }
diff --git a/services/core/java/com/android/server/am/AppErrors.java b/services/core/java/com/android/server/am/AppErrors.java
index 02aeaa2..c19a571 100644
--- a/services/core/java/com/android/server/am/AppErrors.java
+++ b/services/core/java/com/android/server/am/AppErrors.java
@@ -22,7 +22,6 @@
 import com.android.internal.os.ProcessCpuTracker;
 import com.android.server.Watchdog;
 
-import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityOptions;
 import android.app.ActivityThread;
@@ -33,10 +32,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
-import android.content.pm.IPackageDataObserver;
-import android.content.pm.PackageManager;
 import android.os.Binder;
-import android.os.Bundle;
 import android.os.Message;
 import android.os.Process;
 import android.os.RemoteException;
@@ -58,7 +54,7 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.concurrent.Semaphore;
+import java.util.Set;
 
 import static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
 import static com.android.server.am.ActivityManagerDebugConfig.DEBUG_ANR;
@@ -358,7 +354,7 @@
                 return;
             }
 
-            Message msg = Message.obtain();
+            final Message msg = Message.obtain();
             msg.what = ActivityManagerService.SHOW_ERROR_UI_MSG;
 
             task = data.task;
@@ -386,8 +382,8 @@
                     } catch (IllegalArgumentException e) {
                         // Hmm, that didn't work, app might have crashed before creating a
                         // recents entry. Let's see if we have a safe-to-restart intent.
-                        if (task.intent.getCategories().contains(
-                                Intent.CATEGORY_LAUNCHER)) {
+                        final Set<String> cats = task.intent.getCategories();
+                        if (cats != null && cats.contains(Intent.CATEGORY_LAUNCHER)) {
                             mService.startActivityInPackage(task.mCallingUid,
                                     task.mCallingPackage, task.intent,
                                     null, null, null, 0, 0,
@@ -574,6 +570,8 @@
     boolean handleAppCrashLocked(ProcessRecord app, String reason,
             String shortMsg, String longMsg, String stackTrace, AppErrorDialog.Data data) {
         long now = SystemClock.uptimeMillis();
+        boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
+                Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
 
         Long crashTime;
         Long crashTimePersistent;
@@ -611,7 +609,9 @@
                 // processes run critical code.
                 mService.removeProcessLocked(app, false, false, "crash");
                 mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
-                return false;
+                if (!showBackground) {
+                    return false;
+                }
             }
             mService.mStackSupervisor.resumeFocusedStackTopActivityLocked();
         } else {
@@ -626,12 +626,21 @@
             }
         }
 
+        boolean procIsBoundForeground =
+                (app.curProcState == ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE);
         // Bump up the crash count of any services currently running in the proc.
         for (int i=app.services.size()-1; i>=0; i--) {
             // Any services running in the application need to be placed
             // back in the pending list.
             ServiceRecord sr = app.services.valueAt(i);
             sr.crashCount++;
+
+            // Allow restarting for started or bound foreground services that are crashing the
+            // first time. This includes wallpapers.
+            if ((data != null) && (sr.crashCount <= 1)
+                    && (sr.isForeground || procIsBoundForeground)) {
+                data.isRestartableForService = true;
+            }
         }
 
         // If the crashing process is what we consider to be the "home process" and it has been
@@ -695,7 +704,7 @@
             }
             final boolean crashSilenced = mAppsNotReportingCrashes != null &&
                     mAppsNotReportingCrashes.contains(proc.info.packageName);
-            if (mService.canShowErrorDialogs() && !crashSilenced) {
+            if ((mService.canShowErrorDialogs() || showBackground) && !crashSilenced) {
                 proc.crashDialog = new AppErrorDialog(mContext, mService, data);
             } else {
                 // The device is asleep, so just pretend that the user
@@ -938,7 +947,9 @@
                     null, null, 0, null, null, null, AppOpsManager.OP_NONE,
                     null, false, false, MY_PID, Process.SYSTEM_UID, 0 /* TODO: Verify */);
 
-            if (mService.canShowErrorDialogs()) {
+            boolean showBackground = Settings.Secure.getInt(mContext.getContentResolver(),
+                    Settings.Secure.ANR_SHOW_BACKGROUND, 0) != 0;
+            if (mService.canShowErrorDialogs() || showBackground) {
                 d = new AppNotRespondingDialog(mService,
                         mContext, proc, (ActivityRecord)data.get("activity"),
                         msg.arg1 != 0);
diff --git a/services/core/java/com/android/server/am/BatteryStatsService.java b/services/core/java/com/android/server/am/BatteryStatsService.java
index def6828..ff13125 100644
--- a/services/core/java/com/android/server/am/BatteryStatsService.java
+++ b/services/core/java/com/android/server/am/BatteryStatsService.java
@@ -484,6 +484,20 @@
         }
     }
 
+    public void noteLongPartialWakelockStart(String name, String historyName, int uid) {
+        enforceCallingPermission();
+        synchronized (mStats) {
+            mStats.noteLongPartialWakelockStart(name, historyName, uid);
+        }
+    }
+
+    public void noteLongPartialWakelockFinish(String name, String historyName, int uid) {
+        enforceCallingPermission();
+        synchronized (mStats) {
+            mStats.noteLongPartialWakelockFinish(name, historyName, uid);
+        }
+    }
+
     public void noteStartSensor(int uid, int sensor) {
         enforceCallingPermission();
         synchronized (mStats) {
@@ -710,7 +724,7 @@
     }
 
     @Override
-    public void noteWifiRadioPowerState(int powerState, long tsNanos) {
+    public void noteWifiRadioPowerState(int powerState, long tsNanos, int uid) {
         enforceCallingPermission();
 
         // There was a change in WiFi power state.
@@ -723,7 +737,7 @@
                 mHandler.scheduleSync("wifi-data: " + type,
                         BatteryStatsImpl.ExternalStatsSync.UPDATE_WIFI);
             }
-            mStats.noteWifiRadioPowerState(powerState, tsNanos);
+            mStats.noteWifiRadioPowerState(powerState, tsNanos, uid);
         }
     }
 
diff --git a/services/core/java/com/android/server/am/BroadcastQueue.java b/services/core/java/com/android/server/am/BroadcastQueue.java
index f78f29c..7873b7a 100644
--- a/services/core/java/com/android/server/am/BroadcastQueue.java
+++ b/services/core/java/com/android/server/am/BroadcastQueue.java
@@ -226,11 +226,13 @@
     }
 
     public final boolean replaceParallelBroadcastLocked(BroadcastRecord r) {
+        final Intent intent = r.intent;
         for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) {
-            if (r.intent.filterEquals(mParallelBroadcasts.get(i).intent)) {
+            final Intent curIntent = mParallelBroadcasts.get(i).intent;
+            if (intent.filterEquals(curIntent)) {
                 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
                         "***** DROPPING PARALLEL ["
-                + mQueueName + "]: " + r.intent);
+                + mQueueName + "]: " + intent);
                 mParallelBroadcasts.set(i, r);
                 return true;
             }
@@ -239,11 +241,12 @@
     }
 
     public final boolean replaceOrderedBroadcastLocked(BroadcastRecord r) {
+        final Intent intent = r.intent;
         for (int i = mOrderedBroadcasts.size() - 1; i > 0; i--) {
-            if (r.intent.filterEquals(mOrderedBroadcasts.get(i).intent)) {
+            if (intent.filterEquals(mOrderedBroadcasts.get(i).intent)) {
                 if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST,
                         "***** DROPPING ORDERED ["
-                        + mQueueName + "]: " + r.intent);
+                        + mQueueName + "]: " + intent);
                 mOrderedBroadcasts.set(i, r);
                 return true;
             }
diff --git a/services/core/java/com/android/server/am/BroadcastRecord.java b/services/core/java/com/android/server/am/BroadcastRecord.java
index 3437ae6..1e7911a 100644
--- a/services/core/java/com/android/server/am/BroadcastRecord.java
+++ b/services/core/java/com/android/server/am/BroadcastRecord.java
@@ -219,6 +219,9 @@
             int _resultCode, String _resultData, Bundle _resultExtras, boolean _serialized,
             boolean _sticky, boolean _initialSticky,
             int _userId) {
+        if (_intent == null) {
+            throw new NullPointerException("Can't construct with a null intent");
+        }
         queue = _queue;
         intent = _intent;
         targetComp = _intent.getComponent();
diff --git a/services/core/java/com/android/server/am/CoreSettingsObserver.java b/services/core/java/com/android/server/am/CoreSettingsObserver.java
index 9dd07a9..73a17c6 100644
--- a/services/core/java/com/android/server/am/CoreSettingsObserver.java
+++ b/services/core/java/com/android/server/am/CoreSettingsObserver.java
@@ -42,6 +42,7 @@
             String, Class<?>>();
     static {
         sSecureSettingToTypeMap.put(Settings.Secure.LONG_PRESS_TIMEOUT, int.class);
+        sSecureSettingToTypeMap.put(Settings.Secure.MULTI_PRESS_TIMEOUT, int.class);
         // add other secure settings here...
 
         sSystemSettingToTypeMap.put(Settings.System.TIME_12_24, String.class);
diff --git a/services/core/java/com/android/server/am/PendingIntentRecord.java b/services/core/java/com/android/server/am/PendingIntentRecord.java
index 2467a90..9c08453 100644
--- a/services/core/java/com/android/server/am/PendingIntentRecord.java
+++ b/services/core/java/com/android/server/am/PendingIntentRecord.java
@@ -38,6 +38,7 @@
 
 import java.io.PrintWriter;
 import java.lang.ref.WeakReference;
+import java.util.Objects;
 
 final class PendingIntentRecord extends IIntentSender.Stub {
     private static final String TAG = TAG_WITH_CLASS_NAME ? "PendingIntentRecord" : TAG_AM;
@@ -102,7 +103,7 @@
             if (requestResolvedType != null) {
                 hash = (ODD_PRIME_NUMBER*hash) + requestResolvedType.hashCode();
             }
-            hash = (ODD_PRIME_NUMBER*hash) + _p.hashCode();
+            hash = (ODD_PRIME_NUMBER*hash) + (_p != null ? _p.hashCode() : 0);
             hash = (ODD_PRIME_NUMBER*hash) + _t;
             hashCode = hash;
             //Slog.i(ActivityManagerService.TAG, this + " hashCode=0x"
@@ -121,20 +122,14 @@
                 if (userId != other.userId){
                     return false;
                 }
-                if (!packageName.equals(other.packageName)) {
+                if (!Objects.equals(packageName, other.packageName)) {
                     return false;
                 }
                 if (activity != other.activity) {
                     return false;
                 }
-                if (who != other.who) {
-                    if (who != null) {
-                        if (!who.equals(other.who)) {
-                            return false;
-                        }
-                    } else if (other.who != null) {
-                        return false;
-                    }
+                if (!Objects.equals(who, other.who)) {
+                    return false;
                 }
                 if (requestCode != other.requestCode) {
                     return false;
@@ -148,14 +143,8 @@
                         return false;
                     }
                 }
-                if (requestResolvedType != other.requestResolvedType) {
-                    if (requestResolvedType != null) {
-                        if (!requestResolvedType.equals(other.requestResolvedType)) {
-                            return false;
-                        }
-                    } else if (other.requestResolvedType != null) {
-                        return false;
-                    }
+                if (!Objects.equals(requestResolvedType, other.requestResolvedType)) {
+                    return false;
                 }
                 if (flags != other.flags) {
                     return false;
diff --git a/services/core/java/com/android/server/am/PreBootBroadcaster.java b/services/core/java/com/android/server/am/PreBootBroadcaster.java
index 1f3ccf5..f4f6b66 100644
--- a/services/core/java/com/android/server/am/PreBootBroadcaster.java
+++ b/services/core/java/com/android/server/am/PreBootBroadcaster.java
@@ -19,17 +19,24 @@
 import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY;
 
 import android.app.AppOpsManager;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
 import android.content.ComponentName;
+import android.content.Context;
 import android.content.IIntentReceiver;
 import android.content.Intent;
 import android.content.pm.ResolveInfo;
 import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
 import android.os.Process;
 import android.os.UserHandle;
 import android.util.Slog;
 
 import com.android.internal.R;
 import com.android.internal.util.ProgressReporter;
+import com.android.server.UiThread;
 
 import java.util.List;
 
@@ -44,6 +51,7 @@
     private final ActivityManagerService mService;
     private final int mUserId;
     private final ProgressReporter mProgress;
+    private final boolean mQuiet;
 
     private final Intent mIntent;
     private final List<ResolveInfo> mTargets;
@@ -51,10 +59,11 @@
     private int mIndex = 0;
 
     public PreBootBroadcaster(ActivityManagerService service, int userId,
-            ProgressReporter progress) {
+            ProgressReporter progress, boolean quiet) {
         mService = service;
         mUserId = userId;
         mProgress = progress;
+        mQuiet = quiet;
 
         mIntent = new Intent(Intent.ACTION_PRE_BOOT_COMPLETED);
         mIntent.addFlags(Intent.FLAG_RECEIVER_BOOT_UPGRADE | Intent.FLAG_DEBUG_TRIAGED_MISSING);
@@ -65,16 +74,22 @@
 
     public void sendNext() {
         if (mIndex >= mTargets.size()) {
+            mHandler.obtainMessage(MSG_HIDE).sendToTarget();
             onFinished();
             return;
         }
 
         if (!mService.isUserRunning(mUserId, 0)) {
             Slog.i(TAG, "User " + mUserId + " is no longer running; skipping remaining receivers");
+            mHandler.obtainMessage(MSG_HIDE).sendToTarget();
             onFinished();
             return;
         }
 
+        if (!mQuiet) {
+            mHandler.obtainMessage(MSG_SHOW, mTargets.size(), mIndex).sendToTarget();
+        }
+
         final ResolveInfo ri = mTargets.get(mIndex++);
         final ComponentName componentName = ri.activityInfo.getComponentName();
 
@@ -100,5 +115,58 @@
         sendNext();
     }
 
+    private static final int MSG_SHOW = 1;
+    private static final int MSG_HIDE = 2;
+
+    private Handler mHandler = new Handler(UiThread.get().getLooper(), null, true) {
+        @Override
+        public void handleMessage(Message msg) {
+            final Context context = mService.mContext;
+            final NotificationManager notifManager = context
+                    .getSystemService(NotificationManager.class);
+            final int max = msg.arg1;
+            final int index = msg.arg2;
+
+            switch (msg.what) {
+                case MSG_SHOW:
+                    final CharSequence title = context
+                            .getText(R.string.android_upgrading_notification_title);
+
+                    final Intent intent = new Intent();
+                    intent.setClassName("com.android.settings",
+                            "com.android.settings.HelpTrampoline");
+                    intent.putExtra(Intent.EXTRA_TEXT, "help_url_upgrading");
+
+                    final PendingIntent contentIntent;
+                    if (context.getPackageManager().resolveActivity(intent, 0) != null) {
+                        contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
+                    } else {
+                        contentIntent = null;
+                    }
+
+                    final Notification notif = new Notification.Builder(mService.mContext)
+                            .setSmallIcon(R.drawable.stat_sys_adb)
+                            .setWhen(0)
+                            .setOngoing(true)
+                            .setTicker(title)
+                            .setDefaults(0)
+                            .setPriority(Notification.PRIORITY_MAX)
+                            .setColor(context.getColor(
+                                    com.android.internal.R.color.system_notification_accent_color))
+                            .setContentTitle(title)
+                            .setContentIntent(contentIntent)
+                            .setVisibility(Notification.VISIBILITY_PUBLIC)
+                            .setProgress(max, index, false)
+                            .build();
+                    notifManager.notifyAsUser(TAG, 0, notif, UserHandle.of(mUserId));
+                    break;
+
+                case MSG_HIDE:
+                    notifManager.cancelAsUser(TAG, 0, UserHandle.of(mUserId));
+                    break;
+            }
+        }
+    };
+
     public abstract void onFinished();
 }
diff --git a/services/core/java/com/android/server/am/ProcessList.java b/services/core/java/com/android/server/am/ProcessList.java
index f073e5c..475b155 100644
--- a/services/core/java/com/android/server/am/ProcessList.java
+++ b/services/core/java/com/android/server/am/ProcessList.java
@@ -130,6 +130,9 @@
     static final int SCHED_GROUP_DEFAULT = 1;
     // Activity manager's version of Process.THREAD_GROUP_TOP_APP
     static final int SCHED_GROUP_TOP_APP = 2;
+    // Activity manager's version of Process.THREAD_GROUP_TOP_APP
+    // Disambiguate between actual top app and processes bound to the top app
+    static final int SCHED_GROUP_TOP_APP_BOUND = 3;
 
     // The minimum number of cached apps we want to be able to keep around,
     // without empty apps being able to push them out of memory.
diff --git a/services/core/java/com/android/server/am/ProcessRecord.java b/services/core/java/com/android/server/am/ProcessRecord.java
index 8911a3e..3fffefb 100644
--- a/services/core/java/com/android/server/am/ProcessRecord.java
+++ b/services/core/java/com/android/server/am/ProcessRecord.java
@@ -97,11 +97,14 @@
     int verifiedAdj;            // The last adjustment that was verified as actually being set
     int curSchedGroup;          // Currently desired scheduling class
     int setSchedGroup;          // Last set to background scheduling class
+    int vrThreadTid;            // Thread currently set for VR scheduling
     int trimMemoryLevel;        // Last selected memory trimming level
     int curProcState = PROCESS_STATE_NONEXISTENT; // Currently computed process state
     int repProcState = PROCESS_STATE_NONEXISTENT; // Last reported process state
     int setProcState = PROCESS_STATE_NONEXISTENT; // Last set process state in process tracker
     int pssProcState = PROCESS_STATE_NONEXISTENT; // Currently requesting pss for
+    int savedPriority;          // Previous priority value if we're switching to non-SCHED_OTHER
+    int renderThreadTid;        // TID for RenderThread
     boolean serviceb;           // Process currently is on the service B list
     boolean serviceHighRam;     // We are forcing to service B list due to its RAM use
     boolean setIsForeground;    // Running foreground UI when last set?
@@ -113,6 +116,8 @@
     boolean repForegroundActivities; // Last reported foreground activities.
     boolean systemNoUi;         // This is a system process, but not currently showing UI.
     boolean hasShownUi;         // Has UI been shown in this process since it was started?
+    boolean hasTopUi;           // Is this process currently showing "top-level" UI that is not an
+                                // activity?
     boolean pendingUiClean;     // Want to clean up resources from showing UI?
     boolean hasAboveClient;     // Bound using BIND_ABOVE_CLIENT, so want to be lower
     boolean treatLikeActivity;  // Bound using BIND_TREAT_LIKE_ACTIVITY
@@ -293,6 +298,7 @@
                 pw.print(" setSchedGroup="); pw.print(setSchedGroup);
                 pw.print(" systemNoUi="); pw.print(systemNoUi);
                 pw.print(" trimMemoryLevel="); pw.println(trimMemoryLevel);
+        pw.print(prefix); pw.print("vrThreadTid="); pw.print(vrThreadTid);
         pw.print(prefix); pw.print("curProcState="); pw.print(curProcState);
                 pw.print(" repProcState="); pw.print(repProcState);
                 pw.print(" pssProcState="); pw.print(pssProcState);
@@ -430,6 +436,9 @@
                 pw.print(prefix); pw.print("  - "); pw.println(receivers.valueAt(i));
             }
         }
+        if (hasTopUi) {
+            pw.print(prefix); pw.print("hasTopUi="); pw.print(hasTopUi);
+        }
     }
 
     ProcessRecord(BatteryStatsImpl _batteryStats, ApplicationInfo _info,
diff --git a/services/core/java/com/android/server/am/RecentTasks.java b/services/core/java/com/android/server/am/RecentTasks.java
index 5c05ab6..beb863b 100644
--- a/services/core/java/com/android/server/am/RecentTasks.java
+++ b/services/core/java/com/android/server/am/RecentTasks.java
@@ -653,12 +653,17 @@
                             && task.realActivity.equals(tr.realActivity);
                     // If the document is open in another app or is not the same
                     // document, we don't need to trim it.
-                    if (!sameActivity || !sameIntentFilter || multiTasksAllowed) {
+                    if (!sameActivity) {
                         continue;
                     // Otherwise only trim if we are over our max recents for this task
-                    } else if (maxRecents > 0 && !doTrim) {
+                    } else if (maxRecents > 0) {
                         --maxRecents;
-                        continue;
+                        if (!doTrim || !sameIntentFilter || multiTasksAllowed) {
+                            // We don't want to trim if we are not over the max allowed entries and
+                            // the caller doesn't want us to trim, the tasks are not of the same
+                            // intent filter, or multiple entries fot the task is allowed.
+                            continue;
+                        }
                     }
                     // Hit the maximum number of documents for this task. Fall through
                     // and remove this document from recents.
diff --git a/services/core/java/com/android/server/am/ServiceRecord.java b/services/core/java/com/android/server/am/ServiceRecord.java
index 2bfc402..71c7fd3 100644
--- a/services/core/java/com/android/server/am/ServiceRecord.java
+++ b/services/core/java/com/android/server/am/ServiceRecord.java
@@ -543,27 +543,25 @@
     }
     
     public void cancelNotification() {
-        if (foregroundId != 0) {
-            // Do asynchronous communication with notification manager to
-            // avoid deadlocks.
-            final String localPackageName = packageName;
-            final int localForegroundId = foregroundId;
-            ams.mHandler.post(new Runnable() {
-                public void run() {
-                    INotificationManager inm = NotificationManager.getService();
-                    if (inm == null) {
-                        return;
-                    }
-                    try {
-                        inm.cancelNotificationWithTag(localPackageName, null,
-                                localForegroundId, userId);
-                    } catch (RuntimeException e) {
-                        Slog.w(TAG, "Error canceling notification for service", e);
-                    } catch (RemoteException e) {
-                    }
+        // Do asynchronous communication with notification manager to
+        // avoid deadlocks.
+        final String localPackageName = packageName;
+        final int localForegroundId = foregroundId;
+        ams.mHandler.post(new Runnable() {
+            public void run() {
+                INotificationManager inm = NotificationManager.getService();
+                if (inm == null) {
+                    return;
                 }
-            });
-        }
+                try {
+                    inm.cancelNotificationWithTag(localPackageName, null,
+                            localForegroundId, userId);
+                } catch (RuntimeException e) {
+                    Slog.w(TAG, "Error canceling notification for service", e);
+                } catch (RemoteException e) {
+                }
+            }
+        });
     }
 
     public void stripForegroundServiceFlagFromNotification() {
diff --git a/services/core/java/com/android/server/am/TaskPersister.java b/services/core/java/com/android/server/am/TaskPersister.java
index 48fecd5..43eb251 100644
--- a/services/core/java/com/android/server/am/TaskPersister.java
+++ b/services/core/java/com/android/server/am/TaskPersister.java
@@ -87,6 +87,8 @@
     private final RecentTasks mRecentTasks;
     private final SparseArray<SparseBooleanArray> mTaskIdsInFile = new SparseArray<>();
     private final File mTaskIdsDir;
+    // To lock file operations in TaskPersister
+    private final Object mIoLock = new Object();
 
     /**
      * Value determines write delay mode as follows: < 0 We are Flushing. No delays between writes
@@ -195,52 +197,52 @@
             return mTaskIdsInFile.get(userId).clone();
         }
         final SparseBooleanArray persistedTaskIds = new SparseBooleanArray();
-        BufferedReader reader = null;
-        String line;
-        try {
-            reader = new BufferedReader(new FileReader(getUserPersistedTaskIdsFile(userId)));
-            while ((line = reader.readLine()) != null) {
-                for (String taskIdString : line.split("\\s+")) {
-                    int id = Integer.parseInt(taskIdString);
-                    persistedTaskIds.put(id, true);
+        synchronized (mIoLock) {
+            BufferedReader reader = null;
+            String line;
+            try {
+                reader = new BufferedReader(new FileReader(getUserPersistedTaskIdsFile(userId)));
+                while ((line = reader.readLine()) != null) {
+                    for (String taskIdString : line.split("\\s+")) {
+                        int id = Integer.parseInt(taskIdString);
+                        persistedTaskIds.put(id, true);
+                    }
                 }
+            } catch (FileNotFoundException e) {
+                // File doesn't exist. Ignore.
+            } catch (Exception e) {
+                Slog.e(TAG, "Error while reading taskIds file for user " + userId, e);
+            } finally {
+                IoUtils.closeQuietly(reader);
             }
-        } catch (FileNotFoundException e) {
-            // File doesn't exist. Ignore.
-        } catch (Exception e) {
-            Slog.e(TAG, "Error while reading taskIds file for user " + userId, e);
-        } finally {
-            IoUtils.closeQuietly(reader);
         }
         mTaskIdsInFile.put(userId, persistedTaskIds);
         return persistedTaskIds.clone();
     }
 
+
     @VisibleForTesting
-    void maybeWritePersistedTaskIdsForUser(@NonNull SparseBooleanArray taskIds, int userId) {
+    void writePersistedTaskIdsForUser(@NonNull SparseBooleanArray taskIds, int userId) {
         if (userId < 0) {
             return;
         }
-        SparseBooleanArray persistedIdsInFile = mTaskIdsInFile.get(userId);
-        if (persistedIdsInFile != null && persistedIdsInFile.equals(taskIds)) {
-            return;
-        }
         final File persistedTaskIdsFile = getUserPersistedTaskIdsFile(userId);
-        BufferedWriter writer = null;
-        try {
-            writer = new BufferedWriter(new FileWriter(persistedTaskIdsFile));
-            for (int i = 0; i < taskIds.size(); i++) {
-                if (taskIds.valueAt(i)) {
-                    writer.write(String.valueOf(taskIds.keyAt(i)));
-                    writer.newLine();
+        synchronized (mIoLock) {
+            BufferedWriter writer = null;
+            try {
+                writer = new BufferedWriter(new FileWriter(persistedTaskIdsFile));
+                for (int i = 0; i < taskIds.size(); i++) {
+                    if (taskIds.valueAt(i)) {
+                        writer.write(String.valueOf(taskIds.keyAt(i)));
+                        writer.newLine();
+                    }
                 }
+            } catch (Exception e) {
+                Slog.e(TAG, "Error while writing taskIds file for user " + userId, e);
+            } finally {
+                IoUtils.closeQuietly(writer);
             }
-        } catch (Exception e) {
-            Slog.e(TAG, "Error while writing taskIds file for user " + userId, e);
-        } finally {
-            IoUtils.closeQuietly(writer);
         }
-        mTaskIdsInFile.put(userId, taskIds.clone());
     }
 
     void unloadUserDataFromMemory(int userId) {
@@ -543,16 +545,23 @@
     }
 
     private void writeTaskIdsFiles() {
-        int candidateUserIds[];
+        SparseArray<SparseBooleanArray> changedTaskIdsPerUser = new SparseArray<>();
         synchronized (mService) {
-            candidateUserIds = mRecentTasks.usersWithRecentsLoadedLocked();
-        }
-        SparseBooleanArray taskIdsToSave;
-        for (int userId : candidateUserIds) {
-            synchronized (mService) {
-                taskIdsToSave = mRecentTasks.mPersistedTaskIds.get(userId).clone();
+            for (int userId : mRecentTasks.usersWithRecentsLoadedLocked()) {
+                SparseBooleanArray taskIdsToSave = mRecentTasks.mPersistedTaskIds.get(userId);
+                SparseBooleanArray persistedIdsInFile = mTaskIdsInFile.get(userId);
+                if (persistedIdsInFile != null && persistedIdsInFile.equals(taskIdsToSave)) {
+                    continue;
+                } else {
+                    SparseBooleanArray taskIdsToSaveCopy = taskIdsToSave.clone();
+                    mTaskIdsInFile.put(userId, taskIdsToSaveCopy);
+                    changedTaskIdsPerUser.put(userId, taskIdsToSaveCopy);
+                }
             }
-            maybeWritePersistedTaskIdsForUser(taskIdsToSave, userId);
+        }
+        for (int i = 0; i < changedTaskIdsPerUser.size(); i++) {
+            writePersistedTaskIdsForUser(changedTaskIdsPerUser.valueAt(i),
+                    changedTaskIdsPerUser.keyAt(i));
         }
     }
 
diff --git a/services/core/java/com/android/server/am/UriPermission.java b/services/core/java/com/android/server/am/UriPermission.java
index 6e371c1..0aa54d9 100644
--- a/services/core/java/com/android/server/am/UriPermission.java
+++ b/services/core/java/com/android/server/am/UriPermission.java
@@ -19,6 +19,7 @@
 import android.content.Intent;
 import android.os.UserHandle;
 import android.util.ArraySet;
+import android.util.Log;
 import android.util.Slog;
 
 import com.android.server.am.ActivityManagerService.GrantUri;
@@ -93,7 +94,16 @@
     }
 
     private void updateModeFlags() {
+        final int oldModeFlags = modeFlags;
         modeFlags = ownedModeFlags | globalModeFlags | persistableModeFlags | persistedModeFlags;
+
+        if (Log.isLoggable(TAG, Log.VERBOSE) && (modeFlags != oldModeFlags)) {
+            Slog.d(TAG,
+                    "Permission for " + targetPkg + " to " + uri + " is changing from 0x"
+                            + Integer.toHexString(oldModeFlags) + " to 0x"
+                            + Integer.toHexString(modeFlags),
+                    new Throwable());
+        }
     }
 
     /**
diff --git a/services/core/java/com/android/server/am/UserController.java b/services/core/java/com/android/server/am/UserController.java
index b685dd3..4bc148b 100644
--- a/services/core/java/com/android/server/am/UserController.java
+++ b/services/core/java/com/android/server/am/UserController.java
@@ -76,6 +76,7 @@
 import android.os.UserManagerInternal;
 import android.os.storage.IMountService;
 import android.os.storage.StorageManager;
+import android.util.ArraySet;
 import android.util.IntArray;
 import android.util.Pair;
 import android.util.Slog;
@@ -86,6 +87,7 @@
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.util.ArrayUtils;
+import com.android.internal.util.Preconditions;
 import com.android.internal.widget.LockPatternUtils;
 import com.android.server.LocalServices;
 import com.android.server.pm.UserManagerService;
@@ -97,6 +99,7 @@
 import java.util.List;
 import java.util.Objects;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * Helper class for {@link ActivityManagerService} responsible for multi-user functionality.
@@ -151,9 +154,9 @@
             = new RemoteCallbackList<>();
 
     /**
-     * Currently active user switch.
+     * Currently active user switch callbacks.
      */
-    Object mCurUserSwitchCallback;
+    private volatile ArraySet<String> mCurWaitingUserSwitchCallbacks;
 
     private volatile UserManagerService mUserManager;
 
@@ -224,6 +227,7 @@
 
     private void finishUserBoot(UserState uss, IIntentReceiver resultTo) {
         final int userId = uss.mHandle.getIdentifier();
+
         Slog.d(TAG, "Finishing user boot " + userId);
         synchronized (mService) {
             // Bail if we ended up with a stale user
@@ -275,6 +279,7 @@
      */
     private void finishUserUnlocking(final UserState uss) {
         final int userId = uss.mHandle.getIdentifier();
+        boolean proceedWithUnlock = false;
         synchronized (mService) {
             // Bail if we ended up with a stale user
             if (mStartedUsers.get(uss.mHandle.getIdentifier()) != uss) return;
@@ -284,20 +289,24 @@
 
             if (uss.setState(STATE_RUNNING_LOCKED, STATE_RUNNING_UNLOCKING)) {
                 getUserManagerInternal().setUserState(userId, uss.state);
-                uss.mUnlockProgress.start();
-
-                // Prepare app storage before we go any further
-                uss.mUnlockProgress.setProgress(5,
-                        mService.mContext.getString(R.string.android_start_title));
-                mUserManager.onBeforeUnlockUser(userId);
-                uss.mUnlockProgress.setProgress(20);
-
-                // Dispatch unlocked to system services; when fully dispatched,
-                // that calls through to the next "unlocked" phase
-                mHandler.obtainMessage(SYSTEM_USER_UNLOCK_MSG, userId, 0, uss)
-                        .sendToTarget();
+                proceedWithUnlock = true;
             }
         }
+
+        if (proceedWithUnlock) {
+            uss.mUnlockProgress.start();
+
+            // Prepare app storage before we go any further
+            uss.mUnlockProgress.setProgress(5,
+                    mService.mContext.getString(R.string.android_start_title));
+            mUserManager.onBeforeUnlockUser(userId);
+            uss.mUnlockProgress.setProgress(20);
+
+            // Dispatch unlocked to system services; when fully dispatched,
+            // that calls through to the next "unlocked" phase
+            mHandler.obtainMessage(SYSTEM_USER_UNLOCK_MSG, userId, 0, uss)
+                    .sendToTarget();
+        }
     }
 
     /**
@@ -347,7 +356,17 @@
                 // PRE_BOOT receivers are finished to avoid ANR'ing apps
                 final UserInfo info = getUserInfo(userId);
                 if (!Objects.equals(info.lastLoggedInFingerprint, Build.FINGERPRINT)) {
-                    new PreBootBroadcaster(mService, userId, null) {
+                    // Suppress double notifications for managed profiles that
+                    // were unlocked automatically as part of their parent user
+                    // being unlocked.
+                    final boolean quiet;
+                    if (info.isManagedProfile()) {
+                        quiet = !uss.tokenProvided
+                                || !mLockPatternUtils.isSeparateProfileChallengeEnabled(userId);
+                    } else {
+                        quiet = false;
+                    }
+                    new PreBootBroadcaster(mService, userId, null, quiet) {
                         @Override
                         public void onFinished() {
                             finishUserUnlockedCompleted(uss);
@@ -948,6 +967,7 @@
 
     boolean unlockUserCleared(final int userId, byte[] token, byte[] secret,
             IProgressListener listener) {
+        UserState uss;
         synchronized (mService) {
             // TODO Move this block outside of synchronized if it causes lock contention
             if (!StorageManager.isUserKeyUnlocked(userId)) {
@@ -962,15 +982,20 @@
             }
             // Bail if user isn't actually running, otherwise register the given
             // listener to watch for unlock progress
-            final UserState uss = mStartedUsers.get(userId);
+            uss = mStartedUsers.get(userId);
             if (uss == null) {
                 notifyFinished(userId, listener);
                 return false;
             } else {
                 uss.mUnlockProgress.addListener(listener);
+                uss.tokenProvided = (token != null);
             }
+        }
 
-            finishUserUnlocking(uss);
+        finishUserUnlocking(uss);
+
+        final ArraySet<Integer> childProfilesToUnlock = new ArraySet<>();
+        synchronized (mService) {
 
             // We just unlocked a user, so let's now attempt to unlock any
             // managed profiles under that user.
@@ -980,11 +1005,16 @@
                 if (parent != null && parent.id == userId && testUserId != userId) {
                     Slog.d(TAG, "User " + testUserId + " (parent " + parent.id
                             + "): attempting unlock because parent was just unlocked");
-                    maybeUnlockUser(testUserId);
+                    childProfilesToUnlock.add(testUserId);
                 }
             }
         }
 
+        final int size = childProfilesToUnlock.size();
+        for (int i = 0; i < size; i++) {
+            maybeUnlockUser(childProfilesToUnlock.valueAt(i));
+        }
+
         return true;
     }
 
@@ -1039,7 +1069,8 @@
 
     void timeoutUserSwitch(UserState uss, int oldUserId, int newUserId) {
         synchronized (mService) {
-            Slog.wtf(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId);
+            Slog.wtf(TAG, "User switch timeout: from " + oldUserId + " to " + newUserId
+                    + ". Observers that didn't send results: " + mCurWaitingUserSwitchCallbacks);
             sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
         }
     }
@@ -1048,28 +1079,37 @@
         Slog.d(TAG, "Dispatch onUserSwitching oldUser #" + oldUserId + " newUser #" + newUserId);
         final int observerCount = mUserSwitchObservers.beginBroadcast();
         if (observerCount > 0) {
-            final IRemoteCallback callback = new IRemoteCallback.Stub() {
-                int mCount = 0;
-                @Override
-                public void sendResult(Bundle data) throws RemoteException {
-                    synchronized (mService) {
-                        if (mCurUserSwitchCallback == this) {
-                            mCount++;
-                            if (mCount == observerCount) {
-                                sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
-                            }
-                        }
-                    }
-                }
-            };
+            final ArraySet<String> curWaitingUserSwitchCallbacks = new ArraySet<>();
             synchronized (mService) {
                 uss.switching = true;
-                mCurUserSwitchCallback = callback;
+                mCurWaitingUserSwitchCallbacks = curWaitingUserSwitchCallbacks;
             }
+            final AtomicInteger waitingCallbacksCount = new AtomicInteger(observerCount);
             for (int i = 0; i < observerCount; i++) {
                 try {
-                    mUserSwitchObservers.getBroadcastItem(i).onUserSwitching(
-                            newUserId, callback);
+                    // Prepend with unique prefix to guarantee that keys are unique
+                    final String name = "#" + i + " " + mUserSwitchObservers.getBroadcastCookie(i);
+                    synchronized (mService) {
+                        curWaitingUserSwitchCallbacks.add(name);
+                    }
+                    final IRemoteCallback callback = new IRemoteCallback.Stub() {
+                        @Override
+                        public void sendResult(Bundle data) throws RemoteException {
+                            synchronized (mService) {
+                                // Early return if this session is no longer valid
+                                if (curWaitingUserSwitchCallbacks
+                                        != mCurWaitingUserSwitchCallbacks) {
+                                    return;
+                                }
+                                curWaitingUserSwitchCallbacks.remove(name);
+                                // Continue switching if all callbacks have been notified
+                                if (waitingCallbacksCount.decrementAndGet() == 0) {
+                                    sendContinueUserSwitchLocked(uss, oldUserId, newUserId);
+                                }
+                            }
+                        }
+                    };
+                    mUserSwitchObservers.getBroadcastItem(i).onUserSwitching(newUserId, callback);
                 } catch (RemoteException e) {
                 }
             }
@@ -1082,7 +1122,7 @@
     }
 
     void sendContinueUserSwitchLocked(UserState uss, int oldUserId, int newUserId) {
-        mCurUserSwitchCallback = null;
+        mCurWaitingUserSwitchCallbacks = null;
         mHandler.removeMessages(USER_SWITCH_TIMEOUT_MSG);
         mHandler.sendMessage(mHandler.obtainMessage(ActivityManagerService.CONTINUE_USER_SWITCH_MSG,
                 oldUserId, newUserId, uss));
@@ -1247,7 +1287,8 @@
                 ? getCurrentUserIdLocked(): userId;
     }
 
-    void registerUserSwitchObserver(IUserSwitchObserver observer) {
+    void registerUserSwitchObserver(IUserSwitchObserver observer, String name) {
+        Preconditions.checkNotNull(name, "Observer name cannot be null");
         if (mService.checkCallingPermission(INTERACT_ACROSS_USERS_FULL)
                 != PackageManager.PERMISSION_GRANTED) {
             final String msg = "Permission Denial: registerUserSwitchObserver() from pid="
@@ -1257,8 +1298,7 @@
             Slog.w(TAG, msg);
             throw new SecurityException(msg);
         }
-
-        mUserSwitchObservers.register(observer);
+        mUserSwitchObservers.register(observer, name);
     }
 
     void unregisterUserSwitchObserver(IUserSwitchObserver observer) {
@@ -1453,6 +1493,9 @@
     }
 
     boolean isSameProfileGroup(int callingUserId, int targetUserId) {
+        if (callingUserId == targetUserId) {
+            return true;
+        }
         synchronized (mUserProfileGroupIdsSelfLocked) {
             int callingProfile = mUserProfileGroupIdsSelfLocked.get(callingUserId,
                     UserInfo.NO_PROFILE_GROUP_ID);
diff --git a/services/core/java/com/android/server/am/UserState.java b/services/core/java/com/android/server/am/UserState.java
index 952283e..ff8014c 100644
--- a/services/core/java/com/android/server/am/UserState.java
+++ b/services/core/java/com/android/server/am/UserState.java
@@ -54,6 +54,7 @@
     public int state = STATE_BOOTING;
     public int lastState = STATE_BOOTING;
     public boolean switching;
+    public boolean tokenProvided;
 
     /**
      * The last time that a provider was reported to usage stats as being brought to important
diff --git a/services/core/java/com/android/server/am/UserSwitchingDialog.java b/services/core/java/com/android/server/am/UserSwitchingDialog.java
index 10e88e6..7022856 100644
--- a/services/core/java/com/android/server/am/UserSwitchingDialog.java
+++ b/services/core/java/com/android/server/am/UserSwitchingDialog.java
@@ -68,6 +68,12 @@
         String viewMessage;
         if (UserManager.isSplitSystemUser() && newUser.id == UserHandle.USER_SYSTEM) {
             viewMessage = res.getString(R.string.user_logging_out_message, oldUser.name);
+        } else if (UserManager.isDeviceInDemoMode(context)) {
+            if (oldUser.isDemo()) {
+                viewMessage = res.getString(R.string.demo_restarting_message);
+            } else {
+                viewMessage = res.getString(R.string.demo_starting_message);
+            }
         } else {
             viewMessage = res.getString(R.string.user_switching_message, newUser.name);
         }
diff --git a/services/core/java/com/android/server/audio/AudioService.java b/services/core/java/com/android/server/audio/AudioService.java
index 7777ae23f..a9277ce 100644
--- a/services/core/java/com/android/server/audio/AudioService.java
+++ b/services/core/java/com/android/server/audio/AudioService.java
@@ -1130,8 +1130,11 @@
         final int currentUser = getCurrentUserId();
 
         // Check the current user restriction.
-        boolean masterMute = mUserManagerInternal.getUserRestriction(
-                    currentUser, UserManager.DISALLOW_ADJUST_VOLUME);
+        boolean masterMute =
+                mUserManagerInternal.getUserRestriction(currentUser,
+                        UserManager.DISALLLOW_UNMUTE_DEVICE)
+                        || mUserManagerInternal.getUserRestriction(currentUser,
+                        UserManager.DISALLOW_ADJUST_VOLUME);
         if (mUseFixedVolume) {
             masterMute = false;
             AudioSystem.setMasterVolume(1.0f);
@@ -5380,9 +5383,11 @@
             // Update speaker mute state.
             {
                 final boolean wasRestricted =
-                        prevRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME);
+                        prevRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME)
+                                || prevRestrictions.getBoolean(UserManager.DISALLLOW_UNMUTE_DEVICE);
                 final boolean isRestricted =
-                        newRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME);
+                        newRestrictions.getBoolean(UserManager.DISALLOW_ADJUST_VOLUME)
+                                || newRestrictions.getBoolean(UserManager.DISALLLOW_UNMUTE_DEVICE);
                 if (wasRestricted != isRestricted) {
                     setMasterMuteInternalNoCallerCheck(isRestricted, /* flags =*/ 0, userId);
                 }
@@ -5604,6 +5609,8 @@
                 } else { // config == AudioSystem.FORCE_NONE
                     mBecomingNoisyIntentDevices |= AudioSystem.DEVICE_OUT_ALL_A2DP;
                 }
+                sendMsg(mAudioHandler, MSG_REPORT_NEW_ROUTES,
+                        SENDMSG_NOOP, 0, 0, null, 0);
                 break;
             case AudioSystem.FOR_DOCK:
                 if (config == AudioSystem.FORCE_ANALOG_DOCK) {
diff --git a/services/core/java/com/android/server/audio/FocusRequester.java b/services/core/java/com/android/server/audio/FocusRequester.java
index 49be879..cc181141 100644
--- a/services/core/java/com/android/server/audio/FocusRequester.java
+++ b/services/core/java/com/android/server/audio/FocusRequester.java
@@ -40,9 +40,9 @@
     private static final String TAG = "MediaFocusControl";
     private static final boolean DEBUG = false;
 
-    private AudioFocusDeathHandler mDeathHandler;
-    private final IAudioFocusDispatcher mFocusDispatcher; // may be null
-    private final IBinder mSourceRef;
+    private AudioFocusDeathHandler mDeathHandler; // may be null
+    private IAudioFocusDispatcher mFocusDispatcher; // may be null
+    private final IBinder mSourceRef; // may be null
     private final String mClientId;
     private final String mPackageName;
     private final int mCallingUid;
@@ -205,6 +205,7 @@
             if (mSourceRef != null && mDeathHandler != null) {
                 mSourceRef.unlinkToDeath(mDeathHandler, 0);
                 mDeathHandler = null;
+                mFocusDispatcher = null;
             }
         } catch (java.util.NoSuchElementException e) {
             Log.e(TAG, "FocusRequester.release() hit ", e);
@@ -275,12 +276,13 @@
             mFocusLossReceived = AudioManager.AUDIOFOCUS_NONE;
             mFocusController.notifyExtPolicyFocusGrant_syncAf(toAudioFocusInfo(),
                     AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
-            if (mFocusDispatcher != null) {
+            final IAudioFocusDispatcher fd = mFocusDispatcher;
+            if (fd != null) {
                 if (DEBUG) {
                     Log.v(TAG, "dispatching " + focusChangeToString(focusGain) + " to "
                         + mClientId);
                 }
-                mFocusDispatcher.dispatchAudioFocusChange(focusGain, mClientId);
+                fd.dispatchAudioFocusChange(focusGain, mClientId);
             }
         } catch (android.os.RemoteException e) {
             Log.e(TAG, "Failure to signal gain of audio focus due to: ", e);
@@ -311,14 +313,15 @@
                             toAudioFocusInfo(), false /* wasDispatched */);
                     return;
                 }
-                if (mFocusDispatcher != null) {
+                final IAudioFocusDispatcher fd = mFocusDispatcher;
+                if (fd != null) {
                     if (DEBUG) {
                         Log.v(TAG, "dispatching " + focusChangeToString(mFocusLossReceived) + " to "
                             + mClientId);
                     }
                     mFocusController.notifyExtPolicyFocusLoss_syncAf(
                             toAudioFocusInfo(), true /* wasDispatched */);
-                    mFocusDispatcher.dispatchAudioFocusChange(mFocusLossReceived, mClientId);
+                    fd.dispatchAudioFocusChange(mFocusLossReceived, mClientId);
                 }
             }
         } catch (android.os.RemoteException e) {
diff --git a/services/core/java/com/android/server/audio/MediaFocusControl.java b/services/core/java/com/android/server/audio/MediaFocusControl.java
index 278d70b..206834e 100644
--- a/services/core/java/com/android/server/audio/MediaFocusControl.java
+++ b/services/core/java/com/android/server/audio/MediaFocusControl.java
@@ -160,6 +160,7 @@
                     Log.i(TAG, "AudioFocus  removeFocusStackEntry(): removing entry for "
                             + clientToRemove);
                     stackIterator.remove();
+                    // stack entry not used anymore, clear references
                     fr.release();
                 }
             }
@@ -171,7 +172,7 @@
      * Called synchronized on mAudioFocusLock
      * Remove focus listeners from the focus stack for a particular client when it has died.
      */
-    private void removeFocusStackEntryForClient(IBinder cb) {
+    private void removeFocusStackEntryOnDeath(IBinder cb) {
         // is the owner of the audio focus part of the client to remove?
         boolean isTopOfStackForClientToRemove = !mFocusStack.isEmpty() &&
                 mFocusStack.peek().hasSameBinder(cb);
@@ -181,9 +182,10 @@
         while(stackIterator.hasNext()) {
             FocusRequester fr = stackIterator.next();
             if(fr.hasSameBinder(cb)) {
-                Log.i(TAG, "AudioFocus  removeFocusStackEntry(): removing entry for " + cb);
+                Log.i(TAG, "AudioFocus  removeFocusStackEntryOnDeath(): removing entry for " + cb);
                 stackIterator.remove();
-                // the client just died, no need to unlink to its death
+                // stack entry not used anymore, clear references
+                fr.release();
             }
         }
         if (isTopOfStackForClientToRemove) {
@@ -257,14 +259,9 @@
 
         public void binderDied() {
             synchronized(mAudioFocusLock) {
-                Log.w(TAG, "  AudioFocus   audio focus client died");
-                removeFocusStackEntryForClient(mCb);
+                removeFocusStackEntryOnDeath(mCb);
             }
         }
-
-        public IBinder getBinder() {
-            return mCb;
-        }
     }
 
     /**
@@ -420,6 +417,7 @@
             // (premature death == death before abandoning focus)
             // Register for client death notification
             AudioFocusDeathHandler afdh = new AudioFocusDeathHandler(cb);
+
             try {
                 cb.linkToDeath(afdh, 0);
             } catch (RemoteException e) {
diff --git a/services/core/java/com/android/server/clipboard/ClipboardService.java b/services/core/java/com/android/server/clipboard/ClipboardService.java
index 1c26846..66aa403 100644
--- a/services/core/java/com/android/server/clipboard/ClipboardService.java
+++ b/services/core/java/com/android/server/clipboard/ClipboardService.java
@@ -188,6 +188,14 @@
                     if (!canCopy) {
                         clip = null;
                     } else {
+                        // We want to fix the uris of the related user's clip without changing the
+                        // uris of the current user's clip.
+                        // So, copy the ClipData, and then copy all the items, so that nothing
+                        // is shared in memmory.
+                        clip = new ClipData(clip);
+                        for (int i = clip.getItemCount() - 1; i >= 0; i--) {
+                            clip.setItemAt(i, new ClipData.Item(clip.getItemAt(i)));
+                        }
                         clip.fixUrisLight(userId);
                     }
                     for (int i = 0; i < size; i++) {
diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java b/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java
new file mode 100644
index 0000000..f1ef947
--- /dev/null
+++ b/services/core/java/com/android/server/connectivity/IpConnectivityEventBuilder.java
@@ -0,0 +1,257 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.net.ConnectivityMetricsEvent;
+import android.net.metrics.ApfProgramEvent;
+import android.net.metrics.ApfStats;
+import android.net.metrics.DefaultNetworkEvent;
+import android.net.metrics.DhcpClientEvent;
+import android.net.metrics.DhcpErrorEvent;
+import android.net.metrics.DnsEvent;
+import android.net.metrics.IpManagerEvent;
+import android.net.metrics.IpReachabilityEvent;
+import android.net.metrics.NetworkEvent;
+import android.net.metrics.RaEvent;
+import android.net.metrics.ValidationProbeEvent;
+import android.os.Parcelable;
+import com.android.server.connectivity.metrics.IpConnectivityLogClass;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent;
+import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityLog;
+import static com.android.server.connectivity.metrics.IpConnectivityLogClass.NetworkId;
+
+/** {@hide} */
+final public class IpConnectivityEventBuilder {
+    private IpConnectivityEventBuilder() {
+    }
+
+    public static byte[] serialize(int dropped, List<ConnectivityMetricsEvent> events)
+            throws IOException {
+        final IpConnectivityLog log = new IpConnectivityLog();
+        log.events = toProto(events);
+        log.droppedEvents = dropped;
+        return IpConnectivityLog.toByteArray(log);
+    }
+
+    public static IpConnectivityEvent[] toProto(List<ConnectivityMetricsEvent> eventsIn) {
+        final ArrayList<IpConnectivityEvent> eventsOut = new ArrayList<>(eventsIn.size());
+        for (ConnectivityMetricsEvent in : eventsIn) {
+            final IpConnectivityEvent out = toProto(in);
+            if (out == null) {
+                continue;
+            }
+            eventsOut.add(out);
+        }
+        return eventsOut.toArray(new IpConnectivityEvent[eventsOut.size()]);
+    }
+
+    public static IpConnectivityEvent toProto(ConnectivityMetricsEvent ev) {
+        final IpConnectivityEvent out = new IpConnectivityEvent();
+        if (!setEvent(out, ev.data)) {
+            return null;
+        }
+        out.timeMs = ev.timestamp;
+        return out;
+    }
+
+    private static boolean setEvent(IpConnectivityEvent out, Parcelable in) {
+        if (in instanceof DhcpErrorEvent) {
+            setDhcpErrorEvent(out, (DhcpErrorEvent) in);
+            return true;
+        }
+
+        if (in instanceof DhcpClientEvent) {
+            setDhcpClientEvent(out, (DhcpClientEvent) in);
+            return true;
+        }
+
+        if (in instanceof DnsEvent) {
+            setDnsEvent(out, (DnsEvent) in);
+            return true;
+        }
+
+        if (in instanceof IpManagerEvent) {
+            setIpManagerEvent(out, (IpManagerEvent) in);
+            return true;
+        }
+
+        if (in instanceof IpReachabilityEvent) {
+            setIpReachabilityEvent(out, (IpReachabilityEvent) in);
+            return true;
+        }
+
+        if (in instanceof DefaultNetworkEvent) {
+            setDefaultNetworkEvent(out, (DefaultNetworkEvent) in);
+            return true;
+        }
+
+        if (in instanceof NetworkEvent) {
+            setNetworkEvent(out, (NetworkEvent) in);
+            return true;
+        }
+
+        if (in instanceof ValidationProbeEvent) {
+            setValidationProbeEvent(out, (ValidationProbeEvent) in);
+            return true;
+        }
+
+        if (in instanceof ApfProgramEvent) {
+            setApfProgramEvent(out, (ApfProgramEvent) in);
+            return true;
+        }
+
+        if (in instanceof ApfStats) {
+            setApfStats(out, (ApfStats) in);
+            return true;
+        }
+
+        if (in instanceof RaEvent) {
+            setRaEvent(out, (RaEvent) in);
+            return true;
+        }
+
+        return false;
+    }
+
+    private static void setDhcpErrorEvent(IpConnectivityEvent out, DhcpErrorEvent in) {
+        out.dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
+        out.dhcpEvent.ifName = in.ifName;
+        out.dhcpEvent.errorCode = in.errorCode;
+    }
+
+    private static void setDhcpClientEvent(IpConnectivityEvent out, DhcpClientEvent in) {
+        out.dhcpEvent = new IpConnectivityLogClass.DHCPEvent();
+        out.dhcpEvent.ifName = in.ifName;
+        out.dhcpEvent.stateTransition = in.msg;
+        out.dhcpEvent.durationMs = in.durationMs;
+    }
+
+    private static void setDnsEvent(IpConnectivityEvent out, DnsEvent in) {
+        out.dnsLookupBatch = new IpConnectivityLogClass.DNSLookupBatch();
+        out.dnsLookupBatch.networkId = netIdOf(in.netId);
+        out.dnsLookupBatch.eventTypes = bytesToInts(in.eventTypes);
+        out.dnsLookupBatch.returnCodes = bytesToInts(in.returnCodes);
+        out.dnsLookupBatch.latenciesMs = in.latenciesMs;
+    }
+
+    private static void setIpManagerEvent(IpConnectivityEvent out, IpManagerEvent in) {
+        out.ipProvisioningEvent = new IpConnectivityLogClass.IpProvisioningEvent();
+        out.ipProvisioningEvent.ifName = in.ifName;
+        out.ipProvisioningEvent.eventType = in.eventType;
+        out.ipProvisioningEvent.latencyMs = (int) in.durationMs;
+    }
+
+    private static void setIpReachabilityEvent(IpConnectivityEvent out, IpReachabilityEvent in) {
+        out.ipReachabilityEvent = new IpConnectivityLogClass.IpReachabilityEvent();
+        out.ipReachabilityEvent.ifName = in.ifName;
+        out.ipReachabilityEvent.eventType = in.eventType;
+    }
+
+    private static void setDefaultNetworkEvent(IpConnectivityEvent out, DefaultNetworkEvent in) {
+        out.defaultNetworkEvent = new IpConnectivityLogClass.DefaultNetworkEvent();
+        out.defaultNetworkEvent.networkId = netIdOf(in.netId);
+        out.defaultNetworkEvent.previousNetworkId = netIdOf(in.prevNetId);
+        out.defaultNetworkEvent.transportTypes = in.transportTypes;
+        out.defaultNetworkEvent.previousNetworkIpSupport = ipSupportOf(in);
+    }
+
+    private static void setNetworkEvent(IpConnectivityEvent out, NetworkEvent in) {
+        out.networkEvent = new IpConnectivityLogClass.NetworkEvent();
+        out.networkEvent.networkId = netIdOf(in.netId);
+        out.networkEvent.eventType = in.eventType;
+        out.networkEvent.latencyMs = (int) in.durationMs;
+    }
+
+    private static void setValidationProbeEvent(IpConnectivityEvent out, ValidationProbeEvent in) {
+        out.validationProbeEvent = new IpConnectivityLogClass.ValidationProbeEvent();
+        out.validationProbeEvent.networkId = netIdOf(in.netId);
+        out.validationProbeEvent.latencyMs = (int) in.durationMs;
+        out.validationProbeEvent.probeType = in.probeType;
+        out.validationProbeEvent.probeResult = in.returnCode;
+    }
+
+    private static void setApfProgramEvent(IpConnectivityEvent out, ApfProgramEvent in) {
+        out.apfProgramEvent = new IpConnectivityLogClass.ApfProgramEvent();
+        out.apfProgramEvent.lifetime = in.lifetime;
+        out.apfProgramEvent.filteredRas = in.filteredRas;
+        out.apfProgramEvent.currentRas = in.currentRas;
+        out.apfProgramEvent.programLength = in.programLength;
+        if (isBitSet(in.flags, ApfProgramEvent.FLAG_MULTICAST_FILTER_ON)) {
+            out.apfProgramEvent.dropMulticast = true;
+        }
+        if (isBitSet(in.flags, ApfProgramEvent.FLAG_HAS_IPV4_ADDRESS)) {
+            out.apfProgramEvent.hasIpv4Addr = true;
+        }
+    }
+
+    private static void setApfStats(IpConnectivityEvent out, ApfStats in) {
+        out.apfStatistics = new IpConnectivityLogClass.ApfStatistics();
+        out.apfStatistics.durationMs = in.durationMs;
+        out.apfStatistics.receivedRas = in.receivedRas;
+        out.apfStatistics.matchingRas = in.matchingRas;
+        out.apfStatistics.droppedRas = in.droppedRas;
+        out.apfStatistics.zeroLifetimeRas = in.zeroLifetimeRas;
+        out.apfStatistics.parseErrors = in.parseErrors;
+        out.apfStatistics.programUpdates = in.programUpdates;
+        out.apfStatistics.maxProgramSize = in.maxProgramSize;
+    }
+
+    private static void setRaEvent(IpConnectivityEvent out, RaEvent in) {
+        out.raEvent = new IpConnectivityLogClass.RaEvent();
+        out.raEvent.routerLifetime = in.routerLifetime;
+        out.raEvent.prefixValidLifetime = in.prefixValidLifetime;
+        out.raEvent.prefixPreferredLifetime = in.prefixPreferredLifetime;
+        out.raEvent.routeInfoLifetime = in.routeInfoLifetime;
+        out.raEvent.rdnssLifetime = in.rdnssLifetime;
+        out.raEvent.dnsslLifetime = in.dnsslLifetime;
+    }
+
+    private static int[] bytesToInts(byte[] in) {
+        final int[] out = new int[in.length];
+        for (int i = 0; i < in.length; i++) {
+            out[i] = in[i] & 0xFF;
+        }
+        return out;
+    }
+
+    private static NetworkId netIdOf(int netid) {
+        final NetworkId ni = new NetworkId();
+        ni.networkId = netid;
+        return ni;
+    }
+
+    private static int ipSupportOf(DefaultNetworkEvent in) {
+        if (in.prevIPv4 && in.prevIPv6) {
+            return IpConnectivityLogClass.DefaultNetworkEvent.DUAL;
+        }
+        if (in.prevIPv6) {
+            return IpConnectivityLogClass.DefaultNetworkEvent.IPV6;
+        }
+        if (in.prevIPv4) {
+            return IpConnectivityLogClass.DefaultNetworkEvent.IPV4;
+        }
+        return IpConnectivityLogClass.DefaultNetworkEvent.NONE;
+    }
+
+    private static boolean isBitSet(int flags, int bit) {
+        return (flags & (1 << bit)) != 0;
+    }
+}
diff --git a/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java
new file mode 100644
index 0000000..28e724c
--- /dev/null
+++ b/services/core/java/com/android/server/connectivity/IpConnectivityMetrics.java
@@ -0,0 +1,229 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.content.Context;
+import android.net.ConnectivityMetricsEvent;
+import android.net.IIpConnectivityMetrics;
+import android.net.metrics.IpConnectivityLog;
+import android.os.IBinder;
+import android.os.Parcelable;
+import android.text.TextUtils;
+import android.util.Base64;
+import android.util.Log;
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.server.SystemService;
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+
+import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityEvent;
+
+/** {@hide} */
+final public class IpConnectivityMetrics extends SystemService {
+    private static final String TAG = IpConnectivityMetrics.class.getSimpleName();
+    private static final boolean DBG = false;
+
+    private static final String SERVICE_NAME = IpConnectivityLog.SERVICE_NAME;
+
+    // Default size of the event buffer. Once the buffer is full, incoming events are dropped.
+    private static final int DEFAULT_BUFFER_SIZE = 2000;
+
+    // Lock ensuring that concurrent manipulations of the event buffer are correct.
+    // There are three concurrent operations to synchronize:
+    //  - appending events to the buffer.
+    //  - iterating throught the buffer.
+    //  - flushing the buffer content and replacing it by a new buffer.
+    private final Object mLock = new Object();
+
+    @VisibleForTesting
+    public final Impl impl = new Impl();
+    private NetdEventListenerService mNetdListener;
+
+    @GuardedBy("mLock")
+    private ArrayList<ConnectivityMetricsEvent> mBuffer;
+    @GuardedBy("mLock")
+    private int mDropped;
+    @GuardedBy("mLock")
+    private int mCapacity;
+
+    public IpConnectivityMetrics(Context ctx) {
+        super(ctx);
+        initBuffer();
+    }
+
+    @Override
+    public void onStart() {
+        if (DBG) Log.d(TAG, "onStart");
+    }
+
+    @Override
+    public void onBootPhase(int phase) {
+        if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
+            if (DBG) Log.d(TAG, "onBootPhase");
+            mNetdListener = new NetdEventListenerService(getContext());
+
+            publishBinderService(SERVICE_NAME, impl);
+            publishBinderService(mNetdListener.SERVICE_NAME, mNetdListener);
+        }
+    }
+
+    @VisibleForTesting
+    public int bufferCapacity() {
+        return DEFAULT_BUFFER_SIZE; // TODO: read from config
+    }
+
+    private void initBuffer() {
+        synchronized (mLock) {
+            mDropped = 0;
+            mCapacity = bufferCapacity();
+            mBuffer = new ArrayList<>(mCapacity);
+        }
+    }
+
+    private int append(ConnectivityMetricsEvent event) {
+        if (DBG) Log.d(TAG, "logEvent: " + event);
+        synchronized (mLock) {
+            final int left = mCapacity - mBuffer.size();
+            if (event == null) {
+                return left;
+            }
+            if (left == 0) {
+                mDropped++;
+                return 0;
+            }
+            mBuffer.add(event);
+            return left - 1;
+        }
+    }
+
+    private String flushEncodedOutput() {
+        final ArrayList<ConnectivityMetricsEvent> events;
+        final int dropped;
+        synchronized (mLock) {
+            events = mBuffer;
+            dropped = mDropped;
+            initBuffer();
+        }
+
+        final byte[] data;
+        try {
+            data = IpConnectivityEventBuilder.serialize(dropped, events);
+        } catch (IOException e) {
+            Log.e(TAG, "could not serialize events", e);
+            return "";
+        }
+
+        return Base64.encodeToString(data, Base64.DEFAULT);
+    }
+
+    /**
+     * Clears the event buffer and prints its content as a protobuf serialized byte array
+     * inside a base64 encoded string.
+     */
+    private void cmdFlush(FileDescriptor fd, PrintWriter pw, String[] args) {
+        pw.print(flushEncodedOutput());
+    }
+
+    /**
+     * Prints the content of the event buffer, either using the events ASCII representation
+     * or using protobuf text format.
+     */
+    private void cmdList(FileDescriptor fd, PrintWriter pw, String[] args) {
+        final ArrayList<ConnectivityMetricsEvent> events;
+        synchronized (mLock) {
+            events = new ArrayList(mBuffer);
+        }
+
+        if (args.length > 1 && args[1].equals("proto")) {
+            for (IpConnectivityEvent ev : IpConnectivityEventBuilder.toProto(events)) {
+                pw.print(ev.toString());
+            }
+            return;
+        }
+
+        for (ConnectivityMetricsEvent ev : events) {
+            pw.println(ev.toString());
+        }
+    }
+
+    private void cmdStats(FileDescriptor fd, PrintWriter pw, String[] args) {
+        synchronized (mLock) {
+            pw.println("Buffered events: " + mBuffer.size());
+            pw.println("Buffer capacity: " + mCapacity);
+            pw.println("Dropped events: " + mDropped);
+        }
+        if (mNetdListener != null) {
+            mNetdListener.dump(pw);
+        }
+    }
+
+    private void cmdDefault(FileDescriptor fd, PrintWriter pw, String[] args) {
+        if (args.length == 0) {
+            pw.println("No command");
+            return;
+        }
+        pw.println("Unknown command " + TextUtils.join(" ", args));
+    }
+
+    public final class Impl extends IIpConnectivityMetrics.Stub {
+        static final String CMD_FLUSH   = "flush";
+        static final String CMD_LIST    = "list";
+        static final String CMD_STATS   = "stats";
+        static final String CMD_DEFAULT = CMD_STATS;
+
+        @Override
+        public int logEvent(ConnectivityMetricsEvent event) {
+            enforceConnectivityInternalPermission();
+            return append(event);
+        }
+
+        @Override
+        public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+            enforceDumpPermission();
+            if (DBG) Log.d(TAG, "dumpsys " + TextUtils.join(" ", args));
+            final String cmd = (args.length > 0) ? args[0] : CMD_DEFAULT;
+            switch (cmd) {
+                case CMD_FLUSH:
+                    cmdFlush(fd, pw, args);
+                    return;
+                case CMD_LIST:
+                    cmdList(fd, pw, args);
+                    return;
+                case CMD_STATS:
+                    cmdStats(fd, pw, args);
+                    return;
+                default:
+                    cmdDefault(fd, pw, args);
+            }
+        }
+
+        private void enforceConnectivityInternalPermission() {
+            enforcePermission(android.Manifest.permission.CONNECTIVITY_INTERNAL);
+        }
+
+        private void enforceDumpPermission() {
+            enforcePermission(android.Manifest.permission.DUMP);
+        }
+
+        private void enforcePermission(String what) {
+            getContext().enforceCallingOrSelfPermission(what, "IpConnectivityMetrics");
+        }
+    };
+}
diff --git a/services/core/java/com/android/server/connectivity/LingerMonitor.java b/services/core/java/com/android/server/connectivity/LingerMonitor.java
new file mode 100644
index 0000000..635db19
--- /dev/null
+++ b/services/core/java/com/android/server/connectivity/LingerMonitor.java
@@ -0,0 +1,304 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.app.PendingIntent;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.net.NetworkCapabilities;
+import android.os.SystemClock;
+import android.os.UserHandle;
+import android.text.TextUtils;
+import android.text.format.DateUtils;
+import android.util.Log;
+import android.util.SparseArray;
+import android.util.SparseIntArray;
+import android.util.SparseBooleanArray;
+import java.util.Arrays;
+import java.util.HashMap;
+
+import com.android.internal.R;
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.MessageUtils;
+import com.android.server.connectivity.NetworkNotificationManager;
+import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
+
+import static android.net.ConnectivityManager.NETID_UNSET;
+
+/**
+ * Class that monitors default network linger events and possibly notifies the user of network
+ * switches.
+ *
+ * This class is not thread-safe and all its methods must be called on the ConnectivityService
+ * handler thread.
+ */
+public class LingerMonitor {
+
+    private static final boolean DBG = true;
+    private static final boolean VDBG = false;
+    private static final String TAG = LingerMonitor.class.getSimpleName();
+
+    public static final int DEFAULT_NOTIFICATION_DAILY_LIMIT = 3;
+    public static final long DEFAULT_NOTIFICATION_RATE_LIMIT_MILLIS = DateUtils.MINUTE_IN_MILLIS;
+
+    private static final HashMap<String, Integer> TRANSPORT_NAMES = makeTransportToNameMap();
+    @VisibleForTesting
+    public static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName(
+            "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
+
+    @VisibleForTesting
+    public static final int NOTIFY_TYPE_NONE         = 0;
+    public static final int NOTIFY_TYPE_NOTIFICATION = 1;
+    public static final int NOTIFY_TYPE_TOAST        = 2;
+
+    private static SparseArray<String> sNotifyTypeNames = MessageUtils.findMessageNames(
+            new Class[] { LingerMonitor.class }, new String[]{ "NOTIFY_TYPE_" });
+
+    private final Context mContext;
+    private final NetworkNotificationManager mNotifier;
+    private final int mDailyLimit;
+    private final long mRateLimitMillis;
+
+    private long mFirstNotificationMillis;
+    private long mLastNotificationMillis;
+    private int mNotificationCounter;
+
+    /** Current notifications. Maps the netId we switched away from to the netId we switched to. */
+    private final SparseIntArray mNotifications = new SparseIntArray();
+
+    /** Whether we ever notified that we switched away from a particular network. */
+    private final SparseBooleanArray mEverNotified = new SparseBooleanArray();
+
+    public LingerMonitor(Context context, NetworkNotificationManager notifier,
+            int dailyLimit, long rateLimitMillis) {
+        mContext = context;
+        mNotifier = notifier;
+        mDailyLimit = dailyLimit;
+        mRateLimitMillis = rateLimitMillis;
+    }
+
+    private static HashMap<String, Integer> makeTransportToNameMap() {
+        SparseArray<String> numberToName = MessageUtils.findMessageNames(
+            new Class[] { NetworkCapabilities.class }, new String[]{ "TRANSPORT_" });
+        HashMap<String, Integer> nameToNumber = new HashMap<>();
+        for (int i = 0; i < numberToName.size(); i++) {
+            // MessageUtils will fail to initialize if there are duplicate constant values, so there
+            // are no duplicates here.
+            nameToNumber.put(numberToName.valueAt(i), numberToName.keyAt(i));
+        }
+        return nameToNumber;
+    }
+
+    private static boolean hasTransport(NetworkAgentInfo nai, int transport) {
+        return nai.networkCapabilities.hasTransport(transport);
+    }
+
+    private int getNotificationSource(NetworkAgentInfo toNai) {
+        for (int i = 0; i < mNotifications.size(); i++) {
+            if (mNotifications.valueAt(i) == toNai.network.netId) {
+                return mNotifications.keyAt(i);
+            }
+        }
+        return NETID_UNSET;
+    }
+
+    private boolean everNotified(NetworkAgentInfo nai) {
+        return mEverNotified.get(nai.network.netId, false);
+    }
+
+    @VisibleForTesting
+    public boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
+        // TODO: Evaluate moving to CarrierConfigManager.
+        String[] notifySwitches =
+                mContext.getResources().getStringArray(R.array.config_networkNotifySwitches);
+
+        if (VDBG) {
+            Log.d(TAG, "Notify on network switches: " + Arrays.toString(notifySwitches));
+        }
+
+        for (String notifySwitch : notifySwitches) {
+            if (TextUtils.isEmpty(notifySwitch)) continue;
+            String[] transports = notifySwitch.split("-", 2);
+            if (transports.length != 2) {
+                Log.e(TAG, "Invalid network switch notification configuration: " + notifySwitch);
+                continue;
+            }
+            int fromTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[0]);
+            int toTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[1]);
+            if (hasTransport(fromNai, fromTransport) && hasTransport(toNai, toTransport)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    private void showNotification(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
+        mNotifier.showNotification(fromNai.network.netId, NotificationType.NETWORK_SWITCH,
+                fromNai, toNai, createNotificationIntent(), true);
+    }
+
+    @VisibleForTesting
+    protected PendingIntent createNotificationIntent() {
+        return PendingIntent.getActivityAsUser(mContext, 0, CELLULAR_SETTINGS,
+                PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
+    }
+
+    // Removes any notification that was put up as a result of switching to nai.
+    private void maybeStopNotifying(NetworkAgentInfo nai) {
+        int fromNetId = getNotificationSource(nai);
+        if (fromNetId != NETID_UNSET) {
+            mNotifications.delete(fromNetId);
+            mNotifier.clearNotification(fromNetId);
+            // Toasts can't be deleted.
+        }
+    }
+
+    // Notify the user of a network switch using a notification or a toast.
+    private void notify(NetworkAgentInfo fromNai, NetworkAgentInfo toNai, boolean forceToast) {
+        int notifyType =
+                mContext.getResources().getInteger(R.integer.config_networkNotifySwitchType);
+        if (notifyType == NOTIFY_TYPE_NOTIFICATION && forceToast) {
+            notifyType = NOTIFY_TYPE_TOAST;
+        }
+
+        if (VDBG) {
+            Log.d(TAG, "Notify type: " + sNotifyTypeNames.get(notifyType, "" + notifyType));
+        }
+
+        switch (notifyType) {
+            case NOTIFY_TYPE_NONE:
+                return;
+            case NOTIFY_TYPE_NOTIFICATION:
+                showNotification(fromNai, toNai);
+                break;
+            case NOTIFY_TYPE_TOAST:
+                mNotifier.showToast(fromNai, toNai);
+                break;
+            default:
+                Log.e(TAG, "Unknown notify type " + notifyType);
+                return;
+        }
+
+        if (DBG) {
+            Log.d(TAG, "Notifying switch from=" + fromNai.name() + " to=" + toNai.name() +
+                    " type=" + sNotifyTypeNames.get(notifyType, "unknown(" + notifyType + ")"));
+        }
+
+        mNotifications.put(fromNai.network.netId, toNai.network.netId);
+        mEverNotified.put(fromNai.network.netId, true);
+    }
+
+    // The default network changed from fromNai to toNai due to a change in score.
+    public void noteLingerDefaultNetwork(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
+        if (VDBG) {
+            Log.d(TAG, "noteLingerDefaultNetwork from=" + fromNai.name() +
+                    " everValidated=" + fromNai.everValidated +
+                    " lastValidated=" + fromNai.lastValidated +
+                    " to=" + toNai.name());
+        }
+
+        // If we are currently notifying the user because the device switched to fromNai, now that
+        // we are switching away from it we should remove the notification. This includes the case
+        // where we switch back to toNai because its score improved again (e.g., because it regained
+        // Internet access).
+        maybeStopNotifying(fromNai);
+
+        // If this network never validated, don't notify. Otherwise, we could do things like:
+        //
+        // 1. Unvalidated wifi connects.
+        // 2. Unvalidated mobile data connects.
+        // 3. Cell validates, and we show a notification.
+        // or:
+        // 1. User connects to wireless printer.
+        // 2. User turns on cellular data.
+        // 3. We show a notification.
+        if (!fromNai.everValidated) return;
+
+        // If this network is a captive portal, don't notify. This cannot happen on initial connect
+        // to a captive portal, because the everValidated check above will fail. However, it can
+        // happen if the captive portal reasserts itself (e.g., because its timeout fires). In that
+        // case, as soon as the captive portal reasserts itself, we'll show a sign-in notification.
+        // We don't want to overwrite that notification with this one; the user has already been
+        // notified, and of the two, the captive portal notification is the more useful one because
+        // it allows the user to sign in to the captive portal. In this case, display a toast
+        // in addition to the captive portal notification.
+        //
+        // Note that if the network we switch to is already up when the captive portal reappears,
+        // this won't work because NetworkMonitor tells ConnectivityService that the network is
+        // unvalidated (causing a switch) before asking it to show the sign in notification. In this
+        // case, the toast won't show and we'll only display the sign in notification. This is the
+        // best we can do at this time.
+        boolean forceToast = fromNai.networkCapabilities.hasCapability(
+                NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL);
+
+        // Only show the notification once, in order to avoid irritating the user every time.
+        // TODO: should we do this?
+        if (everNotified(fromNai)) {
+            if (VDBG) {
+                Log.d(TAG, "Not notifying handover from " + fromNai.name() + ", already notified");
+            }
+            return;
+        }
+
+        // Only show the notification if we switched away because a network became unvalidated, not
+        // because its score changed.
+        // TODO: instead of just skipping notification, keep a note of it, and show it if it becomes
+        // unvalidated.
+        if (fromNai.lastValidated) return;
+
+        if (!isNotificationEnabled(fromNai, toNai)) return;
+
+        final long now = SystemClock.elapsedRealtime();
+        if (isRateLimited(now) || isAboveDailyLimit(now)) return;
+
+        notify(fromNai, toNai, forceToast);
+    }
+
+    public void noteDisconnect(NetworkAgentInfo nai) {
+        mNotifications.delete(nai.network.netId);
+        mEverNotified.delete(nai.network.netId);
+        maybeStopNotifying(nai);
+        // No need to cancel notifications on nai: NetworkMonitor does that on disconnect.
+    }
+
+    private boolean isRateLimited(long now) {
+        final long millisSinceLast = now - mLastNotificationMillis;
+        if (millisSinceLast < mRateLimitMillis) {
+            return true;
+        }
+        mLastNotificationMillis = now;
+        return false;
+    }
+
+    private boolean isAboveDailyLimit(long now) {
+        if (mFirstNotificationMillis == 0) {
+            mFirstNotificationMillis = now;
+        }
+        final long millisSinceFirst = now - mFirstNotificationMillis;
+        if (millisSinceFirst > DateUtils.DAY_IN_MILLIS) {
+            mNotificationCounter = 0;
+            mFirstNotificationMillis = 0;
+        }
+        if (mNotificationCounter >= mDailyLimit) {
+            return true;
+        }
+        mNotificationCounter++;
+        return false;
+    }
+}
diff --git a/services/core/java/com/android/server/connectivity/MetricsLoggerService.java b/services/core/java/com/android/server/connectivity/MetricsLoggerService.java
index 3675b3e..1c9feb2 100644
--- a/services/core/java/com/android/server/connectivity/MetricsLoggerService.java
+++ b/services/core/java/com/android/server/connectivity/MetricsLoggerService.java
@@ -16,6 +16,7 @@
 
 package com.android.server.connectivity;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.server.SystemService;
 
 import android.app.PendingIntent;
@@ -55,22 +56,14 @@
             if (DBG) Log.d(TAG, "onBootPhase: PHASE_SYSTEM_SERVICES_READY");
             publishBinderService(ConnectivityMetricsLogger.CONNECTIVITY_METRICS_LOGGER_SERVICE,
                     mBinder);
-            mNetdListener = new NetdEventListenerService(getContext());
-            publishBinderService(mNetdListener.SERVICE_NAME, mNetdListener);
         }
     }
 
-    // TODO: read from system property
-    private final int MAX_NUMBER_OF_EVENTS = 1000;
-
-    // TODO: read from system property
-    private final int EVENTS_NOTIFICATION_THRESHOLD = 300;
-
-    // TODO: read from system property
-    private final int THROTTLING_TIME_INTERVAL_MILLIS = 60 * 60 * 1000; // 1 hour
-
-    // TODO: read from system property
+    // TODO: read these constants from system property
+    private final int EVENTS_NOTIFICATION_THRESHOLD                   = 300;
+    private final int MAX_NUMBER_OF_EVENTS                            = 1000;
     private final int THROTTLING_MAX_NUMBER_OF_MESSAGES_PER_COMPONENT = 1000;
+    private final long THROTTLING_TIME_INTERVAL_MILLIS                = DateUtils.HOUR_IN_MILLIS;
 
     private int mEventCounter = 0;
 
@@ -91,8 +84,6 @@
 
     private final ArrayDeque<ConnectivityMetricsEvent> mEvents = new ArrayDeque<>();
 
-    private NetdEventListenerService mNetdListener;
-
     private void enforceConnectivityInternalPermission() {
         getContext().enforceCallingOrSelfPermission(
                 android.Manifest.permission.CONNECTIVITY_INTERNAL,
@@ -127,10 +118,13 @@
         mEvents.addLast(e);
     }
 
+    @VisibleForTesting
+    final MetricsLoggerImpl mBinder = new MetricsLoggerImpl();
+
     /**
      * Implementation of the IConnectivityMetricsLogger interface.
      */
-    private final IConnectivityMetricsLogger.Stub mBinder = new IConnectivityMetricsLogger.Stub() {
+    final class MetricsLoggerImpl extends IConnectivityMetricsLogger.Stub {
 
         private final ArrayList<PendingIntent> mPendingIntents = new ArrayList<>();
 
@@ -221,9 +215,6 @@
                     }
                 }
             }
-
-            pw.println();
-            mNetdListener.dump(pw);
         }
 
         public long logEvent(ConnectivityMetricsEvent event) {
diff --git a/services/core/java/com/android/server/connectivity/NetdEventListenerService.java b/services/core/java/com/android/server/connectivity/NetdEventListenerService.java
index 98f34ca..21e5d3c 100644
--- a/services/core/java/com/android/server/connectivity/NetdEventListenerService.java
+++ b/services/core/java/com/android/server/connectivity/NetdEventListenerService.java
@@ -17,15 +17,17 @@
 package com.android.server.connectivity;
 
 import android.content.Context;
-import android.net.metrics.DnsEvent;
 import android.net.ConnectivityManager;
 import android.net.ConnectivityManager.NetworkCallback;
 import android.net.Network;
 import android.net.NetworkRequest;
+import android.net.metrics.DnsEvent;
 import android.net.metrics.INetdEventListener;
+import android.net.metrics.IpConnectivityLog;
 import android.util.Log;
 
 import com.android.internal.annotations.GuardedBy;
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.IndentingPrintWriter;
 
 import java.io.PrintWriter;
@@ -45,12 +47,13 @@
     private static final boolean DBG = true;
     private static final boolean VDBG = false;
 
+    // TODO: read this constant from system property
     private static final int MAX_LOOKUPS_PER_DNS_EVENT = 100;
 
     // Stores the results of a number of consecutive DNS lookups on the same network.
     // This class is not thread-safe and it is the responsibility of the service to call its methods
     // on one thread at a time.
-    private static class DnsEventBatch {
+    private class DnsEventBatch {
         private final int mNetId;
 
         private final byte[] mEventTypes = new byte[MAX_LOOKUPS_PER_DNS_EVENT];
@@ -82,7 +85,7 @@
             byte[] eventTypes = Arrays.copyOf(mEventTypes, mEventCount);
             byte[] returnCodes = Arrays.copyOf(mReturnCodes, mEventCount);
             int[] latenciesMs = Arrays.copyOf(mLatenciesMs, mEventCount);
-            DnsEvent.logEvent(mNetId, eventTypes, returnCodes, latenciesMs);
+            mMetricsLog.log(new DnsEvent(mNetId, eventTypes, returnCodes, latenciesMs));
             maybeLog(String.format("Logging %d results for netId %d", mEventCount, mNetId));
             mEventCount = 0;
         }
@@ -96,13 +99,14 @@
     // Only sorted for ease of debugging. Because we only typically have a handful of networks up
     // at any given time, performance is not a concern.
     @GuardedBy("this")
-    private SortedMap<Integer, DnsEventBatch> mEventBatches = new TreeMap<>();
+    private final SortedMap<Integer, DnsEventBatch> mEventBatches = new TreeMap<>();
 
     // We register a NetworkCallback to ensure that when a network disconnects, we flush the DNS
     // queries we've logged on that network. Because we do not do this periodically, we might lose
     // up to MAX_LOOKUPS_PER_DNS_EVENT lookup stats on each network when the system is shutting
     // down. We believe this to be sufficient for now.
     private final ConnectivityManager mCm;
+    private final IpConnectivityLog mMetricsLog;
     private final NetworkCallback mNetworkCallback = new NetworkCallback() {
         @Override
         public void onLost(Network network) {
@@ -116,11 +120,15 @@
     };
 
     public NetdEventListenerService(Context context) {
+        this(context.getSystemService(ConnectivityManager.class), new IpConnectivityLog());
+    }
+
+    @VisibleForTesting
+    public NetdEventListenerService(ConnectivityManager cm, IpConnectivityLog log) {
         // We are started when boot is complete, so ConnectivityService should already be running.
-        final NetworkRequest request = new NetworkRequest.Builder()
-            .clearCapabilities()
-            .build();
-        mCm = context.getSystemService(ConnectivityManager.class);
+        mCm = cm;
+        mMetricsLog = log;
+        final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
         mCm.registerNetworkCallback(request, mNetworkCallback);
     }
 
diff --git a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java
index d487bd0..2a618bc 100644
--- a/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java
+++ b/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java
@@ -28,14 +28,21 @@
 import android.net.NetworkState;
 import android.os.Handler;
 import android.os.Messenger;
+import android.os.SystemClock;
+import android.util.Log;
 import android.util.SparseArray;
 
 import com.android.internal.util.AsyncChannel;
+import com.android.internal.util.WakeupMessage;
 import com.android.server.ConnectivityService;
 import com.android.server.connectivity.NetworkMonitor;
 
+import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Comparator;
+import java.util.Objects;
+import java.util.SortedSet;
+import java.util.TreeSet;
 
 /**
  * A bag class used by ConnectivityService for holding a collection of most recent
@@ -97,15 +104,18 @@
 // -----------------------------------------------
 // If a network has no chance of satisfying any requests (even if it were to become validated
 // and enter state #5), ConnectivityService will disconnect the NetworkAgent's AsyncChannel.
-// If the network ever for any period of time had satisfied a NetworkRequest (i.e. had been
-// the highest scoring that satisfied the NetworkRequest's constraints), but is no longer the
-// highest scoring network for any NetworkRequest, then there will be a 30s pause before
-// ConnectivityService disconnects the NetworkAgent's AsyncChannel.  During this pause the
-// network is considered "lingering".  This pause exists to allow network communication to be
-// wrapped up rather than abruptly terminated.  During this pause if the network begins satisfying
-// a NetworkRequest, ConnectivityService will cancel the future disconnection of the NetworkAgent's
-// AsyncChannel, and the network is no longer considered "lingering".
+//
+// If the network was satisfying a foreground NetworkRequest (i.e. had been the highest scoring that
+// satisfied the NetworkRequest's constraints), but is no longer the highest scoring network for any
+// foreground NetworkRequest, then there will be a 30s pause to allow network communication to be
+// wrapped up rather than abruptly terminated. During this pause the network is said to be
+// "lingering". During this pause if the network begins satisfying a foreground NetworkRequest,
+// ConnectivityService will cancel the future disconnection of the NetworkAgent's AsyncChannel, and
+// the network is no longer considered "lingering". After the linger timer expires, if the network
+// is satisfying one or more background NetworkRequests it is kept up in the background. If it is
+// not, ConnectivityService disconnects the NetworkAgent's AsyncChannel.
 public class NetworkAgentInfo implements Comparable<NetworkAgentInfo> {
+
     public NetworkInfo networkInfo;
     // This Network object should always be used if possible, so as to encourage reuse of the
     // enclosed socket factory and connection pool.  Avoid creating other Network objects.
@@ -130,12 +140,14 @@
     public boolean everValidated;
 
     // The result of the last validation attempt on this network (true if validated, false if not).
-    // This bit exists only because we never unvalidate a network once it's been validated, and that
-    // is because the network scoring and revalidation code does not (may not?) deal properly with
-    // networks becoming unvalidated.
-    // TODO: Fix the network scoring code, remove this, and rename everValidated to validated.
     public boolean lastValidated;
 
+    // If true, becoming unvalidated will lower the network's score. This is only meaningful if the
+    // system is configured not to do this for certain networks, e.g., if the
+    // config_networkAvoidBadWifi option is set to 0 and the user has not overridden that via
+    // Settings.Global.NETWORK_AVOID_BAD_WIFI.
+    public boolean avoidUnvalidated;
+
     // Whether a captive portal was ever detected on this network.
     // This is a sticky bit; once set it is never cleared.
     public boolean everCaptivePortalDetected;
@@ -143,12 +155,69 @@
     // Whether a captive portal was found during the last network validation attempt.
     public boolean lastCaptivePortalDetected;
 
-    // Indicates whether the network is lingering.  Networks are lingered when they become unneeded
-    // as a result of their NetworkRequests being satisfied by a different network, so as to allow
-    // communication to wrap up before the network is taken down.  This usually only happens to the
-    // default network.  Lingering ends with either the linger timeout expiring and the network
-    // being taken down, or the network satisfying a request again.
-    public boolean lingering;
+    // Networks are lingered when they become unneeded as a result of their NetworkRequests being
+    // satisfied by a higher-scoring network. so as to allow communication to wrap up before the
+    // network is taken down.  This usually only happens to the default network. Lingering ends with
+    // either the linger timeout expiring and the network being taken down, or the network
+    // satisfying a request again.
+    public static class LingerTimer implements Comparable<LingerTimer> {
+        public final NetworkRequest request;
+        public final long expiryMs;
+
+        public LingerTimer(NetworkRequest request, long expiryMs) {
+            this.request = request;
+            this.expiryMs = expiryMs;
+        }
+        public boolean equals(Object o) {
+            if (!(o instanceof LingerTimer)) return false;
+            LingerTimer other = (LingerTimer) o;
+            return (request.requestId == other.request.requestId) && (expiryMs == other.expiryMs);
+        }
+        public int hashCode() {
+            return Objects.hash(request.requestId, expiryMs);
+        }
+        public int compareTo(LingerTimer other) {
+            return (expiryMs != other.expiryMs) ?
+                    Long.compare(expiryMs, other.expiryMs) :
+                    Integer.compare(request.requestId, other.request.requestId);
+        }
+        public String toString() {
+            return String.format("%s, expires %dms", request.toString(),
+                    expiryMs - SystemClock.elapsedRealtime());
+        }
+    }
+
+    /**
+     * Inform ConnectivityService that the network LINGER period has
+     * expired.
+     * obj = this NetworkAgentInfo
+     */
+    public static final int EVENT_NETWORK_LINGER_COMPLETE = 1001;
+
+    // All linger timers for this network, sorted by expiry time. A linger timer is added whenever
+    // a request is moved to a network with a better score, regardless of whether the network is or
+    // was lingering or not.
+    // TODO: determine if we can replace this with a smaller or unsorted data structure. (e.g.,
+    // SparseLongArray) combined with the timestamp of when the last timer is scheduled to fire.
+    private final SortedSet<LingerTimer> mLingerTimers = new TreeSet<>();
+
+    // For fast lookups. Indexes into mLingerTimers by request ID.
+    private final SparseArray<LingerTimer> mLingerTimerForRequest = new SparseArray<>();
+
+    // Linger expiry timer. Armed whenever mLingerTimers is non-empty, regardless of whether the
+    // network is lingering or not. Always set to the expiry of the LingerTimer that expires last.
+    // When the timer fires, all linger state is cleared, and if the network has no requests, it is
+    // torn down.
+    private WakeupMessage mLingerMessage;
+
+    // Linger expiry. Holds the expiry time of the linger timer, or 0 if the timer is not armed.
+    private long mLingerExpiryMs;
+
+    // Whether the network is lingering or not. Must be maintained separately from the above because
+    // it depends on the state of other networks and requests, which only ConnectivityService knows.
+    // (Example: we don't linger a network if it would become the best for a NetworkRequest if it
+    // validated).
+    private boolean mLingering;
 
     // This represents the last score received from the NetworkAgent.
     private int currentScore;
@@ -162,11 +231,13 @@
     private static final int MAXIMUM_NETWORK_SCORE = 100;
 
     // The list of NetworkRequests being satisfied by this Network.
-    public final SparseArray<NetworkRequest> networkRequests = new SparseArray<NetworkRequest>();
-    // The list of NetworkRequests that this Network previously satisfied with the highest
-    // score.  A non-empty list indicates that if this Network was validated it is lingered.
-    // NOTE: This list is only used for debugging.
-    public final ArrayList<NetworkRequest> networkLingered = new ArrayList<NetworkRequest>();
+    private final SparseArray<NetworkRequest> mNetworkRequests = new SparseArray<>();
+
+    // How many of the satisfied requests are actual requests and not listens.
+    private int mNumRequestNetworkRequests = 0;
+
+    // How many of the satisfied requests are of type BACKGROUND_REQUEST.
+    private int mNumBackgroundNetworkRequests = 0;
 
     public final Messenger messenger;
     public final AsyncChannel asyncChannel;
@@ -174,6 +245,12 @@
     // Used by ConnectivityService to keep track of 464xlat.
     public Nat464Xlat clatd;
 
+    private static final String TAG = ConnectivityService.class.getSimpleName();
+    private static final boolean VDBG = false;
+    private final ConnectivityService mConnService;
+    private final Context mContext;
+    private final Handler mHandler;
+
     public NetworkAgentInfo(Messenger messenger, AsyncChannel ac, Network net, NetworkInfo info,
             LinkProperties lp, NetworkCapabilities nc, int score, Context context, Handler handler,
             NetworkMisc misc, NetworkRequest defaultRequest, ConnectivityService connService) {
@@ -184,22 +261,131 @@
         linkProperties = lp;
         networkCapabilities = nc;
         currentScore = score;
-        networkMonitor = connService.createNetworkMonitor(context, handler, this, defaultRequest);
+        mConnService = connService;
+        mContext = context;
+        mHandler = handler;
+        networkMonitor = mConnService.createNetworkMonitor(context, handler, this, defaultRequest);
         networkMisc = misc;
     }
 
+    // Functions for manipulating the requests satisfied by this network.
+    //
+    // These functions must only called on ConnectivityService's main thread.
+
+    private static final boolean ADD = true;
+    private static final boolean REMOVE = false;
+
+    private void updateRequestCounts(boolean add, NetworkRequest request) {
+        int delta = add ? +1 : -1;
+        switch (request.type) {
+            case REQUEST:
+            case TRACK_DEFAULT:
+                mNumRequestNetworkRequests += delta;
+                break;
+
+            case BACKGROUND_REQUEST:
+                mNumRequestNetworkRequests += delta;
+                mNumBackgroundNetworkRequests += delta;
+                break;
+
+            case LISTEN:
+                break;
+
+            case NONE:
+            default:
+                Log.wtf(TAG, "Unhandled request type " + request.type);
+                break;
+        }
+    }
+
     /**
      * Add {@code networkRequest} to this network as it's satisfied by this network.
-     * NOTE: This function must only be called on ConnectivityService's main thread.
      * @return true if {@code networkRequest} was added or false if {@code networkRequest} was
      *         already present.
      */
     public boolean addRequest(NetworkRequest networkRequest) {
-        if (networkRequests.get(networkRequest.requestId) == networkRequest) return false;
-        networkRequests.put(networkRequest.requestId, networkRequest);
+        NetworkRequest existing = mNetworkRequests.get(networkRequest.requestId);
+        if (existing == networkRequest) return false;
+        if (existing != null) {
+            // Should only happen if the requestId wraps. If that happens lots of other things will
+            // be broken as well.
+            Log.wtf(TAG, String.format("Duplicate requestId for %s and %s on %s",
+                    networkRequest, existing, name()));
+            updateRequestCounts(REMOVE, existing);
+        }
+        mNetworkRequests.put(networkRequest.requestId, networkRequest);
+        updateRequestCounts(ADD, networkRequest);
         return true;
     }
 
+    /**
+     * Remove the specified request from this network.
+     */
+    public void removeRequest(int requestId) {
+        NetworkRequest existing = mNetworkRequests.get(requestId);
+        if (existing == null) return;
+        updateRequestCounts(REMOVE, existing);
+        mNetworkRequests.remove(requestId);
+        if (existing.isRequest()) {
+            unlingerRequest(existing);
+        }
+    }
+
+    /**
+     * Returns whether this network is currently satisfying the request with the specified ID.
+     */
+    public boolean isSatisfyingRequest(int id) {
+        return mNetworkRequests.get(id) != null;
+    }
+
+    /**
+     * Returns the request at the specified position in the list of requests satisfied by this
+     * network.
+     */
+    public NetworkRequest requestAt(int index) {
+        return mNetworkRequests.valueAt(index);
+    }
+
+    /**
+     * Returns the number of requests currently satisfied by this network for which
+     * {@link android.net.NetworkRequest#isRequest} returns {@code true}.
+     */
+    public int numRequestNetworkRequests() {
+        return mNumRequestNetworkRequests;
+    }
+
+    /**
+     * Returns the number of requests currently satisfied by this network of type
+     * {@link android.net.NetworkRequest.Type.BACKGROUND_REQUEST}.
+     */
+    public int numBackgroundNetworkRequests() {
+        return mNumBackgroundNetworkRequests;
+    }
+
+    /**
+     * Returns the number of foreground requests currently satisfied by this network.
+     */
+    public int numForegroundNetworkRequests() {
+        return mNumRequestNetworkRequests - mNumBackgroundNetworkRequests;
+    }
+
+    /**
+     * Returns the number of requests of any type currently satisfied by this network.
+     */
+    public int numNetworkRequests() {
+        return mNetworkRequests.size();
+    }
+
+    /**
+     * Returns whether the network is a background network. A network is a background network if it
+     * is satisfying no foreground requests and at least one background request. (If it did not have
+     * a background request, it would be a speculative network that is only being kept up because
+     * it might satisfy a request if it validated).
+     */
+    public boolean isBackgroundNetwork() {
+        return !isVPN() && numForegroundNetworkRequests() == 0 && mNumBackgroundNetworkRequests > 0;
+    }
+
     // Does this network satisfy request?
     public boolean satisfies(NetworkRequest request) {
         return created &&
@@ -232,16 +418,22 @@
         }
 
         int score = currentScore;
-        // Use NET_CAPABILITY_VALIDATED here instead of lastValidated, this allows
-        // ConnectivityService.updateCapabilities() to compute the old score prior to updating
-        // networkCapabilities (with a potentially different validated state).
-        if (!networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED) && !pretendValidated) {
+        if (!lastValidated && !pretendValidated && !ignoreWifiUnvalidationPenalty()) {
             score -= UNVALIDATED_SCORE_PENALTY;
         }
         if (score < 0) score = 0;
         return score;
     }
 
+    // Return true on devices configured to ignore score penalty for wifi networks
+    // that become unvalidated (b/31075769).
+    private boolean ignoreWifiUnvalidationPenalty() {
+        boolean isWifi = networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) &&
+                networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+        boolean avoidBadWifi = mConnService.avoidBadWifi() || avoidUnvalidated;
+        return isWifi && !avoidBadWifi && everValidated;
+    }
+
     // Get the current score for this Network.  This may be modified from what the
     // NetworkAgent sent, as it has modifiers applied to it.
     public int getCurrentScore() {
@@ -269,13 +461,103 @@
         }
     }
 
+    /**
+     * Sets the specified request to linger on this network for the specified time. Called by
+     * ConnectivityService when the request is moved to another network with a higher score.
+     */
+    public void lingerRequest(NetworkRequest request, long now, long duration) {
+        if (mLingerTimerForRequest.get(request.requestId) != null) {
+            // Cannot happen. Once a request is lingering on a particular network, we cannot
+            // re-linger it unless that network becomes the best for that request again, in which
+            // case we should have unlingered it.
+            Log.wtf(TAG, this.name() + ": request " + request.requestId + " already lingered");
+        }
+        final long expiryMs = now + duration;
+        LingerTimer timer = new LingerTimer(request, expiryMs);
+        if (VDBG) Log.d(TAG, "Adding LingerTimer " + timer + " to " + this.name());
+        mLingerTimers.add(timer);
+        mLingerTimerForRequest.put(request.requestId, timer);
+    }
+
+    /**
+     * Cancel lingering. Called by ConnectivityService when a request is added to this network.
+     * Returns true if the given request was lingering on this network, false otherwise.
+     */
+    public boolean unlingerRequest(NetworkRequest request) {
+        LingerTimer timer = mLingerTimerForRequest.get(request.requestId);
+        if (timer != null) {
+            if (VDBG) Log.d(TAG, "Removing LingerTimer " + timer + " from " + this.name());
+            mLingerTimers.remove(timer);
+            mLingerTimerForRequest.remove(request.requestId);
+            return true;
+        }
+        return false;
+    }
+
+    public long getLingerExpiry() {
+        return mLingerExpiryMs;
+    }
+
+    public void updateLingerTimer() {
+        long newExpiry = mLingerTimers.isEmpty() ? 0 : mLingerTimers.last().expiryMs;
+        if (newExpiry == mLingerExpiryMs) return;
+
+        // Even if we're going to reschedule the timer, cancel it first. This is because the
+        // semantics of WakeupMessage guarantee that if cancel is called then the alarm will
+        // never call its callback (handleLingerComplete), even if it has already fired.
+        // WakeupMessage makes no such guarantees about rescheduling a message, so if mLingerMessage
+        // has already been dispatched, rescheduling to some time in the future it won't stop it
+        // from calling its callback immediately.
+        if (mLingerMessage != null) {
+            mLingerMessage.cancel();
+            mLingerMessage = null;
+        }
+
+        if (newExpiry > 0) {
+            mLingerMessage = mConnService.makeWakeupMessage(
+                    mContext, mHandler,
+                    "NETWORK_LINGER_COMPLETE." + network.netId,
+                    EVENT_NETWORK_LINGER_COMPLETE, this);
+            mLingerMessage.schedule(newExpiry);
+        }
+
+        mLingerExpiryMs = newExpiry;
+    }
+
+    public void linger() {
+        mLingering = true;
+    }
+
+    public void unlinger() {
+        mLingering = false;
+    }
+
+    public boolean isLingering() {
+        return mLingering;
+    }
+
+    public void clearLingerState() {
+        if (mLingerMessage != null) {
+            mLingerMessage.cancel();
+            mLingerMessage = null;
+        }
+        mLingerTimers.clear();
+        mLingerTimerForRequest.clear();
+        updateLingerTimer();  // Sets mLingerExpiryMs, cancels and nulls out mLingerMessage.
+        mLingering = false;
+    }
+
+    public void dumpLingerTimers(PrintWriter pw) {
+        for (LingerTimer timer : mLingerTimers) { pw.println(timer); }
+    }
+
     public String toString() {
         return "NetworkAgentInfo{ ni{" + networkInfo + "}  " +
                 "network{" + network + "}  nethandle{" + network.getNetworkHandle() + "}  " +
                 "lp{" + linkProperties + "}  " +
                 "nc{" + networkCapabilities + "}  Score{" + getCurrentScore() + "}  " +
                 "everValidated{" + everValidated + "}  lastValidated{" + lastValidated + "}  " +
-                "created{" + created + "} lingering{" + lingering + "} " +
+                "created{" + created + "} lingering{" + isLingering() + "} " +
                 "explicitlySelected{" + networkMisc.explicitlySelected + "} " +
                 "acceptUnvalidated{" + networkMisc.acceptUnvalidated + "} " +
                 "everCaptivePortalDetected{" + everCaptivePortalDetected + "} " +
diff --git a/services/core/java/com/android/server/connectivity/NetworkMonitor.java b/services/core/java/com/android/server/connectivity/NetworkMonitor.java
index f4e1424..6eb89fa 100644
--- a/services/core/java/com/android/server/connectivity/NetworkMonitor.java
+++ b/services/core/java/com/android/server/connectivity/NetworkMonitor.java
@@ -23,7 +23,6 @@
 import android.app.AlarmManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
-import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -34,16 +33,15 @@
 import android.net.ProxyInfo;
 import android.net.TrafficStats;
 import android.net.Uri;
-import android.net.metrics.ValidationProbeEvent;
+import android.net.metrics.IpConnectivityLog;
 import android.net.metrics.NetworkEvent;
+import android.net.metrics.ValidationProbeEvent;
+import android.net.util.Stopwatch;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
-import android.net.util.Stopwatch;
 import android.os.Handler;
 import android.os.Message;
-import android.os.Process;
 import android.os.SystemClock;
-import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.telephony.CellIdentityCdma;
@@ -65,28 +63,39 @@
 import com.android.internal.util.Protocol;
 import com.android.internal.util.State;
 import com.android.internal.util.StateMachine;
-import com.android.internal.util.WakeupMessage;
-import com.android.server.connectivity.NetworkAgentInfo;
 
 import java.io.IOException;
 import java.net.HttpURLConnection;
 import java.net.InetAddress;
 import java.net.MalformedURLException;
-import java.net.UnknownHostException;
 import java.net.URL;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.atomic.AtomicReference;
+import java.net.UnknownHostException;
 import java.util.List;
 import java.util.Random;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 
 /**
  * {@hide}
  */
 public class NetworkMonitor extends StateMachine {
-    private static final boolean DBG = false;
     private static final String TAG = NetworkMonitor.class.getSimpleName();
-    private static final String DEFAULT_SERVER = "connectivitycheck.gstatic.com";
+    private static final boolean DBG = false;
+
+    // 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.
+    private static final String DEFAULT_HTTPS_URL     = "https://www.google.com/generate_204";
+    private static final String DEFAULT_HTTP_URL      =
+            "http://connectivitycheck.gstatic.com/generate_204";
+    private static final String DEFAULT_FALLBACK_URL  = "http://www.google.com/gen_204";
+    private static final String DEFAULT_USER_AGENT    = "Mozilla/5.0 (X11; Linux x86_64) "
+                                                      + "AppleWebKit/537.36 (KHTML, like Gecko) "
+                                                      + "Chrome/52.0.2743.82 Safari/537.36";
+
     private static final int SOCKET_TIMEOUT_MS = 10000;
+    private static final int PROBE_TIMEOUT_MS  = 3000;
+
     public static final String ACTION_NETWORK_CONDITIONS_MEASURED =
             "android.net.conn.NETWORK_CONDITIONS_MEASURED";
     public static final String EXTRA_CONNECTIVITY_TYPE = "extra_connectivity_type";
@@ -132,31 +141,6 @@
     public static final int EVENT_NETWORK_TESTED = BASE + 2;
 
     /**
-     * Inform NetworkMonitor to linger a network.  The Monitor should
-     * start a timer and/or start watching for zero live connections while
-     * moving towards LINGER_COMPLETE.  After the Linger period expires
-     * (or other events mark the end of the linger state) the LINGER_COMPLETE
-     * event should be sent and the network will be shut down.  If a
-     * CMD_NETWORK_CONNECTED happens before the LINGER completes
-     * it indicates further desire to keep the network alive and so
-     * the LINGER is aborted.
-     */
-    public static final int CMD_NETWORK_LINGER = BASE + 3;
-
-    /**
-     * Message to self indicating linger delay has expired.
-     * arg1 = Token to ignore old messages.
-     */
-    private static final int CMD_LINGER_EXPIRED = BASE + 4;
-
-    /**
-     * Inform ConnectivityService that the network LINGER period has
-     * expired.
-     * obj = NetworkAgentInfo
-     */
-    public static final int EVENT_NETWORK_LINGER_COMPLETE = BASE + 5;
-
-    /**
      * Message to self indicating it's time to evaluate a network's connectivity.
      * arg1 = Token to ignore old messages.
      */
@@ -204,12 +188,6 @@
      */
     private static final int CMD_CAPTIVE_PORTAL_RECHECK = BASE + 12;
 
-    private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger";
-    // Default to 30s linger time-out.  Modifyable only for testing.
-    private static int DEFAULT_LINGER_DELAY_MS = 30000;
-    private final int mLingerDelayMs;
-    private int mLingerToken = 0;
-
     // Start mReevaluateDelayMs at this value and double.
     private static final int INITIAL_REEVALUATE_DELAY_MS = 1000;
     private static final int MAX_REEVALUATE_DELAY_MS = 10*60*1000;
@@ -231,6 +209,7 @@
     private final WifiManager mWifiManager;
     private final AlarmManager mAlarmManager;
     private final NetworkRequest mDefaultRequest;
+    private final IpConnectivityLog mMetricsLog;
 
     private boolean mIsCaptivePortalCheckEnabled;
     private boolean mUseHttps;
@@ -247,7 +226,6 @@
     private final State mMaybeNotifyState = new MaybeNotifyState();
     private final State mEvaluatingState = new EvaluatingState();
     private final State mCaptivePortalState = new CaptivePortalState();
-    private final State mLingeringState = new LingeringState();
 
     private CustomIntentReceiver mLaunchCaptivePortalAppBroadcastReceiver = null;
 
@@ -255,12 +233,22 @@
 
     private final Stopwatch mEvaluationTimer = new Stopwatch();
 
+    // This variable is set before transitioning to the mCaptivePortalState.
+    private CaptivePortalProbeResult mLastPortalProbeResult = CaptivePortalProbeResult.FAILED;
+
     public NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo,
             NetworkRequest defaultRequest) {
+        this(context, handler, networkAgentInfo, defaultRequest, new IpConnectivityLog());
+    }
+
+    @VisibleForTesting
+    protected NetworkMonitor(Context context, Handler handler, NetworkAgentInfo networkAgentInfo,
+            NetworkRequest defaultRequest, IpConnectivityLog logger) {
         // Add suffix indicating which NetworkMonitor we're talking about.
         super(TAG + networkAgentInfo.name());
 
         mContext = context;
+        mMetricsLog = logger;
         mConnectivityServiceHandler = handler;
         mNetworkAgentInfo = networkAgentInfo;
         mNetId = mNetworkAgentInfo.network.netId;
@@ -274,11 +262,8 @@
         addState(mMaybeNotifyState, mDefaultState);
             addState(mEvaluatingState, mMaybeNotifyState);
             addState(mCaptivePortalState, mMaybeNotifyState);
-        addState(mLingeringState, mDefaultState);
         setInitialState(mDefaultState);
 
-        mLingerDelayMs = SystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS);
-
         mIsCaptivePortalCheckEnabled = Settings.Global.getInt(mContext.getContentResolver(),
                 Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED, 1) == 1;
         mUseHttps = Settings.Global.getInt(mContext.getContentResolver(),
@@ -307,16 +292,12 @@
         @Override
         public boolean processMessage(Message message) {
             switch (message.what) {
-                case CMD_NETWORK_LINGER:
-                    log("Lingering");
-                    transitionTo(mLingeringState);
-                    return HANDLED;
                 case CMD_NETWORK_CONNECTED:
-                    NetworkEvent.logEvent(mNetId, NetworkEvent.NETWORK_CONNECTED);
+                    logNetworkEvent(NetworkEvent.NETWORK_CONNECTED);
                     transitionTo(mEvaluatingState);
                     return HANDLED;
                 case CMD_NETWORK_DISCONNECTED:
-                    NetworkEvent.logEvent(mNetId, NetworkEvent.NETWORK_DISCONNECTED);
+                    logNetworkEvent(NetworkEvent.NETWORK_DISCONNECTED);
                     if (mLaunchCaptivePortalAppBroadcastReceiver != null) {
                         mContext.unregisterReceiver(mLaunchCaptivePortalAppBroadcastReceiver);
                         mLaunchCaptivePortalAppBroadcastReceiver = null;
@@ -381,10 +362,7 @@
     private class ValidatedState extends State {
         @Override
         public void enter() {
-            if (mEvaluationTimer.isRunning()) {
-                NetworkEvent.logValidated(mNetId, mEvaluationTimer.stop());
-                mEvaluationTimer.reset();
-            }
+            maybeLogEvaluationResult(NetworkEvent.NETWORK_VALIDATED);
             mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
                     NETWORK_TEST_RESULT_VALID, mNetworkAgentInfo.network.netId, null));
         }
@@ -423,6 +401,8 @@
                                     sendMessage(CMD_CAPTIVE_PORTAL_APP_FINISHED, response);
                                 }
                             }));
+                    intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL,
+                            mLastPortalProbeResult.detectUrl);
                     intent.setFlags(
                             Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                     mContext.startActivityAsUser(intent, UserHandle.CURRENT);
@@ -446,14 +426,22 @@
      */
     @VisibleForTesting
     public static final class CaptivePortalProbeResult {
-        static final CaptivePortalProbeResult FAILED = new CaptivePortalProbeResult(599, null);
+        static final CaptivePortalProbeResult FAILED = new CaptivePortalProbeResult(599);
 
-        final int mHttpResponseCode; // HTTP response code returned from Internet probe.
-        final String mRedirectUrl;   // Redirect destination returned from Internet probe.
+        private final int mHttpResponseCode;  // HTTP response code returned from Internet probe.
+        final String redirectUrl;             // Redirect destination returned from Internet probe.
+        final String detectUrl;               // URL where a 204 response code indicates
+                                              // captive portal has been appeased.
 
-        public CaptivePortalProbeResult(int httpResponseCode, String redirectUrl) {
+        public CaptivePortalProbeResult(
+                int httpResponseCode, String redirectUrl, String detectUrl) {
             mHttpResponseCode = httpResponseCode;
-            mRedirectUrl = redirectUrl;
+            this.redirectUrl = redirectUrl;
+            this.detectUrl = detectUrl;
+        }
+
+        public CaptivePortalProbeResult(int httpResponseCode) {
+            this(httpResponseCode, null, null);
         }
 
         boolean isSuccessful() { return mHttpResponseCode == 204; }
@@ -526,15 +514,16 @@
                         transitionTo(mValidatedState);
                     } else if (probeResult.isPortal()) {
                         mConnectivityServiceHandler.sendMessage(obtainMessage(EVENT_NETWORK_TESTED,
-                                NETWORK_TEST_RESULT_INVALID, mNetId, probeResult.mRedirectUrl));
+                                NETWORK_TEST_RESULT_INVALID, mNetId, probeResult.redirectUrl));
+                        mLastPortalProbeResult = probeResult;
                         transitionTo(mCaptivePortalState);
                     } else {
                         final Message msg = obtainMessage(CMD_REEVALUATE, ++mReevaluateToken, 0);
                         sendMessageDelayed(msg, mReevaluateDelayMs);
-                        NetworkEvent.logEvent(mNetId, NetworkEvent.NETWORK_VALIDATION_FAILED);
+                        logNetworkEvent(NetworkEvent.NETWORK_VALIDATION_FAILED);
                         mConnectivityServiceHandler.sendMessage(obtainMessage(
                                 EVENT_NETWORK_TESTED, NETWORK_TEST_RESULT_INVALID, mNetId,
-                                probeResult.mRedirectUrl));
+                                probeResult.redirectUrl));
                         if (mAttempts >= BLAME_FOR_EVALUATION_ATTEMPTS) {
                             // Don't continue to blame UID forever.
                             TrafficStats.clearThreadStatsUid();
@@ -591,10 +580,7 @@
 
         @Override
         public void enter() {
-            if (mEvaluationTimer.isRunning()) {
-                NetworkEvent.logCaptivePortalFound(mNetId, mEvaluationTimer.stop());
-                mEvaluationTimer.reset();
-            }
+            maybeLogEvaluationResult(NetworkEvent.NETWORK_CAPTIVE_PORTAL_FOUND);
             // Don't annoy user with sign-in notifications.
             if (mDontDisplaySigninNotification) return;
             // Create a CustomIntentReceiver that sends us a
@@ -618,92 +604,37 @@
 
         @Override
         public void exit() {
-             removeMessages(CMD_CAPTIVE_PORTAL_RECHECK);
+            removeMessages(CMD_CAPTIVE_PORTAL_RECHECK);
         }
     }
 
-    // Being in the LingeringState State indicates a Network's validated bit is true and it once
-    // was the highest scoring Network satisfying a particular NetworkRequest, but since then
-    // another Network satisfied the NetworkRequest with a higher score and hence this Network
-    // is "lingered" for a fixed period of time before it is disconnected.  This period of time
-    // allows apps to wrap up communication and allows for seamless reactivation if the other
-    // higher scoring Network happens to disconnect.
-    private class LingeringState extends State {
-        private static final String ACTION_LINGER_EXPIRED = "android.net.netmon.lingerExpired";
-
-        private WakeupMessage mWakeupMessage;
-
-        @Override
-        public void enter() {
-            mEvaluationTimer.reset();
-            final String cmdName = ACTION_LINGER_EXPIRED + "." + mNetId;
-            mWakeupMessage = makeWakeupMessage(mContext, getHandler(), cmdName, CMD_LINGER_EXPIRED);
-            long wakeupTime = SystemClock.elapsedRealtime() + mLingerDelayMs;
-            mWakeupMessage.schedule(wakeupTime);
-        }
-
-        @Override
-        public boolean processMessage(Message message) {
-            switch (message.what) {
-                case CMD_NETWORK_CONNECTED:
-                    log("Unlingered");
-                    // If already validated, go straight to validated state.
-                    if (mNetworkAgentInfo.lastValidated) {
-                        transitionTo(mValidatedState);
-                        return HANDLED;
-                    }
-                    return NOT_HANDLED;
-                case CMD_LINGER_EXPIRED:
-                    mConnectivityServiceHandler.sendMessage(
-                            obtainMessage(EVENT_NETWORK_LINGER_COMPLETE, mNetworkAgentInfo));
-                    return HANDLED;
-                case CMD_FORCE_REEVALUATION:
-                    // Ignore reevaluation attempts when lingering.  A reevaluation could result
-                    // in a transition to the validated state which would abort the linger
-                    // timeout.  Lingering is the result of score assessment; validity is
-                    // irrelevant.
-                    return HANDLED;
-                case CMD_CAPTIVE_PORTAL_APP_FINISHED:
-                    // Ignore user network determination as this could abort linger timeout.
-                    // Networks are only lingered once validated because:
-                    // - Unvalidated networks are never lingered (see rematchNetworkAndRequests).
-                    // - Once validated, a Network's validated bit is never cleared.
-                    // Since networks are only lingered after being validated a user's
-                    // determination will not change the death sentence that lingering entails:
-                    // - If the user wants to use the network or bypasses the captive portal,
-                    //   the network's score will not be increased beyond its current value
-                    //   because it is already validated.  Without a score increase there is no
-                    //   chance of reactivation (i.e. aborting linger timeout).
-                    // - If the user does not want the network, lingering will disconnect the
-                    //   network anyhow.
-                    return HANDLED;
-                default:
-                    return NOT_HANDLED;
-            }
-        }
-
-        @Override
-        public void exit() {
-            mWakeupMessage.cancel();
-        }
+    private static String getCaptivePortalServerHttpsUrl(Context context) {
+        return getSetting(context, Settings.Global.CAPTIVE_PORTAL_HTTPS_URL, DEFAULT_HTTPS_URL);
     }
 
-    private static String getCaptivePortalServerUrl(Context context, boolean isHttps) {
-        String server = Settings.Global.getString(context.getContentResolver(),
-                Settings.Global.CAPTIVE_PORTAL_SERVER);
-        if (server == null) server = DEFAULT_SERVER;
-        return (isHttps ? "https" : "http") + "://" + server + "/generate_204";
+    public static String getCaptivePortalServerHttpUrl(Context context) {
+        return getSetting(context, Settings.Global.CAPTIVE_PORTAL_HTTP_URL, DEFAULT_HTTP_URL);
     }
 
-    public static String getCaptivePortalServerUrl(Context context) {
-        return getCaptivePortalServerUrl(context, false);
+    private static String getCaptivePortalFallbackUrl(Context context) {
+        return getSetting(context,
+                Settings.Global.CAPTIVE_PORTAL_FALLBACK_URL, DEFAULT_FALLBACK_URL);
+    }
+
+    private static String getCaptivePortalUserAgent(Context context) {
+        return getSetting(context, Settings.Global.CAPTIVE_PORTAL_USER_AGENT, DEFAULT_USER_AGENT);
+    }
+
+    private static String getSetting(Context context, String symbol, String defaultValue) {
+        final String value = Settings.Global.getString(context.getContentResolver(), symbol);
+        return value != null ? value : defaultValue;
     }
 
     @VisibleForTesting
     protected CaptivePortalProbeResult isCaptivePortal() {
-        if (!mIsCaptivePortalCheckEnabled) return new CaptivePortalProbeResult(204, null);
+        if (!mIsCaptivePortalCheckEnabled) return new CaptivePortalProbeResult(204);
 
-        URL pacUrl = null, httpUrl = null, httpsUrl = null;
+        URL pacUrl = null, httpsUrl = null, httpUrl = null, fallbackUrl = null;
 
         // On networks with a PAC instead of fetching a URL that should result in a 204
         // response, we instead simply fetch the PAC script.  This is done for a few reasons:
@@ -724,20 +655,17 @@
         //    results for network validation.
         final ProxyInfo proxyInfo = mNetworkAgentInfo.linkProperties.getHttpProxy();
         if (proxyInfo != null && !Uri.EMPTY.equals(proxyInfo.getPacFileUrl())) {
-            try {
-                pacUrl = new URL(proxyInfo.getPacFileUrl().toString());
-            } catch (MalformedURLException e) {
-                validationLog("Invalid PAC URL: " + proxyInfo.getPacFileUrl().toString());
+            pacUrl = makeURL(proxyInfo.getPacFileUrl().toString());
+            if (pacUrl == null) {
                 return CaptivePortalProbeResult.FAILED;
             }
         }
 
         if (pacUrl == null) {
-            try {
-                httpUrl = new URL(getCaptivePortalServerUrl(mContext, false));
-                httpsUrl = new URL(getCaptivePortalServerUrl(mContext, true));
-            } catch (MalformedURLException e) {
-                validationLog("Bad validation URL: " + getCaptivePortalServerUrl(mContext, false));
+            httpsUrl = makeURL(getCaptivePortalServerHttpsUrl(mContext));
+            httpUrl = makeURL(getCaptivePortalServerHttpUrl(mContext));
+            fallbackUrl = makeURL(getCaptivePortalFallbackUrl(mContext));
+            if (httpUrl == null || httpsUrl == null) {
                 return CaptivePortalProbeResult.FAILED;
             }
         }
@@ -759,11 +687,12 @@
         if (!TextUtils.isEmpty(hostToResolve)) {
             String probeName = ValidationProbeEvent.getProbeName(ValidationProbeEvent.PROBE_DNS);
             final Stopwatch dnsTimer = new Stopwatch().start();
+            int dnsResult;
+            long dnsLatency;
             try {
                 InetAddress[] addresses = mNetworkAgentInfo.network.getAllByName(hostToResolve);
-                long dnsLatency = dnsTimer.stop();
-                ValidationProbeEvent.logEvent(mNetId, dnsLatency,
-                        ValidationProbeEvent.PROBE_DNS, ValidationProbeEvent.DNS_SUCCESS);
+                dnsResult = ValidationProbeEvent.DNS_SUCCESS;
+                dnsLatency = dnsTimer.stop();
                 final StringBuffer connectInfo = new StringBuffer(", " + hostToResolve + "=");
                 for (InetAddress address : addresses) {
                     connectInfo.append(address.getHostAddress());
@@ -771,18 +700,18 @@
                 }
                 validationLog(probeName + " OK " + dnsLatency + "ms" + connectInfo);
             } catch (UnknownHostException e) {
-                long dnsLatency = dnsTimer.stop();
-                ValidationProbeEvent.logEvent(mNetId, dnsLatency,
-                        ValidationProbeEvent.PROBE_DNS, ValidationProbeEvent.DNS_FAILURE);
+                dnsResult = ValidationProbeEvent.DNS_FAILURE;
+                dnsLatency = dnsTimer.stop();
                 validationLog(probeName + " FAIL " + dnsLatency + "ms, " + hostToResolve);
             }
+            logValidationProbe(dnsLatency, ValidationProbeEvent.PROBE_DNS, dnsResult);
         }
 
         CaptivePortalProbeResult result;
         if (pacUrl != null) {
             result = sendHttpProbe(pacUrl, ValidationProbeEvent.PROBE_PAC);
         } else if (mUseHttps) {
-            result = sendParallelHttpProbes(httpsUrl, httpUrl);
+            result = sendParallelHttpProbes(httpsUrl, httpUrl, fallbackUrl);
         } else {
             result = sendHttpProbe(httpUrl, ValidationProbeEvent.PROBE_HTTP);
         }
@@ -812,6 +741,10 @@
             urlConnection.setConnectTimeout(SOCKET_TIMEOUT_MS);
             urlConnection.setReadTimeout(SOCKET_TIMEOUT_MS);
             urlConnection.setUseCaches(false);
+            final String userAgent = getCaptivePortalUserAgent(mContext);
+            if (userAgent != null) {
+               urlConnection.setRequestProperty("User-Agent", userAgent);
+            }
 
             // Time how long it takes to get a response to our request
             long requestTimestamp = SystemClock.elapsedRealtime();
@@ -856,29 +789,25 @@
                 urlConnection.disconnect();
             }
         }
-        ValidationProbeEvent.logEvent(mNetId, probeTimer.stop(), probeType, httpResponseCode);
-        return new CaptivePortalProbeResult(httpResponseCode, redirectUrl);
+        logValidationProbe(probeTimer.stop(), probeType, httpResponseCode);
+        return new CaptivePortalProbeResult(httpResponseCode, redirectUrl, url.toString());
     }
 
-    private CaptivePortalProbeResult sendParallelHttpProbes(URL httpsUrl, URL httpUrl) {
-        // Number of probes to wait for. We might wait for all of them, but we might also return if
-        // only one of them has replied. For example, we immediately return if the HTTP probe finds
-        // a captive portal, even if the HTTPS probe is timing out.
+    private CaptivePortalProbeResult sendParallelHttpProbes(
+            URL httpsUrl, URL httpUrl, URL fallbackUrl) {
+        // Number of probes to wait for. If a probe completes with a conclusive answer
+        // it shortcuts the latch immediately by forcing the count to 0.
         final CountDownLatch latch = new CountDownLatch(2);
 
-        // Which probe result we're going to use. This doesn't need to be atomic, but it does need
-        // to be final because otherwise we can't set it from the ProbeThreads.
-        final AtomicReference<CaptivePortalProbeResult> finalResult = new AtomicReference<>();
-
         final class ProbeThread extends Thread {
             private final boolean mIsHttps;
-            private volatile CaptivePortalProbeResult mResult;
+            private volatile CaptivePortalProbeResult mResult = CaptivePortalProbeResult.FAILED;
 
             public ProbeThread(boolean isHttps) {
                 mIsHttps = isHttps;
             }
 
-            public CaptivePortalProbeResult getResult() {
+            public CaptivePortalProbeResult result() {
                 return mResult;
             }
 
@@ -890,32 +819,66 @@
                     mResult = sendHttpProbe(httpUrl, ValidationProbeEvent.PROBE_HTTP);
                 }
                 if ((mIsHttps && mResult.isSuccessful()) || (!mIsHttps && mResult.isPortal())) {
-                    // HTTPS succeeded, or HTTP found a portal. Don't wait for the other probe.
-                    finalResult.compareAndSet(null, mResult);
-                    latch.countDown();
+                    // Stop waiting immediately if https succeeds or if http finds a portal.
+                    while (latch.getCount() > 0) {
+                        latch.countDown();
+                    }
                 }
-                // Signal that one probe has completed. If we've already made a decision, or if this
-                // is the second probe, the latch will be at zero and we'll return a result.
+                // Signal this probe has completed.
                 latch.countDown();
             }
         }
 
-        ProbeThread httpsProbe = new ProbeThread(true);
-        ProbeThread httpProbe = new ProbeThread(false);
-        httpsProbe.start();
-        httpProbe.start();
+        final ProbeThread httpsProbe = new ProbeThread(true);
+        final ProbeThread httpProbe = new ProbeThread(false);
 
         try {
-            latch.await();
+            httpsProbe.start();
+            httpProbe.start();
+            latch.await(PROBE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
         } catch (InterruptedException e) {
-            validationLog("Error: probe wait interrupted!");
+            validationLog("Error: probes wait interrupted!");
             return CaptivePortalProbeResult.FAILED;
         }
 
-        // If there was no deciding probe, that means that both probes completed. Return HTTPS.
-        finalResult.compareAndSet(null, httpsProbe.getResult());
+        final CaptivePortalProbeResult httpsResult = httpsProbe.result();
+        final CaptivePortalProbeResult httpResult = httpProbe.result();
 
-        return finalResult.get();
+        // Look for a conclusive probe result first.
+        if (httpResult.isPortal()) {
+            return httpResult;
+        }
+        // httpsResult.isPortal() is not expected, but check it nonetheless.
+        if (httpsResult.isPortal() || httpsResult.isSuccessful()) {
+            return httpsResult;
+        }
+        // If a fallback url is specified, use a fallback probe to try again portal detection.
+        if (fallbackUrl != null) {
+            CaptivePortalProbeResult result =
+                    sendHttpProbe(fallbackUrl, ValidationProbeEvent.PROBE_FALLBACK);
+            if (result.isPortal()) {
+                return result;
+            }
+        }
+        // Otherwise wait until https probe completes and use its result.
+        try {
+            httpsProbe.join();
+        } catch (InterruptedException e) {
+            validationLog("Error: https probe wait interrupted!");
+            return CaptivePortalProbeResult.FAILED;
+        }
+        return httpsProbe.result();
+    }
+
+    private URL makeURL(String url) {
+        if (url != null) {
+            try {
+                return new URL(url);
+            } catch (MalformedURLException e) {
+                validationLog("Bad URL: " + url);
+            }
+        }
+        return null;
     }
 
     /**
@@ -1000,17 +963,18 @@
                 PERMISSION_ACCESS_NETWORK_CONDITIONS);
     }
 
-    // Allow tests to override linger time.
-    @VisibleForTesting
-    public static void SetDefaultLingerTime(int time_ms) {
-        if (Process.myUid() == Process.SYSTEM_UID) {
-            throw new SecurityException("SetDefaultLingerTime only for internal testing.");
-        }
-        DEFAULT_LINGER_DELAY_MS = time_ms;
+    private void logNetworkEvent(int evtype) {
+        mMetricsLog.log(new NetworkEvent(mNetId, evtype));
     }
 
-    @VisibleForTesting
-    protected WakeupMessage makeWakeupMessage(Context c, Handler h, String s, int i) {
-        return new WakeupMessage(c, h, s, i);
+    private void maybeLogEvaluationResult(int evtype) {
+        if (mEvaluationTimer.isRunning()) {
+            mMetricsLog.log(new NetworkEvent(mNetId, evtype, mEvaluationTimer.stop()));
+            mEvaluationTimer.reset();
+        }
+    }
+
+    private void logValidationProbe(long durationMs, int probeType, int probeResult) {
+        mMetricsLog.log(new ValidationProbeEvent(mNetId, durationMs, probeType, probeResult));
     }
 }
diff --git a/services/core/java/com/android/server/connectivity/NetworkNotificationManager.java b/services/core/java/com/android/server/connectivity/NetworkNotificationManager.java
new file mode 100644
index 0000000..f7b01be
--- /dev/null
+++ b/services/core/java/com/android/server/connectivity/NetworkNotificationManager.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.widget.Toast;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Resources;
+import android.net.NetworkCapabilities;
+import android.os.UserHandle;
+import android.telephony.TelephonyManager;
+import android.util.Slog;
+
+import com.android.internal.R;
+
+import static android.net.NetworkCapabilities.*;
+
+
+public class NetworkNotificationManager {
+
+    public static enum NotificationType { SIGN_IN, NO_INTERNET, LOST_INTERNET, NETWORK_SWITCH };
+
+    private static final String NOTIFICATION_ID = "Connectivity.Notification";
+
+    private static final String TAG = NetworkNotificationManager.class.getSimpleName();
+    private static final boolean DBG = true;
+    private static final boolean VDBG = false;
+
+    private final Context mContext;
+    private final TelephonyManager mTelephonyManager;
+    private final NotificationManager mNotificationManager;
+
+    public NetworkNotificationManager(Context c, TelephonyManager t, NotificationManager n) {
+        mContext = c;
+        mTelephonyManager = t;
+        mNotificationManager = n;
+    }
+
+    // TODO: deal more gracefully with multi-transport networks.
+    private static int getFirstTransportType(NetworkAgentInfo nai) {
+        for (int i = 0; i < 64; i++) {
+            if (nai.networkCapabilities.hasTransport(i)) return i;
+        }
+        return -1;
+    }
+
+    private static String getTransportName(int transportType) {
+        Resources r = Resources.getSystem();
+        String[] networkTypes = r.getStringArray(R.array.network_switch_type_name);
+        try {
+            return networkTypes[transportType];
+        } catch (IndexOutOfBoundsException e) {
+            return r.getString(R.string.network_switch_type_name_unknown);
+        }
+    }
+
+    private static int getIcon(int transportType) {
+        return (transportType == TRANSPORT_WIFI) ?
+                R.drawable.stat_notify_wifi_in_range :  // TODO: Distinguish ! from ?.
+                R.drawable.stat_notify_rssi_in_range;
+    }
+
+    /**
+     * Show or hide network provisioning notifications.
+     *
+     * We use notifications for two purposes: to notify that a network requires sign in
+     * (NotificationType.SIGN_IN), or to notify that a network does not have Internet access
+     * (NotificationType.NO_INTERNET). We display at most one notification per ID, so on a
+     * particular network we can display the notification type that was most recently requested.
+     * So for example if a captive portal fails to reply within a few seconds of connecting, we
+     * might first display NO_INTERNET, and then when the captive portal check completes, display
+     * SIGN_IN.
+     *
+     * @param id an identifier that uniquely identifies this notification.  This must match
+     *         between show and hide calls.  We use the NetID value but for legacy callers
+     *         we concatenate the range of types with the range of NetIDs.
+     * @param nai the network with which the notification is associated. For a SIGN_IN, NO_INTERNET,
+     *         or LOST_INTERNET notification, this is the network we're connecting to. For a
+     *         NETWORK_SWITCH notification it's the network that we switched from. When this network
+     *         disconnects the notification is removed.
+     * @param switchToNai for a NETWORK_SWITCH notification, the network we are switching to. Null
+     *         in all other cases. Only used to determine the text of the notification.
+     */
+    public void showNotification(int id, NotificationType notifyType, NetworkAgentInfo nai,
+            NetworkAgentInfo switchToNai, PendingIntent intent, boolean highPriority) {
+        int transportType;
+        String extraInfo;
+        if (nai != null) {
+            transportType = getFirstTransportType(nai);
+            extraInfo = nai.networkInfo.getExtraInfo();
+            // Only notify for Internet-capable networks.
+            if (!nai.networkCapabilities.hasCapability(NET_CAPABILITY_INTERNET)) return;
+        } else {
+            // Legacy notifications.
+            transportType = TRANSPORT_CELLULAR;
+            extraInfo = null;
+        }
+
+        if (DBG) {
+            Slog.d(TAG, "showNotification " + notifyType
+                    + " transportType=" + getTransportName(transportType)
+                    + " extraInfo=" + extraInfo + " highPriority=" + highPriority);
+        }
+
+        Resources r = Resources.getSystem();
+        CharSequence title;
+        CharSequence details;
+        int icon = getIcon(transportType);
+        if (notifyType == NotificationType.NO_INTERNET && transportType == TRANSPORT_WIFI) {
+            title = r.getString(R.string.wifi_no_internet, 0);
+            details = r.getString(R.string.wifi_no_internet_detailed);
+        } else if (notifyType == NotificationType.LOST_INTERNET &&
+                transportType == TRANSPORT_WIFI) {
+            title = r.getString(R.string.wifi_no_internet, 0);
+            details = r.getString(R.string.wifi_no_internet_detailed);
+        } else if (notifyType == NotificationType.SIGN_IN) {
+            switch (transportType) {
+                case TRANSPORT_WIFI:
+                    title = r.getString(R.string.wifi_available_sign_in, 0);
+                    details = r.getString(R.string.network_available_sign_in_detailed, extraInfo);
+                    break;
+                case TRANSPORT_CELLULAR:
+                    title = r.getString(R.string.network_available_sign_in, 0);
+                    // TODO: Change this to pull from NetworkInfo once a printable
+                    // name has been added to it
+                    details = mTelephonyManager.getNetworkOperatorName();
+                    break;
+                default:
+                    title = r.getString(R.string.network_available_sign_in, 0);
+                    details = r.getString(R.string.network_available_sign_in_detailed, extraInfo);
+                    break;
+            }
+        } else if (notifyType == NotificationType.NETWORK_SWITCH) {
+            String fromTransport = getTransportName(transportType);
+            String toTransport = getTransportName(getFirstTransportType(switchToNai));
+            title = r.getString(R.string.network_switch_metered, toTransport);
+            details = r.getString(R.string.network_switch_metered_detail, toTransport,
+                    fromTransport);
+        } else {
+            Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network transport "
+                    + getTransportName(transportType));
+            return;
+        }
+
+        Notification.Builder builder = new Notification.Builder(mContext)
+                .setWhen(System.currentTimeMillis())
+                .setShowWhen(notifyType == NotificationType.NETWORK_SWITCH)
+                .setSmallIcon(icon)
+                .setAutoCancel(true)
+                .setTicker(title)
+                .setColor(mContext.getColor(
+                        com.android.internal.R.color.system_notification_accent_color))
+                .setContentTitle(title)
+                .setContentIntent(intent)
+                .setLocalOnly(true)
+                .setPriority(highPriority ?
+                        Notification.PRIORITY_HIGH :
+                        Notification.PRIORITY_DEFAULT)
+                .setDefaults(highPriority ? Notification.DEFAULT_ALL : 0)
+                .setOnlyAlertOnce(true);
+
+        if (notifyType == NotificationType.NETWORK_SWITCH) {
+            builder.setStyle(new Notification.BigTextStyle().bigText(details));
+        } else {
+            builder.setContentText(details);
+        }
+
+        Notification notification = builder.build();
+
+        try {
+            mNotificationManager.notifyAsUser(NOTIFICATION_ID, id, notification, UserHandle.ALL);
+        } catch (NullPointerException npe) {
+            Slog.d(TAG, "setNotificationVisible: visible notificationManager npe=" + npe);
+        }
+    }
+
+    public void clearNotification(int id) {
+        if (DBG) {
+            Slog.d(TAG, "clearNotification id=" + id);
+        }
+        try {
+            mNotificationManager.cancelAsUser(NOTIFICATION_ID, id, UserHandle.ALL);
+        } catch (NullPointerException npe) {
+            Slog.d(TAG, "setNotificationVisible: cancel notificationManager npe=" + npe);
+        }
+    }
+
+    /**
+     * Legacy provisioning notifications coming directly from DcTracker.
+     */
+    public void setProvNotificationVisible(boolean visible, int id, String action) {
+        if (visible) {
+            Intent intent = new Intent(action);
+            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
+            showNotification(id, NotificationType.SIGN_IN, null, null, pendingIntent, false);
+        } else {
+            clearNotification(id);
+        }
+    }
+
+    public void showToast(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
+        String fromTransport = getTransportName(getFirstTransportType(fromNai));
+        String toTransport = getTransportName(getFirstTransportType(toNai));
+        String text = mContext.getResources().getString(
+                R.string.network_switch_metered_toast, fromTransport, toTransport);
+        Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
+    }
+}
diff --git a/services/core/java/com/android/server/connectivity/PermissionMonitor.java b/services/core/java/com/android/server/connectivity/PermissionMonitor.java
index eb1c77e..e084ff8 100644
--- a/services/core/java/com/android/server/connectivity/PermissionMonitor.java
+++ b/services/core/java/com/android/server/connectivity/PermissionMonitor.java
@@ -18,6 +18,7 @@
 
 import static android.Manifest.permission.CHANGE_NETWORK_STATE;
 import static android.Manifest.permission.CONNECTIVITY_INTERNAL;
+import static android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS;
 import static android.content.pm.ApplicationInfo.FLAG_SYSTEM;
 import static android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
 import static android.content.pm.PackageManager.GET_PERMISSIONS;
@@ -65,10 +66,10 @@
     private final BroadcastReceiver mIntentReceiver;
 
     // Values are User IDs.
-    private final Set<Integer> mUsers = new HashSet<Integer>();
+    private final Set<Integer> mUsers = new HashSet<>();
 
     // Keys are App IDs. Values are true for SYSTEM permission and false for NETWORK permission.
-    private final Map<Integer, Boolean> mApps = new HashMap<Integer, Boolean>();
+    private final Map<Integer, Boolean> mApps = new HashMap<>();
 
     public PermissionMonitor(Context context, INetworkManagementService netd) {
         mContext = context;
@@ -126,14 +127,14 @@
             }
 
             boolean isNetwork = hasNetworkPermission(app);
-            boolean isSystem = hasSystemPermission(app);
+            boolean hasRestrictedPermission = hasRestrictedNetworkPermission(app);
 
-            if (isNetwork || isSystem) {
+            if (isNetwork || hasRestrictedPermission) {
                 Boolean permission = mApps.get(uid);
                 // If multiple packages share a UID (cf: android:sharedUserId) and ask for different
                 // permissions, don't downgrade (i.e., if it's already SYSTEM, leave it as is).
                 if (permission == null || permission == NETWORK) {
-                    mApps.put(uid, isSystem);
+                    mApps.put(uid, hasRestrictedPermission);
                 }
             }
         }
@@ -164,12 +165,13 @@
         return hasPermission(app, CHANGE_NETWORK_STATE);
     }
 
-    private boolean hasSystemPermission(PackageInfo app) {
+    private boolean hasRestrictedNetworkPermission(PackageInfo app) {
         int flags = app.applicationInfo != null ? app.applicationInfo.flags : 0;
         if ((flags & FLAG_SYSTEM) != 0 || (flags & FLAG_UPDATED_SYSTEM_APP) != 0) {
             return true;
         }
-        return hasPermission(app, CONNECTIVITY_INTERNAL);
+        return hasPermission(app, CONNECTIVITY_INTERNAL)
+                || hasPermission(app, CONNECTIVITY_USE_RESTRICTED_NETWORKS);
     }
 
     private int[] toIntArray(List<Integer> list) {
@@ -181,8 +183,8 @@
     }
 
     private void update(Set<Integer> users, Map<Integer, Boolean> apps, boolean add) {
-        List<Integer> network = new ArrayList<Integer>();
-        List<Integer> system = new ArrayList<Integer>();
+        List<Integer> network = new ArrayList<>();
+        List<Integer> system = new ArrayList<>();
         for (Entry<Integer, Boolean> app : apps.entrySet()) {
             List<Integer> list = app.getValue() ? system : network;
             for (int user : users) {
@@ -209,7 +211,7 @@
         }
         mUsers.add(user);
 
-        Set<Integer> users = new HashSet<Integer>();
+        Set<Integer> users = new HashSet<>();
         users.add(user);
         update(users, mApps, true);
     }
@@ -221,7 +223,7 @@
         }
         mUsers.remove(user);
 
-        Set<Integer> users = new HashSet<Integer>();
+        Set<Integer> users = new HashSet<>();
         users.add(user);
         update(users, mApps, false);
     }
@@ -234,9 +236,9 @@
         try {
             final PackageInfo app = mPackageManager.getPackageInfo(name, GET_PERMISSIONS);
             final boolean isNetwork = hasNetworkPermission(app);
-            final boolean isSystem = hasSystemPermission(app);
-            if (isNetwork || isSystem) {
-                currentPermission = isSystem;
+            final boolean hasRestrictedPermission = hasRestrictedNetworkPermission(app);
+            if (isNetwork || hasRestrictedPermission) {
+                currentPermission = hasRestrictedPermission;
             }
         } catch (NameNotFoundException e) {
             // App not found.
@@ -257,7 +259,7 @@
         if (permission != mApps.get(appUid)) {
             mApps.put(appUid, permission);
 
-            Map<Integer, Boolean> apps = new HashMap<Integer, Boolean>();
+            Map<Integer, Boolean> apps = new HashMap<>();
             apps.put(appUid, permission);
             update(mUsers, apps, true);
         }
@@ -268,7 +270,7 @@
             loge("Invalid app in onAppRemoved: " + appUid);
             return;
         }
-        Map<Integer, Boolean> apps = new HashMap<Integer, Boolean>();
+        Map<Integer, Boolean> apps = new HashMap<>();
 
         Boolean permission = null;
         String[] packages = mPackageManager.getPackagesForUid(appUid);
diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java
index b3d5efa..da9c547 100644
--- a/services/core/java/com/android/server/connectivity/Tethering.java
+++ b/services/core/java/com/android/server/connectivity/Tethering.java
@@ -71,6 +71,7 @@
 import com.android.internal.util.StateMachine;
 import com.android.server.IoThread;
 import com.android.server.connectivity.tethering.IControlsTethering;
+import com.android.server.connectivity.tethering.IPv6TetheringCoordinator;
 import com.android.server.connectivity.tethering.TetherInterfaceStateMachine;
 import com.android.server.net.BaseNetworkObserver;
 
@@ -404,11 +405,13 @@
         // Check carrier config for entitlement checks
         final CarrierConfigManager configManager = (CarrierConfigManager) mContext
              .getSystemService(Context.CARRIER_CONFIG_SERVICE);
-        boolean isEntitlementCheckRequired = configManager.getConfig().getBoolean(
-             CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL);
-
-        if (!isEntitlementCheckRequired) {
-            return false;
+        if (configManager != null && configManager.getConfig() != null) {
+            // we do have a CarrierConfigManager and it has a config.
+            boolean isEntitlementCheckRequired = configManager.getConfig().getBoolean(
+                    CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL);
+            if (!isEntitlementCheckRequired) {
+                return false;
+            }
         }
         return (provisionApp.length == 2);
     }
@@ -592,13 +595,13 @@
         synchronized (mPublicSync) {
             TetherState tetherState = mTetherStates.get(iface);
             if (tetherState == null) {
-                Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
+                Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring");
                 return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
             }
             // Ignore the error status of the interface.  If the interface is available,
             // the errors are referring to past tethering attempts anyway.
             if (tetherState.mLastState != IControlsTethering.STATE_AVAILABLE) {
-                Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
+                Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring");
                 return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
             }
             tetherState.mStateMachine.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_REQUESTED);
@@ -1024,15 +1027,29 @@
      */
     class UpstreamNetworkCallback extends NetworkCallback {
         @Override
+        public void onAvailable(Network network) {
+            mTetherMasterSM.sendMessage(TetherMasterSM.EVENT_UPSTREAM_CALLBACK,
+                    UpstreamNetworkMonitor.EVENT_ON_AVAILABLE, 0, network);
+        }
+
+        @Override
+        public void onCapabilitiesChanged(Network network, NetworkCapabilities newNc) {
+            mTetherMasterSM.sendMessage(TetherMasterSM.EVENT_UPSTREAM_CALLBACK,
+                    UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES, 0,
+                    new NetworkState(null, null, newNc, network, null, null));
+        }
+
+        @Override
         public void onLinkPropertiesChanged(Network network, LinkProperties newLp) {
-            mTetherMasterSM.sendMessage(
-                    TetherMasterSM.EVENT_UPSTREAM_LINKPROPERTIES_CHANGED,
+            mTetherMasterSM.sendMessage(TetherMasterSM.EVENT_UPSTREAM_CALLBACK,
+                    UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES, 0,
                     new NetworkState(null, newLp, null, network, null, null));
         }
 
         @Override
         public void onLost(Network network) {
-            mTetherMasterSM.sendMessage(TetherMasterSM.EVENT_UPSTREAM_LOST, network);
+            mTetherMasterSM.sendMessage(TetherMasterSM.EVENT_UPSTREAM_CALLBACK,
+                    UpstreamNetworkMonitor.EVENT_ON_LOST, 0, network);
         }
     }
 
@@ -1051,6 +1068,11 @@
      * could/should be moved here.
      */
     class UpstreamNetworkMonitor {
+        static final int EVENT_ON_AVAILABLE      = 1;
+        static final int EVENT_ON_CAPABILITIES   = 2;
+        static final int EVENT_ON_LINKPROPERTIES = 3;
+        static final int EVENT_ON_LOST           = 4;
+
         final HashMap<Network, NetworkState> mNetworkMap = new HashMap<>();
         NetworkCallback mDefaultNetworkCallback;
         NetworkCallback mDunTetheringCallback;
@@ -1085,33 +1107,107 @@
             mNetworkMap.clear();
         }
 
-        // Returns true if these updated LinkProperties pertain to the current
-        // upstream network interface, false otherwise (or if there is not
-        // currently any upstream tethering interface).
-        boolean processLinkPropertiesChanged(NetworkState networkState) {
-            if (networkState == null ||
-                    networkState.network == null ||
-                    networkState.linkProperties == null) {
-                return false;
-            }
+        NetworkState lookup(Network network) {
+            return (network != null) ? mNetworkMap.get(network) : null;
+        }
 
-            mNetworkMap.put(networkState.network, networkState);
-
-            if (mCurrentUpstreamIface != null) {
-                for (String ifname : networkState.linkProperties.getAllInterfaceNames()) {
-                    if (mCurrentUpstreamIface.equals(ifname)) {
-                        return true;
+        NetworkState processCallback(int arg1, Object obj) {
+            switch (arg1) {
+                case EVENT_ON_AVAILABLE: {
+                    final Network network = (Network) obj;
+                    if (VDBG) {
+                        Log.d(TAG, "EVENT_ON_AVAILABLE for " + network);
                     }
+                    if (!mNetworkMap.containsKey(network)) {
+                        mNetworkMap.put(network,
+                                new NetworkState(null, null, null, network, null, null));
+                    }
+
+                    final ConnectivityManager cm = getConnectivityManager();
+
+                    if (mDefaultNetworkCallback != null) {
+                        cm.requestNetworkCapabilities(mDefaultNetworkCallback);
+                        cm.requestLinkProperties(mDefaultNetworkCallback);
+                    }
+
+                    // Requesting updates for mDunTetheringCallback is not
+                    // necessary. Because it's a listen, it will already have
+                    // heard all NetworkCapabilities and LinkProperties updates
+                    // since UpstreamNetworkMonitor was started. Because we
+                    // start UpstreamNetworkMonitor before chooseUpstreamType()
+                    // is ever invoked (it can register a DUN request) this is
+                    // mostly safe. However, if a DUN network is already up for
+                    // some reason (unlikely, because DUN is restricted and,
+                    // unless the DUN network is shared with another APN, only
+                    // the system can request it and this is the only part of
+                    // the system that requests it) we won't know its
+                    // LinkProperties or NetworkCapabilities.
+
+                    return mNetworkMap.get(network);
+                }
+                case EVENT_ON_CAPABILITIES: {
+                    final NetworkState ns = (NetworkState) obj;
+                    if (!mNetworkMap.containsKey(ns.network)) {
+                        // Ignore updates for networks for which we have not yet
+                        // received onAvailable() - which should never happen -
+                        // or for which we have already received onLost().
+                        return null;
+                    }
+                    if (VDBG) {
+                        Log.d(TAG, String.format("EVENT_ON_CAPABILITIES for %s: %s",
+                                ns.network, ns.networkCapabilities));
+                    }
+
+                    final NetworkState prev = mNetworkMap.get(ns.network);
+                    mNetworkMap.put(ns.network,
+                            new NetworkState(null, prev.linkProperties, ns.networkCapabilities,
+                                             ns.network, null, null));
+                    return mNetworkMap.get(ns.network);
+                }
+                case EVENT_ON_LINKPROPERTIES: {
+                    final NetworkState ns = (NetworkState) obj;
+                    if (!mNetworkMap.containsKey(ns.network)) {
+                        // Ignore updates for networks for which we have not yet
+                        // received onAvailable() - which should never happen -
+                        // or for which we have already received onLost().
+                        return null;
+                    }
+                    if (VDBG) {
+                        Log.d(TAG, String.format("EVENT_ON_LINKPROPERTIES for %s: %s",
+                                ns.network, ns.linkProperties));
+                    }
+
+                    final NetworkState prev = mNetworkMap.get(ns.network);
+                    mNetworkMap.put(ns.network,
+                            new NetworkState(null, ns.linkProperties, prev.networkCapabilities,
+                                             ns.network, null, null));
+                    return mNetworkMap.get(ns.network);
+                }
+                case EVENT_ON_LOST: {
+                    final Network network = (Network) obj;
+                    if (VDBG) {
+                        Log.d(TAG, "EVENT_ON_LOST for " + network);
+                    }
+                    return mNetworkMap.remove(network);
+                }
+                default:
+                    return null;
+            }
+        }
+    }
+
+    // Needed because the canonical source of upstream truth is just the
+    // upstream interface name, |mCurrentUpstreamIface|.  This is ripe for
+    // future simplification, once the upstream Network is canonical.
+    boolean pertainsToCurrentUpstream(NetworkState ns) {
+        if (ns != null && ns.linkProperties != null && mCurrentUpstreamIface != null) {
+            for (String ifname : ns.linkProperties.getAllInterfaceNames()) {
+                if (mCurrentUpstreamIface.equals(ifname)) {
+                    return true;
                 }
             }
-            return false;
         }
-
-        void processNetworkLost(Network network) {
-            if (network != null) {
-                mNetworkMap.remove(network);
-            }
-        }
+        return false;
     }
 
     class TetherMasterSM extends StateMachine {
@@ -1126,8 +1222,7 @@
         static final int CMD_RETRY_UPSTREAM                     = BASE_MASTER + 4;
         // Events from NetworkCallbacks that we process on the master state
         // machine thread on behalf of the UpstreamNetworkMonitor.
-        static final int EVENT_UPSTREAM_LINKPROPERTIES_CHANGED  = BASE_MASTER + 5;
-        static final int EVENT_UPSTREAM_LOST                    = BASE_MASTER + 6;
+        static final int EVENT_UPSTREAM_CALLBACK                = BASE_MASTER + 5;
 
         private State mInitialState;
         private State mTetherModeAliveState;
@@ -1150,7 +1245,8 @@
         // Because we excise interfaces immediately from mTetherStates, we must maintain mNotifyList
         // so that the garbage collector does not clean up the state machine before it has a chance
         // to tear itself down.
-        private ArrayList<TetherInterfaceStateMachine> mNotifyList;
+        private final ArrayList<TetherInterfaceStateMachine> mNotifyList;
+        private final IPv6TetheringCoordinator mIPv6TetheringCoordinator;
 
         private int mMobileApnReserved = ConnectivityManager.TYPE_NONE;
         private NetworkCallback mMobileUpstreamCallback;
@@ -1178,6 +1274,7 @@
             addState(mSetDnsForwardersErrorState);
 
             mNotifyList = new ArrayList<>();
+            mIPv6TetheringCoordinator = new IPv6TetheringCoordinator(mNotifyList);
             setInitialState(mInitialState);
         }
 
@@ -1282,6 +1379,7 @@
             }
 
             protected void chooseUpstreamType(boolean tryCell) {
+                final ConnectivityManager cm = getConnectivityManager();
                 int upType = ConnectivityManager.TYPE_NONE;
                 String iface = null;
 
@@ -1296,8 +1394,9 @@
                     }
 
                     for (Integer netType : mUpstreamIfaceTypes) {
-                        NetworkInfo info =
-                                getConnectivityManager().getNetworkInfo(netType.intValue());
+                        NetworkInfo info = cm.getNetworkInfo(netType.intValue());
+                        // TODO: if the network is suspended we should consider
+                        // that to be the same as connected here.
                         if ((info != null) && info.isConnected()) {
                             upType = netType.intValue();
                             break;
@@ -1338,9 +1437,9 @@
                         break;
                 }
 
+                Network network = null;
                 if (upType != ConnectivityManager.TYPE_NONE) {
-                    LinkProperties linkProperties =
-                            getConnectivityManager().getLinkProperties(upType);
+                    LinkProperties linkProperties = cm.getLinkProperties(upType);
                     if (linkProperties != null) {
                         // Find the interface with the default IPv4 route. It may be the
                         // interface described by linkProperties, or one of the interfaces
@@ -1357,7 +1456,7 @@
                     }
 
                     if (iface != null) {
-                        Network network = getConnectivityManager().getNetworkForType(upType);
+                        network = cm.getNetworkForType(upType);
                         if (network == null) {
                             Log.e(TAG, "No Network for upstream type " + upType + "!");
                         }
@@ -1365,6 +1464,17 @@
                     }
                 }
                 notifyTetheredOfNewUpstreamIface(iface);
+                NetworkState ns = mUpstreamNetworkMonitor.lookup(network);
+                if (ns != null && pertainsToCurrentUpstream(ns)) {
+                    // If we already have NetworkState for this network examine
+                    // it immediately, because there likely will be no second
+                    // EVENT_ON_AVAILABLE (it was already received).
+                    handleNewUpstreamNetworkState(ns);
+                } else if (mCurrentUpstreamIface == null) {
+                    // There are no available upstream networks, or none that
+                    // have an IPv4 default route (current metric for success).
+                    handleNewUpstreamNetworkState(null);
+                }
             }
 
             protected void setDnsForwarders(final Network network, final LinkProperties lp) {
@@ -1397,6 +1507,10 @@
                             ifaceName);
                 }
             }
+
+            protected void handleNewUpstreamNetworkState(NetworkState ns) {
+                mIPv6TetheringCoordinator.updateUpstreamNetworkState(ns);
+            }
         }
 
         private final AtomicInteger mSimBcastGenerationNumber = new AtomicInteger(0);
@@ -1505,6 +1619,7 @@
                         if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
                         if (mNotifyList.indexOf(who) < 0) {
                             mNotifyList.add(who);
+                            mIPv6TetheringCoordinator.addActiveDownstream(who);
                         }
                         transitionTo(mTetherModeAliveState);
                         break;
@@ -1512,6 +1627,7 @@
                         who = (TetherInterfaceStateMachine)message.obj;
                         if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
                         mNotifyList.remove(who);
+                        mIPv6TetheringCoordinator.removeActiveDownstream(who);
                         break;
                     default:
                         retValue = false;
@@ -1534,6 +1650,7 @@
                 chooseUpstreamType(mTryCell);
                 mTryCell = !mTryCell;
             }
+
             @Override
             public void exit() {
                 // TODO: examine if we should check the return value.
@@ -1541,23 +1658,27 @@
                 mUpstreamNetworkMonitor.stop();
                 stopListeningForSimChanges();
                 notifyTetheredOfNewUpstreamIface(null);
+                handleNewUpstreamNetworkState(null);
             }
+
             @Override
             public boolean processMessage(Message message) {
                 maybeLogMessage(this, message.what);
                 boolean retValue = true;
                 switch (message.what) {
-                    case CMD_TETHER_MODE_REQUESTED:
+                    case CMD_TETHER_MODE_REQUESTED: {
                         TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj;
                         if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
                         if (mNotifyList.indexOf(who) < 0) {
                             mNotifyList.add(who);
+                            mIPv6TetheringCoordinator.addActiveDownstream(who);
                         }
                         who.sendMessage(TetherInterfaceStateMachine.CMD_TETHER_CONNECTION_CHANGED,
                                 mCurrentUpstreamIface);
                         break;
-                    case CMD_TETHER_MODE_UNREQUESTED:
-                        who = (TetherInterfaceStateMachine)message.obj;
+                    }
+                    case CMD_TETHER_MODE_UNREQUESTED: {
+                        TetherInterfaceStateMachine who = (TetherInterfaceStateMachine)message.obj;
                         if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
                         if (mNotifyList.remove(who)) {
                             if (DBG) Log.d(TAG, "TetherModeAlive removing notifyee " + who);
@@ -1575,7 +1696,9 @@
                         } else {
                            Log.e(TAG, "TetherModeAliveState UNREQUESTED has unknown who: " + who);
                         }
+                        mIPv6TetheringCoordinator.removeActiveDownstream(who);
                         break;
+                    }
                     case CMD_UPSTREAM_CHANGED:
                         // need to try DUN immediately if Wifi goes down
                         mTryCell = true;
@@ -1586,24 +1709,56 @@
                         chooseUpstreamType(mTryCell);
                         mTryCell = !mTryCell;
                         break;
-                    case EVENT_UPSTREAM_LINKPROPERTIES_CHANGED:
-                        NetworkState state = (NetworkState) message.obj;
-                        if (mUpstreamNetworkMonitor.processLinkPropertiesChanged(state)) {
-                            setDnsForwarders(state.network, state.linkProperties);
-                        } else if (mCurrentUpstreamIface == null) {
-                            // If we have no upstream interface, try to run through upstream
-                            // selection again.  If, for example, IPv4 connectivity has shown up
-                            // after IPv6 (e.g., 464xlat became available) we want the chance to
-                            // notice and act accordingly.
-                            chooseUpstreamType(false);
+                    case EVENT_UPSTREAM_CALLBACK: {
+                        // First: always update local state about every network.
+                        final NetworkState ns = mUpstreamNetworkMonitor.processCallback(
+                                message.arg1, message.obj);
+
+                        if (ns == null || !pertainsToCurrentUpstream(ns)) {
+                            // TODO: In future, this is where upstream evaluation and selection
+                            // could be handled for notifications which include sufficient data.
+                            // For example, after CONNECTIVITY_ACTION listening is removed, here
+                            // is where we could observe a Wi-Fi network becoming available and
+                            // passing validation.
+                            if (mCurrentUpstreamIface == null) {
+                                // If we have no upstream interface, try to run through upstream
+                                // selection again.  If, for example, IPv4 connectivity has shown up
+                                // after IPv6 (e.g., 464xlat became available) we want the chance to
+                                // notice and act accordingly.
+                                chooseUpstreamType(false);
+                            }
+                            break;
+                        }
+
+                        switch (message.arg1) {
+                            case UpstreamNetworkMonitor.EVENT_ON_AVAILABLE:
+                                // The default network changed, or DUN connected
+                                // before this callback was processed. Updates
+                                // for the current NetworkCapabilities and
+                                // LinkProperties have been requested (default
+                                // request) or are being sent shortly (DUN). Do
+                                // nothing until they arrive; if no updates
+                                // arrive there's nothing to do.
+                                break;
+                            case UpstreamNetworkMonitor.EVENT_ON_CAPABILITIES:
+                                handleNewUpstreamNetworkState(ns);
+                                break;
+                            case UpstreamNetworkMonitor.EVENT_ON_LINKPROPERTIES:
+                                setDnsForwarders(ns.network, ns.linkProperties);
+                                handleNewUpstreamNetworkState(ns);
+                                break;
+                            case UpstreamNetworkMonitor.EVENT_ON_LOST:
+                                // TODO: Re-evaluate possible upstreams. Currently upstream
+                                // reevaluation is triggered via received CONNECTIVITY_ACTION
+                                // broadcasts that result in being passed a
+                                // TetherMasterSM.CMD_UPSTREAM_CHANGED.
+                                handleNewUpstreamNetworkState(null);
+                                break;
+                            default:
+                                break;
                         }
                         break;
-                    case EVENT_UPSTREAM_LOST:
-                        // TODO: Re-evaluate possible upstreams. Currently upstream reevaluation
-                        // is triggered via received CONNECTIVITY_ACTION broadcasts that result
-                        // in being passed a TetherMasterSM.CMD_UPSTREAM_CHANGED.
-                        mUpstreamNetworkMonitor.processNetworkLost((Network) message.obj);
-                        break;
+                    }
                     default:
                         retValue = false;
                         break;
diff --git a/services/core/java/com/android/server/connectivity/tethering/IPv6TetheringCoordinator.java b/services/core/java/com/android/server/connectivity/tethering/IPv6TetheringCoordinator.java
new file mode 100644
index 0000000..9173feb
--- /dev/null
+++ b/services/core/java/com/android/server/connectivity/tethering/IPv6TetheringCoordinator.java
@@ -0,0 +1,279 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity.tethering;
+
+import android.net.ConnectivityManager;
+import android.net.IpPrefix;
+import android.net.LinkAddress;
+import android.net.LinkProperties;
+import android.net.Network;
+import android.net.NetworkCapabilities;
+import android.net.NetworkState;
+import android.net.RouteInfo;
+import android.util.Log;
+
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.util.ArrayList;
+import java.util.LinkedList;
+
+
+/**
+ * IPv6 tethering is rather different from IPv4 owing to the absence of NAT.
+ * This coordinator is responsible for evaluating the dedicated prefixes
+ * assigned to the device and deciding how to divvy them up among downstream
+ * interfaces.
+ *
+ * @hide
+ */
+public class IPv6TetheringCoordinator {
+    private static final String TAG = IPv6TetheringCoordinator.class.getSimpleName();
+    private static final boolean DBG = false;
+    private static final boolean VDBG = false;
+
+    private final ArrayList<TetherInterfaceStateMachine> mNotifyList;
+    private final LinkedList<TetherInterfaceStateMachine> mActiveDownstreams;
+    private NetworkState mUpstreamNetworkState;
+
+    public IPv6TetheringCoordinator(ArrayList<TetherInterfaceStateMachine> notifyList) {
+        mNotifyList = notifyList;
+        mActiveDownstreams = new LinkedList<>();
+    }
+
+    public void addActiveDownstream(TetherInterfaceStateMachine downstream) {
+        if (mActiveDownstreams.indexOf(downstream) == -1) {
+            // Adding a new downstream appends it to the list. Adding a
+            // downstream a second time without first removing it has no effect.
+            mActiveDownstreams.offer(downstream);
+            updateIPv6TetheringInterfaces();
+        }
+    }
+
+    public void removeActiveDownstream(TetherInterfaceStateMachine downstream) {
+        stopIPv6TetheringOn(downstream);
+        if (mActiveDownstreams.remove(downstream)) {
+            updateIPv6TetheringInterfaces();
+        }
+    }
+
+    public void updateUpstreamNetworkState(NetworkState ns) {
+        if (VDBG) {
+            Log.d(TAG, "updateUpstreamNetworkState: " + toDebugString(ns));
+        }
+        if (!canTetherIPv6(ns)) {
+            stopIPv6TetheringOnAllInterfaces();
+            setUpstreamNetworkState(null);
+            return;
+        }
+
+        if (mUpstreamNetworkState != null &&
+            !ns.network.equals(mUpstreamNetworkState.network)) {
+            stopIPv6TetheringOnAllInterfaces();
+        }
+
+        setUpstreamNetworkState(ns);
+        updateIPv6TetheringInterfaces();
+    }
+
+    private void stopIPv6TetheringOnAllInterfaces() {
+        for (TetherInterfaceStateMachine sm : mNotifyList) {
+            stopIPv6TetheringOn(sm);
+        }
+    }
+
+    private void setUpstreamNetworkState(NetworkState ns) {
+        if (ns == null) {
+            mUpstreamNetworkState = null;
+        } else {
+            // Make a deep copy of the parts we need.
+            mUpstreamNetworkState = new NetworkState(
+                    null,
+                    new LinkProperties(ns.linkProperties),
+                    new NetworkCapabilities(ns.networkCapabilities),
+                    new Network(ns.network),
+                    null,
+                    null);
+        }
+
+        if (DBG) {
+            Log.d(TAG, "setUpstreamNetworkState: " + toDebugString(mUpstreamNetworkState));
+        }
+    }
+
+    private void updateIPv6TetheringInterfaces() {
+        for (TetherInterfaceStateMachine sm : mNotifyList) {
+            final LinkProperties lp = getInterfaceIPv6LinkProperties(sm);
+            sm.sendMessage(TetherInterfaceStateMachine.CMD_IPV6_TETHER_UPDATE, 0, 0, lp);
+            break;
+        }
+    }
+
+    private LinkProperties getInterfaceIPv6LinkProperties(TetherInterfaceStateMachine sm) {
+        if (mUpstreamNetworkState == null) return null;
+
+        if (sm.interfaceType() == ConnectivityManager.TETHERING_BLUETOOTH) {
+            // TODO: Figure out IPv6 support on PAN interfaces.
+            return null;
+        }
+
+        // NOTE: Here, in future, we would have policies to decide how to divvy
+        // up the available dedicated prefixes among downstream interfaces.
+        // At this time we have no such mechanism--we only support tethering
+        // IPv6 toward the oldest (first requested) active downstream.
+
+        final TetherInterfaceStateMachine currentActive = mActiveDownstreams.peek();
+        if (currentActive != null && currentActive == sm) {
+            final LinkProperties lp = getIPv6OnlyLinkProperties(
+                    mUpstreamNetworkState.linkProperties);
+            if (lp.hasIPv6DefaultRoute() && lp.hasGlobalIPv6Address()) {
+                return lp;
+            }
+        }
+
+        return null;
+    }
+
+    private static boolean canTetherIPv6(NetworkState ns) {
+        // Broadly speaking:
+        //
+        //     [1] does the upstream have an IPv6 default route?
+        //
+        // and
+        //
+        //     [2] does the upstream have one or more global IPv6 /64s
+        //         dedicated to this device?
+        //
+        // In lieu of Prefix Delegation and other evaluation of whether a
+        // prefix may or may not be dedicated to this device, for now just
+        // check whether the upstream is TRANSPORT_CELLULAR. This works
+        // because "[t]he 3GPP network allocates each default bearer a unique
+        // /64 prefix", per RFC 6459, Section 5.2.
+
+        final boolean canTether =
+                (ns != null) && (ns.network != null) &&
+                (ns.linkProperties != null) && (ns.networkCapabilities != null) &&
+                // At least one upstream DNS server:
+                ns.linkProperties.isProvisioned() &&
+                // Minimal amount of IPv6 provisioning:
+                ns.linkProperties.hasIPv6DefaultRoute() &&
+                ns.linkProperties.hasGlobalIPv6Address() &&
+                // Temporary approximation of "dedicated prefix":
+                ns.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
+
+        // For now, we do not support separate IPv4 and IPv6 upstreams (e.g.
+        // tethering with 464xlat involved). TODO: Rectify this shortcoming,
+        // likely by calling NetworkManagementService#startInterfaceForwarding()
+        // for all upstream interfaces.
+        RouteInfo v4default = null;
+        RouteInfo v6default = null;
+        if (canTether) {
+            for (RouteInfo r : ns.linkProperties.getAllRoutes()) {
+                if (r.isIPv4Default()) {
+                    v4default = r;
+                } else if (r.isIPv6Default()) {
+                    v6default = r;
+                }
+
+                if (v4default != null && v6default != null) {
+                    break;
+                }
+            }
+        }
+
+        final boolean supportedConfiguration =
+                (v4default != null) && (v6default != null) &&
+                (v4default.getInterface() != null) &&
+                v4default.getInterface().equals(v6default.getInterface());
+
+        final boolean outcome = canTether && supportedConfiguration;
+
+        if (VDBG) {
+            if (ns == null) {
+                Log.d(TAG, "No available upstream.");
+            } else {
+                Log.d(TAG, String.format("IPv6 tethering is %s for upstream: %s",
+                        (outcome ? "available" : "not available"), toDebugString(ns)));
+            }
+        }
+
+        return outcome;
+    }
+
+    private static LinkProperties getIPv6OnlyLinkProperties(LinkProperties lp) {
+        final LinkProperties v6only = new LinkProperties();
+        if (lp == null) {
+            return v6only;
+        }
+
+        // NOTE: At this time we don't copy over any information about any
+        // stacked links. No current stacked link configuration has IPv6.
+
+        v6only.setInterfaceName(lp.getInterfaceName());
+
+        v6only.setMtu(lp.getMtu());
+
+        for (LinkAddress linkAddr : lp.getLinkAddresses()) {
+            if (linkAddr.isGlobalPreferred() && linkAddr.getPrefixLength() == 64) {
+                v6only.addLinkAddress(linkAddr);
+            }
+        }
+
+        for (RouteInfo routeInfo : lp.getRoutes()) {
+            final IpPrefix destination = routeInfo.getDestination();
+            if ((destination.getAddress() instanceof Inet6Address) &&
+                (destination.getPrefixLength() <= 64)) {
+                v6only.addRoute(routeInfo);
+            }
+        }
+
+        for (InetAddress dnsServer : lp.getDnsServers()) {
+            if (isIPv6GlobalAddress(dnsServer)) {
+                // For now we include ULAs.
+                v6only.addDnsServer(dnsServer);
+            }
+        }
+
+        v6only.setDomains(lp.getDomains());
+
+        return v6only;
+    }
+
+    // TODO: Delete this and switch to LinkAddress#isGlobalPreferred once we
+    // announce our own IPv6 address as DNS server.
+    private static boolean isIPv6GlobalAddress(InetAddress ip) {
+        return (ip instanceof Inet6Address) &&
+               !ip.isAnyLocalAddress() &&
+               !ip.isLoopbackAddress() &&
+               !ip.isLinkLocalAddress() &&
+               !ip.isSiteLocalAddress() &&
+               !ip.isMulticastAddress();
+    }
+
+    private static String toDebugString(NetworkState ns) {
+        if (ns == null) {
+            return "NetworkState{null}";
+        }
+        return String.format("NetworkState{%s, %s, %s}",
+                ns.network,
+                ns.networkCapabilities,
+                ns.linkProperties);
+    }
+
+    private static void stopIPv6TetheringOn(TetherInterfaceStateMachine sm) {
+        sm.sendMessage(TetherInterfaceStateMachine.CMD_IPV6_TETHER_UPDATE, 0, 0, null);
+    }
+}
diff --git a/services/core/java/com/android/server/connectivity/tethering/IPv6TetheringInterfaceServices.java b/services/core/java/com/android/server/connectivity/tethering/IPv6TetheringInterfaceServices.java
new file mode 100644
index 0000000..c2c1a8c
--- /dev/null
+++ b/services/core/java/com/android/server/connectivity/tethering/IPv6TetheringInterfaceServices.java
@@ -0,0 +1,280 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity.tethering;
+
+import android.net.INetd;
+import android.net.IpPrefix;
+import android.net.LinkAddress;
+import android.net.LinkProperties;
+import android.net.NetworkCapabilities;
+import android.net.NetworkState;
+import android.net.RouteInfo;
+import android.net.ip.RouterAdvertisementDaemon;
+import android.net.ip.RouterAdvertisementDaemon.RaParams;
+import android.net.util.NetdService;
+import android.os.INetworkManagementService;
+import android.os.ServiceSpecificException;
+import android.os.RemoteException;
+import android.util.Log;
+import android.util.Slog;
+
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Objects;
+
+
+/**
+ * @hide
+ */
+class IPv6TetheringInterfaceServices {
+    private static final String TAG = IPv6TetheringInterfaceServices.class.getSimpleName();
+    private static final IpPrefix LINK_LOCAL_PREFIX = new IpPrefix("fe80::/64");
+    private static final int RFC7421_IP_PREFIX_LENGTH = 64;
+
+    private final String mIfName;
+    private final INetworkManagementService mNMService;
+
+    private NetworkInterface mNetworkInterface;
+    private byte[] mHwAddr;
+    private LinkProperties mLastIPv6LinkProperties;
+    private RouterAdvertisementDaemon mRaDaemon;
+    private RaParams mLastRaParams;
+
+    IPv6TetheringInterfaceServices(String ifname, INetworkManagementService nms) {
+        mIfName = ifname;
+        mNMService = nms;
+    }
+
+    public boolean start() {
+        try {
+            mNetworkInterface = NetworkInterface.getByName(mIfName);
+        } catch (SocketException e) {
+            Log.e(TAG, "Failed to find NetworkInterface for " + mIfName, e);
+            stop();
+            return false;
+        }
+
+        try {
+            mHwAddr = mNetworkInterface.getHardwareAddress();
+        } catch (SocketException e) {
+            Log.e(TAG, "Failed to find hardware address for " + mIfName, e);
+            stop();
+            return false;
+        }
+
+        final int ifindex = mNetworkInterface.getIndex();
+        mRaDaemon = new RouterAdvertisementDaemon(mIfName, ifindex, mHwAddr);
+        if (!mRaDaemon.start()) {
+            stop();
+            return false;
+        }
+
+        return true;
+    }
+
+    public void stop() {
+        mNetworkInterface = null;
+        mHwAddr = null;
+        setRaParams(null);
+
+        if (mRaDaemon != null) {
+            mRaDaemon.stop();
+            mRaDaemon = null;
+        }
+    }
+
+    // IPv6TetheringCoordinator sends updates with carefully curated IPv6-only
+    // LinkProperties. These have extraneous data filtered out and only the
+    // necessary prefixes included (per its prefix distribution policy).
+    //
+    // TODO: Evaluate using a data structure than is more directly suited to
+    // communicating only the relevant information.
+    public void updateUpstreamIPv6LinkProperties(LinkProperties v6only) {
+        if (mRaDaemon == null) return;
+
+        // Avoid unnecessary work on spurious updates.
+        if (Objects.equals(mLastIPv6LinkProperties, v6only)) {
+            return;
+        }
+
+        RaParams params = null;
+
+        if (v6only != null) {
+            params = new RaParams();
+            params.mtu = v6only.getMtu();
+            params.hasDefaultRoute = v6only.hasIPv6DefaultRoute();
+
+            for (LinkAddress linkAddr : v6only.getLinkAddresses()) {
+                if (linkAddr.getPrefixLength() != RFC7421_IP_PREFIX_LENGTH) continue;
+
+                final IpPrefix prefix = new IpPrefix(
+                        linkAddr.getAddress(), linkAddr.getPrefixLength());
+                params.prefixes.add(prefix);
+
+                final Inet6Address dnsServer = getLocalDnsIpFor(prefix);
+                if (dnsServer != null) {
+                    params.dnses.add(dnsServer);
+                }
+            }
+        }
+        // If v6only is null, we pass in null to setRaParams(), which handles
+        // deprecation of any existing RA data.
+
+        setRaParams(params);
+        mLastIPv6LinkProperties = v6only;
+    }
+
+
+    private void configureLocalRoutes(
+            HashSet<IpPrefix> deprecatedPrefixes, HashSet<IpPrefix> newPrefixes) {
+        // [1] Remove the routes that are deprecated.
+        if (!deprecatedPrefixes.isEmpty()) {
+            final ArrayList<RouteInfo> toBeRemoved = getLocalRoutesFor(deprecatedPrefixes);
+            try {
+                final int removalFailures = mNMService.removeRoutesFromLocalNetwork(toBeRemoved);
+                if (removalFailures > 0) {
+                    Log.e(TAG, String.format("Failed to remove %d IPv6 routes from local table.",
+                            removalFailures));
+                }
+            } catch (RemoteException e) {
+                Log.e(TAG, "Failed to remove IPv6 routes from local table: ", e);
+            }
+        }
+
+        // [2] Add only the routes that have not previously been added.
+        if (newPrefixes != null && !newPrefixes.isEmpty()) {
+            HashSet<IpPrefix> addedPrefixes = (HashSet) newPrefixes.clone();
+            if (mLastRaParams != null) {
+                addedPrefixes.removeAll(mLastRaParams.prefixes);
+            }
+
+            if (mLastRaParams == null || mLastRaParams.prefixes.isEmpty()) {
+                // We need to be able to send unicast RAs, and clients might
+                // like to ping the default router's link-local address.  Note
+                // that we never remove the link-local route from the network
+                // until Tethering disables tethering on the interface. We
+                // only need to add the link-local prefix once, but in the
+                // event we add it more than once netd silently ignores EEXIST.
+                addedPrefixes.add(LINK_LOCAL_PREFIX);
+            }
+
+            if (!addedPrefixes.isEmpty()) {
+                final ArrayList<RouteInfo> toBeAdded = getLocalRoutesFor(addedPrefixes);
+                try {
+                    // It's safe to call addInterfaceToLocalNetwork() even if
+                    // the interface is already in the local_network. Note also
+                    // that adding routes that already exist does not cause an
+                    // error (EEXIST is silently ignored).
+                    mNMService.addInterfaceToLocalNetwork(mIfName, toBeAdded);
+                } catch (RemoteException e) {
+                    Log.e(TAG, "Failed to add IPv6 routes to local table: ", e);
+                }
+            }
+        }
+    }
+
+    private void configureLocalDns(
+            HashSet<Inet6Address> deprecatedDnses, HashSet<Inet6Address> newDnses) {
+        final INetd netd = NetdService.getInstance();
+        if (netd == null) {
+            if (newDnses != null) newDnses.clear();
+            Log.e(TAG, "No netd service instance available; not setting local IPv6 addresses");
+            return;
+        }
+
+        // [1] Remove deprecated local DNS IP addresses.
+        if (!deprecatedDnses.isEmpty()) {
+            for (Inet6Address dns : deprecatedDnses) {
+                final String dnsString = dns.getHostAddress();
+                try {
+                    netd.interfaceDelAddress(mIfName, dnsString, RFC7421_IP_PREFIX_LENGTH);
+                } catch (ServiceSpecificException | RemoteException e) {
+                    Log.e(TAG, "Failed to remove local dns IP: " + dnsString, e);
+                }
+            }
+        }
+
+        // [2] Add only the local DNS IP addresses that have not previously been added.
+        if (newDnses != null && !newDnses.isEmpty()) {
+            final HashSet<Inet6Address> addedDnses = (HashSet) newDnses.clone();
+            if (mLastRaParams != null) {
+                addedDnses.removeAll(mLastRaParams.dnses);
+            }
+
+            for (Inet6Address dns : addedDnses) {
+                final String dnsString = dns.getHostAddress();
+                try {
+                    netd.interfaceAddAddress(mIfName, dnsString, RFC7421_IP_PREFIX_LENGTH);
+                } catch (ServiceSpecificException | RemoteException e) {
+                    Log.e(TAG, "Failed to add local dns IP: " + dnsString, e);
+                    newDnses.remove(dns);
+                }
+            }
+        }
+
+        try {
+            netd.tetherApplyDnsInterfaces();
+        } catch (ServiceSpecificException | RemoteException e) {
+            Log.e(TAG, "Failed to update local DNS caching server");
+            if (newDnses != null) newDnses.clear();
+        }
+    }
+
+    private void setRaParams(RaParams newParams) {
+        if (mRaDaemon != null) {
+            final RaParams deprecatedParams =
+                    RaParams.getDeprecatedRaParams(mLastRaParams, newParams);
+
+            configureLocalRoutes(deprecatedParams.prefixes,
+                    (newParams != null) ? newParams.prefixes : null);
+
+            configureLocalDns(deprecatedParams.dnses,
+                    (newParams != null) ? newParams.dnses : null);
+
+            mRaDaemon.buildNewRa(deprecatedParams, newParams);
+        }
+
+        mLastRaParams = newParams;
+    }
+
+    // Accumulate routes representing "prefixes to be assigned to the local
+    // interface", for subsequent modification of local_network routing.
+    private ArrayList<RouteInfo> getLocalRoutesFor(HashSet<IpPrefix> prefixes) {
+        final ArrayList<RouteInfo> localRoutes = new ArrayList<RouteInfo>();
+        for (IpPrefix ipp : prefixes) {
+            localRoutes.add(new RouteInfo(ipp, null, mIfName));
+        }
+        return localRoutes;
+    }
+
+    // Given a prefix like 2001:db8::/64 return 2001:db8::1.
+    private static Inet6Address getLocalDnsIpFor(IpPrefix localPrefix) {
+        final byte[] dnsBytes = localPrefix.getRawAddress();
+        dnsBytes[dnsBytes.length - 1] = 0x1;
+        try {
+            return Inet6Address.getByAddress(null, dnsBytes, 0);
+        } catch (UnknownHostException e) {
+            Slog.wtf(TAG, "Failed to construct Inet6Address from: " + localPrefix);
+            return null;
+        }
+    }
+}
diff --git a/services/core/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachine.java b/services/core/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachine.java
index 76ad9d7..6ca4e27 100644
--- a/services/core/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachine.java
+++ b/services/core/java/com/android/server/connectivity/tethering/TetherInterfaceStateMachine.java
@@ -20,6 +20,7 @@
 import android.net.INetworkStatsService;
 import android.net.InterfaceConfiguration;
 import android.net.LinkAddress;
+import android.net.LinkProperties;
 import android.net.NetworkUtils;
 import android.os.INetworkManagementService;
 import android.os.Looper;
@@ -73,6 +74,8 @@
     public static final int CMD_SET_DNS_FORWARDERS_ERROR    = BASE_IFACE + 11;
     // the upstream connection has changed
     public static final int CMD_TETHER_CONNECTION_CHANGED   = BASE_IFACE + 12;
+    // new IPv6 tethering parameters need to be processed
+    public static final int CMD_IPV6_TETHER_UPDATE          = BASE_IFACE + 13;
 
     private final State mInitialState;
     private final State mTetheredState;
@@ -84,6 +87,7 @@
 
     private final String mIfaceName;
     private final int mInterfaceType;
+    private final IPv6TetheringInterfaceServices mIPv6TetherSvc;
 
     private int mLastError;
     private String mMyUpstreamIfaceName;  // may change over time
@@ -97,6 +101,7 @@
         mTetherController = tetherController;
         mIfaceName = ifaceName;
         mInterfaceType = interfaceType;
+        mIPv6TetherSvc = new IPv6TetheringInterfaceServices(mIfaceName, mNMService);
         mLastError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
 
         mInitialState = new InitialState();
@@ -186,6 +191,10 @@
                 case CMD_INTERFACE_DOWN:
                     transitionTo(mUnavailableState);
                     break;
+                case CMD_IPV6_TETHER_UPDATE:
+                    mIPv6TetherSvc.updateUpstreamIPv6LinkProperties(
+                            (LinkProperties) message.obj);
+                    break;
                 default:
                     retValue = false;
                     break;
@@ -211,6 +220,11 @@
                 transitionTo(mInitialState);
                 return;
             }
+
+            if (!mIPv6TetherSvc.start()) {
+                Log.e(TAG, "Failed to start IPv6TetheringInterfaceServices");
+            }
+
             if (DBG) Log.d(TAG, "Tethered " + mIfaceName);
             mTetherController.notifyInterfaceStateChange(
                     mIfaceName, TetherInterfaceStateMachine.this,
@@ -222,6 +236,7 @@
             // Note that at this point, we're leaving the tethered state.  We can fail any
             // of these operations, but it doesn't really change that we have to try them
             // all in sequence.
+            mIPv6TetherSvc.stop();
             cleanupUpstream();
 
             try {
@@ -298,6 +313,10 @@
                     }
                     mMyUpstreamIfaceName = newUpstreamIfaceName;
                     break;
+                case CMD_IPV6_TETHER_UPDATE:
+                    mIPv6TetherSvc.updateUpstreamIPv6LinkProperties(
+                            (LinkProperties) message.obj);
+                    break;
                 case CMD_IP_FORWARDING_ENABLE_ERROR:
                 case CMD_IP_FORWARDING_DISABLE_ERROR:
                 case CMD_START_TETHERING_ERROR:
diff --git a/services/core/java/com/android/server/content/ContentService.java b/services/core/java/com/android/server/content/ContentService.java
index 01b2393..4e236d1 100644
--- a/services/core/java/com/android/server/content/ContentService.java
+++ b/services/core/java/com/android/server/content/ContentService.java
@@ -482,8 +482,7 @@
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
                 syncManager.scheduleSync(account, userId, uId, authority, extras,
-                        0 /* no delay */, 0 /* no delay */,
-                        false /* onlyThoseWithUnkownSyncableState */);
+                        SyncStorageEngine.AuthorityInfo.UNDEFINED);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -547,12 +546,9 @@
                 getSyncManager().updateOrAddPeriodicSync(info, runAtTime,
                         flextime, extras);
             } else {
-                long beforeRuntimeMillis = (flextime) * 1000;
-                long runtimeMillis = runAtTime * 1000;
                 syncManager.scheduleSync(
                         request.getAccount(), userId, callerUid, request.getProvider(), extras,
-                        beforeRuntimeMillis, runtimeMillis,
-                        false /* onlyThoseWithUnknownSyncableState */);
+                        SyncStorageEngine.AuthorityInfo.UNDEFINED);
             }
         } finally {
             restoreCallingIdentity(identityToken);
@@ -841,7 +837,7 @@
         try {
             SyncManager syncManager = getSyncManager();
             if (syncManager != null) {
-                return syncManager.getIsSyncable(
+                return syncManager.computeSyncable(
                         account, userId, providerName);
             }
         } finally {
diff --git a/services/core/java/com/android/server/content/SyncManager.java b/services/core/java/com/android/server/content/SyncManager.java
index 39ddc3a..6870c56 100644
--- a/services/core/java/com/android/server/content/SyncManager.java
+++ b/services/core/java/com/android/server/content/SyncManager.java
@@ -19,6 +19,7 @@
 import android.accounts.Account;
 import android.accounts.AccountAndUser;
 import android.accounts.AccountManager;
+import android.accounts.AccountManagerInternal;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
 import android.app.AppGlobals;
@@ -47,6 +48,7 @@
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.pm.PackageManagerInternal;
 import android.content.pm.ProviderInfo;
 import android.content.pm.RegisteredServicesCache;
 import android.content.pm.RegisteredServicesCacheListener;
@@ -64,6 +66,7 @@
 import android.os.Message;
 import android.os.Messenger;
 import android.os.PowerManager;
+import android.os.RemoteCallback;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemClock;
@@ -79,6 +82,7 @@
 import android.util.Pair;
 import android.util.Slog;
 
+import com.android.internal.util.ArrayUtils;
 import com.android.server.LocalServices;
 import com.android.server.job.JobSchedulerInternal;
 import com.google.android.collect.Lists;
@@ -133,6 +137,8 @@
 public class SyncManager {
     static final String TAG = "SyncManager";
 
+    private static final boolean DEBUG_ACCOUNT_ACCESS = false;
+
     /** Delay a sync due to local changes this long. In milliseconds */
     private static final long LOCAL_SYNC_DELAY;
 
@@ -194,6 +200,11 @@
     private static final String HANDLE_SYNC_ALARM_WAKE_LOCK = "SyncManagerHandleSyncAlarm";
     private static final String SYNC_LOOP_WAKE_LOCK = "SyncLoopWakeLock";
 
+
+    private static final int SYNC_OP_STATE_VALID = 0;
+    private static final int SYNC_OP_STATE_INVALID = 1;
+    private static final int SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS = 2;
+
     private Context mContext;
 
     private static final AccountAndUser[] INITIAL_ACCOUNTS_ARRAY = new AccountAndUser[0];
@@ -310,6 +321,12 @@
 
     private final UserManager mUserManager;
 
+    private final AccountManager mAccountManager;
+
+    private final AccountManagerInternal mAccountManagerInternal;
+
+    private final PackageManagerInternal mPackageManagerInternal;
+
     private List<UserInfo> getAllUsers() {
         return mUserManager.getUsers();
     }
@@ -490,9 +507,7 @@
             @Override
             public void onSyncRequest(SyncStorageEngine.EndPoint info, int reason, Bundle extras) {
                 scheduleSync(info.account, info.userId, reason, info.provider, extras,
-                        0 /* no flexMillis */,
-                        0 /* run immediately */,
-                        false);
+                        AuthorityInfo.UNDEFINED);
             }
         });
 
@@ -522,8 +537,7 @@
                 if (!removed) {
                     scheduleSync(null, UserHandle.USER_ALL,
                             SyncOperation.REASON_SERVICE_CHANGED,
-                            type.authority, null, 0 /* no delay */, 0 /* no delay */,
-                            false /* onlyThoseWithUnkownSyncableState */);
+                            type.authority, null, AuthorityInfo.UNDEFINED);
                 }
             }
         }, mSyncHandler);
@@ -562,6 +576,19 @@
         }
         mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+        mAccountManager = (AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE);
+        mAccountManagerInternal = LocalServices.getService(AccountManagerInternal.class);
+        mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
+
+        mAccountManagerInternal.addOnAppPermissionChangeListener((Account account, int uid) -> {
+            // If the UID gained access to the account kick-off syncs lacking account access
+            if (mAccountManagerInternal.hasAccountAccess(account, uid)) {
+                scheduleSync(account, UserHandle.getUserId(uid),
+                        SyncOperation.REASON_ACCOUNTS_UPDATED,
+                        null, null, AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS);
+            }
+        });
+
         mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
                 BatteryStats.SERVICE_NAME));
 
@@ -631,6 +658,37 @@
                 mContext.startService(startServiceIntent);
             }
         });
+
+        // Sync adapters were able to access the synced account without the accounts
+        // permission which circumvents our permission model. Therefore, we require
+        // sync adapters that don't have access to the account to get user consent.
+        // This can be noisy, therefore we will white-list sync adapters installed
+        // before we started checking for account access because they already know
+        // the account (they run before) which is the genie is out of the bottle.
+        whiteListExistingSyncAdaptersIfNeeded();
+    }
+
+    private void whiteListExistingSyncAdaptersIfNeeded() {
+        if (!mSyncStorageEngine.shouldGrantSyncAdaptersAccountAccess()) {
+            return;
+        }
+        List<UserInfo> users = mUserManager.getUsers(true);
+        final int userCount = users.size();
+        for (int i = 0; i < userCount; i++) {
+            UserHandle userHandle = users.get(i).getUserHandle();
+            final int userId = userHandle.getIdentifier();
+            for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> service
+                    : mSyncAdapters.getAllServices(userId)) {
+                String packageName = service.componentName.getPackageName();
+                for (Account account : mAccountManager.getAccountsByTypeAsUser(
+                        service.type.accountType, userHandle)) {
+                    if (!canAccessAccount(account, packageName, userId)) {
+                        mAccountManager.updateAppPermission(account,
+                                AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, service.uid, true);
+                    }
+                }
+            }
+        }
     }
 
     private boolean isDeviceProvisioned() {
@@ -655,7 +713,7 @@
         return mSyncStorageEngine;
     }
 
-    public int getIsSyncable(Account account, int userId, String providerName) {
+    private int getIsSyncable(Account account, int userId, String providerName) {
         int isSyncable = mSyncStorageEngine.getIsSyncable(account, userId, providerName);
         UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId);
 
@@ -666,22 +724,22 @@
         RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
                 mSyncAdapters.getServiceInfo(
                         SyncAdapterType.newKey(providerName, account.type), userId);
-        if (syncAdapterInfo == null) return isSyncable;
+        if (syncAdapterInfo == null) return AuthorityInfo.NOT_SYNCABLE;
 
         PackageInfo pInfo = null;
         try {
             pInfo = AppGlobals.getPackageManager().getPackageInfo(
                     syncAdapterInfo.componentName.getPackageName(), 0, userId);
-            if (pInfo == null) return isSyncable;
+            if (pInfo == null) return AuthorityInfo.NOT_SYNCABLE;
         } catch (RemoteException re) {
             // Shouldn't happen.
-            return isSyncable;
+            return AuthorityInfo.NOT_SYNCABLE;
         }
         if (pInfo.restrictedAccountType != null
                 && pInfo.restrictedAccountType.equals(account.type)) {
             return isSyncable;
         } else {
-            return 0;
+            return AuthorityInfo.NOT_SYNCABLE;
         }
     }
 
@@ -733,13 +791,11 @@
      * @param extras a Map of SyncAdapter-specific information to control
      *          syncs of a specific provider. Can be null. Is ignored
      *          if the url is null.
-     * @param beforeRuntimeMillis milliseconds before runtimeMillis that this sync can run.
-     * @param runtimeMillis maximum milliseconds in the future to wait before performing sync.
-     * @param onlyThoseWithUnkownSyncableState Only sync authorities that have unknown state.
+     * @param targetSyncState Only sync authorities that have the specified sync state.
+     *           Use {@link AuthorityInfo#UNDEFINED} to sync all authorities.
      */
     public void scheduleSync(Account requestedAccount, int userId, int reason,
-            String requestedAuthority, Bundle extras, long beforeRuntimeMillis,
-            long runtimeMillis, boolean onlyThoseWithUnkownSyncableState) {
+            String requestedAuthority, Bundle extras, int targetSyncState) {
         final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
         if (extras == null) {
             extras = new Bundle();
@@ -749,17 +805,27 @@
                     + requestedAuthority);
         }
 
-        AccountAndUser[] accounts;
-        if (requestedAccount != null && userId != UserHandle.USER_ALL) {
-            accounts = new AccountAndUser[] { new AccountAndUser(requestedAccount, userId) };
+        AccountAndUser[] accounts = null;
+        if (requestedAccount != null) {
+            if (userId != UserHandle.USER_ALL) {
+                accounts = new AccountAndUser[]{new AccountAndUser(requestedAccount, userId)};
+            } else {
+                for (AccountAndUser runningAccount : mRunningAccounts) {
+                    if (requestedAccount.equals(runningAccount.account)) {
+                        accounts = ArrayUtils.appendElement(AccountAndUser.class,
+                                accounts, runningAccount);
+                    }
+                }
+            }
         } else {
             accounts = mRunningAccounts;
-            if (accounts.length == 0) {
-                if (isLoggable) {
-                    Slog.v(TAG, "scheduleSync: no accounts configured, dropping");
-                }
-                return;
+        }
+
+        if (ArrayUtils.isEmpty(accounts)) {
+            if (isLoggable) {
+                Slog.v(TAG, "scheduleSync: no accounts configured, dropping");
             }
+            return;
         }
 
         final boolean uploadOnly = extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false);
@@ -808,29 +874,45 @@
             }
 
             for (String authority : syncableAuthorities) {
-                int isSyncable = getIsSyncable(account.account, account.userId,
-                        authority);
+                int isSyncable = computeSyncable(account.account, account.userId, authority);
+
                 if (isSyncable == AuthorityInfo.NOT_SYNCABLE) {
                     continue;
                 }
-                final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo;
-                syncAdapterInfo = mSyncAdapters.getServiceInfo(
-                        SyncAdapterType.newKey(authority, account.account.type), account.userId);
+
+                final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
+                        mSyncAdapters.getServiceInfo(SyncAdapterType.newKey(authority,
+                                account.account.type), account.userId);
                 if (syncAdapterInfo == null) {
                     continue;
                 }
+
                 final int owningUid = syncAdapterInfo.uid;
-                final String owningPackage = syncAdapterInfo.componentName.getPackageName();
-                try {
-                    if (ActivityManagerNative.getDefault().getAppStartMode(owningUid,
-                            owningPackage) == ActivityManager.APP_START_MODE_DISABLED) {
-                        Slog.w(TAG, "Not scheduling job " + syncAdapterInfo.uid + ":"
-                                + syncAdapterInfo.componentName
-                                + " -- package not allowed to start");
+
+                if (isSyncable == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
+                    if (isLoggable) {
+                        Slog.v(TAG, "    Not scheduling sync operation: "
+                                + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
+                    }
+                    Bundle finalExtras = new Bundle(extras);
+                    String packageName = syncAdapterInfo.componentName.getPackageName();
+                    // If the app did not run and has no account access, done
+                    if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
                         continue;
                     }
-                } catch (RemoteException e) {
+                    mAccountManagerInternal.requestAccountAccess(account.account,
+                            packageName, userId,
+                            new RemoteCallback((Bundle result) -> {
+                                if (result != null
+                                        && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
+                                    scheduleSync(account.account, userId, reason, authority,
+                                            finalExtras, targetSyncState);
+                                }
+                            }
+                        ));
+                    continue;
                 }
+
                 final boolean allowParallelSyncs = syncAdapterInfo.type.allowParallelSyncs();
                 final boolean isAlwaysSyncable = syncAdapterInfo.type.isAlwaysSyncable();
                 if (isSyncable < 0 && isAlwaysSyncable) {
@@ -838,9 +920,11 @@
                             account.account, account.userId, authority, AuthorityInfo.SYNCABLE);
                     isSyncable = AuthorityInfo.SYNCABLE;
                 }
-                if (onlyThoseWithUnkownSyncableState && isSyncable >= 0) {
+
+                if (targetSyncState != AuthorityInfo.UNDEFINED && targetSyncState != isSyncable) {
                     continue;
                 }
+
                 if (!syncAdapterInfo.type.supportsUploading() && uploadOnly) {
                     continue;
                 }
@@ -863,7 +947,10 @@
                                 account.account, authority, account.userId);
                 long delayUntil =
                         mSyncStorageEngine.getDelayUntilTime(info);
-                if (isSyncable < 0) {
+
+                final String owningPackage = syncAdapterInfo.componentName.getPackageName();
+
+                if (isSyncable == AuthorityInfo.NOT_INITIALIZED) {
                     // Initialisation sync.
                     Bundle newExtras = new Bundle();
                     newExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true);
@@ -882,13 +969,11 @@
                                     owningUid, owningPackage, reason, source,
                                     authority, newExtras, allowParallelSyncs)
                     );
-                }
-                if (!onlyThoseWithUnkownSyncableState) {
+                } else if (targetSyncState == AuthorityInfo.UNDEFINED
+                        || targetSyncState == isSyncable) {
                     if (isLoggable) {
                         Slog.v(TAG, "scheduleSync:"
                                 + " delay until " + delayUntil
-                                + " run by " + runtimeMillis
-                                + " flexMillis " + beforeRuntimeMillis
                                 + ", source " + source
                                 + ", account " + account
                                 + ", authority " + authority
@@ -904,6 +989,56 @@
         }
     }
 
+    public int computeSyncable(Account account, int userId, String authority) {
+        final int status = getIsSyncable(account, userId, authority);
+        if (status == AuthorityInfo.NOT_SYNCABLE) {
+            return AuthorityInfo.NOT_SYNCABLE;
+        }
+        final SyncAdapterType type = SyncAdapterType.newKey(authority, account.type);
+        final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
+                mSyncAdapters.getServiceInfo(type, userId);
+        if (syncAdapterInfo == null) {
+            return AuthorityInfo.NOT_SYNCABLE;
+        }
+        final int owningUid = syncAdapterInfo.uid;
+        final String owningPackage = syncAdapterInfo.componentName.getPackageName();
+        try {
+            if (ActivityManagerNative.getDefault().getAppStartMode(owningUid,
+                    owningPackage) == ActivityManager.APP_START_MODE_DISABLED) {
+                Slog.w(TAG, "Not scheduling job " + syncAdapterInfo.uid + ":"
+                        + syncAdapterInfo.componentName
+                        + " -- package not allowed to start");
+                return AuthorityInfo.NOT_SYNCABLE;
+            }
+        } catch (RemoteException e) {
+            /* ignore - local call */
+        }
+        if (!canAccessAccount(account, owningPackage, owningUid)) {
+            Log.w(TAG, "Access to " + account + " denied for package "
+                    + owningPackage + " in UID " + syncAdapterInfo.uid);
+            return AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS;
+        }
+
+        return status;
+    }
+
+    private boolean canAccessAccount(Account account, String packageName, int uid) {
+        if (mAccountManager.hasAccountAccess(account, packageName,
+                UserHandle.getUserHandleForUid(uid))) {
+            return true;
+        }
+        // We relax the account access rule to also include the system apps as
+        // they are trusted and we want to minimize the cases where the user
+        // involvement is required to grant access to the synced account.
+        try {
+            mContext.getPackageManager().getApplicationInfoAsUser(packageName,
+                    PackageManager.MATCH_SYSTEM_ONLY, UserHandle.getUserId(uid));
+            return true;
+        } catch (NameNotFoundException e) {
+            return false;
+        }
+    }
+
     private void removeSyncsForAuthority(EndPoint info) {
         verifyJobScheduler();
         List<SyncOperation> ops = getAllPendingSyncs();
@@ -960,9 +1095,7 @@
         final Bundle extras = new Bundle();
         extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
         scheduleSync(account, userId, reason, authority, extras,
-                LOCAL_SYNC_DELAY /* earliest run time */,
-                2 * LOCAL_SYNC_DELAY /* latest sync time. */,
-                false /* onlyThoseWithUnkownSyncableState */);
+                AuthorityInfo.UNDEFINED);
     }
 
     public SyncAdapterType[] getSyncAdapterTypes(int userId) {
@@ -1421,8 +1554,7 @@
                 mContext.getOpPackageName());
         for (Account account : accounts) {
             scheduleSync(account, userId, SyncOperation.REASON_USER_START, null, null,
-                    0 /* no delay */, 0 /* No flexMillis */,
-                    true /* onlyThoseWithUnknownSyncableState */);
+                    AuthorityInfo.NOT_INITIALIZED);
         }
     }
 
@@ -2530,13 +2662,18 @@
                 }
             }
 
-            if (isOperationValid(op)) {
-                if (!dispatchSyncOperation(op)) {
+            final int syncOpState = computeSyncOpState(op);
+            switch (syncOpState) {
+                case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS:
+                case SYNC_OP_STATE_INVALID: {
                     mSyncJobService.callJobFinished(op.jobId, false);
-                }
-            } else {
+                } return;
+            }
+
+            if (!dispatchSyncOperation(op)) {
                 mSyncJobService.callJobFinished(op.jobId, false);
             }
+
             setAuthorityPendingState(op.target);
         }
 
@@ -2596,8 +2733,8 @@
 
             if (syncTargets != null) {
                 scheduleSync(syncTargets.account, syncTargets.userId,
-                        SyncOperation.REASON_ACCOUNTS_UPDATED, syncTargets.provider, null, 0, 0,
-                        true);
+                        SyncOperation.REASON_ACCOUNTS_UPDATED, syncTargets.provider,
+                null, AuthorityInfo.NOT_INITIALIZED);
             }
         }
 
@@ -2665,6 +2802,31 @@
                     SyncStorageEngine.SOURCE_PERIODIC, extras,
                     syncAdapterInfo.type.allowParallelSyncs(), true, SyncOperation.NO_JOB_ID,
                     pollFrequencyMillis, flexMillis);
+
+            final int syncOpState = computeSyncOpState(op);
+            switch (syncOpState) {
+                case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS: {
+                    String packageName = op.owningPackage;
+                    final int userId = UserHandle.getUserId(op.owningUid);
+                    // If the app did not run and has no account access, done
+                    if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
+                        return;
+                    }
+                    mAccountManagerInternal.requestAccountAccess(op.target.account,
+                            packageName, userId, new RemoteCallback((Bundle result) -> {
+                                if (result != null
+                                        && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
+                                    updateOrAddPeriodicSync(target, pollFrequency, flex, extras);
+                                }
+                            }
+                        ));
+                } return;
+
+                case SYNC_OP_STATE_INVALID: {
+                    return;
+                }
+            }
+
             scheduleSyncOperationH(op);
             mSyncStorageEngine.reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
         }
@@ -2725,29 +2887,38 @@
         /**
          * Determine if a sync is no longer valid and should be dropped.
          */
-        private boolean isOperationValid(SyncOperation op) {
+        private int computeSyncOpState(SyncOperation op) {
             final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
             int state;
             final EndPoint target = op.target;
-            boolean syncEnabled = mSyncStorageEngine.getMasterSyncAutomatically(target.userId);
+
             // Drop the sync if the account of this operation no longer exists.
             AccountAndUser[] accounts = mRunningAccounts;
             if (!containsAccountAndUser(accounts, target.account, target.userId)) {
                 if (isLoggable) {
                     Slog.v(TAG, "    Dropping sync operation: account doesn't exist.");
                 }
-                return false;
+                return SYNC_OP_STATE_INVALID;
             }
             // Drop this sync request if it isn't syncable.
-            state = getIsSyncable(target.account, target.userId, target.provider);
-            if (state == 0) {
+            state = computeSyncable(target.account, target.userId, target.provider);
+            if (state == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
                 if (isLoggable) {
-                    Slog.v(TAG, "    Dropping sync operation: isSyncable == 0.");
+                    Slog.v(TAG, "    Dropping sync operation: "
+                            + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
                 }
-                return false;
+                return SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS;
             }
-            syncEnabled = syncEnabled && mSyncStorageEngine.getSyncAutomatically(
-                    target.account, target.userId, target.provider);
+            if (state == AuthorityInfo.NOT_SYNCABLE) {
+                if (isLoggable) {
+                    Slog.v(TAG, "    Dropping sync operation: isSyncable == NOT_SYNCABLE");
+                }
+                return SYNC_OP_STATE_INVALID;
+            }
+
+            final boolean syncEnabled = mSyncStorageEngine.getMasterSyncAutomatically(target.userId)
+                    && mSyncStorageEngine.getSyncAutomatically(target.account,
+                            target.userId, target.provider);
 
             // We ignore system settings that specify the sync is invalid if:
             // 1) It's manual - we try it anyway. When/if it fails it will be rescheduled.
@@ -2760,9 +2931,9 @@
                 if (isLoggable) {
                     Slog.v(TAG, "    Dropping sync operation: disallowed by settings/network.");
                 }
-                return false;
+                return SYNC_OP_STATE_INVALID;
             }
-            return true;
+            return SYNC_OP_STATE_VALID;
         }
 
         private boolean dispatchSyncOperation(SyncOperation op) {
diff --git a/services/core/java/com/android/server/content/SyncStorageEngine.java b/services/core/java/com/android/server/content/SyncStorageEngine.java
index bc3fc6a..069ae73 100644
--- a/services/core/java/com/android/server/content/SyncStorageEngine.java
+++ b/services/core/java/com/android/server/content/SyncStorageEngine.java
@@ -137,7 +137,7 @@
     private static final boolean SYNC_ENABLED_DEFAULT = false;
 
     // the version of the accounts xml file format
-    private static final int ACCOUNTS_VERSION = 2;
+    private static final int ACCOUNTS_VERSION = 3;
 
     private static HashMap<String, String> sAuthorityRenames;
     private static PeriodicSyncAddedListener mPeriodicSyncAddedListener;
@@ -211,6 +211,12 @@
 
     public static class AuthorityInfo {
         // Legal values of getIsSyncable
+
+        /**
+         * The syncable state is undefined.
+         */
+        public static final int UNDEFINED = -2;
+
         /**
          * Default state for a newly installed adapter. An uninitialized adapter will receive an
          * initialization sync which are governed by a different set of rules to that of regular
@@ -234,6 +240,12 @@
          */
         public static final int SYNCABLE_NOT_INITIALIZED = 2;
 
+        /**
+         * The adapter is syncable but does not have access to the synced account and needs a
+         * user access approval.
+         */
+        public static final int SYNCABLE_NO_ACCOUNT_ACCESS = 3;
+
         final EndPoint target;
         final int ident;
         boolean enabled;
@@ -402,6 +414,8 @@
     private OnSyncRequestListener mSyncRequestListener;
     private OnAuthorityRemovedListener mAuthorityRemovedListener;
 
+    private boolean mGrantSyncAdaptersAccountAccess;
+
     private SyncStorageEngine(Context context, File dataDir) {
         mContext = context;
         sSyncStorageEngine = this;
@@ -1404,6 +1418,10 @@
         }
     }
 
+    public boolean shouldGrantSyncAdaptersAccountAccess() {
+        return mGrantSyncAdaptersAccountAccess;
+    }
+
     /**
      * public for testing
      */
@@ -1458,6 +1476,11 @@
                 } catch (NumberFormatException e) {
                     version = 0;
                 }
+
+                if (version < 3) {
+                    mGrantSyncAdaptersAccountAccess = true;
+                }
+
                 String nextIdString = parser.getAttributeValue(null, XML_ATTR_NEXT_AUTHORITY_ID);
                 try {
                     int id = (nextIdString == null) ? 0 : Integer.parseInt(nextIdString);
diff --git a/services/core/java/com/android/server/display/AutomaticBrightnessController.java b/services/core/java/com/android/server/display/AutomaticBrightnessController.java
index c16dac2..a3febd6 100644
--- a/services/core/java/com/android/server/display/AutomaticBrightnessController.java
+++ b/services/core/java/com/android/server/display/AutomaticBrightnessController.java
@@ -22,6 +22,7 @@
 import com.android.server.twilight.TwilightManager;
 import com.android.server.twilight.TwilightState;
 
+import android.annotation.Nullable;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
@@ -49,13 +50,7 @@
     // If true, enables the use of the screen auto-brightness adjustment setting.
     private static final boolean USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT = true;
 
-    // Hysteresis constraints for brightening or darkening.
-    // The recent lux must have changed by at least this fraction relative to the
-    // current ambient lux before a change will be considered.
-    private static final float BRIGHTENING_LIGHT_HYSTERESIS = 0.10f;
-    private static final float DARKENING_LIGHT_HYSTERESIS = 0.20f;
-
-    // How long the current sensor reading is assumed to be valid beyond the current time.
+   // How long the current sensor reading is assumed to be valid beyond the current time.
     // This provides a bit of prediction, as well as ensures that the weight for the last sample is
     // non-zero, which in turn ensures that the total weight is non-zero.
     private static final long AMBIENT_LIGHT_PREDICTION_TIME_MILLIS = 100;
@@ -70,7 +65,7 @@
     private static final int MSG_UPDATE_AMBIENT_LUX = 1;
     private static final int MSG_BRIGHTNESS_ADJUSTMENT_SAMPLE = 2;
 
-    // Callbacks for requesting updates to the the display's power state
+    // Callbacks for requesting updates to the display's power state
     private final Callbacks mCallbacks;
 
     // The sensor manager.
@@ -114,6 +109,9 @@
     // weighting values positive.
     private final int mWeightingIntercept;
 
+    // accessor object for determining thresholds to change brightness dynamically
+    private final HysteresisLevels mDynamicHysteresis;
+
     // Amount of time to delay auto-brightness after screen on while waiting for
     // the light sensor to warm-up in milliseconds.
     // May be 0 if no warm-up is required.
@@ -189,7 +187,8 @@
             int brightnessMin, int brightnessMax, float dozeScaleFactor,
             int lightSensorRate, long brighteningLightDebounceConfig,
             long darkeningLightDebounceConfig, boolean resetAmbientLuxAfterWarmUpConfig,
-            int ambientLightHorizon, float autoBrightnessAdjustmentMaxGamma ) {
+            int ambientLightHorizon, float autoBrightnessAdjustmentMaxGamma,
+            HysteresisLevels dynamicHysteresis) {
         mCallbacks = callbacks;
         mTwilight = LocalServices.getService(TwilightManager.class);
         mSensorManager = sensorManager;
@@ -205,6 +204,7 @@
         mAmbientLightHorizon = ambientLightHorizon;
         mWeightingIntercept = ambientLightHorizon;
         mScreenAutoBrightnessAdjustmentMaxGamma = autoBrightnessAdjustmentMaxGamma;
+        mDynamicHysteresis = dynamicHysteresis;
 
         mHandler = new AutomaticBrightnessHandler(looper);
         mAmbientLightRingBuffer =
@@ -268,7 +268,7 @@
         pw.println();
         pw.println("Automatic Brightness Controller State:");
         pw.println("  mLightSensor=" + mLightSensor);
-        pw.println("  mTwilight.getCurrentState()=" + mTwilight.getCurrentState());
+        pw.println("  mTwilight.getLastTwilightState()=" + mTwilight.getLastTwilightState());
         pw.println("  mLightSensorEnabled=" + mLightSensorEnabled);
         pw.println("  mLightSensorEnableTime=" + TimeUtils.formatUptime(mLightSensorEnableTime));
         pw.println("  mAmbientLux=" + mAmbientLux);
@@ -343,8 +343,8 @@
 
     private void setAmbientLux(float lux) {
         mAmbientLux = lux;
-        mBrighteningLuxThreshold = mAmbientLux * (1.0f + BRIGHTENING_LIGHT_HYSTERESIS);
-        mDarkeningLuxThreshold = mAmbientLux * (1.0f - DARKENING_LIGHT_HYSTERESIS);
+        mBrighteningLuxThreshold = mDynamicHysteresis.getBrighteningThreshold(lux);
+        mDarkeningLuxThreshold = mDynamicHysteresis.getDarkeningThreshold(lux);
     }
 
     private float calculateAmbientLux(long now) {
@@ -495,12 +495,14 @@
         }
 
         if (mUseTwilight) {
-            TwilightState state = mTwilight.getCurrentState();
+            TwilightState state = mTwilight.getLastTwilightState();
             if (state != null && state.isNight()) {
-                final long now = System.currentTimeMillis();
-                gamma *= 1 + state.getAmount() * TWILIGHT_ADJUSTMENT_MAX_GAMMA;
+                final long duration = state.sunriseTimeMillis() - state.sunsetTimeMillis();
+                final long progress = System.currentTimeMillis() - state.sunsetTimeMillis();
+                final float amount = (float) Math.pow(2.0 * progress / duration - 1.0, 2.0);
+                gamma *= 1 + amount * TWILIGHT_ADJUSTMENT_MAX_GAMMA;
                 if (DEBUG) {
-                    Slog.d(TAG, "updateAutoBrightness: twilight amount=" + state.getAmount());
+                    Slog.d(TAG, "updateAutoBrightness: twilight amount=" + amount);
                 }
             }
         }
@@ -621,7 +623,7 @@
 
     private final TwilightListener mTwilightListener = new TwilightListener() {
         @Override
-        public void onTwilightStateChanged() {
+        public void onTwilightStateChanged(@Nullable TwilightState state) {
             updateAutoBrightness(true /*sendUpdate*/);
         }
     };
diff --git a/services/core/java/com/android/server/display/DisplayAdapter.java b/services/core/java/com/android/server/display/DisplayAdapter.java
index 701b9f1..6ba25a5 100644
--- a/services/core/java/com/android/server/display/DisplayAdapter.java
+++ b/services/core/java/com/android/server/display/DisplayAdapter.java
@@ -49,13 +49,6 @@
      */
     private static final AtomicInteger NEXT_DISPLAY_MODE_ID = new AtomicInteger(1);  // 0 = no mode.
 
-    /**
-     * Used to generate globally unique color transform ids.
-     *
-     * Valid IDs start at 1 with 0 as the sentinel value for the default mode.
-     */
-    private static final AtomicInteger NEXT_COLOR_TRANSFORM_ID = new AtomicInteger(1);
-
     // Called with SyncRoot lock held.
     public DisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
             Context context, Handler handler, Listener listener, String name) {
@@ -141,11 +134,6 @@
                 NEXT_DISPLAY_MODE_ID.getAndIncrement(), width, height, refreshRate);
     }
 
-    public static Display.ColorTransform createColorTransform(int colorTransform) {
-        return new Display.ColorTransform(
-                NEXT_COLOR_TRANSFORM_ID.getAndIncrement(), colorTransform);
-    }
-
     public interface Listener {
         public void onDisplayDeviceEvent(DisplayDevice device, int event);
         public void onTraversalRequested();
diff --git a/services/core/java/com/android/server/display/DisplayDevice.java b/services/core/java/com/android/server/display/DisplayDevice.java
index 7af0bdb..839ab4d 100644
--- a/services/core/java/com/android/server/display/DisplayDevice.java
+++ b/services/core/java/com/android/server/display/DisplayDevice.java
@@ -94,6 +94,11 @@
     }
 
     /**
+     * Returns whether the unique id of the device is stable across reboots.
+     */
+    public abstract boolean hasStableUniqueId();
+
+    /**
      * Gets information about the display device.
      *
      * The information returned should not change between calls unless the display
@@ -135,7 +140,7 @@
     /**
      * Sets the mode, if supported.
      */
-    public void requestColorTransformAndModeInTransactionLocked(int colorTransformId, int modeId) {
+    public void requestDisplayModesInTransactionLocked(int colorMode, int modeId) {
     }
 
     /**
diff --git a/services/core/java/com/android/server/display/DisplayDeviceInfo.java b/services/core/java/com/android/server/display/DisplayDeviceInfo.java
index 5ce66fa..6719182 100644
--- a/services/core/java/com/android/server/display/DisplayDeviceInfo.java
+++ b/services/core/java/com/android/server/display/DisplayDeviceInfo.java
@@ -118,6 +118,11 @@
     public static final int DIFF_OTHER = 1 << 1;
 
     /**
+     * Diff result: The color mode fields differ.
+     */
+    public static final int DIFF_COLOR_MODE = 1 << 2;
+
+    /**
      * Gets the name of the display device, which may be derived from EDID or
      * other sources. The name may be localized and displayed to the user.
      */
@@ -155,14 +160,11 @@
      */
     public Display.Mode[] supportedModes = Display.Mode.EMPTY_ARRAY;
 
-    /** The active color transform of the display */
-    public int colorTransformId;
+    /** The active color mode of the display */
+    public int colorMode;
 
-    /** The default color transform of the display */
-    public int defaultColorTransformId;
-
-    /** The supported color transforms of the display */
-    public Display.ColorTransform[] supportedColorTransforms = Display.ColorTransform.EMPTY_ARRAY;
+    /** The supported color modes of the display */
+    public int[] supportedColorModes = { Display.COLOR_MODE_DEFAULT };
 
     /**
      * The HDR capabilities this display claims to support.
@@ -283,6 +285,9 @@
         if (state != other.state) {
             diff |= DIFF_STATE;
         }
+        if (colorMode != other.colorMode) {
+            diff |= DIFF_COLOR_MODE;
+        }
         if (!Objects.equal(name, other.name)
                 || !Objects.equal(uniqueId, other.uniqueId)
                 || width != other.width
@@ -290,9 +295,7 @@
                 || modeId != other.modeId
                 || defaultModeId != other.defaultModeId
                 || !Arrays.equals(supportedModes, other.supportedModes)
-                || colorTransformId != other.colorTransformId
-                || defaultColorTransformId != other.defaultColorTransformId
-                || !Arrays.equals(supportedColorTransforms, other.supportedColorTransforms)
+                || !Arrays.equals(supportedColorModes, other.supportedColorModes)
                 || !Objects.equal(hdrCapabilities, other.hdrCapabilities)
                 || densityDpi != other.densityDpi
                 || xDpi != other.xDpi
@@ -324,9 +327,8 @@
         modeId = other.modeId;
         defaultModeId = other.defaultModeId;
         supportedModes = other.supportedModes;
-        colorTransformId = other.colorTransformId;
-        defaultColorTransformId = other.defaultColorTransformId;
-        supportedColorTransforms = other.supportedColorTransforms;
+        colorMode = other.colorMode;
+        supportedColorModes = other.supportedColorModes;
         hdrCapabilities = other.hdrCapabilities;
         densityDpi = other.densityDpi;
         xDpi = other.xDpi;
@@ -353,9 +355,8 @@
         sb.append(", modeId ").append(modeId);
         sb.append(", defaultModeId ").append(defaultModeId);
         sb.append(", supportedModes ").append(Arrays.toString(supportedModes));
-        sb.append(", colorTransformId ").append(colorTransformId);
-        sb.append(", defaultColorTransformId ").append(defaultColorTransformId);
-        sb.append(", supportedColorTransforms ").append(Arrays.toString(supportedColorTransforms));
+        sb.append(", colorMode ").append(colorMode);
+        sb.append(", supportedColorModes ").append(Arrays.toString(supportedColorModes));
         sb.append(", HdrCapabilities ").append(hdrCapabilities);
         sb.append(", density ").append(densityDpi);
         sb.append(", ").append(xDpi).append(" x ").append(yDpi).append(" dpi");
diff --git a/services/core/java/com/android/server/display/DisplayManagerService.java b/services/core/java/com/android/server/display/DisplayManagerService.java
index 6a6570b..971989b 100644
--- a/services/core/java/com/android/server/display/DisplayManagerService.java
+++ b/services/core/java/com/android/server/display/DisplayManagerService.java
@@ -239,11 +239,17 @@
 
     @Override
     public void onStart() {
+        // We need to pre-load the persistent data store so it's ready before the default display
+        // adapter is up so that we have it's configuration. We could load it lazily, but since
+        // we're going to have to read it in eventually we may as well do it here rather than after
+        // we've waited for the diplay to register itself with us.
+        mPersistentDataStore.loadIfNeeded();
         mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTER);
 
         publishBinderService(Context.DISPLAY_SERVICE, new BinderService(),
                 true /*allowIsolated*/);
         publishLocalService(DisplayManagerInternal.class, new LocalService());
+        publishLocalService(DisplayTransformManager.class, new DisplayTransformManager());
     }
 
     @Override
@@ -540,12 +546,12 @@
         }
     }
 
-    private void requestColorTransformInternal(int displayId, int colorTransformId) {
+    private void requestColorModeInternal(int displayId, int colorMode) {
         synchronized (mSyncRoot) {
             LogicalDisplay display = mLogicalDisplays.get(displayId);
             if (display != null &&
-                    display.getRequestedColorTransformIdLocked() != colorTransformId) {
-                display.setRequestedColorTransformIdLocked(colorTransformId);
+                    display.getRequestedColorModeLocked() != colorMode) {
+                display.setRequestedColorModeLocked(colorMode);
                 scheduleTraversalLocked(false);
             }
         }
@@ -690,11 +696,15 @@
         device.mDebugLastLoggedDeviceInfo = info;
 
         mDisplayDevices.add(device);
-        addLogicalDisplayLocked(device);
+        LogicalDisplay display = addLogicalDisplayLocked(device);
         Runnable work = updateDisplayStateLocked(device);
         if (work != null) {
             work.run();
         }
+        if (display != null && display.getPrimaryDisplayDeviceLocked() == device) {
+            int colorMode = mPersistentDataStore.getColorMode(device);
+            display.setRequestedColorModeLocked(colorMode);
+        }
         scheduleTraversalLocked(false);
     }
 
@@ -713,6 +723,13 @@
             } else if (diff != 0) {
                 Slog.i(TAG, "Display device changed: " + info);
             }
+            if ((diff & DisplayDeviceInfo.DIFF_COLOR_MODE) != 0) {
+                try {
+                    mPersistentDataStore.setColorMode(device, info.colorMode);
+                } finally {
+                    mPersistentDataStore.saveIfNeeded();
+                }
+            }
             device.mDebugLastLoggedDeviceInfo = info;
 
             device.applyPendingDisplayDeviceInfoChangesLocked();
@@ -765,7 +782,7 @@
 
     // Adds a new logical display based on the given display device.
     // Sends notifications if needed.
-    private void addLogicalDisplayLocked(DisplayDevice device) {
+    private LogicalDisplay addLogicalDisplayLocked(DisplayDevice device) {
         DisplayDeviceInfo deviceInfo = device.getDisplayDeviceInfoLocked();
         boolean isDefault = (deviceInfo.flags
                 & DisplayDeviceInfo.FLAG_DEFAULT_DISPLAY) != 0;
@@ -777,7 +794,7 @@
         if (!isDefault && mSingleDisplayDemoMode) {
             Slog.i(TAG, "Not creating a logical display for a secondary display "
                     + " because single display demo mode is enabled: " + deviceInfo);
-            return;
+            return null;
         }
 
         final int displayId = assignDisplayIdLocked(isDefault);
@@ -789,7 +806,7 @@
             // This should never happen currently.
             Slog.w(TAG, "Ignoring display device because the logical display "
                     + "created from it was not considered valid: " + deviceInfo);
-            return;
+            return null;
         }
 
         mLogicalDisplays.put(displayId, display);
@@ -800,6 +817,7 @@
         }
 
         sendDisplayEventLocked(displayId, DisplayManagerGlobal.EVENT_DISPLAY_ADDED);
+        return display;
     }
 
     private int assignDisplayIdLocked(boolean isDefault) {
@@ -1067,6 +1085,9 @@
             if (mDisplayPowerController != null) {
                 mDisplayPowerController.dump(pw);
             }
+
+            pw.println();
+            mPersistentDataStore.dump(pw);
         }
     }
 
@@ -1351,13 +1372,13 @@
         }
 
         @Override // Binder call
-        public void requestColorTransform(int displayId, int colorTransformId) {
+        public void requestColorMode(int displayId, int colorMode) {
             mContext.enforceCallingOrSelfPermission(
-                    Manifest.permission.CONFIGURE_DISPLAY_COLOR_TRANSFORM,
-                    "Permission required to change the display color transform");
+                    Manifest.permission.CONFIGURE_DISPLAY_COLOR_MODE,
+                    "Permission required to change the display color mode");
             final long token = Binder.clearCallingIdentity();
             try {
-                requestColorTransformInternal(displayId, colorTransformId);
+                requestColorModeInternal(displayId, colorMode);
             } finally {
                 Binder.restoreCallingIdentity(token);
             }
@@ -1381,6 +1402,9 @@
                 throw new IllegalArgumentException("width, height, and densityDpi must be "
                         + "greater than 0");
             }
+            if (surface != null && surface.isSingleBuffered()) {
+                throw new IllegalArgumentException("Surface can't be single-buffered");
+            }
 
             if ((flags & DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC) != 0) {
                 flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
@@ -1439,6 +1463,9 @@
 
         @Override // Binder call
         public void setVirtualDisplaySurface(IVirtualDisplayCallback callback, Surface surface) {
+            if (surface != null && surface.isSingleBuffered()) {
+                throw new IllegalArgumentException("Surface can't be single-buffered");
+            }
             final long token = Binder.clearCallingIdentity();
             try {
                 setVirtualDisplaySurfaceInternal(callback.asBinder(), surface);
diff --git a/services/core/java/com/android/server/display/DisplayPowerController.java b/services/core/java/com/android/server/display/DisplayPowerController.java
index 8f8afd5..df5def9 100644
--- a/services/core/java/com/android/server/display/DisplayPowerController.java
+++ b/services/core/java/com/android/server/display/DisplayPowerController.java
@@ -72,7 +72,7 @@
     private static final String TAG = "DisplayPowerController";
     private static final String SCREEN_ON_BLOCKED_TRACE_NAME = "Screen on blocked";
 
-    private static boolean DEBUG = false;
+    private static final boolean DEBUG = false;
     private static final boolean DEBUG_PRETEND_PROXIMITY_SENSOR_ABSENT = false;
 
     // If true, uses the color fade on animation.
@@ -102,9 +102,6 @@
     // Trigger proximity if distance is less than 5 cm.
     private static final float TYPICAL_PROXIMITY_THRESHOLD = 5.0f;
 
-    // Brightness animation ramp rate in brightness units per second.
-    private static final int BRIGHTNESS_RAMP_RATE_SLOW = 40;
-
     private static final int REPORTED_TO_POLICY_SCREEN_OFF = 0;
     private static final int REPORTED_TO_POLICY_SCREEN_TURNING_ON = 1;
     private static final int REPORTED_TO_POLICY_SCREEN_ON = 2;
@@ -243,8 +240,9 @@
     private boolean mAppliedDimming;
     private boolean mAppliedLowPower;
 
-    // Brightness ramp rate fast.
+    // Brightness animation ramp rates in brightness units per second
     private final int mBrightnessRampRateFast;
+    private final int mBrightnessRampRateSlow;
 
     // The controller for the automatic brightness level.
     private AutomaticBrightnessController mAutomaticBrightnessController;
@@ -307,6 +305,8 @@
 
         mBrightnessRampRateFast = resources.getInteger(
                 com.android.internal.R.integer.config_brightness_ramp_rate_fast);
+        mBrightnessRampRateSlow = resources.getInteger(
+                com.android.internal.R.integer.config_brightness_ramp_rate_slow);
 
         int lightSensorRate = resources.getInteger(
                 com.android.internal.R.integer.config_autoBrightnessLightSensorRate);
@@ -322,6 +322,15 @@
                 com.android.internal.R.fraction.config_autoBrightnessAdjustmentMaxGamma,
                 1, 1);
 
+        int[] brightLevels = resources.getIntArray(
+                com.android.internal.R.array.config_dynamicHysteresisBrightLevels);
+        int[] darkLevels = resources.getIntArray(
+                com.android.internal.R.array.config_dynamicHysteresisDarkLevels);
+        int[] luxLevels = resources.getIntArray(
+                com.android.internal.R.array.config_dynamicHysteresisLuxLevels);
+        HysteresisLevels dynamicHysteresis = new HysteresisLevels(
+                brightLevels, darkLevels, luxLevels);
+
         if (mUseSoftwareAutoBrightnessConfig) {
             int[] lux = resources.getIntArray(
                     com.android.internal.R.array.config_autoBrightnessLevels);
@@ -358,8 +367,8 @@
                         lightSensorWarmUpTimeConfig, screenBrightnessRangeMinimum,
                         mScreenBrightnessRangeMaximum, dozeScaleFactor, lightSensorRate,
                         brighteningLightDebounce, darkeningLightDebounce,
-                        autoBrightnessResetAmbientLuxAfterWarmUp,
-                        ambientLightHorizon, autoBrightnessAdjustmentMaxGamma);
+                        autoBrightnessResetAmbientLuxAfterWarmUp, ambientLightHorizon,
+                        autoBrightnessAdjustmentMaxGamma, dynamicHysteresis);
             }
         }
 
@@ -703,7 +712,7 @@
         if (!mPendingScreenOff) {
             if (state == Display.STATE_ON || state == Display.STATE_DOZE) {
                 animateScreenBrightness(brightness,
-                        slowChange ? BRIGHTNESS_RAMP_RATE_SLOW : mBrightnessRampRateFast);
+                        slowChange ? mBrightnessRampRateSlow : mBrightnessRampRateFast);
             } else {
                 animateScreenBrightness(brightness, 0);
             }
@@ -1181,6 +1190,10 @@
     }
 
     private static Spline createAutoBrightnessSpline(int[] lux, int[] brightness) {
+        if (lux == null || lux.length == 0 || brightness == null || brightness.length == 0) {
+            Slog.e(TAG, "Could not create auto-brightness spline.");
+            return null;
+        }
         try {
             final int n = brightness.length;
             float[] x = new float[n];
diff --git a/services/core/java/com/android/server/display/DisplayTransformManager.java b/services/core/java/com/android/server/display/DisplayTransformManager.java
new file mode 100644
index 0000000..6902b1a
--- /dev/null
+++ b/services/core/java/com/android/server/display/DisplayTransformManager.java
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display;
+
+import android.opengl.Matrix;
+import android.os.IBinder;
+import android.os.Parcel;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.util.Slog;
+import android.util.SparseArray;
+
+import com.android.internal.annotations.GuardedBy;
+
+import java.util.Arrays;
+
+/**
+ * Manager for applying color transformations to the display.
+ */
+public class DisplayTransformManager {
+
+    private static final String TAG = "DisplayTransformManager";
+
+    /**
+     * Color transform level used by Night display to tint the display red.
+     */
+    public static final int LEVEL_COLOR_MATRIX_NIGHT_DISPLAY = 100;
+    /**
+     * Color transform level used by A11y services to make the display monochromatic.
+     */
+    public static final int LEVEL_COLOR_MATRIX_GRAYSCALE = 200;
+    /**
+     * Color transform level used by A11y services to invert the display colors.
+     */
+    public static final int LEVEL_COLOR_MATRIX_INVERT_COLOR = 300;
+
+    /**
+     * Map of level -> color transformation matrix.
+     */
+    @GuardedBy("mColorMatrix")
+    private final SparseArray<float[]> mColorMatrix = new SparseArray<>(3);
+    /**
+     * Temporary matrix used internally by {@link #computeColorMatrixLocked()}.
+     */
+    @GuardedBy("mColorMatrix")
+    private final float[][] mTempColorMatrix = new float[2][16];
+
+    /**
+     * Lock used for synchronize access to {@link #mDaltonizerMode}.
+     */
+    private final Object mDaltonizerModeLock = new Object();
+    @GuardedBy("mDaltonizerModeLock")
+    private int mDaltonizerMode = -1;
+
+    /* package */ DisplayTransformManager() {
+    }
+
+    /**
+     * Returns a copy of the color transform matrix set for a given level.
+     */
+    public float[] getColorMatrix(int key) {
+        synchronized (mColorMatrix) {
+            final float[] value = mColorMatrix.get(key);
+            return value == null ? null : Arrays.copyOf(value, value.length);
+        }
+    }
+
+    /**
+     * Sets and applies a current color transform matrix for a given level.
+     * <p>
+     * Note: all color transforms are first composed to a single matrix in ascending order based
+     * on level before being applied to the display.
+     *
+     * @param level the level used to identify and compose the color transform (low -> high)
+     * @param value the 4x4 color transform matrix (in column-major order), or {@code null} to
+     *              remove the color transform matrix associated with the provided level
+     */
+    public void setColorMatrix(int level, float[] value) {
+        if (value != null && value.length != 16) {
+            throw new IllegalArgumentException("Expected length: 16 (4x4 matrix)"
+                    + ", actual length: " + value.length);
+        }
+
+        synchronized (mColorMatrix) {
+            final float[] oldValue = mColorMatrix.get(level);
+            if (!Arrays.equals(oldValue, value)) {
+                if (value == null) {
+                    mColorMatrix.remove(level);
+                } else if (oldValue == null) {
+                    mColorMatrix.put(level, Arrays.copyOf(value, value.length));
+                } else {
+                    System.arraycopy(value, 0, oldValue, 0, value.length);
+                }
+
+                // Update the current color transform.
+                applyColorMatrix(computeColorMatrixLocked());
+            }
+        }
+    }
+
+    /**
+     * Returns the composition of all current color matrices, or {@code null} if there are none.
+     */
+    @GuardedBy("mColorMatrix")
+    private float[] computeColorMatrixLocked() {
+        final int count = mColorMatrix.size();
+        if (count == 0) {
+            return null;
+        }
+
+        final float[][] result = mTempColorMatrix;
+        Matrix.setIdentityM(result[0], 0);
+        for (int i = 0; i < count; i++) {
+            float[] rhs = mColorMatrix.valueAt(i);
+            Matrix.multiplyMM(result[(i + 1) % 2], 0, result[i % 2], 0, rhs, 0);
+        }
+        return result[count % 2];
+    }
+
+    /**
+     * Returns the current Daltonization mode.
+     */
+    public int getDaltonizerMode() {
+        synchronized (mDaltonizerModeLock) {
+            return mDaltonizerMode;
+        }
+    }
+
+    /**
+     * Sets the current Daltonization mode. This adjusts the color space to correct for or simulate
+     * various types of color blindness.
+     *
+     * @param mode the new Daltonization mode, or -1 to disable
+     */
+    public void setDaltonizerMode(int mode) {
+        synchronized (mDaltonizerModeLock) {
+            if (mDaltonizerMode != mode) {
+                mDaltonizerMode = mode;
+                applyDaltonizerMode(mode);
+            }
+        }
+    }
+
+    /**
+     * Propagates the provided color transformation matrix to the SurfaceFlinger.
+     */
+    private static void applyColorMatrix(float[] m) {
+        final IBinder flinger = ServiceManager.getService("SurfaceFlinger");
+        if (flinger != null) {
+            final Parcel data = Parcel.obtain();
+            data.writeInterfaceToken("android.ui.ISurfaceComposer");
+            if (m != null) {
+                data.writeInt(1);
+                for (int i = 0; i < 16; i++) {
+                    data.writeFloat(m[i]);
+                }
+            } else {
+                data.writeInt(0);
+            }
+            try {
+                flinger.transact(1015, data, null, 0);
+            } catch (RemoteException ex) {
+                Slog.e(TAG, "Failed to set color transform", ex);
+            } finally {
+                data.recycle();
+            }
+        }
+    }
+
+    /**
+     * Propagates the provided Daltonization mode to the SurfaceFlinger.
+     */
+    private static void applyDaltonizerMode(int mode) {
+        final IBinder flinger = ServiceManager.getService("SurfaceFlinger");
+        if (flinger != null) {
+            final Parcel data = Parcel.obtain();
+            data.writeInterfaceToken("android.ui.ISurfaceComposer");
+            data.writeInt(mode);
+            try {
+                flinger.transact(1014, data, null, 0);
+            } catch (RemoteException ex) {
+                Slog.e(TAG, "Failed to set Daltonizer mode", ex);
+            } finally {
+                data.recycle();
+            }
+        }
+    }
+}
diff --git a/services/core/java/com/android/server/display/HysteresisLevels.java b/services/core/java/com/android/server/display/HysteresisLevels.java
new file mode 100644
index 0000000..b062225
--- /dev/null
+++ b/services/core/java/com/android/server/display/HysteresisLevels.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display;
+
+import android.util.Slog;
+
+/**
+ * A helper class for handling access to illuminance hysteresis level values.
+ */
+final class HysteresisLevels {
+    private static final String TAG = "HysteresisLevels";
+
+    // Default hysteresis constraints for brightening or darkening.
+    // The recent lux must have changed by at least this fraction relative to the
+    // current ambient lux before a change will be considered.
+    private static final float DEFAULT_BRIGHTENING_HYSTERESIS = 0.10f;
+    private static final float DEFAULT_DARKENING_HYSTERESIS = 0.20f;
+
+    private static final boolean DEBUG = false;
+
+    private final float[] mBrightLevels;
+    private final float[] mDarkLevels;
+    private final float[] mLuxLevels;
+
+  /**
+   * Creates a {@code HysteresisLevels} object with the given equal-length
+   * integer arrays.
+   * @param brightLevels an array of brightening hysteresis constraint constants
+   * @param darkLevels an array of darkening hysteresis constraint constants
+   * @param luxLevels a monotonically increasing array of illuminance
+   *                  thresholds in units of lux
+   */
+    public HysteresisLevels(int[] brightLevels, int[] darkLevels, int[] luxLevels) {
+        if (brightLevels.length != darkLevels.length || darkLevels.length != luxLevels.length + 1) {
+            throw new IllegalArgumentException("Mismatch between hysteresis array lengths.");
+        }
+        mBrightLevels = setArrayFormat(brightLevels, 1000.0f);
+        mDarkLevels = setArrayFormat(darkLevels, 1000.0f);
+        mLuxLevels = setArrayFormat(luxLevels, 1.0f);
+    }
+
+    /**
+     * Return the brightening hysteresis threshold for the given lux level.
+     */
+    public float getBrighteningThreshold(float lux) {
+        float brightConstant = getReferenceLevel(lux, mBrightLevels);
+        float brightThreshold = lux * (1.0f + brightConstant);
+        if (DEBUG) {
+            Slog.d(TAG, "bright hysteresis constant=: " + brightConstant + ", threshold="
+                + brightThreshold + ", lux=" + lux);
+        }
+        return brightThreshold;
+    }
+
+    /**
+     * Return the darkening hysteresis threshold for the given lux level.
+     */
+    public float getDarkeningThreshold(float lux) {
+        float darkConstant = getReferenceLevel(lux, mDarkLevels);
+        float darkThreshold = lux * (1.0f - darkConstant);
+        if (DEBUG) {
+            Slog.d(TAG, "dark hysteresis constant=: " + darkConstant + ", threshold="
+                + darkThreshold + ", lux=" + lux);
+        }
+        return darkThreshold;
+    }
+
+    /**
+     * Return the hysteresis constant for the closest lux threshold value to the
+     * current illuminance from the given array.
+     */
+    private float getReferenceLevel(float lux, float[] referenceLevels) {
+        int index = 0;
+        while (mLuxLevels.length > index && lux >= mLuxLevels[index]) {
+            ++index;
+        }
+        return referenceLevels[index];
+    }
+
+    /**
+     * Return a float array where each i-th element equals {@code configArray[i]/divideFactor}.
+     */
+    private float[] setArrayFormat(int[] configArray, float divideFactor) {
+        float[] levelArray = new float[configArray.length];
+        for (int index = 0; levelArray.length > index; ++index) {
+            levelArray[index] = (float)configArray[index] / divideFactor;
+        }
+        return levelArray;
+    }
+}
diff --git a/services/core/java/com/android/server/display/LocalDisplayAdapter.java b/services/core/java/com/android/server/display/LocalDisplayAdapter.java
index ebba354..61c2eac 100644
--- a/services/core/java/com/android/server/display/LocalDisplayAdapter.java
+++ b/services/core/java/com/android/server/display/LocalDisplayAdapter.java
@@ -39,6 +39,8 @@
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
 
 /**
  * A display adapter for the local displays managed by Surface Flinger.
@@ -100,14 +102,25 @@
                         builtInDisplayId);
                 return;
             }
+            int activeColorMode = SurfaceControl.getActiveColorMode(displayToken);
+            if (activeColorMode < 0) {
+                // We failed to get the active color mode. We don't bail out here since on the next
+                // configuration pass we'll go ahead and set it to whatever it was set to last (or
+                // COLOR_MODE_NATIVE if this is the first configuration).
+                Slog.w(TAG, "Unable to get active color mode for display device " +
+                        builtInDisplayId);
+                activeColorMode = Display.COLOR_MODE_INVALID;
+            }
+            int[] colorModes = SurfaceControl.getDisplayColorModes(displayToken);
             LocalDisplayDevice device = mDevices.get(builtInDisplayId);
             if (device == null) {
                 // Display was added.
                 device = new LocalDisplayDevice(displayToken, builtInDisplayId,
-                        configs, activeConfig);
+                        configs, activeConfig, colorModes, activeColorMode);
                 mDevices.put(builtInDisplayId, device);
                 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_ADDED);
-            } else if (device.updatePhysicalDisplayInfoLocked(configs, activeConfig)) {
+            } else if (device.updatePhysicalDisplayInfoLocked(configs, activeConfig,
+                        colorModes, activeColorMode)) {
                 // Display properties changed.
                 sendDisplayDeviceEventLocked(device, DISPLAY_DEVICE_EVENT_CHANGED);
             }
@@ -144,8 +157,7 @@
         private final int mBuiltInDisplayId;
         private final Light mBacklight;
         private final SparseArray<DisplayModeRecord> mSupportedModes = new SparseArray<>();
-        private final SparseArray<Display.ColorTransform> mSupportedColorTransforms =
-                new SparseArray<>();
+        private final ArrayList<Integer> mSupportedColorModes = new ArrayList<>();
 
         private DisplayDeviceInfo mInfo;
         private boolean mHavePendingChanges;
@@ -155,18 +167,20 @@
         private int mDefaultModeId;
         private int mActiveModeId;
         private boolean mActiveModeInvalid;
-        private int mDefaultColorTransformId;
-        private int mActiveColorTransformId;
-        private boolean mActiveColorTransformInvalid;
+        private int mActiveColorMode;
+        private boolean mActiveColorModeInvalid;
         private Display.HdrCapabilities mHdrCapabilities;
 
         private  SurfaceControl.PhysicalDisplayInfo mDisplayInfos[];
 
         public LocalDisplayDevice(IBinder displayToken, int builtInDisplayId,
-                SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo) {
+                SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo,
+                int[] colorModes, int activeColorMode) {
             super(LocalDisplayAdapter.this, displayToken, UNIQUE_ID_PREFIX + builtInDisplayId);
             mBuiltInDisplayId = builtInDisplayId;
-            updatePhysicalDisplayInfoLocked(physicalDisplayInfos, activeDisplayInfo);
+            updatePhysicalDisplayInfoLocked(physicalDisplayInfos, activeDisplayInfo,
+                    colorModes, activeColorMode);
+            updateColorModesLocked(colorModes, activeColorMode);
             if (mBuiltInDisplayId == SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) {
                 LightsManager lights = LocalServices.getService(LightsManager.class);
                 mBacklight = lights.getLight(LightsManager.LIGHT_ID_BACKLIGHT);
@@ -176,42 +190,16 @@
             mHdrCapabilities = SurfaceControl.getHdrCapabilities(displayToken);
         }
 
+        @Override
+        public boolean hasStableUniqueId() {
+            return true;
+        }
+
         public boolean updatePhysicalDisplayInfoLocked(
-                SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo) {
+                SurfaceControl.PhysicalDisplayInfo[] physicalDisplayInfos, int activeDisplayInfo,
+                int[] colorModes, int activeColorMode) {
             mDisplayInfos = Arrays.copyOf(physicalDisplayInfos, physicalDisplayInfos.length);
             mActivePhysIndex = activeDisplayInfo;
-            ArrayList<Display.ColorTransform> colorTransforms = new ArrayList<>();
-
-            // Build an updated list of all existing color transforms.
-            boolean colorTransformsAdded = false;
-            Display.ColorTransform activeColorTransform = null;
-            for (int i = 0; i < physicalDisplayInfos.length; i++) {
-                SurfaceControl.PhysicalDisplayInfo info = physicalDisplayInfos[i];
-                // First check to see if we've already added this color transform
-                boolean existingMode = false;
-                for (int j = 0; j < colorTransforms.size(); j++) {
-                    if (colorTransforms.get(j).getColorTransform() == info.colorTransform) {
-                        existingMode = true;
-                        if (i == activeDisplayInfo) {
-                            activeColorTransform = colorTransforms.get(j);
-                        }
-                        break;
-                    }
-                }
-                if (existingMode) {
-                    continue;
-                }
-                Display.ColorTransform colorTransform = findColorTransform(info);
-                if (colorTransform == null) {
-                    colorTransform = createColorTransform(info.colorTransform);
-                    colorTransformsAdded = true;
-                }
-                colorTransforms.add(colorTransform);
-                if (i == activeDisplayInfo) {
-                    activeColorTransform = colorTransform;
-                }
-            }
-
             // Build an updated list of all existing modes.
             ArrayList<DisplayModeRecord> records = new ArrayList<DisplayModeRecord>();
             boolean modesAdded = false;
@@ -257,21 +245,10 @@
                 mActiveModeInvalid = true;
                 sendTraversalRequestLocked();
             }
-            // Check whether surface flinger spontaneously changed color transforms out from under
-            // us.
-            if (mActiveColorTransformId != 0
-                    && mActiveColorTransformId != activeColorTransform.getId()) {
-                mActiveColorTransformInvalid = true;
-                sendTraversalRequestLocked();
-            }
 
-            boolean colorTransformsChanged =
-                    colorTransforms.size() != mSupportedColorTransforms.size()
-                    || colorTransformsAdded;
             boolean recordsChanged = records.size() != mSupportedModes.size() || modesAdded;
-            // If neither the records nor the supported color transforms have changed then we're
-            // done here.
-            if (!recordsChanged && !colorTransformsChanged) {
+            // If the records haven't changed then we're done here.
+            if (!recordsChanged) {
                 return false;
             }
             // Update the index of modes.
@@ -281,24 +258,13 @@
             for (DisplayModeRecord record : records) {
                 mSupportedModes.put(record.mMode.getModeId(), record);
             }
-            mSupportedColorTransforms.clear();
-            for (Display.ColorTransform colorTransform : colorTransforms) {
-                mSupportedColorTransforms.put(colorTransform.getId(), colorTransform);
-            }
-
-            // Update the default mode and color transform if needed. This needs to be done in
-            // tandem so we always have a default state to fall back to.
-            if (findDisplayInfoIndexLocked(mDefaultColorTransformId, mDefaultModeId) < 0) {
+            // Update the default mode, if needed.
+            if (findDisplayInfoIndexLocked(mDefaultModeId) < 0) {
                 if (mDefaultModeId != 0) {
                     Slog.w(TAG, "Default display mode no longer available, using currently"
                             + " active mode as default.");
                 }
                 mDefaultModeId = activeRecord.mMode.getModeId();
-                if (mDefaultColorTransformId != 0) {
-                    Slog.w(TAG, "Default color transform no longer available, using currently"
-                            + " active color transform as default");
-                }
-                mDefaultColorTransformId = activeColorTransform.getId();
             }
             // Determine whether the active mode is still there.
             if (mSupportedModes.indexOfKey(mActiveModeId) < 0) {
@@ -310,20 +276,62 @@
                 mActiveModeInvalid = true;
             }
 
-            // Determine whether the active color transform is still there.
-            if (mSupportedColorTransforms.indexOfKey(mActiveColorTransformId) < 0) {
-                if (mActiveColorTransformId != 0) {
-                    Slog.w(TAG, "Active color transform no longer available, reverting"
-                            + " to default transform.");
-                }
-                mActiveColorTransformId = mDefaultColorTransformId;
-                mActiveColorTransformInvalid = true;
-            }
             // Schedule traversals so that we apply pending changes.
             sendTraversalRequestLocked();
             return true;
         }
 
+        private boolean updateColorModesLocked(int[] colorModes,
+                int activeColorMode) {
+            List<Integer> pendingColorModes = new ArrayList<>();
+
+            // Build an updated list of all existing color modes.
+            boolean colorModesAdded = false;
+            for (int colorMode: colorModes) {
+                if (!mSupportedColorModes.contains(colorMode)) {
+                    colorModesAdded = true;
+                }
+                pendingColorModes.add(colorMode);
+            }
+
+            boolean colorModesChanged =
+                    pendingColorModes.size() != mSupportedColorModes.size()
+                    || colorModesAdded;
+
+            // If the supported color modes haven't changed then we're done here.
+            if (!colorModesChanged) {
+                return false;
+            }
+
+            mHavePendingChanges = true;
+
+            mSupportedColorModes.clear();
+            mSupportedColorModes.addAll(pendingColorModes);
+            Collections.sort(mSupportedColorModes);
+
+            // Determine whether the active color mode is still there.
+            if (!mSupportedColorModes.contains(mActiveColorMode)) {
+                if (mActiveColorMode != 0) {
+                    Slog.w(TAG, "Active color mode no longer available, reverting"
+                            + " to default mode.");
+                    mActiveColorMode = Display.COLOR_MODE_DEFAULT;
+                    mActiveColorModeInvalid = true;
+                } else {
+                    if (!mSupportedColorModes.isEmpty()) {
+                        // This should never happen.
+                        Slog.e(TAG, "Default and active color mode is no longer available!"
+                                + " Reverting to first available mode.");
+                        mActiveColorMode = mSupportedColorModes.get(0);
+                        mActiveColorModeInvalid = true;
+                    } else {
+                        // This should really never happen.
+                        Slog.e(TAG, "No color modes available!");
+                    }
+                }
+            }
+            return true;
+        }
+
         private DisplayModeRecord findDisplayModeRecord(SurfaceControl.PhysicalDisplayInfo info) {
             for (int i = 0; i < mSupportedModes.size(); i++) {
                 DisplayModeRecord record = mSupportedModes.valueAt(i);
@@ -334,16 +342,6 @@
             return null;
         }
 
-        private Display.ColorTransform findColorTransform(SurfaceControl.PhysicalDisplayInfo info) {
-            for (int i = 0; i < mSupportedColorTransforms.size(); i++) {
-                Display.ColorTransform transform = mSupportedColorTransforms.valueAt(i);
-                if (transform.getColorTransform() == info.colorTransform) {
-                    return transform;
-                }
-            }
-            return null;
-        }
-
         @Override
         public void applyPendingDisplayDeviceInfoChangesLocked() {
             if (mHavePendingChanges) {
@@ -366,12 +364,11 @@
                     DisplayModeRecord record = mSupportedModes.valueAt(i);
                     mInfo.supportedModes[i] = record.mMode;
                 }
-                mInfo.colorTransformId = mActiveColorTransformId;
-                mInfo.defaultColorTransformId = mDefaultColorTransformId;
-                mInfo.supportedColorTransforms =
-                        new Display.ColorTransform[mSupportedColorTransforms.size()];
-                for (int i = 0; i < mSupportedColorTransforms.size(); i++) {
-                    mInfo.supportedColorTransforms[i] = mSupportedColorTransforms.valueAt(i);
+                mInfo.colorMode = mActiveColorMode;
+                mInfo.supportedColorModes =
+                        new int[mSupportedColorModes.size()];
+                for (int i = 0; i < mSupportedColorModes.size(); i++) {
+                    mInfo.supportedColorModes[i] = mSupportedColorModes.get(i);
                 }
                 mInfo.hdrCapabilities = mHdrCapabilities;
                 mInfo.appVsyncOffsetNanos = phys.appVsyncOffsetNanos;
@@ -523,8 +520,15 @@
         }
 
         @Override
-        public void requestColorTransformAndModeInTransactionLocked(
-                int colorTransformId, int modeId) {
+        public void requestDisplayModesInTransactionLocked(
+                int colorMode, int modeId) {
+            if (requestModeInTransactionLocked(modeId) ||
+                    requestColorModeInTransactionLocked(colorMode)) {
+                updateDeviceInfoLocked();
+            }
+        }
+
+        public boolean requestModeInTransactionLocked(int modeId) {
             if (modeId == 0) {
                 modeId = mDefaultModeId;
             } else if (mSupportedModes.indexOfKey(modeId) < 0) {
@@ -533,37 +537,36 @@
                 modeId = mDefaultModeId;
             }
 
-            if (colorTransformId == 0) {
-                colorTransformId = mDefaultColorTransformId;
-            } else if (mSupportedColorTransforms.indexOfKey(colorTransformId) < 0) {
-                Slog.w(TAG, "Requested color transform " + colorTransformId + " is not supported"
-                        + " by this display, reverting to the default color transform");
-                colorTransformId = mDefaultColorTransformId;
-            }
-            int physIndex = findDisplayInfoIndexLocked(colorTransformId, modeId);
+            int physIndex = findDisplayInfoIndexLocked(modeId);
             if (physIndex < 0) {
-                Slog.w(TAG, "Requested color transform, mode ID pair (" + colorTransformId + ", "
-                        + modeId + ") not available, trying color transform with default mode ID");
+                Slog.w(TAG, "Requested mode ID " + modeId + " not available,"
+                        + " trying with default mode ID");
                 modeId = mDefaultModeId;
-                physIndex = findDisplayInfoIndexLocked(colorTransformId, modeId);
-                if (physIndex < 0) {
-                    Slog.w(TAG, "Requested color transform with default mode ID still not"
-                            + " available, falling back to default color transform with default"
-                            + " mode.");
-                    colorTransformId = mDefaultColorTransformId;
-                    physIndex = findDisplayInfoIndexLocked(colorTransformId, modeId);
-                }
+                physIndex = findDisplayInfoIndexLocked(modeId);
             }
             if (mActivePhysIndex == physIndex) {
-                return;
+                return false;
             }
             SurfaceControl.setActiveConfig(getDisplayTokenLocked(), physIndex);
             mActivePhysIndex = physIndex;
             mActiveModeId = modeId;
             mActiveModeInvalid = false;
-            mActiveColorTransformId = colorTransformId;
-            mActiveColorTransformInvalid = false;
-            updateDeviceInfoLocked();
+            return true;
+        }
+
+        public boolean requestColorModeInTransactionLocked(int colorMode) {
+            if (mActiveColorMode == colorMode) {
+                return false;
+            }
+            if (!mSupportedColorModes.contains(colorMode)) {
+                Slog.w(TAG, "Unable to find color mode " + colorMode
+                        + ", ignoring request.");
+                return false;
+            }
+            SurfaceControl.setActiveColorMode(getDisplayTokenLocked(), colorMode);
+            mActiveColorMode = colorMode;
+            mActiveColorModeInvalid = false;
+            return true;
         }
 
         @Override
@@ -572,7 +575,7 @@
             pw.println("mBuiltInDisplayId=" + mBuiltInDisplayId);
             pw.println("mActivePhysIndex=" + mActivePhysIndex);
             pw.println("mActiveModeId=" + mActiveModeId);
-            pw.println("mActiveColorTransformId=" + mActiveColorTransformId);
+            pw.println("mActiveColorMode=" + mActiveColorMode);
             pw.println("mState=" + Display.stateToString(mState));
             pw.println("mBrightness=" + mBrightness);
             pw.println("mBacklight=" + mBacklight);
@@ -584,24 +587,22 @@
             for (int i = 0; i < mSupportedModes.size(); i++) {
                 pw.println("  " + mSupportedModes.valueAt(i));
             }
-            pw.println("mSupportedColorTransforms=[");
-            for (int i = 0; i < mSupportedColorTransforms.size(); i++) {
+            pw.print("mSupportedColorModes=[");
+            for (int i = 0; i < mSupportedColorModes.size(); i++) {
                 if (i != 0) {
                     pw.print(", ");
                 }
-                pw.print(mSupportedColorTransforms.valueAt(i));
+                pw.print(mSupportedColorModes.get(i));
             }
             pw.println("]");
         }
 
-        private int findDisplayInfoIndexLocked(int colorTransformId, int modeId) {
+        private int findDisplayInfoIndexLocked(int modeId) {
             DisplayModeRecord record = mSupportedModes.get(modeId);
-            Display.ColorTransform transform = mSupportedColorTransforms.get(colorTransformId);
-            if (record != null && transform != null) {
+            if (record != null) {
                 for (int i = 0; i < mDisplayInfos.length; i++) {
                     SurfaceControl.PhysicalDisplayInfo info = mDisplayInfos[i];
-                    if (info.colorTransform == transform.getColorTransform()
-                            && record.hasMatchingMode(info)){
+                    if (record.hasMatchingMode(info)){
                         return i;
                     }
                 }
diff --git a/services/core/java/com/android/server/display/LogicalDisplay.java b/services/core/java/com/android/server/display/LogicalDisplay.java
index 973f04c..287a25a 100644
--- a/services/core/java/com/android/server/display/LogicalDisplay.java
+++ b/services/core/java/com/android/server/display/LogicalDisplay.java
@@ -74,7 +74,7 @@
     private boolean mHasContent;
 
     private int mRequestedModeId;
-    private int mRequestedColorTransformId;
+    private int mRequestedColorMode;
 
     // The display offsets to apply to the display projection.
     private int mDisplayOffsetX;
@@ -236,11 +236,10 @@
             mBaseDisplayInfo.defaultModeId = deviceInfo.defaultModeId;
             mBaseDisplayInfo.supportedModes = Arrays.copyOf(
                     deviceInfo.supportedModes, deviceInfo.supportedModes.length);
-            mBaseDisplayInfo.colorTransformId = deviceInfo.colorTransformId;
-            mBaseDisplayInfo.defaultColorTransformId = deviceInfo.defaultColorTransformId;
-            mBaseDisplayInfo.supportedColorTransforms = Arrays.copyOf(
-                    deviceInfo.supportedColorTransforms,
-                    deviceInfo.supportedColorTransforms.length);
+            mBaseDisplayInfo.colorMode = deviceInfo.colorMode;
+            mBaseDisplayInfo.supportedColorModes = Arrays.copyOf(
+                    deviceInfo.supportedColorModes,
+                    deviceInfo.supportedColorModes.length);
             mBaseDisplayInfo.hdrCapabilities = deviceInfo.hdrCapabilities;
             mBaseDisplayInfo.logicalDensityDpi = deviceInfo.densityDpi;
             mBaseDisplayInfo.physicalXDpi = deviceInfo.xDpi;
@@ -282,12 +281,12 @@
         // Set the layer stack.
         device.setLayerStackInTransactionLocked(isBlanked ? BLANK_LAYER_STACK : mLayerStack);
 
-        // Set the color transform and mode.
+        // Set the color mode and mode.
         if (device == mPrimaryDisplayDevice) {
-            device.requestColorTransformAndModeInTransactionLocked(
-                    mRequestedColorTransformId, mRequestedModeId);
+            device.requestDisplayModesInTransactionLocked(
+                    mRequestedColorMode, mRequestedModeId);
         } else {
-            device.requestColorTransformAndModeInTransactionLocked(0, 0);  // Revert to default.
+            device.requestDisplayModesInTransactionLocked(0, 0);  // Revert to default.
         }
 
         // Only grab the display info now as it may have been changed based on the requests above.
@@ -391,15 +390,15 @@
     }
 
     /**
-     * Requests the given color transform.
+     * Requests the given color mode.
      */
-    public void setRequestedColorTransformIdLocked(int colorTransformId) {
-        mRequestedColorTransformId = colorTransformId;
+    public void setRequestedColorModeLocked(int colorMode) {
+        mRequestedColorMode = colorMode;
     }
 
-    /** Returns the pending requested color transform. */
-    public int getRequestedColorTransformIdLocked() {
-        return mRequestedColorTransformId;
+    /** Returns the pending requested color mode. */
+    public int getRequestedColorModeLocked() {
+        return mRequestedColorMode;
     }
 
     /**
@@ -429,7 +428,7 @@
         pw.println("mLayerStack=" + mLayerStack);
         pw.println("mHasContent=" + mHasContent);
         pw.println("mRequestedMode=" + mRequestedModeId);
-        pw.println("mRequestedColorTransformId=" + mRequestedColorTransformId);
+        pw.println("mRequestedColorMode=" + mRequestedColorMode);
         pw.println("mDisplayOffset=(" + mDisplayOffsetX + ", " + mDisplayOffsetY + ")");
         pw.println("mPrimaryDisplayDevice=" + (mPrimaryDisplayDevice != null ?
                 mPrimaryDisplayDevice.getNameLocked() : "null"));
diff --git a/services/core/java/com/android/server/display/NightDisplayService.java b/services/core/java/com/android/server/display/NightDisplayService.java
new file mode 100644
index 0000000..0ec48b9
--- /dev/null
+++ b/services/core/java/com/android/server/display/NightDisplayService.java
@@ -0,0 +1,559 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.display;
+
+import android.animation.Animator;
+import android.animation.AnimatorListenerAdapter;
+import android.animation.TypeEvaluator;
+import android.animation.ValueAnimator;
+import android.annotation.NonNull;
+import android.annotation.Nullable;
+import android.app.AlarmManager;
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.opengl.Matrix;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.RemoteException;
+import android.os.UserHandle;
+import android.provider.Settings.Secure;
+import android.service.vr.IVrManager;
+import android.service.vr.IVrStateCallbacks;
+import android.util.MathUtils;
+import android.util.Slog;
+import android.view.animation.AnimationUtils;
+
+import com.android.internal.app.NightDisplayController;
+import com.android.server.SystemService;
+import com.android.server.twilight.TwilightListener;
+import com.android.server.twilight.TwilightManager;
+import com.android.server.twilight.TwilightState;
+import com.android.server.vr.VrManagerService;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.Calendar;
+import java.util.TimeZone;
+
+import static com.android.server.display.DisplayTransformManager.LEVEL_COLOR_MATRIX_NIGHT_DISPLAY;
+
+/**
+ * Tints the display at night.
+ */
+public final class NightDisplayService extends SystemService
+        implements NightDisplayController.Callback {
+
+    private static final String TAG = "NightDisplayService";
+    private static final boolean DEBUG = false;
+
+    /**
+     * Night display ~= 3400 K.
+     */
+    private static final float[] MATRIX_NIGHT = new float[] {
+        1,      0,      0, 0,
+        0, 0.754f,      0, 0,
+        0,      0, 0.516f, 0,
+        0,      0,      0, 1
+    };
+
+    /**
+     * The identity matrix, used if one of the given matrices is {@code null}.
+     */
+    private static final float[] MATRIX_IDENTITY = new float[16];
+    static {
+        Matrix.setIdentityM(MATRIX_IDENTITY, 0);
+    }
+
+    /**
+     * Evaluator used to animate color matrix transitions.
+     */
+    private static final ColorMatrixEvaluator COLOR_MATRIX_EVALUATOR = new ColorMatrixEvaluator();
+
+    private final Handler mHandler;
+    private final AtomicBoolean mIgnoreAllColorMatrixChanges = new AtomicBoolean();
+    private final IVrStateCallbacks mVrStateCallbacks = new IVrStateCallbacks.Stub() {
+        @Override
+        public void onVrStateChanged(final boolean enabled) {
+            // Turn off all night mode display stuff while device is in VR mode.
+            mIgnoreAllColorMatrixChanges.set(enabled);
+            mHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    // Cancel in-progress animations
+                    if (mColorMatrixAnimator != null) {
+                        mColorMatrixAnimator.cancel();
+                    }
+
+                    final DisplayTransformManager dtm =
+                            getLocalService(DisplayTransformManager.class);
+                    if (enabled) {
+                        dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, MATRIX_IDENTITY);
+                    } else if (mController.isActivated()) {
+                        dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, MATRIX_NIGHT);
+                    }
+                }
+            });
+        }
+    };
+
+    private int mCurrentUser = UserHandle.USER_NULL;
+    private ContentObserver mUserSetupObserver;
+    private boolean mBootCompleted;
+
+    private NightDisplayController mController;
+    private ValueAnimator mColorMatrixAnimator;
+    private Boolean mIsActivated;
+    private AutoMode mAutoMode;
+
+    public NightDisplayService(Context context) {
+        super(context);
+        mHandler = new Handler(Looper.getMainLooper());
+    }
+
+    @Override
+    public void onStart() {
+        // Nothing to publish.
+    }
+
+    @Override
+    public void onBootPhase(int phase) {
+        if (phase == PHASE_SYSTEM_SERVICES_READY) {
+            IVrManager vrManager =
+                    (IVrManager) getBinderService(VrManagerService.VR_MANAGER_BINDER_SERVICE);
+            if (vrManager != null) {
+                try {
+                    vrManager.registerListener(mVrStateCallbacks);
+                } catch (RemoteException e) {
+                    Slog.e(TAG, "Failed to register VR mode state listener: " + e);
+                }
+            }
+        } else if (phase == PHASE_BOOT_COMPLETED) {
+            mBootCompleted = true;
+
+            // Register listeners now that boot is complete.
+            if (mCurrentUser != UserHandle.USER_NULL && mUserSetupObserver == null) {
+                setUp();
+            }
+        }
+    }
+
+    @Override
+    public void onStartUser(int userHandle) {
+        super.onStartUser(userHandle);
+
+        if (mCurrentUser == UserHandle.USER_NULL) {
+            onUserChanged(userHandle);
+        }
+    }
+
+    @Override
+    public void onSwitchUser(int userHandle) {
+        super.onSwitchUser(userHandle);
+
+        onUserChanged(userHandle);
+    }
+
+    @Override
+    public void onStopUser(int userHandle) {
+        super.onStopUser(userHandle);
+
+        if (mCurrentUser == userHandle) {
+            onUserChanged(UserHandle.USER_NULL);
+        }
+    }
+
+    private void onUserChanged(int userHandle) {
+        final ContentResolver cr = getContext().getContentResolver();
+
+        if (mCurrentUser != UserHandle.USER_NULL) {
+            if (mUserSetupObserver != null) {
+                cr.unregisterContentObserver(mUserSetupObserver);
+                mUserSetupObserver = null;
+            } else if (mBootCompleted) {
+                tearDown();
+            }
+        }
+
+        mCurrentUser = userHandle;
+
+        if (mCurrentUser != UserHandle.USER_NULL) {
+            if (!isUserSetupCompleted(cr, mCurrentUser)) {
+                mUserSetupObserver = new ContentObserver(mHandler) {
+                    @Override
+                    public void onChange(boolean selfChange, Uri uri) {
+                        if (isUserSetupCompleted(cr, mCurrentUser)) {
+                            cr.unregisterContentObserver(this);
+                            mUserSetupObserver = null;
+
+                            if (mBootCompleted) {
+                                setUp();
+                            }
+                        }
+                    }
+                };
+                cr.registerContentObserver(Secure.getUriFor(Secure.USER_SETUP_COMPLETE),
+                        false /* notifyForDescendents */, mUserSetupObserver, mCurrentUser);
+            } else if (mBootCompleted) {
+                setUp();
+            }
+        }
+    }
+
+    private static boolean isUserSetupCompleted(ContentResolver cr, int userHandle) {
+        return Secure.getIntForUser(cr, Secure.USER_SETUP_COMPLETE, 0, userHandle) == 1;
+    }
+
+    private void setUp() {
+        Slog.d(TAG, "setUp: currentUser=" + mCurrentUser);
+
+        // Create a new controller for the current user and start listening for changes.
+        mController = new NightDisplayController(getContext(), mCurrentUser);
+        mController.setListener(this);
+
+        // Initialize the current auto mode.
+        onAutoModeChanged(mController.getAutoMode());
+
+        // Force the initialization current activated state.
+        if (mIsActivated == null) {
+            onActivated(mController.isActivated());
+        }
+    }
+
+    private void tearDown() {
+        Slog.d(TAG, "tearDown: currentUser=" + mCurrentUser);
+
+        if (mController != null) {
+            mController.setListener(null);
+            mController = null;
+        }
+
+        if (mAutoMode != null) {
+            mAutoMode.onStop();
+            mAutoMode = null;
+        }
+
+        if (mColorMatrixAnimator != null) {
+            mColorMatrixAnimator.end();
+            mColorMatrixAnimator = null;
+        }
+
+        mIsActivated = null;
+    }
+
+    @Override
+    public void onActivated(boolean activated) {
+        if (mIsActivated == null || mIsActivated != activated) {
+            Slog.i(TAG, activated ? "Turning on night display" : "Turning off night display");
+
+            if (mAutoMode != null) {
+                mAutoMode.onActivated(activated);
+            }
+
+            mIsActivated = activated;
+
+            // Cancel the old animator if still running.
+            if (mColorMatrixAnimator != null) {
+                mColorMatrixAnimator.cancel();
+            }
+
+            // Don't do any color matrix change animations if we are ignoring them anyway.
+            if (mIgnoreAllColorMatrixChanges.get()) {
+                return;
+            }
+
+            final DisplayTransformManager dtm = getLocalService(DisplayTransformManager.class);
+            final float[] from = dtm.getColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY);
+            final float[] to = mIsActivated ? MATRIX_NIGHT : null;
+
+            mColorMatrixAnimator = ValueAnimator.ofObject(COLOR_MATRIX_EVALUATOR,
+                    from == null ? MATRIX_IDENTITY : from, to == null ? MATRIX_IDENTITY : to);
+            mColorMatrixAnimator.setDuration(getContext().getResources()
+                    .getInteger(android.R.integer.config_longAnimTime));
+            mColorMatrixAnimator.setInterpolator(AnimationUtils.loadInterpolator(
+                    getContext(), android.R.interpolator.fast_out_slow_in));
+            mColorMatrixAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
+                @Override
+                public void onAnimationUpdate(ValueAnimator animator) {
+                    final float[] value = (float[]) animator.getAnimatedValue();
+                    dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, value);
+                }
+            });
+            mColorMatrixAnimator.addListener(new AnimatorListenerAdapter() {
+
+                private boolean mIsCancelled;
+
+                @Override
+                public void onAnimationCancel(Animator animator) {
+                    mIsCancelled = true;
+                }
+
+                @Override
+                public void onAnimationEnd(Animator animator) {
+                    if (!mIsCancelled) {
+                        // Ensure final color matrix is set at the end of the animation. If the
+                        // animation is cancelled then don't set the final color matrix so the new
+                        // animator can pick up from where this one left off.
+                        dtm.setColorMatrix(LEVEL_COLOR_MATRIX_NIGHT_DISPLAY, to);
+                    }
+                    mColorMatrixAnimator = null;
+                }
+            });
+            mColorMatrixAnimator.start();
+        }
+    }
+
+    @Override
+    public void onAutoModeChanged(int autoMode) {
+        Slog.d(TAG, "onAutoModeChanged: autoMode=" + autoMode);
+
+        if (mAutoMode != null) {
+            mAutoMode.onStop();
+            mAutoMode = null;
+        }
+
+        if (autoMode == NightDisplayController.AUTO_MODE_CUSTOM) {
+            mAutoMode = new CustomAutoMode();
+        } else if (autoMode == NightDisplayController.AUTO_MODE_TWILIGHT) {
+            mAutoMode = new TwilightAutoMode();
+        }
+
+        if (mAutoMode != null) {
+            mAutoMode.onStart();
+        }
+    }
+
+    @Override
+    public void onCustomStartTimeChanged(NightDisplayController.LocalTime startTime) {
+        Slog.d(TAG, "onCustomStartTimeChanged: startTime=" + startTime);
+
+        if (mAutoMode != null) {
+            mAutoMode.onCustomStartTimeChanged(startTime);
+        }
+    }
+
+    @Override
+    public void onCustomEndTimeChanged(NightDisplayController.LocalTime endTime) {
+        Slog.d(TAG, "onCustomEndTimeChanged: endTime=" + endTime);
+
+        if (mAutoMode != null) {
+            mAutoMode.onCustomEndTimeChanged(endTime);
+        }
+    }
+
+    private abstract class AutoMode implements NightDisplayController.Callback {
+        public abstract void onStart();
+        public abstract void onStop();
+    }
+
+    private class CustomAutoMode extends AutoMode implements AlarmManager.OnAlarmListener {
+
+        private final AlarmManager mAlarmManager;
+        private final BroadcastReceiver mTimeChangedReceiver;
+
+        private NightDisplayController.LocalTime mStartTime;
+        private NightDisplayController.LocalTime mEndTime;
+
+        private Calendar mLastActivatedTime;
+
+        public CustomAutoMode() {
+            mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
+            mTimeChangedReceiver = new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    updateActivated();
+                }
+            };
+        }
+
+        private void updateActivated() {
+            final Calendar now = Calendar.getInstance();
+            final Calendar startTime = mStartTime.getDateTimeBefore(now);
+            final Calendar endTime = mEndTime.getDateTimeAfter(startTime);
+            final boolean activated = now.before(endTime);
+
+            boolean setActivated = mIsActivated == null || mLastActivatedTime == null;
+            if (!setActivated && mIsActivated != activated) {
+                final TimeZone currentTimeZone = now.getTimeZone();
+                if (!currentTimeZone.equals(mLastActivatedTime.getTimeZone())) {
+                    final int year = mLastActivatedTime.get(Calendar.YEAR);
+                    final int dayOfYear = mLastActivatedTime.get(Calendar.DAY_OF_YEAR);
+                    final int hourOfDay = mLastActivatedTime.get(Calendar.HOUR_OF_DAY);
+                    final int minute = mLastActivatedTime.get(Calendar.MINUTE);
+
+                    mLastActivatedTime.setTimeZone(currentTimeZone);
+                    mLastActivatedTime.set(Calendar.YEAR, year);
+                    mLastActivatedTime.set(Calendar.DAY_OF_YEAR, dayOfYear);
+                    mLastActivatedTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
+                    mLastActivatedTime.set(Calendar.MINUTE, minute);
+                }
+
+                if (mIsActivated) {
+                    setActivated = now.before(mStartTime.getDateTimeBefore(mLastActivatedTime))
+                            || now.after(mEndTime.getDateTimeAfter(mLastActivatedTime));
+                } else {
+                    setActivated = now.before(mEndTime.getDateTimeBefore(mLastActivatedTime))
+                            || now.after(mStartTime.getDateTimeAfter(mLastActivatedTime));
+                }
+            }
+
+            if (setActivated) {
+                mController.setActivated(activated);
+            }
+            updateNextAlarm(mIsActivated, now);
+        }
+
+        private void updateNextAlarm(@Nullable Boolean activated, @NonNull Calendar now) {
+            if (activated != null) {
+                final Calendar next = activated ? mEndTime.getDateTimeAfter(now)
+                        : mStartTime.getDateTimeAfter(now);
+                mAlarmManager.setExact(AlarmManager.RTC, next.getTimeInMillis(), TAG, this, null);
+            }
+        }
+
+        @Override
+        public void onStart() {
+            final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_CHANGED);
+            intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
+            getContext().registerReceiver(mTimeChangedReceiver, intentFilter);
+
+            mStartTime = mController.getCustomStartTime();
+            mEndTime = mController.getCustomEndTime();
+
+            // Force an update to initialize state.
+            updateActivated();
+        }
+
+        @Override
+        public void onStop() {
+            getContext().unregisterReceiver(mTimeChangedReceiver);
+
+            mAlarmManager.cancel(this);
+            mLastActivatedTime = null;
+        }
+
+        @Override
+        public void onActivated(boolean activated) {
+            final Calendar now = Calendar.getInstance();
+            if (mIsActivated != null) {
+                mLastActivatedTime = now;
+            }
+            updateNextAlarm(activated, now);
+        }
+
+        @Override
+        public void onCustomStartTimeChanged(NightDisplayController.LocalTime startTime) {
+            mStartTime = startTime;
+            mLastActivatedTime = null;
+            updateActivated();
+        }
+
+        @Override
+        public void onCustomEndTimeChanged(NightDisplayController.LocalTime endTime) {
+            mEndTime = endTime;
+            mLastActivatedTime = null;
+            updateActivated();
+        }
+
+        @Override
+        public void onAlarm() {
+            Slog.d(TAG, "onAlarm");
+            updateActivated();
+        }
+    }
+
+    private class TwilightAutoMode extends AutoMode implements TwilightListener {
+
+        private final TwilightManager mTwilightManager;
+
+        private Calendar mLastActivatedTime;
+
+        public TwilightAutoMode() {
+            mTwilightManager = getLocalService(TwilightManager.class);
+        }
+
+        private void updateActivated(TwilightState state) {
+            final boolean isNight = state != null && state.isNight();
+            boolean setActivated = mIsActivated == null || mIsActivated != isNight;
+            if (setActivated && state != null && mLastActivatedTime != null) {
+                final Calendar sunrise = state.sunrise();
+                final Calendar sunset = state.sunset();
+                if (sunrise.before(sunset)) {
+                    setActivated = mLastActivatedTime.before(sunrise)
+                            || mLastActivatedTime.after(sunset);
+                } else {
+                    setActivated = mLastActivatedTime.before(sunset)
+                            || mLastActivatedTime.after(sunrise);
+                }
+            }
+
+            if (setActivated) {
+                mController.setActivated(isNight);
+            }
+        }
+
+        @Override
+        public void onStart() {
+            mTwilightManager.registerListener(this, mHandler);
+
+            // Force an update to initialize state.
+            updateActivated(mTwilightManager.getLastTwilightState());
+        }
+
+        @Override
+        public void onStop() {
+            mTwilightManager.unregisterListener(this);
+            mLastActivatedTime = null;
+        }
+
+        @Override
+        public void onActivated(boolean activated) {
+            if (mIsActivated != null) {
+                mLastActivatedTime = Calendar.getInstance();
+            }
+        }
+
+        @Override
+        public void onTwilightStateChanged(@Nullable TwilightState state) {
+            Slog.d(TAG, "onTwilightStateChanged: isNight="
+                    + (state == null ? null : state.isNight()));
+            updateActivated(state);
+        }
+    }
+
+    /**
+     * Interpolates between two 4x4 color transform matrices (in column-major order).
+     */
+    private static class ColorMatrixEvaluator implements TypeEvaluator<float[]> {
+
+        /**
+         * Result matrix returned by {@link #evaluate(float, float[], float[])}.
+         */
+        private final float[] mResultMatrix = new float[16];
+
+        @Override
+        public float[] evaluate(float fraction, float[] startValue, float[] endValue) {
+            for (int i = 0; i < mResultMatrix.length; i++) {
+                mResultMatrix[i] = MathUtils.lerp(startValue[i], endValue[i], fraction);
+            }
+            return mResultMatrix;
+        }
+    }
+}
diff --git a/services/core/java/com/android/server/display/OverlayDisplayAdapter.java b/services/core/java/com/android/server/display/OverlayDisplayAdapter.java
index cf6264a..27327d4 100644
--- a/services/core/java/com/android/server/display/OverlayDisplayAdapter.java
+++ b/services/core/java/com/android/server/display/OverlayDisplayAdapter.java
@@ -266,6 +266,11 @@
         }
 
         @Override
+        public boolean hasStableUniqueId() {
+            return false;
+        }
+
+        @Override
         public void performTraversalInTransactionLocked() {
             if (mSurfaceTexture != null) {
                 if (mSurface == null) {
@@ -310,7 +315,7 @@
         }
 
         @Override
-        public void requestColorTransformAndModeInTransactionLocked(int color, int id) {
+        public void requestDisplayModesInTransactionLocked(int color, int id) {
             int index = -1;
             if (id == 0) {
                 // Use the default.
diff --git a/services/core/java/com/android/server/display/PersistentDataStore.java b/services/core/java/com/android/server/display/PersistentDataStore.java
index d676b35..5616fb9 100644
--- a/services/core/java/com/android/server/display/PersistentDataStore.java
+++ b/services/core/java/com/android/server/display/PersistentDataStore.java
@@ -27,6 +27,7 @@
 import android.util.AtomicFile;
 import android.util.Slog;
 import android.util.Xml;
+import android.view.Display;
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
@@ -35,8 +36,11 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.PrintWriter;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
 
 import libcore.io.IoUtils;
 import libcore.util.Objects;
@@ -50,8 +54,13 @@
  * &lt;display-manager-state>
  *   &lt;remembered-wifi-displays>
  *     &lt;wifi-display deviceAddress="00:00:00:00:00:00" deviceName="XXXX" deviceAlias="YYYY" />
- *   &gt;remembered-wifi-displays>
- * &gt;/display-manager-state>
+ *   &lt;remembered-wifi-displays>
+ *   &lt;display-states>
+ *      &lt;display>
+ *          &lt;color-mode>0&lt;/color-mode>
+ *      &lt;/display>
+ *  &lt;/display-states>
+ * &lt;/display-manager-state>
  * </code>
  *
  * TODO: refactor this to extract common code shared with the input manager's data store
@@ -62,6 +71,10 @@
     // Remembered Wifi display devices.
     private ArrayList<WifiDisplay> mRememberedWifiDisplays = new ArrayList<WifiDisplay>();
 
+    // Display state by unique id.
+    private final HashMap<String, DisplayState> mDisplayStates =
+            new HashMap<String, DisplayState>();
+
     // The atomic file used to safely read or write the file.
     private final AtomicFile mAtomicFile;
 
@@ -168,7 +181,41 @@
         return -1;
     }
 
-    private void loadIfNeeded() {
+    public int getColorMode(DisplayDevice device) {
+        if (!device.hasStableUniqueId()) {
+            return Display.COLOR_MODE_DEFAULT;
+        }
+        DisplayState state = getDisplayState(device.getUniqueId(), false);
+        if (state == null) {
+            return Display.COLOR_MODE_DEFAULT;
+        }
+        return state.getColorMode();
+    }
+
+    public boolean setColorMode(DisplayDevice device, int colorMode) {
+        if (!device.hasStableUniqueId()) {
+            return false;
+        }
+        DisplayState state = getDisplayState(device.getUniqueId(), true);
+        if (state.setColorMode(colorMode)) {
+            setDirty();
+            return true;
+        }
+        return false;
+    }
+
+    private DisplayState getDisplayState(String uniqueId, boolean createIfAbsent) {
+        loadIfNeeded();
+        DisplayState state = mDisplayStates.get(uniqueId);
+        if (state == null && createIfAbsent) {
+            state = new DisplayState();
+            mDisplayStates.put(uniqueId, state);
+            setDirty();
+        }
+        return state;
+    }
+
+    public void loadIfNeeded() {
         if (!mLoaded) {
             load();
             mLoaded = true;
@@ -240,6 +287,9 @@
             if (parser.getName().equals("remembered-wifi-displays")) {
                 loadRememberedWifiDisplaysFromXml(parser);
             }
+            if (parser.getName().equals("display-states")) {
+                loadDisplaysFromXml(parser);
+            }
         }
     }
 
@@ -267,6 +317,27 @@
         }
     }
 
+    private void loadDisplaysFromXml(XmlPullParser parser)
+            throws IOException, XmlPullParserException {
+        final int outerDepth = parser.getDepth();
+        while (XmlUtils.nextElementWithin(parser, outerDepth)) {
+            if (parser.getName().equals("display")) {
+                String uniqueId = parser.getAttributeValue(null, "unique-id");
+                if (uniqueId == null) {
+                    throw new XmlPullParserException(
+                            "Missing unique-id attribute on display.");
+                }
+                if (mDisplayStates.containsKey(uniqueId)) {
+                    throw new XmlPullParserException("Found duplicate display.");
+                }
+
+                DisplayState state = new DisplayState();
+                state.loadFromXml(parser);
+                mDisplayStates.put(uniqueId, state);
+            }
+        }
+    }
+
     private void saveToXml(XmlSerializer serializer) throws IOException {
         serializer.startDocument(null, true);
         serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
@@ -282,7 +353,72 @@
             serializer.endTag(null, "wifi-display");
         }
         serializer.endTag(null, "remembered-wifi-displays");
+        serializer.startTag(null, "display-states");
+        for (Map.Entry<String, DisplayState> entry : mDisplayStates.entrySet()) {
+            final String uniqueId = entry.getKey();
+            final DisplayState state = entry.getValue();
+            serializer.startTag(null, "display");
+            serializer.attribute(null, "unique-id", uniqueId);
+            state.saveToXml(serializer);
+            serializer.endTag(null, "display");
+        }
+        serializer.endTag(null, "display-states");
         serializer.endTag(null, "display-manager-state");
         serializer.endDocument();
     }
+
+    public void dump(PrintWriter pw) {
+        pw.println("PersistentDataStore");
+        pw.println("  mLoaded=" + mLoaded);
+        pw.println("  mDirty=" + mDirty);
+        pw.println("  RememberedWifiDisplays:");
+        int i = 0;
+        for (WifiDisplay display : mRememberedWifiDisplays) {
+            pw.println("    " + i++ + ": " + display);
+        }
+        pw.println("  DisplayStates:");
+        i = 0;
+        for (Map.Entry<String, DisplayState> entry : mDisplayStates.entrySet()) {
+            pw.println("    " + i++ + ": " + entry.getKey());
+            entry.getValue().dump(pw, "      ");
+        }
+    }
+
+    private static final class DisplayState {
+        private int mColorMode;
+
+        public boolean setColorMode(int colorMode) {
+            if (colorMode == mColorMode) {
+                return false;
+            }
+            mColorMode = colorMode;
+            return true;
+        }
+
+        public int getColorMode() {
+            return mColorMode;
+        }
+
+        public void loadFromXml(XmlPullParser parser)
+                throws IOException, XmlPullParserException {
+            final int outerDepth = parser.getDepth();
+
+            while (XmlUtils.nextElementWithin(parser, outerDepth)) {
+                if (parser.getName().equals("color-mode")) {
+                    String value = parser.nextText();
+                    mColorMode = Integer.parseInt(value);
+                }
+            }
+        }
+
+        public void saveToXml(XmlSerializer serializer) throws IOException {
+            serializer.startTag(null, "color-mode");
+            serializer.text(Integer.toString(mColorMode));
+            serializer.endTag(null, "color-mode");
+        }
+
+        private void dump(final PrintWriter pw, final String prefix) {
+            pw.println(prefix + "ColorMode=" + mColorMode);
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/display/VirtualDisplayAdapter.java b/services/core/java/com/android/server/display/VirtualDisplayAdapter.java
index 986efd69..9d0fde5 100644
--- a/services/core/java/com/android/server/display/VirtualDisplayAdapter.java
+++ b/services/core/java/com/android/server/display/VirtualDisplayAdapter.java
@@ -225,6 +225,11 @@
         }
 
         @Override
+        public boolean hasStableUniqueId() {
+            return false;
+        }
+
+        @Override
         public Runnable requestDisplayStateLocked(int state, int brightness) {
             if (state != mDisplayState) {
                 mDisplayState = state;
diff --git a/services/core/java/com/android/server/display/WifiDisplayAdapter.java b/services/core/java/com/android/server/display/WifiDisplayAdapter.java
index 64bc729..08c0a1a 100644
--- a/services/core/java/com/android/server/display/WifiDisplayAdapter.java
+++ b/services/core/java/com/android/server/display/WifiDisplayAdapter.java
@@ -602,6 +602,11 @@
             mMode = createMode(width, height, refreshRate);
         }
 
+        @Override
+        public boolean hasStableUniqueId() {
+            return true;
+        }
+
         public void destroyLocked() {
             if (mSurface != null) {
                 mSurface.release();
diff --git a/services/core/java/com/android/server/dreams/DreamController.java b/services/core/java/com/android/server/dreams/DreamController.java
index a633996..3072f43 100644
--- a/services/core/java/com/android/server/dreams/DreamController.java
+++ b/services/core/java/com/android/server/dreams/DreamController.java
@@ -24,8 +24,11 @@
 import android.content.Intent;
 import android.content.ServiceConnection;
 import android.os.Binder;
+import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.IRemoteCallback;
+import android.os.PowerManager;
 import android.os.RemoteException;
 import android.os.IBinder.DeathRecipient;
 import android.os.SystemClock;
@@ -116,19 +119,19 @@
     }
 
     public void startDream(Binder token, ComponentName name,
-            boolean isTest, boolean canDoze, int userId) {
+            boolean isTest, boolean canDoze, int userId, PowerManager.WakeLock wakeLock) {
         stopDream(true /*immediate*/);
 
         Trace.traceBegin(Trace.TRACE_TAG_POWER, "startDream");
         try {
-            // Close the notification shade. Don't need to send to all, but better to be explicit.
+            // Close the notification shade. No need to send to all, but better to be explicit.
             mContext.sendBroadcastAsUser(mCloseNotificationShadeIntent, UserHandle.ALL);
 
             Slog.i(TAG, "Starting dream: name=" + name
                     + ", isTest=" + isTest + ", canDoze=" + canDoze
                     + ", userId=" + userId);
 
-            mCurrentDream = new DreamRecord(token, name, isTest, canDoze, userId);
+            mCurrentDream = new DreamRecord(token, name, isTest, canDoze, userId, wakeLock);
 
             mDreamStartTime = SystemClock.elapsedRealtime();
             MetricsLogger.visible(mContext,
@@ -230,6 +233,7 @@
             if (oldDream.mBound) {
                 mContext.unbindService(oldDream);
             }
+            oldDream.releaseWakeLockIfNeeded();
 
             try {
                 mIWindowManager.removeWindowToken(oldDream.mToken);
@@ -251,7 +255,8 @@
     private void attach(IDreamService service) {
         try {
             service.asBinder().linkToDeath(mCurrentDream, 0);
-            service.attach(mCurrentDream.mToken, mCurrentDream.mCanDoze);
+            service.attach(mCurrentDream.mToken, mCurrentDream.mCanDoze,
+                    mCurrentDream.mDreamingStartedCallback);
         } catch (RemoteException ex) {
             Slog.e(TAG, "The dream service died unexpectedly.", ex);
             stopDream(true /*immediate*/);
@@ -280,6 +285,7 @@
         public final boolean mCanDoze;
         public final int mUserId;
 
+        public PowerManager.WakeLock mWakeLock;
         public boolean mBound;
         public boolean mConnected;
         public IDreamService mService;
@@ -288,12 +294,17 @@
         public boolean mWakingGently;
 
         public DreamRecord(Binder token, ComponentName name,
-                boolean isTest, boolean canDoze, int userId) {
+                boolean isTest, boolean canDoze, int userId, PowerManager.WakeLock wakeLock) {
             mToken = token;
             mName = name;
             mIsTest = isTest;
             mCanDoze = canDoze;
             mUserId  = userId;
+            mWakeLock = wakeLock;
+            // Hold the lock while we're waiting for the service to connect and start dreaming.
+            // Released after the service has started dreaming, we stop dreaming, or it timed out.
+            mWakeLock.acquire();
+            mHandler.postDelayed(mReleaseWakeLockIfNeeded, 10000);
         }
 
         // May be called on any thread.
@@ -319,6 +330,9 @@
                     mConnected = true;
                     if (mCurrentDream == DreamRecord.this && mService == null) {
                         attach(IDreamService.Stub.asInterface(service));
+                        // Wake lock will be released once dreaming starts.
+                    } else {
+                        releaseWakeLockIfNeeded();
                     }
                 }
             });
@@ -337,5 +351,23 @@
                 }
             });
         }
+
+        void releaseWakeLockIfNeeded() {
+            if (mWakeLock != null) {
+                mWakeLock.release();
+                mWakeLock = null;
+                mHandler.removeCallbacks(mReleaseWakeLockIfNeeded);
+            }
+        }
+
+        final Runnable mReleaseWakeLockIfNeeded = this::releaseWakeLockIfNeeded;
+
+        final IRemoteCallback mDreamingStartedCallback = new IRemoteCallback.Stub() {
+            // May be called on any thread.
+            @Override
+            public void sendResult(Bundle data) throws RemoteException {
+                mHandler.post(mReleaseWakeLockIfNeeded);
+            }
+        };
     }
 }
\ No newline at end of file
diff --git a/services/core/java/com/android/server/dreams/DreamManagerService.java b/services/core/java/com/android/server/dreams/DreamManagerService.java
index 1f6616e..20bccf1 100644
--- a/services/core/java/com/android/server/dreams/DreamManagerService.java
+++ b/services/core/java/com/android/server/dreams/DreamManagerService.java
@@ -20,6 +20,7 @@
 
 import com.android.internal.util.DumpUtils;
 import com.android.server.FgThread;
+import com.android.server.LocalServices;
 import com.android.server.SystemService;
 
 import android.Manifest;
@@ -32,6 +33,8 @@
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.ServiceInfo;
+import android.database.ContentObserver;
+import android.hardware.input.InputManagerInternal;
 import android.os.Binder;
 import android.os.Build;
 import android.os.Handler;
@@ -111,11 +114,16 @@
             mContext.registerReceiver(new BroadcastReceiver() {
                 @Override
                 public void onReceive(Context context, Intent intent) {
+                    writePulseGestureEnabled();
                     synchronized (mLock) {
                         stopDreamLocked(false /*immediate*/);
                     }
                 }
             }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mHandler);
+            mContext.getContentResolver().registerContentObserver(
+                    Settings.Secure.getUriFor(Settings.Secure.DOZE_ENABLED), false,
+                    mDozeEnabledObserver, UserHandle.USER_ALL);
+            writePulseGestureEnabled();
         }
     }
 
@@ -362,12 +370,10 @@
         mCurrentDreamCanDoze = canDoze;
         mCurrentDreamUserId = userId;
 
-        mHandler.post(new Runnable() {
-            @Override
-            public void run() {
-                mController.startDream(newToken, name, isTest, canDoze, userId);
-            }
-        });
+        PowerManager.WakeLock wakeLock = mPowerManager
+                .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "startDream");
+        mHandler.post(wakeLock.wrap(
+                () -> mController.startDream(newToken, name, isTest, canDoze, userId, wakeLock)));
     }
 
     private void stopDreamLocked(final boolean immediate) {
@@ -414,6 +420,12 @@
         }
     }
 
+    private void writePulseGestureEnabled() {
+        ComponentName name = getDozeComponent();
+        boolean dozeEnabled = validateDream(name);
+        LocalServices.getService(InputManagerInternal.class).setPulseGestureEnabled(dozeEnabled);
+    }
+
     private static String componentsToString(ComponentName[] componentNames) {
         StringBuilder names = new StringBuilder();
         if (componentNames != null) {
@@ -450,6 +462,13 @@
         }
     };
 
+    private final ContentObserver mDozeEnabledObserver = new ContentObserver(null) {
+        @Override
+        public void onChange(boolean selfChange) {
+            writePulseGestureEnabled();
+        }
+    };
+
     /**
      * Handler for asynchronous operations performed by the dream manager.
      * Ensures operations to {@link DreamController} are single-threaded.
diff --git a/services/core/java/com/android/server/emergency/EmergencyAffordanceService.java b/services/core/java/com/android/server/emergency/EmergencyAffordanceService.java
new file mode 100644
index 0000000..cca9f10
--- /dev/null
+++ b/services/core/java/com/android/server/emergency/EmergencyAffordanceService.java
@@ -0,0 +1,312 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.server.emergency;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.os.Looper;
+import android.os.Message;
+import android.provider.Settings;
+import android.telephony.CellInfo;
+import android.telephony.CellInfoGsm;
+import android.telephony.CellInfoLte;
+import android.telephony.CellInfoWcdma;
+import android.telephony.CellLocation;
+import android.telephony.PhoneStateListener;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import com.android.server.SystemService;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * A service that listens to connectivity and SIM card changes and determines if the emergency mode
+ * should be enabled
+ */
+public class EmergencyAffordanceService extends SystemService {
+
+    private static final String TAG = "EmergencyAffordanceService";
+
+    private static final int NUM_SCANS_UNTIL_ABORT = 4;
+
+    private static final int INITIALIZE_STATE = 1;
+    private static final int CELL_INFO_STATE_CHANGED = 2;
+    private static final int SUBSCRIPTION_CHANGED = 3;
+
+    /**
+     * Global setting, whether the last scan of the sim cards reveal that a sim was inserted that
+     * requires the emergency affordance. The value is a boolean (1 or 0).
+     * @hide
+     */
+    private static final String EMERGENCY_SIM_INSERTED_SETTING = "emergency_sim_inserted_before";
+
+    private final Context mContext;
+    private final ArrayList<Integer> mEmergencyCallMccNumbers;
+
+    private final Object mLock = new Object();
+
+    private TelephonyManager mTelephonyManager;
+    private SubscriptionManager mSubscriptionManager;
+    private boolean mEmergencyAffordanceNeeded;
+    private MyHandler mHandler;
+    private int mScansCompleted;
+    private PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
+        @Override
+        public void onCellInfoChanged(List<CellInfo> cellInfo) {
+            if (!isEmergencyAffordanceNeeded()) {
+                requestCellScan();
+            }
+        }
+
+        @Override
+        public void onCellLocationChanged(CellLocation location) {
+            if (!isEmergencyAffordanceNeeded()) {
+                requestCellScan();
+            }
+        }
+    };
+    private BroadcastReceiver mAirplaneModeReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (Settings.Global.getInt(context.getContentResolver(),
+                    Settings.Global.AIRPLANE_MODE_ON, 0) == 0) {
+                startScanning();
+                requestCellScan();
+            }
+        }
+    };
+    private boolean mSimNeedsEmergencyAffordance;
+    private boolean mNetworkNeedsEmergencyAffordance;
+
+    private void requestCellScan() {
+        mHandler.obtainMessage(CELL_INFO_STATE_CHANGED).sendToTarget();
+    }
+
+    private SubscriptionManager.OnSubscriptionsChangedListener mSubscriptionChangedListener
+            = new SubscriptionManager.OnSubscriptionsChangedListener() {
+        @Override
+        public void onSubscriptionsChanged() {
+            mHandler.obtainMessage(SUBSCRIPTION_CHANGED).sendToTarget();
+        }
+    };
+
+    public EmergencyAffordanceService(Context context) {
+        super(context);
+        mContext = context;
+        int[] numbers = context.getResources().getIntArray(
+                com.android.internal.R.array.config_emergency_mcc_codes);
+        mEmergencyCallMccNumbers = new ArrayList<>(numbers.length);
+        for (int i = 0; i < numbers.length; i++) {
+            mEmergencyCallMccNumbers.add(numbers[i]);
+        }
+    }
+
+    private void updateEmergencyAffordanceNeeded() {
+        synchronized (mLock) {
+            mEmergencyAffordanceNeeded = mSimNeedsEmergencyAffordance ||
+                    mNetworkNeedsEmergencyAffordance;
+            Settings.Global.putInt(mContext.getContentResolver(),
+                    Settings.Global.EMERGENCY_AFFORDANCE_NEEDED,
+                    mEmergencyAffordanceNeeded ? 1 : 0);
+            if (mEmergencyAffordanceNeeded) {
+                stopScanning();
+            }
+        }
+    }
+
+    private void stopScanning() {
+        synchronized (mLock) {
+            mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE);
+            mScansCompleted = 0;
+        }
+    }
+
+    private boolean isEmergencyAffordanceNeeded() {
+        synchronized (mLock) {
+            return mEmergencyAffordanceNeeded;
+        }
+    }
+
+    @Override
+    public void onStart() {
+    }
+
+    @Override
+    public void onBootPhase(int phase) {
+        if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
+            mTelephonyManager = mContext.getSystemService(TelephonyManager.class);
+            mSubscriptionManager = SubscriptionManager.from(mContext);
+            HandlerThread thread = new HandlerThread(TAG);
+            thread.start();
+            mHandler = new MyHandler(thread.getLooper());
+            mHandler.obtainMessage(INITIALIZE_STATE).sendToTarget();
+            startScanning();
+            IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
+            mContext.registerReceiver(mAirplaneModeReceiver, filter);
+            mSubscriptionManager.addOnSubscriptionsChangedListener(mSubscriptionChangedListener);
+        }
+    }
+
+    private void startScanning() {
+        mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CELL_INFO
+                | PhoneStateListener.LISTEN_CELL_LOCATION);
+    }
+
+    /** Handler to do the heavier work on */
+    private class MyHandler extends Handler {
+
+        public MyHandler(Looper l) {
+            super(l);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case INITIALIZE_STATE:
+                    handleInitializeState();
+                    break;
+                case CELL_INFO_STATE_CHANGED:
+                    handleUpdateCellInfo();
+                    break;
+                case SUBSCRIPTION_CHANGED:
+                    handleUpdateSimSubscriptionInfo();
+                    break;
+            }
+        }
+    }
+
+    private void handleInitializeState() {
+        if (handleUpdateSimSubscriptionInfo()) {
+            return;
+        }
+        if (handleUpdateCellInfo()) {
+            return;
+        }
+        updateEmergencyAffordanceNeeded();
+    }
+
+    private boolean handleUpdateSimSubscriptionInfo() {
+        boolean neededBefore = simNeededAffordanceBefore();
+        boolean neededNow = neededBefore;
+        List<SubscriptionInfo> activeSubscriptionInfoList =
+                mSubscriptionManager.getActiveSubscriptionInfoList();
+        if (activeSubscriptionInfoList == null) {
+            return neededNow;
+        }
+        for (SubscriptionInfo info : activeSubscriptionInfoList) {
+            int mcc = info.getMcc();
+            if (mccRequiresEmergencyAffordance(mcc)) {
+                neededNow = true;
+                break;
+            } else if (mcc != 0 && mcc != Integer.MAX_VALUE){
+                // a Sim with a different mcc code was found
+                neededNow = false;
+            }
+            String simOperator  = mTelephonyManager.getSimOperator(info.getSubscriptionId());
+            mcc = 0;
+            if (simOperator != null && simOperator.length() >= 3) {
+                mcc = Integer.parseInt(simOperator.substring(0, 3));
+            }
+            if (mcc != 0) {
+                if (mccRequiresEmergencyAffordance(mcc)) {
+                    neededNow = true;
+                    break;
+                } else {
+                    // a Sim with a different mcc code was found
+                    neededNow = false;
+                }
+            }
+        }
+        if (neededNow != neededBefore) {
+            setSimNeedsEmergencyAffordance(neededNow);
+        }
+        return neededNow;
+    }
+
+    private void setSimNeedsEmergencyAffordance(boolean simNeedsEmergencyAffordance) {
+        mSimNeedsEmergencyAffordance = simNeedsEmergencyAffordance;
+        Settings.Global.putInt(mContext.getContentResolver(),
+                EMERGENCY_SIM_INSERTED_SETTING,
+                simNeedsEmergencyAffordance ? 1 : 0);
+        updateEmergencyAffordanceNeeded();
+    }
+
+    private boolean simNeededAffordanceBefore() {
+        return Settings.Global.getInt(mContext.getContentResolver(),
+                "emergency_sim_inserted_before", 0) != 0;
+    }
+
+    private boolean handleUpdateCellInfo() {
+        List<CellInfo> cellInfos = mTelephonyManager.getAllCellInfo();
+        if (cellInfos == null) {
+            return false;
+        }
+        boolean stopScanningAfterScan = false;
+        for (CellInfo cellInfo : cellInfos) {
+            int mcc = 0;
+            if (cellInfo instanceof CellInfoGsm) {
+                mcc = ((CellInfoGsm) cellInfo).getCellIdentity().getMcc();
+            } else if (cellInfo instanceof CellInfoLte) {
+                mcc = ((CellInfoLte) cellInfo).getCellIdentity().getMcc();
+            } else if (cellInfo instanceof CellInfoWcdma) {
+                mcc = ((CellInfoWcdma) cellInfo).getCellIdentity().getMcc();
+            }
+            if (mccRequiresEmergencyAffordance(mcc)) {
+                setNetworkNeedsEmergencyAffordance(true);
+                return true;
+            } else if (mcc != 0 && mcc != Integer.MAX_VALUE) {
+                // we found an mcc that isn't in the list, abort
+                stopScanningAfterScan = true;
+            }
+        }
+        if (stopScanningAfterScan) {
+            stopScanning();
+        } else {
+            onCellScanFinishedUnsuccessful();
+        }
+        setNetworkNeedsEmergencyAffordance(false);
+        return false;
+    }
+
+    private void setNetworkNeedsEmergencyAffordance(boolean needsAffordance) {
+        synchronized (mLock) {
+            mNetworkNeedsEmergencyAffordance = needsAffordance;
+            updateEmergencyAffordanceNeeded();
+        }
+    }
+
+    private void onCellScanFinishedUnsuccessful() {
+        synchronized (mLock) {
+            mScansCompleted++;
+            if (mScansCompleted >= NUM_SCANS_UNTIL_ABORT) {
+                stopScanning();
+            }
+        }
+    }
+
+    private boolean mccRequiresEmergencyAffordance(int mcc) {
+        return mEmergencyCallMccNumbers.contains(mcc);
+    }
+}
diff --git a/services/core/java/com/android/server/fingerprint/AuthenticationClient.java b/services/core/java/com/android/server/fingerprint/AuthenticationClient.java
index c04b9a1..5297589 100644
--- a/services/core/java/com/android/server/fingerprint/AuthenticationClient.java
+++ b/services/core/java/com/android/server/fingerprint/AuthenticationClient.java
@@ -39,9 +39,9 @@
     public abstract void resetFailedAttempts();
 
     public AuthenticationClient(Context context, long halDeviceId, IBinder token,
-            IFingerprintServiceReceiver receiver, int callingUserId, int groupId, long opId,
+            IFingerprintServiceReceiver receiver, int targetUserId, int groupId, long opId,
             boolean restricted, String owner) {
-        super(context, halDeviceId, token, receiver, callingUserId, groupId, restricted, owner);
+        super(context, halDeviceId, token, receiver, targetUserId, groupId, restricted, owner);
         mOpId = opId;
     }
 
@@ -65,7 +65,7 @@
                     Fingerprint fp = !getIsRestricted()
                             ? new Fingerprint("" /* TODO */, groupId, fingerId, getHalDeviceId())
                             : null;
-                    receiver.onAuthenticationSucceeded(getHalDeviceId(), fp);
+                    receiver.onAuthenticationSucceeded(getHalDeviceId(), fp, getTargetUserId());
                 }
             } catch (RemoteException e) {
                 Slog.w(TAG, "Failed to notify Authenticated:", e);
@@ -115,6 +115,7 @@
             final int result = daemon.authenticate(mOpId, getGroupId());
             if (result != 0) {
                 Slog.w(TAG, "startAuthentication failed, result=" + result);
+                MetricsLogger.histogram(getContext(), "fingeprintd_auth_start_error", result);
                 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE);
                 return result;
             }
diff --git a/services/core/java/com/android/server/fingerprint/EnrollClient.java b/services/core/java/com/android/server/fingerprint/EnrollClient.java
index 6a533c9..640a46f 100644
--- a/services/core/java/com/android/server/fingerprint/EnrollClient.java
+++ b/services/core/java/com/android/server/fingerprint/EnrollClient.java
@@ -88,6 +88,7 @@
             final int result = daemon.enroll(mCryptoToken, getGroupId(), timeout);
             if (result != 0) {
                 Slog.w(TAG, "startEnroll failed, result=" + result);
+                MetricsLogger.histogram(getContext(), "fingerprintd_enroll_start_error", result);
                 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE);
                 return result;
             }
diff --git a/services/core/java/com/android/server/fingerprint/EnumerateClient.java b/services/core/java/com/android/server/fingerprint/EnumerateClient.java
index 52dbd5d..26b1916 100644
--- a/services/core/java/com/android/server/fingerprint/EnumerateClient.java
+++ b/services/core/java/com/android/server/fingerprint/EnumerateClient.java
@@ -23,6 +23,7 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.util.Slog;
+import com.android.internal.logging.MetricsLogger;
 
 /**
  * A class to keep track of the enumeration state for a given client.
@@ -43,6 +44,7 @@
             if (result != 0) {
                 Slog.w(TAG, "start enumerate for user " + getTargetUserId()
                     + " failed, result=" + result);
+                MetricsLogger.histogram(getContext(), "fingerprintd_enum_start_error", result);
                 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE);
                 return result;
             }
diff --git a/services/core/java/com/android/server/fingerprint/FingerprintService.java b/services/core/java/com/android/server/fingerprint/FingerprintService.java
index 87ba475..6c11794 100644
--- a/services/core/java/com/android/server/fingerprint/FingerprintService.java
+++ b/services/core/java/com/android/server/fingerprint/FingerprintService.java
@@ -72,6 +72,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
 
 /**
@@ -90,10 +91,17 @@
     private static final String ACTION_LOCKOUT_RESET =
             "com.android.server.fingerprint.ACTION_LOCKOUT_RESET";
 
+    private class PerformanceStats {
+        int accept; // number of accepted fingerprints
+        int reject; // number of rejected fingerprints
+        int acquire; // total number of acquisitions. Should be >= accept+reject due to poor image
+                     // acquisition in some cases (too fast, too slow, dirty sensor, etc.)
+        int lockout; // total number of lockouts
+    }
+
     private final ArrayList<FingerprintServiceLockoutResetMonitor> mLockoutMonitors =
             new ArrayList<>();
     private final AppOpsManager mAppOps;
-
     private static final long FAIL_LOCKOUT_TIMEOUT_MS = 30*1000;
     private static final int MAX_FAILED_ATTEMPTS = 5;
     private static final long CANCEL_TIMEOUT_LIMIT = 3000; // max wait for onCancel() from HAL,in ms
@@ -110,6 +118,15 @@
     private ClientMonitor mCurrentClient;
     private ClientMonitor mPendingClient;
     private long mCurrentAuthenticatorId;
+    private PerformanceStats mPerformanceStats;
+
+    // Normal fingerprint authentications are tracked by mPerformanceMap.
+    private HashMap<Integer, PerformanceStats> mPerformanceMap
+            = new HashMap<Integer, PerformanceStats>();
+
+    // Transactions that make use of CryptoObjects are tracked by mCryptoPerformaceMap.
+    private HashMap<Integer, PerformanceStats> mCryptoPerformanceMap
+            = new HashMap<Integer, PerformanceStats>();
 
     private Handler mHandler = new Handler() {
         @Override
@@ -177,6 +194,7 @@
     @Override
     public void binderDied() {
         Slog.v(TAG, "fingerprintd died");
+        MetricsLogger.count(mContext, "fingerprintd_died", 1);
         mDaemon = null;
         mCurrentUserId = UserHandle.USER_CURRENT;
         handleError(mHalDeviceId, FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE);
@@ -194,6 +212,7 @@
                         updateActiveGroup(ActivityManager.getCurrentUser(), null);
                     } else {
                         Slog.w(TAG, "Failed to open Fingerprint HAL!");
+                        MetricsLogger.count(mContext, "fingerprintd_openhal_error", 1);
                         mDaemon = null;
                     }
                 } catch (RemoteException e) {
@@ -247,6 +266,11 @@
         if (client != null && client.onAuthenticated(fingerId, groupId)) {
             removeClient(client);
         }
+        if (fingerId != 0) {
+            mPerformanceStats.accept++;
+        } else {
+            mPerformanceStats.reject++;
+        }
     }
 
     protected void handleAcquired(long deviceId, int acquiredInfo) {
@@ -254,6 +278,11 @@
         if (client != null && client.onAcquired(acquiredInfo)) {
             removeClient(client);
         }
+        if (mPerformanceStats != null && !inLockoutMode()
+                && client instanceof AuthenticationClient) {
+            // ignore enrollment acquisitions or acquisitions when we're locked out
+            mPerformanceStats.acquire++;
+        }
     }
 
     protected void handleEnrollResult(long deviceId, int fingerId, int groupId, int remaining) {
@@ -444,10 +473,10 @@
 
     /**
      * @param opPackageName name of package for caller
-     * @param foregroundOnly only allow this call while app is in the foreground
+     * @param requireForeground only allow this call while app is in the foreground
      * @return true if caller can use fingerprint API
      */
-    private boolean canUseFingerprint(String opPackageName, boolean foregroundOnly, int uid,
+    private boolean canUseFingerprint(String opPackageName, boolean requireForeground, int uid,
             int pid) {
         checkPermission(USE_FINGERPRINT);
         if (isKeyguard(opPackageName)) {
@@ -462,7 +491,7 @@
             Slog.w(TAG, "Rejecting " + opPackageName + " ; permission denied");
             return false;
         }
-        if (foregroundOnly && !isForegroundActivity(uid, pid)) {
+        if (requireForeground && !(isForegroundActivity(uid, pid) || currentClient(opPackageName))){
             Slog.w(TAG, "Rejecting " + opPackageName + " ; not in foreground");
             return false;
         }
@@ -470,6 +499,14 @@
     }
 
     /**
+     * @param opPackageName package of the caller
+     * @return true if this is the same client currently using fingerprint
+     */
+    private boolean currentClient(String opPackageName) {
+        return mCurrentClient != null && mCurrentClient.getOwnerString().equals(opPackageName);
+    }
+
+    /**
      * @param clientPackage
      * @return true if this is keyguard package
      */
@@ -502,10 +539,13 @@
         if (DEBUG) Slog.v(TAG, "startAuthentication(" + opPackageName + ")");
 
         AuthenticationClient client = new AuthenticationClient(getContext(), mHalDeviceId, token,
-                receiver, callingUserId, groupId, opId, restricted, opPackageName) {
+                receiver, mCurrentUserId, groupId, opId, restricted, opPackageName) {
             @Override
             public boolean handleFailedAttempt() {
                 mFailedAttempts++;
+                if (mFailedAttempts == MAX_FAILED_ATTEMPTS) {
+                    mPerformanceStats.lockout++;
+                }
                 if (inLockoutMode()) {
                     // Failing multiple times will continue to push out the lockout time.
                     scheduleLockoutReset();
@@ -743,12 +783,24 @@
             mHandler.post(new Runnable() {
                 @Override
                 public void run() {
-                    MetricsLogger.histogram(mContext, "fingerprint_token", opId != 0L ? 1 : 0);
                     if (!canUseFingerprint(opPackageName, true /* foregroundOnly */,
                             callingUid, pid)) {
                         if (DEBUG) Slog.v(TAG, "authenticate(): reject " + opPackageName);
                         return;
                     }
+
+                    MetricsLogger.histogram(mContext, "fingerprint_token", opId != 0L ? 1 : 0);
+
+                    // Get performance stats object for this user.
+                    HashMap<Integer, PerformanceStats> pmap
+                            = (opId == 0) ? mPerformanceMap : mCryptoPerformanceMap;
+                    PerformanceStats stats = pmap.get(mCurrentUserId);
+                    if (stats == null) {
+                        stats = new PerformanceStats();
+                        pmap.put(mCurrentUserId, stats);
+                    }
+                    mPerformanceStats = stats;
+
                     startAuthentication(token, opId, callingUserId, groupId, receiver,
                             flags, restricted, opPackageName);
                 }
@@ -925,9 +977,21 @@
             for (UserInfo user : UserManager.get(getContext()).getUsers()) {
                 final int userId = user.getUserHandle().getIdentifier();
                 final int N = mFingerprintUtils.getFingerprintsForUser(mContext, userId).size();
+                PerformanceStats stats = mPerformanceMap.get(userId);
+                PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
                 JSONObject set = new JSONObject();
                 set.put("id", userId);
                 set.put("count", N);
+                set.put("accept", (stats != null) ? stats.accept : 0);
+                set.put("reject", (stats != null) ? stats.reject : 0);
+                set.put("acquire", (stats != null) ? stats.acquire : 0);
+                set.put("lockout", (stats != null) ? stats.lockout : 0);
+                // cryptoStats measures statistics about secure fingerprint transactions
+                // (e.g. to unlock password storage, make secure purchases, etc.)
+                set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
+                set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
+                set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
+                set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
                 sets.put(set);
             }
 
@@ -948,6 +1012,7 @@
 
     private void updateActiveGroup(int userId, String clientPackage) {
         IFingerprintDaemon daemon = getFingerprintDaemon();
+
         if (daemon != null) {
             try {
                 userId = getUserOrWorkProfileId(clientPackage, userId);
@@ -1014,7 +1079,7 @@
                     public void onForegroundProfileSwitch(int newProfileId) {
                         // Ignore.
                     }
-                });
+                }, TAG);
         } catch (RemoteException e) {
             Slog.w(TAG, "Failed to listen for user switching event" ,e);
         }
diff --git a/services/core/java/com/android/server/fingerprint/RemovalClient.java b/services/core/java/com/android/server/fingerprint/RemovalClient.java
index bcf2264..f939f41 100644
--- a/services/core/java/com/android/server/fingerprint/RemovalClient.java
+++ b/services/core/java/com/android/server/fingerprint/RemovalClient.java
@@ -24,6 +24,7 @@
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.util.Slog;
+import com.android.internal.logging.MetricsLogger;
 
 /**
  * A class to keep track of the remove state for a given client.
@@ -46,6 +47,7 @@
             final int result = daemon.remove(mFingerId, getGroupId());
             if (result != 0) {
                 Slog.w(TAG, "startRemove with id = " + mFingerId + " failed, result=" + result);
+                MetricsLogger.histogram(getContext(), "fingerprintd_remove_start_error", result);
                 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE);
                 return result;
             }
diff --git a/services/core/java/com/android/server/input/InputManagerService.java b/services/core/java/com/android/server/input/InputManagerService.java
index aa1d73f..74095ac 100644
--- a/services/core/java/com/android/server/input/InputManagerService.java
+++ b/services/core/java/com/android/server/input/InputManagerService.java
@@ -18,7 +18,9 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.os.Build;
 import android.os.LocaleList;
+import android.util.Log;
 import android.view.Display;
 import com.android.internal.inputmethod.InputMethodSubtypeHandle;
 import com.android.internal.os.SomeArgs;
@@ -98,8 +100,11 @@
 import java.io.FileDescriptor;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -109,6 +114,7 @@
 import java.util.List;
 import java.util.Locale;
 
+import libcore.io.IoUtils;
 import libcore.io.Streams;
 import libcore.util.Objects;
 
@@ -136,6 +142,8 @@
     private final Context mContext;
     private final InputManagerHandler mHandler;
 
+    private final File mDoubleTouchGestureEnableFile;
+
     private WindowManagerCallbacks mWindowManagerCallbacks;
     private WiredAccessoryCallbacks mWiredAccessoryCallbacks;
     private boolean mSystemReady;
@@ -301,6 +309,11 @@
                 + mUseDevInputEventForAudioJack);
         mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());
 
+        String doubleTouchGestureEnablePath = context.getResources().getString(
+                R.string.config_doubleTouchGestureEnableFile);
+        mDoubleTouchGestureEnableFile = TextUtils.isEmpty(doubleTouchGestureEnablePath) ? null :
+            new File(doubleTouchGestureEnablePath);
+
         LocalServices.addService(InputManagerInternal.class, new LocalService());
     }
 
@@ -2279,5 +2292,20 @@
         public void toggleCapsLock(int deviceId) {
             nativeToggleCapsLock(mPtr, deviceId);
         }
+
+        @Override
+        public void setPulseGestureEnabled(boolean enabled) {
+            if (mDoubleTouchGestureEnableFile != null) {
+                FileWriter writer = null;
+                try {
+                    writer = new FileWriter(mDoubleTouchGestureEnableFile);
+                    writer.write(enabled ? "1" : "0");
+                } catch (IOException e) {
+                    Log.wtf(TAG, "Unable to setPulseGestureEnabled", e);
+                } finally {
+                    IoUtils.closeQuietly(writer);
+                }
+            }
+        }
     }
 }
diff --git a/services/core/java/com/android/server/job/JobPackageTracker.java b/services/core/java/com/android/server/job/JobPackageTracker.java
index ba96b74..82e2eb4 100644
--- a/services/core/java/com/android/server/job/JobPackageTracker.java
+++ b/services/core/java/com/android/server/job/JobPackageTracker.java
@@ -395,7 +395,10 @@
             return 0;
         }
         final long now = SystemClock.uptimeMillis();
-        long time = cur.getActiveTime(now) + cur.getPendingTime(now);
+        long time = 0;
+        if (cur != null) {
+            time += cur.getActiveTime(now) + cur.getPendingTime(now);
+        }
         long period = mCurDataSet.getTotalTime(now);
         if (last != null) {
             time += last.getActiveTime(now) + last.getPendingTime(now);
diff --git a/services/core/java/com/android/server/job/JobSchedulerService.java b/services/core/java/com/android/server/job/JobSchedulerService.java
index 8589de1..bb3e877 100644
--- a/services/core/java/com/android/server/job/JobSchedulerService.java
+++ b/services/core/java/com/android/server/job/JobSchedulerService.java
@@ -28,6 +28,7 @@
 import java.util.Iterator;
 import java.util.List;
 
+import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
 import android.app.AppGlobals;
@@ -43,7 +44,9 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.IPackageManager;
+import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ServiceInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
@@ -214,7 +217,7 @@
         private static final int DEFAULT_FG_JOB_COUNT = 4;
         private static final int DEFAULT_BG_NORMAL_JOB_COUNT = 6;
         private static final int DEFAULT_BG_MODERATE_JOB_COUNT = 4;
-        private static final int DEFAULT_BG_LOW_JOB_COUNT = 2;
+        private static final int DEFAULT_BG_LOW_JOB_COUNT = 1;
         private static final int DEFAULT_BG_CRITICAL_JOB_COUNT = 1;
 
         /**
@@ -396,10 +399,11 @@
     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
+            final String action = intent.getAction();
             if (DEBUG) {
-                Slog.d(TAG, "Receieved: " + intent.getAction());
+                Slog.d(TAG, "Receieved: " + action);
             }
-            if (Intent.ACTION_PACKAGE_CHANGED.equals(intent.getAction())) {
+            if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) {
                 // Purge the app's jobs if the whole package was just disabled.  When this is
                 // the case the component name will be a bare package name.
                 final String pkgName = getPackageName(intent);
@@ -425,7 +429,18 @@
                                         }
                                         cancelJobsForUid(pkgUid, true);
                                     }
-                                } catch (RemoteException e) { /* cannot happen */ }
+                                } catch (RemoteException|IllegalArgumentException e) {
+                                    /*
+                                     * IllegalArgumentException means that the package doesn't exist.
+                                     * This arises when PACKAGE_CHANGED broadcast delivery has lagged
+                                     * behind outright uninstall, so by the time we try to act it's gone.
+                                     * We don't need to act on this PACKAGE_CHANGED when this happens;
+                                     * we'll get a PACKAGE_REMOVED later and clean up then.
+                                     *
+                                     * RemoteException can't actually happen; the package manager is
+                                     * running in this same process.
+                                     */
+                                }
                                 break;
                             }
                         }
@@ -433,7 +448,7 @@
                 } else {
                     Slog.w(TAG, "PACKAGE_CHANGED for " + pkgName + " / uid " + pkgUid);
                 }
-            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
+            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
                 // If this is an outright uninstall rather than the first half of an
                 // app update sequence, cancel the jobs associated with the app.
                 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
@@ -443,12 +458,43 @@
                     }
                     cancelJobsForUid(uidRemoved, true);
                 }
-            } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) {
+            } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
                 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
                 if (DEBUG) {
                     Slog.d(TAG, "Removing jobs for user: " + userId);
                 }
                 cancelJobsForUser(userId);
+            } else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) {
+                // Has this package scheduled any jobs, such that we will take action
+                // if it were to be force-stopped?
+                final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
+                final String pkgName = intent.getData().getSchemeSpecificPart();
+                if (pkgUid != -1) {
+                    List<JobStatus> jobsForUid;
+                    synchronized (mLock) {
+                        jobsForUid = mJobs.getJobsByUid(pkgUid);
+                    }
+                    for (int i = jobsForUid.size() - 1; i >= 0; i--) {
+                        if (jobsForUid.get(i).getSourcePackageName().equals(pkgName)) {
+                            if (DEBUG) {
+                                Slog.d(TAG, "Restart query: package " + pkgName + " at uid "
+                                        + pkgUid + " has jobs");
+                            }
+                            setResultCode(Activity.RESULT_OK);
+                            break;
+                        }
+                    }
+                }
+            } else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) {
+                // possible force-stop
+                final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1);
+                final String pkgName = intent.getData().getSchemeSpecificPart();
+                if (pkgUid != -1) {
+                    if (DEBUG) {
+                        Slog.d(TAG, "Removing jobs for pkg " + pkgName + " at uid " + pkgUid);
+                    }
+                    cancelJobsForPackageAndUid(pkgName, pkgUid);
+                }
             }
         }
     };
@@ -583,6 +629,19 @@
         }
     }
 
+    void cancelJobsForPackageAndUid(String pkgName, int uid) {
+        List<JobStatus> jobsForUid;
+        synchronized (mLock) {
+            jobsForUid = mJobs.getJobsByUid(uid);
+        }
+        for (int i = jobsForUid.size() - 1; i >= 0; i--) {
+            final JobStatus job = jobsForUid.get(i);
+            if (job.getSourcePackageName().equals(pkgName)) {
+                cancelJobImpl(job, null);
+            }
+        }
+    }
+
     /**
      * Entry point from client to cancel all jobs originating from their uid.
      * This will remove the job from the master list, and cancel the job if it was staged for
@@ -754,6 +813,8 @@
             final IntentFilter filter = new IntentFilter();
             filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
             filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
+            filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
+            filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
             filter.addDataScheme("package");
             getContext().registerReceiverAsUser(
                     mBroadcastReceiver, UserHandle.ALL, filter, null, null);
@@ -857,7 +918,11 @@
     private boolean isCurrentlyActiveLocked(JobStatus job) {
         for (int i=0; i<mActiveServices.size(); i++) {
             JobServiceContext serviceContext = mActiveServices.get(i);
-            final JobStatus running = serviceContext.getRunningJob();
+            // The 'unsafe' direct-internal-reference running-job inspector is okay to
+            // use here because we are already holding the necessary lock *and* we
+            // immediately discard the returned object reference, if any; we return
+            // only a boolean state indicator to the caller.
+            final JobStatus running = serviceContext.getRunningJobUnsafeLocked();
             if (running != null && running.matches(job.getUid(), job.getJobId())) {
                 return true;
             }
@@ -1556,6 +1621,11 @@
                 }
             }
 
+            if ((job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0) {
+                getContext().enforceCallingOrSelfPermission(
+                        android.Manifest.permission.CONNECTIVITY_INTERNAL, TAG);
+            }
+
             long ident = Binder.clearCallingIdentity();
             try {
                 return JobSchedulerService.this.schedule(job, uid);
diff --git a/services/core/java/com/android/server/job/JobServiceContext.java b/services/core/java/com/android/server/job/JobServiceContext.java
index 31528e5..5e495fa 100644
--- a/services/core/java/com/android/server/job/JobServiceContext.java
+++ b/services/core/java/com/android/server/job/JobServiceContext.java
@@ -230,6 +230,15 @@
         return job == null ? null : new JobStatus(job);
     }
 
+    /**
+     * Internal non-cloning inspection of the currently running job, if any.  The lock
+     * must be held when calling this *and* for the entire lifetime of using its returned
+     * JobStatus object!
+     */
+    JobStatus getRunningJobUnsafeLocked() {
+        return mRunningJob;
+    }
+
     /** Called externally when a job that was scheduled for execution should be cancelled. */
     void cancelExecutingJob(int reason) {
         mCallbackHandler.obtainMessage(MSG_CANCEL, reason, 0 /* unused */).sendToTarget();
@@ -306,10 +315,24 @@
         this.service = IJobService.Stub.asInterface(service);
         final PowerManager pm =
                 (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
-        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, runningJob.getTag());
-        mWakeLock.setWorkSource(new WorkSource(runningJob.getSourceUid()));
-        mWakeLock.setReferenceCounted(false);
-        mWakeLock.acquire();
+        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
+                runningJob.getTag());
+        wl.setWorkSource(new WorkSource(runningJob.getSourceUid()));
+        wl.setReferenceCounted(false);
+        wl.acquire();
+        synchronized (mLock) {
+            // We use a new wakelock instance per job.  In rare cases there is a race between
+            // teardown following job completion/cancellation and new job service spin-up
+            // such that if we simply assign mWakeLock to be the new instance, we orphan
+            // the currently-live lock instead of cleanly replacing it.  Watch for this and
+            // explicitly fast-forward the release if we're in that situation.
+            if (mWakeLock != null) {
+                Slog.w(TAG, "Bound new job " + runningJob + " but live wakelock " + mWakeLock
+                        + " tag=" + mWakeLock.getTag());
+                mWakeLock.release();
+            }
+            mWakeLock = wl;
+        }
         mCallbackHandler.obtainMessage(MSG_SERVICE_BOUND).sendToTarget();
     }
 
diff --git a/services/core/java/com/android/server/job/controllers/ContentObserverController.java b/services/core/java/com/android/server/job/controllers/ContentObserverController.java
index a42d0cd..5d209fc 100644
--- a/services/core/java/com/android/server/job/controllers/ContentObserverController.java
+++ b/services/core/java/com/android/server/job/controllers/ContentObserverController.java
@@ -16,6 +16,7 @@
 
 package com.android.server.job.controllers;
 
+import android.annotation.UserIdInt;
 import android.app.job.JobInfo;
 import android.content.Context;
 import android.database.ContentObserver;
@@ -23,6 +24,7 @@
 import android.os.Handler;
 import android.os.UserHandle;
 import android.util.Slog;
+import android.util.SparseArray;
 import android.util.TimeUtils;
 import android.util.ArrayMap;
 import android.util.ArraySet;
@@ -35,6 +37,7 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * Controller for monitoring changes to content URIs through a ContentObserver.
@@ -59,7 +62,11 @@
     private static volatile ContentObserverController sController;
 
     final private List<JobStatus> mTrackedTasks = new ArrayList<JobStatus>();
-    ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> mObservers = new ArrayMap<>();
+    /**
+     * Per-userid {@link JobInfo.TriggerContentUri} keyed ContentObserver cache.
+     */
+    SparseArray<ArrayMap<JobInfo.TriggerContentUri, ObserverInstance>> mObservers =
+            new SparseArray<>();
     final Handler mHandler;
 
     public static ContentObserverController get(JobSchedulerService taskManagerService) {
@@ -203,18 +210,21 @@
 
     final class ObserverInstance extends ContentObserver {
         final JobInfo.TriggerContentUri mUri;
+        final @UserIdInt int mUserId;
         final ArraySet<JobInstance> mJobs = new ArraySet<>();
 
-        public ObserverInstance(Handler handler, JobInfo.TriggerContentUri uri) {
+        public ObserverInstance(Handler handler, JobInfo.TriggerContentUri uri,
+                @UserIdInt int userId) {
             super(handler);
             mUri = uri;
+            mUserId = userId;
         }
 
         @Override
         public void onChange(boolean selfChange, Uri uri) {
             if (DEBUG) {
                 Slog.i(TAG, "onChange(self=" + selfChange + ") for " + uri
-                        + " when mUri=" + mUri);
+                        + " when mUri=" + mUri + " mUserId=" + mUserId);
             }
             synchronized (mLock) {
                 final int N = mJobs.size();
@@ -258,27 +268,38 @@
 
         boolean mTriggerPending;
 
+        // This constructor must be called with the master job scheduler lock held.
         JobInstance(JobStatus jobStatus) {
             mJobStatus = jobStatus;
             mExecuteRunner = new TriggerRunnable(this);
             mTimeoutRunner = new TriggerRunnable(this);
             final JobInfo.TriggerContentUri[] uris = jobStatus.getJob().getTriggerContentUris();
+            final int sourceUserId = jobStatus.getSourceUserId();
+            ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> observersOfUser =
+                    mObservers.get(sourceUserId);
+            if (observersOfUser == null) {
+                observersOfUser = new ArrayMap<>();
+                mObservers.put(sourceUserId, observersOfUser);
+            }
             if (uris != null) {
                 for (JobInfo.TriggerContentUri uri : uris) {
-                    ObserverInstance obs = mObservers.get(uri);
+                    ObserverInstance obs = observersOfUser.get(uri);
                     if (obs == null) {
-                        obs = new ObserverInstance(mHandler, uri);
-                        mObservers.put(uri, obs);
+                        obs = new ObserverInstance(mHandler, uri, jobStatus.getSourceUserId());
+                        observersOfUser.put(uri, obs);
                         final boolean andDescendants = (uri.getFlags() &
                                 JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS) != 0;
                         if (DEBUG) {
                             Slog.v(TAG, "New observer " + obs + " for " + uri.getUri()
-                                    + " andDescendants=" + andDescendants);
+                                    + " andDescendants=" + andDescendants
+                                    + " sourceUserId=" + sourceUserId);
                         }
                         mContext.getContentResolver().registerContentObserver(
                                 uri.getUri(),
                                 andDescendants,
-                                obs);
+                                obs,
+                                sourceUserId
+                        );
                     } else {
                         if (DEBUG) {
                             final boolean andDescendants = (uri.getFlags() &
@@ -342,7 +363,11 @@
                         Slog.i(TAG, "Unregistering observer " + obs + " for " + obs.mUri.getUri());
                     }
                     mContext.getContentResolver().unregisterContentObserver(obs);
-                    mObservers.remove(obs.mUri);
+                    ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> observerOfUser =
+                            mObservers.get(obs.mUserId);
+                    if (observerOfUser !=  null) {
+                        observerOfUser.remove(obs.mUri);
+                    }
                 }
             }
         }
@@ -366,60 +391,66 @@
         int N = mObservers.size();
         if (N > 0) {
             pw.println("  Observers:");
-            for (int i = 0; i < N; i++) {
-                ObserverInstance obs = mObservers.valueAt(i);
-                int M = obs.mJobs.size();
-                boolean shouldDump = false;
-                for (int j=0; j<M; j++) {
-                    JobInstance inst = obs.mJobs.valueAt(j);
-                    if (inst.mJobStatus.shouldDump(filterUid)) {
-                        shouldDump = true;
-                        break;
+            for (int userIdx = 0; userIdx < N; userIdx++) {
+                final int userId = mObservers.keyAt(userIdx);
+                ArrayMap<JobInfo.TriggerContentUri, ObserverInstance> observersOfUser =
+                        mObservers.get(userId);
+                int numbOfObserversPerUser = observersOfUser.size();
+                for (int observerIdx = 0 ; observerIdx < numbOfObserversPerUser; observerIdx++) {
+                    ObserverInstance obs = observersOfUser.valueAt(observerIdx);
+                    int M = obs.mJobs.size();
+                    boolean shouldDump = false;
+                    for (int j = 0; j < M; j++) {
+                        JobInstance inst = obs.mJobs.valueAt(j);
+                        if (inst.mJobStatus.shouldDump(filterUid)) {
+                            shouldDump = true;
+                            break;
+                        }
                     }
-                }
-                if (!shouldDump) {
-                    continue;
-                }
-                pw.print("    ");
-                JobInfo.TriggerContentUri trigger = mObservers.keyAt(i);
-                pw.print(trigger.getUri());
-                pw.print(" 0x");
-                pw.print(Integer.toHexString(trigger.getFlags()));
-                pw.print(" (");
-                pw.print(System.identityHashCode(obs));
-                pw.println("):");
-                pw.println("      Jobs:");
-                for (int j=0; j<M; j++) {
-                    JobInstance inst = obs.mJobs.valueAt(j);
-                    pw.print("        #");
-                    inst.mJobStatus.printUniqueId(pw);
-                    pw.print(" from ");
-                    UserHandle.formatUid(pw, inst.mJobStatus.getSourceUid());
-                    if (inst.mChangedAuthorities != null) {
-                        pw.println(":");
-                        if (inst.mTriggerPending) {
-                            pw.print("          Trigger pending: update=");
-                            TimeUtils.formatDuration(
-                                    inst.mJobStatus.getTriggerContentUpdateDelay(), pw);
-                            pw.print(", max=");
-                            TimeUtils.formatDuration(
-                                    inst.mJobStatus.getTriggerContentMaxDelay(), pw);
+                    if (!shouldDump) {
+                        continue;
+                    }
+                    pw.print("    ");
+                    JobInfo.TriggerContentUri trigger = observersOfUser.keyAt(observerIdx);
+                    pw.print(trigger.getUri());
+                    pw.print(" 0x");
+                    pw.print(Integer.toHexString(trigger.getFlags()));
+                    pw.print(" (");
+                    pw.print(System.identityHashCode(obs));
+                    pw.println("):");
+                    pw.println("      Jobs:");
+                    for (int j = 0; j < M; j++) {
+                        JobInstance inst = obs.mJobs.valueAt(j);
+                        pw.print("        #");
+                        inst.mJobStatus.printUniqueId(pw);
+                        pw.print(" from ");
+                        UserHandle.formatUid(pw, inst.mJobStatus.getSourceUid());
+                        if (inst.mChangedAuthorities != null) {
+                            pw.println(":");
+                            if (inst.mTriggerPending) {
+                                pw.print("          Trigger pending: update=");
+                                TimeUtils.formatDuration(
+                                        inst.mJobStatus.getTriggerContentUpdateDelay(), pw);
+                                pw.print(", max=");
+                                TimeUtils.formatDuration(
+                                        inst.mJobStatus.getTriggerContentMaxDelay(), pw);
+                                pw.println();
+                            }
+                            pw.println("          Changed Authorities:");
+                            for (int k = 0; k < inst.mChangedAuthorities.size(); k++) {
+                                pw.print("          ");
+                                pw.println(inst.mChangedAuthorities.valueAt(k));
+                            }
+                            if (inst.mChangedUris != null) {
+                                pw.println("          Changed URIs:");
+                                for (int k = 0; k < inst.mChangedUris.size(); k++) {
+                                    pw.print("          ");
+                                    pw.println(inst.mChangedUris.valueAt(k));
+                                }
+                            }
+                        } else {
                             pw.println();
                         }
-                        pw.println("          Changed Authorities:");
-                        for (int k=0; k<inst.mChangedAuthorities.size(); k++) {
-                            pw.print("          ");
-                            pw.println(inst.mChangedAuthorities.valueAt(k));
-                        }
-                        if (inst.mChangedUris != null) {
-                            pw.println("          Changed URIs:");
-                            for (int k = 0; k<inst.mChangedUris.size(); k++) {
-                                pw.print("          ");
-                                pw.println(inst.mChangedUris.valueAt(k));
-                            }
-                        }
-                    } else {
-                        pw.println();
                     }
                 }
             }
diff --git a/services/core/java/com/android/server/job/controllers/JobStatus.java b/services/core/java/com/android/server/job/controllers/JobStatus.java
index 552c990..0947554 100644
--- a/services/core/java/com/android/server/job/controllers/JobStatus.java
+++ b/services/core/java/com/android/server/job/controllers/JobStatus.java
@@ -486,6 +486,7 @@
                 + ":[" + job.getService()
                 + ",jId=" + job.getId()
                 + ",u" + getUserId()
+                + ",suid=" + getSourceUid()
                 + ",R=(" + formatRunTime(earliestRunTimeElapsedMillis, NO_EARLIEST_RUNTIME)
                 + "," + formatRunTime(latestRunTimeElapsedMillis, NO_LATEST_RUNTIME) + ")"
                 + ",N=" + job.getNetworkType() + ",C=" + job.isRequireCharging()
diff --git a/services/core/java/com/android/server/lights/LightsService.java b/services/core/java/com/android/server/lights/LightsService.java
index 07048a4..ca64817 100644
--- a/services/core/java/com/android/server/lights/LightsService.java
+++ b/services/core/java/com/android/server/lights/LightsService.java
@@ -172,10 +172,12 @@
         if (phase == PHASE_SYSTEM_SERVICES_READY) {
             IVrManager vrManager =
                     (IVrManager) getBinderService(VrManagerService.VR_MANAGER_BINDER_SERVICE);
-            try {
-                vrManager.registerListener(mVrStateCallbacks);
-            } catch (RemoteException e) {
-                Slog.e(TAG, "Failed to register VR mode state listener: " + e);
+            if (vrManager != null) {
+                try {
+                    vrManager.registerListener(mVrStateCallbacks);
+                } catch (RemoteException e) {
+                    Slog.e(TAG, "Failed to register VR mode state listener: " + e);
+                }
             }
         }
     }
diff --git a/services/core/java/com/android/server/location/GnssLocationProvider.java b/services/core/java/com/android/server/location/GnssLocationProvider.java
index 173f76f..ae98077 100644
--- a/services/core/java/com/android/server/location/GnssLocationProvider.java
+++ b/services/core/java/com/android/server/location/GnssLocationProvider.java
@@ -393,6 +393,15 @@
     // SIM/Carrier info.
     private final static String SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED";
 
+    // Persist property for LPP_PROFILE
+    private final static String LPP_PROFILE = "persist.sys.gps.lpp";
+
+    // VZW PLMN info
+    private static final String[] VzwMccMncList = {"311480", "310004", "20404"};
+    // corresponding GID1 value, empty string means ignore gid1 match.
+    private static final String[] VzwGid1List = {"", "", "BAE0000000000000"};
+
+
     private final PowerManager mPowerManager;
     private final AlarmManager mAlarmManager;
     private final PendingIntent mWakeupIntent;
@@ -443,8 +452,12 @@
             new ConnectivityManager.NetworkCallback() {
         @Override
         public void onAvailable(Network network) {
-            requestUtcTime();
-            xtraDownloadRequest();
+            if (mInjectNtpTimePending == STATE_PENDING_NETWORK) {
+                requestUtcTime();
+            }
+            if (mDownloadXtraDataPending == STATE_PENDING_NETWORK) {
+                xtraDownloadRequest();
+            }
         }
     };
 
@@ -507,14 +520,43 @@
         }
     };
 
+    private final boolean isVerizon(String mccMnc, String imsi, String groupId) {
+        if (DEBUG) Log.d(TAG, "simOperator: " + mccMnc);
+        if (!TextUtils.isEmpty(mccMnc) || !TextUtils.isEmpty(imsi)) {
+            for (int i = 0; i < VzwMccMncList.length; i++) {
+                if ((!TextUtils.isEmpty(mccMnc) && mccMnc.equals(VzwMccMncList[i])) ||
+                        (!TextUtils.isEmpty(imsi) && imsi.startsWith(VzwMccMncList[i]))) {
+                    // check gid too if needed
+                    if (TextUtils.isEmpty(VzwGid1List[i]) || VzwGid1List[i].equals(groupId)) {
+                        if (DEBUG) Log.d(TAG, "Verizon UICC");
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
+
     private void subscriptionOrSimChanged(Context context) {
         if (DEBUG) Log.d(TAG, "received SIM related action: ");
         TelephonyManager phone = (TelephonyManager)
                 mContext.getSystemService(Context.TELEPHONY_SERVICE);
         String mccMnc = phone.getSimOperator();
+        String imsi = phone.getSubscriberId();
+        String groupId = phone.getGroupIdLevel1();
         if (!TextUtils.isEmpty(mccMnc)) {
             if (DEBUG) Log.d(TAG, "SIM MCC/MNC is available: " + mccMnc);
             synchronized (mLock) {
+                if (isVerizon(mccMnc, imsi, groupId)) {
+                        // load current properties for carrier VZW
+                        loadPropertiesFromResource(context, mProperties);
+                        String lpp_profile = mProperties.getProperty("LPP_PROFILE");
+                        // set the persist property LPP_PROFILE for VZW
+                        SystemProperties.set(LPP_PROFILE, lpp_profile);
+                } else {
+                        // reset the persist property for Non VZW
+                        SystemProperties.set(LPP_PROFILE, "");
+                }
                 reloadGpsProperties(context, mProperties);
                 mNIHandler.setSuplEsEnabled(mSuplEsEnabled);
             }
@@ -571,8 +613,10 @@
     private void reloadGpsProperties(Context context, Properties properties) {
         if (DEBUG) Log.d(TAG, "Reset GPS properties, previous size = " + properties.size());
         loadPropertiesFromResource(context, properties);
+
         boolean isPropertiesLoadedFromFile = false;
         final String gpsHardware = SystemProperties.get("ro.hardware.gps");
+
         if (!TextUtils.isEmpty(gpsHardware)) {
             final String propFilename =
                     PROPERTIES_FILE_PREFIX + "." + gpsHardware + PROPERTIES_FILE_SUFFIX;
@@ -583,7 +627,11 @@
             loadPropertiesFromFile(DEFAULT_PROPERTIES_FILE, properties);
         }
         if (DEBUG) Log.d(TAG, "GPS properties reloaded, size = " + properties.size());
-
+        String lpp_prof = SystemProperties.get(LPP_PROFILE);
+        if (!TextUtils.isEmpty(lpp_prof)) {
+                // override default value of this if lpp_prof is not empty
+                properties.setProperty("LPP_PROFILE", lpp_prof);
+        }
         // TODO: we should get rid of C2K specific setting.
         setSuplHostPort(properties.getProperty("SUPL_HOST"),
                         properties.getProperty("SUPL_PORT"));
@@ -958,6 +1006,11 @@
     }
 
     private void handleDownloadXtraData() {
+        if (!mSupportsXtra) {
+            // native code reports xtra not supported, don't try
+            Log.d(TAG, "handleDownloadXtraData() called when Xtra not supported");
+            return;
+        }
         if (mDownloadXtraDataPending == STATE_DOWNLOADING) {
             // already downloading data
             return;
@@ -2081,9 +2134,7 @@
                     handleInjectNtpTime();
                     break;
                 case DOWNLOAD_XTRA_DATA:
-                    if (mSupportsXtra) {
-                        handleDownloadXtraData();
-                    }
+                    handleDownloadXtraData();
                     break;
                 case INJECT_NTP_TIME_FINISHED:
                     mInjectNtpTimePending = STATE_IDLE;
diff --git a/services/core/java/com/android/server/media/MediaResourceMonitorService.java b/services/core/java/com/android/server/media/MediaResourceMonitorService.java
index e169d63..0eb8b55 100644
--- a/services/core/java/com/android/server/media/MediaResourceMonitorService.java
+++ b/services/core/java/com/android/server/media/MediaResourceMonitorService.java
@@ -24,6 +24,7 @@
 import android.os.Binder;
 import android.os.RemoteException;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.util.Log;
 import android.util.Slog;
 import com.android.server.SystemService;
@@ -59,12 +60,20 @@
             final long identity = Binder.clearCallingIdentity();
             try {
                 String pkgNames[] = getPackageNamesFromPid(pid);
-                if (pkgNames != null) {
-                    Intent intent = new Intent(Intent.ACTION_MEDIA_RESOURCE_GRANTED);
-                    intent.putExtra(Intent.EXTRA_PACKAGES, pkgNames);
-                    intent.putExtra(Intent.EXTRA_MEDIA_RESOURCE_TYPE, type);
-                    getContext().sendBroadcastAsUser(intent,
-                            new UserHandle(ActivityManager.getCurrentUser()),
+                if (pkgNames == null) {
+                    return;
+                }
+                UserManager manager = (UserManager) getContext().getSystemService(
+                        Context.USER_SERVICE);
+                int[] userIds = manager.getEnabledProfileIds(ActivityManager.getCurrentUser());
+                if (userIds == null || userIds.length == 0) {
+                    return;
+                }
+                Intent intent = new Intent(Intent.ACTION_MEDIA_RESOURCE_GRANTED);
+                intent.putExtra(Intent.EXTRA_PACKAGES, pkgNames);
+                intent.putExtra(Intent.EXTRA_MEDIA_RESOURCE_TYPE, type);
+                for (int userId : userIds) {
+                    getContext().sendBroadcastAsUser(intent, UserHandle.of(userId),
                             android.Manifest.permission.RECEIVE_MEDIA_RESOURCE_USAGE);
                 }
             } finally {
diff --git a/services/core/java/com/android/server/media/MediaSessionRecord.java b/services/core/java/com/android/server/media/MediaSessionRecord.java
index 29c54e9..88d6c14 100644
--- a/services/core/java/com/android/server/media/MediaSessionRecord.java
+++ b/services/core/java/com/android/server/media/MediaSessionRecord.java
@@ -454,7 +454,7 @@
 
     @Override
     public String toString() {
-        return mPackageName + "/" + mTag;
+        return mPackageName + "/" + mTag + " (uid=" + mUserId + ")";
     }
 
     private void postAdjustLocalVolume(final int stream, final int direction, final int flags,
diff --git a/services/core/java/com/android/server/media/MediaSessionService.java b/services/core/java/com/android/server/media/MediaSessionService.java
index c9ca650..4c58ffd 100644
--- a/services/core/java/com/android/server/media/MediaSessionService.java
+++ b/services/core/java/com/android/server/media/MediaSessionService.java
@@ -52,6 +52,7 @@
 import android.os.ResultReceiver;
 import android.os.ServiceManager;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.provider.Settings;
 import android.speech.RecognizerIntent;
 import android.text.TextUtils;
@@ -68,6 +69,7 @@
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -76,6 +78,8 @@
 public class MediaSessionService extends SystemService implements Monitor {
     private static final String TAG = "MediaSessionService";
     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
+    // Leave log for media key event always.
+    private static final boolean DEBUG_MEDIA_KEY_EVENT = DEBUG || true;
 
     private static final int WAKELOCK_TIMEOUT = 5000;
 
@@ -98,7 +102,9 @@
     private ContentResolver mContentResolver;
     private SettingsObserver mSettingsObserver;
 
-    private int mCurrentUserId = -1;
+    // List of user IDs running in the foreground.
+    // Multiple users can be in the foreground if the work profile is on.
+    private final List<Integer> mCurrentUserIdList = new ArrayList<>();
 
     // Used to notify system UI when remote volume was changed. TODO find a
     // better way to handle this.
@@ -182,22 +188,26 @@
     }
 
     @Override
-    public void onStartUser(int userHandle) {
+    public void onStartUser(int userId) {
+        if (DEBUG) Log.d(TAG, "onStartUser: " + userId);
         updateUser();
     }
 
     @Override
-    public void onSwitchUser(int userHandle) {
+    public void onSwitchUser(int userId) {
+        if (DEBUG) Log.d(TAG, "onSwitchUser: " + userId);
         updateUser();
     }
 
     @Override
-    public void onStopUser(int userHandle) {
+    public void onStopUser(int userId) {
+        if (DEBUG) Log.d(TAG, "onStopUser: " + userId);
         synchronized (mLock) {
-            UserRecord user = mUserRecords.get(userHandle);
+            UserRecord user = mUserRecords.get(userId);
             if (user != null) {
                 destroyUserLocked(user);
             }
+            updateUser();
         }
     }
 
@@ -228,19 +238,25 @@
     }
 
     private void updateUser() {
-        int userId = ActivityManager.getCurrentUser();
         synchronized (mLock) {
-            if (mCurrentUserId != userId) {
-                final int oldUserId = mCurrentUserId;
-                mCurrentUserId = userId; // do this first
-
-                UserRecord oldUser = mUserRecords.get(oldUserId);
-                if (oldUser != null) {
-                    oldUser.stopLocked();
+            UserManager manager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
+            int currentUser = ActivityManager.getCurrentUser();
+            // Include all profiles even though they aren't yet enabled to handle work profile case.
+            int[] userIds = manager.getProfileIdsWithDisabled(currentUser);
+            mCurrentUserIdList.clear();
+            if (userIds != null && userIds.length > 0) {
+                for (int userId : userIds) {
+                    mCurrentUserIdList.add(userId);
                 }
-
-                UserRecord newUser = getOrCreateUser(userId);
-                newUser.startLocked();
+            } else {
+                // This shouldn't happen.
+                Log.w(TAG, "Failed to get enabled profiles.");
+                mCurrentUserIdList.add(currentUser);
+            }
+            for (int userId : mCurrentUserIdList) {
+                if (mUserRecords.get(userId) == null) {
+                    mUserRecords.put(userId, new UserRecord(getContext(), userId));
+                }
             }
         }
     }
@@ -273,7 +289,6 @@
      * @param user The user to dispose of
      */
     private void destroyUserLocked(UserRecord user) {
-        user.stopLocked();
         user.destroyLocked();
         mUserRecords.remove(user.mUserId);
     }
@@ -289,7 +304,7 @@
      */
     private void destroySessionLocked(MediaSessionRecord session) {
         if (DEBUG) {
-            Log.d(TAG, "Destroying session : " + session.toString());
+            Log.d(TAG, "Destroying " + session);
         }
         int userId = session.getUserId();
         UserRecord user = mUserRecords.get(userId);
@@ -395,7 +410,7 @@
                     if (component != null) {
                         if (compName.equals(component)) {
                             if (DEBUG) {
-                                Log.d(TAG, "ok to get sessions: " + component +
+                                Log.d(TAG, "ok to get sessions. " + component +
                                         " is authorized notification listener");
                             }
                             return true;
@@ -404,7 +419,7 @@
                 }
             }
             if (DEBUG) {
-                Log.d(TAG, "not ok to get sessions, " + compName +
+                Log.d(TAG, "not ok to get sessions. " + compName +
                         " is not in list of ENABLED_NOTIFICATION_LISTENERS for user " + userId);
             }
         }
@@ -428,6 +443,12 @@
     private MediaSessionRecord createSessionLocked(int callerPid, int callerUid, int userId,
             String callerPackageName, ISessionCallback cb, String tag) {
 
+        UserRecord user = mUserRecords.get(userId);
+        if (user == null) {
+            Log.wtf(TAG, "Request from invalid user: " +  userId);
+            throw new RuntimeException("Session request from invalid user.");
+        }
+
         final MediaSessionRecord session = new MediaSessionRecord(callerPid, callerUid, userId,
                 callerPackageName, cb, tag, this, mHandler);
         try {
@@ -437,28 +458,17 @@
         }
 
         mAllSessions.add(session);
-        mPriorityStack.addSession(session);
-
-        UserRecord user = getOrCreateUser(userId);
+        mPriorityStack.addSession(session, mCurrentUserIdList.contains(userId));
         user.addSessionLocked(session);
 
         mHandler.post(MessageHandler.MSG_SESSIONS_CHANGED, userId, 0);
 
         if (DEBUG) {
-            Log.d(TAG, "Created session for package " + callerPackageName + " with tag " + tag);
+            Log.d(TAG, "Created session for " + callerPackageName + " with tag " + tag);
         }
         return session;
     }
 
-    private UserRecord getOrCreateUser(int userId) {
-        UserRecord user = mUserRecords.get(userId);
-        if (user == null) {
-            user = new UserRecord(getContext(), userId);
-            mUserRecords.put(userId, user);
-        }
-        return user;
-    }
-
     private int findIndexOfSessionsListenerLocked(IActiveSessionsListener listener) {
         for (int i = mSessionsListeners.size() - 1; i >= 0; i--) {
             if (mSessionsListeners.get(i).mListener.asBinder() == listener.asBinder()) {
@@ -537,13 +547,6 @@
             restoreMediaButtonReceiver();
         }
 
-        public void startLocked() {
-        }
-
-        public void stopLocked() {
-            // nothing for now
-        }
-
         public void destroyLocked() {
             for (int i = mSessions.size() - 1; i >= 0; i--) {
                 MediaSessionRecord session = mSessions.get(i);
@@ -579,7 +582,7 @@
 
         private void restoreMediaButtonReceiver() {
             String receiverName = Settings.Secure.getStringForUser(mContentResolver,
-                    Settings.System.MEDIA_BUTTON_RECEIVER, UserHandle.USER_CURRENT);
+                    Settings.System.MEDIA_BUTTON_RECEIVER, mUserId);
             if (!TextUtils.isEmpty(receiverName)) {
                 ComponentName eventReceiver = ComponentName.unflattenFromString(receiverName);
                 if (eventReceiver == null) {
@@ -775,12 +778,22 @@
                 synchronized (mLock) {
                     // If we don't have a media button receiver to fall back on
                     // include non-playing sessions for dispatching
-                    UserRecord ur = mUserRecords.get(mCurrentUserId);
-                    boolean useNotPlayingSessions = (ur == null) ||
-                            (ur.mLastMediaButtonReceiver == null
-                                && ur.mRestoredMediaButtonReceiver == null);
-                    MediaSessionRecord session = mPriorityStack
-                            .getDefaultMediaButtonSession(mCurrentUserId, useNotPlayingSessions);
+                    boolean useNotPlayingSessions = true;
+                    for (int userId : mCurrentUserIdList) {
+                        UserRecord ur = mUserRecords.get(userId);
+                        if (ur.mLastMediaButtonReceiver != null
+                                || ur.mRestoredMediaButtonReceiver != null) {
+                            useNotPlayingSessions = false;
+                            break;
+                        }
+                    }
+
+                    if (DEBUG) {
+                        Log.d(TAG, "dispatchMediaKeyEvent, useNotPlayingSessions="
+                                + useNotPlayingSessions);
+                    }
+                    MediaSessionRecord session = mPriorityStack.getDefaultMediaButtonSession(
+                            mCurrentUserIdList, useNotPlayingSessions);
                     if (isVoiceKey(keyEvent.getKeyCode())) {
                         handleVoiceKeyEventLocked(keyEvent, needWakeLock, session);
                     } else {
@@ -794,13 +807,11 @@
 
         @Override
         public void dispatchAdjustVolume(int suggestedStream, int delta, int flags) {
-            final int pid = Binder.getCallingPid();
-            final int uid = Binder.getCallingUid();
             final long token = Binder.clearCallingIdentity();
             try {
                 synchronized (mLock) {
                     MediaSessionRecord session = mPriorityStack
-                            .getDefaultVolumeSession(mCurrentUserId);
+                            .getDefaultVolumeSession(mCurrentUserIdList);
                     dispatchAdjustVolumeLocked(suggestedStream, delta, flags, session);
                 }
             } finally {
@@ -879,17 +890,16 @@
 
         private void dispatchAdjustVolumeLocked(int suggestedStream, int direction, int flags,
                 MediaSessionRecord session) {
-            if (DEBUG) {
-                String description = session == null ? null : session.toString();
-                Log.d(TAG, "Adjusting session " + description + " by " + direction + ". flags="
-                        + flags + ", suggestedStream=" + suggestedStream);
-
-            }
             boolean preferSuggestedStream = false;
             if (isValidLocalStreamType(suggestedStream)
                     && AudioSystem.isStreamActive(suggestedStream, 0)) {
                 preferSuggestedStream = true;
             }
+            if (DEBUG) {
+                Log.d(TAG, "Adjusting " + session + " by " + direction + ". flags="
+                        + flags + ", suggestedStream=" + suggestedStream
+                        + ", preferSuggestedStream=" + preferSuggestedStream);
+            }
             if (session == null || preferSuggestedStream) {
                 if ((flags & AudioManager.FLAG_ACTIVE_MEDIA_ONLY) != 0
                         && !AudioSystem.isStreamActive(AudioManager.STREAM_MUSIC, 0)) {
@@ -918,7 +928,7 @@
                 });
             } else {
                 session.adjustVolume(direction, flags, getContext().getPackageName(),
-                        UserHandle.myUserId(), true);
+                        Process.SYSTEM_UID, true);
             }
         }
 
@@ -955,8 +965,8 @@
         private void dispatchMediaKeyEventLocked(KeyEvent keyEvent, boolean needWakeLock,
                 MediaSessionRecord session) {
             if (session != null) {
-                if (DEBUG) {
-                    Log.d(TAG, "Sending media key to " + session.toString());
+                if (DEBUG_MEDIA_KEY_EVENT) {
+                    Log.d(TAG, "Sending " + keyEvent + " to " + session);
                 }
                 if (needWakeLock) {
                     mKeyEventReceiver.aquireWakeLockLocked();
@@ -965,17 +975,15 @@
                 // won't release it later
                 session.sendMediaButton(keyEvent,
                         needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1,
-                        mKeyEventReceiver, getContext().getApplicationInfo().uid,
+                        mKeyEventReceiver, Process.SYSTEM_UID,
                         getContext().getPackageName());
             } else {
                 // Launch the last PendingIntent we had with priority
-                UserRecord user = mUserRecords.get(mCurrentUserId);
-                if (user != null && (user.mLastMediaButtonReceiver != null
-                        || user.mRestoredMediaButtonReceiver != null)) {
-                    if (DEBUG) {
-                        Log.d(TAG, "Sending media key to last known PendingIntent "
-                                + user.mLastMediaButtonReceiver + " or restored Intent "
-                                + user.mRestoredMediaButtonReceiver);
+                for (int userId : mCurrentUserIdList) {
+                    UserRecord user = mUserRecords.get(userId);
+                    if (user.mLastMediaButtonReceiver == null
+                            && user.mRestoredMediaButtonReceiver == null) {
+                        continue;
                     }
                     if (needWakeLock) {
                         mKeyEventReceiver.aquireWakeLockLocked();
@@ -985,36 +993,45 @@
                     mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
                     try {
                         if (user.mLastMediaButtonReceiver != null) {
+                            if (DEBUG_MEDIA_KEY_EVENT) {
+                                Log.d(TAG, "Sending " + keyEvent
+                                        + " to the last known pendingIntent "
+                                        + user.mLastMediaButtonReceiver);
+                            }
                             user.mLastMediaButtonReceiver.send(getContext(),
                                     needWakeLock ? mKeyEventReceiver.mLastTimeoutId : -1,
                                     mediaButtonIntent, mKeyEventReceiver, mHandler);
                         } else {
+                            if (DEBUG_MEDIA_KEY_EVENT) {
+                                Log.d(TAG, "Sending " + keyEvent + " to the restored intent "
+                                        + user.mRestoredMediaButtonReceiver);
+                            }
                             mediaButtonIntent.setComponent(user.mRestoredMediaButtonReceiver);
                             getContext().sendBroadcastAsUser(mediaButtonIntent,
-                                    new UserHandle(mCurrentUserId));
+                                    UserHandle.of(userId));
                         }
                     } catch (CanceledException e) {
                         Log.i(TAG, "Error sending key event to media button receiver "
                                 + user.mLastMediaButtonReceiver, e);
                     }
-                } else {
-                    if (DEBUG) {
-                        Log.d(TAG, "Sending media key ordered broadcast");
-                    }
-                    if (needWakeLock) {
-                        mMediaEventWakeLock.acquire();
-                    }
-                    // Fallback to legacy behavior
-                    Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
-                    keyIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
-                    keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
-                    if (needWakeLock) {
-                        keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED,
-                                WAKELOCK_RELEASE_ON_FINISHED);
-                    }
-                    getContext().sendOrderedBroadcastAsUser(keyIntent, UserHandle.CURRENT,
-                            null, mKeyEventDone, mHandler, Activity.RESULT_OK, null, null);
+                    return;
                 }
+                if (DEBUG) {
+                    Log.d(TAG, "Sending media key ordered broadcast");
+                }
+                if (needWakeLock) {
+                    mMediaEventWakeLock.acquire();
+                }
+                // Fallback to legacy behavior
+                Intent keyIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);
+                keyIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
+                keyIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
+                if (needWakeLock) {
+                    keyIntent.putExtra(EXTRA_WAKELOCK_ACQUIRED, WAKELOCK_RELEASE_ON_FINISHED);
+                }
+                // Send broadcast only to the full user.
+                getContext().sendOrderedBroadcastAsUser(keyIntent, UserHandle.CURRENT,
+                        null, mKeyEventDone, mHandler, Activity.RESULT_OK, null, null);
             }
         }
 
@@ -1044,6 +1061,7 @@
                 if (voiceIntent != null) {
                     voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                             | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
+                    if (DEBUG) Log.d(TAG, "voiceIntent: " + voiceIntent);
                     getContext().startActivityAsUser(voiceIntent, UserHandle.CURRENT);
                 }
             } catch (ActivityNotFoundException e) {
diff --git a/services/core/java/com/android/server/media/MediaSessionStack.java b/services/core/java/com/android/server/media/MediaSessionStack.java
index cc007ef..3327b36 100644
--- a/services/core/java/com/android/server/media/MediaSessionStack.java
+++ b/services/core/java/com/android/server/media/MediaSessionStack.java
@@ -32,7 +32,7 @@
  * Keeps track of media sessions and their priority for notifications, media
  * button dispatch, etc.
  */
-public class MediaSessionStack {
+class MediaSessionStack {
     /**
      * These are states that usually indicate the user took an action and should
      * bump priority regardless of the old state.
@@ -68,13 +68,10 @@
      * Checks if a media session is created from the most recent app.
      *
      * @param record A media session record to be examined.
-     * @return true if the media session's package name equals to the most recent app, false
-     * otherwise.
+     * @return {@code true} if the media session's package name equals to the most recent app, false
+     *            otherwise.
      */
     private static boolean isFromMostRecentApp(MediaSessionRecord record) {
-        if (ActivityManager.getCurrentUser() != record.getUserId()) {
-            return false;
-        }
         try {
             List<ActivityManager.RecentTaskInfo> tasks =
                     ActivityManagerNative.getDefault().getRecentTasks(1,
@@ -84,9 +81,10 @@
                             ActivityManager.RECENT_WITH_EXCLUDED, record.getUserId()).getList();
             if (tasks != null && !tasks.isEmpty()) {
                 ActivityManager.RecentTaskInfo recentTask = tasks.get(0);
-                if (recentTask.baseIntent != null)
+                if (recentTask.userId == record.getUserId() && recentTask.baseIntent != null) {
                     return recentTask.baseIntent.getComponent().getPackageName()
                             .equals(record.getPackageName());
+                }
             }
         } catch (RemoteException e) {
             return false;
@@ -98,11 +96,12 @@
      * Add a record to the priority tracker.
      *
      * @param record The record to add.
+     * @param fromForegroundUser {@code true} if the session is created by the foreground user.
      */
-    public void addSession(MediaSessionRecord record) {
+    public void addSession(MediaSessionRecord record, boolean fromForegroundUser) {
         mSessions.add(record);
         clearCache();
-        if (isFromMostRecentApp(record)) {
+        if (fromForegroundUser && isFromMostRecentApp(record)) {
             mLastInterestingRecord = record;
         }
     }
@@ -210,12 +209,13 @@
     /**
      * Get the highest priority session that can handle media buttons.
      *
-     * @param userId The user to check.
+     * @param userIdList The user lists to check.
      * @param includeNotPlaying Return a non-playing session if nothing else is
      *            available
      * @return The default media button session or null.
      */
-    public MediaSessionRecord getDefaultMediaButtonSession(int userId, boolean includeNotPlaying) {
+    public MediaSessionRecord getDefaultMediaButtonSession(
+            List<Integer> userIdList, boolean includeNotPlaying) {
         if (mGlobalPrioritySession != null && mGlobalPrioritySession.isActive()) {
             return mGlobalPrioritySession;
         }
@@ -223,7 +223,7 @@
             return mCachedButtonReceiver;
         }
         ArrayList<MediaSessionRecord> records = getPriorityListLocked(true,
-                MediaSession.FLAG_HANDLES_MEDIA_BUTTONS, userId);
+                MediaSession.FLAG_HANDLES_MEDIA_BUTTONS, userIdList);
         if (records.size() > 0) {
             MediaSessionRecord record = records.get(0);
             if (record.isPlaybackActive(false)) {
@@ -248,14 +248,14 @@
         return mCachedButtonReceiver;
     }
 
-    public MediaSessionRecord getDefaultVolumeSession(int userId) {
+    public MediaSessionRecord getDefaultVolumeSession(List<Integer> userIdList) {
         if (mGlobalPrioritySession != null && mGlobalPrioritySession.isActive()) {
             return mGlobalPrioritySession;
         }
         if (mCachedVolumeDefault != null) {
             return mCachedVolumeDefault;
         }
-        ArrayList<MediaSessionRecord> records = getPriorityListLocked(true, 0, userId);
+        ArrayList<MediaSessionRecord> records = getPriorityListLocked(true, 0, userIdList);
         int size = records.size();
         for (int i = 0; i < size; i++) {
             MediaSessionRecord record = records.get(i);
@@ -298,6 +298,13 @@
         }
     }
 
+    private ArrayList<MediaSessionRecord> getPriorityListLocked(boolean activeOnly, int withFlags,
+            int userId) {
+        List<Integer> userIdList = new ArrayList<>();
+        userIdList.add(userId);
+        return getPriorityListLocked(activeOnly, withFlags, userIdList);
+    }
+
     /**
      * Get a priority sorted list of sessions. Can filter to only return active
      * sessions or sessions with specific flags.
@@ -306,22 +313,23 @@
      *            all sessions.
      * @param withFlags Only return sessions with all the specified flags set. 0
      *            returns all sessions.
-     * @param userId The user to get sessions for. {@link UserHandle#USER_ALL}
+     * @param userIdList The user to get sessions for. {@link UserHandle#USER_ALL}
      *            will return sessions for all users.
      * @return The priority sorted list of sessions.
      */
     private ArrayList<MediaSessionRecord> getPriorityListLocked(boolean activeOnly, int withFlags,
-            int userId) {
+            List<Integer> userIdList) {
         ArrayList<MediaSessionRecord> result = new ArrayList<MediaSessionRecord>();
         int lastLocalIndex = 0;
         int lastActiveIndex = 0;
         int lastPublishedIndex = 0;
 
+        boolean filterUser = !userIdList.contains(UserHandle.USER_ALL);
         int size = mSessions.size();
         for (int i = 0; i < size; i++) {
             final MediaSessionRecord session = mSessions.get(i);
 
-            if (userId != UserHandle.USER_ALL && userId != session.getUserId()) {
+            if (filterUser && !userIdList.contains(session.getUserId())) {
                 // Filter out sessions for the wrong user
                 continue;
             }
diff --git a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
index 330f46a..5eab309 100644
--- a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
+++ b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java
@@ -89,6 +89,8 @@
 import static org.xmlpull.v1.XmlPullParser.START_TAG;
 
 import android.Manifest;
+import android.annotation.IntDef;
+import android.annotation.Nullable;
 import android.app.ActivityManager;
 import android.app.AppGlobals;
 import android.app.AppOpsManager;
@@ -132,7 +134,6 @@
 import android.os.HandlerThread;
 import android.os.IDeviceIdleController;
 import android.os.INetworkManagementService;
-import android.os.IPowerManager;
 import android.os.Message;
 import android.os.MessageQueue.IdleHandler;
 import android.os.PowerManager;
@@ -141,6 +142,7 @@
 import android.os.RemoteException;
 import android.os.ResultReceiver;
 import android.os.ServiceManager;
+import android.os.Trace;
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.provider.Settings;
@@ -162,8 +164,8 @@
 import android.util.Xml;
 
 import com.android.internal.R;
+import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.internal.content.PackageMonitor;
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.FastXmlSerializer;
 import com.android.internal.util.IndentingPrintWriter;
@@ -187,6 +189,8 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.PrintWriter;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -199,6 +203,26 @@
  * Derives active rules by combining a given policy with other system status,
  * and delivers to listeners, such as {@link ConnectivityManager}, for
  * enforcement.
+ *
+ * <p>
+ * This class uses 2-3 locks to synchronize state:
+ * <ul>
+ * <li>{@code mUidRulesFirstLock}: used to guard state related to individual UIDs (such as firewall
+ * rules).
+ * <li>{@code mNetworkPoliciesSecondLock}: used to guard state related to network interfaces (such
+ * as network policies).
+ * <li>{@code allLocks}: not a "real" lock, but an indication (through @GuardedBy) that all locks
+ * must be held.
+ * </ul>
+ *
+ * <p>
+ * As such, methods that require synchronization have the following prefixes:
+ * <ul>
+ * <li>{@code UL()}: require the "UID" lock ({@code mUidRulesFirstLock}).
+ * <li>{@code NL()}: require the "Network" lock ({@code mNetworkPoliciesSecondLock}).
+ * <li>{@code AL()}: require all locks, which must be obtained in order ({@code mUidRulesFirstLock}
+ * first, then {@code mNetworkPoliciesSecondLock}, then {@code mYetAnotherGuardThirdLock}, etc..
+ * </ul>
  */
 public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
     static final String TAG = "NetworkPolicy";
@@ -262,15 +286,14 @@
     private static final int MSG_LIMIT_REACHED = 5;
     private static final int MSG_RESTRICT_BACKGROUND_CHANGED = 6;
     private static final int MSG_ADVISE_PERSIST_THRESHOLD = 7;
-    private static final int MSG_SCREEN_ON_CHANGED = 8;
     private static final int MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED = 9;
     private static final int MSG_UPDATE_INTERFACE_QUOTA = 10;
     private static final int MSG_REMOVE_INTERFACE_QUOTA = 11;
     private static final int MSG_RESTRICT_BACKGROUND_BLACKLIST_CHANGED = 12;
+    private static final int MSG_SET_FIREWALL_RULES = 13;
 
     private final Context mContext;
     private final IActivityManager mActivityManager;
-    private final IPowerManager mPowerManager;
     private final INetworkStatsService mNetworkStats;
     private final INetworkManagementService mNetworkManager;
     private UsageStatsManagerInternal mUsageStats;
@@ -282,13 +305,15 @@
     private PowerManagerInternal mPowerManagerInternal;
     private IDeviceIdleController mDeviceIdleController;
 
-    final Object mRulesLock = new Object();
+    // See main javadoc for instructions on how to use these locks.
+    final Object mUidRulesFirstLock = new Object();
+    final Object mNetworkPoliciesSecondLock = new Object();
 
-    volatile boolean mSystemReady;
-    volatile boolean mScreenOn;
-    volatile boolean mRestrictBackground;
-    volatile boolean mRestrictPower;
-    volatile boolean mDeviceIdleMode;
+    @GuardedBy("allLocks") volatile boolean mSystemReady;
+
+    @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictBackground;
+    @GuardedBy("mUidRulesFirstLock") volatile boolean mRestrictPower;
+    @GuardedBy("mUidRulesFirstLock") volatile boolean mDeviceIdleMode;
 
     private final boolean mSuppressDefaultPolicy;
 
@@ -298,15 +323,19 @@
     final ArrayMap<NetworkPolicy, String[]> mNetworkRules = new ArrayMap<>();
 
     /** Defined UID policies. */
-    final SparseIntArray mUidPolicy = new SparseIntArray();
+    @GuardedBy("mUidRulesFirstLock") final SparseIntArray mUidPolicy = new SparseIntArray();
     /** Currently derived rules for each UID. */
-    final SparseIntArray mUidRules = new SparseIntArray();
+    @GuardedBy("mUidRulesFirstLock") final SparseIntArray mUidRules = new SparseIntArray();
 
+    @GuardedBy("mUidRulesFirstLock")
     final SparseIntArray mUidFirewallStandbyRules = new SparseIntArray();
+    @GuardedBy("mUidRulesFirstLock")
     final SparseIntArray mUidFirewallDozableRules = new SparseIntArray();
+    @GuardedBy("mUidRulesFirstLock")
     final SparseIntArray mUidFirewallPowerSaveRules = new SparseIntArray();
 
     /** Set of states for the child firewall chains. True if the chain is active. */
+    @GuardedBy("mUidRulesFirstLock")
     final SparseBooleanArray mFirewallChainStates = new SparseBooleanArray();
 
     /**
@@ -314,6 +343,7 @@
      * in power save mode, except device idle (doze) still applies.
      * TODO: An int array might be sufficient
      */
+    @GuardedBy("mUidRulesFirstLock")
     private final SparseBooleanArray mPowerSaveWhitelistExceptIdleAppIds = new SparseBooleanArray();
 
     /**
@@ -321,18 +351,22 @@
      * in power save mode.
      * TODO: An int array might be sufficient
      */
+    @GuardedBy("mUidRulesFirstLock")
     private final SparseBooleanArray mPowerSaveWhitelistAppIds = new SparseBooleanArray();
 
+    @GuardedBy("mUidRulesFirstLock")
     private final SparseBooleanArray mPowerSaveTempWhitelistAppIds = new SparseBooleanArray();
 
     /**
      * UIDs that have been white-listed to avoid restricted background.
      */
+    @GuardedBy("mUidRulesFirstLock")
     private final SparseBooleanArray mRestrictBackgroundWhitelistUids = new SparseBooleanArray();
 
     /**
      * UIDs that have been initially white-listed by system to avoid restricted background.
      */
+    @GuardedBy("mUidRulesFirstLock")
     private final SparseBooleanArray mDefaultRestrictBackgroundWhitelistUids =
             new SparseBooleanArray();
 
@@ -340,18 +374,23 @@
      * UIDs that have been initially white-listed by system to avoid restricted background,
      * but later revoked by user.
      */
+    @GuardedBy("mUidRulesFirstLock")
     private final SparseBooleanArray mRestrictBackgroundWhitelistRevokedUids =
             new SparseBooleanArray();
 
     /** Set of ifaces that are metered. */
+    @GuardedBy("mNetworkPoliciesSecondLock")
     private ArraySet<String> mMeteredIfaces = new ArraySet<>();
     /** Set of over-limit templates that have been notified. */
+    @GuardedBy("mNetworkPoliciesSecondLock")
     private final ArraySet<NetworkTemplate> mOverLimitNotified = new ArraySet<>();
 
     /** Set of currently active {@link Notification} tags. */
+    @GuardedBy("mNetworkPoliciesSecondLock")
     private final ArraySet<String> mActiveNotifs = new ArraySet<String>();
 
     /** Foreground at UID granularity. */
+    @GuardedBy("mUidRulesFirstLock")
     final SparseIntArray mUidState = new SparseIntArray();
 
     /** Higher priority listener before general event dispatch */
@@ -362,11 +401,11 @@
 
     final Handler mHandler;
 
+    @GuardedBy("allLocks")
     private final AtomicFile mPolicyFile;
 
     private final AppOpsManager mAppOps;
 
-    private final MyPackageMonitor mPackageMonitor;
     private final IPackageManager mIPm;
 
 
@@ -376,9 +415,8 @@
     // TODO: migrate notifications to SystemUI
 
     public NetworkPolicyManagerService(Context context, IActivityManager activityManager,
-            IPowerManager powerManager, INetworkStatsService networkStats,
-            INetworkManagementService networkManagement) {
-        this(context, activityManager, powerManager, networkStats, networkManagement,
+            INetworkStatsService networkStats, INetworkManagementService networkManagement) {
+        this(context, activityManager, networkStats, networkManagement,
                 NtpTrustedTime.getInstance(context), getSystemDir(), false);
     }
 
@@ -387,12 +425,10 @@
     }
 
     public NetworkPolicyManagerService(Context context, IActivityManager activityManager,
-            IPowerManager powerManager, INetworkStatsService networkStats,
-            INetworkManagementService networkManagement, TrustedTime time, File systemDir,
-            boolean suppressDefaultPolicy) {
+            INetworkStatsService networkStats, INetworkManagementService networkManagement,
+            TrustedTime time, File systemDir, boolean suppressDefaultPolicy) {
         mContext = checkNotNull(context, "missing context");
         mActivityManager = checkNotNull(activityManager, "missing activityManager");
-        mPowerManager = checkNotNull(powerManager, "missing powerManager");
         mNetworkStats = checkNotNull(networkStats, "missing networkStats");
         mNetworkManager = checkNotNull(networkManagement, "missing networkManagement");
         mDeviceIdleController = IDeviceIdleController.Stub.asInterface(ServiceManager.getService(
@@ -411,8 +447,6 @@
 
         mAppOps = context.getSystemService(AppOpsManager.class);
 
-        mPackageMonitor = new MyPackageMonitor();
-
         // Expose private service for system components to use.
         LocalServices.addService(NetworkPolicyManagerInternal.class,
                 new NetworkPolicyManagerInternalImpl());
@@ -426,7 +460,7 @@
         mNotifManager = checkNotNull(notifManager, "missing INotificationManager");
     }
 
-    void updatePowerSaveWhitelistLocked() {
+    void updatePowerSaveWhitelistUL() {
         try {
             int[] whitelist = mDeviceIdleController.getAppIdWhitelistExceptIdle();
             mPowerSaveWhitelistExceptIdleAppIds.clear();
@@ -452,19 +486,19 @@
      *
      * @return whether any uid has been added to {@link #mRestrictBackgroundWhitelistUids}.
      */
-    boolean addDefaultRestrictBackgroundWhitelistUidsLocked() {
+    boolean addDefaultRestrictBackgroundWhitelistUidsUL() {
         final List<UserInfo> users = mUserManager.getUsers();
         final int numberUsers = users.size();
 
         boolean changed = false;
         for (int i = 0; i < numberUsers; i++) {
             final UserInfo user = users.get(i);
-            changed = addDefaultRestrictBackgroundWhitelistUidsLocked(user.id) || changed;
+            changed = addDefaultRestrictBackgroundWhitelistUidsUL(user.id) || changed;
         }
         return changed;
     }
 
-    private boolean addDefaultRestrictBackgroundWhitelistUidsLocked(int userId) {
+    private boolean addDefaultRestrictBackgroundWhitelistUidsUL(int userId) {
         final SystemConfig sysConfig = SystemConfig.getInstance();
         final PackageManager pm = mContext.getPackageManager();
         final ArraySet<String> allowDataUsage = sysConfig.getAllowInDataUsageSave();
@@ -478,12 +512,13 @@
             try {
                 app = pm.getApplicationInfoAsUser(pkg, PackageManager.MATCH_SYSTEM_ONLY, userId);
             } catch (PackageManager.NameNotFoundException e) {
-                // Should not happen
-                Slog.wtf(TAG, "No ApplicationInfo for package " + pkg);
+                if (LOGD) Slog.d(TAG, "No ApplicationInfo for package " + pkg);
+                // Ignore it - some apps on allow-in-data-usage-save are optional.
                 continue;
             }
             if (!app.isPrivilegedApp()) {
-                Slog.wtf(TAG, "pm.getApplicationInfoAsUser() returned non-privileged app: " + pkg);
+                Slog.e(TAG, "addDefaultRestrictBackgroundWhitelistUidsUL(): "
+                        + "skipping non-privileged app  " + pkg);
                 continue;
             }
             final int uid = UserHandle.getUid(userId, app.uid);
@@ -493,8 +528,9 @@
                         + "background whitelist. Revoked status: "
                         + mRestrictBackgroundWhitelistRevokedUids.get(uid));
             if (!mRestrictBackgroundWhitelistRevokedUids.get(uid)) {
-                Slog.i(TAG, "adding default package " + pkg + " (uid " + uid + " for user "
-                        + userId + ") to restrict background whitelist");
+                if (LOGD)
+                    Slog.d(TAG, "adding default package " + pkg + " (uid " + uid + " for user "
+                            + userId + ") to restrict background whitelist");
                 mRestrictBackgroundWhitelistUids.append(uid, true);
                 changed = true;
             }
@@ -502,7 +538,7 @@
         return changed;
     }
 
-    void updatePowerSaveTempWhitelistLocked() {
+    void updatePowerSaveTempWhitelistUL() {
         try {
             // Clear the states of the current whitelist
             final int N = mPowerSaveTempWhitelistAppIds.size();
@@ -523,7 +559,7 @@
     /**
      * Remove unnecessary entries in the temp whitelist
      */
-    void purgePowerSaveTempWhitelistLocked() {
+    void purgePowerSaveTempWhitelistUL() {
         final int N = mPowerSaveTempWhitelistAppIds.size();
         for (int i = N - 1; i >= 0; i--) {
             if (mPowerSaveTempWhitelistAppIds.valueAt(i) == false) {
@@ -533,131 +569,131 @@
     }
 
     public void systemReady() {
-        if (!isBandwidthControlEnabled()) {
-            Slog.w(TAG, "bandwidth controls disabled, unable to enforce policy");
-            return;
-        }
-
-        mUsageStats = LocalServices.getService(UsageStatsManagerInternal.class);
-
-        mPackageMonitor.register(mContext, mHandler.getLooper(), UserHandle.ALL, true);
-
-        synchronized (mRulesLock) {
-            updatePowerSaveWhitelistLocked();
-            mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
-            mPowerManagerInternal.registerLowPowerModeObserver(
-                    new PowerManagerInternal.LowPowerModeListener() {
-                @Override
-                public void onLowPowerModeChanged(boolean enabled) {
-                    if (LOGD) Slog.d(TAG, "onLowPowerModeChanged(" + enabled + ")");
-                    synchronized (mRulesLock) {
-                        if (mRestrictPower != enabled) {
-                            mRestrictPower = enabled;
-                            updateRulesForRestrictPowerLocked();
-                            updateRulesForGlobalChangeLocked(true);
-                        }
-                    }
-                }
-            });
-            mRestrictPower = mPowerManagerInternal.getLowPowerModeEnabled();
-
-            mSystemReady = true;
-
-            // read policy from disk
-            readPolicyLocked();
-
-            if (addDefaultRestrictBackgroundWhitelistUidsLocked()) {
-                writePolicyLocked();
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "systemReady");
+        try {
+            if (!isBandwidthControlEnabled()) {
+                Slog.w(TAG, "bandwidth controls disabled, unable to enforce policy");
+                return;
             }
 
-            updateRulesForGlobalChangeLocked(false);
-            updateNotificationsLocked();
+            mUsageStats = LocalServices.getService(UsageStatsManagerInternal.class);
+
+            synchronized (mUidRulesFirstLock) {
+                synchronized (mNetworkPoliciesSecondLock) {
+                    updatePowerSaveWhitelistUL();
+                    mPowerManagerInternal = LocalServices.getService(PowerManagerInternal.class);
+                    mPowerManagerInternal.registerLowPowerModeObserver(
+                            new PowerManagerInternal.LowPowerModeListener() {
+                        @Override
+                        public void onLowPowerModeChanged(boolean enabled) {
+                            if (LOGD) Slog.d(TAG, "onLowPowerModeChanged(" + enabled + ")");
+                            synchronized (mUidRulesFirstLock) {
+                                if (mRestrictPower != enabled) {
+                                    mRestrictPower = enabled;
+                                    updateRulesForRestrictPowerUL();
+                                }
+                            }
+                        }
+                    });
+                    mRestrictPower = mPowerManagerInternal.getLowPowerModeEnabled();
+
+                    mSystemReady = true;
+
+                    // read policy from disk
+                    readPolicyAL();
+
+                    if (addDefaultRestrictBackgroundWhitelistUidsUL()) {
+                        writePolicyAL();
+                    }
+
+                    setRestrictBackgroundUL(mRestrictBackground);
+                    updateRulesForGlobalChangeAL(false);
+                    updateNotificationsNL();
+                }
+            }
+
+            try {
+                mActivityManager.registerUidObserver(mUidObserver,
+                        ActivityManager.UID_OBSERVER_PROCSTATE|ActivityManager.UID_OBSERVER_GONE);
+                mNetworkManager.registerObserver(mAlertObserver);
+            } catch (RemoteException e) {
+                // ignored; both services live in system_server
+            }
+
+            // listen for changes to power save whitelist
+            final IntentFilter whitelistFilter = new IntentFilter(
+                    PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED);
+            mContext.registerReceiver(mPowerSaveWhitelistReceiver, whitelistFilter, null, mHandler);
+
+            DeviceIdleController.LocalService deviceIdleService
+                    = LocalServices.getService(DeviceIdleController.LocalService.class);
+            deviceIdleService.setNetworkPolicyTempWhitelistCallback(mTempPowerSaveChangedCallback);
+
+            // watch for network interfaces to be claimed
+            final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION);
+            mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler);
+
+            // listen for package changes to update policy
+            final IntentFilter packageFilter = new IntentFilter();
+            packageFilter.addAction(ACTION_PACKAGE_ADDED);
+            packageFilter.addDataScheme("package");
+            mContext.registerReceiver(mPackageReceiver, packageFilter, null, mHandler);
+
+            // listen for UID changes to update policy
+            mContext.registerReceiver(
+                    mUidRemovedReceiver, new IntentFilter(ACTION_UID_REMOVED), null, mHandler);
+
+            // listen for user changes to update policy
+            final IntentFilter userFilter = new IntentFilter();
+            userFilter.addAction(ACTION_USER_ADDED);
+            userFilter.addAction(ACTION_USER_REMOVED);
+            mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler);
+
+            // listen for stats update events
+            final IntentFilter statsFilter = new IntentFilter(ACTION_NETWORK_STATS_UPDATED);
+            mContext.registerReceiver(
+                    mStatsReceiver, statsFilter, READ_NETWORK_USAGE_HISTORY, mHandler);
+
+            // listen for restrict background changes from notifications
+            final IntentFilter allowFilter = new IntentFilter(ACTION_ALLOW_BACKGROUND);
+            mContext.registerReceiver(mAllowReceiver, allowFilter, MANAGE_NETWORK_POLICY, mHandler);
+
+            // listen for snooze warning from notifications
+            final IntentFilter snoozeWarningFilter = new IntentFilter(ACTION_SNOOZE_WARNING);
+            mContext.registerReceiver(mSnoozeWarningReceiver, snoozeWarningFilter,
+                    MANAGE_NETWORK_POLICY, mHandler);
+
+            // listen for configured wifi networks to be removed
+            final IntentFilter wifiConfigFilter =
+                    new IntentFilter(CONFIGURED_NETWORKS_CHANGED_ACTION);
+            mContext.registerReceiver(mWifiConfigReceiver, wifiConfigFilter, null, mHandler);
+
+            // listen for wifi state changes to catch metered hint
+            final IntentFilter wifiStateFilter = new IntentFilter(
+                    WifiManager.NETWORK_STATE_CHANGED_ACTION);
+            mContext.registerReceiver(mWifiStateReceiver, wifiStateFilter, null, mHandler);
+
+            mUsageStats.addAppIdleStateChangeListener(new AppIdleStateChangeListener());
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
         }
-
-        updateScreenOn();
-
-        try {
-            mActivityManager.registerUidObserver(mUidObserver,
-                    ActivityManager.UID_OBSERVER_PROCSTATE|ActivityManager.UID_OBSERVER_GONE);
-            mNetworkManager.registerObserver(mAlertObserver);
-        } catch (RemoteException e) {
-            // ignored; both services live in system_server
-        }
-
-        // TODO: traverse existing processes to know foreground state, or have
-        // activitymanager dispatch current state when new observer attached.
-
-        final IntentFilter screenFilter = new IntentFilter();
-        screenFilter.addAction(Intent.ACTION_SCREEN_ON);
-        screenFilter.addAction(Intent.ACTION_SCREEN_OFF);
-        mContext.registerReceiver(mScreenReceiver, screenFilter);
-
-        // listen for changes to power save whitelist
-        final IntentFilter whitelistFilter = new IntentFilter(
-                PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED);
-        mContext.registerReceiver(mPowerSaveWhitelistReceiver, whitelistFilter, null, mHandler);
-
-        DeviceIdleController.LocalService deviceIdleService
-                = LocalServices.getService(DeviceIdleController.LocalService.class);
-        deviceIdleService.setNetworkPolicyTempWhitelistCallback(mTempPowerSaveChangedCallback);
-
-        // watch for network interfaces to be claimed
-        final IntentFilter connFilter = new IntentFilter(CONNECTIVITY_ACTION);
-        mContext.registerReceiver(mConnReceiver, connFilter, CONNECTIVITY_INTERNAL, mHandler);
-
-        // listen for package changes to update policy
-        final IntentFilter packageFilter = new IntentFilter();
-        packageFilter.addAction(ACTION_PACKAGE_ADDED);
-        packageFilter.addDataScheme("package");
-        mContext.registerReceiver(mPackageReceiver, packageFilter, null, mHandler);
-
-        // listen for UID changes to update policy
-        mContext.registerReceiver(
-                mUidRemovedReceiver, new IntentFilter(ACTION_UID_REMOVED), null, mHandler);
-
-        // listen for user changes to update policy
-        final IntentFilter userFilter = new IntentFilter();
-        userFilter.addAction(ACTION_USER_ADDED);
-        userFilter.addAction(ACTION_USER_REMOVED);
-        mContext.registerReceiver(mUserReceiver, userFilter, null, mHandler);
-
-        // listen for stats update events
-        final IntentFilter statsFilter = new IntentFilter(ACTION_NETWORK_STATS_UPDATED);
-        mContext.registerReceiver(
-                mStatsReceiver, statsFilter, READ_NETWORK_USAGE_HISTORY, mHandler);
-
-        // listen for restrict background changes from notifications
-        final IntentFilter allowFilter = new IntentFilter(ACTION_ALLOW_BACKGROUND);
-        mContext.registerReceiver(mAllowReceiver, allowFilter, MANAGE_NETWORK_POLICY, mHandler);
-
-        // listen for snooze warning from notifications
-        final IntentFilter snoozeWarningFilter = new IntentFilter(ACTION_SNOOZE_WARNING);
-        mContext.registerReceiver(mSnoozeWarningReceiver, snoozeWarningFilter,
-                MANAGE_NETWORK_POLICY, mHandler);
-
-        // listen for configured wifi networks to be removed
-        final IntentFilter wifiConfigFilter = new IntentFilter(CONFIGURED_NETWORKS_CHANGED_ACTION);
-        mContext.registerReceiver(mWifiConfigReceiver, wifiConfigFilter, null, mHandler);
-
-        // listen for wifi state changes to catch metered hint
-        final IntentFilter wifiStateFilter = new IntentFilter(
-                WifiManager.NETWORK_STATE_CHANGED_ACTION);
-        mContext.registerReceiver(mWifiStateReceiver, wifiStateFilter, null, mHandler);
-
-        mUsageStats.addAppIdleStateChangeListener(new AppIdleStateChangeListener());
-
     }
 
     final private IUidObserver mUidObserver = new IUidObserver.Stub() {
         @Override public void onUidStateChanged(int uid, int procState) throws RemoteException {
-            synchronized (mRulesLock) {
-                updateUidStateLocked(uid, procState);
+            Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "onUidStateChanged");
+            try {
+                synchronized (mUidRulesFirstLock) {
+                    updateUidStateUL(uid, procState);
+                }
+            } finally {
+                Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
             }
         }
 
         @Override public void onUidGone(int uid) throws RemoteException {
-            synchronized (mRulesLock) {
-                removeUidStateLocked(uid);
+            synchronized (mUidRulesFirstLock) {
+                removeUidStateUL(uid);
             }
         }
 
@@ -672,9 +708,10 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             // on background handler thread, and POWER_SAVE_WHITELIST_CHANGED is protected
-            synchronized (mRulesLock) {
-                updatePowerSaveWhitelistLocked();
-                updateRulesForGlobalChangeLocked(false);
+            synchronized (mUidRulesFirstLock) {
+                updatePowerSaveWhitelistUL();
+                updateRulesForRestrictPowerUL();
+                updateRulesForAppIdleUL();
             }
         }
     };
@@ -682,23 +719,14 @@
     final private Runnable mTempPowerSaveChangedCallback = new Runnable() {
         @Override
         public void run() {
-            synchronized (mRulesLock) {
-                updatePowerSaveTempWhitelistLocked();
-                updateRulesForTempWhitelistChangeLocked();
-                purgePowerSaveTempWhitelistLocked();
+            synchronized (mUidRulesFirstLock) {
+                updatePowerSaveTempWhitelistUL();
+                updateRulesForTempWhitelistChangeUL();
+                purgePowerSaveTempWhitelistUL();
             }
         }
     };
 
-    final private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            // screen-related broadcasts are protected by system, no need
-            // for permissions check.
-            mHandler.obtainMessage(MSG_SCREEN_ON_CHANGED).sendToTarget();
-        }
-    };
-
     final private BroadcastReceiver mPackageReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -712,8 +740,8 @@
                 // update rules for UID, since it might be subject to
                 // global background data policy
                 if (LOGV) Slog.v(TAG, "ACTION_PACKAGE_ADDED for uid=" + uid);
-                synchronized (mRulesLock) {
-                    updateRestrictionRulesForUidLocked(uid);
+                synchronized (mUidRulesFirstLock) {
+                    updateRestrictionRulesForUidUL(uid);
                 }
             }
         }
@@ -729,10 +757,13 @@
 
             // remove any policy and update rules to clean up
             if (LOGV) Slog.v(TAG, "ACTION_UID_REMOVED for uid=" + uid);
-            synchronized (mRulesLock) {
+            synchronized (mUidRulesFirstLock) {
                 mUidPolicy.delete(uid);
-                updateRestrictionRulesForUidLocked(uid);
-                writePolicyLocked();
+                removeRestrictBackgroundWhitelistedUidUL(uid, true, true);
+                updateRestrictionRulesForUidUL(uid);
+                synchronized (mNetworkPoliciesSecondLock) {
+                    writePolicyAL();
+                }
             }
         }
     };
@@ -750,16 +781,18 @@
             switch (action) {
                 case ACTION_USER_REMOVED:
                 case ACTION_USER_ADDED:
-                    synchronized (mRulesLock) {
+                    synchronized (mUidRulesFirstLock) {
                         // Remove any persistable state for the given user; both cleaning up after a
                         // USER_REMOVED, and one last sanity check during USER_ADDED
-                        removeUserStateLocked(userId, true);
+                        removeUserStateUL(userId, true);
                         if (action == ACTION_USER_ADDED) {
                             // Add apps that are whitelisted by default.
-                            addDefaultRestrictBackgroundWhitelistUidsLocked(userId);
+                            addDefaultRestrictBackgroundWhitelistUidsUL(userId);
                         }
                         // Update global restrict for that user
-                        updateRulesForGlobalChangeLocked(true);
+                        synchronized (mNetworkPoliciesSecondLock) {
+                            updateRulesForGlobalChangeAL(true);
+                        }
                     }
                     break;
             }
@@ -777,9 +810,9 @@
             // READ_NETWORK_USAGE_HISTORY permission above.
 
             maybeRefreshTrustedTime();
-            synchronized (mRulesLock) {
-                updateNetworkEnabledLocked();
-                updateNotificationsLocked();
+            synchronized (mNetworkPoliciesSecondLock) {
+                updateNetworkEnabledNL();
+                updateNotificationsNL();
             }
         }
     };
@@ -828,10 +861,12 @@
                         EXTRA_WIFI_CONFIGURATION);
                 if (config.SSID != null) {
                     final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(config.SSID);
-                    synchronized (mRulesLock) {
-                        if (mNetworkPolicy.containsKey(template)) {
-                            mNetworkPolicy.remove(template);
-                            writePolicyLocked();
+                    synchronized (mUidRulesFirstLock) {
+                        synchronized (mNetworkPoliciesSecondLock) {
+                            if (mNetworkPolicy.containsKey(template)) {
+                                mNetworkPolicy.remove(template);
+                                writePolicyAL();
+                            }
                         }
                     }
                 }
@@ -857,13 +892,13 @@
             final boolean meteredHint = info.getMeteredHint();
 
             final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(info.getSSID());
-            synchronized (mRulesLock) {
+            synchronized (mNetworkPoliciesSecondLock) {
                 NetworkPolicy policy = mNetworkPolicy.get(template);
                 if (policy == null && meteredHint) {
                     // policy doesn't exist, and AP is hinting that it's
                     // metered: create an inferred policy.
                     policy = newWifiPolicy(template, meteredHint);
-                    addNetworkPolicyLocked(policy);
+                    addNetworkPolicyNL(policy);
 
                 } else if (policy != null && policy.inferred) {
                     // policy exists, and was inferred: update its current
@@ -872,7 +907,7 @@
 
                     // since this is inferred for each wifi session, just update
                     // rules without persisting.
-                    updateNetworkRulesLocked();
+                    updateNetworkRulesNL();
                 }
             }
         }
@@ -904,8 +939,8 @@
      * Check {@link NetworkPolicy} against current {@link INetworkStatsService}
      * to show visible notifications as needed.
      */
-    void updateNotificationsLocked() {
-        if (LOGV) Slog.v(TAG, "updateNotificationsLocked()");
+    void updateNotificationsNL() {
+        if (LOGV) Slog.v(TAG, "updateNotificationsNL()");
 
         // keep track of previously active notifications
         final ArraySet<String> beforeNotifs = new ArraySet<String>(mActiveNotifs);
@@ -931,11 +966,11 @@
                     enqueueNotification(policy, TYPE_LIMIT_SNOOZED, totalBytes);
                 } else {
                     enqueueNotification(policy, TYPE_LIMIT, totalBytes);
-                    notifyOverLimitLocked(policy.template);
+                    notifyOverLimitNL(policy.template);
                 }
 
             } else {
-                notifyUnderLimitLocked(policy.template);
+                notifyUnderLimitNL(policy.template);
 
                 if (policy.isOverWarning(totalBytes) && policy.lastWarningSnooze < start) {
                     enqueueNotification(policy, TYPE_WARNING, totalBytes);
@@ -983,14 +1018,14 @@
      * Notify that given {@link NetworkTemplate} is over
      * {@link NetworkPolicy#limitBytes}, potentially showing dialog to user.
      */
-    private void notifyOverLimitLocked(NetworkTemplate template) {
+    private void notifyOverLimitNL(NetworkTemplate template) {
         if (!mOverLimitNotified.contains(template)) {
-            mContext.startActivity(buildNetworkOverLimitIntent(template));
+            mContext.startActivity(buildNetworkOverLimitIntent(mContext.getResources(), template));
             mOverLimitNotified.add(template);
         }
     }
 
-    private void notifyUnderLimitLocked(NetworkTemplate template) {
+    private void notifyUnderLimitNL(NetworkTemplate template) {
         mOverLimitNotified.remove(template);
     }
 
@@ -1024,12 +1059,14 @@
                 builder.setTicker(title);
                 builder.setContentTitle(title);
                 builder.setContentText(body);
+                builder.setDefaults(Notification.DEFAULT_ALL);
+                builder.setPriority(Notification.PRIORITY_HIGH);
 
                 final Intent snoozeIntent = buildSnoozeWarningIntent(policy.template);
                 builder.setDeleteIntent(PendingIntent.getBroadcast(
                         mContext, 0, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT));
 
-                final Intent viewIntent = buildViewDataUsageIntent(policy.template);
+                final Intent viewIntent = buildViewDataUsageIntent(res, policy.template);
                 builder.setContentIntent(PendingIntent.getActivity(
                         mContext, 0, viewIntent, PendingIntent.FLAG_UPDATE_CURRENT));
 
@@ -1065,7 +1102,7 @@
                 builder.setContentTitle(title);
                 builder.setContentText(body);
 
-                final Intent intent = buildNetworkOverLimitIntent(policy.template);
+                final Intent intent = buildNetworkOverLimitIntent(res, policy.template);
                 builder.setContentIntent(PendingIntent.getActivity(
                         mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
                 break;
@@ -1100,7 +1137,7 @@
                 builder.setContentTitle(title);
                 builder.setContentText(body);
 
-                final Intent intent = buildViewDataUsageIntent(policy.template);
+                final Intent intent = buildViewDataUsageIntent(res, policy.template);
                 builder.setContentIntent(PendingIntent.getActivity(
                         mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
                 break;
@@ -1142,12 +1179,12 @@
             // permission above.
 
             maybeRefreshTrustedTime();
-            synchronized (mRulesLock) {
-                ensureActiveMobilePolicyLocked();
-                normalizePoliciesLocked();
-                updateNetworkEnabledLocked();
-                updateNetworkRulesLocked();
-                updateNotificationsLocked();
+            synchronized (mNetworkPoliciesSecondLock) {
+                ensureActiveMobilePolicyNL();
+                normalizePoliciesNL();
+                updateNetworkEnabledNL();
+                updateNetworkRulesNL();
+                updateNotificationsNL();
             }
         }
     };
@@ -1156,8 +1193,8 @@
      * Proactively control network data connections when they exceed
      * {@link NetworkPolicy#limitBytes}.
      */
-    void updateNetworkEnabledLocked() {
-        if (LOGV) Slog.v(TAG, "updateNetworkEnabledLocked()");
+    void updateNetworkEnabledNL() {
+        if (LOGV) Slog.v(TAG, "updateNetworkEnabledNL()");
 
         // TODO: reset any policy-disabled networks when any policy is removed
         // completely, which is currently rare case.
@@ -1191,6 +1228,24 @@
     private void setNetworkTemplateEnabled(NetworkTemplate template, boolean enabled) {
         // TODO: reach into ConnectivityManager to proactively disable bringing
         // up this network, since we know that traffic will be blocked.
+
+        if (template.getMatchRule() == MATCH_MOBILE_ALL) {
+            // If mobile data usage hits the limit or if the user resumes the data, we need to
+            // notify telephony.
+            final SubscriptionManager sm = SubscriptionManager.from(mContext);
+            final TelephonyManager tm = TelephonyManager.from(mContext);
+
+            final int[] subIds = sm.getActiveSubscriptionIdList();
+            for (int subId : subIds) {
+                final String subscriberId = tm.getSubscriberId(subId);
+                final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
+                        TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true);
+                // Template is matched when subscriber id matches.
+                if (template.matches(probeIdent)) {
+                    tm.setPolicyDataEnabled(enabled, subId);
+                }
+            }
+        }
     }
 
     /**
@@ -1198,8 +1253,8 @@
      * {@link NetworkPolicy} that need to be enforced. When matches found, set
      * remaining quota based on usage cycle and historical stats.
      */
-    void updateNetworkRulesLocked() {
-        if (LOGV) Slog.v(TAG, "updateNetworkRulesLocked()");
+    void updateNetworkRulesNL() {
+        if (LOGV) Slog.v(TAG, "updateNetworkRulesNL()");
 
         final NetworkState[] states;
         try {
@@ -1349,8 +1404,8 @@
      * Once any {@link #mNetworkPolicy} are loaded from disk, ensure that we
      * have at least a default mobile policy defined.
      */
-    private void ensureActiveMobilePolicyLocked() {
-        if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyLocked()");
+    private void ensureActiveMobilePolicyNL() {
+        if (LOGV) Slog.v(TAG, "ensureActiveMobilePolicyNL()");
         if (mSuppressDefaultPolicy) return;
 
         final TelephonyManager tele = TelephonyManager.from(mContext);
@@ -1359,11 +1414,11 @@
         final int[] subIds = sub.getActiveSubscriptionIdList();
         for (int subId : subIds) {
             final String subscriberId = tele.getSubscriberId(subId);
-            ensureActiveMobilePolicyLocked(subscriberId);
+            ensureActiveMobilePolicyNL(subscriberId);
         }
     }
 
-    private void ensureActiveMobilePolicyLocked(String subscriberId) {
+    private void ensureActiveMobilePolicyNL(String subscriberId) {
         // Poke around to see if we already have a policy
         final NetworkIdentity probeIdent = new NetworkIdentity(TYPE_MOBILE,
                 TelephonyManager.NETWORK_TYPE_UNKNOWN, subscriberId, null, false, true);
@@ -1382,8 +1437,14 @@
                 + "; generating default policy");
 
         // Build default mobile policy, and assume usage cycle starts today
-        final long warningBytes = mContext.getResources().getInteger(
-                com.android.internal.R.integer.config_networkPolicyDefaultWarning) * MB_IN_BYTES;
+        final int dataWarningConfig = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_networkPolicyDefaultWarning);
+        final long warningBytes;
+        if (dataWarningConfig == WARNING_DISABLED) {
+            warningBytes = WARNING_DISABLED;
+        } else {
+            warningBytes = dataWarningConfig * MB_IN_BYTES;
+        }
 
         final Time time = new Time();
         time.setToNow();
@@ -1394,11 +1455,11 @@
         final NetworkTemplate template = buildTemplateMobileAll(subscriberId);
         final NetworkPolicy policy = new NetworkPolicy(template, cycleDay, cycleTimezone,
                 warningBytes, LIMIT_DISABLED, SNOOZE_NEVER, SNOOZE_NEVER, true, true);
-        addNetworkPolicyLocked(policy);
+        addNetworkPolicyNL(policy);
     }
 
-    private void readPolicyLocked() {
-        if (LOGV) Slog.v(TAG, "readPolicyLocked()");
+    private void readPolicyAL() {
+        if (LOGV) Slog.v(TAG, "readPolicyAL()");
 
         // clear any existing policy and read from disk
         mNetworkPolicy.clear();
@@ -1498,7 +1559,7 @@
                         final int policy = readIntAttribute(in, ATTR_POLICY);
 
                         if (UserHandle.isApp(uid)) {
-                            setUidPolicyUncheckedLocked(uid, policy, false);
+                            setUidPolicyUncheckedUL(uid, policy, false);
                         } else {
                             Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                         }
@@ -1510,7 +1571,7 @@
                         // app policy is deprecated so this is only used in pre system user split.
                         final int uid = UserHandle.getUid(UserHandle.USER_SYSTEM, appId);
                         if (UserHandle.isApp(uid)) {
-                            setUidPolicyUncheckedLocked(uid, policy, false);
+                            setUidPolicyUncheckedUL(uid, policy, false);
                         } else {
                             Slog.w(TAG, "unable to apply policy to UID " + uid + "; ignoring");
                         }
@@ -1533,7 +1594,7 @@
 
         } catch (FileNotFoundException e) {
             // missing policy is okay, probably first boot
-            upgradeLegacyBackgroundData();
+            upgradeLegacyBackgroundDataUL();
         } catch (IOException e) {
             Log.wtf(TAG, "problem reading network policy", e);
         } catch (XmlPullParserException e) {
@@ -1547,7 +1608,7 @@
      * Upgrade legacy background data flags, notifying listeners of one last
      * change to always-true.
      */
-    private void upgradeLegacyBackgroundData() {
+    private void upgradeLegacyBackgroundDataUL() {
         mRestrictBackground = Settings.Secure.getInt(
                 mContext.getContentResolver(), Settings.Secure.BACKGROUND_DATA, 1) != 1;
 
@@ -1559,8 +1620,8 @@
         }
     }
 
-    void writePolicyLocked() {
-        if (LOGV) Slog.v(TAG, "writePolicyLocked()");
+    void writePolicyAL() {
+        if (LOGV) Slog.v(TAG, "writePolicyAL()");
 
         FileOutputStream fos = null;
         try {
@@ -1657,13 +1718,12 @@
         if (!UserHandle.isApp(uid)) {
             throw new IllegalArgumentException("cannot apply policy to UID " + uid);
         }
-
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             final long token = Binder.clearCallingIdentity();
             try {
                 final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE);
                 if (oldPolicy != policy) {
-                    setUidPolicyUncheckedLocked(uid, oldPolicy, policy, true);
+                    setUidPolicyUncheckedUL(uid, oldPolicy, policy, true);
                 }
             } finally {
                 Binder.restoreCallingIdentity(token);
@@ -1679,11 +1739,11 @@
             throw new IllegalArgumentException("cannot apply policy to UID " + uid);
         }
 
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE);
             policy |= oldPolicy;
             if (oldPolicy != policy) {
-                setUidPolicyUncheckedLocked(uid, oldPolicy, policy, true);
+                setUidPolicyUncheckedUL(uid, oldPolicy, policy, true);
             }
         }
     }
@@ -1696,17 +1756,17 @@
             throw new IllegalArgumentException("cannot apply policy to UID " + uid);
         }
 
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             final int oldPolicy = mUidPolicy.get(uid, POLICY_NONE);
             policy = oldPolicy & ~policy;
             if (oldPolicy != policy) {
-                setUidPolicyUncheckedLocked(uid, oldPolicy, policy, true);
+                setUidPolicyUncheckedUL(uid, oldPolicy, policy, true);
             }
         }
     }
 
-    private void setUidPolicyUncheckedLocked(int uid, int oldPolicy, int policy, boolean persist) {
-        setUidPolicyUncheckedLocked(uid, policy, persist);
+    private void setUidPolicyUncheckedUL(int uid, int oldPolicy, int policy, boolean persist) {
+        setUidPolicyUncheckedUL(uid, policy, persist);
 
         final boolean isBlacklisted = policy == POLICY_REJECT_METERED_BACKGROUND;
         mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_BLACKLIST_CHANGED, uid,
@@ -1721,13 +1781,15 @@
         }
     }
 
-    private void setUidPolicyUncheckedLocked(int uid, int policy, boolean persist) {
+    private void setUidPolicyUncheckedUL(int uid, int policy, boolean persist) {
         mUidPolicy.put(uid, policy);
 
         // uid policy changed, recompute rules and persist policy.
-        updateRulesForDataUsageRestrictionsLocked(uid);
+        updateRulesForDataUsageRestrictionsUL(uid);
         if (persist) {
-            writePolicyLocked();
+            synchronized (mNetworkPoliciesSecondLock) {
+                writePolicyAL();
+            }
         }
     }
 
@@ -1735,7 +1797,7 @@
     public int getUidPolicy(int uid) {
         mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
 
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             return mUidPolicy.get(uid, POLICY_NONE);
         }
     }
@@ -1745,7 +1807,7 @@
         mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
 
         int[] uids = new int[0];
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             for (int i = 0; i < mUidPolicy.size(); i++) {
                 final int uid = mUidPolicy.keyAt(i);
                 final int uidPolicy = mUidPolicy.valueAt(i);
@@ -1761,9 +1823,9 @@
      * Removes any persistable state associated with given {@link UserHandle}, persisting
      * if any changes that are made.
      */
-    boolean removeUserStateLocked(int userId, boolean writePolicy) {
+    boolean removeUserStateUL(int userId, boolean writePolicy) {
 
-        if (LOGV) Slog.v(TAG, "removeUserStateLocked()");
+        if (LOGV) Slog.v(TAG, "removeUserStateUL()");
         boolean changed = false;
 
         // Remove entries from restricted background UID whitelist
@@ -1777,7 +1839,7 @@
 
         if (wlUids.length > 0) {
             for (int uid : wlUids) {
-                removeRestrictBackgroundWhitelistedUidLocked(uid, false, false);
+                removeRestrictBackgroundWhitelistedUidUL(uid, false, false);
             }
             changed = true;
         }
@@ -1806,11 +1868,11 @@
             }
             changed = true;
         }
-
-        updateRulesForGlobalChangeLocked(true);
-
-        if (writePolicy && changed) {
-            writePolicyLocked();
+        synchronized (mNetworkPoliciesSecondLock) {
+            updateRulesForGlobalChangeAL(true);
+            if (writePolicy && changed) {
+                writePolicyAL();
+            }
         }
         return changed;
     }
@@ -1845,19 +1907,21 @@
         final long token = Binder.clearCallingIdentity();
         try {
             maybeRefreshTrustedTime();
-            synchronized (mRulesLock) {
-                normalizePoliciesLocked(policies);
-                updateNetworkEnabledLocked();
-                updateNetworkRulesLocked();
-                updateNotificationsLocked();
-                writePolicyLocked();
+            synchronized (mUidRulesFirstLock) {
+                synchronized (mNetworkPoliciesSecondLock) {
+                    normalizePoliciesNL(policies);
+                    updateNetworkEnabledNL();
+                    updateNetworkRulesNL();
+                    updateNotificationsNL();
+                    writePolicyAL();
+                }
             }
         } finally {
             Binder.restoreCallingIdentity(token);
         }
     }
 
-    void addNetworkPolicyLocked(NetworkPolicy policy) {
+    void addNetworkPolicyNL(NetworkPolicy policy) {
         NetworkPolicy[] policies = getNetworkPolicies(mContext.getOpPackageName());
         policies = ArrayUtils.appendElement(NetworkPolicy.class, policies, policy);
         setNetworkPolicies(policies);
@@ -1879,7 +1943,7 @@
             }
         }
 
-        synchronized (mRulesLock) {
+        synchronized (mNetworkPoliciesSecondLock) {
             final int size = mNetworkPolicy.size();
             final NetworkPolicy[] policies = new NetworkPolicy[size];
             for (int i = 0; i < size; i++) {
@@ -1889,11 +1953,11 @@
         }
     }
 
-    private void normalizePoliciesLocked() {
-        normalizePoliciesLocked(getNetworkPolicies(mContext.getOpPackageName()));
+    private void normalizePoliciesNL() {
+        normalizePoliciesNL(getNetworkPolicies(mContext.getOpPackageName()));
     }
 
-    private void normalizePoliciesLocked(NetworkPolicy[] policies) {
+    private void normalizePoliciesNL(NetworkPolicy[] policies) {
         final TelephonyManager tele = TelephonyManager.from(mContext);
         final String[] merged = tele.getMergedSubscriberIds();
 
@@ -1927,29 +1991,31 @@
     void performSnooze(NetworkTemplate template, int type) {
         maybeRefreshTrustedTime();
         final long currentTime = currentTimeMillis();
-        synchronized (mRulesLock) {
-            // find and snooze local policy that matches
-            final NetworkPolicy policy = mNetworkPolicy.get(template);
-            if (policy == null) {
-                throw new IllegalArgumentException("unable to find policy for " + template);
-            }
+        synchronized (mUidRulesFirstLock) {
+            synchronized (mNetworkPoliciesSecondLock) {
+                // find and snooze local policy that matches
+                final NetworkPolicy policy = mNetworkPolicy.get(template);
+                if (policy == null) {
+                    throw new IllegalArgumentException("unable to find policy for " + template);
+                }
 
-            switch (type) {
-                case TYPE_WARNING:
-                    policy.lastWarningSnooze = currentTime;
-                    break;
-                case TYPE_LIMIT:
-                    policy.lastLimitSnooze = currentTime;
-                    break;
-                default:
-                    throw new IllegalArgumentException("unexpected type");
-            }
+                switch (type) {
+                    case TYPE_WARNING:
+                        policy.lastWarningSnooze = currentTime;
+                        break;
+                    case TYPE_LIMIT:
+                        policy.lastLimitSnooze = currentTime;
+                        break;
+                    default:
+                        throw new IllegalArgumentException("unexpected type");
+                }
 
-            normalizePoliciesLocked();
-            updateNetworkEnabledLocked();
-            updateNetworkRulesLocked();
-            updateNotificationsLocked();
-            writePolicyLocked();
+                normalizePoliciesNL();
+                updateNetworkEnabledNL();
+                updateNetworkRulesNL();
+                updateNotificationsNL();
+                writePolicyAL();
+            }
         }
     }
 
@@ -1957,7 +2023,7 @@
     public void onTetheringChanged(String iface, boolean tethering) {
         // No need to enforce permission because setRestrictBackground() will do it.
         if (LOGD) Log.d(TAG, "onTetherStateChanged(" + iface + ", " + tethering + ")");
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             if (mRestrictBackground && tethering) {
                 Log.d(TAG, "Tethering on (" + iface +"); disable Data Saver");
                 setRestrictBackground(false);
@@ -1971,13 +2037,13 @@
         final long token = Binder.clearCallingIdentity();
         try {
             maybeRefreshTrustedTime();
-            synchronized (mRulesLock) {
+            synchronized (mUidRulesFirstLock) {
                 if (restrictBackground == mRestrictBackground) {
                     // Ideally, UI should never allow this scenario...
                     Slog.w(TAG, "setRestrictBackground: already " + restrictBackground);
                     return;
                 }
-                setRestrictBackgroundLocked(restrictBackground);
+                setRestrictBackgroundUL(restrictBackground);
             }
 
         } finally {
@@ -1988,27 +2054,29 @@
                 .sendToTarget();
     }
 
-    private void setRestrictBackgroundLocked(boolean restrictBackground) {
-        Slog.d(TAG, "setRestrictBackgroundLocked(): " + restrictBackground);
+    private void setRestrictBackgroundUL(boolean restrictBackground) {
+        Slog.d(TAG, "setRestrictBackgroundUL(): " + restrictBackground);
         final boolean oldRestrictBackground = mRestrictBackground;
         mRestrictBackground = restrictBackground;
         // Must whitelist foreground apps before turning data saver mode on.
         // TODO: there is no need to iterate through all apps here, just those in the foreground,
         // so it could call AM to get the UIDs of such apps, and iterate through them instead.
-        updateRulesForRestrictBackgroundLocked();
+        updateRulesForRestrictBackgroundUL();
         try {
             if (!mNetworkManager.setDataSaverModeEnabled(mRestrictBackground)) {
                 Slog.e(TAG, "Could not change Data Saver Mode on NMS to " + mRestrictBackground);
                 mRestrictBackground = oldRestrictBackground;
                 // TODO: if it knew the foreground apps (see TODO above), it could call
-                // updateRulesForRestrictBackgroundLocked() again to restore state.
+                // updateRulesForRestrictBackgroundUL() again to restore state.
                 return;
             }
         } catch (RemoteException e) {
             // ignored; service lives in system_server
         }
-        updateNotificationsLocked();
-        writePolicyLocked();
+        synchronized (mNetworkPoliciesSecondLock) {
+            updateNotificationsNL();
+            writePolicyAL();
+        }
     }
 
     @Override
@@ -2016,7 +2084,8 @@
         mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
         final boolean oldStatus;
         final boolean needFirewallRules;
-        synchronized (mRulesLock) {
+        int changed;
+        synchronized (mUidRulesFirstLock) {
             oldStatus = mRestrictBackgroundWhitelistUids.get(uid);
             if (oldStatus) {
                 if (LOGD) Slog.d(TAG, "uid " + uid + " is already whitelisted");
@@ -2033,12 +2102,14 @@
             }
             if (needFirewallRules) {
                 // Only update firewall rules if necessary...
-                updateRulesForDataUsageRestrictionsLocked(uid);
+                updateRulesForDataUsageRestrictionsUL(uid);
             }
             // ...but always persists the whitelist request.
-            writePolicyLocked();
+            synchronized (mNetworkPoliciesSecondLock) {
+                writePolicyAL();
+            }
+            changed = (mRestrictBackground && !oldStatus && needFirewallRules) ? 1 : 0;
         }
-        int changed = (mRestrictBackground && !oldStatus && needFirewallRules) ? 1 : 0;
         mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed,
                 Boolean.TRUE).sendToTarget();
     }
@@ -2047,8 +2118,8 @@
     public void removeRestrictBackgroundWhitelistedUid(int uid) {
         mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
         final boolean changed;
-        synchronized (mRulesLock) {
-            changed = removeRestrictBackgroundWhitelistedUidLocked(uid, false, true);
+        synchronized (mUidRulesFirstLock) {
+            changed = removeRestrictBackgroundWhitelistedUidUL(uid, false, true);
         }
         mHandler.obtainMessage(MSG_RESTRICT_BACKGROUND_WHITELIST_CHANGED, uid, changed ? 1 : 0,
                 Boolean.FALSE).sendToTarget();
@@ -2058,7 +2129,7 @@
      * Removes a uid from the restricted background whitelist, returning whether its current
      * {@link ConnectivityManager.RestrictBackgroundStatus} changed.
      */
-    private boolean removeRestrictBackgroundWhitelistedUidLocked(int uid, boolean uidDeleted,
+    private boolean removeRestrictBackgroundWhitelistedUidUL(int uid, boolean uidDeleted,
             boolean updateNow) {
         final boolean oldStatus = mRestrictBackgroundWhitelistUids.get(uid);
         if (!oldStatus && !uidDeleted) {
@@ -2078,11 +2149,13 @@
         }
         if (needFirewallRules) {
             // Only update firewall rules if necessary...
-            updateRulesForDataUsageRestrictionsLocked(uid, uidDeleted);
+            updateRulesForDataUsageRestrictionsUL(uid, uidDeleted);
         }
         if (updateNow) {
             // ...but always persists the whitelist request.
-            writePolicyLocked();
+            synchronized (mNetworkPoliciesSecondLock) {
+                writePolicyAL();
+            }
         }
         // Status only changes if Data Saver is turned on (otherwise it is DISABLED, even if the
         // app was whitelisted before).
@@ -2092,7 +2165,7 @@
     @Override
     public int[] getRestrictBackgroundWhitelistedUids() {
         mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             final int size = mRestrictBackgroundWhitelistUids.size();
             final int[] whitelist = new int[size];
             for (int i = 0; i < size; i++) {
@@ -2111,7 +2184,7 @@
         mContext.enforceCallingOrSelfPermission(ACCESS_NETWORK_STATE, TAG);
         final int uid = Binder.getCallingUid();
 
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             // Must clear identity because getUidPolicy() is restricted to system.
             final long token = Binder.clearCallingIdentity();
             final int policy;
@@ -2137,7 +2210,7 @@
     public boolean getRestrictBackground() {
         mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
 
-        synchronized (mRulesLock) {
+        synchronized (mUidRulesFirstLock) {
             return mRestrictBackground;
         }
     }
@@ -2145,25 +2218,30 @@
     @Override
     public void setDeviceIdleMode(boolean enabled) {
         mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
-
-        synchronized (mRulesLock) {
-            if (mDeviceIdleMode != enabled) {
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "setDeviceIdleMode");
+        try {
+            synchronized (mUidRulesFirstLock) {
+                if (mDeviceIdleMode == enabled) {
+                    return;
+                }
                 mDeviceIdleMode = enabled;
                 if (mSystemReady) {
                     // Device idle change means we need to rebuild rules for all
                     // known apps, so do a global refresh.
-                    updateRulesForGlobalChangeLocked(false);
-                }
-                if (enabled) {
-                    EventLogTags.writeDeviceIdleOnPhase("net");
-                } else {
-                    EventLogTags.writeDeviceIdleOffPhase("net");
+                    updateRulesForRestrictPowerUL();
                 }
             }
+            if (enabled) {
+                EventLogTags.writeDeviceIdleOnPhase("net");
+            } else {
+                EventLogTags.writeDeviceIdleOffPhase("net");
+            }
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
         }
     }
 
-    private NetworkPolicy findPolicyForNetworkLocked(NetworkIdentity ident) {
+    private NetworkPolicy findPolicyForNetworkNL(NetworkIdentity ident) {
         for (int i = mNetworkPolicy.size()-1; i >= 0; i--) {
             NetworkPolicy policy = mNetworkPolicy.valueAt(i);
             if (policy.template.matches(ident)) {
@@ -2191,8 +2269,8 @@
         final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);
 
         final NetworkPolicy policy;
-        synchronized (mRulesLock) {
-            policy = findPolicyForNetworkLocked(ident);
+        synchronized (mNetworkPoliciesSecondLock) {
+            policy = findPolicyForNetworkNL(ident);
         }
 
         if (policy == null || !policy.hasCycle()) {
@@ -2224,21 +2302,16 @@
 
         final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state);
 
-        // roaming networks are always considered metered
-        if (ident.getRoaming()) {
-            return true;
-        }
-
         final NetworkPolicy policy;
-        synchronized (mRulesLock) {
-            policy = findPolicyForNetworkLocked(ident);
+        synchronized (mNetworkPoliciesSecondLock) {
+            policy = findPolicyForNetworkNL(ident);
         }
 
         if (policy != null) {
             return policy.metered;
         } else {
             final int type = state.networkInfo.getType();
-            if (isNetworkTypeMobile(type) || type == TYPE_WIMAX) {
+            if ((isNetworkTypeMobile(type) && ident.getMetered()) || type == TYPE_WIMAX) {
                 return true;
             }
             return false;
@@ -2256,155 +2329,157 @@
             argSet.add(arg);
         }
 
-        synchronized (mRulesLock) {
-            if (argSet.contains("--unsnooze")) {
-                for (int i = mNetworkPolicy.size()-1; i >= 0; i--) {
-                    mNetworkPolicy.valueAt(i).clearSnooze();
+        synchronized (mUidRulesFirstLock) {
+            synchronized (mNetworkPoliciesSecondLock) {
+                if (argSet.contains("--unsnooze")) {
+                    for (int i = mNetworkPolicy.size()-1; i >= 0; i--) {
+                        mNetworkPolicy.valueAt(i).clearSnooze();
+                    }
+
+                    normalizePoliciesNL();
+                    updateNetworkEnabledNL();
+                    updateNetworkRulesNL();
+                    updateNotificationsNL();
+                    writePolicyAL();
+
+                    fout.println("Cleared snooze timestamps");
+                    return;
                 }
 
-                normalizePoliciesLocked();
-                updateNetworkEnabledLocked();
-                updateNetworkRulesLocked();
-                updateNotificationsLocked();
-                writePolicyLocked();
-
-                fout.println("Cleared snooze timestamps");
-                return;
-            }
-
-            fout.print("System ready: "); fout.println(mSystemReady);
-            fout.print("Restrict background: "); fout.println(mRestrictBackground);
-            fout.print("Restrict power: "); fout.println(mRestrictPower);
-            fout.print("Device idle: "); fout.println(mDeviceIdleMode);
-            fout.println("Network policies:");
-            fout.increaseIndent();
-            for (int i = 0; i < mNetworkPolicy.size(); i++) {
-                fout.println(mNetworkPolicy.valueAt(i).toString());
-            }
-            fout.decreaseIndent();
-
-            fout.print("Metered ifaces: "); fout.println(String.valueOf(mMeteredIfaces));
-
-            fout.println("Policy for UIDs:");
-            fout.increaseIndent();
-            int size = mUidPolicy.size();
-            for (int i = 0; i < size; i++) {
-                final int uid = mUidPolicy.keyAt(i);
-                final int policy = mUidPolicy.valueAt(i);
-                fout.print("UID=");
-                fout.print(uid);
-                fout.print(" policy=");
-                fout.print(DebugUtils.flagsToString(NetworkPolicyManager.class, "POLICY_", policy));
-                fout.println();
-            }
-            fout.decreaseIndent();
-
-            size = mPowerSaveWhitelistExceptIdleAppIds.size();
-            if (size > 0) {
-                fout.println("Power save whitelist (except idle) app ids:");
+                fout.print("System ready: "); fout.println(mSystemReady);
+                fout.print("Restrict background: "); fout.println(mRestrictBackground);
+                fout.print("Restrict power: "); fout.println(mRestrictPower);
+                fout.print("Device idle: "); fout.println(mDeviceIdleMode);
+                fout.println("Network policies:");
                 fout.increaseIndent();
+                for (int i = 0; i < mNetworkPolicy.size(); i++) {
+                    fout.println(mNetworkPolicy.valueAt(i).toString());
+                }
+                fout.decreaseIndent();
+
+                fout.print("Metered ifaces: "); fout.println(String.valueOf(mMeteredIfaces));
+
+                fout.println("Policy for UIDs:");
+                fout.increaseIndent();
+                int size = mUidPolicy.size();
                 for (int i = 0; i < size; i++) {
+                    final int uid = mUidPolicy.keyAt(i);
+                    final int policy = mUidPolicy.valueAt(i);
                     fout.print("UID=");
-                    fout.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i));
-                    fout.print(": ");
-                    fout.print(mPowerSaveWhitelistExceptIdleAppIds.valueAt(i));
+                    fout.print(uid);
+                    fout.print(" policy=");
+                    fout.print(DebugUtils.flagsToString(NetworkPolicyManager.class, "POLICY_", policy));
+                    fout.println();
+                }
+                fout.decreaseIndent();
+
+                size = mPowerSaveWhitelistExceptIdleAppIds.size();
+                if (size > 0) {
+                    fout.println("Power save whitelist (except idle) app ids:");
+                    fout.increaseIndent();
+                    for (int i = 0; i < size; i++) {
+                        fout.print("UID=");
+                        fout.print(mPowerSaveWhitelistExceptIdleAppIds.keyAt(i));
+                        fout.print(": ");
+                        fout.print(mPowerSaveWhitelistExceptIdleAppIds.valueAt(i));
+                        fout.println();
+                    }
+                    fout.decreaseIndent();
+                }
+
+                size = mPowerSaveWhitelistAppIds.size();
+                if (size > 0) {
+                    fout.println("Power save whitelist app ids:");
+                    fout.increaseIndent();
+                    for (int i = 0; i < size; i++) {
+                        fout.print("UID=");
+                        fout.print(mPowerSaveWhitelistAppIds.keyAt(i));
+                        fout.print(": ");
+                        fout.print(mPowerSaveWhitelistAppIds.valueAt(i));
+                        fout.println();
+                    }
+                    fout.decreaseIndent();
+                }
+
+                size = mRestrictBackgroundWhitelistUids.size();
+                if (size > 0) {
+                    fout.println("Restrict background whitelist uids:");
+                    fout.increaseIndent();
+                    for (int i = 0; i < size; i++) {
+                        fout.print("UID=");
+                        fout.print(mRestrictBackgroundWhitelistUids.keyAt(i));
+                        fout.println();
+                    }
+                    fout.decreaseIndent();
+                }
+
+                size = mDefaultRestrictBackgroundWhitelistUids.size();
+                if (size > 0) {
+                    fout.println("Default restrict background whitelist uids:");
+                    fout.increaseIndent();
+                    for (int i = 0; i < size; i++) {
+                        fout.print("UID=");
+                        fout.print(mDefaultRestrictBackgroundWhitelistUids.keyAt(i));
+                        fout.println();
+                    }
+                    fout.decreaseIndent();
+                }
+
+                size = mRestrictBackgroundWhitelistRevokedUids.size();
+                if (size > 0) {
+                    fout.println("Default restrict background whitelist uids revoked by users:");
+                    fout.increaseIndent();
+                    for (int i = 0; i < size; i++) {
+                        fout.print("UID=");
+                        fout.print(mRestrictBackgroundWhitelistRevokedUids.keyAt(i));
+                        fout.println();
+                    }
+                    fout.decreaseIndent();
+                }
+
+                final SparseBooleanArray knownUids = new SparseBooleanArray();
+                collectKeys(mUidState, knownUids);
+                collectKeys(mUidRules, knownUids);
+
+                fout.println("Status for all known UIDs:");
+                fout.increaseIndent();
+                size = knownUids.size();
+                for (int i = 0; i < size; i++) {
+                    final int uid = knownUids.keyAt(i);
+                    fout.print("UID=");
+                    fout.print(uid);
+
+                    final int state = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY);
+                    fout.print(" state=");
+                    fout.print(state);
+                    if (state <= ActivityManager.PROCESS_STATE_TOP) {
+                        fout.print(" (fg)");
+                    } else {
+                        fout.print(state <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
+                                ? " (fg svc)" : " (bg)");
+                    }
+
+                    final int uidRules = mUidRules.get(uid, RULE_NONE);
+                    fout.print(" rules=");
+                    fout.print(uidRulesToString(uidRules));
+                    fout.println();
+                }
+                fout.decreaseIndent();
+
+                fout.println("Status for just UIDs with rules:");
+                fout.increaseIndent();
+                size = mUidRules.size();
+                for (int i = 0; i < size; i++) {
+                    final int uid = mUidRules.keyAt(i);
+                    fout.print("UID=");
+                    fout.print(uid);
+                    final int uidRules = mUidRules.get(uid, RULE_NONE);
+                    fout.print(" rules=");
+                    fout.print(uidRulesToString(uidRules));
                     fout.println();
                 }
                 fout.decreaseIndent();
             }
-
-            size = mPowerSaveWhitelistAppIds.size();
-            if (size > 0) {
-                fout.println("Power save whitelist app ids:");
-                fout.increaseIndent();
-                for (int i = 0; i < size; i++) {
-                    fout.print("UID=");
-                    fout.print(mPowerSaveWhitelistAppIds.keyAt(i));
-                    fout.print(": ");
-                    fout.print(mPowerSaveWhitelistAppIds.valueAt(i));
-                    fout.println();
-                }
-                fout.decreaseIndent();
-            }
-
-            size = mRestrictBackgroundWhitelistUids.size();
-            if (size > 0) {
-                fout.println("Restrict background whitelist uids:");
-                fout.increaseIndent();
-                for (int i = 0; i < size; i++) {
-                    fout.print("UID=");
-                    fout.print(mRestrictBackgroundWhitelistUids.keyAt(i));
-                    fout.println();
-                }
-                fout.decreaseIndent();
-            }
-
-            size = mDefaultRestrictBackgroundWhitelistUids.size();
-            if (size > 0) {
-                fout.println("Default restrict background whitelist uids:");
-                fout.increaseIndent();
-                for (int i = 0; i < size; i++) {
-                    fout.print("UID=");
-                    fout.print(mDefaultRestrictBackgroundWhitelistUids.keyAt(i));
-                    fout.println();
-                }
-                fout.decreaseIndent();
-            }
-
-            size = mRestrictBackgroundWhitelistRevokedUids.size();
-            if (size > 0) {
-                fout.println("Default restrict background whitelist uids revoked by users:");
-                fout.increaseIndent();
-                for (int i = 0; i < size; i++) {
-                    fout.print("UID=");
-                    fout.print(mRestrictBackgroundWhitelistRevokedUids.keyAt(i));
-                    fout.println();
-                }
-                fout.decreaseIndent();
-            }
-
-            final SparseBooleanArray knownUids = new SparseBooleanArray();
-            collectKeys(mUidState, knownUids);
-            collectKeys(mUidRules, knownUids);
-
-            fout.println("Status for all known UIDs:");
-            fout.increaseIndent();
-            size = knownUids.size();
-            for (int i = 0; i < size; i++) {
-                final int uid = knownUids.keyAt(i);
-                fout.print("UID=");
-                fout.print(uid);
-
-                final int state = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY);
-                fout.print(" state=");
-                fout.print(state);
-                if (state <= ActivityManager.PROCESS_STATE_TOP) {
-                    fout.print(" (fg)");
-                } else {
-                    fout.print(state <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
-                            ? " (fg svc)" : " (bg)");
-                }
-
-                final int uidRules = mUidRules.get(uid, RULE_NONE);
-                fout.print(" rules=");
-                fout.print(uidRulesToString(uidRules));
-                fout.println();
-            }
-            fout.decreaseIndent();
-
-            fout.println("Status for just UIDs with rules:");
-            fout.increaseIndent();
-            size = mUidRules.size();
-            for (int i = 0; i < size; i++) {
-                final int uid = mUidRules.keyAt(i);
-                fout.print("UID=");
-                fout.print(uid);
-                final int uidRules = mUidRules.get(uid, RULE_NONE);
-                fout.print(" rules=");
-                fout.print(uidRulesToString(uidRules));
-                fout.println();
-            }
-            fout.decreaseIndent();
         }
     }
 
@@ -2419,74 +2494,79 @@
     public boolean isUidForeground(int uid) {
         mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG);
 
-        synchronized (mRulesLock) {
-            return isUidForegroundLocked(uid);
+        synchronized (mUidRulesFirstLock) {
+            return isUidForegroundUL(uid);
         }
     }
 
-    private boolean isUidForegroundLocked(int uid) {
-        return isUidStateForegroundLocked(
+    private boolean isUidForegroundUL(int uid) {
+        return isUidStateForegroundUL(
                 mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY));
     }
 
-    private boolean isUidForegroundOnRestrictBackgroundLocked(int uid) {
+    private boolean isUidForegroundOnRestrictBackgroundUL(int uid) {
         final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY);
-        return isProcStateAllowedWhileOnRestrictBackgroundLocked(procState);
+        return isProcStateAllowedWhileOnRestrictBackground(procState);
     }
 
-    private boolean isUidForegroundOnRestrictPowerLocked(int uid) {
+    private boolean isUidForegroundOnRestrictPowerUL(int uid) {
         final int procState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY);
         return isProcStateAllowedWhileIdleOrPowerSaveMode(procState);
     }
 
-    private boolean isUidStateForegroundLocked(int state) {
+    private boolean isUidStateForegroundUL(int state) {
         // only really in foreground when screen is also on
-        return mScreenOn && state <= ActivityManager.PROCESS_STATE_TOP;
+        return state <= ActivityManager.PROCESS_STATE_TOP;
     }
 
     /**
      * Process state of UID changed; if needed, will trigger
-     * {@link #updateRulesForDataUsageRestrictionsLocked(int)} and
-     * {@link #updateRulesForPowerRestrictionsLocked(int)}
+     * {@link #updateRulesForDataUsageRestrictionsUL(int)} and
+     * {@link #updateRulesForPowerRestrictionsUL(int)}
      */
-    private void updateUidStateLocked(int uid, int uidState) {
-        final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY);
-        if (oldUidState != uidState) {
-            // state changed, push updated rules
-            mUidState.put(uid, uidState);
-            updateRestrictBackgroundRulesOnUidStatusChangedLocked(uid, oldUidState, uidState);
-            if (isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState)
-                    != isProcStateAllowedWhileIdleOrPowerSaveMode(uidState) ) {
-                if (isUidIdle(uid)) {
-                    updateRuleForAppIdleLocked(uid);
+    private void updateUidStateUL(int uid, int uidState) {
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateUidStateUL");
+        try {
+            final int oldUidState = mUidState.get(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY);
+            if (oldUidState != uidState) {
+                // state changed, push updated rules
+                mUidState.put(uid, uidState);
+                updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState, uidState);
+                if (isProcStateAllowedWhileIdleOrPowerSaveMode(oldUidState)
+                        != isProcStateAllowedWhileIdleOrPowerSaveMode(uidState) ) {
+                    if (isUidIdle(uid)) {
+                        updateRuleForAppIdleUL(uid);
+                    }
+                    if (mDeviceIdleMode) {
+                        updateRuleForDeviceIdleUL(uid);
+                    }
+                    if (mRestrictPower) {
+                        updateRuleForRestrictPowerUL(uid);
+                    }
+                    updateRulesForPowerRestrictionsUL(uid);
                 }
-                if (mDeviceIdleMode) {
-                    updateRuleForDeviceIdleLocked(uid);
-                }
-                if (mRestrictPower) {
-                    updateRuleForRestrictPowerLocked(uid);
-                }
-                updateRulesForPowerRestrictionsLocked(uid);
+                updateNetworkStats(uid, isUidStateForegroundUL(uidState));
             }
-            updateNetworkStats(uid, isUidStateForegroundLocked(uidState));
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
         }
     }
 
-    private void removeUidStateLocked(int uid) {
+    private void removeUidStateUL(int uid) {
         final int index = mUidState.indexOfKey(uid);
         if (index >= 0) {
             final int oldUidState = mUidState.valueAt(index);
             mUidState.removeAt(index);
             if (oldUidState != ActivityManager.PROCESS_STATE_CACHED_EMPTY) {
-                updateRestrictBackgroundRulesOnUidStatusChangedLocked(uid, oldUidState,
+                updateRestrictBackgroundRulesOnUidStatusChangedUL(uid, oldUidState,
                         ActivityManager.PROCESS_STATE_CACHED_EMPTY);
                 if (mDeviceIdleMode) {
-                    updateRuleForDeviceIdleLocked(uid);
+                    updateRuleForDeviceIdleUL(uid);
                 }
                 if (mRestrictPower) {
-                    updateRuleForRestrictPowerLocked(uid);
+                    updateRuleForRestrictPowerUL(uid);
                 }
-                updateRulesForPowerRestrictionsLocked(uid);
+                updateRulesForPowerRestrictionsUL(uid);
                 updateNetworkStats(uid, false);
             }
         }
@@ -2501,39 +2581,14 @@
         }
     }
 
-    private void updateRestrictBackgroundRulesOnUidStatusChangedLocked(int uid, int oldUidState,
+    private void updateRestrictBackgroundRulesOnUidStatusChangedUL(int uid, int oldUidState,
             int newUidState) {
         final boolean oldForeground =
-                isProcStateAllowedWhileOnRestrictBackgroundLocked(oldUidState);
+                isProcStateAllowedWhileOnRestrictBackground(oldUidState);
         final boolean newForeground =
-                isProcStateAllowedWhileOnRestrictBackgroundLocked(newUidState);
+                isProcStateAllowedWhileOnRestrictBackground(newUidState);
         if (oldForeground != newForeground) {
-            updateRulesForDataUsageRestrictionsLocked(uid);
-        }
-    }
-
-    private void updateScreenOn() {
-        synchronized (mRulesLock) {
-            try {
-                mScreenOn = mPowerManager.isInteractive();
-            } catch (RemoteException e) {
-                // ignored; service lives in system_server
-            }
-            updateRulesForScreenLocked();
-        }
-    }
-
-    /**
-     * Update rules that might be changed by {@link #mScreenOn} value.
-     */
-    private void updateRulesForScreenLocked() {
-        // only update rules for anyone with foreground activities
-        final int size = mUidState.size();
-        for (int i = 0; i < size; i++) {
-            if (mUidState.valueAt(i) <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) {
-                final int uid = mUidState.keyAt(i);
-                updateRestrictionRulesForUidLocked(uid);
-            }
+            updateRulesForDataUsageRestrictionsUL(uid);
         }
     }
 
@@ -2541,31 +2596,41 @@
         return procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
     }
 
-    static boolean isProcStateAllowedWhileOnRestrictBackgroundLocked(int procState) {
+    static boolean isProcStateAllowedWhileOnRestrictBackground(int procState) {
         return procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
     }
 
-    void updateRulesForRestrictPowerLocked() {
-        updateRulesForWhitelistedPowerSaveLocked(mRestrictPower, FIREWALL_CHAIN_POWERSAVE,
-                mUidFirewallPowerSaveRules);
+    void updateRulesForPowerSaveUL() {
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForPowerSaveUL");
+        try {
+            updateRulesForWhitelistedPowerSaveUL(mRestrictPower, FIREWALL_CHAIN_POWERSAVE,
+                    mUidFirewallPowerSaveRules);
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
+        }
     }
 
-    void updateRuleForRestrictPowerLocked(int uid) {
-        updateRulesForWhitelistedPowerSaveLocked(uid, mRestrictPower, FIREWALL_CHAIN_POWERSAVE);
+    void updateRuleForRestrictPowerUL(int uid) {
+        updateRulesForWhitelistedPowerSaveUL(uid, mRestrictPower, FIREWALL_CHAIN_POWERSAVE);
     }
 
-    void updateRulesForDeviceIdleLocked() {
-        updateRulesForWhitelistedPowerSaveLocked(mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE,
-                mUidFirewallDozableRules);
+    void updateRulesForDeviceIdleUL() {
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForDeviceIdleUL");
+        try {
+            updateRulesForWhitelistedPowerSaveUL(mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE,
+                    mUidFirewallDozableRules);
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
+        }
     }
 
-    void updateRuleForDeviceIdleLocked(int uid) {
-        updateRulesForWhitelistedPowerSaveLocked(uid, mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE);
+    void updateRuleForDeviceIdleUL(int uid) {
+        updateRulesForWhitelistedPowerSaveUL(uid, mDeviceIdleMode, FIREWALL_CHAIN_DOZABLE);
     }
 
     // NOTE: since both fw_dozable and fw_powersave uses the same map
     // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method.
-    private void updateRulesForWhitelistedPowerSaveLocked(boolean enabled, int chain,
+    private void updateRulesForWhitelistedPowerSaveUL(boolean enabled, int chain,
             SparseIntArray rules) {
         if (enabled) {
             // Sync the whitelists before enabling the chain.  We don't care about the rules if
@@ -2593,26 +2658,22 @@
                     uidRules.put(mUidState.keyAt(i), FIREWALL_RULE_ALLOW);
                 }
             }
-            setUidFirewallRules(chain, uidRules);
+            setUidFirewallRulesAsync(chain, uidRules, CHAIN_TOGGLE_ENABLE);
+        } else {
+            setUidFirewallRulesAsync(chain, null, CHAIN_TOGGLE_DISABLE);
         }
-
-        enableFirewallChainLocked(chain, enabled);
     }
 
-    private void updateRulesForNonMeteredNetworksLocked() {
-
-    }
-
-    private boolean isWhitelistedBatterySaverLocked(int uid) {
+    private boolean isWhitelistedBatterySaverUL(int uid) {
         final int appId = UserHandle.getAppId(uid);
         return mPowerSaveTempWhitelistAppIds.get(appId) || mPowerSaveWhitelistAppIds.get(appId);
     }
 
     // NOTE: since both fw_dozable and fw_powersave uses the same map
     // (mPowerSaveTempWhitelistAppIds) for whitelisting, we can reuse their logic in this method.
-    private void updateRulesForWhitelistedPowerSaveLocked(int uid, boolean enabled, int chain) {
+    private void updateRulesForWhitelistedPowerSaveUL(int uid, boolean enabled, int chain) {
         if (enabled) {
-            if (isWhitelistedBatterySaverLocked(uid)
+            if (isWhitelistedBatterySaverUL(uid)
                     || isProcStateAllowedWhileIdleOrPowerSaveMode(mUidState.get(uid))) {
                 setUidFirewallRule(chain, uid, FIREWALL_RULE_ALLOW);
             } else {
@@ -2621,97 +2682,168 @@
         }
     }
 
-    void updateRulesForAppIdleLocked() {
-        final SparseIntArray uidRules = mUidFirewallStandbyRules;
-        uidRules.clear();
+    void updateRulesForAppIdleUL() {
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForAppIdleUL");
+        try {
+            final SparseIntArray uidRules = mUidFirewallStandbyRules;
+            uidRules.clear();
 
-        // Fully update the app idle firewall chain.
-        final List<UserInfo> users = mUserManager.getUsers();
-        for (int ui = users.size() - 1; ui >= 0; ui--) {
-            UserInfo user = users.get(ui);
-            int[] idleUids = mUsageStats.getIdleUidsForUser(user.id);
-            for (int uid : idleUids) {
-                if (!mPowerSaveTempWhitelistAppIds.get(UserHandle.getAppId(uid), false)) {
-                    // quick check: if this uid doesn't have INTERNET permission, it
-                    // doesn't have network access anyway, so it is a waste to mess
-                    // with it here.
-                    if (hasInternetPermissions(uid)) {
-                        uidRules.put(uid, FIREWALL_RULE_DENY);
+            // Fully update the app idle firewall chain.
+            final List<UserInfo> users = mUserManager.getUsers();
+            for (int ui = users.size() - 1; ui >= 0; ui--) {
+                UserInfo user = users.get(ui);
+                int[] idleUids = mUsageStats.getIdleUidsForUser(user.id);
+                for (int uid : idleUids) {
+                    if (!mPowerSaveTempWhitelistAppIds.get(UserHandle.getAppId(uid), false)) {
+                        // quick check: if this uid doesn't have INTERNET permission, it
+                        // doesn't have network access anyway, so it is a waste to mess
+                        // with it here.
+                        if (hasInternetPermissions(uid)) {
+                            uidRules.put(uid, FIREWALL_RULE_DENY);
+                        }
                     }
                 }
             }
-        }
 
-        setUidFirewallRules(FIREWALL_CHAIN_STANDBY, uidRules);
+            setUidFirewallRulesAsync(FIREWALL_CHAIN_STANDBY, uidRules, CHAIN_TOGGLE_NONE);
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
+        }
     }
 
-    void updateRuleForAppIdleLocked(int uid) {
+    void updateRuleForAppIdleUL(int uid) {
         if (!isUidValidForBlacklistRules(uid)) return;
 
         int appId = UserHandle.getAppId(uid);
         if (!mPowerSaveTempWhitelistAppIds.get(appId) && isUidIdle(uid)
-                && !isUidForegroundOnRestrictPowerLocked(uid)) {
+                && !isUidForegroundOnRestrictPowerUL(uid)) {
             setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DENY);
         } else {
             setUidFirewallRule(FIREWALL_CHAIN_STANDBY, uid, FIREWALL_RULE_DEFAULT);
         }
     }
 
-    void updateRulesForAppIdleParoleLocked() {
-        boolean enableChain = !mUsageStats.isAppIdleParoleOn();
-        enableFirewallChainLocked(FIREWALL_CHAIN_STANDBY, enableChain);
+    /**
+     * Toggle the firewall standby chain and inform listeners if the uid rules have effectively
+     * changed.
+     */
+    void updateRulesForAppIdleParoleUL() {
+        boolean paroled = mUsageStats.isAppIdleParoleOn();
+        boolean enableChain = !paroled;
+        enableFirewallChainUL(FIREWALL_CHAIN_STANDBY, enableChain);
+
+        int ruleCount = mUidFirewallStandbyRules.size();
+        for (int i = 0; i < ruleCount; i++) {
+            int uid = mUidFirewallStandbyRules.keyAt(i);
+            int oldRules = mUidRules.get(uid);
+            if (enableChain) {
+                // Chain wasn't enabled before and the other power-related
+                // chains are whitelists, so we can clear the
+                // MASK_ALL_NETWORKS part of the rules and re-inform listeners if
+                // the effective rules result in blocking network access.
+                oldRules &= MASK_METERED_NETWORKS;
+            } else {
+                // Skip if it had no restrictions to begin with
+                if ((oldRules & MASK_ALL_NETWORKS) == 0) continue;
+            }
+            updateRulesForPowerRestrictionsUL(uid, oldRules, paroled);
+        }
     }
 
     /**
      * Update rules that might be changed by {@link #mRestrictBackground},
      * {@link #mRestrictPower}, or {@link #mDeviceIdleMode} value.
      */
-    private void updateRulesForGlobalChangeLocked(boolean restrictedNetworksChanged) {
-        long start;
-        if (LOGD) start = System.currentTimeMillis();
+    private void updateRulesForGlobalChangeAL(boolean restrictedNetworksChanged) {
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForGlobalChangeAL");
+        try {
+            updateRulesForAppIdleUL();
+            updateRulesForRestrictPowerUL();
+            updateRulesForRestrictBackgroundUL();
 
-        updateRulesForDeviceIdleLocked();
-        updateRulesForAppIdleLocked();
-        updateRulesForRestrictPowerLocked();
-        updateRulesForRestrictBackgroundLocked();
-        setRestrictBackgroundLocked(mRestrictBackground);
-
-        // If the set of restricted networks may have changed, re-evaluate those.
-        if (restrictedNetworksChanged) {
-            normalizePoliciesLocked();
-            updateNetworkRulesLocked();
-        }
-        if (LOGD) {
-            final long delta = System.currentTimeMillis() - start;
-            Slog.d(TAG, "updateRulesForGlobalChangeLocked(" + restrictedNetworksChanged + ") took "
-                    + delta + "ms");
+            // If the set of restricted networks may have changed, re-evaluate those.
+            if (restrictedNetworksChanged) {
+                normalizePoliciesNL();
+                updateNetworkRulesNL();
+            }
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
         }
     }
 
-    private void updateRulesForRestrictBackgroundLocked() {
-        final PackageManager pm = mContext.getPackageManager();
+    // TODO: rename / document to make it clear these are global (not app-specific) rules
+    private void updateRulesForRestrictPowerUL() {
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL");
+        try {
+            updateRulesForDeviceIdleUL();
+            updateRulesForPowerSaveUL();
+            updateRulesForAllAppsUL(TYPE_RESTRICT_POWER);
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
+        }
+    }
 
-        // update rules for all installed applications
-        final List<UserInfo> users = mUserManager.getUsers();
-        final List<ApplicationInfo> apps = pm.getInstalledApplications(
-                PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_DISABLED_COMPONENTS
-                        | PackageManager.MATCH_DIRECT_BOOT_AWARE
-                        | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
+    private void updateRulesForRestrictBackgroundUL() {
+        Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictBackgroundUL");
+        try {
+            updateRulesForAllAppsUL(TYPE_RESTRICT_BACKGROUND);
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
+        }
+    }
 
-        final int usersSize = users.size();
-        final int appsSize = apps.size();
-        for (int i = 0; i < usersSize; i++) {
-            final UserInfo user = users.get(i);
-            for (int j = 0; j < appsSize; j++) {
-                final ApplicationInfo app = apps.get(j);
-                final int uid = UserHandle.getUid(user.id, app.uid);
-                updateRulesForDataUsageRestrictionsLocked(uid);
-                updateRulesForPowerRestrictionsLocked(uid);
+    private static final int TYPE_RESTRICT_BACKGROUND = 1;
+    private static final int TYPE_RESTRICT_POWER = 2;
+    @Retention(RetentionPolicy.SOURCE)
+    @IntDef(flag = false, value = {
+            TYPE_RESTRICT_BACKGROUND,
+            TYPE_RESTRICT_POWER,
+    })
+    public @interface RestrictType {
+    }
+
+    // TODO: refactor / consolidate all those updateXyz methods, there are way too many of them...
+    private void updateRulesForAllAppsUL(@RestrictType int type) {
+        if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) {
+            Trace.traceBegin(Trace.TRACE_TAG_NETWORK, "updateRulesForRestrictPowerUL-" + type);
+        }
+        try {
+            final PackageManager pm = mContext.getPackageManager();
+
+            // update rules for all installed applications
+            final List<UserInfo> users = mUserManager.getUsers();
+            final List<ApplicationInfo> apps = pm.getInstalledApplications(
+                    PackageManager.MATCH_UNINSTALLED_PACKAGES | PackageManager.MATCH_DISABLED_COMPONENTS
+                            | PackageManager.MATCH_DIRECT_BOOT_AWARE
+                            | PackageManager.MATCH_DIRECT_BOOT_UNAWARE);
+
+            final int usersSize = users.size();
+            final int appsSize = apps.size();
+            for (int i = 0; i < usersSize; i++) {
+                final UserInfo user = users.get(i);
+                for (int j = 0; j < appsSize; j++) {
+                    final ApplicationInfo app = apps.get(j);
+                    final int uid = UserHandle.getUid(user.id, app.uid);
+                    switch (type) {
+                        case TYPE_RESTRICT_BACKGROUND:
+                            updateRulesForDataUsageRestrictionsUL(uid);
+                            break;
+                        case TYPE_RESTRICT_POWER:
+                            updateRulesForPowerRestrictionsUL(uid);
+                            break;
+                        default:
+                            Slog.w(TAG, "Invalid type for updateRulesForAllApps: " + type);
+                    }
+                }
+            }
+        } finally {
+            if (Trace.isTagEnabled(Trace.TRACE_TAG_NETWORK)) {
+                Trace.traceEnd(Trace.TRACE_TAG_NETWORK);
             }
         }
     }
 
-    private void updateRulesForTempWhitelistChangeLocked() {
+    private void updateRulesForTempWhitelistChangeUL() {
         final List<UserInfo> users = mUserManager.getUsers();
         for (int i = 0; i < users.size(); i++) {
             final UserInfo user = users.get(i);
@@ -2719,11 +2851,11 @@
                 int appId = mPowerSaveTempWhitelistAppIds.keyAt(j);
                 int uid = UserHandle.getUid(user.id, appId);
                 // Update external firewall rules.
-                updateRuleForAppIdleLocked(uid);
-                updateRuleForDeviceIdleLocked(uid);
-                updateRuleForRestrictPowerLocked(uid);
+                updateRuleForAppIdleUL(uid);
+                updateRuleForDeviceIdleUL(uid);
+                updateRuleForRestrictPowerUL(uid);
                 // Update internal rules.
-                updateRulesForPowerRestrictionsLocked(uid);
+                updateRulesForPowerRestrictionsUL(uid);
             }
         }
     }
@@ -2787,17 +2919,17 @@
      *
      * <p>This method changes both the external firewall rules and the internal state.
      */
-    private void updateRestrictionRulesForUidLocked(int uid) {
+    private void updateRestrictionRulesForUidUL(int uid) {
         // Methods below only changes the firewall rules for the power-related modes.
-        updateRuleForDeviceIdleLocked(uid);
-        updateRuleForAppIdleLocked(uid);
-        updateRuleForRestrictPowerLocked(uid);
+        updateRuleForDeviceIdleUL(uid);
+        updateRuleForAppIdleUL(uid);
+        updateRuleForRestrictPowerUL(uid);
 
         // Update internal state for power-related modes.
-        updateRulesForPowerRestrictionsLocked(uid);
+        updateRulesForPowerRestrictionsUL(uid);
 
         // Update firewall and internal rules for Data Saver Mode.
-        updateRulesForDataUsageRestrictionsLocked(uid);
+        updateRulesForDataUsageRestrictionsUL(uid);
     }
 
     /**
@@ -2819,7 +2951,7 @@
      * {@link #setUidPolicy(int, int)} and {@link #addRestrictBackgroundWhitelistedUid(int)} /
      * {@link #removeRestrictBackgroundWhitelistedUid(int)} methods (for blacklist and whitelist
      * respectively): these methods set the proper internal state (blacklist / whitelist), then call
-     * this ({@link #updateRulesForDataUsageRestrictionsLocked(int)}) to propagate the rules to
+     * this ({@link #updateRulesForDataUsageRestrictionsUL(int)}) to propagate the rules to
      * {@link INetworkManagementService}, but this method should also be called in events (like
      * Data Saver Mode flips or UID state changes) that might affect the foreground app, since the
      * following rules should also be applied:
@@ -2839,15 +2971,15 @@
      * <p>The {@link #mUidRules} map is used to define the transtion of states of an UID.
      *
      */
-    private void updateRulesForDataUsageRestrictionsLocked(int uid) {
-        updateRulesForDataUsageRestrictionsLocked(uid, false);
+    private void updateRulesForDataUsageRestrictionsUL(int uid) {
+        updateRulesForDataUsageRestrictionsUL(uid, false);
     }
 
     /**
-     * Overloaded version of {@link #updateRulesForDataUsageRestrictionsLocked(int)} called when an
+     * Overloaded version of {@link #updateRulesForDataUsageRestrictionsUL(int)} called when an
      * app is removed - it ignores the UID validity check.
      */
-    private void updateRulesForDataUsageRestrictionsLocked(int uid, boolean uidDeleted) {
+    private void updateRulesForDataUsageRestrictionsUL(int uid, boolean uidDeleted) {
         if (!uidDeleted && !isUidValidForWhitelistRules(uid)) {
             if (LOGD) Slog.d(TAG, "no need to update restrict data rules for uid " + uid);
             return;
@@ -2855,7 +2987,7 @@
 
         final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE);
         final int oldUidRules = mUidRules.get(uid, RULE_NONE);
-        final boolean isForeground = isUidForegroundOnRestrictBackgroundLocked(uid);
+        final boolean isForeground = isUidForegroundOnRestrictBackgroundUL(uid);
 
         final boolean isBlacklisted = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
         final boolean isWhitelisted = mRestrictBackgroundWhitelistUids.get(uid);
@@ -2879,7 +3011,7 @@
         final int newUidRules = newRule | (oldUidRules & MASK_ALL_NETWORKS);
 
         if (LOGV) {
-            Log.v(TAG, "updateRuleForRestrictBackgroundLocked(" + uid + ")"
+            Log.v(TAG, "updateRuleForRestrictBackgroundUL(" + uid + ")"
                     + ": isForeground=" +isForeground
                     + ", isBlacklisted=" + isBlacklisted
                     + ", isWhitelisted=" + isWhitelisted
@@ -2895,12 +3027,8 @@
             mUidRules.put(uid, newUidRules);
         }
 
-        boolean changed = false;
-
         // Second step: apply bw changes based on change of state.
         if (newRule != oldRule) {
-            changed = true;
-
             if ((newRule & RULE_TEMPORARY_ALLOW_METERED) != 0) {
                 // Temporarily whitelist foreground app, removing from blacklist if necessary
                 // (since bw_penalty_box prevails over bw_happy_box).
@@ -2972,19 +3100,38 @@
      * <p>
      * <strong>NOTE: </strong>This method does not update the firewall rules on {@code netd}.
      */
-    private void updateRulesForPowerRestrictionsLocked(int uid) {
+    private void updateRulesForPowerRestrictionsUL(int uid) {
+        final int oldUidRules = mUidRules.get(uid, RULE_NONE);
+
+        final int newUidRules = updateRulesForPowerRestrictionsUL(uid, oldUidRules, false);
+
+        if (newUidRules == RULE_NONE) {
+            mUidRules.delete(uid);
+        } else {
+            mUidRules.put(uid, newUidRules);
+        }
+    }
+
+    /**
+     * Similar to above but ignores idle state if app standby is currently disabled by parole.
+     *
+     * @param uid the uid of the app to update rules for
+     * @param oldUidRules the current rules for the uid, in order to determine if there's a change
+     * @param paroled whether to ignore idle state of apps and only look at other restrictions.
+     *
+     * @return the new computed rules for the uid
+     */
+    private int updateRulesForPowerRestrictionsUL(int uid, int oldUidRules, boolean paroled) {
         if (!isUidValidForBlacklistRules(uid)) {
             if (LOGD) Slog.d(TAG, "no need to update restrict power rules for uid " + uid);
-            return;
+            return RULE_NONE;
         }
 
-        final boolean isIdle = isUidIdle(uid);
+        final boolean isIdle = !paroled && isUidIdle(uid);
         final boolean restrictMode = isIdle || mRestrictPower || mDeviceIdleMode;
-        final int uidPolicy = mUidPolicy.get(uid, POLICY_NONE);
-        final int oldUidRules = mUidRules.get(uid, RULE_NONE);
-        final boolean isForeground = isUidForegroundOnRestrictPowerLocked(uid);
+        final boolean isForeground = isUidForegroundOnRestrictPowerUL(uid);
 
-        final boolean isWhitelisted = isWhitelistedBatterySaverLocked(uid);
+        final boolean isWhitelisted = isWhitelistedBatterySaverUL(uid);
         final int oldRule = oldUidRules & MASK_ALL_NETWORKS;
         int newRule = RULE_NONE;
 
@@ -3003,7 +3150,7 @@
         final int newUidRules = (oldUidRules & MASK_METERED_NETWORKS) | newRule;
 
         if (LOGV) {
-            Log.v(TAG, "updateRulesForNonMeteredNetworksLocked(" + uid + ")"
+            Log.v(TAG, "updateRulesForPowerRestrictionsUL(" + uid + ")"
                     + ", isIdle: " + isIdle
                     + ", mRestrictPower: " + mRestrictPower
                     + ", mDeviceIdleMode: " + mDeviceIdleMode
@@ -3015,12 +3162,6 @@
                     + ", oldUidRules=" + uidRulesToString(oldUidRules));
         }
 
-        if (newUidRules == RULE_NONE) {
-            mUidRules.delete(uid);
-        } else {
-            mUidRules.put(uid, newUidRules);
-        }
-
         // Second step: notify listeners if state changed.
         if (newRule != oldRule) {
             if (newRule == RULE_NONE || (newRule & RULE_ALLOW_ALL) != 0) {
@@ -3037,6 +3178,8 @@
             }
             mHandler.obtainMessage(MSG_RULES_CHANGED, uid, newUidRules).sendToTarget();
         }
+
+        return newUidRules;
     }
 
     private class AppIdleStateChangeListener
@@ -3048,9 +3191,9 @@
                 final int uid = mContext.getPackageManager().getPackageUidAsUser(packageName,
                         PackageManager.MATCH_UNINSTALLED_PACKAGES, userId);
                 if (LOGV) Log.v(TAG, "onAppIdleStateChanged(): uid=" + uid + ", idle=" + idle);
-                synchronized (mRulesLock) {
-                    updateRuleForAppIdleLocked(uid);
-                    updateRulesForPowerRestrictionsLocked(uid);
+                synchronized (mUidRulesFirstLock) {
+                    updateRuleForAppIdleUL(uid);
+                    updateRulesForPowerRestrictionsUL(uid);
                 }
             } catch (NameNotFoundException nnfe) {
             }
@@ -3058,8 +3201,8 @@
 
         @Override
         public void onParoleStateChanged(boolean isParoleOn) {
-            synchronized (mRulesLock) {
-                updateRulesForAppIdleParoleLocked();
+            synchronized (mUidRulesFirstLock) {
+                updateRulesForAppIdleParoleUL();
             }
         }
     }
@@ -3144,7 +3287,7 @@
                     final String iface = (String) msg.obj;
 
                     maybeRefreshTrustedTime();
-                    synchronized (mRulesLock) {
+                    synchronized (mNetworkPoliciesSecondLock) {
                         if (mMeteredIfaces.contains(iface)) {
                             try {
                                 // force stats update to make sure we have
@@ -3154,8 +3297,8 @@
                                 // ignored; service lives in system_server
                             }
 
-                            updateNetworkEnabledLocked();
-                            updateNotificationsLocked();
+                            updateNetworkEnabledNL();
+                            updateNotificationsNL();
                         }
                     }
                     return true;
@@ -3245,10 +3388,6 @@
                     }
                     return true;
                 }
-                case MSG_SCREEN_ON_CHANGED: {
-                    updateScreenOn();
-                    return true;
-                }
                 case MSG_UPDATE_INTERFACE_QUOTA: {
                     removeInterfaceQuota((String) msg.obj);
                     // int params need to be stitched back into a long
@@ -3260,6 +3399,18 @@
                     removeInterfaceQuota((String) msg.obj);
                     return true;
                 }
+                case MSG_SET_FIREWALL_RULES: {
+                    final int chain = msg.arg1;
+                    final int toggle = msg.arg2;
+                    final SparseIntArray uidRules = (SparseIntArray) msg.obj;
+                    if (uidRules != null) {
+                        setUidFirewallRules(chain, uidRules);
+                    }
+                    if (toggle != CHAIN_TOGGLE_NONE) {
+                        enableFirewallChainUL(chain, toggle == CHAIN_TOGGLE_ENABLE);
+                    }
+                    return true;
+                }
                 default: {
                     return false;
                 }
@@ -3309,6 +3460,31 @@
         }
     }
 
+    private static final int CHAIN_TOGGLE_NONE = 0;
+    private static final int CHAIN_TOGGLE_ENABLE = 1;
+    private static final int CHAIN_TOGGLE_DISABLE = 2;
+    @Retention(RetentionPolicy.SOURCE)
+    @IntDef(flag = false, value = {
+            CHAIN_TOGGLE_NONE,
+            CHAIN_TOGGLE_ENABLE,
+            CHAIN_TOGGLE_DISABLE
+    })
+    public @interface ChainToggleType {
+    }
+
+    /**
+     * Calls {@link #setUidFirewallRules(int, SparseIntArray)} and
+     * {@link #enableFirewallChainUL(int, boolean)} asynchronously.
+     *
+     * @param chain firewall chain.
+     * @param uidRules new UID rules; if {@code null}, only toggles chain state.
+     * @param toggle whether the chain should be enabled, disabled, or not changed.
+     */
+    private void setUidFirewallRulesAsync(int chain, @Nullable SparseIntArray uidRules,
+            @ChainToggleType int toggle) {
+        mHandler.obtainMessage(MSG_SET_FIREWALL_RULES, chain, toggle, uidRules).sendToTarget();
+    }
+
     /**
      * Set uid rules on a particular firewall chain. This is going to synchronize the rules given
      * here to netd.  It will clean up dead rules and make sure the target chain only contains rules
@@ -3355,7 +3531,7 @@
     /**
      * Add or remove a uid to the firewall blacklist for all network ifaces.
      */
-    private void enableFirewallChainLocked(int chain, boolean enable) {
+    private void enableFirewallChainUL(int chain, boolean enable) {
         if (mFirewallChainStates.indexOfKey(chain) >= 0 &&
                 mFirewallChainStates.get(chain) == enable) {
             // All is the same, nothing to do.
@@ -3418,19 +3594,19 @@
         return intent;
     }
 
-    private static Intent buildNetworkOverLimitIntent(NetworkTemplate template) {
+    private static Intent buildNetworkOverLimitIntent(Resources res, NetworkTemplate template) {
         final Intent intent = new Intent();
-        intent.setComponent(new ComponentName(
-                "com.android.systemui", "com.android.systemui.net.NetworkOverLimitActivity"));
+        intent.setComponent(ComponentName.unflattenFromString(
+                res.getString(R.string.config_networkOverLimitComponent)));
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         intent.putExtra(EXTRA_NETWORK_TEMPLATE, template);
         return intent;
     }
 
-    private static Intent buildViewDataUsageIntent(NetworkTemplate template) {
+    private static Intent buildViewDataUsageIntent(Resources res, NetworkTemplate template) {
         final Intent intent = new Intent();
-        intent.setComponent(new ComponentName(
-                "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
+        intent.setComponent(ComponentName.unflattenFromString(
+                res.getString(R.string.config_dataUsageSummaryComponent)));
         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         intent.putExtra(EXTRA_NETWORK_TEMPLATE, template);
         return intent;
@@ -3479,27 +3655,17 @@
         }
     }
 
-    private class MyPackageMonitor extends PackageMonitor {
-
-        @Override
-        public void onPackageRemoved(String packageName, int uid) {
-            if (LOGV) Slog.v(TAG, "onPackageRemoved: " + packageName + " ->" + uid);
-            synchronized (mRulesLock) {
-                removeRestrictBackgroundWhitelistedUidLocked(uid, true, true);
-                updateRestrictionRulesForUidLocked(uid);
-            }
-        }
-    }
-
     private class NetworkPolicyManagerInternalImpl extends NetworkPolicyManagerInternal {
 
         @Override
         public void resetUserState(int userId) {
-            synchronized (mRulesLock) {
-                boolean changed = removeUserStateLocked(userId, false);
-                changed = addDefaultRestrictBackgroundWhitelistUidsLocked(userId) || changed;
+            synchronized (mUidRulesFirstLock) {
+                boolean changed = removeUserStateUL(userId, false);
+                changed = addDefaultRestrictBackgroundWhitelistUidsUL(userId) || changed;
                 if (changed) {
-                    writePolicyLocked();
+                    synchronized (mNetworkPoliciesSecondLock) {
+                        writePolicyAL();
+                    }
                 }
             }
         }
diff --git a/services/core/java/com/android/server/net/NetworkStatsService.java b/services/core/java/com/android/server/net/NetworkStatsService.java
index 23c111e..4658c046 100644
--- a/services/core/java/com/android/server/net/NetworkStatsService.java
+++ b/services/core/java/com/android/server/net/NetworkStatsService.java
@@ -31,6 +31,7 @@
 import static android.net.NetworkStats.SET_ALL;
 import static android.net.NetworkStats.SET_DEFAULT;
 import static android.net.NetworkStats.SET_FOREGROUND;
+import static android.net.NetworkStats.TAG_ALL;
 import static android.net.NetworkStats.TAG_NONE;
 import static android.net.NetworkStats.UID_ALL;
 import static android.net.NetworkTemplate.buildTemplateMobileWildcard;
@@ -79,6 +80,7 @@
 import android.net.INetworkStatsService;
 import android.net.INetworkStatsSession;
 import android.net.LinkProperties;
+import android.net.NetworkCapabilities;
 import android.net.NetworkIdentity;
 import android.net.NetworkInfo;
 import android.net.NetworkState;
@@ -179,6 +181,11 @@
     private static final String PREFIX_UID_TAG = "uid_tag";
 
     /**
+     * Virtual network interface for video telephony. This is for VT data usage counting purpose.
+     */
+    public static final String VT_INTERFACE = "vt_data0";
+
+    /**
      * Settings that can be changed externally.
      */
     public interface NetworkStatsSettings {
@@ -967,6 +974,23 @@
                 if (baseIface != null) {
                     findOrCreateNetworkIdentitySet(mActiveIfaces, baseIface).add(ident);
                     findOrCreateNetworkIdentitySet(mActiveUidIfaces, baseIface).add(ident);
+
+                    // Build a separate virtual interface for VT (Video Telephony) data usage.
+                    // Only do this when IMS is not metered, but VT is metered.
+                    // If IMS is metered, then the IMS network usage has already included VT usage.
+                    // VT is considered always metered in framework's layer. If VT is not metered
+                    // per carrier's policy, modem will report 0 usage for VT calls.
+                    if (state.networkCapabilities.hasCapability(
+                            NetworkCapabilities.NET_CAPABILITY_IMS) && !ident.getMetered()) {
+
+                        // Copy the identify from IMS one but mark it as metered.
+                        NetworkIdentity vtIdent = new NetworkIdentity(ident.getType(),
+                                ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(),
+                                ident.getRoaming(), true);
+                        findOrCreateNetworkIdentitySet(mActiveIfaces, VT_INTERFACE).add(vtIdent);
+                        findOrCreateNetworkIdentitySet(mActiveUidIfaces, VT_INTERFACE).add(vtIdent);
+                    }
+
                     if (isMobile) {
                         mobileIfaces.add(baseIface);
                     }
@@ -1004,9 +1028,9 @@
 
     private void recordSnapshotLocked(long currentTime) throws RemoteException {
         // snapshot and record current counters; read UID stats first to
-        // avoid overcounting dev stats.
+        // avoid over counting dev stats.
         final NetworkStats uidSnapshot = getNetworkStatsUidDetail();
-        final NetworkStats xtSnapshot = mNetworkManager.getNetworkStatsSummaryXt();
+        final NetworkStats xtSnapshot = getNetworkStatsXtAndVt();
         final NetworkStats devSnapshot = mNetworkManager.getNetworkStatsSummaryDev();
 
 
@@ -1312,6 +1336,42 @@
     }
 
     /**
+     * Return snapshot of current XT plus VT statistics.
+     */
+    private NetworkStats getNetworkStatsXtAndVt() throws RemoteException {
+        final NetworkStats xtSnapshot = mNetworkManager.getNetworkStatsSummaryXt();
+
+        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(
+                Context.TELEPHONY_SERVICE);
+
+        long usage = tm.getVtDataUsage();
+
+        if (LOGV) Slog.d(TAG, "VT call data usage = " + usage);
+
+        final NetworkStats vtSnapshot = new NetworkStats(SystemClock.elapsedRealtime(), 1);
+
+        final NetworkStats.Entry entry = new NetworkStats.Entry();
+        entry.iface = VT_INTERFACE;
+        entry.uid = -1;
+        entry.set = TAG_ALL;
+        entry.tag = TAG_NONE;
+
+        // Since modem only tell us the total usage instead of each usage for RX and TX,
+        // we need to split it up (though it might not quite accurate). At
+        // least we can make sure the data usage report to the user will still be accurate.
+        entry.rxBytes = usage / 2;
+        entry.rxPackets = 0;
+        entry.txBytes = usage - entry.rxBytes;
+        entry.txPackets = 0;
+        vtSnapshot.combineValues(entry);
+
+        // Merge VT int XT
+        xtSnapshot.combineAllValues(vtSnapshot);
+
+        return xtSnapshot;
+    }
+
+    /**
      * Return snapshot of current tethering statistics. Will return empty
      * {@link NetworkStats} if any problems are encountered.
      */
diff --git a/services/core/java/com/android/server/notification/ConditionProviders.java b/services/core/java/com/android/server/notification/ConditionProviders.java
index 62fe70c..2fab288 100644
--- a/services/core/java/com/android/server/notification/ConditionProviders.java
+++ b/services/core/java/com/android/server/notification/ConditionProviders.java
@@ -17,6 +17,8 @@
 package com.android.server.notification;
 
 import android.annotation.NonNull;
+import android.app.INotificationManager;
+import android.app.NotificationManager;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -161,6 +163,25 @@
         }
     }
 
+    @Override
+    public void onPackagesChanged(boolean removingPackage, String[] pkgList) {
+        if (removingPackage) {
+            INotificationManager inm = NotificationManager.getService();
+
+            if (pkgList != null && (pkgList.length > 0)) {
+                for (String pkgName : pkgList) {
+                    try {
+                        inm.removeAutomaticZenRules(pkgName);
+                        inm.setNotificationPolicyAccessGranted(pkgName, false);
+                    } catch (Exception e) {
+                        Slog.e(TAG, "Failed to clean up rules for " + pkgName, e);
+                    }
+                }
+            }
+        }
+        super.onPackagesChanged(removingPackage, pkgList);
+    }
+
     public ManagedServiceInfo checkServiceToken(IConditionProvider provider) {
         synchronized(mMutex) {
             return checkServiceTokenLocked(provider);
diff --git a/services/core/java/com/android/server/notification/ManagedServices.java b/services/core/java/com/android/server/notification/ManagedServices.java
index dc85dd7..14e2ba3 100644
--- a/services/core/java/com/android/server/notification/ManagedServices.java
+++ b/services/core/java/com/android/server/notification/ManagedServices.java
@@ -217,8 +217,8 @@
         return mEnabledServicesPackageNames.contains(pkg);
     }
 
-    public void onPackagesChanged(boolean queryReplace, String[] pkgList) {
-        if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace
+    public void onPackagesChanged(boolean removingPackage, String[] pkgList) {
+        if (DEBUG) Slog.d(TAG, "onPackagesChanged removingPackage=" + removingPackage
                 + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList))
                 + " mEnabledServicesPackageNames=" + mEnabledServicesPackageNames);
         boolean anyServicesInvolved = false;
@@ -234,7 +234,7 @@
 
         if (anyServicesInvolved) {
             // if we're not replacing a package, clean up orphaned bits
-            if (!queryReplace) {
+            if (removingPackage) {
                 updateSettingsAccordingToInstalledServices();
                 rebuildRestoredPackages();
             }
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index 8910b42..d31c7b5 100644
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -58,7 +58,6 @@
 import android.app.NotificationManager;
 import android.app.NotificationManager.Policy;
 import android.app.PendingIntent;
-import android.app.RemoteInput;
 import android.app.StatusBarManager;
 import android.app.backup.BackupManager;
 import android.app.usage.UsageEvents;
@@ -93,7 +92,6 @@
 import android.os.IInterface;
 import android.os.Looper;
 import android.os.Message;
-import android.os.Parcelable;
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.SystemClock;
@@ -122,6 +120,8 @@
 import android.util.Slog;
 import android.util.SparseArray;
 import android.util.Xml;
+import android.view.WindowManager;
+import android.view.WindowManagerInternal;
 import android.view.accessibility.AccessibilityEvent;
 import android.view.accessibility.AccessibilityManager;
 import android.widget.Toast;
@@ -138,8 +138,8 @@
 import com.android.server.lights.Light;
 import com.android.server.lights.LightsManager;
 import com.android.server.notification.ManagedServices.ManagedServiceInfo;
+import com.android.server.policy.PhoneWindowManager;
 import com.android.server.statusbar.StatusBarManagerInternal;
-import com.android.server.vr.VrManagerInternal;
 import com.android.server.notification.ManagedServices.UserProfiles;
 
 import libcore.io.IoUtils;
@@ -180,7 +180,7 @@
             = SystemProperties.getBoolean("debug.child_notifs", true);
 
     static final int MAX_PACKAGE_NOTIFICATIONS = 50;
-    static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 50f;
+    static final float DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE = 10f;
 
     // message codes
     static final int MESSAGE_TIMEOUT = 2;
@@ -193,7 +193,7 @@
     private static final int MESSAGE_RECONSIDER_RANKING = 1000;
     private static final int MESSAGE_RANKING_SORT = 1001;
 
-    static final int LONG_DELAY = 3500; // 3.5 seconds
+    static final int LONG_DELAY = PhoneWindowManager.TOAST_WINDOW_TIMEOUT;
     static final int SHORT_DELAY = 2000; // 2 seconds
 
     static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
@@ -231,7 +231,7 @@
     AudioManagerInternal mAudioManagerInternal;
     @Nullable StatusBarManagerInternal mStatusBar;
     Vibrator mVibrator;
-    private VrManagerInternal mVrManagerInternal;
+    private WindowManagerInternal mWindowManagerInternal;
 
     final IBinder mForegroundToken = new Binder();
     private Handler mHandler;
@@ -305,6 +305,7 @@
     private RankingHandler mRankingHandler;
     private long mLastOverRateLogTime;
     private float mMaxPackageEnqueueRate = DEFAULT_MAX_NOTIFICATION_ENQUEUE_RATE;
+    private String mSystemNotificationSound;
 
     private static class Archive {
         final int mBufferSize;
@@ -451,13 +452,15 @@
         final String pkg;
         final ITransientNotification callback;
         int duration;
+        Binder token;
 
-        ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
-        {
+        ToastRecord(int pid, String pkg, ITransientNotification callback, int duration,
+                    Binder token) {
             this.pid = pid;
             this.pkg = pkg;
             this.callback = callback;
             this.duration = duration;
+            this.token = token;
         }
 
         void update(int duration) {
@@ -695,9 +698,9 @@
                 int changeUserId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
                         UserHandle.USER_ALL);
                 String pkgList[] = null;
-                boolean queryReplace = queryRemove &&
-                        intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
-                if (DBG) Slog.i(TAG, "action=" + action + " queryReplace=" + queryReplace);
+                boolean removingPackage = queryRemove &&
+                        !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
+                if (DBG) Slog.i(TAG, "action=" + action + " removing=" + removingPackage);
                 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
                     pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
                 } else if (action.equals(Intent.ACTION_PACKAGES_SUSPENDED)) {
@@ -746,10 +749,10 @@
                         }
                     }
                 }
-                mListeners.onPackagesChanged(queryReplace, pkgList);
-                mRankerServices.onPackagesChanged(queryReplace, pkgList);
-                mConditionProviders.onPackagesChanged(queryReplace, pkgList);
-                mRankingHelper.onPackagesChanged(queryReplace, pkgList);
+                mListeners.onPackagesChanged(removingPackage, pkgList);
+                mRankerServices.onPackagesChanged(removingPackage, pkgList);
+                mConditionProviders.onPackagesChanged(removingPackage, pkgList);
+                mRankingHelper.onPackagesChanged(removingPackage, pkgList);
             }
         }
     };
@@ -817,6 +820,8 @@
     private final class SettingsObserver extends ContentObserver {
         private final Uri NOTIFICATION_LIGHT_PULSE_URI
                 = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
+        private final Uri NOTIFICATION_SOUND_URI
+                = Settings.System.getUriFor(Settings.System.NOTIFICATION_SOUND);
         private final Uri NOTIFICATION_RATE_LIMIT_URI
                 = Settings.Global.getUriFor(Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE);
 
@@ -828,6 +833,8 @@
             ContentResolver resolver = getContext().getContentResolver();
             resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
                     false, this, UserHandle.USER_ALL);
+            resolver.registerContentObserver(NOTIFICATION_SOUND_URI,
+                    false, this, UserHandle.USER_ALL);
             resolver.registerContentObserver(NOTIFICATION_RATE_LIMIT_URI,
                     false, this, UserHandle.USER_ALL);
             update(null);
@@ -851,6 +858,10 @@
                 mMaxPackageEnqueueRate = Settings.Global.getFloat(resolver,
                             Settings.Global.MAX_NOTIFICATION_ENQUEUE_RATE, mMaxPackageEnqueueRate);
             }
+            if (uri == null || NOTIFICATION_SOUND_URI.equals(uri)) {
+                mSystemNotificationSound = Settings.System.getString(resolver,
+                        Settings.System.NOTIFICATION_SOUND);
+            }
         }
     }
 
@@ -903,6 +914,11 @@
         mHandler = handler;
     }
 
+    @VisibleForTesting
+    void setSystemNotificationSound(String systemNotificationSound) {
+        mSystemNotificationSound = systemNotificationSound;
+    }
+
     @Override
     public void onStart() {
         Resources resources = getContext().getResources();
@@ -1110,7 +1126,7 @@
             // Grab our optional AudioService
             mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
             mAudioManagerInternal = getLocalService(AudioManagerInternal.class);
-            mVrManagerInternal = getLocalService(VrManagerInternal.class);
+            mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class);
             mZenModeHelper.onSystemReady();
         } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) {
             // This observer will force an update when observe is called, causing us to
@@ -1311,10 +1327,13 @@
                             }
                         }
 
-                        record = new ToastRecord(callingPid, pkg, callback, duration);
+                        Binder token = new Binder();
+                        mWindowManagerInternal.addWindowToken(token,
+                                WindowManager.LayoutParams.TYPE_TOAST);
+                        record = new ToastRecord(callingPid, pkg, callback, duration, token);
                         mToastQueue.add(record);
                         index = mToastQueue.size() - 1;
-                        keepProcessAliveLocked(callingPid);
+                        keepProcessAliveIfNeededLocked(callingPid);
                     }
                     // If it's at index 0, it's the current toast.  It doesn't matter if it's
                     // new or just been updated.  Call back and tell it to show itself.
@@ -1837,7 +1856,7 @@
             enforceSystemOrSystemUIOrVolume("INotificationManager.setZenMode");
             final long identity = Binder.clearCallingIdentity();
             try {
-                mZenModeHelper.setManualZenMode(mode, conditionId, reason);
+                mZenModeHelper.setManualZenMode(mode, conditionId, null, reason);
             } finally {
                 Binder.restoreCallingIdentity(identity);
             }
@@ -1914,7 +1933,7 @@
             if (zen == -1) throw new IllegalArgumentException("Invalid filter: " + filter);
             final long identity = Binder.clearCallingIdentity();
             try {
-                mZenModeHelper.setManualZenMode(zen, null, "setInterruptionFilter");
+                mZenModeHelper.setManualZenMode(zen, null, pkg, "setInterruptionFilter");
             } finally {
                 Binder.restoreCallingIdentity(identity);
             }
@@ -2272,6 +2291,7 @@
                                         .setFlag(Notification.FLAG_AUTOGROUP_SUMMARY, true)
                                         .setFlag(Notification.FLAG_GROUP_SUMMARY, true)
                                         .setColor(adjustedSbn.getNotification().color)
+                                        .setLocalOnly(true)
                                         .build();
                         summaryNotification.extras.putAll(extras);
                         Intent appIntent = getContext().getPackageManager()
@@ -2523,20 +2543,32 @@
 
         mUsageStats.registerEnqueuedByApp(pkg);
 
+
+        if (pkg == null || notification == null) {
+            throw new IllegalArgumentException("null not allowed: pkg=" + pkg
+                    + " id=" + id + " notification=" + notification);
+        }
+        final StatusBarNotification n = new StatusBarNotification(
+                pkg, opPkg, id, tag, callingUid, callingPid, 0, notification,
+                user);
+
         // Limit the number of notifications that any given package except the android
         // package or a registered listener can enqueue.  Prevents DOS attacks and deals with leaks.
         if (!isSystemNotification && !isNotificationFromListener) {
             synchronized (mNotificationList) {
-                final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg);
-                if (appEnqueueRate > mMaxPackageEnqueueRate) {
-                    mUsageStats.registerOverRateQuota(pkg);
-                    final long now = SystemClock.elapsedRealtime();
-                    if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) {
-                        Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate
-                                + ". Shedding events. package=" + pkg);
-                        mLastOverRateLogTime = now;
+                if(mNotificationsByKey.get(n.getKey()) != null) {
+                    // this is an update, rate limit updates only
+                    final float appEnqueueRate = mUsageStats.getAppEnqueueRate(pkg);
+                    if (appEnqueueRate > mMaxPackageEnqueueRate) {
+                        mUsageStats.registerOverRateQuota(pkg);
+                        final long now = SystemClock.elapsedRealtime();
+                        if ((now - mLastOverRateLogTime) > MIN_PACKAGE_OVERRATE_LOG_INTERVAL) {
+                            Slog.e(TAG, "Package enqueue rate is " + appEnqueueRate
+                                    + ". Shedding events. package=" + pkg);
+                            mLastOverRateLogTime = now;
+                        }
+                        return;
                     }
-                    return;
                 }
 
                 int count = 0;
@@ -2559,11 +2591,6 @@
             }
         }
 
-        if (pkg == null || notification == null) {
-            throw new IllegalArgumentException("null not allowed: pkg=" + pkg
-                    + " id=" + id + " notification=" + notification);
-        }
-
         // Whitelist pending intents.
         if (notification.allPendingIntents != null) {
             final int intentCount = notification.allPendingIntents.size();
@@ -2586,9 +2613,6 @@
                 Notification.PRIORITY_MAX);
 
         // setup local book-keeping
-        final StatusBarNotification n = new StatusBarNotification(
-                pkg, opPkg, id, tag, callingUid, callingPid, 0, notification,
-                user);
         final NotificationRecord r = new NotificationRecord(getContext(), n);
         mHandler.post(new EnqueueNotificationRunnable(userId, r));
 
@@ -2824,9 +2848,7 @@
                 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
 
                 // check to see if the default notification sound is silent
-                ContentResolver resolver = getContext().getContentResolver();
-                hasValidSound = Settings.System.getString(resolver,
-                       Settings.System.NOTIFICATION_SOUND) != null;
+                hasValidSound = mSystemNotificationSound != null;
             } else if (notification.sound != null) {
                 soundUri = notification.sound;
                 hasValidSound = (soundUri != null);
@@ -2974,7 +2996,7 @@
         while (record != null) {
             if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
             try {
-                record.callback.show();
+                record.callback.show(record.token);
                 scheduleTimeoutLocked(record);
                 return;
             } catch (RemoteException e) {
@@ -2985,7 +3007,7 @@
                 if (index >= 0) {
                     mToastQueue.remove(index);
                 }
-                keepProcessAliveLocked(record.pid);
+                keepProcessAliveIfNeededLocked(record.pid);
                 if (mToastQueue.size() > 0) {
                     record = mToastQueue.get(0);
                 } else {
@@ -3005,8 +3027,11 @@
             // don't worry about this, we're about to remove it from
             // the list anyway
         }
-        mToastQueue.remove(index);
-        keepProcessAliveLocked(record.pid);
+
+        ToastRecord lastToast = mToastQueue.remove(index);
+        mWindowManagerInternal.removeWindowToken(lastToast.token, true);
+
+        keepProcessAliveIfNeededLocked(record.pid);
         if (mToastQueue.size() > 0) {
             // Show the next one. If the callback fails, this will remove
             // it from the list, so don't assume that the list hasn't changed
@@ -3050,7 +3075,7 @@
     }
 
     // lock on mToastQueue
-    void keepProcessAliveLocked(int pid)
+    void keepProcessAliveIfNeededLocked(int pid)
     {
         int toastCount = 0; // toasts from this pid
         ArrayList<ToastRecord> list = mToastQueue;
@@ -3127,6 +3152,12 @@
         }
     }
 
+    private void recordCallerLocked(NotificationRecord record) {
+        if (mZenModeHelper.isCall(record)) {
+            mZenModeHelper.recordCaller(record);
+        }
+    }
+
     // let zen mode evaluate this record
     private void applyZenModeLocked(NotificationRecord record) {
         record.setIntercepted(mZenModeHelper.shouldIntercept(record));
@@ -3266,6 +3297,10 @@
     }
 
     private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete, int reason) {
+
+        // Record caller.
+        recordCallerLocked(r);
+
         // tell the app
         if (sendDelete) {
             if (r.getNotification().deleteIntent != null) {
@@ -3867,14 +3902,14 @@
         }
 
         @Override
-        public void onPackagesChanged(boolean queryReplace, String[] pkgList) {
-            if (DEBUG) Slog.d(TAG, "onPackagesChanged queryReplace=" + queryReplace
+        public void onPackagesChanged(boolean removingPackage, String[] pkgList) {
+            if (DEBUG) Slog.d(TAG, "onPackagesChanged removingPackage=" + removingPackage
                     + " pkgList=" + (pkgList == null ? null : Arrays.asList(pkgList)));
             if (mRankerServicePackageName == null) {
                 return;
             }
 
-            if (pkgList != null && (pkgList.length > 0)) {
+            if (pkgList != null && (pkgList.length > 0) && !removingPackage) {
                 for (String pkgName : pkgList) {
                     if (mRankerServicePackageName.equals(pkgName)) {
                         registerRanker();
diff --git a/services/core/java/com/android/server/notification/RankingHelper.java b/services/core/java/com/android/server/notification/RankingHelper.java
index c7325bb..32c5b13 100644
--- a/services/core/java/com/android/server/notification/RankingHelper.java
+++ b/services/core/java/com/android/server/notification/RankingHelper.java
@@ -504,8 +504,8 @@
         return packageBans;
     }
 
-    public void onPackagesChanged(boolean queryReplace, String[] pkgList) {
-        if (queryReplace || pkgList == null || pkgList.length == 0
+    public void onPackagesChanged(boolean removingPackage, String[] pkgList) {
+        if (!removingPackage || pkgList == null || pkgList.length == 0
                 || mRestoredWithoutUids.isEmpty()) {
             return; // nothing to do
         }
diff --git a/services/core/java/com/android/server/notification/ScheduleCalendar.java b/services/core/java/com/android/server/notification/ScheduleCalendar.java
index 22ca702..9e8b2e3 100644
--- a/services/core/java/com/android/server/notification/ScheduleCalendar.java
+++ b/services/core/java/com/android/server/notification/ScheduleCalendar.java
@@ -82,15 +82,13 @@
         if (end <= start) {
             end = addDays(end, 1);
         }
-        boolean isInSchedule =
-                isInSchedule(-1, time, start, end) || isInSchedule(0, time, start, end);
-        if (isInSchedule && mSchedule.exitAtAlarm
+        return isInSchedule(-1, time, start, end) || isInSchedule(0, time, start, end);
+    }
+
+    public boolean shouldExitForAlarm(long time) {
+        return mSchedule.exitAtAlarm
                 && mSchedule.nextAlarm != 0
-                && time >= mSchedule.nextAlarm) {
-            return false;
-        } else {
-            return isInSchedule;
-        }
+                && time >= mSchedule.nextAlarm;
     }
 
     private boolean isInSchedule(int daysOffset, long time, long start, long end) {
diff --git a/services/core/java/com/android/server/notification/ScheduleConditionProvider.java b/services/core/java/com/android/server/notification/ScheduleConditionProvider.java
index 8197544..32d03ce 100644
--- a/services/core/java/com/android/server/notification/ScheduleConditionProvider.java
+++ b/services/core/java/com/android/server/notification/ScheduleConditionProvider.java
@@ -25,11 +25,15 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.Uri;
+import android.os.Binder;
+import android.provider.Settings;
 import android.service.notification.Condition;
 import android.service.notification.IConditionProvider;
 import android.service.notification.ZenModeConfig;
 import android.service.notification.ZenModeConfig.ScheduleInfo;
+import android.text.TextUtils;
 import android.util.ArrayMap;
+import android.util.ArraySet;
 import android.util.Log;
 import android.util.Slog;
 
@@ -53,9 +57,13 @@
     private static final String ACTION_EVALUATE =  SIMPLE_NAME + ".EVALUATE";
     private static final int REQUEST_CODE_EVALUATE = 1;
     private static final String EXTRA_TIME = "time";
+    private static final String SEPARATOR = ";";
+    private static final String SCP_SETTING = "snoozed_schedule_condition_provider";
+
 
     private final Context mContext = this;
     private final ArrayMap<Uri, ScheduleCalendar> mSubscriptions = new ArrayMap<>();
+    private ArraySet<Uri> mSnoozed = new ArraySet<>();
 
     private AlarmManager mAlarmManager;
     private boolean mConnected;
@@ -90,6 +98,7 @@
             pw.print("            ");
             pw.println(mSubscriptions.get(conditionId).toString());
         }
+        pw.println("      snoozed due to alarm: " + TextUtils.join(SEPARATOR, mSnoozed));
         dumpUpcomingTime(pw, "mNextAlarmTime", mNextAlarmTime, now);
     }
 
@@ -97,6 +106,7 @@
     public void onConnected() {
         if (DEBUG) Slog.d(TAG, "onConnected");
         mConnected = true;
+        readSnoozed();
     }
 
     @Override
@@ -126,6 +136,7 @@
     public void onUnsubscribe(Uri conditionId) {
         if (DEBUG) Slog.d(TAG, "onUnsubscribe " + conditionId);
         mSubscriptions.remove(conditionId);
+        removeSnoozed(conditionId);
         evaluateSubscriptions();
     }
 
@@ -150,10 +161,16 @@
         for (Uri conditionId : mSubscriptions.keySet()) {
             final ScheduleCalendar cal = mSubscriptions.get(conditionId);
             if (cal != null && cal.isInSchedule(now)) {
-                notifyCondition(conditionId, Condition.STATE_TRUE, "meetsSchedule");
+                if (conditionSnoozed(conditionId) || cal.shouldExitForAlarm(now)) {
+                    notifyCondition(conditionId, Condition.STATE_FALSE, "alarmCanceled");
+                    addSnoozed(conditionId);
+                } else {
+                    notifyCondition(conditionId, Condition.STATE_TRUE, "meetsSchedule");
+                }
                 cal.maybeSetNextAlarm(now, nextUserAlarmTime);
             } else {
                 notifyCondition(conditionId, Condition.STATE_FALSE, "!meetsSchedule");
+                removeSnoozed(conditionId);
                 if (nextUserAlarmTime == 0) {
                     cal.maybeSetNextAlarm(now, nextUserAlarmTime);
                 }
@@ -194,7 +211,7 @@
         return info != null ? info.getTriggerTime() : 0;
     }
 
-    private static boolean meetsSchedule(ScheduleCalendar cal, long time) {
+    private boolean meetsSchedule(ScheduleCalendar cal, long time) {
         return cal != null && cal.isInSchedule(time);
     }
 
@@ -237,6 +254,62 @@
         return new Condition(id, summary, line1, line2, 0, state, Condition.FLAG_RELEVANT_ALWAYS);
     }
 
+    private boolean conditionSnoozed(Uri conditionId) {
+        synchronized (mSnoozed) {
+            return mSnoozed.contains(conditionId);
+        }
+    }
+
+    private void addSnoozed(Uri conditionId) {
+        synchronized (mSnoozed) {
+            mSnoozed.add(conditionId);
+            saveSnoozedLocked();
+        }
+    }
+
+    private void removeSnoozed(Uri conditionId) {
+        synchronized (mSnoozed) {
+            mSnoozed.remove(conditionId);
+            saveSnoozedLocked();
+        }
+    }
+
+    public void saveSnoozedLocked() {
+        final String setting = TextUtils.join(SEPARATOR, mSnoozed);
+        final int currentUser = ActivityManager.getCurrentUser();
+        Settings.Secure.putStringForUser(mContext.getContentResolver(),
+                SCP_SETTING,
+                setting,
+                currentUser);
+    }
+
+    public void readSnoozed() {
+        synchronized (mSnoozed) {
+            long identity = Binder.clearCallingIdentity();
+            try {
+                final String setting = Settings.Secure.getStringForUser(
+                        mContext.getContentResolver(),
+                        SCP_SETTING,
+                        ActivityManager.getCurrentUser());
+                if (setting != null) {
+                    final String[] tokens = setting.split(SEPARATOR);
+                    for (int i = 0; i < tokens.length; i++) {
+                        String token = tokens[i];
+                        if (token != null) {
+                            token = token.trim();
+                        }
+                        if (TextUtils.isEmpty(token)) {
+                            continue;
+                        }
+                        mSnoozed.add(Uri.parse(token));
+                    }
+                }
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
+        }
+    }
+
     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
diff --git a/services/core/java/com/android/server/notification/ZenModeConditions.java b/services/core/java/com/android/server/notification/ZenModeConditions.java
index 4cd61fd..7badecf 100644
--- a/services/core/java/com/android/server/notification/ZenModeConditions.java
+++ b/services/core/java/com/android/server/notification/ZenModeConditions.java
@@ -152,6 +152,7 @@
                     mSubscriptions.put(rule.conditionId, rule.component);
                 }
             } else {
+                rule.condition = null;
                 if (DEBUG) Log.d(TAG, "zmc failed to subscribe");
             }
         }
diff --git a/services/core/java/com/android/server/notification/ZenModeFiltering.java b/services/core/java/com/android/server/notification/ZenModeFiltering.java
index 80dc523..cbaad46 100644
--- a/services/core/java/com/android/server/notification/ZenModeFiltering.java
+++ b/services/core/java/com/android/server/notification/ZenModeFiltering.java
@@ -81,7 +81,9 @@
         if (zen == Global.ZEN_MODE_NO_INTERRUPTIONS) return false; // nothing gets through
         if (zen == Global.ZEN_MODE_ALARMS) return false; // not an alarm
         if (zen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
-            if (config.allowRepeatCallers && REPEAT_CALLERS.isRepeat(context, extras)) return true;
+            if (config.allowRepeatCallers && REPEAT_CALLERS.isRepeat(context, extras)) {
+                return true;
+            }
             if (!config.allowCalls) return false; // no other calls get through
             if (validator != null) {
                 final float contactAffinity = validator.getContactAffinity(userHandle, extras,
@@ -97,6 +99,10 @@
                 ? record.sbn.getNotification().extras : null;
     }
 
+    protected void recordCall(NotificationRecord record) {
+        REPEAT_CALLERS.recordCall(mContext, extras(record));
+    }
+
     public boolean shouldIntercept(int zen, ZenModeConfig config, NotificationRecord record) {
         if (isSystem(record)) {
             return false;
@@ -233,28 +239,45 @@
     }
 
     private static class RepeatCallers {
+        // Person : time
         private final ArrayMap<String, Long> mCalls = new ArrayMap<>();
         private int mThresholdMinutes;
 
+        private synchronized void recordCall(Context context, Bundle extras) {
+            setThresholdMinutes(context);
+            if (mThresholdMinutes <= 0 || extras == null) return;
+            final String peopleString = peopleString(extras);
+            if (peopleString == null) return;
+            final long now = System.currentTimeMillis();
+            cleanUp(mCalls, now);
+            mCalls.put(peopleString, now);
+        }
+
         private synchronized boolean isRepeat(Context context, Bundle extras) {
-            if (mThresholdMinutes <= 0) {
-                mThresholdMinutes = context.getResources().getInteger(com.android.internal.R.integer
-                        .config_zen_repeat_callers_threshold);
-            }
+            setThresholdMinutes(context);
             if (mThresholdMinutes <= 0 || extras == null) return false;
             final String peopleString = peopleString(extras);
             if (peopleString == null) return false;
             final long now = System.currentTimeMillis();
-            final int N = mCalls.size();
+            cleanUp(mCalls, now);
+            return mCalls.containsKey(peopleString);
+        }
+
+        private synchronized void cleanUp(ArrayMap<String, Long> calls, long now) {
+            final int N = calls.size();
             for (int i = N - 1; i >= 0; i--) {
                 final long time = mCalls.valueAt(i);
                 if (time > now || (now - time) > mThresholdMinutes * 1000 * 60) {
-                    mCalls.removeAt(i);
+                    calls.removeAt(i);
                 }
             }
-            final boolean isRepeat = mCalls.containsKey(peopleString);
-            mCalls.put(peopleString, now);
-            return isRepeat;
+        }
+
+        private void setThresholdMinutes(Context context) {
+            if (mThresholdMinutes <= 0) {
+                mThresholdMinutes = context.getResources().getInteger(com.android.internal.R.integer
+                        .config_zen_repeat_callers_threshold);
+            }
         }
 
         private static String peopleString(Bundle extras) {
diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index 6864ed8..afd42ea 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -35,6 +35,7 @@
 import android.content.res.Resources;
 import android.content.res.XmlResourceParser;
 import android.database.ContentObserver;
+import android.media.AudioAttributes;
 import android.media.AudioManager;
 import android.media.AudioManagerInternal;
 import android.media.AudioSystem;
@@ -54,7 +55,6 @@
 import android.service.notification.ZenModeConfig.EventInfo;
 import android.service.notification.ZenModeConfig.ScheduleInfo;
 import android.service.notification.ZenModeConfig.ZenRule;
-import android.text.TextUtils;
 import android.util.AndroidRuntimeException;
 import android.util.Log;
 import android.util.SparseArray;
@@ -140,8 +140,7 @@
             ValidateNotificationPeople validator, int contactsTimeoutMs, float timeoutAffinity) {
         synchronized (mConfig) {
             return ZenModeFiltering.matchesCallFilter(mContext, mZenMode, mConfig, userHandle,
-                    extras,
-                    validator, contactsTimeoutMs, timeoutAffinity);
+                    extras, validator, contactsTimeoutMs, timeoutAffinity);
         }
     }
 
@@ -149,6 +148,10 @@
         return mFiltering.isCall(record);
     }
 
+    public void recordCaller(NotificationRecord record) {
+        mFiltering.recordCall(record);
+    }
+
     public boolean shouldIntercept(NotificationRecord record) {
         synchronized (mConfig) {
             return mFiltering.shouldIntercept(mZenMode, mConfig, record);
@@ -229,7 +232,7 @@
     public void requestFromListener(ComponentName name, int filter) {
         final int newZen = NotificationManager.zenModeFromInterruptionFilter(filter, -1);
         if (newZen != -1) {
-            setManualZenMode(newZen, null,
+            setManualZenMode(newZen, null, name != null ? name.getPackageName() : null,
                     "listener:" + (name != null ? name.flattenToShortString() : null));
         }
     }
@@ -452,11 +455,11 @@
                 rule.creationTime);
     }
 
-    public void setManualZenMode(int zenMode, Uri conditionId, String reason) {
-        setManualZenMode(zenMode, conditionId, reason, true /*setRingerMode*/);
+    public void setManualZenMode(int zenMode, Uri conditionId, String caller, String reason) {
+        setManualZenMode(zenMode, conditionId, reason, caller, true /*setRingerMode*/);
     }
 
-    private void setManualZenMode(int zenMode, Uri conditionId, String reason,
+    private void setManualZenMode(int zenMode, Uri conditionId, String reason, String caller,
             boolean setRingerMode) {
         ZenModeConfig newConfig;
         synchronized (mConfig) {
@@ -478,6 +481,7 @@
                 newRule.enabled = true;
                 newRule.zenMode = zenMode;
                 newRule.conditionId = conditionId;
+                newRule.enabler = caller;
                 newConfig.manualRule = newRule;
             }
             setConfigLocked(newConfig, reason, setRingerMode);
@@ -733,13 +737,14 @@
         // total silence restrictions
         final boolean muteEverything = mZenMode == Global.ZEN_MODE_NO_INTERRUPTIONS;
 
-        for (int i = USAGE_UNKNOWN; i <= USAGE_VIRTUAL_SOURCE; i++) {
-            if (i == USAGE_NOTIFICATION) {
-                applyRestrictions(muteNotifications || muteEverything, i);
-            } else if (i == USAGE_NOTIFICATION_RINGTONE) {
-                applyRestrictions(muteCalls || muteEverything, i);
+        for (int usage : AudioAttributes.SDK_USAGES) {
+            final int suppressionBehavior = AudioAttributes.SUPPRESSIBLE_USAGES.get(usage);
+            if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_NOTIFICATION) {
+                applyRestrictions(muteNotifications || muteEverything, usage);
+            } else if (suppressionBehavior == AudioAttributes.SUPPRESSIBLE_CALL) {
+                applyRestrictions(muteCalls || muteEverything, usage);
             } else {
-                applyRestrictions(muteEverything, i);
+                applyRestrictions(muteEverything, usage);
             }
         }
     }
@@ -950,7 +955,8 @@
                     break;
             }
             if (newZen != -1) {
-                setManualZenMode(newZen, null, "ringerModeInternal", false /*setRingerMode*/);
+                setManualZenMode(newZen, null, "ringerModeInternal", null,
+                        false /*setRingerMode*/);
             }
 
             if (isChange || newZen != -1 || ringerModeExternal != ringerModeExternalOut) {
@@ -988,7 +994,8 @@
                     break;
             }
             if (newZen != -1) {
-                setManualZenMode(newZen, null, "ringerModeExternal", false /*setRingerMode*/);
+                setManualZenMode(newZen, null, "ringerModeExternal", caller,
+                        false /*setRingerMode*/);
             }
 
             ZenLog.traceSetRingerModeExternal(ringerModeOld, ringerModeNew, caller,
diff --git a/services/core/java/com/android/server/os/SchedulingPolicyService.java b/services/core/java/com/android/server/os/SchedulingPolicyService.java
index f98012b..62c9f4c 100644
--- a/services/core/java/com/android/server/os/SchedulingPolicyService.java
+++ b/services/core/java/com/android/server/os/SchedulingPolicyService.java
@@ -55,7 +55,8 @@
             Process.setThreadGroup(tid, Binder.getCallingPid() == pid ?
                     Process.THREAD_GROUP_AUDIO_SYS : Process.THREAD_GROUP_AUDIO_APP);
             // must be in this order or it fails the schedulability constraint
-            Process.setThreadScheduler(tid, Process.SCHED_FIFO, prio);
+            Process.setThreadScheduler(tid, Process.SCHED_FIFO | Process.SCHED_RESET_ON_FORK,
+                prio);
         } catch (RuntimeException e) {
             return PackageManager.PERMISSION_DENIED;
         }
diff --git a/services/core/java/com/android/server/pm/BackgroundDexOptService.java b/services/core/java/com/android/server/pm/BackgroundDexOptService.java
index 87f0581..cec1058 100644
--- a/services/core/java/com/android/server/pm/BackgroundDexOptService.java
+++ b/services/core/java/com/android/server/pm/BackgroundDexOptService.java
@@ -28,10 +28,13 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.os.BatteryManager;
+import android.os.Environment;
 import android.os.ServiceManager;
+import android.os.storage.StorageManager;
 import android.util.ArraySet;
 import android.util.Log;
 
+import java.io.File;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.TimeUnit;
 
@@ -66,6 +69,8 @@
      */
     final AtomicBoolean mExitPostBootUpdate = new AtomicBoolean(false);
 
+    private final File dataDir = Environment.getDataDirectory();
+
     public static void schedule(Context context) {
         JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
 
@@ -113,6 +118,16 @@
         return (100 * level / scale);
     }
 
+    private long getLowStorageThreshold() {
+        @SuppressWarnings("deprecation")
+        final long lowThreshold = StorageManager.from(this).getStorageLowBytes(dataDir);
+        if (lowThreshold == 0) {
+            Log.e(TAG, "Invalid low storage threshold");
+        }
+
+        return lowThreshold;
+    }
+
     private boolean runPostBootUpdate(final JobParameters jobParams,
             final PackageManagerService pm, final ArraySet<String> pkgs) {
         if (mExitPostBootUpdate.get()) {
@@ -124,6 +139,8 @@
         final int lowBatteryThreshold = getResources().getInteger(
                 com.android.internal.R.integer.config_lowBatteryWarningLevel);
 
+        final long lowThreshold = getLowStorageThreshold();
+
         mAbortPostBootUpdate.set(false);
         new Thread("BackgroundDexOptService_PostBootUpdate") {
             @Override
@@ -141,6 +158,14 @@
                         // Rather bail than completely drain the battery.
                         break;
                     }
+                    long usableSpace = dataDir.getUsableSpace();
+                    if (usableSpace < lowThreshold) {
+                        // Rather bail than completely fill up the disk.
+                        Log.w(TAG, "Aborting background dex opt job due to low storage: " +
+                                usableSpace);
+                        break;
+                    }
+
                     if (DEBUG_DEXOPT) {
                         Log.i(TAG, "Updating package " + pkg);
                     }
@@ -171,6 +196,9 @@
         mExitPostBootUpdate.set(true);
 
         mAbortIdleOptimization.set(false);
+
+        final long lowThreshold = getLowStorageThreshold();
+
         new Thread("BackgroundDexOptService_IdleOptimization") {
             @Override
             public void run() {
@@ -183,6 +211,15 @@
                         // Skip previously failing package
                         continue;
                     }
+
+                    long usableSpace = dataDir.getUsableSpace();
+                    if (usableSpace < lowThreshold) {
+                        // Rather bail than completely fill up the disk.
+                        Log.w(TAG, "Aborting background dex opt job due to low storage: " +
+                                usableSpace);
+                        break;
+                    }
+
                     // Conservatively add package to the list of failing ones in case performDexOpt
                     // never returns.
                     synchronized (sFailedPackageNames) {
@@ -213,6 +250,9 @@
             Log.i(TAG, "onStartJob");
         }
 
+        // NOTE: PackageManagerService.isStorageLow uses a different set of criteria from
+        // the checks above. This check is not "live" - the value is determined by a background
+        // restart with a period of ~1 minute.
         PackageManagerService pm = (PackageManagerService)ServiceManager.getService("package");
         if (pm.isStorageLow()) {
             if (DEBUG_DEXOPT) {
diff --git a/services/core/java/com/android/server/pm/CompilerStats.java b/services/core/java/com/android/server/pm/CompilerStats.java
index 46efd98..8c2fc3e 100644
--- a/services/core/java/com/android/server/pm/CompilerStats.java
+++ b/services/core/java/com/android/server/pm/CompilerStats.java
@@ -265,6 +265,7 @@
         try {
             f = file.startWrite();
             OutputStreamWriter osw = new OutputStreamWriter(f);
+            write(osw);
             osw.flush();
             file.finishWrite(f);
         } catch (IOException e) {
diff --git a/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java b/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java
index 098b39e..5016ec0 100644
--- a/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java
+++ b/services/core/java/com/android/server/pm/DefaultPermissionGrantPolicy.java
@@ -17,6 +17,7 @@
 package com.android.server.pm;
 
 import android.Manifest;
+import android.annotation.NonNull;
 import android.app.DownloadManager;
 import android.app.admin.DevicePolicyManager;
 import android.content.Intent;
@@ -30,7 +31,11 @@
 import android.content.pm.ResolveInfo;
 import android.net.Uri;
 import android.os.Build;
+import android.os.Environment;
+import android.os.Handler;
+import android.os.Message;
 import android.os.UserHandle;
+import android.os.storage.StorageManager;
 import android.print.PrintManager;
 import android.provider.CalendarContract;
 import android.provider.ContactsContract;
@@ -38,12 +43,23 @@
 import android.provider.Telephony.Sms.Intents;
 import android.telephony.TelephonyManager;
 import android.security.Credentials;
+import android.util.ArrayMap;
 import android.util.ArraySet;
 import android.util.Log;
+import android.util.Slog;
+import android.util.Xml;
+import com.android.internal.util.XmlUtils;
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
 
+import java.io.BufferedInputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import static android.os.Process.FIRST_APPLICATION_UID;
@@ -64,6 +80,13 @@
 
     private static final String AUDIO_MIME_TYPE = "audio/mpeg";
 
+    private static final String TAG_EXCEPTIONS = "exceptions";
+    private static final String TAG_EXCEPTION = "exception";
+    private static final String TAG_PERMISSION = "permission";
+    private static final String ATTR_PACKAGE = "package";
+    private static final String ATTR_NAME = "name";
+    private static final String ATTR_FIXED = "fixed";
+
     private static final Set<String> PHONE_PERMISSIONS = new ArraySet<>();
     static {
         PHONE_PERMISSIONS.add(Manifest.permission.READ_PHONE_STATE);
@@ -125,7 +148,14 @@
         STORAGE_PERMISSIONS.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
     }
 
+    private static final int MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS = 1;
+
+    private static final String ACTION_TWINNING =
+            "com.google.android.clockwork.intent.TWINNING_SETTINGS";
+    private static final String ACTION_TRACK = "com.android.fitness.TRACK";
+
     private final PackageManagerService mService;
+    private final Handler mHandler;
 
     private PackagesProvider mLocationPackagesProvider;
     private PackagesProvider mVoiceInteractionPackagesProvider;
@@ -134,8 +164,22 @@
     private PackagesProvider mSimCallManagerPackagesProvider;
     private SyncAdapterPackagesProvider mSyncAdapterPackagesProvider;
 
+    private ArrayMap<String, List<DefaultPermissionGrant>> mGrantExceptions;
+
     public DefaultPermissionGrantPolicy(PackageManagerService service) {
         mService = service;
+        mHandler = new Handler(mService.mHandlerThread.getLooper()) {
+            @Override
+            public void handleMessage(Message msg) {
+                if (msg.what == MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS) {
+                    synchronized (mService.mPackages) {
+                        if (mGrantExceptions == null) {
+                            mGrantExceptions = readDefaultPermissionExceptionsLPw();
+                        }
+                    }
+                }
+            }
+        };
     }
 
     public void setLocationPackagesProviderLPw(PackagesProvider provider) {
@@ -165,6 +209,11 @@
     public void grantDefaultPermissions(int userId) {
         grantPermissionsToSysComponentsAndPrivApps(userId);
         grantDefaultSystemHandlerPermissions(userId);
+        grantDefaultPermissionExceptions(userId);
+    }
+
+    public void scheduleReadDefaultPermissionExceptions() {
+        mHandler.sendEmptyMessage(MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS);
     }
 
     private void grantPermissionsToSysComponentsAndPrivApps(int userId) {
@@ -473,6 +522,7 @@
             if (emailPackage != null
                     && doesPackageSupportRuntimePermissions(emailPackage)) {
                 grantRuntimePermissionsLPw(emailPackage, CONTACTS_PERMISSIONS, userId);
+                grantRuntimePermissionsLPw(emailPackage, CALENDAR_PERMISSIONS, userId);
             }
 
             // Browser
@@ -557,8 +607,9 @@
                 grantRuntimePermissionsLPw(musicPackage, STORAGE_PERMISSIONS, userId);
             }
 
-            // Android Wear Home
+            // Watches
             if (mService.hasSystemFeature(PackageManager.FEATURE_WATCH, 0)) {
+                // Home application on watches
                 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
                 homeIntent.addCategory(Intent.CATEGORY_HOME_MAIN);
 
@@ -575,6 +626,27 @@
                     grantRuntimePermissionsLPw(wearHomePackage, LOCATION_PERMISSIONS, false,
                             userId);
                 }
+
+                // Twinning on watches
+                Intent twinningIntent = new Intent(ACTION_TWINNING);
+                PackageParser.Package twinningPackage = getDefaultSystemHandlerActivityPackageLPr(
+                        twinningIntent, userId);
+
+                if (twinningPackage != null
+                        && doesPackageSupportRuntimePermissions(twinningPackage)) {
+                    grantRuntimePermissionsLPw(twinningPackage, PHONE_PERMISSIONS, false, userId);
+                    grantRuntimePermissionsLPw(twinningPackage, SMS_PERMISSIONS, false, userId);
+                }
+
+                // Fitness tracking on watches
+                Intent trackIntent = new Intent(ACTION_TRACK);
+                PackageParser.Package trackPackage = getDefaultSystemHandlerActivityPackageLPr(
+                        trackIntent, userId);
+                if (trackPackage != null
+                        && doesPackageSupportRuntimePermissions(trackPackage)) {
+                    grantRuntimePermissionsLPw(trackPackage, SENSORS_PERMISSIONS, false, userId);
+                    grantRuntimePermissionsLPw(trackPackage, LOCATION_PERMISSIONS, false, userId);
+                }
             }
 
             // Print Spooler
@@ -605,6 +677,15 @@
                 grantRuntimePermissionsLPw(nfcTagPkg, CONTACTS_PERMISSIONS, false, userId);
                 grantRuntimePermissionsLPw(nfcTagPkg, PHONE_PERMISSIONS, false, userId);
             }
+
+            // Storage Manager
+            Intent storageManagerIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
+            PackageParser.Package storageManagerPckg = getDefaultSystemHandlerActivityPackageLPr(
+                    storageManagerIntent, userId);
+            if (storageManagerPckg != null
+                    && doesPackageSupportRuntimePermissions(storageManagerPckg)) {
+                grantRuntimePermissionsLPw(storageManagerPckg, STORAGE_PERMISSIONS, true, userId);
+            }
             mService.mSettings.onDefaultRuntimePermissionsGrantedLPr(userId);
         }
     }
@@ -619,6 +700,7 @@
             grantRuntimePermissionsLPw(dialerPackage, CONTACTS_PERMISSIONS, userId);
             grantRuntimePermissionsLPw(dialerPackage, SMS_PERMISSIONS, userId);
             grantRuntimePermissionsLPw(dialerPackage, MICROPHONE_PERMISSIONS, userId);
+            grantRuntimePermissionsLPw(dialerPackage, CAMERA_PERMISSIONS, userId);
         }
     }
 
@@ -656,6 +738,7 @@
             grantRuntimePermissionsLPw(dialerPackage, CONTACTS_PERMISSIONS, false, true, userId);
             grantRuntimePermissionsLPw(dialerPackage, SMS_PERMISSIONS, false, true, userId);
             grantRuntimePermissionsLPw(dialerPackage, MICROPHONE_PERMISSIONS, false, true, userId);
+            grantRuntimePermissionsLPw(dialerPackage, CAMERA_PERMISSIONS, false, true, userId);
         }
     }
 
@@ -903,7 +986,175 @@
                 pkg.mSignatures) == PackageManager.SIGNATURE_MATCH;
     }
 
+    private void grantDefaultPermissionExceptions(int userId) {
+        synchronized (mService.mPackages) {
+            mHandler.removeMessages(MSG_READ_DEFAULT_PERMISSION_EXCEPTIONS);
+
+            if (mGrantExceptions == null) {
+                mGrantExceptions = readDefaultPermissionExceptionsLPw();
+            }
+
+            // mGrantExceptions is null only before the first read and then
+            // it serves as a cache of the default grants that should be
+            // performed for every user. If there is an entry then the app
+            // is on the system image and supports runtime permissions.
+            Set<String> permissions = null;
+            final int exceptionCount = mGrantExceptions.size();
+            for (int i = 0; i < exceptionCount; i++) {
+                String packageName = mGrantExceptions.keyAt(i);
+                PackageParser.Package pkg = getSystemPackageLPr(packageName);
+                List<DefaultPermissionGrant> permissionGrants = mGrantExceptions.valueAt(i);
+                final int permissionGrantCount = permissionGrants.size();
+                for (int j = 0; j < permissionGrantCount; j++) {
+                    DefaultPermissionGrant permissionGrant = permissionGrants.get(j);
+                    if (permissions == null) {
+                        permissions = new ArraySet<>();
+                    } else {
+                        permissions.clear();
+                    }
+                    permissions.add(permissionGrant.name);
+                    grantRuntimePermissionsLPw(pkg, permissions, false,
+                            permissionGrant.fixed, userId);
+                }
+            }
+        }
+    }
+
+    private @NonNull ArrayMap<String, List<DefaultPermissionGrant>>
+            readDefaultPermissionExceptionsLPw() {
+        File dir = new File(Environment.getRootDirectory(), "etc/default-permissions");
+        if (!dir.exists() || !dir.isDirectory() || !dir.canRead()) {
+            return new ArrayMap<>(0);
+        }
+
+        File[] files = dir.listFiles();
+        if (files == null) {
+            return new ArrayMap<>(0);
+        }
+
+        ArrayMap<String, List<DefaultPermissionGrant>> grantExceptions = new ArrayMap<>();
+
+        // Iterate over the files in the directory and scan .xml files
+        for (File file : files) {
+            if (!file.getPath().endsWith(".xml")) {
+                Slog.i(TAG, "Non-xml file " + file + " in " + dir + " directory, ignoring");
+                continue;
+            }
+            if (!file.canRead()) {
+                Slog.w(TAG, "Default permissions file " + file + " cannot be read");
+                continue;
+            }
+            try (
+                InputStream str = new BufferedInputStream(new FileInputStream(file))
+            ) {
+                XmlPullParser parser = Xml.newPullParser();
+                parser.setInput(str, null);
+                parse(parser, grantExceptions);
+            } catch (XmlPullParserException | IOException e) {
+                Slog.w(TAG, "Error reading default permissions file " + file, e);
+            }
+        }
+
+        return grantExceptions;
+    }
+
+    private void parse(XmlPullParser parser, Map<String, List<DefaultPermissionGrant>>
+            outGrantExceptions) throws IOException, XmlPullParserException {
+        final int outerDepth = parser.getDepth();
+        int type;
+        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
+                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
+            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
+                continue;
+            }
+            if (TAG_EXCEPTIONS.equals(parser.getName())) {
+                parseExceptions(parser, outGrantExceptions);
+            } else {
+                Log.e(TAG, "Unknown tag " + parser.getName());
+            }
+        }
+    }
+
+    private void parseExceptions(XmlPullParser parser, Map<String, List<DefaultPermissionGrant>>
+            outGrantExceptions) throws IOException, XmlPullParserException {
+        final int outerDepth = parser.getDepth();
+        int type;
+        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
+                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
+            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
+                continue;
+            }
+            if (TAG_EXCEPTION.equals(parser.getName())) {
+                String packageName = parser.getAttributeValue(null, ATTR_PACKAGE);
+
+                List<DefaultPermissionGrant> packageExceptions =
+                        outGrantExceptions.get(packageName);
+                if (packageExceptions == null) {
+                    // The package must be on the system image
+                    PackageParser.Package pkg = getSystemPackageLPr(packageName);
+                    if (pkg == null) {
+                        Log.w(TAG, "Unknown package:" + packageName);
+                        XmlUtils.skipCurrentTag(parser);
+                        continue;
+                    }
+
+                    // The package must support runtime permissions
+                    if (!doesPackageSupportRuntimePermissions(pkg)) {
+                        Log.w(TAG, "Skipping non supporting runtime permissions package:"
+                                + packageName);
+                        XmlUtils.skipCurrentTag(parser);
+                        continue;
+                    }
+                    packageExceptions = new ArrayList<>();
+                    outGrantExceptions.put(packageName, packageExceptions);
+                }
+
+                parsePermission(parser, packageExceptions);
+            } else {
+                Log.e(TAG, "Unknown tag " + parser.getName() + "under <exceptions>");
+            }
+        }
+    }
+
+    private void parsePermission(XmlPullParser parser, List<DefaultPermissionGrant>
+            outPackageExceptions) throws IOException, XmlPullParserException {
+        final int outerDepth = parser.getDepth();
+        int type;
+        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
+                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
+            if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
+                continue;
+            }
+
+            if (TAG_PERMISSION.contains(parser.getName())) {
+                String name = parser.getAttributeValue(null, ATTR_NAME);
+                if (name == null) {
+                    Log.w(TAG, "Mandatory name attribute missing for permission tag");
+                    XmlUtils.skipCurrentTag(parser);
+                    continue;
+                }
+
+                final boolean fixed = XmlUtils.readBooleanAttribute(parser, ATTR_FIXED);
+
+                DefaultPermissionGrant exception = new DefaultPermissionGrant(name, fixed);
+                outPackageExceptions.add(exception);
+            } else {
+                Log.e(TAG, "Unknown tag " + parser.getName() + "under <exception>");
+            }
+        }
+    }
+
     private static boolean doesPackageSupportRuntimePermissions(PackageParser.Package pkg) {
         return pkg.applicationInfo.targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1;
     }
+
+    private static final class DefaultPermissionGrant {
+        final String name;
+        final boolean fixed;
+
+        public DefaultPermissionGrant(String name, boolean fixed) {
+            this.name = name;
+            this.fixed = fixed;
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/pm/EphemeralResolverConnection.java b/services/core/java/com/android/server/pm/EphemeralResolverConnection.java
index fe6fb1f..68b465a 100644
--- a/services/core/java/com/android/server/pm/EphemeralResolverConnection.java
+++ b/services/core/java/com/android/server/pm/EphemeralResolverConnection.java
@@ -16,6 +16,8 @@
 
 package com.android.server.pm;
 
+import android.app.EphemeralResolverService;
+import android.app.IEphemeralResolver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
@@ -30,9 +32,6 @@
 import android.os.UserHandle;
 import android.util.TimedRemoteCaller;
 
-import com.android.internal.app.EphemeralResolverService;
-import com.android.internal.app.IEphemeralResolver;
-
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -61,14 +60,15 @@
 
     public EphemeralResolverConnection(Context context, ComponentName componentName) {
         mContext = context;
-        mIntent = new Intent().setComponent(componentName);
+        mIntent = new Intent(Intent.ACTION_RESOLVE_EPHEMERAL_PACKAGE).setComponent(componentName);
     }
 
-    public final List<EphemeralResolveInfo> getEphemeralResolveInfoList(int hashPrefix) {
+    public final List<EphemeralResolveInfo> getEphemeralResolveInfoList(
+            int hashPrefix[], int prefixMask) {
         throwIfCalledOnMainThread();
         try {
             return mGetEphemeralResolveInfoCaller.getEphemeralResolveInfoList(
-                    getRemoteInstanceLazy(), hashPrefix);
+                    getRemoteInstanceLazy(), hashPrefix, prefixMask);
         } catch (RemoteException re) {
         } catch (TimeoutException te) {
         } finally {
@@ -177,10 +177,10 @@
         }
 
         public List<EphemeralResolveInfo> getEphemeralResolveInfoList(
-                IEphemeralResolver target, int hashPrefix)
+                IEphemeralResolver target, int hashPrefix[], int prefixMask)
                         throws RemoteException, TimeoutException {
             final int sequence = onBeforeRemoteCall();
-            target.getEphemeralResolveInfoList(mCallback, hashPrefix, sequence);
+            target.getEphemeralResolveInfoList(mCallback, hashPrefix, prefixMask, sequence);
             return getResultTimed(sequence);
         }
     }
diff --git a/services/core/java/com/android/server/pm/IntentFilterVerificationState.java b/services/core/java/com/android/server/pm/IntentFilterVerificationState.java
index c6e7911..a4e9d10 100644
--- a/services/core/java/com/android/server/pm/IntentFilterVerificationState.java
+++ b/services/core/java/com/android/server/pm/IntentFilterVerificationState.java
@@ -96,7 +96,12 @@
             if (i > 0) {
                 sb.append(" ");
             }
-            sb.append(mHosts.valueAt(i));
+            String host = mHosts.valueAt(i);
+            // "*.example.tld" is validated via https://example.tld
+            if (host.startsWith("*.")) {
+                host = host.substring(2);
+            }
+            sb.append(host);
         }
         return sb.toString();
     }
diff --git a/services/core/java/com/android/server/pm/LauncherAppsService.java b/services/core/java/com/android/server/pm/LauncherAppsService.java
index ffe8f75..4387e76 100644
--- a/services/core/java/com/android/server/pm/LauncherAppsService.java
+++ b/services/core/java/com/android/server/pm/LauncherAppsService.java
@@ -17,8 +17,9 @@
 package com.android.server.pm;
 
 import android.annotation.NonNull;
-import android.annotation.Nullable;
 import android.annotation.UserIdInt;
+import android.app.ActivityManager;
+import android.app.ActivityManagerInternal;
 import android.app.AppGlobals;
 import android.content.ComponentName;
 import android.content.Context;
@@ -98,6 +99,7 @@
         private final Context mContext;
         private final PackageManager mPm;
         private final UserManager mUm;
+        private final ActivityManagerInternal mActivityManagerInternal;
         private final ShortcutServiceInternal mShortcutServiceInternal;
         private final PackageCallbackList<IOnAppsChangedListener> mListeners
                 = new PackageCallbackList<IOnAppsChangedListener>();
@@ -110,6 +112,8 @@
             mContext = context;
             mPm = mContext.getPackageManager();
             mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+            mActivityManagerInternal = Preconditions.checkNotNull(
+                    LocalServices.getService(ActivityManagerInternal.class));
             mShortcutServiceInternal = Preconditions.checkNotNull(
                     LocalServices.getService(ShortcutServiceInternal.class));
             mShortcutServiceInternal.addListener(mPackageMonitor);
@@ -380,7 +384,8 @@
                         "To query by shortcut ID, package name must also be set");
             }
 
-            return new ParceledListSlice<>(
+            // TODO(b/29399275): Eclipse compiler requires explicit List<ShortcutInfo> cast below.
+            return new ParceledListSlice<>((List<ShortcutInfo>)
                     mShortcutServiceInternal.getShortcuts(getCallingUserId(),
                             callingPackage, changedSince, packageName, shortcutIds,
                             componentName, flags, user.getIdentifier()));
@@ -447,23 +452,41 @@
                 ensureShortcutPermission(callingPackage, userId);
             }
 
-            final Intent intent = mShortcutServiceInternal.createShortcutIntent(getCallingUserId(),
-                    callingPackage, packageName, shortcutId, userId);
-            if (intent == null) {
+            final Intent[] intents = mShortcutServiceInternal.createShortcutIntents(
+                    getCallingUserId(), callingPackage, packageName, shortcutId, userId);
+            if (intents == null || intents.length == 0) {
                 return false;
             }
             // Note the target activity doesn't have to be exported.
 
-            intent.setSourceBounds(sourceBounds);
-            prepareIntentForLaunch(intent, sourceBounds);
+            intents[0].addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            intents[0].setSourceBounds(sourceBounds);
 
-            final long ident = Binder.clearCallingIdentity();
+            return startShortcutIntentsAsPublisher(
+                    intents, packageName, startActivityOptions, userId);
+        }
+
+        private boolean startShortcutIntentsAsPublisher(@NonNull Intent[] intents,
+                @NonNull String publisherPackage, Bundle startActivityOptions, int userId) {
+            final int code;
+            final long ident = injectClearCallingIdentity();
             try {
-                mContext.startActivityAsUser(intent, startActivityOptions, UserHandle.of(userId));
+                code = mActivityManagerInternal.startActivitiesAsPackage(publisherPackage,
+                        userId, intents, startActivityOptions);
+                if (code >= ActivityManager.START_SUCCESS) {
+                    return true; // Success
+                } else {
+                    Log.e(TAG, "Couldn't start activity, code=" + code);
+                }
+                return code >= ActivityManager.START_SUCCESS;
+            } catch (SecurityException e) {
+                if (DEBUG) {
+                    Slog.d(TAG, "SecurityException while launching intent", e);
+                }
+                return false;
             } finally {
-                Binder.restoreCallingIdentity(ident);
+                injectRestoreCallingIdentity(ident);
             }
-            return true;
         }
 
         @Override
@@ -497,7 +520,9 @@
 
             Intent launchIntent = new Intent(Intent.ACTION_MAIN);
             launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
-            prepareIntentForLaunch(launchIntent, sourceBounds);
+            launchIntent.setSourceBounds(sourceBounds);
+            launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
+                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
             launchIntent.setPackage(component.getPackageName());
 
             long ident = Binder.clearCallingIdentity();
@@ -538,13 +563,6 @@
             }
         }
 
-        private void prepareIntentForLaunch(@NonNull Intent launchIntent,
-                @Nullable Rect sourceBounds) {
-            launchIntent.setSourceBounds(sourceBounds);
-            launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
-                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
-        }
-
         @Override
         public void showAppDetailsAsUser(ComponentName component, Rect sourceBounds,
                 Bundle opts, UserHandle user) throws RemoteException {
@@ -613,17 +631,20 @@
             public void onPackageAdded(String packageName, int uid) {
                 UserHandle user = new UserHandle(getChangingUserId());
                 final int n = mListeners.beginBroadcast();
-                for (int i = 0; i < n; i++) {
-                    IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
-                    BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
-                    if (!isEnabledProfileOf(user, cookie.user, "onPackageAdded")) continue;
-                    try {
-                        listener.onPackageAdded(user, packageName);
-                    } catch (RemoteException re) {
-                        Slog.d(TAG, "Callback failed ", re);
+                try {
+                    for (int i = 0; i < n; i++) {
+                        IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
+                        BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
+                        if (!isEnabledProfileOf(user, cookie.user, "onPackageAdded")) continue;
+                        try {
+                            listener.onPackageAdded(user, packageName);
+                        } catch (RemoteException re) {
+                            Slog.d(TAG, "Callback failed ", re);
+                        }
                     }
+                } finally {
+                    mListeners.finishBroadcast();
                 }
-                mListeners.finishBroadcast();
 
                 super.onPackageAdded(packageName, uid);
             }
@@ -632,17 +653,20 @@
             public void onPackageRemoved(String packageName, int uid) {
                 UserHandle user = new UserHandle(getChangingUserId());
                 final int n = mListeners.beginBroadcast();
-                for (int i = 0; i < n; i++) {
-                    IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
-                    BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
-                    if (!isEnabledProfileOf(user, cookie.user, "onPackageRemoved")) continue;
-                    try {
-                        listener.onPackageRemoved(user, packageName);
-                    } catch (RemoteException re) {
-                        Slog.d(TAG, "Callback failed ", re);
+                try {
+                    for (int i = 0; i < n; i++) {
+                        IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
+                        BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
+                        if (!isEnabledProfileOf(user, cookie.user, "onPackageRemoved")) continue;
+                        try {
+                            listener.onPackageRemoved(user, packageName);
+                        } catch (RemoteException re) {
+                            Slog.d(TAG, "Callback failed ", re);
+                        }
                     }
+                } finally {
+                    mListeners.finishBroadcast();
                 }
-                mListeners.finishBroadcast();
 
                 super.onPackageRemoved(packageName, uid);
             }
@@ -651,17 +675,20 @@
             public void onPackageModified(String packageName) {
                 UserHandle user = new UserHandle(getChangingUserId());
                 final int n = mListeners.beginBroadcast();
-                for (int i = 0; i < n; i++) {
-                    IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
-                    BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
-                    if (!isEnabledProfileOf(user, cookie.user, "onPackageModified")) continue;
-                    try {
-                        listener.onPackageChanged(user, packageName);
-                    } catch (RemoteException re) {
-                        Slog.d(TAG, "Callback failed ", re);
+                try {
+                    for (int i = 0; i < n; i++) {
+                        IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
+                        BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
+                        if (!isEnabledProfileOf(user, cookie.user, "onPackageModified")) continue;
+                        try {
+                            listener.onPackageChanged(user, packageName);
+                        } catch (RemoteException re) {
+                            Slog.d(TAG, "Callback failed ", re);
+                        }
                     }
+                } finally {
+                    mListeners.finishBroadcast();
                 }
-                mListeners.finishBroadcast();
 
                 super.onPackageModified(packageName);
             }
@@ -670,17 +697,20 @@
             public void onPackagesAvailable(String[] packages) {
                 UserHandle user = new UserHandle(getChangingUserId());
                 final int n = mListeners.beginBroadcast();
-                for (int i = 0; i < n; i++) {
-                    IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
-                    BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
-                    if (!isEnabledProfileOf(user, cookie.user, "onPackagesAvailable")) continue;
-                    try {
-                        listener.onPackagesAvailable(user, packages, isReplacing());
-                    } catch (RemoteException re) {
-                        Slog.d(TAG, "Callback failed ", re);
+                try {
+                    for (int i = 0; i < n; i++) {
+                        IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
+                        BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
+                        if (!isEnabledProfileOf(user, cookie.user, "onPackagesAvailable")) continue;
+                        try {
+                            listener.onPackagesAvailable(user, packages, isReplacing());
+                        } catch (RemoteException re) {
+                            Slog.d(TAG, "Callback failed ", re);
+                        }
                     }
+                } finally {
+                    mListeners.finishBroadcast();
                 }
-                mListeners.finishBroadcast();
 
                 super.onPackagesAvailable(packages);
             }
@@ -689,17 +719,20 @@
             public void onPackagesUnavailable(String[] packages) {
                 UserHandle user = new UserHandle(getChangingUserId());
                 final int n = mListeners.beginBroadcast();
-                for (int i = 0; i < n; i++) {
-                    IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
-                    BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
-                    if (!isEnabledProfileOf(user, cookie.user, "onPackagesUnavailable")) continue;
-                    try {
-                        listener.onPackagesUnavailable(user, packages, isReplacing());
-                    } catch (RemoteException re) {
-                        Slog.d(TAG, "Callback failed ", re);
+                try {
+                    for (int i = 0; i < n; i++) {
+                        IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
+                        BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
+                        if (!isEnabledProfileOf(user, cookie.user, "onPackagesUnavailable")) continue;
+                        try {
+                            listener.onPackagesUnavailable(user, packages, isReplacing());
+                        } catch (RemoteException re) {
+                            Slog.d(TAG, "Callback failed ", re);
+                        }
                     }
+                } finally {
+                    mListeners.finishBroadcast();
                 }
-                mListeners.finishBroadcast();
 
                 super.onPackagesUnavailable(packages);
             }
@@ -708,17 +741,20 @@
             public void onPackagesSuspended(String[] packages) {
                 UserHandle user = new UserHandle(getChangingUserId());
                 final int n = mListeners.beginBroadcast();
-                for (int i = 0; i < n; i++) {
-                    IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
-                    BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
-                    if (!isEnabledProfileOf(user, cookie.user, "onPackagesSuspended")) continue;
-                    try {
-                        listener.onPackagesSuspended(user, packages);
-                    } catch (RemoteException re) {
-                        Slog.d(TAG, "Callback failed ", re);
+                try {
+                    for (int i = 0; i < n; i++) {
+                        IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
+                        BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
+                        if (!isEnabledProfileOf(user, cookie.user, "onPackagesSuspended")) continue;
+                        try {
+                            listener.onPackagesSuspended(user, packages);
+                        } catch (RemoteException re) {
+                            Slog.d(TAG, "Callback failed ", re);
+                        }
                     }
+                } finally {
+                    mListeners.finishBroadcast();
                 }
-                mListeners.finishBroadcast();
 
                 super.onPackagesSuspended(packages);
             }
@@ -727,17 +763,20 @@
             public void onPackagesUnsuspended(String[] packages) {
                 UserHandle user = new UserHandle(getChangingUserId());
                 final int n = mListeners.beginBroadcast();
-                for (int i = 0; i < n; i++) {
-                    IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
-                    BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
-                    if (!isEnabledProfileOf(user, cookie.user, "onPackagesUnsuspended")) continue;
-                    try {
-                        listener.onPackagesUnsuspended(user, packages);
-                    } catch (RemoteException re) {
-                        Slog.d(TAG, "Callback failed ", re);
+                try {
+                    for (int i = 0; i < n; i++) {
+                        IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
+                        BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
+                        if (!isEnabledProfileOf(user, cookie.user, "onPackagesUnsuspended")) continue;
+                        try {
+                            listener.onPackagesUnsuspended(user, packages);
+                        } catch (RemoteException re) {
+                            Slog.d(TAG, "Callback failed ", re);
+                        }
                     }
+                } finally {
+                    mListeners.finishBroadcast();
                 }
-                mListeners.finishBroadcast();
 
                 super.onPackagesUnsuspended(packages);
             }
@@ -745,50 +784,52 @@
             @Override
             public void onShortcutChanged(@NonNull String packageName,
                     @UserIdInt int userId) {
-                if (!ShortcutService.FEATURE_ENABLED) {
-                    return;
-                }
                 postToPackageMonitorHandler(() -> onShortcutChangedInner(packageName, userId));
             }
 
             private void onShortcutChangedInner(@NonNull String packageName,
                     @UserIdInt int userId) {
-                final UserHandle user = UserHandle.of(userId);
-
                 final int n = mListeners.beginBroadcast();
-                for (int i = 0; i < n; i++) {
-                    IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
-                    BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
-                    if (!isEnabledProfileOf(user, cookie.user, "onShortcutChanged")) continue;
+                try {
+                    final UserHandle user = UserHandle.of(userId);
 
-                    final int launcherUserId = cookie.user.getIdentifier();
+                    for (int i = 0; i < n; i++) {
+                        IOnAppsChangedListener listener = mListeners.getBroadcastItem(i);
+                        BroadcastCookie cookie = (BroadcastCookie) mListeners.getBroadcastCookie(i);
+                        if (!isEnabledProfileOf(user, cookie.user, "onShortcutChanged")) continue;
 
-                    // Make sure the caller has the permission.
-                    if (!mShortcutServiceInternal.hasShortcutHostPermission(
-                            launcherUserId, cookie.packageName)) {
-                        continue;
+                        final int launcherUserId = cookie.user.getIdentifier();
+
+                        // Make sure the caller has the permission.
+                        if (!mShortcutServiceInternal.hasShortcutHostPermission(
+                                launcherUserId, cookie.packageName)) {
+                            continue;
+                        }
+                        // Each launcher has a different set of pinned shortcuts, so we need to do a
+                        // query in here.
+                        // (As of now, only one launcher has the permission at a time, so it's bit
+                        // moot, but we may change the permission model eventually.)
+                        final List<ShortcutInfo> list =
+                                mShortcutServiceInternal.getShortcuts(launcherUserId,
+                                        cookie.packageName,
+                                        /* changedSince= */ 0, packageName, /* shortcutIds=*/ null,
+                                        /* component= */ null,
+                                        ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY
+                                        | ShortcutQuery.FLAG_GET_ALL_KINDS
+                                        , userId);
+                        try {
+                            listener.onShortcutChanged(user, packageName,
+                                    new ParceledListSlice<>(list));
+                        } catch (RemoteException re) {
+                            Slog.d(TAG, "Callback failed ", re);
+                        }
                     }
-                    // Each launcher has a different set of pinned shortcuts, so we need to do a
-                    // query in here.
-                    // (As of now, only one launcher has the permission at a time, so it's bit
-                    // moot, but we may change the permission model eventually.)
-                    final List<ShortcutInfo> list =
-                            mShortcutServiceInternal.getShortcuts(launcherUserId,
-                                    cookie.packageName,
-                                    /* changedSince= */ 0, packageName, /* shortcutIds=*/ null,
-                                    /* component= */ null,
-                                    ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY
-                                    | ShortcutQuery.FLAG_GET_PINNED
-                                    | ShortcutQuery.FLAG_GET_DYNAMIC
-                                    , userId);
-                    try {
-                        listener.onShortcutChanged(user, packageName,
-                                new ParceledListSlice<>(list));
-                    } catch (RemoteException re) {
-                        Slog.d(TAG, "Callback failed ", re);
-                    }
+                } catch (RuntimeException e) {
+                    // When the user is locked we get IllegalState, so just catch all.
+                    Log.w(TAG, e.getMessage(), e);
+                } finally {
+                    mListeners.finishBroadcast();
                 }
-                mListeners.finishBroadcast();
             }
         }
 
diff --git a/services/core/java/com/android/server/pm/PackageInstallerService.java b/services/core/java/com/android/server/pm/PackageInstallerService.java
index 83af017..d25abbf 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerService.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerService.java
@@ -182,6 +182,10 @@
      */
     private final Random mRandom = new SecureRandom();
 
+    /** All sessions allocated */
+    @GuardedBy("mSessions")
+    private final SparseBooleanArray mAllocatedSessions = new SparseBooleanArray();
+
     @GuardedBy("mSessions")
     private final SparseArray<PackageInstallerSession> mSessions = new SparseArray<>();
 
@@ -365,6 +369,7 @@
                             // keep details around for dumpsys.
                             mHistoricalSessions.put(session.sessionId, session);
                         }
+                        mAllocatedSessions.put(session.sessionId, true);
                     }
                 }
             }
@@ -666,23 +671,26 @@
                         "Too many historical sessions for UID " + callingUid);
             }
 
-            final long createdMillis = System.currentTimeMillis();
             sessionId = allocateSessionIdLocked();
+        }
 
-            // We're staging to exactly one location
-            File stageDir = null;
-            String stageCid = null;
-            if ((params.installFlags & PackageManager.INSTALL_INTERNAL) != 0) {
-                final boolean isEphemeral =
-                        (params.installFlags & PackageManager.INSTALL_EPHEMERAL) != 0;
-                stageDir = buildStageDir(params.volumeUuid, sessionId, isEphemeral);
-            } else {
-                stageCid = buildExternalStageCid(sessionId);
-            }
+        final long createdMillis = System.currentTimeMillis();
+        // We're staging to exactly one location
+        File stageDir = null;
+        String stageCid = null;
+        if ((params.installFlags & PackageManager.INSTALL_INTERNAL) != 0) {
+            final boolean isEphemeral =
+                    (params.installFlags & PackageManager.INSTALL_EPHEMERAL) != 0;
+            stageDir = buildStageDir(params.volumeUuid, sessionId, isEphemeral);
+        } else {
+            stageCid = buildExternalStageCid(sessionId);
+        }
 
-            session = new PackageInstallerSession(mInternalCallback, mContext, mPm,
-                    mInstallThread.getLooper(), sessionId, userId, installerPackageName, callingUid,
-                    params, createdMillis, stageDir, stageCid, false, false);
+        session = new PackageInstallerSession(mInternalCallback, mContext, mPm,
+                mInstallThread.getLooper(), sessionId, userId, installerPackageName, callingUid,
+                params, createdMillis, stageDir, stageCid, false, false);
+
+        synchronized (mSessions) {
             mSessions.put(sessionId, session);
         }
 
@@ -765,8 +773,8 @@
         int sessionId;
         do {
             sessionId = mRandom.nextInt(Integer.MAX_VALUE - 1) + 1;
-            if (mSessions.get(sessionId) == null && mHistoricalSessions.get(sessionId) == null
-                    && !mLegacySessions.get(sessionId, false)) {
+            if (!mAllocatedSessions.get(sessionId, false)) {
+                mAllocatedSessions.put(sessionId, true);
                 return sessionId;
             }
         } while (n++ < 32);
@@ -862,13 +870,8 @@
                 IntentSender statusReceiver, int userId) {
         final int callingUid = Binder.getCallingUid();
         mPm.enforceCrossUserPermission(callingUid, userId, true, true, "uninstall");
-        boolean allowSilentUninstall = true;
         if ((callingUid != Process.SHELL_UID) && (callingUid != Process.ROOT_UID)) {
             mAppOps.checkPackage(callingUid, callerPackageName);
-            final String installerPackageName = mPm.getInstallerPackageName(packageName);
-            allowSilentUninstall = mPm.isOrphaned(packageName) ||
-                    (installerPackageName != null
-                            && installerPackageName.equals(callerPackageName));
         }
 
         // Check whether the caller is device owner, in which case we do it silently.
@@ -879,8 +882,8 @@
 
         final PackageDeleteObserverAdapter adapter = new PackageDeleteObserverAdapter(mContext,
                 statusReceiver, packageName, isDeviceOwner, userId);
-        if (allowSilentUninstall && mContext.checkCallingOrSelfPermission(
-                android.Manifest.permission.DELETE_PACKAGES) == PackageManager.PERMISSION_GRANTED) {
+        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DELETE_PACKAGES)
+                    == PackageManager.PERMISSION_GRANTED) {
             // Sweet, call straight through!
             mPm.deletePackage(packageName, adapter.getBinder(), userId, flags);
         } else if (isDeviceOwner) {
diff --git a/services/core/java/com/android/server/pm/PackageInstallerSession.java b/services/core/java/com/android/server/pm/PackageInstallerSession.java
index 6cdc40f..0b8a347 100644
--- a/services/core/java/com/android/server/pm/PackageInstallerSession.java
+++ b/services/core/java/com/android/server/pm/PackageInstallerSession.java
@@ -44,6 +44,7 @@
 import android.content.pm.PackageParser.PackageLite;
 import android.content.pm.PackageParser.PackageParserException;
 import android.content.pm.Signature;
+import android.os.Binder;
 import android.os.Bundle;
 import android.os.FileBridge;
 import android.os.FileUtils;
@@ -109,6 +110,7 @@
     final int installerUid;
     final SessionParams params;
     final long createdMillis;
+    final int defaultContainerGid;
 
     /** Staging location where client data is written. */
     final File stageDir;
@@ -199,13 +201,19 @@
     private final Handler.Callback mHandlerCallback = new Handler.Callback() {
         @Override
         public boolean handleMessage(Message msg) {
+            // Cache package manager data without the lock held
+            final PackageInfo pkgInfo = mPm.getPackageInfo(
+                    params.appPackageName, PackageManager.GET_SIGNATURES /*flags*/, userId);
+            final ApplicationInfo appInfo = mPm.getApplicationInfo(
+                    params.appPackageName, 0, userId);
+
             synchronized (mLock) {
                 if (msg.obj != null) {
                     mRemoteObserver = (IPackageInstallObserver2) msg.obj;
                 }
 
                 try {
-                    commitLocked();
+                    commitLocked(pkgInfo, appInfo);
                 } catch (PackageManagerException e) {
                     final String completeMsg = ExceptionUtils.getCompleteMessage(e);
                     Slog.e(TAG, "Commit of session " + sessionId + " failed: " + completeMsg);
@@ -264,6 +272,14 @@
         } else {
             mPermissionsAccepted = false;
         }
+        final long identity = Binder.clearCallingIdentity();
+        try {
+            final int uid = mPm.getPackageUid(PackageManagerService.DEFAULT_CONTAINER_PACKAGE,
+                    PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM);
+            defaultContainerGid = UserHandle.getSharedAppGid(uid);
+        } finally {
+            Binder.restoreCallingIdentity(identity);
+        }
     }
 
     public SessionInfo generateInfo() {
@@ -520,7 +536,8 @@
         mHandler.obtainMessage(MSG_COMMIT, adapter.getBinder()).sendToTarget();
     }
 
-    private void commitLocked() throws PackageManagerException {
+    private void commitLocked(PackageInfo pkgInfo, ApplicationInfo appInfo)
+            throws PackageManagerException {
         if (mDestroyed) {
             throw new PackageManagerException(INSTALL_FAILED_INTERNAL_ERROR, "Session destroyed");
         }
@@ -538,7 +555,7 @@
         // Verify that stage looks sane with respect to existing application.
         // This currently only ensures packageName, versionCode, and certificate
         // consistency.
-        validateInstallLocked();
+        validateInstallLocked(pkgInfo, appInfo);
 
         Preconditions.checkNotNull(mPackageName);
         Preconditions.checkNotNull(mSignatures);
@@ -650,7 +667,8 @@
      * Note that upgrade compatibility is still performed by
      * {@link PackageManagerService}.
      */
-    private void validateInstallLocked() throws PackageManagerException {
+    private void validateInstallLocked(PackageInfo pkgInfo, ApplicationInfo appInfo)
+            throws PackageManagerException {
         mPackageName = null;
         mVersionCode = -1;
         mSignatures = null;
@@ -729,10 +747,8 @@
 
         if (removeSplitList.size() > 0) {
             // validate split names marked for removal
-            final int flags = mSignatures == null ? PackageManager.GET_SIGNATURES : 0;
-            final PackageInfo pkg = mPm.getPackageInfo(params.appPackageName, flags, userId);
             for (String splitName : removeSplitList) {
-                if (!ArrayUtils.contains(pkg.splitNames, splitName)) {
+                if (!ArrayUtils.contains(pkgInfo.splitNames, splitName)) {
                     throw new PackageManagerException(INSTALL_FAILED_INVALID_APK,
                             "Split not found: " + splitName);
                 }
@@ -740,11 +756,11 @@
 
             // ensure we've got appropriate package name, version code and signatures
             if (mPackageName == null) {
-                mPackageName = pkg.packageName;
-                mVersionCode = pkg.versionCode;
+                mPackageName = pkgInfo.packageName;
+                mVersionCode = pkgInfo.versionCode;
             }
             if (mSignatures == null) {
-                mSignatures = pkg.signatures;
+                mSignatures = pkgInfo.signatures;
             }
         }
 
@@ -757,8 +773,7 @@
 
         } else {
             // Partial installs must be consistent with existing install
-            final ApplicationInfo app = mPm.getApplicationInfo(mPackageName, 0, userId);
-            if (app == null) {
+            if (appInfo == null) {
                 throw new PackageManagerException(INSTALL_FAILED_INVALID_APK,
                         "Missing existing base package for " + mPackageName);
             }
@@ -766,8 +781,8 @@
             final PackageLite existing;
             final ApkLite existingBase;
             try {
-                existing = PackageParser.parsePackageLite(new File(app.getCodePath()), 0);
-                existingBase = PackageParser.parseApkLite(new File(app.getBaseCodePath()),
+                existing = PackageParser.parsePackageLite(new File(appInfo.getCodePath()), 0);
+                existingBase = PackageParser.parseApkLite(new File(appInfo.getBaseCodePath()),
                         PackageParser.PARSE_COLLECT_CERTIFICATES);
             } catch (PackageParserException e) {
                 throw PackageManagerException.from(e);
@@ -777,7 +792,7 @@
 
             // Inherit base if not overridden
             if (mResolvedBaseFile == null) {
-                mResolvedBaseFile = new File(app.getBaseCodePath());
+                mResolvedBaseFile = new File(appInfo.getBaseCodePath());
                 mResolvedInheritedFiles.add(mResolvedBaseFile);
             }
 
@@ -794,7 +809,7 @@
             }
 
             // Inherit compiled oat directory.
-            final File packageInstallDir = (new File(app.getBaseCodePath())).getParentFile();
+            final File packageInstallDir = (new File(appInfo.getBaseCodePath())).getParentFile();
             mInheritedFilesBase = packageInstallDir;
             final File oatDir = new File(packageInstallDir, "oat");
             if (oatDir.exists()) {
@@ -822,7 +837,8 @@
         }
     }
 
-    private void assertApkConsistent(String tag, ApkLite apk) throws PackageManagerException {
+    private void assertApkConsistent(String tag, ApkLite apk)
+            throws PackageManagerException {
         if (!mPackageName.equals(apk.packageName)) {
             throw new PackageManagerException(INSTALL_FAILED_INVALID_APK, tag + " package "
                     + apk.packageName + " inconsistent with " + mPackageName);
@@ -1035,10 +1051,7 @@
                     "Failed to finalize container " + cid);
         }
 
-        final int uid = mPm.getPackageUid(PackageManagerService.DEFAULT_CONTAINER_PACKAGE,
-                PackageManager.MATCH_SYSTEM_ONLY, UserHandle.USER_SYSTEM);
-        final int gid = UserHandle.getSharedAppGid(uid);
-        if (!PackageHelper.fixSdPermissions(cid, gid, null)) {
+        if (!PackageHelper.fixSdPermissions(cid, defaultContainerGid, null)) {
             throw new PackageManagerException(INSTALL_FAILED_CONTAINER_ERROR,
                     "Failed to fix permissions on container " + cid);
         }
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index 12495eb..56a0173 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -35,7 +35,6 @@
 import static android.content.pm.PackageManager.INSTALL_EXTERNAL;
 import static android.content.pm.PackageManager.INSTALL_FAILED_ALREADY_EXISTS;
 import static android.content.pm.PackageManager.INSTALL_FAILED_CONFLICTING_PROVIDER;
-import static android.content.pm.PackageManager.INSTALL_FAILED_DEXOPT;
 import static android.content.pm.PackageManager.INSTALL_FAILED_DUPLICATE_PACKAGE;
 import static android.content.pm.PackageManager.INSTALL_FAILED_DUPLICATE_PERMISSION;
 import static android.content.pm.PackageManager.INSTALL_FAILED_EPHEMERAL_INVALID;
@@ -101,7 +100,6 @@
 import android.Manifest;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.annotation.UserIdInt;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
 import android.app.IActivityManager;
@@ -111,6 +109,7 @@
 import android.app.backup.IBackupManager;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.IIntentReceiver;
 import android.content.Intent;
@@ -124,6 +123,7 @@
 import android.content.pm.ComponentInfo;
 import android.content.pm.EphemeralApplicationInfo;
 import android.content.pm.EphemeralResolveInfo;
+import android.content.pm.EphemeralResolveInfo.EphemeralDigest;
 import android.content.pm.EphemeralResolveInfo.EphemeralResolveIntentInfo;
 import android.content.pm.FeatureInfo;
 import android.content.pm.IOnPermissionsChangeListener;
@@ -178,6 +178,7 @@
 import android.os.Message;
 import android.os.Parcel;
 import android.os.ParcelFileDescriptor;
+import android.os.PatternMatcher;
 import android.os.Process;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
@@ -196,6 +197,8 @@
 import android.os.storage.StorageManager;
 import android.os.storage.VolumeInfo;
 import android.os.storage.VolumeRecord;
+import android.provider.Settings.Global;
+import android.provider.Settings.Secure;
 import android.security.KeyStore;
 import android.security.SystemKeyStore;
 import android.system.ErrnoException;
@@ -210,6 +213,7 @@
 import android.util.Log;
 import android.util.LogPrinter;
 import android.util.MathUtils;
+import android.util.Pair;
 import android.util.PrintStreamPrinter;
 import android.util.Slog;
 import android.util.SparseArray;
@@ -357,13 +361,14 @@
     static final boolean DEBUG_DEXOPT = false;
 
     private static final boolean DEBUG_ABI_SELECTION = false;
-    private static final boolean DEBUG_EPHEMERAL = false;
+    private static final boolean DEBUG_EPHEMERAL = Build.IS_DEBUGGABLE;
     private static final boolean DEBUG_TRIAGED_MISSING = false;
     private static final boolean DEBUG_APP_DATA = false;
 
     static final boolean CLEAR_RUNTIME_PERMISSIONS_ON_UPGRADE = false;
 
-    private static final boolean DISABLE_EPHEMERAL_APPS = true;
+    private static final boolean DISABLE_EPHEMERAL_APPS = false;
+    private static final boolean HIDE_EPHEMERAL_APIS = true;
 
     private static final int RADIO_UID = Process.PHONE_UID;
     private static final int LOG_UID = Process.LOG_UID;
@@ -455,7 +460,18 @@
 
     private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";
 
+    private static final String PACKAGE_SCHEME = "package";
+
     private static final String VENDOR_OVERLAY_DIR = "/vendor/overlay";
+    /**
+     * If VENDOR_OVERLAY_SKU_PROPERTY is set, search for runtime resource overlay APKs in
+     * VENDOR_OVERLAY_DIR/<value of VENDOR_OVERLAY_SKU_PROPERTY> rather than in
+     * VENDOR_OVERLAY_DIR.
+     */
+    private static final String VENDOR_OVERLAY_SKU_PROPERTY = "ro.boot.vendor.overlay.sku";
+
+    private static int DEFAULT_EPHEMERAL_HASH_PREFIX_MASK = 0xFFFFF000;
+    private static int DEFAULT_EPHEMERAL_HASH_PREFIX_COUNT = 5;
 
     /** Permission grant: not grant the permission. */
     private static final int GRANT_DENIED = 1;
@@ -528,6 +544,10 @@
     final String[] mSeparateProcesses;
     final boolean mIsUpgrade;
     final boolean mIsPreNUpgrade;
+    final boolean mIsPreNMR1Upgrade;
+
+    @GuardedBy("mPackages")
+    private boolean mDexOptDialogShown;
 
     /** The location for ASEC container files on internal storage. */
     final String mAsecInternalPath;
@@ -617,7 +637,7 @@
     @GuardedBy("mPackages")
     final ArraySet<String> mFrozenPackages = new ArraySet<>();
 
-    final ProtectedPackages mProtectedPackages = new ProtectedPackages();
+    final ProtectedPackages mProtectedPackages;
 
     boolean mFirstBoot;
 
@@ -732,8 +752,7 @@
     final SparseArray<IntentFilterVerificationState> mIntentFilterVerificationStates
             = new SparseArray<IntentFilterVerificationState>();
 
-    final DefaultPermissionGrantPolicy mDefaultPermissionPolicy =
-            new DefaultPermissionGrantPolicy(this);
+    final DefaultPermissionGrantPolicy mDefaultPermissionPolicy;
 
     // List of packages names to keep cached, even if they are uninstalled for all users
     private List<String> mKeepUninstalledPackages;
@@ -1115,7 +1134,9 @@
 
     final @Nullable String mRequiredVerifierPackage;
     final @NonNull String mRequiredInstallerPackage;
+    final @NonNull String mRequiredUninstallerPackage;
     final @Nullable String mSetupWizardPackage;
+    final @Nullable String mStorageManagerPackage;
     final @NonNull String mServicesSystemSharedLibraryPackageName;
     final @NonNull String mSharedSystemSharedLibraryPackageName;
 
@@ -1958,10 +1979,6 @@
         PackageManagerService m = new PackageManagerService(context, installer,
                 factoryTest, onlyCore);
         m.enableSystemUserPackages();
-        // Disable any carrier apps. We do this very early in boot to prevent the apps from being
-        // disabled after already being started.
-        CarrierAppUtils.disableCarrierAppsUntilPrivileged(context.getOpPackageName(), m,
-                UserHandle.USER_SYSTEM);
         ServiceManager.addService("package", m);
         return m;
     }
@@ -2102,6 +2119,8 @@
         mSystemPermissions = systemConfig.getSystemPermissions();
         mAvailableFeatures = systemConfig.getAvailableFeatures();
 
+        mProtectedPackages = new ProtectedPackages(mContext);
+
         synchronized (mInstallLock) {
         // writer
         synchronized (mPackages) {
@@ -2112,6 +2131,8 @@
             mProcessLoggingHandler = new ProcessLoggingHandler();
             Watchdog.getInstance().addThread(mHandler, WATCHDOG_TIMEOUT);
 
+            mDefaultPermissionPolicy = new DefaultPermissionGrantPolicy(this);
+
             File dataDir = Environment.getDataDirectory();
             mAppInstallDir = new File(dataDir, "app");
             mAppLib32InstallDir = new File(dataDir, "app-lib");
@@ -2236,6 +2257,8 @@
             // as there is no profiling data available.
             mIsPreNUpgrade = mIsUpgrade && ver.sdkVersion < Build.VERSION_CODES.N;
 
+            mIsPreNMR1Upgrade = mIsUpgrade && ver.sdkVersion < Build.VERSION_CODES.N_MR1;
+
             // save off the names of pre-existing system packages prior to scanning; we don't
             // want to automatically grant runtime permissions for new system apps
             if (mPromoteSystemApps) {
@@ -2251,8 +2274,14 @@
             // Collect vendor overlay packages.
             // (Do this before scanning any apps.)
             // For security and version matching reason, only consider
-            // overlay packages if they reside in VENDOR_OVERLAY_DIR.
-            File vendorOverlayDir = new File(VENDOR_OVERLAY_DIR);
+            // overlay packages if they reside in the right directory.
+            File vendorOverlayDir;
+            String overlaySkuDir = SystemProperties.get(VENDOR_OVERLAY_SKU_PROPERTY);
+            if (!overlaySkuDir.isEmpty()) {
+                vendorOverlayDir = new File(VENDOR_OVERLAY_DIR, overlaySkuDir);
+            } else {
+                vendorOverlayDir = new File(VENDOR_OVERLAY_DIR);
+            }
             scanDirTracedLI(vendorOverlayDir, mDefParseFlags
                     | PackageParser.PARSE_IS_SYSTEM
                     | PackageParser.PARSE_IS_SYSTEM_DIR
@@ -2455,6 +2484,9 @@
             }
             mExpectingBetter.clear();
 
+            // Resolve the storage manager.
+            mStorageManagerPackage = getStorageManagerPackageName();
+
             // Resolve protected action filters. Only the setup wizard is allowed to
             // have a high priority filter for these actions.
             mSetupWizardPackage = getSetupWizardPackageName();
@@ -2618,6 +2650,7 @@
             if (!mOnlyCore) {
                 mRequiredVerifierPackage = getRequiredButNotReallyRequiredVerifierLPr();
                 mRequiredInstallerPackage = getRequiredInstallerLPr();
+                mRequiredUninstallerPackage = getRequiredUninstallerLPr();
                 mIntentFilterVerifierComponent = getIntentFilterVerifierComponentNameLPr();
                 mIntentFilterVerifier = new IntentVerifierProxy(mContext,
                         mIntentFilterVerifierComponent);
@@ -2628,6 +2661,7 @@
             } else {
                 mRequiredVerifierPackage = null;
                 mRequiredInstallerPackage = null;
+                mRequiredUninstallerPackage = null;
                 mIntentFilterVerifierComponent = null;
                 mIntentFilterVerifier = null;
                 mServicesSystemSharedLibraryPackageName = null;
@@ -2705,10 +2739,11 @@
                 UserHandle.USER_SYSTEM);
         if (matches.size() == 1) {
             return matches.get(0).getComponentInfo().packageName;
-        } else {
-            Log.e(TAG, "There should probably be exactly one verifier; found " + matches);
+        } else if (matches.size() == 0) {
+            Log.e(TAG, "There should probably be a verifier, but, none were found");
             return null;
         }
+        throw new RuntimeException("There must be exactly one verifier; found " + matches);
     }
 
     private @NonNull String getRequiredSharedLibraryLPr(String libraryName) {
@@ -2740,6 +2775,22 @@
         }
     }
 
+    private @NonNull String getRequiredUninstallerLPr() {
+        final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
+        intent.addCategory(Intent.CATEGORY_DEFAULT);
+        intent.setData(Uri.fromParts(PACKAGE_SCHEME, "foo.bar", null));
+
+        final ResolveInfo resolveInfo = resolveIntent(intent, null,
+                MATCH_SYSTEM_ONLY | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE,
+                UserHandle.USER_SYSTEM);
+        if (resolveInfo == null ||
+                mResolveActivity.name.equals(resolveInfo.getComponentInfo().name)) {
+            throw new RuntimeException("There must be exactly one uninstaller; found "
+                    + resolveInfo);
+        }
+        return resolveInfo.getComponentInfo().packageName;
+    }
+
     private @NonNull ComponentName getIntentFilterVerifierComponentNameLPr() {
         final Intent intent = new Intent(Intent.ACTION_INTENT_FILTER_NEEDS_VERIFICATION);
 
@@ -2771,17 +2822,20 @@
     private @Nullable ComponentName getEphemeralResolverLPr() {
         final String[] packageArray =
                 mContext.getResources().getStringArray(R.array.config_ephemeralResolverPackage);
-        if (packageArray.length == 0) {
+        if (packageArray.length == 0 && !Build.IS_DEBUGGABLE) {
             if (DEBUG_EPHEMERAL) {
                 Slog.d(TAG, "Ephemeral resolver NOT found; empty package list");
             }
             return null;
         }
 
+        final int resolveFlags =
+                MATCH_DIRECT_BOOT_AWARE
+                | MATCH_DIRECT_BOOT_UNAWARE
+                | (!Build.IS_DEBUGGABLE ? MATCH_SYSTEM_ONLY : 0);
         final Intent resolverIntent = new Intent(Intent.ACTION_RESOLVE_EPHEMERAL_PACKAGE);
         final List<ResolveInfo> resolvers = queryIntentServicesInternal(resolverIntent, null,
-                MATCH_SYSTEM_ONLY | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE,
-                UserHandle.USER_SYSTEM);
+                resolveFlags, UserHandle.USER_SYSTEM);
 
         final int N = resolvers.size();
         if (N == 0) {
@@ -2800,7 +2854,7 @@
             }
 
             final String packageName = info.serviceInfo.packageName;
-            if (!possiblePackages.contains(packageName)) {
+            if (!possiblePackages.contains(packageName) && !Build.IS_DEBUGGABLE) {
                 if (DEBUG_EPHEMERAL) {
                     Slog.d(TAG, "Ephemeral resolver not in allowed package list;"
                             + " pkg: " + packageName + ", info:" + info);
@@ -2825,9 +2879,12 @@
         intent.addCategory(Intent.CATEGORY_DEFAULT);
         intent.setDataAndType(Uri.fromFile(new File("foo.apk")), PACKAGE_MIME_TYPE);
 
+        final int resolveFlags =
+                MATCH_DIRECT_BOOT_AWARE
+                | MATCH_DIRECT_BOOT_UNAWARE
+                | (!Build.IS_DEBUGGABLE ? MATCH_SYSTEM_ONLY : 0);
         final List<ResolveInfo> matches = queryIntentActivitiesInternal(intent, PACKAGE_MIME_TYPE,
-                MATCH_SYSTEM_ONLY | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE,
-                UserHandle.USER_SYSTEM);
+                resolveFlags, UserHandle.USER_SYSTEM);
         if (matches.size() == 0) {
             return null;
         } else if (matches.size() == 1) {
@@ -3002,8 +3059,12 @@
 
         final PermissionsState permissionsState = ps.getPermissionsState();
 
-        final int[] gids = permissionsState.computeGids(userId);
-        final Set<String> permissions = permissionsState.getPermissions(userId);
+        // Compute GIDs only if requested
+        final int[] gids = (flags & PackageManager.GET_GIDS) == 0
+                ? EMPTY_INT_ARRAY : permissionsState.computeGids(userId);
+        // Compute granted permissions only if package has requested permissions
+        final Set<String> permissions = ArrayUtils.isEmpty(p.requestedPermissions)
+                ? Collections.<String>emptySet() : permissionsState.getPermissions(userId);
         final PackageUserState state = ps.readUserState(userId);
 
         return PackageParser.generatePackageInfo(p, gids, flags,
@@ -4718,20 +4779,6 @@
 
             final ResolveInfo bestChoice =
                     chooseBestActivity(intent, resolvedType, flags, query, userId);
-
-            if (isEphemeralAllowed(intent, query, userId)) {
-                Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "resolveEphemeral");
-                final EphemeralResolveInfo ai =
-                        getEphemeralResolveInfo(intent, resolvedType, userId);
-                if (ai != null) {
-                    if (DEBUG_EPHEMERAL) {
-                        Slog.v(TAG, "Returning an EphemeralResolveInfo");
-                    }
-                    bestChoice.ephemeralInstaller = mEphemeralInstallerInfo;
-                    bestChoice.ephemeralResolveInfo = ai;
-                }
-                Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
-            }
             return bestChoice;
         } finally {
             Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
@@ -4772,11 +4819,28 @@
                 false, false, false, userId);
     }
 
+    private boolean isEphemeralDisabled() {
+        // ephemeral apps have been disabled across the board
+        if (DISABLE_EPHEMERAL_APPS) {
+            return true;
+        }
+        // system isn't up yet; can't read settings, so, assume no ephemeral apps
+        if (!mSystemReady) {
+            return true;
+        }
+        // we can't get a content resolver until the system is ready; these checks must happen last
+        final ContentResolver resolver = mContext.getContentResolver();
+        if (Global.getInt(resolver, Global.ENABLE_EPHEMERAL_FEATURE, 1) == 0) {
+            return true;
+        }
+        return Secure.getInt(resolver, Secure.WEB_ACTION_ENABLED, 1) == 0;
+    }
 
     private boolean isEphemeralAllowed(
-            Intent intent, List<ResolveInfo> resolvedActivites, int userId) {
+            Intent intent, List<ResolveInfo> resolvedActivities, int userId,
+            boolean skipPackageCheck) {
         // Short circuit and return early if possible.
-        if (DISABLE_EPHEMERAL_APPS) {
+        if (isEphemeralDisabled()) {
             return false;
         }
         final int callingUser = UserHandle.getCallingUserId();
@@ -4789,18 +4853,21 @@
         if (intent.getComponent() != null) {
             return false;
         }
-        if (intent.getPackage() != null) {
+        if ((intent.getFlags() & Intent.FLAG_IGNORE_EPHEMERAL) != 0) {
+            return false;
+        }
+        if (!skipPackageCheck && intent.getPackage() != null) {
             return false;
         }
         final boolean isWebUri = hasWebURI(intent);
-        if (!isWebUri) {
+        if (!isWebUri || intent.getData().getHost() == null) {
             return false;
         }
         // Deny ephemeral apps if the user chose _ALWAYS or _ALWAYS_ASK for intent resolution.
         synchronized (mPackages) {
-            final int count = resolvedActivites.size();
+            final int count = (resolvedActivities == null ? 0 : resolvedActivities.size());
             for (int n = 0; n < count; n++) {
-                ResolveInfo info = resolvedActivites.get(n);
+                ResolveInfo info = resolvedActivities.get(n);
                 String packageName = info.activityInfo.packageName;
                 PackageSetting ps = mSettings.mPackages.get(packageName);
                 if (ps != null) {
@@ -4822,50 +4889,51 @@
         return true;
     }
 
-    private EphemeralResolveInfo getEphemeralResolveInfo(Intent intent, String resolvedType,
-            int userId) {
-        MessageDigest digest = null;
-        try {
-            digest = MessageDigest.getInstance(EphemeralResolveInfo.SHA_ALGORITHM);
-        } catch (NoSuchAlgorithmException e) {
-            // If we can't create a digest, ignore ephemeral apps.
-            return null;
-        }
-
-        final byte[] hostBytes = intent.getData().getHost().getBytes();
-        final byte[] digestBytes = digest.digest(hostBytes);
-        int shaPrefix =
-                digestBytes[0] << 24
-                | digestBytes[1] << 16
-                | digestBytes[2] << 8
-                | digestBytes[3] << 0;
+    private static EphemeralResolveInfo getEphemeralResolveInfo(
+            Context context, EphemeralResolverConnection resolverConnection, Intent intent,
+            String resolvedType, int userId, String packageName) {
+        final int ephemeralPrefixMask = Global.getInt(context.getContentResolver(),
+                Global.EPHEMERAL_HASH_PREFIX_MASK, DEFAULT_EPHEMERAL_HASH_PREFIX_MASK);
+        final int ephemeralPrefixCount = Global.getInt(context.getContentResolver(),
+                Global.EPHEMERAL_HASH_PREFIX_COUNT, DEFAULT_EPHEMERAL_HASH_PREFIX_COUNT);
+        final EphemeralDigest digest = new EphemeralDigest(intent.getData(), ephemeralPrefixMask,
+                ephemeralPrefixCount);
+        final int[] shaPrefix = digest.getDigestPrefix();
+        final byte[][] digestBytes = digest.getDigestBytes();
         final List<EphemeralResolveInfo> ephemeralResolveInfoList =
-                mEphemeralResolverConnection.getEphemeralResolveInfoList(shaPrefix);
+                resolverConnection.getEphemeralResolveInfoList(shaPrefix, ephemeralPrefixMask);
         if (ephemeralResolveInfoList == null || ephemeralResolveInfoList.size() == 0) {
             // No hash prefix match; there are no ephemeral apps for this domain.
             return null;
         }
-        for (int i = ephemeralResolveInfoList.size() - 1; i >= 0; --i) {
-            EphemeralResolveInfo ephemeralApplication = ephemeralResolveInfoList.get(i);
-            if (!Arrays.equals(digestBytes, ephemeralApplication.getDigestBytes())) {
-                continue;
-            }
-            final List<IntentFilter> filters = ephemeralApplication.getFilters();
-            // No filters; this should never happen.
-            if (filters.isEmpty()) {
-                continue;
-            }
-            // We have a domain match; resolve the filters to see if anything matches.
-            final EphemeralIntentResolver ephemeralResolver = new EphemeralIntentResolver();
-            for (int j = filters.size() - 1; j >= 0; --j) {
-                final EphemeralResolveIntentInfo intentInfo =
-                        new EphemeralResolveIntentInfo(filters.get(j), ephemeralApplication);
-                ephemeralResolver.addFilter(intentInfo);
-            }
-            List<EphemeralResolveInfo> matchedResolveInfoList = ephemeralResolver.queryIntent(
-                    intent, resolvedType, false /*defaultOnly*/, userId);
-            if (!matchedResolveInfoList.isEmpty()) {
-                return matchedResolveInfoList.get(0);
+
+        // Go in reverse order so we match the narrowest scope first.
+        for (int i = shaPrefix.length - 1; i >= 0 ; --i) {
+            for (EphemeralResolveInfo ephemeralApplication : ephemeralResolveInfoList) {
+                if (!Arrays.equals(digestBytes[i], ephemeralApplication.getDigestBytes())) {
+                    continue;
+                }
+                final List<IntentFilter> filters = ephemeralApplication.getFilters();
+                // No filters; this should never happen.
+                if (filters.isEmpty()) {
+                    continue;
+                }
+                if (packageName != null
+                        && !packageName.equals(ephemeralApplication.getPackageName())) {
+                    continue;
+                }
+                // We have a domain match; resolve the filters to see if anything matches.
+                final EphemeralIntentResolver ephemeralResolver = new EphemeralIntentResolver();
+                for (int j = filters.size() - 1; j >= 0; --j) {
+                    final EphemeralResolveIntentInfo intentInfo =
+                            new EphemeralResolveIntentInfo(filters.get(j), ephemeralApplication);
+                    ephemeralResolver.addFilter(intentInfo);
+                }
+                List<EphemeralResolveInfo> matchedResolveInfoList = ephemeralResolver.queryIntent(
+                        intent, resolvedType, false /*defaultOnly*/, userId);
+                if (!matchedResolveInfoList.isEmpty()) {
+                    return matchedResolveInfoList.get(0);
+                }
             }
         }
         // Hash or filter mis-match; no ephemeral apps for this domain.
@@ -5257,8 +5325,12 @@
         }
 
         // reader
+        boolean sortResult = false;
+        boolean addEphemeral = false;
+        boolean matchEphemeralPackage = false;
+        List<ResolveInfo> result;
+        final String pkgName = intent.getPackage();
         synchronized (mPackages) {
-            final String pkgName = intent.getPackage();
             if (pkgName == null) {
                 List<CrossProfileIntentFilter> matchingFilters =
                         getMatchingCrossProfileIntentFilters(intent, resolvedType, userId);
@@ -5266,15 +5338,16 @@
                 ResolveInfo xpResolveInfo  = querySkipCurrentProfileIntents(matchingFilters, intent,
                         resolvedType, flags, userId);
                 if (xpResolveInfo != null) {
-                    List<ResolveInfo> result = new ArrayList<ResolveInfo>(1);
-                    result.add(xpResolveInfo);
-                    return filterIfNotSystemUser(result, userId);
+                    List<ResolveInfo> xpResult = new ArrayList<ResolveInfo>(1);
+                    xpResult.add(xpResolveInfo);
+                    return filterIfNotSystemUser(xpResult, userId);
                 }
 
                 // Check for results in the current profile.
-                List<ResolveInfo> result = mActivities.queryIntent(
-                        intent, resolvedType, flags, userId);
-                result = filterIfNotSystemUser(result, userId);
+                result = filterIfNotSystemUser(mActivities.queryIntent(
+                        intent, resolvedType, flags, userId), userId);
+                addEphemeral =
+                        isEphemeralAllowed(intent, result, userId, false /*skipPackageCheck*/);
 
                 // Check for cross profile results.
                 boolean hasNonNegativePriorityResult = hasNonNegativePriority(result);
@@ -5286,7 +5359,7 @@
                             Collections.singletonList(xpResolveInfo), userId).size() > 0;
                     if (isVisibleToUser) {
                         result.add(xpResolveInfo);
-                        Collections.sort(result, mResolvePrioritySorter);
+                        sortResult = true;
                     }
                 }
                 if (hasWebURI(intent)) {
@@ -5302,28 +5375,61 @@
                             // in the result.
                             result.remove(xpResolveInfo);
                         }
-                        if (result.size() == 0) {
+                        if (result.size() == 0 && !addEphemeral) {
                             result.add(xpDomainInfo.resolveInfo);
                             return result;
                         }
-                    } else if (result.size() <= 1) {
-                        return result;
                     }
-                    result = filterCandidatesWithDomainPreferredActivitiesLPr(intent, flags, result,
-                            xpDomainInfo, userId);
-                    Collections.sort(result, mResolvePrioritySorter);
+                    if (result.size() > 1 || addEphemeral) {
+                        result = filterCandidatesWithDomainPreferredActivitiesLPr(
+                                intent, flags, result, xpDomainInfo, userId);
+                        sortResult = true;
+                    }
                 }
-                return result;
+            } else {
+                final PackageParser.Package pkg = mPackages.get(pkgName);
+                if (pkg != null) {
+                    result = filterIfNotSystemUser(
+                            mActivities.queryIntentForPackage(
+                                    intent, resolvedType, flags, pkg.activities, userId),
+                            userId);
+                } else {
+                    // the caller wants to resolve for a particular package; however, there
+                    // were no installed results, so, try to find an ephemeral result
+                    addEphemeral = isEphemeralAllowed(
+                            intent, null /*result*/, userId, true /*skipPackageCheck*/);
+                    matchEphemeralPackage = true;
+                    result = new ArrayList<ResolveInfo>();
+                }
             }
-            final PackageParser.Package pkg = mPackages.get(pkgName);
-            if (pkg != null) {
-                return filterIfNotSystemUser(
-                        mActivities.queryIntentForPackage(
-                                intent, resolvedType, flags, pkg.activities, userId),
-                        userId);
-            }
-            return new ArrayList<ResolveInfo>();
         }
+        if (addEphemeral) {
+            Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "resolveEphemeral");
+            final EphemeralResolveInfo ai = getEphemeralResolveInfo(
+                    mContext, mEphemeralResolverConnection, intent, resolvedType, userId,
+                    matchEphemeralPackage ? pkgName : null);
+            if (ai != null) {
+                if (DEBUG_EPHEMERAL) {
+                    Slog.v(TAG, "Adding ephemeral installer to the ResolveInfo list");
+                }
+                final ResolveInfo ephemeralInstaller = new ResolveInfo(mEphemeralInstallerInfo);
+                ephemeralInstaller.ephemeralResolveInfo = ai;
+                // make sure this resolver is the default
+                ephemeralInstaller.isDefault = true;
+                ephemeralInstaller.match = IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART
+                        | IntentFilter.MATCH_ADJUSTMENT_NORMAL;
+                // add a non-generic filter
+                ephemeralInstaller.filter = new IntentFilter(intent.getAction());
+                ephemeralInstaller.filter.addDataPath(
+                        intent.getData().getPath(), PatternMatcher.PATTERN_LITERAL);
+                result.add(ephemeralInstaller);
+            }
+            Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
+        }
+        if (sortResult) {
+            Collections.sort(result, mResolvePrioritySorter);
+        }
+        return result;
     }
 
     private static class CrossProfileDomainInfo {
@@ -6203,7 +6309,7 @@
 
     @Override
     public ParceledListSlice<EphemeralApplicationInfo> getEphemeralApplications(int userId) {
-        if (DISABLE_EPHEMERAL_APPS) {
+        if (HIDE_EPHEMERAL_APIS || isEphemeralDisabled()) {
             return null;
         }
 
@@ -6227,7 +6333,7 @@
         enforceCrossUserPermission(Binder.getCallingUid(), userId,
                 true /* requireFullPermission */, false /* checkShell */,
                 "isEphemeral");
-        if (DISABLE_EPHEMERAL_APPS) {
+        if (HIDE_EPHEMERAL_APIS || isEphemeralDisabled()) {
             return false;
         }
 
@@ -6245,7 +6351,7 @@
 
     @Override
     public byte[] getEphemeralApplicationCookie(String packageName, int userId) {
-        if (DISABLE_EPHEMERAL_APPS) {
+        if (HIDE_EPHEMERAL_APIS || isEphemeralDisabled()) {
             return null;
         }
 
@@ -6263,7 +6369,7 @@
 
     @Override
     public boolean setEphemeralApplicationCookie(String packageName, byte[] cookie, int userId) {
-        if (DISABLE_EPHEMERAL_APPS) {
+        if (HIDE_EPHEMERAL_APIS || isEphemeralDisabled()) {
             return true;
         }
 
@@ -6281,7 +6387,7 @@
 
     @Override
     public Bitmap getEphemeralApplicationIcon(String packageName, int userId) {
-        if (DISABLE_EPHEMERAL_APPS) {
+        if (HIDE_EPHEMERAL_APIS || isEphemeralDisabled()) {
             return null;
         }
 
@@ -6594,11 +6700,30 @@
         }
     }
 
+    private long getLastModifiedTime(PackageParser.Package pkg, File srcFile) {
+        if (srcFile.isDirectory()) {
+            final File baseFile = new File(pkg.baseCodePath);
+            long maxModifiedTime = baseFile.lastModified();
+            if (pkg.splitCodePaths != null) {
+                for (int i = pkg.splitCodePaths.length - 1; i >=0; --i) {
+                    final File splitFile = new File(pkg.splitCodePaths[i]);
+                    maxModifiedTime = Math.max(maxModifiedTime, splitFile.lastModified());
+                }
+            }
+            return maxModifiedTime;
+        }
+        return srcFile.lastModified();
+    }
+
     private void collectCertificatesLI(PackageSetting ps, PackageParser.Package pkg, File srcFile,
             final int policyFlags) throws PackageManagerException {
+        // When upgrading from pre-N MR1, verify the package time stamp using the package
+        // directory and not the APK file.
+        final long lastModifiedTime = mIsPreNMR1Upgrade
+                ? new File(pkg.codePath).lastModified() : getLastModifiedTime(pkg, srcFile);
         if (ps != null
                 && ps.codePath.equals(srcFile)
-                && ps.timeStamp == srcFile.lastModified()
+                && ps.timeStamp == lastModifiedTime
                 && !isCompatSignatureUpdateNeeded(pkg)
                 && !isRecoverSignatureUpdateNeeded(pkg)) {
             long mSigningKeySetId = ps.keySetData.getProperSigningKeySet();
@@ -6620,7 +6745,7 @@
             Slog.w(TAG, "PackageSetting for " + ps.name
                     + " is missing signatures.  Collecting certs again to recover them.");
         } else {
-            Log.i(TAG, srcFile.toString() + " changed; collecting certs");
+            Slog.i(TAG, srcFile.toString() + " changed; collecting certs");
         }
 
         try {
@@ -7045,26 +7170,25 @@
         try {
             IMountService ms = PackageHelper.getMountService();
             if (ms != null) {
-                final boolean isUpgrade = isUpgrade();
-                boolean doTrim = isUpgrade;
-                if (doTrim) {
-                    Slog.w(TAG, "Running disk maintenance immediately due to system update");
-                } else {
-                    final long interval = android.provider.Settings.Global.getLong(
-                            mContext.getContentResolver(),
-                            android.provider.Settings.Global.FSTRIM_MANDATORY_INTERVAL,
-                            DEFAULT_MANDATORY_FSTRIM_INTERVAL);
-                    if (interval > 0) {
-                        final long timeSinceLast = System.currentTimeMillis() - ms.lastMaintenance();
-                        if (timeSinceLast > interval) {
-                            doTrim = true;
-                            Slog.w(TAG, "No disk maintenance in " + timeSinceLast
-                                    + "; running immediately");
-                        }
+                boolean doTrim = false;
+                final long interval = android.provider.Settings.Global.getLong(
+                        mContext.getContentResolver(),
+                        android.provider.Settings.Global.FSTRIM_MANDATORY_INTERVAL,
+                        DEFAULT_MANDATORY_FSTRIM_INTERVAL);
+                if (interval > 0) {
+                    final long timeSinceLast = System.currentTimeMillis() - ms.lastMaintenance();
+                    if (timeSinceLast > interval) {
+                        doTrim = true;
+                        Slog.w(TAG, "No disk maintenance in " + timeSinceLast
+                                + "; running immediately");
                     }
                 }
                 if (doTrim) {
-                    if (!isFirstBoot()) {
+                    final boolean dexOptDialogShown;
+                    synchronized (mPackages) {
+                        dexOptDialogShown = mDexOptDialogShown;
+                    }
+                    if (!isFirstBoot() && dexOptDialogShown) {
                         try {
                             ActivityManagerNative.getDefault().showBootMessage(
                                     mContext.getResources().getString(
@@ -7158,6 +7282,22 @@
                                     numberOfPackagesVisited, numberOfPackagesToDexopt), true);
                 } catch (RemoteException e) {
                 }
+                synchronized (mPackages) {
+                    mDexOptDialogShown = true;
+                }
+            }
+
+            // If the OTA updates a system app which was previously preopted to a non-preopted state
+            // the app might end up being verified at runtime. That's because by default the apps
+            // are verify-profile but for preopted apps there's no profile.
+            // Do a hacky check to ensure that if we have no profiles (a reasonable indication
+            // that before the OTA the app was preopted) the app gets compiled with a non-profile
+            // filter (by default interpret-only).
+            // Note that at this stage unused apps are already filtered.
+            if (isSystemApp(pkg) &&
+                    DexFile.isProfileGuidedCompilerFilter(compilerFilter) &&
+                    !Environment.getReferenceProfile(pkg.packageName).exists()) {
+                compilerFilter = getNonProfileGuidedCompilerFilter(compilerFilter);
             }
 
             // If the OTA updates a system app which was previously preopted to a non-preopted state
@@ -7816,7 +7956,9 @@
         } catch (IOException ignore) {
         } finally {
             try {
-                jarFile.close();
+                if (jarFile != null) {
+                    jarFile.close();
+                }
             } catch (IOException ignore) {}
         }
         return false;
@@ -8250,7 +8392,7 @@
 
         final String pkgName = pkg.packageName;
 
-        final long scanFileTime = scanFile.lastModified();
+        final long scanFileTime = getLastModifiedTime(pkg, scanFile);
         final boolean forceDex = (scanFlags & SCAN_FORCE_DEX) != 0;
         pkg.applicationInfo.processName = fixProcessName(
                 pkg.applicationInfo.packageName,
@@ -8353,6 +8495,10 @@
             pkg.applicationInfo.flags |= ApplicationInfo.FLAG_FACTORY_TEST;
         }
 
+        if (isSystemApp(pkg)) {
+            pkgSetting.isOrphaned = true;
+        }
+
         ArrayList<PackageParser.Package> clientLibPkgs = null;
 
         if ((scanFlags & SCAN_CHECK_ONLY) != 0) {
@@ -8636,7 +8782,9 @@
             for (i=0; i<N; i++) {
                 PackageParser.PermissionGroup pg = pkg.permissionGroups.get(i);
                 PackageParser.PermissionGroup cur = mPermissionGroups.get(pg.info.name);
-                if (cur == null) {
+                final String curPackageName = cur == null ? null : cur.info.packageName;
+                final boolean isPackageUpdate = pg.info.packageName.equals(curPackageName);
+                if (cur == null || isPackageUpdate) {
                     mPermissionGroups.put(pg.info.name, pg);
                     if ((policyFlags&PackageParser.PARSE_CHATTY) != 0) {
                         if (r == null) {
@@ -8644,6 +8792,9 @@
                         } else {
                             r.append(' ');
                         }
+                        if (isPackageUpdate) {
+                            r.append("UPD:");
+                        }
                         r.append(pg.info.name);
                     }
                 } else {
@@ -9176,15 +9327,17 @@
         mEphemeralInstallerActivity.packageName = pkg.applicationInfo.packageName;
         mEphemeralInstallerActivity.processName = pkg.applicationInfo.packageName;
         mEphemeralInstallerActivity.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
-        mEphemeralInstallerActivity.flags = ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS |
-                ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
+        mEphemeralInstallerActivity.flags = ActivityInfo.FLAG_EXCLUDE_FROM_RECENTS
+                | ActivityInfo.FLAG_FINISH_ON_CLOSE_SYSTEM_DIALOGS;
         mEphemeralInstallerActivity.theme = 0;
         mEphemeralInstallerActivity.exported = true;
         mEphemeralInstallerActivity.enabled = true;
         mEphemeralInstallerInfo.activityInfo = mEphemeralInstallerActivity;
         mEphemeralInstallerInfo.priority = 0;
-        mEphemeralInstallerInfo.preferredOrder = 0;
-        mEphemeralInstallerInfo.match = 0;
+        mEphemeralInstallerInfo.preferredOrder = 1;
+        mEphemeralInstallerInfo.isDefault = true;
+        mEphemeralInstallerInfo.match = IntentFilter.MATCH_CATEGORY_SCHEME_SPECIFIC_PART
+                | IntentFilter.MATCH_ADJUSTMENT_NORMAL;
 
         if (DEBUG_EPHEMERAL) {
             Slog.d(TAG, "Set ephemeral installer activity: " + mEphemeralInstallerComponent);
@@ -11152,6 +11305,19 @@
 
     private static final class EphemeralIntentResolver
             extends IntentResolver<EphemeralResolveIntentInfo, EphemeralResolveInfo> {
+        /**
+         * The result that has the highest defined order. Ordering applies on a
+         * per-package basis. Mapping is from package name to Pair of order and
+         * EphemeralResolveInfo.
+         * <p>
+         * NOTE: This is implemented as a field variable for convenience and efficiency.
+         * By having a field variable, we're able to track filter ordering as soon as
+         * a non-zero order is defined. Otherwise, multiple loops across the result set
+         * would be needed to apply ordering. If the intent resolver becomes re-entrant,
+         * this needs to be contained entirely within {@link #filterResults()}.
+         */
+        final ArrayMap<String, Pair<Integer, EphemeralResolveInfo>> mOrderResult = new ArrayMap<>();
+
         @Override
         protected EphemeralResolveIntentInfo[] newArray(int size) {
             return new EphemeralResolveIntentInfo[size];
@@ -11168,7 +11334,51 @@
             if (!sUserManager.exists(userId)) {
                 return null;
             }
-            return info.getEphemeralResolveInfo();
+            final String packageName = info.getEphemeralResolveInfo().getPackageName();
+            final Integer order = info.getOrder();
+            final Pair<Integer, EphemeralResolveInfo> lastOrderResult =
+                    mOrderResult.get(packageName);
+            // ordering is enabled and this item's order isn't high enough
+            if (lastOrderResult != null && lastOrderResult.first >= order) {
+                return null;
+            }
+            final EphemeralResolveInfo res = info.getEphemeralResolveInfo();
+            if (order > 0) {
+                // non-zero order, enable ordering
+                mOrderResult.put(packageName, new Pair<>(order, res));
+            }
+            return res;
+        }
+
+        @Override
+        protected void filterResults(List<EphemeralResolveInfo> results) {
+            // only do work if ordering is enabled [most of the time it won't be]
+            if (mOrderResult.size() == 0) {
+                return;
+            }
+            int resultSize = results.size();
+            for (int i = 0; i < resultSize; i++) {
+                final EphemeralResolveInfo info = results.get(i);
+                final String packageName = info.getPackageName();
+                final Pair<Integer, EphemeralResolveInfo> savedInfo = mOrderResult.get(packageName);
+                if (savedInfo == null) {
+                    // package doesn't having ordering
+                    continue;
+                }
+                if (savedInfo.second == info) {
+                    // circled back to the highest ordered item; remove from order list
+                    mOrderResult.remove(savedInfo);
+                    if (mOrderResult.size() == 0) {
+                        // no more ordered items
+                        break;
+                    }
+                    continue;
+                }
+                // item has a worse order, remove it from the result list
+                results.remove(i);
+                resultSize--;
+                i--;
+            }
         }
     }
 
@@ -11237,7 +11447,7 @@
                     }
                     for (int id : resolvedUserIds) {
                         final Intent intent = new Intent(action,
-                                pkg != null ? Uri.fromParts("package", pkg, null) : null);
+                                pkg != null ? Uri.fromParts(PACKAGE_SCHEME, pkg, null) : null);
                         if (extras != null) {
                             intent.putExtras(extras);
                         }
@@ -11493,6 +11703,18 @@
                 if (pkgSetting == null) {
                     return false;
                 }
+                // Do not allow "android" is being disabled
+                if ("android".equals(packageName)) {
+                    Slog.w(TAG, "Cannot hide package: android");
+                    return false;
+                }
+                // Only allow protected packages to hide themselves.
+                if (hidden && !UserHandle.isSameApp(uid, pkgSetting.appId)
+                        && mProtectedPackages.isPackageStateProtected(userId, packageName)) {
+                    Slog.w(TAG, "Not hiding protected package: " + packageName);
+                    return false;
+                }
+
                 if (pkgSetting.getHidden(userId) != hidden) {
                     pkgSetting.setHidden(hidden, userId);
                     mSettings.writePackageRestrictionsLPr(userId);
@@ -11721,6 +11943,12 @@
             return false;
         }
 
+        if (packageName.equals(mRequiredUninstallerPackage)) {
+            Slog.w(TAG, "Cannot suspend/un-suspend package \"" + packageName
+                    + "\": required for package uninstallation");
+            return false;
+        }
+
         if (packageName.equals(mRequiredVerifierPackage)) {
             Slog.w(TAG, "Cannot suspend/un-suspend package \"" + packageName
                     + "\": required for package verification");
@@ -11733,6 +11961,12 @@
             return false;
         }
 
+        if (mProtectedPackages.isPackageStateProtected(userId, packageName)) {
+            Slog.w(TAG, "Cannot suspend/un-suspend package \"" + packageName
+                    + "\": protected package");
+            return false;
+        }
+
         return true;
     }
 
@@ -12266,6 +12500,9 @@
             public void run() {
                 for (int i = 0; i < mRunningInstalls.size(); i++) {
                     final PostInstallData data = mRunningInstalls.valueAt(i);
+                    if (data.res.returnCode != PackageManager.INSTALL_SUCCEEDED) {
+                        continue;
+                    }
                     if (pkgName.equals(data.res.pkg.applicationInfo.packageName)) {
                         // right package; but is it for the right user?
                         for (int uIndex = 0; uIndex < data.res.newUsers.length; uIndex++) {
@@ -15276,6 +15513,17 @@
         Preconditions.checkNotNull(packageName);
         Preconditions.checkNotNull(observer);
         final int uid = Binder.getCallingUid();
+        if (!isOrphaned(packageName)
+                && !isCallerAllowedToSilentlyUninstall(uid, packageName)) {
+            try {
+                final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
+                intent.setData(Uri.fromParts(PACKAGE_SCHEME, packageName, null));
+                intent.putExtra(PackageInstaller.EXTRA_CALLBACK, observer.asBinder());
+                observer.onUserActionRequired(intent);
+            } catch (RemoteException re) {
+            }
+            return;
+        }
         final boolean deleteAllUsers = (deleteFlags & PackageManager.DELETE_ALL_USERS) != 0;
         final int[] users = deleteAllUsers ? sUserManager.getUserIds() : new int[]{ userId };
         if (UserHandle.getUserId(uid) != userId || (deleteAllUsers && users.length > 1)) {
@@ -15344,6 +15592,37 @@
         });
     }
 
+    private boolean isCallerAllowedToSilentlyUninstall(int callingUid, String pkgName) {
+        if (callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID
+              || callingUid == Process.SYSTEM_UID) {
+            return true;
+        }
+        final int callingUserId = UserHandle.getUserId(callingUid);
+        // If the caller installed the pkgName, then allow it to silently uninstall.
+        if (callingUid == getPackageUid(getInstallerPackageName(pkgName), 0, callingUserId)) {
+            return true;
+        }
+
+        // Allow package verifier to silently uninstall.
+        if (mRequiredVerifierPackage != null &&
+                callingUid == getPackageUid(mRequiredVerifierPackage, 0, callingUserId)) {
+            return true;
+        }
+
+        // Allow package uninstaller to silently uninstall.
+        if (mRequiredUninstallerPackage != null &&
+                callingUid == getPackageUid(mRequiredUninstallerPackage, 0, callingUserId)) {
+            return true;
+        }
+
+        // Allow storage manager to silently uninstall.
+        if (mStorageManagerPackage != null &&
+                callingUid == getPackageUid(mStorageManagerPackage, 0, callingUserId)) {
+            return true;
+        }
+        return false;
+    }
+
     private int[] getBlockUninstallForUsers(String packageName, int[] userIds) {
         int[] result = EMPTY_INT_ARRAY;
         for (int userId : userIds) {
@@ -16270,8 +16549,9 @@
         enforceCrossUserPermission(Binder.getCallingUid(), userId,
                 true /* requireFullPermission */, false /* checkShell */, "clear application data");
 
-        if (mProtectedPackages.canPackageBeWiped(userId, packageName)) {
-            throw new SecurityException("Cannot clear data for a device owner or a profile owner");
+        if (mProtectedPackages.isPackageDataProtected(userId, packageName)) {
+            throw new SecurityException("Cannot clear data for a protected package: "
+                    + packageName);
         }
         // Queue up an async operation since the package deletion may take a little while.
         mHandler.post(new Runnable() {
@@ -16663,9 +16943,28 @@
             filter.dump(new LogPrinter(Log.INFO, TAG), "  ");
             pir.addFilter(new PreferredActivity(filter, match, set, activity, always));
             scheduleWritePackageRestrictionsLocked(userId);
+            postPreferredActivityChangedBroadcast(userId);
         }
     }
 
+    private void postPreferredActivityChangedBroadcast(int userId) {
+        mHandler.post(() -> {
+            final IActivityManager am = ActivityManagerNative.getDefault();
+            if (am == null) {
+                return;
+            }
+
+            final Intent intent = new Intent(Intent.ACTION_PREFERRED_ACTIVITY_CHANGED);
+            intent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
+            try {
+                am.broadcastIntent(null, intent, null, null,
+                        0, null, null, null, android.app.AppOpsManager.OP_NONE,
+                        null, false, false, userId);
+            } catch (RemoteException e) {
+            }
+        });
+    }
+
     @Override
     public void replacePreferredActivity(IntentFilter filter, int match,
             ComponentName[] set, ComponentName activity, int userId) {
@@ -16817,6 +17116,9 @@
                 changed = true;
             }
         }
+        if (changed) {
+            postPreferredActivityChangedBroadcast(userId);
+        }
         return changed;
     }
 
@@ -16930,6 +17232,7 @@
             mSettings.editPersistentPreferredActivitiesLPw(userId).addFilter(
                     new PersistentPreferredActivity(filter, activity));
             scheduleWritePackageRestrictionsLocked(userId);
+            postPreferredActivityChangedBroadcast(userId);
         }
     }
 
@@ -16972,6 +17275,7 @@
 
             if (changed) {
                 scheduleWritePackageRestrictionsLocked(userId);
+                postPreferredActivityChangedBroadcast(userId);
             }
         }
     }
@@ -17457,6 +17761,7 @@
     private Intent getHomeIntent() {
         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
+        intent.addCategory(Intent.CATEGORY_DEFAULT);
         return intent;
     }
 
@@ -17530,6 +17835,22 @@
         }
     }
 
+    private @Nullable String getStorageManagerPackageName() {
+        final Intent intent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
+
+        final List<ResolveInfo> matches = queryIntentActivitiesInternal(intent, null,
+                MATCH_SYSTEM_ONLY | MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE
+                        | MATCH_DISABLED_COMPONENTS,
+                UserHandle.myUserId());
+        if (matches.size() == 1) {
+            return matches.get(0).getComponentInfo().packageName;
+        } else {
+            Slog.e(TAG, "There should probably be exactly one storage manager; found "
+                    + matches.size() + ": matches=" + matches);
+            return null;
+        }
+    }
+
     @Override
     public void setApplicationEnabledSetting(String appPackageName,
             int newState, int flags, int userId, String callingPackage) {
@@ -17597,9 +17918,9 @@
                         + Binder.getCallingPid()
                         + ", uid=" + uid + ", package uid=" + pkgSetting.appId);
             }
-            // Don't allow changing profile and device owners.
-            if (mProtectedPackages.canPackageStateBeChanged(userId, packageName)) {
-                throw new SecurityException("Cannot disable a device owner or a profile owner");
+            // Don't allow changing protected packages.
+            if (mProtectedPackages.isPackageStateProtected(userId, packageName)) {
+                throw new SecurityException("Cannot disable a protected package: " + packageName);
             }
         }
 
@@ -17816,6 +18137,11 @@
     public void systemReady() {
         mSystemReady = true;
 
+        // Disable any carrier apps. We do this very early in boot to prevent the apps from being
+        // disabled after already being started.
+        CarrierAppUtils.disableCarrierAppsUntilPrivileged(mContext.getOpPackageName(), this,
+                mContext.getContentResolver(), UserHandle.USER_SYSTEM);
+
         // Read the compatibilty setting when the system is ready.
         boolean compatibilityModeEnabled = android.provider.Settings.Global.getInt(
                 mContext.getContentResolver(),
@@ -17871,6 +18197,13 @@
             mDefaultPermissionPolicy.grantDefaultPermissions(userId);
         }
 
+        // If we did not grant default permissions, we preload from this the
+        // default permission exceptions lazily to ensure we don't hit the
+        // disk on a new user creation.
+        if (grantPermissionsUserIds == EMPTY_INT_ARRAY) {
+            mDefaultPermissionPolicy.scheduleReadDefaultPermissionExceptions();
+        }
+
         // Kick off any messages waiting for system ready
         if (mPostSystemReadyMessages != null) {
             for (Message msg : mPostSystemReadyMessages) {
@@ -19481,8 +19814,6 @@
         final File ceDir = Environment.getDataUserCeDirectory(volumeUuid, userId);
         final File deDir = Environment.getDataUserDeDirectory(volumeUuid, userId);
 
-        boolean restoreconNeeded = false;
-
         // First look for stale data that doesn't belong, and check if things
         // have changed since we did our last restorecon
         if ((flags & StorageManager.FLAG_STORAGE_CE) != 0) {
@@ -19493,8 +19824,6 @@
                                 + " was still locked; this would have caused massive data loss!");
             }
 
-            restoreconNeeded |= SELinuxMMAC.isRestoreconNeeded(ceDir);
-
             final File[] files = FileUtils.listFilesOrEmpty(ceDir);
             for (File file : files) {
                 final String packageName = file.getName();
@@ -19512,8 +19841,6 @@
             }
         }
         if ((flags & StorageManager.FLAG_STORAGE_DE) != 0) {
-            restoreconNeeded |= SELinuxMMAC.isRestoreconNeeded(deDir);
-
             final File[] files = FileUtils.listFilesOrEmpty(deDir);
             for (File file : files) {
                 final String packageName = file.getName();
@@ -19548,29 +19875,19 @@
             }
 
             if (ps.getInstalled(userId)) {
-                prepareAppDataLIF(ps.pkg, userId, flags, restoreconNeeded);
+                prepareAppDataLIF(ps.pkg, userId, flags);
 
                 if (maybeMigrateAppDataLIF(ps.pkg, userId)) {
                     // We may have just shuffled around app data directories, so
                     // prepare them one more time
-                    prepareAppDataLIF(ps.pkg, userId, flags, restoreconNeeded);
+                    prepareAppDataLIF(ps.pkg, userId, flags);
                 }
 
                 preparedCount++;
             }
         }
 
-        if (restoreconNeeded) {
-            if ((flags & StorageManager.FLAG_STORAGE_CE) != 0) {
-                SELinuxMMAC.setRestoreconDone(ceDir);
-            }
-            if ((flags & StorageManager.FLAG_STORAGE_DE) != 0) {
-                SELinuxMMAC.setRestoreconDone(deDir);
-            }
-        }
-
-        Slog.v(TAG, "reconcileAppsData finished " + preparedCount
-                + " packages; restoreconNeeded was " + restoreconNeeded);
+        Slog.v(TAG, "reconcileAppsData finished " + preparedCount + " packages");
     }
 
     /**
@@ -19605,9 +19922,8 @@
             }
 
             if (ps.getInstalled(user.id)) {
-                // Whenever an app changes, force a restorecon of its data
                 // TODO: when user data is locked, mark that we're still dirty
-                prepareAppDataLIF(pkg, user.id, flags, true);
+                prepareAppDataLIF(pkg, user.id, flags);
             }
         }
     }
@@ -19620,24 +19936,22 @@
      * will try recovering system apps by wiping data; third-party app data is
      * left intact.
      */
-    private void prepareAppDataLIF(PackageParser.Package pkg, int userId, int flags,
-            boolean restoreconNeeded) {
+    private void prepareAppDataLIF(PackageParser.Package pkg, int userId, int flags) {
         if (pkg == null) {
             Slog.wtf(TAG, "Package was null!", new Throwable());
             return;
         }
-        prepareAppDataLeafLIF(pkg, userId, flags, restoreconNeeded);
+        prepareAppDataLeafLIF(pkg, userId, flags);
         final int childCount = (pkg.childPackages != null) ? pkg.childPackages.size() : 0;
         for (int i = 0; i < childCount; i++) {
-            prepareAppDataLeafLIF(pkg.childPackages.get(i), userId, flags, restoreconNeeded);
+            prepareAppDataLeafLIF(pkg.childPackages.get(i), userId, flags);
         }
     }
 
-    private void prepareAppDataLeafLIF(PackageParser.Package pkg, int userId, int flags,
-            boolean restoreconNeeded) {
+    private void prepareAppDataLeafLIF(PackageParser.Package pkg, int userId, int flags) {
         if (DEBUG_APP_DATA) {
             Slog.v(TAG, "prepareAppData for " + pkg.packageName + " u" + userId + " 0x"
-                    + Integer.toHexString(flags) + (restoreconNeeded ? " restoreconNeeded" : ""));
+                    + Integer.toHexString(flags));
         }
 
         final String volumeUuid = pkg.volumeUuid;
@@ -19667,15 +19981,6 @@
             }
         }
 
-        if (restoreconNeeded) {
-            try {
-                mInstaller.restoreconAppData(volumeUuid, packageName, userId, flags, appId,
-                        app.seinfo);
-            } catch (InstallerException e) {
-                Slog.e(TAG, "Failed to restorecon for " + packageName + ": " + e);
-            }
-        }
-
         if ((flags & StorageManager.FLAG_STORAGE_CE) != 0) {
             try {
                 // CE storage is unlocked right now, so read out the inode and
@@ -20266,12 +20571,7 @@
         }
     }
 
-    void onBeforeUserStartUninitialized(final int userId) {
-        synchronized (mPackages) {
-            if (mSettings.areDefaultRuntimePermissionsGrantedLPr(userId)) {
-                return;
-            }
-        }
+    void onNewUserCreated(final int userId) {
         mDefaultPermissionPolicy.grantDefaultPermissions(userId);
         // If permission review for legacy apps is required, we represent
         // dagerous permissions for such apps as always granted runtime
@@ -20772,9 +21072,15 @@
         }
 
         @Override
-        public boolean canPackageBeWiped(int userId, String packageName) {
-            return mProtectedPackages.canPackageBeWiped(userId,
-                    packageName);
+        public boolean isPackageDataProtected(int userId, String packageName) {
+            return mProtectedPackages.isPackageDataProtected(userId, packageName);
+        }
+
+        @Override
+        public boolean wasPackageEverLaunched(String packageName, int userId) {
+            synchronized (mPackages) {
+                return mSettings.wasPackageEverLaunchedLPr(packageName, userId);
+            }
         }
     }
 
diff --git a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
index 251b277..b6cecc9 100644
--- a/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
+++ b/services/core/java/com/android/server/pm/PackageManagerShellCommand.java
@@ -1099,8 +1099,9 @@
 
         try {
             mInterface.setHomeActivity(componentName, userId);
+            pw.println("Success");
             return 0;
-        } catch (RemoteException e) {
+        } catch (Exception e) {
             pw.println(e.toString());
             return 1;
         }
@@ -1236,7 +1237,7 @@
                     PackageInstaller.STATUS_FAILURE);
             if (status == PackageInstaller.STATUS_SUCCESS) {
                 if (logSuccess) {
-                    System.out.println("Success");
+                    pw.println("Success");
                 }
             } else {
                 pw.println("Failure ["
diff --git a/services/core/java/com/android/server/pm/PermissionsState.java b/services/core/java/com/android/server/pm/PermissionsState.java
index 007b738..8f9968ec 100644
--- a/services/core/java/com/android/server/pm/PermissionsState.java
+++ b/services/core/java/com/android/server/pm/PermissionsState.java
@@ -274,7 +274,7 @@
             return Collections.emptySet();
         }
 
-        Set<String> permissions = new ArraySet<>();
+        Set<String> permissions = new ArraySet<>(mPermissions.size());
 
         final int permissionCount = mPermissions.size();
         for (int i = 0; i < permissionCount; i++) {
@@ -282,6 +282,7 @@
 
             if (hasInstallPermission(permission)) {
                 permissions.add(permission);
+                continue;
             }
 
             if (userId != UserHandle.USER_ALL) {
diff --git a/services/core/java/com/android/server/pm/ProtectedPackages.java b/services/core/java/com/android/server/pm/ProtectedPackages.java
index 7bdea18..e67364a 100644
--- a/services/core/java/com/android/server/pm/ProtectedPackages.java
+++ b/services/core/java/com/android/server/pm/ProtectedPackages.java
@@ -16,10 +16,15 @@
 
 package com.android.server.pm;
 
+import android.annotation.Nullable;
 import android.annotation.UserIdInt;
+import android.content.Context;
 import android.os.UserHandle;
 import android.util.SparseArray;
 
+import com.android.internal.R;
+import com.android.internal.annotations.GuardedBy;
+
 /**
  * Manages package names that need special protection.
  *
@@ -29,61 +34,87 @@
  */
 public class ProtectedPackages {
     @UserIdInt
+    @GuardedBy("this")
     private int mDeviceOwnerUserId;
 
+    @Nullable
+    @GuardedBy("this")
     private String mDeviceOwnerPackage;
 
+    @Nullable
+    @GuardedBy("this")
     private SparseArray<String> mProfileOwnerPackages;
 
-    private final Object mLock = new Object();
+    @Nullable
+    @GuardedBy("this")
+    private final String mDeviceProvisioningPackage;
+
+    private final Context mContext;
+
+    public ProtectedPackages(Context context) {
+        mContext = context;
+        mDeviceProvisioningPackage = mContext.getResources().getString(
+                R.string.config_deviceProvisioningPackage);
+    }
 
     /**
      * Sets the device/profile owner information.
      */
-    public void setDeviceAndProfileOwnerPackages(
+    public synchronized void setDeviceAndProfileOwnerPackages(
             int deviceOwnerUserId, String deviceOwnerPackage,
             SparseArray<String> profileOwnerPackages) {
-        synchronized (mLock) {
-            mDeviceOwnerUserId = deviceOwnerUserId;
-            mDeviceOwnerPackage =
-                    (deviceOwnerUserId == UserHandle.USER_NULL) ? null : deviceOwnerPackage;
-            mProfileOwnerPackages = (profileOwnerPackages == null) ? null
-                    : profileOwnerPackages.clone();
-        }
+        mDeviceOwnerUserId = deviceOwnerUserId;
+        mDeviceOwnerPackage =
+                (deviceOwnerUserId == UserHandle.USER_NULL) ? null : deviceOwnerPackage;
+        mProfileOwnerPackages = (profileOwnerPackages == null) ? null
+                : profileOwnerPackages.clone();
     }
 
-    private boolean hasDeviceOwnerOrProfileOwner(int userId, String packageName) {
+    private synchronized boolean hasDeviceOwnerOrProfileOwner(int userId, String packageName) {
         if (packageName == null) {
             return false;
         }
-        synchronized (mLock) {
-            if (mDeviceOwnerPackage != null) {
-                if ((mDeviceOwnerUserId == userId)
-                        && (packageName.equals(mDeviceOwnerPackage))) {
-                    return true;
-                }
+        if (mDeviceOwnerPackage != null) {
+            if ((mDeviceOwnerUserId == userId)
+                    && (packageName.equals(mDeviceOwnerPackage))) {
+                return true;
             }
-            if (mProfileOwnerPackages != null) {
-                if (packageName.equals(mProfileOwnerPackages.get(userId))) {
-                    return true;
-                }
+        }
+        if (mProfileOwnerPackages != null) {
+            if (packageName.equals(mProfileOwnerPackages.get(userId))) {
+                return true;
             }
         }
         return false;
     }
 
     /**
-     * Whether a package or the components in a package's enabled state can be changed
-     * by other callers than itself.
+     * Returns {@code true} if a given package is protected. Otherwise, returns {@code false}.
+     *
+     * <p>A protected package means that, apart from the package owner, no system or privileged apps
+     * can modify its data or package state.
      */
-    public boolean canPackageStateBeChanged(@UserIdInt int userId, String packageName) {
-        return hasDeviceOwnerOrProfileOwner(userId, packageName);
+    private synchronized boolean isProtectedPackage(String packageName) {
+        return packageName != null && packageName.equals(mDeviceProvisioningPackage);
     }
 
     /**
-     * Whether a package's data be cleared.
+     * Returns {@code true} if a given package's state is protected. Otherwise, returns
+     * {@code false}.
+     *
+     * <p>This is not applicable if the caller is the package owner.
      */
-    public boolean canPackageBeWiped(@UserIdInt int userId, String packageName) {
-        return hasDeviceOwnerOrProfileOwner(userId, packageName);
+    public boolean isPackageStateProtected(@UserIdInt int userId, String packageName) {
+        return hasDeviceOwnerOrProfileOwner(userId, packageName)
+                || isProtectedPackage(packageName);
+    }
+
+    /**
+     * Returns {@code true} if a given package's data is protected. Otherwise, returns
+     * {@code false}.
+     */
+    public boolean isPackageDataProtected(@UserIdInt int userId, String packageName) {
+        return hasDeviceOwnerOrProfileOwner(userId, packageName)
+                || isProtectedPackage(packageName);
     }
 }
diff --git a/services/core/java/com/android/server/pm/SELinuxMMAC.java b/services/core/java/com/android/server/pm/SELinuxMMAC.java
index 5c46ca0..2176eb1 100644
--- a/services/core/java/com/android/server/pm/SELinuxMMAC.java
+++ b/services/core/java/com/android/server/pm/SELinuxMMAC.java
@@ -19,9 +19,6 @@
 import android.content.pm.PackageParser;
 import android.content.pm.Signature;
 import android.os.Environment;
-import android.system.ErrnoException;
-import android.system.Os;
-import android.system.OsConstants;
 import android.util.Slog;
 import android.util.Xml;
 
@@ -33,10 +30,7 @@
 import java.io.File;
 import java.io.FileReader;
 import java.io.IOException;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -64,22 +58,10 @@
     // to synchronize access during policy load and access attempts.
     private static List<Policy> sPolicies = new ArrayList<>();
 
-    /** Path to version on rootfs */
-    private static final File VERSION_FILE = new File("/selinux_version");
-
     /** Path to MAC permissions on system image */
     private static final File MAC_PERMISSIONS = new File(Environment.getRootDirectory(),
             "/etc/security/mac_permissions.xml");
 
-    /** Path to app contexts on rootfs */
-    private static final File SEAPP_CONTEXTS = new File("/seapp_contexts");
-
-    /** Calculated hash of {@link #SEAPP_CONTEXTS} */
-    private static final byte[] SEAPP_CONTEXTS_HASH = returnHash(SEAPP_CONTEXTS);
-
-    /** Attribute where {@link #SEAPP_CONTEXTS_HASH} is stored */
-    private static final String XATTR_SEAPP_HASH = "user.seapp_hash";
-
     // Append privapp to existing seinfo label
     private static final String PRIVILEGED_APP_STR = ":privapp";
 
@@ -310,60 +292,6 @@
                     "seinfo=" + pkg.applicationInfo.seinfo);
         }
     }
-
-    /**
-     * Determines if a recursive restorecon on the given package data directory
-     * is needed. It does this by comparing the SHA-1 of the seapp_contexts file
-     * against the stored hash in an xattr.
-     * <p>
-     * Note that the xattr isn't in the 'security' namespace, so this should
-     * only be run on directories owned by the system.
-     *
-     * @return Returns true if the restorecon should occur or false otherwise.
-     */
-    public static boolean isRestoreconNeeded(File file) {
-        try {
-            final byte[] buf = Os.getxattr(file.getAbsolutePath(), XATTR_SEAPP_HASH);
-            if ((buf.length == 20) && Arrays.equals(SEAPP_CONTEXTS_HASH, buf)) {
-                return false;
-            }
-        } catch (ErrnoException e) {
-            if (e.errno != OsConstants.ENODATA) {
-                Slog.e(TAG, "Failed to read seapp hash for " + file, e);
-            }
-        }
-
-        return true;
-    }
-
-    /**
-     * Stores the SHA-1 of the seapp_contexts into an xattr.
-     * <p>
-     * Note that the xattr isn't in the 'security' namespace, so this should
-     * only be run on directories owned by the system.
-     */
-    public static void setRestoreconDone(File file) {
-        try {
-            Os.setxattr(file.getAbsolutePath(), XATTR_SEAPP_HASH, SEAPP_CONTEXTS_HASH, 0);
-        } catch (ErrnoException e) {
-            Slog.e(TAG, "Failed to persist seapp hash in " + file, e);
-        }
-    }
-
-    /**
-     * Return the SHA-1 of a file.
-     *
-     * @param file The path to the file given as a string.
-     * @return Returns the SHA-1 of the file as a byte array.
-     */
-    private static byte[] returnHash(File file) {
-        try {
-            final byte[] contents = IoUtils.readFileAsByteArray(file.getAbsolutePath());
-            return MessageDigest.getInstance("SHA-1").digest(contents);
-        } catch (IOException | NoSuchAlgorithmException e) {
-            throw new RuntimeException(e);
-        }
-    }
 }
 
 /**
diff --git a/services/core/java/com/android/server/pm/Settings.java b/services/core/java/com/android/server/pm/Settings.java
index dfd6dfe..b0c536f 100644
--- a/services/core/java/com/android/server/pm/Settings.java
+++ b/services/core/java/com/android/server/pm/Settings.java
@@ -4011,7 +4011,7 @@
         file.delete();
         removeCrossProfileIntentFiltersLPw(userId);
 
-        mRuntimePermissionsPersistence.onUserRemoved(userId);
+        mRuntimePermissionsPersistence.onUserRemovedLPw(userId);
 
         writePackageListLPr();
     }
@@ -4154,6 +4154,14 @@
         return pkg.getCurrentEnabledStateLPr(classNameStr, userId);
     }
 
+    boolean wasPackageEverLaunchedLPr(String packageName, int userId) {
+        final PackageSetting pkgSetting = mPackages.get(packageName);
+        if (pkgSetting == null) {
+            throw new IllegalArgumentException("Unknown package: " + packageName);
+        }
+        return !pkgSetting.getNotLaunched(userId);
+    }
+
     boolean setPackageStoppedStateLPw(PackageManagerService pm, String packageName,
             boolean stopped, boolean allowedByPermission, int uid, int userId) {
         int appId = UserHandle.getAppId(uid);
@@ -5108,7 +5116,7 @@
             }
         }
 
-        private void onUserRemoved(int userId) {
+        private void onUserRemovedLPw(int userId) {
             // Make sure we do not
             mHandler.removeMessages(userId);
 
@@ -5119,6 +5127,9 @@
             for (SettingBase sb : mSharedUsers.values()) {
                 revokeRuntimePermissionsAndClearFlags(sb, userId);
             }
+
+            mDefaultPermissionsGranted.delete(userId);
+            mFingerprints.remove(userId);
         }
 
         private void revokeRuntimePermissionsAndClearFlags(SettingBase sb, int userId) {
diff --git a/services/core/java/com/android/server/pm/ShortcutLauncher.java b/services/core/java/com/android/server/pm/ShortcutLauncher.java
index 76d47a8..2af1bcb 100644
--- a/services/core/java/com/android/server/pm/ShortcutLauncher.java
+++ b/services/core/java/com/android/server/pm/ShortcutLauncher.java
@@ -17,6 +17,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.UserIdInt;
+import android.content.pm.PackageInfo;
 import android.content.pm.ShortcutInfo;
 import android.util.ArrayMap;
 import android.util.ArraySet;
@@ -25,6 +26,8 @@
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.server.pm.ShortcutUser.PackageWithUser;
 
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 import org.xmlpull.v1.XmlSerializer;
@@ -36,6 +39,8 @@
 
 /**
  * Launcher information used by {@link ShortcutService}.
+ *
+ * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
  */
 class ShortcutLauncher extends ShortcutPackageItem {
     private static final String TAG = ShortcutService.TAG;
@@ -80,26 +85,31 @@
      * Called when the new package can't receive the backup, due to signature or version mismatch.
      */
     @Override
-    protected void onRestoreBlocked(ShortcutService s) {
+    protected void onRestoreBlocked() {
         final ArrayList<PackageWithUser> pinnedPackages =
                 new ArrayList<>(mPinnedShortcuts.keySet());
         mPinnedShortcuts.clear();
         for (int i = pinnedPackages.size() - 1; i >= 0; i--) {
             final PackageWithUser pu = pinnedPackages.get(i);
-            s.getPackageShortcutsLocked(pu.packageName, pu.userId)
-                    .refreshPinnedFlags(s);
+            final ShortcutPackage p = mShortcutUser.getPackageShortcutsIfExists(pu.packageName);
+            if (p != null) {
+                p.refreshPinnedFlags();
+            }
         }
     }
 
     @Override
-    protected void onRestored(ShortcutService s) {
+    protected void onRestored() {
         // Nothing to do.
     }
 
-    public void pinShortcuts(@NonNull ShortcutService s, @UserIdInt int packageUserId,
+    public void pinShortcuts(@UserIdInt int packageUserId,
             @NonNull String packageName, @NonNull List<String> ids) {
         final ShortcutPackage packageShortcuts =
-                s.getPackageShortcutsLocked(packageName, packageUserId);
+                mShortcutUser.getPackageShortcutsIfExists(packageName);
+        if (packageShortcuts == null) {
+            return; // No need to instantiate.
+        }
 
         final PackageWithUser pu = PackageWithUser.of(packageUserId, packageName);
 
@@ -120,13 +130,14 @@
                 if (si == null) {
                     continue;
                 }
-                if (si.isDynamic() || (prevSet != null && prevSet.contains(id))) {
+                if (si.isDynamic() || si.isManifestShortcut()
+                        || (prevSet != null && prevSet.contains(id))) {
                     newSet.add(id);
                 }
             }
             mPinnedShortcuts.put(pu, newSet);
         }
-        packageShortcuts.refreshPinnedFlags(s);
+        packageShortcuts.refreshPinnedFlags();
     }
 
     /**
@@ -141,6 +152,16 @@
         return mPinnedShortcuts.remove(PackageWithUser.of(packageUserId, packageName)) != null;
     }
 
+    public void ensureVersionInfo() {
+        final PackageInfo pi = mShortcutUser.mService.getPackageInfoWithSignatures(
+                getPackageName(), getPackageUserId());
+        if (pi == null) {
+            Slog.w(TAG, "Package not found: " + getPackageName());
+            return;
+        }
+        getPackageInfo().updateVersionInfo(pi);
+    }
+
     /**
      * Persist.
      */
@@ -192,7 +213,7 @@
                 fromBackup ? ownerUserId
                 : ShortcutService.parseIntAttribute(parser, ATTR_LAUNCHER_USER_ID, ownerUserId);
 
-        final ShortcutLauncher ret = new ShortcutLauncher(shortcutUser, launcherUserId,
+        final ShortcutLauncher ret = new ShortcutLauncher(shortcutUser, ownerUserId,
                 launcherPackageName, launcherUserId);
 
         ArraySet<String> ids = null;
@@ -240,7 +261,7 @@
         return ret;
     }
 
-    public void dump(@NonNull ShortcutService s, @NonNull PrintWriter pw, @NonNull String prefix) {
+    public void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
         pw.println();
 
         pw.print(prefix);
@@ -252,7 +273,7 @@
         pw.print(getOwnerUserId());
         pw.println();
 
-        getPackageInfo().dump(s, pw, prefix + "  ");
+        getPackageInfo().dump(pw, prefix + "  ");
         pw.println();
 
         final int size = mPinnedShortcuts.size();
@@ -280,6 +301,15 @@
         }
     }
 
+    @Override
+    public JSONObject dumpCheckin(boolean clear) throws JSONException {
+        final JSONObject result = super.dumpCheckin(clear);
+
+        // Nothing really interesting to dump.
+
+        return result;
+    }
+
     @VisibleForTesting
     ArraySet<String> getAllPinnedShortcutsForTest(String packageName, int packageUserId) {
         return new ArraySet<>(mPinnedShortcuts.get(PackageWithUser.of(packageUserId, packageName)));
diff --git a/services/core/java/com/android/server/pm/ShortcutPackage.java b/services/core/java/com/android/server/pm/ShortcutPackage.java
index 151f61e..d558b07 100644
--- a/services/core/java/com/android/server/pm/ShortcutPackage.java
+++ b/services/core/java/com/android/server/pm/ShortcutPackage.java
@@ -20,16 +20,24 @@
 import android.annotation.UserIdInt;
 import android.content.ComponentName;
 import android.content.Intent;
+import android.content.pm.PackageInfo;
 import android.content.pm.ShortcutInfo;
+import android.content.res.Resources;
 import android.os.PersistableBundle;
 import android.text.format.Formatter;
 import android.util.ArrayMap;
 import android.util.ArraySet;
+import android.util.Log;
 import android.util.Slog;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.Preconditions;
 import com.android.internal.util.XmlUtils;
+import com.android.server.pm.ShortcutService.ShortcutOperation;
+import com.android.server.pm.ShortcutService.Stats;
 
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 import org.xmlpull.v1.XmlSerializer;
@@ -38,35 +46,50 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Set;
 import java.util.function.Predicate;
 
 /**
  * Package information used by {@link ShortcutService}.
+ * User information used by {@link ShortcutService}.
+ *
+ * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
  */
 class ShortcutPackage extends ShortcutPackageItem {
     private static final String TAG = ShortcutService.TAG;
+    private static final String TAG_VERIFY = ShortcutService.TAG + ".verify";
 
     static final String TAG_ROOT = "package";
-    private static final String TAG_INTENT_EXTRAS = "intent-extras";
+    private static final String TAG_INTENT_EXTRAS_LEGACY = "intent-extras";
+    private static final String TAG_INTENT = "intent";
     private static final String TAG_EXTRAS = "extras";
     private static final String TAG_SHORTCUT = "shortcut";
     private static final String TAG_CATEGORIES = "categories";
 
     private static final String ATTR_NAME = "name";
-    private static final String ATTR_DYNAMIC_COUNT = "dynamic-count";
     private static final String ATTR_CALL_COUNT = "call-count";
     private static final String ATTR_LAST_RESET = "last-reset";
     private static final String ATTR_ID = "id";
     private static final String ATTR_ACTIVITY = "activity";
     private static final String ATTR_TITLE = "title";
+    private static final String ATTR_TITLE_RES_ID = "titleid";
+    private static final String ATTR_TITLE_RES_NAME = "titlename";
     private static final String ATTR_TEXT = "text";
-    private static final String ATTR_INTENT = "intent";
-    private static final String ATTR_WEIGHT = "weight";
+    private static final String ATTR_TEXT_RES_ID = "textid";
+    private static final String ATTR_TEXT_RES_NAME = "textname";
+    private static final String ATTR_DISABLED_MESSAGE = "dmessage";
+    private static final String ATTR_DISABLED_MESSAGE_RES_ID = "dmessageid";
+    private static final String ATTR_DISABLED_MESSAGE_RES_NAME = "dmessagename";
+    private static final String ATTR_INTENT_LEGACY = "intent";
+    private static final String ATTR_INTENT_NO_EXTRA = "intent-base";
+    private static final String ATTR_RANK = "rank";
     private static final String ATTR_TIMESTAMP = "timestamp";
     private static final String ATTR_FLAGS = "flags";
-    private static final String ATTR_ICON_RES = "icon-res";
+    private static final String ATTR_ICON_RES_ID = "icon-res";
+    private static final String ATTR_ICON_RES_NAME = "icon-resname";
     private static final String ATTR_BITMAP_PATH = "bitmap-path";
 
     private static final String NAME_CATEGORIES = "categories";
@@ -74,17 +97,18 @@
     private static final String TAG_STRING_ARRAY_XMLUTILS = "string-array";
     private static final String ATTR_NAME_XMLUTILS = "name";
 
+    private static final String KEY_DYNAMIC = "dynamic";
+    private static final String KEY_MANIFEST = "manifest";
+    private static final String KEY_PINNED = "pinned";
+    private static final String KEY_BITMAPS = "bitmaps";
+    private static final String KEY_BITMAP_BYTES = "bitmapBytes";
+
     /**
      * All the shortcuts from the package, keyed on IDs.
      */
     final private ArrayMap<String, ShortcutInfo> mShortcuts = new ArrayMap<>();
 
     /**
-     * # of dynamic shortcuts.
-     */
-    private int mDynamicShortcutCount = 0;
-
-    /**
      * # of times the package has called rate-limited APIs.
      */
     private int mApiCallCount;
@@ -98,17 +122,16 @@
 
     private long mLastKnownForegroundElapsedTime;
 
-    private ShortcutPackage(ShortcutService s, ShortcutUser shortcutUser,
+    private ShortcutPackage(ShortcutUser shortcutUser,
             int packageUserId, String packageName, ShortcutPackageInfo spi) {
         super(shortcutUser, packageUserId, packageName,
                 spi != null ? spi : ShortcutPackageInfo.newEmpty());
 
-        mPackageUid = s.injectGetPackageUid(packageName, packageUserId);
+        mPackageUid = shortcutUser.mService.injectGetPackageUid(packageName, packageUserId);
     }
 
-    public ShortcutPackage(ShortcutService s, ShortcutUser shortcutUser,
-            int packageUserId, String packageName) {
-        this(s, shortcutUser, packageUserId, packageName, null);
+    public ShortcutPackage(ShortcutUser shortcutUser, int packageUserId, String packageName) {
+        this(shortcutUser, packageUserId, packageName, null);
     }
 
     @Override
@@ -121,38 +144,23 @@
         return mPackageUid;
     }
 
-    /**
-     * Called when a shortcut is about to be published.  At this point we know the publisher package
-     * exists (as opposed to Launcher trying to fetch shortcuts from a non-existent package), so
-     * we do some initialization for the package.
-     */
-    private void onShortcutPublish(ShortcutService s) {
-        // Make sure we have the version code for the app.  We need the version code in
-        // handlePackageUpdated().
-        if (getPackageInfo().getVersionCode() < 0) {
-            final int versionCode = s.getApplicationVersionCode(getPackageName(), getOwnerUserId());
-            if (ShortcutService.DEBUG) {
-                Slog.d(TAG, String.format("Package %s version = %d", getPackageName(),
-                        versionCode));
-            }
-            if (versionCode >= 0) {
-                getPackageInfo().setVersionCode(versionCode);
-                s.scheduleSaveUser(getOwnerUserId());
-            }
-        }
+    @Nullable
+    public Resources getPackageResources() {
+        return mShortcutUser.mService.injectGetResourcesForApplicationAsUser(
+                getPackageName(), getPackageUserId());
     }
 
     @Override
-    protected void onRestoreBlocked(ShortcutService s) {
+    protected void onRestoreBlocked() {
         // Can't restore due to version/signature mismatch.  Remove all shortcuts.
         mShortcuts.clear();
     }
 
     @Override
-    protected void onRestored(ShortcutService s) {
+    protected void onRestored() {
         // Because some launchers may not have been restored (e.g. allowBackup=false),
         // we need to re-calculate the pinned shortcuts.
-        refreshPinnedFlags(s);
+        refreshPinnedFlags();
     }
 
     /**
@@ -163,19 +171,48 @@
         return mShortcuts.get(id);
     }
 
-    private ShortcutInfo deleteShortcut(@NonNull ShortcutService s,
-            @NonNull String id) {
+    private void ensureNotImmutable(@Nullable ShortcutInfo shortcut) {
+        if (shortcut != null && shortcut.isImmutable()) {
+            throw new IllegalArgumentException(
+                    "Manifest shortcut ID=" + shortcut.getId()
+                            + " may not be manipulated via APIs");
+        }
+    }
+
+    private void ensureNotImmutable(@NonNull String id) {
+        ensureNotImmutable(mShortcuts.get(id));
+    }
+
+    public void ensureImmutableShortcutsNotIncludedWithIds(@NonNull List<String> shortcutIds) {
+        for (int i = shortcutIds.size() - 1; i >= 0; i--) {
+            ensureNotImmutable(shortcutIds.get(i));
+        }
+    }
+
+    public void ensureImmutableShortcutsNotIncluded(@NonNull List<ShortcutInfo> shortcuts) {
+        for (int i = shortcuts.size() - 1; i >= 0; i--) {
+            ensureNotImmutable(shortcuts.get(i).getId());
+        }
+    }
+
+    private ShortcutInfo deleteShortcutInner(@NonNull String id) {
         final ShortcutInfo shortcut = mShortcuts.remove(id);
         if (shortcut != null) {
-            s.removeIcon(getPackageUserId(), shortcut);
-            shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED);
+            mShortcutUser.mService.removeIcon(getPackageUserId(), shortcut);
+            shortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_PINNED
+                    | ShortcutInfo.FLAG_MANIFEST);
         }
         return shortcut;
     }
 
-    void addShortcut(@NonNull ShortcutService s, @NonNull ShortcutInfo newShortcut) {
-        deleteShortcut(s, newShortcut.getId());
+    private void addShortcutInner(@NonNull ShortcutInfo newShortcut) {
+        final ShortcutService s = mShortcutUser.mService;
+
+        deleteShortcutInner(newShortcut.getId());
+
+        // Extract Icon and update the icon res ID and the bitmap path.
         s.saveIconAndFixUpShortcut(getPackageUserId(), newShortcut);
+        s.fixUpShortcutResourceNamesAndValues(newShortcut);
         mShortcuts.put(newShortcut.getId(), newShortcut);
     }
 
@@ -184,52 +221,45 @@
      *
      * It checks the max number of dynamic shortcuts.
      */
-    public void addDynamicShortcut(@NonNull ShortcutService s,
-            @NonNull ShortcutInfo newShortcut) {
+    public void addOrUpdateDynamicShortcut(@NonNull ShortcutInfo newShortcut) {
 
-        onShortcutPublish(s);
+        Preconditions.checkArgument(newShortcut.isEnabled(),
+                "add/setDynamicShortcuts() cannot publish disabled shortcuts");
 
         newShortcut.addFlags(ShortcutInfo.FLAG_DYNAMIC);
 
         final ShortcutInfo oldShortcut = mShortcuts.get(newShortcut.getId());
 
         final boolean wasPinned;
-        final int newDynamicCount;
 
         if (oldShortcut == null) {
             wasPinned = false;
-            newDynamicCount = mDynamicShortcutCount + 1; // adding a dynamic shortcut.
         } else {
+            // It's an update case.
+            // Make sure the target is updatable. (i.e. should be mutable.)
+            oldShortcut.ensureUpdatableWith(newShortcut);
+
             wasPinned = oldShortcut.isPinned();
-            if (oldShortcut.isDynamic()) {
-                newDynamicCount = mDynamicShortcutCount; // not adding a dynamic shortcut.
-            } else {
-                newDynamicCount = mDynamicShortcutCount + 1; // adding a dynamic shortcut.
-            }
         }
 
-        // Make sure there's still room.
-        s.enforceMaxDynamicShortcuts(newDynamicCount);
-
-        // Okay, make it dynamic and add.
+        // If it was originally pinned, the new one should be pinned too.
         if (wasPinned) {
             newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
         }
 
-        addShortcut(s, newShortcut);
-        mDynamicShortcutCount = newDynamicCount;
+        addShortcutInner(newShortcut);
     }
 
     /**
      * Remove all shortcuts that aren't pinned nor dynamic.
      */
-    private void removeOrphans(@NonNull ShortcutService s) {
+    private void removeOrphans() {
         ArrayList<String> removeList = null; // Lazily initialize.
 
         for (int i = mShortcuts.size() - 1; i >= 0; i--) {
             final ShortcutInfo si = mShortcuts.valueAt(i);
 
-            if (si.isPinned() || si.isDynamic()) continue;
+            if (si.isAlive()) continue;
 
             if (removeList == null) {
                 removeList = new ArrayList<>();
@@ -238,7 +268,7 @@
         }
         if (removeList != null) {
             for (int i = removeList.size() - 1; i >= 0; i--) {
-                deleteShortcut(s, removeList.get(i));
+                deleteShortcutInner(removeList.get(i));
             }
         }
     }
@@ -246,30 +276,103 @@
     /**
      * Remove all dynamic shortcuts.
      */
-    public void deleteAllDynamicShortcuts(@NonNull ShortcutService s) {
+    public void deleteAllDynamicShortcuts() {
+        final long now = mShortcutUser.mService.injectCurrentTimeMillis();
+
+        boolean changed = false;
         for (int i = mShortcuts.size() - 1; i >= 0; i--) {
-            mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_DYNAMIC);
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+            if (si.isDynamic()) {
+                changed = true;
+
+                si.setTimestamp(now);
+                si.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
+                si.setRank(0); // It may still be pinned, so clear the rank.
+            }
         }
-        removeOrphans(s);
-        mDynamicShortcutCount = 0;
+        if (changed) {
+            removeOrphans();
+        }
     }
 
     /**
-     * Remove a dynamic shortcut by ID.
+     * Remove a dynamic shortcut by ID.  It'll be removed from the dynamic set, but if the shortcut
+     * is pinned, it'll remain as a pinned shortcut, and is still enabled.
+     *
+     * @return true if it's actually removed because it wasn't pinned, or false if it's still
+     * pinned.
      */
-    public void deleteDynamicWithId(@NonNull ShortcutService s, @NonNull String shortcutId) {
+    public boolean deleteDynamicWithId(@NonNull String shortcutId) {
+        final ShortcutInfo removed = deleteOrDisableWithId(
+                shortcutId, /* disable =*/ false, /* overrideImmutable=*/ false);
+        return removed == null;
+    }
+
+    /**
+     * Disable a dynamic shortcut by ID.  It'll be removed from the dynamic set, but if the shortcut
+     * is pinned, it'll remain as a pinned shortcut, but will be disabled.
+     *
+     * @return true if it's actually removed because it wasn't pinned, or false if it's still
+     * pinned.
+     */
+    private boolean disableDynamicWithId(@NonNull String shortcutId) {
+        final ShortcutInfo disabled = deleteOrDisableWithId(
+                shortcutId, /* disable =*/ true, /* overrideImmutable=*/ false);
+        return disabled == null;
+    }
+
+    /**
+     * Disable a dynamic shortcut by ID.  It'll be removed from the dynamic set, but if the shortcut
+     * is pinned, it'll remain as a pinned shortcut but will be disabled.
+     */
+    public void disableWithId(@NonNull String shortcutId, String disabledMessage,
+            int disabledMessageResId, boolean overrideImmutable) {
+        final ShortcutInfo disabled = deleteOrDisableWithId(shortcutId, /* disable =*/ true,
+                overrideImmutable);
+
+        if (disabled != null) {
+            if (disabledMessage != null) {
+                disabled.setDisabledMessage(disabledMessage);
+            } else if (disabledMessageResId != 0) {
+                disabled.setDisabledMessageResId(disabledMessageResId);
+
+                mShortcutUser.mService.fixUpShortcutResourceNamesAndValues(disabled);
+            }
+        }
+    }
+
+    @Nullable
+    private ShortcutInfo deleteOrDisableWithId(@NonNull String shortcutId, boolean disable,
+            boolean overrideImmutable) {
         final ShortcutInfo oldShortcut = mShortcuts.get(shortcutId);
 
-        if (oldShortcut == null) {
-            return;
+        if (oldShortcut == null || !oldShortcut.isEnabled()) {
+            return null; // Doesn't exist or already disabled.
         }
-        if (oldShortcut.isDynamic()) {
-            mDynamicShortcutCount--;
+        if (!overrideImmutable) {
+            ensureNotImmutable(oldShortcut);
         }
         if (oldShortcut.isPinned()) {
-            oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC);
+
+            oldShortcut.setRank(0);
+            oldShortcut.clearFlags(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_MANIFEST);
+            if (disable) {
+                oldShortcut.addFlags(ShortcutInfo.FLAG_DISABLED);
+            }
+            oldShortcut.setTimestamp(mShortcutUser.mService.injectCurrentTimeMillis());
+
+            return oldShortcut;
         } else {
-            deleteShortcut(s, shortcutId);
+            deleteShortcutInner(shortcutId);
+            return null;
+        }
+    }
+
+    public void enableWithId(@NonNull String shortcutId) {
+        final ShortcutInfo shortcut = mShortcuts.get(shortcutId);
+        if (shortcut != null) {
+            ensureNotImmutable(shortcut);
+            shortcut.clearFlags(ShortcutInfo.FLAG_DISABLED);
         }
     }
 
@@ -279,14 +382,15 @@
      *
      * <p>Then remove all shortcuts that are not dynamic and no longer pinned either.
      */
-    public void refreshPinnedFlags(@NonNull ShortcutService s) {
+    public void refreshPinnedFlags() {
         // First, un-pin all shortcuts
         for (int i = mShortcuts.size() - 1; i >= 0; i--) {
             mShortcuts.valueAt(i).clearFlags(ShortcutInfo.FLAG_PINNED);
         }
 
         // Then, for the pinned set for each launcher, set the pin flag one by one.
-        s.getUserShortcutsLocked(getPackageUserId()).forAllLaunchers(launcherShortcuts -> {
+        mShortcutUser.mService.getUserShortcutsLocked(getPackageUserId())
+                .forAllLaunchers(launcherShortcuts -> {
             final ArraySet<String> pinned = launcherShortcuts.getPinnedShortcutIds(
                     getPackageName(), getPackageUserId());
 
@@ -308,7 +412,7 @@
         });
 
         // Lastly, remove the ones that are no longer pinned nor dynamic.
-        removeOrphans(s);
+        removeOrphans();
     }
 
     /**
@@ -317,8 +421,8 @@
      * <p>This takes care of the resetting the counter for foreground apps as well as after
      * locale changes.
      */
-    public int getApiCallCount(@NonNull ShortcutService s) {
-        mShortcutUser.resetThrottlingIfNeeded(s);
+    public int getApiCallCount() {
+        final ShortcutService s = mShortcutUser.mService;
 
         // Reset the counter if:
         // - the package is in foreground now.
@@ -328,7 +432,7 @@
                 || mLastKnownForegroundElapsedTime
                     < s.getUidLastForegroundElapsedTimeLocked(mPackageUid)) {
             mLastKnownForegroundElapsedTime = s.injectElapsedRealtime();
-            resetRateLimiting(s);
+            resetRateLimiting();
         }
 
         // Note resetThrottlingIfNeeded() and resetRateLimiting() will set 0 to mApiCallCount,
@@ -349,8 +453,8 @@
         // If not reset yet, then reset.
         if (mLastResetTime < last) {
             if (ShortcutService.DEBUG) {
-                Slog.d(TAG, String.format("My last reset=%d, now=%d, last=%d: resetting",
-                        mLastResetTime, now, last));
+                Slog.d(TAG, String.format("%s: last reset=%d, now=%d, last=%d: resetting",
+                        getPackageName(), mLastResetTime, now, last));
             }
             mApiCallCount = 0;
             mLastResetTime = last;
@@ -365,8 +469,10 @@
      * <p>This takes care of the resetting the counter for foreground apps as well as after
      * locale changes, which is done internally by {@link #getApiCallCount}.
      */
-    public boolean tryApiCall(@NonNull ShortcutService s) {
-        if (getApiCallCount(s) >= s.mMaxUpdatesPerInterval) {
+    public boolean tryApiCall() {
+        final ShortcutService s = mShortcutUser.mService;
+
+        if (getApiCallCount() >= s.mMaxUpdatesPerInterval) {
             return false;
         }
         mApiCallCount++;
@@ -374,13 +480,13 @@
         return true;
     }
 
-    public void resetRateLimiting(@NonNull ShortcutService s) {
+    public void resetRateLimiting() {
         if (ShortcutService.DEBUG) {
             Slog.d(TAG, "resetRateLimiting: " + getPackageName());
         }
         if (mApiCallCount > 0) {
             mApiCallCount = 0;
-            s.scheduleSaveUser(getOwnerUserId());
+            mShortcutUser.mService.scheduleSaveUser(getOwnerUserId());
         }
     }
 
@@ -392,9 +498,9 @@
     /**
      * Find all shortcuts that match {@code query}.
      */
-    public void findAll(@NonNull ShortcutService s, @NonNull List<ShortcutInfo> result,
+    public void findAll(@NonNull List<ShortcutInfo> result,
             @Nullable Predicate<ShortcutInfo> query, int cloneFlag) {
-        findAll(s, result, query, cloneFlag, null, 0);
+        findAll(result, query, cloneFlag, null, 0);
     }
 
     /**
@@ -404,7 +510,7 @@
      * by the calling launcher will not be included in the result, and also "isPinned" will be
      * adjusted for the caller too.
      */
-    public void findAll(@NonNull ShortcutService s, @NonNull List<ShortcutInfo> result,
+    public void findAll(@NonNull List<ShortcutInfo> result,
             @Nullable Predicate<ShortcutInfo> query, int cloneFlag,
             @Nullable String callingLauncher, int launcherUserId) {
         if (getPackageInfo().isShadow()) {
@@ -412,6 +518,8 @@
             return;
         }
 
+        final ShortcutService s = mShortcutUser.mService;
+
         // Set of pinned shortcuts by the calling launcher.
         final ArraySet<String> pinnedByCallerSet = (callingLauncher == null) ? null
                 : s.getLauncherShortcutsLocked(callingLauncher, getPackageUserId(), launcherUserId)
@@ -420,22 +528,19 @@
         for (int i = 0; i < mShortcuts.size(); i++) {
             final ShortcutInfo si = mShortcuts.valueAt(i);
 
-            // If it's called by non-launcher (i.e. publisher, always include -> true.
-            // Otherwise, only include non-dynamic pinned one, if the calling launcher has pinned
-            // it.
+            // Need to adjust PINNED flag depending on the caller.
+            // Basically if the caller is a launcher (callingLauncher != null) and the launcher
+            // isn't pinning it, then we need to clear PINNED for this caller.
             final boolean isPinnedByCaller = (callingLauncher == null)
                     || ((pinnedByCallerSet != null) && pinnedByCallerSet.contains(si.getId()));
-            if (!si.isDynamic()) {
-                if (!si.isPinned()) {
-                    s.wtf("Shortcut not pinned: package " + getPackageName()
-                            + ", user=" + getPackageUserId() + ", id=" + si.getId());
-                    continue;
-                }
+
+            if (si.isFloating()) {
                 if (!isPinnedByCaller) {
                     continue;
                 }
             }
             final ShortcutInfo clone = si.clone(cloneFlag);
+
             // Fix up isPinned for the caller.  Note we need to do it before the "test" callback,
             // since it may check isPinned.
             if (!isPinnedByCaller) {
@@ -452,30 +557,186 @@
     }
 
     /**
-     * Called when the package is updated.  If there are shortcuts with resource icons, update
-     * their timestamps.
+     * Return the filenames (excluding path names) of icon bitmap files from this package.
      */
-    public void handlePackageUpdated(ShortcutService s, int newVersionCode) {
-        if (getPackageInfo().getVersionCode() >= newVersionCode) {
-            // Version hasn't changed; nothing to do.
-            return;
-        }
-        if (ShortcutService.DEBUG) {
-            Slog.d(TAG, String.format("Package %s updated, version %d -> %d", getPackageName(),
-                    getPackageInfo().getVersionCode(), newVersionCode));
-        }
+    public ArraySet<String> getUsedBitmapFiles() {
+        final ArraySet<String> usedFiles = new ArraySet<>(mShortcuts.size());
 
-        getPackageInfo().setVersionCode(newVersionCode);
-
-        boolean changed = false;
         for (int i = mShortcuts.size() - 1; i >= 0; i--) {
             final ShortcutInfo si = mShortcuts.valueAt(i);
-
-            if (si.hasIconResource()) {
-                changed = true;
-                si.setTimestamp(s.injectCurrentTimeMillis());
+            if (si.getBitmapPath() != null) {
+                usedFiles.add(getFileName(si.getBitmapPath()));
             }
         }
+        return usedFiles;
+    }
+
+    private static String getFileName(@NonNull String path) {
+        final int sep = path.lastIndexOf(File.separatorChar);
+        if (sep == -1) {
+            return path;
+        } else {
+            return path.substring(sep + 1);
+        }
+    }
+
+    /**
+     * @return false if any of the target activities are no longer enabled.
+     */
+    private boolean areAllActivitiesStillEnabled() {
+        if (mShortcuts.size() == 0) {
+            return true;
+        }
+        final ShortcutService s = mShortcutUser.mService;
+
+        // Normally the number of target activities is 1 or so, so no need to use a complex
+        // structure like a set.
+        final ArrayList<ComponentName> checked = new ArrayList<>(4);
+
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+            final ComponentName activity = si.getActivity();
+
+            if (checked.contains(activity)) {
+                continue; // Already checked.
+            }
+            checked.add(activity);
+
+            if (!s.injectIsActivityEnabledAndExported(activity, getOwnerUserId())) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Called when the package may be added or updated, or its activities may be disabled, and
+     * if so, rescan the package and do the necessary stuff.
+     *
+     * Add case:
+     * - Publish manifest shortcuts.
+     *
+     * Update case:
+     * - Re-publish manifest shortcuts.
+     * - If there are shortcuts with resources (icons or strings), update their timestamps.
+     * - Disable shortcuts whose target activities are disabled.
+     *
+     * @return TRUE if any shortcuts have been changed.
+     */
+    public boolean rescanPackageIfNeeded(boolean isNewApp, boolean forceRescan) {
+        final ShortcutService s = mShortcutUser.mService;
+        final long start = s.injectElapsedRealtime();
+
+        final PackageInfo pi;
+        try {
+            pi = mShortcutUser.mService.getPackageInfo(
+                    getPackageName(), getPackageUserId());
+            if (pi == null) {
+                return false; // Shouldn't happen.
+            }
+
+            if (!isNewApp && !forceRescan) {
+                // Return if the package hasn't changed, ie:
+                // - version code hasn't change
+                // - lastUpdateTime hasn't change
+                // - all target activities are still enabled.
+
+                // Note, system apps timestamps do *not* change after OTAs.  (But they do
+                // after an adb sync or a local flash.)
+                // This means if a system app's version code doesn't change on an OTA,
+                // we don't notice it's updated.  But that's fine since their version code *should*
+                // really change on OTAs.
+                if ((getPackageInfo().getVersionCode() == pi.versionCode)
+                        && (getPackageInfo().getLastUpdateTime() == pi.lastUpdateTime)
+                        && areAllActivitiesStillEnabled()) {
+                    return false;
+                }
+            }
+        } finally {
+            s.logDurationStat(Stats.PACKAGE_UPDATE_CHECK, start);
+        }
+
+        // Now prepare to publish manifest shortcuts.
+        List<ShortcutInfo> newManifestShortcutList = null;
+        try {
+            newManifestShortcutList = ShortcutParser.parseShortcuts(mShortcutUser.mService,
+                    getPackageName(), getPackageUserId());
+        } catch (IOException|XmlPullParserException e) {
+            Slog.e(TAG, "Failed to load shortcuts from AndroidManifest.xml.", e);
+        }
+        final int manifestShortcutSize = newManifestShortcutList == null ? 0
+                : newManifestShortcutList.size();
+        if (ShortcutService.DEBUG) {
+            Slog.d(TAG, String.format("Package %s has %d manifest shortcut(s)",
+                    getPackageName(), manifestShortcutSize));
+        }
+        if (isNewApp && (manifestShortcutSize == 0)) {
+            // If it's a new app, and it doesn't have manifest shortcuts, then nothing to do.
+
+            // If it's an update, then it may already have manifest shortcuts, which need to be
+            // disabled.
+            return false;
+        }
+        if (ShortcutService.DEBUG) {
+            Slog.d(TAG, String.format("Package %s %s, version %d -> %d", getPackageName(),
+                    (isNewApp ? "added" : "updated"),
+                    getPackageInfo().getVersionCode(), pi.versionCode));
+        }
+
+        getPackageInfo().updateVersionInfo(pi);
+
+        boolean changed = false;
+
+        // For existing shortcuts, update timestamps if they have any resources.
+        // Also check if shortcuts' activities are still main activities.  Otherwise, disable them.
+        if (!isNewApp) {
+            Resources publisherRes = null;
+
+            for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+                final ShortcutInfo si = mShortcuts.valueAt(i);
+
+                if (si.isDynamic()) {
+                    if (!s.injectIsMainActivity(si.getActivity(), getPackageUserId())) {
+                        Slog.w(TAG, String.format(
+                                "%s is no longer main activity. Disabling shorcut %s.",
+                                getPackageName(), si.getId()));
+                        if (disableDynamicWithId(si.getId())) {
+                            continue; // Actually removed.
+                        }
+                        // Still pinned, so fall-through and possibly update the resources.
+                    }
+                    changed = true;
+                }
+
+                if (si.hasAnyResources()) {
+                    if (!si.isOriginallyFromManifest()) {
+                        if (publisherRes == null) {
+                            publisherRes = getPackageResources();
+                            if (publisherRes == null) {
+                                break; // Resources couldn't be loaded.
+                            }
+                        }
+
+                        // If this shortcut is not from a manifest, then update all resource IDs
+                        // from resource names.  (We don't allow resource strings for
+                        // non-manifest at the moment, but icons can still be resources.)
+                        si.lookupAndFillInResourceIds(publisherRes);
+                    }
+                    changed = true;
+                    si.setTimestamp(s.injectCurrentTimeMillis());
+                }
+            }
+        }
+
+        // (Re-)publish manifest shortcut.
+        changed |= publishManifestShortcuts(newManifestShortcutList);
+
+        if (newManifestShortcutList != null) {
+            changed |= pushOutExcessShortcuts();
+        }
+
+        s.verifyStates();
+
         if (changed) {
             // This will send a notification to the launcher, and also save .
             s.packageShortcutsChanged(getPackageName(), getPackageUserId());
@@ -483,9 +744,398 @@
             // Still save the version code.
             s.scheduleSaveUser(getPackageUserId());
         }
+        return changed;
     }
 
-    public void dump(@NonNull ShortcutService s, @NonNull PrintWriter pw, @NonNull String prefix) {
+    private boolean publishManifestShortcuts(List<ShortcutInfo> newManifestShortcutList) {
+        if (ShortcutService.DEBUG) {
+            Slog.d(TAG, String.format(
+                    "Package %s: publishing manifest shortcuts", getPackageName()));
+        }
+        boolean changed = false;
+
+        // Keep the previous IDs.
+        ArraySet<String> toDisableList = null;
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+
+            if (si.isManifestShortcut()) {
+                if (toDisableList == null) {
+                    toDisableList = new ArraySet<>();
+                }
+                toDisableList.add(si.getId());
+            }
+        }
+
+        // Publish new ones.
+        if (newManifestShortcutList != null) {
+            final int newListSize = newManifestShortcutList.size();
+
+            for (int i = 0; i < newListSize; i++) {
+                changed = true;
+
+                final ShortcutInfo newShortcut = newManifestShortcutList.get(i);
+                final boolean newDisabled = !newShortcut.isEnabled();
+
+                final String id = newShortcut.getId();
+                final ShortcutInfo oldShortcut = mShortcuts.get(id);
+
+                boolean wasPinned = false;
+
+                if (oldShortcut != null) {
+                    if (!oldShortcut.isOriginallyFromManifest()) {
+                        Slog.e(TAG, "Shortcut with ID=" + newShortcut.getId()
+                                + " exists but is not from AndroidManifest.xml, not updating.");
+                        continue;
+                    }
+                    // Take over the pinned flag.
+                    if (oldShortcut.isPinned()) {
+                        wasPinned = true;
+                        newShortcut.addFlags(ShortcutInfo.FLAG_PINNED);
+                    }
+                }
+                if (newDisabled && !wasPinned) {
+                    // If the shortcut is disabled, and it was *not* pinned, then this
+                    // just doesn't have to be published.
+                    // Just keep it in toDisableList, so the previous one would be removed.
+                    continue;
+                }
+
+                // Note even if enabled=false, we still need to update all fields, so do it
+                // regardless.
+                addShortcutInner(newShortcut); // This will clean up the old one too.
+
+                if (!newDisabled && toDisableList != null) {
+                    // Still alive, don't remove.
+                    toDisableList.remove(id);
+                }
+            }
+        }
+
+        // Disable the previous manifest shortcuts that are no longer in the manifest.
+        if (toDisableList != null) {
+            if (ShortcutService.DEBUG) {
+                Slog.d(TAG, String.format(
+                        "Package %s: disabling %d stale shortcuts", getPackageName(),
+                        toDisableList.size()));
+            }
+            for (int i = toDisableList.size() - 1; i >= 0; i--) {
+                changed = true;
+
+                final String id = toDisableList.valueAt(i);
+
+                disableWithId(id, /* disable message =*/ null, /* disable message resid */ 0,
+                        /* overrideImmutable=*/ true);
+            }
+            removeOrphans();
+        }
+        adjustRanks();
+        return changed;
+    }
+
+    /**
+     * For each target activity, make sure # of dynamic + manifest shortcuts <= max.
+     * If too many, we'll remove the dynamic with the lowest ranks.
+     */
+    private boolean pushOutExcessShortcuts() {
+        final ShortcutService service = mShortcutUser.mService;
+        final int maxShortcuts = service.getMaxActivityShortcuts();
+
+        boolean changed = false;
+
+        final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
+                sortShortcutsToActivities();
+        for (int outer = all.size() - 1; outer >= 0; outer--) {
+            final ArrayList<ShortcutInfo> list = all.valueAt(outer);
+            if (list.size() <= maxShortcuts) {
+                continue;
+            }
+            // Sort by isManifestShortcut() and getRank().
+            Collections.sort(list, mShortcutTypeAndRankComparator);
+
+            // Keep [0 .. max), and remove (as dynamic) [max .. size)
+            for (int inner = list.size() - 1; inner >= maxShortcuts; inner--) {
+                final ShortcutInfo shortcut = list.get(inner);
+
+                if (shortcut.isManifestShortcut()) {
+                    // This shouldn't happen -- excess shortcuts should all be non-manifest.
+                    // But just in case.
+                    service.wtf("Found manifest shortcuts in excess list.");
+                    continue;
+                }
+                deleteDynamicWithId(shortcut.getId());
+            }
+        }
+
+        return changed;
+    }
+
+    /**
+     * To sort by isManifestShortcut() and getRank(). i.e.  manifest shortcuts come before
+     * non-manifest shortcuts, then sort by rank.
+     *
+     * This is used to decide which dynamic shortcuts to remove when an upgraded version has more
+     * manifest shortcuts than before and as a result we need to remove some of the dynamic
+     * shortcuts.  We sort manifest + dynamic shortcuts by this order, and remove the ones with
+     * the last ones.
+     *
+     * (Note the number of manifest shortcuts is always <= the max number, because if there are
+     * more, ShortcutParser would ignore the rest.)
+     */
+    final Comparator<ShortcutInfo> mShortcutTypeAndRankComparator = (ShortcutInfo a,
+            ShortcutInfo b) -> {
+        if (a.isManifestShortcut() && !b.isManifestShortcut()) {
+            return -1;
+        }
+        if (!a.isManifestShortcut() && b.isManifestShortcut()) {
+            return 1;
+        }
+        return Integer.compare(a.getRank(), b.getRank());
+    };
+
+    /**
+     * Build a list of shortcuts for each target activity and return as a map. The result won't
+     * contain "floating" shortcuts because they don't belong on any activities.
+     */
+    private ArrayMap<ComponentName, ArrayList<ShortcutInfo>> sortShortcutsToActivities() {
+        final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> activitiesToShortcuts
+                = new ArrayMap<>();
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+            if (si.isFloating()) {
+                continue; // Ignore floating shortcuts, which are not tied to any activities.
+            }
+
+            final ComponentName activity = si.getActivity();
+
+            ArrayList<ShortcutInfo> list = activitiesToShortcuts.get(activity);
+            if (list == null) {
+                list = new ArrayList<>();
+                activitiesToShortcuts.put(activity, list);
+            }
+            list.add(si);
+        }
+        return activitiesToShortcuts;
+    }
+
+    /** Used by {@link #enforceShortcutCountsBeforeOperation} */
+    private void incrementCountForActivity(ArrayMap<ComponentName, Integer> counts,
+            ComponentName cn, int increment) {
+        Integer oldValue = counts.get(cn);
+        if (oldValue == null) {
+            oldValue = 0;
+        }
+
+        counts.put(cn, oldValue + increment);
+    }
+
+    /**
+     * Called by
+     * {@link android.content.pm.ShortcutManager#setDynamicShortcuts},
+     * {@link android.content.pm.ShortcutManager#addDynamicShortcuts}, and
+     * {@link android.content.pm.ShortcutManager#updateShortcuts} before actually performing
+     * the operation to make sure the operation wouldn't result in the target activities having
+     * more than the allowed number of dynamic/manifest shortcuts.
+     *
+     * @param newList shortcut list passed to set, add or updateShortcuts().
+     * @param operation add, set or update.
+     * @throws IllegalArgumentException if the operation would result in going over the max
+     *                                  shortcut count for any activity.
+     */
+    public void enforceShortcutCountsBeforeOperation(List<ShortcutInfo> newList,
+            @ShortcutOperation int operation) {
+        final ShortcutService service = mShortcutUser.mService;
+
+        // Current # of dynamic / manifest shortcuts for each activity.
+        // (If it's for update, then don't count dynamic shortcuts, since they'll be replaced
+        // anyway.)
+        final ArrayMap<ComponentName, Integer> counts = new ArrayMap<>(4);
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo shortcut = mShortcuts.valueAt(i);
+
+            if (shortcut.isManifestShortcut()) {
+                incrementCountForActivity(counts, shortcut.getActivity(), 1);
+            } else if (shortcut.isDynamic() && (operation != ShortcutService.OPERATION_SET)) {
+                incrementCountForActivity(counts, shortcut.getActivity(), 1);
+            }
+        }
+
+        for (int i = newList.size() - 1; i >= 0; i--) {
+            final ShortcutInfo newShortcut = newList.get(i);
+            final ComponentName newActivity = newShortcut.getActivity();
+            if (newActivity == null) {
+                if (operation != ShortcutService.OPERATION_UPDATE) {
+                    service.wtf("Activity must not be null at this point");
+                    continue; // Just ignore this invalid case.
+                }
+                continue; // Activity can be null for update.
+            }
+
+            final ShortcutInfo original = mShortcuts.get(newShortcut.getId());
+            if (original == null) {
+                if (operation == ShortcutService.OPERATION_UPDATE) {
+                    continue; // When updating, ignore if there's no target.
+                }
+                // Add() or set(), and there's no existing shortcut with the same ID.  We're
+                // simply publishing (as opposed to updating) this shortcut, so just +1.
+                incrementCountForActivity(counts, newActivity, 1);
+                continue;
+            }
+            if (original.isFloating() && (operation == ShortcutService.OPERATION_UPDATE)) {
+                // Updating floating shortcuts doesn't affect the count, so ignore.
+                continue;
+            }
+
+            // If it's add() or update(), then need to decrement for the previous activity.
+            // Skip it for set() since it's already been taken care of by not counting the original
+            // dynamic shortcuts in the first loop.
+            if (operation != ShortcutService.OPERATION_SET) {
+                final ComponentName oldActivity = original.getActivity();
+                if (!original.isFloating()) {
+                    incrementCountForActivity(counts, oldActivity, -1);
+                }
+            }
+            incrementCountForActivity(counts, newActivity, 1);
+        }
+
+        // Then make sure none of the activities have more than the max number of shortcuts.
+        for (int i = counts.size() - 1; i >= 0; i--) {
+            service.enforceMaxActivityShortcuts(counts.valueAt(i));
+        }
+    }
+
+    /**
+     * For all the text fields, refresh the string values if they're from resources.
+     */
+    public void resolveResourceStrings() {
+        final ShortcutService s = mShortcutUser.mService;
+        boolean changed = false;
+
+        Resources publisherRes = null;
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+
+            if (si.hasStringResources()) {
+                changed = true;
+
+                if (publisherRes == null) {
+                    publisherRes = getPackageResources();
+                    if (publisherRes == null) {
+                        break; // Resources couldn't be loaded.
+                    }
+                }
+
+                si.resolveResourceStrings(publisherRes);
+                si.setTimestamp(s.injectCurrentTimeMillis());
+            }
+        }
+        if (changed) {
+            s.packageShortcutsChanged(getPackageName(), getPackageUserId());
+        }
+    }
+
+    /** Clears the implicit ranks for all shortcuts. */
+    public void clearAllImplicitRanks() {
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+            si.clearImplicitRankAndRankChangedFlag();
+        }
+    }
+
+    /**
+     * Used to sort shortcuts for rank auto-adjusting.
+     */
+    final Comparator<ShortcutInfo> mShortcutRankComparator = (ShortcutInfo a, ShortcutInfo b) -> {
+        // First, sort by rank.
+        int ret = Integer.compare(a.getRank(), b.getRank());
+        if (ret != 0) {
+            return ret;
+        }
+        // When ranks are tie, then prioritize the ones that have just been assigned new ranks.
+        // e.g. when there are 3 shortcuts, "s1" "s2" and "s3" with rank 0, 1, 2 respectively,
+        // adding a shortcut "s4" with rank 1 will "insert" it between "s1" and "s2", because
+        // "s2" and "s4" have the same rank 1 but s4 has isRankChanged() set.
+        // Similarly, updating s3's rank to 1 will insert it between s1 and s2.
+        if (a.isRankChanged() != b.isRankChanged()) {
+            return a.isRankChanged() ? -1 : 1;
+        }
+        // If they're still tie, sort by implicit rank -- i.e. preserve the order in which
+        // they're passed to the API.
+        ret = Integer.compare(a.getImplicitRank(), b.getImplicitRank());
+        if (ret != 0) {
+            return ret;
+        }
+        // If they're stil tie, just sort by their IDs.
+        // This may happen with updateShortcuts() -- see
+        // the testUpdateShortcuts_noManifestShortcuts() test.
+        return a.getId().compareTo(b.getId());
+    };
+
+    /**
+     * Re-calculate the ranks for all shortcuts.
+     */
+    public void adjustRanks() {
+        final ShortcutService s = mShortcutUser.mService;
+        final long now = s.injectCurrentTimeMillis();
+
+        // First, clear ranks for floating shortcuts.
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+            if (si.isFloating()) {
+                if (si.getRank() != 0) {
+                    si.setTimestamp(now);
+                    si.setRank(0);
+                }
+            }
+        }
+
+        // Then adjust ranks.  Ranks are unique for each activity, so we first need to sort
+        // shortcuts to each activity.
+        // Then sort the shortcuts within each activity with mShortcutRankComparator, and
+        // assign ranks from 0.
+        final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
+                sortShortcutsToActivities();
+        for (int outer = all.size() - 1; outer >= 0; outer--) { // For each activity.
+            final ArrayList<ShortcutInfo> list = all.valueAt(outer);
+
+            // Sort by ranks and other signals.
+            Collections.sort(list, mShortcutRankComparator);
+
+            int rank = 0;
+
+            final int size = list.size();
+            for (int i = 0; i < size; i++) {
+                final ShortcutInfo si = list.get(i);
+                if (si.isManifestShortcut()) {
+                    // Don't adjust ranks for manifest shortcuts.
+                    continue;
+                }
+                // At this point, it must be dynamic.
+                if (!si.isDynamic()) {
+                    s.wtf("Non-dynamic shortcut found.");
+                    continue;
+                }
+                final int thisRank = rank++;
+                if (si.getRank() != thisRank) {
+                    si.setTimestamp(now);
+                    si.setRank(thisRank);
+                }
+            }
+        }
+    }
+
+    /** @return true if there's any shortcuts that are not manifest shortcuts. */
+    public boolean hasNonManifestShortcuts() {
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+            if (!si.isDeclaredInManifest()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
         pw.println();
 
         pw.print(prefix);
@@ -498,7 +1148,7 @@
         pw.print(prefix);
         pw.print("  ");
         pw.print("Calls: ");
-        pw.print(getApiCallCount(s));
+        pw.print(getApiCallCount());
         pw.println();
 
         // getApiCallCount() may have updated mLastKnownForegroundElapsedTime.
@@ -514,10 +1164,10 @@
         pw.print("Last reset: [");
         pw.print(mLastResetTime);
         pw.print("] ");
-        pw.print(s.formatTime(mLastResetTime));
+        pw.print(ShortcutService.formatTime(mLastResetTime));
         pw.println();
 
-        getPackageInfo().dump(s, pw, prefix + "  ");
+        getPackageInfo().dump(pw, prefix + "  ");
         pw.println();
 
         pw.print(prefix);
@@ -545,11 +1195,47 @@
         pw.print("Total bitmap size: ");
         pw.print(totalBitmapSize);
         pw.print(" (");
-        pw.print(Formatter.formatFileSize(s.mContext, totalBitmapSize));
+        pw.print(Formatter.formatFileSize(mShortcutUser.mService.mContext, totalBitmapSize));
         pw.println(")");
     }
 
     @Override
+    public JSONObject dumpCheckin(boolean clear) throws JSONException {
+        final JSONObject result = super.dumpCheckin(clear);
+
+        int numDynamic = 0;
+        int numPinned = 0;
+        int numManifest = 0;
+        int numBitmaps = 0;
+        long totalBitmapSize = 0;
+
+        final ArrayMap<String, ShortcutInfo> shortcuts = mShortcuts;
+        final int size = shortcuts.size();
+        for (int i = 0; i < size; i++) {
+            final ShortcutInfo si = shortcuts.valueAt(i);
+
+            if (si.isDynamic()) numDynamic++;
+            if (si.isDeclaredInManifest()) numManifest++;
+            if (si.isPinned()) numPinned++;
+
+            if (si.getBitmapPath() != null) {
+                numBitmaps++;
+                totalBitmapSize += new File(si.getBitmapPath()).length();
+            }
+        }
+
+        result.put(KEY_DYNAMIC, numDynamic);
+        result.put(KEY_MANIFEST, numManifest);
+        result.put(KEY_PINNED, numPinned);
+        result.put(KEY_BITMAPS, numBitmaps);
+        result.put(KEY_BITMAP_BYTES, totalBitmapSize);
+
+        // TODO Log update frequency too.
+
+        return result;
+    }
+
+    @Override
     public void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
             throws IOException, XmlPullParserException {
         final int size = mShortcuts.size();
@@ -561,7 +1247,6 @@
         out.startTag(null, TAG_ROOT);
 
         ShortcutService.writeAttr(out, ATTR_NAME, getPackageName());
-        ShortcutService.writeAttr(out, ATTR_DYNAMIC_COUNT, mDynamicShortcutCount);
         ShortcutService.writeAttr(out, ATTR_CALL_COUNT, mApiCallCount);
         ShortcutService.writeAttr(out, ATTR_LAST_RESET, mLastResetTime);
         getPackageInfo().saveToXml(out);
@@ -576,19 +1261,26 @@
     private static void saveShortcut(XmlSerializer out, ShortcutInfo si, boolean forBackup)
             throws IOException, XmlPullParserException {
         if (forBackup) {
-            if (!si.isPinned()) {
-                return; // Backup only pinned icons.
+            if (!(si.isPinned() && si.isEnabled())) {
+                return; // We only backup pinned shortcuts that are enabled.
             }
         }
         out.startTag(null, TAG_SHORTCUT);
         ShortcutService.writeAttr(out, ATTR_ID, si.getId());
         // writeAttr(out, "package", si.getPackageName()); // not needed
-        ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivityComponent());
+        ShortcutService.writeAttr(out, ATTR_ACTIVITY, si.getActivity());
         // writeAttr(out, "icon", si.getIcon());  // We don't save it.
         ShortcutService.writeAttr(out, ATTR_TITLE, si.getTitle());
+        ShortcutService.writeAttr(out, ATTR_TITLE_RES_ID, si.getTitleResId());
+        ShortcutService.writeAttr(out, ATTR_TITLE_RES_NAME, si.getTitleResName());
         ShortcutService.writeAttr(out, ATTR_TEXT, si.getText());
-        ShortcutService.writeAttr(out, ATTR_INTENT, si.getIntentNoExtras());
-        ShortcutService.writeAttr(out, ATTR_WEIGHT, si.getWeight());
+        ShortcutService.writeAttr(out, ATTR_TEXT_RES_ID, si.getTextResId());
+        ShortcutService.writeAttr(out, ATTR_TEXT_RES_NAME, si.getTextResName());
+        ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE, si.getDisabledMessage());
+        ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_ID,
+                si.getDisabledMessageResourceId());
+        ShortcutService.writeAttr(out, ATTR_DISABLED_MESSAGE_RES_NAME,
+                si.getDisabledMessageResName());
         ShortcutService.writeAttr(out, ATTR_TIMESTAMP,
                 si.getLastChangedTimestamp());
         if (forBackup) {
@@ -598,8 +1290,13 @@
                             ~(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES
                             | ShortcutInfo.FLAG_DYNAMIC));
         } else {
+            // When writing for backup, ranks shouldn't be saved, since shortcuts won't be restored
+            // as dynamic.
+            ShortcutService.writeAttr(out, ATTR_RANK, si.getRank());
+
             ShortcutService.writeAttr(out, ATTR_FLAGS, si.getFlags());
-            ShortcutService.writeAttr(out, ATTR_ICON_RES, si.getIconResourceId());
+            ShortcutService.writeAttr(out, ATTR_ICON_RES_ID, si.getIconResourceId());
+            ShortcutService.writeAttr(out, ATTR_ICON_RES_NAME, si.getIconResName());
             ShortcutService.writeAttr(out, ATTR_BITMAP_PATH, si.getBitmapPath());
         }
 
@@ -612,9 +1309,16 @@
                 out.endTag(null, TAG_CATEGORIES);
             }
         }
+        final Intent[] intentsNoExtras = si.getIntentsNoExtras();
+        final PersistableBundle[] intentsExtras = si.getIntentPersistableExtrases();
+        final int numIntents = intentsNoExtras.length;
+        for (int i = 0; i < numIntents; i++) {
+            out.startTag(null, TAG_INTENT);
+            ShortcutService.writeAttr(out, ATTR_INTENT_NO_EXTRA, intentsNoExtras[i]);
+            ShortcutService.writeTagExtra(out, TAG_EXTRAS, intentsExtras[i]);
+            out.endTag(null, TAG_INTENT);
+        }
 
-        ShortcutService.writeTagExtra(out, TAG_INTENT_EXTRAS,
-                si.getIntentPersistableExtras());
         ShortcutService.writeTagExtra(out, TAG_EXTRAS, si.getExtras());
 
         out.endTag(null, TAG_SHORTCUT);
@@ -627,11 +1331,9 @@
         final String packageName = ShortcutService.parseStringAttribute(parser,
                 ATTR_NAME);
 
-        final ShortcutPackage ret = new ShortcutPackage(s, shortcutUser,
+        final ShortcutPackage ret = new ShortcutPackage(shortcutUser,
                 shortcutUser.getUserId(), packageName);
 
-        ret.mDynamicShortcutCount =
-                ShortcutService.parseIntAttribute(parser, ATTR_DYNAMIC_COUNT);
         ret.mApiCallCount =
                 ShortcutService.parseIntAttribute(parser, ATTR_CALL_COUNT);
         ret.mLastResetTime =
@@ -671,14 +1373,23 @@
         ComponentName activityComponent;
         // Icon icon;
         String title;
+        int titleResId;
+        String titleResName;
         String text;
-        Intent intent;
-        PersistableBundle intentPersistableExtras = null;
-        int weight;
+        int textResId;
+        String textResName;
+        String disabledMessage;
+        int disabledMessageResId;
+        String disabledMessageResName;
+        Intent intentLegacy;
+        PersistableBundle intentPersistableExtrasLegacy = null;
+        ArrayList<Intent> intents = new ArrayList<>();
+        int rank;
         PersistableBundle extras = null;
         long lastChangedTimestamp;
         int flags;
-        int iconRes;
+        int iconResId;
+        String iconResName;
         String bitmapPath;
         ArraySet<String> categories = null;
 
@@ -686,12 +1397,22 @@
         activityComponent = ShortcutService.parseComponentNameAttribute(parser,
                 ATTR_ACTIVITY);
         title = ShortcutService.parseStringAttribute(parser, ATTR_TITLE);
+        titleResId = ShortcutService.parseIntAttribute(parser, ATTR_TITLE_RES_ID);
+        titleResName = ShortcutService.parseStringAttribute(parser, ATTR_TITLE_RES_NAME);
         text = ShortcutService.parseStringAttribute(parser, ATTR_TEXT);
-        intent = ShortcutService.parseIntentAttribute(parser, ATTR_INTENT);
-        weight = (int) ShortcutService.parseLongAttribute(parser, ATTR_WEIGHT);
+        textResId = ShortcutService.parseIntAttribute(parser, ATTR_TEXT_RES_ID);
+        textResName = ShortcutService.parseStringAttribute(parser, ATTR_TEXT_RES_NAME);
+        disabledMessage = ShortcutService.parseStringAttribute(parser, ATTR_DISABLED_MESSAGE);
+        disabledMessageResId = ShortcutService.parseIntAttribute(parser,
+                ATTR_DISABLED_MESSAGE_RES_ID);
+        disabledMessageResName = ShortcutService.parseStringAttribute(parser,
+                ATTR_DISABLED_MESSAGE_RES_NAME);
+        intentLegacy = ShortcutService.parseIntentAttributeNoDefault(parser, ATTR_INTENT_LEGACY);
+        rank = (int) ShortcutService.parseLongAttribute(parser, ATTR_RANK);
         lastChangedTimestamp = ShortcutService.parseLongAttribute(parser, ATTR_TIMESTAMP);
         flags = (int) ShortcutService.parseLongAttribute(parser, ATTR_FLAGS);
-        iconRes = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES);
+        iconResId = (int) ShortcutService.parseLongAttribute(parser, ATTR_ICON_RES_ID);
+        iconResName = ShortcutService.parseStringAttribute(parser, ATTR_ICON_RES_NAME);
         bitmapPath = ShortcutService.parseStringAttribute(parser, ATTR_BITMAP_PATH);
 
         final int outerDepth = parser.getDepth();
@@ -708,8 +1429,11 @@
                         depth, type, tag));
             }
             switch (tag) {
-                case TAG_INTENT_EXTRAS:
-                    intentPersistableExtras = PersistableBundle.restoreFromXml(parser);
+                case TAG_INTENT_EXTRAS_LEGACY:
+                    intentPersistableExtrasLegacy = PersistableBundle.restoreFromXml(parser);
+                    continue;
+                case TAG_INTENT:
+                    intents.add(parseIntent(parser));
                     continue;
                 case TAG_EXTRAS:
                     extras = PersistableBundle.restoreFromXml(parser);
@@ -732,15 +1456,146 @@
             throw ShortcutService.throwForInvalidTag(depth, tag);
         }
 
+        if (intentLegacy != null) {
+            // For the legacy file format which supported only one intent per shortcut.
+            ShortcutInfo.setIntentExtras(intentLegacy, intentPersistableExtrasLegacy);
+            intents.clear();
+            intents.add(intentLegacy);
+        }
+
         return new ShortcutInfo(
-                userId, id, packageName, activityComponent, /* icon =*/ null, title, text,
-                categories, intent,
-                intentPersistableExtras, weight, extras, lastChangedTimestamp, flags,
-                iconRes, bitmapPath);
+                userId, id, packageName, activityComponent, /* icon =*/ null,
+                title, titleResId, titleResName, text, textResId, textResName,
+                disabledMessage, disabledMessageResId, disabledMessageResName,
+                categories,
+                intents.toArray(new Intent[intents.size()]),
+                rank, extras, lastChangedTimestamp, flags,
+                iconResId, iconResName, bitmapPath);
+    }
+
+    private static Intent parseIntent(XmlPullParser parser)
+            throws IOException, XmlPullParserException {
+
+        Intent intent = ShortcutService.parseIntentAttribute(parser,
+                ATTR_INTENT_NO_EXTRA);
+
+        final int outerDepth = parser.getDepth();
+        int type;
+        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
+                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
+            if (type != XmlPullParser.START_TAG) {
+                continue;
+            }
+            final int depth = parser.getDepth();
+            final String tag = parser.getName();
+            if (ShortcutService.DEBUG_LOAD) {
+                Slog.d(TAG, String.format("  depth=%d type=%d name=%s",
+                        depth, type, tag));
+            }
+            switch (tag) {
+                case TAG_EXTRAS:
+                    ShortcutInfo.setIntentExtras(intent,
+                            PersistableBundle.restoreFromXml(parser));
+                    continue;
+            }
+            throw ShortcutService.throwForInvalidTag(depth, tag);
+        }
+        return intent;
     }
 
     @VisibleForTesting
     List<ShortcutInfo> getAllShortcutsForTest() {
         return new ArrayList<>(mShortcuts.values());
     }
+
+    @Override
+    public void verifyStates() {
+        super.verifyStates();
+
+        boolean failed = false;
+
+        final ArrayMap<ComponentName, ArrayList<ShortcutInfo>> all =
+                sortShortcutsToActivities();
+
+        // Make sure each activity won't have more than max shortcuts.
+        for (int outer = all.size() - 1; outer >= 0; outer--) {
+            final ArrayList<ShortcutInfo> list = all.valueAt(outer);
+            if (list.size() > mShortcutUser.mService.getMaxActivityShortcuts()) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": activity " + all.keyAt(outer)
+                        + " has " + all.valueAt(outer).size() + " shortcuts.");
+            }
+
+            // Sort by rank.
+            Collections.sort(list, (a, b) -> Integer.compare(a.getRank(), b.getRank()));
+
+            // Split into two arrays for each kind.
+            final ArrayList<ShortcutInfo> dynamicList = new ArrayList<>(list);
+            dynamicList.removeIf((si) -> !si.isDynamic());
+
+            final ArrayList<ShortcutInfo> manifestList = new ArrayList<>(list);
+            dynamicList.removeIf((si) -> !si.isManifestShortcut());
+
+            verifyRanksSequential(dynamicList);
+            verifyRanksSequential(manifestList);
+        }
+
+        // Verify each shortcut's status.
+        for (int i = mShortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = mShortcuts.valueAt(i);
+            if (!(si.isDeclaredInManifest() || si.isDynamic() || si.isPinned())) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
+                        + " is not manifest, dynamic or pinned.");
+            }
+            if (si.isDeclaredInManifest() && si.isDynamic()) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
+                        + " is both dynamic and manifest at the same time.");
+            }
+            if (si.getActivity() == null) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
+                        + " has null activity.");
+            }
+            if ((si.isDynamic() || si.isManifestShortcut()) && !si.isEnabled()) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
+                        + " is not floating, but is disabled.");
+            }
+            if (si.isFloating() && si.getRank() != 0) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
+                        + " is floating, but has rank=" + si.getRank());
+            }
+            if (si.getIcon() != null) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
+                        + " still has an icon");
+            }
+            if (si.hasIconFile() && si.hasIconResource()) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
+                        + " has both resource and bitmap icons");
+            }
+        }
+
+        if (failed) {
+            throw new IllegalStateException("See logcat for errors");
+        }
+    }
+
+    private boolean verifyRanksSequential(List<ShortcutInfo> list) {
+        boolean failed = false;
+
+        for (int i = 0; i < list.size(); i++) {
+            final ShortcutInfo si = list.get(i);
+            if (si.getRank() != i) {
+                failed = true;
+                Log.e(TAG_VERIFY, "Package " + getPackageName() + ": shortcut " + si.getId()
+                        + " rank=" + si.getRank() + " but expected to be "+ i);
+            }
+        }
+        return failed;
+    }
 }
diff --git a/services/core/java/com/android/server/pm/ShortcutPackageInfo.java b/services/core/java/com/android/server/pm/ShortcutPackageInfo.java
index 74969f0..4de15de 100644
--- a/services/core/java/com/android/server/pm/ShortcutPackageInfo.java
+++ b/services/core/java/com/android/server/pm/ShortcutPackageInfo.java
@@ -15,10 +15,12 @@
  */
 package com.android.server.pm;
 
+import android.annotation.NonNull;
 import android.annotation.UserIdInt;
 import android.content.pm.PackageInfo;
 import android.util.Slog;
 
+import com.android.internal.annotations.VisibleForTesting;
 import com.android.server.backup.BackupUtils;
 
 import libcore.io.Base64;
@@ -34,12 +36,15 @@
 
 /**
  * Package information used by {@link android.content.pm.ShortcutManager} for backup / restore.
+ *
+ * All methods should be guarded by {@code ShortcutService.mLock}.
  */
 class ShortcutPackageInfo {
     private static final String TAG = ShortcutService.TAG;
 
     static final String TAG_ROOT = "package-info";
     private static final String ATTR_VERSION = "version";
+    private static final String ATTR_LAST_UPDATE_TIME = "last_udpate_time";
     private static final String ATTR_SHADOW = "shadow";
 
     private static final String TAG_SIGNATURE = "signature";
@@ -53,16 +58,20 @@
      */
     private boolean mIsShadow;
     private int mVersionCode = VERSION_UNKNOWN;
+    private long mLastUpdateTime;
     private ArrayList<byte[]> mSigHashes;
 
-    private ShortcutPackageInfo(int versionCode, ArrayList<byte[]> sigHashes, boolean isShadow) {
+    private ShortcutPackageInfo(int versionCode, long lastUpdateTime,
+            ArrayList<byte[]> sigHashes, boolean isShadow) {
         mVersionCode = versionCode;
+        mLastUpdateTime = lastUpdateTime;
         mIsShadow = isShadow;
         mSigHashes = sigHashes;
     }
 
     public static ShortcutPackageInfo newEmpty() {
-        return new ShortcutPackageInfo(VERSION_UNKNOWN, new ArrayList<>(0), /* isShadow */ false);
+        return new ShortcutPackageInfo(VERSION_UNKNOWN, /* last update time =*/ 0,
+                new ArrayList<>(0), /* isShadow */ false);
     }
 
     public boolean isShadow() {
@@ -77,8 +86,16 @@
         return mVersionCode;
     }
 
-    public void setVersionCode(int versionCode) {
-        mVersionCode = versionCode;
+    public long getLastUpdateTime() {
+        return mLastUpdateTime;
+    }
+
+    /** Set {@link #mVersionCode} and {@link #mLastUpdateTime} from a {@link PackageInfo}. */
+    public void updateVersionInfo(@NonNull PackageInfo pi) {
+        if (pi != null) {
+            mVersionCode = pi.versionCode;
+            mLastUpdateTime = pi.lastUpdateTime;
+        }
     }
 
     public boolean hasSignatures() {
@@ -104,20 +121,21 @@
         return true;
     }
 
-    public static ShortcutPackageInfo generateForInstalledPackage(
+    @VisibleForTesting
+    public static ShortcutPackageInfo generateForInstalledPackageForTest(
             ShortcutService s, String packageName, @UserIdInt int packageUserId) {
         final PackageInfo pi = s.getPackageInfoWithSignatures(packageName, packageUserId);
         if (pi.signatures == null || pi.signatures.length == 0) {
             Slog.e(TAG, "Can't get signatures: package=" + packageName);
             return null;
         }
-        final ShortcutPackageInfo ret = new ShortcutPackageInfo(pi.versionCode,
+        final ShortcutPackageInfo ret = new ShortcutPackageInfo(pi.versionCode, pi.lastUpdateTime,
                 BackupUtils.hashSignatureArray(pi.signatures), /* shadow=*/ false);
 
         return ret;
     }
 
-    public void refresh(ShortcutService s, ShortcutPackageItem pkg) {
+    public void refreshSignature(ShortcutService s, ShortcutPackageItem pkg) {
         if (mIsShadow) {
             s.wtf("Attempted to refresh package info for shadow package " + pkg.getPackageName()
                     + ", user=" + pkg.getOwnerUserId());
@@ -130,7 +148,6 @@
             Slog.w(TAG, "Package not found: " + pkg.getPackageName());
             return;
         }
-        mVersionCode = pi.versionCode;
         mSigHashes = BackupUtils.hashSignatureArray(pi.signatures);
     }
 
@@ -139,6 +156,7 @@
         out.startTag(null, TAG_ROOT);
 
         ShortcutService.writeAttr(out, ATTR_VERSION, mVersionCode);
+        ShortcutService.writeAttr(out, ATTR_LAST_UPDATE_TIME, mLastUpdateTime);
         ShortcutService.writeAttr(out, ATTR_SHADOW, mIsShadow);
 
         for (int i = 0; i < mSigHashes.size(); i++) {
@@ -154,6 +172,9 @@
 
         final int versionCode = ShortcutService.parseIntAttribute(parser, ATTR_VERSION);
 
+        final long lastUpdateTime = ShortcutService.parseLongAttribute(
+                parser, ATTR_LAST_UPDATE_TIME);
+
         // When restoring from backup, it's always shadow.
         final boolean shadow =
                 fromBackup || ShortcutService.parseBooleanAttribute(parser, ATTR_SHADOW);
@@ -185,11 +206,12 @@
 
         // Successfully loaded; replace the feilds.
         mVersionCode = versionCode;
+        mLastUpdateTime = lastUpdateTime;
         mIsShadow = shadow;
         mSigHashes = hashes;
     }
 
-    public void dump(ShortcutService s, PrintWriter pw, String prefix) {
+    public void dump(PrintWriter pw, String prefix) {
         pw.println();
 
         pw.print(prefix);
@@ -205,6 +227,11 @@
         pw.print(mVersionCode);
         pw.println();
 
+        pw.print(prefix);
+        pw.print("  Last package update time: ");
+        pw.print(mLastUpdateTime);
+        pw.println();
+
         for (int i = 0; i < mSigHashes.size(); i++) {
             pw.print(prefix);
             pw.print("    ");
diff --git a/services/core/java/com/android/server/pm/ShortcutPackageItem.java b/services/core/java/com/android/server/pm/ShortcutPackageItem.java
index 6fbdb82..1f195a7 100644
--- a/services/core/java/com/android/server/pm/ShortcutPackageItem.java
+++ b/services/core/java/com/android/server/pm/ShortcutPackageItem.java
@@ -21,20 +21,26 @@
 
 import com.android.internal.util.Preconditions;
 
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.xmlpull.v1.XmlPullParserException;
 import org.xmlpull.v1.XmlSerializer;
 
 import java.io.IOException;
 
+/**
+ * All methods should be guarded by {@code #mShortcutUser.mService.mLock}.
+ */
 abstract class ShortcutPackageItem {
     private static final String TAG = ShortcutService.TAG;
+    private static final String KEY_NAME = "name";
 
     private final int mPackageUserId;
     private final String mPackageName;
 
     private final ShortcutPackageInfo mPackageInfo;
 
-    protected final ShortcutUser mShortcutUser;
+    protected ShortcutUser mShortcutUser;
 
     protected ShortcutPackageItem(@NonNull ShortcutUser shortcutUser,
             int packageUserId, @NonNull String packageName,
@@ -46,10 +52,20 @@
     }
 
     /**
+     * Change the parent {@link ShortcutUser}.  Need it in the restore code.
+     */
+    public void replaceUser(ShortcutUser user) {
+        mShortcutUser = user;
+    }
+
+    public ShortcutUser getUser() {
+        return mShortcutUser;
+    }
+
+    /**
      * ID of the user who actually has this package running on.  For {@link ShortcutPackage},
      * this is the same thing as {@link #getOwnerUserId}, but if it's a {@link ShortcutLauncher} and
-     * {@link #getOwnerUserId} is of a work profile, then this ID could be the user who owns the
-     * profile.
+     * {@link #getOwnerUserId} is of work profile, then this ID is of the primary user.
      */
     public int getPackageUserId() {
         return mPackageUserId;
@@ -69,18 +85,20 @@
         return mPackageInfo;
     }
 
-    public void refreshPackageInfoAndSave(ShortcutService s) {
+    public void refreshPackageSignatureAndSave() {
         if (mPackageInfo.isShadow()) {
             return; // Don't refresh for shadow user.
         }
-        mPackageInfo.refresh(s, this);
+        final ShortcutService s = mShortcutUser.mService;
+        mPackageInfo.refreshSignature(s, this);
         s.scheduleSaveUser(getOwnerUserId());
     }
 
-    public void attemptToRestoreIfNeededAndSave(ShortcutService s) {
+    public void attemptToRestoreIfNeededAndSave() {
         if (!mPackageInfo.isShadow()) {
             return; // Already installed, nothing to do.
         }
+        final ShortcutService s = mShortcutUser.mService;
         if (!s.isPackageInstalled(mPackageName, mPackageUserId)) {
             if (ShortcutService.DEBUG) {
                 Slog.d(TAG, String.format("Package still not installed: %s user=%d",
@@ -91,14 +109,14 @@
         if (!mPackageInfo.hasSignatures()) {
             s.wtf("Attempted to restore package " + mPackageName + ", user=" + mPackageUserId
                     + " but signatures not found in the restore data.");
-            onRestoreBlocked(s);
+            onRestoreBlocked();
             return;
         }
 
         final PackageInfo pi = s.getPackageInfoWithSignatures(mPackageName, mPackageUserId);
         if (!mPackageInfo.canRestoreTo(s, pi)) {
             // Package is now installed, but can't restore.  Let the subclass do the cleanup.
-            onRestoreBlocked(s);
+            onRestoreBlocked();
             return;
         }
         if (ShortcutService.DEBUG) {
@@ -106,7 +124,7 @@
                     mPackageUserId, getOwnerUserId()));
         }
 
-        onRestored(s);
+        onRestored();
 
         // Now the package is not shadow.
         mPackageInfo.setShadow(false);
@@ -118,13 +136,25 @@
      * Called when the new package can't be restored because it has a lower version number
      * or different signatures.
      */
-    protected abstract void onRestoreBlocked(ShortcutService s);
+    protected abstract void onRestoreBlocked();
 
     /**
      * Called when the new package is successfully restored.
      */
-    protected abstract void onRestored(ShortcutService s);
+    protected abstract void onRestored();
 
     public abstract void saveToXml(@NonNull XmlSerializer out, boolean forBackup)
             throws IOException, XmlPullParserException;
+
+    public JSONObject dumpCheckin(boolean clear) throws JSONException {
+        final JSONObject result = new JSONObject();
+        result.put(KEY_NAME, mPackageName);
+        return result;
+    }
+
+    /**
+     * Verify various internal states.
+     */
+    public void verifyStates() {
+    }
 }
diff --git a/services/core/java/com/android/server/pm/ShortcutParser.java b/services/core/java/com/android/server/pm/ShortcutParser.java
new file mode 100644
index 0000000..3cf4200
--- /dev/null
+++ b/services/core/java/com/android/server/pm/ShortcutParser.java
@@ -0,0 +1,368 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import android.annotation.Nullable;
+import android.annotation.UserIdInt;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ResolveInfo;
+import android.content.pm.ShortcutInfo;
+import android.content.res.TypedArray;
+import android.content.res.XmlResourceParser;
+import android.text.TextUtils;
+import android.util.ArraySet;
+import android.util.AttributeSet;
+import android.util.Log;
+import android.util.Slog;
+import android.util.TypedValue;
+import android.util.Xml;
+
+import com.android.internal.R;
+import com.android.internal.annotations.VisibleForTesting;
+
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+public class ShortcutParser {
+    private static final String TAG = ShortcutService.TAG;
+
+    private static final boolean DEBUG = ShortcutService.DEBUG || false; // DO NOT SUBMIT WITH TRUE
+
+    @VisibleForTesting
+    static final String METADATA_KEY = "android.app.shortcuts";
+
+    private static final String TAG_SHORTCUTS = "shortcuts";
+    private static final String TAG_SHORTCUT = "shortcut";
+    private static final String TAG_INTENT = "intent";
+    private static final String TAG_CATEGORIES = "categories";
+
+    @Nullable
+    public static List<ShortcutInfo> parseShortcuts(ShortcutService service,
+            String packageName, @UserIdInt int userId) throws IOException, XmlPullParserException {
+        if (ShortcutService.DEBUG) {
+            Slog.d(TAG, String.format("Scanning package %s for manifest shortcuts on user %d",
+                    packageName, userId));
+        }
+        final List<ResolveInfo> activities = service.injectGetMainActivities(packageName, userId);
+        if (activities == null || activities.size() == 0) {
+            return null;
+        }
+
+        List<ShortcutInfo> result = null;
+
+        try {
+            final int size = activities.size();
+            for (int i = 0; i < size; i++) {
+                final ActivityInfo activityInfoNoMetadata = activities.get(i).activityInfo;
+                if (activityInfoNoMetadata == null) {
+                    continue;
+                }
+
+                final ActivityInfo activityInfoWithMetadata =
+                        service.getActivityInfoWithMetadata(
+                        activityInfoNoMetadata.getComponentName(), userId);
+                if (activityInfoWithMetadata != null) {
+                    result = parseShortcutsOneFile(
+                            service, activityInfoWithMetadata, packageName, userId, result);
+                }
+            }
+        } catch (RuntimeException e) {
+            // Resource ID mismatch may cause various runtime exceptions when parsing XMLs,
+            // But we don't crash the device, so just swallow them.
+            service.wtf(
+                    "Exception caught while parsing shortcut XML for package=" + packageName, e);
+            return null;
+        }
+        return result;
+    }
+
+    private static List<ShortcutInfo> parseShortcutsOneFile(
+            ShortcutService service,
+            ActivityInfo activityInfo, String packageName, @UserIdInt int userId,
+            List<ShortcutInfo> result) throws IOException, XmlPullParserException {
+        if (ShortcutService.DEBUG) {
+            Slog.d(TAG, String.format(
+                    "Checking main activity %s", activityInfo.getComponentName()));
+        }
+
+        XmlResourceParser parser = null;
+        try {
+            parser = service.injectXmlMetaData(activityInfo, METADATA_KEY);
+            if (parser == null) {
+                return result;
+            }
+
+            final ComponentName activity = new ComponentName(packageName, activityInfo.name);
+
+            final AttributeSet attrs = Xml.asAttributeSet(parser);
+
+            int type;
+
+            int rank = 0;
+            final int maxShortcuts = service.getMaxActivityShortcuts();
+            int numShortcuts = 0;
+
+            // We instantiate ShortcutInfo at <shortcut>, but we add it to the list at </shortcut>,
+            // after parsing <intent>.  We keep the current one in here.
+            ShortcutInfo currentShortcut = null;
+
+            Set<String> categories = null;
+            final ArrayList<Intent> intents = new ArrayList<>();
+
+            outer:
+            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
+                    && (type != XmlPullParser.END_TAG || parser.getDepth() > 0)) {
+                final int depth = parser.getDepth();
+                final String tag = parser.getName();
+
+                // When a shortcut tag is closing, publish.
+                if ((type == XmlPullParser.END_TAG) && (depth == 2) && (TAG_SHORTCUT.equals(tag))) {
+                    if (currentShortcut == null) {
+                        // Shortcut was invalid.
+                        continue;
+                    }
+                    final ShortcutInfo si = currentShortcut;
+                    currentShortcut = null; // Make sure to null out for the next iteration.
+
+                    if (si.isEnabled()) {
+                        if (intents.size() == 0) {
+                            Log.e(TAG, "Shortcut " + si.getId() + " has no intent. Skipping it.");
+                            continue;
+                        }
+                    } else {
+                        // Just set the default intent to disabled shortcuts.
+                        intents.clear();
+                        intents.add(new Intent(Intent.ACTION_VIEW));
+                    }
+
+                    if (numShortcuts >= maxShortcuts) {
+                        Log.e(TAG, "More than " + maxShortcuts + " shortcuts found for "
+                                + activityInfo.getComponentName() + ". Skipping the rest.");
+                        return result;
+                    }
+
+                    // Same flag as what TaskStackBuilder adds.
+                    intents.get(0).addFlags(
+                            Intent.FLAG_ACTIVITY_NEW_TASK |
+                            Intent.FLAG_ACTIVITY_CLEAR_TASK |
+                            Intent.FLAG_ACTIVITY_TASK_ON_HOME);
+                    try {
+                        si.setIntents(intents.toArray(new Intent[intents.size()]));
+                    } catch (RuntimeException e) {
+                        // This shouldn't happen because intents in XML can't have complicated
+                        // extras, but just in case Intent.parseIntent() supports such a thing one
+                        // day.
+                        Log.e(TAG, "Shortcut's extras contain un-persistable values. Skipping it.");
+                        continue;
+                    }
+                    intents.clear();
+
+                    if (categories != null) {
+                        si.setCategories(categories);
+                        categories = null;
+                    }
+
+                    if (result == null) {
+                        result = new ArrayList<>();
+                    }
+                    result.add(si);
+                    numShortcuts++;
+                    rank++;
+                    if (ShortcutService.DEBUG) {
+                        Slog.d(TAG, "Shortcut added: " + si.toInsecureString());
+                    }
+                    continue;
+                }
+
+                // Otherwise, just look at start tags.
+                if (type != XmlPullParser.START_TAG) {
+                    continue;
+                }
+
+                if (depth == 1 && TAG_SHORTCUTS.equals(tag)) {
+                    continue; // Root tag.
+                }
+                if (depth == 2 && TAG_SHORTCUT.equals(tag)) {
+                    final ShortcutInfo si = parseShortcutAttributes(
+                            service, attrs, packageName, activity, userId, rank);
+                    if (si == null) {
+                        // Shortcut was invalid.
+                        continue;
+                    }
+                    if (ShortcutService.DEBUG) {
+                        Slog.d(TAG, "Shortcut found: " + si.toInsecureString());
+                    }
+                    if (result != null) {
+                        for (int i = result.size() - 1; i >= 0; i--) {
+                            if (si.getId().equals(result.get(i).getId())) {
+                                Log.e(TAG, "Duplicate shortcut ID detected. Skipping it.");
+                                continue outer;
+                            }
+                        }
+                    }
+                    currentShortcut = si;
+                    categories = null;
+                    continue;
+                }
+                if (depth == 3 && TAG_INTENT.equals(tag)) {
+                    if ((currentShortcut == null)
+                            || !currentShortcut.isEnabled()) {
+                        Log.e(TAG, "Ignoring excessive intent tag.");
+                        continue;
+                    }
+
+                    final Intent intent = Intent.parseIntent(service.mContext.getResources(),
+                            parser, attrs);
+                    if (TextUtils.isEmpty(intent.getAction())) {
+                        Log.e(TAG, "Shortcut intent action must be provided. activity=" + activity);
+                        currentShortcut = null; // Invalidate the current shortcut.
+                        continue;
+                    }
+                    intents.add(intent);
+                    continue;
+                }
+                if (depth == 3 && TAG_CATEGORIES.equals(tag)) {
+                    if ((currentShortcut == null)
+                            || (currentShortcut.getCategories() != null)) {
+                        continue;
+                    }
+                    final String name = parseCategories(service, attrs);
+                    if (TextUtils.isEmpty(name)) {
+                        Log.e(TAG, "Empty category found. activity=" + activity);
+                        continue;
+                    }
+
+                    if (categories == null) {
+                        categories = new ArraySet<>();
+                    }
+                    categories.add(name);
+                    continue;
+                }
+
+                Log.w(TAG, String.format("Invalid tag '%s' found at depth %d", tag, depth));
+            }
+        } finally {
+            if (parser != null) {
+                parser.close();
+            }
+        }
+        return result;
+    }
+
+    private static String parseCategories(ShortcutService service, AttributeSet attrs) {
+        final TypedArray sa = service.mContext.getResources().obtainAttributes(attrs,
+                R.styleable.ShortcutCategories);
+        try {
+            if (sa.getType(R.styleable.ShortcutCategories_name) == TypedValue.TYPE_STRING) {
+                return sa.getNonResourceString(R.styleable.ShortcutCategories_name);
+            } else {
+                Log.w(TAG, "android:name for shortcut category must be string literal.");
+                return null;
+            }
+        } finally {
+            sa.recycle();
+        }
+    }
+
+    private static ShortcutInfo parseShortcutAttributes(ShortcutService service,
+            AttributeSet attrs, String packageName, ComponentName activity,
+            @UserIdInt int userId, int rank) {
+        final TypedArray sa = service.mContext.getResources().obtainAttributes(attrs,
+                R.styleable.Shortcut);
+        try {
+            if (sa.getType(R.styleable.Shortcut_shortcutId) != TypedValue.TYPE_STRING) {
+                Log.w(TAG, "android:shortcutId must be string literal. activity=" + activity);
+                return null;
+            }
+            final String id = sa.getNonResourceString(R.styleable.Shortcut_shortcutId);
+            final boolean enabled = sa.getBoolean(R.styleable.Shortcut_enabled, true);
+            final int iconResId = sa.getResourceId(R.styleable.Shortcut_icon, 0);
+            final int titleResId = sa.getResourceId(R.styleable.Shortcut_shortcutShortLabel, 0);
+            final int textResId = sa.getResourceId(R.styleable.Shortcut_shortcutLongLabel, 0);
+            final int disabledMessageResId = sa.getResourceId(
+                    R.styleable.Shortcut_shortcutDisabledMessage, 0);
+
+            if (TextUtils.isEmpty(id)) {
+                Log.w(TAG, "android:shortcutId must be provided. activity=" + activity);
+                return null;
+            }
+            if (titleResId == 0) {
+                Log.w(TAG, "android:shortcutShortLabel must be provided. activity=" + activity);
+                return null;
+            }
+
+            return createShortcutFromManifest(
+                    service,
+                    userId,
+                    id,
+                    packageName,
+                    activity,
+                    titleResId,
+                    textResId,
+                    disabledMessageResId,
+                    rank,
+                    iconResId,
+                    enabled);
+        } finally {
+            sa.recycle();
+        }
+    }
+
+    private static ShortcutInfo createShortcutFromManifest(ShortcutService service,
+            @UserIdInt int userId, String id, String packageName, ComponentName activityComponent,
+            int titleResId, int textResId, int disabledMessageResId,
+            int rank, int iconResId, boolean enabled) {
+
+        final int flags =
+                (enabled ? ShortcutInfo.FLAG_MANIFEST : ShortcutInfo.FLAG_DISABLED)
+                | ShortcutInfo.FLAG_IMMUTABLE
+                | ((iconResId != 0) ? ShortcutInfo.FLAG_HAS_ICON_RES : 0);
+
+        // Note we don't need to set resource names here yet.  They'll be set when they're about
+        // to be published.
+        return new ShortcutInfo(
+                userId,
+                id,
+                packageName,
+                activityComponent,
+                null, // icon
+                null, // title string
+                titleResId,
+                null, // title res name
+                null, // text string
+                textResId,
+                null, // text res name
+                null, // disabled message string
+                disabledMessageResId,
+                null, // disabled message res name
+                null, // categories
+                null, // intent
+                rank,
+                null, // extras
+                service.injectCurrentTimeMillis(),
+                flags,
+                iconResId,
+                null, // icon res name
+                null); // bitmap path
+    }
+}
diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java
index 8268776..13f558e 100644
--- a/services/core/java/com/android/server/pm/ShortcutService.java
+++ b/services/core/java/com/android/server/pm/ShortcutService.java
@@ -15,16 +15,22 @@
  */
 package com.android.server.pm;
 
+import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.UserIdInt;
 import android.app.ActivityManager;
+import android.app.ActivityManagerInternal;
 import android.app.ActivityManagerNative;
 import android.app.AppGlobals;
 import android.app.IUidObserver;
+import android.app.usage.UsageStatsManagerInternal;
+import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.IPackageManager;
 import android.content.pm.IShortcutService;
@@ -32,21 +38,27 @@
 import android.content.pm.LauncherApps.ShortcutQuery;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.PackageManagerInternal;
 import android.content.pm.ParceledListSlice;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ShortcutInfo;
 import android.content.pm.ShortcutServiceInternal;
 import android.content.pm.ShortcutServiceInternal.ShortcutChangeListener;
+import android.content.res.Resources;
+import android.content.res.XmlResourceParser;
 import android.graphics.Bitmap;
 import android.graphics.Bitmap.CompressFormat;
 import android.graphics.Canvas;
 import android.graphics.RectF;
 import android.graphics.drawable.Icon;
+import android.net.Uri;
 import android.os.Binder;
+import android.os.Build;
 import android.os.Environment;
 import android.os.FileUtils;
 import android.os.Handler;
+import android.os.LocaleList;
 import android.os.Looper;
 import android.os.ParcelFileDescriptor;
 import android.os.PersistableBundle;
@@ -54,6 +66,7 @@
 import android.os.RemoteException;
 import android.os.ResultReceiver;
 import android.os.SELinux;
+import android.os.ServiceManager;
 import android.os.ShellCommand;
 import android.os.SystemClock;
 import android.os.UserHandle;
@@ -63,18 +76,19 @@
 import android.util.ArraySet;
 import android.util.AtomicFile;
 import android.util.KeyValueListParser;
+import android.util.Log;
 import android.util.Slog;
 import android.util.SparseArray;
+import android.util.SparseBooleanArray;
 import android.util.SparseIntArray;
 import android.util.SparseLongArray;
 import android.util.TypedValue;
 import android.util.Xml;
+import android.view.IWindowManager;
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
-import com.android.internal.content.PackageMonitor;
 import com.android.internal.os.BackgroundThread;
-import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.FastXmlSerializer;
 import com.android.internal.util.Preconditions;
 import com.android.server.LocalServices;
@@ -83,6 +97,9 @@
 
 import libcore.io.IoUtils;
 
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 import org.xmlpull.v1.XmlSerializer;
@@ -100,33 +117,28 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
 import java.util.function.Predicate;
 
 /**
  * TODO:
- *
- * - Default launcher check does take a few ms.  Worth caching.
- *
- * - Clear data -> remove all dynamic?  but not the pinned?
- *
- * - Scan and remove orphan bitmaps (just in case).
+ * - getIconMaxWidth()/getIconMaxHeight() should use xdpi and ydpi.
+ *   -> But TypedValue.applyDimension() doesn't differentiate x and y..?
  *
  * - Detect when already registered instances are passed to APIs again, which might break
- *   internal bitmap handling.
- *
- * - Add more call stats.
+ * internal bitmap handling.
  */
 public class ShortcutService extends IShortcutService.Stub {
     static final String TAG = "ShortcutService";
 
-    public static final boolean FEATURE_ENABLED = false;
-
     static final boolean DEBUG = false; // STOPSHIP if true
     static final boolean DEBUG_LOAD = false; // STOPSHIP if true
     static final boolean DEBUG_PROCSTATE = false; // STOPSHIP if true
@@ -168,10 +180,15 @@
 
     private static final String TAG_ROOT = "root";
     private static final String TAG_LAST_RESET_TIME = "last_reset_time";
-    private static final String TAG_LOCALE_CHANGE_SEQUENCE_NUMBER = "locale_seq_no";
 
     private static final String ATTR_VALUE = "value";
 
+    private static final String LAUNCHER_INTENT_CATEGORY = Intent.CATEGORY_LAUNCHER;
+
+    private static final String KEY_SHORTCUT = "shortcut";
+    private static final String KEY_LOW_RAM = "lowRam";
+    private static final String KEY_ICON_SIZE = "iconSize";
+
     @VisibleForTesting
     interface ConfigConstants {
         /**
@@ -200,7 +217,7 @@
         String KEY_MAX_ICON_DIMENSION_DP_LOWRAM = "max_icon_dimension_dp_lowram";
 
         /**
-         * Key name for the max dynamic shortcuts per app. (int)
+         * Key name for the max dynamic shortcuts per activity. (int)
          */
         String KEY_MAX_SHORTCUTS = "max_shortcuts";
 
@@ -219,6 +236,13 @@
 
     private final Object mLock = new Object();
 
+    private static List<ResolveInfo> EMPTY_RESOLVE_INFO = new ArrayList<>(0);
+
+    private static Predicate<ResolveInfo> ACTIVITY_NOT_EXPORTED =
+            ri -> !ri.activityInfo.exported;
+
+    private static Predicate<PackageInfo> PACKAGE_NOT_INSTALLED = pi -> !isInstalled(pi);
+
     private final Handler mHandler;
 
     @GuardedBy("mLock")
@@ -234,9 +258,9 @@
     private final SparseArray<ShortcutUser> mUsers = new SparseArray<>();
 
     /**
-     * Max number of dynamic shortcuts that each application can have at a time.
+     * Max number of dynamic + manifest shortcuts that each application can have at a time.
      */
-    private int mMaxDynamicShortcuts;
+    private int mMaxShortcuts;
 
     /**
      * Max number of updating API calls that each application can make during the interval.
@@ -261,6 +285,8 @@
     private final IPackageManager mIPackageManager;
     private final PackageManagerInternal mPackageManagerInternal;
     private final UserManager mUserManager;
+    private final UsageStatsManagerInternal mUsageStatsManagerInternal;
+    private final ActivityManagerInternal mActivityManagerInternal;
 
     @GuardedBy("mLock")
     final SparseIntArray mUidState = new SparseIntArray();
@@ -271,19 +297,15 @@
     @GuardedBy("mLock")
     private List<Integer> mDirtyUserIds = new ArrayList<>();
 
-    /**
-     * A counter that increments every time the system locale changes.  We keep track of it to reset
-     * throttling counters on the first call from each package after the last locale change.
-     *
-     * We need this mechanism because we can't do much in the locale change callback, which is
-     * {@link ShortcutServiceInternal#onSystemLocaleChangedNoLock()}.
-     */
-    private final AtomicLong mLocaleChangeSequenceNumber = new AtomicLong();
+    private final AtomicBoolean mBootCompleted = new AtomicBoolean();
 
     private static final int PACKAGE_MATCH_FLAGS =
             PackageManager.MATCH_DIRECT_BOOT_AWARE
-            | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
-            | PackageManager.MATCH_UNINSTALLED_PACKAGES;
+                    | PackageManager.MATCH_DIRECT_BOOT_UNAWARE
+                    | PackageManager.MATCH_UNINSTALLED_PACKAGES;
+
+    @GuardedBy("mLock")
+    final SparseBooleanArray mUnlockedUsers = new SparseBooleanArray();
 
     // Stats
     @VisibleForTesting
@@ -293,10 +315,40 @@
         int GET_PACKAGE_INFO_WITH_SIG = 2;
         int GET_APPLICATION_INFO = 3;
         int LAUNCHER_PERMISSION_CHECK = 4;
+        int CLEANUP_DANGLING_BITMAPS = 5;
+        int GET_ACTIVITY_WITH_METADATA = 6;
+        int GET_INSTALLED_PACKAGES = 7;
+        int CHECK_PACKAGE_CHANGES = 8;
+        int GET_APPLICATION_RESOURCES = 9;
+        int RESOURCE_NAME_LOOKUP = 10;
+        int GET_LAUNCHER_ACTIVITY = 11;
+        int CHECK_LAUNCHER_ACTIVITY = 12;
+        int IS_ACTIVITY_ENABLED = 13;
+        int PACKAGE_UPDATE_CHECK = 14;
+        int ASYNC_PRELOAD_USER_DELAY = 15;
 
-        int COUNT = LAUNCHER_PERMISSION_CHECK + 1;
+        int COUNT = ASYNC_PRELOAD_USER_DELAY + 1;
     }
 
+    private static final String[] STAT_LABELS = {
+            "getHomeActivities()",
+            "Launcher permission check",
+            "getPackageInfo()",
+            "getPackageInfo(SIG)",
+            "getApplicationInfo",
+            "cleanupDanglingBitmaps",
+            "getActivity+metadata",
+            "getInstalledPackages",
+            "checkPackageChanges",
+            "getApplicationResources",
+            "resourceNameLookup",
+            "getLauncherActivity",
+            "checkLauncherActivity",
+            "isActivityEnabled",
+            "packageUpdateCheck",
+            "asyncPreloadUserDelay"
+    };
+
     final Object mStatLock = new Object();
 
     @GuardedBy("mStatLock")
@@ -308,23 +360,79 @@
     private static final int PROCESS_STATE_FOREGROUND_THRESHOLD =
             ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
 
+    static final int OPERATION_SET = 0;
+    static final int OPERATION_ADD = 1;
+    static final int OPERATION_UPDATE = 2;
+
+    /** @hide */
+    @IntDef(value = {
+            OPERATION_SET,
+            OPERATION_ADD,
+            OPERATION_UPDATE
+    })
+    @Retention(RetentionPolicy.SOURCE)
+    @interface ShortcutOperation {
+    }
+
+    @GuardedBy("mLock")
+    private int mWtfCount = 0;
+
+    @GuardedBy("mLock")
+    private Exception mLastWtfStacktrace;
+
+    static class InvalidFileFormatException extends Exception {
+        public InvalidFileFormatException(String message, Throwable cause) {
+            super(message, cause);
+        }
+    }
+
     public ShortcutService(Context context) {
-        this(context, BackgroundThread.get().getLooper());
+        this(context, BackgroundThread.get().getLooper(), /*onyForPackgeManagerApis*/ false);
     }
 
     @VisibleForTesting
-    ShortcutService(Context context, Looper looper) {
+    ShortcutService(Context context, Looper looper, boolean onlyForPackageManagerApis) {
         mContext = Preconditions.checkNotNull(context);
         LocalServices.addService(ShortcutServiceInternal.class, new LocalService());
         mHandler = new Handler(looper);
         mIPackageManager = AppGlobals.getPackageManager();
-        mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
-        mUserManager = context.getSystemService(UserManager.class);
+        mPackageManagerInternal = Preconditions.checkNotNull(
+                LocalServices.getService(PackageManagerInternal.class));
+        mUserManager = Preconditions.checkNotNull(context.getSystemService(UserManager.class));
+        mUsageStatsManagerInternal = Preconditions.checkNotNull(
+                LocalServices.getService(UsageStatsManagerInternal.class));
+        mActivityManagerInternal = Preconditions.checkNotNull(
+                LocalServices.getService(ActivityManagerInternal.class));
 
-        if (!FEATURE_ENABLED) {
-            return;
+        if (onlyForPackageManagerApis) {
+            return; // Don't do anything further.  For unit tests only.
         }
-        mPackageMonitor.register(context, looper, UserHandle.ALL, /* externalStorage= */ false);
+
+        // Register receivers.
+
+        // We need to set a priority, so let's just not use PackageMonitor for now.
+        // TODO Refactor PackageMonitor to support priorities.
+        final IntentFilter packageFilter = new IntentFilter();
+        packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
+        packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
+        packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
+        packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
+        packageFilter.addDataScheme("package");
+        packageFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
+        mContext.registerReceiverAsUser(mPackageMonitor, UserHandle.ALL,
+                packageFilter, null, mHandler);
+
+        final IntentFilter preferedActivityFilter = new IntentFilter();
+        preferedActivityFilter.addAction(Intent.ACTION_PREFERRED_ACTIVITY_CHANGED);
+        preferedActivityFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
+        mContext.registerReceiverAsUser(mPackageMonitor, UserHandle.ALL,
+                preferedActivityFilter, null, mHandler);
+
+        final IntentFilter localeFilter = new IntentFilter();
+        localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
+        localeFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
+        mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL,
+                localeFilter, null, mHandler);
 
         injectRegisterUidObserver(mUidObserver, ActivityManager.UID_OBSERVER_PROCSTATE
                 | ActivityManager.UID_OBSERVER_GONE);
@@ -333,27 +441,32 @@
     void logDurationStat(int statId, long start) {
         synchronized (mStatLock) {
             mCountStats[statId]++;
-            mDurationStats[statId] += (System.currentTimeMillis() - start);
+            mDurationStats[statId] += (injectElapsedRealtime() - start);
         }
     }
 
-    public long getLocaleChangeSequenceNumber() {
-        return mLocaleChangeSequenceNumber.get();
+    public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
+        // TODO This should get the per-user locale.  b/30123329 b/30119489
+        return LocaleList.getDefault().toLanguageTags();
     }
 
     final private IUidObserver mUidObserver = new IUidObserver.Stub() {
-        @Override public void onUidStateChanged(int uid, int procState) throws RemoteException {
+        @Override
+        public void onUidStateChanged(int uid, int procState) throws RemoteException {
             handleOnUidStateChanged(uid, procState);
         }
 
-        @Override public void onUidGone(int uid) throws RemoteException {
+        @Override
+        public void onUidGone(int uid) throws RemoteException {
             handleOnUidStateChanged(uid, ActivityManager.MAX_PROCESS_STATE);
         }
 
-        @Override public void onUidActive(int uid) throws RemoteException {
+        @Override
+        public void onUidActive(int uid) throws RemoteException {
         }
 
-        @Override public void onUidIdle(int uid) throws RemoteException {
+        @Override
+        public void onUidIdle(int uid) throws RemoteException {
         }
     };
 
@@ -374,7 +487,8 @@
     }
 
     private boolean isProcessStateForeground(int processState) {
-        return processState <= PROCESS_STATE_FOREGROUND_THRESHOLD;
+        return (processState != ActivityManager.PROCESS_STATE_NONEXISTENT)
+                && (processState <= PROCESS_STATE_FOREGROUND_THRESHOLD);
     }
 
     boolean isUidForegroundLocked(int uid) {
@@ -383,7 +497,13 @@
             // so it's foreground anyway.
             return true;
         }
-        return isProcessStateForeground(mUidState.get(uid, ActivityManager.MAX_PROCESS_STATE));
+        // First, check with the local cache.
+        if (isProcessStateForeground(mUidState.get(uid, ActivityManager.MAX_PROCESS_STATE))) {
+            return true;
+        }
+        // If the cache says background, reach out to AM.  Since it'll internally need to hold
+        // the AM lock, we use it as a last resort.
+        return isProcessStateForeground(mActivityManagerInternal.getUidProcessState(uid));
     }
 
     long getUidLastForegroundElapsedTimeLocked(int uid) {
@@ -424,7 +544,6 @@
 
     /** lifecycle event */
     void onBootPhase(int phase) {
-        // We want to call initialize() to initialize the configurations, so we don't disable this.
         if (DEBUG) {
             Slog.d(TAG, "onBootPhase: " + phase);
         }
@@ -432,29 +551,46 @@
             case SystemService.PHASE_LOCK_SETTINGS_READY:
                 initialize();
                 break;
+            case SystemService.PHASE_BOOT_COMPLETED:
+                mBootCompleted.set(true);
+                break;
         }
     }
 
     /** lifecycle event */
     void handleUnlockUser(int userId) {
-        if (!FEATURE_ENABLED) {
-            return;
+        if (DEBUG) {
+        Slog.d(TAG, "handleUnlockUser: user=" + userId);
         }
         synchronized (mLock) {
-            // Preload
-            getUserShortcutsLocked(userId);
-
-            checkPackageChanges(userId);
+            mUnlockedUsers.put(userId, true);
         }
+
+        // Preload the user data.
+        // Note, we don't use mHandler here but instead just start a new thread.
+        // This is because mHandler (which uses com.android.internal.os.BackgroundThread) is very
+        // busy at this point and this could take hundreds of milliseconds, which would be too
+        // late since the launcher would already have started.
+        // So we just create a new thread.  This code runs rarely, so we don't use a thread pool
+        // or anything.
+        final long start = injectElapsedRealtime();
+        injectRunOnNewThread(() -> {
+            synchronized (mLock) {
+                logDurationStat(Stats.ASYNC_PRELOAD_USER_DELAY, start);
+                getUserShortcutsLocked(userId);
+            }
+        });
     }
 
     /** lifecycle event */
     void handleCleanupUser(int userId) {
-        if (!FEATURE_ENABLED) {
-            return;
+        if (DEBUG) {
+            Slog.d(TAG, "handleCleanupUser: user=" + userId);
         }
         synchronized (mLock) {
             unloadUserLocked(userId);
+
+            mUnlockedUsers.put(userId, false);
         }
     }
 
@@ -520,16 +656,16 @@
         mMaxUpdatesPerInterval = Math.max(0, (int) parser.getLong(
                 ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL, DEFAULT_MAX_UPDATES_PER_INTERVAL));
 
-        mMaxDynamicShortcuts = Math.max(0, (int) parser.getLong(
+        mMaxShortcuts = Math.max(0, (int) parser.getLong(
                 ConfigConstants.KEY_MAX_SHORTCUTS, DEFAULT_MAX_SHORTCUTS_PER_APP));
 
         final int iconDimensionDp = Math.max(1, injectIsLowRamDevice()
                 ? (int) parser.getLong(
-                    ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM,
-                    DEFAULT_MAX_ICON_DIMENSION_LOWRAM_DP)
+                ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM,
+                DEFAULT_MAX_ICON_DIMENSION_LOWRAM_DP)
                 : (int) parser.getLong(
-                    ConfigConstants.KEY_MAX_ICON_DIMENSION_DP,
-                    DEFAULT_MAX_ICON_DIMENSION_DP));
+                ConfigConstants.KEY_MAX_ICON_DIMENSION_DP,
+                DEFAULT_MAX_ICON_DIMENSION_DP));
 
         mMaxIconDimension = injectDipToPixel(iconDimensionDp);
 
@@ -602,17 +738,27 @@
     }
 
     @Nullable
-    static Intent parseIntentAttribute(XmlPullParser parser, String attribute) {
+    static Intent parseIntentAttributeNoDefault(XmlPullParser parser, String attribute) {
         final String value = parseStringAttribute(parser, attribute);
-        if (TextUtils.isEmpty(value)) {
-            return null;
+        Intent parsed = null;
+        if (!TextUtils.isEmpty(value)) {
+            try {
+                parsed = Intent.parseUri(value, /* flags =*/ 0);
+            } catch (URISyntaxException e) {
+                Slog.e(TAG, "Error parsing intent", e);
+            }
         }
-        try {
-            return Intent.parseUri(value, /* flags =*/ 0);
-        } catch (URISyntaxException e) {
-            Slog.e(TAG, "Error parsing intent", e);
-            return null;
+        return parsed;
+    }
+
+    @Nullable
+    static Intent parseIntentAttribute(XmlPullParser parser, String attribute) {
+        Intent parsed = parseIntentAttributeNoDefault(parser, attribute);
+        if (parsed == null) {
+            // Default intent.
+            parsed = new Intent(Intent.ACTION_VIEW);
         }
+        return parsed;
     }
 
     static void writeTagValue(XmlSerializer out, String tag, String value) throws IOException {
@@ -641,10 +787,10 @@
         out.endTag(null, tag);
     }
 
-    static void writeAttr(XmlSerializer out, String name, String value) throws IOException {
+    static void writeAttr(XmlSerializer out, String name, CharSequence value) throws IOException {
         if (TextUtils.isEmpty(value)) return;
 
-        out.attribute(null, name, value);
+        out.attribute(null, name, value.toString());
     }
 
     static void writeAttr(XmlSerializer out, String name, long value) throws IOException {
@@ -687,8 +833,6 @@
 
             // Body.
             writeTagValue(out, TAG_LAST_RESET_TIME, mRawLastResetTime);
-            writeTagValue(out, TAG_LOCALE_CHANGE_SEQUENCE_NUMBER,
-                    mLocaleChangeSequenceNumber.get());
 
             // Epilogue.
             out.endTag(null, TAG_ROOT);
@@ -733,9 +877,6 @@
                     case TAG_LAST_RESET_TIME:
                         mRawLastResetTime = parseLongAttribute(parser, ATTR_VALUE);
                         break;
-                    case TAG_LOCALE_CHANGE_SEQUENCE_NUMBER:
-                        mLocaleChangeSequenceNumber.set(parseLongAttribute(parser, ATTR_VALUE));
-                        break;
                     default:
                         Slog.e(TAG, "Invalid tag: " + tag);
                         break;
@@ -743,7 +884,7 @@
             }
         } catch (FileNotFoundException e) {
             // Use the default
-        } catch (IOException|XmlPullParserException e) {
+        } catch (IOException | XmlPullParserException e) {
             Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e);
 
             mRawLastResetTime = 0;
@@ -752,12 +893,17 @@
         getLastResetTimeLocked();
     }
 
+    @VisibleForTesting
+    final File getUserFile(@UserIdInt int userId) {
+        return new File(injectUserDataPath(userId), FILENAME_USER_PACKAGES);
+    }
+
     private void saveUserLocked(@UserIdInt int userId) {
-        final File path = new File(injectUserDataPath(userId), FILENAME_USER_PACKAGES);
+        final File path = getUserFile(userId);
         if (DEBUG) {
             Slog.d(TAG, "Saving to " + path);
         }
-        path.mkdirs();
+        path.getParentFile().mkdirs();
         final AtomicFile file = new AtomicFile(path);
         FileOutputStream os = null;
         try {
@@ -766,7 +912,10 @@
             saveUserInternalLocked(userId, os, /* forBackup= */ false);
 
             file.finishWrite(os);
-        } catch (XmlPullParserException|IOException e) {
+
+            // Remove all dangling bitmap files.
+            cleanupDanglingBitmapDirectoriesLocked(userId);
+        } catch (XmlPullParserException | IOException e) {
             Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e);
             file.failWrite(os);
         }
@@ -782,7 +931,7 @@
         out.setOutput(bos, StandardCharsets.UTF_8.name());
         out.startDocument(null, true);
 
-        getUserShortcutsLocked(userId).saveToXml(this, out, forBackup);
+        getUserShortcutsLocked(userId).saveToXml(out, forBackup);
 
         out.endDocument();
 
@@ -800,7 +949,7 @@
 
     @Nullable
     private ShortcutUser loadUserLocked(@UserIdInt int userId) {
-        final File path = new File(injectUserDataPath(userId), FILENAME_USER_PACKAGES);
+        final File path = getUserFile(userId);
         if (DEBUG) {
             Slog.d(TAG, "Loading from " + path);
         }
@@ -816,8 +965,9 @@
             return null;
         }
         try {
-            return loadUserInternal(userId, in, /* forBackup= */ false);
-        } catch (IOException|XmlPullParserException e) {
+            final ShortcutUser ret = loadUserInternal(userId, in, /* forBackup= */ false);
+            return ret;
+        } catch (IOException | XmlPullParserException | InvalidFileFormatException e) {
             Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e);
             return null;
         } finally {
@@ -826,7 +976,8 @@
     }
 
     private ShortcutUser loadUserInternal(@UserIdInt int userId, InputStream is,
-            boolean fromBackup) throws XmlPullParserException, IOException {
+            boolean fromBackup) throws XmlPullParserException, IOException,
+            InvalidFileFormatException {
 
         final BufferedInputStream bis = new BufferedInputStream(is);
 
@@ -885,16 +1036,20 @@
         if (DEBUG) {
             Slog.d(TAG, "saveDirtyInfo");
         }
-        synchronized (mLock) {
-            for (int i = mDirtyUserIds.size() - 1; i >= 0; i--) {
-                final int userId = mDirtyUserIds.get(i);
-                if (userId == UserHandle.USER_NULL) { // USER_NULL for base state.
-                    saveBaseStateLocked();
-                } else {
-                    saveUserLocked(userId);
+        try {
+            synchronized (mLock) {
+                for (int i = mDirtyUserIds.size() - 1; i >= 0; i--) {
+                    final int userId = mDirtyUserIds.get(i);
+                    if (userId == UserHandle.USER_NULL) { // USER_NULL for base state.
+                        saveBaseStateLocked();
+                    } else {
+                        saveUserLocked(userId);
+                    }
                 }
+                mDirtyUserIds.clear();
             }
-            mDirtyUserIds.clear();
+        } catch (Exception e) {
+            wtf("Exception in saveDirtyInfo", e);
         }
     }
 
@@ -944,6 +1099,31 @@
         }
     }
 
+    // Requires mLock held, but "Locked" prefix would look weired so we just say "L".
+    protected boolean isUserUnlockedL(@UserIdInt int userId) {
+        // First, check the local copy.
+        if (mUnlockedUsers.get(userId)) {
+            return true;
+        }
+        // If the local copy says the user is locked, check with AM for the actual state, since
+        // the user might just have been unlocked.
+        // Note we just don't use isUserUnlockingOrUnlocked() here, because it'll return false
+        // when the user is STOPPING, which we still want to consider as "unlocked".
+        final long token = injectClearCallingIdentity();
+        try {
+            return mUserManager.isUserUnlockingOrUnlocked(userId);
+        } finally {
+            injectRestoreCallingIdentity(token);
+        }
+    }
+
+    // Requires mLock held, but "Locked" prefix would look weired so we jsut say "L".
+    void throwIfUserLockedL(@UserIdInt int userId) {
+        if (!isUserUnlockedL(userId)) {
+            throw new IllegalStateException("User " + userId + " is locked or not running");
+        }
+    }
+
     @GuardedBy("mLock")
     @NonNull
     private boolean isUserLoadedLocked(@UserIdInt int userId) {
@@ -954,13 +1134,20 @@
     @GuardedBy("mLock")
     @NonNull
     ShortcutUser getUserShortcutsLocked(@UserIdInt int userId) {
+        if (!isUserUnlockedL(userId)) {
+            wtf("User still locked");
+        }
+
         ShortcutUser userPackages = mUsers.get(userId);
         if (userPackages == null) {
             userPackages = loadUserLocked(userId);
             if (userPackages == null) {
-                userPackages = new ShortcutUser(userId);
+                userPackages = new ShortcutUser(this, userId);
             }
             mUsers.put(userId, userPackages);
+
+            // Also when a user's data is first accessed, scan all packages.
+            checkPackageChanges(userId);
         }
         return userPackages;
     }
@@ -971,12 +1158,25 @@
         }
     }
 
-    /** Return the per-user per-package state. */
+    /**
+     * Return the per-user per-package state.  If the caller is a publisher, use
+     * {@link #getPackageShortcutsForPublisherLocked} instead.
+     */
     @GuardedBy("mLock")
     @NonNull
     ShortcutPackage getPackageShortcutsLocked(
             @NonNull String packageName, @UserIdInt int userId) {
-        return getUserShortcutsLocked(userId).getPackageShortcuts(this, packageName);
+        return getUserShortcutsLocked(userId).getPackageShortcuts(packageName);
+    }
+
+    /** Return the per-user per-package state.  Use this when the caller is a publisher. */
+    @GuardedBy("mLock")
+    @NonNull
+    ShortcutPackage getPackageShortcutsForPublisherLocked(
+            @NonNull String packageName, @UserIdInt int userId) {
+        final ShortcutPackage ret = getUserShortcutsLocked(userId).getPackageShortcuts(packageName);
+        ret.getUser().onCalledByPublisher(packageName);
+        return ret;
     }
 
     @GuardedBy("mLock")
@@ -985,22 +1185,17 @@
             @NonNull String packageName, @UserIdInt int ownerUserId,
             @UserIdInt int launcherUserId) {
         return getUserShortcutsLocked(ownerUserId)
-                .getLauncherShortcuts(this, packageName, launcherUserId);
+                .getLauncherShortcuts(packageName, launcherUserId);
     }
 
     // === Caller validation ===
 
     void removeIcon(@UserIdInt int userId, ShortcutInfo shortcut) {
-        if (shortcut.getBitmapPath() != null) {
-            if (DEBUG) {
-                Slog.d(TAG, "Removing " + shortcut.getBitmapPath());
-            }
-            new File(shortcut.getBitmapPath()).delete();
-
-            shortcut.setBitmapPath(null);
-            shortcut.setIconResourceId(0);
-            shortcut.clearFlags(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES);
-        }
+        // Do not remove the actual bitmap file yet, because if the device crashes before saving
+        // he XML we'd lose the icon.  We just remove all dangling files after saving the XML.
+        shortcut.setIconResourceId(0);
+        shortcut.setIconResName(null);
+        shortcut.clearFlags(ShortcutInfo.FLAG_HAS_ICON_FILE | ShortcutInfo.FLAG_HAS_ICON_RES);
     }
 
     public void cleanupBitmapsForPackage(@UserIdInt int userId, String packageName) {
@@ -1013,6 +1208,58 @@
         }
     }
 
+    private void cleanupDanglingBitmapDirectoriesLocked(@UserIdInt int userId) {
+        if (DEBUG) {
+            Slog.d(TAG, "cleanupDanglingBitmaps: userId=" + userId);
+        }
+        final long start = injectElapsedRealtime();
+
+        final ShortcutUser user = getUserShortcutsLocked(userId);
+
+        final File bitmapDir = getUserBitmapFilePath(userId);
+        final File[] children = bitmapDir.listFiles();
+        if (children == null) {
+            return;
+        }
+        for (File child : children) {
+            if (!child.isDirectory()) {
+                continue;
+            }
+            final String packageName = child.getName();
+            if (DEBUG) {
+                Slog.d(TAG, "cleanupDanglingBitmaps: Found directory=" + packageName);
+            }
+            if (!user.hasPackage(packageName)) {
+                if (DEBUG) {
+                    Slog.d(TAG, "Removing dangling bitmap directory: " + packageName);
+                }
+                cleanupBitmapsForPackage(userId, packageName);
+            } else {
+                cleanupDanglingBitmapFilesLocked(userId, user, packageName, child);
+            }
+        }
+        logDurationStat(Stats.CLEANUP_DANGLING_BITMAPS, start);
+    }
+
+    private void cleanupDanglingBitmapFilesLocked(@UserIdInt int userId, @NonNull ShortcutUser user,
+            @NonNull String packageName, @NonNull File path) {
+        final ArraySet<String> usedFiles =
+                user.getPackageShortcuts(packageName).getUsedBitmapFiles();
+
+        for (File child : path.listFiles()) {
+            if (!child.isFile()) {
+                continue;
+            }
+            final String name = child.getName();
+            if (!usedFiles.contains(name)) {
+                if (DEBUG) {
+                    Slog.d(TAG, "Removing dangling bitmap file: " + child.getAbsolutePath());
+                }
+                child.delete();
+            }
+        }
+    }
+
     @VisibleForTesting
     static class FileOutputStreamWithPath extends FileOutputStream {
         private final File mFile;
@@ -1036,7 +1283,7 @@
     FileOutputStreamWithPath openIconFileForWrite(@UserIdInt int userId, ShortcutInfo shortcut)
             throws IOException {
         final File packagePath = new File(getUserBitmapFilePath(userId),
-                shortcut.getPackageName());
+                shortcut.getPackage());
         if (!packagePath.isDirectory()) {
             packagePath.mkdirs();
             if (!packagePath.isDirectory()) {
@@ -1046,7 +1293,7 @@
         }
 
         final String baseName = String.valueOf(injectCurrentTimeMillis());
-        for (int suffix = 0;; suffix++) {
+        for (int suffix = 0; ; suffix++) {
             final String filename = (suffix == 0 ? baseName : baseName + "_" + suffix) + ".png";
             final File file = new File(packagePath, filename);
             if (!file.exists()) {
@@ -1066,8 +1313,7 @@
         final long token = injectClearCallingIdentity();
         try {
             // Clear icon info on the shortcut.
-            shortcut.setIconResourceId(0);
-            shortcut.setBitmapPath(null);
+            removeIcon(userId, shortcut);
 
             final Icon icon = shortcut.getIcon();
             if (icon == null) {
@@ -1075,7 +1321,6 @@
             }
 
             Bitmap bitmap;
-            Bitmap bitmapToRecycle = null;
             try {
                 switch (icon.getType()) {
                     case Icon.TYPE_RESOURCE: {
@@ -1119,7 +1364,7 @@
                     } finally {
                         IoUtils.closeQuietly(out);
                     }
-                } catch (IOException|RuntimeException e) {
+                } catch (IOException | RuntimeException e) {
                     // STOPSHIP Change wtf to e
                     Slog.wtf(ShortcutService.TAG, "Unable to write bitmap to file", e);
                     if (path != null && path.exists()) {
@@ -1127,9 +1372,6 @@
                     }
                 }
             } finally {
-                if (bitmapToRecycle != null) {
-                    bitmapToRecycle.recycle();
-                }
                 // Once saved, we won't use the original icon information, so null it out.
                 shortcut.clearIcon();
             }
@@ -1142,7 +1384,7 @@
     // so override in unit tests.
     // TODO CTS this case.
     void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
-        if (!shortcut.getPackageName().equals(icon.getResPackage())) {
+        if (!shortcut.getPackage().equals(icon.getResPackage())) {
             throw new IllegalArgumentException(
                     "Icon resource must reside in shortcut owner package");
         }
@@ -1179,11 +1421,29 @@
         return scaledBitmap;
     }
 
+    /**
+     * For a shortcut, update all resource names from resource IDs, and also update all
+     * resource-based strings.
+     */
+    void fixUpShortcutResourceNamesAndValues(ShortcutInfo si) {
+        final Resources publisherRes = injectGetResourcesForApplicationAsUser(
+                si.getPackage(), si.getUserId());
+        if (publisherRes != null) {
+            final long start = injectElapsedRealtime();
+            try {
+                si.lookupAndFillInResourceNames(publisherRes);
+            } finally {
+                logDurationStat(Stats.RESOURCE_NAME_LOOKUP, start);
+            }
+            si.resolveResourceStrings(publisherRes);
+        }
+    }
+
     // === Caller validation ===
 
     private boolean isCallerSystem() {
         final int callingUid = injectBinderCallingUid();
-         return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID);
+        return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID);
     }
 
     private boolean isCallerShell() {
@@ -1192,26 +1452,39 @@
     }
 
     private void enforceSystemOrShell() {
-        Preconditions.checkState(isCallerSystem() || isCallerShell(),
-                "Caller must be system or shell");
+        if (!(isCallerSystem() || isCallerShell())) {
+            throw new SecurityException("Caller must be system or shell");
+        }
     }
 
     private void enforceShell() {
-        Preconditions.checkState(isCallerShell(), "Caller must be shell");
+        if (!isCallerShell()) {
+            throw new SecurityException("Caller must be shell");
+        }
     }
 
     private void enforceSystem() {
-        Preconditions.checkState(isCallerSystem(), "Caller must be system");
+        if (!isCallerSystem()) {
+            throw new SecurityException("Caller must be system");
+        }
     }
 
     private void enforceResetThrottlingPermission() {
         if (isCallerSystem()) {
             return;
         }
-        injectEnforceCallingPermission(
+        enforceCallingOrSelfPermission(
                 android.Manifest.permission.RESET_SHORTCUT_MANAGER_THROTTLING, null);
     }
 
+    private void enforceCallingOrSelfPermission(
+            @NonNull String permission, @Nullable String message) {
+        if (isCallerSystem()) {
+            return;
+        }
+        injectEnforceCallingPermission(permission, message);
+    }
+
     /**
      * Somehow overriding ServiceContext.enforceCallingPermission() in the unit tests would confuse
      * mockito.  So instead we extracted it here and override it in the tests.
@@ -1241,20 +1514,33 @@
         throw new SecurityException("Calling package name mismatch");
     }
 
-    void postToHandler(Runnable r) {
+    // Overridden in unit tests to execute r synchronously.
+    void injectPostToHandler(Runnable r) {
         mHandler.post(r);
     }
 
+    void injectRunOnNewThread(Runnable r) {
+        new Thread(r).start();
+    }
+
     /**
-     * Throw if {@code numShortcuts} is bigger than {@link #mMaxDynamicShortcuts}.
+     * @throws IllegalArgumentException if {@code numShortcuts} is bigger than
+     *                                  {@link #getMaxActivityShortcuts()}.
      */
-    void enforceMaxDynamicShortcuts(int numShortcuts) {
-        if (numShortcuts > mMaxDynamicShortcuts) {
+    void enforceMaxActivityShortcuts(int numShortcuts) {
+        if (numShortcuts > mMaxShortcuts) {
             throw new IllegalArgumentException("Max number of dynamic shortcuts exceeded");
         }
     }
 
     /**
+     * Return the max number of dynamic + manifest shortcuts for each launcher icon.
+     */
+    int getMaxActivityShortcuts() {
+        return mMaxShortcuts;
+    }
+
+    /**
      * - Sends a notification to LauncherApps
      * - Write to file
      */
@@ -1268,17 +1554,21 @@
     }
 
     private void notifyListeners(@NonNull String packageName, @UserIdInt int userId) {
-        if (!mUserManager.isUserRunning(userId)) {
-            return;
-        }
-        postToHandler(() -> {
-            final ArrayList<ShortcutChangeListener> copy;
-            synchronized (mLock) {
-                copy = new ArrayList<>(mListeners);
-            }
-            // Note onShortcutChanged() needs to be called with the system service permissions.
-            for (int i = copy.size() - 1; i >= 0; i--) {
-                copy.get(i).onShortcutChanged(packageName, userId);
+        injectPostToHandler(() -> {
+            try {
+                final ArrayList<ShortcutChangeListener> copy;
+                synchronized (mLock) {
+                    if (!isUserUnlockedL(userId)) {
+                        return;
+                    }
+
+                    copy = new ArrayList<>(mListeners);
+                }
+                // Note onShortcutChanged() needs to be called with the system service permissions.
+                for (int i = copy.size() - 1; i >= 0; i--) {
+                    copy.get(i).onShortcutChanged(packageName, userId);
+                }
+            } catch (Exception ignore) {
             }
         });
     }
@@ -1287,72 +1577,61 @@
      * Clean up / validate an incoming shortcut.
      * - Make sure all mandatory fields are set.
      * - Make sure the intent's extras are persistable, and them to set
-     *  {@link ShortcutInfo#mIntentPersistableExtras}.  Also clear its extras.
+     * {@link ShortcutInfo#mIntentPersistableExtrases}.  Also clear its extras.
      * - Clear flags.
      *
      * TODO Detailed unit tests
      */
     private void fixUpIncomingShortcutInfo(@NonNull ShortcutInfo shortcut, boolean forUpdate) {
         Preconditions.checkNotNull(shortcut, "Null shortcut detected");
-        if (shortcut.getActivityComponent() != null) {
+        if (shortcut.getActivity() != null) {
             Preconditions.checkState(
-                    shortcut.getPackageName().equals(
-                            shortcut.getActivityComponent().getPackageName()),
-                    "Activity package name mismatch");
+                    shortcut.getPackage().equals(shortcut.getActivity().getPackageName()),
+                    "Cannot publish shortcut: activity " + shortcut.getActivity() + " does not"
+                    + " belong to package " + shortcut.getPackage());
+            Preconditions.checkState(
+                    injectIsMainActivity(shortcut.getActivity(), shortcut.getUserId()),
+                    "Cannot publish shortcut: activity " + shortcut.getActivity() + " is not"
+                            + " main activity");
         }
 
         if (!forUpdate) {
             shortcut.enforceMandatoryFields();
+            Preconditions.checkArgument(
+                    injectIsMainActivity(shortcut.getActivity(), shortcut.getUserId()),
+                    "Cannot publish shortcut: " + shortcut.getActivity() + " is not main activity");
         }
         if (shortcut.getIcon() != null) {
             ShortcutInfo.validateIcon(shortcut.getIcon());
         }
 
-        validateForXml(shortcut.getId());
-        validateForXml(shortcut.getTitle());
-        validatePersistableBundleForXml(shortcut.getIntentPersistableExtras());
-        validatePersistableBundleForXml(shortcut.getExtras());
-
         shortcut.replaceFlags(0);
     }
 
-    // KXmlSerializer is strict and doesn't allow certain characters, so we disallow those
-    // characters.
+    /**
+     * When a shortcut has no target activity, set the default one from the package.
+     */
+    private void fillInDefaultActivity(List<ShortcutInfo> shortcuts) {
 
-    private static void validatePersistableBundleForXml(PersistableBundle b) {
-        if (b == null || b.size() == 0) {
-            return;
-        }
-        for (String key : b.keySet()) {
-            validateForXml(key);
-            final Object value = b.get(key);
-            if (value == null) {
-                continue;
-            } else if (value instanceof String) {
-                validateForXml((String) value);
-            } else if (value instanceof String[]) {
-                for (String v : (String[]) value) {
-                    validateForXml(v);
+        ComponentName defaultActivity = null;
+        for (int i = shortcuts.size() - 1; i >= 0; i--) {
+            final ShortcutInfo si = shortcuts.get(i);
+            if (si.getActivity() == null) {
+                if (defaultActivity == null) {
+                    defaultActivity = injectGetDefaultMainActivity(
+                            si.getPackage(), si.getUserId());
+                    Preconditions.checkState(defaultActivity != null,
+                            "Launcher activity not found for package " + si.getPackage());
                 }
-            } else if (value instanceof PersistableBundle) {
-                validatePersistableBundleForXml((PersistableBundle) value);
+                si.setActivity(defaultActivity);
             }
         }
     }
 
-    private static void validateForXml(String s) {
-        if (TextUtils.isEmpty(s)) {
-            return;
+    private void assignImplicitRanks(List<ShortcutInfo> shortcuts) {
+        for (int i = shortcuts.size() - 1; i >= 0; i--) {
+            shortcuts.get(i).setImplicitRank(i);
         }
-        for (int i = s.length() - 1; i >= 0; i--) {
-            if (!isAllowedInXml(s.charAt(i))) {
-                throw new IllegalArgumentException("Unsupported character detected in: " + s);
-            }
-        }
-    }
-
-    private static boolean isAllowedInXml(char c) {
-        return (c >= 0x20 && c <= 0xd7ff) || (c >= 0xe000 && c <= 0xfffd);
     }
 
     // === APIs ===
@@ -1366,29 +1645,45 @@
         final int size = newShortcuts.size();
 
         synchronized (mLock) {
-            final ShortcutPackage ps = getPackageShortcutsLocked(packageName, userId);
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+
+            ps.ensureImmutableShortcutsNotIncluded(newShortcuts);
+
+            fillInDefaultActivity(newShortcuts);
+
+            ps.enforceShortcutCountsBeforeOperation(newShortcuts, OPERATION_SET);
 
             // Throttling.
-            if (!ps.tryApiCall(this)) {
+            if (!ps.tryApiCall()) {
                 return false;
             }
-            enforceMaxDynamicShortcuts(size);
 
-            // Validate the shortcuts.
+            // Initialize the implicit ranks for ShortcutPackage.adjustRanks().
+            ps.clearAllImplicitRanks();
+            assignImplicitRanks(newShortcuts);
+
             for (int i = 0; i < size; i++) {
                 fixUpIncomingShortcutInfo(newShortcuts.get(i), /* forUpdate= */ false);
             }
 
             // First, remove all un-pinned; dynamic shortcuts
-            ps.deleteAllDynamicShortcuts(this);
+            ps.deleteAllDynamicShortcuts();
 
             // Then, add/update all.  We need to make sure to take over "pinned" flag.
             for (int i = 0; i < size; i++) {
                 final ShortcutInfo newShortcut = newShortcuts.get(i);
-                ps.addDynamicShortcut(this, newShortcut);
+                ps.addOrUpdateDynamicShortcut(newShortcut);
             }
+
+            // Lastly, adjust the ranks.
+            ps.adjustRanks();
         }
         packageShortcutsChanged(packageName, userId);
+
+        verifyStates();
+
         return true;
     }
 
@@ -1401,34 +1696,74 @@
         final int size = newShortcuts.size();
 
         synchronized (mLock) {
-            final ShortcutPackage ps = getPackageShortcutsLocked(packageName, userId);
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+
+            ps.ensureImmutableShortcutsNotIncluded(newShortcuts);
+
+            // For update, don't fill in the default activity.  Having null activity means
+            // "don't update the activity" here.
+
+            ps.enforceShortcutCountsBeforeOperation(newShortcuts, OPERATION_UPDATE);
 
             // Throttling.
-            if (!ps.tryApiCall(this)) {
+            if (!ps.tryApiCall()) {
                 return false;
             }
 
+            // Initialize the implicit ranks for ShortcutPackage.adjustRanks().
+            ps.clearAllImplicitRanks();
+            assignImplicitRanks(newShortcuts);
+
             for (int i = 0; i < size; i++) {
                 final ShortcutInfo source = newShortcuts.get(i);
                 fixUpIncomingShortcutInfo(source, /* forUpdate= */ true);
 
                 final ShortcutInfo target = ps.findShortcutById(source.getId());
-                if (target != null) {
-                    final boolean replacingIcon = (source.getIcon() != null);
-                    if (replacingIcon) {
-                        removeIcon(userId, target);
-                    }
+                if (target == null) {
+                    continue;
+                }
 
-                    target.copyNonNullFieldsFrom(source);
+                if (target.isEnabled() != source.isEnabled()) {
+                    Slog.w(TAG,
+                            "ShortcutInfo.enabled cannot be changed with updateShortcuts()");
+                }
 
-                    if (replacingIcon) {
-                        saveIconAndFixUpShortcut(userId, target);
-                    }
+                // When updating the rank, we need to insert between existing ranks, so set
+                // this setRankChanged, and also copy the implicit rank fo adjustRanks().
+                if (source.hasRank()) {
+                    target.setRankChanged();
+                    target.setImplicitRank(source.getImplicitRank());
+                }
+
+                final boolean replacingIcon = (source.getIcon() != null);
+                if (replacingIcon) {
+                    removeIcon(userId, target);
+                }
+
+                // Note copyNonNullFieldsFrom() does the "updatable with?" check too.
+                target.copyNonNullFieldsFrom(source);
+                target.setTimestamp(injectCurrentTimeMillis());
+
+                if (replacingIcon) {
+                    saveIconAndFixUpShortcut(userId, target);
+                }
+
+                // When we're updating any resource related fields, re-extract the res names and
+                // the values.
+                if (replacingIcon || source.hasStringResources()) {
+                    fixUpShortcutResourceNamesAndValues(target);
                 }
             }
+
+            // Lastly, adjust the ranks.
+            ps.adjustRanks();
         }
         packageShortcutsChanged(packageName, userId);
 
+        verifyStates();
+
         return true;
     }
 
@@ -1441,10 +1776,22 @@
         final int size = newShortcuts.size();
 
         synchronized (mLock) {
-            final ShortcutPackage ps = getPackageShortcutsLocked(packageName, userId);
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+
+            ps.ensureImmutableShortcutsNotIncluded(newShortcuts);
+
+            fillInDefaultActivity(newShortcuts);
+
+            ps.enforceShortcutCountsBeforeOperation(newShortcuts, OPERATION_ADD);
+
+            // Initialize the implicit ranks for ShortcutPackage.adjustRanks().
+            ps.clearAllImplicitRanks();
+            assignImplicitRanks(newShortcuts);
 
             // Throttling.
-            if (!ps.tryApiCall(this)) {
+            if (!ps.tryApiCall()) {
                 return false;
             }
             for (int i = 0; i < size; i++) {
@@ -1453,13 +1800,73 @@
                 // Validate the shortcut.
                 fixUpIncomingShortcutInfo(newShortcut, /* forUpdate= */ false);
 
+                // When ranks are changing, we need to insert between ranks, so set the
+                // "rank changed" flag.
+                newShortcut.setRankChanged();
+
                 // Add it.
-                ps.addDynamicShortcut(this, newShortcut);
+                ps.addOrUpdateDynamicShortcut(newShortcut);
+            }
+
+            // Lastly, adjust the ranks.
+            ps.adjustRanks();
+        }
+        packageShortcutsChanged(packageName, userId);
+
+        verifyStates();
+
+        return true;
+    }
+
+    @Override
+    public void disableShortcuts(String packageName, List shortcutIds,
+            CharSequence disabledMessage, int disabledMessageResId, @UserIdInt int userId) {
+        verifyCaller(packageName, userId);
+        Preconditions.checkNotNull(shortcutIds, "shortcutIds must be provided");
+
+        synchronized (mLock) {
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+
+            ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds);
+
+            final String disabledMessageString =
+                    (disabledMessage == null) ? null : disabledMessage.toString();
+
+            for (int i = shortcutIds.size() - 1; i >= 0; i--) {
+                ps.disableWithId(Preconditions.checkStringNotEmpty((String) shortcutIds.get(i)),
+                        disabledMessageString, disabledMessageResId,
+                        /* overrideImmutable=*/ false);
+            }
+
+            // We may have removed dynamic shortcuts which may have left a gap, so adjust the ranks.
+            ps.adjustRanks();
+        }
+        packageShortcutsChanged(packageName, userId);
+
+        verifyStates();
+    }
+
+    @Override
+    public void enableShortcuts(String packageName, List shortcutIds, @UserIdInt int userId) {
+        verifyCaller(packageName, userId);
+        Preconditions.checkNotNull(shortcutIds, "shortcutIds must be provided");
+
+        synchronized (mLock) {
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+
+            ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds);
+
+            for (int i = shortcutIds.size() - 1; i >= 0; i--) {
+                ps.enableWithId((String) shortcutIds.get(i));
             }
         }
         packageShortcutsChanged(packageName, userId);
 
-        return true;
+        verifyStates();
     }
 
     @Override
@@ -1469,12 +1876,23 @@
         Preconditions.checkNotNull(shortcutIds, "shortcutIds must be provided");
 
         synchronized (mLock) {
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+
+            ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds);
+
             for (int i = shortcutIds.size() - 1; i >= 0; i--) {
-                getPackageShortcutsLocked(packageName, userId).deleteDynamicWithId(this,
+                ps.deleteDynamicWithId(
                         Preconditions.checkStringNotEmpty((String) shortcutIds.get(i)));
             }
+
+            // We may have removed dynamic shortcuts which may have left a gap, so adjust the ranks.
+            ps.adjustRanks();
         }
         packageShortcutsChanged(packageName, userId);
+
+        verifyStates();
     }
 
     @Override
@@ -1482,16 +1900,24 @@
         verifyCaller(packageName, userId);
 
         synchronized (mLock) {
-            getPackageShortcutsLocked(packageName, userId).deleteAllDynamicShortcuts(this);
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+            ps.deleteAllDynamicShortcuts();
         }
         packageShortcutsChanged(packageName, userId);
+
+        verifyStates();
     }
 
     @Override
     public ParceledListSlice<ShortcutInfo> getDynamicShortcuts(String packageName,
             @UserIdInt int userId) {
         verifyCaller(packageName, userId);
+
         synchronized (mLock) {
+            throwIfUserLockedL(userId);
+
             return getShortcutsWithQueryLocked(
                     packageName, userId, ShortcutInfo.CLONE_REMOVE_FOR_CREATOR,
                     ShortcutInfo::isDynamic);
@@ -1499,10 +1925,27 @@
     }
 
     @Override
+    public ParceledListSlice<ShortcutInfo> getManifestShortcuts(String packageName,
+            @UserIdInt int userId) {
+        verifyCaller(packageName, userId);
+
+        synchronized (mLock) {
+            throwIfUserLockedL(userId);
+
+            return getShortcutsWithQueryLocked(
+                    packageName, userId, ShortcutInfo.CLONE_REMOVE_FOR_CREATOR,
+                    ShortcutInfo::isManifestShortcut);
+        }
+    }
+
+    @Override
     public ParceledListSlice<ShortcutInfo> getPinnedShortcuts(String packageName,
             @UserIdInt int userId) {
         verifyCaller(packageName, userId);
+
         synchronized (mLock) {
+            throwIfUserLockedL(userId);
+
             return getShortcutsWithQueryLocked(
                     packageName, userId, ShortcutInfo.CLONE_REMOVE_FOR_CREATOR,
                     ShortcutInfo::isPinned);
@@ -1514,17 +1957,18 @@
 
         final ArrayList<ShortcutInfo> ret = new ArrayList<>();
 
-        getPackageShortcutsLocked(packageName, userId).findAll(this, ret, query, cloneFlags);
+        final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+        ps.findAll(ret, query, cloneFlags);
 
         return new ParceledListSlice<>(ret);
     }
 
     @Override
-    public int getMaxDynamicShortcutCount(String packageName, @UserIdInt int userId)
+    public int getMaxShortcutCountPerActivity(String packageName, @UserIdInt int userId)
             throws RemoteException {
         verifyCaller(packageName, userId);
 
-        return mMaxDynamicShortcuts;
+        return mMaxShortcuts;
     }
 
     @Override
@@ -1532,8 +1976,10 @@
         verifyCaller(packageName, userId);
 
         synchronized (mLock) {
-            return mMaxUpdatesPerInterval
-                    - getPackageShortcutsLocked(packageName, userId).getApiCallCount(this);
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+            return mMaxUpdatesPerInterval - ps.getApiCallCount();
         }
     }
 
@@ -1542,12 +1988,14 @@
         verifyCaller(packageName, userId);
 
         synchronized (mLock) {
+            throwIfUserLockedL(userId);
+
             return getNextResetTimeLocked();
         }
     }
 
     @Override
-    public int getIconMaxDimensions(String packageName, int userId) throws RemoteException {
+    public int getIconMaxDimensions(String packageName, int userId) {
         verifyCaller(packageName, userId);
 
         synchronized (mLock) {
@@ -1555,8 +2003,40 @@
         }
     }
 
+    @Override
+    public void reportShortcutUsed(String packageName, String shortcutId, int userId) {
+        verifyCaller(packageName, userId);
+
+        Preconditions.checkNotNull(shortcutId);
+
+        if (DEBUG) {
+            Slog.d(TAG, String.format("reportShortcutUsed: Shortcut %s package %s used on user %d",
+                    shortcutId, packageName, userId));
+        }
+
+        synchronized (mLock) {
+            throwIfUserLockedL(userId);
+
+            final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);
+
+            if (ps.findShortcutById(shortcutId) == null) {
+                Log.w(TAG, String.format("reportShortcutUsed: package %s doesn't have shortcut %s",
+                        packageName, shortcutId));
+                return;
+            }
+        }
+
+        final long token = injectClearCallingIdentity();
+        try {
+            mUsageStatsManagerInternal.reportShortcutUsage(packageName, shortcutId, userId);
+        } finally {
+            injectRestoreCallingIdentity(token);
+        }
+    }
+
     /**
-     * Reset all throttling, for developer options and command line.  Only system/shell can call it.
+     * Reset all throttling, for developer options and command line.  Only system/shell can call
+     * it.
      */
     @Override
     public void resetThrottling() {
@@ -1567,6 +2047,11 @@
 
     void resetThrottlingInner(@UserIdInt int userId) {
         synchronized (mLock) {
+            if (!isUserUnlockedL(userId)) {
+                Log.w(TAG, "User " + userId + " is locked or not running");
+                return;
+            }
+
             getUserShortcutsLocked(userId).resetThrottling();
         }
         scheduleSaveUser(userId);
@@ -1581,26 +2066,33 @@
         Slog.i(TAG, "ShortcutManager: throttling counter reset for all users");
     }
 
-    void resetPackageThrottling(String packageName, int userId) {
-        synchronized (mLock) {
-            getPackageShortcutsLocked(packageName, userId)
-                    .resetRateLimitingForCommandLineNoSaving();
-            saveUserLocked(userId);
-        }
-    }
-
     @Override
     public void onApplicationActive(String packageName, int userId) {
         if (DEBUG) {
             Slog.d(TAG, "onApplicationActive: package=" + packageName + "  userid=" + userId);
         }
         enforceResetThrottlingPermission();
-        resetPackageThrottling(packageName, userId);
+
+        synchronized (mLock) {
+            if (!isUserUnlockedL(userId)) {
+                // This is called by system UI, so no need to throw.  Just ignore.
+                return;
+            }
+
+            getPackageShortcutsLocked(packageName, userId)
+                    .resetRateLimitingForCommandLineNoSaving();
+            saveUserLocked(userId);
+        }
     }
 
     // We override this method in unit tests to do a simpler check.
     boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId) {
-        return hasShortcutHostPermissionInner(callingPackage, userId);
+        final long start = injectElapsedRealtime();
+        try {
+            return hasShortcutHostPermissionInner(callingPackage, userId);
+        } finally {
+            logDurationStat(Stats.LAUNCHER_PERMISSION_CHECK, start);
+        }
     }
 
     // This method is extracted so we can directly call this method from unit tests,
@@ -1608,15 +2100,24 @@
     @VisibleForTesting
     boolean hasShortcutHostPermissionInner(@NonNull String callingPackage, int userId) {
         synchronized (mLock) {
-            final long start = System.currentTimeMillis();
+            throwIfUserLockedL(userId);
 
             final ShortcutUser user = getUserShortcutsLocked(userId);
 
+            // Always trust the in-memory cache.
+            final ComponentName cached = user.getCachedLauncher();
+            if (cached != null) {
+                if (cached.getPackageName().equals(callingPackage)) {
+                    return true;
+                }
+            }
+            // If the cached one doesn't match, then go ahead
+
             final List<ResolveInfo> allHomeCandidates = new ArrayList<>();
 
             // Default launcher from package manager.
-            final long startGetHomeActivitiesAsUser = System.currentTimeMillis();
-            final ComponentName defaultLauncher = injectPackageManagerInternal()
+            final long startGetHomeActivitiesAsUser = injectElapsedRealtime();
+            final ComponentName defaultLauncher = mPackageManagerInternal
                     .getHomeActivitiesAsUser(allHomeCandidates, userId);
             logDurationStat(Stats.GET_DEFAULT_HOME, startGetHomeActivitiesAsUser);
 
@@ -1627,11 +2128,18 @@
                     Slog.v(TAG, "Default launcher from PM: " + detected);
                 }
             } else {
-                detected = user.getLauncherComponent();
+                detected = user.getLastKnownLauncher();
 
-                // TODO: Make sure it's still enabled.
-                if (DEBUG) {
-                    Slog.v(TAG, "Cached launcher: " + detected);
+                if (detected != null) {
+                    if (injectIsActivityEnabledAndExported(detected, userId)) {
+                        if (DEBUG) {
+                            Slog.v(TAG, "Cached launcher: " + detected);
+                        }
+                    } else {
+                        Slog.w(TAG, "Cached launcher " + detected + " no longer exists");
+                        detected = null;
+                        user.clearLauncher();
+                    }
                 }
             }
 
@@ -1661,13 +2169,13 @@
                     lastPriority = ri.priority;
                 }
             }
-            logDurationStat(Stats.LAUNCHER_PERMISSION_CHECK, start);
 
+            // Update the cache.
+            user.setLauncher(detected);
             if (detected != null) {
                 if (DEBUG) {
                     Slog.v(TAG, "Detected launcher: " + detected);
                 }
-                user.setLauncherComponent(this, detected);
                 return detected.getPackageName().equals(callingPackage);
             } else {
                 // Default launcher not found.
@@ -1678,10 +2186,12 @@
 
     // === House keeping ===
 
-    private void cleanUpPackageForAllLoadedUsers(String packageName, @UserIdInt int packageUserId) {
+    private void cleanUpPackageForAllLoadedUsers(String packageName, @UserIdInt int packageUserId,
+            boolean appStillExists) {
         synchronized (mLock) {
             forEachLoadedUserLocked(user ->
-                    cleanUpPackageLocked(packageName, user.getUserId(), packageUserId));
+                    cleanUpPackageLocked(packageName, user.getUserId(), packageUserId,
+                            appStillExists));
         }
     }
 
@@ -1693,7 +2203,8 @@
      * This is called when an app is uninstalled, or an app gets "clear data"ed.
      */
     @VisibleForTesting
-    void cleanUpPackageLocked(String packageName, int owningUserId, int packageUserId) {
+    void cleanUpPackageLocked(String packageName, int owningUserId, int packageUserId,
+            boolean appStillExists) {
         final boolean wasUserLoaded = isUserLoadedLocked(owningUserId);
 
         final ShortcutUser user = getUserShortcutsLocked(owningUserId);
@@ -1701,7 +2212,7 @@
 
         // First, remove the package from the package list (if the package is a publisher).
         if (packageUserId == owningUserId) {
-            if (user.removePackage(this, packageName) != null) {
+            if (user.removePackage(packageName) != null) {
                 doNotify = true;
             }
         }
@@ -1714,7 +2225,7 @@
 
         // Now there may be orphan shortcuts because we removed pinned shortcuts at the previous
         // step.  Remove them too.
-        user.forAllPackages(p -> p.refreshPinnedFlags(this));
+        user.forAllPackages(p -> p.refreshPinnedFlags());
 
         scheduleSaveUser(owningUserId);
 
@@ -1722,6 +2233,13 @@
             notifyListeners(packageName, owningUserId);
         }
 
+        // If the app still exists (i.e. data cleared), we need to re-publish manifest shortcuts.
+        if (appStillExists && (packageUserId == owningUserId)) {
+            // This will do the notification and save when needed, so do it after the above
+            // notifyListeners.
+            user.rescanPackageIfNeeded(packageName, /* forceRescan=*/ true);
+        }
+
         if (!wasUserLoaded) {
             // Note this will execute the scheduled save.
             unloadUserLocked(owningUserId);
@@ -1740,17 +2258,21 @@
                 @Nullable ComponentName componentName,
                 int queryFlags, int userId) {
             final ArrayList<ShortcutInfo> ret = new ArrayList<>();
-            final int cloneFlag =
-                    ((queryFlags & ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY) == 0)
-                            ? ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER
-                            : ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO;
+
+            final boolean cloneKeyFieldOnly =
+                    ((queryFlags & ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY) != 0);
+            final int cloneFlag = cloneKeyFieldOnly ? ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO
+                    : ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER;
             if (packageName == null) {
                 shortcutIds = null; // LauncherAppsService already threw for it though.
             }
 
             synchronized (mLock) {
+                throwIfUserLockedL(userId);
+                throwIfUserLockedL(launcherUserId);
+
                 getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
-                        .attemptToRestoreIfNeededAndSave(ShortcutService.this);
+                        .attemptToRestoreIfNeededAndSave();
 
                 if (packageName != null) {
                     getShortcutsInnerLocked(launcherUserId,
@@ -1775,7 +2297,13 @@
             final ArraySet<String> ids = shortcutIds == null ? null
                     : new ArraySet<>(shortcutIds);
 
-            getPackageShortcutsLocked(packageName, userId).findAll(ShortcutService.this, ret,
+            final ShortcutPackage p = getUserShortcutsLocked(userId)
+                    .getPackageShortcutsIfExists(packageName);
+            if (p == null) {
+                return; // No need to instantiate ShortcutPackage.
+            }
+
+            p.findAll(ret,
                     (ShortcutInfo si) -> {
                         if (si.getLastChangedTimestamp() < changedSince) {
                             return false;
@@ -1783,17 +2311,25 @@
                         if (ids != null && !ids.contains(si.getId())) {
                             return false;
                         }
-                        if (componentName != null
-                                && !componentName.equals(si.getActivityComponent())) {
-                            return false;
+                        if (componentName != null) {
+                            if (si.getActivity() != null
+                                    && !si.getActivity().equals(componentName)) {
+                                return false;
+                            }
                         }
-                        final boolean matchDynamic =
-                                ((queryFlags & ShortcutQuery.FLAG_GET_DYNAMIC) != 0)
-                                        && si.isDynamic();
-                        final boolean matchPinned =
-                                ((queryFlags & ShortcutQuery.FLAG_GET_PINNED) != 0)
-                                        && si.isPinned();
-                        return matchDynamic || matchPinned;
+                        if (((queryFlags & ShortcutQuery.FLAG_GET_DYNAMIC) != 0)
+                                && si.isDynamic()) {
+                            return true;
+                        }
+                        if (((queryFlags & ShortcutQuery.FLAG_GET_PINNED) != 0)
+                                && si.isPinned()) {
+                            return true;
+                        }
+                        if (((queryFlags & ShortcutQuery.FLAG_GET_MANIFEST) != 0)
+                                && si.isManifestShortcut()) {
+                            return true;
+                        }
+                        return false;
                     }, cloneFlag, callingPackage, launcherUserId);
         }
 
@@ -1804,8 +2340,11 @@
             Preconditions.checkStringNotEmpty(shortcutId, "shortcutId");
 
             synchronized (mLock) {
+                throwIfUserLockedL(userId);
+                throwIfUserLockedL(launcherUserId);
+
                 getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
-                        .attemptToRestoreIfNeededAndSave(ShortcutService.this);
+                        .attemptToRestoreIfNeededAndSave();
 
                 final ShortcutInfo si = getShortcutInfoLocked(
                         launcherUserId, callingPackage, packageName, shortcutId, userId);
@@ -1819,9 +2358,17 @@
             Preconditions.checkStringNotEmpty(packageName, "packageName");
             Preconditions.checkStringNotEmpty(shortcutId, "shortcutId");
 
+            throwIfUserLockedL(userId);
+            throwIfUserLockedL(launcherUserId);
+
+            final ShortcutPackage p = getUserShortcutsLocked(userId)
+                    .getPackageShortcutsIfExists(packageName);
+            if (p == null) {
+                return null;
+            }
+
             final ArrayList<ShortcutInfo> list = new ArrayList<>(1);
-            getPackageShortcutsLocked(packageName, userId).findAll(
-                    ShortcutService.this, list,
+            p.findAll(list,
                     (ShortcutInfo si) -> shortcutId.equals(si.getId()),
                     /* clone flags=*/ 0, callingPackage, launcherUserId);
             return list.size() == 0 ? null : list.get(0);
@@ -1836,18 +2383,22 @@
             Preconditions.checkNotNull(shortcutIds, "shortcutIds");
 
             synchronized (mLock) {
+                throwIfUserLockedL(userId);
+                throwIfUserLockedL(launcherUserId);
+
                 final ShortcutLauncher launcher =
                         getLauncherShortcutsLocked(callingPackage, userId, launcherUserId);
-                launcher.attemptToRestoreIfNeededAndSave(ShortcutService.this);
+                launcher.attemptToRestoreIfNeededAndSave();
 
-                launcher.pinShortcuts(
-                        ShortcutService.this, userId, packageName, shortcutIds);
+                launcher.pinShortcuts(userId, packageName, shortcutIds);
             }
             packageShortcutsChanged(packageName, userId);
+
+            verifyStates();
         }
 
         @Override
-        public Intent createShortcutIntent(int launcherUserId,
+        public Intent[] createShortcutIntents(int launcherUserId,
                 @NonNull String callingPackage,
                 @NonNull String packageName, @NonNull String shortcutId, int userId) {
             // Calling permission must be checked by LauncherAppsImpl.
@@ -1855,17 +2406,21 @@
             Preconditions.checkStringNotEmpty(shortcutId, "shortcutId can't be empty");
 
             synchronized (mLock) {
+                throwIfUserLockedL(userId);
+                throwIfUserLockedL(launcherUserId);
+
                 getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
-                        .attemptToRestoreIfNeededAndSave(ShortcutService.this);
+                        .attemptToRestoreIfNeededAndSave();
 
                 // Make sure the shortcut is actually visible to the launcher.
                 final ShortcutInfo si = getShortcutInfoLocked(
                         launcherUserId, callingPackage, packageName, shortcutId, userId);
                 // "si == null" should suffice here, but check the flags too just to make sure.
-                if (si == null || !(si.isDynamic() || si.isPinned())) {
+                if (si == null || !si.isEnabled() || !si.isAlive()) {
+                    Log.e(TAG, "Shortcut " + shortcutId + " does not exist or disabled");
                     return null;
                 }
-                return si.getIntent();
+                return si.getIntents();
             }
         }
 
@@ -1884,11 +2439,19 @@
             Preconditions.checkNotNull(shortcutId, "shortcutId");
 
             synchronized (mLock) {
-                getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
-                        .attemptToRestoreIfNeededAndSave(ShortcutService.this);
+                throwIfUserLockedL(userId);
+                throwIfUserLockedL(launcherUserId);
 
-                final ShortcutInfo shortcutInfo = getPackageShortcutsLocked(
-                        packageName, userId).findShortcutById(shortcutId);
+                getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
+                        .attemptToRestoreIfNeededAndSave();
+
+                final ShortcutPackage p = getUserShortcutsLocked(userId)
+                        .getPackageShortcutsIfExists(packageName);
+                if (p == null) {
+                    return 0;
+                }
+
+                final ShortcutInfo shortcutInfo = p.findShortcutById(shortcutId);
                 return (shortcutInfo != null && shortcutInfo.hasIconResource())
                         ? shortcutInfo.getIconResourceId() : 0;
             }
@@ -1903,11 +2466,19 @@
             Preconditions.checkNotNull(shortcutId, "shortcutId");
 
             synchronized (mLock) {
-                getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
-                        .attemptToRestoreIfNeededAndSave(ShortcutService.this);
+                throwIfUserLockedL(userId);
+                throwIfUserLockedL(launcherUserId);
 
-                final ShortcutInfo shortcutInfo = getPackageShortcutsLocked(
-                        packageName, userId).findShortcutById(shortcutId);
+                getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
+                        .attemptToRestoreIfNeededAndSave();
+
+                final ShortcutPackage p = getUserShortcutsLocked(userId)
+                        .getPackageShortcutsIfExists(packageName);
+                if (p == null) {
+                    return null;
+                }
+
+                final ShortcutInfo shortcutInfo = p.findShortcutById(shortcutId);
                 if (shortcutInfo == null || !shortcutInfo.hasIconFile()) {
                     return null;
                 }
@@ -1931,30 +2502,37 @@
                 @NonNull String callingPackage) {
             return ShortcutService.this.hasShortcutHostPermission(callingPackage, launcherUserId);
         }
+    }
 
-        /**
-         * Called by AM when the system locale changes *within the AM lock.  ABSOLUTELY do not take
-         * any locks in this method.
-         */
+    final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
-        public void onSystemLocaleChangedNoLock() {
-            if (!FEATURE_ENABLED) {
-                return;
+        public void onReceive(Context context, Intent intent) {
+            if (!mBootCompleted.get()) {
+                return; // Boot not completed, ignore the broadcast.
             }
-            // DO NOT HOLD ANY LOCKS HERE.
+            try {
+                if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) {
+                    handleLocaleChanged();
+                }
+            } catch (Exception e) {
+                wtf("Exception in mReceiver.onReceive", e);
+            }
+        }
+    };
 
-            // We want to reset throttling for all packages for all users.  But we can't just do so
-            // here because:
-            // - We can't load/save users that are locked.
-            // - Even for loaded users, resetting the counters would require us to hold mLock.
-            //
-            // So we use a "pull" model instead.  In here, we just increment the "locale change
-            // sequence number".  Each ShortcutUser has the "last known locale change sequence".
-            //
-            // This allows ShortcutUser's to detect the system locale change, so they can reset
-            // counters.
-            mLocaleChangeSequenceNumber.incrementAndGet();
-            postToHandler(() -> scheduleSaveBaseState());
+    void handleLocaleChanged() {
+        if (DEBUG) {
+            Slog.d(TAG, "handleLocaleChanged");
+        }
+        scheduleSaveBaseState();
+
+        synchronized (mLock) {
+            final long token = injectClearCallingIdentity();
+            try {
+                forEachLoadedUserLocked(user -> user.detectLocaleChange());
+            } finally {
+                injectRestoreCallingIdentity(token);
+            }
         }
     }
 
@@ -1962,25 +2540,76 @@
      * Package event callbacks.
      */
     @VisibleForTesting
-    final PackageMonitor mPackageMonitor = new PackageMonitor() {
+    final BroadcastReceiver mPackageMonitor = new BroadcastReceiver() {
         @Override
-        public void onPackageAdded(String packageName, int uid) {
-            handlePackageAdded(packageName, getChangingUserId());
-        }
+        public void onReceive(Context context, Intent intent) {
+            final int userId  = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
+            if (userId == UserHandle.USER_NULL) {
+                Slog.w(TAG, "Intent broadcast does not contain user handle: " + intent);
+                return;
+            }
 
-        @Override
-        public void onPackageUpdateFinished(String packageName, int uid) {
-            handlePackageUpdateFinished(packageName, getChangingUserId());
-        }
+            final String action = intent.getAction();
 
-        @Override
-        public void onPackageRemoved(String packageName, int uid) {
-            handlePackageRemoved(packageName, getChangingUserId());
-        }
+            // This is normally called on Handler, so clearCallingIdentity() isn't needed,
+            // but we still check it in unit tests.
+            final long token = injectClearCallingIdentity();
+            try {
+                synchronized (mLock) {
+                    if (!isUserUnlockedL(userId)) {
+                        if (DEBUG) {
+                            Slog.d(TAG, "Ignoring package broadcast " + action
+                                    + " for locked/stopped user " + userId);
+                        }
+                        return;
+                    }
 
-        @Override
-        public void onPackageDataCleared(String packageName, int uid) {
-            handlePackageDataCleared(packageName, getChangingUserId());
+                    // Whenever we get one of those package broadcasts, or get
+                    // ACTION_PREFERRED_ACTIVITY_CHANGED, we purge the default launcher cache.
+                    final ShortcutUser user = getUserShortcutsLocked(userId);
+                    user.clearLauncher();
+                }
+                if (Intent.ACTION_PREFERRED_ACTIVITY_CHANGED.equals(action)) {
+                    // Nothing farther to do.
+                    return;
+                }
+
+                final Uri intentUri = intent.getData();
+                final String packageName = (intentUri != null) ? intentUri.getSchemeSpecificPart()
+                        : null;
+                if (packageName == null) {
+                    Slog.w(TAG, "Intent broadcast does not contain package name: " + intent);
+                    return;
+                }
+
+                final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
+
+                switch (action) {
+                    case Intent.ACTION_PACKAGE_ADDED:
+                        if (replacing) {
+                            handlePackageUpdateFinished(packageName, userId);
+                        } else {
+                            handlePackageAdded(packageName, userId);
+                        }
+                        break;
+                    case Intent.ACTION_PACKAGE_REMOVED:
+                        if (!replacing) {
+                            handlePackageRemoved(packageName, userId);
+                        }
+                        break;
+                    case Intent.ACTION_PACKAGE_CHANGED:
+                        handlePackageChanged(packageName, userId);
+
+                        break;
+                    case Intent.ACTION_PACKAGE_DATA_CLEARED:
+                        handlePackageDataCleared(packageName, userId);
+                        break;
+                }
+            } catch (Exception e) {
+                wtf("Exception in mPackageMonitor.onReceive", e);
+            } finally {
+                injectRestoreCallingIdentity(token);
+            }
         }
     };
 
@@ -1988,39 +2617,76 @@
      * Called when a user is unlocked.
      * - Check all known packages still exist, and otherwise perform cleanup.
      * - If a package still exists, check the version code.  If it's been updated, may need to
-     *   update timestamps of its shortcuts.
+     * update timestamps of its shortcuts.
      */
     @VisibleForTesting
     void checkPackageChanges(@UserIdInt int ownerUserId) {
         if (DEBUG) {
             Slog.d(TAG, "checkPackageChanges() ownerUserId=" + ownerUserId);
         }
-        final ArrayList<PackageWithUser> gonePackages = new ArrayList<>();
-
-        synchronized (mLock) {
-            final ShortcutUser user = getUserShortcutsLocked(ownerUserId);
-
-            user.forAllPackageItems(spi -> {
-                if (spi.getPackageInfo().isShadow()) {
-                    return; // Don't delete shadow information.
-                }
-                final int versionCode = getApplicationVersionCode(
-                        spi.getPackageName(), spi.getPackageUserId());
-                if (versionCode >= 0) {
-                    // Package still installed, see if it's updated.
-                    getUserShortcutsLocked(ownerUserId).handlePackageUpdated(
-                            this, spi.getPackageName(), versionCode);
-                } else {
-                    gonePackages.add(PackageWithUser.of(spi));
-                }
-            });
-            if (gonePackages.size() > 0) {
-                for (int i = gonePackages.size() - 1; i >= 0; i--) {
-                    final PackageWithUser pu = gonePackages.get(i);
-                    cleanUpPackageLocked(pu.packageName, ownerUserId, pu.userId);
-                }
-            }
+        if (injectIsSafeModeEnabled()) {
+            Slog.i(TAG, "Safe mode, skipping checkPackageChanges()");
+            return;
         }
+
+        final long start = injectElapsedRealtime();
+        try {
+            final ArrayList<PackageWithUser> gonePackages = new ArrayList<>();
+
+            synchronized (mLock) {
+                final ShortcutUser user = getUserShortcutsLocked(ownerUserId);
+
+                // Find packages that have been uninstalled.
+                user.forAllPackageItems(spi -> {
+                    if (spi.getPackageInfo().isShadow()) {
+                        return; // Don't delete shadow information.
+                    }
+                    if (!isPackageInstalled(spi.getPackageName(), spi.getPackageUserId())) {
+                        if (DEBUG) {
+                            Slog.d(TAG, "Uninstalled: " + spi.getPackageName()
+                                    + " user " + spi.getPackageUserId());
+                        }
+                        gonePackages.add(PackageWithUser.of(spi));
+                    }
+                });
+                if (gonePackages.size() > 0) {
+                    for (int i = gonePackages.size() - 1; i >= 0; i--) {
+                        final PackageWithUser pu = gonePackages.get(i);
+                        cleanUpPackageLocked(pu.packageName, ownerUserId, pu.userId,
+                                /* appStillExists = */ false);
+                    }
+                }
+
+                rescanUpdatedPackagesLocked(ownerUserId, user.getLastAppScanTime(),
+                        /* forceRescan=*/ false);
+            }
+        } finally {
+            logDurationStat(Stats.CHECK_PACKAGE_CHANGES, start);
+        }
+        verifyStates();
+    }
+
+    private void rescanUpdatedPackagesLocked(@UserIdInt int userId, long lastScanTime,
+            boolean forceRescan) {
+        final ShortcutUser user = getUserShortcutsLocked(userId);
+
+        // Note after each OTA, we'll need to rescan all system apps, as their lastUpdateTime
+        // is not reliable.
+        final long now = injectCurrentTimeMillis();
+        final boolean afterOta =
+                !injectBuildFingerprint().equals(user.getLastAppScanOsFingerprint());
+
+        // Then for each installed app, publish manifest shortcuts when needed.
+        forUpdatedPackages(userId, lastScanTime, afterOta, ai -> {
+            user.attemptToRestoreIfNeededAndSave(this, ai.packageName, userId);
+            user.rescanPackageIfNeeded(ai.packageName, forceRescan);
+        });
+
+        // Write the time just before the scan, because there may be apps that have just
+        // been updated, and we want to catch them in the next time.
+        user.setLastAppScanTime(now);
+        user.setLastAppScanOsFingerprint(injectBuildFingerprint());
+        scheduleSaveUser(userId);
     }
 
     private void handlePackageAdded(String packageName, @UserIdInt int userId) {
@@ -2028,9 +2694,11 @@
             Slog.d(TAG, String.format("handlePackageAdded: %s user=%d", packageName, userId));
         }
         synchronized (mLock) {
-            forEachLoadedUserLocked(user ->
-                    user.attemptToRestoreIfNeededAndSave(this, packageName, userId));
+            final ShortcutUser user = getUserShortcutsLocked(userId);
+            user.attemptToRestoreIfNeededAndSave(this, packageName, userId);
+            user.rescanPackageIfNeeded(packageName, /* forceRescan=*/ true);
         }
+        verifyStates();
     }
 
     private void handlePackageUpdateFinished(String packageName, @UserIdInt int userId) {
@@ -2039,15 +2707,14 @@
                     packageName, userId));
         }
         synchronized (mLock) {
-            forEachLoadedUserLocked(user ->
-                    user.attemptToRestoreIfNeededAndSave(this, packageName, userId));
+            final ShortcutUser user = getUserShortcutsLocked(userId);
+            user.attemptToRestoreIfNeededAndSave(this, packageName, userId);
 
-            final int versionCode = getApplicationVersionCode(packageName, userId);
-            if (versionCode < 0) {
-                return; // shouldn't happen
+            if (isPackageInstalled(packageName, userId)) {
+                user.rescanPackageIfNeeded(packageName, /* forceRescan=*/ true);
             }
-            getUserShortcutsLocked(userId).handlePackageUpdated(this, packageName, versionCode);
         }
+        verifyStates();
     }
 
     private void handlePackageRemoved(String packageName, @UserIdInt int packageUserId) {
@@ -2055,7 +2722,9 @@
             Slog.d(TAG, String.format("handlePackageRemoved: %s user=%d", packageName,
                     packageUserId));
         }
-        cleanUpPackageForAllLoadedUsers(packageName, packageUserId);
+        cleanUpPackageForAllLoadedUsers(packageName, packageUserId, /* appStillExists = */ false);
+
+        verifyStates();
     }
 
     private void handlePackageDataCleared(String packageName, int packageUserId) {
@@ -2063,20 +2732,49 @@
             Slog.d(TAG, String.format("handlePackageDataCleared: %s user=%d", packageName,
                     packageUserId));
         }
-        cleanUpPackageForAllLoadedUsers(packageName, packageUserId);
+        cleanUpPackageForAllLoadedUsers(packageName, packageUserId, /* appStillExists = */ true);
+
+        verifyStates();
+    }
+
+    private void handlePackageChanged(String packageName, int packageUserId) {
+        if (DEBUG) {
+            Slog.d(TAG, String.format("handlePackageChanged: %s user=%d", packageName,
+                    packageUserId));
+        }
+
+        // Activities may be disabled or enabled.  Just rescan the package.
+        synchronized (mLock) {
+            final ShortcutUser user = getUserShortcutsLocked(packageUserId);
+
+            user.rescanPackageIfNeeded(packageName, /* forceRescan=*/ true);
+        }
+
+        verifyStates();
     }
 
     // === PackageManager interaction ===
 
-    PackageInfo getPackageInfoWithSignatures(String packageName, @UserIdInt int userId) {
-        return injectPackageInfo(packageName, userId, true);
+    /**
+     * Returns {@link PackageInfo} unless it's uninstalled or disabled.
+     */
+    @Nullable
+    final PackageInfo getPackageInfoWithSignatures(String packageName, @UserIdInt int userId) {
+        return getPackageInfo(packageName, userId, true);
+    }
+
+    /**
+     * Returns {@link PackageInfo} unless it's uninstalled or disabled.
+     */
+    @Nullable
+    final PackageInfo getPackageInfo(String packageName, @UserIdInt int userId) {
+        return getPackageInfo(packageName, userId, false);
     }
 
     int injectGetPackageUid(@NonNull String packageName, @UserIdInt int userId) {
         final long token = injectClearCallingIdentity();
         try {
-            return mIPackageManager.getPackageUid(packageName, PACKAGE_MATCH_FLAGS
-                    , userId);
+            return mIPackageManager.getPackageUid(packageName, PACKAGE_MATCH_FLAGS, userId);
         } catch (RemoteException e) {
             // Shouldn't happen.
             Slog.wtf(TAG, "RemoteException", e);
@@ -2086,15 +2784,30 @@
         }
     }
 
+    /**
+     * Returns {@link PackageInfo} unless it's uninstalled or disabled.
+     */
+    @Nullable
     @VisibleForTesting
-    PackageInfo injectPackageInfo(String packageName, @UserIdInt int userId,
+    final PackageInfo getPackageInfo(String packageName, @UserIdInt int userId,
             boolean getSignatures) {
-        final long start = System.currentTimeMillis();
+        return isInstalledOrNull(injectPackageInfoWithUninstalled(
+                packageName, userId, getSignatures));
+    }
+
+    /**
+     * Do not use directly; this returns uninstalled packages too.
+     */
+    @Nullable
+    @VisibleForTesting
+    PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
+            boolean getSignatures) {
+        final long start = injectElapsedRealtime();
         final long token = injectClearCallingIdentity();
         try {
-            return mIPackageManager.getPackageInfo(packageName, PACKAGE_MATCH_FLAGS
-                    | (getSignatures ? PackageManager.GET_SIGNATURES : 0)
-                    , userId);
+            return mIPackageManager.getPackageInfo(
+                    packageName, PACKAGE_MATCH_FLAGS
+                            | (getSignatures ? PackageManager.GET_SIGNATURES : 0), userId);
         } catch (RemoteException e) {
             // Shouldn't happen.
             Slog.wtf(TAG, "RemoteException", e);
@@ -2108,9 +2821,23 @@
         }
     }
 
+    /**
+     * Returns {@link ApplicationInfo} unless it's uninstalled or disabled.
+     */
+    @Nullable
     @VisibleForTesting
-    ApplicationInfo injectApplicationInfo(String packageName, @UserIdInt int userId) {
-        final long start = System.currentTimeMillis();
+    final ApplicationInfo getApplicationInfo(String packageName, @UserIdInt int userId) {
+        return isInstalledOrNull(injectApplicationInfoWithUninstalled(packageName, userId));
+    }
+
+    /**
+     * Do not use directly; this returns uninstalled packages too.
+     */
+    @Nullable
+    @VisibleForTesting
+    ApplicationInfo injectApplicationInfoWithUninstalled(
+            String packageName, @UserIdInt int userId) {
+        final long start = injectElapsedRealtime();
         final long token = injectClearCallingIdentity();
         try {
             return mIPackageManager.getApplicationInfo(packageName, PACKAGE_MATCH_FLAGS, userId);
@@ -2125,24 +2852,278 @@
         }
     }
 
-    private boolean isApplicationFlagSet(String packageName, int userId, int flags) {
-        final ApplicationInfo ai = injectApplicationInfo(packageName, userId);
-        return (ai != null) && ((ai.flags & flags) == flags);
-    }
-
-    boolean isPackageInstalled(String packageName, int userId) {
-        return isApplicationFlagSet(packageName, userId, ApplicationInfo.FLAG_INSTALLED);
+    /**
+     * Returns {@link ActivityInfo} with its metadata unless it's uninstalled or disabled.
+     */
+    @Nullable
+    final ActivityInfo getActivityInfoWithMetadata(ComponentName activity, @UserIdInt int userId) {
+        return isInstalledOrNull(injectGetActivityInfoWithMetadataWithUninstalled(
+                activity, userId));
     }
 
     /**
-     * @return the version code of the package, or -1 if the app is not installed.
+     * Do not use directly; this returns uninstalled packages too.
      */
-    int getApplicationVersionCode(String packageName, int userId) {
-        final ApplicationInfo ai = injectApplicationInfo(packageName, userId);
-        if ((ai == null) || ((ai.flags & ApplicationInfo.FLAG_INSTALLED) == 0)) {
-            return -1;
+    @Nullable
+    @VisibleForTesting
+    ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(
+            ComponentName activity, @UserIdInt int userId) {
+        final long start = injectElapsedRealtime();
+        final long token = injectClearCallingIdentity();
+        try {
+            return mIPackageManager.getActivityInfo(activity,
+                    (PACKAGE_MATCH_FLAGS | PackageManager.GET_META_DATA), userId);
+        } catch (RemoteException e) {
+            // Shouldn't happen.
+            Slog.wtf(TAG, "RemoteException", e);
+            return null;
+        } finally {
+            injectRestoreCallingIdentity(token);
+
+            logDurationStat(Stats.GET_ACTIVITY_WITH_METADATA, start);
         }
-        return ai.versionCode;
+    }
+
+    /**
+     * Return all installed and enabled packages.
+     */
+    @NonNull
+    @VisibleForTesting
+    final List<PackageInfo> getInstalledPackages(@UserIdInt int userId) {
+        final long start = injectElapsedRealtime();
+        final long token = injectClearCallingIdentity();
+        try {
+            final List<PackageInfo> all = injectGetPackagesWithUninstalled(userId);
+
+            all.removeIf(PACKAGE_NOT_INSTALLED);
+
+            return all;
+        } catch (RemoteException e) {
+            // Shouldn't happen.
+            Slog.wtf(TAG, "RemoteException", e);
+            return null;
+        } finally {
+            injectRestoreCallingIdentity(token);
+
+            logDurationStat(Stats.GET_INSTALLED_PACKAGES, start);
+        }
+    }
+
+    /**
+     * Do not use directly; this returns uninstalled packages too.
+     */
+    @NonNull
+    @VisibleForTesting
+    List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId)
+            throws RemoteException {
+        final ParceledListSlice<PackageInfo> parceledList =
+                mIPackageManager.getInstalledPackages(PACKAGE_MATCH_FLAGS, userId);
+        if (parceledList == null) {
+            return Collections.emptyList();
+        }
+        return parceledList.getList();
+    }
+
+    private void forUpdatedPackages(@UserIdInt int userId, long lastScanTime, boolean afterOta,
+            Consumer<ApplicationInfo> callback) {
+        if (DEBUG) {
+            Slog.d(TAG, "forUpdatedPackages for user " + userId + ", lastScanTime=" + lastScanTime);
+        }
+        final List<PackageInfo> list = getInstalledPackages(userId);
+        for (int i = list.size() - 1; i >= 0; i--) {
+            final PackageInfo pi = list.get(i);
+
+            // If the package has been updated since the last scan time, then scan it.
+            // Also if it's a system app with no update, lastUpdateTime is not reliable, so
+            // just scan it.
+            if (pi.lastUpdateTime >= lastScanTime
+                    || (afterOta && isPureSystemApp(pi.applicationInfo))) {
+                if (DEBUG) {
+                    Slog.d(TAG, "Found updated package " + pi.packageName);
+                }
+                callback.accept(pi.applicationInfo);
+            }
+        }
+    }
+
+    /**
+     * @return true if it's a system app with no updates.
+     */
+    private boolean isPureSystemApp(ApplicationInfo ai) {
+        return ai.isSystemApp() && !ai.isUpdatedSystemApp();
+    }
+
+    private boolean isApplicationFlagSet(@NonNull String packageName, int userId, int flags) {
+        final ApplicationInfo ai = injectApplicationInfoWithUninstalled(packageName, userId);
+        return (ai != null) && ((ai.flags & flags) == flags);
+    }
+
+    private static boolean isInstalled(@Nullable ApplicationInfo ai) {
+        return (ai != null) && (ai.flags & ApplicationInfo.FLAG_INSTALLED) != 0;
+    }
+
+    private static boolean isInstalled(@Nullable PackageInfo pi) {
+        return (pi != null) && isInstalled(pi.applicationInfo);
+    }
+
+    private static boolean isInstalled(@Nullable ActivityInfo ai) {
+        return (ai != null) && isInstalled(ai.applicationInfo);
+    }
+
+    private static ApplicationInfo isInstalledOrNull(ApplicationInfo ai) {
+        return isInstalled(ai) ? ai : null;
+    }
+
+    private static PackageInfo isInstalledOrNull(PackageInfo pi) {
+        return isInstalled(pi) ? pi : null;
+    }
+
+    private static ActivityInfo isInstalledOrNull(ActivityInfo ai) {
+        return isInstalled(ai) ? ai : null;
+    }
+
+    boolean isPackageInstalled(String packageName, int userId) {
+        return getApplicationInfo(packageName, userId) != null;
+    }
+
+    @Nullable
+    XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
+        return activityInfo.loadXmlMetaData(mContext.getPackageManager(), key);
+    }
+
+    @Nullable
+    Resources injectGetResourcesForApplicationAsUser(String packageName, int userId) {
+        final long start = injectElapsedRealtime();
+        final long token = injectClearCallingIdentity();
+        try {
+            return mContext.getPackageManager().getResourcesForApplicationAsUser(
+                    packageName, userId);
+        } catch (NameNotFoundException e) {
+            Slog.e(TAG, "Resources for package " + packageName + " not found");
+            return null;
+        } finally {
+            injectRestoreCallingIdentity(token);
+
+            logDurationStat(Stats.GET_APPLICATION_RESOURCES, start);
+        }
+    }
+
+    private Intent getMainActivityIntent() {
+        final Intent intent = new Intent(Intent.ACTION_MAIN);
+        intent.addCategory(LAUNCHER_INTENT_CATEGORY);
+        return intent;
+    }
+
+    /**
+     * Same as queryIntentActivitiesAsUser, except it makes sure the package is installed,
+     * and only returns exported activities.
+     */
+    @NonNull
+    @VisibleForTesting
+    List<ResolveInfo> queryActivities(@NonNull Intent baseIntent,
+            @NonNull String packageName, @Nullable ComponentName activity, int userId) {
+
+        baseIntent.setPackage(Preconditions.checkNotNull(packageName));
+        if (activity != null) {
+            baseIntent.setComponent(activity);
+        }
+
+        final List<ResolveInfo> resolved =
+                mContext.getPackageManager().queryIntentActivitiesAsUser(
+                        baseIntent, PACKAGE_MATCH_FLAGS, userId);
+        if (resolved == null || resolved.size() == 0) {
+            return EMPTY_RESOLVE_INFO;
+        }
+        // Make sure the package is installed.
+        if (!isInstalled(resolved.get(0).activityInfo)) {
+            return EMPTY_RESOLVE_INFO;
+        }
+        resolved.removeIf(ACTIVITY_NOT_EXPORTED);
+        return resolved;
+    }
+
+    /**
+     * Return the main activity that is enabled and exported.  If multiple activities are found,
+     * return the first one.
+     */
+    @Nullable
+    ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
+        final long start = injectElapsedRealtime();
+        final long token = injectClearCallingIdentity();
+        try {
+            final List<ResolveInfo> resolved =
+                    queryActivities(getMainActivityIntent(), packageName, null, userId);
+            return resolved.size() == 0 ? null : resolved.get(0).activityInfo.getComponentName();
+        } finally {
+            injectRestoreCallingIdentity(token);
+
+            logDurationStat(Stats.GET_LAUNCHER_ACTIVITY, start);
+        }
+    }
+
+    /**
+     * Return whether an activity is enabled, exported and main.
+     */
+    boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
+        final long start = injectElapsedRealtime();
+        final long token = injectClearCallingIdentity();
+        try {
+            final List<ResolveInfo> resolved =
+                    queryActivities(getMainActivityIntent(), activity.getPackageName(),
+                            activity, userId);
+            return resolved.size() > 0;
+        } finally {
+            injectRestoreCallingIdentity(token);
+
+            logDurationStat(Stats.CHECK_LAUNCHER_ACTIVITY, start);
+        }
+    }
+
+    /**
+     * Return all the enabled, exported and main activities from a package.
+     */
+    @NonNull
+    List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
+        final long start = injectElapsedRealtime();
+        final long token = injectClearCallingIdentity();
+        try {
+            return queryActivities(getMainActivityIntent(), packageName, null, userId);
+        } finally {
+            injectRestoreCallingIdentity(token);
+
+            logDurationStat(Stats.CHECK_LAUNCHER_ACTIVITY, start);
+        }
+    }
+
+    /**
+     * Return whether an activity is enabled and exported.
+     */
+    @VisibleForTesting
+    boolean injectIsActivityEnabledAndExported(
+            @NonNull ComponentName activity, @UserIdInt int userId) {
+        final long start = injectElapsedRealtime();
+        final long token = injectClearCallingIdentity();
+        try {
+            return queryActivities(new Intent(), activity.getPackageName(), activity, userId)
+                    .size() > 0;
+        } finally {
+            injectRestoreCallingIdentity(token);
+
+            logDurationStat(Stats.IS_ACTIVITY_ENABLED, start);
+        }
+    }
+
+    boolean injectIsSafeModeEnabled() {
+        final long token = injectClearCallingIdentity();
+        try {
+            return IWindowManager.Stub
+                    .asInterface(ServiceManager.getService(Context.WINDOW_SERVICE))
+                    .isSafeModeEnabled();
+        } catch (RemoteException e) {
+            return false; // Shouldn't happen though.
+        } finally {
+            injectRestoreCallingIdentity(token);
+        }
     }
 
     // === Backup & restore ===
@@ -2162,19 +3143,34 @@
             Slog.d(TAG, "Backing up user " + userId);
         }
         synchronized (mLock) {
-            final ShortcutUser user = getUserShortcutsLocked(userId);
-            if (user == null) {
-                Slog.w(TAG, "Can't backup: user not found: id=" + userId);
+            if (!isUserUnlockedL(userId)) {
+                wtf("Can't backup: user " + userId + " is locked or not running");
                 return null;
             }
 
-            user.forAllPackageItems(spi -> spi.refreshPackageInfoAndSave(this));
+            final ShortcutUser user = getUserShortcutsLocked(userId);
+            if (user == null) {
+                wtf("Can't backup: user not found: id=" + userId);
+                return null;
+            }
 
-            // Then save.
+            // Update the signatures for all packages.
+            user.forAllPackageItems(spi -> spi.refreshPackageSignatureAndSave());
+
+            // Set the version code for the launchers.
+            // We shouldn't do this for publisher packages, because we don't want to update the
+            // version code without rescanning the manifest.
+            user.forAllLaunchers(launcher -> launcher.ensureVersionInfo());
+
+            // Save to the filesystem.
+            scheduleSaveUser(userId);
+            saveDirtyInfo();
+
+            // Then create the backup payload.
             final ByteArrayOutputStream os = new ByteArrayOutputStream(32 * 1024);
             try {
                 saveUserInternalLocked(userId, os, /* forBackup */ true);
-            } catch (XmlPullParserException|IOException e) {
+            } catch (XmlPullParserException | IOException e) {
                 // Shouldn't happen.
                 Slog.w(TAG, "Backup failed.", e);
                 return null;
@@ -2189,23 +3185,26 @@
         if (DEBUG) {
             Slog.d(TAG, "Restoring user " + userId);
         }
-        final ShortcutUser user;
-        final ByteArrayInputStream is = new ByteArrayInputStream(payload);
-        try {
-            user = loadUserInternal(userId, is, /* fromBackup */ true);
-        } catch (XmlPullParserException|IOException e) {
-            Slog.w(TAG, "Restoration failed.", e);
-            return;
-        }
         synchronized (mLock) {
-            mUsers.put(userId, user);
-
-            // Then purge all the save images.
-            final File bitmapPath = getUserBitmapFilePath(userId);
-            final boolean success = FileUtils.deleteContents(bitmapPath);
-            if (!success) {
-                Slog.w(TAG, "Failed to delete " + bitmapPath);
+            if (!isUserUnlockedL(userId)) {
+                wtf("Can't restore: user " + userId + " is locked or not running");
+                return;
             }
+            // Actually do restore.
+            final ShortcutUser restored;
+            final ByteArrayInputStream is = new ByteArrayInputStream(payload);
+            try {
+                restored = loadUserInternal(userId, is, /* fromBackup */ true);
+            } catch (XmlPullParserException | IOException | InvalidFileFormatException e) {
+                Slog.w(TAG, "Restoration failed.", e);
+                return;
+            }
+            getUserShortcutsLocked(userId).mergeRestoredFile(restored);
+
+            // Rescan all packages to re-publish manifest shortcuts and do other checks.
+            rescanUpdatedPackagesLocked(userId,
+                    0, // lastScanTime = 0; rescan all packages.
+                    /* forceRescan= */ true);
 
             saveUserLocked(userId);
         }
@@ -2215,20 +3214,29 @@
 
     @Override
     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
-        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
-                != PackageManager.PERMISSION_GRANTED) {
-            pw.println("Permission Denial: can't dump UserManager from from pid="
-                    + Binder.getCallingPid()
-                    + ", uid=" + Binder.getCallingUid()
-                    + " without permission "
-                    + android.Manifest.permission.DUMP);
-            return;
+        enforceCallingOrSelfPermission(android.Manifest.permission.DUMP,
+                "can't dump by this caller");
+        boolean checkin = false;
+        boolean clear = false;
+        if (args != null) {
+            for (String arg : args) {
+                if ("-c".equals(arg)) {
+                    checkin = true;
+                } else if ("--checkin".equals(arg)) {
+                    checkin = true;
+                    clear = true;
+                }
+            }
         }
-        dumpInner(pw, args);
+
+        if (checkin) {
+            dumpCheckin(pw, clear);
+        } else {
+            dumpInner(pw);
+        }
     }
 
-    @VisibleForTesting
-    void dumpInner(PrintWriter pw, String[] args) {
+    private void dumpInner(PrintWriter pw) {
         synchronized (mLock) {
             final long now = injectCurrentTimeMillis();
             pw.print("Now: [");
@@ -2253,10 +3261,6 @@
             pw.print("] ");
             pw.print(formatTime(next));
 
-            pw.print("  Locale change seq#: ");
-            pw.print(mLocaleChangeSequenceNumber.get());
-            pw.println();
-
             pw.print("  Config:");
             pw.print("    Max icon dim: ");
             pw.println(mMaxIconDimension);
@@ -2270,24 +3274,29 @@
             pw.println(mResetInterval);
             pw.print("    maxUpdatesPerInterval: ");
             pw.println(mMaxUpdatesPerInterval);
-            pw.print("    maxDynamicShortcuts: ");
-            pw.println(mMaxDynamicShortcuts);
+            pw.print("    maxShortcutsPerActivity: ");
+            pw.println(mMaxShortcuts);
             pw.println();
 
             pw.println("  Stats:");
             synchronized (mStatLock) {
-                final String p = "    ";
-                dumpStatLS(pw, p, Stats.GET_DEFAULT_HOME, "getHomeActivities()");
-                dumpStatLS(pw, p, Stats.LAUNCHER_PERMISSION_CHECK, "Launcher permission check");
+                for (int i = 0; i < Stats.COUNT; i++) {
+                    dumpStatLS(pw, "    ", i);
+                }
+            }
 
-                dumpStatLS(pw, p, Stats.GET_PACKAGE_INFO, "getPackageInfo()");
-                dumpStatLS(pw, p, Stats.GET_PACKAGE_INFO_WITH_SIG, "getPackageInfo(SIG)");
-                dumpStatLS(pw, p, Stats.GET_APPLICATION_INFO, "getApplicationInfo");
+            pw.println();
+            pw.print("  #Failures: ");
+            pw.println(mWtfCount);
+
+            if (mLastWtfStacktrace != null) {
+                pw.print("  Last failure stack trace: ");
+                pw.println(Log.getStackTraceString(mLastWtfStacktrace));
             }
 
             for (int i = 0; i < mUsers.size(); i++) {
                 pw.println();
-                mUsers.valueAt(i).dump(this, pw, "  ");
+                mUsers.valueAt(i).dump(pw, "  ");
             }
 
             pw.println();
@@ -2316,15 +3325,43 @@
         return tobj.format("%Y-%m-%d %H:%M:%S");
     }
 
-    private void dumpStatLS(PrintWriter pw, String prefix, int statId, String label) {
+    private void dumpStatLS(PrintWriter pw, String prefix, int statId) {
         pw.print(prefix);
         final int count = mCountStats[statId];
         final long dur = mDurationStats[statId];
         pw.println(String.format("%s: count=%d, total=%dms, avg=%.1fms",
-                label, count, dur,
+                STAT_LABELS[statId], count, dur,
                 (count == 0 ? 0 : ((double) dur) / count)));
     }
 
+    /**
+     * Dumpsys for checkin.
+     *
+     * @param clear if true, clear the history information.  Some other system services have this
+     * behavior but shortcut service doesn't for now.
+     */
+    private  void dumpCheckin(PrintWriter pw, boolean clear) {
+        synchronized (mLock) {
+            try {
+                final JSONArray users = new JSONArray();
+
+                for (int i = 0; i < mUsers.size(); i++) {
+                    users.put(mUsers.valueAt(i).dumpCheckin(clear));
+                }
+
+                final JSONObject result = new JSONObject();
+
+                result.put(KEY_SHORTCUT, users);
+                result.put(KEY_LOW_RAM, injectIsLowRamDevice());
+                result.put(KEY_ICON_SIZE, mMaxIconDimension);
+
+                pw.println(result.toString(1));
+            } catch (JSONException e) {
+                Slog.e(TAG, "Unable to write in json", e);
+            }
+        }
+    }
+
     // === Shell support ===
 
     @Override
@@ -2333,7 +3370,13 @@
 
         enforceShell();
 
-        (new MyShellCommand()).exec(this, in, out, err, args, resultReceiver);
+        final long token = injectClearCallingIdentity();
+        try {
+            final int status = (new MyShellCommand()).exec(this, in, out, err, args, resultReceiver);
+            resultReceiver.send(status, null);
+        } finally {
+            injectRestoreCallingIdentity(token);
+        }
     }
 
     static class CommandException extends Exception {
@@ -2349,7 +3392,7 @@
 
         private int mUserId = UserHandle.USER_SYSTEM;
 
-        private void parseOptions(boolean takeUser)
+        private void parseOptionsLocked(boolean takeUser)
                 throws CommandException {
             String opt;
             while ((opt = getNextOption()) != null) {
@@ -2357,6 +3400,10 @@
                     case "--user":
                         if (takeUser) {
                             mUserId = UserHandle.parseUserArg(getNextArgRequired());
+                            if (!isUserUnlockedL(mUserId)) {
+                                throw new CommandException(
+                                        "User " + mUserId + " is not running or locked");
+                            }
                             break;
                         }
                         // fallthrough
@@ -2374,9 +3421,6 @@
             final PrintWriter pw = getOutPrintWriter();
             try {
                 switch (cmd) {
-                    case "reset-package-throttling":
-                        handleResetPackageThrottling();
-                        break;
                     case "reset-throttling":
                         handleResetThrottling();
                         break;
@@ -2395,15 +3439,15 @@
                     case "get-default-launcher":
                         handleGetDefaultLauncher();
                         break;
-                    case "refresh-default-launcher":
-                        handleRefreshDefaultLauncher();
-                        break;
                     case "unload-user":
                         handleUnloadUser();
                         break;
                     case "clear-shortcuts":
                         handleClearShortcuts();
                         break;
+                    case "verify-states": // hidden command to verify various internal states.
+                        handleVerifyStates();
+                        break;
                     default:
                         return handleDefaultCommands(cmd);
                 }
@@ -2420,9 +3464,6 @@
             final PrintWriter pw = getOutPrintWriter();
             pw.println("Usage: cmd shortcut COMMAND [options ...]");
             pw.println();
-            pw.println("cmd shortcut reset-package-throttling [--user USER_ID] PACKAGE");
-            pw.println("    Reset throttling for a package");
-            pw.println();
             pw.println("cmd shortcut reset-throttling [--user USER_ID]");
             pw.println("    Reset throttling for all packages and users");
             pw.println();
@@ -2439,10 +3480,7 @@
             pw.println("    Clear the cached default launcher");
             pw.println();
             pw.println("cmd shortcut get-default-launcher [--user USER_ID]");
-            pw.println("    Show the cached default launcher");
-            pw.println();
-            pw.println("cmd shortcut refresh-default-launcher [--user USER_ID]");
-            pw.println("    Refresh the cached default launcher");
+            pw.println("    Show the default launcher");
             pw.println();
             pw.println("cmd shortcut unload-user [--user USER_ID]");
             pw.println("    Unload a user from the memory");
@@ -2454,11 +3492,13 @@
         }
 
         private void handleResetThrottling() throws CommandException {
-            parseOptions(/* takeUser =*/ true);
+            synchronized (mLock) {
+                parseOptionsLocked(/* takeUser =*/ true);
 
-            Slog.i(TAG, "cmd: handleResetThrottling");
+                Slog.i(TAG, "cmd: handleResetThrottling: user=" + mUserId);
 
-            resetThrottlingInner(mUserId);
+                resetThrottlingInner(mUserId);
+            }
         }
 
         private void handleResetAllThrottling() {
@@ -2467,16 +3507,6 @@
             resetAllThrottlingInner();
         }
 
-        private void handleResetPackageThrottling() throws CommandException {
-            parseOptions(/* takeUser =*/ true);
-
-            final String packageName = getNextArgRequired();
-
-            Slog.i(TAG, "cmd: handleResetPackageThrottling: " + packageName);
-
-            resetPackageThrottling(packageName, mUserId);
-        }
-
         private void handleOverrideConfig() throws CommandException {
             final String config = getNextArgRequired();
 
@@ -2499,8 +3529,7 @@
 
         private void clearLauncher() {
             synchronized (mLock) {
-                getUserShortcutsLocked(mUserId).setLauncherComponent(
-                        ShortcutService.this, null);
+                getUserShortcutsLocked(mUserId).forceClearLauncher();
             }
         }
 
@@ -2510,44 +3539,55 @@
                 hasShortcutHostPermissionInner("-", mUserId);
 
                 getOutPrintWriter().println("Launcher: "
-                        + getUserShortcutsLocked(mUserId).getLauncherComponent());
+                        + getUserShortcutsLocked(mUserId).getLastKnownLauncher());
             }
         }
 
         private void handleClearDefaultLauncher() throws CommandException {
-            parseOptions(/* takeUser =*/ true);
+            synchronized (mLock) {
+                parseOptionsLocked(/* takeUser =*/ true);
 
-            clearLauncher();
+                clearLauncher();
+            }
         }
 
         private void handleGetDefaultLauncher() throws CommandException {
-            parseOptions(/* takeUser =*/ true);
+            synchronized (mLock) {
+                parseOptionsLocked(/* takeUser =*/ true);
 
-            showLauncher();
-        }
-
-        private void handleRefreshDefaultLauncher() throws CommandException {
-            parseOptions(/* takeUser =*/ true);
-
-            clearLauncher();
-            showLauncher();
+                clearLauncher();
+                showLauncher();
+            }
         }
 
         private void handleUnloadUser() throws CommandException {
-            parseOptions(/* takeUser =*/ true);
+            synchronized (mLock) {
+                parseOptionsLocked(/* takeUser =*/ true);
 
-            Slog.i(TAG, "cmd: handleUnloadUser: " + mUserId);
+                Slog.i(TAG, "cmd: handleUnloadUser: user=" + mUserId);
 
-            ShortcutService.this.handleCleanupUser(mUserId);
+                ShortcutService.this.handleCleanupUser(mUserId);
+            }
         }
 
         private void handleClearShortcuts() throws CommandException {
-            parseOptions(/* takeUser =*/ true);
-            final String packageName = getNextArgRequired();
+            synchronized (mLock) {
+                parseOptionsLocked(/* takeUser =*/ true);
+                final String packageName = getNextArgRequired();
 
-            Slog.i(TAG, "cmd: handleClearShortcuts: " + mUserId + ", " + packageName);
+                Slog.i(TAG, "cmd: handleClearShortcuts: user" + mUserId + ", " + packageName);
 
-            ShortcutService.this.cleanUpPackageForAllLoadedUsers(packageName, mUserId);
+                ShortcutService.this.cleanUpPackageForAllLoadedUsers(packageName, mUserId,
+                        /* appStillExists = */ true);
+            }
+        }
+
+        private void handleVerifyStates() throws CommandException {
+            try {
+                verifyStatesForce(); // This will throw when there's an issue.
+            } catch (Throwable th) {
+                throw new CommandException(th.getMessage() + "\n" + Log.getStackTraceString(th));
+            }
         }
     }
 
@@ -2586,12 +3626,25 @@
         Binder.restoreCallingIdentity(token);
     }
 
+    // Injection point.
+    @VisibleForTesting
+    String injectBuildFingerprint() {
+        return Build.FINGERPRINT;
+    }
+
     final void wtf(String message) {
-        wtf( message, /* exception= */ null);
+        wtf(message, /* exception= */ null);
     }
 
     // Injection point.
-    void wtf(String message, Exception e) {
+    void wtf(String message, Throwable e) {
+        if (e == null) {
+            e = new RuntimeException("Stacktrace");
+        }
+        synchronized (mLock) {
+            mWtfCount++;
+            mLastWtfStacktrace = new Exception("Last failure was logged here:");
+        }
         Slog.wtf(TAG, message, e);
     }
 
@@ -2618,11 +3671,6 @@
         }
     }
 
-    @VisibleForTesting
-    PackageManagerInternal injectPackageManagerInternal() {
-        return mPackageManagerInternal;
-    }
-
     File getUserBitmapFilePath(@UserIdInt int userId) {
         return new File(injectUserDataPath(userId), DIRECTORY_BITMAPS);
     }
@@ -2633,8 +3681,8 @@
     }
 
     @VisibleForTesting
-    int getMaxDynamicShortcutsForTest() {
-        return mMaxDynamicShortcuts;
+    int getMaxShortcutsForTest() {
+        return mMaxShortcuts;
     }
 
     @VisibleForTesting
@@ -2663,15 +3711,51 @@
     }
 
     @VisibleForTesting
-    ShortcutInfo getPackageShortcutForTest(String packageName, String shortcutId, int userId) {
+    ShortcutPackage getPackageShortcutForTest(String packageName, int userId) {
         synchronized (mLock) {
             final ShortcutUser user = mUsers.get(userId);
             if (user == null) return null;
 
-            final ShortcutPackage pkg = user.getAllPackagesForTest().get(packageName);
+            return user.getAllPackagesForTest().get(packageName);
+        }
+    }
+
+    @VisibleForTesting
+    ShortcutInfo getPackageShortcutForTest(String packageName, String shortcutId, int userId) {
+        synchronized (mLock) {
+            final ShortcutPackage pkg = getPackageShortcutForTest(packageName, userId);
             if (pkg == null) return null;
 
             return pkg.findShortcutById(shortcutId);
         }
     }
+
+    /**
+     * Control whether {@link #verifyStates} should be performed.  We always perform it during unit
+     * tests.
+     */
+    @VisibleForTesting
+    boolean injectShouldPerformVerification() {
+        return DEBUG;
+    }
+
+    /**
+     * Check various internal states and throws if there's any inconsistency.
+     * This is normally only enabled during unit tests.
+     */
+    final void verifyStates() {
+        if (injectShouldPerformVerification()) {
+            verifyStatesInner();
+        }
+    }
+
+    private final void verifyStatesForce() {
+        verifyStatesInner();
+    }
+
+    private void verifyStatesInner() {
+        synchronized (mLock) {
+            forEachLoadedUserLocked(u -> u.forAllPackageItems(ShortcutPackageItem::verifyStates));
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/pm/ShortcutUser.java b/services/core/java/com/android/server/pm/ShortcutUser.java
index 7d19a78..5d4bfa4 100644
--- a/services/core/java/com/android/server/pm/ShortcutUser.java
+++ b/services/core/java/com/android/server/pm/ShortcutUser.java
@@ -16,18 +16,27 @@
 package com.android.server.pm;
 
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.annotation.UserIdInt;
 import android.content.ComponentName;
+import android.content.pm.ShortcutManager;
+import android.text.TextUtils;
 import android.text.format.Formatter;
 import android.util.ArrayMap;
+import android.util.Log;
 import android.util.Slog;
 import android.util.SparseArray;
 
+import com.android.internal.annotations.GuardedBy;
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.Preconditions;
+import com.android.server.pm.ShortcutService.InvalidFileFormatException;
 
 import libcore.util.Objects;
 
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 import org.xmlpull.v1.XmlSerializer;
@@ -39,6 +48,8 @@
 
 /**
  * User information used by {@link ShortcutService}.
+ *
+ * All methods should be guarded by {@code #mService.mLock}.
  */
 class ShortcutUser {
     private static final String TAG = ShortcutService.TAG;
@@ -47,7 +58,14 @@
     private static final String TAG_LAUNCHER = "launcher";
 
     private static final String ATTR_VALUE = "value";
-    private static final String ATTR_KNOWN_LOCALE_CHANGE_SEQUENCE_NUMBER = "locale-seq-no";
+    private static final String ATTR_KNOWN_LOCALES = "locales";
+
+    // Suffix "2" was added to force rescan all packages after the next OTA.
+    private static final String ATTR_LAST_APP_SCAN_TIME = "last-app-scan-time2";
+    private static final String ATTR_LAST_APP_SCAN_OS_FINGERPRINT = "last-app-scan-fp";
+    private static final String KEY_USER_ID = "userId";
+    private static final String KEY_LAUNCHERS = "launchers";
+    private static final String KEY_PACKAGES = "packages";
 
     static final class PackageWithUser {
         final int userId;
@@ -83,25 +101,37 @@
 
         @Override
         public String toString() {
-            return String.format("{Package: %d, %s}", userId, packageName);
+            return String.format("[Package: %d, %s]", userId, packageName);
         }
     }
 
+    final ShortcutService mService;
+
     @UserIdInt
     private final int mUserId;
 
     private final ArrayMap<String, ShortcutPackage> mPackages = new ArrayMap<>();
 
-    private final SparseArray<ShortcutPackage> mPackagesFromUid = new SparseArray<>();
-
     private final ArrayMap<PackageWithUser, ShortcutLauncher> mLaunchers = new ArrayMap<>();
 
-    /** Default launcher that can access the launcher apps APIs. */
-    private ComponentName mLauncherComponent;
+    /**
+     * Last known launcher.  It's used when the default launcher isn't set in PM -- i.e.
+     * when getHomeActivitiesAsUser() return null.  We need it so that in this situation the
+     * previously default launcher can still access shortcuts.
+     */
+    private ComponentName mLastKnownLauncher;
 
-    private long mKnownLocaleChangeSequenceNumber;
+    /** In-memory-cached default launcher. */
+    private ComponentName mCachedLauncher;
 
-    public ShortcutUser(int userId) {
+    private String mKnownLocales;
+
+    private long mLastAppScanTime;
+
+    private String mLastAppScanOsFingerprint;
+
+    public ShortcutUser(ShortcutService service, int userId) {
+        mService = service;
         mUserId = userId;
     }
 
@@ -109,6 +139,22 @@
         return mUserId;
     }
 
+    public long getLastAppScanTime() {
+        return mLastAppScanTime;
+    }
+
+    public void setLastAppScanTime(long lastAppScanTime) {
+        mLastAppScanTime = lastAppScanTime;
+    }
+
+    public String getLastAppScanOsFingerprint() {
+        return mLastAppScanOsFingerprint;
+    }
+
+    public void setLastAppScanOsFingerprint(String lastAppScanOsFingerprint) {
+        mLastAppScanOsFingerprint = lastAppScanOsFingerprint;
+    }
+
     // We don't expose this directly to non-test code because only ShortcutUser should add to/
     // remove from it.
     @VisibleForTesting
@@ -116,10 +162,19 @@
         return mPackages;
     }
 
-    public ShortcutPackage removePackage(@NonNull ShortcutService s, @NonNull String packageName) {
+    public boolean hasPackage(@NonNull String packageName) {
+        return mPackages.containsKey(packageName);
+    }
+
+    private void addPackage(@NonNull ShortcutPackage p) {
+        p.replaceUser(this);
+        mPackages.put(p.getPackageName(), p);
+    }
+
+    public ShortcutPackage removePackage(@NonNull String packageName) {
         final ShortcutPackage removed = mPackages.remove(packageName);
 
-        s.cleanupBitmapsForPackage(mUserId, packageName);
+        mService.cleanupBitmapsForPackage(mUserId, packageName);
 
         return removed;
     }
@@ -131,28 +186,39 @@
         return mLaunchers;
     }
 
-    public void addLauncher(ShortcutLauncher launcher) {
+    private void addLauncher(ShortcutLauncher launcher) {
+        launcher.replaceUser(this);
         mLaunchers.put(PackageWithUser.of(launcher.getPackageUserId(),
                 launcher.getPackageName()), launcher);
     }
 
+    @Nullable
     public ShortcutLauncher removeLauncher(
             @UserIdInt int packageUserId, @NonNull String packageName) {
         return mLaunchers.remove(PackageWithUser.of(packageUserId, packageName));
     }
 
-    public ShortcutPackage getPackageShortcuts(ShortcutService s, @NonNull String packageName) {
-        ShortcutPackage ret = mPackages.get(packageName);
-        if (ret == null) {
-            ret = new ShortcutPackage(s, this, mUserId, packageName);
-            mPackages.put(packageName, ret);
-        } else {
-            ret.attemptToRestoreIfNeededAndSave(s);
+    @Nullable
+    public ShortcutPackage getPackageShortcutsIfExists(@NonNull String packageName) {
+        final ShortcutPackage ret = mPackages.get(packageName);
+        if (ret != null) {
+            ret.attemptToRestoreIfNeededAndSave();
         }
         return ret;
     }
 
-    public ShortcutLauncher getLauncherShortcuts(ShortcutService s, @NonNull String packageName,
+    @NonNull
+    public ShortcutPackage getPackageShortcuts(@NonNull String packageName) {
+        ShortcutPackage ret = getPackageShortcutsIfExists(packageName);
+        if (ret == null) {
+            ret = new ShortcutPackage(this, mUserId, packageName);
+            mPackages.put(packageName, ret);
+        }
+        return ret;
+    }
+
+    @NonNull
+    public ShortcutLauncher getLauncherShortcuts(@NonNull String packageName,
             @UserIdInt int launcherUserId) {
         final PackageWithUser key = PackageWithUser.of(launcherUserId, packageName);
         ShortcutLauncher ret = mLaunchers.get(key);
@@ -160,7 +226,7 @@
             ret = new ShortcutLauncher(this, mUserId, packageName, launcherUserId);
             mLaunchers.put(key, ret);
         } else {
-            ret.attemptToRestoreIfNeededAndSave(s);
+            ret.attemptToRestoreIfNeededAndSave();
         }
         return ret;
     }
@@ -195,72 +261,111 @@
     }
 
     /**
-     * Reset all throttling counters for all packages, if there has been a system locale change.
+     * Must be called at any entry points on {@link ShortcutManager} APIs to make sure the
+     * information on the package is up-to-date.
+     *
+     * We use broadcasts to handle locale changes and package changes, but because broadcasts
+     * are asynchronous, there's a chance a publisher calls getXxxShortcuts() after a certain event
+     * (e.g. system locale change) but shortcut manager hasn't finished processing the broadcast.
+     *
+     * So we call this method at all entry points from publishers to make sure we update all
+     * relevant information.
+     *
+     * Similar inconsistencies can happen when the launcher fetches shortcut information, but
+     * that's a less of an issue because for the launcher we report shortcut changes with
+     * callbacks.
      */
-    public void resetThrottlingIfNeeded(ShortcutService s) {
-        final long currentNo = s.getLocaleChangeSequenceNumber();
-        if (mKnownLocaleChangeSequenceNumber < currentNo) {
-            if (ShortcutService.DEBUG) {
-                Slog.d(TAG, "LocaleChange detected for user " + mUserId);
-            }
+    public void onCalledByPublisher(@NonNull String packageName) {
+        detectLocaleChange();
+        rescanPackageIfNeeded(packageName, /*forceRescan=*/ false);
+    }
 
-            mKnownLocaleChangeSequenceNumber = currentNo;
-
-            forAllPackages(p -> p.resetRateLimiting(s));
-
-            s.scheduleSaveUser(mUserId);
+    private String getKnownLocales() {
+        if (TextUtils.isEmpty(mKnownLocales)) {
+            mKnownLocales = mService.injectGetLocaleTagsForUser(mUserId);
+            mService.scheduleSaveUser(mUserId);
         }
+        return mKnownLocales;
     }
 
     /**
-     * Called when a package is updated.
+     * Check to see if the system locale has changed, and if so, reset throttling
+     * and update resource strings.
      */
-    public void handlePackageUpdated(ShortcutService s, @NonNull String packageName,
-            int newVersionCode) {
-        if (!mPackages.containsKey(packageName)) {
+    public void detectLocaleChange() {
+        final String currentLocales = mService.injectGetLocaleTagsForUser(mUserId);
+        if (getKnownLocales().equals(currentLocales)) {
             return;
         }
-        getPackageShortcuts(s, packageName).handlePackageUpdated(s, newVersionCode);
+        if (ShortcutService.DEBUG) {
+            Slog.d(TAG, "Locale changed from " + currentLocales + " to " + mKnownLocales
+                    + " for user " + mUserId);
+        }
+        mKnownLocales = currentLocales;
+
+        forAllPackages(pkg -> {
+            pkg.resetRateLimiting();
+            pkg.resolveResourceStrings();
+        });
+
+        mService.scheduleSaveUser(mUserId);
+    }
+
+    public void rescanPackageIfNeeded(@NonNull String packageName, boolean forceRescan) {
+        final boolean isNewApp = !mPackages.containsKey(packageName);
+
+        final ShortcutPackage shortcutPackage = getPackageShortcuts(packageName);
+
+        if (!shortcutPackage.rescanPackageIfNeeded(isNewApp, forceRescan)) {
+            if (isNewApp) {
+                mPackages.remove(packageName);
+            }
+        }
     }
 
     public void attemptToRestoreIfNeededAndSave(ShortcutService s, @NonNull String packageName,
             @UserIdInt int packageUserId) {
         forPackageItem(packageName, packageUserId, spi -> {
-            spi.attemptToRestoreIfNeededAndSave(s);
+            spi.attemptToRestoreIfNeededAndSave();
         });
     }
 
-    public void saveToXml(ShortcutService s, XmlSerializer out, boolean forBackup)
+    public void saveToXml(XmlSerializer out, boolean forBackup)
             throws IOException, XmlPullParserException {
         out.startTag(null, TAG_ROOT);
 
-        ShortcutService.writeAttr(out, ATTR_KNOWN_LOCALE_CHANGE_SEQUENCE_NUMBER,
-                mKnownLocaleChangeSequenceNumber);
+        if (!forBackup) {
+            // Don't have to back them up.
+            ShortcutService.writeAttr(out, ATTR_KNOWN_LOCALES, mKnownLocales);
+            ShortcutService.writeAttr(out, ATTR_LAST_APP_SCAN_TIME,
+                    mLastAppScanTime);
+            ShortcutService.writeAttr(out, ATTR_LAST_APP_SCAN_OS_FINGERPRINT,
+                    mLastAppScanOsFingerprint);
 
-        ShortcutService.writeTagValue(out, TAG_LAUNCHER,
-                mLauncherComponent);
+            ShortcutService.writeTagValue(out, TAG_LAUNCHER, mLastKnownLauncher);
+        }
 
         // Can't use forEachPackageItem due to the checked exceptions.
         {
             final int size = mLaunchers.size();
             for (int i = 0; i < size; i++) {
-                saveShortcutPackageItem(s, out, mLaunchers.valueAt(i), forBackup);
+                saveShortcutPackageItem(out, mLaunchers.valueAt(i), forBackup);
             }
         }
         {
             final int size = mPackages.size();
             for (int i = 0; i < size; i++) {
-                saveShortcutPackageItem(s, out, mPackages.valueAt(i), forBackup);
+                saveShortcutPackageItem(out, mPackages.valueAt(i), forBackup);
             }
         }
 
         out.endTag(null, TAG_ROOT);
     }
 
-    private void saveShortcutPackageItem(ShortcutService s, XmlSerializer out,
+    private void saveShortcutPackageItem(XmlSerializer out,
             ShortcutPackageItem spi, boolean forBackup) throws IOException, XmlPullParserException {
         if (forBackup) {
-            if (!s.shouldBackupApp(spi.getPackageName(), spi.getPackageUserId())) {
+            if (!mService.shouldBackupApp(spi.getPackageName(), spi.getPackageUserId())) {
                 return; // Don't save.
             }
             if (spi.getPackageUserId() != spi.getOwnerUserId()) {
@@ -271,60 +376,99 @@
     }
 
     public static ShortcutUser loadFromXml(ShortcutService s, XmlPullParser parser, int userId,
-            boolean fromBackup) throws IOException, XmlPullParserException {
-        final ShortcutUser ret = new ShortcutUser(userId);
+            boolean fromBackup) throws IOException, XmlPullParserException, InvalidFileFormatException {
+        final ShortcutUser ret = new ShortcutUser(s, userId);
 
-        ret.mKnownLocaleChangeSequenceNumber = ShortcutService.parseLongAttribute(parser,
-                ATTR_KNOWN_LOCALE_CHANGE_SEQUENCE_NUMBER);
+        try {
+            ret.mKnownLocales = ShortcutService.parseStringAttribute(parser,
+                    ATTR_KNOWN_LOCALES);
 
-        final int outerDepth = parser.getDepth();
-        int type;
-        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
-                && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
-            if (type != XmlPullParser.START_TAG) {
-                continue;
-            }
-            final int depth = parser.getDepth();
-            final String tag = parser.getName();
+            // If lastAppScanTime is in the future, that means the clock went backwards.
+            // Just scan all apps again.
+            final long lastAppScanTime = ShortcutService.parseLongAttribute(parser,
+                    ATTR_LAST_APP_SCAN_TIME);
+            final long currentTime = s.injectCurrentTimeMillis();
+            ret.mLastAppScanTime = lastAppScanTime < currentTime ? lastAppScanTime : 0;
+            ret.mLastAppScanOsFingerprint = ShortcutService.parseStringAttribute(parser,
+                    ATTR_LAST_APP_SCAN_OS_FINGERPRINT);
+            final int outerDepth = parser.getDepth();
+            int type;
+            while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
+                    && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
+                if (type != XmlPullParser.START_TAG) {
+                    continue;
+                }
+                final int depth = parser.getDepth();
+                final String tag = parser.getName();
 
-            if (depth == outerDepth + 1) {
-                switch (tag) {
-                    case TAG_LAUNCHER: {
-                        ret.mLauncherComponent = ShortcutService.parseComponentNameAttribute(
-                                parser, ATTR_VALUE);
-                        continue;
-                    }
-                    case ShortcutPackage.TAG_ROOT: {
-                        final ShortcutPackage shortcuts = ShortcutPackage.loadFromXml(
-                                s, ret, parser, fromBackup);
+                if (depth == outerDepth + 1) {
+                    switch (tag) {
+                        case TAG_LAUNCHER: {
+                            ret.mLastKnownLauncher = ShortcutService.parseComponentNameAttribute(
+                                    parser, ATTR_VALUE);
+                            continue;
+                        }
+                        case ShortcutPackage.TAG_ROOT: {
+                            final ShortcutPackage shortcuts = ShortcutPackage.loadFromXml(
+                                    s, ret, parser, fromBackup);
 
-                        // Don't use addShortcut(), we don't need to save the icon.
-                        ret.mPackages.put(shortcuts.getPackageName(), shortcuts);
-                        continue;
-                    }
+                            // Don't use addShortcut(), we don't need to save the icon.
+                            ret.mPackages.put(shortcuts.getPackageName(), shortcuts);
+                            continue;
+                        }
 
-                    case ShortcutLauncher.TAG_ROOT: {
-                        ret.addLauncher(
-                                ShortcutLauncher.loadFromXml(parser, ret, userId, fromBackup));
-                        continue;
+                        case ShortcutLauncher.TAG_ROOT: {
+                            ret.addLauncher(
+                                    ShortcutLauncher.loadFromXml(parser, ret, userId, fromBackup));
+                            continue;
+                        }
                     }
                 }
+                ShortcutService.warnForInvalidTag(depth, tag);
             }
-            ShortcutService.warnForInvalidTag(depth, tag);
+        } catch (RuntimeException e) {
+            throw new ShortcutService.InvalidFileFormatException(
+                    "Unable to parse file", e);
         }
         return ret;
     }
 
-    public ComponentName getLauncherComponent() {
-        return mLauncherComponent;
+    public ComponentName getLastKnownLauncher() {
+        return mLastKnownLauncher;
     }
 
-    public void setLauncherComponent(ShortcutService s, ComponentName launcherComponent) {
-        if (Objects.equal(mLauncherComponent, launcherComponent)) {
+    public void setLauncher(ComponentName launcherComponent) {
+        setLauncher(launcherComponent, /* allowPurgeLastKnown */ false);
+    }
+
+    /** Clears the launcher information without clearing the last known one */
+    public void clearLauncher() {
+        setLauncher(null);
+    }
+
+    /**
+     * Clears the launcher information *with(* clearing the last known one; we do this witl
+     * "cmd shortcut clear-default-launcher".
+     */
+    public void forceClearLauncher() {
+        setLauncher(null, /* allowPurgeLastKnown */ true);
+    }
+
+    private void setLauncher(ComponentName launcherComponent, boolean allowPurgeLastKnown) {
+        mCachedLauncher = launcherComponent; // Always update the in-memory cache.
+
+        if (Objects.equal(mLastKnownLauncher, launcherComponent)) {
             return;
         }
-        mLauncherComponent = launcherComponent;
-        s.scheduleSaveUser(mUserId);
+        if (!allowPurgeLastKnown && launcherComponent == null) {
+            return;
+        }
+        mLastKnownLauncher = launcherComponent;
+        mService.scheduleSaveUser(mUserId);
+    }
+
+    public ComponentName getCachedLauncher() {
+        return mCachedLauncher;
     }
 
     public void resetThrottling() {
@@ -333,36 +477,92 @@
         }
     }
 
-    public void dump(@NonNull ShortcutService s, @NonNull PrintWriter pw, @NonNull String prefix) {
+    public void mergeRestoredFile(ShortcutUser restored) {
+        final ShortcutService s = mService;
+        // Note, a restore happens only at the end of setup wizard.  At this point, no apps are
+        // installed from Play Store yet, but it's still possible that system apps have already
+        // published dynamic shortcuts, since some apps do so on BOOT_COMPLETED.
+        // When such a system app has allowbackup=true, then we go ahead and replace all existing
+        // shortcuts with the restored shortcuts.  (Then we'll re-publish manifest shortcuts later
+        // in the call site.)
+        // When such a system app has allowbackup=false, then we'll keep the shortcuts that have
+        // already been published.  So we selectively add restored ShortcutPackages here.
+        //
+        // The same logic applies to launchers, but since launchers shouldn't pin shortcuts
+        // without users interaction it's really not a big deal, so we just clear existing
+        // ShortcutLauncher instances in mLaunchers and add all the restored ones here.
+
+        mLaunchers.clear();
+        restored.forAllLaunchers(sl -> {
+            // If the app is already installed and allowbackup = false, then ignore the restored
+            // data.
+            if (s.isPackageInstalled(sl.getPackageName(), getUserId())
+                    && !s.shouldBackupApp(sl.getPackageName(), getUserId())) {
+                return;
+            }
+            addLauncher(sl);
+        });
+        restored.forAllPackages(sp -> {
+            // If the app is already installed and allowbackup = false, then ignore the restored
+            // data.
+            if (s.isPackageInstalled(sp.getPackageName(), getUserId())
+                    && !s.shouldBackupApp(sp.getPackageName(), getUserId())) {
+                return;
+            }
+
+            final ShortcutPackage previous = getPackageShortcutsIfExists(sp.getPackageName());
+            if (previous != null && previous.hasNonManifestShortcuts()) {
+                Log.w(TAG, "Shortcuts for package " + sp.getPackageName() + " are being restored."
+                        + " Existing non-manifeset shortcuts will be overwritten.");
+            }
+            addPackage(sp);
+        });
+        // Empty the launchers and packages in restored to avoid accidentally using them.
+        restored.mLaunchers.clear();
+        restored.mPackages.clear();
+    }
+
+    public void dump(@NonNull PrintWriter pw, @NonNull String prefix) {
         pw.print(prefix);
         pw.print("User: ");
         pw.print(mUserId);
-        pw.print("  Known locale seq#: ");
-        pw.print(mKnownLocaleChangeSequenceNumber);
+        pw.print("  Known locales: ");
+        pw.print(mKnownLocales);
+        pw.print("  Last app scan: [");
+        pw.print(mLastAppScanTime);
+        pw.print("] ");
+        pw.print(ShortcutService.formatTime(mLastAppScanTime));
+        pw.print("  Last app scan FP: ");
+        pw.print(mLastAppScanOsFingerprint);
         pw.println();
 
         prefix += prefix + "  ";
 
         pw.print(prefix);
-        pw.print("Default launcher: ");
-        pw.print(mLauncherComponent);
+        pw.print("Cached launcher: ");
+        pw.print(mCachedLauncher);
+        pw.println();
+
+        pw.print(prefix);
+        pw.print("Last known launcher: ");
+        pw.print(mLastKnownLauncher);
         pw.println();
 
         for (int i = 0; i < mLaunchers.size(); i++) {
-            mLaunchers.valueAt(i).dump(s, pw, prefix);
+            mLaunchers.valueAt(i).dump(pw, prefix);
         }
 
         for (int i = 0; i < mPackages.size(); i++) {
-            mPackages.valueAt(i).dump(s, pw, prefix);
+            mPackages.valueAt(i).dump(pw, prefix);
         }
 
         pw.println();
         pw.print(prefix);
         pw.println("Bitmap directories: ");
-        dumpDirectorySize(s, pw, prefix + "  ", s.getUserBitmapFilePath(mUserId));
+        dumpDirectorySize(pw, prefix + "  ", mService.getUserBitmapFilePath(mUserId));
     }
 
-    private void dumpDirectorySize(@NonNull ShortcutService s, @NonNull PrintWriter pw,
+    private void dumpDirectorySize(@NonNull PrintWriter pw,
             @NonNull String prefix, File path) {
         int numFiles = 0;
         long size = 0;
@@ -373,7 +573,7 @@
                     numFiles++;
                     size += child.length();
                 } else if (child.isDirectory()) {
-                    dumpDirectorySize(s, pw, prefix + "  ", child);
+                    dumpDirectorySize(pw, prefix + "  ", child);
                 }
             }
         }
@@ -385,7 +585,31 @@
         pw.print(" files, size=");
         pw.print(size);
         pw.print(" (");
-        pw.print(Formatter.formatFileSize(s.mContext, size));
+        pw.print(Formatter.formatFileSize(mService.mContext, size));
         pw.println(")");
     }
+
+    public JSONObject dumpCheckin(boolean clear) throws JSONException {
+        final JSONObject result = new JSONObject();
+
+        result.put(KEY_USER_ID, mUserId);
+
+        {
+            final JSONArray launchers = new JSONArray();
+            for (int i = 0; i < mLaunchers.size(); i++) {
+                launchers.put(mLaunchers.valueAt(i).dumpCheckin(clear));
+            }
+            result.put(KEY_LAUNCHERS, launchers);
+        }
+
+        {
+            final JSONArray packages = new JSONArray();
+            for (int i = 0; i < mPackages.size(); i++) {
+                packages.put(mPackages.valueAt(i).dumpCheckin(clear));
+            }
+            result.put(KEY_PACKAGES, packages);
+        }
+
+        return result;
+    }
 }
diff --git a/services/core/java/com/android/server/pm/UserManagerService.java b/services/core/java/com/android/server/pm/UserManagerService.java
index 0fb27df..0a385f3 100644
--- a/services/core/java/com/android/server/pm/UserManagerService.java
+++ b/services/core/java/com/android/server/pm/UserManagerService.java
@@ -27,15 +27,18 @@
 import android.app.ActivityManager;
 import android.app.ActivityManagerInternal;
 import android.app.ActivityManagerNative;
+import android.app.AppGlobals;
 import android.app.IActivityManager;
 import android.app.IStopUserCallback;
 import android.app.KeyguardManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.IntentSender;
+import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
 import android.content.pm.UserInfo;
@@ -70,6 +73,7 @@
 import android.system.ErrnoException;
 import android.system.Os;
 import android.system.OsConstants;
+import android.text.TextUtils;
 import android.util.AtomicFile;
 import android.util.IntArray;
 import android.util.Log;
@@ -176,7 +180,8 @@
             UserInfo.FLAG_MANAGED_PROFILE
             | UserInfo.FLAG_EPHEMERAL
             | UserInfo.FLAG_RESTRICTED
-            | UserInfo.FLAG_GUEST;
+            | UserInfo.FLAG_GUEST
+            | UserInfo.FLAG_DEMO;
 
     private static final int MIN_USER_ID = 10;
     // We need to keep process uid within Integer.MAX_VALUE.
@@ -452,6 +457,7 @@
             // user restriction was not a default guest restriction.
             setUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, true, currentGuestUser.id);
         }
+
         mContext.registerReceiver(mDisableQuietModeCallback,
                 new IntentFilter(ACTION_DISABLE_QUIET_MODE_AFTER_UNLOCK),
                 null, mHandler);
@@ -466,6 +472,8 @@
                 UserInfo ui = mUsers.valueAt(i).info;
                 if ((ui.partial || ui.guestToRemove || ui.isEphemeral()) && i != 0) {
                     partials.add(ui);
+                    mRemovingUserIds.append(ui.id, true);
+                    ui.partial = true;
                 }
             }
         }
@@ -862,12 +870,25 @@
             }
         }
         synchronized (mUsersLock) {
-            UserInfo userInfo =  getUserInfoLU(userId);
+            UserInfo userInfo = getUserInfoLU(userId);
             return userInfo != null && userInfo.isManagedProfile();
         }
     }
 
     @Override
+    public boolean isDemoUser(int userId) {
+        int callingUserId = UserHandle.getCallingUserId();
+        if (callingUserId != userId && !hasManageUsersPermission()) {
+            throw new SecurityException("You need MANAGE_USERS permission to query if u=" + userId
+                    + " is a demo user");
+        }
+        synchronized (mUsersLock) {
+            UserInfo userInfo = getUserInfoLU(userId);
+            return userInfo != null && userInfo.isDemo();
+        }
+    }
+
+    @Override
     public boolean isRestricted() {
         synchronized (mUsersLock) {
             return getUserInfoLU(UserHandle.getCallingUserId()).isRestricted();
@@ -1779,6 +1800,18 @@
         mUserVersion = USER_VERSION;
 
         Bundle restrictions = new Bundle();
+        try {
+            final String[] defaultFirstUserRestrictions = mContext.getResources().getStringArray(
+                    com.android.internal.R.array.config_defaultFirstUserRestrictions);
+            for (String userRestriction : defaultFirstUserRestrictions) {
+                if (UserRestrictionsUtils.isValidRestriction(userRestriction)) {
+                    restrictions.putBoolean(userRestriction, true);
+                }
+            }
+        } catch (Resources.NotFoundException e) {
+            Log.e(LOG_TAG, "Couldn't find resource: config_defaultFirstUserRestrictions", e);
+        }
+
         synchronized (mRestrictionsLock) {
             mBaseUserRestrictions.append(UserHandle.USER_SYSTEM, restrictions);
         }
@@ -2162,6 +2195,7 @@
         final boolean isGuest = (flags & UserInfo.FLAG_GUEST) != 0;
         final boolean isManagedProfile = (flags & UserInfo.FLAG_MANAGED_PROFILE) != 0;
         final boolean isRestricted = (flags & UserInfo.FLAG_RESTRICTED) != 0;
+        final boolean isDemo = (flags & UserInfo.FLAG_DEMO) != 0;
         final long ident = Binder.clearCallingIdentity();
         UserInfo userInfo;
         UserData userData;
@@ -2179,8 +2213,8 @@
                     Log.e(LOG_TAG, "Cannot add more managed profiles for user " + parentId);
                     return null;
                 }
-                if (!isGuest && !isManagedProfile && isUserLimitReached()) {
-                    // If we're not adding a guest user or a managed profile and the limit has
+                if (!isGuest && !isManagedProfile && !isDemo && isUserLimitReached()) {
+                    // If we're not adding a guest/demo user or a managed profile and the limit has
                     // been reached, cannot add a user.
                     return null;
                 }
@@ -2206,7 +2240,8 @@
                         return null;
                     }
                 }
-                if (!UserManager.isSplitSystemUser() && (flags & UserInfo.FLAG_EPHEMERAL) != 0) {
+                if (!UserManager.isSplitSystemUser() && (flags & UserInfo.FLAG_EPHEMERAL) != 0
+                        && (flags & UserInfo.FLAG_DEMO) == 0) {
                     Log.e(LOG_TAG,
                             "Ephemeral users are supported on split-system-user systems only.");
                     return null;
@@ -2282,6 +2317,7 @@
             synchronized (mRestrictionsLock) {
                 mBaseUserRestrictions.append(userId, restrictions);
             }
+            mPm.onNewUserCreated(userId);
             Intent addedIntent = new Intent(Intent.ACTION_USER_ADDED);
             addedIntent.putExtra(Intent.EXTRA_USER_HANDLE, userId);
             mContext.sendBroadcastAsUser(addedIntent, UserHandle.ALL,
@@ -2852,11 +2888,9 @@
             synchronized (mRestrictionsLock) {
                 applyUserRestrictionsLR(userId);
             }
-            UserInfo userInfo = getUserInfoNoChecks(userId);
-            if (userInfo != null && !userInfo.isInitialized()) {
-                mPm.onBeforeUserStartUninitialized(userId);
-            }
         }
+
+        maybeInitializeDemoMode(userId);
     }
 
     /**
@@ -2871,6 +2905,7 @@
 
     /**
      * Make a note of the last started time of a user and do some cleanup.
+     * This is called with ActivityManagerService lock held.
      * @param userId the user that was just foregrounded
      */
     public void onUserLoggedIn(@UserIdInt int userId) {
@@ -2888,6 +2923,29 @@
         scheduleWriteUser(userData);
     }
 
+    private void maybeInitializeDemoMode(int userId) {
+        if (UserManager.isDeviceInDemoMode(mContext) && userId != UserHandle.USER_SYSTEM) {
+            String demoLauncher =
+                    mContext.getResources().getString(
+                            com.android.internal.R.string.config_demoModeLauncherComponent);
+            if (!TextUtils.isEmpty(demoLauncher)) {
+                ComponentName componentToEnable = ComponentName.unflattenFromString(demoLauncher);
+                String demoLauncherPkg = componentToEnable.getPackageName();
+                try {
+                    final IPackageManager iPm = AppGlobals.getPackageManager();
+                    iPm.setComponentEnabledSetting(componentToEnable,
+                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, /* flags= */ 0,
+                            /* userId= */ userId);
+                    iPm.setApplicationEnabledSetting(demoLauncherPkg,
+                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, /* flags= */ 0,
+                            /* userId= */ userId, null);
+                } catch (RemoteException re) {
+                    // Internal, shouldn't happen
+                }
+            }
+        }
+    }
+
     /**
      * Returns the next available user id, filling in any holes in the ids.
      * TODO: May not be a good idea to recycle ids, in case it results in confusion
@@ -2919,6 +2977,14 @@
      *             number is mismatched.
      */
     public static void enforceSerialNumber(File file, int serialNumber) throws IOException {
+        if (StorageManager.isFileEncryptedEmulatedOnly()) {
+            // When we're emulating FBE, the directory may have been chmod
+            // 000'ed, meaning we can't read the serial number to enforce it;
+            // instead of destroying the user, just log a warning.
+            Slog.w(LOG_TAG, "Device is emulating FBE; assuming current serial number is valid");
+            return;
+        }
+
         final int foundSerial = getSerialNumber(file);
         Slog.v(LOG_TAG, "Found " + file + " with serial number " + foundSerial);
 
diff --git a/services/core/java/com/android/server/pm/UserRestrictionsUtils.java b/services/core/java/com/android/server/pm/UserRestrictionsUtils.java
index 1f1c6f8..3df13a9 100644
--- a/services/core/java/com/android/server/pm/UserRestrictionsUtils.java
+++ b/services/core/java/com/android/server/pm/UserRestrictionsUtils.java
@@ -26,13 +26,12 @@
 import android.app.ActivityManagerNative;
 import android.content.ContentResolver;
 import android.content.Context;
-import android.net.Uri;
 import android.os.Binder;
 import android.os.Bundle;
 import android.os.RemoteException;
-import android.os.SystemProperties;
 import android.os.UserHandle;
 import android.os.UserManager;
+import android.service.persistentdata.PersistentDataBlockManager;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.util.Log;
@@ -103,7 +102,9 @@
             UserManager.DISALLOW_RUN_IN_BACKGROUND,
             UserManager.DISALLOW_DATA_ROAMING,
             UserManager.DISALLOW_SET_USER_ICON,
-            UserManager.DISALLOW_SET_WALLPAPER
+            UserManager.DISALLOW_SET_WALLPAPER,
+            UserManager.DISALLOW_OEM_UNLOCK,
+            UserManager.DISALLLOW_UNMUTE_DEVICE,
     });
 
     /**
@@ -137,7 +138,8 @@
      */
     private static final Set<String> IMMUTABLE_BY_OWNERS = Sets.newArraySet(
             UserManager.DISALLOW_RECORD_AUDIO,
-            UserManager.DISALLOW_WALLPAPER
+            UserManager.DISALLOW_WALLPAPER,
+            UserManager.DISALLOW_OEM_UNLOCK
     );
 
     /**
@@ -147,7 +149,8 @@
     private static final Set<String> GLOBAL_RESTRICTIONS = Sets.newArraySet(
             UserManager.DISALLOW_ADJUST_VOLUME,
             UserManager.DISALLOW_RUN_IN_BACKGROUND,
-            UserManager.DISALLOW_UNMUTE_MICROPHONE
+            UserManager.DISALLOW_UNMUTE_MICROPHONE,
+            UserManager.DISALLLOW_UNMUTE_DEVICE
     );
 
     /**
@@ -183,8 +186,7 @@
         serializer.endTag(null, tag);
     }
 
-    public static void readRestrictions(XmlPullParser parser, Bundle restrictions)
-            throws IOException {
+    public static void readRestrictions(XmlPullParser parser, Bundle restrictions) {
         for (String key : USER_RESTRICTIONS) {
             final String value = parser.getAttributeValue(null, key);
             if (value != null) {
@@ -427,6 +429,22 @@
                             android.provider.Settings.Global.SAFE_BOOT_DISALLOWED,
                             newValue ? 1 : 0);
                     break;
+                case UserManager.DISALLOW_FACTORY_RESET:
+                case UserManager.DISALLOW_OEM_UNLOCK:
+                    if (newValue) {
+                        PersistentDataBlockManager manager = (PersistentDataBlockManager) context
+                                .getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
+                        if (manager != null
+                                && manager.getOemUnlockEnabled()
+                                && manager.getFlashLockState()
+                                        != PersistentDataBlockManager.FLASH_LOCK_UNLOCKED) {
+                            // Only disable OEM unlock if the bootloader is locked. If it's already
+                            // unlocked, setting the OEM unlock enabled flag to false has no effect
+                            // (the bootloader would remain unlocked).
+                            manager.setOemUnlockEnabled(false);
+                        }
+                    }
+                    break;
             }
         } finally {
             Binder.restoreCallingIdentity(id);
diff --git a/services/core/java/com/android/server/policy/GlobalActions.java b/services/core/java/com/android/server/policy/GlobalActions.java
index 5ef518e..a8bd4d2 100644
--- a/services/core/java/com/android/server/policy/GlobalActions.java
+++ b/services/core/java/com/android/server/policy/GlobalActions.java
@@ -20,6 +20,7 @@
 import com.android.internal.app.AlertController.AlertParams;
 import com.android.internal.logging.MetricsLogger;
 import com.android.internal.logging.MetricsProto.MetricsEvent;
+import com.android.internal.policy.EmergencyAffordanceManager;
 import com.android.internal.telephony.TelephonyIntents;
 import com.android.internal.telephony.TelephonyProperties;
 import com.android.internal.R;
@@ -27,7 +28,6 @@
 
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
-import android.app.AlertDialog;
 import android.app.Dialog;
 import android.content.BroadcastReceiver;
 import android.content.Context;
@@ -103,6 +103,7 @@
     private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
     private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
     private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
+    private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
 
     private final Context mContext;
     private final WindowManagerFuncs mWindowManagerFuncs;
@@ -124,6 +125,7 @@
     private boolean mHasTelephony;
     private boolean mHasVibrator;
     private final boolean mShowSilentToggle;
+    private final EmergencyAffordanceManager mEmergencyAffordanceManager;
 
     /**
      * @param context everything needs a context :(
@@ -158,6 +160,8 @@
 
         mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
                 com.android.internal.R.bool.config_useFixedVolume);
+
+        mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
     }
 
     /**
@@ -298,6 +302,8 @@
                 mItems.add(getVoiceAssistAction());
             } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
                 mItems.add(getAssistAction());
+            } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
+                mItems.add(new RestartAction());
             } else {
                 Log.e(TAG, "Invalid global action key " + actionKey);
             }
@@ -305,6 +311,10 @@
             addedKeys.add(actionKey);
         }
 
+        if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
+            mItems.add(getEmergencyAction());
+        }
+
         mAdapter = new MyAdapter();
 
         AlertParams params = new AlertParams(mContext);
@@ -369,6 +379,38 @@
         }
     }
 
+    private final class RestartAction extends SinglePressAction implements LongPressAction {
+        private RestartAction() {
+            super(R.drawable.ic_restart, R.string.global_action_restart);
+        }
+
+        @Override
+        public boolean onLongPress() {
+            UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+            if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
+                mWindowManagerFuncs.rebootSafeMode(true);
+                return true;
+            }
+            return false;
+        }
+
+        @Override
+        public boolean showDuringKeyguard() {
+            return true;
+        }
+
+        @Override
+        public boolean showBeforeProvisioning() {
+            return true;
+        }
+
+        @Override
+        public void onPress() {
+            mWindowManagerFuncs.reboot(false /* confirm */);
+        }
+    }
+
+
     private class BugReportAction extends SinglePressAction implements LongPressAction {
 
         public BugReportAction() {
@@ -458,6 +500,26 @@
         };
     }
 
+    private Action getEmergencyAction() {
+        return new SinglePressAction(com.android.internal.R.drawable.emergency_icon,
+                R.string.global_action_emergency) {
+            @Override
+            public void onPress() {
+                mEmergencyAffordanceManager.performEmergencyCall();
+            }
+
+            @Override
+            public boolean showDuringKeyguard() {
+                return true;
+            }
+
+            @Override
+            public boolean showBeforeProvisioning() {
+                return true;
+            }
+        };
+    }
+
     private Action getAssistAction() {
         return new SinglePressAction(com.android.internal.R.drawable.ic_action_assist_focused,
                 R.string.global_action_assist) {
@@ -1145,7 +1207,7 @@
         public GlobalActionsDialog(Context context, AlertParams params) {
             super(context, getDialogTheme(context));
             mContext = getContext();
-            mAlert = new AlertController(mContext, this, getWindow());
+            mAlert = AlertController.create(mContext, this, getWindow());
             mAdapter = (MyAdapter) params.mAdapter;
             mWindowTouchSlop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
             params.apply(mAlert);
diff --git a/services/core/java/com/android/server/policy/ImmersiveModeConfirmation.java b/services/core/java/com/android/server/policy/ImmersiveModeConfirmation.java
index a77d512..9bf0476 100644
--- a/services/core/java/com/android/server/policy/ImmersiveModeConfirmation.java
+++ b/services/core/java/com/android/server/policy/ImmersiveModeConfirmation.java
@@ -25,17 +25,19 @@
 import android.content.IntentFilter;
 import android.graphics.PixelFormat;
 import android.graphics.drawable.ColorDrawable;
+import android.os.Binder;
 import android.os.Handler;
+import android.os.IBinder;
 import android.os.Message;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.provider.Settings;
 import android.service.vr.IVrManager;
 import android.service.vr.IVrStateCallbacks;
 import android.util.DisplayMetrics;
 import android.util.Slog;
-import android.util.SparseBooleanArray;
 import android.view.Gravity;
 import android.view.MotionEvent;
 import android.view.View;
@@ -65,6 +67,7 @@
     private final H mHandler;
     private final long mShowDelayMs;
     private final long mPanicThresholdMs;
+    private final IBinder mWindowToken = new Binder();
 
     private boolean mConfirmed;
     private ClingWindowView mClingWindow;
@@ -133,7 +136,7 @@
     }
 
     public void immersiveModeChangedLw(String pkg, boolean isImmersiveMode,
-            boolean userSetupComplete) {
+            boolean userSetupComplete, boolean navBarEmpty) {
         mHandler.removeMessages(H.SHOW);
         if (isImmersiveMode) {
             final boolean disabled = PolicyControl.disableImmersiveConfirmation(pkg);
@@ -142,7 +145,9 @@
             if (!disabled
                     && (DEBUG_SHOW_EVERY_TIME || !mConfirmed)
                     && userSetupComplete
-                    && !mVrModeEnabled) {
+                    && !mVrModeEnabled
+                    && !navBarEmpty
+                    && !UserManager.isDeviceInDemoMode(mContext)) {
                 mHandler.sendEmptyMessageDelayed(H.SHOW, mShowDelayMs);
             }
         } else {
@@ -150,12 +155,13 @@
         }
     }
 
-    public boolean onPowerKeyDown(boolean isScreenOn, long time, boolean inImmersiveMode) {
+    public boolean onPowerKeyDown(boolean isScreenOn, long time, boolean inImmersiveMode,
+            boolean navBarEmpty) {
         if (!isScreenOn && (time - mPanicTime < mPanicThresholdMs)) {
             // turning the screen back on within the panic threshold
             return mClingWindow == null;
         }
-        if (isScreenOn && inImmersiveMode) {
+        if (isScreenOn && inImmersiveMode && !navBarEmpty) {
             // turning the screen off, remember if we were in immersive mode
             mPanicTime = time;
         } else {
@@ -186,13 +192,13 @@
                 WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
                 0
                         | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
-                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                         | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
                 ,
                 PixelFormat.TRANSLUCENT);
         lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
         lp.setTitle("ImmersiveModeConfirmation");
         lp.windowAnimations = com.android.internal.R.style.Animation_ImmersiveModeConfirmation;
+        lp.token = getWindowToken();
         return lp;
     }
 
@@ -204,6 +210,13 @@
                 Gravity.CENTER_HORIZONTAL | Gravity.TOP);
     }
 
+    /**
+     * @return the window token that's used by all ImmersiveModeConfirmation windows.
+     */
+    public IBinder getWindowToken() {
+        return mWindowToken;
+    }
+
     private class ClingWindowView extends FrameLayout {
         private static final int BGCOLOR = 0x80000000;
         private static final int OFFSET_DP = 96;
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index a4408fc..46a236d 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -182,6 +182,8 @@
     // No longer recommended for desk docks;
     static final boolean ENABLE_DESK_DOCK_HOME_CAPTURE = false;
 
+    static final boolean ALTERNATE_CAR_MODE_NAV_SIZE = false;
+
     static final int SHORT_PRESS_POWER_NOTHING = 0;
     static final int SHORT_PRESS_POWER_GO_TO_SLEEP = 1;
     static final int SHORT_PRESS_POWER_REALLY_GO_TO_SLEEP = 2;
@@ -200,6 +202,11 @@
     static final int MULTI_PRESS_POWER_THEATER_MODE = 1;
     static final int MULTI_PRESS_POWER_BRIGHTNESS_BOOST = 2;
 
+    // Number of presses needed before we induce panic press behavior on the back button
+    static final int PANIC_PRESS_BACK_COUNT = 4;
+    static final int PANIC_PRESS_BACK_NOTHING = 0;
+    static final int PANIC_PRESS_BACK_HOME = 1;
+
     // These need to match the documentation/constant in
     // core/res/res/values/config.xml
     static final int LONG_PRESS_HOME_NOTHING = 0;
@@ -216,6 +223,8 @@
     static final int SHORT_PRESS_SLEEP_GO_TO_SLEEP = 0;
     static final int SHORT_PRESS_SLEEP_GO_TO_SLEEP_AND_GO_HOME = 1;
 
+    static final int PENDING_KEY_NULL = -1;
+
     // Controls navigation bar opacity depending on which workspace stacks are currently
     // visible.
     // Nav bar is always opaque when either the freeform stack or docked stack is visible.
@@ -262,6 +271,10 @@
     private static final String SYSUI_SCREENSHOT_ERROR_RECEIVER =
             "com.android.systemui.screenshot.ScreenshotServiceErrorReceiver";
 
+    private static final int NAV_BAR_BOTTOM = 0;
+    private static final int NAV_BAR_RIGHT = 1;
+    private static final int NAV_BAR_LEFT = 2;
+
     /**
      * Keyguard stuff
      */
@@ -295,6 +308,9 @@
     /** Amount of time (in milliseconds) to wait for windows drawn before powering on. */
     static final int WAITING_FOR_DRAWN_TIMEOUT = 1000;
 
+    /** Amount of time (in milliseconds) a toast window can be shown. */
+    public static final int TOAST_WINDOW_TIMEOUT = 3500; // 3.5 seconds
+
     /**
      * Lock protecting internal state.  Must not call out into window
      * manager with lock held.  (This lock will be acquired in places
@@ -354,9 +370,8 @@
     int mStatusBarHeight;
     WindowState mNavigationBar = null;
     boolean mHasNavigationBar = false;
-    boolean mCanHideNavigationBar = false;
     boolean mNavigationBarCanMove = false; // can the navigation bar ever move to the side?
-    boolean mNavigationBarOnBottom = true; // is the navigation bar on the bottom *right now*?
+    int mNavigationBarPosition = NAV_BAR_BOTTOM;
     int[] mNavigationBarHeightForRotationDefault = new int[4];
     int[] mNavigationBarWidthForRotationDefault = new int[4];
     int[] mNavigationBarHeightForRotationInCarMode = new int[4];
@@ -401,12 +416,17 @@
     volatile boolean mBackKeyHandled;
     volatile boolean mBeganFromNonInteractive;
     volatile int mPowerKeyPressCounter;
+    volatile int mBackKeyPressCounter;
     volatile boolean mEndCallKeyHandled;
     volatile boolean mCameraGestureTriggeredDuringGoingToSleep;
     volatile boolean mGoingToSleep;
     volatile boolean mRecentsVisible;
     volatile boolean mTvPictureInPictureVisible;
 
+    // Used to hold the last user key used to wake the device.  This helps us prevent up events
+    // from being passed to the foregrounded app without a corresponding down event
+    volatile int mPendingWakeKey = PENDING_KEY_NULL;
+
     int mRecentAppsHeldModifiers;
     boolean mLanguageSwitchKeyPressed;
 
@@ -455,6 +475,7 @@
     int mDoublePressOnPowerBehavior;
     int mTriplePressOnPowerBehavior;
     int mLongPressOnBackBehavior;
+    int mPanicPressOnBackBehavior;
     int mShortPressOnSleepBehavior;
     int mShortPressWindowBehavior;
     boolean mAwake;
@@ -628,6 +649,9 @@
     // (See Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR.)
     int mIncallPowerBehavior;
 
+    // Behavior of Back button while in-call and screen on
+    int mIncallBackBehavior;
+
     Display mDisplay;
 
     private int mDisplayRotation;
@@ -717,6 +741,7 @@
     private static final int MSG_SHOW_TV_PICTURE_IN_PICTURE_MENU = 17;
     private static final int MSG_BACK_LONG_PRESS = 18;
     private static final int MSG_DISPOSE_INPUT_CONSUMER = 19;
+    private static final int MSG_BACK_DELAYED_PRESS = 20;
 
     private static final int MSG_REQUEST_TRANSIENT_BARS_ARG_STATUS = 0;
     private static final int MSG_REQUEST_TRANSIENT_BARS_ARG_NAVIGATION = 1;
@@ -783,10 +808,15 @@
                     break;
                 case MSG_BACK_LONG_PRESS:
                     backLongPress();
+                    finishBackKeyPress();
                     break;
                 case MSG_DISPOSE_INPUT_CONSUMER:
                     disposeInputConsumer((InputConsumer) msg.obj);
                     break;
+                case MSG_BACK_DELAYED_PRESS:
+                    backMultiPressAction((Long) msg.obj, msg.arg1);
+                    finishBackKeyPress();
+                    break;
             }
         }
     }
@@ -813,6 +843,9 @@
                     Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR), false, this,
                     UserHandle.USER_ALL);
             resolver.registerContentObserver(Settings.Secure.getUriFor(
+                    Settings.Secure.INCALL_BACK_BUTTON_BEHAVIOR), false, this,
+                    UserHandle.USER_ALL);
+            resolver.registerContentObserver(Settings.Secure.getUriFor(
                     Settings.Secure.WAKE_GESTURE_ENABLED), false, this,
                     UserHandle.USER_ALL);
             resolver.registerContentObserver(Settings.System.getUriFor(
@@ -1001,6 +1034,73 @@
         }
     }
 
+    private void interceptBackKeyDown() {
+        // Reset back key state for long press
+        mBackKeyHandled = false;
+
+        // Cancel multi-press detection timeout.
+        if (hasPanicPressOnBackBehavior()) {
+            if (mBackKeyPressCounter != 0
+                    && mBackKeyPressCounter < PANIC_PRESS_BACK_COUNT) {
+                mHandler.removeMessages(MSG_BACK_DELAYED_PRESS);
+            }
+        }
+
+        if (hasLongPressOnBackBehavior()) {
+            Message msg = mHandler.obtainMessage(MSG_BACK_LONG_PRESS);
+            msg.setAsynchronous(true);
+            mHandler.sendMessageDelayed(msg,
+                    ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
+        }
+    }
+
+    // returns true if the key was handled and should not be passed to the user
+    private boolean interceptBackKeyUp(KeyEvent event) {
+        // Cache handled state
+        boolean handled = mBackKeyHandled;
+
+        if (hasPanicPressOnBackBehavior()) {
+            // Check for back key panic press
+            ++mBackKeyPressCounter;
+
+            final long eventTime = event.getDownTime();
+
+            if (mBackKeyPressCounter <= PANIC_PRESS_BACK_COUNT) {
+                // This could be a multi-press.  Wait a little bit longer to confirm.
+                Message msg = mHandler.obtainMessage(MSG_BACK_DELAYED_PRESS,
+                    mBackKeyPressCounter, 0, eventTime);
+                msg.setAsynchronous(true);
+                mHandler.sendMessageDelayed(msg, ViewConfiguration.getMultiPressTimeout());
+            }
+        }
+
+        // Reset back long press state
+        cancelPendingBackKeyAction();
+
+        if (mHasFeatureWatch) {
+            TelecomManager telecomManager = getTelecommService();
+
+            if (telecomManager != null) {
+                if (telecomManager.isRinging()) {
+                    // Pressing back while there's a ringing incoming
+                    // call should silence the ringer.
+                    telecomManager.silenceRinger();
+
+                    // It should not prevent navigating away
+                    return false;
+                } else if (
+                    (mIncallBackBehavior & Settings.Secure.INCALL_BACK_BUTTON_BEHAVIOR_HANGUP) != 0
+                        && telecomManager.isInCall()) {
+                    // Otherwise, if "Back button ends call" is enabled,
+                    // the Back button will hang up any current active call.
+                    return telecomManager.endCall();
+                }
+            }
+        }
+
+        return handled;
+    }
+
     private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
         // Hold a wake lock until the power key is released.
         if (!mPowerKeyWakeLock.isHeld()) {
@@ -1015,7 +1115,8 @@
         // Detect user pressing the power button in panic when an application has
         // taken over the whole screen.
         boolean panic = mImmersiveModeConfirmation.onPowerKeyDown(interactive,
-                SystemClock.elapsedRealtime(), isImmersiveMode(mLastSystemUiFlags));
+                SystemClock.elapsedRealtime(), isImmersiveMode(mLastSystemUiFlags),
+                isNavBarEmpty(mLastSystemUiFlags));
         if (panic) {
             mHandler.post(mHiddenNavPanic);
         }
@@ -1130,6 +1231,10 @@
         }
     }
 
+    private void finishBackKeyPress() {
+        mBackKeyPressCounter = 0;
+    }
+
     private void cancelPendingPowerKeyAction() {
         if (!mPowerKeyHandled) {
             mPowerKeyHandled = true;
@@ -1144,6 +1249,18 @@
         }
     }
 
+    private void backMultiPressAction(long eventTime, int count) {
+        if (count >= PANIC_PRESS_BACK_COUNT) {
+            switch (mPanicPressOnBackBehavior) {
+                case PANIC_PRESS_BACK_NOTHING:
+                    break;
+                case PANIC_PRESS_BACK_HOME:
+                    launchHomeFromHotKey();
+                    break;
+            }
+        }
+    }
+
     private void powerPress(long eventTime, boolean interactive, int count) {
         if (mScreenOnEarly && !mScreenOnFully) {
             Slog.i(TAG, "Suppressed redundant power key press while "
@@ -1258,8 +1375,13 @@
             case LONG_PRESS_BACK_NOTHING:
                 break;
             case LONG_PRESS_BACK_GO_TO_VOICE_ASSIST:
-                Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
-                startActivityAsUser(intent, UserHandle.CURRENT_OR_SELF);
+                final boolean keyguardActive = mKeyguardDelegate == null
+                        ? false
+                        : mKeyguardDelegate.isShowing();
+                if (!keyguardActive) {
+                    Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
+                    startActivityAsUser(intent, UserHandle.CURRENT_OR_SELF);
+                }
                 break;
         }
     }
@@ -1302,6 +1424,10 @@
         return mLongPressOnBackBehavior != LONG_PRESS_BACK_NOTHING;
     }
 
+    private boolean hasPanicPressOnBackBehavior() {
+        return mPanicPressOnBackBehavior != PANIC_PRESS_BACK_NOTHING;
+    }
+
     private void interceptScreenshotChord() {
         if (mScreenshotChordEnabled
                 && mScreenshotChordVolumeDownKeyTriggered && mScreenshotChordPowerKeyTriggered
@@ -1629,6 +1755,8 @@
 
         mLongPressOnBackBehavior = mContext.getResources().getInteger(
                 com.android.internal.R.integer.config_longPressOnBackBehavior);
+        mPanicPressOnBackBehavior = mContext.getResources().getInteger(
+                com.android.internal.R.integer.config_backPanicBehavior);
 
         mShortPressOnPowerBehavior = mContext.getResources().getInteger(
                 com.android.internal.R.integer.config_shortPressOnPowerBehavior);
@@ -1683,13 +1811,19 @@
                     }
                     @Override
                     public void onSwipeFromBottom() {
-                        if (mNavigationBar != null && mNavigationBarOnBottom) {
+                        if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_BOTTOM) {
                             requestTransientBars(mNavigationBar);
                         }
                     }
                     @Override
                     public void onSwipeFromRight() {
-                        if (mNavigationBar != null && !mNavigationBarOnBottom) {
+                        if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_RIGHT) {
+                            requestTransientBars(mNavigationBar);
+                        }
+                    }
+                    @Override
+                    public void onSwipeFromLeft() {
+                        if (mNavigationBar != null && mNavigationBarPosition == NAV_BAR_LEFT) {
                             requestTransientBars(mNavigationBar);
                         }
                     }
@@ -1920,6 +2054,10 @@
                     Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR,
                     Settings.Secure.INCALL_POWER_BUTTON_BEHAVIOR_DEFAULT,
                     UserHandle.USER_CURRENT);
+            mIncallBackBehavior = Settings.Secure.getIntForUser(resolver,
+                    Settings.Secure.INCALL_BACK_BUTTON_BEHAVIOR,
+                    Settings.Secure.INCALL_BACK_BUTTON_BEHAVIOR_DEFAULT,
+                    UserHandle.USER_CURRENT);
 
             // Configure wake gesture.
             boolean wakeGestureEnabledSetting = Settings.Secure.getIntForUser(resolver,
@@ -2213,6 +2351,22 @@
                     attrs.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
                 }
                 break;
+
+            case TYPE_SCREENSHOT:
+                attrs.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
+                break;
+
+            case TYPE_TOAST:
+                // While apps should use the dedicated toast APIs to add such windows
+                // it possible legacy apps to add the window directly. Therefore, we
+                // make windows added directly by the app behave as a toast as much
+                // as possible in terms of timeout and animation.
+                if (attrs.hideTimeoutMilliseconds < 0
+                        || attrs.hideTimeoutMilliseconds > TOAST_WINDOW_TIMEOUT) {
+                    attrs.hideTimeoutMilliseconds = TOAST_WINDOW_TIMEOUT;
+                }
+                attrs.windowAnimations = com.android.internal.R.style.Animation_Toast;
+                break;
         }
 
         if (attrs.type != TYPE_STATUS_BAR) {
@@ -2301,22 +2455,24 @@
         mNavigationBarWidthForRotationDefault[mSeascapeRotation] =
                 res.getDimensionPixelSize(com.android.internal.R.dimen.navigation_bar_width);
 
-        // Height of the navigation bar when presented horizontally at bottom
-        mNavigationBarHeightForRotationInCarMode[mPortraitRotation] =
-        mNavigationBarHeightForRotationInCarMode[mUpsideDownRotation] =
-                res.getDimensionPixelSize(
-                        com.android.internal.R.dimen.navigation_bar_height_car_mode);
-        mNavigationBarHeightForRotationInCarMode[mLandscapeRotation] =
-        mNavigationBarHeightForRotationInCarMode[mSeascapeRotation] = res.getDimensionPixelSize(
-                com.android.internal.R.dimen.navigation_bar_height_landscape_car_mode);
+        if (ALTERNATE_CAR_MODE_NAV_SIZE) {
+            // Height of the navigation bar when presented horizontally at bottom
+            mNavigationBarHeightForRotationInCarMode[mPortraitRotation] =
+            mNavigationBarHeightForRotationInCarMode[mUpsideDownRotation] =
+                    res.getDimensionPixelSize(
+                            com.android.internal.R.dimen.navigation_bar_height_car_mode);
+            mNavigationBarHeightForRotationInCarMode[mLandscapeRotation] =
+            mNavigationBarHeightForRotationInCarMode[mSeascapeRotation] = res.getDimensionPixelSize(
+                    com.android.internal.R.dimen.navigation_bar_height_landscape_car_mode);
 
-        // Width of the navigation bar when presented vertically along one side
-        mNavigationBarWidthForRotationInCarMode[mPortraitRotation] =
-        mNavigationBarWidthForRotationInCarMode[mUpsideDownRotation] =
-        mNavigationBarWidthForRotationInCarMode[mLandscapeRotation] =
-        mNavigationBarWidthForRotationInCarMode[mSeascapeRotation] =
-                res.getDimensionPixelSize(
-                        com.android.internal.R.dimen.navigation_bar_width_car_mode);
+            // Width of the navigation bar when presented vertically along one side
+            mNavigationBarWidthForRotationInCarMode[mPortraitRotation] =
+            mNavigationBarWidthForRotationInCarMode[mUpsideDownRotation] =
+            mNavigationBarWidthForRotationInCarMode[mLandscapeRotation] =
+            mNavigationBarWidthForRotationInCarMode[mSeascapeRotation] =
+                    res.getDimensionPixelSize(
+                            com.android.internal.R.dimen.navigation_bar_width_car_mode);
+        }
     }
 
     /** {@inheritDoc} */
@@ -2448,7 +2604,7 @@
     }
 
     private int getNavigationBarWidth(int rotation, int uiMode) {
-        if ((uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_CAR) {
+        if (ALTERNATE_CAR_MODE_NAV_SIZE && (uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_CAR) {
             return mNavigationBarWidthForRotationInCarMode[rotation];
         } else {
             return mNavigationBarWidthForRotationDefault[rotation];
@@ -2469,7 +2625,7 @@
     }
 
     private int getNavigationBarHeight(int rotation, int uiMode) {
-        if ((uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_CAR) {
+        if (ALTERNATE_CAR_MODE_NAV_SIZE && (uiMode & UI_MODE_TYPE_MASK) == UI_MODE_TYPE_CAR) {
             return mNavigationBarHeightForRotationInCarMode[rotation];
         } else {
             return mNavigationBarHeightForRotationDefault[rotation];
@@ -2782,16 +2938,20 @@
             if (win.getAttrs().windowAnimations != 0) {
                 return 0;
             }
-            // This can be on either the bottom or the right.
-            if (mNavigationBarOnBottom) {
+            // This can be on either the bottom or the right or the left.
+            if (mNavigationBarPosition == NAV_BAR_BOTTOM) {
                 if (transit == TRANSIT_EXIT
                         || transit == TRANSIT_HIDE) {
-                    return R.anim.dock_bottom_exit;
+                    if (isKeyguardShowingAndNotOccluded()) {
+                        return R.anim.dock_bottom_exit_keyguard;
+                    } else {
+                        return R.anim.dock_bottom_exit;
+                    }
                 } else if (transit == TRANSIT_ENTER
                         || transit == TRANSIT_SHOW) {
                     return R.anim.dock_bottom_enter;
                 }
-            } else {
+            } else if (mNavigationBarPosition == NAV_BAR_RIGHT) {
                 if (transit == TRANSIT_EXIT
                         || transit == TRANSIT_HIDE) {
                     return R.anim.dock_right_exit;
@@ -2799,6 +2959,14 @@
                         || transit == TRANSIT_SHOW) {
                     return R.anim.dock_right_enter;
                 }
+            } else if (mNavigationBarPosition == NAV_BAR_LEFT) {
+                if (transit == TRANSIT_EXIT
+                        || transit == TRANSIT_HIDE) {
+                    return R.anim.dock_left_exit;
+                } else if (transit == TRANSIT_ENTER
+                        || transit == TRANSIT_SHOW) {
+                    return R.anim.dock_left_enter;
+                }
             }
         } else if (win.getAttrs().type == TYPE_DOCK_DIVIDER) {
             return selectDockedDividerAnimationLw(win, transit);
@@ -2827,10 +2995,12 @@
         // If the divider is behind the navigation bar, don't animate.
         final Rect frame = win.getFrameLw();
         final boolean behindNavBar = mNavigationBar != null
-                && ((mNavigationBarOnBottom
+                && ((mNavigationBarPosition == NAV_BAR_BOTTOM
                         && frame.top + insets >= mNavigationBar.getFrameLw().top)
-                || (!mNavigationBarOnBottom
-                        && frame.left + insets >= mNavigationBar.getFrameLw().left));
+                || (mNavigationBarPosition == NAV_BAR_RIGHT
+                        && frame.left + insets >= mNavigationBar.getFrameLw().left)
+                || (mNavigationBarPosition == NAV_BAR_LEFT
+                        && frame.right - insets <= mNavigationBar.getFrameLw().right));
         final boolean landscape = frame.height() > frame.width();
         final boolean offscreenLandscape = landscape && (frame.right - insets <= 0
                 || frame.left + insets >= win.getDisplayFrameLw().right);
@@ -2855,9 +3025,14 @@
                 + mTopFullscreenOpaqueWindowState + " rotationAnimation="
                 + (mTopFullscreenOpaqueWindowState == null ?
                         "0" : mTopFullscreenOpaqueWindowState.getAttrs().rotationAnimation));
-        if (mTopFullscreenOpaqueWindowState != null && mTopIsFullscreen) {
-            switch (mTopFullscreenOpaqueWindowState.getAttrs().rotationAnimation) {
+        if (mTopFullscreenOpaqueWindowState != null) {
+            int animationHint = mTopFullscreenOpaqueWindowState.getRotationAnimationHint();
+            if (animationHint < 0 && mTopIsFullscreen) {
+                animationHint = mTopFullscreenOpaqueWindowState.getAttrs().rotationAnimation;
+            }
+            switch (animationHint) {
                 case ROTATION_ANIMATION_CROSSFADE:
+                case ROTATION_ANIMATION_SEAMLESS: // Crossfade is fallback for seamless.
                     anim[0] = R.anim.rotation_animation_xfade_exit;
                     anim[1] = R.anim.rotation_animation_enter;
                     break;
@@ -3031,15 +3206,6 @@
                     return -1;
                 }
 
-                // If an incoming call is ringing, HOME is totally disabled.
-                // (The user is already on the InCallUI at this point,
-                // and his ONLY options are to answer or reject the call.)
-                TelecomManager telecomManager = getTelecommService();
-                if (telecomManager != null && telecomManager.isRinging()) {
-                    Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
-                    return -1;
-                }
-
                 // Delay handling home if a double-tap is possible.
                 if (mDoubleTapOnHomeBehavior != DOUBLE_TAP_HOME_NOTHING) {
                     mHandler.removeCallbacks(mHomeDoubleTapTimeoutRunnable); // just in case
@@ -3539,6 +3705,14 @@
         }
     }
 
+    @Override
+    public boolean canShowDismissingWindowWhileLockedLw() {
+        // If the keyguard is trusted, it will unlock without a challange. Therefore, windows with
+        // FLAG_DISMISS_KEYGUARD don't need to be force hidden, as they will unlock the phone right
+        // away anyways.
+        return mKeyguardDelegate != null && mKeyguardDelegate.isTrusted();
+    }
+
     private void launchAssistLongPressAction() {
         performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
         sendCloseSystemWindows(SYSTEM_DIALOG_REASON_ASSIST);
@@ -3799,11 +3973,19 @@
     };
 
     @Override
+    public void setRecentsVisibilityLw(boolean visible) {
+        mRecentsVisible = visible;
+    }
+
+    @Override
+    public void setTvPipVisibilityLw(boolean visible) {
+        mTvPictureInPictureVisible = visible;
+    }
+
+    @Override
     public int adjustSystemUiVisibilityLw(int visibility) {
         mStatusBarController.adjustSystemUiVisibilityLw(mLastSystemUiFlags, visibility);
         mNavigationBarController.adjustSystemUiVisibilityLw(mLastSystemUiFlags, visibility);
-        mRecentsVisible = (visibility & View.RECENT_APPS_VISIBLE) > 0;
-        mTvPictureInPictureVisible = (visibility & View.TV_PICTURE_IN_PICTURE_VISIBLE) > 0;
 
         // Reset any bits in mForceClearingStatusBarVisibility that
         // are now clear.
@@ -4023,7 +4205,7 @@
             navVisible |= !canHideNavigationBar();
 
             boolean updateSysUiVisibility = layoutNavigationBar(displayWidth, displayHeight,
-                    displayRotation, uiMode, overscanRight, overscanBottom, dcf, navVisible, navTranslucent,
+                    displayRotation, uiMode, overscanLeft, overscanRight, overscanBottom, dcf, navVisible, navTranslucent,
                     navAllowedHidden, statusBarExpandedNotKeyguard);
             if (DEBUG_LAYOUT) Slog.i(TAG, String.format("mDock rect: (%d,%d - %d,%d)",
                     mDockLeft, mDockTop, mDockRight, mDockBottom));
@@ -4102,8 +4284,8 @@
     }
 
     private boolean layoutNavigationBar(int displayWidth, int displayHeight, int displayRotation,
-            int uiMode, int overscanRight, int overscanBottom, Rect dcf, boolean navVisible,
-            boolean navTranslucent, boolean navAllowedHidden,
+            int uiMode, int overscanLeft, int overscanRight, int overscanBottom, Rect dcf,
+            boolean navVisible, boolean navTranslucent, boolean navAllowedHidden,
             boolean statusBarExpandedNotKeyguard) {
         if (mNavigationBar != null) {
             boolean transientNavBarShowing = mNavigationBarController.isTransientShowing();
@@ -4111,8 +4293,9 @@
             // size.  We need to do this directly, instead of relying on
             // it to bubble up from the nav bar, because this needs to
             // change atomically with screen rotations.
-            mNavigationBarOnBottom = isNavigationBarOnBottom(displayWidth, displayHeight);
-            if (mNavigationBarOnBottom) {
+            mNavigationBarPosition = navigationBarPosition(displayWidth, displayHeight,
+                    displayRotation);
+            if (mNavigationBarPosition == NAV_BAR_BOTTOM) {
                 // It's a system nav bar or a portrait screen; nav bar goes on bottom.
                 int top = displayHeight - overscanBottom
                         - getNavigationBarHeight(displayRotation, uiMode);
@@ -4138,7 +4321,7 @@
                     // we can tell the app that it is covered by it.
                     mSystemBottom = mTmpNavigationFrame.top;
                 }
-            } else {
+            } else if (mNavigationBarPosition == NAV_BAR_RIGHT) {
                 // Landscape screen; nav bar goes to the right.
                 int left = displayWidth - overscanRight
                         - getNavigationBarWidth(displayRotation, uiMode);
@@ -4164,6 +4347,33 @@
                     // we can tell the app that it is covered by it.
                     mSystemRight = mTmpNavigationFrame.left;
                 }
+            } else if (mNavigationBarPosition == NAV_BAR_LEFT) {
+                // Seascape screen; nav bar goes to the left.
+                int right = overscanLeft + getNavigationBarWidth(displayRotation, uiMode);
+                mTmpNavigationFrame.set(overscanLeft, 0, right, displayHeight);
+                mStableLeft = mStableFullscreenLeft = mTmpNavigationFrame.right;
+                if (transientNavBarShowing) {
+                    mNavigationBarController.setBarShowingLw(true);
+                } else if (navVisible) {
+                    mNavigationBarController.setBarShowingLw(true);
+                    mDockLeft = mTmpNavigationFrame.right;
+                    // TODO: not so sure about those:
+                    mRestrictedScreenLeft = mRestrictedOverscanScreenLeft = mDockLeft;
+                    mRestrictedScreenWidth = mDockRight - mRestrictedScreenLeft;
+                    mRestrictedOverscanScreenWidth = mDockRight - mRestrictedOverscanScreenLeft;
+                } else {
+                    // We currently want to hide the navigation UI - unless we expanded the status
+                    // bar.
+                    mNavigationBarController.setBarShowingLw(statusBarExpandedNotKeyguard);
+                }
+                if (navVisible && !navTranslucent && !navAllowedHidden
+                        && !mNavigationBar.isAnimatingLw()
+                        && !mNavigationBarController.wasRecentlyTranslucent()) {
+                    // If the nav bar is currently requested to be visible,
+                    // and not in the process of animating on or off, then
+                    // we can tell the app that it is covered by it.
+                    mSystemLeft = mTmpNavigationFrame.right;
+                }
             }
             // Make sure the content and current rectangles are updated to
             // account for the restrictions from the navigation bar.
@@ -4184,8 +4394,15 @@
         return false;
     }
 
-    private boolean isNavigationBarOnBottom(int displayWidth, int displayHeight) {
-        return !mNavigationBarCanMove || displayWidth < displayHeight;
+    private int navigationBarPosition(int displayWidth, int displayHeight, int displayRotation) {
+        if (mNavigationBarCanMove && displayWidth > displayHeight) {
+            if (displayRotation == Surface.ROTATION_270) {
+                return NAV_BAR_LEFT;
+            } else {
+                return NAV_BAR_RIGHT;
+            }
+        }
+        return NAV_BAR_BOTTOM;
     }
 
     /** {@inheritDoc} */
@@ -4361,7 +4578,11 @@
             if (mStatusBar != null && mFocusedWindow == mStatusBar && canReceiveInput(mStatusBar)) {
                 // The status bar forces the navigation bar while it's visible. Make sure the IME
                 // avoids the navigation bar in that case.
-                pf.right = df.right = of.right = cf.right = vf.right = mStableRight;
+                if (mNavigationBarPosition == NAV_BAR_RIGHT) {
+                    pf.right = df.right = of.right = cf.right = vf.right = mStableRight;
+                } else if (mNavigationBarPosition == NAV_BAR_LEFT) {
+                    pf.left = df.left = of.left = cf.left = vf.left = mStableLeft;
+                }
             }
             // IM dock windows always go to the bottom of the screen.
             attrs.gravity = Gravity.BOTTOM;
@@ -4390,6 +4611,8 @@
             } else {
                 vf.set(cf);
             }
+        } else if (attrs.type == TYPE_WALLPAPER) {
+           layoutWallpaper(win, pf, df, of, cf);
         } else if (win == mStatusBar) {
             pf.left = df.left = of.left = mUnrestrictedScreenLeft;
             pf.top = df.top = of.top = mUnrestrictedScreenTop;
@@ -4613,17 +4836,6 @@
                             + mOverscanScreenWidth;
                     pf.bottom = df.bottom = of.bottom = cf.bottom = mOverscanScreenTop
                             + mOverscanScreenHeight;
-                } else if (attrs.type == TYPE_WALLPAPER) {
-                    // The wallpaper also has Real Ultimate Power, but we want to tell
-                    // it about the overscan area.
-                    pf.left = df.left = mOverscanScreenLeft;
-                    pf.top = df.top = mOverscanScreenTop;
-                    pf.right = df.right = mOverscanScreenLeft + mOverscanScreenWidth;
-                    pf.bottom = df.bottom = mOverscanScreenTop + mOverscanScreenHeight;
-                    of.left = cf.left = mUnrestrictedScreenLeft;
-                    of.top = cf.top = mUnrestrictedScreenTop;
-                    of.right = cf.right = mUnrestrictedScreenLeft + mUnrestrictedScreenWidth;
-                    of.bottom = cf.bottom = mUnrestrictedScreenTop + mUnrestrictedScreenHeight;
                 } else if ((fl & FLAG_LAYOUT_IN_OVERSCAN) != 0
                         && attrs.type >= WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW
                         && attrs.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
@@ -4816,6 +5028,20 @@
         }
     }
 
+    private void layoutWallpaper(WindowState win, Rect pf, Rect df, Rect of, Rect cf) {
+
+        // The wallpaper also has Real Ultimate Power, but we want to tell
+        // it about the overscan area.
+        pf.left = df.left = mOverscanScreenLeft;
+        pf.top = df.top = mOverscanScreenTop;
+        pf.right = df.right = mOverscanScreenLeft + mOverscanScreenWidth;
+        pf.bottom = df.bottom = mOverscanScreenTop + mOverscanScreenHeight;
+        of.left = cf.left = mUnrestrictedScreenLeft;
+        of.top = cf.top = mUnrestrictedScreenTop;
+        of.right = cf.right = mUnrestrictedScreenLeft + mUnrestrictedScreenWidth;
+        of.bottom = cf.bottom = mUnrestrictedScreenTop + mUnrestrictedScreenHeight;
+    }
+
     private void offsetInputMethodWindowLw(WindowState win) {
         int top = Math.max(win.getDisplayFrameLw().top, win.getContentFrameLw().top);
         top += win.getGivenContentInsetsLw().top;
@@ -4995,9 +5221,26 @@
             }
         }
 
-        // Keep track of the window if it's dimming but not necessarily fullscreen.
         final boolean reallyVisible = win.isVisibleOrBehindKeyguardLw() && !win.isGoneForLayoutLw();
-        if (mTopFullscreenOpaqueOrDimmingWindowState == null &&  reallyVisible
+
+        // Voice interaction overrides both top fullscreen and top docked.
+        if (reallyVisible && win.getAttrs().type == TYPE_VOICE_INTERACTION) {
+            if (mTopFullscreenOpaqueWindowState == null) {
+                mTopFullscreenOpaqueWindowState = win;
+                if (mTopFullscreenOpaqueOrDimmingWindowState == null) {
+                    mTopFullscreenOpaqueOrDimmingWindowState = win;
+                }
+            }
+            if (mTopDockedOpaqueWindowState == null) {
+                mTopDockedOpaqueWindowState = win;
+                if (mTopDockedOpaqueOrDimmingWindowState == null) {
+                    mTopDockedOpaqueOrDimmingWindowState = win;
+                }
+            }
+        }
+
+        // Keep track of the window if it's dimming but not necessarily fullscreen.
+        if (mTopFullscreenOpaqueOrDimmingWindowState == null && reallyVisible
                 && win.isDimming() && StackId.normallyFullscreenWindows(stackId)) {
             mTopFullscreenOpaqueOrDimmingWindowState = win;
         }
@@ -5174,7 +5417,10 @@
                 }
             } else if (mDismissKeyguard != DISMISS_KEYGUARD_NONE) {
                 mKeyguardHidden = false;
-                if (setKeyguardOccludedLw(false)) {
+                final boolean trusted = mKeyguardDelegate.isTrusted();
+                if (trusted) {
+                    // No need to un-occlude keyguard - we'll dimiss it right away anyways.
+                } else if (setKeyguardOccludedLw(false)) {
                     changes |= FINISH_LAYOUT_REDO_LAYOUT
                             | FINISH_LAYOUT_REDO_CONFIG
                             | FINISH_LAYOUT_REDO_WALLPAPER;
@@ -5184,7 +5430,7 @@
                     mHandler.post(new Runnable() {
                         @Override
                         public void run() {
-                            mKeyguardDelegate.dismiss();
+                            mKeyguardDelegate.dismiss(trusted /* allowWhileOccluded */);
                         }
                     });
                 }
@@ -5221,12 +5467,18 @@
         boolean showing = mKeyguardDelegate.isShowing();
         if (wasOccluded && !isOccluded && showing) {
             mKeyguardOccluded = false;
-            mKeyguardDelegate.setOccluded(false);
+            mKeyguardDelegate.setOccluded(false, true /* animate */);
             mStatusBar.getAttrs().privateFlags |= PRIVATE_FLAG_KEYGUARD;
+            if (!mKeyguardDelegate.hasLockscreenWallpaper()) {
+                mStatusBar.getAttrs().flags |= FLAG_SHOW_WALLPAPER;
+            }
+            Animation anim = AnimationUtils.loadAnimation(mContext,
+                    com.android.internal.R.anim.wallpaper_open_exit);
+            mWindowManagerFuncs.overridePlayingAppAnimationsLw(anim);
             return true;
         } else if (!wasOccluded && isOccluded && showing) {
             mKeyguardOccluded = true;
-            mKeyguardDelegate.setOccluded(true);
+            mKeyguardDelegate.setOccluded(true, false /* animate */);
             mStatusBar.getAttrs().privateFlags &= ~PRIVATE_FLAG_KEYGUARD;
             mStatusBar.getAttrs().flags &= ~FLAG_SHOW_WALLPAPER;
             return true;
@@ -5411,7 +5663,14 @@
 
                 @Override
                 public void onServiceDisconnected(ComponentName name) {
-                    notifyScreenshotError();
+                    synchronized (mScreenshotLock) {
+                        if (mScreenshotConnection != null) {
+                            mContext.unbindService(mScreenshotConnection);
+                            mScreenshotConnection = null;
+                            mHandler.removeCallbacks(mScreenshotTimeout);
+                            notifyScreenshotError();
+                        }
+                    }
                 }
             };
             if (mContext.bindServiceAsUser(serviceIntent, conn,
@@ -5476,12 +5735,24 @@
             // key to the application.
             result = ACTION_PASS_TO_USER;
             isWakeKey = false;
-        } else if (!interactive && shouldDispatchInputWhenNonInteractive()) {
+
+            if (interactive) {
+                // If the screen is awake, but the button pressed was the one that woke the device
+                // then don't pass it to the application
+                if (keyCode == mPendingWakeKey && !down) {
+                    result = 0;
+                }
+                // Reset the pending key
+                mPendingWakeKey = PENDING_KEY_NULL;
+            }
+        } else if (!interactive && shouldDispatchInputWhenNonInteractive(event)) {
             // If we're currently dozing with the screen on and the keyguard showing, pass the key
             // to the application but preserve its wake key status to make sure we still move
             // from dozing to fully interactive if we would normally go from off to fully
             // interactive.
             result = ACTION_PASS_TO_USER;
+            // Since we're dispatching the input, reset the pending key
+            mPendingWakeKey = PENDING_KEY_NULL;
         } else {
             // When the screen is off and the key is not injected, determine whether
             // to wake the device but don't pass the key to the application.
@@ -5489,6 +5760,10 @@
             if (isWakeKey && (!down || !isWakeKeyWhenScreenOff(keyCode))) {
                 isWakeKey = false;
             }
+            // Cache the wake key on down event so we can also avoid sending the up event to the app
+            if (isWakeKey && down) {
+                mPendingWakeKey = keyCode;
+            }
         }
 
         // If the key would be handled globally, just return the result, don't worry about special
@@ -5509,20 +5784,11 @@
         switch (keyCode) {
             case KeyEvent.KEYCODE_BACK: {
                 if (down) {
-                    mBackKeyHandled = false;
-                    if (hasLongPressOnBackBehavior()) {
-                        Message msg = mHandler.obtainMessage(MSG_BACK_LONG_PRESS);
-                        msg.setAsynchronous(true);
-                        mHandler.sendMessageDelayed(msg,
-                                ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
-                    }
+                    interceptBackKeyDown();
                 } else {
-                    boolean handled = mBackKeyHandled;
+                    boolean handled = interceptBackKeyUp(event);
 
-                    // Reset back key state
-                    cancelPendingBackKeyAction();
-
-                    // Don't pass back press to app if we've already handled it
+                    // Don't pass back press to app if we've already handled it via long press
                     if (handled) {
                         result &= ~ACTION_PASS_TO_USER;
                     }
@@ -5655,6 +5921,18 @@
                 break;
             }
 
+            case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN:
+                // fall through
+            case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP:
+                // fall through
+            case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT:
+                // fall through
+            case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT: {
+                result &= ~ACTION_PASS_TO_USER;
+                interceptSystemNavigationKey(event);
+                break;
+            }
+
             case KeyEvent.KEYCODE_SLEEP: {
                 result &= ~ACTION_PASS_TO_USER;
                 isWakeKey = false;
@@ -5776,6 +6054,23 @@
     }
 
     /**
+     * Handle statusbar expansion events.
+     * @param event
+     */
+    private void interceptSystemNavigationKey(KeyEvent event) {
+        if (event.getAction() == KeyEvent.ACTION_UP && areSystemNavigationKeysEnabled()) {
+            IStatusBarService sbar = getStatusBarService();
+            if (sbar != null) {
+                try {
+                    sbar.handleSystemNavigationKey(event.getKeyCode());
+                } catch (RemoteException e1) {
+                    // oops, no statusbar. Ignore event.
+                }
+            }
+        }
+    }
+
+    /**
      * Returns true if the key can have global actions attached to it.
      * We reserve all power management keys for the system since they require
      * very careful handling.
@@ -5836,7 +6131,7 @@
             }
         }
 
-        if (shouldDispatchInputWhenNonInteractive()) {
+        if (shouldDispatchInputWhenNonInteractive(null)) {
             return ACTION_PASS_TO_USER;
         }
 
@@ -5851,7 +6146,7 @@
         return 0;
     }
 
-    private boolean shouldDispatchInputWhenNonInteractive() {
+    private boolean shouldDispatchInputWhenNonInteractive(KeyEvent event) {
         final boolean displayOff = (mDisplay == null || mDisplay.getState() == Display.STATE_OFF);
 
         if (displayOff && !mHasFeatureWatch) {
@@ -5863,6 +6158,14 @@
             return true;
         }
 
+        // Watches handle BACK specially
+        if (mHasFeatureWatch
+                && event != null
+                && (event.getKeyCode() == KeyEvent.KEYCODE_BACK
+                        || event.getKeyCode() == KeyEvent.KEYCODE_STEM_PRIMARY)) {
+            return false;
+        }
+
         // Send events to a dozing dream even if the screen is off since the dream
         // is in control of the state of the screen.
         IDreamManager dreamManager = getDreamManager();
@@ -6050,7 +6353,9 @@
                     return;
                 }
                 mPendingPanicGestureUptime = SystemClock.uptimeMillis();
-                mNavigationBarController.showTransient();
+                if (!isNavBarEmpty(mLastSystemUiFlags)) {
+                    mNavigationBarController.showTransient();
+                }
             }
         }
     };
@@ -6062,7 +6367,8 @@
                 return;
             }
             boolean sb = mStatusBarController.checkShowTransientBarLw();
-            boolean nb = mNavigationBarController.checkShowTransientBarLw();
+            boolean nb = mNavigationBarController.checkShowTransientBarLw()
+                    && !isNavBarEmpty(mLastSystemUiFlags);
             if (sb || nb) {
                 // Don't show status bar when swiping on already visible navigation bar
                 if (!nb && swipeTarget == mNavigationBar) {
@@ -6373,7 +6679,7 @@
                 @Override
                 public void run() {
                     // ask the keyguard to prompt the user to authenticate if necessary
-                    mKeyguardDelegate.dismiss();
+                    mKeyguardDelegate.dismiss(false /* allowWhileOccluded */);
                 }
             });
         }
@@ -6425,10 +6731,13 @@
 
         // Only navigation bar
         if (mNavigationBar != null) {
-            if (isNavigationBarOnBottom(displayWidth, displayHeight)) {
+            int position = navigationBarPosition(displayWidth, displayHeight, displayRotation);
+            if (position == NAV_BAR_BOTTOM) {
                 outInsets.bottom = getNavigationBarHeight(displayRotation, mUiMode);
-            } else {
+            } else if (position == NAV_BAR_RIGHT) {
                 outInsets.right = getNavigationBarWidth(displayRotation, mUiMode);
+            } else if (position == NAV_BAR_LEFT) {
+                outInsets.left = getNavigationBarWidth(displayRotation, mUiMode);
             }
         }
     }
@@ -6774,9 +7083,7 @@
             @Override public void run() {
                 if (mBootMsgDialog == null) {
                     int theme;
-                    if (mHasFeatureWatch) {
-                        theme = com.android.internal.R.style.Theme_Micro_Dialog_Alert;
-                    } else if (mContext.getPackageManager().hasSystemFeature(FEATURE_TELEVISION)) {
+                    if (mContext.getPackageManager().hasSystemFeature(FEATURE_TELEVISION)) {
                         theme = com.android.internal.R.style.Theme_Leanback_Dialog_Alert;
                     } else {
                         theme = 0;
@@ -7165,6 +7472,11 @@
                 Settings.Global.ENABLE_ACCESSIBILITY_GLOBAL_GESTURE_ENABLED, 0) == 1;
     }
 
+    private boolean areSystemNavigationKeysEnabled() {
+        return Settings.Secure.getIntForUser(mContext.getContentResolver(),
+                Settings.Secure.SYSTEM_NAVIGATION_KEYS_ENABLED, 0, UserHandle.USER_CURRENT) == 1;
+    }
+
     @Override
     public boolean performHapticFeedbackLw(WindowState win, int effectId, boolean always) {
         if (!mVibrator.hasVibrator()) {
@@ -7237,11 +7549,20 @@
     private int updateSystemUiVisibilityLw() {
         // If there is no window focused, there will be nobody to handle the events
         // anyway, so just hang on in whatever state we're in until things settle down.
-        final WindowState win = mFocusedWindow != null ? mFocusedWindow
+        WindowState winCandidate = mFocusedWindow != null ? mFocusedWindow
                 : mTopFullscreenOpaqueWindowState;
-        if (win == null) {
+        if (winCandidate == null) {
             return 0;
         }
+        if (winCandidate.getAttrs().token == mImmersiveModeConfirmation.getWindowToken()) {
+            // The immersive mode confirmation should never affect the system bar visibility,
+            // otherwise it will unhide the navigation bar and hide itself.
+            winCandidate = isStatusBarKeyguard() ? mStatusBar : mTopFullscreenOpaqueWindowState;
+            if (winCandidate == null) {
+                return 0;
+            }
+        }
+        final WindowState win = winCandidate;
         if ((win.getAttrs().privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 && mHideLockScreen == true) {
             // We are updating at a point where the keyguard has gotten
             // focus, but we were last in a state where the top window is
@@ -7409,7 +7730,9 @@
             // we're no longer on the Keyguard and the screen is ready. We can now request the bars.
             mPendingPanicGestureUptime = 0;
             mStatusBarController.showTransient();
-            mNavigationBarController.showTransient();
+            if (!isNavBarEmpty(vis)) {
+                mNavigationBarController.showTransient();
+            }
         }
 
         final boolean denyTransientStatus = mStatusBarController.isTransientShowRequested()
@@ -7441,7 +7764,7 @@
         if (win != null && oldImmersiveMode != newImmersiveMode) {
             final String pkg = win.getOwningPackage();
             mImmersiveModeConfirmation.immersiveModeChangedLw(pkg, newImmersiveMode,
-                    isUserSetupComplete());
+                    isUserSetupComplete(), isNavBarEmpty(win.getSystemUiVisibility()));
         }
 
         vis = mNavigationBarController.updateVisibilityLw(transientNavBarAllowed, oldVis, vis);
@@ -7500,6 +7823,14 @@
                 && canHideNavigationBar();
     }
 
+    private static boolean isNavBarEmpty(int systemUiFlags) {
+        final int disableNavigationBar = (View.STATUS_BAR_DISABLE_HOME
+                | View.STATUS_BAR_DISABLE_BACK
+                | View.STATUS_BAR_DISABLE_RECENT);
+
+        return (systemUiFlags & disableNavigationBar) == disableNavigationBar;
+    }
+
     /**
      * @return whether the navigation or status bar can be made translucent
      *
@@ -7564,6 +7895,43 @@
     }
 
     @Override
+    public boolean shouldRotateSeamlessly(int oldRotation, int newRotation) {
+        // For the upside down rotation we don't rotate seamlessly as the navigation
+        // bar moves position.
+        // Note most apps (using orientation:sensor or user as opposed to fullSensor)
+        // will not enter the reverse portrait orientation, so actually the
+        // orientation won't change at all.
+        if (oldRotation == mUpsideDownRotation || newRotation == mUpsideDownRotation) {
+            return false;
+        }
+        int delta = newRotation - oldRotation;
+        if (delta < 0) delta += 4;
+        // Likewise we don't rotate seamlessly for 180 degree rotations
+        // in this case the surfaces never resize, and our logic to
+        // revert the transformations on size change will fail. We could
+        // fix this in the future with the "tagged" frames idea.
+        if (delta == Surface.ROTATION_180) {
+            return false;
+        }
+
+        final WindowState w = mTopFullscreenOpaqueWindowState;
+        if (w != mFocusedWindow) {
+            return false;
+        }
+
+        // We only enable seamless rotation if the top window has requested
+        // it and is in the fullscreen opaque state. Seamless rotation
+        // requires freezing various Surface states and won't work well
+        // with animations, so we disable it in the animation case for now.
+        if (w != null && !w.isAnimatingLw() &&
+                ((w.getAttrs().rotationAnimation == ROTATION_ANIMATION_JUMPCUT) ||
+                        (w.getAttrs().rotationAnimation == ROTATION_ANIMATION_SEAMLESS))) {
+            return true;
+        }
+        return false;
+    }
+
+    @Override
     public void dump(String prefix, PrintWriter pw, String[] args) {
         pw.print(prefix); pw.print("mSafeMode="); pw.print(mSafeMode);
                 pw.print(" mSystemReady="); pw.print(mSystemReady);
@@ -7736,6 +8104,7 @@
                 pw.print(" mLockScreenTimerActive="); pw.println(mLockScreenTimerActive);
         pw.print(prefix); pw.print("mEndcallBehavior="); pw.print(mEndcallBehavior);
                 pw.print(" mIncallPowerBehavior="); pw.print(mIncallPowerBehavior);
+                pw.print(" mIncallBackBehavior="); pw.print(mIncallBackBehavior);
                 pw.print(" mLongPressOnHomeBehavior="); pw.println(mLongPressOnHomeBehavior);
         pw.print(prefix); pw.print("mLandscapeRotation="); pw.print(mLandscapeRotation);
                 pw.print(" mSeascapeRotation="); pw.println(mSeascapeRotation);
diff --git a/services/core/java/com/android/server/policy/SystemGesturesPointerEventListener.java b/services/core/java/com/android/server/policy/SystemGesturesPointerEventListener.java
index 80e4341..598c58e 100644
--- a/services/core/java/com/android/server/policy/SystemGesturesPointerEventListener.java
+++ b/services/core/java/com/android/server/policy/SystemGesturesPointerEventListener.java
@@ -43,6 +43,7 @@
     private static final int SWIPE_FROM_TOP = 1;
     private static final int SWIPE_FROM_BOTTOM = 2;
     private static final int SWIPE_FROM_RIGHT = 3;
+    private static final int SWIPE_FROM_LEFT = 4;
 
     private final Context mContext;
     private final int mSwipeStartThreshold;
@@ -127,6 +128,9 @@
                     } else if (swipe == SWIPE_FROM_RIGHT) {
                         if (DEBUG) Slog.d(TAG, "Firing onSwipeFromRight");
                         mCallbacks.onSwipeFromRight();
+                    } else if (swipe == SWIPE_FROM_LEFT) {
+                        if (DEBUG) Slog.d(TAG, "Firing onSwipeFromLeft");
+                        mCallbacks.onSwipeFromLeft();
                     }
                 }
                 break;
@@ -229,6 +233,11 @@
                 && elapsed < SWIPE_TIMEOUT_MS) {
             return SWIPE_FROM_RIGHT;
         }
+        if (fromX <= mSwipeStartThreshold
+                && x > fromX + mSwipeDistanceThreshold
+                && elapsed < SWIPE_TIMEOUT_MS) {
+            return SWIPE_FROM_LEFT;
+        }
         return SWIPE_NONE;
     }
 
@@ -265,6 +274,7 @@
         void onSwipeFromTop();
         void onSwipeFromBottom();
         void onSwipeFromRight();
+        void onSwipeFromLeft();
         void onFling(int durationMs);
         void onDown();
         void onUpOrCancel();
diff --git a/services/core/java/com/android/server/policy/WindowOrientationListener.java b/services/core/java/com/android/server/policy/WindowOrientationListener.java
index a32c017..8ef0acb 100644
--- a/services/core/java/com/android/server/policy/WindowOrientationListener.java
+++ b/services/core/java/com/android/server/policy/WindowOrientationListener.java
@@ -49,6 +49,7 @@
             "debug.orientation.log", false);
 
     private static final boolean USE_GRAVITY_SENSOR = false;
+    private static final int DEFAULT_BATCH_LATENCY = 100000;
 
     private Handler mHandler;
     private SensorManager mSensorManager;
@@ -118,7 +119,12 @@
                     Slog.d(TAG, "WindowOrientationListener enabled");
                 }
                 mOrientationJudge.resetLocked();
-                mSensorManager.registerListener(mOrientationJudge, mSensor, mRate, mHandler);
+                if (mSensor.getType() == Sensor.TYPE_ACCELEROMETER) {
+                    mSensorManager.registerListener(
+                            mOrientationJudge, mSensor, mRate, DEFAULT_BATCH_LATENCY, mHandler);
+                } else {
+                    mSensorManager.registerListener(mOrientationJudge, mSensor, mRate, mHandler);
+                }
                 mEnabled = true;
             }
         }
diff --git a/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java b/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java
index 7e27558..2af4c9b 100644
--- a/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java
+++ b/services/core/java/com/android/server/policy/keyguard/KeyguardServiceDelegate.java
@@ -59,6 +59,7 @@
             showingAndNotOccluded = true;
             secure = true;
             deviceHasKeyguard = true;
+            currentUser = UserHandle.USER_NULL;
         }
         boolean showing;
         boolean showingAndNotOccluded;
@@ -157,6 +158,10 @@
             if (mKeyguardState.systemIsReady) {
                 // If the system is ready, it means keyguard crashed and restarted.
                 mKeyguardService.onSystemReady();
+                if (mKeyguardState.currentUser != UserHandle.USER_NULL) {
+                    // There has been a user switch earlier
+                    mKeyguardService.setCurrentUser(mKeyguardState.currentUser);
+                }
                 // This is used to hide the scrim once keyguard displays.
                 if (mKeyguardState.interactiveState == INTERACTIVE_STATE_AWAKE) {
                     mKeyguardService.onStartedWakingUp();
@@ -175,7 +180,7 @@
                 mKeyguardService.onBootCompleted();
             }
             if (mKeyguardState.occluded) {
-                mKeyguardService.setOccluded(mKeyguardState.occluded);
+                mKeyguardService.setOccluded(mKeyguardState.occluded, false /* animate */);
             }
         }
 
@@ -194,6 +199,20 @@
         return mKeyguardState.showing;
     }
 
+    public boolean isTrusted() {
+        if (mKeyguardService != null) {
+            return mKeyguardService.isTrusted();
+        }
+        return false;
+    }
+
+    public boolean hasLockscreenWallpaper() {
+        if (mKeyguardService != null) {
+            return mKeyguardService.hasLockscreenWallpaper();
+        }
+        return false;
+    }
+
     public boolean isInputRestricted() {
         if (mKeyguardService != null) {
             mKeyguardState.inputRestricted = mKeyguardService.isInputRestricted();
@@ -213,16 +232,17 @@
         }
     }
 
-    public void setOccluded(boolean isOccluded) {
+    public void setOccluded(boolean isOccluded, boolean animate) {
         if (mKeyguardService != null) {
-            mKeyguardService.setOccluded(isOccluded);
+            if (DEBUG) Log.v(TAG, "setOccluded(" + isOccluded + ") animate=" + animate);
+            mKeyguardService.setOccluded(isOccluded, animate);
         }
         mKeyguardState.occluded = isOccluded;
     }
 
-    public void dismiss() {
+    public void dismiss(boolean allowWhileOccluded) {
         if (mKeyguardService != null) {
-            mKeyguardService.dismiss();
+            mKeyguardService.dismiss(allowWhileOccluded);
         }
     }
 
diff --git a/services/core/java/com/android/server/policy/keyguard/KeyguardServiceWrapper.java b/services/core/java/com/android/server/policy/keyguard/KeyguardServiceWrapper.java
index 57e8857..2169927 100644
--- a/services/core/java/com/android/server/policy/keyguard/KeyguardServiceWrapper.java
+++ b/services/core/java/com/android/server/policy/keyguard/KeyguardServiceWrapper.java
@@ -63,9 +63,9 @@
     }
 
     @Override // Binder interface
-    public void setOccluded(boolean isOccluded) {
+    public void setOccluded(boolean isOccluded, boolean animate) {
         try {
-            mService.setOccluded(isOccluded);
+            mService.setOccluded(isOccluded, animate);
         } catch (RemoteException e) {
             Slog.w(TAG , "Remote Exception", e);
         }
@@ -81,9 +81,9 @@
     }
 
     @Override // Binder interface
-    public void dismiss() {
+    public void dismiss(boolean allowWhileOccluded) {
         try {
-            mService.dismiss();
+            mService.dismiss(allowWhileOccluded);
         } catch (RemoteException e) {
             Slog.w(TAG , "Remote Exception", e);
         }
@@ -234,6 +234,14 @@
         return mKeyguardStateMonitor.isShowing();
     }
 
+    public boolean isTrusted() {
+        return mKeyguardStateMonitor.isTrusted();
+    }
+
+    public boolean hasLockscreenWallpaper() {
+        return mKeyguardStateMonitor.hasLockscreenWallpaper();
+    }
+
     public boolean isSecure(int userId) {
         return mKeyguardStateMonitor.isSecure(userId);
     }
diff --git a/services/core/java/com/android/server/policy/keyguard/KeyguardStateMonitor.java b/services/core/java/com/android/server/policy/keyguard/KeyguardStateMonitor.java
index 138f068..08eaaa9 100644
--- a/services/core/java/com/android/server/policy/keyguard/KeyguardStateMonitor.java
+++ b/services/core/java/com/android/server/policy/keyguard/KeyguardStateMonitor.java
@@ -43,6 +43,8 @@
     private volatile boolean mIsShowing = true;
     private volatile boolean mSimSecure = true;
     private volatile boolean mInputRestricted = true;
+    private volatile boolean mTrusted = false;
+    private volatile boolean mHasLockscreenWallpaper = false;
 
     private int mCurrentUserId;
 
@@ -70,6 +72,14 @@
         return mInputRestricted;
     }
 
+    public boolean isTrusted() {
+        return mTrusted;
+    }
+
+    public boolean hasLockscreenWallpaper() {
+        return mHasLockscreenWallpaper;
+    }
+
     @Override // Binder interface
     public void onShowingStateChanged(boolean showing) {
         mIsShowing = showing;
@@ -93,12 +103,23 @@
         mInputRestricted = inputRestricted;
     }
 
+    @Override // Binder interface
+    public void onTrustedChanged(boolean trusted) {
+        mTrusted = trusted;
+    }
+
+    @Override // Binder interface
+    public void onHasLockscreenWallpaperChanged(boolean hasLockscreenWallpaper) {
+        mHasLockscreenWallpaper = hasLockscreenWallpaper;
+    }
+
     public void dump(String prefix, PrintWriter pw) {
         pw.println(prefix + TAG);
         prefix += "  ";
         pw.println(prefix + "mIsShowing=" + mIsShowing);
         pw.println(prefix + "mSimSecure=" + mSimSecure);
         pw.println(prefix + "mInputRestricted=" + mInputRestricted);
+        pw.println(prefix + "mTrusted=" + mTrusted);
         pw.println(prefix + "mCurrentUserId=" + mCurrentUserId);
     }
 }
\ No newline at end of file
diff --git a/services/core/java/com/android/server/power/Notifier.java b/services/core/java/com/android/server/power/Notifier.java
index 7108f4a..4e9f5a2 100644
--- a/services/core/java/com/android/server/power/Notifier.java
+++ b/services/core/java/com/android/server/power/Notifier.java
@@ -18,6 +18,7 @@
 
 import android.app.ActivityManagerInternal;
 import android.app.AppOpsManager;
+import android.app.RetailDemoModeServiceInternal;
 
 import com.android.internal.app.IAppOpsService;
 import com.android.internal.app.IBatteryStats;
@@ -91,6 +92,7 @@
     private final ActivityManagerInternal mActivityManagerInternal;
     private final InputManagerInternal mInputManagerInternal;
     private final InputMethodManagerInternal mInputMethodManagerInternal;
+    private final RetailDemoModeServiceInternal mRetailDemoModeServiceInternal;
 
     private final NotifierHandler mHandler;
     private final Intent mScreenOnIntent;
@@ -136,6 +138,7 @@
         mActivityManagerInternal = LocalServices.getService(ActivityManagerInternal.class);
         mInputManagerInternal = LocalServices.getService(InputManagerInternal.class);
         mInputMethodManagerInternal = LocalServices.getService(InputMethodManagerInternal.class);
+        mRetailDemoModeServiceInternal = LocalServices.getService(RetailDemoModeServiceInternal.class);
 
         mHandler = new NotifierHandler(looper);
         mScreenOnIntent = new Intent(Intent.ACTION_SCREEN_ON);
@@ -191,6 +194,48 @@
         }
     }
 
+    public void onLongPartialWakeLockStart(String tag, int ownerUid, WorkSource workSource,
+            String historyTag) {
+        if (DEBUG) {
+            Slog.d(TAG, "onLongPartialWakeLockStart: ownerUid=" + ownerUid
+                    + ", workSource=" + workSource);
+        }
+
+        try {
+            if (workSource != null) {
+                final int N = workSource.size();
+                for (int i=0; i<N; i++) {
+                    mBatteryStats.noteLongPartialWakelockStart(tag, historyTag, workSource.get(i));
+                }
+            } else {
+                mBatteryStats.noteLongPartialWakelockStart(tag, historyTag, ownerUid);
+            }
+        } catch (RemoteException ex) {
+            // Ignore
+        }
+    }
+
+    public void onLongPartialWakeLockFinish(String tag, int ownerUid, WorkSource workSource,
+            String historyTag) {
+        if (DEBUG) {
+            Slog.d(TAG, "onLongPartialWakeLockFinish: ownerUid=" + ownerUid
+                    + ", workSource=" + workSource);
+        }
+
+        try {
+            if (workSource != null) {
+                final int N = workSource.size();
+                for (int i=0; i<N; i++) {
+                    mBatteryStats.noteLongPartialWakelockFinish(tag, historyTag, workSource.get(i));
+                }
+            } else {
+                mBatteryStats.noteLongPartialWakelockFinish(tag, historyTag, ownerUid);
+            }
+        } catch (RemoteException ex) {
+            // Ignore
+        }
+    }
+
     /**
      * Called when a wake lock is changing.
      */
@@ -534,7 +579,9 @@
             }
             mUserActivityPending = false;
         }
-
+        if (mRetailDemoModeServiceInternal != null) {
+            mRetailDemoModeServiceInternal.onUserActivity();
+        }
         mPolicy.userActivity();
     }
 
diff --git a/services/core/java/com/android/server/power/PowerManagerService.java b/services/core/java/com/android/server/power/PowerManagerService.java
index 67103a4..4b91654 100644
--- a/services/core/java/com/android/server/power/PowerManagerService.java
+++ b/services/core/java/com/android/server/power/PowerManagerService.java
@@ -56,6 +56,7 @@
 import android.service.vr.IVrManager;
 import android.service.vr.IVrStateCallbacks;
 import android.util.EventLog;
+import android.util.PrintWriterPrinter;
 import android.util.Slog;
 import android.util.SparseIntArray;
 import android.util.TimeUtils;
@@ -73,9 +74,7 @@
 import com.android.server.am.BatteryStatsService;
 import com.android.server.lights.Light;
 import com.android.server.lights.LightsManager;
-import com.android.server.vr.VrManagerInternal;
 import com.android.server.vr.VrManagerService;
-import com.android.server.vr.VrStateListener;
 import libcore.util.Objects;
 
 import java.io.FileDescriptor;
@@ -108,6 +107,8 @@
     private static final int MSG_SANDMAN = 2;
     // Message: Sent when the screen brightness boost expires.
     private static final int MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT = 3;
+    // Message: Polling to look for long held wake locks.
+    private static final int MSG_CHECK_FOR_LONG_WAKELOCKS = 4;
 
     // Dirty bit: mWakeLocks changed
     private static final int DIRTY_WAKE_LOCKS = 1 << 0;
@@ -159,6 +160,9 @@
     // This should perhaps be a setting.
     private static final int SCREEN_BRIGHTNESS_BOOST_TIMEOUT = 5 * 1000;
 
+    // How long a partial wake lock must be held until we consider it a long wake lock.
+    static final long MIN_LONG_WAKE_CHECK_INTERVAL = 60*1000;
+
     // Power hints defined in hardware/libhardware/include/hardware/power.h.
     private static final int POWER_HINT_LOW_POWER = 5;
     private static final int POWER_HINT_VR_MODE = 7;
@@ -221,6 +225,15 @@
     // A bitfield that summarizes the state of all active wakelocks.
     private int mWakeLockSummary;
 
+    // Have we scheduled a message to check for long wake locks?  This is when we will check.
+    private long mNotifyLongScheduled;
+
+    // Last time we checked for long wake locks.
+    private long mNotifyLongDispatched;
+
+    // The time we decided to do next long check.
+    private long mNotifyLongNextCheck;
+
     // If true, instructs the display controller to wait for the proximity sensor to
     // go negative before turning the screen on.
     private boolean mRequestWaitForNegativeProximity;
@@ -637,10 +650,12 @@
                     false, mSettingsObserver, UserHandle.USER_ALL);
             IVrManager vrManager =
                     (IVrManager) getBinderService(VrManagerService.VR_MANAGER_BINDER_SERVICE);
-            try {
-                vrManager.registerListener(mVrStateCallbacks);
-            } catch (RemoteException e) {
-                Slog.e(TAG, "Failed to register VR mode state listener: " + e);
+            if (vrManager != null) {
+                try {
+                    vrManager.registerListener(mVrStateCallbacks);
+                } catch (RemoteException e) {
+                    Slog.e(TAG, "Failed to register VR mode state listener: " + e);
+                }
             }
             // Go.
             readConfigurationLocked();
@@ -795,9 +810,10 @@
     }
 
     private void updateLowPowerModeLocked() {
-        if (mIsPowered && mLowPowerModeSetting) {
+        if ((mIsPowered || !mBatteryLevelLow && !mBootCompleted) && mLowPowerModeSetting) {
             if (DEBUG_SPEW) {
-                Slog.d(TAG, "updateLowPowerModeLocked: powered, turning setting off");
+                Slog.d(TAG, "updateLowPowerModeLocked: powered or booting with sufficient battery,"
+                        + " turning setting off");
             }
             // Turn setting off if powered
             Settings.Global.putInt(mContext.getContentResolver(),
@@ -1025,6 +1041,38 @@
             mNotifier.onWakeLockAcquired(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName,
                     wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource,
                     wakeLock.mHistoryTag);
+            restartNofifyLongTimerLocked(wakeLock);
+        }
+    }
+
+    private void enqueueNotifyLongMsgLocked(long time) {
+        mNotifyLongScheduled = time;
+        Message msg = mHandler.obtainMessage(MSG_CHECK_FOR_LONG_WAKELOCKS);
+        msg.setAsynchronous(true);
+        mHandler.sendMessageAtTime(msg, time);
+    }
+
+    private void restartNofifyLongTimerLocked(WakeLock wakeLock) {
+        wakeLock.mAcquireTime = SystemClock.uptimeMillis();
+        if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
+                == PowerManager.PARTIAL_WAKE_LOCK && mNotifyLongScheduled == 0) {
+            enqueueNotifyLongMsgLocked(wakeLock.mAcquireTime + MIN_LONG_WAKE_CHECK_INTERVAL);
+        }
+    }
+
+    private void notifyWakeLockLongStartedLocked(WakeLock wakeLock) {
+        if (mSystemReady && !wakeLock.mDisabled) {
+            wakeLock.mNotifiedLong = true;
+            mNotifier.onLongPartialWakeLockStart(wakeLock.mTag, wakeLock.mOwnerUid,
+                    wakeLock.mWorkSource, wakeLock.mHistoryTag);
+        }
+    }
+
+    private void notifyWakeLockLongFinishedLocked(WakeLock wakeLock) {
+        if (wakeLock.mNotifiedLong) {
+            wakeLock.mNotifiedLong = false;
+            mNotifier.onLongPartialWakeLockFinish(wakeLock.mTag, wakeLock.mOwnerUid,
+                    wakeLock.mWorkSource, wakeLock.mHistoryTag);
         }
     }
 
@@ -1034,15 +1082,23 @@
             mNotifier.onWakeLockChanging(wakeLock.mFlags, wakeLock.mTag, wakeLock.mPackageName,
                     wakeLock.mOwnerUid, wakeLock.mOwnerPid, wakeLock.mWorkSource,
                     wakeLock.mHistoryTag, flags, tag, packageName, uid, pid, ws, historyTag);
+            notifyWakeLockLongFinishedLocked(wakeLock);
+            // Changing the wake lock will count as releasing the old wake lock(s) and
+            // acquiring the new ones...  we do this because otherwise once a wakelock
+            // becomes long, if we just continued to treat it as long we can get in to
+            // situations where we spam battery stats with every following change to it.
+            restartNofifyLongTimerLocked(wakeLock);
         }
     }
 
     private void notifyWakeLockReleasedLocked(WakeLock wakeLock) {
         if (mSystemReady && wakeLock.mNotifiedAcquired) {
             wakeLock.mNotifiedAcquired = false;
+            wakeLock.mAcquireTime = 0;
             mNotifier.onWakeLockReleased(wakeLock.mFlags, wakeLock.mTag,
                     wakeLock.mPackageName, wakeLock.mOwnerUid, wakeLock.mOwnerPid,
                     wakeLock.mWorkSource, wakeLock.mHistoryTag);
+            notifyWakeLockLongFinishedLocked(wakeLock);
         }
     }
 
@@ -1599,6 +1655,42 @@
         }
     }
 
+    void checkForLongWakeLocks() {
+        synchronized (mLock) {
+            final long now = SystemClock.uptimeMillis();
+            mNotifyLongDispatched = now;
+            final long when = now - MIN_LONG_WAKE_CHECK_INTERVAL;
+            long nextCheckTime = Long.MAX_VALUE;
+            final int numWakeLocks = mWakeLocks.size();
+            for (int i = 0; i < numWakeLocks; i++) {
+                final WakeLock wakeLock = mWakeLocks.get(i);
+                if ((wakeLock.mFlags & PowerManager.WAKE_LOCK_LEVEL_MASK)
+                        == PowerManager.PARTIAL_WAKE_LOCK) {
+                    if (wakeLock.mNotifiedAcquired && !wakeLock.mNotifiedLong) {
+                        if (wakeLock.mAcquireTime < when) {
+                            // This wake lock has exceeded the long acquire time, report!
+                            notifyWakeLockLongStartedLocked(wakeLock);
+                        } else {
+                            // This wake lock could still become a long one, at this time.
+                            long checkTime = wakeLock.mAcquireTime + MIN_LONG_WAKE_CHECK_INTERVAL;
+                            if (checkTime < nextCheckTime) {
+                                nextCheckTime = checkTime;
+                            }
+                        }
+                    }
+                }
+            }
+            mNotifyLongScheduled = 0;
+            mHandler.removeMessages(MSG_CHECK_FOR_LONG_WAKELOCKS);
+            if (nextCheckTime != Long.MAX_VALUE) {
+                mNotifyLongNextCheck = nextCheckTime;
+                enqueueNotifyLongMsgLocked(nextCheckTime);
+            } else {
+                mNotifyLongNextCheck = 0;
+            }
+        }
+    }
+
     /**
      * Updates the value of mUserActivitySummary to summarize the user requested
      * state of the system such as whether the screen should be bright or dim.
@@ -1996,7 +2088,12 @@
             float screenAutoBrightnessAdjustment = 0.0f;
             boolean autoBrightness = (mScreenBrightnessModeSetting ==
                     Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
-            if (isValidBrightness(mScreenBrightnessOverrideFromWindowManager)) {
+            if (!mBootCompleted) {
+                // Keep the brightness steady during boot. This requires the
+                // bootloader brightness and the default brightness to be identical.
+                autoBrightness = false;
+                brightnessSetByUser = false;
+            } else if (isValidBrightness(mScreenBrightnessOverrideFromWindowManager)) {
                 screenBrightness = mScreenBrightnessOverrideFromWindowManager;
                 autoBrightness = false;
                 brightnessSetByUser = false;
@@ -2444,18 +2541,18 @@
 
     boolean setDeviceIdleModeInternal(boolean enabled) {
         synchronized (mLock) {
-            if (mDeviceIdleMode != enabled) {
-                mDeviceIdleMode = enabled;
-                updateWakeLockDisabledStatesLocked();
-                if (enabled) {
-                    EventLogTags.writeDeviceIdleOnPhase("power");
-                } else {
-                    EventLogTags.writeDeviceIdleOffPhase("power");
-                }
-                return true;
+            if (mDeviceIdleMode == enabled) {
+                return false;
             }
-            return false;
+            mDeviceIdleMode = enabled;
+            updateWakeLockDisabledStatesLocked();
         }
+        if (enabled) {
+            EventLogTags.writeDeviceIdleOnPhase("power");
+        } else {
+            EventLogTags.writeDeviceIdleOffPhase("power");
+        }
+        return true;
     }
 
     boolean setLightDeviceIdleModeInternal(boolean enabled) {
@@ -2748,6 +2845,27 @@
             pw.println("  mHalAutoSuspendModeEnabled=" + mHalAutoSuspendModeEnabled);
             pw.println("  mHalInteractiveModeEnabled=" + mHalInteractiveModeEnabled);
             pw.println("  mWakeLockSummary=0x" + Integer.toHexString(mWakeLockSummary));
+            pw.print("  mNotifyLongScheduled=");
+            if (mNotifyLongScheduled == 0) {
+                pw.print("(none)");
+            } else {
+                TimeUtils.formatDuration(mNotifyLongScheduled, SystemClock.uptimeMillis(), pw);
+            }
+            pw.println();
+            pw.print("  mNotifyLongDispatched=");
+            if (mNotifyLongDispatched == 0) {
+                pw.print("(none)");
+            } else {
+                TimeUtils.formatDuration(mNotifyLongDispatched, SystemClock.uptimeMillis(), pw);
+            }
+            pw.println();
+            pw.print("  mNotifyLongNextCheck=");
+            if (mNotifyLongNextCheck == 0) {
+                pw.print("(none)");
+            } else {
+                TimeUtils.formatDuration(mNotifyLongNextCheck, SystemClock.uptimeMillis(), pw);
+            }
+            pw.println();
             pw.println("  mUserActivitySummary=0x" + Integer.toHexString(mUserActivitySummary));
             pw.println("  mRequestWaitForNegativeProximity=" + mRequestWaitForNegativeProximity);
             pw.println("  mSandmanScheduled=" + mSandmanScheduled);
@@ -2856,6 +2974,10 @@
             }
 
             pw.println();
+            pw.println("Looper state:");
+            mHandler.getLooper().dump(new PrintWriterPrinter(pw), "  ");
+
+            pw.println();
             pw.println("Wake Locks: size=" + mWakeLocks.size());
             for (WakeLock wl : mWakeLocks) {
                 pw.println("  " + wl);
@@ -2984,6 +3106,9 @@
                 case MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT:
                     handleScreenBrightnessBoostTimeout();
                     break;
+                case MSG_CHECK_FOR_LONG_WAKELOCKS:
+                    checkForLongWakeLocks();
+                    break;
             }
         }
     }
@@ -3000,7 +3125,9 @@
         public String mHistoryTag;
         public final int mOwnerUid;
         public final int mOwnerPid;
+        public long mAcquireTime;
         public boolean mNotifiedAcquired;
+        public boolean mNotifiedLong;
         public boolean mDisabled;
 
         public WakeLock(IBinder lock, int flags, String tag, String packageName,
@@ -3059,9 +3186,34 @@
 
         @Override
         public String toString() {
-            return getLockLevelString()
-                    + " '" + mTag + "'" + getLockFlagsString() + (mDisabled ? " DISABLED" : "")
-                    + " (uid=" + mOwnerUid + ", pid=" + mOwnerPid + ", ws=" + mWorkSource + ")";
+            StringBuilder sb = new StringBuilder();
+            sb.append(getLockLevelString());
+            sb.append(" '");
+            sb.append(mTag);
+            sb.append("'");
+            sb.append(getLockFlagsString());
+            if (mDisabled) {
+                sb.append(" DISABLED");
+            }
+            if (mNotifiedAcquired) {
+                sb.append(" ACQ=");
+                TimeUtils.formatDuration(mAcquireTime-SystemClock.uptimeMillis(), sb);
+            }
+            if (mNotifiedLong) {
+                sb.append(" LONG");
+            }
+            sb.append(" (uid=");
+            sb.append(mOwnerUid);
+            if (mOwnerPid != 0) {
+                sb.append(" pid=");
+                sb.append(mOwnerPid);
+            }
+            if (mWorkSource != null) {
+                sb.append(" ws=");
+                sb.append(mWorkSource);
+            }
+            sb.append(")");
+            return sb.toString();
         }
 
         @SuppressWarnings("deprecation")
diff --git a/services/core/java/com/android/server/power/ShutdownThread.java b/services/core/java/com/android/server/power/ShutdownThread.java
index f7f79f3..44894ed 100644
--- a/services/core/java/com/android/server/power/ShutdownThread.java
+++ b/services/core/java/com/android/server/power/ShutdownThread.java
@@ -543,7 +543,8 @@
                 }
 
                 try {
-                    bluetoothOff = bluetooth == null || !bluetooth.isEnabled();
+                    bluetoothOff = bluetooth == null ||
+                            bluetooth.getState() == BluetoothAdapter.STATE_OFF;
                     if (!bluetoothOff) {
                         Log.w(TAG, "Disabling Bluetooth...");
                         bluetooth.disable(false);  // disable but don't persist new state
@@ -577,7 +578,7 @@
 
                     if (!bluetoothOff) {
                         try {
-                            bluetoothOff = !bluetooth.isEnabled();
+                            bluetoothOff = bluetooth.getState() == BluetoothAdapter.STATE_OFF;
                         } catch (RemoteException ex) {
                             Log.e(TAG, "RemoteException during bluetooth shutdown", ex);
                             bluetoothOff = true;
diff --git a/services/core/java/com/android/server/search/SearchManagerService.java b/services/core/java/com/android/server/search/SearchManagerService.java
index 4d91814..f3b9b18 100644
--- a/services/core/java/com/android/server/search/SearchManagerService.java
+++ b/services/core/java/com/android/server/search/SearchManagerService.java
@@ -33,6 +33,7 @@
 import android.database.ContentObserver;
 import android.os.Binder;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.os.UserManager;
@@ -42,6 +43,7 @@
 
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.content.PackageMonitor;
+import com.android.internal.os.BackgroundThread;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.server.LocalServices;
 import com.android.server.SystemService;
@@ -57,6 +59,7 @@
  */
 public class SearchManagerService extends ISearchManager.Stub {
     private static final String TAG = "SearchManagerService";
+    final Handler mHandler;
 
     public static class Lifecycle extends SystemService {
         private SearchManagerService mService;
@@ -72,8 +75,13 @@
         }
 
         @Override
-        public void onUnlockUser(int userHandle) {
-            mService.onUnlockUser(userHandle);
+        public void onUnlockUser(final int userId) {
+            mService.mHandler.post(new Runnable() {
+                @Override
+                public void run() {
+                    mService.onUnlockUser(userId);
+                }
+            });
         }
 
         @Override
@@ -99,6 +107,7 @@
         mContext = context;
         new MyPackageMonitor().register(context, null, UserHandle.ALL, true);
         new GlobalSearchProviderObserver(context.getContentResolver());
+        mHandler = BackgroundThread.getHandler();
     }
 
     private Searchables getSearchables(int userId) {
@@ -132,7 +141,12 @@
     }
 
     private void onUnlockUser(int userId) {
-        getSearchables(userId, true);
+        try {
+            getSearchables(userId, true);
+        } catch (IllegalStateException ignored) {
+            // We're just trying to warm a cache, so we don't mind if the user
+            // was stopped or destroyed before we got here.
+        }
     }
 
     private void onCleanupUser(int userId) {
diff --git a/services/core/java/com/android/server/statusbar/StatusBarManagerService.java b/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
index baa7f1e..552803f 100644
--- a/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
+++ b/services/core/java/com/android/server/statusbar/StatusBarManagerService.java
@@ -29,8 +29,11 @@
 import android.os.RemoteException;
 import android.os.ResultReceiver;
 import android.os.UserHandle;
+import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.Slog;
+import android.view.KeyEvent;
+
 import com.android.internal.statusbar.IStatusBar;
 import com.android.internal.statusbar.IStatusBarService;
 import com.android.internal.statusbar.NotificationVisibility;
@@ -415,6 +418,18 @@
     }
 
     @Override
+    public void handleSystemNavigationKey(int key) throws RemoteException {
+        enforceExpandStatusBar();
+
+        if (mBar != null) {
+            try {
+                mBar.handleSystemNavigationKey(key);
+            } catch (RemoteException ex) {
+            }
+        }
+    }
+
+    @Override
     public void disable(int what, IBinder token, String pkg) {
         disableForUser(what, token, pkg, mCurrentUserId);
     }
@@ -928,6 +943,20 @@
                                 + " token=" + tok.token);
             }
             pw.println("  mCurrentUserId=" + mCurrentUserId);
+            pw.println("  mIcons=");
+            for (String slot : mIcons.keySet()) {
+                pw.println("    ");
+                pw.print(slot);
+                pw.print(" -> ");
+                final StatusBarIcon icon = mIcons.get(slot);
+                pw.print(icon);
+                if (!TextUtils.isEmpty(icon.contentDescription)) {
+                    pw.print(" \"");
+                    pw.print(icon.contentDescription);
+                    pw.print("\"");
+                }
+                pw.println();
+            }
         }
     }
 }
diff --git a/services/core/java/com/android/server/storage/DeviceStorageMonitorService.java b/services/core/java/com/android/server/storage/DeviceStorageMonitorService.java
index cbbcb0e..90c711a 100644
--- a/services/core/java/com/android/server/storage/DeviceStorageMonitorService.java
+++ b/services/core/java/com/android/server/storage/DeviceStorageMonitorService.java
@@ -87,6 +87,11 @@
     private static final long DEFAULT_DISK_FREE_CHANGE_REPORTING_THRESHOLD = 2 * 1024 * 1024; // 2MB
     private static final long DEFAULT_CHECK_INTERVAL = MONITOR_INTERVAL*60*1000;
 
+    // com.android.internal.R.string.low_internal_storage_view_text_no_boot
+    // hard codes 250MB in the message as the storage space required for the
+    // boot image.
+    private static final long BOOT_IMAGE_STORAGE_REQUIREMENT = 250 * 1024 * 1024;
+
     private long mFreeMem;  // on /data
     private long mFreeMemAfterLastCacheClear;  // on /data
     private long mLastReportedFreeMem;
@@ -290,9 +295,10 @@
                     mLowMemFlag = false;
                 }
             }
-            if (!mLowMemFlag && !mIsBootImageOnDisk) {
+            if (!mLowMemFlag && !mIsBootImageOnDisk && mFreeMem < BOOT_IMAGE_STORAGE_REQUIREMENT) {
                 Slog.i(TAG, "No boot image on disk due to lack of space. Sending notification");
                 sendNotification();
+                mLowMemFlag = true;
             }
             if (mFreeMem < mMemFullThreshold) {
                 if (!mMemFullFlag) {
@@ -383,7 +389,7 @@
 
         @Override
         public boolean isMemoryLow() {
-            return mLowMemFlag || !mIsBootImageOnDisk;
+            return mLowMemFlag;
         }
 
         @Override
@@ -455,9 +461,7 @@
         //log the event to event log with the amount of free storage(in bytes) left on the device
         EventLog.writeEvent(EventLogTags.LOW_STORAGE, mFreeMem);
         //  Pack up the values and broadcast them to everyone
-        Intent lowMemIntent = new Intent(Environment.isExternalStorageEmulated()
-                ? Settings.ACTION_INTERNAL_STORAGE_SETTINGS
-                : Intent.ACTION_MANAGE_PACKAGE_STORAGE);
+        Intent lowMemIntent = new Intent(StorageManager.ACTION_MANAGE_STORAGE);
         lowMemIntent.putExtra("memory", mFreeMem);
         lowMemIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         NotificationManager mNotificationMgr =
diff --git a/services/core/java/com/android/server/trust/TrustArchive.java b/services/core/java/com/android/server/trust/TrustArchive.java
index fd63d48..aaac297 100644
--- a/services/core/java/com/android/server/trust/TrustArchive.java
+++ b/services/core/java/com/android/server/trust/TrustArchive.java
@@ -37,6 +37,7 @@
     private static final int TYPE_AGENT_CONNECTED = 4;
     private static final int TYPE_AGENT_STOPPED = 5;
     private static final int TYPE_MANAGING_TRUST = 6;
+    private static final int TYPE_POLICY_CHANGED = 7;
 
     private static final int HISTORY_LIMIT = 200;
 
@@ -99,6 +100,10 @@
         addEvent(new Event(TYPE_MANAGING_TRUST, userId, agent, null, 0, 0, managing));
     }
 
+    public void logDevicePolicyChanged() {
+        addEvent(new Event(TYPE_POLICY_CHANGED, UserHandle.USER_ALL, null, null, 0, 0, false));
+    }
+
     private void addEvent(Event e) {
         if (mEvents.size() >= HISTORY_LIMIT) {
             mEvents.removeFirst();
@@ -112,7 +117,8 @@
         Iterator<Event> iter = mEvents.descendingIterator();
         while (iter.hasNext() && count < limit) {
             Event ev = iter.next();
-            if (userId != UserHandle.USER_ALL && userId != ev.userId) {
+            if (userId != UserHandle.USER_ALL && userId != ev.userId
+                    && ev.userId != UserHandle.USER_ALL) {
                 continue;
             }
 
@@ -122,11 +128,13 @@
             if (userId == UserHandle.USER_ALL) {
                 writer.print("user="); writer.print(ev.userId); writer.print(", ");
             }
-            writer.print("agent=");
-            if (duplicateSimpleNames) {
-                writer.print(ev.agent.flattenToShortString());
-            } else {
-                writer.print(getSimpleName(ev.agent));
+            if (ev.agent != null) {
+                writer.print("agent=");
+                if (duplicateSimpleNames) {
+                    writer.print(ev.agent.flattenToShortString());
+                } else {
+                    writer.print(getSimpleName(ev.agent));
+                }
             }
             switch (ev.type) {
                 case TYPE_GRANT_TRUST:
@@ -181,6 +189,8 @@
                 return "AgentStopped";
             case TYPE_MANAGING_TRUST:
                 return "ManagingTrust";
+            case TYPE_POLICY_CHANGED:
+                return "DevicePolicyChanged";
             default:
                 return "Unknown(" + type + ")";
         }
diff --git a/services/core/java/com/android/server/trust/TrustManagerService.java b/services/core/java/com/android/server/trust/TrustManagerService.java
index c1868a4..a7b9cf4 100644
--- a/services/core/java/com/android/server/trust/TrustManagerService.java
+++ b/services/core/java/com/android/server/trust/TrustManagerService.java
@@ -102,9 +102,8 @@
     private static final int MSG_START_USER = 7;
     private static final int MSG_CLEANUP_USER = 8;
     private static final int MSG_SWITCH_USER = 9;
-    private static final int MSG_SET_DEVICE_LOCKED = 10;
-    private static final int MSG_FLUSH_TRUST_USUALLY_MANAGED = 11;
-    private static final int MSG_UNLOCK_USER = 12;
+    private static final int MSG_FLUSH_TRUST_USUALLY_MANAGED = 10;
+    private static final int MSG_UNLOCK_USER = 11;
 
     private static final int TRUST_USUALLY_MANAGED_FLUSH_DELAY = 2 * 60 * 1000;
 
@@ -317,20 +316,6 @@
         }
     }
 
-    public void setDeviceLockedForUser(int userId, boolean locked) {
-        if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
-            synchronized (mDeviceLockedForUser) {
-                mDeviceLockedForUser.put(userId, locked);
-            }
-            if (locked) {
-                try {
-                    ActivityManagerNative.getDefault().notifyLockedProfile(userId);
-                } catch (RemoteException e) {
-                }
-            }
-        }
-    }
-
     boolean isDeviceLockedInner(int userId) {
         synchronized (mDeviceLockedForUser) {
             return mDeviceLockedForUser.get(userId, true);
@@ -399,12 +384,17 @@
     }
 
     void updateDevicePolicyFeatures() {
+        boolean changed = false;
         for (int i = 0; i < mActiveAgents.size(); i++) {
             AgentInfo info = mActiveAgents.valueAt(i);
             if (info.agent.isConnected()) {
                 info.agent.updateDevicePolicyFeatures();
+                changed = true;
             }
         }
+        if (changed) {
+            mArchive.logDevicePolicyChanged();
+        }
     }
 
     private void removeAgentsOfPackage(String packageName) {
@@ -833,10 +823,24 @@
         }
 
         @Override
-        public void setDeviceLockedForUser(int userId, boolean value) {
+        public void setDeviceLockedForUser(int userId, boolean locked) {
             enforceReportPermission();
-            mHandler.obtainMessage(MSG_SET_DEVICE_LOCKED, value ? 1 : 0, userId)
-                    .sendToTarget();
+            final long identity = Binder.clearCallingIdentity();
+            try {
+                if (mLockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
+                    synchronized (mDeviceLockedForUser) {
+                        mDeviceLockedForUser.put(userId, locked);
+                    }
+                    if (locked) {
+                        try {
+                            ActivityManagerNative.getDefault().notifyLockedProfile(userId);
+                        } catch (RemoteException e) {
+                        }
+                    }
+                }
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
         }
 
         @Override
@@ -912,9 +916,6 @@
                     mCurrentUser = msg.arg1;
                     refreshDeviceLockedForUser(UserHandle.USER_ALL);
                     break;
-                case MSG_SET_DEVICE_LOCKED:
-                    setDeviceLockedForUser(msg.arg2, msg.arg1 != 0);
-                    break;
                 case MSG_FLUSH_TRUST_USUALLY_MANAGED:
                     SparseBooleanArray usuallyManaged;
                     synchronized (mTrustUsuallyManagedForUser) {
diff --git a/services/core/java/com/android/server/twilight/TwilightListener.java b/services/core/java/com/android/server/twilight/TwilightListener.java
index 29ead44..58dcef6 100644
--- a/services/core/java/com/android/server/twilight/TwilightListener.java
+++ b/services/core/java/com/android/server/twilight/TwilightListener.java
@@ -16,6 +16,14 @@
 
 package com.android.server.twilight;
 
+import android.annotation.Nullable;
+
+/**
+ * Callback for when the twilight state has changed.
+ */
 public interface TwilightListener {
-    void onTwilightStateChanged();
+    /**
+     * Called when the twilight state has changed.
+     */
+    void onTwilightStateChanged(@Nullable TwilightState state);
 }
\ No newline at end of file
diff --git a/services/core/java/com/android/server/twilight/TwilightManager.java b/services/core/java/com/android/server/twilight/TwilightManager.java
index 56137a4..5ef9417 100644
--- a/services/core/java/com/android/server/twilight/TwilightManager.java
+++ b/services/core/java/com/android/server/twilight/TwilightManager.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,10 +16,30 @@
 
 package com.android.server.twilight;
 
+import android.annotation.NonNull;
 import android.os.Handler;
 
+/**
+ * This class provides sunrise/sunset information based on the device's current location.
+ */
 public interface TwilightManager {
-    void registerListener(TwilightListener listener, Handler handler);
-    void unregisterListener(TwilightListener listener);
-    TwilightState getCurrentState();
+    /**
+     * Register a listener to be notified whenever the twilight state changes.
+     *
+     * @param listener the {@link TwilightListener} to be notified
+     * @param handler the {@link Handler} to use to notify the listener
+     */
+    void registerListener(@NonNull TwilightListener listener, @NonNull Handler handler);
+
+    /**
+     * Unregisters a previously registered listener.
+     *
+     * @param listener the {@link TwilightListener} to be unregistered
+     */
+    void unregisterListener(@NonNull TwilightListener listener);
+
+    /**
+     * Returns the last {@link TwilightState}, or {@code null} if not available.
+     */
+    TwilightState getLastTwilightState();
 }
diff --git a/services/core/java/com/android/server/twilight/TwilightService.java b/services/core/java/com/android/server/twilight/TwilightService.java
index 6158c92..db7df25 100644
--- a/services/core/java/com/android/server/twilight/TwilightService.java
+++ b/services/core/java/com/android/server/twilight/TwilightService.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,618 +16,295 @@
 
 package com.android.server.twilight;
 
-import com.android.server.SystemService;
-import com.android.server.TwilightCalculator;
-
-import android.app.ActivityManager;
+import android.annotation.NonNull;
 import android.app.AlarmManager;
-import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
-import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.database.ContentObserver;
-import android.location.Criteria;
+import android.icu.impl.CalendarAstronomer;
+import android.icu.util.Calendar;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.Looper;
 import android.os.Message;
-import android.os.SystemClock;
-import android.os.UserHandle;
-import android.provider.Settings;
-import android.provider.Settings.Secure;
-import android.text.format.DateUtils;
-import android.text.format.Time;
+import android.util.ArrayMap;
 import android.util.Slog;
 
-import java.util.ArrayList;
-import java.util.Iterator;
+import com.android.internal.annotations.GuardedBy;
+import com.android.server.SystemService;
 
-import libcore.util.Objects;
+import java.util.Objects;
 
 /**
  * Figures out whether it's twilight time based on the user's location.
- *
+ * <p>
  * Used by the UI mode manager and other components to adjust night mode
  * effects based on sunrise and sunset.
  */
-public final class TwilightService extends SystemService {
-    static final String TAG = "TwilightService";
-    static final boolean DEBUG = false;
-    static final String ACTION_UPDATE_TWILIGHT_STATE =
-            "com.android.server.action.UPDATE_TWILIGHT_STATE";
+public final class TwilightService extends SystemService
+        implements AlarmManager.OnAlarmListener, Handler.Callback, LocationListener {
 
-    // The amount of time after or before sunrise over which to start adjusting
-    // twilight affected things.  We want the change to happen gradually so that
-    // it is below the threshold of perceptibility and so that the adjustment has
-    // maximum effect well after dusk.
-    private static final long TWILIGHT_ADJUSTMENT_TIME = DateUtils.HOUR_IN_MILLIS * 2;
+    private static final String TAG = "TwilightService";
+    private static final boolean DEBUG = false;
 
-    // Broadcast when twilight changes.
-    public static final String ACTION_TWILIGHT_CHANGED = "android.intent.action.TWILIGHT_CHANGED";
+    private static final int MSG_START_LISTENING = 1;
+    private static final int MSG_STOP_LISTENING = 2;
 
-    public static final String EXTRA_IS_NIGHT = "isNight";
-    public static final String EXTRA_AMOUNT = "amount";
+    @GuardedBy("mListeners")
+    private final ArrayMap<TwilightListener, Handler> mListeners = new ArrayMap<>();
 
-    // Amount of time the TwilightService will stay locked in an override state before switching
-    // back to auto.
-    private static final long RESET_TIME = DateUtils.HOUR_IN_MILLIS * 2;
-    private static final String EXTRA_RESET_USER = "user";
+    private final Handler mHandler;
 
-    private static final String ACTION_RESET_TWILIGHT_AUTO =
-            "com.android.server.action.RESET_TWILIGHT_AUTO";
+    private AlarmManager mAlarmManager;
+    private LocationManager mLocationManager;
 
-    final Object mLock = new Object();
-
-    AlarmManager mAlarmManager;
-    LocationManager mLocationManager;
-    LocationHandler mLocationHandler;
-
-    final ArrayList<TwilightListenerRecord> mListeners =
-            new ArrayList<TwilightListenerRecord>();
-
-    TwilightState mTwilightState;
-
-    private int mCurrentUser;
-    private boolean mLocked;
     private boolean mBootCompleted;
+    private boolean mHasListeners;
+
+    private BroadcastReceiver mTimeChangedReceiver;
+    private Location mLastLocation;
+
+    @GuardedBy("mListeners")
+    private TwilightState mLastTwilightState;
 
     public TwilightService(Context context) {
         super(context);
+        mHandler = new Handler(Looper.getMainLooper(), this);
     }
 
     @Override
     public void onStart() {
-        mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
-        mLocationManager = (LocationManager) getContext().getSystemService(
-                Context.LOCATION_SERVICE);
-        mLocationHandler = new LocationHandler();
-        mCurrentUser = ActivityManager.getCurrentUser();
+        publishLocalService(TwilightManager.class, new TwilightManager() {
+            @Override
+            public void registerListener(@NonNull TwilightListener listener,
+                    @NonNull Handler handler) {
+                synchronized (mListeners) {
+                    final boolean wasEmpty = mListeners.isEmpty();
+                    mListeners.put(listener, handler);
 
-        IntentFilter filter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
-        filter.addAction(Intent.ACTION_TIME_CHANGED);
-        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
-        filter.addAction(Intent.ACTION_USER_SWITCHED);
-        filter.addAction(ACTION_UPDATE_TWILIGHT_STATE);
-        getContext().registerReceiver(mReceiver, filter);
+                    if (wasEmpty && !mListeners.isEmpty()) {
+                        mHandler.sendEmptyMessage(MSG_START_LISTENING);
+                    }
+                }
+            }
 
-        publishLocalService(TwilightManager.class, mService);
+            @Override
+            public void unregisterListener(@NonNull TwilightListener listener) {
+                synchronized (mListeners) {
+                    final boolean wasEmpty = mListeners.isEmpty();
+                    mListeners.remove(listener);
+
+                    if (!wasEmpty && mListeners.isEmpty()) {
+                        mHandler.sendEmptyMessage(MSG_STOP_LISTENING);
+                    }
+                }
+            }
+
+            @Override
+            public TwilightState getLastTwilightState() {
+                synchronized (mListeners) {
+                    return mLastTwilightState;
+                }
+            }
+        });
     }
 
     @Override
     public void onBootPhase(int phase) {
         if (phase == PHASE_BOOT_COMPLETED) {
-            getContext().getContentResolver().registerContentObserver(
-                    Secure.getUriFor(Secure.TWILIGHT_MODE), false, mContentObserver, mCurrentUser);
-            mContentObserver.onChange(true);
+            final Context c = getContext();
+            mAlarmManager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
+            mLocationManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
+
             mBootCompleted = true;
-            sendBroadcast();
-        }
-    }
-
-    private void reregisterSettingObserver() {
-        final ContentResolver contentResolver = getContext().getContentResolver();
-        contentResolver.unregisterContentObserver(mContentObserver);
-        contentResolver.registerContentObserver(Secure.getUriFor(Secure.TWILIGHT_MODE), false,
-                mContentObserver, mCurrentUser);
-        mContentObserver.onChange(true);
-    }
-
-    private void setLockedState(TwilightState state) {
-        synchronized (mLock) {
-            // Make sure we aren't locked so we can set the state.
-            mLocked = false;
-            setTwilightState(state);
-            // Make sure we leave the state locked, so it cant be changed.
-            mLocked = true;
-            // TODO: Don't bother updating state when locked.
-        }
-    }
-
-    private void setTwilightState(TwilightState state) {
-        synchronized (mLock) {
-            if (mLocked) {
-                // State has been locked by secure setting, shouldn't be changed.
-                return;
-            }
-            if (!Objects.equal(mTwilightState, state)) {
-                if (DEBUG) {
-                    Slog.d(TAG, "Twilight state changed: " + state);
-                }
-
-                mTwilightState = state;
-
-                final int listenerLen = mListeners.size();
-                for (int i = 0; i < listenerLen; i++) {
-                    mListeners.get(i).postUpdate();
-                }
-            }
-        }
-        sendBroadcast();
-    }
-
-    private void sendBroadcast() {
-        synchronized (mLock) {
-            if (mTwilightState == null) {
-                return;
-            }
-            if (mBootCompleted) {
-                Intent intent = new Intent(ACTION_TWILIGHT_CHANGED);
-                intent.putExtra(EXTRA_IS_NIGHT, mTwilightState.isNight());
-                intent.putExtra(EXTRA_AMOUNT, mTwilightState.getAmount());
-                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
-                getContext().sendStickyBroadcastAsUser(intent, UserHandle.ALL);
+            if (mHasListeners) {
+                startListening();
             }
         }
     }
 
-    private void scheduleReset() {
-        long resetTime = System.currentTimeMillis() + RESET_TIME;
-        Intent resetIntent = new Intent(ACTION_RESET_TWILIGHT_AUTO);
-        resetIntent.putExtra(EXTRA_RESET_USER, mCurrentUser);
-        PendingIntent pendingIntent = PendingIntent.getBroadcast(
-                getContext(), 0, resetIntent, 0);
-        mAlarmManager.cancel(pendingIntent);
-        mAlarmManager.setExact(AlarmManager.RTC, resetTime, pendingIntent);
-    }
-
-    private static class TwilightListenerRecord implements Runnable {
-        private final TwilightListener mListener;
-        private final Handler mHandler;
-
-        public TwilightListenerRecord(TwilightListener listener, Handler handler) {
-            mListener = listener;
-            mHandler = handler;
-        }
-
-        public void postUpdate() {
-            mHandler.post(this);
-        }
-
-        @Override
-        public void run() {
-            mListener.onTwilightStateChanged();
-        }
-
-    }
-
-    private final TwilightManager mService = new TwilightManager() {
-        /**
-         * Gets the current twilight state.
-         *
-         * @return The current twilight state, or null if no information is available.
-         */
-        @Override
-        public TwilightState getCurrentState() {
-            synchronized (mLock) {
-                return mTwilightState;
-            }
-        }
-
-        /**
-         * Listens for twilight time.
-         *
-         * @param listener The listener.
-         */
-        @Override
-        public void registerListener(TwilightListener listener, Handler handler) {
-            synchronized (mLock) {
-                mListeners.add(new TwilightListenerRecord(listener, handler));
-
-                if (mListeners.size() == 1) {
-                    mLocationHandler.enableLocationUpdates();
-                }
-            }
-        }
-
-        @Override
-        public void unregisterListener(TwilightListener listener) {
-            synchronized (mLock) {
-                for (int i = 0; i < mListeners.size(); i++) {
-                    if (mListeners.get(i).mListener == listener) {
-                        mListeners.remove(i);
+    @Override
+    public boolean handleMessage(Message msg) {
+        switch (msg.what) {
+            case MSG_START_LISTENING:
+                if (!mHasListeners) {
+                    mHasListeners = true;
+                    if (mBootCompleted) {
+                        startListening();
                     }
                 }
-
-                if (mListeners.size() == 0) {
-                    mLocationHandler.disableLocationUpdates();
+                return true;
+            case MSG_STOP_LISTENING:
+                if (mHasListeners) {
+                    mHasListeners = false;
+                    if (mBootCompleted) {
+                        stopListening();
+                    }
                 }
-            }
+                return true;
         }
-    };
-
-    // The user has moved if the accuracy circles of the two locations don't overlap.
-    private static boolean hasMoved(Location from, Location to) {
-        if (to == null) {
-            return false;
-        }
-
-        if (from == null) {
-            return true;
-        }
-
-        // if new location is older than the current one, the device hasn't moved.
-        if (to.getElapsedRealtimeNanos() < from.getElapsedRealtimeNanos()) {
-            return false;
-        }
-
-        // Get the distance between the two points.
-        float distance = from.distanceTo(to);
-
-        // Get the total accuracy radius for both locations.
-        float totalAccuracy = from.getAccuracy() + to.getAccuracy();
-
-        // If the distance is greater than the combined accuracy of the two
-        // points then they can't overlap and hence the user has moved.
-        return distance >= totalAccuracy;
+        return false;
     }
 
-    private final class LocationHandler extends Handler {
-        private static final int MSG_ENABLE_LOCATION_UPDATES = 1;
-        private static final int MSG_GET_NEW_LOCATION_UPDATE = 2;
-        private static final int MSG_PROCESS_NEW_LOCATION = 3;
-        private static final int MSG_DO_TWILIGHT_UPDATE = 4;
-        private static final int MSG_DISABLE_LOCATION_UPDATES = 5;
+    private void startListening() {
+        Slog.d(TAG, "startListening");
 
-        private static final long LOCATION_UPDATE_MS = 24 * DateUtils.HOUR_IN_MILLIS;
-        private static final long MIN_LOCATION_UPDATE_MS = 30 * DateUtils.MINUTE_IN_MILLIS;
-        private static final float LOCATION_UPDATE_DISTANCE_METER = 1000 * 20;
-        private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MIN = 5000;
-        private static final long LOCATION_UPDATE_ENABLE_INTERVAL_MAX =
-                15 * DateUtils.MINUTE_IN_MILLIS;
-        private static final double FACTOR_GMT_OFFSET_LONGITUDE =
-                1000.0 * 360.0 / DateUtils.DAY_IN_MILLIS;
+        // Start listening for location updates (default: low power, max 1h, min 10m).
+        mLocationManager.requestLocationUpdates(
+                null /* default */, this, Looper.getMainLooper());
 
-        private boolean mPassiveListenerEnabled;
-        private boolean mNetworkListenerEnabled;
-        private boolean mDidFirstInit;
-        private long mLastNetworkRegisterTime = -MIN_LOCATION_UPDATE_MS;
-        private long mLastUpdateInterval;
-        private Location mLocation;
-        private final TwilightCalculator mTwilightCalculator = new TwilightCalculator();
-
-        public void processNewLocation(Location location) {
-            Message msg = obtainMessage(MSG_PROCESS_NEW_LOCATION, location);
-            sendMessage(msg);
+        // Request the device's location immediately if a previous location isn't available.
+        if (mLocationManager.getLastLocation() == null) {
+            if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
+                mLocationManager.requestSingleUpdate(
+                        LocationManager.NETWORK_PROVIDER, this, Looper.getMainLooper());
+            } else if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
+                mLocationManager.requestSingleUpdate(
+                        LocationManager.GPS_PROVIDER, this, Looper.getMainLooper());
+            }
         }
 
-        public void enableLocationUpdates() {
-            sendEmptyMessage(MSG_ENABLE_LOCATION_UPDATES);
-        }
-
-        public void disableLocationUpdates() {
-            sendEmptyMessage(MSG_DISABLE_LOCATION_UPDATES);
-        }
-
-        public void requestLocationUpdate() {
-            sendEmptyMessage(MSG_GET_NEW_LOCATION_UPDATE);
-        }
-
-        public void requestTwilightUpdate() {
-            sendEmptyMessage(MSG_DO_TWILIGHT_UPDATE);
-        }
-
-        @Override
-        public void handleMessage(Message msg) {
-            switch (msg.what) {
-                case MSG_PROCESS_NEW_LOCATION: {
-                    final Location location = (Location)msg.obj;
-                    final boolean hasMoved = hasMoved(mLocation, location);
-                    final boolean hasBetterAccuracy = mLocation == null
-                            || location.getAccuracy() < mLocation.getAccuracy();
-                    if (DEBUG) {
-                        Slog.d(TAG, "Processing new location: " + location
-                               + ", hasMoved=" + hasMoved
-                               + ", hasBetterAccuracy=" + hasBetterAccuracy);
-                    }
-                    if (hasMoved || hasBetterAccuracy) {
-                        setLocation(location);
-                    }
-                    break;
-                }
-
-                case MSG_GET_NEW_LOCATION_UPDATE:
-                    if (!mNetworkListenerEnabled) {
-                        // Don't do anything -- we are still trying to get a
-                        // location.
-                        return;
-                    }
-                    if ((mLastNetworkRegisterTime + MIN_LOCATION_UPDATE_MS) >=
-                            SystemClock.elapsedRealtime()) {
-                        // Don't do anything -- it hasn't been long enough
-                        // since we last requested an update.
-                        return;
-                    }
-
-                    // Unregister the current location monitor, so we can
-                    // register a new one for it to get an immediate update.
-                    mNetworkListenerEnabled = false;
-                    mLocationManager.removeUpdates(mEmptyLocationListener);
-
-                    // Fall through to re-register listener.
-                case MSG_ENABLE_LOCATION_UPDATES:
-                    // enable network provider to receive at least location updates for a given
-                    // distance.
-                    boolean networkLocationEnabled;
-                    try {
-                        networkLocationEnabled =
-                            mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
-                    } catch (Exception e) {
-                        // we may get IllegalArgumentException if network location provider
-                        // does not exist or is not yet installed.
-                        networkLocationEnabled = false;
-                    }
-                    if (!mNetworkListenerEnabled && networkLocationEnabled) {
-                        mNetworkListenerEnabled = true;
-                        mLastNetworkRegisterTime = SystemClock.elapsedRealtime();
-                        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
-                                LOCATION_UPDATE_MS, 0, mEmptyLocationListener);
-
-                        if (!mDidFirstInit) {
-                            mDidFirstInit = true;
-                            if (mLocation == null) {
-                                retrieveLocation();
-                            }
-                        }
-                    }
-
-                    // enable passive provider to receive updates from location fixes (gps
-                    // and network).
-                    boolean passiveLocationEnabled;
-                    try {
-                        passiveLocationEnabled =
-                            mLocationManager.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
-                    } catch (Exception e) {
-                        // we may get IllegalArgumentException if passive location provider
-                        // does not exist or is not yet installed.
-                        passiveLocationEnabled = false;
-                    }
-
-                    if (!mPassiveListenerEnabled && passiveLocationEnabled) {
-                        mPassiveListenerEnabled = true;
-                        mLocationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,
-                                0, LOCATION_UPDATE_DISTANCE_METER , mLocationListener);
-                    }
-
-                    if (!(mNetworkListenerEnabled && mPassiveListenerEnabled)) {
-                        mLastUpdateInterval *= 1.5;
-                        if (mLastUpdateInterval == 0) {
-                            mLastUpdateInterval = LOCATION_UPDATE_ENABLE_INTERVAL_MIN;
-                        } else if (mLastUpdateInterval > LOCATION_UPDATE_ENABLE_INTERVAL_MAX) {
-                            mLastUpdateInterval = LOCATION_UPDATE_ENABLE_INTERVAL_MAX;
-                        }
-                        sendEmptyMessageDelayed(MSG_ENABLE_LOCATION_UPDATES, mLastUpdateInterval);
-                    }
-                    break;
-
-                case MSG_DISABLE_LOCATION_UPDATES:
-                    mLocationManager.removeUpdates(mLocationListener);
-                    removeMessages(MSG_ENABLE_LOCATION_UPDATES);
-                    break;
-
-                case MSG_DO_TWILIGHT_UPDATE:
+        // Update whenever the system clock is changed.
+        if (mTimeChangedReceiver == null) {
+            mTimeChangedReceiver = new BroadcastReceiver() {
+                @Override
+                public void onReceive(Context context, Intent intent) {
+                    Slog.d(TAG, "onReceive: " + intent);
                     updateTwilightState();
-                    break;
+                }
+            };
+
+            final IntentFilter intentFilter = new IntentFilter(Intent.ACTION_TIME_CHANGED);
+            intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
+            getContext().registerReceiver(mTimeChangedReceiver, intentFilter);
+        }
+
+        // Force an update now that we have listeners registered.
+        updateTwilightState();
+    }
+
+    private void stopListening() {
+        Slog.d(TAG, "stopListening");
+
+        if (mTimeChangedReceiver != null) {
+            getContext().unregisterReceiver(mTimeChangedReceiver);
+            mTimeChangedReceiver = null;
+        }
+
+        if (mLastTwilightState != null) {
+            mAlarmManager.cancel(this);
+        }
+
+        mLocationManager.removeUpdates(this);
+        mLastLocation = null;
+    }
+
+    private void updateTwilightState() {
+        // Calculate the twilight state based on the current time and location.
+        final long currentTimeMillis = System.currentTimeMillis();
+        final Location location = mLastLocation != null ? mLastLocation
+                : mLocationManager.getLastLocation();
+        final TwilightState state = calculateTwilightState(location, currentTimeMillis);
+        if (DEBUG) {
+            Slog.d(TAG, "updateTwilightState: " + state);
+        }
+
+        // Notify listeners if the state has changed.
+        synchronized (mListeners) {
+            if (!Objects.equals(mLastTwilightState, state)) {
+                mLastTwilightState = state;
+
+                for (int i = mListeners.size() - 1; i >= 0; --i) {
+                    final TwilightListener listener = mListeners.keyAt(i);
+                    final Handler handler = mListeners.valueAt(i);
+                    handler.post(new Runnable() {
+                        @Override
+                        public void run() {
+                            listener.onTwilightStateChanged(state);
+                        }
+                    });
+                }
             }
         }
 
-        private void retrieveLocation() {
-            Location location = null;
-            final Iterator<String> providers =
-                    mLocationManager.getProviders(new Criteria(), true).iterator();
-            while (providers.hasNext()) {
-                final Location lastKnownLocation =
-                        mLocationManager.getLastKnownLocation(providers.next());
-                // pick the most recent location
-                if (location == null || (lastKnownLocation != null &&
-                        location.getElapsedRealtimeNanos() <
-                        lastKnownLocation.getElapsedRealtimeNanos())) {
-                    location = lastKnownLocation;
-                }
-            }
-
-            // In the case there is no location available (e.g. GPS fix or network location
-            // is not available yet), the longitude of the location is estimated using the timezone,
-            // latitude and accuracy are set to get a good average.
-            if (location == null) {
-                Time currentTime = new Time();
-                currentTime.set(System.currentTimeMillis());
-                double lngOffset = FACTOR_GMT_OFFSET_LONGITUDE *
-                        (currentTime.gmtoff - (currentTime.isDst > 0 ? 3600 : 0));
-                location = new Location("fake");
-                location.setLongitude(lngOffset);
-                location.setLatitude(0);
-                location.setAccuracy(417000.0f);
-                location.setTime(System.currentTimeMillis());
-                location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
-
-                if (DEBUG) {
-                    Slog.d(TAG, "Estimated location from timezone: " + location);
-                }
-            }
-
-            setLocation(location);
+        // Schedule an alarm to update the state at the next sunrise or sunset.
+        if (state != null) {
+            final long triggerAtMillis = state.isNight()
+                    ? state.sunriseTimeMillis() : state.sunsetTimeMillis();
+            mAlarmManager.setExact(AlarmManager.RTC, triggerAtMillis, TAG, this, mHandler);
         }
+    }
 
-        private void setLocation(Location location) {
-            mLocation = location;
+    @Override
+    public void onAlarm() {
+        Slog.d(TAG, "onAlarm");
+        updateTwilightState();
+    }
+
+    @Override
+    public void onLocationChanged(Location location) {
+        if (location != null) {
+            Slog.d(TAG, "onLocationChanged:"
+                    + " provider=" + location.getProvider()
+                    + " accuracy=" + location.getAccuracy()
+                    + " time=" + location.getTime());
+            mLastLocation = location;
             updateTwilightState();
         }
-
-        private void updateTwilightState() {
-            if (mLocation == null) {
-                setTwilightState(null);
-                return;
-            }
-
-            final long now = System.currentTimeMillis();
-
-            // calculate today's twilight
-            mTwilightCalculator.calculateTwilight(now,
-                    mLocation.getLatitude(), mLocation.getLongitude());
-            final boolean isNight = (mTwilightCalculator.mState == TwilightCalculator.NIGHT);
-            final long todaySunrise = mTwilightCalculator.mSunrise;
-            final long todaySunset = mTwilightCalculator.mSunset;
-
-            // calculate tomorrow's twilight
-            mTwilightCalculator.calculateTwilight(now + DateUtils.DAY_IN_MILLIS,
-                    mLocation.getLatitude(), mLocation.getLongitude());
-            final long tomorrowSunrise = mTwilightCalculator.mSunrise;
-
-            float amount = 0;
-            if (isNight) {
-                if (todaySunrise == -1 || todaySunset == -1) {
-                    amount = 1;
-                } else if (now > todaySunset) {
-                    amount = Math.min(1, (now - todaySunset) / (float) TWILIGHT_ADJUSTMENT_TIME);
-                } else {
-                    amount = Math.max(0, 1
-                            - (todaySunrise - now) / (float) TWILIGHT_ADJUSTMENT_TIME);
-                }
-            }
-            // set twilight state
-            TwilightState state = new TwilightState(isNight, amount);
-            if (DEBUG) {
-                Slog.d(TAG, "Updating twilight state: " + state);
-            }
-            setTwilightState(state);
-
-            // schedule next update
-            long nextUpdate = 0;
-            if (todaySunrise == -1 || todaySunset == -1) {
-                // In the case the day or night never ends the update is scheduled 12 hours later.
-                nextUpdate = now + 12 * DateUtils.HOUR_IN_MILLIS;
-            } else {
-                // add some extra time to be on the safe side.
-                nextUpdate += DateUtils.MINUTE_IN_MILLIS;
-
-                if (amount == 1 || amount == 0) {
-                    if (now > todaySunset) {
-                        nextUpdate += tomorrowSunrise;
-                    } else if (now > todaySunrise) {
-                        nextUpdate += todaySunset;
-                    } else {
-                        nextUpdate += todaySunrise;
-                    }
-                } else {
-                    // This is the update rate while transitioning.
-                    // Leave at 10 min for now (one from above).
-                    nextUpdate += 9 * DateUtils.MINUTE_IN_MILLIS;
-                }
-            }
-
-            if (DEBUG) {
-                Slog.d(TAG, "Next update in " + (nextUpdate - now) + " ms");
-            }
-
-            Intent updateIntent = new Intent(ACTION_UPDATE_TWILIGHT_STATE);
-            PendingIntent pendingIntent = PendingIntent.getBroadcast(
-                    getContext(), 0, updateIntent, 0);
-            mAlarmManager.cancel(pendingIntent);
-            mAlarmManager.setExact(AlarmManager.RTC, nextUpdate, pendingIntent);
-        }
     }
 
-    private final ContentObserver mContentObserver = new ContentObserver(new Handler()) {
-        @Override
-        public void onChange(boolean selfChange) {
-            super.onChange(selfChange);
-            int value = Secure.getIntForUser(getContext().getContentResolver(),
-                    Secure.TWILIGHT_MODE, Secure.TWILIGHT_MODE_LOCKED_OFF, mCurrentUser);
-            if (value == Secure.TWILIGHT_MODE_LOCKED_OFF) {
-                setLockedState(new TwilightState(false, 0));
-            } else if (value == Secure.TWILIGHT_MODE_LOCKED_ON) {
-                setLockedState(new TwilightState(true, 1));
-            } else if (value == Secure.TWILIGHT_MODE_AUTO_OVERRIDE_OFF) {
-                setLockedState(new TwilightState(false, 0));
-                scheduleReset();
-            } else if (value == Secure.TWILIGHT_MODE_AUTO_OVERRIDE_ON) {
-                setLockedState(new TwilightState(true, 1));
-                scheduleReset();
-            } else {
-                mLocked = false;
-                mLocationHandler.requestTwilightUpdate();
-            }
-        }
-    };
+    @Override
+    public void onStatusChanged(String provider, int status, Bundle extras) {
+    }
 
-    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
-                mCurrentUser = ActivityManager.getCurrentUser();
-                reregisterSettingObserver();
-                return;
-            }
-            if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(intent.getAction())
-                    && !intent.getBooleanExtra("state", false)) {
-                // Airplane mode is now off!
-                mLocationHandler.requestLocationUpdate();
-                return;
-            }
+    @Override
+    public void onProviderEnabled(String provider) {
+    }
 
-            if (ACTION_RESET_TWILIGHT_AUTO.equals(intent.getAction())) {
-                int user = intent.getIntExtra(EXTRA_RESET_USER, 0);
-                Settings.Secure.putIntForUser(getContext().getContentResolver(),
-                        Secure.TWILIGHT_MODE, Secure.TWILIGHT_MODE_AUTO, user);
-                return;
-            }
-            // Time zone has changed or alarm expired.
-            mLocationHandler.requestTwilightUpdate();
-        }
-    };
+    @Override
+    public void onProviderDisabled(String provider) {
+    }
 
-    // A LocationListener to initialize the network location provider. The location updates
-    // are handled through the passive location provider.
-    private final LocationListener mEmptyLocationListener =  new LocationListener() {
-        public void onLocationChanged(Location location) {
+    /**
+     * Calculates the twilight state for a specific location and time.
+     *
+     * @param location the location to use
+     * @param timeMillis the reference time to use
+     * @return the calculated {@link TwilightState}, or {@code null} if location is {@code null}
+     */
+    private static TwilightState calculateTwilightState(Location location, long timeMillis) {
+        if (location == null) {
+            return null;
         }
 
-        public void onProviderDisabled(String provider) {
+        final CalendarAstronomer ca = new CalendarAstronomer(
+                location.getLongitude(), location.getLatitude());
+
+        final Calendar noon = Calendar.getInstance();
+        noon.setTimeInMillis(timeMillis);
+        noon.set(Calendar.HOUR_OF_DAY, 12);
+        noon.set(Calendar.MINUTE, 0);
+        noon.set(Calendar.SECOND, 0);
+        noon.set(Calendar.MILLISECOND, 0);
+        ca.setTime(noon.getTimeInMillis());
+
+        long sunriseTimeMillis = ca.getSunRiseSet(true /* rise */);
+        long sunsetTimeMillis = ca.getSunRiseSet(false /* rise */);
+
+        if (sunsetTimeMillis < timeMillis) {
+            noon.add(Calendar.DATE, 1);
+            ca.setTime(noon.getTimeInMillis());
+            sunriseTimeMillis = ca.getSunRiseSet(true /* rise */);
+        } else if (sunriseTimeMillis > timeMillis) {
+            noon.add(Calendar.DATE, -1);
+            ca.setTime(noon.getTimeInMillis());
+            sunsetTimeMillis = ca.getSunRiseSet(false /* rise */);
         }
 
-        public void onProviderEnabled(String provider) {
-        }
-
-        public void onStatusChanged(String provider, int status, Bundle extras) {
-        }
-    };
-
-    private final LocationListener mLocationListener = new LocationListener() {
-        public void onLocationChanged(Location location) {
-            mLocationHandler.processNewLocation(location);
-        }
-
-        public void onProviderDisabled(String provider) {
-        }
-
-        public void onProviderEnabled(String provider) {
-        }
-
-        public void onStatusChanged(String provider, int status, Bundle extras) {
-        }
-    };
+        return new TwilightState(sunriseTimeMillis, sunsetTimeMillis);
+    }
 }
diff --git a/services/core/java/com/android/server/twilight/TwilightState.java b/services/core/java/com/android/server/twilight/TwilightState.java
index 81abc13..a12965d 100644
--- a/services/core/java/com/android/server/twilight/TwilightState.java
+++ b/services/core/java/com/android/server/twilight/TwilightState.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,58 +16,89 @@
 
 package com.android.server.twilight;
 
-import java.text.DateFormat;
-import java.util.Date;
+import android.text.format.DateFormat;
+
+import java.util.Calendar;
 
 /**
- * Describes whether it is day or night.
- * This object is immutable.
+ * The twilight state, consisting of the sunrise and sunset times (in millis) for the current
+ * period.
+ * <p/>
+ * Note: This object is immutable.
  */
-public class TwilightState {
-    private final boolean mIsNight;
-    private final float mAmount;
+public final class TwilightState {
 
-    TwilightState(boolean isNight, float amount) {
-        mIsNight = isNight;
-        mAmount = amount;
+    private final long mSunriseTimeMillis;
+    private final long mSunsetTimeMillis;
+
+    TwilightState(long sunriseTimeMillis, long sunsetTimeMillis) {
+        mSunriseTimeMillis = sunriseTimeMillis;
+        mSunsetTimeMillis = sunsetTimeMillis;
     }
 
     /**
-     * Returns true if it is currently night time.
+     * Returns the time (in UTC milliseconds from epoch) of the upcoming or previous sunrise if
+     * it's night or day respectively.
+     */
+    public long sunriseTimeMillis() {
+        return mSunriseTimeMillis;
+    }
+
+    /**
+     * Returns a new {@link Calendar} instance initialized to {@link #sunriseTimeMillis()}.
+     */
+    public Calendar sunrise() {
+        final Calendar sunrise = Calendar.getInstance();
+        sunrise.setTimeInMillis(mSunriseTimeMillis);
+        return sunrise;
+    }
+
+    /**
+     * Returns the time (in UTC milliseconds from epoch) of the upcoming or previous sunset if
+     * it's day or night respectively.
+     */
+    public long sunsetTimeMillis() {
+        return mSunsetTimeMillis;
+    }
+
+    /**
+     * Returns a new {@link Calendar} instance initialized to {@link #sunsetTimeMillis()}.
+     */
+    public Calendar sunset() {
+        final Calendar sunset = Calendar.getInstance();
+        sunset.setTimeInMillis(mSunsetTimeMillis);
+        return sunset;
+    }
+
+    /**
+     * Returns {@code true} if it is currently night time.
      */
     public boolean isNight() {
-        return mIsNight;
-    }
-
-    /**
-     * For twilight affects that change gradually over time, this is the amount they
-     * should currently be in effect.
-     */
-    public float getAmount() {
-        return mAmount;
+        final long now = System.currentTimeMillis();
+        return now >= mSunsetTimeMillis && now < mSunriseTimeMillis;
     }
 
     @Override
     public boolean equals(Object o) {
-        return o instanceof TwilightState && equals((TwilightState)o);
+        return o instanceof TwilightState && equals((TwilightState) o);
     }
 
     public boolean equals(TwilightState other) {
         return other != null
-                && mIsNight == other.mIsNight
-                && mAmount == other.mAmount;
+                && mSunriseTimeMillis == other.mSunriseTimeMillis
+                && mSunsetTimeMillis == other.mSunsetTimeMillis;
     }
 
     @Override
     public int hashCode() {
-        return 0; // don't care
+        return Long.hashCode(mSunriseTimeMillis) ^ Long.hashCode(mSunsetTimeMillis);
     }
 
     @Override
     public String toString() {
-        DateFormat f = DateFormat.getDateTimeInstance();
-        return "{TwilightState: isNight=" + mIsNight
-                + ", mAmount=" + mAmount
-                + "}";
+        return "TwilightState {"
+                + " sunrise=" + DateFormat.format("MM-dd HH:mm", mSunriseTimeMillis)
+                + " sunset="+ DateFormat.format("MM-dd HH:mm", mSunsetTimeMillis)
+                + " }";
     }
 }
diff --git a/services/core/java/com/android/server/vr/EnabledComponentsObserver.java b/services/core/java/com/android/server/vr/EnabledComponentsObserver.java
index 77e8b1f..7126cb5 100644
--- a/services/core/java/com/android/server/vr/EnabledComponentsObserver.java
+++ b/services/core/java/com/android/server/vr/EnabledComponentsObserver.java
@@ -215,7 +215,11 @@
      */
     public ArraySet<ComponentName> getInstalled(int userId) {
         synchronized (mLock) {
-            return mInstalledSet.get(userId);
+            ArraySet<ComponentName> ret = mInstalledSet.get(userId);
+            if (ret == null) {
+                return new ArraySet<ComponentName>();
+            }
+            return ret;
         }
     }
 
@@ -227,7 +231,12 @@
      */
     public ArraySet<ComponentName> getEnabled(int userId) {
         synchronized (mLock) {
-            return mEnabledSet.get(userId);
+            ArraySet<ComponentName> ret = mEnabledSet.get(userId);
+            if (ret == null) {
+                return new ArraySet<ComponentName>();
+            }
+            return ret;
+
         }
     }
 
@@ -246,7 +255,9 @@
         Intent queryIntent = new Intent(serviceName);
         List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
                 queryIntent,
-                PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
+                PackageManager.GET_SERVICES | PackageManager.GET_META_DATA |
+                                    PackageManager.MATCH_DIRECT_BOOT_AWARE |
+                                    PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
                 userId);
         if (installedServices != null) {
             for (int i = 0, count = installedServices.size(); i < count; i++) {
diff --git a/services/core/java/com/android/server/vr/VrManagerService.java b/services/core/java/com/android/server/vr/VrManagerService.java
index 5fefd4c..fdadc8d 100644
--- a/services/core/java/com/android/server/vr/VrManagerService.java
+++ b/services/core/java/com/android/server/vr/VrManagerService.java
@@ -46,6 +46,7 @@
 import android.service.vr.IVrManager;
 import android.service.vr.IVrStateCallbacks;
 import android.service.vr.VrListenerService;
+import android.text.TextUtils;
 import android.util.ArrayMap;
 import android.util.ArraySet;
 import android.util.Slog;
@@ -218,6 +219,7 @@
                     String packageName = mNotificationAccessPackageToUserId.keyAt(i);
                     revokeNotificationListenerAccess(packageName, grantUserId);
                     revokeNotificationPolicyAccess(packageName);
+                    revokeCoarseLocationPermissionIfNeeded(packageName, grantUserId);
                     mNotificationAccessPackageToUserId.removeAt(i);
                 }
             }
@@ -226,6 +228,7 @@
                 if (!packageNames.contains(pkg)) {
                     revokeNotificationListenerAccess(pkg, currentUserId);
                     revokeNotificationPolicyAccess(pkg);
+                    revokeCoarseLocationPermissionIfNeeded(pkg, currentUserId);
                     mNotificationAccessPackageToUserId.remove(pkg);
                 }
             }
@@ -233,6 +236,7 @@
                 if (!allowed.contains(pkg)) {
                     grantNotificationPolicyAccess(pkg);
                     grantNotificationListenerAccess(pkg, currentUserId);
+                    grantCoarseLocationPermissionIfNeeded(pkg, currentUserId);
                     mNotificationAccessPackageToUserId.put(pkg, currentUserId);
                 }
             }
@@ -403,107 +407,6 @@
 
         publishLocalService(VrManagerInternal.class, new LocalService());
         publishBinderService(VR_MANAGER_BINDER_SERVICE, mVrManager.asBinder());
-
-        // If there are no VR packages installed on the device, then disable VR
-        // components, otherwise, enable them.
-        setEnabledStatusOfVrComponents();
-    }
-
-    private void setEnabledStatusOfVrComponents() {
-        ArraySet<ComponentName> vrComponents = SystemConfig.getInstance().getDefaultVrComponents();
-        if (vrComponents == null) {
-           return;
-        }
-
-        // We only want to enable VR components if there is a VR package installed on the device.
-        // The VR components themselves do not quality as a VR package, so exclude them.
-        ArraySet<String> vrComponentPackageNames = new ArraySet<>();
-        for (ComponentName componentName : vrComponents) {
-            vrComponentPackageNames.add(componentName.getPackageName());
-        }
-
-        // Check to see if there are any packages on the device, other than the VR component
-        // packages.
-        PackageManager pm = mContext.getPackageManager();
-        List<PackageInfo> packageInfos = pm.getInstalledPackages(
-                PackageManager.GET_CONFIGURATIONS);
-        boolean vrModeIsUsed = false;
-        for (PackageInfo packageInfo : packageInfos) {
-            if (packageInfo != null && packageInfo.packageName != null &&
-                    pm.getApplicationEnabledSetting(packageInfo.packageName) ==
-                            PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
-                vrModeIsUsed = enableVrComponentsIfVrModeUsed(pm, packageInfo,
-                        vrComponentPackageNames, vrComponents);
-                if (vrModeIsUsed) {
-                    break;
-                }
-            }
-        }
-
-        if (!vrModeIsUsed) {
-            Slog.i(TAG, "No VR packages found, disabling VR components");
-            setVrComponentsEnabledOrDisabled(vrComponents, false);
-
-            // Register to receive an intent when a new package is installed, in case that package
-            // requires VR components.
-            IntentFilter intentFilter = new IntentFilter();
-            intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
-            intentFilter.addDataScheme("package");
-            mContext.registerReceiver(new BroadcastReceiver() {
-                @Override
-                public void onReceive(Context context, Intent intent) {
-                    PackageManager pm = context.getPackageManager();
-                    final String packageName = intent.getData().getSchemeSpecificPart();
-                    if (packageName != null) {
-                        try {
-                            PackageInfo packageInfo = pm.getPackageInfo(packageName,
-                                    PackageManager.GET_CONFIGURATIONS);
-                            enableVrComponentsIfVrModeUsed(pm, packageInfo,
-                                    vrComponentPackageNames, vrComponents);
-                        } catch (NameNotFoundException e) {
-                        }
-                    }
-                };
-            }, intentFilter);
-        }
-    }
-
-    private void setVrComponentsEnabledOrDisabled(ArraySet<ComponentName> vrComponents,
-            boolean enabled) {
-        int state = enabled ?
-                PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
-                PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
-        PackageManager pm = mContext.getPackageManager();
-        for (ComponentName componentName : vrComponents) {
-            try {
-                // Note that we must first check for the existance of the package before trying
-                // to set its enabled state.  This is to prevent PackageManager from throwing
-                // an excepton if the package is not found (not just a NameNotFoundException
-                // exception).
-                PackageInfo packageInfo = pm.getPackageInfo(componentName.getPackageName(),
-                        PackageManager.GET_CONFIGURATIONS);
-                pm.setApplicationEnabledSetting(componentName.getPackageName(), state , 0);
-            } catch (NameNotFoundException e) {
-            }
-        }
-    }
-
-    private boolean enableVrComponentsIfVrModeUsed(PackageManager pm, PackageInfo packageInfo,
-            ArraySet<String> vrComponentPackageNames, ArraySet<ComponentName> vrComponents) {
-        boolean isVrComponent = vrComponents != null &&
-                vrComponentPackageNames.contains(packageInfo.packageName);
-        if (packageInfo != null && packageInfo.reqFeatures != null && !isVrComponent) {
-            for (FeatureInfo featureInfo : packageInfo.reqFeatures) {
-                if (featureInfo.name != null &&
-                    (featureInfo.name.equals(PackageManager.FEATURE_VR_MODE) ||
-                     featureInfo.name.equals(PackageManager.FEATURE_VR_MODE_HIGH_PERFORMANCE))) {
-                    Slog.i(TAG, "VR package found, enabling VR components");
-                    setVrComponentsEnabledOrDisabled(vrComponents, true);
-                    return true;
-                }
-            }
-        }
-        return false;
     }
 
     @Override
@@ -744,7 +647,7 @@
 
         for (String c : current) {
             ComponentName component = ComponentName.unflattenFromString(c);
-            if (component.getPackageName().equals(pkg)) {
+            if (component != null && component.getPackageName().equals(pkg)) {
                 toRemove.add(c);
             }
         }
@@ -757,6 +660,22 @@
                 flatSettings, userId);
     }
 
+    private void grantCoarseLocationPermissionIfNeeded(String pkg, int userId) {
+        // Don't clobber the user if permission set in current state explicitly
+        if (!isPermissionUserUpdated(Manifest.permission.ACCESS_COARSE_LOCATION, pkg, userId)) {
+            mContext.getPackageManager().grantRuntimePermission(pkg,
+                    Manifest.permission.ACCESS_COARSE_LOCATION, new UserHandle(userId));
+        }
+    }
+
+    private void revokeCoarseLocationPermissionIfNeeded(String pkg, int userId) {
+        // Don't clobber the user if permission set in current state explicitly
+        if (!isPermissionUserUpdated(Manifest.permission.ACCESS_COARSE_LOCATION, pkg, userId)) {
+            mContext.getPackageManager().revokeRuntimePermission(pkg,
+                    Manifest.permission.ACCESS_COARSE_LOCATION, new UserHandle(userId));
+        }
+    }
+
     private boolean isPermissionUserUpdated(String permission, String pkg, int userId) {
         final int flags = mContext.getPackageManager().getPermissionFlags(
                 permission, pkg, new UserHandle(userId));
@@ -772,7 +691,9 @@
         if (flat != null) {
             String[] allowed = flat.split(":");
             for (String s : allowed) {
-                current.add(s);
+                if (!TextUtils.isEmpty(s)) {
+                    current.add(s);
+                }
             }
         }
         return current;
diff --git a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
index 8137c4e..5f6800a 100644
--- a/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
+++ b/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java
@@ -85,9 +85,11 @@
 
 import com.android.internal.R;
 import com.android.internal.content.PackageMonitor;
+import com.android.internal.os.BackgroundThread;
 import com.android.internal.util.FastXmlSerializer;
 import com.android.internal.util.JournaledFile;
 import com.android.server.EventLogTags;
+import com.android.server.FgThread;
 import com.android.server.SystemService;
 
 import libcore.io.IoUtils;
@@ -166,7 +168,7 @@
      * Observes the wallpaper for changes and notifies all IWallpaperServiceCallbacks
      * that the wallpaper has changed. The CREATE is triggered when there is no
      * wallpaper set and is created for the first time. The CLOSE_WRITE is triggered
-     * everytime the wallpaper is changed.
+     * every time the wallpaper is changed.
      */
     private class WallpaperObserver extends FileObserver {
 
@@ -175,7 +177,6 @@
         final File mWallpaperDir;
         final File mWallpaperFile;
         final File mWallpaperLockFile;
-        final File mWallpaperInfoFile;
 
         public WallpaperObserver(WallpaperData wallpaper) {
             super(getWallpaperDir(wallpaper.userId).getAbsolutePath(),
@@ -185,7 +186,6 @@
             mWallpaper = wallpaper;
             mWallpaperFile = new File(mWallpaperDir, WALLPAPER);
             mWallpaperLockFile = new File(mWallpaperDir, WALLPAPER_LOCK_ORIG);
-            mWallpaperInfoFile = new File(mWallpaperDir, WALLPAPER_INFO);
         }
 
         private WallpaperData dataForEvent(boolean sysChanged, boolean lockChanged) {
@@ -253,6 +253,7 @@
                             if (DEBUG) {
                                 Slog.v(TAG, "Wallpaper written; generating crop");
                             }
+                            SELinux.restorecon(changedFile);
                             if (moved) {
                                 // This is a restore, so generate the crop using any just-restored new
                                 // crop guidelines, making sure to preserve our local dimension hints.
@@ -260,7 +261,6 @@
                                 if (DEBUG) {
                                     Slog.v(TAG, "moved-to, therefore restore; reloading metadata");
                                 }
-                                SELinux.restorecon(changedFile);
                                 loadSettingsLocked(wallpaper.userId, true);
                             }
                             generateCrop(wallpaper);
@@ -353,9 +353,17 @@
                         (cropHint.right > options.outWidth ? options.outWidth - cropHint.right : 0),
                         (cropHint.bottom > options.outHeight ? options.outHeight - cropHint.bottom : 0));
 
+                // If the crop hint was larger than the image we just overshot. Patch things up.
+                if (cropHint.left < 0) {
+                    cropHint.left = 0;
+                }
+                if (cropHint.top < 0) {
+                    cropHint.top = 0;
+                }
+
                 // Don't bother cropping if what we're left with is identity
-                needCrop = (options.outHeight >= cropHint.height()
-                        && options.outWidth >= cropHint.width());
+                needCrop = (options.outHeight > cropHint.height()
+                        || options.outWidth > cropHint.width());
             }
 
             // scale if the crop height winds up not matching the recommended metrics
@@ -488,6 +496,7 @@
     final SparseArray<WallpaperData> mWallpaperMap = new SparseArray<WallpaperData>();
     final SparseArray<WallpaperData> mLockWallpaperMap = new SparseArray<WallpaperData>();
 
+    final SparseArray<Boolean> mUserRestorecon = new SparseArray<Boolean>();
     int mCurrentUserId;
 
     static class WallpaperData {
@@ -581,6 +590,11 @@
 
     class WallpaperConnection extends IWallpaperConnection.Stub
             implements ServiceConnection {
+
+        /** Time in milliseconds until we expect the wallpaper to reconnect (unless we're in the
+         *  middle of an update). If exceeded, the wallpaper gets reset to the system default. */
+        private static final long WALLPAPER_RECONNECT_TIMEOUT_MS = 5000;
+
         final WallpaperInfo mInfo;
         final Binder mToken = new Binder();
         IWallpaperService mService;
@@ -591,6 +605,18 @@
         boolean mDimensionsChanged = false;
         boolean mPaddingChanged = false;
 
+        private Runnable mResetRunnable = () -> {
+            synchronized (mLock) {
+                if (!mWallpaper.wallpaperUpdating
+                        && mWallpaper.userId == mCurrentUserId) {
+                    Slog.w(TAG, "Wallpaper reconnect timed out, "
+                            + "reverting to built-in wallpaper!");
+                    clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId,
+                            null);
+                }
+            }
+        };
+
         public WallpaperConnection(WallpaperInfo info, WallpaperData wallpaper) {
             mInfo = info;
             mWallpaper = wallpaper;
@@ -607,6 +633,7 @@
                     // locking there and anyway we always need to be able to
                     // recover if there is something wrong.
                     saveSettingsLocked(mWallpaper.userId);
+                    FgThread.getHandler().removeCallbacks(mResetRunnable);
                 }
             }
         }
@@ -633,6 +660,12 @@
                             clearWallpaperLocked(true, FLAG_SYSTEM, mWallpaper.userId, null);
                         } else {
                             mWallpaper.lastDiedTime = SystemClock.uptimeMillis();
+
+                            // If we didn't reset it right away, do so after we couldn't connect to
+                            // it for an extended amount of time to avoid having a black wallpaper.
+                            FgThread.getHandler().removeCallbacks(mResetRunnable);
+                            FgThread.getHandler().postDelayed(mResetRunnable,
+                                    WALLPAPER_RECONNECT_TIMEOUT_MS);
                         }
                         final String flattened = name.flattenToString();
                         EventLog.writeEvent(EventLogTags.WP_WALLPAPER_CRASHED,
@@ -744,6 +777,10 @@
                     if (wallpaper.wallpaperComponent != null
                             && wallpaper.wallpaperComponent.getPackageName().equals(packageName)) {
                         wallpaper.wallpaperUpdating = true;
+                        if (wallpaper.connection != null) {
+                            FgThread.getHandler().removeCallbacks(
+                                    wallpaper.connection.mResetRunnable);
+                        }
                     }
                 }
             }
@@ -910,10 +947,9 @@
                         public void onForegroundProfileSwitch(int newProfileId) {
                             // Ignore.
                         }
-                    });
+                    }, TAG);
         } catch (RemoteException e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+            e.rethrowAsRuntimeException();
         }
     }
 
@@ -944,10 +980,34 @@
         mLockWallpaperMap.remove(userId);
     }
 
-    void onUnlockUser(int userId) {
+    void onUnlockUser(final int userId) {
         synchronized (mLock) {
-            if (mCurrentUserId == userId && mWaitingForUnlock) {
-                switchUser(userId, null);
+            if (mCurrentUserId == userId) {
+                if (mWaitingForUnlock) {
+                    // If we're switching users, now is when we transition the wallpaper
+                    switchUser(userId, null);
+                }
+
+                // Make sure that the SELinux labeling of all the relevant files is correct.
+                // This corrects for mislabeling bugs that might have arisen from move-to
+                // operations involving the wallpaper files.  This isn't timing-critical,
+                // so we do it in the background to avoid holding up the user unlock operation.
+                if (mUserRestorecon.get(userId) != Boolean.TRUE) {
+                    mUserRestorecon.put(userId, Boolean.TRUE);
+                    Runnable relabeler = new Runnable() {
+                        @Override
+                        public void run() {
+                            final File wallpaperDir = getWallpaperDir(userId);
+                            for (String filename : sPerUserFiles) {
+                                File f = new File(wallpaperDir, filename);
+                                if (f.exists()) {
+                                    SELinux.restorecon(f);
+                                }
+                            }
+                        }
+                    };
+                    BackgroundThread.getHandler().post(relabeler);
+                }
             }
         }
     }
@@ -1283,8 +1343,9 @@
     }
 
     @Override
-    public WallpaperInfo getWallpaperInfo() {
-        int userId = UserHandle.getCallingUserId();
+    public WallpaperInfo getWallpaperInfo(int userId) {
+        userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
+                Binder.getCallingUid(), userId, false, true, "getWallpaperIdForUser", null);
         synchronized (mLock) {
             WallpaperData wallpaper = mWallpaperMap.get(userId);
             if (wallpaper != null && wallpaper.connection != null) {
@@ -1326,7 +1387,9 @@
     @Override
     public ParcelFileDescriptor setWallpaper(String name, String callingPackage,
             Rect cropHint, boolean allowBackup, Bundle extras, int which,
-            IWallpaperManagerCallback completion) {
+            IWallpaperManagerCallback completion, int userId) {
+        userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
+                false /* all */, true /* full */, "changing wallpaper", null /* pkg */);
         checkPermission(android.Manifest.permission.SET_WALLPAPER);
 
         if ((which & (FLAG_LOCK|FLAG_SYSTEM)) == 0) {
@@ -1350,8 +1413,6 @@
             }
         }
 
-        final int userId = UserHandle.getCallingUserId();
-
         synchronized (mLock) {
             if (DEBUG) Slog.v(TAG, "setWallpaper which=0x" + Integer.toHexString(which));
             WallpaperData wallpaper;
@@ -1376,9 +1437,7 @@
                     wallpaper.whichPending = which;
                     wallpaper.setComplete = completion;
                     wallpaper.cropHint.set(cropHint);
-                    if ((which & FLAG_SYSTEM) != 0) {
-                        wallpaper.allowBackup = allowBackup;
-                    }
+                    wallpaper.allowBackup = allowBackup;
                 }
                 return pfd;
             } finally {
@@ -1453,19 +1512,27 @@
     }
 
     @Override
-    public void setWallpaperComponentChecked(ComponentName name, String callingPackage) {
+    public void setWallpaperComponentChecked(ComponentName name, String callingPackage,
+            int userId) {
+
         if (isWallpaperSupported(callingPackage) && isSetWallpaperAllowed(callingPackage)) {
-            setWallpaperComponent(name);
+            setWallpaperComponent(name, userId);
         }
     }
 
     // ToDo: Remove this version of the function
     @Override
     public void setWallpaperComponent(ComponentName name) {
+        setWallpaperComponent(name, UserHandle.getCallingUserId());
+    }
+
+    private void setWallpaperComponent(ComponentName name, int userId) {
+        userId = ActivityManager.handleIncomingUser(getCallingPid(), getCallingUid(), userId,
+                false /* all */, true /* full */, "changing live wallpaper", null /* pkg */);
         checkPermission(android.Manifest.permission.SET_WALLPAPER_COMPONENT);
+
         synchronized (mLock) {
             if (DEBUG) Slog.v(TAG, "setWallpaperComponent name=" + name);
-            int userId = UserHandle.getCallingUserId();
             WallpaperData wallpaper = mWallpaperMap.get(userId);
             if (wallpaper == null) {
                 throw new IllegalStateException("Wallpaper not yet initialized for user " + userId);
@@ -1721,12 +1788,14 @@
     }
 
     @Override
-    public boolean isWallpaperBackupEligible(int userId) {
+    public boolean isWallpaperBackupEligible(int which, int userId) {
         if (Binder.getCallingUid() != Process.SYSTEM_UID) {
             throw new SecurityException("Only the system may call isWallpaperBackupEligible");
         }
 
-        WallpaperData wallpaper = getWallpaperSafeLocked(userId, FLAG_SYSTEM);
+        WallpaperData wallpaper = (which == FLAG_LOCK)
+                ? mLockWallpaperMap.get(userId)
+                : mWallpaperMap.get(userId);
         return (wallpaper != null) ? wallpaper.allowBackup : false;
     }
 
diff --git a/services/core/java/com/android/server/wm/AccessibilityController.java b/services/core/java/com/android/server/wm/AccessibilityController.java
index 8be5dfb..66b1b3d 100644
--- a/services/core/java/com/android/server/wm/AccessibilityController.java
+++ b/services/core/java/com/android/server/wm/AccessibilityController.java
@@ -348,6 +348,7 @@
                     }
                     switch (type) {
                         case WindowManager.LayoutParams.TYPE_APPLICATION:
+                        case WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION:
                         case WindowManager.LayoutParams.TYPE_APPLICATION_PANEL:
                         case WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA:
                         case WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL:
@@ -643,7 +644,7 @@
                 final int windowCount = windowList.size();
                 for (int i = 0; i < windowCount; i++) {
                     WindowState windowState = windowList.get(i);
-                    if (windowState.isOnScreen() &&
+                    if (windowState.isOnScreen() && windowState.isVisibleLw() &&
                             !windowState.mWinAnimator.mEnterAnimationPending) {
                         outWindows.put(windowState.mLayer, windowState);
                     }
@@ -1071,14 +1072,15 @@
                                 Region.Op.REVERSE_DIFFERENCE);
                     }
 
-                    // We figured out what is touchable for the entire screen - done.
-                    if (unaccountedSpace.isEmpty()) {
-                        break;
-                    }
-
                     // If a window is modal it prevents other windows from being touched
                     if ((flags & (WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                             | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL)) == 0) {
+                        // Account for all space in the task, whether the windows in it are
+                        // touchable or not. The modal window blocks all touches from the task's
+                        // area.
+                        unaccountedSpace.op(windowState.getDisplayFrameLw(), unaccountedSpace,
+                                Region.Op.REVERSE_DIFFERENCE);
+
                         if (task != null) {
                             // If the window is associated with a particular task, we can skip the
                             // rest of the windows for that task.
@@ -1090,6 +1092,10 @@
                             break;
                         }
                     }
+                    // We figured out what is touchable for the entire screen - done.
+                    if (unaccountedSpace.isEmpty()) {
+                        break;
+                    }
                 }
 
                 // Always report the focused window.
diff --git a/services/core/java/com/android/server/wm/AppTransition.java b/services/core/java/com/android/server/wm/AppTransition.java
index 4553f8e..cd46165 100644
--- a/services/core/java/com/android/server/wm/AppTransition.java
+++ b/services/core/java/com/android/server/wm/AppTransition.java
@@ -162,6 +162,9 @@
     private final WindowManagerService mService;
 
     private int mNextAppTransition = TRANSIT_UNSET;
+    private int mLastUsedAppTransition = TRANSIT_UNSET;
+    private String mLastOpeningApp;
+    private String mLastClosingApp;
 
     private static final int NEXT_TRANSIT_TYPE_NONE = 0;
     private static final int NEXT_TRANSIT_TYPE_CUSTOM = 1;
@@ -285,6 +288,13 @@
 
     private void setAppTransition(int transit) {
         mNextAppTransition = transit;
+        setLastAppTransition(TRANSIT_UNSET, null, null);
+    }
+
+    void setLastAppTransition(int transit, AppWindowToken openingApp, AppWindowToken closingApp) {
+        mLastUsedAppTransition = transit;
+        mLastOpeningApp = "" + openingApp;
+        mLastClosingApp = "" + closingApp;
     }
 
     boolean isReady() {
@@ -364,6 +374,7 @@
 
     void goodToGo(AppWindowAnimator topOpeningAppAnimator, AppWindowAnimator topClosingAppAnimator,
             ArraySet<AppWindowToken> openingApps, ArraySet<AppWindowToken> closingApps) {
+        int appTransition = mNextAppTransition;
         mNextAppTransition = TRANSIT_UNSET;
         mAppTransitionState = APP_STATE_RUNNING;
         notifyAppTransitionStartingLocked(
@@ -372,7 +383,7 @@
                 topOpeningAppAnimator != null ? topOpeningAppAnimator.animation : null,
                 topClosingAppAnimator != null ? topClosingAppAnimator.animation : null);
         mService.getDefaultDisplayContentLocked().getDockedDividerController()
-                .notifyAppTransitionStarting();
+                .notifyAppTransitionStarting(openingApps, appTransition);
 
         // Prolong the start for the transition when docking a task from recents, unless recents
         // ended it already then we don't need to wait.
@@ -604,7 +615,7 @@
             float scaleH = mTmpRect.height() / (float) appHeight;
             Animation scale = new ScaleAnimation(scaleW, 1, scaleH, 1,
                     computePivot(mTmpRect.left, scaleW),
-                    computePivot(mTmpRect.right, scaleH));
+                    computePivot(mTmpRect.top, scaleH));
             scale.setInterpolator(mDecelerateInterpolator);
 
             Animation alpha = new AlphaAnimation(0, 1);
@@ -1057,7 +1068,7 @@
         final float thumbWidth = thumbWidthI > 0 ? thumbWidthI : 1;
         final int thumbHeightI = mTmpRect.height();
         final float thumbHeight = thumbHeightI > 0 ? thumbHeightI : 1;
-        final int thumbStartX = mTmpRect.left - containingFrame.left;
+        final int thumbStartX = mTmpRect.left - containingFrame.left - contentInsets.left;
         final int thumbStartY = mTmpRect.top - containingFrame.top;
 
         switch (thumbTransitState) {
@@ -1615,8 +1626,7 @@
         if (isTransitionSet()) {
             clear();
             mNextAppTransitionType = NEXT_TRANSIT_TYPE_SCALE_UP;
-            putDefaultNextAppTransitionCoordinates(startX, startY, startX + startWidth,
-                    startY + startHeight, null);
+            putDefaultNextAppTransitionCoordinates(startX, startY, startWidth, startHeight, null);
             postAnimationCallback();
         }
     }
@@ -1905,6 +1915,14 @@
             pw.print(prefix); pw.print("mNextAppTransitionCallback=");
                     pw.println(mNextAppTransitionCallback);
         }
+        if (mLastUsedAppTransition != TRANSIT_NONE) {
+            pw.print(prefix); pw.print("mLastUsedAppTransition=");
+                    pw.println(appTransitionToString(mLastUsedAppTransition));
+            pw.print(prefix); pw.print("mLastOpeningApp=");
+                    pw.println(mLastOpeningApp);
+            pw.print(prefix); pw.print("mLastClosingApp=");
+                    pw.println(mLastClosingApp);
+        }
     }
 
     public void setCurrentUser(int newUserId) {
diff --git a/services/core/java/com/android/server/wm/AppWindowToken.java b/services/core/java/com/android/server/wm/AppWindowToken.java
index e425e7d1..b1d2edf 100644
--- a/services/core/java/com/android/server/wm/AppWindowToken.java
+++ b/services/core/java/com/android/server/wm/AppWindowToken.java
@@ -17,7 +17,9 @@
 package com.android.server.wm;
 
 import static android.app.ActivityManager.StackId;
+import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
+import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
@@ -28,6 +30,7 @@
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
 import static com.android.server.wm.WindowManagerService.WINDOW_REPLACEMENT_TIMEOUT_DURATION;
 import static com.android.server.wm.WindowManagerService.H.NOTIFY_ACTIVITY_DRAWN;
+import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_NONE;
 
 import com.android.server.input.InputApplicationHandle;
 import com.android.server.wm.WindowManagerService.H;
@@ -42,6 +45,7 @@
 import android.view.IApplicationToken;
 import android.view.View;
 import android.view.WindowManager;
+import android.view.animation.Animation;
 
 import java.io.PrintWriter;
 import java.util.ArrayDeque;
@@ -132,6 +136,7 @@
     boolean mAlwaysFocusable;
 
     boolean mAppStopped;
+    int mRotationAnimationHint;
     int mPendingRelaunchCount;
 
     private ArrayList<WindowSurfaceController.SurfaceControlWithBackground> mSurfaceViewBackgrounds =
@@ -336,16 +341,66 @@
         }
     }
 
-    // Here we destroy surfaces which have been marked as eligible by the animator, taking care
-    // to ensure the client has finished with them. If the client could still be using them
-    // we will skip destruction and try again when the client has stopped.
+    void clearAnimatingFlags() {
+        boolean wallpaperMightChange = false;
+        for (int i = allAppWindows.size() - 1; i >= 0; i--) {
+            final WindowState win = allAppWindows.get(i);
+            // We don't want to clear it out for windows that get replaced, because the
+            // animation depends on the flag to remove the replaced window.
+            //
+            // We also don't clear the mAnimatingExit flag for windows which have the
+            // mRemoveOnExit flag. This indicates an explicit remove request has been issued
+            // by the client. We should let animation proceed and not clear this flag or
+            // they won't eventually be removed by WindowStateAnimator#finishExit.
+            if (!win.mWillReplaceWindow && !win.mRemoveOnExit) {
+                // Clear mAnimating flag together with mAnimatingExit. When animation
+                // changes from exiting to entering, we need to clear this flag until the
+                // new animation gets applied, so that isAnimationStarting() becomes true
+                // until then.
+                // Otherwise applySurfaceChangesTransaction will faill to skip surface
+                // placement for this window during this period, one or more frame will
+                // show up with wrong position or scale.
+                if (win.mAnimatingExit) {
+                    win.mAnimatingExit = false;
+                    wallpaperMightChange = true;
+                }
+                if (win.mWinAnimator.mAnimating) {
+                    win.mWinAnimator.mAnimating = false;
+                    wallpaperMightChange = true;
+                }
+                if (win.mDestroying) {
+                    win.mDestroying = false;
+                    service.mDestroySurface.remove(win);
+                    wallpaperMightChange = true;
+                }
+            }
+        }
+        if (wallpaperMightChange) {
+            requestUpdateWallpaperIfNeeded();
+        }
+    }
+
     void destroySurfaces() {
+        destroySurfaces(false /*cleanupOnResume*/);
+    }
+
+    /**
+     * Destroy surfaces which have been marked as eligible by the animator, taking care to ensure
+     * the client has finished with them.
+     *
+     * @param cleanupOnResume whether this is done when app is resumed without fully stopped. If
+     * set to true, destroy only surfaces of removed windows, and clear relevant flags of the
+     * others so that they are ready to be reused. If set to false (common case), destroy all
+     * surfaces that's eligible, if the app is already stopped.
+     */
+
+    private void destroySurfaces(boolean cleanupOnResume) {
         final ArrayList<WindowState> allWindows = (ArrayList<WindowState>) allAppWindows.clone();
         final DisplayContentList displayList = new DisplayContentList();
         for (int i = allWindows.size() - 1; i >= 0; i--) {
             final WindowState win = allWindows.get(i);
 
-            if (!(mAppStopped || win.mWindowRemovalAllowed)) {
+            if (!(mAppStopped || win.mWindowRemovalAllowed || cleanupOnResume)) {
                 continue;
             }
 
@@ -360,7 +415,9 @@
                     + " win.mWindowRemovalAllowed=" + win.mWindowRemovalAllowed
                     + " win.mRemoveOnExit=" + win.mRemoveOnExit);
 
-            win.destroyOrSaveSurface();
+            if (!cleanupOnResume || win.mRemoveOnExit) {
+                win.destroyOrSaveSurface();
+            }
             if (win.mRemoveOnExit) {
                 service.removeWindowInnerLocked(win);
             }
@@ -368,6 +425,9 @@
             if (displayContent != null && !displayList.contains(displayContent)) {
                 displayList.add(displayContent);
             }
+            if (cleanupOnResume) {
+                win.requestUpdateWallpaperIfNeeded();
+            }
             win.mDestroying = false;
         }
         for (int i = 0; i < displayList.size(); i++) {
@@ -378,18 +438,31 @@
     }
 
     /**
-     * If the application has stopped it is okay to destroy any surfaces which were keeping alive
-     * in case they were still being used.
+     * Notify that the app is now resumed, and it was not stopped before, perform a clean
+     * up of the surfaces
      */
-    void notifyAppStopped(boolean stopped) {
-        if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: stopped=" + stopped + " " + this);
-        mAppStopped = stopped;
-
-        if (stopped) {
-            destroySurfaces();
-            // Remove any starting window that was added for this app if they are still around.
-            mTask.mService.scheduleRemoveStartingWindowLocked(this);
+    void notifyAppResumed(boolean wasStopped, boolean allowSavedSurface) {
+        if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppResumed: wasStopped=" + wasStopped
+                + " allowSavedSurface=" + allowSavedSurface + " " + this);
+        mAppStopped = false;
+        if (!wasStopped) {
+            destroySurfaces(true /*cleanupOnResume*/);
         }
+        if (!allowSavedSurface) {
+            destroySavedSurfaces();
+        }
+    }
+
+    /**
+     * Notify that the app has stopped, and it is okay to destroy any surfaces which were
+     * keeping alive in case they were still being used.
+     */
+    void notifyAppStopped() {
+        if (DEBUG_ADD_REMOVE) Slog.v(TAG, "notifyAppStopped: " + this);
+        mAppStopped = true;
+        destroySurfaces();
+        // Remove any starting window that was added for this app if they are still around.
+        mTask.mService.scheduleRemoveStartingWindowLocked(this);
     }
 
     /**
@@ -628,6 +701,16 @@
         }
     }
 
+    void clearRelaunching() {
+        if (mPendingRelaunchCount == 0) {
+            return;
+        }
+        if (canFreezeBounds()) {
+            unfreezeBounds();
+        }
+        mPendingRelaunchCount = 0;
+    }
+
     void addWindow(WindowState w) {
         for (int i = allAppWindows.size() - 1; i >= 0; i--) {
             WindowState candidate = allAppWindows.get(i);
@@ -707,8 +790,12 @@
      * Unfreezes the previously frozen bounds. See {@link #freezeBounds}.
      */
     private void unfreezeBounds() {
-        mFrozenBounds.remove();
-        mFrozenMergedConfig.remove();
+        if (!mFrozenBounds.isEmpty()) {
+            mFrozenBounds.remove();
+        }
+        if (!mFrozenMergedConfig.isEmpty()) {
+            mFrozenMergedConfig.remove();
+        }
         for (int i = windows.size() - 1; i >= 0; i--) {
             final WindowState win = windows.get(i);
             if (!win.mHasSurface) {
@@ -753,6 +840,21 @@
         }
     }
 
+    /**
+     * See {@link WindowManagerService#overridePlayingAppAnimationsLw}
+     */
+    void overridePlayingAppAnimations(Animation a) {
+        if (mAppAnimator.isAnimating()) {
+            final WindowState win = findMainWindow();
+            if (win == null) {
+                return;
+            }
+            final int width = win.mContainingFrame.width();
+            final int height = win.mContainingFrame.height();
+            mAppAnimator.setAnimation(a, width, height, false, STACK_CLIP_NONE);
+        }
+    }
+
     @Override
     void dump(PrintWriter pw, String prefix) {
         super.dump(pw, prefix);
diff --git a/services/core/java/com/android/server/wm/DimLayerController.java b/services/core/java/com/android/server/wm/DimLayerController.java
index 2b9879e..7f97c46 100644
--- a/services/core/java/com/android/server/wm/DimLayerController.java
+++ b/services/core/java/com/android/server/wm/DimLayerController.java
@@ -1,5 +1,6 @@
 package com.android.server.wm;
 
+import static android.view.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DIM_LAYER;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
@@ -250,6 +251,13 @@
                     duration = getDimLayerFadeDuration(duration);
                 }
                 state.dimLayer.show(dimLayer, dimAmount, duration);
+
+                // If we showed a dim layer, make sure to redo the layout because some things depend
+                // on whether a dim layer is showing or not.
+                if (targetAlpha == 0) {
+                    mDisplayContent.pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
+                    mDisplayContent.layoutNeeded = true;
+                }
             }
         } else if (state.dimLayer.getLayer() != dimLayer) {
             state.dimLayer.setLayer(dimLayer);
diff --git a/services/core/java/com/android/server/wm/DisplayContent.java b/services/core/java/com/android/server/wm/DisplayContent.java
index fba439f..12399bd 100644
--- a/services/core/java/com/android/server/wm/DisplayContent.java
+++ b/services/core/java/com/android/server/wm/DisplayContent.java
@@ -22,19 +22,25 @@
 import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
 import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
 import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
+import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
 import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
 
 import android.app.ActivityManager.StackId;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
 import android.graphics.Rect;
 import android.graphics.Region;
 import android.graphics.Region.Op;
+import android.os.Build;
+import android.os.UserHandle;
 import android.util.DisplayMetrics;
 import android.util.Slog;
 import android.view.Display;
 import android.view.DisplayInfo;
 import android.view.Surface;
+import android.view.animation.Animation;
 
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -191,6 +197,16 @@
         return mHomeStack;
     }
 
+    TaskStack getStackById(int stackId) {
+        for (int i = mStacks.size() - 1; i >= 0; --i) {
+            final TaskStack stack = mStacks.get(i);
+            if (stack.mStackId == stackId) {
+                return stack;
+            }
+        }
+        return null;
+    }
+
     void updateDisplayInfo() {
         mDisplay.getDisplayInfo(mDisplayInfo);
         mDisplay.getMetrics(mDisplayMetrics);
@@ -627,7 +643,7 @@
      */
     TaskStack getDockedStackVisibleForUserLocked() {
         final TaskStack stack = mService.mStackIdToStack.get(DOCKED_STACK_ID);
-        return (stack != null && stack.isVisibleForUserLocked()) ? stack : null;
+        return (stack != null && stack.isVisibleLocked(true /* ignoreKeyguard */)) ? stack : null;
     }
 
     /**
@@ -664,4 +680,49 @@
 
         return touchedWin;
     }
+
+    /**
+     * See {@link WindowManagerService#overridePlayingAppAnimationsLw}.
+     */
+    void overridePlayingAppAnimationsLw(Animation a) {
+        for (int i = mStacks.size() - 1; i >= 0; i--) {
+            mStacks.get(i).overridePlayingAppAnimations(a);
+        }
+    }
+
+    boolean canAddToastWindowForUid(int uid) {
+        // We allow one toast window per UID being shown at a time.
+        WindowList windows = getWindowList();
+        final int windowCount = windows.size();
+        for (int i = 0; i < windowCount; i++) {
+            WindowState window = windows.get(i);
+            if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == uid
+                    && !window.mPermanentlyHidden && !window.mAnimatingExit
+                    && !window.mRemoveOnExit) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus,
+                                                   WindowState newFocus) {
+        if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
+            return;
+        }
+        final int lostFocusUid = oldFocus.mOwnerUid;
+        WindowList windows = getWindowList();
+        final int windowCount = windows.size();
+        for (int i = 0; i < windowCount; i++) {
+            WindowState window = windows.get(i);
+            if (window.mAttrs.type == TYPE_TOAST && window.mOwnerUid == lostFocusUid) {
+                if (!mService.mH.hasMessages(WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window)) {
+                    mService.mH.sendMessageDelayed(
+                            mService.mH.obtainMessage(
+                                    WindowManagerService.H.WINDOW_HIDE_TIMEOUT, window),
+                            window.mAttrs.hideTimeoutMilliseconds);
+                }
+            }
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/wm/DockedStackDividerController.java b/services/core/java/com/android/server/wm/DockedStackDividerController.java
index e73649d..6f0a43a 100644
--- a/services/core/java/com/android/server/wm/DockedStackDividerController.java
+++ b/services/core/java/com/android/server/wm/DockedStackDividerController.java
@@ -29,6 +29,7 @@
 import static android.view.WindowManager.DOCKED_TOP;
 import static com.android.server.wm.AppTransition.DEFAULT_APP_TRANSITION_DURATION;
 import static com.android.server.wm.AppTransition.TOUCH_RESPONSE_INTERPOLATOR;
+import static com.android.server.wm.AppTransition.TRANSIT_NONE;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
 import static com.android.server.wm.WindowManagerService.H.NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED;
@@ -38,6 +39,7 @@
 import android.graphics.Rect;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
+import android.util.ArraySet;
 import android.util.Slog;
 import android.view.DisplayInfo;
 import android.view.IDockedStackListener;
@@ -388,8 +390,8 @@
                 inputMethodManagerInternal.hideCurrentInputMethod();
                 mImeHideRequested = true;
             }
-        } else {
-            setMinimizedDockedStack(false);
+        } else if (setMinimizedDockedStack(false)) {
+            mService.mWindowPlacerLocked.performSurfacePlacement();
         }
     }
 
@@ -492,8 +494,32 @@
         checkMinimizeChanged(false /* animate */);
     }
 
-    void notifyAppTransitionStarting() {
+    void notifyAppTransitionStarting(ArraySet<AppWindowToken> openingApps, int appTransition) {
+        final boolean wasMinimized = mMinimizedDock;
         checkMinimizeChanged(true /* animate */);
+
+        // We were minimized, and now we are still minimized, but somebody is trying to launch an
+        // app in docked stack, better show recent apps so we actually get unminimized! This catches
+        // any case that was missed in ActivityStarter.postStartActivityUncheckedProcessing because
+        // we couldn't retrace the launch of the app in the docked stack to the launch from
+        // homescreen.
+        if (wasMinimized && mMinimizedDock && containsAppInDockedStack(openingApps)
+                && appTransition != TRANSIT_NONE) {
+            mService.showRecentApps(true /* fromHome */);
+        }
+    }
+
+    /**
+     * @return true if {@param apps} contains an activity in the docked stack, false otherwise.
+     */
+    private boolean containsAppInDockedStack(ArraySet<AppWindowToken> apps) {
+        for (int i = apps.size() - 1; i >= 0; i--) {
+            final AppWindowToken token = apps.valueAt(i);
+            if (token.mTask != null && token.mTask.mStack.mStackId == DOCKED_STACK_ID) {
+                return true;
+            }
+        }
+        return false;
     }
 
     boolean isMinimizedDock() {
@@ -542,31 +568,43 @@
             return;
         }
 
-        clearImeAdjustAnimation();
+        final boolean imeChanged = clearImeAdjustAnimation();
+        boolean minimizedChange = false;
         if (minimizedDock) {
             if (animate) {
                 startAdjustAnimation(0f, 1f);
             } else {
-                setMinimizedDockedStack(true);
+                minimizedChange |= setMinimizedDockedStack(true);
             }
         } else {
             if (animate) {
                 startAdjustAnimation(1f, 0f);
             } else {
-                setMinimizedDockedStack(false);
+                minimizedChange |= setMinimizedDockedStack(false);
             }
         }
+        if (imeChanged || minimizedChange) {
+            if (imeChanged && !minimizedChange) {
+                Slog.d(TAG, "setMinimizedDockedStack: IME adjust changed due to minimizing,"
+                        + " minimizedDock=" + minimizedDock
+                        + " minimizedChange=" + minimizedChange);
+            }
+            mService.mWindowPlacerLocked.performSurfacePlacement();
+        }
     }
 
-    private void clearImeAdjustAnimation() {
+    private boolean clearImeAdjustAnimation() {
+        boolean changed = false;
         final ArrayList<TaskStack> stacks = mDisplayContent.getStacks();
         for (int i = stacks.size() - 1; i >= 0; --i) {
             final TaskStack stack = stacks.get(i);
             if (stack != null && stack.isAdjustedForIme()) {
                 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
+                changed  = true;
             }
         }
         mAnimatingForIme = false;
+        return changed;
     }
 
     private void startAdjustAnimation(float from, float to) {
@@ -625,8 +663,21 @@
                 if (mDelayedImeWin != null) {
                     mDelayedImeWin.mWinAnimator.endDelayingAnimationStart();
                 }
+                // If the adjust status changed since this was posted, only notify
+                // the new states and don't animate.
+                long duration = 0;
+                if (mAdjustedForIme == adjustedForIme
+                        && mAdjustedForDivider == adjustedForDivider) {
+                    duration = IME_ADJUST_ANIM_DURATION;
+                } else {
+                    Slog.w(TAG, "IME adjust changed while waiting for drawn:"
+                            + " adjustedForIme=" + adjustedForIme
+                            + " adjustedForDivider=" + adjustedForDivider
+                            + " mAdjustedForIme=" + mAdjustedForIme
+                            + " mAdjustedForDivider=" + mAdjustedForDivider);
+                }
                 notifyAdjustedForImeChanged(
-                        adjustedForIme || adjustedForDivider, IME_ADJUST_ANIM_DURATION);
+                        mAdjustedForIme || mAdjustedForDivider, duration);
             };
         } else {
             notifyAdjustedForImeChanged(
@@ -634,15 +685,10 @@
         }
     }
 
-    private void setMinimizedDockedStack(boolean minimized) {
+    private boolean setMinimizedDockedStack(boolean minimized) {
         final TaskStack stack = mDisplayContent.getDockedStackVisibleForUserLocked();
         notifyDockedStackMinimizedChanged(minimized, 0);
-        if (stack == null) {
-            return;
-        }
-        if (stack.setAdjustedForMinimizedDock(minimized ? 1f : 0f)) {
-            mService.mWindowPlacerLocked.performSurfacePlacement();
-        }
+        return stack != null && stack.setAdjustedForMinimizedDock(minimized ? 1f : 0f);
     }
 
     private boolean isAnimationMaximizing() {
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index b4387b9..ca183010 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -38,6 +38,7 @@
 import android.util.Slog;
 import android.view.DisplayInfo;
 import android.view.Surface;
+import android.view.animation.Animation;
 
 import com.android.server.EventLogTags;
 
@@ -624,8 +625,8 @@
                     //
                     // As we use this flag as a hint to freeze surface boundary updates,
                     // we'd like to only apply this to TYPE_BASE_APPLICATION,
-                    // windows of TYPE_APPLICATION like dialogs, could appear
-                    // to not be drag resizing while they resize, but we'd
+                    // windows of TYPE_APPLICATION (or TYPE_DRAWN_APPLICATION) like dialogs,
+                    // could appear to not be drag resizing while they resize, but we'd
                     // still like to manipulate their frame to update crop, etc...
                     //
                     // Anyway we don't need to synchronize position and content updates for these
@@ -677,19 +678,6 @@
         return (tokensCount != 0) && mAppTokens.get(tokensCount - 1).showForAllUsers;
     }
 
-    boolean isVisibleForUser() {
-        for (int i = mAppTokens.size() - 1; i >= 0; i--) {
-            final AppWindowToken appToken = mAppTokens.get(i);
-            for (int j = appToken.allAppWindows.size() - 1; j >= 0; j--) {
-                WindowState window = appToken.allAppWindows.get(j);
-                if (!window.isHiddenFromUserLocked()) {
-                    return true;
-                }
-            }
-        }
-        return false;
-    }
-
     boolean isVisible() {
         for (int i = mAppTokens.size() - 1; i >= 0; i--) {
             final AppWindowToken appToken = mAppTokens.get(i);
@@ -778,6 +766,15 @@
         return mStack.getDisplayContent().getDisplayInfo();
     }
 
+    /**
+     * See {@link WindowManagerService#overridePlayingAppAnimationsLw}
+     */
+    void overridePlayingAppAnimations(Animation a) {
+        for (int i = mAppTokens.size() - 1; i >= 0; i--) {
+            mAppTokens.get(i).overridePlayingAppAnimations(a);
+        }
+    }
+
     @Override
     public String toString() {
         return "{taskId=" + mTaskId + " appTokens=" + mAppTokens + " mdr=" + mDeferRemoval + "}";
diff --git a/services/core/java/com/android/server/wm/TaskStack.java b/services/core/java/com/android/server/wm/TaskStack.java
index 8be5b19..8f8f642 100644
--- a/services/core/java/com/android/server/wm/TaskStack.java
+++ b/services/core/java/com/android/server/wm/TaskStack.java
@@ -44,6 +44,7 @@
 import android.view.DisplayInfo;
 import android.view.Surface;
 import android.view.SurfaceControl;
+import android.view.animation.Animation;
 
 import com.android.internal.policy.DividerSnapAlgorithm;
 import com.android.internal.policy.DividerSnapAlgorithm.SnapTarget;
@@ -398,23 +399,21 @@
             return false;
         }
 
-        final int oldDockSide = mStackId == DOCKED_STACK_ID ? getDockSide() : DOCKED_INVALID;
         mTmpRect2.set(mBounds);
         mDisplayContent.rotateBounds(mRotation, newRotation, mTmpRect2);
         if (mStackId == DOCKED_STACK_ID) {
             repositionDockedStackAfterRotation(mTmpRect2);
             snapDockedStackAfterRotation(mTmpRect2);
             final int newDockSide = getDockSide(mTmpRect2);
-            if (oldDockSide != newDockSide) {
-                // Update the dock create mode and clear the dock create bounds, these
-                // might change after a rotation and the original values will be invalid.
-                mService.setDockedStackCreateStateLocked(
-                        (newDockSide == DOCKED_LEFT || newDockSide == DOCKED_TOP)
-                        ? DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT
-                        : DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT,
-                        null);
-                mDisplayContent.getDockedDividerController().notifyDockSideChanged(newDockSide);
-            }
+
+            // Update the dock create mode and clear the dock create bounds, these
+            // might change after a rotation and the original values will be invalid.
+            mService.setDockedStackCreateStateLocked(
+                    (newDockSide == DOCKED_LEFT || newDockSide == DOCKED_TOP)
+                    ? DOCKED_STACK_CREATE_MODE_TOP_OR_LEFT
+                    : DOCKED_STACK_CREATE_MODE_BOTTOM_OR_RIGHT,
+                    null);
+            mDisplayContent.getDockedDividerController().notifyDockSideChanged(newDockSide);
         }
 
         mBoundsAfterRotation.set(mTmpRect2);
@@ -890,7 +889,7 @@
             mAdjustImeAmount = adjustAmount;
             mAdjustDividerAmount = adjustDividerAmount;
             updateAdjustedBounds();
-            return isVisibleForUserLocked();
+            return isVisibleLocked(true /* ignoreKeyguard */);
         } else {
             return false;
         }
@@ -926,7 +925,7 @@
         if (minimizeAmount != mMinimizeAmount) {
             mMinimizeAmount = minimizeAmount;
             updateAdjustedBounds();
-            return isVisibleForUserLocked();
+            return isVisibleLocked(true /* ignoreKeyguard*/);
         } else {
             return false;
         }
@@ -943,7 +942,7 @@
     void beginImeAdjustAnimation() {
         for (int j = mTasks.size() - 1; j >= 0; j--) {
             final Task task = mTasks.get(j);
-            if (task.isVisibleForUser()) {
+            if (task.isVisible()) {
                 task.setDragResizing(true, DRAG_RESIZE_MODE_DOCKED_DIVIDER);
                 task.addWindowsWaitingForDrawnIfResizingChanged();
             }
@@ -1233,9 +1232,13 @@
     }
 
     boolean isVisibleLocked() {
+        return isVisibleLocked(false /* ignoreKeyguard */);
+    }
+
+    boolean isVisibleLocked(boolean ignoreKeyguard) {
         final boolean keyguardOn = mService.mPolicy.isKeyguardShowingOrOccluded()
                 && !mService.mAnimator.mKeyguardGoingAway;
-        if (keyguardOn && !StackId.isAllowedOverLockscreen(mStackId)) {
+        if (!ignoreKeyguard && keyguardOn && !StackId.isAllowedOverLockscreen(mStackId)) {
             // The keyguard is showing and the stack shouldn't show on top of the keyguard.
             return false;
         }
@@ -1252,20 +1255,6 @@
         return false;
     }
 
-    /**
-     * @return true if a the stack is visible for the current in user, ignoring any other visibility
-     *         aspects, and false otherwise
-     */
-    boolean isVisibleForUserLocked() {
-        for (int i = mTasks.size() - 1; i >= 0; i--) {
-            final Task task = mTasks.get(i);
-            if (task.isVisibleForUser()) {
-                return true;
-            }
-        }
-        return false;
-    }
-
     boolean isDragResizing() {
         return mDragResizing;
     }
@@ -1379,4 +1368,13 @@
     public boolean getBoundsAnimating() {
         return mBoundsAnimating;
     }
+
+    /**
+     * See {@link WindowManagerService#overridePlayingAppAnimationsLw}
+     */
+    void overridePlayingAppAnimations(Animation a) {
+        for (int i = mTasks.size() - 1; i >= 0; --i) {
+            mTasks.get(i).overridePlayingAppAnimations(a);
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/wm/WallpaperController.java b/services/core/java/com/android/server/wm/WallpaperController.java
index 88028be..e7ceba9 100644
--- a/services/core/java/com/android/server/wm/WallpaperController.java
+++ b/services/core/java/com/android/server/wm/WallpaperController.java
@@ -246,7 +246,10 @@
 
     boolean updateWallpaperOffset(WindowState wallpaperWin, int dw, int dh, boolean sync) {
         boolean rawChanged = false;
-        float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : 0.5f;
+        // Set the default wallpaper x-offset to either edge of the screen (depending on RTL), to
+        // match the behavior of most Launchers
+        float defaultWallpaperX = wallpaperWin.isRtl() ? 1f : 0f;
+        float wpx = mLastWallpaperX >= 0 ? mLastWallpaperX : defaultWallpaperX;
         float wpxs = mLastWallpaperXStep >= 0 ? mLastWallpaperXStep : -1.0f;
         int availw = wallpaperWin.mFrame.right - wallpaperWin.mFrame.left - dw;
         int offset = availw > 0 ? -(int)(availw * wpx + .5f) : 0;
@@ -479,6 +482,8 @@
         boolean resetTopWallpaper = false;
         boolean inFreeformSpace = false;
         boolean replacing = false;
+        boolean keyguardGoingAwayWithWallpaper = false;
+
         for (int i = windows.size() - 1; i >= 0; i--) {
             w = windows.get(i);
             if ((w.mAttrs.type == TYPE_WALLPAPER)) {
@@ -506,13 +511,11 @@
                 inFreeformSpace = stack != null && stack.mStackId == FREEFORM_WORKSPACE_STACK_ID;
             }
 
-            replacing = replacing || w.mWillReplaceWindow;
+            replacing |= w.mWillReplaceWindow;
+            keyguardGoingAwayWithWallpaper |= (w.mAppToken != null
+                    && w.mWinAnimator.mKeyguardGoingAwayWithWallpaper);
 
-            // If the app is executing an animation because the keyguard is going away (and the
-            // keyguard was showing the wallpaper) keep the wallpaper during the animation so it
-            // doesn't flicker out.
-            final boolean hasWallpaper = (w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0
-                    || (w.mAppToken != null && w.mWinAnimator.mKeyguardGoingAwayWithWallpaper);
+            final boolean hasWallpaper = (w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0;
             if (hasWallpaper && w.isOnScreen() && (mWallpaperTarget == w || w.isDrawFinishedLw())) {
                 if (DEBUG_WALLPAPER) Slog.v(TAG, "Found wallpaper target: #" + i + "=" + w);
                 result.setWallpaperTarget(w, i);
@@ -529,18 +532,26 @@
             }
         }
 
-        if (result.wallpaperTarget == null && windowDetachedI >= 0) {
+        if (result.wallpaperTarget != null) {
+            return;
+        }
+
+        if (windowDetachedI >= 0) {
             if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
                     "Found animating detached wallpaper activity: #" + windowDetachedI + "=" + w);
             result.setWallpaperTarget(w, windowDetachedI);
-        }
-        if (result.wallpaperTarget == null
-                && (inFreeformSpace || (replacing && mWallpaperTarget != null))) {
+        } else if (inFreeformSpace || (replacing && mWallpaperTarget != null)) {
             // In freeform mode we set the wallpaper as its own target, so we don't need an
             // additional window to make it visible. When we are replacing a window and there was
             // wallpaper before replacement, we want to keep the window until the new windows fully
             // appear and can determine the visibility, to avoid flickering.
             result.setWallpaperTarget(result.topWallpaper, result.topWallpaperIndex);
+
+        } else if (keyguardGoingAwayWithWallpaper) {
+            // If the app is executing an animation because the keyguard is going away (and the
+            // keyguard was showing the wallpaper) keep the wallpaper during the animation so it
+            // doesn't flicker out by having it be its own target.
+            result.setWallpaperTarget(result.topWallpaper, result.topWallpaperIndex);
         }
     }
 
@@ -575,27 +586,24 @@
                     if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
                             "New i: " + wallpaperTargetIndex + " old i: " + oldI);
                     if (oldI >= 0) {
-                        if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
-                                "Animating wallpapers: old#" + oldI + "=" + oldW + "; new#"
-                                + wallpaperTargetIndex + "=" + wallpaperTarget);
+                        final boolean newTargetHidden =
+                                wallpaperTarget.mAppToken != null && wallpaperTarget.mAppToken.hiddenRequested;
+                        final boolean oldTargetHidden =
+                                oldW.mAppToken != null && oldW.mAppToken.hiddenRequested;
+                        if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG, "Animating wallpapers:"
+                                + " old#" + oldI + "=" + oldW + " hidden=" + oldTargetHidden
+                                + " new#" + wallpaperTargetIndex + "=" + wallpaperTarget
+                                + " hidden=" + newTargetHidden);
 
-                        // Set the new target correctly.
-                        if (wallpaperTarget.mAppToken != null
-                                && wallpaperTarget.mAppToken.hiddenRequested) {
-                            if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
-                                    "Old wallpaper still the target.");
-                            mWallpaperTarget = oldW;
-                            wallpaperTarget = oldW;
-                            wallpaperTargetIndex = oldI;
-                        }
-                        // Now set the upper and lower wallpaper targets correctly,
+                        // Set the upper and lower wallpaper targets correctly,
                         // and make sure that we are positioning the wallpaper below the lower.
-                        else if (wallpaperTargetIndex > oldI) {
+                        if (wallpaperTargetIndex > oldI) {
                             // The new target is on top of the old one.
                             if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
                                     "Found target above old target.");
                             mUpperWallpaperTarget = wallpaperTarget;
                             mLowerWallpaperTarget = oldW;
+
                             wallpaperTarget = oldW;
                             wallpaperTargetIndex = oldI;
                         } else {
@@ -605,6 +613,22 @@
                             mUpperWallpaperTarget = oldW;
                             mLowerWallpaperTarget = wallpaperTarget;
                         }
+                        if (newTargetHidden && !oldTargetHidden) {
+                            if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
+                                    "Old wallpaper still the target.");
+                            // Use the old target if new target is hidden but old target
+                            // is not. If they're both hidden, still use the new target.
+                            mWallpaperTarget = oldW;
+                        } else if (newTargetHidden == oldTargetHidden
+                                && !mService.mOpeningApps.contains(wallpaperTarget.mAppToken)
+                                    && (mService.mOpeningApps.contains(oldW.mAppToken)
+                                    || mService.mClosingApps.contains(oldW.mAppToken))) {
+                            // If they're both hidden (or both not hidden), prefer the one that's
+                            // currently in opening or closing app list, this allows transition
+                            // selection logic to better determine the wallpaper status of
+                            // opening/closing apps.
+                            mWallpaperTarget = oldW;
+                        }
                     }
                 }
             }
@@ -733,14 +757,16 @@
                 }
 
                 // Now stick it in. For apps over wallpaper keep the wallpaper at the bottommost
-                // layer. For keyguard over wallpaper put the wallpaper under the keyguard.
+                // layer. For keyguard over wallpaper put the wallpaper under the lowest window that
+                // is currently on screen, i.e. not hidden by policy.
                 int insertionIndex = 0;
                 if (visible && wallpaperTarget != null) {
                     final int type = wallpaperTarget.mAttrs.type;
                     final int privateFlags = wallpaperTarget.mAttrs.privateFlags;
                     if ((privateFlags & PRIVATE_FLAG_KEYGUARD) != 0
                             || type == TYPE_KEYGUARD_SCRIM) {
-                        insertionIndex = windows.indexOf(wallpaperTarget);
+                        insertionIndex = Math.min(windows.indexOf(wallpaperTarget),
+                                findLowestWindowOnScreen(windows));
                     }
                 }
                 if (DEBUG_WALLPAPER_LIGHT || DEBUG_WINDOW_MOVEMENT
@@ -757,6 +783,21 @@
         return changed;
     }
 
+    /**
+     * @return The index in {@param windows} of the lowest window that is currently on screen and
+     *         not hidden by the policy.
+     */
+    private int findLowestWindowOnScreen(WindowList windows) {
+        final int size = windows.size();
+        for (int index = 0; index < size; index++) {
+            final WindowState win = windows.get(index);
+            if (win.isOnScreen()) {
+                return index;
+            }
+        }
+        return Integer.MAX_VALUE;
+    }
+
     boolean adjustWallpaperWindows() {
         mService.mWindowPlacerLocked.mWallpaperMayChange = false;
 
diff --git a/services/core/java/com/android/server/wm/WindowAnimator.java b/services/core/java/com/android/server/wm/WindowAnimator.java
index be060d2..1ee5a22 100644
--- a/services/core/java/com/android/server/wm/WindowAnimator.java
+++ b/services/core/java/com/android/server/wm/WindowAnimator.java
@@ -16,6 +16,7 @@
 
 package com.android.server.wm;
 
+import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
 import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
 import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
@@ -118,6 +119,11 @@
     // check if some got replaced and can be removed.
     private boolean mRemoveReplacedWindows = false;
 
+    private final AppTokenList mTmpExitingAppTokens = new AppTokenList();
+
+    /** The window that was previously hiding the Keyguard. */
+    private WindowState mLastShowWinWhenLocked;
+
     private String forceHidingToString() {
         switch (mForceHiding) {
             case KEYGUARD_NOT_SHOWN:    return "KEYGUARD_NOT_SHOWN";
@@ -188,10 +194,19 @@
                 }
             }
 
-            final AppTokenList exitingAppTokens = stack.mExitingAppTokens;
-            final int exitingCount = exitingAppTokens.size();
+            mTmpExitingAppTokens.clear();
+            mTmpExitingAppTokens.addAll(stack.mExitingAppTokens);
+
+            final int exitingCount = mTmpExitingAppTokens.size();
             for (int i = 0; i < exitingCount; i++) {
-                final AppWindowAnimator appAnimator = exitingAppTokens.get(i).mAppAnimator;
+                final AppWindowAnimator appAnimator = mTmpExitingAppTokens.get(i).mAppAnimator;
+                // stepAnimation can trigger finishExit->removeWindowInnerLocked
+                // ->performSurfacePlacement
+                // performSurfacePlacement will directly manipulate the mExitingAppTokens list
+                // so we need to iterate over a copy and check for modifications.
+                if (!stack.mExitingAppTokens.contains(appAnimator)) {
+                    continue;
+                }
                 appAnimator.wasAnimating = appAnimator.animating;
                 if (appAnimator.stepAnimationLocked(mCurrentTime, displayId)) {
                     setAnimating(true);
@@ -209,13 +224,30 @@
         }
     }
 
+    /**
+     * @return The window that is currently hiding the Keyguard, or if it was hiding the Keyguard,
+     *         and it's still animating.
+     */
+    private WindowState getWinShowWhenLockedOrAnimating() {
+        final WindowState winShowWhenLocked = (WindowState) mPolicy.getWinShowWhenLockedLw();
+        if (winShowWhenLocked != null) {
+            return winShowWhenLocked;
+        }
+        if (mLastShowWinWhenLocked != null && mLastShowWinWhenLocked.isOnScreen()
+                && mLastShowWinWhenLocked.isAnimatingLw()
+                && (mLastShowWinWhenLocked.mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0) {
+            return mLastShowWinWhenLocked;
+        }
+        return null;
+    }
+
     private boolean shouldForceHide(WindowState win) {
         final WindowState imeTarget = mService.mInputMethodTarget;
         final boolean showImeOverKeyguard = imeTarget != null && imeTarget.isVisibleNow() &&
                 ((imeTarget.getAttrs().flags & FLAG_SHOW_WHEN_LOCKED) != 0
                         || !mPolicy.canBeForceHidden(imeTarget, imeTarget.mAttrs));
 
-        final WindowState winShowWhenLocked = (WindowState) mPolicy.getWinShowWhenLockedLw();
+        final WindowState winShowWhenLocked = getWinShowWhenLockedOrAnimating();
         final AppWindowToken appShowWhenLocked = winShowWhenLocked == null ?
                 null : winShowWhenLocked.mAppToken;
 
@@ -229,10 +261,16 @@
             allowWhenLocked |= appShowWhenLocked == win.mAppToken
                     // Show all SHOW_WHEN_LOCKED windows if some apps are shown over lockscreen
                     || (win.mAttrs.flags & FLAG_SHOW_WHEN_LOCKED) != 0
-                    // Show error dialogs over apps that dismiss keyguard.
+                    // Show error dialogs over apps that are shown on lockscreen
                     || (win.mAttrs.privateFlags & PRIVATE_FLAG_SYSTEM_ERROR) != 0;
         }
 
+        // Allow showing a window that dismisses Keyguard if the policy allows it. This is used for
+        // when the policy knows that the Keyguard can be dismissed without user interaction to
+        // provide a smooth transition in that case.
+        allowWhenLocked |= (win.mAttrs.flags & FLAG_DISMISS_KEYGUARD) != 0
+                && mPolicy.canShowDismissingWindowWhileLockedLw();
+
         // Only hide windows if the keyguard is active and not animating away.
         boolean keyguardOn = mPolicy.isKeyguardShowingOrOccluded()
                 && mForceHiding != KEYGUARD_ANIMATING_OUT;
@@ -537,6 +575,11 @@
                 mPostKeyguardExitAnimation = null;
             }
         }
+
+        final WindowState winShowWhenLocked = (WindowState) mPolicy.getWinShowWhenLockedLw();
+        if (winShowWhenLocked != null) {
+            mLastShowWinWhenLocked = winShowWhenLocked;
+        }
     }
 
     private void updateWallpaperLocked(int displayId) {
diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java
index 3687512..7a73759 100644
--- a/services/core/java/com/android/server/wm/WindowManagerService.java
+++ b/services/core/java/com/android/server/wm/WindowManagerService.java
@@ -21,6 +21,7 @@
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.app.ActivityManager;
 import android.app.ActivityManagerInternal;
 import android.app.ActivityManagerNative;
 import android.app.AppOpsManager;
@@ -35,14 +36,17 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.res.CompatibilityInfo;
 import android.content.res.Configuration;
 import android.database.ContentObserver;
 import android.graphics.Bitmap;
 import android.graphics.PixelFormat;
+import android.graphics.Matrix;
 import android.graphics.Point;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.graphics.Region;
 import android.hardware.display.DisplayManager;
 import android.hardware.display.DisplayManagerInternal;
@@ -189,6 +193,7 @@
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
 import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
+import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
 import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
 import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
 import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
@@ -198,6 +203,7 @@
 import static android.view.WindowManager.LayoutParams.TYPE_PRIVATE_PRESENTATION;
 import static android.view.WindowManager.LayoutParams.TYPE_QS_DIALOG;
 import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
+import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
 import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
 import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
 import static android.view.WindowManagerGlobal.RELAYOUT_DEFER_SURFACE_DESTROY;
@@ -246,6 +252,7 @@
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
 import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
+import static com.android.server.wm.WindowStateAnimator.STACK_CLIP_NONE;
 
 /** {@hide} */
 public class WindowManagerService extends IWindowManager.Stub
@@ -291,6 +298,9 @@
     /** Amount of time (in milliseconds) to delay before declaring a window freeze timeout. */
     static final int WINDOW_FREEZE_TIMEOUT_DURATION = 2000;
 
+    /** Amount of time (in milliseconds) to delay before declaring a seamless rotation timeout. */
+    static final int SEAMLESS_ROTATION_TIMEOUT_DURATION = 2000;
+
     /** Amount of time (in milliseconds) to delay before declaring a window replacement timeout. */
     static final int WINDOW_REPLACEMENT_TIMEOUT_DURATION = 2000;
 
@@ -516,6 +526,9 @@
     final Rect mTmpRect = new Rect();
     final Rect mTmpRect2 = new Rect();
     final Rect mTmpRect3 = new Rect();
+    final RectF mTmpRectF = new RectF();
+
+    final Matrix mTmpTransform = new Matrix();
 
     boolean mDisplayReady;
     boolean mSafeMode;
@@ -537,7 +550,7 @@
     SparseArray<DisplayContent> mDisplayContents = new SparseArray<>(2);
 
     int mRotation = 0;
-    int mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
+    int mLastOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
     boolean mAltOrientation = false;
 
     private boolean mKeyguardWaitingForActivityDrawn;
@@ -641,6 +654,12 @@
     WindowManagerInternal.OnHardKeyboardStatusChangeListener mHardKeyboardStatusChangeListener;
     SettingsObserver mSettingsObserver;
 
+    // A count of the windows which are 'seamlessly rotated', e.g. a surface
+    // at an old orientation is being transformed. We freeze orientation updates
+    // while any windows are seamlessly rotated, so we need to track when this
+    // hits zero so we can apply deferred orientation updates.
+    int mSeamlessRotationCount = 0;
+
     private final class SettingsObserver extends ContentObserver {
         private final Uri mDisplayInversionEnabledUri =
                 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
@@ -1859,6 +1878,7 @@
         boolean reportNewConfig = false;
         WindowState attachedWindow = null;
         long origId;
+        final int callingUid = Binder.getCallingUid();
         final int type = attrs.type;
 
         synchronized(mWindowMap) {
@@ -1906,6 +1926,8 @@
             boolean addToken = false;
             WindowToken token = mTokenMap.get(attrs.token);
             AppWindowToken atoken = null;
+            boolean addToastWindowRequiresToken = false;
+
             if (token == null) {
                 if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
                     Slog.w(TAG_WM, "Attempted to add application window with unknown token "
@@ -1942,6 +1964,15 @@
                             + attrs.token + ".  Aborting.");
                     return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
                 }
+                if (type == TYPE_TOAST) {
+                    // Apps targeting SDK above N MR1 cannot arbitrary add toast windows.
+                    if (doesAddToastWindowRequireToken(attrs.packageName, callingUid,
+                            attachedWindow)) {
+                        Slog.w(TAG_WM, "Attempted to add a toast window with unknown token "
+                                + attrs.token + ".  Aborting.");
+                        return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
+                    }
+                }
                 token = new WindowToken(this, attrs.token, -1, false);
                 addToken = true;
             } else if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
@@ -1991,6 +2022,15 @@
                             + attrs.token + ".  Aborting.");
                     return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
                 }
+            } else if (type == TYPE_TOAST) {
+                // Apps targeting SDK above N MR1 cannot arbitrary add toast windows.
+                addToastWindowRequiresToken = doesAddToastWindowRequireToken(attrs.packageName,
+                        callingUid, attachedWindow);
+                if (addToastWindowRequiresToken && token.windowType != TYPE_TOAST) {
+                    Slog.w(TAG_WM, "Attempted to add a toast window with bad token "
+                            + attrs.token + ".  Aborting.");
+                    return WindowManagerGlobal.ADD_BAD_APP_TOKEN;
+                }
             } else if (type == TYPE_QS_DIALOG) {
                 if (token.windowType != TYPE_QS_DIALOG) {
                     Slog.w(TAG_WM, "Attempted to add QS dialog window with bad token "
@@ -2035,6 +2075,36 @@
                 win.openInputChannel(outInputChannel);
             }
 
+            // If adding a toast requires a token for this app we always schedule hiding
+            // toast windows to make sure they don't stick around longer then necessary.
+            // We hide instead of remove such windows as apps aren't prepared to handle
+            // windows being removed under them.
+            //
+            // If the app is older it can add toasts without a token and hence overlay
+            // other apps. To be maximally compatible with these apps we will hide the
+            // window after the toast timeout only if the focused window is from another
+            // UID, otherwise we allow unlimited duration. When a UID looses focus we
+            // schedule hiding all of its toast windows.
+            if (type == TYPE_TOAST) {
+                if (!getDefaultDisplayContentLocked().canAddToastWindowForUid(callingUid)) {
+                    Slog.w(TAG_WM, "Adding more than one toast window for UID at a time.");
+                    return WindowManagerGlobal.ADD_DUPLICATE_ADD;
+                }
+                // Make sure this happens before we moved focus as one can make the
+                // toast focusable to force it not being hidden after the timeout.
+                // Focusable toasts are always timed out to prevent a focused app to
+                // show a focusable toasts while it has focus which will be kept on
+                // the screen after the activity goes away.
+                if (addToastWindowRequiresToken
+                        || (attrs.flags & LayoutParams.FLAG_NOT_FOCUSABLE) == 0
+                        || mCurrentFocus == null
+                        || mCurrentFocus.mOwnerUid != callingUid) {
+                    mH.sendMessageDelayed(
+                            mH.obtainMessage(H.WINDOW_HIDE_TIMEOUT, win),
+                            win.mAttrs.hideTimeoutMilliseconds);
+                }
+            }
+
             // From now on, no exceptions or errors allowed!
 
             res = WindowManagerGlobal.ADD_OKAY;
@@ -2173,11 +2243,6 @@
             if (win.isVisibleOrAdding() && updateOrientationFromAppTokensLocked(false)) {
                 reportNewConfig = true;
             }
-            if (attrs.removeTimeoutMilliseconds > 0) {
-                mH.sendMessageDelayed(
-                        mH.obtainMessage(H.WINDOW_REMOVE_TIMEOUT, win),
-                        attrs.removeTimeoutMilliseconds);
-            }
         }
 
         if (reportNewConfig) {
@@ -2189,6 +2254,32 @@
         return res;
     }
 
+    private boolean doesAddToastWindowRequireToken(String packageName, int callingUid,
+            WindowState attachedWindow) {
+        // Try using the target SDK of the root window
+        if (attachedWindow != null) {
+            return attachedWindow.mAppToken != null
+                    && attachedWindow.mAppToken.targetSdk > Build.VERSION_CODES.N_MR1;
+        } else {
+            // Otherwise, look at the package
+            try {
+                ApplicationInfo appInfo = mContext.getPackageManager()
+                        .getApplicationInfoAsUser(packageName, 0,
+                                UserHandle.getUserId(callingUid));
+                if (appInfo.uid != callingUid) {
+                    throw new SecurityException("Package " + packageName + " not in UID "
+                            + callingUid);
+                }
+                if (appInfo.targetSdkVersion > Build.VERSION_CODES.N_MR1) {
+                    return true;
+                }
+            } catch (PackageManager.NameNotFoundException e) {
+                /* ignore */
+            }
+        }
+        return false;
+    }
+
     /**
      * Returns true if we're done setting up any transitions.
      */
@@ -2899,9 +2990,22 @@
                     }
                     result |= RELAYOUT_RES_SURFACE_CHANGED;
                 }
+                if (viewVisibility == View.VISIBLE && winAnimator.hasSurface()) {
+                    // We already told the client to go invisible, but the message may not be
+                    // handled yet, or it might want to draw a last frame. If we already have a
+                    // surface, let the client use that, but don't create new surface at this point.
+                    winAnimator.mSurfaceController.getSurface(outSurface);
+                } else {
+                    if (DEBUG_VISIBILITY) Slog.i(TAG_WM, "Releasing surface in: " + win);
 
-                outSurface.release();
-                if (DEBUG_VISIBILITY) Slog.i(TAG_WM, "Releasing surface in: " + win);
+                    try {
+                        Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "wmReleaseOutSurface_"
+                                + win.mAttrs.getTitle());
+                        outSurface.release();
+                    } finally {
+                        Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
+                    }
+                }
             }
 
             if (focusMayChange) {
@@ -3036,6 +3140,7 @@
         } else {
             // For some reason there isn't a surface.  Clear the
             // caller's object so they see the same state.
+            Slog.w(TAG_WM, "Failed to create surface control for " + win);
             outSurface.release();
         }
         return result;
@@ -3058,6 +3163,9 @@
         if (oldVisibility == View.GONE) {
             winAnimator.mEnterAnimationPending = true;
         }
+
+        win.mLastVisibleLayoutRotation = mRotation;
+
         winAnimator.mEnteringAnimation = true;
         if ((result & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) {
             win.prepareWindowToDisplayDuringRelayout(outConfig);
@@ -3161,6 +3269,7 @@
         // frozen, there is no reason to animate and it can cause strange
         // artifacts when we unfreeze the display if some different animation
         // is running.
+        Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "WM#applyAnimationLocked");
         if (okToDisplay()) {
             DisplayInfo displayInfo = getDefaultDisplayInfoLocked();
             final int width = displayInfo.appWidth;
@@ -3212,6 +3321,7 @@
         } else {
             atoken.mAppAnimator.clearAnimation();
         }
+        Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
 
         return atoken.mAppAnimator.animation != null;
     }
@@ -3420,7 +3530,7 @@
             int requestedOrientation, boolean fullscreen, boolean showForAllUsers, int userId,
             int configChanges, boolean voiceInteraction, boolean launchTaskBehind,
             Rect taskBounds, Configuration config, int taskResizeMode, boolean alwaysFocusable,
-            boolean homeTask, int targetSdkVersion) {
+            boolean homeTask, int targetSdkVersion, int rotationAnimationHint) {
         if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
                 "addAppToken()")) {
             throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
@@ -3458,6 +3568,7 @@
             atoken.mAlwaysFocusable = alwaysFocusable;
             if (DEBUG_TOKEN_MOVEMENT || DEBUG_ADD_REMOVE) Slog.v(TAG_WM, "addAppToken: " + atoken
                     + " to stack=" + stackId + " task=" + taskId + " at " + addPos);
+            atoken.mRotationAnimationHint = rotationAnimationHint;
 
             Task task = mTaskIdToTask.get(taskId);
             if (task == null) {
@@ -3510,6 +3621,16 @@
                 // can re-appear and inflict its own orientation on us.  Keep the
                 // orientation stable until this all settles down.
                 return mLastWindowForcedOrientation;
+            } else if (mPolicy.isKeyguardLocked()) {
+                // Use the last orientation the while the display is frozen with the
+                // keyguard locked. This could be the keyguard forced orientation or
+                // from a SHOW_WHEN_LOCKED window. We don't want to check the show when
+                // locked window directly though as things aren't stable while
+                // the display is frozen, for example the window could be momentarily unavailable
+                // due to activity relaunch.
+                if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display is frozen while keyguard locked, "
+                        + "return " + mLastOrientation);
+                return mLastOrientation;
             }
         } else {
             // TODO(multidisplay): Change to the correct display.
@@ -3639,12 +3760,12 @@
             }
         }
         if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
-                "No app is requesting an orientation, return " + mForcedAppOrientation);
+                "No app is requesting an orientation, return " + mLastOrientation);
         // The next app has not been requested to be visible, so we keep the current orientation
         // to prevent freezing/unfreezing the display too early unless we are in multi-window, in
         // which we don't let the app customize the orientation unless it was the home task that
         // is handled above.
-        return inMultiWindow ? SCREEN_ORIENTATION_UNSPECIFIED : mForcedAppOrientation;
+        return inMultiWindow ? SCREEN_ORIENTATION_UNSPECIFIED : mLastOrientation;
     }
 
     @Override
@@ -3675,7 +3796,11 @@
         Configuration config = null;
 
         if (updateOrientationFromAppTokensLocked(false)) {
-            if (freezeThisOneIfNeeded != null) {
+            // If we changed the orientation but mOrientationChangeComplete is
+            // already true, we used seamless rotation, and we don't need
+            // to freeze the screen.
+            if (freezeThisOneIfNeeded != null &&
+                    !mWindowPlacerLocked.mOrientationChangeComplete) {
                 AppWindowToken atoken = findAppWindowToken(freezeThisOneIfNeeded);
                 if (atoken != null) {
                     startAppFreezingScreenLocked(atoken);
@@ -3727,8 +3852,8 @@
         long ident = Binder.clearCallingIdentity();
         try {
             int req = getOrientationLocked();
-            if (req != mForcedAppOrientation) {
-                mForcedAppOrientation = req;
+            if (req != mLastOrientation) {
+                mLastOrientation = req;
                 //send a message to Policy indicating orientation change to take
                 //action like disabling/enabling sensors etc.,
                 mPolicy.setCurrentOrientationLw(req);
@@ -3744,6 +3869,19 @@
         }
     }
 
+    // If this is true we have updated our desired orientation, but not yet
+    // changed the real orientation our applied our screen rotation animation.
+    // For example, because a previous screen rotation was in progress.
+    boolean rotationNeedsUpdateLocked() {
+        int rotation = mPolicy.rotationForOrientationLw(mLastOrientation, mRotation);
+        boolean altOrientation = !mPolicy.rotationHasCompatibleMetricsLw(
+                mLastOrientation, rotation);
+        if (mRotation == rotation && mAltOrientation == altOrientation) {
+            return false;
+        }
+        return true;
+    }
+
     @Override
     public int[] setNewConfiguration(Configuration config) {
         if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
@@ -4440,7 +4578,25 @@
     }
 
     @Override
-    public void notifyAppStopped(IBinder token, boolean stopped) {
+    public void notifyAppResumed(IBinder token, boolean wasStopped, boolean allowSavedSurface) {
+        if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
+                "notifyAppResumed()")) {
+            throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
+        }
+
+        synchronized(mWindowMap) {
+            final AppWindowToken wtoken;
+            wtoken = findAppWindowToken(token);
+            if (wtoken == null) {
+                Slog.w(TAG_WM, "Attempted to notify resumed of non-existing app token: " + token);
+                return;
+            }
+            wtoken.notifyAppResumed(wasStopped, allowSavedSurface);
+        }
+    }
+
+    @Override
+    public void notifyAppStopped(IBinder token) {
         if (!checkCallingPermission(android.Manifest.permission.MANAGE_APP_TOKENS,
                 "notifyAppStopped()")) {
             throw new SecurityException("Requires MANAGE_APP_TOKENS permission");
@@ -4450,10 +4606,10 @@
             final AppWindowToken wtoken;
             wtoken = findAppWindowToken(token);
             if (wtoken == null) {
-                Slog.w(TAG_WM, "Attempted to set visibility of non-existing app token: " + token);
+                Slog.w(TAG_WM, "Attempted to notify stopped of non-existing app token: " + token);
                 return;
             }
-            wtoken.notifyAppStopped(stopped);
+            wtoken.notifyAppStopped();
         }
     }
 
@@ -4990,18 +5146,32 @@
         try {
             synchronized (mWindowMap) {
                 final DisplayContent displayContent = mDisplayContents.get(displayId);
+                boolean attachedToDisplay = false;
                 if (displayContent != null) {
                     TaskStack stack = mStackIdToStack.get(stackId);
                     if (stack == null) {
                         if (DEBUG_STACK) Slog.d(TAG_WM, "attachStack: stackId=" + stackId);
-                        stack = new TaskStack(this, stackId);
+
+                        stack = displayContent.getStackById(stackId);
+                        if (stack != null) {
+                            // It's already attached to the display. Detach and re-attach
+                            // because onTop might change, and be sure to clear mDeferDetach!
+                            displayContent.detachStack(stack);
+                            stack.mDeferDetach = false;
+                            attachedToDisplay = true;
+                        } else {
+                            stack = new TaskStack(this, stackId);
+                        }
+
                         mStackIdToStack.put(stackId, stack);
                         if (stackId == DOCKED_STACK_ID) {
                             getDefaultDisplayContentLocked().mDividerControllerLocked
                                     .notifyDockedStackExistsChanged(true);
                         }
                     }
-                    stack.attachDisplayContent(displayContent);
+                    if (!attachedToDisplay) {
+                        stack.attachDisplayContent(displayContent);
+                    }
                     displayContent.attachStack(stack, onTop);
                     if (stack.getRawFullscreen()) {
                         return null;
@@ -5140,6 +5310,11 @@
         }
     }
 
+    @Override
+    public void overridePlayingAppAnimationsLw(Animation a) {
+        getDefaultDisplayContentLocked().overridePlayingAppAnimationsLw(a);
+    }
+
     /**
      * Re-sizes a stack and its containing tasks.
      * @param stackId Id of stack to resize.
@@ -5652,6 +5827,12 @@
 
     // Called by window manager policy.  Not exposed externally.
     @Override
+    public void reboot(boolean confirm) {
+        ShutdownThread.reboot(mContext, PowerManager.SHUTDOWN_USER_REQUESTED, confirm);
+    }
+
+    // Called by window manager policy.  Not exposed externally.
+    @Override
     public void rebootSafeMode(boolean confirm) {
         ShutdownThread.rebootSafeMode(mContext, confirm);
     }
@@ -5809,7 +5990,8 @@
             if (w.isDrawnLw()) {
                 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
                     haveBootMsg = true;
-                } else if (w.mAttrs.type == TYPE_APPLICATION) {
+                } else if (w.mAttrs.type == TYPE_APPLICATION
+                        || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
                     haveApp = true;
                 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
                     haveWallpaper = true;
@@ -6141,6 +6323,21 @@
         }
     }
 
+    @Override
+    public Bitmap screenshotWallpaper() {
+        if (!checkCallingPermission(Manifest.permission.READ_FRAME_BUFFER,
+                "screenshotWallpaper()")) {
+            throw new SecurityException("Requires READ_FRAME_BUFFER permission");
+        }
+        try {
+            Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "screenshotWallpaper");
+            return screenshotApplicationsInner(null, Display.DEFAULT_DISPLAY, -1, -1, true, 1f,
+                    Bitmap.Config.ARGB_8888, true);
+        } finally {
+            Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
+        }
+    }
+
     /**
      * Takes a snapshot of the screen.  In landscape mode this grabs the whole screen.
      * In portrait mode, it grabs the upper region of the screen based on the vertical dimension
@@ -6157,7 +6354,7 @@
             @Override
             public void run() {
                 Bitmap bm = screenshotApplicationsInner(null, Display.DEFAULT_DISPLAY, -1, -1,
-                        true, 1f, Bitmap.Config.ARGB_8888);
+                        true, 1f, Bitmap.Config.ARGB_8888, false);
                 try {
                     receiver.send(bm);
                 } catch (RemoteException e) {
@@ -6187,14 +6384,27 @@
         try {
             Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "screenshotApplications");
             return screenshotApplicationsInner(appToken, displayId, width, height, false,
-                    frameScale, Bitmap.Config.RGB_565);
+                    frameScale, Bitmap.Config.RGB_565, false);
         } finally {
             Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
         }
     }
 
+    /**
+     * Takes a snapshot of the screen.  In landscape mode this grabs the whole screen.
+     * In portrait mode, it grabs the full screenshot.
+     *
+     * @param displayId the Display to take a screenshot of.
+     * @param width the width of the target bitmap
+     * @param height the height of the target bitmap
+     * @param includeFullDisplay true if the screen should not be cropped before capture
+     * @param frameScale the scale to apply to the frame, only used when width = -1 and height = -1
+     * @param config of the output bitmap
+     * @param wallpaperOnly true if only the wallpaper layer should be included in the screenshot
+     */
     Bitmap screenshotApplicationsInner(IBinder appToken, int displayId, int width, int height,
-            boolean includeFullDisplay, float frameScale, Bitmap.Config config) {
+            boolean includeFullDisplay, float frameScale, Bitmap.Config config,
+            boolean wallpaperOnly) {
         final DisplayContent displayContent;
         synchronized(mWindowMap) {
             displayContent = getDisplayContentLocked(displayId);
@@ -6221,7 +6431,7 @@
 
         boolean screenshotReady;
         int minLayer;
-        if (appToken == null) {
+        if (appToken == null && !wallpaperOnly) {
             screenshotReady = true;
             minLayer = 0;
         } else {
@@ -6261,11 +6471,20 @@
                 if (ws.mLayer >= aboveAppLayer) {
                     continue;
                 }
+                if (wallpaperOnly && !ws.mIsWallpaper) {
+                    continue;
+                }
                 if (ws.mIsImWindow) {
                     if (!includeImeInScreenshot) {
                         continue;
                     }
                 } else if (ws.mIsWallpaper) {
+                    // If this is the wallpaper layer and we're only looking for the wallpaper layer
+                    // then the target window state is this one.
+                    if (wallpaperOnly) {
+                        appWin = ws;
+                    }
+
                     if (appWin == null) {
                         // We have not ran across the target window yet, so it is probably
                         // behind the wallpaper. This can happen when the keyguard is up and
@@ -6313,8 +6532,10 @@
                     }
                 }
 
-                if (ws.mAppToken != null && ws.mAppToken.token == appToken &&
-                        ws.isDisplayedLw() && winAnim.getShown()) {
+                final boolean foundTargetWs =
+                        (ws.mAppToken != null && ws.mAppToken.token == appToken)
+                        || (appWin != null && wallpaperOnly);
+                if (foundTargetWs && ws.isDisplayedLw() && winAnim.getShown()) {
                     screenshotReady = true;
                 }
 
@@ -6567,6 +6788,7 @@
         Binder.restoreCallingIdentity(origId);
     }
 
+
     // TODO(multidisplay): Rotate any display?
     /**
      * Updates the current rotation.
@@ -6591,6 +6813,13 @@
             if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
             return false;
         }
+        if (mDisplayFrozen) {
+            // Even if the screen rotation animation has finished (e.g. isAnimating
+            // returns false), there is still some time where we haven't yet unfrozen
+            // the display. We also need to abort rotation here.
+            if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, still finishing previous rotation");
+            return false;
+        }
 
         if (!mDisplayEnabled) {
             // No point choosing a rotation if the display is not enabled.
@@ -6598,32 +6827,64 @@
             return false;
         }
 
+        final DisplayContent displayContent = getDefaultDisplayContentLocked();
+        final WindowList windows = displayContent.getWindowList();
+
+        final int oldRotation = mRotation;
+        boolean rotateSeamlessly = mPolicy.shouldRotateSeamlessly(oldRotation, mRotation);
+
+        if (rotateSeamlessly) {
+            for (int i = windows.size() - 1; i >= 0; i--) {
+                WindowState w = windows.get(i);
+                // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
+                // to complete (that is, waiting for windows to redraw). It's tempting to check
+                // w.mSeamlessRotationCount but that could be incorrect in the case of window-removal.
+                if (w.mSeamlesslyRotated) {
+                    return false;
+                }
+                // In what can only be called an unfortunate workaround we require
+                // seamlessly rotated child windows to have the TRANSFORM_TO_DISPLAY_INVERSE
+                // flag. Due to limitations in the client API, there is no way for
+                // the client to set this flag in a race free fashion. If we seamlessly rotate
+                // a window which does not have this flag, but then gains it, we will get
+                // an incorrect visual result (rotated viewfinder). This means if we want to
+                // support seamlessly rotating windows which could gain this flag, we can't
+                // rotate windows without it. This limits seamless rotation in N to camera framework
+                // users, windows without children, and native code. This is unfortunate but
+                // having the camera work is our primary goal.
+                if (w.isChildWindow() & w.isVisibleNow() &&
+                        !w.mWinAnimator.mSurfaceController.getTransformToDisplayInverse()) {
+                    rotateSeamlessly = false;
+                }
+            }
+        }
+
         // TODO: Implement forced rotation changes.
         //       Set mAltOrientation to indicate that the application is receiving
         //       an orientation that has different metrics than it expected.
         //       eg. Portrait instead of Landscape.
 
-        int rotation = mPolicy.rotationForOrientationLw(mForcedAppOrientation, mRotation);
+        int rotation = mPolicy.rotationForOrientationLw(mLastOrientation, mRotation);
         boolean altOrientation = !mPolicy.rotationHasCompatibleMetricsLw(
-                mForcedAppOrientation, rotation);
+                mLastOrientation, rotation);
 
         if (DEBUG_ORIENTATION) {
-            Slog.v(TAG_WM, "Application requested orientation "
-                    + mForcedAppOrientation + ", got rotation " + rotation
+            Slog.v(TAG_WM, "Selected orientation "
+                    + mLastOrientation + ", got rotation " + rotation
                     + " which has " + (altOrientation ? "incompatible" : "compatible")
                     + " metrics");
         }
 
         if (mRotation == rotation && mAltOrientation == altOrientation) {
             // No change.
-            return false;
+             return false;
         }
 
         if (DEBUG_ORIENTATION) {
             Slog.v(TAG_WM,
                 "Rotation changed to " + rotation + (altOrientation ? " (alt)" : "")
                 + " from " + mRotation + (mAltOrientation ? " (alt)" : "")
-                + ", forceApp=" + mForcedAppOrientation);
+                + ", lastOrientation=" + mLastOrientation);
         }
 
         mRotation = rotation;
@@ -6634,7 +6895,6 @@
         mH.removeMessages(H.WINDOW_FREEZE_TIMEOUT);
         mH.sendEmptyMessageDelayed(H.WINDOW_FREEZE_TIMEOUT, WINDOW_FREEZE_TIMEOUT_DURATION);
         mWaitingForConfig = true;
-        final DisplayContent displayContent = getDefaultDisplayContentLocked();
         displayContent.layoutNeeded = true;
         final int[] anim = new int[2];
         if (displayContent.isDimming()) {
@@ -6642,10 +6902,23 @@
         } else {
             mPolicy.selectRotationAnimationLw(anim);
         }
-        startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
-        // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
-        screenRotationAnimation =
+
+        if (!rotateSeamlessly) {
+            startFreezingDisplayLocked(inTransaction, anim[0], anim[1]);
+            // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
+            screenRotationAnimation =
                 mAnimator.getScreenRotationAnimationLocked(Display.DEFAULT_DISPLAY);
+        } else {
+            // The screen rotation animation uses a screenshot to freeze the screen
+            // while windows resize underneath.
+            // When we are rotating seamlessly, we allow the elements to transition
+            // to their rotated state independently and without a freeze required.
+            screenRotationAnimation = null;
+
+            // We have to reset this in case a window was removed before it
+            // finished seamless rotation.
+            mSeamlessRotationCount = 0;
+        }
 
         // We need to update our screen size information to match the new rotation. If the rotation
         // has actually changed then this method will return true and, according to the comment at
@@ -6674,6 +6947,13 @@
                 }
             }
 
+            if (rotateSeamlessly) {
+                for (int i = windows.size() - 1; i >= 0; i--) {
+                    WindowState w = windows.get(i);
+                    w.mWinAnimator.seamlesslyRotateWindow(oldRotation, mRotation);
+                }
+            }
+
             mDisplayManagerInternal.performTraversalInTransactionFromWindowManager();
         } finally {
             if (!inTransaction) {
@@ -6684,19 +6964,22 @@
             }
         }
 
-        final WindowList windows = displayContent.getWindowList();
         for (int i = windows.size() - 1; i >= 0; i--) {
             WindowState w = windows.get(i);
             // Discard surface after orientation change, these can't be reused.
             if (w.mAppToken != null) {
                 w.mAppToken.destroySavedSurfaces();
             }
-            if (w.mHasSurface) {
+            if (w.mHasSurface && !rotateSeamlessly) {
                 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
                 w.mOrientationChanging = true;
                 mWindowPlacerLocked.mOrientationChangeComplete = false;
+                w.mLastFreezeDuration = 0;
             }
-            w.mLastFreezeDuration = 0;
+        }
+        if (rotateSeamlessly) {
+            mH.removeMessages(H.SEAMLESS_ROTATION_TIMEOUT);
+            mH.sendEmptyMessageDelayed(H.SEAMLESS_ROTATION_TIMEOUT, SEAMLESS_ROTATION_TIMEOUT_DURATION);
         }
 
         for (int i=mRotationWatchers.size()-1; i>=0; i--) {
@@ -6706,7 +6989,7 @@
             }
         }
 
-        //TODO (multidisplay): Magnification is supported only for the default display.
+        // TODO (multidisplay): Magnification is supported only for the default display.
         // Announce rotation only if we will not animate as we already have the
         // windows in final state. Otherwise, we make this call at the rotation end.
         if (screenRotationAnimation == null && mAccessibilityController != null
@@ -7993,9 +8276,9 @@
         public static final int NOTIFY_APP_TRANSITION_FINISHED = 49;
         public static final int NOTIFY_STARTING_WINDOW_DRAWN = 50;
         public static final int UPDATE_ANIMATION_SCALE = 51;
-        public static final int WINDOW_REMOVE_TIMEOUT = 52;
-
+        public static final int WINDOW_HIDE_TIMEOUT = 52;
         public static final int NOTIFY_DOCKED_STACK_MINIMIZED_CHANGED = 53;
+        public static final int SEAMLESS_ROTATION_TIMEOUT = 54;
 
         /**
          * Used to denote that an integer field in a message will not be used.
@@ -8613,7 +8896,7 @@
                     mAmInternal.notifyStartingWindowDrawn();
                 }
                 break;
-                case WINDOW_REMOVE_TIMEOUT: {
+                case WINDOW_HIDE_TIMEOUT: {
                     final WindowState window = (WindowState) msg.obj;
                     synchronized(mWindowMap) {
                         // TODO: This is all about fixing b/21693547
@@ -8624,8 +8907,11 @@
                         // running under debugger) to crash (b/29105388). The windows will
                         // eventually be removed when the client process finishes.
                         // The best we can do for now is remove the FLAG_KEEP_SCREEN_ON
-                        // and prevent the symptoms of b/21693547.
+                        // and prevent the symptoms of b/21693547. Since apps don't
+                        // support windows being removed under them we hide the window
+                        // and it will be removed when the app dies.
                         window.mAttrs.flags &= ~FLAG_KEEP_SCREEN_ON;
+                        window.hidePermanentlyLw();
                         window.setDisplayLayoutNeeded();
                         mWindowPlacerLocked.performSurfacePlacement();
                     }
@@ -8635,6 +8921,27 @@
                     mAmInternal.notifyDockedStackMinimizedChanged(msg.arg1 == 1);
                 }
                 break;
+                case SEAMLESS_ROTATION_TIMEOUT: {
+                    // Rotation only supported on primary display.
+                    // TODO(multi-display)
+                    synchronized(mWindowMap) {
+                        final DisplayContent displayContent = getDefaultDisplayContentLocked();
+                        final WindowList windows = displayContent.getWindowList();
+                        boolean layoutNeeded = false;
+                        for (int i = windows.size() - 1; i >= 0; i--) {
+                            WindowState w = windows.get(i);
+                            if (w.mSeamlesslyRotated) {
+                                layoutNeeded = true;
+                                w.setDisplayLayoutNeeded();
+                                markForSeamlessRotation(w, false);
+                            }
+                        }
+                        if (layoutNeeded) {
+                            mWindowPlacerLocked.performSurfacePlacement();
+                        }
+                    }
+                }
+                break;
             }
             if (DEBUG_WINDOW_TRACE) {
                 Slog.v(TAG_WM, "handleMessage: exit");
@@ -8922,7 +9229,7 @@
     }
 
     @Override
-    public void setForcedDisplayDensity(int displayId, int density) {
+    public void setForcedDisplayDensityForUser(int displayId, int density, int userId) {
         if (mContext.checkCallingOrSelfPermission(
                 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
                 PackageManager.PERMISSION_GRANTED) {
@@ -8932,16 +9239,20 @@
         if (displayId != Display.DEFAULT_DISPLAY) {
             throw new IllegalArgumentException("Can only set the default display");
         }
+
+        final int targetUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
+                Binder.getCallingUid(), userId, false, true, "setForcedDisplayDensityForUser",
+                null);
         final long ident = Binder.clearCallingIdentity();
         try {
             synchronized(mWindowMap) {
                 final DisplayContent displayContent = getDisplayContentLocked(displayId);
-                if (displayContent != null) {
+                if (displayContent != null && mCurrentUserId == targetUserId) {
                     setForcedDisplayDensityLocked(displayContent, density);
-                    Settings.Secure.putStringForUser(mContext.getContentResolver(),
-                            Settings.Secure.DISPLAY_DENSITY_FORCED,
-                            Integer.toString(density), mCurrentUserId);
                 }
+                Settings.Secure.putStringForUser(mContext.getContentResolver(),
+                        Settings.Secure.DISPLAY_DENSITY_FORCED,
+                        Integer.toString(density), targetUserId);
             }
         } finally {
             Binder.restoreCallingIdentity(ident);
@@ -8949,7 +9260,7 @@
     }
 
     @Override
-    public void clearForcedDisplayDensity(int displayId) {
+    public void clearForcedDisplayDensityForUser(int displayId, int userId) {
         if (mContext.checkCallingOrSelfPermission(
                 android.Manifest.permission.WRITE_SECURE_SETTINGS) !=
                 PackageManager.PERMISSION_GRANTED) {
@@ -8959,16 +9270,20 @@
         if (displayId != Display.DEFAULT_DISPLAY) {
             throw new IllegalArgumentException("Can only set the default display");
         }
+
+        final int callingUserId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
+                Binder.getCallingUid(), userId, false, true, "clearForcedDisplayDensityForUser",
+                null);
         final long ident = Binder.clearCallingIdentity();
         try {
             synchronized(mWindowMap) {
                 final DisplayContent displayContent = getDisplayContentLocked(displayId);
-                if (displayContent != null) {
+                if (displayContent != null && mCurrentUserId == callingUserId) {
                     setForcedDisplayDensityLocked(displayContent,
                             displayContent.mInitialDisplayDensity);
-                    Settings.Secure.putStringForUser(mContext.getContentResolver(),
-                            Settings.Secure.DISPLAY_DENSITY_FORCED, "", mCurrentUserId);
                 }
+                Settings.Secure.putStringForUser(mContext.getContentResolver(),
+                        Settings.Secure.DISPLAY_DENSITY_FORCED, "", callingUserId);
             }
         } finally {
             Binder.restoreCallingIdentity(ident);
@@ -9294,7 +9609,7 @@
             if (task != null && task.mStack.getBoundsAnimating()) {
                 return;
             }
-            w.setInsetsChanged();
+            w.setReportResizeHints();
             boolean configChanged = w.isConfigChanged();
             if (DEBUG_CONFIGURATION && configChanged) {
                 Slog.v(TAG_WM, "Win " + w + " config changed: "
@@ -9302,15 +9617,22 @@
             }
             final boolean dragResizingChanged = w.isDragResizeChanged()
                     && !w.isDragResizingChangeReported();
+
             if (localLOGV) Slog.v(TAG_WM, "Resizing " + w
                     + ": configChanged=" + configChanged
                     + " dragResizingChanged=" + dragResizingChanged
                     + " last=" + w.mLastFrame + " frame=" + w.mFrame);
+
+            // We update mLastFrame always rather than in the conditional with the
+            // last inset variables, because mFrameSizeChanged only tracks the
+            // width and height changing.
             w.mLastFrame.set(w.mFrame);
+
             if (w.mContentInsetsChanged
                     || w.mVisibleInsetsChanged
                     || winAnimator.mSurfaceResized
                     || w.mOutsetsChanged
+                    || w.mFrameSizeChanged
                     || configChanged
                     || dragResizingChanged
                     || !w.isResizedWhileNotDragResizingReported()) {
@@ -9633,6 +9955,12 @@
 
             adjustForImeIfNeeded(displayContent);
 
+            // We may need to schedule some toast windows to be removed. The
+            // toasts for an app that does not have input focus are removed
+            // within a timeout to prevent apps to redress other apps' UI.
+            getDefaultDisplayContentLocked().scheduleToastWindowsTimeoutIfNeededLocked(
+                        oldFocus, newFocus);
+
             Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
             return true;
         }
@@ -9935,6 +10263,32 @@
     }
 
     @Override
+    public void setRecentsVisibility(boolean visible) {
+        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR)
+                != PackageManager.PERMISSION_GRANTED) {
+            throw new SecurityException("Caller does not hold permission "
+                    + android.Manifest.permission.STATUS_BAR);
+        }
+
+        synchronized (mWindowMap) {
+            mPolicy.setRecentsVisibilityLw(visible);
+        }
+    }
+
+    @Override
+    public void setTvPipVisibility(boolean visible) {
+        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR)
+                != PackageManager.PERMISSION_GRANTED) {
+            throw new SecurityException("Caller does not hold permission "
+                    + android.Manifest.permission.STATUS_BAR);
+        }
+
+        synchronized (mWindowMap) {
+            mPolicy.setTvPipVisibilityLw(visible);
+        }
+    }
+
+    @Override
     public void statusBarVisibilityChanged(int visibility) {
         if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR)
                 != PackageManager.PERMISSION_GRANTED) {
@@ -10143,6 +10497,15 @@
         }
     }
 
+    public void notifyAppRelaunchesCleared(IBinder token) {
+        synchronized (mWindowMap) {
+            final AppWindowToken appWindow = findAppWindowToken(token);
+            if (appWindow != null) {
+                appWindow.clearRelaunching();
+            }
+        }
+    }
+
     @Override
     public int getDockedDividerInsetsLw() {
         return getDefaultDisplayContentLocked().getDockedDividerController().getContentInsets();
@@ -10404,7 +10767,7 @@
             pw.print("  mRotation="); pw.print(mRotation);
                     pw.print(" mAltOrientation="); pw.println(mAltOrientation);
             pw.print("  mLastWindowForcedOrientation="); pw.print(mLastWindowForcedOrientation);
-                    pw.print(" mForcedAppOrientation="); pw.println(mForcedAppOrientation);
+                    pw.print(" mLastOrientation="); pw.println(mLastOrientation);
             pw.print("  mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
             pw.print("  Animation settings: disabled="); pw.print(mAnimationsDisabled);
                     pw.print(" window="); pw.print(mWindowAnimationScaleSetting);
@@ -11157,6 +11520,26 @@
         mPolicy.registerShortcutKey(shortcutCode, shortcutKeyReceiver);
     }
 
+    void markForSeamlessRotation(WindowState w, boolean seamlesslyRotated) {
+        if (seamlesslyRotated == w.mSeamlesslyRotated) {
+            return;
+        }
+        w.mSeamlesslyRotated = seamlesslyRotated;
+        if (seamlesslyRotated) {
+            mSeamlessRotationCount++;
+        } else {
+            mSeamlessRotationCount--;
+        }
+        if (mSeamlessRotationCount == 0) {
+            if (DEBUG_ORIENTATION) {
+                Slog.i(TAG, "Performing post-rotate rotation after seamless rotation");
+            }
+            if (updateRotationUncheckedLocked(false)) {
+                mH.sendEmptyMessage(H.SEND_NEW_CONFIGURATION);
+            }
+        }
+    }
+
     private final class LocalService extends WindowManagerInternal {
         @Override
         public void requestTraversalFromDisplayManager() {
@@ -11283,8 +11666,9 @@
                 for (int winNdx = windows.size() - 1; winNdx >= 0; --winNdx) {
                     final WindowState win = windows.get(winNdx);
                     final boolean isForceHiding = mPolicy.isForceHiding(win.mAttrs);
+                    final boolean keyguard = mPolicy.isKeyguardHostWindow(win.mAttrs);
                     if (win.isVisibleLw()
-                            && (win.mAppToken != null || isForceHiding)) {
+                            && (win.mAppToken != null || isForceHiding || keyguard)) {
                         win.mWinAnimator.mDrawState = DRAW_PENDING;
                         // Force add to mResizingWindows.
                         win.mLastContentInsets.set(-1, -1, -1, -1);
diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java
index 08bfa2d..55bf394 100644
--- a/services/core/java/com/android/server/wm/WindowState.java
+++ b/services/core/java/com/android/server/wm/WindowState.java
@@ -87,6 +87,7 @@
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
 import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
 import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
+import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
 import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
 import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
@@ -165,6 +166,7 @@
     boolean mPolicyVisibility = true;
     boolean mPolicyVisibilityAfterAnim = true;
     boolean mAppOpVisibility = true;
+    boolean mPermanentlyHidden; // the window should never be shown again
     boolean mAppFreezing;
     boolean mAttachedHidden;    // is our parent window hidden?
     boolean mWallpaperVisible;  // for wallpaper, what was last vis report?
@@ -287,6 +289,7 @@
     // "Real" frame that the application sees, in display coordinate space.
     final Rect mFrame = new Rect();
     final Rect mLastFrame = new Rect();
+    boolean mFrameSizeChanged = false;
     // Frame that is scaled to the application's coordinate space when in
     // screen size compatibility mode.
     final Rect mCompatFrame = new Rect();
@@ -394,6 +397,13 @@
     boolean mOrientationChanging;
 
     /**
+     * The orientation during the last visible call to relayout. If our
+     * current orientation is different, the window can't be ready
+     * to be shown.
+     */
+    int mLastVisibleLayoutRotation = -1;
+
+    /**
      * How long we last kept the screen frozen.
      */
     int mLastFreezeDuration;
@@ -494,6 +504,15 @@
     /** @see #isResizedWhileNotDragResizingReported(). */
     private boolean mResizedWhileNotDragResizingReported;
 
+    /**
+     * During seamless rotation we have two phases, first the old window contents
+     * are rotated to look as if they didn't move in the new coordinate system. Then we
+     * have to freeze updates to this layer (to preserve the transformation) until
+     * the resize actually occurs. This is true from when the transformation is set
+     * and false until the transaction to resize is sent.
+     */
+    boolean mSeamlesslyRotated = false;
+
     WindowState(WindowManagerService service, Session s, IWindow c, WindowToken token,
            WindowState attachedWindow, int appOp, int seq, WindowManager.LayoutParams a,
            int viewVisibility, final DisplayContent displayContent) {
@@ -812,10 +831,10 @@
             final int height = Math.min(mFrame.height(), mContentFrame.height());
             final int width = Math.min(mContentFrame.width(), mFrame.width());
             final DisplayMetrics displayMetrics = getDisplayContent().getDisplayMetrics();
-            final int minVisibleHeight = WindowManagerService.dipToPixel(
-                    MINIMUM_VISIBLE_HEIGHT_IN_DP, displayMetrics);
-            final int minVisibleWidth = WindowManagerService.dipToPixel(
-                    MINIMUM_VISIBLE_WIDTH_IN_DP, displayMetrics);
+            final int minVisibleHeight = Math.min(height, WindowManagerService.dipToPixel(
+                    MINIMUM_VISIBLE_HEIGHT_IN_DP, displayMetrics));
+            final int minVisibleWidth = Math.min(width, WindowManagerService.dipToPixel(
+                    MINIMUM_VISIBLE_WIDTH_IN_DP, displayMetrics));
             final int top = Math.max(mContentFrame.top,
                     Math.min(mFrame.top, mContentFrame.bottom - minVisibleHeight));
             final int left = Math.max(mContentFrame.left + minVisibleWidth - width,
@@ -1046,14 +1065,16 @@
         return mAppToken != null && mAppToken.voiceInteraction;
     }
 
-    boolean setInsetsChanged() {
+    boolean setReportResizeHints() {
         mOverscanInsetsChanged |= !mLastOverscanInsets.equals(mOverscanInsets);
         mContentInsetsChanged |= !mLastContentInsets.equals(mContentInsets);
         mVisibleInsetsChanged |= !mLastVisibleInsets.equals(mVisibleInsets);
         mStableInsetsChanged |= !mLastStableInsets.equals(mStableInsets);
         mOutsetsChanged |= !mLastOutsets.equals(mOutsets);
+        mFrameSizeChanged |= (mLastFrame.width() != mFrame.width()) ||
+                (mLastFrame.height() != mFrame.height());
         return mOverscanInsetsChanged || mContentInsetsChanged || mVisibleInsetsChanged
-                || mOutsetsChanged;
+                || mOutsetsChanged || mFrameSizeChanged;
     }
 
     public DisplayContent getDisplayContent() {
@@ -1270,7 +1291,8 @@
         final boolean isViewVisible = (mAppToken == null || !mAppToken.clientHidden)
                 && (mViewVisibility == View.VISIBLE) && !mWindowRemovalAllowed;
         return (isOnScreenIgnoringKeyguard() && (!visibleOnly || isViewVisible)
-                || mWinAnimator.mAttrType == TYPE_BASE_APPLICATION)
+                || mWinAnimator.mAttrType == TYPE_BASE_APPLICATION
+                || mWinAnimator.mAttrType == TYPE_DRAWN_APPLICATION)
                 && !mAnimatingExit && !mDestroying;
     }
 
@@ -1315,7 +1337,8 @@
                 && ((!mAttachedHidden && mViewVisibility == View.VISIBLE && !mRootToken.hidden)
                         || mWinAnimator.mAnimation != null
                         || ((atoken != null) && (atoken.mAppAnimator.animation != null)
-                                && !mWinAnimator.isDummyAnimation()));
+                                && !mWinAnimator.isDummyAnimation())
+                        || isAnimatingWithSavedSurface());
     }
 
     /**
@@ -1395,7 +1418,7 @@
      */
     boolean hasMoved() {
         return mHasSurface && (mContentChanged || mMovedByResize)
-                && !mAnimatingExit && mService.okToDisplay()
+                && !mAnimatingExit
                 && (mFrame.top != mLastFrame.top || mFrame.left != mLastFrame.left)
                 && (mAttachedWindow == null || !mAttachedWindow.hasMoved());
     }
@@ -1854,6 +1877,11 @@
             // Being hidden due to app op request.
             return false;
         }
+        if (mPermanentlyHidden) {
+            // Permanently hidden until the app exists as apps aren't prepared
+            // to handle their windows being removed from under them.
+            return false;
+        }
         if (mPolicyVisibility && mPolicyVisibilityAfterAnim) {
             // Already showing.
             return false;
@@ -1944,6 +1972,13 @@
         }
     }
 
+    public void hidePermanentlyLw() {
+        if (!mPermanentlyHidden) {
+            mPermanentlyHidden = true;
+            hideLw(true, true);
+        }
+    }
+
     public void pokeDrawLockLw(long timeout) {
         if (isVisibleOrAdding()) {
             if (mDrawLock == null) {
@@ -2083,6 +2118,7 @@
                 Slog.v(TAG, "Destroying saved surface: " + this);
             }
             mWinAnimator.destroySurfaceLocked();
+            mSurfaceSaved = false;
         }
         mWasVisibleBeforeClientHidden = false;
     }
@@ -2091,7 +2127,18 @@
         if (!mSurfaceSaved) {
             return;
         }
+
+        // Sometimes we save surfaces due to layout invisible
+        // directly after rotation occurs. However this means
+        // the surface was never laid out in the new orientation.
+        // We can only restore to the last rotation we were
+        // laid out as visible in.
+        if (mLastVisibleLayoutRotation != mService.mRotation) {
+            destroySavedSurface();
+            return;
+        }
         mSurfaceSaved = false;
+
         if (mWinAnimator.mSurfaceController != null) {
             setHasSurface(true);
             mWinAnimator.mDrawState = WindowStateAnimator.READY_TO_SHOW;
@@ -2335,6 +2382,7 @@
             mVisibleInsetsChanged = false;
             mStableInsetsChanged = false;
             mOutsetsChanged = false;
+            mFrameSizeChanged = false;
             mResizedWhileNotDragResizingReported = true;
             mWinAnimator.mSurfaceResized = false;
         } catch (RemoteException e) {
@@ -2580,7 +2628,7 @@
             pw.println(Integer.toHexString(mSystemUiVisibility));
         }
         if (!mPolicyVisibility || !mPolicyVisibilityAfterAnim || !mAppOpVisibility
-                || mAttachedHidden) {
+                || mAttachedHidden || mPermanentlyHidden) {
             pw.print(prefix); pw.print("mPolicyVisibility=");
                     pw.print(mPolicyVisibility);
                     pw.print(" mPolicyVisibilityAfterAnim=");
@@ -2588,6 +2636,7 @@
                     pw.print(" mAppOpVisibility=");
                     pw.print(mAppOpVisibility);
                     pw.print(" mAttachedHidden="); pw.println(mAttachedHidden);
+                    pw.print(" mPermanentlyHidden="); pw.println(mPermanentlyHidden);
         }
         if (!mRelayoutCalled || mLayoutNeeded) {
             pw.print(prefix); pw.print("mRelayoutCalled="); pw.print(mRelayoutCalled);
@@ -2892,11 +2941,24 @@
     // for only child windows (as the main window is handled by window preservation)
     // and the big surface.
     //
-    // Though windows of TYPE_APPLICATION (as opposed to TYPE_BASE_APPLICATION)
-    // are not children in the sense of an attached window, we also want to replace
-    // them at such phases, as they won't be covered by window preservation,
-    // and in general we expect them to return following relaunch.
+    // Though windows of TYPE_APPLICATION or TYPE_DRAWN_APPLICATION (as opposed to
+    // TYPE_BASE_APPLICATION) are not children in the sense of an attached window,
+    // we also want to replace them at such phases, as they won't be covered by window
+    // preservation, and in general we expect them to return following relaunch.
     boolean shouldBeReplacedWithChildren() {
-        return isChildWindow() || mAttrs.type == TYPE_APPLICATION;
+        return isChildWindow() || mAttrs.type == TYPE_APPLICATION
+                || mAttrs.type == TYPE_DRAWN_APPLICATION;
+    }
+
+    public int getRotationAnimationHint() {
+        if (mAppToken != null) {
+            return mAppToken.mRotationAnimationHint;
+        } else {
+            return -1;
+        }
+    }
+
+    public boolean isRtl() {
+        return mMergedConfiguration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
     }
 }
diff --git a/services/core/java/com/android/server/wm/WindowStateAnimator.java b/services/core/java/com/android/server/wm/WindowStateAnimator.java
index afd451d..0fc6ef8 100644
--- a/services/core/java/com/android/server/wm/WindowStateAnimator.java
+++ b/services/core/java/com/android/server/wm/WindowStateAnimator.java
@@ -51,12 +51,15 @@
 import android.graphics.PixelFormat;
 import android.graphics.Point;
 import android.graphics.Rect;
+import android.graphics.RectF;
 import android.graphics.Region;
 import android.os.Debug;
 import android.os.RemoteException;
+import android.os.Trace;
 import android.util.Slog;
 import android.view.DisplayInfo;
 import android.view.MagnificationSpec;
+import android.view.Surface;
 import android.view.Surface.OutOfResourcesException;
 import android.view.SurfaceControl;
 import android.view.WindowManager;
@@ -225,8 +228,6 @@
     int mAttrType;
 
     static final long PENDING_TRANSACTION_FINISH_WAIT_TIME = 100;
-    long mDeferTransactionUntilFrame = -1;
-    long mDeferTransactionTime = -1;
 
     boolean mForceScaleUntilResize;
 
@@ -923,6 +924,22 @@
         mPendingDestroySurface = null;
     }
 
+    void applyMagnificationSpec(MagnificationSpec spec, Matrix transform) {
+        final int surfaceInsetLeft = mWin.mAttrs.surfaceInsets.left;
+        final int surfaceInsetTop = mWin.mAttrs.surfaceInsets.top;
+
+        if (spec != null && !spec.isNop()) {
+            float scale = spec.scale;
+            transform.postScale(scale, scale);
+            transform.postTranslate(spec.offsetX, spec.offsetY);
+
+            // As we are scaling the whole surface, to keep the content
+            // in the same position we will also have to scale the surfaceInsets.
+            transform.postTranslate(-(surfaceInsetLeft*scale - surfaceInsetLeft),
+                    -(surfaceInsetTop*scale - surfaceInsetTop));
+        }
+    }
+
     void computeShownFrameLocked() {
         final boolean selfTransformation = mHasLocalTransformation;
         Transformation attachedTransformation =
@@ -1013,10 +1030,7 @@
             if (mService.mAccessibilityController != null && displayId == DEFAULT_DISPLAY) {
                 MagnificationSpec spec = mService.mAccessibilityController
                         .getMagnificationSpecForWindowLocked(mWin);
-                if (spec != null && !spec.isNop()) {
-                    tmpMatrix.postScale(spec.scale, spec.scale);
-                    tmpMatrix.postTranslate(spec.offsetX, spec.offsetY);
-                }
+                applyMagnificationSpec(spec, tmpMatrix);
             }
 
             // "convert" it into SurfaceFlinger's format
@@ -1115,10 +1129,7 @@
             tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale);
             tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
 
-            if (spec != null && !spec.isNop()) {
-                tmpMatrix.postScale(spec.scale, spec.scale);
-                tmpMatrix.postTranslate(spec.offsetX, spec.offsetY);
-            }
+            applyMagnificationSpec(spec, tmpMatrix);
 
             tmpMatrix.getValues(tmpFloats);
 
@@ -1400,6 +1411,9 @@
         mExtraHScale = (float) 1.0;
         mExtraVScale = (float) 1.0;
 
+        boolean wasForceScaled = mForceScaleUntilResize;
+        boolean wasSeamlesslyRotated = w.mSeamlesslyRotated;
+
         // Once relayout has been called at least once, we need to make sure
         // we only resize the client surface during calls to relayout. For
         // clients which use indeterminate measure specs (MATCH_PARENT),
@@ -1407,21 +1421,17 @@
         // However, this would be unsafe, as the client may be in the middle
         // of producing a frame at the old size, having just completed layout
         // to find the surface size changed underneath it.
-        //
-        // TODO: For N we only apply this fix to the pinned workspace. As we
-        // aren't observing known issues here outside of PiP resizing. (Typically
-        // the other windows that use -1 are PopupWindows which aren't likely
-        // to be rendering while we resize).
-
-        boolean wasForceScaled = mForceScaleUntilResize;
-
-        if (!w.inPinnedWorkspace() || (!w.mRelayoutCalled || w.mInRelayout)) {
+        if (!w.mRelayoutCalled || w.mInRelayout) {
             mSurfaceResized = mSurfaceController.setSizeInTransaction(
                     mTmpSize.width(), mTmpSize.height(), recoveringMemory);
         } else {
             mSurfaceResized = false;
         }
         mForceScaleUntilResize = mForceScaleUntilResize && !mSurfaceResized;
+        // If we are undergoing seamless rotation, the surface has already
+        // been set up to persist at it's old location. We need to freeze
+        // updates until a resize occurs.
+        mService.markForSeamlessRotation(w, w.mSeamlesslyRotated && !mSurfaceResized);
 
         calculateSurfaceWindowCrop(mTmpClipRect, mTmpFinalClipRect);
 
@@ -1471,17 +1481,20 @@
             // will be seamless.
             mForceScaleUntilResize = true;
         } else {
-            mSurfaceController.setPositionInTransaction(mTmpSize.left, mTmpSize.top,
-                    recoveringMemory);
+            if (!w.mSeamlesslyRotated) {
+                mSurfaceController.setPositionInTransaction(mTmpSize.left, mTmpSize.top,
+                        recoveringMemory);
+            }
         }
 
         // If we are ending the scaling mode. We switch to SCALING_MODE_FREEZE
-        // to prevent further updates until buffer latch. Normally position
-        // would continue to apply immediately. But we need a different position
-        // before and after resize (since we have scaled the shadows, as discussed
-        // above).
-        if (wasForceScaled && !mForceScaleUntilResize) {
-            mSurfaceController.setPositionAppliesWithResizeInTransaction(true);
+        // to prevent further updates until buffer latch.
+        // When ending both force scaling, and seamless rotation, we need to freeze
+        // the Surface geometry until a buffer comes in at the new size (normally position and crop
+        // are unfrozen). setGeometryAppliesWithResizeInTransaction accomplishes this for us.
+        if ((wasForceScaled && !mForceScaleUntilResize) ||
+                (wasSeamlesslyRotated && !w.mSeamlesslyRotated)) {
+            mSurfaceController.setGeometryAppliesWithResizeInTransaction(true);
             mSurfaceController.forceScaleableInTransaction(false);
         }
 
@@ -1493,12 +1506,13 @@
                     -w.mAttrs.surfaceInsets.right, -w.mAttrs.surfaceInsets.bottom);
         }
 
-        updateSurfaceWindowCrop(clipRect, mTmpFinalClipRect, recoveringMemory);
-
-        mSurfaceController.setMatrixInTransaction(mDsDx * w.mHScale * mExtraHScale,
-                mDtDx * w.mVScale * mExtraVScale,
-                mDsDy * w.mHScale * mExtraHScale,
-                mDtDy * w.mVScale * mExtraVScale, recoveringMemory);
+        if (!w.mSeamlesslyRotated) {
+            updateSurfaceWindowCrop(clipRect, mTmpFinalClipRect, recoveringMemory);
+            mSurfaceController.setMatrixInTransaction(mDsDx * w.mHScale * mExtraHScale,
+                    mDtDx * w.mVScale * mExtraVScale,
+                    mDsDy * w.mHScale * mExtraHScale,
+                    mDtDy * w.mVScale * mExtraVScale, recoveringMemory);
+        }
 
         if (mSurfaceResized) {
             mReportSurfaceResized = true;
@@ -1858,6 +1872,7 @@
         // frozen, there is no reason to animate and it can cause strange
         // artifacts when we unfreeze the display if some different animation
         // is running.
+        Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "WSA#applyAnimationLocked");
         if (mService.okToDisplay()) {
             int anim = mPolicy.selectAnimationLw(mWin, transit);
             int attr = -1;
@@ -1897,6 +1912,8 @@
         } else {
             clearAnimation();
         }
+        Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
+
         if (mWin.mAttrs.type == TYPE_INPUT_METHOD) {
             mService.adjustForImeIfNeeded(mWin.mDisplayContent);
             if (isEntrance) {
@@ -2036,34 +2053,11 @@
         if (!mWin.isChildWindow()) {
             return;
         }
-        mDeferTransactionUntilFrame = frameNumber;
-        mDeferTransactionTime = System.currentTimeMillis();
         mSurfaceController.deferTransactionUntil(
                 mWin.mAttachedWindow.mWinAnimator.mSurfaceController.getHandle(),
                 frameNumber);
     }
 
-    // Defer the current transaction to the frame number of the last saved transaction.
-    // We do this to avoid shooting through an unsynchronized transaction while something is
-    // pending. This is generally fine, as either we will get in on the synchronization,
-    // or SurfaceFlinger will see that the frame has already occured. The only
-    // potential problem is in frame number resets so we reset things with a timeout
-    // every so often to be careful.
-    void deferToPendingTransaction() {
-        if (mDeferTransactionUntilFrame < 0) {
-            return;
-        }
-        long time = System.currentTimeMillis();
-        if (time > mDeferTransactionTime + PENDING_TRANSACTION_FINISH_WAIT_TIME) {
-            mDeferTransactionTime = -1;
-            mDeferTransactionUntilFrame = -1;
-        } else {
-            mSurfaceController.deferTransactionUntil(
-                    mWin.mAttachedWindow.mWinAnimator.mSurfaceController.getHandle(),
-                    mDeferTransactionUntilFrame);
-        }
-    }
-
     /**
      * Sometimes we need to synchronize the first frame of animation with some external event.
      * To achieve this, we prolong the start of the animation and keep producing the first frame of
@@ -2084,4 +2078,98 @@
     void endDelayingAnimationStart() {
         mAnimationStartDelayed = false;
     }
+
+    void seamlesslyRotateWindow(int oldRotation, int newRotation) {
+        final WindowState w = mWin;
+        if (!w.isVisibleNow() || w.mIsWallpaper) {
+            return;
+        }
+
+        final Rect cropRect = mService.mTmpRect;
+        final Rect displayRect = mService.mTmpRect2;
+        final RectF frameRect = mService.mTmpRectF;
+        final Matrix transform = mService.mTmpTransform;
+
+        final float x = w.mFrame.left;
+        final float y = w.mFrame.top;
+        final float width = w.mFrame.width();
+        final float height = w.mFrame.height();
+
+        mService.getDefaultDisplayContentLocked().getLogicalDisplayRect(displayRect);
+        final float displayWidth = displayRect.width();
+        final float displayHeight = displayRect.height();
+
+        // Compute a transform matrix to undo the coordinate space transformation,
+        // and present the window at the same physical position it previously occupied.
+        final int deltaRotation = DisplayContent.deltaRotation(newRotation, oldRotation);
+        switch (deltaRotation) {
+        case Surface.ROTATION_0:
+            transform.reset();
+            break;
+        case Surface.ROTATION_270:
+            transform.setRotate(270, 0, 0);
+            transform.postTranslate(0, displayHeight);
+            transform.postTranslate(y, 0);
+            break;
+        case Surface.ROTATION_180:
+            transform.reset();
+            break;
+        case Surface.ROTATION_90:
+            transform.setRotate(90, 0, 0);
+            transform.postTranslate(displayWidth, 0);
+            transform.postTranslate(-y, x);
+            break;
+        }
+
+        // We have two cases:
+        //  1. Windows with NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY:
+        //     These windows never change buffer size when rotating. Rather the window manager
+        //     just updates the scaling factors to fit in the new coordinate system,
+        //     and SurfaceFlinger takes care of updating the buffer contents. So in this case
+        //     we just need we just need to update the scaling factors and things are seamless
+        //     already.
+        //  2. Other windows:
+        //     In this case, we need to apply a rotation matrix to the window. For example
+        //     if we have a portrait window and rotate to landscape, the window is still portrait
+        //     and now extends off the bottom of the screen (and only halfway across). Essentially we
+        //     apply a transform to display the current buffer at it's old position
+        //     (in the new coordinate space). We then freeze layer updates until the resize
+        //     occurs, at which point we undo, them.
+        if (w.isChildWindow() && mSurfaceController.getTransformToDisplayInverse()) {
+            frameRect.set(x, y, x+width, y+height);
+            transform.mapRect(frameRect);
+
+            w.mAttrs.x = (int) frameRect.left - w.mAttachedWindow.mFrame.left;
+            w.mAttrs.y = (int) frameRect.top - w.mAttachedWindow.mFrame.top;
+            w.mAttrs.width = (int) Math.ceil(frameRect.width());
+            w.mAttrs.height = (int) Math.ceil(frameRect.height());
+
+            w.setWindowScale(w.mRequestedWidth, w.mRequestedHeight);
+
+            w.applyGravityAndUpdateFrame(w.mContainingFrame, w.mDisplayFrame);
+            computeShownFrameLocked();
+            setSurfaceBoundariesLocked(false);
+
+            // The stack bounds will not yet be rotated at this point so setSurfaceBoundaries locked
+            // will crop us incorrectly. Overwrite the crop, exposing the full surface. By the next
+            // transaction this will be corrected.
+            cropRect.set(0, 0, w.mRequestedWidth, w.mRequestedWidth + w.mRequestedHeight);
+            mSurfaceController.setCropInTransaction(cropRect, false);
+        } else {
+            mService.markForSeamlessRotation(w, true);
+            transform.getValues(mService.mTmpFloats);
+
+            float DsDx = mService.mTmpFloats[Matrix.MSCALE_X];
+            float DtDx = mService.mTmpFloats[Matrix.MSKEW_Y];
+            float DsDy = mService.mTmpFloats[Matrix.MSKEW_X];
+            float DtDy = mService.mTmpFloats[Matrix.MSCALE_Y];
+            float nx = mService.mTmpFloats[Matrix.MTRANS_X];
+            float ny = mService.mTmpFloats[Matrix.MTRANS_Y];
+            mSurfaceController.setPositionInTransaction(nx, ny, false);
+            mSurfaceController.setMatrixInTransaction(DsDx * w.mHScale,
+                    DtDx * w.mVScale,
+                    DsDy * w.mHScale,
+                    DtDy * w.mVScale, false);
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/wm/WindowSurfaceController.java b/services/core/java/com/android/server/wm/WindowSurfaceController.java
index 570a6ec..f5ed9d1 100644
--- a/services/core/java/com/android/server/wm/WindowSurfaceController.java
+++ b/services/core/java/com/android/server/wm/WindowSurfaceController.java
@@ -153,9 +153,9 @@
     }
 
     void destroyInTransaction() {
-        //        if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
-        Slog.i(TAG, "Destroying surface " + this + " called by " + Debug.getCallers(8));
-        //        }
+        if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
+            Slog.i(TAG, "Destroying surface " + this + " called by " + Debug.getCallers(8));
+        }
         try {
             if (mSurfaceControl != null) {
                 mSurfaceControl.destroy();
@@ -260,8 +260,8 @@
         }
     }
 
-    void setPositionAppliesWithResizeInTransaction(boolean recoveringMemory) {
-        mSurfaceControl.setPositionAppliesWithResize();
+    void setGeometryAppliesWithResizeInTransaction(boolean recoveringMemory) {
+        mSurfaceControl.setGeometryAppliesWithResize();
     }
 
     void setMatrixInTransaction(float dsdx, float dtdx, float dsdy, float dtdy,
@@ -458,6 +458,10 @@
         return mSurfaceControl.getHandle();
     }
 
+    boolean getTransformToDisplayInverse() {
+        return mSurfaceControl.getTransformToDisplayInverse();
+    }
+
     void getSurface(Surface outSurface) {
         outSurface.copyFrom(mSurfaceControl);
     }
@@ -582,10 +586,10 @@
         }
 
         @Override
-        public void setPositionAppliesWithResize() {
-            if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setPositionAppliesWithResize(): OLD: "
-                    + this + ". Called by" + Debug.getCallers(9));
-            super.setPositionAppliesWithResize();
+        public void setGeometryAppliesWithResize() {
+            if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setGeometryAppliesWithResize(): OLD: "
+                    + this + ". Called by" + Debug.getCallers(3));
+            super.setGeometryAppliesWithResize();
         }
 
         @Override
diff --git a/services/core/java/com/android/server/wm/WindowSurfacePlacer.java b/services/core/java/com/android/server/wm/WindowSurfacePlacer.java
index 359063c..eba52f9 100644
--- a/services/core/java/com/android/server/wm/WindowSurfacePlacer.java
+++ b/services/core/java/com/android/server/wm/WindowSurfacePlacer.java
@@ -101,6 +101,10 @@
     static final int SET_WALLPAPER_ACTION_PENDING       = 1 << 5;
 
     boolean mWallpaperMayChange = false;
+    // During an orientation change, we track whether all windows have rendered
+    // at the new orientation, and this will be false from changing orientation until that occurs.
+    // For seamless rotation cases this always stays true, as the windows complete their orientation
+    // changes 1 by 1 without disturbing global state.
     boolean mOrientationChangeComplete = true;
     boolean mWallpaperActionPending = false;
 
@@ -717,11 +721,13 @@
                     final boolean adjustedForMinimizedDockOrIme = task != null
                                 && (task.mStack.isAdjustedForMinimizedDockedStack()
                                     || task.mStack.isAdjustedForIme());
-                    if ((w.mAttrs.privateFlags & PRIVATE_FLAG_NO_MOVE_ANIMATION) == 0
-                            && !w.isDragResizing() && !adjustedForMinimizedDockOrIme
-                            && (task == null || w.getTask().mStack.hasMovementAnimations())
-                            && !w.mWinAnimator.mLastHidden) {
-                        winAnimator.setMoveAnimation(left, top);
+                    if (mService.okToDisplay()) {
+                        if ((w.mAttrs.privateFlags & PRIVATE_FLAG_NO_MOVE_ANIMATION) == 0
+                                && !w.isDragResizing() && !adjustedForMinimizedDockOrIme
+                                && (task == null || w.getTask().mStack.hasMovementAnimations())
+                                && !w.mWinAnimator.mLastHidden) {
+                            winAnimator.setMoveAnimation(left, top);
+                        }
                     }
 
                     //TODO (multidisplay): Accessibility supported only for the default display.
@@ -742,10 +748,6 @@
 
                 // Moved from updateWindowsAndWallpaperLocked().
                 if (w.mHasSurface) {
-                    // If we have recently synchronized a previous transaction for this
-                    // window ensure we don't push through an unsynchronized one now.
-                    winAnimator.deferToPendingTransaction();
-
                     // Take care of the window being ready to display.
                     final boolean committed = winAnimator.commitFinishDrawingLocked();
                     if (isDefaultDisplay && committed) {
@@ -974,7 +976,7 @@
             // windows, since that means "perform layout as normal,
             // just don't display").
             if (!gone || !win.mHaveFrame || win.mLayoutNeeded
-                    || ((win.isConfigChanged() || win.setInsetsChanged())
+                    || ((win.isConfigChanged() || win.setReportResizeHints())
                             && !win.isGoneForLayoutLw() &&
                             ((win.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
                             (win.mHasSurface && win.mAppToken != null &&
@@ -1074,6 +1076,8 @@
         if (!transitionGoodToGo(appsCount)) {
             return 0;
         }
+        Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "AppTransitionReady");
+
         if (DEBUG_APP_TRANSITIONS) Slog.v(TAG, "**** GOOD TO GO");
         int transit = mService.mAppTransition.getAppTransition();
         if (mService.mSkipAppTransitionAnimation) {
@@ -1095,6 +1099,26 @@
         boolean fullscreenAnim = false;
         boolean voiceInteraction = false;
 
+        int i;
+        for (i = 0; i < appsCount; i++) {
+            final AppWindowToken wtoken = mService.mOpeningApps.valueAt(i);
+            // Clearing the mAnimatingExit flag before entering animation. It's set to
+            // true if app window is removed, or window relayout to invisible.
+            // This also affects window visibility. We need to clear it *before*
+            // maybeUpdateTransitToWallpaper() as the transition selection depends on
+            // wallpaper target visibility.
+            wtoken.clearAnimatingFlags();
+
+        }
+        // Adjust wallpaper before we pull the lower/upper target, since pending changes
+        // (like the clearAnimatingFlags() above) might affect wallpaper target result.
+        final DisplayContent displayContent = mService.getDefaultDisplayContentLocked();
+        if ((displayContent.pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0 &&
+                mWallpaperControllerLocked.adjustWallpaperWindows()) {
+            mService.mLayersController.assignLayersLocked(windows);
+            displayContent.layoutNeeded = true;
+        }
+
         final WindowState lowerWallpaperTarget =
                 mWallpaperControllerLocked.getLowerWallpaperTarget();
         final WindowState upperWallpaperTarget =
@@ -1111,7 +1135,6 @@
             upperWallpaperAppToken = upperWallpaperTarget.mAppToken;
         }
 
-        int i;
         // Do a first pass through the tokens for two
         // things:
         // (1) Determine if both the closing and opening
@@ -1181,6 +1204,8 @@
         final AppWindowToken topOpeningApp = handleOpeningApps(transit,
                 animLp, voiceInteraction, topClosingLayer);
 
+        mService.mAppTransition.setLastAppTransition(transit, topOpeningApp, topClosingApp);
+
         final AppWindowAnimator openingAppAnimator = (topOpeningApp == null) ?  null :
                 topOpeningApp.mAppAnimator;
         final AppWindowAnimator closingAppAnimator = (topClosingApp == null) ? null :
@@ -1196,7 +1221,7 @@
 
         // This has changed the visibility of windows, so perform
         // a new layout to get them all up-to-date.
-        mService.getDefaultDisplayContentLocked().layoutNeeded = true;
+        displayContent.layoutNeeded = true;
 
         // TODO(multidisplay): IMEs are only supported on the default display.
         if (windows == mService.getDefaultWindowListLocked()
@@ -1207,6 +1232,9 @@
                 true /*updateInputWindows*/);
         mService.mFocusMayChange = false;
         mService.notifyActivityDrawnForKeyguard();
+
+        Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
+
         return FINISH_LAYOUT_REDO_LAYOUT | FINISH_LAYOUT_REDO_CONFIG;
     }
 
@@ -1257,26 +1285,6 @@
                 int layer = -1;
                 for (int j = 0; j < wtoken.allAppWindows.size(); j++) {
                     final WindowState win = wtoken.allAppWindows.get(j);
-                    // Clearing the mAnimatingExit flag before entering animation. It will be set to true
-                    // if app window is removed, or window relayout to invisible. We don't want to
-                    // clear it out for windows that get replaced, because the animation depends on
-                    // the flag to remove the replaced window.
-                    //
-                    // We also don't clear the mAnimatingExit flag for windows which have the
-                    // mRemoveOnExit flag. This indicates an explicit remove request has been issued
-                    // by the client. We should let animation proceed and not clear this flag or
-                    // they won't eventually be removed by WindowStateAnimator#finishExit.
-                    if (!win.mWillReplaceWindow && !win.mRemoveOnExit) {
-                        win.mAnimatingExit = false;
-                        // Clear mAnimating flag together with mAnimatingExit. When animation
-                        // changes from exiting to entering, we need to clear this flag until the
-                        // new animation gets applied, so that isAnimationStarting() becomes true
-                        // until then.
-                        // Otherwise applySurfaceChangesTransaction will faill to skip surface
-                        // placement for this window during this period, one or more frame will
-                        // show up with wrong position or scale.
-                        win.mWinAnimator.mAnimating = false;
-                    }
                     if (win.mWinAnimator.mAnimLayer > layer) {
                         layer = win.mWinAnimator.mAnimLayer;
                     }
@@ -1351,8 +1359,25 @@
                 "Checking " + appsCount + " opening apps (frozen="
                         + mService.mDisplayFrozen + " timeout="
                         + mService.mAppTransition.isTimeout() + ")...");
+        final ScreenRotationAnimation screenRotationAnimation =
+            mService.mAnimator.getScreenRotationAnimationLocked(
+                    Display.DEFAULT_DISPLAY);
+
         int reason = APP_TRANSITION_TIMEOUT;
         if (!mService.mAppTransition.isTimeout()) {
+            // Imagine the case where we are changing orientation due to an app transition, but a previous
+            // orientation change is still in progress. We won't process the orientation change
+            // for our transition because we need to wait for the rotation animation to finish.
+            // If we start the app transition at this point, we will interrupt it halfway with a new rotation
+            // animation after the old one finally finishes. It's better to defer the
+            // app transition.
+            if (screenRotationAnimation != null && screenRotationAnimation.isAnimating() &&
+                    mService.rotationNeedsUpdateLocked()) {
+                if (DEBUG_APP_TRANSITIONS) {
+                    Slog.v(TAG, "Delaying app transition for screen rotation animation to finish");
+                }
+                return false;
+            }
             for (int i = 0; i < appsCount; i++) {
                 AppWindowToken wtoken = mService.mOpeningApps.valueAt(i);
                 if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
@@ -1477,7 +1502,7 @@
             mObscured = true;
         }
 
-        if (w.mHasSurface) {
+        if (w.mHasSurface && canBeSeen) {
             if ((attrFlags&FLAG_KEEP_SCREEN_ON) != 0) {
                 mHoldScreen = w.mSession;
                 mHoldScreenWindow = w;
@@ -1500,43 +1525,39 @@
             }
 
             final int type = attrs.type;
-            if (canBeSeen
-                    && (type == TYPE_SYSTEM_DIALOG
-                     || type == TYPE_SYSTEM_ERROR
-                     || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0)) {
+            if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
+                    || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
                 mSyswin = true;
             }
 
-            if (canBeSeen) {
-                // This function assumes that the contents of the default display are
-                // processed first before secondary displays.
-                final DisplayContent displayContent = w.getDisplayContent();
-                if (displayContent != null && displayContent.isDefaultDisplay) {
-                    // While a dream or keyguard is showing, obscure ordinary application
-                    // content on secondary displays (by forcibly enabling mirroring unless
-                    // there is other content we want to show) but still allow opaque
-                    // keyguard dialogs to be shown.
-                    if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
-                        mObscureApplicationContentOnSecondaryDisplays = true;
-                    }
-                    mDisplayHasContent = true;
-                } else if (displayContent != null &&
-                        (!mObscureApplicationContentOnSecondaryDisplays
-                        || (mObscured && type == TYPE_KEYGUARD_DIALOG))) {
-                    // Allow full screen keyguard presentation dialogs to be seen.
-                    mDisplayHasContent = true;
+            // This function assumes that the contents of the default display are
+            // processed first before secondary displays.
+            final DisplayContent displayContent = w.getDisplayContent();
+            if (displayContent != null && displayContent.isDefaultDisplay) {
+                // While a dream or keyguard is showing, obscure ordinary application
+                // content on secondary displays (by forcibly enabling mirroring unless
+                // there is other content we want to show) but still allow opaque
+                // keyguard dialogs to be shown.
+                if (type == TYPE_DREAM || (attrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
+                    mObscureApplicationContentOnSecondaryDisplays = true;
                 }
-                if (mPreferredRefreshRate == 0
-                        && w.mAttrs.preferredRefreshRate != 0) {
-                    mPreferredRefreshRate = w.mAttrs.preferredRefreshRate;
-                }
-                if (mPreferredModeId == 0
-                        && w.mAttrs.preferredDisplayModeId != 0) {
-                    mPreferredModeId = w.mAttrs.preferredDisplayModeId;
-                }
-                if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
-                    mSustainedPerformanceModeCurrent = true;
-                }
+                mDisplayHasContent = true;
+            } else if (displayContent != null &&
+                    (!mObscureApplicationContentOnSecondaryDisplays
+                            || (mObscured && type == TYPE_KEYGUARD_DIALOG))) {
+                // Allow full screen keyguard presentation dialogs to be seen.
+                mDisplayHasContent = true;
+            }
+            if (mPreferredRefreshRate == 0
+                    && w.mAttrs.preferredRefreshRate != 0) {
+                mPreferredRefreshRate = w.mAttrs.preferredRefreshRate;
+            }
+            if (mPreferredModeId == 0
+                    && w.mAttrs.preferredDisplayModeId != 0) {
+                mPreferredModeId = w.mAttrs.preferredDisplayModeId;
+            }
+            if ((privateflags & PRIVATE_FLAG_SUSTAINED_PERFORMANCE_MODE) != 0) {
+                mSustainedPerformanceModeCurrent = true;
             }
         }
     }
diff --git a/services/core/proto/ipconnectivity.proto b/services/core/proto/ipconnectivity.proto
new file mode 100644
index 0000000..e0d7f09
--- /dev/null
+++ b/services/core/proto/ipconnectivity.proto
@@ -0,0 +1,274 @@
+// LINT: LEGACY_NAMES
+syntax = "proto2";
+
+package clearcut.connectivity;
+
+option java_package = "com.android.server.connectivity.metrics";
+option java_outer_classname = "IpConnectivityLogClass";
+
+// NetworkId represents the id given by the system to a physical network on the
+// Android device. It is used to relates events to each other for devices with
+// multiple networks (WiFi, 4G, ...).
+message NetworkId {
+  // Every network gets assigned a network_id on creation based on order of
+  // creation. Thus network_id N is assigned to the network created directly
+  // after network N-1. Thus there is no PII involved here. Zero means no
+  // network. The value 0 is never assigned to a network.
+  optional int32 network_id = 1;
+};
+
+// Logs changes in the system default network. Changes can be 1) acquiring a
+// default network with no previous default, 2) a switch of the system default
+// network to a new default network, 3) a loss of the system default network.
+// This message is associated to android.net.metrics.DefaultNetworkEvent.
+message DefaultNetworkEvent {
+  // A value of 0 means this is a loss of the system default network.
+  optional NetworkId network_id = 1;
+
+  // A value of 0 means there was no previous default network.
+  optional NetworkId previous_network_id = 2;
+
+  // Whether the network supports IPv4, IPv6, or both.
+  enum IPSupport {
+    NONE = 0;
+    IPV4 = 1;
+    IPV6 = 2;
+    DUAL = 3;
+  };
+
+  // Best available information about IP support of the previous network when
+  // disconnecting or switching to a new default network.
+  optional IPSupport previous_network_ip_support = 3;
+
+  // The transport types of the new default network, represented by
+  // TRANSPORT_* constants as defined in NetworkCapabilities.
+  repeated int32 transport_types = 4;
+};
+
+// Logs IpReachabilityMonitor probe events and NUD_FAILED events.
+// This message is associated to android.net.metrics.IpReachabilityEvent.
+message IpReachabilityEvent {
+  // The interface name (wlan, rmnet, lo, ...) on which the probe was sent.
+  optional string if_name = 1;
+
+  // The event type code of the probe, represented by constants defined in
+  // android.net.metrics.IpReachabilityEvent.
+  optional int32 event_type = 2;
+};
+
+// Logs NetworkMonitor and ConnectivityService events related to the state of
+// a network: connection, evaluation, validation, lingering, and disconnection.
+// This message is associated to android.net.metrics.NetworkEvent.
+message NetworkEvent {
+  // The id of the network on which this event happened.
+  optional NetworkId network_id = 1;
+
+  // The type of network event, represented by NETWORK_* constants defined in
+  // android.net.metrics.NetworkEvent.
+  optional int32 event_type = 2;
+
+  // Only valid after finishing evaluating a network for Internet connectivity.
+  // The time it took for this evaluation to complete.
+  optional int32 latency_ms = 3;
+}
+
+// Logs individual captive portal probing events that are performed when
+// evaluating or reevaluating networks for Internet connectivity.
+// This message is associated to android.net.metrics.ValidationProbeEvent.
+message ValidationProbeEvent {
+  // The id of the network for which the probe was sent.
+  optional NetworkId network_id = 1;
+
+  // The time it took for that probe to complete or time out.
+  optional int32 latency_ms = 2;
+
+  // The type of portal probe, represented by PROBE_* constants defined in
+  // android.net.metrics.ValidationProbeEvent.
+  optional int32 probe_type = 3;
+
+  // The http code result of the probe test.
+  optional int32 probe_result = 4;
+}
+
+// Logs DNS lookup latencies. Repeated fields must have the same length.
+// This message is associated to android.net.metrics.DnsEvent.
+message DNSLookupBatch {
+  // The id of the network on which the DNS lookups took place.
+  optional NetworkId network_id = 1;
+
+  // The types of the DNS lookups, as defined in android.net.metrics.DnsEvent.
+  repeated int32 event_types = 2;
+
+  // The return values of the DNS resolver for each DNS lookups.
+  repeated int32 return_codes = 3;
+
+  // The time it took for each DNS lookups to complete.
+  repeated int32 latencies_ms = 4;
+};
+
+// Represents a DHCP event on a single interface, which can be a DHCPClient
+// state transition or a response packet parsing error.
+// This message is associated to android.net.metrics.DhcpClientEvent and
+// android.net.metrics.DhcpErrorEvent.
+message DHCPEvent {
+  // The interface name (wlan, rmnet, lo, ...) on which the event happened.
+  optional string if_name = 1;
+
+  oneof value {
+    // The name of a state in the DhcpClient state machine, represented by
+    // the inner classes of android.net.dhcp.DhcpClient.
+    string state_transition = 2;
+
+    // The error code of a DHCP error, represented by constants defined in
+    // android.net.metrics.DhcpErrorEvent.
+    int32 error_code = 3;
+  }
+
+  // Lifetime duration in milliseconds of a DhcpClient state, or transition
+  // time in milliseconds between specific pairs of DhcpClient's states.
+  // Only populated when state_transition is populated.
+  optional int32 duration_ms = 4;
+}
+
+// Represents the generation of an Android Packet Filter program.
+message ApfProgramEvent {
+  // Lifetime of the program in seconds.
+  optional int64 lifetime = 1;
+
+  // Number of RAs filtered by the APF program.
+  optional int32 filtered_ras = 2;
+
+  // Total number of RAs to filter currently tracked by ApfFilter. Can be more
+  // than filtered_ras if all available program size was exhausted.
+  optional int32 current_ras = 3;
+
+  // Length of the APF program in bytes.
+  optional int32 program_length = 4;
+
+  // True if the APF program is dropping multicast and broadcast traffic.
+  optional bool drop_multicast = 5;
+
+  // True if the interface on which APF runs has an IPv4 address.
+  optional bool has_ipv4_addr = 6;
+}
+
+// Represents Router Advertisement listening statistics for an interface with
+// Android Packet Filter enabled.
+message ApfStatistics {
+  // The time interval in milliseconds these stastistics cover.
+  optional int64 duration_ms = 1;
+
+  // The total number of received RAs.
+  optional int32 received_ras = 2;
+
+  // The total number of received RAs that matched a known RA.
+  optional int32 matching_ras = 3;
+
+  // The total number of received RAs ignored due to the MAX_RAS limit.
+  optional int32 dropped_ras = 5;
+
+  // The total number of received RAs with an effective lifetime of 0 seconds.
+  // Effective lifetime for APF is the minimum of all lifetimes in a RA.
+  optional int32 zero_lifetime_ras = 6;
+
+  // The total number of received RAs that could not be parsed.
+  optional int32 parse_errors = 7;
+
+  // The total number of APF program updates triggered by an RA reception.
+  optional int32 program_updates = 8;
+
+  // The maximum APF program size in byte advertised by hardware.
+  optional int32 max_program_size = 9;
+}
+
+// Represents the reception of a Router Advertisement packet for an interface
+// with Android Packet Filter enabled.
+message RaEvent {
+  // All lifetime values are expressed in seconds. The default value for an
+  // option lifetime that was not present in the RA option list is -1.
+  // The lifetime of an option (e.g., the Prefix Information Option) is the
+  // minimum lifetime of all such options in the packet.
+
+  // The value of the router lifetime in the RA packet.
+  optional int64 router_lifetime = 1;
+
+  // Prefix valid lifetime from the prefix information option.
+  optional int64 prefix_valid_lifetime = 2;
+
+  // Prefix preferred lifetime from the prefix information option.
+  optional int64 prefix_preferred_lifetime = 3;
+
+  // Route info lifetime.
+  optional int64 route_info_lifetime = 4;
+
+  // Recursive DNS server lifetime.
+  optional int64 rdnss_lifetime = 5;
+
+  // DNS search list lifetime.
+  optional int64 dnssl_lifetime = 6;
+}
+
+// Represents an IP provisioning event in IpManager and how long the
+// provisioning action took.
+// This message is associated to android.net.metrics.IpManagerEvent.
+message IpProvisioningEvent {
+  // The interface name (wlan, rmnet, lo, ...) on which the probe was sent.
+  optional string if_name = 1;
+
+  // The code of the IP provisioning event, represented by constants defined in
+  // android.net.metrics.IpManagerEvent.
+  optional int32 event_type = 2;
+
+  // The duration of the provisioning action that resulted in this event.
+  optional int32 latency_ms = 3;
+}
+
+// Represents one of the IP connectivity event defined in this file.
+// Next tag: 12
+message IpConnectivityEvent {
+  // Time in ms when the event was recorded.
+  optional int64 time_ms = 1;
+
+  // Event type.
+  oneof event {
+
+    // An event about the system default network.
+    DefaultNetworkEvent default_network_event = 2;
+
+    // An IP reachability probe event.
+    IpReachabilityEvent ip_reachability_event = 3;
+
+    // A network lifecycle event.
+    NetworkEvent network_event = 4;
+
+    // A batch of DNS lookups.
+    DNSLookupBatch dns_lookup_batch = 5;
+
+    // A DHCP client event or DHCP receive error.
+    DHCPEvent dhcp_event = 6;
+
+    // An IP provisioning event.
+    IpProvisioningEvent ip_provisioning_event = 7;
+
+    // A network validation probe event.
+    ValidationProbeEvent validation_probe_event = 8;
+
+    // An Android Packet Filter program event.
+    ApfProgramEvent apf_program_event = 9;
+
+    // An Android Packet Filter statistics event.
+    ApfStatistics apf_statistics = 10;
+
+    // An RA packet reception event.
+    RaEvent ra_event = 11;
+  };
+};
+
+// The information about IP connectivity events.
+message IpConnectivityLog {
+  // An array of IP connectivity events.
+  repeated IpConnectivityEvent events = 1;
+
+  // The number of events that had to be dropped due to a full buffer.
+  optional int32 dropped_events = 2;
+};
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
index 2aaf945..fc14444 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -29,6 +29,7 @@
 
 import android.Manifest.permission;
 import android.accessibilityservice.AccessibilityServiceInfo;
+import android.accounts.Account;
 import android.accounts.AccountManager;
 import android.annotation.IntDef;
 import android.annotation.NonNull;
@@ -219,6 +220,8 @@
     private static final String ATTR_SETUP_COMPLETE = "setup-complete";
     private static final String ATTR_PROVISIONING_STATE = "provisioning-state";
     private static final String ATTR_PERMISSION_POLICY = "permission-policy";
+    private static final String ATTR_DEVICE_PROVISIONING_CONFIG_APPLIED =
+            "device-provisioning-config-applied";
 
     private static final String ATTR_DELEGATED_CERT_INSTALLER = "delegated-cert-installer";
     private static final String ATTR_APPLICATION_RESTRICTIONS_MANAGER
@@ -306,6 +309,12 @@
 
     private static final int DEVICE_ADMIN_DEACTIVATE_TIMEOUT = 10000;
 
+    /**
+     * Minimum timeout in milliseconds after which unlocking with weak auth times out,
+     * i.e. the user has to use a strong authentication method like password, PIN or pattern.
+     */
+    private static final long MINIMUM_STRONG_AUTH_TIMEOUT_MS = 1 * 60 * 60 * 1000; // 1h
+
     final Context mContext;
     final Injector mInjector;
     final IPackageManager mIPackageManager;
@@ -418,6 +427,8 @@
         int mUserProvisioningState;
         int mPermissionPolicy;
 
+        boolean mDeviceProvisioningConfigApplied = false;
+
         final ArrayMap<ComponentName, ActiveAdmin> mAdminMap = new ArrayMap<>();
         final ArrayList<ActiveAdmin> mAdminList = new ArrayList<>();
         final ArrayList<ComponentName> mRemovingAdmins = new ArrayList<>();
@@ -485,6 +496,12 @@
                     }
                 });
             }
+            // STOPSHIP: Remove this code once all dogfood devices are fixed. See b/31754835
+            if (Intent.ACTION_BOOT_COMPLETED.equals(action) && !mOwners.hasDeviceOwner()
+                    && !isBackupServiceEnabledInternal()) {
+                setBackupServiceEnabledInternal(true);
+                Slog.w(LOG_TAG, "Fix backup for device that is not in Device Owner mode.");
+            }
             if (Intent.ACTION_USER_UNLOCKED.equals(action)
                     || Intent.ACTION_USER_STARTED.equals(action)
                     || KeyChain.ACTION_STORAGE_CHANGED.equals(action)) {
@@ -492,9 +509,9 @@
                 new MonitoringCertNotificationTask().execute(userId);
             }
             if (Intent.ACTION_USER_ADDED.equals(action)) {
-                disableSecurityLoggingIfNotCompliant();
+                disableDeviceOwnerManagedSingleUserFeaturesIfNeeded();
             } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
-                disableSecurityLoggingIfNotCompliant();
+                disableDeviceOwnerManagedSingleUserFeaturesIfNeeded();
                 removeUserData(userHandle);
             } else if (Intent.ACTION_USER_STARTED.equals(action)) {
                 synchronized (DevicePolicyManagerService.this) {
@@ -520,6 +537,7 @@
 
     static class ActiveAdmin {
         private static final String TAG_DISABLE_KEYGUARD_FEATURES = "disable-keyguard-features";
+        private static final String TAG_TEST_ONLY_ADMIN = "test-only-admin";
         private static final String TAG_DISABLE_CAMERA = "disable-camera";
         private static final String TAG_DISABLE_CALLER_ID = "disable-caller-id";
         private static final String TAG_DISABLE_CONTACTS_SEARCH = "disable-contacts-search";
@@ -544,6 +562,7 @@
         private static final String TAG_PERMITTED_IMES = "permitted-imes";
         private static final String TAG_MAX_FAILED_PASSWORD_WIPE = "max-failed-password-wipe";
         private static final String TAG_MAX_TIME_TO_UNLOCK = "max-time-to-unlock";
+        private static final String TAG_STRONG_AUTH_UNLOCK_TIMEOUT = "strong-auth-unlock-timeout";
         private static final String TAG_MIN_PASSWORD_NONLETTER = "min-password-nonletter";
         private static final String TAG_MIN_PASSWORD_SYMBOLS = "min-password-symbols";
         private static final String TAG_MIN_PASSWORD_NUMERIC = "min-password-numeric";
@@ -598,6 +617,8 @@
         static final long DEF_MAXIMUM_TIME_TO_UNLOCK = 0;
         long maximumTimeToUnlock = DEF_MAXIMUM_TIME_TO_UNLOCK;
 
+        long strongAuthUnlockTimeout = DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS;
+
         static final int DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE = 0;
         int maximumFailedPasswordsForWipe = DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE;
 
@@ -612,6 +633,7 @@
         int disabledKeyguardFeatures = DEF_KEYGUARD_FEATURES_DISABLED;
 
         boolean encryptionRequested = false;
+        boolean testOnlyAdmin = false;
         boolean disableCamera = false;
         boolean disableCallerId = false;
         boolean disableContactsSearch = false;
@@ -746,6 +768,11 @@
                 out.attribute(null, ATTR_VALUE, Long.toString(maximumTimeToUnlock));
                 out.endTag(null, TAG_MAX_TIME_TO_UNLOCK);
             }
+            if (strongAuthUnlockTimeout != DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS) {
+                out.startTag(null, TAG_STRONG_AUTH_UNLOCK_TIMEOUT);
+                out.attribute(null, ATTR_VALUE, Long.toString(strongAuthUnlockTimeout));
+                out.endTag(null, TAG_STRONG_AUTH_UNLOCK_TIMEOUT);
+            }
             if (maximumFailedPasswordsForWipe != DEF_MAXIMUM_FAILED_PASSWORDS_FOR_WIPE) {
                 out.startTag(null, TAG_MAX_FAILED_PASSWORD_WIPE);
                 out.attribute(null, ATTR_VALUE, Integer.toString(maximumFailedPasswordsForWipe));
@@ -781,6 +808,11 @@
                 out.attribute(null, ATTR_VALUE, Boolean.toString(encryptionRequested));
                 out.endTag(null, TAG_ENCRYPTION_REQUESTED);
             }
+            if (testOnlyAdmin) {
+                out.startTag(null, TAG_TEST_ONLY_ADMIN);
+                out.attribute(null, ATTR_VALUE, Boolean.toString(testOnlyAdmin));
+                out.endTag(null, TAG_TEST_ONLY_ADMIN);
+            }
             if (disableCamera) {
                 out.startTag(null, TAG_DISABLE_CAMERA);
                 out.attribute(null, ATTR_VALUE, Boolean.toString(disableCamera));
@@ -955,6 +987,9 @@
                 } else if (TAG_MAX_TIME_TO_UNLOCK.equals(tag)) {
                     maximumTimeToUnlock = Long.parseLong(
                             parser.getAttributeValue(null, ATTR_VALUE));
+                } else if (TAG_STRONG_AUTH_UNLOCK_TIMEOUT.equals(tag)) {
+                    strongAuthUnlockTimeout = Long.parseLong(
+                            parser.getAttributeValue(null, ATTR_VALUE));
                 } else if (TAG_MAX_FAILED_PASSWORD_WIPE.equals(tag)) {
                     maximumFailedPasswordsForWipe = Integer.parseInt(
                             parser.getAttributeValue(null, ATTR_VALUE));
@@ -976,6 +1011,9 @@
                 } else if (TAG_ENCRYPTION_REQUESTED.equals(tag)) {
                     encryptionRequested = Boolean.parseBoolean(
                             parser.getAttributeValue(null, ATTR_VALUE));
+                } else if (TAG_TEST_ONLY_ADMIN.equals(tag)) {
+                    testOnlyAdmin = Boolean.parseBoolean(
+                            parser.getAttributeValue(null, ATTR_VALUE));
                 } else if (TAG_DISABLE_CAMERA.equals(tag)) {
                     disableCamera = Boolean.parseBoolean(
                             parser.getAttributeValue(null, ATTR_VALUE));
@@ -1174,6 +1212,8 @@
 
         void dump(String prefix, PrintWriter pw) {
             pw.print(prefix); pw.print("uid="); pw.println(getUid());
+            pw.print(prefix); pw.print("testOnlyAdmin=");
+            pw.println(testOnlyAdmin);
             pw.print(prefix); pw.println("policies:");
             ArrayList<DeviceAdminInfo.PolicyInfo> pols = info.getUsedPolicies();
             if (pols != null) {
@@ -1201,6 +1241,8 @@
                     pw.println(minimumPasswordNonLetter);
             pw.print(prefix); pw.print("maximumTimeToUnlock=");
                     pw.println(maximumTimeToUnlock);
+            pw.print(prefix); pw.print("strongAuthUnlockTimeout=");
+                    pw.println(strongAuthUnlockTimeout);
             pw.print(prefix); pw.print("maximumFailedPasswordsForWipe=");
                     pw.println(maximumFailedPasswordsForWipe);
             pw.print(prefix); pw.print("specifiesGlobalProxy=");
@@ -1695,7 +1737,7 @@
             if (mOwners.hasDeviceOwner()) {
                 mInjector.systemPropertiesSet(PROPERTY_DEVICE_OWNER_PRESENT, "true");
                 Slog.i(LOG_TAG, "Set ro.device_owner property to true");
-                disableSecurityLoggingIfNotCompliant();
+                disableDeviceOwnerManagedSingleUserFeaturesIfNeeded();
                 if (mInjector.securityLogGetLoggingEnabledProperty()) {
                     mSecurityLogMonitor.start();
                 }
@@ -2094,8 +2136,9 @@
 
     void removeActiveAdminLocked(final ComponentName adminReceiver, final int userHandle) {
         final ActiveAdmin admin = getActiveAdminUncheckedLocked(adminReceiver, userHandle);
-        if (admin != null) {
-            getUserData(userHandle).mRemovingAdmins.add(adminReceiver);
+        DevicePolicyData policy = getUserData(userHandle);
+        if (admin != null && !policy.mRemovingAdmins.contains(adminReceiver)) {
+            policy.mRemovingAdmins.add(adminReceiver);
             sendAdminCommandLocked(admin,
                     DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED,
                     new BroadcastReceiver() {
@@ -2178,6 +2221,10 @@
                 out.attribute(null, ATTR_SETUP_COMPLETE,
                         Boolean.toString(true));
             }
+            if (policy.mDeviceProvisioningConfigApplied) {
+                out.attribute(null, ATTR_DEVICE_PROVISIONING_CONFIG_APPLIED,
+                        Boolean.toString(true));
+            }
             if (policy.mUserProvisioningState != DevicePolicyManager.STATE_USER_UNMANAGED) {
                 out.attribute(null, ATTR_PROVISIONING_STATE,
                         Integer.toString(policy.mUserProvisioningState));
@@ -2338,6 +2385,12 @@
             if (userSetupComplete != null && Boolean.toString(true).equals(userSetupComplete)) {
                 policy.mUserSetupComplete = true;
             }
+            String deviceProvisioningConfigApplied = parser.getAttributeValue(null,
+                    ATTR_DEVICE_PROVISIONING_CONFIG_APPLIED);
+            if (deviceProvisioningConfigApplied != null
+                    && Boolean.toString(true).equals(deviceProvisioningConfigApplied)) {
+                policy.mDeviceProvisioningConfigApplied = true;
+            }
             String provisioningState = parser.getAttributeValue(null, ATTR_PROVISIONING_STATE);
             if (!TextUtils.isEmpty(provisioningState)) {
                 policy.mUserProvisioningState = Integer.parseInt(provisioningState);
@@ -2813,8 +2866,9 @@
         synchronized (this) {
             long ident = mInjector.binderClearCallingIdentity();
             try {
-                if (!refreshing
-                        && getActiveAdminUncheckedLocked(adminReceiver, userHandle) != null) {
+                final ActiveAdmin existingAdmin
+                        = getActiveAdminUncheckedLocked(adminReceiver, userHandle);
+                if (!refreshing && existingAdmin != null) {
                     throw new IllegalArgumentException("Admin is already added");
                 }
                 if (policy.mRemovingAdmins.contains(adminReceiver)) {
@@ -2822,6 +2876,9 @@
                             "Trying to set an admin which is being removed");
                 }
                 ActiveAdmin newAdmin = new ActiveAdmin(info, /* parent */ false);
+                newAdmin.testOnlyAdmin =
+                        (existingAdmin != null) ? existingAdmin.testOnlyAdmin
+                                : isPackageTestOnly(adminReceiver.getPackageName(), userHandle);
                 policy.mAdminMap.put(adminReceiver, newAdmin);
                 int replaceIndex = -1;
                 final int N = policy.mAdminList.size();
@@ -2933,23 +2990,13 @@
         enforceShell("forceRemoveActiveAdmin");
         long ident = mInjector.binderClearCallingIdentity();
         try {
-            final ApplicationInfo ai;
-            try {
-                ai = mIPackageManager.getApplicationInfo(adminReceiver.getPackageName(),
-                        0, userHandle);
-            } catch (RemoteException e) {
-                throw new IllegalStateException(e);
-            }
-            if (ai == null) {
-                throw new IllegalStateException("Couldn't find package to remove admin "
-                        + adminReceiver.getPackageName() + " " + userHandle);
-            }
-            if ((ai.flags & ApplicationInfo.FLAG_TEST_ONLY) == 0) {
-                throw new SecurityException("Attempt to remove non-test admin " + adminReceiver
-                        + adminReceiver + " " + userHandle);
-            }
-            // If admin is a device or profile owner tidy that up first.
             synchronized (this)  {
+                if (!isAdminTestOnlyLocked(adminReceiver, userHandle)) {
+                    throw new SecurityException("Attempt to remove non-test admin "
+                            + adminReceiver + " " + userHandle);
+                }
+
+                // If admin is a device or profile owner tidy that up first.
                 if (isDeviceOwner(adminReceiver, userHandle)) {
                     clearDeviceOwnerLocked(getDeviceOwnerAdminLocked(), userHandle);
                 }
@@ -2961,11 +3008,47 @@
             }
             // Remove the admin skipping sending the broadcast.
             removeAdminArtifacts(adminReceiver, userHandle);
+            Slog.i(LOG_TAG, "Admin " + adminReceiver + " removed from user " + userHandle);
         } finally {
             mInjector.binderRestoreCallingIdentity(ident);
         }
     }
 
+    /**
+     * Return if a given package has testOnly="true", in which case we'll relax certain rules
+     * for CTS.
+     *
+     * DO NOT use this method except in {@link #setActiveAdmin}.  Use {@link #isAdminTestOnlyLocked}
+     * to check wehter an active admin is test-only or not.
+     *
+     * The system allows this flag to be changed when an app is updated, which is not good
+     * for us.  So we persist the flag in {@link ActiveAdmin} when an admin is first installed,
+     * and used the persisted version in actual checks. (See b/31382361 and b/28928996)
+     */
+    private boolean isPackageTestOnly(String packageName, int userHandle) {
+        final ApplicationInfo ai;
+        try {
+            ai = mIPackageManager.getApplicationInfo(packageName,
+                    (PackageManager.MATCH_DIRECT_BOOT_AWARE
+                            | PackageManager.MATCH_DIRECT_BOOT_UNAWARE), userHandle);
+        } catch (RemoteException e) {
+            throw new IllegalStateException(e);
+        }
+        if (ai == null) {
+            throw new IllegalStateException("Couldn't find package: "
+                    + packageName + " on user " + userHandle);
+        }
+        return (ai.flags & ApplicationInfo.FLAG_TEST_ONLY) != 0;
+    }
+
+    /**
+     * See {@link #isPackageTestOnly}.
+     */
+    private boolean isAdminTestOnlyLocked(ComponentName who, int userHandle) {
+        final ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userHandle);
+        return (admin != null) && admin.testOnlyAdmin;
+    }
+
     private void enforceShell(String method) {
         final int callingUid = Binder.getCallingUid();
         if (callingUid != Process.SHELL_UID && callingUid != Process.ROOT_UID) {
@@ -3663,12 +3746,16 @@
 
     private boolean isActivePasswordSufficientForUserLocked(
             DevicePolicyData policy, int userHandle, boolean parent) {
-        if (policy.mActivePasswordQuality < getPasswordQuality(null, userHandle, parent)
-                || policy.mActivePasswordLength < getPasswordMinimumLength(
+        final int requiredPasswordQuality = getPasswordQuality(null, userHandle, parent);
+        if (policy.mActivePasswordQuality < requiredPasswordQuality) {
+            return false;
+        }
+        if (requiredPasswordQuality >= DevicePolicyManager.PASSWORD_QUALITY_NUMERIC
+                && policy.mActivePasswordLength < getPasswordMinimumLength(
                         null, userHandle, parent)) {
             return false;
         }
-        if (policy.mActivePasswordQuality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) {
+        if (requiredPasswordQuality != DevicePolicyManager.PASSWORD_QUALITY_COMPLEX) {
             return true;
         }
         return policy.mActivePasswordUpperCase >= getPasswordMinimumUpperCase(
@@ -4161,6 +4248,60 @@
     }
 
     @Override
+    public void setRequiredStrongAuthTimeout(ComponentName who, long timeoutMs,
+            boolean parent) {
+        if (!mHasFeature) {
+            return;
+        }
+        Preconditions.checkNotNull(who, "ComponentName is null");
+        Preconditions.checkArgument(timeoutMs >= MINIMUM_STRONG_AUTH_TIMEOUT_MS,
+                "Timeout must not be lower than the minimum strong auth timeout.");
+        Preconditions.checkArgument(timeoutMs <= DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS,
+                "Timeout must not be higher than the default strong auth timeout.");
+
+        final int userHandle = mInjector.userHandleGetCallingUserId();
+        synchronized (this) {
+            ActiveAdmin ap = getActiveAdminForCallerLocked(who,
+                    DeviceAdminInfo.USES_POLICY_PROFILE_OWNER, parent);
+            if (ap.strongAuthUnlockTimeout != timeoutMs) {
+                ap.strongAuthUnlockTimeout = timeoutMs;
+                saveSettingsLocked(userHandle);
+            }
+        }
+    }
+
+    /**
+     * Return a single admin's strong auth unlock timeout or minimum value (strictest) of all
+     * admins if who is null.
+     * Returns default timeout if not configured.
+     */
+    @Override
+    public long getRequiredStrongAuthTimeout(ComponentName who, int userId, boolean parent) {
+        if (!mHasFeature) {
+            return DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS;
+        }
+        enforceFullCrossUsersPermission(userId);
+        synchronized (this) {
+            if (who != null) {
+                ActiveAdmin admin = getActiveAdminUncheckedLocked(who, userId, parent);
+                return admin != null ? Math.max(admin.strongAuthUnlockTimeout,
+                        MINIMUM_STRONG_AUTH_TIMEOUT_MS)
+                        : DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS;
+            }
+
+            // Return the strictest policy across all participating admins.
+            List<ActiveAdmin> admins = getActiveAdminsForLockscreenPoliciesLocked(userId, parent);
+
+            long strongAuthUnlockTimeout = DevicePolicyManager.DEFAULT_STRONG_AUTH_TIMEOUT_MS;
+            for (int i = 0; i < admins.size(); i++) {
+                strongAuthUnlockTimeout = Math.min(admins.get(i).strongAuthUnlockTimeout,
+                        strongAuthUnlockTimeout);
+            }
+            return Math.max(strongAuthUnlockTimeout, MINIMUM_STRONG_AUTH_TIMEOUT_MS);
+        }
+    }
+
+    @Override
     public void lockNow(boolean parent) {
         if (!mHasFeature) {
             return;
@@ -4434,6 +4575,7 @@
         intent.putExtra(DeviceAdminReceiver.EXTRA_CHOOSE_PRIVATE_KEY_URI, uri);
         intent.putExtra(DeviceAdminReceiver.EXTRA_CHOOSE_PRIVATE_KEY_ALIAS, alias);
         intent.putExtra(DeviceAdminReceiver.EXTRA_CHOOSE_PRIVATE_KEY_RESPONSE, response);
+        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
 
         final long id = mInjector.binderClearCallingIdentity();
         try {
@@ -4501,7 +4643,7 @@
      * not installed and therefore not available.
      *
      * @throws SecurityException if the caller is not a profile or device owner.
-     * @throws UnsupportedException if the package does not support being set as always-on.
+     * @throws UnsupportedOperationException if the package does not support being set as always-on.
      */
     @Override
     public boolean setAlwaysOnVpnPackage(ComponentName admin, String vpnPackage, boolean lockdown)
@@ -5103,10 +5245,6 @@
         boolean legacyApp = false;
         if (ai.targetSdkVersion <= Build.VERSION_CODES.M) {
             legacyApp = true;
-        } else if ("com.google.android.apps.enterprise.dmagent".equals(ai.packageName)
-                && ai.versionCode == 697) {
-            // TODO: STOPSHIP remove this (revert ag/895987) once a new prebuilt is dropped
-            legacyApp = true;
         }
 
         final int rawStatus = getEncryptionStatus();
@@ -5701,8 +5839,9 @@
                     + " for device owner");
         }
         synchronized (this) {
-            enforceCanSetDeviceOwnerLocked(userId);
-            if (getActiveAdminUncheckedLocked(admin, userId) == null) {
+            enforceCanSetDeviceOwnerLocked(admin, userId);
+            if (getActiveAdminUncheckedLocked(admin, userId) == null
+                    || getUserData(userId).mRemovingAdmins.contains(admin)) {
                 throw new IllegalArgumentException("Not active admin: " + admin);
             }
 
@@ -5732,6 +5871,7 @@
             } finally {
                 mInjector.binderRestoreCallingIdentity(ident);
             }
+            Slog.i(LOG_TAG, "Device owner set: " + admin + " on user " + userId);
             return true;
         }
     }
@@ -5853,6 +5993,7 @@
             } finally {
                 mInjector.binderRestoreCallingIdentity(ident);
             }
+            Slog.i(LOG_TAG, "Device owner removed: " + deviceOwnerComponent);
         }
     }
 
@@ -5868,10 +6009,12 @@
         mOwners.clearDeviceOwner();
         mOwners.writeDeviceOwner();
         updateDeviceOwnerLocked();
-        disableSecurityLoggingIfNotCompliant();
+        disableDeviceOwnerManagedSingleUserFeaturesIfNeeded();
         try {
-            // Reactivate backup service.
-            mInjector.getIBackupManager().setBackupServiceActive(UserHandle.USER_SYSTEM, true);
+            if (mInjector.getIBackupManager() != null) {
+                // Reactivate backup service.
+                mInjector.getIBackupManager().setBackupServiceActive(UserHandle.USER_SYSTEM, true);
+            }
         } catch (RemoteException e) {
             throw new IllegalStateException("Failed reactivating backup service.", e);
         }
@@ -5888,14 +6031,16 @@
                     + " not installed for userId:" + userHandle);
         }
         synchronized (this) {
-            enforceCanSetProfileOwnerLocked(userHandle);
+            enforceCanSetProfileOwnerLocked(who, userHandle);
 
-            if (getActiveAdminUncheckedLocked(who, userHandle) == null) {
+            if (getActiveAdminUncheckedLocked(who, userHandle) == null
+                    || getUserData(userHandle).mRemovingAdmins.contains(who)) {
                 throw new IllegalArgumentException("Not active admin: " + who);
             }
 
             mOwners.setProfileOwner(who, ownerName, userHandle);
             mOwners.writeProfileOwner(userHandle);
+            Slog.i(LOG_TAG, "Profile owner set: " + who + " on user " + userHandle);
             return true;
         }
     }
@@ -5920,6 +6065,7 @@
             } finally {
                 mInjector.binderRestoreCallingIdentity(ident);
             }
+            Slog.i(LOG_TAG, "Profile owner " + who + " removed from user " + userId);
         }
     }
 
@@ -6196,9 +6342,9 @@
      * The profile owner can only be set before the user setup phase has completed,
      * except for:
      * - SYSTEM_UID
-     * - adb if there are not accounts.
+     * - adb if there are no accounts. (But see {@link #hasIncompatibleAccountsLocked})
      */
-    private void enforceCanSetProfileOwnerLocked(int userHandle) {
+    private void enforceCanSetProfileOwnerLocked(@Nullable ComponentName owner, int userHandle) {
         UserInfo info = getUserInfo(userHandle);
         if (info == null) {
             // User doesn't exist.
@@ -6218,8 +6364,8 @@
         }
         int callingUid = mInjector.binderGetCallingUid();
         if (callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID) {
-            if (hasUserSetupCompleted(userHandle) &&
-                    AccountManager.get(mContext).getAccountsAsUser(userHandle).length > 0) {
+            if (hasUserSetupCompleted(userHandle)
+                    && hasIncompatibleAccountsLocked(userHandle, owner)) {
                 throw new IllegalStateException("Not allowed to set the profile owner because "
                         + "there are already some accounts on the profile");
             }
@@ -6236,14 +6382,14 @@
      * The Device owner can only be set by adb or an app with the MANAGE_PROFILE_AND_DEVICE_OWNERS
      * permission.
      */
-    private void enforceCanSetDeviceOwnerLocked(int userId) {
+    private void enforceCanSetDeviceOwnerLocked(@Nullable ComponentName owner, int userId) {
         int callingUid = mInjector.binderGetCallingUid();
         boolean isAdb = callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID;
         if (!isAdb) {
             enforceCanManageProfileAndDeviceOwners();
         }
 
-        final int code = checkSetDeviceOwnerPreCondition(userId, isAdb);
+        final int code = checkSetDeviceOwnerPreConditionLocked(owner, userId, isAdb);
         switch (code) {
             case CODE_OK:
                 return;
@@ -7926,17 +8072,7 @@
         Preconditions.checkNotNull(who, "ComponentName is null");
         synchronized (this) {
             getActiveAdminForCallerLocked(who, DeviceAdminInfo.USES_POLICY_PROFILE_OWNER);
-            int userId = UserHandle.getCallingUserId();
-            long identity = mInjector.binderClearCallingIdentity();
-            try {
-                IAudioService iAudioService = IAudioService.Stub.asInterface(
-                        ServiceManager.getService(Context.AUDIO_SERVICE));
-                iAudioService.setMasterMute(on, 0, mContext.getPackageName(), userId);
-            } catch (RemoteException re) {
-                Slog.e(LOG_TAG, "Failed to setMasterMute", re);
-            } finally {
-                mInjector.binderRestoreCallingIdentity(identity);
-            }
+            setUserRestriction(who, UserManager.DISALLLOW_UNMUTE_DEVICE, on);
         }
     }
 
@@ -8200,6 +8336,12 @@
 
     @Override
     public SystemUpdatePolicy getSystemUpdatePolicy() {
+        if (UserManager.isDeviceInDemoMode(mContext)) {
+            // Pretending to have an automatic update policy when the device is in retail demo
+            // mode. This will allow the device to download and install an ota without
+            // any user interaction.
+            return SystemUpdatePolicy.createAutomaticInstallPolicy();
+        }
         synchronized (this) {
             SystemUpdatePolicy policy =  mOwners.getSystemUpdatePolicy();
             if (policy != null && !policy.isValid()) {
@@ -8464,8 +8606,8 @@
      * The device owner can only be set before the setup phase of the primary user has completed,
      * except for adb command if no accounts or additional users are present on the device.
      */
-    private synchronized @DeviceOwnerPreConditionCode int checkSetDeviceOwnerPreCondition(
-            int deviceOwnerUserId, boolean isAdb) {
+    private synchronized @DeviceOwnerPreConditionCode int checkSetDeviceOwnerPreConditionLocked(
+            @Nullable ComponentName owner, int deviceOwnerUserId, boolean isAdb) {
         if (mOwners.hasDeviceOwner()) {
             return CODE_HAS_DEVICE_OWNER;
         }
@@ -8482,7 +8624,7 @@
                     if (mUserManager.getUserCount() > 1) {
                         return CODE_NONSYSTEM_USER_EXISTS;
                     }
-                    if (AccountManager.get(mContext).getAccounts().length > 0) {
+                    if (hasIncompatibleAccountsLocked(UserHandle.USER_SYSTEM, owner)) {
                         return CODE_ACCOUNTS_NOT_EMPTY;
                     }
                 } else {
@@ -8508,7 +8650,10 @@
     }
 
     private boolean isDeviceOwnerProvisioningAllowed(int deviceOwnerUserId) {
-        return CODE_OK == checkSetDeviceOwnerPreCondition(deviceOwnerUserId, /* isAdb */ false);
+        synchronized (this) {
+            return CODE_OK == checkSetDeviceOwnerPreConditionLocked(
+                    /* owner unknown */ null, deviceOwnerUserId, /* isAdb */ false);
+        }
     }
 
     private boolean hasFeatureManagedUsers() {
@@ -8840,10 +8985,14 @@
         return false;
     }
 
-    private synchronized void disableSecurityLoggingIfNotCompliant() {
+    private synchronized void disableDeviceOwnerManagedSingleUserFeaturesIfNeeded() {
         if (!isDeviceOwnerManagedSingleUserDevice()) {
             mInjector.securityLogSetLoggingEnabledProperty(false);
             Slog.w(LOG_TAG, "Security logging turned off as it's no longer a single user device.");
+            if (mOwners.hasDeviceOwner()) {
+                setBackupServiceEnabledInternal(false);
+                Slog.w(LOG_TAG, "Backup is off as it's a managed device that has more that one user.");
+            }
         }
     }
 
@@ -8975,6 +9124,11 @@
         }
     }
 
+    @Override
+    public boolean isDeviceProvisioned() {
+        return !TextUtils.isEmpty(mInjector.systemPropertiesGet(PROPERTY_DEVICE_OWNER_PRESENT));
+    }
+
     private void removePackageIfRequired(final String packageName, final int userId) {
         if (!packageHasActiveAdmins(packageName, userId)) {
             // Will not do anything if uninstall was not requested or was already started.
@@ -9036,9 +9190,152 @@
             saveSettingsLocked(userHandle);
             updateMaximumTimeToLockLocked(userHandle);
             policy.mRemovingAdmins.remove(adminReceiver);
+
+            Slog.i(LOG_TAG, "Device admin " + adminReceiver + " removed from user " + userHandle);
         }
         // The removed admin might have disabled camera, so update user
         // restrictions.
         pushUserRestrictions(userHandle);
     }
+
+    @Override
+    public void setDeviceProvisioningConfigApplied() {
+        enforceManageUsers();
+        synchronized (this) {
+            DevicePolicyData policy = getUserData(UserHandle.USER_SYSTEM);
+            policy.mDeviceProvisioningConfigApplied = true;
+            saveSettingsLocked(UserHandle.USER_SYSTEM);
+        }
+    }
+
+    @Override
+    public boolean isDeviceProvisioningConfigApplied() {
+        enforceManageUsers();
+        synchronized (this) {
+            final DevicePolicyData policy = getUserData(UserHandle.USER_SYSTEM);
+            return policy.mDeviceProvisioningConfigApplied;
+        }
+    }
+
+    /**
+     * Return true if a given user has any accounts that'll prevent installing a device or profile
+     * owner {@code owner}.
+     * - If the user has no accounts, then return false.
+     * - Otherwise, if the owner is unknown (== null), or is not test-only, then return true.
+     * - Otherwise, if there's any account that does not have ..._ALLOWED, or does have
+     *   ..._DISALLOWED, return true.
+     * - Otherwise return false.
+     */
+    private boolean hasIncompatibleAccountsLocked(int userId, @Nullable ComponentName owner) {
+        final long token = mInjector.binderClearCallingIdentity();
+        try {
+            final AccountManager am = AccountManager.get(mContext);
+            final Account accounts[] = am.getAccountsAsUser(userId);
+            if (accounts.length == 0) {
+                return false;
+            }
+            final String[] feature_allow =
+                    { DevicePolicyManager.ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_ALLOWED };
+            final String[] feature_disallow =
+                    { DevicePolicyManager.ACCOUNT_FEATURE_DEVICE_OR_PROFILE_OWNER_DISALLOWED };
+
+            // Even if we find incompatible accounts along the way, we still check all accounts
+            // for logging.
+            boolean compatible = true;
+            for (Account account : accounts) {
+                if (hasAccountFeatures(am, account, feature_disallow)) {
+                    Log.e(LOG_TAG, account + " has " + feature_disallow[0]);
+                    compatible = false;
+                }
+                if (!hasAccountFeatures(am, account, feature_allow)) {
+                    Log.e(LOG_TAG, account + " doesn't have " + feature_allow[0]);
+                    compatible = false;
+                }
+            }
+            if (compatible) {
+                Log.w(LOG_TAG, "All accounts are compatible");
+            } else {
+                Log.e(LOG_TAG, "Found incompatible accounts");
+            }
+
+            // Then check if the owner is test-only.
+            String log;
+            if (owner == null) {
+                // Owner is unknown.  Suppose it's not test-only
+                compatible = false;
+                log = "Only test-only device/profile owner can be installed with accounts";
+            } else if (isAdminTestOnlyLocked(owner, userId)) {
+                if (compatible) {
+                    log = "Installing test-only owner " + owner;
+                } else {
+                    log = "Can't install test-only owner " + owner + " with incompatible accounts";
+                }
+            } else {
+                compatible = false;
+                log = "Can't install non test-only owner " + owner + " with accounts";
+            }
+            if (compatible) {
+                Log.w(LOG_TAG, log);
+            } else {
+                Log.e(LOG_TAG, log);
+            }
+            return !compatible;
+        } finally {
+            mInjector.binderRestoreCallingIdentity(token);
+        }
+    }
+
+    private boolean hasAccountFeatures(AccountManager am, Account account, String[] features) {
+        try {
+            return am.hasFeatures(account, features, null, null).getResult();
+        } catch (Exception e) {
+            Log.w(LOG_TAG, "Failed to get account feature", e);
+            return false;
+        }
+    }
+
+    @Override
+    public void setBackupServiceEnabled(ComponentName admin, boolean enabled) {
+        Preconditions.checkNotNull(admin);
+        if (!mHasFeature) {
+            return;
+        }
+        ensureDeviceOwnerManagingSingleUser(admin);
+        setBackupServiceEnabledInternal(enabled);
+    }
+
+    private synchronized void setBackupServiceEnabledInternal(boolean enabled) {
+        long ident = mInjector.binderClearCallingIdentity();
+        try {
+            IBackupManager ibm = mInjector.getIBackupManager();
+            if (ibm != null) {
+                ibm.setBackupServiceActive(UserHandle.USER_SYSTEM, enabled);
+            }
+        } catch (RemoteException e) {
+            throw new IllegalStateException(
+                "Failed " + (enabled ? "" : "de") + "activating backup service.", e);
+        } finally {
+            mInjector.binderRestoreCallingIdentity(ident);
+        }
+    }
+
+    @Override
+    public boolean isBackupServiceEnabled(ComponentName admin) {
+        Preconditions.checkNotNull(admin);
+        if (!mHasFeature) {
+            return true;
+        }
+        synchronized (this) {
+            getActiveAdminForCallerLocked(admin, DeviceAdminInfo.USES_POLICY_DEVICE_OWNER);
+            return isBackupServiceEnabledInternal();
+        }
+    }
+    private boolean isBackupServiceEnabledInternal() {
+        try {
+            IBackupManager ibm = mInjector.getIBackupManager();
+            return ibm != null && ibm.isBackupServiceActive(UserHandle.USER_SYSTEM);
+        } catch (RemoteException e) {
+            throw new IllegalStateException("Failed requesting backup service state.", e);
+        }
+    }
 }
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index 1242560..839ea0b 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -16,7 +16,6 @@
 
 package com.android.server;
 
-import android.app.ActivityManagerNative;
 import android.app.ActivityThread;
 import android.app.INotificationManager;
 import android.app.usage.UsageStatsManagerInternal;
@@ -32,7 +31,6 @@
 import android.os.Environment;
 import android.os.FactoryTest;
 import android.os.FileUtils;
-import android.os.IPowerManager;
 import android.os.Looper;
 import android.os.PowerManager;
 import android.os.RemoteException;
@@ -42,26 +40,33 @@
 import android.os.SystemProperties;
 import android.os.Trace;
 import android.os.UserHandle;
+import android.os.UserManager;
 import android.os.storage.IMountService;
+import android.provider.Settings;
 import android.util.DisplayMetrics;
 import android.util.EventLog;
 import android.util.Slog;
 import android.view.WindowManager;
 
 import com.android.internal.R;
+import com.android.internal.app.NightDisplayController;
 import com.android.internal.os.BinderInternal;
 import com.android.internal.os.SamplingProfilerIntegration;
 import com.android.internal.os.ZygoteInit;
+import com.android.internal.policy.EmergencyAffordanceManager;
 import com.android.internal.widget.ILockSettings;
 import com.android.server.accessibility.AccessibilityManagerService;
 import com.android.server.am.ActivityManagerService;
 import com.android.server.audio.AudioService;
 import com.android.server.camera.CameraService;
 import com.android.server.clipboard.ClipboardService;
+import com.android.server.connectivity.IpConnectivityMetrics;
 import com.android.server.connectivity.MetricsLoggerService;
 import com.android.server.devicepolicy.DevicePolicyManagerService;
 import com.android.server.display.DisplayManagerService;
+import com.android.server.display.NightDisplayService;
 import com.android.server.dreams.DreamManagerService;
+import com.android.server.emergency.EmergencyAffordanceService;
 import com.android.server.fingerprint.FingerprintService;
 import com.android.server.hdmi.HdmiControlService;
 import com.android.server.input.InputManagerService;
@@ -85,6 +90,7 @@
 import com.android.server.power.PowerManagerService;
 import com.android.server.power.ShutdownThread;
 import com.android.server.restrictions.RestrictionsManagerService;
+import com.android.server.retaildemo.RetailDemoModeService;
 import com.android.server.soundtrigger.SoundTriggerService;
 import com.android.server.statusbar.StatusBarManagerService;
 import com.android.server.storage.DeviceStorageMonitorService;
@@ -154,6 +160,10 @@
             "com.google.android.clockwork.ThermalObserver";
     private static final String WEAR_BLUETOOTH_SERVICE_CLASS =
             "com.google.android.clockwork.bluetooth.WearBluetoothService";
+    private static final String WEAR_WIFI_MEDIATOR_SERVICE_CLASS =
+            "com.google.android.clockwork.wifi.WearWifiMediatorService";
+    private static final String WEAR_TIME_SERVICE_CLASS =
+            "com.google.android.clockwork.time.WearTimeService";
     private static final String ACCOUNT_SERVICE_CLASS =
             "com.android.server.accounts.AccountManagerService$Lifecycle";
     private static final String CONTENT_SERVICE_CLASS =
@@ -176,7 +186,7 @@
      * visual content.
      */
     private static final int DEFAULT_SYSTEM_THEME =
-            com.android.internal.R.style.Theme_DeviceDefault_Light_DarkActionBar;
+            com.android.internal.R.style.Theme_DeviceDefault_System;
 
     private final int mFactoryTestMode;
     private Timer mProfilerSnapshotTimer;
@@ -539,9 +549,15 @@
                 false);
         boolean disableTrustManager = SystemProperties.getBoolean("config.disable_trustmanager",
                 false);
-        boolean disableTextServices = SystemProperties.getBoolean("config.disable_textservices", false);
+        boolean disableTextServices = SystemProperties.getBoolean("config.disable_textservices",
+                false);
         boolean disableSamplingProfiler = SystemProperties.getBoolean("config.disable_samplingprof",
                 false);
+        boolean disableConsumerIr = SystemProperties.getBoolean("config.disable_consumerir", false);
+        boolean disableVrManager = SystemProperties.getBoolean("config.disable_vrmanager", false);
+        boolean disableCameraService = SystemProperties.getBoolean("config.disable_cameraservice",
+                false);
+
         boolean isEmulator = SystemProperties.get("ro.kernel.qemu").equals("1");
 
         try {
@@ -565,8 +581,10 @@
 
             mContentResolver = context.getContentResolver();
 
-            Slog.i(TAG, "Camera Service");
-            mSystemServiceManager.startService(CameraService.class);
+            if (!disableCameraService) {
+                Slog.i(TAG, "Camera Service");
+                mSystemServiceManager.startService(CameraService.class);
+            }
 
             // The AccountManager must come before the ContentService
             traceBeginAndSlog("StartAccountManagerService");
@@ -586,10 +604,12 @@
             ServiceManager.addService("vibrator", vibrator);
             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            traceBeginAndSlog("StartConsumerIrService");
-            consumerIr = new ConsumerIrService(context);
-            ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr);
-            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+            if (!disableConsumerIr) {
+                traceBeginAndSlog("StartConsumerIrService");
+                consumerIr = new ConsumerIrService(context);
+                ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr);
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+            }
 
             traceBeginAndSlog("StartAlarmManagerService");
             mSystemServiceManager.startService(AlarmManagerService.class);
@@ -612,9 +632,11 @@
             ServiceManager.addService(Context.INPUT_SERVICE, inputManager);
             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-            traceBeginAndSlog("StartVrManagerService");
-            mSystemServiceManager.startService(VrManagerService.class);
-            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+            if (!disableVrManager) {
+                traceBeginAndSlog("StartVrManagerService");
+                mSystemServiceManager.startService(VrManagerService.class);
+                Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+            }
 
             mActivityManagerService.setWindowManager(wm);
 
@@ -644,6 +666,10 @@
             mSystemServiceManager.startService(MetricsLoggerService.class);
             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
+            traceBeginAndSlog("IpConnectivityMetrics");
+            mSystemServiceManager.startService(IpConnectivityMetrics.class);
+            Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
+
             traceBeginAndSlog("PinnerService");
             mSystemServiceManager.startService(PinnerService.class);
             Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
@@ -719,14 +745,6 @@
         }
         Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
 
-        try {
-            ActivityManagerNative.getDefault().showBootMessage(
-                    context.getResources().getText(
-                            com.android.internal.R.string.android_upgrading_starting_apps),
-                    false);
-        } catch (RemoteException e) {
-        }
-
         if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
             if (!disableNonCoreServices) {
                 traceBeginAndSlog("StartLockSettingsService");
@@ -808,10 +826,8 @@
 
                 traceBeginAndSlog("StartNetworkPolicyManagerService");
                 try {
-                    networkPolicy = new NetworkPolicyManagerService(
-                            context, mActivityManagerService,
-                            (IPowerManager)ServiceManager.getService(Context.POWER_SERVICE),
-                            networkStats, networkManagement);
+                    networkPolicy = new NetworkPolicyManagerService(context,
+                            mActivityManagerService, networkStats, networkManagement);
                     ServiceManager.addService(Context.NETWORK_POLICY_SERVICE, networkPolicy);
                 } catch (Throwable e) {
                     reportWtf("starting NetworkPolicy Service", e);
@@ -998,6 +1014,10 @@
 
             mSystemServiceManager.startService(TwilightService.class);
 
+            if (NightDisplayController.isAvailable(context)) {
+                mSystemServiceManager.startService(NightDisplayService.class);
+            }
+
             mSystemServiceManager.startService(JobSchedulerService.class);
 
             mSystemServiceManager.startService(SoundTriggerService.class);
@@ -1077,6 +1097,11 @@
                 Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
             }
 
+            if (!disableNetwork && !disableNonCoreServices && EmergencyAffordanceManager.ENABLED) {
+                // EmergencyMode sevice
+                mSystemServiceManager.startService(EmergencyAffordanceService.class);
+            }
+
             if (!disableNonCoreServices) {
                 // Dreams (interactive idle-time views, a/k/a screen savers, and doze mode)
                 mSystemServiceManager.startService(DreamManagerService.class);
@@ -1160,6 +1185,10 @@
 
         if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH)) {
             mSystemServiceManager.startService(WEAR_BLUETOOTH_SERVICE_CLASS);
+            mSystemServiceManager.startService(WEAR_WIFI_MEDIATOR_SERVICE_CLASS);
+          if (!disableNonCoreServices) {
+              mSystemServiceManager.startService(WEAR_TIME_SERVICE_CLASS);
+          }
         }
 
         // Before things start rolling, be sure we have decided whether
@@ -1177,6 +1206,11 @@
         // MMS service broker
         mmsService = mSystemServiceManager.startService(MmsServiceBroker.class);
 
+        if (Settings.Global.getInt(mContentResolver, Settings.Global.DEVICE_PROVISIONED, 0) == 0 ||
+                UserManager.isDeviceInDemoMode(mSystemContext)) {
+            mSystemServiceManager.startService(RetailDemoModeService.class);
+        }
+
         // It is now time to start up the app processes...
 
         Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakeVibratorServiceReady");
diff --git a/services/net/Android.mk b/services/net/Android.mk
index 336bc45..408794e 100644
--- a/services/net/Android.mk
+++ b/services/net/Android.mk
@@ -7,4 +7,7 @@
 LOCAL_SRC_FILES += \
     $(call all-java-files-under,java)
 
+LOCAL_AIDL_INCLUDES += \
+    system/netd/server/binder
+
 include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/services/net/java/android/net/apf/ApfFilter.java b/services/net/java/android/net/apf/ApfFilter.java
index bae889c..4c75452 100644
--- a/services/net/java/android/net/apf/ApfFilter.java
+++ b/services/net/java/android/net/apf/ApfFilter.java
@@ -18,15 +18,22 @@
 
 import static android.system.OsConstants.*;
 
+import android.os.SystemClock;
+import android.net.LinkAddress;
 import android.net.LinkProperties;
 import android.net.NetworkUtils;
 import android.net.apf.ApfGenerator;
 import android.net.apf.ApfGenerator.IllegalInstructionException;
 import android.net.apf.ApfGenerator.Register;
 import android.net.ip.IpManager;
+import android.net.metrics.ApfProgramEvent;
+import android.net.metrics.ApfStats;
+import android.net.metrics.IpConnectivityLog;
+import android.net.metrics.RaEvent;
 import android.system.ErrnoException;
 import android.system.Os;
 import android.system.PacketSocketAddress;
+import android.text.format.DateUtils;
 import android.util.Log;
 import android.util.Pair;
 
@@ -38,6 +45,7 @@
 import java.io.FileDescriptor;
 import java.io.IOException;
 import java.lang.Thread;
+import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
@@ -69,6 +77,17 @@
  * @hide
  */
 public class ApfFilter {
+
+    // Enums describing the outcome of receiving an RA packet.
+    private static enum ProcessRaResult {
+        MATCH,          // Received RA matched a known RA
+        DROPPED,        // Received RA ignored due to MAX_RAS
+        PARSE_ERROR,    // Received RA could not be parsed
+        ZERO_LIFETIME,  // Received RA had 0 lifetime
+        UPDATE_NEW_RA,  // APF program updated for new RA
+        UPDATE_EXPIRY   // APF program updated for expiry
+    }
+
     // Thread to listen for RAs.
     @VisibleForTesting
     class ReceiveThread extends Thread {
@@ -76,6 +95,16 @@
         private final FileDescriptor mSocket;
         private volatile boolean mStopped;
 
+        // Starting time of the RA receiver thread.
+        private final long mStart = SystemClock.elapsedRealtime();
+
+        private int mReceivedRas;     // Number of received RAs
+        private int mMatchingRas;     // Number of received RAs matching a known RA
+        private int mDroppedRas;      // Number of received RAs ignored due to the MAX_RAS limit
+        private int mParseErrors;     // Number of received RAs that could not be parsed
+        private int mZeroLifetimeRas; // Number of received RAs with a 0 lifetime
+        private int mProgramUpdates;  // Number of APF program updates triggered by receiving a RA
+
         public ReceiveThread(FileDescriptor socket) {
             mSocket = socket;
         }
@@ -94,13 +123,46 @@
             while (!mStopped) {
                 try {
                     int length = Os.read(mSocket, mPacket, 0, mPacket.length);
-                    processRa(mPacket, length);
+                    updateStats(processRa(mPacket, length));
                 } catch (IOException|ErrnoException e) {
                     if (!mStopped) {
                         Log.e(TAG, "Read error", e);
                     }
                 }
             }
+            logStats();
+        }
+
+        private void updateStats(ProcessRaResult result) {
+            mReceivedRas++;
+            switch(result) {
+                case MATCH:
+                    mMatchingRas++;
+                    return;
+                case DROPPED:
+                    mDroppedRas++;
+                    return;
+                case PARSE_ERROR:
+                    mParseErrors++;
+                    return;
+                case ZERO_LIFETIME:
+                    mZeroLifetimeRas++;
+                    return;
+                case UPDATE_EXPIRY:
+                    mMatchingRas++;
+                    mProgramUpdates++;
+                    return;
+                case UPDATE_NEW_RA:
+                    mProgramUpdates++;
+                    return;
+            }
+        }
+
+        private void logStats() {
+            long durationMs = SystemClock.elapsedRealtime() - mStart;
+            int maxSize = mApfCapabilities.maximumApfProgramSize;
+            mMetricsLog.log(new ApfStats(durationMs, mReceivedRas, mMatchingRas, mDroppedRas,
+                     mZeroLifetimeRas, mParseErrors, mProgramUpdates, maxSize));
         }
     }
 
@@ -111,8 +173,8 @@
     private static final int ETH_HEADER_LEN = 14;
     private static final int ETH_DEST_ADDR_OFFSET = 0;
     private static final int ETH_ETHERTYPE_OFFSET = 12;
-    private static final byte[] ETH_BROADCAST_MAC_ADDRESS = new byte[]{
-            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
+    private static final byte[] ETH_BROADCAST_MAC_ADDRESS =
+            {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
     // TODO: Make these offsets relative to end of link-layer header; don't include ETH_HEADER_LEN.
     private static final int IPV4_FRAGMENT_OFFSET_OFFSET = ETH_HEADER_LEN + 6;
     // Endianness is not an issue for this constant because the APF interpreter always operates in
@@ -120,6 +182,8 @@
     private static final int IPV4_FRAGMENT_OFFSET_MASK = 0x1fff;
     private static final int IPV4_PROTOCOL_OFFSET = ETH_HEADER_LEN + 9;
     private static final int IPV4_DEST_ADDR_OFFSET = ETH_HEADER_LEN + 16;
+    private static final int IPV4_ANY_HOST_ADDRESS = 0;
+    private static final int IPV4_BROADCAST_ADDRESS = -1; // 255.255.255.255
 
     private static final int IPV6_NEXT_HEADER_OFFSET = ETH_HEADER_LEN + 6;
     private static final int IPV6_SRC_ADDR_OFFSET = ETH_HEADER_LEN + 8;
@@ -127,7 +191,7 @@
     private static final int IPV6_HEADER_LEN = 40;
     // The IPv6 all nodes address ff02::1
     private static final byte[] IPV6_ALL_NODES_ADDRESS =
-            new byte[]{ (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
+            { (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
 
     private static final int ICMP6_TYPE_OFFSET = ETH_HEADER_LEN + IPV6_HEADER_LEN;
     private static final int ICMP6_NEIGHBOR_ANNOUNCEMENT = 136;
@@ -141,19 +205,22 @@
     // NOTE: this must be added to the IPv4 header length in IPV4_HEADER_SIZE_MEMORY_SLOT
     private static final int DHCP_CLIENT_MAC_OFFSET = ETH_HEADER_LEN + UDP_HEADER_LEN + 28;
 
-    private static int ARP_HEADER_OFFSET = ETH_HEADER_LEN;
-    private static final byte[] ARP_IPV4_REQUEST_HEADER = new byte[]{
+    private static final int ARP_HEADER_OFFSET = ETH_HEADER_LEN;
+    private static final int ARP_OPCODE_OFFSET = ARP_HEADER_OFFSET + 6;
+    private static final short ARP_OPCODE_REQUEST = 1;
+    private static final short ARP_OPCODE_REPLY = 2;
+    private static final byte[] ARP_IPV4_HEADER = {
             0, 1, // Hardware type: Ethernet (1)
             8, 0, // Protocol type: IP (0x0800)
             6,    // Hardware size: 6
             4,    // Protocol size: 4
-            0, 1  // Opcode: request (1)
     };
-    private static int ARP_TARGET_IP_ADDRESS_OFFSET = ETH_HEADER_LEN + 24;
+    private static final int ARP_TARGET_IP_ADDRESS_OFFSET = ETH_HEADER_LEN + 24;
 
     private final ApfCapabilities mApfCapabilities;
     private final IpManager.Callback mIpManagerCallback;
     private final NetworkInterface mNetworkInterface;
+    private final IpConnectivityLog mMetricsLog;
     @VisibleForTesting
     byte[] mHardwareAddress;
     @VisibleForTesting
@@ -165,14 +232,18 @@
     // Our IPv4 address, if we have just one, otherwise null.
     @GuardedBy("this")
     private byte[] mIPv4Address;
+    // The subnet prefix length of our IPv4 network. Only valid if mIPv4Address is not null.
+    @GuardedBy("this")
+    private int mIPv4PrefixLength;
 
     @VisibleForTesting
     ApfFilter(ApfCapabilities apfCapabilities, NetworkInterface networkInterface,
-            IpManager.Callback ipManagerCallback, boolean multicastFilter) {
+            IpManager.Callback ipManagerCallback, boolean multicastFilter, IpConnectivityLog log) {
         mApfCapabilities = apfCapabilities;
         mIpManagerCallback = ipManagerCallback;
         mNetworkInterface = networkInterface;
         mMulticastFilter = multicastFilter;
+        mMetricsLog = log;
 
         maybeStartFilter();
     }
@@ -213,8 +284,9 @@
     }
 
     // Returns seconds since Unix Epoch.
+    // TODO: use SystemClock.elapsedRealtime() instead
     private static long curTime() {
-        return System.currentTimeMillis() / 1000L;
+        return System.currentTimeMillis() / DateUtils.SECOND_IN_MILLIS;
     }
 
     // A class to hold information about an RA.
@@ -297,23 +369,7 @@
         }
 
         // Can't be static because it's in a non-static inner class.
-        // TODO: Make this final once RA is its own class.
-        private int uint8(byte b) {
-            return b & 0xff;
-        }
-
-        private int uint16(short s) {
-            return s & 0xffff;
-        }
-
-        private long uint32(int s) {
-            return s & 0xffffffffL;
-        }
-
-        private long getUint16(ByteBuffer buffer, int position) {
-            return uint16(buffer.getShort(position));
-        }
-
+        // TODO: Make this static once RA is its own class.
         private void prefixOptionToString(StringBuffer sb, int offset) {
             String prefix = IPv6AddresstoString(offset + 16);
             int length = uint8(mPacket.get(offset + 2));
@@ -360,7 +416,7 @@
          * @param lifetimeOffset offset from mPacket.position() to the next lifetime data.
          * @param lifetimeLength length of the next lifetime data.
          * @return offset within packet of where the next binary range of data not including
-         *         a lifetime.  This can be passed into the next invocation of this function
+         *         a lifetime. This can be passed into the next invocation of this function
          *         via {@code lastNonLifetimeStart}.
          */
         private int addNonLifetime(int lastNonLifetimeStart, int lifetimeOffset,
@@ -371,13 +427,17 @@
             return lifetimeOffset + lifetimeLength;
         }
 
+        private int addNonLifetimeU32(int lastNonLifetimeStart) {
+            return addNonLifetime(lastNonLifetimeStart,
+                    ICMP6_4_BYTE_LIFETIME_OFFSET, ICMP6_4_BYTE_LIFETIME_LEN);
+        }
+
         // Note that this parses RA and may throw IllegalArgumentException (from
         // Buffer.position(int) or due to an invalid-length option) or IndexOutOfBoundsException
         // (from ByteBuffer.get(int) ) if parsing encounters something non-compliant with
         // specifications.
         Ra(byte[] packet, int length) {
-            mPacket = ByteBuffer.allocate(length).put(ByteBuffer.wrap(packet, 0, length));
-            mPacket.clear();
+            mPacket = ByteBuffer.wrap(Arrays.copyOf(packet, length));
             mLastSeen = curTime();
 
             // Sanity check packet in case a packet arrives before we attach RA filter
@@ -389,6 +449,8 @@
             }
 
 
+            RaEvent.Builder builder = new RaEvent.Builder();
+
             // Ignore the checksum.
             int lastNonLifetimeStart = addNonLifetime(0,
                     ICMP6_RA_CHECKSUM_OFFSET,
@@ -398,35 +460,50 @@
             lastNonLifetimeStart = addNonLifetime(lastNonLifetimeStart,
                     ICMP6_RA_ROUTER_LIFETIME_OFFSET,
                     ICMP6_RA_ROUTER_LIFETIME_LEN);
+            builder.updateRouterLifetime(getUint16(mPacket, ICMP6_RA_ROUTER_LIFETIME_OFFSET));
 
             // Ensures that the RA is not truncated.
             mPacket.position(ICMP6_RA_OPTION_OFFSET);
             while (mPacket.hasRemaining()) {
-                int optionType = ((int)mPacket.get(mPacket.position())) & 0xff;
-                int optionLength = (((int)mPacket.get(mPacket.position() + 1)) & 0xff) * 8;
+                final int position = mPacket.position();
+                final int optionType = uint8(mPacket.get(position));
+                final int optionLength = uint8(mPacket.get(position + 1)) * 8;
+                long lifetime;
                 switch (optionType) {
                     case ICMP6_PREFIX_OPTION_TYPE:
                         // Parse valid lifetime
                         lastNonLifetimeStart = addNonLifetime(lastNonLifetimeStart,
                                 ICMP6_PREFIX_OPTION_VALID_LIFETIME_OFFSET,
                                 ICMP6_PREFIX_OPTION_VALID_LIFETIME_LEN);
+                        lifetime = getUint32(mPacket,
+                                position + ICMP6_PREFIX_OPTION_VALID_LIFETIME_OFFSET);
+                        builder.updatePrefixValidLifetime(lifetime);
                         // Parse preferred lifetime
                         lastNonLifetimeStart = addNonLifetime(lastNonLifetimeStart,
                                 ICMP6_PREFIX_OPTION_PREFERRED_LIFETIME_OFFSET,
                                 ICMP6_PREFIX_OPTION_PREFERRED_LIFETIME_LEN);
-                        mPrefixOptionOffsets.add(mPacket.position());
+                        lifetime = getUint32(mPacket,
+                                position + ICMP6_PREFIX_OPTION_PREFERRED_LIFETIME_OFFSET);
+                        builder.updatePrefixPreferredLifetime(lifetime);
+                        mPrefixOptionOffsets.add(position);
                         break;
-                    // These three options have the same lifetime offset and size, so process
-                    // together:
+                    // These three options have the same lifetime offset and size, and
+                    // are processed with the same specialized addNonLifetimeU32:
                     case ICMP6_RDNSS_OPTION_TYPE:
-                        mRdnssOptionOffsets.add(mPacket.position());
-                        // Fall through.
+                        mRdnssOptionOffsets.add(position);
+                        lastNonLifetimeStart = addNonLifetimeU32(lastNonLifetimeStart);
+                        lifetime = getUint32(mPacket, position + ICMP6_4_BYTE_LIFETIME_OFFSET);
+                        builder.updateRdnssLifetime(lifetime);
+                        break;
                     case ICMP6_ROUTE_INFO_OPTION_TYPE:
+                        lastNonLifetimeStart = addNonLifetimeU32(lastNonLifetimeStart);
+                        lifetime = getUint32(mPacket, position + ICMP6_4_BYTE_LIFETIME_OFFSET);
+                        builder.updateRouteInfoLifetime(lifetime);
+                        break;
                     case ICMP6_DNSSL_OPTION_TYPE:
-                        // Parse lifetime
-                        lastNonLifetimeStart = addNonLifetime(lastNonLifetimeStart,
-                                ICMP6_4_BYTE_LIFETIME_OFFSET,
-                                ICMP6_4_BYTE_LIFETIME_LEN);
+                        lastNonLifetimeStart = addNonLifetimeU32(lastNonLifetimeStart);
+                        lifetime = getUint32(mPacket, position + ICMP6_4_BYTE_LIFETIME_OFFSET);
+                        builder.updateDnsslLifetime(lifetime);
                         break;
                     default:
                         // RFC4861 section 4.2 dictates we ignore unknown options for fowards
@@ -437,11 +514,12 @@
                     throw new IllegalArgumentException(String.format(
                         "Invalid option length opt=%d len=%d", optionType, optionLength));
                 }
-                mPacket.position(mPacket.position() + optionLength);
+                mPacket.position(position + optionLength);
             }
             // Mark non-lifetime bytes since last lifetime.
             addNonLifetime(lastNonLifetimeStart, 0, 0);
             mMinLifetime = minLifetime(packet, length);
+            mMetricsLog.log(builder.build());
         }
 
         // Ignoring lifetimes (which may change) does {@code packet} match this RA?
@@ -470,16 +548,19 @@
                      continue;
                 }
 
-                int lifetimeLength = mNonLifetimes.get(i+1).first - offset;
-                long val;
+                final int lifetimeLength = mNonLifetimes.get(i+1).first - offset;
+                final long optionLifetime;
                 switch (lifetimeLength) {
-                    case 2: val = byteBuffer.getShort(offset); break;
-                    case 4: val = byteBuffer.getInt(offset); break;
-                    default: throw new IllegalStateException("bogus lifetime size " + length);
+                    case 2:
+                        optionLifetime = uint16(byteBuffer.getShort(offset));
+                        break;
+                    case 4:
+                        optionLifetime = uint32(byteBuffer.getInt(offset));
+                        break;
+                    default:
+                        throw new IllegalStateException("bogus lifetime size " + lifetimeLength);
                 }
-                // Mask to size, converting signed to unsigned
-                val &= (1L << (lifetimeLength * 8)) - 1;
-                minLifetime = Math.min(minLifetime, val);
+                minLifetime = Math.min(minLifetime, optionLifetime);
             }
             return minLifetime;
         }
@@ -586,23 +667,48 @@
     private void generateArpFilterLocked(ApfGenerator gen) throws IllegalInstructionException {
         // Here's a basic summary of what the ARP filter program does:
         //
-        // if interface has IPv4 address:
-        //   if it's not an ARP IPv4 request:
-        //     pass
-        //   if it's not a request for our IPv4 address:
-        //     drop
+        // if not ARP IPv4
+        //   pass
+        // if not ARP IPv4 reply or request
+        //   pass
+        // if unicast ARP reply
+        //   pass
+        // if interface has no IPv4 address
+        //   if target ip is 0.0.0.0
+        //      drop
+        // else
+        //   if target ip is not the interface ip
+        //      drop
         // pass
 
-        if (mIPv4Address != null) {
-            // if it's not an ARP IPv4 request, pass
-            gen.addLoadImmediate(Register.R0, ARP_HEADER_OFFSET);
-            gen.addJumpIfBytesNotEqual(Register.R0, ARP_IPV4_REQUEST_HEADER, gen.PASS_LABEL);
-            // if it's not a request for our IPv4 address, drop
+        final String checkTargetIPv4 = "checkTargetIPv4";
+
+        // Pass if not ARP IPv4.
+        gen.addLoadImmediate(Register.R0, ARP_HEADER_OFFSET);
+        gen.addJumpIfBytesNotEqual(Register.R0, ARP_IPV4_HEADER, gen.PASS_LABEL);
+
+        // Pass if unknown ARP opcode.
+        gen.addLoad16(Register.R0, ARP_OPCODE_OFFSET);
+        gen.addJumpIfR0Equals(ARP_OPCODE_REQUEST, checkTargetIPv4); // Skip to unicast check
+        gen.addJumpIfR0NotEquals(ARP_OPCODE_REPLY, gen.PASS_LABEL);
+
+        // Pass if unicast reply.
+        gen.addLoadImmediate(Register.R0, ETH_DEST_ADDR_OFFSET);
+        gen.addJumpIfBytesNotEqual(Register.R0, ETH_BROADCAST_MAC_ADDRESS, gen.PASS_LABEL);
+
+        // Either a unicast request, a unicast reply, or a broadcast reply.
+        gen.defineLabel(checkTargetIPv4);
+        if (mIPv4Address == null) {
+            // When there is no IPv4 address, drop GARP replies (b/29404209).
+            gen.addLoad32(Register.R0, ARP_TARGET_IP_ADDRESS_OFFSET);
+            gen.addJumpIfR0Equals(IPV4_ANY_HOST_ADDRESS, gen.DROP_LABEL);
+        } else {
+            // When there is an IPv4 address, drop unicast/broadcast requests
+            // and broadcast replies with a different target IPv4 address.
             gen.addLoadImmediate(Register.R0, ARP_TARGET_IP_ADDRESS_OFFSET);
             gen.addJumpIfBytesNotEqual(Register.R0, mIPv4Address, gen.DROP_LABEL);
         }
 
-        // Otherwise, pass
         gen.addJump(gen.PASS_LABEL);
     }
 
@@ -617,39 +723,57 @@
         // Here's a basic summary of what the IPv4 filter program does:
         //
         // if filtering multicast (i.e. multicast lock not held):
-        //   if it's multicast:
-        //     drop
-        //   if it's not broadcast:
+        //   if it's DHCP destined to our MAC:
         //     pass
-        //   if it's not DHCP destined to our MAC:
+        //   if it's L2 broadcast:
+        //     drop
+        //   if it's IPv4 multicast:
+        //     drop
+        //   if it's IPv4 broadcast:
         //     drop
         // pass
 
         if (mMulticastFilter) {
-            // Check for multicast destination address range
+            final String skipDhcpv4Filter = "skip_dhcp_v4_filter";
+
+            // Pass DHCP addressed to us.
+            // Check it's UDP.
+            gen.addLoad8(Register.R0, IPV4_PROTOCOL_OFFSET);
+            gen.addJumpIfR0NotEquals(IPPROTO_UDP, skipDhcpv4Filter);
+            // Check it's not a fragment. This matches the BPF filter installed by the DHCP client.
+            gen.addLoad16(Register.R0, IPV4_FRAGMENT_OFFSET_OFFSET);
+            gen.addJumpIfR0AnyBitsSet(IPV4_FRAGMENT_OFFSET_MASK, skipDhcpv4Filter);
+            // Check it's addressed to DHCP client port.
+            gen.addLoadFromMemory(Register.R1, gen.IPV4_HEADER_SIZE_MEMORY_SLOT);
+            gen.addLoad16Indexed(Register.R0, UDP_DESTINATION_PORT_OFFSET);
+            gen.addJumpIfR0NotEquals(DHCP_CLIENT_PORT, skipDhcpv4Filter);
+            // Check it's DHCP to our MAC address.
+            gen.addLoadImmediate(Register.R0, DHCP_CLIENT_MAC_OFFSET);
+            // NOTE: Relies on R1 containing IPv4 header offset.
+            gen.addAddR1();
+            gen.addJumpIfBytesNotEqual(Register.R0, mHardwareAddress, skipDhcpv4Filter);
+            gen.addJump(gen.PASS_LABEL);
+
+            // Drop all multicasts/broadcasts.
+            gen.defineLabel(skipDhcpv4Filter);
+
+            // If IPv4 destination address is in multicast range, drop.
             gen.addLoad8(Register.R0, IPV4_DEST_ADDR_OFFSET);
             gen.addAnd(0xf0);
             gen.addJumpIfR0Equals(0xe0, gen.DROP_LABEL);
 
-            // Drop all broadcasts besides DHCP addressed to us
-            // If not a broadcast packet, pass
+            // If IPv4 broadcast packet, drop regardless of L2 (b/30231088).
+            gen.addLoad32(Register.R0, IPV4_DEST_ADDR_OFFSET);
+            gen.addJumpIfR0Equals(IPV4_BROADCAST_ADDRESS, gen.DROP_LABEL);
+            if (mIPv4Address != null && mIPv4PrefixLength < 31) {
+                int broadcastAddr = ipv4BroadcastAddress(mIPv4Address, mIPv4PrefixLength);
+                gen.addJumpIfR0Equals(broadcastAddr, gen.DROP_LABEL);
+            }
+
+            // If L2 broadcast packet, drop.
             gen.addLoadImmediate(Register.R0, ETH_DEST_ADDR_OFFSET);
             gen.addJumpIfBytesNotEqual(Register.R0, ETH_BROADCAST_MAC_ADDRESS, gen.PASS_LABEL);
-            // If not UDP, drop
-            gen.addLoad8(Register.R0, IPV4_PROTOCOL_OFFSET);
-            gen.addJumpIfR0NotEquals(IPPROTO_UDP, gen.DROP_LABEL);
-            // If fragment, drop. This matches the BPF filter installed by the DHCP client.
-            gen.addLoad16(Register.R0, IPV4_FRAGMENT_OFFSET_OFFSET);
-            gen.addJumpIfR0AnyBitsSet(IPV4_FRAGMENT_OFFSET_MASK, gen.DROP_LABEL);
-            // If not to DHCP client port, drop
-            gen.addLoadFromMemory(Register.R1, gen.IPV4_HEADER_SIZE_MEMORY_SLOT);
-            gen.addLoad16Indexed(Register.R0, UDP_DESTINATION_PORT_OFFSET);
-            gen.addJumpIfR0NotEquals(DHCP_CLIENT_PORT, gen.DROP_LABEL);
-            // If not DHCP to our MAC address, drop
-            gen.addLoadImmediate(Register.R0, DHCP_CLIENT_MAC_OFFSET);
-            // NOTE: Relies on R1 containing IPv4 header offset.
-            gen.addAddR1();
-            gen.addJumpIfBytesNotEqual(Register.R0, mHardwareAddress, gen.DROP_LABEL);
+            gen.addJump(gen.DROP_LABEL);
         }
 
         // Otherwise, pass
@@ -774,16 +898,19 @@
         return gen;
     }
 
+    /**
+     * Generate and install a new filter program.
+     */
     @GuardedBy("this")
     @VisibleForTesting
     void installNewProgramLocked() {
         purgeExpiredRasLocked();
+        ArrayList<Ra> rasToFilter = new ArrayList<>();
         final byte[] program;
         long programMinLifetime = Long.MAX_VALUE;
         try {
             // Step 1: Determine how many RA filters we can fit in the program.
             ApfGenerator gen = beginProgramLocked();
-            ArrayList<Ra> rasToFilter = new ArrayList<Ra>();
             for (Ra ra : mRas) {
                 ra.generateFilterLocked(gen);
                 // Stop if we get too big.
@@ -811,17 +938,17 @@
             hexDump("Installing filter: ", program, program.length);
         }
         mIpManagerCallback.installPacketFilter(program);
+        int flags = ApfProgramEvent.flagsFor(mIPv4Address != null, mMulticastFilter);
+        mMetricsLog.log(new ApfProgramEvent(
+                programMinLifetime, rasToFilter.size(), mRas.size(), program.length, flags));
     }
 
-    // Install a new filter program if the last installed one will die soon.
-    @GuardedBy("this")
-    private void maybeInstallNewProgramLocked() {
-        if (mRas.size() == 0) return;
-        // If the current program doesn't expire for a while, don't bother updating.
+    /**
+     * Returns {@code true} if a new program should be installed because the current one dies soon.
+     */
+    private boolean shouldInstallnewProgram() {
         long expiry = mLastTimeInstalledProgram + mLastInstalledProgramMinLifetime;
-        if (expiry < curTime() + MAX_PROGRAM_LIFETIME_WORTH_REFRESHING) {
-            installNewProgramLocked();
-        }
+        return expiry < curTime() + MAX_PROGRAM_LIFETIME_WORTH_REFRESHING;
     }
 
     private void hexDump(String msg, byte[] packet, int length) {
@@ -840,7 +967,12 @@
         }
     }
 
-    private synchronized void processRa(byte[] packet, int length) {
+    /**
+     * Process an RA packet, updating the list of known RAs and installing a new APF program
+     * if the current APF program should be updated.
+     * @return a ProcessRaResult enum describing what action was performed.
+     */
+    private synchronized ProcessRaResult processRa(byte[] packet, int length) {
         if (VDBG) hexDump("Read packet = ", packet, length);
 
         // Have we seen this RA before?
@@ -862,25 +994,34 @@
                 // Swap to front of array.
                 mRas.add(0, mRas.remove(i));
 
-                maybeInstallNewProgramLocked();
-                return;
+                // If the current program doesn't expire for a while, don't update.
+                if (shouldInstallnewProgram()) {
+                    installNewProgramLocked();
+                    return ProcessRaResult.UPDATE_EXPIRY;
+                }
+                return ProcessRaResult.MATCH;
             }
         }
         purgeExpiredRasLocked();
         // TODO: figure out how to proceed when we've received more then MAX_RAS RAs.
-        if (mRas.size() >= MAX_RAS) return;
+        if (mRas.size() >= MAX_RAS) {
+            return ProcessRaResult.DROPPED;
+        }
         final Ra ra;
         try {
             ra = new Ra(packet, length);
         } catch (Exception e) {
             Log.e(TAG, "Error parsing RA: " + e);
-            return;
+            return ProcessRaResult.PARSE_ERROR;
         }
         // Ignore 0 lifetime RAs.
-        if (ra.isExpired()) return;
+        if (ra.isExpired()) {
+            return ProcessRaResult.ZERO_LIFETIME;
+        }
         log("Adding " + ra);
         mRas.add(ra);
         installNewProgramLocked();
+        return ProcessRaResult.UPDATE_NEW_RA;
     }
 
     /**
@@ -905,7 +1046,8 @@
             Log.e(TAG, "Unsupported APF version: " + apfCapabilities.apfVersionSupported);
             return null;
         }
-        return new ApfFilter(apfCapabilities, networkInterface, ipManagerCallback, multicastFilter);
+        return new ApfFilter(apfCapabilities, networkInterface, ipManagerCallback,
+                multicastFilter, new IpConnectivityLog());
     }
 
     public synchronized void shutdown() {
@@ -924,26 +1066,32 @@
         }
     }
 
-    // Find the single IPv4 address if there is one, otherwise return null.
-    private static byte[] findIPv4Address(LinkProperties lp) {
-        byte[] ipv4Address = null;
-        for (InetAddress inetAddr : lp.getAddresses()) {
-            byte[] addr = inetAddr.getAddress();
-            if (addr.length != 4) continue;
-            // More than one IPv4 address, abort
-            if (ipv4Address != null && !Arrays.equals(ipv4Address, addr)) return null;
-            ipv4Address = addr;
+    /** Find the single IPv4 LinkAddress if there is one, otherwise return null. */
+    private static LinkAddress findIPv4LinkAddress(LinkProperties lp) {
+        LinkAddress ipv4Address = null;
+        for (LinkAddress address : lp.getLinkAddresses()) {
+            if (!(address.getAddress() instanceof Inet4Address)) {
+                continue;
+            }
+            if (ipv4Address != null && !ipv4Address.isSameAddressAs(address)) {
+                // More than one IPv4 address, abort.
+                return null;
+            }
+            ipv4Address = address;
         }
         return ipv4Address;
     }
 
     public synchronized void setLinkProperties(LinkProperties lp) {
         // NOTE: Do not keep a copy of LinkProperties as it would further duplicate state.
-        byte[] ipv4Address = findIPv4Address(lp);
-        // If ipv4Address is the same as mIPv4Address, then there's no change, just return.
-        if (Arrays.equals(ipv4Address, mIPv4Address)) return;
-        // Otherwise update mIPv4Address and install new program.
-        mIPv4Address = ipv4Address;
+        final LinkAddress ipv4Address = findIPv4LinkAddress(lp);
+        final byte[] addr = (ipv4Address != null) ? ipv4Address.getAddress().getAddress() : null;
+        final int prefix = (ipv4Address != null) ? ipv4Address.getPrefixLength() : 0;
+        if ((prefix == mIPv4PrefixLength) && Arrays.equals(addr, mIPv4Address)) {
+            return;
+        }
+        mIPv4Address = addr;
+        mIPv4PrefixLength = prefix;
         installNewProgramLocked();
     }
 
@@ -989,4 +1137,38 @@
             pw.decreaseIndent();
         }
     }
+
+    private static int uint8(byte b) {
+        return b & 0xff;
+    }
+
+    private static int uint16(short s) {
+        return s & 0xffff;
+    }
+
+    private static long uint32(int i) {
+        return i & 0xffffffffL;
+    }
+
+    private static long getUint16(ByteBuffer buffer, int position) {
+        return uint16(buffer.getShort(position));
+    }
+
+    private static long getUint32(ByteBuffer buffer, int position) {
+        return uint32(buffer.getInt(position));
+    }
+
+    // TODO: move to android.net.NetworkUtils
+    @VisibleForTesting
+    public static int ipv4BroadcastAddress(byte[] addrBytes, int prefixLength) {
+        return bytesToInt(addrBytes) | (int) (uint32(-1) >>> prefixLength);
+    }
+
+    @VisibleForTesting
+    public static int bytesToInt(byte[] addrBytes) {
+        return (uint8(addrBytes[0]) << 24)
+                + (uint8(addrBytes[1]) << 16)
+                + (uint8(addrBytes[2]) << 8)
+                + (uint8(addrBytes[3]));
+    }
 }
diff --git a/services/net/java/android/net/dhcp/DhcpClient.java b/services/net/java/android/net/dhcp/DhcpClient.java
index 96c852b..ffbea9f 100644
--- a/services/net/java/android/net/dhcp/DhcpClient.java
+++ b/services/net/java/android/net/dhcp/DhcpClient.java
@@ -30,6 +30,7 @@
 import android.net.InterfaceConfiguration;
 import android.net.LinkAddress;
 import android.net.NetworkUtils;
+import android.net.metrics.IpConnectivityLog;
 import android.net.metrics.DhcpClientEvent;
 import android.net.metrics.DhcpErrorEvent;
 import android.os.Message;
@@ -163,6 +164,7 @@
     // System services / libraries we use.
     private final Context mContext;
     private final Random mRandom;
+    private final IpConnectivityLog mMetricsLog = new IpConnectivityLog();
 
     // Sockets.
     // - We use a packet socket to receive, because servers send us packets bound for IP addresses
@@ -192,6 +194,10 @@
     private long mDhcpLeaseExpiry;
     private DhcpResults mOffer;
 
+    // Milliseconds SystemClock timestamps used to record transition times to DhcpBoundState.
+    private long mLastInitEnterTime;
+    private long mLastBoundExitTime;
+
     // States.
     private State mStoppedState = new StoppedState();
     private State mDhcpState = new DhcpState();
@@ -356,14 +362,14 @@
                 } catch (IOException|ErrnoException e) {
                     if (!mStopped) {
                         Log.e(TAG, "Read error", e);
-                        DhcpErrorEvent.logReceiveError(mIfaceName);
+                        logError(DhcpErrorEvent.RECEIVE_ERROR);
                     }
                 } catch (DhcpPacket.ParseException e) {
                     Log.e(TAG, "Can't parse packet: " + e.getMessage());
                     if (PACKET_DBG) {
                         Log.d(TAG, HexDump.dumpHexString(mPacket, 0, length));
                     }
-                    DhcpErrorEvent.logParseError(mIfaceName, e.errorCode);
+                    logError(e.errorCode);
                 }
             }
             if (DBG) Log.d(TAG, "Receive thread stopped");
@@ -490,10 +496,18 @@
     }
 
     abstract class LoggingState extends State {
+        private long mEnterTimeMs;
+
         @Override
         public void enter() {
             if (STATE_DBG) Log.d(TAG, "Entering state " + getName());
-            DhcpClientEvent.logStateEvent(mIfaceName, getName());
+            mEnterTimeMs = SystemClock.elapsedRealtime();
+        }
+
+        @Override
+        public void exit() {
+            long durationMs = SystemClock.elapsedRealtime() - mEnterTimeMs;
+            logState(getName(), (int) durationMs);
         }
 
         private String messageName(int what) {
@@ -518,6 +532,13 @@
             }
             return NOT_HANDLED;
         }
+
+        @Override
+        public String getName() {
+            // All DhcpClient's states are inner classes with a well defined name.
+            // Use getSimpleName() and avoid super's getName() creating new String instances.
+            return getClass().getSimpleName();
+        }
     }
 
     // Sends CMD_PRE_DHCP_ACTION to the controller, waits for the controller to respond with
@@ -544,10 +565,9 @@
         }
     }
 
-    class StoppedState extends LoggingState {
+    class StoppedState extends State {
         @Override
         public boolean processMessage(Message message) {
-            super.processMessage(message);
             switch (message.what) {
                 case CMD_START_DHCP:
                     if (mRegisteredForPreDhcpNotification) {
@@ -576,10 +596,9 @@
         }
     }
 
-    class DhcpState extends LoggingState {
+    class DhcpState extends State {
         @Override
         public void enter() {
-            super.enter();
             clearDhcpState();
             if (initInterface() && initSockets()) {
                 mReceiveThread = new ReceiveThread();
@@ -677,7 +696,9 @@
             }
         }
 
+        @Override
         public void exit() {
+            super.exit();
             mKickAlarm.cancel();
             mTimeoutAlarm.cancel();
         }
@@ -724,6 +745,7 @@
         public void enter() {
             super.enter();
             startNewTransaction();
+            mLastInitEnterTime = SystemClock.elapsedRealtime();
         }
 
         protected boolean sendPacket() {
@@ -782,15 +804,9 @@
         }
     }
 
-    class DhcpHaveLeaseState extends LoggingState {
-        @Override
-        public void enter() {
-            super.enter();
-        }
-
+    class DhcpHaveLeaseState extends State {
         @Override
         public boolean processMessage(Message message) {
-            super.processMessage(message);
             switch (message.what) {
                 case CMD_EXPIRE_DHCP:
                     Log.d(TAG, "Lease expired!");
@@ -854,6 +870,13 @@
             }
 
             scheduleLeaseTimers();
+            logTimeToBoundState();
+        }
+
+        @Override
+        public void exit() {
+            super.exit();
+            mLastBoundExitTime = SystemClock.elapsedRealtime();
         }
 
         @Override
@@ -871,6 +894,15 @@
                     return NOT_HANDLED;
             }
         }
+
+        private void logTimeToBoundState() {
+            long now = SystemClock.elapsedRealtime();
+            if (mLastBoundExitTime > mLastInitEnterTime) {
+                logState(DhcpClientEvent.RENEWING_BOUND, (int)(now - mLastBoundExitTime));
+            } else {
+                logState(DhcpClientEvent.INITIAL_BOUND, (int)(now - mLastInitEnterTime));
+            }
+        }
     }
 
     abstract class DhcpReacquiringState extends PacketRetransmittingState {
@@ -977,4 +1009,12 @@
 
     class DhcpRebootingState extends LoggingState {
     }
+
+    private void logError(int errorCode) {
+        mMetricsLog.log(new DhcpErrorEvent(mIfaceName, errorCode));
+    }
+
+    private void logState(String name, int durationMs) {
+        mMetricsLog.log(new DhcpClientEvent(mIfaceName, name, durationMs));
+    }
 }
diff --git a/services/net/java/android/net/dhcp/DhcpPacket.java b/services/net/java/android/net/dhcp/DhcpPacket.java
index 9aa66fe..ef4bc02 100644
--- a/services/net/java/android/net/dhcp/DhcpPacket.java
+++ b/services/net/java/android/net/dhcp/DhcpPacket.java
@@ -7,6 +7,7 @@
 import android.os.Build;
 import android.os.SystemProperties;
 import android.system.OsConstants;
+import com.android.internal.annotations.VisibleForTesting;
 
 import java.io.UnsupportedEncodingException;
 import java.net.Inet4Address;
@@ -14,9 +15,8 @@
 import java.nio.BufferUnderflowException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
-import java.nio.charset.StandardCharsets;
 import java.nio.ShortBuffer;
-
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -725,7 +725,8 @@
      * A subset of the optional parameters are parsed and are stored
      * in object fields.
      */
-    public static DhcpPacket decodeFullPacket(ByteBuffer packet, int pktType) throws ParseException
+    @VisibleForTesting
+    static DhcpPacket decodeFullPacket(ByteBuffer packet, int pktType) throws ParseException
     {
         // bootp parameters
         int transactionId;
@@ -894,8 +895,12 @@
                         + 64    // skip server host name (64 chars)
                         + 128); // skip boot file name (128 chars)
 
-        int dhcpMagicCookie = packet.getInt();
+        // Ensure this is a DHCP packet with a magic cookie, and not BOOTP. http://b/31850211
+        if (packet.remaining() < 4) {
+            throw new ParseException(DhcpErrorEvent.DHCP_NO_COOKIE, "not a DHCP message");
+        }
 
+        int dhcpMagicCookie = packet.getInt();
         if (dhcpMagicCookie != DHCP_MAGIC_COOKIE) {
             throw new ParseException(DhcpErrorEvent.DHCP_BAD_MAGIC_COOKIE,
                     "Bad magic cookie 0x%08x, should be 0x%08x",
@@ -1090,7 +1095,13 @@
     public static DhcpPacket decodeFullPacket(byte[] packet, int length, int pktType)
             throws ParseException {
         ByteBuffer buffer = ByteBuffer.wrap(packet, 0, length).order(ByteOrder.BIG_ENDIAN);
-        return decodeFullPacket(buffer, pktType);
+        try {
+            return decodeFullPacket(buffer, pktType);
+        } catch (ParseException e) {
+            throw e;
+        } catch (Exception e) {
+            throw new ParseException(DhcpErrorEvent.PARSING_ERROR, e.getMessage());
+        }
     }
 
     /**
diff --git a/services/net/java/android/net/ip/IpManager.java b/services/net/java/android/net/ip/IpManager.java
index cece6c8..01d9304 100644
--- a/services/net/java/android/net/ip/IpManager.java
+++ b/services/net/java/android/net/ip/IpManager.java
@@ -31,7 +31,9 @@
 import android.net.RouteInfo;
 import android.net.StaticIpConfiguration;
 import android.net.dhcp.DhcpClient;
+import android.net.metrics.IpConnectivityLog;
 import android.net.metrics.IpManagerEvent;
+import android.net.util.AvoidBadWifiTracker;
 import android.os.INetworkManagementService;
 import android.os.Message;
 import android.os.RemoteException;
@@ -44,6 +46,7 @@
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.util.IndentingPrintWriter;
+import com.android.internal.util.IState;
 import com.android.internal.util.State;
 import com.android.internal.util.StateMachine;
 import com.android.server.net.NetlinkTracker;
@@ -356,6 +359,7 @@
     }
 
     public static final String DUMP_ARG = "ipmanager";
+    public static final String DUMP_ARG_CONFIRM = "confirm";
 
     private static final int CMD_STOP = 1;
     private static final int CMD_START = 2;
@@ -381,6 +385,7 @@
     private final State mStoppedState = new StoppedState();
     private final State mStoppingState = new StoppingState();
     private final State mStartedState = new StartedState();
+    private final State mRunningState = new RunningState();
 
     private final String mTag;
     private final Context mContext;
@@ -392,7 +397,10 @@
     private final NetlinkTracker mNetlinkTracker;
     private final WakeupMessage mProvisioningTimeoutAlarm;
     private final WakeupMessage mDhcpActionTimeoutAlarm;
+    private final AvoidBadWifiTracker mAvoidBadWifiTracker;
     private final LocalLog mLocalLog;
+    private final MessageHandlingLogger mMsgStateLogger;
+    private final IpConnectivityLog mMetricsLog = new IpConnectivityLog();
 
     private NetworkInterface mNetworkInterface;
 
@@ -464,6 +472,8 @@
             Log.e(mTag, "Couldn't register NetlinkTracker: " + e.toString());
         }
 
+        mAvoidBadWifiTracker = new AvoidBadWifiTracker(mContext, getHandler());
+
         resetLinkProperties();
 
         mProvisioningTimeoutAlarm = new WakeupMessage(mContext, getHandler(),
@@ -474,10 +484,12 @@
         // Super simple StateMachine.
         addState(mStoppedState);
         addState(mStartedState);
+            addState(mRunningState, mStartedState);
         addState(mStoppingState);
 
         setInitialState(mStoppedState);
         mLocalLog = new LocalLog(MAX_LOG_RECORDS);
+        mMsgStateLogger = new MessageHandlingLogger();
         super.start();
     }
 
@@ -555,6 +567,12 @@
     }
 
     public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
+        if (args.length > 0 && DUMP_ARG_CONFIRM.equals(args[0])) {
+            // Execute confirmConfiguration() and take no further action.
+            confirmConfiguration();
+            return;
+        }
+
         IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");
         pw.println("APF dump:");
         pw.increaseIndent();
@@ -568,7 +586,7 @@
         pw.decreaseIndent();
 
         pw.println();
-        pw.println("StateMachine dump:");
+        pw.println(mTag + " StateMachine dump:");
         pw.increaseIndent();
         mLocalLog.readOnlyLocalLog().dump(fd, pw, args);
         pw.decreaseIndent();
@@ -587,9 +605,9 @@
     @Override
     protected String getLogRecString(Message msg) {
         final String logLine = String.format(
-                "%s/%d %d %d %s",
+                "%s/%d %d %d %s [%s]",
                 mInterfaceName, mNetworkInterface == null ? -1 : mNetworkInterface.getIndex(),
-                msg.arg1, msg.arg2, Objects.toString(msg.obj));
+                msg.arg1, msg.arg2, Objects.toString(msg.obj), mMsgStateLogger);
 
         final String richerLogLine = getWhatToString(msg.what) + " " + logLine;
         mLocalLog.log(richerLogLine);
@@ -597,6 +615,7 @@
             Log.d(mTag, richerLogLine);
         }
 
+        mMsgStateLogger.reset();
         return logLine;
     }
 
@@ -605,7 +624,11 @@
         // Don't log EVENT_NETLINK_LINKPROPERTIES_CHANGED. They can be noisy,
         // and we already log any LinkProperties change that results in an
         // invocation of IpManager.Callback#onLinkPropertiesChange().
-        return (msg.what != EVENT_NETLINK_LINKPROPERTIES_CHANGED);
+        final boolean shouldLog = (msg.what != EVENT_NETLINK_LINKPROPERTIES_CHANGED);
+        if (!shouldLog) {
+            mMsgStateLogger.reset();
+        }
+        return shouldLog;
     }
 
     private void getNetworkInterface() {
@@ -634,8 +657,8 @@
 
     private void recordMetric(final int type) {
         if (mStartTimeMillis <= 0) { Log.wtf(mTag, "Start time undefined!"); }
-        IpManagerEvent.logEvent(type, mInterfaceName,
-                SystemClock.elapsedRealtime() - mStartTimeMillis);
+        final long duration = SystemClock.elapsedRealtime() - mStartTimeMillis;
+        mMetricsLog.log(new IpManagerEvent(mInterfaceName, type, duration));
     }
 
     // For now: use WifiStateMachine's historical notion of provisioned.
@@ -651,7 +674,7 @@
     // object that is a correct and complete assessment of what changed, taking
     // account of the asymmetries described in the comments in this function.
     // Then switch to using it everywhere (IpReachabilityMonitor, etc.).
-    private static ProvisioningChange compareProvisioning(
+    private ProvisioningChange compareProvisioning(
             LinkProperties oldLp, LinkProperties newLp) {
         ProvisioningChange delta;
 
@@ -678,6 +701,25 @@
             delta = ProvisioningChange.LOST_PROVISIONING;
         }
 
+        final boolean lostIPv6 = oldLp.isIPv6Provisioned() && !newLp.isIPv6Provisioned();
+        final boolean lostIPv4Address = oldLp.hasIPv4Address() && !newLp.hasIPv4Address();
+        final boolean lostIPv6Router = oldLp.hasIPv6DefaultRoute() && !newLp.hasIPv6DefaultRoute();
+
+        // If bad wifi avoidance is disabled, then ignore IPv6 loss of
+        // provisioning. Otherwise, when a hotspot that loses Internet
+        // access sends out a 0-lifetime RA to its clients, the clients
+        // will disconnect and then reconnect, avoiding the bad hotspot,
+        // instead of getting stuck on the bad hotspot. http://b/31827713 .
+        //
+        // This is incorrect because if the hotspot then regains Internet
+        // access with a different prefix, TCP connections on the
+        // deprecated addresses will remain stuck.
+        //
+        // Note that we can still be disconnected by IpReachabilityMonitor
+        // if the IPv6 default gateway (but not the IPv6 DNS servers; see
+        // accompanying code in IpReachabilityMonitor) is unreachable.
+        final boolean ignoreIPv6ProvisioningLoss = !mAvoidBadWifiTracker.currentValue();
+
         // Additionally:
         //
         // Partial configurations (e.g., only an IPv4 address with no DNS
@@ -690,8 +732,7 @@
         // Because on such a network isProvisioned() will always return false,
         // delta will never be LOST_PROVISIONING. So check for loss of
         // provisioning here too.
-        if ((oldLp.hasIPv4Address() && !newLp.hasIPv4Address()) ||
-                (oldLp.isIPv6Provisioned() && !newLp.isIPv6Provisioned())) {
+        if (lostIPv4Address || (lostIPv6 && !ignoreIPv6ProvisioningLoss)) {
             delta = ProvisioningChange.LOST_PROVISIONING;
         }
 
@@ -700,8 +741,7 @@
         // If the previous link properties had a global IPv6 address and an
         // IPv6 default route then also consider the loss of that default route
         // to be a loss of provisioning. See b/27962810.
-        if (oldLp.hasGlobalIPv6Address() && oldLp.hasIPv6DefaultRoute() &&
-                !newLp.hasIPv6DefaultRoute()) {
+        if (oldLp.hasGlobalIPv6Address() && (lostIPv6Router && !ignoreIPv6ProvisioningLoss)) {
             delta = ProvisioningChange.LOST_PROVISIONING;
         }
 
@@ -766,6 +806,11 @@
         //         - IPv6 addresses
         //         - IPv6 routes
         //         - IPv6 DNS servers
+        //
+        // N.B.: this is fundamentally race-prone and should be fixed by
+        // changing NetlinkTracker from a hybrid edge/level model to an
+        // edge-only model, or by giving IpManager its own netlink socket(s)
+        // so as to track all required information directly.
         LinkProperties netlinkLinkProperties = mNetlinkTracker.getLinkProperties();
         newLp.setLinkAddresses(netlinkLinkProperties.getLinkAddresses());
         for (RouteInfo route : netlinkLinkProperties.getRoutes()) {
@@ -916,12 +961,6 @@
             mDhcpClient = DhcpClient.makeDhcpClient(mContext, IpManager.this, mInterfaceName);
             mDhcpClient.registerForPreDhcpNotification();
             mDhcpClient.sendMessage(DhcpClient.CMD_START_DHCP);
-
-            if (mConfiguration.mProvisioningTimeoutMs > 0) {
-                final long alarmTime = SystemClock.elapsedRealtime() +
-                        mConfiguration.mProvisioningTimeoutMs;
-                mProvisioningTimeoutAlarm.schedule(alarmTime);
-            }
         }
 
         return true;
@@ -943,16 +982,29 @@
         return true;
     }
 
+    private void stopAllIP() {
+        // We don't need to worry about routes, just addresses, because:
+        //     - disableIpv6() will clear autoconf IPv6 routes as well, and
+        //     - we don't get IPv4 routes from netlink
+        // so we neither react to nor need to wait for changes in either.
+
+        try {
+            mNwService.disableIpv6(mInterfaceName);
+        } catch (Exception e) {
+            Log.e(mTag, "Failed to disable IPv6" + e);
+        }
+
+        try {
+            mNwService.clearInterfaceAddresses(mInterfaceName);
+        } catch (Exception e) {
+            Log.e(mTag, "Failed to clear addresses " + e);
+        }
+    }
 
     class StoppedState extends State {
         @Override
         public void enter() {
-            try {
-                mNwService.disableIpv6(mInterfaceName);
-                mNwService.clearInterfaceAddresses(mInterfaceName);
-            } catch (Exception e) {
-                Log.e(mTag, "Failed to clear addresses or disable IPv6" + e);
-            }
+            stopAllIP();
 
             resetLinkProperties();
             if (mStartTimeMillis > 0) {
@@ -998,6 +1050,8 @@
                 default:
                     return NOT_HANDLED;
             }
+
+            mMsgStateLogger.handled(this, getCurrentState());
             return HANDLED;
         }
     }
@@ -1014,6 +1068,13 @@
         @Override
         public boolean processMessage(Message msg) {
             switch (msg.what) {
+                case CMD_STOP:
+                    break;
+
+                case DhcpClient.CMD_CLEAR_LINKADDRESS:
+                    clearIPv4Address();
+                    break;
+
                 case DhcpClient.CMD_ON_QUIT:
                     mDhcpClient = null;
                     transitionTo(mStoppedState);
@@ -1022,17 +1083,80 @@
                 default:
                     deferMessage(msg);
             }
+
+            mMsgStateLogger.handled(this, getCurrentState());
             return HANDLED;
         }
     }
 
     class StartedState extends State {
-        private boolean mDhcpActionInFlight;
-
         @Override
         public void enter() {
             mStartTimeMillis = SystemClock.elapsedRealtime();
 
+            if (mConfiguration.mProvisioningTimeoutMs > 0) {
+                final long alarmTime = SystemClock.elapsedRealtime() +
+                        mConfiguration.mProvisioningTimeoutMs;
+                mProvisioningTimeoutAlarm.schedule(alarmTime);
+            }
+
+            if (readyToProceed()) {
+                transitionTo(mRunningState);
+            } else {
+                // Clear all IPv4 and IPv6 before proceeding to RunningState.
+                // Clean up any leftover state from an abnormal exit from
+                // tethering or during an IpManager restart.
+                stopAllIP();
+            }
+        }
+
+        @Override
+        public void exit() {
+            mProvisioningTimeoutAlarm.cancel();
+        }
+
+        @Override
+        public boolean processMessage(Message msg) {
+            switch (msg.what) {
+                case CMD_STOP:
+                    transitionTo(mStoppingState);
+                    break;
+
+                case EVENT_NETLINK_LINKPROPERTIES_CHANGED:
+                    handleLinkPropertiesUpdate(NO_CALLBACKS);
+                    if (readyToProceed()) {
+                        transitionTo(mRunningState);
+                    }
+                    break;
+
+                case EVENT_PROVISIONING_TIMEOUT:
+                    handleProvisioningFailure();
+                    break;
+
+                default:
+                    // It's safe to process messages out of order because the
+                    // only message that can both
+                    //     a) be received at this time and
+                    //     b) affect provisioning state
+                    // is EVENT_NETLINK_LINKPROPERTIES_CHANGED (handled above).
+                    deferMessage(msg);
+            }
+
+            mMsgStateLogger.handled(this, getCurrentState());
+            return HANDLED;
+        }
+
+        boolean readyToProceed() {
+            return (!mLinkProperties.hasIPv4Address() &&
+                    !mLinkProperties.hasGlobalIPv6Address());
+        }
+    }
+
+    class RunningState extends State {
+        private boolean mDhcpActionInFlight;
+
+        @Override
+        public void enter() {
             mApfFilter = ApfFilter.maybeCreate(mConfiguration.mApfCapabilities, mNetworkInterface,
                     mCallback, mMulticastFiltering);
             // TODO: investigate the effects of any multicast filtering racing/interfering with the
@@ -1046,6 +1170,13 @@
                 startIPv6();
             }
 
+            if (mConfiguration.mEnableIPv4) {
+                if (!startIPv4()) {
+                    transitionTo(mStoppingState);
+                    return;
+                }
+            }
+
             if (mConfiguration.mUsingIpReachabilityMonitor) {
                 mIpReachabilityMonitor = new IpReachabilityMonitor(
                         mContext,
@@ -1055,19 +1186,13 @@
                             public void notifyLost(InetAddress ip, String logMsg) {
                                 mCallback.onReachabilityLost(logMsg);
                             }
-                        });
-            }
-
-            if (mConfiguration.mEnableIPv4) {
-                if (!startIPv4()) {
-                    transitionTo(mStoppingState);
-                }
+                        },
+                        mAvoidBadWifiTracker);
             }
         }
 
         @Override
         public void exit() {
-            mProvisioningTimeoutAlarm.cancel();
             stopDhcpAction();
 
             if (mIpReachabilityMonitor != null) {
@@ -1164,10 +1289,6 @@
                     break;
                 }
 
-                case EVENT_PROVISIONING_TIMEOUT:
-                    handleProvisioningFailure();
-                    break;
-
                 case EVENT_DHCPACTION_TIMEOUT:
                     stopDhcpAction();
                     break;
@@ -1230,7 +1351,29 @@
                 default:
                     return NOT_HANDLED;
             }
+
+            mMsgStateLogger.handled(this, getCurrentState());
             return HANDLED;
         }
     }
+
+    private static class MessageHandlingLogger {
+        public String processedInState;
+        public String receivedInState;
+
+        public void reset() {
+            processedInState = null;
+            receivedInState = null;
+        }
+
+        public void handled(State processedIn, IState receivedIn) {
+            processedInState = processedIn.getClass().getSimpleName();
+            receivedInState = receivedIn.getName();
+        }
+
+        public String toString() {
+            return String.format("rcvd_in=%s, proc_in=%s",
+                                 receivedInState, processedInState);
+        }
+    }
 }
diff --git a/services/net/java/android/net/ip/IpReachabilityMonitor.java b/services/net/java/android/net/ip/IpReachabilityMonitor.java
index 27600a7..a6bb40c 100644
--- a/services/net/java/android/net/ip/IpReachabilityMonitor.java
+++ b/services/net/java/android/net/ip/IpReachabilityMonitor.java
@@ -24,6 +24,7 @@
 import android.net.LinkProperties.ProvisioningChange;
 import android.net.ProxyInfo;
 import android.net.RouteInfo;
+import android.net.metrics.IpConnectivityLog;
 import android.net.metrics.IpReachabilityEvent;
 import android.net.netlink.NetlinkConstants;
 import android.net.netlink.NetlinkErrorMessage;
@@ -33,6 +34,7 @@
 import android.net.netlink.StructNdaCacheInfo;
 import android.net.netlink.StructNdMsg;
 import android.net.netlink.StructNlMsgHdr;
+import android.net.util.AvoidBadWifiTracker;
 import android.os.PowerManager;
 import android.os.SystemClock;
 import android.system.ErrnoException;
@@ -41,6 +43,7 @@
 import android.util.Log;
 
 import java.io.InterruptedIOException;
+import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.NetworkInterface;
@@ -128,7 +131,6 @@
  *          state it may be best for the link to disconnect completely and
  *          reconnect afresh.
  *
- *
  * @hide
  */
 public class IpReachabilityMonitor {
@@ -149,8 +151,10 @@
     private final String mInterfaceName;
     private final int mInterfaceIndex;
     private final Callback mCallback;
+    private final AvoidBadWifiTracker mAvoidBadWifiTracker;
     private final NetlinkSocketObserver mNetlinkSocketObserver;
     private final Thread mObserverThread;
+    private final IpConnectivityLog mMetricsLog = new IpConnectivityLog();
     @GuardedBy("mLock")
     private LinkProperties mLinkProperties = new LinkProperties();
     // TODO: consider a map to a private NeighborState class holding more
@@ -161,6 +165,8 @@
     private int mIpWatchListVersion;
     @GuardedBy("mLock")
     private boolean mRunning;
+    // Time in milliseconds of the last forced probe request.
+    private volatile long mLastProbeTimeMs;
 
     /**
      * Make the kernel perform neighbor reachability detection (IPv4 ARP or IPv6 ND)
@@ -216,8 +222,12 @@
         return errno;
     }
 
-    public IpReachabilityMonitor(Context context, String ifName, Callback callback)
-                throws IllegalArgumentException {
+    public IpReachabilityMonitor(Context context, String ifName, Callback callback) {
+        this(context, ifName, callback, null);
+    }
+
+    public IpReachabilityMonitor(Context context, String ifName, Callback callback,
+            AvoidBadWifiTracker tracker) throws IllegalArgumentException {
         mInterfaceName = ifName;
         int ifIndex = -1;
         try {
@@ -229,6 +239,7 @@
         mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).newWakeLock(
                 PowerManager.PARTIAL_WAKE_LOCK, TAG + "." + mInterfaceName);
         mCallback = callback;
+        mAvoidBadWifiTracker = tracker;
         mNetlinkSocketObserver = new NetlinkSocketObserver();
         mObserverThread = new Thread(mNetlinkSocketObserver);
         mObserverThread.start();
@@ -337,7 +348,7 @@
 
     private void handleNeighborLost(String msg) {
         InetAddress ip = null;
-        ProvisioningChange delta;
+        final ProvisioningChange delta;
         synchronized (mLock) {
             LinkProperties whatIfLp = new LinkProperties(mLinkProperties);
 
@@ -352,14 +363,17 @@
                         whatIfLp.removeRoute(route);
                     }
                 }
-                whatIfLp.removeDnsServer(ip);
+
+                if (avoidingBadLinks() || !(ip instanceof Inet6Address)) {
+                    // We should do this unconditionally, but alas we cannot: b/31827713.
+                    whatIfLp.removeDnsServer(ip);
+                }
             }
 
             delta = LinkProperties.compareProvisioning(mLinkProperties, whatIfLp);
         }
 
         if (delta == ProvisioningChange.LOST_PROVISIONING) {
-            IpReachabilityEvent.logProvisioningLost(mInterfaceName);
             final String logMsg = "FAILURE: LOST_PROVISIONING, " + msg;
             Log.w(TAG, logMsg);
             if (mCallback != null) {
@@ -367,9 +381,12 @@
                 // an InetAddress argument.
                 mCallback.notifyLost(ip, logMsg);
             }
-        } else {
-            IpReachabilityEvent.logNudFailed(mInterfaceName);
         }
+        logNudFailed(delta);
+    }
+
+    private boolean avoidingBadLinks() {
+        return (mAvoidBadWifiTracker != null) ? mAvoidBadWifiTracker.currentValue() : true;
     }
 
     public void probeAll() {
@@ -393,11 +410,12 @@
                 break;
             }
             final int returnValue = probeNeighbor(mInterfaceIndex, target);
-            IpReachabilityEvent.logProbeEvent(mInterfaceName, returnValue);
+            logEvent(IpReachabilityEvent.PROBE, returnValue);
         }
+        mLastProbeTimeMs = SystemClock.elapsedRealtime();
     }
 
-    private long getProbeWakeLockDuration() {
+    private static long getProbeWakeLockDuration() {
         // Ideally, this would be computed by examining the values of:
         //
         //     /proc/sys/net/ipv[46]/neigh/<ifname>/ucast_solicit
@@ -413,6 +431,19 @@
         return (numUnicastProbes * retransTimeMs) + gracePeriodMs;
     }
 
+    private void logEvent(int probeType, int errorCode) {
+        int eventType = probeType | (errorCode & 0xff);
+        mMetricsLog.log(new IpReachabilityEvent(mInterfaceName, eventType));
+    }
+
+    private void logNudFailed(ProvisioningChange delta) {
+        long duration = SystemClock.elapsedRealtime() - mLastProbeTimeMs;
+        boolean isFromProbe = (duration < getProbeWakeLockDuration());
+        boolean isProvisioningLost = (delta == ProvisioningChange.LOST_PROVISIONING);
+        int eventType = IpReachabilityEvent.nudFailureEventType(isFromProbe, isProvisioningLost);
+        mMetricsLog.log(new IpReachabilityEvent(mInterfaceName, eventType));
+    }
+
     // TODO: simplify the number of objects by making this extend Thread.
     private final class NetlinkSocketObserver implements Runnable {
         private NetlinkSocket mSocket;
diff --git a/services/net/java/android/net/ip/RouterAdvertisementDaemon.java b/services/net/java/android/net/ip/RouterAdvertisementDaemon.java
new file mode 100644
index 0000000..6802cff
--- /dev/null
+++ b/services/net/java/android/net/ip/RouterAdvertisementDaemon.java
@@ -0,0 +1,716 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.ip;
+
+import static android.system.OsConstants.*;
+
+import android.net.IpPrefix;
+import android.net.LinkAddress;
+import android.net.LinkProperties;
+import android.net.NetworkUtils;
+import android.system.ErrnoException;
+import android.system.Os;
+import android.system.StructGroupReq;
+import android.system.StructTimeval;
+import android.util.Log;
+
+import com.android.internal.annotations.GuardedBy;
+
+import libcore.io.IoBridge;
+import libcore.util.HexEncoding;
+
+import java.io.FileDescriptor;
+import java.io.InterruptedIOException;
+import java.io.IOException;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketException;
+import java.net.UnknownHostException;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+
+
+/**
+ * Basic IPv6 Router Advertisement Daemon.
+ *
+ * TODO:
+ *
+ *     - Rewrite using Handler (and friends) so that AlarmManager can deliver
+ *       "kick" messages when it's time to send a multicast RA.
+ *
+ * @hide
+ */
+public class RouterAdvertisementDaemon {
+    private static final String TAG = RouterAdvertisementDaemon.class.getSimpleName();
+    private static final byte ICMPV6_ND_ROUTER_SOLICIT = asByte(133);
+    private static final byte ICMPV6_ND_ROUTER_ADVERT  = asByte(134);
+    private static final int IPV6_MIN_MTU = 1280;
+    private static final int MIN_RA_HEADER_SIZE = 16;
+
+    // Summary of various timers and lifetimes.
+    private static final int MIN_RTR_ADV_INTERVAL_SEC = 300;
+    private static final int MAX_RTR_ADV_INTERVAL_SEC = 600;
+    // In general, router, prefix, and DNS lifetimes are all advised to be
+    // greater than or equal to 3 * MAX_RTR_ADV_INTERVAL.  Here, we double
+    // that to allow for multicast packet loss.
+    //
+    // This MAX_RTR_ADV_INTERVAL_SEC and DEFAULT_LIFETIME are also consistent
+    // with the https://tools.ietf.org/html/rfc7772#section-4 discussion of
+    // "approximately 7 RAs per hour".
+    private static final int DEFAULT_LIFETIME = 6 * MAX_RTR_ADV_INTERVAL_SEC;
+    // From https://tools.ietf.org/html/rfc4861#section-10 .
+    private static final int MIN_DELAY_BETWEEN_RAS_SEC = 3;
+    // Both initial and final RAs, but also for changes in RA contents.
+    // From https://tools.ietf.org/html/rfc4861#section-10 .
+    private static final int  MAX_URGENT_RTR_ADVERTISEMENTS = 5;
+
+    private static final int DAY_IN_SECONDS = 86_400;
+
+    private static final byte[] ALL_NODES = new byte[] {
+            (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
+    };
+
+    private final String mIfName;
+    private final int mIfIndex;
+    private final byte[] mHwAddr;
+    private final InetSocketAddress mAllNodes;
+
+    // This lock is to protect the RA from being updated while being
+    // transmitted on another thread  (multicast or unicast).
+    //
+    // TODO: This should be handled with a more RCU-like approach.
+    private final Object mLock = new Object();
+    @GuardedBy("mLock")
+    private final byte[] mRA = new byte[IPV6_MIN_MTU];
+    @GuardedBy("mLock")
+    private int mRaLength;
+    @GuardedBy("mLock")
+    private final DeprecatedInfoTracker mDeprecatedInfoTracker;
+    @GuardedBy("mLock")
+    private RaParams mRaParams;
+
+    private volatile FileDescriptor mSocket;
+    private volatile MulticastTransmitter mMulticastTransmitter;
+    private volatile UnicastResponder mUnicastResponder;
+
+    public static class RaParams {
+        public boolean hasDefaultRoute;
+        public int mtu;
+        public HashSet<IpPrefix> prefixes;
+        public HashSet<Inet6Address> dnses;
+
+        public RaParams() {
+            hasDefaultRoute = false;
+            mtu = IPV6_MIN_MTU;
+            prefixes = new HashSet<IpPrefix>();
+            dnses = new HashSet<Inet6Address>();
+        }
+
+        public RaParams(RaParams other) {
+            hasDefaultRoute = other.hasDefaultRoute;
+            mtu = other.mtu;
+            prefixes = (HashSet) other.prefixes.clone();
+            dnses = (HashSet) other.dnses.clone();
+        }
+
+        // Returns the subset of RA parameters that become deprecated when
+        // moving from announcing oldRa to announcing newRa.
+        //
+        // Currently only tracks differences in |prefixes| and |dnses|.
+        public static RaParams getDeprecatedRaParams(RaParams oldRa, RaParams newRa) {
+            RaParams newlyDeprecated = new RaParams();
+
+            if (oldRa != null) {
+                for (IpPrefix ipp : oldRa.prefixes) {
+                    if (newRa == null || !newRa.prefixes.contains(ipp)) {
+                        newlyDeprecated.prefixes.add(ipp);
+                    }
+                }
+
+                for (Inet6Address dns : oldRa.dnses) {
+                    if (newRa == null || !newRa.dnses.contains(dns)) {
+                        newlyDeprecated.dnses.add(dns);
+                    }
+                }
+            }
+
+            return newlyDeprecated;
+        }
+    }
+
+    private static class DeprecatedInfoTracker {
+        private final HashMap<IpPrefix, Integer> mPrefixes = new HashMap<>();
+        private final HashMap<Inet6Address, Integer> mDnses = new HashMap<>();
+
+        Set<IpPrefix> getPrefixes() { return mPrefixes.keySet(); }
+
+        void putPrefixes(Set<IpPrefix> prefixes) {
+            for (IpPrefix ipp : prefixes) {
+                mPrefixes.put(ipp, MAX_URGENT_RTR_ADVERTISEMENTS);
+            }
+        }
+
+        void removePrefixes(Set<IpPrefix> prefixes) {
+            for (IpPrefix ipp : prefixes) {
+                mPrefixes.remove(ipp);
+            }
+        }
+
+        Set<Inet6Address> getDnses() { return mDnses.keySet(); }
+
+        void putDnses(Set<Inet6Address> dnses) {
+            for (Inet6Address dns : dnses) {
+                mDnses.put(dns, MAX_URGENT_RTR_ADVERTISEMENTS);
+            }
+        }
+
+        void removeDnses(Set<Inet6Address> dnses) {
+            for (Inet6Address dns : dnses) {
+                mDnses.remove(dns);
+            }
+        }
+
+        boolean isEmpty() { return mPrefixes.isEmpty() && mDnses.isEmpty(); }
+
+        private boolean decrementCounters() {
+            boolean removed = decrementCounter(mPrefixes);
+            removed |= decrementCounter(mDnses);
+            return removed;
+        }
+
+        private <T> boolean decrementCounter(HashMap<T, Integer> map) {
+            boolean removed = false;
+
+            for (Iterator<Map.Entry<T, Integer>> it = map.entrySet().iterator();
+                 it.hasNext();) {
+                Map.Entry<T, Integer> kv = it.next();
+                if (kv.getValue() == 0) {
+                    it.remove();
+                    removed = true;
+                } else {
+                    kv.setValue(kv.getValue() - 1);
+                }
+            }
+
+            return removed;
+        }
+    }
+
+
+    public RouterAdvertisementDaemon(String ifname, int ifindex, byte[] hwaddr) {
+        mIfName = ifname;
+        mIfIndex = ifindex;
+        mHwAddr = hwaddr;
+        mAllNodes = new InetSocketAddress(getAllNodesForScopeId(mIfIndex), 0);
+        mDeprecatedInfoTracker = new DeprecatedInfoTracker();
+    }
+
+    public void buildNewRa(RaParams deprecatedParams, RaParams newParams) {
+        synchronized (mLock) {
+            if (deprecatedParams != null) {
+                mDeprecatedInfoTracker.putPrefixes(deprecatedParams.prefixes);
+                mDeprecatedInfoTracker.putDnses(deprecatedParams.dnses);
+            }
+
+            if (newParams != null) {
+                // Process information that is no longer deprecated.
+                mDeprecatedInfoTracker.removePrefixes(newParams.prefixes);
+                mDeprecatedInfoTracker.removeDnses(newParams.dnses);
+            }
+
+            mRaParams = newParams;
+            assembleRaLocked();
+        }
+
+        maybeNotifyMulticastTransmitter();
+    }
+
+    public boolean start() {
+        if (!createSocket()) {
+            return false;
+        }
+
+        mMulticastTransmitter = new MulticastTransmitter();
+        mMulticastTransmitter.start();
+
+        mUnicastResponder = new UnicastResponder();
+        mUnicastResponder.start();
+
+        return true;
+    }
+
+    public void stop() {
+        closeSocket();
+        mMulticastTransmitter = null;
+        mUnicastResponder = null;
+    }
+
+    private void assembleRaLocked() {
+        final ByteBuffer ra = ByteBuffer.wrap(mRA);
+        ra.order(ByteOrder.BIG_ENDIAN);
+
+        boolean shouldSendRA = false;
+
+        try {
+            putHeader(ra, mRaParams != null && mRaParams.hasDefaultRoute);
+            putSlla(ra, mHwAddr);
+            mRaLength = ra.position();
+
+            // https://tools.ietf.org/html/rfc5175#section-4 says:
+            //
+            //     "MUST NOT be added to a Router Advertisement message
+            //      if no flags in the option are set."
+            //
+            // putExpandedFlagsOption(ra);
+
+            if (mRaParams != null) {
+                putMtu(ra, mRaParams.mtu);
+                mRaLength = ra.position();
+
+                for (IpPrefix ipp : mRaParams.prefixes) {
+                    putPio(ra, ipp, DEFAULT_LIFETIME, DEFAULT_LIFETIME);
+                    mRaLength = ra.position();
+                    shouldSendRA = true;
+                }
+
+                if (mRaParams.dnses.size() > 0) {
+                    putRdnss(ra, mRaParams.dnses, DEFAULT_LIFETIME);
+                    mRaLength = ra.position();
+                    shouldSendRA = true;
+                }
+            }
+
+            for (IpPrefix ipp : mDeprecatedInfoTracker.getPrefixes()) {
+                putPio(ra, ipp, 0, 0);
+                mRaLength = ra.position();
+                shouldSendRA = true;
+            }
+
+            final Set<Inet6Address> deprecatedDnses = mDeprecatedInfoTracker.getDnses();
+            if (!deprecatedDnses.isEmpty()) {
+                putRdnss(ra, deprecatedDnses, 0);
+                mRaLength = ra.position();
+                shouldSendRA = true;
+            }
+        } catch (BufferOverflowException e) {
+            // The packet up to mRaLength  is valid, since it has been updated
+            // progressively as the RA was built. Log an error, and continue
+            // on as best as possible.
+            Log.e(TAG, "Could not construct new RA: " + e);
+        }
+
+        // We have nothing worth announcing; indicate as much to maybeSendRA().
+        if (!shouldSendRA) {
+            mRaLength = 0;
+        }
+    }
+
+    private void maybeNotifyMulticastTransmitter() {
+        final MulticastTransmitter m = mMulticastTransmitter;
+        if (m != null) {
+            m.hup();
+        }
+    }
+
+    private static Inet6Address getAllNodesForScopeId(int scopeId) {
+        try {
+            return Inet6Address.getByAddress("ff02::1", ALL_NODES, scopeId);
+        } catch (UnknownHostException uhe) {
+            Log.wtf(TAG, "Failed to construct ff02::1 InetAddress: " + uhe);
+            return null;
+        }
+    }
+
+    private static byte asByte(int value) { return (byte) value; }
+    private static short asShort(int value) { return (short) value; }
+
+    private static void putHeader(ByteBuffer ra, boolean hasDefaultRoute) {
+        /**
+            Router Advertisement Message Format
+
+             0                   1                   2                   3
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |     Type      |     Code      |          Checksum             |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            | Cur Hop Limit |M|O|H|Prf|P|R|R|       Router Lifetime         |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                         Reachable Time                        |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                          Retrans Timer                        |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |   Options ...
+            +-+-+-+-+-+-+-+-+-+-+-+-
+        */
+        final byte DEFAULT_HOPLIMIT = 64;
+        ra.put(ICMPV6_ND_ROUTER_ADVERT)
+          .put(asByte(0))
+          .putShort(asShort(0))
+          .put(DEFAULT_HOPLIMIT)
+          // RFC 4191 "high" preference, iff. advertising a default route.
+          .put(hasDefaultRoute ? asByte(0x08) : asByte(0))
+          .putShort(hasDefaultRoute ? asShort(DEFAULT_LIFETIME) : asShort(0))
+          .putInt(0)
+          .putInt(0);
+    }
+
+    private static void putSlla(ByteBuffer ra, byte[] slla) {
+        /**
+            Source/Target Link-layer Address
+
+             0                   1                   2                   3
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |     Type      |    Length     |    Link-Layer Address ...
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+        */
+        if (slla == null || slla.length != 6) {
+            // Only IEEE 802.3 6-byte addresses are supported.
+            return;
+        }
+        final byte ND_OPTION_SLLA = 1;
+        final byte SLLA_NUM_8OCTETS = 1;
+        ra.put(ND_OPTION_SLLA)
+          .put(SLLA_NUM_8OCTETS)
+          .put(slla);
+    }
+
+    private static void putExpandedFlagsOption(ByteBuffer ra) {
+        /**
+            Router Advertisement Expanded Flags Option
+
+             0                   1                   2                   3
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |     Type      |    Length     |         Bit fields available ..
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            ... for assignment                                              |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+         */
+
+        final byte ND_OPTION_EFO = 26;
+        final byte EFO_NUM_8OCTETS = 1;
+
+        ra.put(ND_OPTION_EFO)
+          .put(EFO_NUM_8OCTETS)
+          .putShort(asShort(0))
+          .putInt(0);
+    }
+
+    private static void putMtu(ByteBuffer ra, int mtu) {
+        /**
+            MTU
+
+             0                   1                   2                   3
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |     Type      |    Length     |           Reserved            |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                              MTU                              |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+        */
+        final byte ND_OPTION_MTU = 5;
+        final byte MTU_NUM_8OCTETS = 1;
+        ra.put(ND_OPTION_MTU)
+          .put(MTU_NUM_8OCTETS)
+          .putShort(asShort(0))
+          .putInt((mtu < IPV6_MIN_MTU) ? IPV6_MIN_MTU : mtu);
+    }
+
+    private static void putPio(ByteBuffer ra, IpPrefix ipp,
+                               int validTime, int preferredTime) {
+        /**
+            Prefix Information
+
+             0                   1                   2                   3
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |     Type      |    Length     | Prefix Length |L|A| Reserved1 |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                         Valid Lifetime                        |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                       Preferred Lifetime                      |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                           Reserved2                           |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                                                               |
+            +                                                               +
+            |                                                               |
+            +                            Prefix                             +
+            |                                                               |
+            +                                                               +
+            |                                                               |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+        */
+        final int prefixLength = ipp.getPrefixLength();
+        if (prefixLength != 64) {
+            return;
+        }
+        final byte ND_OPTION_PIO = 3;
+        final byte PIO_NUM_8OCTETS = 4;
+
+        if (validTime < 0) validTime = 0;
+        if (preferredTime < 0) preferredTime = 0;
+        if (preferredTime > validTime) preferredTime = validTime;
+
+        final byte[] addr = ipp.getAddress().getAddress();
+        ra.put(ND_OPTION_PIO)
+          .put(PIO_NUM_8OCTETS)
+          .put(asByte(prefixLength))
+          .put(asByte(0xc0)) /* L & A set */
+          .putInt(validTime)
+          .putInt(preferredTime)
+          .putInt(0)
+          .put(addr);
+    }
+
+    private static void putRio(ByteBuffer ra, IpPrefix ipp) {
+        /**
+            Route Information Option
+
+             0                   1                   2                   3
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |     Type      |    Length     | Prefix Length |Resvd|Prf|Resvd|
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                        Route Lifetime                         |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                   Prefix (Variable Length)                    |
+            .                                                               .
+            .                                                               .
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+         */
+        final int prefixLength = ipp.getPrefixLength();
+        if (prefixLength > 64) {
+            return;
+        }
+        final byte ND_OPTION_RIO = 24;
+        final byte RIO_NUM_8OCTETS = asByte(
+                (prefixLength == 0) ? 1 : (prefixLength <= 8) ? 2 : 3);
+
+        final byte[] addr = ipp.getAddress().getAddress();
+        ra.put(ND_OPTION_RIO)
+          .put(RIO_NUM_8OCTETS)
+          .put(asByte(prefixLength))
+          .put(asByte(0x18))
+          .putInt(DEFAULT_LIFETIME);
+
+        // Rely upon an IpPrefix's address being properly zeroed.
+        if (prefixLength > 0) {
+            ra.put(addr, 0, (prefixLength <= 64) ? 8 : 16);
+        }
+    }
+
+    private static void putRdnss(ByteBuffer ra, Set<Inet6Address> dnses, int lifetime) {
+        /**
+            Recursive DNS Server (RDNSS) Option
+
+             0                   1                   2                   3
+             0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |     Type      |     Length    |           Reserved            |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                           Lifetime                            |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+            |                                                               |
+            :            Addresses of IPv6 Recursive DNS Servers            :
+            |                                                               |
+            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+         */
+
+        final byte ND_OPTION_RDNSS = 25;
+        final byte RDNSS_NUM_8OCTETS = asByte(dnses.size() * 2 + 1);
+        ra.put(ND_OPTION_RDNSS)
+          .put(RDNSS_NUM_8OCTETS)
+          .putShort(asShort(0))
+          .putInt(lifetime);
+
+        for (Inet6Address dns : dnses) {
+            // NOTE: If the full of list DNS servers doesn't fit in the packet,
+            // this code will cause a buffer overflow and the RA won't include
+            // this instance of the option at all.
+            //
+            // TODO: Consider looking at ra.remaining() to determine how many
+            // DNS servers will fit, and adding only those.
+            ra.put(dns.getAddress());
+        }
+    }
+
+    private boolean createSocket() {
+        final int SEND_TIMEOUT_MS = 300;
+
+        try {
+            mSocket = Os.socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
+            // Setting SNDTIMEO is purely for defensive purposes.
+            Os.setsockoptTimeval(
+                    mSocket, SOL_SOCKET, SO_SNDTIMEO, StructTimeval.fromMillis(SEND_TIMEOUT_MS));
+            Os.setsockoptIfreq(mSocket, SOL_SOCKET, SO_BINDTODEVICE, mIfName);
+            NetworkUtils.protectFromVpn(mSocket);
+            NetworkUtils.setupRaSocket(mSocket, mIfIndex);
+        } catch (ErrnoException | IOException e) {
+            Log.e(TAG, "Failed to create RA daemon socket: " + e);
+            return false;
+        }
+
+        return true;
+    }
+
+    private void closeSocket() {
+        if (mSocket != null) {
+            try {
+                IoBridge.closeAndSignalBlockedThreads(mSocket);
+            } catch (IOException ignored) {}
+        }
+        mSocket = null;
+    }
+
+    private boolean isSocketValid() {
+        final FileDescriptor s = mSocket;
+        return (s != null) && s.valid();
+    }
+
+    private boolean isSuitableDestination(InetSocketAddress dest) {
+        if (mAllNodes.equals(dest)) {
+            return true;
+        }
+
+        final InetAddress destip = dest.getAddress();
+        return (destip instanceof Inet6Address) &&
+                destip.isLinkLocalAddress() &&
+               (((Inet6Address) destip).getScopeId() == mIfIndex);
+    }
+
+    private void maybeSendRA(InetSocketAddress dest) {
+        if (dest == null || !isSuitableDestination(dest)) {
+            dest = mAllNodes;
+        }
+
+        try {
+            synchronized (mLock) {
+                if (mRaLength < MIN_RA_HEADER_SIZE) {
+                    // No actual RA to send.
+                    return;
+                }
+                Os.sendto(mSocket, mRA, 0, mRaLength, 0, dest);
+            }
+            Log.d(TAG, "RA sendto " + dest.getAddress().getHostAddress());
+        } catch (ErrnoException | SocketException e) {
+            if (isSocketValid()) {
+                Log.e(TAG, "sendto error: " + e);
+            }
+        }
+    }
+
+    private final class UnicastResponder extends Thread {
+        private final InetSocketAddress solicitor = new InetSocketAddress();
+        // The recycled buffer for receiving Router Solicitations from clients.
+        // If the RS is larger than IPV6_MIN_MTU the packets are truncated.
+        // This is fine since currently only byte 0 is examined anyway.
+        private final byte mSolication[] = new byte[IPV6_MIN_MTU];
+
+        @Override
+        public void run() {
+            while (isSocketValid()) {
+                try {
+                    // Blocking receive.
+                    final int rval = Os.recvfrom(
+                            mSocket, mSolication, 0, mSolication.length, 0, solicitor);
+                    // Do the least possible amount of validation.
+                    if (rval < 1 || mSolication[0] != ICMPV6_ND_ROUTER_SOLICIT) {
+                        continue;
+                    }
+                } catch (ErrnoException | SocketException e) {
+                    if (isSocketValid()) {
+                        Log.e(TAG, "recvfrom error: " + e);
+                    }
+                    continue;
+                }
+
+                maybeSendRA(solicitor);
+            }
+        }
+    }
+
+    // TODO: Consider moving this to run on a provided Looper as a Handler,
+    // with WakeupMessage-style messages providing the timer driven input.
+    private final class MulticastTransmitter extends Thread {
+        private final Random mRandom = new Random();
+        private final AtomicInteger mUrgentAnnouncements = new AtomicInteger(0);
+
+        @Override
+        public void run() {
+            while (isSocketValid()) {
+                try {
+                    Thread.sleep(getNextMulticastTransmitDelayMs());
+                } catch (InterruptedException ignored) {
+                    // Stop sleeping, immediately send an RA, and continue.
+                }
+
+                maybeSendRA(mAllNodes);
+                synchronized (mLock) {
+                    if (mDeprecatedInfoTracker.decrementCounters()) {
+                        // At least one deprecated PIO has been removed;
+                        // reassemble the RA.
+                        assembleRaLocked();
+                    }
+                }
+            }
+        }
+
+        public void hup() {
+            // Set to one fewer that the desired number, because as soon as
+            // the thread interrupt is processed we immediately send an RA
+            // and mUrgentAnnouncements is not examined until the subsequent
+            // sleep interval computation (i.e. this way we send 3 and not 4).
+            mUrgentAnnouncements.set(MAX_URGENT_RTR_ADVERTISEMENTS - 1);
+            interrupt();
+        }
+
+        private int getNextMulticastTransmitDelaySec() {
+            boolean deprecationInProgress = false;
+            synchronized (mLock) {
+                if (mRaLength < MIN_RA_HEADER_SIZE) {
+                    // No actual RA to send; just sleep for 1 day.
+                    return DAY_IN_SECONDS;
+                }
+                deprecationInProgress = !mDeprecatedInfoTracker.isEmpty();
+            }
+
+            final int urgentPending = mUrgentAnnouncements.getAndDecrement();
+            if ((urgentPending > 0) || deprecationInProgress) {
+                return MIN_DELAY_BETWEEN_RAS_SEC;
+            }
+
+            return MIN_RTR_ADV_INTERVAL_SEC + mRandom.nextInt(
+                    MAX_RTR_ADV_INTERVAL_SEC - MIN_RTR_ADV_INTERVAL_SEC);
+        }
+
+        private long getNextMulticastTransmitDelayMs() {
+            return 1000 * (long) getNextMulticastTransmitDelaySec();
+        }
+    }
+}
diff --git a/services/net/java/android/net/util/AvoidBadWifiTracker.java b/services/net/java/android/net/util/AvoidBadWifiTracker.java
new file mode 100644
index 0000000..c14e811
--- /dev/null
+++ b/services/net/java/android/net/util/AvoidBadWifiTracker.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.util;
+
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.os.Handler;
+import android.os.Message;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.util.Slog;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.R;
+
+import static android.provider.Settings.Global.NETWORK_AVOID_BAD_WIFI;
+
+/**
+ * A class to encapsulate management of the "Smart Networking" capability of
+ * avoiding bad Wi-Fi when, for example upstream connectivity is lost or
+ * certain critical link failures occur.
+ *
+ * This enables the device to switch to another form of connectivity, like
+ * mobile, if it's available and working.
+ *
+ * The Runnable |cb|, if given, is called on the supplied Handler's thread
+ * whether the computed "avoid bad wifi" value changes.
+ *
+ * Disabling this reverts the device to a level of networking sophistication
+ * circa 2012-13 by disabling disparate code paths each of which contribute to
+ * maintaining continuous, working Internet connectivity.
+ *
+ * @hide
+ */
+public class AvoidBadWifiTracker {
+    private static String TAG = AvoidBadWifiTracker.class.getSimpleName();
+
+    private final Context mContext;
+    private final Handler mHandler;
+    private final Runnable mReevaluateRunnable;
+    private final SettingObserver mSettingObserver;
+    private volatile boolean mAvoidBadWifi = true;
+
+    public AvoidBadWifiTracker(Context ctx, Handler handler) {
+        this(ctx, handler, null);
+    }
+
+    public AvoidBadWifiTracker(Context ctx, Handler handler, Runnable cb) {
+        mContext = ctx;
+        mHandler = handler;
+        mReevaluateRunnable = () -> { if (update() && cb != null) cb.run(); };
+        mSettingObserver = new SettingObserver();
+
+        final IntentFilter intentFilter = new IntentFilter();
+        intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
+        mContext.registerReceiverAsUser(new BroadcastReceiver() {
+            public void onReceive(Context context, Intent intent) {
+                reevaluate();
+            }
+        }, UserHandle.ALL, intentFilter, null, null);
+
+        update();
+    }
+
+    public boolean currentValue() {
+        return mAvoidBadWifi;
+    }
+
+    /**
+     * Whether the device or carrier configuration disables avoiding bad wifi by default.
+     */
+    public boolean configRestrictsAvoidBadWifi() {
+        return (mContext.getResources().getInteger(R.integer.config_networkAvoidBadWifi) == 0);
+    }
+
+    /**
+     * Whether we should display a notification when wifi becomes unvalidated.
+     */
+    public boolean shouldNotifyWifiUnvalidated() {
+        return configRestrictsAvoidBadWifi() && getSettingsValue() == null;
+    }
+
+    public String getSettingsValue() {
+        final ContentResolver resolver = mContext.getContentResolver();
+        return Settings.Global.getString(resolver, NETWORK_AVOID_BAD_WIFI);
+    }
+
+    @VisibleForTesting
+    public void reevaluate() {
+        mHandler.post(mReevaluateRunnable);
+    }
+
+    public boolean update() {
+        final boolean settingAvoidBadWifi = "1".equals(getSettingsValue());
+        final boolean prev = mAvoidBadWifi;
+        mAvoidBadWifi = settingAvoidBadWifi || !configRestrictsAvoidBadWifi();
+        return mAvoidBadWifi != prev;
+    }
+
+    private class SettingObserver extends ContentObserver {
+        private final Uri mUri = Settings.Global.getUriFor(NETWORK_AVOID_BAD_WIFI);
+
+        public SettingObserver() {
+            super(null);
+            final ContentResolver resolver = mContext.getContentResolver();
+            resolver.registerContentObserver(mUri, false, this);
+        }
+
+        @Override
+        public void onChange(boolean selfChange) {
+            Slog.wtf(TAG, "Should never be reached.");
+        }
+
+        @Override
+        public void onChange(boolean selfChange, Uri uri) {
+            if (!mUri.equals(uri)) return;
+            reevaluate();
+        }
+    }
+}
diff --git a/services/net/java/android/net/util/NetdService.java b/services/net/java/android/net/util/NetdService.java
new file mode 100644
index 0000000..153cb50
--- /dev/null
+++ b/services/net/java/android/net/util/NetdService.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net.util;
+
+import android.net.INetd;
+import android.os.ServiceManager;
+import android.util.Log;
+
+
+/**
+ * @hide
+ */
+public class NetdService {
+    private static final String TAG = NetdService.class.getSimpleName();
+    private static final String NETD_SERVICE_NAME = "netd";
+
+    /**
+     * It is the caller's responsibility to check for a null return value
+     * and to handle RemoteException errors from invocations on the returned
+     * interface if, for example, netd dies and is restarted.
+     *
+     * @return an INetd instance or null.
+     */
+    public static INetd getInstance() {
+        final INetd netdInstance = INetd.Stub.asInterface(
+                ServiceManager.getService(NETD_SERVICE_NAME));
+        if (netdInstance == null) {
+            Log.w(TAG, "WARNING: returning null INetd instance.");
+        }
+        return netdInstance;
+    }
+}
diff --git a/services/print/java/com/android/server/print/RemotePrintSpooler.java b/services/print/java/com/android/server/print/RemotePrintSpooler.java
index 07cc9c0..07b26e8 100644
--- a/services/print/java/com/android/server/print/RemotePrintSpooler.java
+++ b/services/print/java/com/android/server/print/RemotePrintSpooler.java
@@ -57,6 +57,9 @@
  * spooler if needed, to make the timed remote calls, to handle
  * remote exceptions, and to bind/unbind to the remote instance as
  * needed.
+ *
+ * The calls might be blocking and need the main thread of to be unblocked to finish. Hence do not
+ * call this while holding any monitors that might need to be acquired the main thread.
  */
 final class RemotePrintSpooler {
 
diff --git a/services/print/java/com/android/server/print/UserState.java b/services/print/java/com/android/server/print/UserState.java
index 7a3ebf4..a91cdb3 100644
--- a/services/print/java/com/android/server/print/UserState.java
+++ b/services/print/java/com/android/server/print/UserState.java
@@ -434,12 +434,12 @@
     }
 
     public void createPrinterDiscoverySession(@NonNull IPrinterDiscoveryObserver observer) {
+        mSpooler.clearCustomPrinterIconCache();
+
         synchronized (mLock) {
             throwIfDestroyedLocked();
 
             if (mPrinterDiscoverySession == null) {
-                mSpooler.clearCustomPrinterIconCache();
-
                 // If we do not have a session, tell all service to create one.
                 mPrinterDiscoverySession = new PrinterDiscoverySessionMediator(mContext) {
                     @Override
@@ -731,6 +731,8 @@
 
     @Override
     public void onCustomPrinterIconLoaded(PrinterId printerId, Icon icon) {
+        mSpooler.onCustomPrinterIconLoaded(printerId, icon);
+
         synchronized (mLock) {
             throwIfDestroyedLocked();
 
@@ -738,7 +740,6 @@
             if (mPrinterDiscoverySession == null) {
                 return;
             }
-            mSpooler.onCustomPrinterIconLoaded(printerId, icon);
             mPrinterDiscoverySession.onCustomPrinterIconLoadedLocked(printerId);
         }
     }
@@ -764,9 +765,8 @@
 
     public void updateIfNeededLocked() {
         throwIfDestroyedLocked();
-        if (readConfigurationLocked()) {
-            onConfigurationChangedLocked();
-        }
+        readConfigurationLocked();
+        onConfigurationChangedLocked();
     }
 
     public void destroyLocked() {
@@ -841,14 +841,12 @@
         pw.println();
     }
 
-    private boolean readConfigurationLocked() {
-        boolean somethingChanged = false;
-        somethingChanged |= readInstalledPrintServicesLocked();
-        somethingChanged |= readDisabledPrintServicesLocked();
-        return somethingChanged;
+    private void readConfigurationLocked() {
+        readInstalledPrintServicesLocked();
+        readDisabledPrintServicesLocked();
     }
 
-    private boolean readInstalledPrintServicesLocked() {
+    private void readInstalledPrintServicesLocked() {
         Set<PrintServiceInfo> tempPrintServices = new HashSet<PrintServiceInfo>();
 
         List<ResolveInfo> installedServices = mContext.getPackageManager()
@@ -872,39 +870,8 @@
             tempPrintServices.add(PrintServiceInfo.create(installedService, mContext));
         }
 
-        boolean someServiceChanged = false;
-
-        if (tempPrintServices.size() != mInstalledServices.size()) {
-            someServiceChanged = true;
-        } else {
-            for (PrintServiceInfo newService: tempPrintServices) {
-                final int oldServiceIndex = mInstalledServices.indexOf(newService);
-                if (oldServiceIndex < 0) {
-                    someServiceChanged = true;
-                    break;
-                }
-                // PrintServiceInfo#equals compares only the id not all members,
-                // so we are also comparing the members coming from meta-data.
-                PrintServiceInfo oldService = mInstalledServices.get(oldServiceIndex);
-                if (!TextUtils.equals(oldService.getAddPrintersActivityName(),
-                            newService.getAddPrintersActivityName())
-                        || !TextUtils.equals(oldService.getAdvancedOptionsActivityName(),
-                                newService.getAdvancedOptionsActivityName())
-                        || !TextUtils.equals(oldService.getSettingsActivityName(),
-                                newService.getSettingsActivityName())) {
-                    someServiceChanged = true;
-                    break;
-                }
-            }
-        }
-
-        if (someServiceChanged) {
-            mInstalledServices.clear();
-            mInstalledServices.addAll(tempPrintServices);
-            return true;
-        }
-
-        return false;
+        mInstalledServices.clear();
+        mInstalledServices.addAll(tempPrintServices);
     }
 
     /**
@@ -944,16 +911,14 @@
      *
      * @return true if the state changed.
      */
-    private boolean readDisabledPrintServicesLocked() {
+    private void readDisabledPrintServicesLocked() {
         Set<ComponentName> tempDisabledServiceNameSet = new HashSet<ComponentName>();
         readPrintServicesFromSettingLocked(Settings.Secure.DISABLED_PRINT_SERVICES,
                 tempDisabledServiceNameSet);
         if (!tempDisabledServiceNameSet.equals(mDisabledServices)) {
             mDisabledServices.clear();
             mDisabledServices.addAll(tempDisabledServiceNameSet);
-            return true;
         }
-        return false;
     }
 
     private void readPrintServicesFromSettingLocked(String setting,
@@ -1015,18 +980,21 @@
      * Prune persistent state if a print service was uninstalled
      */
     public void prunePrintServices() {
+        ArrayList<ComponentName> installedComponents;
+
         synchronized (mLock) {
-            ArrayList<ComponentName> installedComponents = getInstalledComponents();
+            installedComponents = getInstalledComponents();
 
             // Remove unnecessary entries from persistent state "disabled services"
             boolean disabledServicesUninstalled = mDisabledServices.retainAll(installedComponents);
             if (disabledServicesUninstalled) {
                 writeDisabledPrintServicesLocked(mDisabledServices);
             }
-
-            // Remove unnecessary entries from persistent state "approved services"
-            mSpooler.pruneApprovedPrintServices(installedComponents);
         }
+
+        // Remove unnecessary entries from persistent state "approved services"
+        mSpooler.pruneApprovedPrintServices(installedComponents);
+
     }
 
     private void onConfigurationChangedLocked() {
diff --git a/services/retaildemo/Android.mk b/services/retaildemo/Android.mk
new file mode 100644
index 0000000..670c6bf
--- /dev/null
+++ b/services/retaildemo/Android.mk
@@ -0,0 +1,12 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := services.retaildemo
+
+LOCAL_SRC_FILES += \
+      $(call all-java-files-under,java)
+
+LOCAL_JAVA_LIBRARIES := services.core
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/services/retaildemo/java/com/android/server/retaildemo/PreloadAppsInstaller.java b/services/retaildemo/java/com/android/server/retaildemo/PreloadAppsInstaller.java
new file mode 100644
index 0000000..2038c9e
--- /dev/null
+++ b/services/retaildemo/java/com/android/server/retaildemo/PreloadAppsInstaller.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.server.retaildemo;
+
+import android.app.AppGlobals;
+import android.app.PackageInstallObserver;
+import android.content.Context;
+import android.content.pm.IPackageManager;
+import android.content.pm.PackageManager;
+import android.os.Bundle;
+import android.os.Environment;
+import android.os.RemoteException;
+import android.os.UserHandle;
+import android.provider.Settings;
+import android.util.ArrayMap;
+import android.util.Log;
+import android.util.Slog;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.util.ArrayUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+
+/**
+ * Helper class for installing preloaded APKs
+ */
+class PreloadAppsInstaller {
+    private static final String SYSTEM_SERVER_PACKAGE_NAME = "android";
+    private static String TAG = PreloadAppsInstaller.class.getSimpleName();
+    private static final String PRELOAD_APK_EXT = ".apk.preload";
+    private static boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
+
+    private final IPackageManager mPackageManager;
+    private final File preloadsAppsDirectory;
+    private final Context mContext;
+
+    private final Map<String, String> mApkToPackageMap;
+
+    PreloadAppsInstaller(Context context) {
+        this(context, AppGlobals.getPackageManager(), Environment.getDataPreloadsAppsDirectory());
+    }
+
+    @VisibleForTesting
+    PreloadAppsInstaller(Context context, IPackageManager packageManager, File preloadsAppsDirectory) {
+        mContext = context;
+        mPackageManager = packageManager;
+        mApkToPackageMap = Collections.synchronizedMap(new ArrayMap<>());
+        this.preloadsAppsDirectory = preloadsAppsDirectory;
+    }
+
+    void installApps(int userId) {
+        File[] files = preloadsAppsDirectory.listFiles();
+        AppInstallCounter counter = new AppInstallCounter(mContext, userId);
+        if (ArrayUtils.isEmpty(files)) {
+            counter.setExpectedAppsCount(0);
+            return;
+        }
+        int expectedCount = 0;
+        for (File file : files) {
+            String apkName = file.getName();
+            if (apkName.endsWith(PRELOAD_APK_EXT) && file.isFile()) {
+                String packageName = mApkToPackageMap.get(apkName);
+                if (packageName != null) {
+                    try {
+                        expectedCount++;
+                        installExistingPackage(packageName, userId, counter);
+                    } catch (Exception e) {
+                        Slog.e(TAG, "Failed to install existing package " + packageName, e);
+                    }
+                } else {
+                    try {
+                        installPackage(file, userId, counter);
+                        expectedCount++;
+                    } catch (Exception e) {
+                        Slog.e(TAG, "Failed to install package from " + file, e);
+                    }
+                }
+            }
+        }
+        counter.setExpectedAppsCount(expectedCount);
+    }
+
+    private void installExistingPackage(String packageName, int userId,
+            AppInstallCounter counter) {
+        if (DEBUG) {
+            Log.d(TAG, "installExistingPackage " + packageName + " u" + userId);
+        }
+        try {
+            mPackageManager.installExistingPackageAsUser(packageName, userId);
+        } catch (RemoteException e) {
+            throw e.rethrowFromSystemServer();
+        } finally {
+            counter.appInstallFinished();
+        }
+    }
+
+    private void installPackage(File file, final int userId, AppInstallCounter counter)
+            throws IOException, RemoteException {
+        final String apkName = file.getName();
+        if (DEBUG) {
+            Log.d(TAG, "installPackage " + apkName + " u" + userId);
+        }
+        mPackageManager.installPackageAsUser(file.getPath(), new PackageInstallObserver() {
+            @Override
+            public void onPackageInstalled(String basePackageName, int returnCode, String msg,
+                    Bundle extras) {
+                if (DEBUG) {
+                    Log.d(TAG, "Package " + basePackageName + " installed u" + userId
+                            + " returnCode: " + returnCode + " msg: " + msg);
+                }
+                // Don't notify the counter for now, we'll do it in installExistingPackage
+                if (returnCode == PackageManager.INSTALL_SUCCEEDED) {
+                    mApkToPackageMap.put(apkName, basePackageName);
+                    // Install on user 0 so that the package is cached when demo user is re-created
+                    installExistingPackage(basePackageName, UserHandle.USER_SYSTEM, counter);
+                } else if (returnCode == PackageManager.INSTALL_FAILED_ALREADY_EXISTS) {
+                    // This can only happen in first session after a reboot
+                    if (!mApkToPackageMap.containsKey(apkName)) {
+                        mApkToPackageMap.put(apkName, basePackageName);
+                    }
+                    installExistingPackage(basePackageName, userId, counter);
+                } else {
+                    Log.e(TAG, "Package " + basePackageName + " cannot be installed from "
+                            + apkName + ": " + msg + " (returnCode " + returnCode + ")");
+                    counter.appInstallFinished();
+                }
+            }
+        }.getBinder(), 0, SYSTEM_SERVER_PACKAGE_NAME, userId);
+    }
+
+    private static class AppInstallCounter {
+        private int expectedCount = -1; // -1 means expectedCount not set
+        private int finishedCount;
+        private final Context mContext;
+        private final int userId;
+
+        AppInstallCounter(Context context, int userId) {
+            mContext = context;
+            this.userId = userId;
+        }
+
+        synchronized void appInstallFinished() {
+            this.finishedCount++;
+            checkIfAllFinished();
+        }
+
+        synchronized void setExpectedAppsCount(int expectedCount) {
+            this.expectedCount = expectedCount;
+            checkIfAllFinished();
+        }
+
+        private void checkIfAllFinished() {
+            if (expectedCount == finishedCount) {
+                Log.i(TAG, "All preloads finished installing for user " + userId);
+                Settings.Secure.putStringForUser(mContext.getContentResolver(),
+                        Settings.Secure.DEMO_USER_SETUP_COMPLETE, "1", userId);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/services/retaildemo/java/com/android/server/retaildemo/RetailDemoModeService.java b/services/retaildemo/java/com/android/server/retaildemo/RetailDemoModeService.java
new file mode 100644
index 0000000..6197c42
--- /dev/null
+++ b/services/retaildemo/java/com/android/server/retaildemo/RetailDemoModeService.java
@@ -0,0 +1,579 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.server.retaildemo;
+
+import android.Manifest;
+import android.app.ActivityManagerInternal;
+import android.app.ActivityManagerNative;
+import android.app.AppGlobals;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.app.RetailDemoModeServiceInternal;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.ContentProvider;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.IPackageManager;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.pm.UserInfo;
+import android.content.res.Configuration;
+import android.database.ContentObserver;
+import android.hardware.camera2.CameraAccessException;
+import android.hardware.camera2.CameraCharacteristics;
+import android.hardware.camera2.CameraManager;
+import android.media.AudioManager;
+import android.media.AudioSystem;
+import android.net.Uri;
+import android.net.wifi.WifiManager;
+import android.os.Environment;
+import android.os.FileUtils;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.os.PowerManager;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.os.SystemProperties;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.provider.CallLog;
+import android.provider.MediaStore;
+import android.provider.Settings;
+import android.util.KeyValueListParser;
+import android.util.Slog;
+import com.android.internal.os.BackgroundThread;
+import com.android.internal.R;
+import com.android.internal.annotations.GuardedBy;
+import com.android.internal.logging.MetricsLogger;
+import com.android.internal.widget.LockPatternUtils;
+import com.android.server.LocalServices;
+import com.android.server.ServiceThread;
+import com.android.server.SystemService;
+import com.android.server.am.ActivityManagerService;
+import com.android.server.retaildemo.UserInactivityCountdownDialog.OnCountDownExpiredListener;
+
+import java.io.File;
+import java.util.ArrayList;
+
+public class RetailDemoModeService extends SystemService {
+    private static final boolean DEBUG = false;
+
+    private static final String TAG = RetailDemoModeService.class.getSimpleName();
+    private static final String DEMO_USER_NAME = "Demo";
+    private static final String ACTION_RESET_DEMO =
+            "com.android.server.retaildemo.ACTION_RESET_DEMO";
+    private static final String SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED = "sys.retaildemo.enabled";
+
+    private static final int MSG_TURN_SCREEN_ON = 0;
+    private static final int MSG_INACTIVITY_TIME_OUT = 1;
+    private static final int MSG_START_NEW_SESSION = 2;
+
+    private static final long SCREEN_WAKEUP_DELAY = 2500;
+    private static final long USER_INACTIVITY_TIMEOUT_MIN = 10000;
+    private static final long USER_INACTIVITY_TIMEOUT_DEFAULT = 90000;
+    private static final long WARNING_DIALOG_TIMEOUT_DEFAULT = 0;
+    private static final long MILLIS_PER_SECOND = 1000;
+
+    private static final int[] VOLUME_STREAMS_TO_MUTE = {
+            AudioSystem.STREAM_RING,
+            AudioSystem.STREAM_MUSIC
+    };
+
+    // Tron Vars
+    private static final String DEMO_SESSION_COUNT = "retail_demo_session_count";
+    private static final String DEMO_SESSION_DURATION = "retail_demo_session_duration";
+
+    boolean mDeviceInDemoMode = false;
+    int mCurrentUserId = UserHandle.USER_SYSTEM;
+    long mUserInactivityTimeout;
+    long mWarningDialogTimeout;
+    private ActivityManagerService mAms;
+    private ActivityManagerInternal mAmi;
+    private AudioManager mAudioManager;
+    private NotificationManager mNm;
+    private UserManager mUm;
+    private PowerManager mPm;
+    private PowerManager.WakeLock mWakeLock;
+    Handler mHandler;
+    private ServiceThread mHandlerThread;
+    private PendingIntent mResetDemoPendingIntent;
+    private CameraManager mCameraManager;
+    private WifiManager mWifiManager;
+    private String[] mCameraIdsWithFlash;
+    private Configuration mSystemUserConfiguration;
+    private PreloadAppsInstaller mPreloadAppsInstaller;
+
+    final Object mActivityLock = new Object();
+    // Whether the newly created demo user has interacted with the screen yet
+    @GuardedBy("mActivityLock")
+    boolean mUserUntouched;
+    @GuardedBy("mActivityLock")
+    long mFirstUserActivityTime;
+    @GuardedBy("mActivityLock")
+    long mLastUserActivityTime;
+
+    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (!mDeviceInDemoMode) {
+                return;
+            }
+            switch (intent.getAction()) {
+                case Intent.ACTION_SCREEN_OFF:
+                    mHandler.removeMessages(MSG_TURN_SCREEN_ON);
+                    mHandler.sendEmptyMessageDelayed(MSG_TURN_SCREEN_ON, SCREEN_WAKEUP_DELAY);
+                    break;
+                case ACTION_RESET_DEMO:
+                    mHandler.sendEmptyMessage(MSG_START_NEW_SESSION);
+                    break;
+            }
+        }
+    };
+
+    final class MainHandler extends Handler {
+
+        MainHandler(Looper looper) {
+            super(looper, null, true);
+        }
+
+        @Override
+        public void handleMessage(Message msg) {
+            switch (msg.what) {
+                case MSG_TURN_SCREEN_ON:
+                    if (mWakeLock.isHeld()) {
+                        mWakeLock.release();
+                    }
+                    mWakeLock.acquire();
+                    break;
+                case MSG_INACTIVITY_TIME_OUT:
+                    if (isDemoLauncherDisabled()) {
+                        Slog.i(TAG, "User inactivity timeout reached");
+                        showInactivityCountdownDialog();
+                    }
+                    break;
+                case MSG_START_NEW_SESSION:
+                    if (DEBUG) {
+                        Slog.d(TAG, "Switching to a new demo user");
+                    }
+                    removeMessages(MSG_START_NEW_SESSION);
+                    removeMessages(MSG_INACTIVITY_TIME_OUT);
+                    if (mCurrentUserId != UserHandle.USER_SYSTEM) {
+                        logSessionDuration();
+                    }
+                    final UserInfo demoUser = getUserManager().createUser(DEMO_USER_NAME,
+                            UserInfo.FLAG_DEMO | UserInfo.FLAG_EPHEMERAL);
+                    if (demoUser != null) {
+                        setupDemoUser(demoUser);
+                        getActivityManager().switchUser(demoUser.id);
+                    }
+                    break;
+            }
+        }
+    }
+
+    private class SettingsObserver extends ContentObserver {
+
+        private final static String KEY_USER_INACTIVITY_TIMEOUT = "user_inactivity_timeout_ms";
+        private final static String KEY_WARNING_DIALOG_TIMEOUT = "warning_dialog_timeout_ms";
+
+        private final Uri mDeviceDemoModeUri = Settings.Global
+                .getUriFor(Settings.Global.DEVICE_DEMO_MODE);
+        private final Uri mDeviceProvisionedUri = Settings.Global
+                .getUriFor(Settings.Global.DEVICE_PROVISIONED);
+        private final Uri mRetailDemoConstantsUri = Settings.Global
+                .getUriFor(Settings.Global.RETAIL_DEMO_MODE_CONSTANTS);
+
+        private final KeyValueListParser mParser = new KeyValueListParser(',');
+
+        public SettingsObserver(Handler handler) {
+            super(handler);
+        }
+
+        public void register() {
+            ContentResolver cr = getContext().getContentResolver();
+            cr.registerContentObserver(mDeviceDemoModeUri, false, this, UserHandle.USER_SYSTEM);
+            cr.registerContentObserver(mDeviceProvisionedUri, false, this, UserHandle.USER_SYSTEM);
+            cr.registerContentObserver(mRetailDemoConstantsUri, false, this,
+                    UserHandle.USER_SYSTEM);
+        }
+
+        @Override
+        public void onChange(boolean selfChange, Uri uri) {
+            if (mRetailDemoConstantsUri.equals(uri)) {
+                refreshTimeoutConstants();
+                return;
+            }
+            if (mDeviceDemoModeUri.equals(uri)) {
+                mDeviceInDemoMode = UserManager.isDeviceInDemoMode(getContext());
+                if (mDeviceInDemoMode) {
+                    putDeviceInDemoMode();
+                } else {
+                    SystemProperties.set(SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED, "0");
+                    if (mWakeLock.isHeld()) {
+                        mWakeLock.release();
+                    }
+                }
+            }
+            // If device is provisioned and left demo mode - run the cleanup in demo folder
+            if (!mDeviceInDemoMode && isDeviceProvisioned()) {
+                // Run on the bg thread to not block the fg thread
+                BackgroundThread.getHandler().post(new Runnable() {
+                    @Override
+                    public void run() {
+                        if (!deletePreloadsFolderContents()) {
+                            Slog.w(TAG, "Failed to delete preloads folder contents");
+                        }
+                    }
+                });
+            }
+        }
+
+        private void refreshTimeoutConstants() {
+            try {
+                mParser.setString(Settings.Global.getString(getContext().getContentResolver(),
+                    Settings.Global.RETAIL_DEMO_MODE_CONSTANTS));
+            } catch (IllegalArgumentException exc) {
+                Slog.e(TAG, "Invalid string passed to KeyValueListParser");
+                // Consuming the exception to fall back to default values.
+            }
+            mWarningDialogTimeout = mParser.getLong(KEY_WARNING_DIALOG_TIMEOUT,
+                    WARNING_DIALOG_TIMEOUT_DEFAULT);
+            mUserInactivityTimeout = mParser.getLong(KEY_USER_INACTIVITY_TIMEOUT,
+                    USER_INACTIVITY_TIMEOUT_DEFAULT);
+            mUserInactivityTimeout = Math.max(mUserInactivityTimeout, USER_INACTIVITY_TIMEOUT_MIN);
+        }
+    }
+
+    private void showInactivityCountdownDialog() {
+        UserInactivityCountdownDialog dialog = new UserInactivityCountdownDialog(getContext(),
+                mWarningDialogTimeout, MILLIS_PER_SECOND);
+        dialog.setNegativeButtonClickListener(null);
+        dialog.setPositiveButtonClickListener(new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                mHandler.sendEmptyMessage(MSG_START_NEW_SESSION);
+            }
+        });
+        dialog.setOnCountDownExpiredListener(new OnCountDownExpiredListener() {
+            @Override
+            public void onCountDownExpired() {
+                mHandler.sendEmptyMessage(MSG_START_NEW_SESSION);
+            }
+        });
+        dialog.show();
+    }
+
+    public RetailDemoModeService(Context context) {
+        super(context);
+        synchronized (mActivityLock) {
+            mFirstUserActivityTime = mLastUserActivityTime = SystemClock.uptimeMillis();
+        }
+    }
+
+    private Notification createResetNotification() {
+        return new Notification.Builder(getContext())
+                .setContentTitle(getContext().getString(R.string.reset_retail_demo_mode_title))
+                .setContentText(getContext().getString(R.string.reset_retail_demo_mode_text))
+                .setOngoing(true)
+                .setSmallIcon(R.drawable.platlogo)
+                .setShowWhen(false)
+                .setVisibility(Notification.VISIBILITY_PUBLIC)
+                .setContentIntent(getResetDemoPendingIntent())
+                .setColor(getContext().getColor(R.color.system_notification_accent_color))
+                .build();
+    }
+
+    private PendingIntent getResetDemoPendingIntent() {
+        if (mResetDemoPendingIntent == null) {
+            Intent intent = new Intent(ACTION_RESET_DEMO);
+            mResetDemoPendingIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);
+        }
+        return mResetDemoPendingIntent;
+    }
+
+    boolean isDemoLauncherDisabled() {
+        IPackageManager pm = AppGlobals.getPackageManager();
+        int enabledState = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
+        String demoLauncherComponent = getContext().getResources()
+                .getString(R.string.config_demoModeLauncherComponent);
+        try {
+            enabledState = pm.getComponentEnabledSetting(
+                    ComponentName.unflattenFromString(demoLauncherComponent),
+                    mCurrentUserId);
+        } catch (RemoteException exc) {
+            Slog.e(TAG, "Unable to talk to Package Manager", exc);
+        }
+        return enabledState == PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
+    }
+
+    private void setupDemoUser(UserInfo userInfo) {
+        UserManager um = getUserManager();
+        UserHandle user = UserHandle.of(userInfo.id);
+        um.setUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, true, user);
+        um.setUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, true, user);
+        um.setUserRestriction(UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS, true, user);
+        um.setUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER, true, user);
+        um.setUserRestriction(UserManager.DISALLOW_MODIFY_ACCOUNTS, true, user);
+        um.setUserRestriction(UserManager.DISALLOW_CONFIG_BLUETOOTH, true, user);
+        // Set this to false because the default is true on user creation
+        um.setUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS, false, user);
+        // Disallow rebooting in safe mode - controlled by user 0
+        getUserManager().setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, true,
+                UserHandle.SYSTEM);
+        Settings.Secure.putIntForUser(getContext().getContentResolver(),
+                Settings.Secure.SKIP_FIRST_USE_HINTS, 1, userInfo.id);
+        Settings.Global.putInt(getContext().getContentResolver(),
+                Settings.Global.PACKAGE_VERIFIER_ENABLE, 0);
+        grantRuntimePermissionToCamera(user);
+        clearPrimaryCallLog();
+    }
+
+    private void grantRuntimePermissionToCamera(UserHandle user) {
+        final Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
+        final PackageManager pm = getContext().getPackageManager();
+        final ResolveInfo handler = pm.resolveActivityAsUser(cameraIntent,
+                PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE,
+                user.getIdentifier());
+        if (handler == null || handler.activityInfo == null) {
+            return;
+        }
+        try {
+            pm.grantRuntimePermission(handler.activityInfo.packageName,
+                    Manifest.permission.ACCESS_FINE_LOCATION, user);
+        } catch (Exception e) {
+            // Ignore
+        }
+    }
+
+    private void clearPrimaryCallLog() {
+        final ContentResolver resolver = getContext().getContentResolver();
+
+        // Deleting primary user call log so that it doesn't get copied to the new demo user
+        final Uri uri = CallLog.Calls.CONTENT_URI;
+        try {
+            resolver.delete(uri, null, null);
+        } catch (Exception e) {
+            Slog.w(TAG, "Deleting call log failed: " + e);
+        }
+    }
+
+    void logSessionDuration() {
+        final int sessionDuration;
+        synchronized (mActivityLock) {
+            sessionDuration = (int) ((mLastUserActivityTime - mFirstUserActivityTime) / 1000);
+        }
+        MetricsLogger.histogram(getContext(), DEMO_SESSION_DURATION, sessionDuration);
+    }
+
+    private ActivityManagerService getActivityManager() {
+        if (mAms == null) {
+            mAms = (ActivityManagerService) ActivityManagerNative.getDefault();
+        }
+        return mAms;
+    }
+
+    private UserManager getUserManager() {
+        if (mUm == null) {
+            mUm = getContext().getSystemService(UserManager.class);
+        }
+        return mUm;
+    }
+
+    private AudioManager getAudioManager() {
+        if (mAudioManager == null) {
+            mAudioManager = getContext().getSystemService(AudioManager.class);
+        }
+        return mAudioManager;
+    }
+
+    private boolean isDeviceProvisioned() {
+        return Settings.Global.getInt(
+                getContext().getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0) != 0;
+    }
+
+    private boolean deletePreloadsFolderContents() {
+        final File dir = Environment.getDataPreloadsDirectory();
+        Slog.i(TAG, "Deleting contents of " + dir);
+        return FileUtils.deleteContents(dir);
+    }
+
+    private void registerBroadcastReceiver() {
+        final IntentFilter filter = new IntentFilter();
+        filter.addAction(Intent.ACTION_SCREEN_OFF);
+        filter.addAction(ACTION_RESET_DEMO);
+        getContext().registerReceiver(mBroadcastReceiver, filter);
+    }
+
+    private String[] getCameraIdsWithFlash() {
+        ArrayList<String> cameraIdsList = new ArrayList<String>();
+        try {
+            for (String cameraId : mCameraManager.getCameraIdList()) {
+                CameraCharacteristics c = mCameraManager.getCameraCharacteristics(cameraId);
+                if (Boolean.TRUE.equals(c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE))) {
+                    cameraIdsList.add(cameraId);
+                }
+            }
+        } catch (CameraAccessException e) {
+            Slog.e(TAG, "Unable to access camera while getting camera id list", e);
+        }
+        return cameraIdsList.toArray(new String[cameraIdsList.size()]);
+    }
+
+    private void turnOffAllFlashLights() {
+        for (String cameraId : mCameraIdsWithFlash) {
+            try {
+                mCameraManager.setTorchMode(cameraId, false);
+            } catch (CameraAccessException e) {
+                Slog.e(TAG, "Unable to access camera " + cameraId + " while turning off flash", e);
+            }
+        }
+    }
+
+    private void muteVolumeStreams() {
+        for (int stream : VOLUME_STREAMS_TO_MUTE) {
+            getAudioManager().setStreamVolume(stream, getAudioManager().getStreamMinVolume(stream),
+                    0);
+        }
+    }
+
+    private Configuration getSystemUsersConfiguration() {
+        if (mSystemUserConfiguration == null) {
+            Settings.System.getConfiguration(getContext().getContentResolver(),
+                    mSystemUserConfiguration = new Configuration());
+        }
+        return mSystemUserConfiguration;
+    }
+
+    private void putDeviceInDemoMode() {
+        SystemProperties.set(SYSTEM_PROPERTY_RETAIL_DEMO_ENABLED, "1");
+        mHandler.sendEmptyMessage(MSG_START_NEW_SESSION);
+    }
+
+    @Override
+    public void onStart() {
+        if (DEBUG) {
+            Slog.d(TAG, "Service starting up");
+        }
+        mHandlerThread = new ServiceThread(TAG, android.os.Process.THREAD_PRIORITY_FOREGROUND,
+                false);
+        mHandlerThread.start();
+        mHandler = new MainHandler(mHandlerThread.getLooper());
+        publishLocalService(RetailDemoModeServiceInternal.class, mLocalService);
+    }
+
+    @Override
+    public void onBootPhase(int bootPhase) {
+        switch (bootPhase) {
+            case PHASE_THIRD_PARTY_APPS_CAN_START:
+                mPreloadAppsInstaller = new PreloadAppsInstaller(getContext());
+                mPm = (PowerManager) getContext().getSystemService(Context.POWER_SERVICE);
+                mAmi = LocalServices.getService(ActivityManagerInternal.class);
+                mWakeLock = mPm
+                        .newWakeLock(
+                                PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
+                                TAG);
+                mNm = NotificationManager.from(getContext());
+                mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
+                mCameraManager = (CameraManager) getContext()
+                        .getSystemService(Context.CAMERA_SERVICE);
+                mCameraIdsWithFlash = getCameraIdsWithFlash();
+                SettingsObserver settingsObserver = new SettingsObserver(mHandler);
+                settingsObserver.register();
+                settingsObserver.refreshTimeoutConstants();
+                registerBroadcastReceiver();
+                break;
+            case PHASE_BOOT_COMPLETED:
+                if (UserManager.isDeviceInDemoMode(getContext())) {
+                    mDeviceInDemoMode = true;
+                    putDeviceInDemoMode();
+                }
+                break;
+        }
+    }
+
+    @Override
+    public void onSwitchUser(int userId) {
+        if (!mDeviceInDemoMode) {
+            return;
+        }
+        if (DEBUG) {
+            Slog.d(TAG, "onSwitchUser: " + userId);
+        }
+        final UserInfo ui = getUserManager().getUserInfo(userId);
+        if (!ui.isDemo()) {
+            Slog.wtf(TAG, "Should not allow switch to non-demo user in demo mode");
+            return;
+        }
+        if (!mWakeLock.isHeld()) {
+            mWakeLock.acquire();
+        }
+        mCurrentUserId = userId;
+        mAmi.updatePersistentConfigurationForUser(getSystemUsersConfiguration(), userId);
+        turnOffAllFlashLights();
+        muteVolumeStreams();
+        if (!mWifiManager.isWifiEnabled()) {
+            mWifiManager.setWifiEnabled(true);
+        }
+        // Disable lock screen for demo users.
+        LockPatternUtils lockPatternUtils = new LockPatternUtils(getContext());
+        lockPatternUtils.setLockScreenDisabled(true, userId);
+        mNm.notifyAsUser(TAG, 1, createResetNotification(), UserHandle.of(userId));
+
+        synchronized (mActivityLock) {
+            mUserUntouched = true;
+        }
+        MetricsLogger.count(getContext(), DEMO_SESSION_COUNT, 1);
+        mHandler.removeMessages(MSG_INACTIVITY_TIME_OUT);
+        mHandler.post(new Runnable() {
+            @Override
+            public void run() {
+                mPreloadAppsInstaller.installApps(userId);
+            }
+        });
+    }
+
+    private RetailDemoModeServiceInternal mLocalService = new RetailDemoModeServiceInternal() {
+        private static final long USER_ACTIVITY_DEBOUNCE_TIME = 2000;
+
+        @Override
+        public void onUserActivity() {
+            if (!mDeviceInDemoMode) {
+                return;
+            }
+            long timeOfActivity = SystemClock.uptimeMillis();
+            synchronized (mActivityLock) {
+                if (timeOfActivity < mLastUserActivityTime + USER_ACTIVITY_DEBOUNCE_TIME) {
+                    return;
+                }
+                mLastUserActivityTime = timeOfActivity;
+                if (mUserUntouched && isDemoLauncherDisabled()) {
+                    Slog.d(TAG, "retail_demo first touch");
+                    mUserUntouched = false;
+                    mFirstUserActivityTime = timeOfActivity;
+                }
+            }
+            mHandler.removeMessages(MSG_INACTIVITY_TIME_OUT);
+            mHandler.sendEmptyMessageDelayed(MSG_INACTIVITY_TIME_OUT, mUserInactivityTimeout);
+        }
+    };
+}
diff --git a/services/retaildemo/java/com/android/server/retaildemo/UserInactivityCountdownDialog.java b/services/retaildemo/java/com/android/server/retaildemo/UserInactivityCountdownDialog.java
new file mode 100644
index 0000000..d14f4eb
--- /dev/null
+++ b/services/retaildemo/java/com/android/server/retaildemo/UserInactivityCountdownDialog.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.server.retaildemo;
+
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.content.Context;
+import android.os.CountDownTimer;
+import android.view.WindowManager;
+import android.widget.TextView;
+
+import com.android.internal.R;
+
+public class UserInactivityCountdownDialog extends AlertDialog {
+
+    private OnCountDownExpiredListener mOnCountDownExpiredListener;
+    private CountDownTimer mCountDownTimer;
+    private long mCountDownDuration;
+    private long mRefreshInterval;
+
+    UserInactivityCountdownDialog(Context context, long duration, long refreshInterval) {
+        super(context);
+        mCountDownDuration = duration;
+        mRefreshInterval = refreshInterval;
+
+        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
+        WindowManager.LayoutParams attrs = getWindow().getAttributes();
+        attrs.privateFlags = WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
+        getWindow().setAttributes(attrs);
+
+        setTitle(R.string.demo_user_inactivity_timeout_title);
+        setMessage(getContext().getString(R.string.demo_user_inactivity_timeout_countdown,
+                duration));
+    }
+
+    public void setOnCountDownExpiredListener(
+            OnCountDownExpiredListener onCountDownExpiredListener) {
+        mOnCountDownExpiredListener = onCountDownExpiredListener;
+    }
+
+    public void setPositiveButtonClickListener(OnClickListener onClickListener) {
+        setButton(Dialog.BUTTON_POSITIVE,
+                getContext().getString(R.string.demo_user_inactivity_timeout_right_button),
+                onClickListener);
+    }
+
+    public void setNegativeButtonClickListener(OnClickListener onClickListener) {
+        setButton(Dialog.BUTTON_NEGATIVE,
+                getContext().getString(R.string.demo_user_inactivity_timeout_left_button),
+                onClickListener);
+    }
+
+    @Override
+    public void show() {
+        super.show();
+        final TextView messageView = (TextView) findViewById(R.id.message);
+        messageView.post(new Runnable() {
+            @Override
+            public void run() {
+                mCountDownTimer = new CountDownTimer(mCountDownDuration, mRefreshInterval) {
+
+                    @Override
+                    public void onTick(long millisUntilFinished) {
+                        String msg = getContext().getString(
+                                R.string.demo_user_inactivity_timeout_countdown,
+                                millisUntilFinished / 1000);
+                        messageView.setText(msg);
+                    }
+
+                    @Override
+                    public void onFinish() {
+                        dismiss();
+                        if (mOnCountDownExpiredListener != null)
+                            mOnCountDownExpiredListener.onCountDownExpired();
+                    }
+                }.start();
+            }
+        });
+    }
+
+    @Override
+    public void onStop() {
+        if (mCountDownTimer != null) {
+            mCountDownTimer.cancel();
+        }
+    }
+
+    interface OnCountDownExpiredListener {
+        void onCountDownExpired();
+    }
+}
diff --git a/services/tests/servicestests/Android.mk b/services/tests/servicestests/Android.mk
index 8e3128c..3183930 100644
--- a/services/tests/servicestests/Android.mk
+++ b/services/tests/servicestests/Android.mk
@@ -20,7 +20,8 @@
     easymocklib \
     guava \
     android-support-test \
-    mockito-target
+    mockito-target \
+    ShortcutManagerTestUtils
 
 LOCAL_JAVA_LIBRARIES := android.test.runner
 
diff --git a/services/tests/servicestests/AndroidManifest.xml b/services/tests/servicestests/AndroidManifest.xml
index 7017d81..b8ace28 100644
--- a/services/tests/servicestests/AndroidManifest.xml
+++ b/services/tests/servicestests/AndroidManifest.xml
@@ -108,9 +108,53 @@
         <service android:name="com.android.server.job.MockPriorityJobService"
                  android:permission="android.permission.BIND_JOB_SERVICE" />
 
-        <activity android:name="com.android.server.pm.ShortcutManagerTest$ShortcutActivity" />
-        <activity android:name="com.android.server.pm.ShortcutManagerTest$ShortcutActivity2" />
-        <activity android:name="com.android.server.pm.ShortcutManagerTest$ShortcutActivity3" />
+        <activity android:name="com.android.server.pm.BaseShortcutManagerTest$ShortcutActivity" />
+        <activity android:name="com.android.server.pm.BaseShortcutManagerTest$ShortcutActivity2" />
+        <activity android:name="com.android.server.pm.BaseShortcutManagerTest$ShortcutActivity3" />
+
+        <activity android:name="com.android.server.pm.ShortcutTestActivity"
+            android:enabled="true" android:exported="true" />
+
+        <activity-alias android:name="a.ShortcutEnabled"
+            android:targetActivity="com.android.server.pm.ShortcutTestActivity"
+            android:enabled="true" android:exported="true">
+        </activity-alias>
+        <activity-alias android:name="a.ShortcutDisabled"
+            android:targetActivity="com.android.server.pm.ShortcutTestActivity"
+            android:enabled="false" android:exported="true">
+            <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcut_5"/>
+        </activity-alias>
+        <activity-alias android:name="a.ShortcutUnexported"
+            android:targetActivity="com.android.server.pm.ShortcutTestActivity"
+            android:enabled="true" android:exported="false">
+            <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcut_5"/>
+        </activity-alias>
+        <activity-alias android:name="a.Shortcut1"
+            android:targetActivity="com.android.server.pm.ShortcutTestActivity"
+            android:enabled="true" android:exported="true">
+            <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcut_1"/>
+        </activity-alias>
+
+        <activity-alias android:name="a.DisabledMain"
+            android:targetActivity="com.android.server.pm.ShortcutTestActivity"
+            android:enabled="false" android:exported="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity-alias>
+
+        <activity-alias android:name="a.UnexportedMain"
+            android:targetActivity="com.android.server.pm.ShortcutTestActivity"
+            android:enabled="true" android:exported="false">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity-alias>
+
     </application>
 
     <instrumentation
diff --git a/services/tests/servicestests/assets/shortcut/dumpsys_expected.txt b/services/tests/servicestests/assets/shortcut/dumpsys_expected.txt
new file mode 100644
index 0000000..eed2087
--- /dev/null
+++ b/services/tests/servicestests/assets/shortcut/dumpsys_expected.txt
@@ -0,0 +1,105 @@
+{
+ "shortcut": [
+  {
+   "userId": 0,
+   "launchers": [
+    {
+     "name": "com.android.launcher.1"
+    },
+    {
+     "name": "com.android.launcher.2"
+    },
+    {
+     "name": "com.android.launcher.3"
+    },
+    {
+     "name": "com.android.launcher.4"
+    },
+    {
+     "name": "com.android.launcher.1"
+    }
+   ],
+   "packages": [
+    {
+     "name": "com.android.test.1",
+     "dynamic": 3,
+     "manifest": 0,
+     "pinned": 4,
+     "bitmaps": 0,
+     "bitmapBytes": 0
+    },
+    {
+     "name": "com.android.test.2",
+     "dynamic": 4,
+     "manifest": 0,
+     "pinned": 5,
+     "bitmaps": 2,
+     "bitmapBytes": ***BITMAP_SIZE***
+    },
+    {
+     "name": "com.android.test.3",
+     "dynamic": 3,
+     "manifest": 0,
+     "pinned": 6,
+     "bitmaps": 0,
+     "bitmapBytes": 0
+    },
+    {
+     "name": "com.android.test.4",
+     "dynamic": 0,
+     "manifest": 0,
+     "pinned": 0,
+     "bitmaps": 0,
+     "bitmapBytes": 0
+    }
+   ]
+  },
+  {
+   "userId": 10,
+   "launchers": [
+    {
+     "name": "com.android.launcher.1"
+    }
+   ],
+   "packages": [
+    {
+     "name": "com.android.test.1",
+     "dynamic": 3,
+     "manifest": 0,
+     "pinned": 2,
+     "bitmaps": 0,
+     "bitmapBytes": 0
+    }
+   ]
+  },
+  {
+   "userId": 20,
+   "launchers": [
+    {
+     "name": "com.android.launcher.1"
+    },
+    {
+     "name": "com.android.launcher.2"
+    },
+    {
+     "name": "com.android.launcher.3"
+    },
+    {
+     "name": "com.android.launcher.1"
+    }
+   ],
+   "packages": [
+    {
+     "name": "com.android.test.1",
+     "dynamic": 3,
+     "manifest": 0,
+     "pinned": 6,
+     "bitmaps": 0,
+     "bitmapBytes": 0
+    }
+   ]
+  }
+ ],
+ "lowRam": false,
+ "iconSize": 128
+}
diff --git a/services/tests/servicestests/assets/shortcut/shortcut_legacy_file.xml b/services/tests/servicestests/assets/shortcut/shortcut_legacy_file.xml
new file mode 100644
index 0000000..f7eee91
--- /dev/null
+++ b/services/tests/servicestests/assets/shortcut/shortcut_legacy_file.xml
@@ -0,0 +1,25 @@
+<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<user locales="en-US" last-app-scan-time="3113976673">
+    <package name="com.android.test.1" call-count="0" last-reset="1468976368772">
+        <package-info version="25" last_udpate_time="1230796800000" />
+        <shortcut id="manifest-shortcut-storage" activity="com.android.test.1/com.android.test.1.Settings" title="Storage" intent="#Intent;action=android.settings.INTERNAL_STORAGE_SETTINGS;end" timestamp="1469050672334" flags="1" >
+            <intent-extras>
+                <int name="key" value="12345" />
+            </intent-extras>
+        </shortcut>
+    </package>
+</user>
diff --git a/services/tests/servicestests/res/drawable/icon3.png b/services/tests/servicestests/res/drawable/icon3.png
new file mode 100644
index 0000000..64eb294
--- /dev/null
+++ b/services/tests/servicestests/res/drawable/icon3.png
Binary files differ
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/services/tests/servicestests/res/values/strings.xml
similarity index 64%
copy from packages/SystemUI/res/layout/night_mode_settings.xml
copy to services/tests/servicestests/res/values/strings.xml
index 3725e78..2f9d06c 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/services/tests/servicestests/res/values/strings.xml
@@ -14,11 +14,11 @@
      limitations under the License.
 -->
 
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-
-    <include layout="@layout/switch_bar" />
-
-</LinearLayout>
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <string name="shortcut_title1"></string>
+    <string name="shortcut_text1"></string>
+    <string name="shortcut_disabled_message1"></string>
+    <string name="shortcut_title2"></string>
+    <string name="shortcut_text2"></string>
+    <string name="shortcut_disabled_message2"></string>
+</resources>
diff --git a/services/tests/servicestests/res/xml/shortcut_0.xml b/services/tests/servicestests/res/xml/shortcut_0.xml
new file mode 100644
index 0000000..fda001e
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_0.xml
@@ -0,0 +1,2 @@
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_1.xml b/services/tests/servicestests/res/xml/shortcut_1.xml
new file mode 100644
index 0000000..e3f9172
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_1.xml
@@ -0,0 +1,18 @@
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms1"
+        android:enabled="true"
+        android:icon="@drawable/icon1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        android:shortcutLongLabel="@string/shortcut_text1"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message1"
+        >
+        <intent
+            android:action="action1"
+            android:data="data1"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+        <categories android:name="android.shortcut.media" />
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_1_alt.xml b/services/tests/servicestests/res/xml/shortcut_1_alt.xml
new file mode 100644
index 0000000..2d5e8e7
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_1_alt.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms1-alt"
+        android:enabled="true"
+        android:icon="@drawable/icon1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        android:shortcutLongLabel="@string/shortcut_text1"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message1"
+        >
+        <intent
+            android:action="action1"
+            android:data="data1"
+        >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+        <categories android:name="android.shortcut.media" />
+    </shortcut>
+</shortcuts>
diff --git a/packages/SystemUI/res/layout/night_mode_settings.xml b/services/tests/servicestests/res/xml/shortcut_1_disable.xml
similarity index 61%
copy from packages/SystemUI/res/layout/night_mode_settings.xml
copy to services/tests/servicestests/res/xml/shortcut_1_disable.xml
index 3725e78..e3ee3a0 100644
--- a/packages/SystemUI/res/layout/night_mode_settings.xml
+++ b/services/tests/servicestests/res/xml/shortcut_1_disable.xml
@@ -13,12 +13,13 @@
      See the License for the specific language governing permissions and
      limitations under the License.
 -->
-
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical">
-
-    <include layout="@layout/switch_bar" />
-
-</LinearLayout>
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms1"
+        android:enabled="false"
+        android:icon="@drawable/icon2"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        android:shortcutLongLabel="@string/shortcut_text2"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message2"
+    />
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_2.xml b/services/tests/servicestests/res/xml/shortcut_2.xml
new file mode 100644
index 0000000..f7ea803
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_2.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms1"
+        android:enabled="true"
+        android:icon="@drawable/icon1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        android:shortcutLongLabel="@string/shortcut_text1"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message1"
+        >
+        <intent
+            android:action="action1"
+            android:data="http://a.b.c/"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+        <categories android:name="android.shortcut.media" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms2"
+        android:enabled="true"
+        android:icon="@drawable/icon2"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        android:shortcutLongLabel="@string/shortcut_text2"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message2"
+        >
+        <intent
+            android:action="action2"
+            android:data="http://a.b.c/2"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_2_duplicate.xml b/services/tests/servicestests/res/xml/shortcut_2_duplicate.xml
new file mode 100644
index 0000000..b00ec60
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_2_duplicate.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="action1"
+            >
+        </intent>
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms1"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        >
+        <intent
+            android:action="action2"
+            >
+        </intent>
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_3.xml b/services/tests/servicestests/res/xml/shortcut_3.xml
new file mode 100644
index 0000000..432ca49
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_3.xml
@@ -0,0 +1,56 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms1"
+        android:enabled="true"
+        android:icon="@drawable/icon1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        android:shortcutLongLabel="@string/shortcut_text1"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message1"
+        >
+        <intent
+            android:action="action1"
+            android:data="http://a.b.c/"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+        <categories android:name="android.shortcut.media" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms2"
+        android:enabled="true"
+        android:icon="@drawable/icon2"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        android:shortcutLongLabel="@string/shortcut_text2"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message2"
+        >
+        <intent
+            android:action="action2"
+            android:data="http://a.b.c/2"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms3"
+        android:enabled="true"
+        android:icon="@drawable/icon3"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        >
+        <intent android:action="action3" />
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_5.xml b/services/tests/servicestests/res/xml/shortcut_5.xml
new file mode 100644
index 0000000..9551100
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_5.xml
@@ -0,0 +1,85 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms1"
+        android:enabled="true"
+        android:icon="@drawable/icon1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        android:shortcutLongLabel="@string/shortcut_text1"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message1"
+        >
+        <intent
+            android:action="action1"
+            android:data="http://a.b.c/1"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+        <categories android:name="android.shortcut.media" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms2"
+        android:enabled="true"
+        android:icon="@drawable/icon2"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        android:shortcutLongLabel="@string/shortcut_text2"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message2"
+        >
+        <intent
+            android:action="action2"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms3"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms4"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        >
+        <intent
+            android:action="android.intent.action.VIEW2"
+            >
+        </intent>
+        <categories />
+        <categories android:name="" />
+        <categories android:name="cat" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms5"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="action"
+            android:data="http://www/"
+            android:targetPackage="abc"
+            android:targetClass=".xyz"
+            android:mimeType="foo/bar"
+            >
+            <categories android:name="cat1" />
+            <categories android:name="cat2" />
+            <extra android:name="key1" android:value="value1" />
+            <extra android:name="key2" android:value="value2" />
+        </intent>
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_5_alt.xml b/services/tests/servicestests/res/xml/shortcut_5_alt.xml
new file mode 100644
index 0000000..f79cd6f
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_5_alt.xml
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms1_alt"
+        android:enabled="true"
+        android:icon="@drawable/icon1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        android:shortcutLongLabel="@string/shortcut_text1"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message1"
+        >
+        <intent
+            android:action="action1"
+            android:data="http://a.b.c/1"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+        <categories android:name="android.shortcut.media" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms2_alt"
+        android:enabled="true"
+        android:icon="@drawable/icon2"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        android:shortcutLongLabel="@string/shortcut_text2"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message2"
+        >
+        <intent
+            android:action="action2"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms3_alt"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms4_alt"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms5_alt"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_5_reverse.xml b/services/tests/servicestests/res/xml/shortcut_5_reverse.xml
new file mode 100644
index 0000000..d5b7c8f
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_5_reverse.xml
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="ms5"
+        android:enabled="true"
+        android:icon="@drawable/icon1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        android:shortcutLongLabel="@string/shortcut_text1"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message1"
+        >
+        <intent
+            android:action="action1"
+            android:data="http://a.b.c/1"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+        <categories android:name="android.shortcut.media" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms4"
+        android:enabled="true"
+        android:icon="@drawable/icon2"
+        android:shortcutShortLabel="@string/shortcut_title2"
+        android:shortcutLongLabel="@string/shortcut_text2"
+        android:shortcutDisabledMessage="@string/shortcut_disabled_message2"
+        >
+        <intent
+            android:action="action2"
+            >
+        </intent>
+        <categories android:name="android.shortcut.conversation" />
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms3"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms2"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+    <shortcut
+        android:shortcutId="ms1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="action"
+            >
+            <categories android:name="cat1" />
+            <categories android:name="cat2" />
+            <extra android:name="key1" android:value="value1" />
+            <extra android:name="key2" android:value="value2" />
+        </intent>
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_error_1.xml b/services/tests/servicestests/res/xml/shortcut_error_1.xml
new file mode 100644
index 0000000..3990d02
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_error_1.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+    <shortcut
+        android:shortcutId="x1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_error_2.xml b/services/tests/servicestests/res/xml/shortcut_error_2.xml
new file mode 100644
index 0000000..a6f7150
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_error_2.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="manifest-shortcut-3"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+    <shortcut
+        android:shortcutId="x2"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_error_3.xml b/services/tests/servicestests/res/xml/shortcut_error_3.xml
new file mode 100644
index 0000000..24ee024
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_error_3.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <shortcut
+        android:shortcutId="manifest-shortcut-3"
+        android:shortcutShortLabel="@string/shortcut_title1"
+    />
+    <shortcut
+        android:shortcutId="@string/shortcut_title1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+    />
+    <shortcut
+        android:shortcutId="x3"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent
+            android:action="android.intent.action.VIEW"
+            >
+        </intent>
+        <categories android:name="@string/shortcut_title1" />
+        <categories android:name="cat2" />
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/res/xml/shortcut_error_4.xml b/services/tests/servicestests/res/xml/shortcut_error_4.xml
new file mode 100644
index 0000000..f680e99
--- /dev/null
+++ b/services/tests/servicestests/res/xml/shortcut_error_4.xml
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2016 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+    <!-- This is valid -->
+    <shortcut
+        android:shortcutId="ms1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent android:action="action1" >
+            <extra android:name="key1" android:value="value1" />
+        </intent>
+    </shortcut>
+
+    <!-- Invalid: no intent -->
+    <shortcut
+        android:shortcutId="ms_ignored1"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        />
+
+    <!-- Valid: more than one intent -->
+    <shortcut
+        android:shortcutId="ms2"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent android:action="action2_1" >
+            <extra android:name="key1" android:value="value1" />
+        </intent>
+        <intent android:action="action2_2">
+            <extra android:name="key2" android:value="value2" />
+        </intent>
+    </shortcut>
+
+    <!-- Valid: disabled shortcut doesn't need an intent -->
+    <shortcut
+        android:shortcutId="ms3"
+        android:enabled="false"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        />
+
+    <!-- Valid, but disabled shortcut's intent will be ignored. -->
+    <shortcut
+        android:shortcutId="ms4"
+        android:enabled="false"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent android:action="action4" />
+    </shortcut>
+
+    <!-- Invalid, no intent action (if any of the intents is invalid, the entire shortcut will be invalid.) -->
+    <shortcut
+        android:shortcutId="ms_ignored2"
+        android:shortcutShortLabel="@string/shortcut_title1"
+        >
+        <intent android:data="x"/>
+        <intent android:action="actionx"/>
+    </shortcut>
+</shortcuts>
diff --git a/services/tests/servicestests/src/android/net/ConnectivityMetricsLoggerTest.java b/services/tests/servicestests/src/android/net/ConnectivityMetricsLoggerTest.java
new file mode 100644
index 0000000..6d42cce
--- /dev/null
+++ b/services/tests/servicestests/src/android/net/ConnectivityMetricsLoggerTest.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2016, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net;
+
+import android.os.Bundle;
+import android.os.Parcel;
+import java.util.List;
+import junit.framework.TestCase;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ConnectivityMetricsLoggerTest extends TestCase {
+
+    // use same Parcel object everywhere for pointer equality
+    static final Bundle FAKE_EV = new Bundle();
+    static final int FAKE_COMPONENT = 1;
+    static final int FAKE_EVENT = 2;
+
+    @Mock IConnectivityMetricsLogger mService;
+    ArgumentCaptor<ConnectivityMetricsEvent> evCaptor;
+    ArgumentCaptor<ConnectivityMetricsEvent[]> evArrayCaptor;
+
+    ConnectivityMetricsLogger mLog;
+
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        evCaptor = ArgumentCaptor.forClass(ConnectivityMetricsEvent.class);
+        evArrayCaptor = ArgumentCaptor.forClass(ConnectivityMetricsEvent[].class);
+        mLog = new ConnectivityMetricsLogger(mService);
+    }
+
+    public void testLogEvents() throws Exception {
+        mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+        mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+        mLog.logEvent(3, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+
+        List<ConnectivityMetricsEvent> gotEvents = verifyEvents(3);
+        assertEventsEqual(expectedEvent(1), gotEvents.get(0));
+        assertEventsEqual(expectedEvent(2), gotEvents.get(1));
+        assertEventsEqual(expectedEvent(3), gotEvents.get(2));
+    }
+
+    public void testLogEventTriggerThrottling() throws Exception {
+        when(mService.logEvent(any())).thenReturn(1234L);
+
+        mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+        mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+
+        List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1);
+        assertEventsEqual(expectedEvent(1), gotEvents.get(0));
+    }
+
+    public void testLogEventFails() throws Exception {
+        when(mService.logEvent(any())).thenReturn(-1L); // Error.
+
+        mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+        mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+
+        List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1);
+        assertEventsEqual(expectedEvent(1), gotEvents.get(0));
+    }
+
+    public void testLogEventWhenThrottling() throws Exception {
+        when(mService.logEvent(any())).thenReturn(Long.MAX_VALUE); // Throttled
+
+        // No events are logged. The service is only called once
+        // After that, throttling state is maintained locally.
+        mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+        mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+
+        List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1);
+        assertEventsEqual(expectedEvent(1), gotEvents.get(0));
+    }
+
+    public void testLogEventRecoverFromThrottling() throws Exception {
+        final long throttleTimeout = System.currentTimeMillis() + 10;
+        when(mService.logEvent(any())).thenReturn(throttleTimeout, 0L);
+
+        mLog.logEvent(1, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+        mLog.logEvent(2, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+        mLog.logEvent(3, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+        Thread.sleep(100);
+        mLog.logEvent(53, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+
+        List<ConnectivityMetricsEvent> gotEvents = verifyEvents(1);
+        assertEventsEqual(expectedEvent(1), gotEvents.get(0));
+
+        verify(mService, times(1)).logEvents(evArrayCaptor.capture());
+        ConnectivityMetricsEvent[] gotOtherEvents = evArrayCaptor.getAllValues().get(0);
+        assertEquals(ConnectivityMetricsLogger.TAG_SKIPPED_EVENTS, gotOtherEvents[0].eventTag);
+        assertEventsEqual(expectedEvent(53), gotOtherEvents[1]);
+    }
+
+    List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception {
+        verify(mService, times(n)).logEvent(evCaptor.capture());
+        return evCaptor.getAllValues();
+    }
+
+    static ConnectivityMetricsEvent expectedEvent(int timestamp) {
+        return new ConnectivityMetricsEvent((long)timestamp, FAKE_COMPONENT, FAKE_EVENT, FAKE_EV);
+    }
+
+    /** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */
+    static void assertEventsEqual(ConnectivityMetricsEvent expected, ConnectivityMetricsEvent got) {
+        assertEquals(expected.timestamp, got.timestamp);
+        assertEquals(expected.componentTag, got.componentTag);
+        assertEquals(expected.eventTag, got.eventTag);
+        assertEquals(expected.data, got.data);
+    }
+}
diff --git a/services/tests/servicestests/src/android/net/apf/ApfTest.java b/services/tests/servicestests/src/android/net/apf/ApfTest.java
index 8ac238a..f7c61d1 100644
--- a/services/tests/servicestests/src/android/net/apf/ApfTest.java
+++ b/services/tests/servicestests/src/android/net/apf/ApfTest.java
@@ -20,20 +20,30 @@
 
 import com.android.frameworks.servicestests.R;
 
+import android.net.LinkAddress;
+import android.net.LinkProperties;
+import android.net.NetworkUtils;
 import android.net.apf.ApfCapabilities;
 import android.net.apf.ApfFilter;
 import android.net.apf.ApfGenerator;
 import android.net.apf.ApfGenerator.IllegalInstructionException;
 import android.net.apf.ApfGenerator.Register;
 import android.net.ip.IpManager;
-import android.net.LinkAddress;
-import android.net.LinkProperties;
+import android.net.metrics.IpConnectivityLog;
+import android.net.metrics.RaEvent;
 import android.os.ConditionVariable;
+import android.os.Parcelable;
 import android.system.ErrnoException;
 import android.system.Os;
 import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.LargeTest;
 
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.verify;
+
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.FileOutputStream;
@@ -43,6 +53,7 @@
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.nio.ByteBuffer;
+import java.util.List;
 
 import libcore.io.IoUtils;
 import libcore.io.Streams;
@@ -51,14 +62,17 @@
  * Tests for APF program generator and interpreter.
  *
  * Build, install and run with:
- *  runtest frameworks-services -c com.android.server.ApfTest
+ *  runtest frameworks-services -c android.net.apf.ApfTest
  */
 public class ApfTest extends AndroidTestCase {
     private static final int TIMEOUT_MS = 500;
 
+    @Mock IpConnectivityLog mLog;
+
     @Override
     public void setUp() throws Exception {
         super.setUp();
+        MockitoAnnotations.initMocks(this);
         // Load up native shared library containing APF interpreter exposed via JNI.
         System.loadLibrary("servicestestsjni");
     }
@@ -70,21 +84,48 @@
     // least the minimum packet size.
     private final static int MIN_PKT_SIZE = 15;
 
+    private final static boolean DROP_MULTICAST = true;
+    private final static boolean ALLOW_MULTICAST = false;
+
+    private static String label(int code) {
+        switch (code) {
+            case PASS: return "PASS";
+            case DROP: return "DROP";
+            default:   return "UNKNOWN";
+        }
+    }
+
+    private static void assertReturnCodesEqual(int expected, int got) {
+        assertEquals(label(expected), label(got));
+    }
+
     private void assertVerdict(int expected, byte[] program, byte[] packet, int filterAge) {
-        assertEquals(expected, apfSimulate(program, packet, filterAge));
+        assertReturnCodesEqual(expected, apfSimulate(program, packet, filterAge));
+    }
+
+    private void assertVerdict(int expected, byte[] program, byte[] packet) {
+        assertReturnCodesEqual(expected, apfSimulate(program, packet, 0));
     }
 
     private void assertPass(byte[] program, byte[] packet, int filterAge) {
         assertVerdict(PASS, program, packet, filterAge);
     }
 
+    private void assertPass(byte[] program, byte[] packet) {
+        assertVerdict(PASS, program, packet);
+    }
+
     private void assertDrop(byte[] program, byte[] packet, int filterAge) {
         assertVerdict(DROP, program, packet, filterAge);
     }
 
+    private void assertDrop(byte[] program, byte[] packet) {
+        assertVerdict(DROP, program, packet);
+    }
+
     private void assertVerdict(int expected, ApfGenerator gen, byte[] packet, int filterAge)
             throws IllegalInstructionException {
-        assertEquals(expected, apfSimulate(gen.generate(), packet, filterAge));
+        assertReturnCodesEqual(expected, apfSimulate(gen.generate(), packet, filterAge));
     }
 
     private void assertPass(ApfGenerator gen, byte[] packet, int filterAge)
@@ -500,7 +541,7 @@
         gen = new ApfGenerator();
         gen.addLoadImmediate(Register.R0, 1);
         gen.addJumpIfBytesNotEqual(Register.R0, new byte[]{123}, gen.DROP_LABEL);
-        byte[] packet123 = new byte[]{0,123,0,0,0,0,0,0,0,0,0,0,0,0,0};
+        byte[] packet123 = {0,123,0,0,0,0,0,0,0,0,0,0,0,0,0};
         assertPass(gen, packet123, 0);
         gen = new ApfGenerator();
         gen.addJumpIfBytesNotEqual(Register.R0, new byte[]{123}, gen.DROP_LABEL);
@@ -508,7 +549,7 @@
         gen = new ApfGenerator();
         gen.addLoadImmediate(Register.R0, 1);
         gen.addJumpIfBytesNotEqual(Register.R0, new byte[]{1,2,30,4,5}, gen.DROP_LABEL);
-        byte[] packet12345 = new byte[]{0,1,2,3,4,5,0,0,0,0,0,0,0,0,0};
+        byte[] packet12345 = {0,1,2,3,4,5,0,0,0,0,0,0,0,0,0};
         assertDrop(gen, packet12345, 0);
         gen = new ApfGenerator();
         gen.addLoadImmediate(Register.R0, 1);
@@ -559,13 +600,13 @@
     }
 
     private static class TestApfFilter extends ApfFilter {
-        public final static byte[] MOCK_MAC_ADDR = new byte[]{1,2,3,4,5,6};
+        public final static byte[] MOCK_MAC_ADDR = {1,2,3,4,5,6};
         private FileDescriptor mWriteSocket;
 
-        public TestApfFilter(IpManager.Callback ipManagerCallback, boolean multicastFilter) throws
-                Exception {
-            super(new ApfCapabilities(2, 1000, ARPHRD_ETHER), NetworkInterface.getByName("lo"),
-                    ipManagerCallback, multicastFilter);
+        public TestApfFilter(IpManager.Callback ipManagerCallback, boolean multicastFilter,
+                IpConnectivityLog log) throws Exception {
+            super(new ApfCapabilities(2, 1700, ARPHRD_ETHER), NetworkInterface.getByName("lo"),
+                    ipManagerCallback, multicastFilter, log);
         }
 
         // Pretend an RA packet has been received and show it to ApfFilter.
@@ -602,20 +643,23 @@
     }
 
     private static final int ETH_HEADER_LEN = 14;
+    private static final int ETH_DEST_ADDR_OFFSET = 0;
     private static final int ETH_ETHERTYPE_OFFSET = 12;
-    private static final byte[] ETH_BROADCAST_MAC_ADDRESS = new byte[]{
-        (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
+    private static final byte[] ETH_BROADCAST_MAC_ADDRESS =
+            {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
 
     private static final int IPV4_VERSION_IHL_OFFSET = ETH_HEADER_LEN + 0;
     private static final int IPV4_PROTOCOL_OFFSET = ETH_HEADER_LEN + 9;
     private static final int IPV4_DEST_ADDR_OFFSET = ETH_HEADER_LEN + 16;
+    private static final byte[] IPV4_BROADCAST_ADDRESS =
+            {(byte) 255, (byte) 255, (byte) 255, (byte) 255};
 
     private static final int IPV6_NEXT_HEADER_OFFSET = ETH_HEADER_LEN + 6;
     private static final int IPV6_HEADER_LEN = 40;
     private static final int IPV6_DEST_ADDR_OFFSET = ETH_HEADER_LEN + 24;
     // The IPv6 all nodes address ff02::1
     private static final byte[] IPV6_ALL_NODES_ADDRESS =
-            new byte[]{ (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
+            { (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
 
     private static final int ICMP6_TYPE_OFFSET = ETH_HEADER_LEN + IPV6_HEADER_LEN;
     private static final int ICMP6_ROUTER_ADVERTISEMENT = 134;
@@ -652,46 +696,80 @@
     private static final int DHCP_CLIENT_PORT = 68;
     private static final int DHCP_CLIENT_MAC_OFFSET = ETH_HEADER_LEN + UDP_HEADER_LEN + 48;
 
-    private static int ARP_HEADER_OFFSET = ETH_HEADER_LEN;
-    private static final byte[] ARP_IPV4_REQUEST_HEADER = new byte[]{
+    private static final int ARP_HEADER_OFFSET = ETH_HEADER_LEN;
+    private static final byte[] ARP_IPV4_REQUEST_HEADER = {
             0, 1, // Hardware type: Ethernet (1)
             8, 0, // Protocol type: IP (0x0800)
             6,    // Hardware size: 6
             4,    // Protocol size: 4
             0, 1  // Opcode: request (1)
     };
-    private static int ARP_TARGET_IP_ADDRESS_OFFSET = ETH_HEADER_LEN + 24;
+    private static final byte[] ARP_IPV4_REPLY_HEADER = {
+            0, 1, // Hardware type: Ethernet (1)
+            8, 0, // Protocol type: IP (0x0800)
+            6,    // Hardware size: 6
+            4,    // Protocol size: 4
+            0, 2  // Opcode: reply (2)
+    };
+    private static final int ARP_TARGET_IP_ADDRESS_OFFSET = ETH_HEADER_LEN + 24;
 
-    private static byte[] MOCK_IPV4_ADDR = new byte[]{10, 0, 0, 1};
+    private static final byte[] MOCK_IPV4_ADDR           = {10, 0, 0, 1};
+    private static final byte[] MOCK_BROADCAST_IPV4_ADDR = {10, 0, 31, (byte) 255}; // prefix = 19
+    private static final byte[] MOCK_MULTICAST_IPV4_ADDR = {(byte) 224, 0, 0, 1};
+    private static final byte[] ANOTHER_IPV4_ADDR        = {10, 0, 0, 2};
+    private static final byte[] IPV4_ANY_HOST_ADDR       = {0, 0, 0, 0};
 
     @LargeTest
     public void testApfFilterIPv4() throws Exception {
         MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
-        ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, true /* multicastFilter */);
+        LinkAddress link = new LinkAddress(InetAddress.getByAddress(MOCK_IPV4_ADDR), 19);
+        LinkProperties lp = new LinkProperties();
+        lp.addLinkAddress(link);
+
+        ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, DROP_MULTICAST, mLog);
+        apfFilter.setLinkProperties(lp);
+
         byte[] program = ipManagerCallback.getApfProgram();
 
         // Verify empty packet of 100 zero bytes is passed
         ByteBuffer packet = ByteBuffer.wrap(new byte[100]);
-        assertPass(program, packet.array(), 0);
+        assertPass(program, packet.array());
 
         // Verify unicast IPv4 packet is passed
+        put(packet, ETH_DEST_ADDR_OFFSET, TestApfFilter.MOCK_MAC_ADDR);
         packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_IP);
-        assertPass(program, packet.array(), 0);
+        put(packet, IPV4_DEST_ADDR_OFFSET, MOCK_IPV4_ADDR);
+        assertPass(program, packet.array());
 
-        // Verify broadcast IPv4, not DHCP to us, is dropped
-        packet.put(ETH_BROADCAST_MAC_ADDRESS);
-        assertDrop(program, packet.array(), 0);
+        // Verify L2 unicast to IPv4 broadcast addresses is dropped (b/30231088)
+        put(packet, IPV4_DEST_ADDR_OFFSET, IPV4_BROADCAST_ADDRESS);
+        assertDrop(program, packet.array());
+        put(packet, IPV4_DEST_ADDR_OFFSET, MOCK_BROADCAST_IPV4_ADDR);
+        assertDrop(program, packet.array());
+
+        // Verify multicast/broadcast IPv4, not DHCP to us, is dropped
+        put(packet, ETH_DEST_ADDR_OFFSET, ETH_BROADCAST_MAC_ADDRESS);
+        assertDrop(program, packet.array());
         packet.put(IPV4_VERSION_IHL_OFFSET, (byte)0x45);
-        assertDrop(program, packet.array(), 0);
+        assertDrop(program, packet.array());
         packet.put(IPV4_PROTOCOL_OFFSET, (byte)IPPROTO_UDP);
-        assertDrop(program, packet.array(), 0);
+        assertDrop(program, packet.array());
         packet.putShort(UDP_DESTINATION_PORT_OFFSET, (short)DHCP_CLIENT_PORT);
-        assertDrop(program, packet.array(), 0);
+        assertDrop(program, packet.array());
+        put(packet, IPV4_DEST_ADDR_OFFSET, MOCK_MULTICAST_IPV4_ADDR);
+        assertDrop(program, packet.array());
+        put(packet, IPV4_DEST_ADDR_OFFSET, MOCK_BROADCAST_IPV4_ADDR);
+        assertDrop(program, packet.array());
+        put(packet, IPV4_DEST_ADDR_OFFSET, IPV4_BROADCAST_ADDRESS);
+        assertDrop(program, packet.array());
 
         // Verify broadcast IPv4 DHCP to us is passed
-        packet.position(DHCP_CLIENT_MAC_OFFSET);
-        packet.put(TestApfFilter.MOCK_MAC_ADDR);
-        assertPass(program, packet.array(), 0);
+        put(packet, DHCP_CLIENT_MAC_OFFSET, TestApfFilter.MOCK_MAC_ADDR);
+        assertPass(program, packet.array());
+
+        // Verify unicast IPv4 DHCP to us is passed
+        put(packet, ETH_DEST_ADDR_OFFSET, TestApfFilter.MOCK_MAC_ADDR);
+        assertPass(program, packet.array());
 
         apfFilter.shutdown();
     }
@@ -699,137 +777,185 @@
     @LargeTest
     public void testApfFilterIPv6() throws Exception {
         MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
-        ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, false /* multicastFilter */);
+        ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, ALLOW_MULTICAST, mLog);
         byte[] program = ipManagerCallback.getApfProgram();
 
         // Verify empty IPv6 packet is passed
         ByteBuffer packet = ByteBuffer.wrap(new byte[100]);
         packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_IPV6);
-        assertPass(program, packet.array(), 0);
+        assertPass(program, packet.array());
 
         // Verify empty ICMPv6 packet is passed
         packet.put(IPV6_NEXT_HEADER_OFFSET, (byte)IPPROTO_ICMPV6);
-        assertPass(program, packet.array(), 0);
+        assertPass(program, packet.array());
 
         // Verify empty ICMPv6 NA packet is passed
         packet.put(ICMP6_TYPE_OFFSET, (byte)ICMP6_NEIGHBOR_ANNOUNCEMENT);
-        assertPass(program, packet.array(), 0);
+        assertPass(program, packet.array());
 
         // Verify ICMPv6 NA to ff02::1 is dropped
-        packet.position(IPV6_DEST_ADDR_OFFSET);
-        packet.put(IPV6_ALL_NODES_ADDRESS);
-        assertDrop(program, packet.array(), 0);
+        put(packet, IPV6_DEST_ADDR_OFFSET, IPV6_ALL_NODES_ADDRESS);
+        assertDrop(program, packet.array());
 
         apfFilter.shutdown();
     }
 
     @LargeTest
     public void testApfFilterMulticast() throws Exception {
+        final byte[] unicastIpv4Addr   = {(byte)192,0,2,63};
+        final byte[] broadcastIpv4Addr = {(byte)192,0,2,(byte)255};
+        final byte[] multicastIpv4Addr = {(byte)224,0,0,1};
+        final byte[] multicastIpv6Addr = {(byte)0xff,2,0,0,0,0,0,0,0,0,0,0,0,0,0,(byte)0xfb};
+
         MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
-        ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, false /* multicastFilter */);
+        LinkAddress link = new LinkAddress(InetAddress.getByAddress(unicastIpv4Addr), 24);
+        LinkProperties lp = new LinkProperties();
+        lp.addLinkAddress(link);
+
+        ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, ALLOW_MULTICAST, mLog);
+        apfFilter.setLinkProperties(lp);
+
         byte[] program = ipManagerCallback.getApfProgram();
 
         // Construct IPv4 and IPv6 multicast packets.
         ByteBuffer mcastv4packet = ByteBuffer.wrap(new byte[100]);
         mcastv4packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_IP);
-        mcastv4packet.position(IPV4_DEST_ADDR_OFFSET);
-        mcastv4packet.put(new byte[]{(byte)224,0,0,1});
+        put(mcastv4packet, IPV4_DEST_ADDR_OFFSET, multicastIpv4Addr);
 
         ByteBuffer mcastv6packet = ByteBuffer.wrap(new byte[100]);
         mcastv6packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_IPV6);
         mcastv6packet.put(IPV6_NEXT_HEADER_OFFSET, (byte)IPPROTO_UDP);
-        mcastv6packet.position(IPV6_DEST_ADDR_OFFSET);
-        mcastv6packet.put(new byte[]{(byte)0xff,2,0,0,0,0,0,0,0,0,0,0,0,0,0,(byte)0xfb});
+        put(mcastv6packet, IPV6_DEST_ADDR_OFFSET, multicastIpv6Addr);
 
         // Construct IPv4 broadcast packet.
-        ByteBuffer bcastv4packet = ByteBuffer.wrap(new byte[100]);
-        bcastv4packet.put(ETH_BROADCAST_MAC_ADDRESS);
-        bcastv4packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_IP);
-        bcastv4packet.position(IPV4_DEST_ADDR_OFFSET);
-        bcastv4packet.put(new byte[]{(byte)192,(byte)0,(byte)2,(byte)63});
+        ByteBuffer bcastv4packet1 = ByteBuffer.wrap(new byte[100]);
+        bcastv4packet1.put(ETH_BROADCAST_MAC_ADDRESS);
+        bcastv4packet1.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_IP);
+        put(bcastv4packet1, IPV4_DEST_ADDR_OFFSET, multicastIpv4Addr);
+
+        ByteBuffer bcastv4packet2 = ByteBuffer.wrap(new byte[100]);
+        bcastv4packet2.put(ETH_BROADCAST_MAC_ADDRESS);
+        bcastv4packet2.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_IP);
+        put(bcastv4packet2, IPV4_DEST_ADDR_OFFSET, IPV4_BROADCAST_ADDRESS);
+
+        // Construct IPv4 broadcast with L2 unicast address packet (b/30231088).
+        ByteBuffer bcastv4unicastl2packet = ByteBuffer.wrap(new byte[100]);
+        bcastv4unicastl2packet.put(TestApfFilter.MOCK_MAC_ADDR);
+        bcastv4unicastl2packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_IP);
+        put(bcastv4unicastl2packet, IPV4_DEST_ADDR_OFFSET, broadcastIpv4Addr);
 
         // Verify initially disabled multicast filter is off
-        assertPass(program, bcastv4packet.array(), 0);
-        assertPass(program, mcastv4packet.array(), 0);
-        assertPass(program, mcastv6packet.array(), 0);
+        assertPass(program, mcastv4packet.array());
+        assertPass(program, mcastv6packet.array());
+        assertPass(program, bcastv4packet1.array());
+        assertPass(program, bcastv4packet2.array());
+        assertPass(program, bcastv4unicastl2packet.array());
 
         // Turn on multicast filter and verify it works
         ipManagerCallback.resetApfProgramWait();
         apfFilter.setMulticastFilter(true);
         program = ipManagerCallback.getApfProgram();
-        assertDrop(program, bcastv4packet.array(), 0);
-        assertDrop(program, mcastv4packet.array(), 0);
-        assertDrop(program, mcastv6packet.array(), 0);
+        assertDrop(program, mcastv4packet.array());
+        assertDrop(program, mcastv6packet.array());
+        assertDrop(program, bcastv4packet1.array());
+        assertDrop(program, bcastv4packet2.array());
+        assertDrop(program, bcastv4unicastl2packet.array());
 
         // Turn off multicast filter and verify it's off
         ipManagerCallback.resetApfProgramWait();
         apfFilter.setMulticastFilter(false);
         program = ipManagerCallback.getApfProgram();
-        assertPass(program, bcastv4packet.array(), 0);
-        assertPass(program, mcastv4packet.array(), 0);
-        assertPass(program, mcastv6packet.array(), 0);
+        assertPass(program, mcastv4packet.array());
+        assertPass(program, mcastv6packet.array());
+        assertPass(program, bcastv4packet1.array());
+        assertPass(program, bcastv4packet2.array());
+        assertPass(program, bcastv4unicastl2packet.array());
 
         // Verify it can be initialized to on
         ipManagerCallback.resetApfProgramWait();
         apfFilter.shutdown();
-        apfFilter = new TestApfFilter(ipManagerCallback, true /* multicastFilter */);
+        apfFilter = new TestApfFilter(ipManagerCallback, DROP_MULTICAST, mLog);
+        apfFilter.setLinkProperties(lp);
         program = ipManagerCallback.getApfProgram();
-        assertDrop(program, bcastv4packet.array(), 0);
-        assertDrop(program, mcastv4packet.array(), 0);
-        assertDrop(program, mcastv6packet.array(), 0);
+        assertDrop(program, mcastv4packet.array());
+        assertDrop(program, mcastv6packet.array());
+        assertDrop(program, bcastv4packet1.array());
+        assertDrop(program, bcastv4unicastl2packet.array());
 
         // Verify that ICMPv6 multicast is not dropped.
         mcastv6packet.put(IPV6_NEXT_HEADER_OFFSET, (byte)IPPROTO_ICMPV6);
-        assertPass(program, mcastv6packet.array(), 0);
+        assertPass(program, mcastv6packet.array());
 
         apfFilter.shutdown();
     }
 
-    private void verifyArpFilter(MockIpManagerCallback ipManagerCallback, ApfFilter apfFilter,
-            LinkProperties linkProperties, int filterResult) {
-        ipManagerCallback.resetApfProgramWait();
-        apfFilter.setLinkProperties(linkProperties);
-        byte[] program = ipManagerCallback.getApfProgram();
-        ByteBuffer packet = ByteBuffer.wrap(new byte[100]);
-        packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_ARP);
-        assertPass(program, packet.array(), 0);
-        packet.position(ARP_HEADER_OFFSET);
-        packet.put(ARP_IPV4_REQUEST_HEADER);
-        assertVerdict(filterResult, program, packet.array(), 0);
-        packet.position(ARP_TARGET_IP_ADDRESS_OFFSET);
-        packet.put(MOCK_IPV4_ADDR);
-        assertPass(program, packet.array(), 0);
+    private byte[] getProgram(MockIpManagerCallback cb, ApfFilter filter, LinkProperties lp) {
+        cb.resetApfProgramWait();
+        filter.setLinkProperties(lp);
+        return cb.getApfProgram();
+    }
+
+    private void verifyArpFilter(byte[] program, int filterResult) {
+        // Verify ARP request packet
+        assertPass(program, arpRequestBroadcast(MOCK_IPV4_ADDR));
+        assertVerdict(filterResult, program, arpRequestBroadcast(ANOTHER_IPV4_ADDR));
+        assertDrop(program, arpRequestBroadcast(IPV4_ANY_HOST_ADDR));
+
+        // Verify unicast ARP reply packet is always accepted.
+        assertPass(program, arpReplyUnicast(MOCK_IPV4_ADDR));
+        assertPass(program, arpReplyUnicast(ANOTHER_IPV4_ADDR));
+        assertPass(program, arpReplyUnicast(IPV4_ANY_HOST_ADDR));
+
+        // Verify GARP reply packets are always filtered
+        assertDrop(program, garpReply());
     }
 
     @LargeTest
     public void testApfFilterArp() throws Exception {
         MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
-        ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, false /* multicastFilter */);
-        byte[] program = ipManagerCallback.getApfProgram();
+        ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, ALLOW_MULTICAST, mLog);
 
-        // Verify initially ARP filter is off
-        ByteBuffer packet = ByteBuffer.wrap(new byte[100]);
-        packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_ARP);
-        assertPass(program, packet.array(), 0);
-        packet.position(ARP_HEADER_OFFSET);
-        packet.put(ARP_IPV4_REQUEST_HEADER);
-        assertPass(program, packet.array(), 0);
-        packet.position(ARP_TARGET_IP_ADDRESS_OFFSET);
-        packet.put(MOCK_IPV4_ADDR);
-        assertPass(program, packet.array(), 0);
+        // Verify initially ARP request filter is off, and GARP filter is on.
+        verifyArpFilter(ipManagerCallback.getApfProgram(), PASS);
 
         // Inform ApfFilter of our address and verify ARP filtering is on
+        LinkAddress linkAddress = new LinkAddress(InetAddress.getByAddress(MOCK_IPV4_ADDR), 24);
         LinkProperties lp = new LinkProperties();
-        assertTrue(lp.addLinkAddress(
-                new LinkAddress(InetAddress.getByAddress(MOCK_IPV4_ADDR), 24)));
-        verifyArpFilter(ipManagerCallback, apfFilter, lp, DROP);
+        assertTrue(lp.addLinkAddress(linkAddress));
+        verifyArpFilter(getProgram(ipManagerCallback, apfFilter, lp), DROP);
 
         // Inform ApfFilter of loss of IP and verify ARP filtering is off
-        verifyArpFilter(ipManagerCallback, apfFilter, new LinkProperties(), PASS);
+        verifyArpFilter(getProgram(ipManagerCallback, apfFilter, new LinkProperties()), PASS);
 
         apfFilter.shutdown();
     }
 
+    private static byte[] arpRequestBroadcast(byte[] tip) {
+        ByteBuffer packet = ByteBuffer.wrap(new byte[100]);
+        packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_ARP);
+        put(packet, ETH_DEST_ADDR_OFFSET, ETH_BROADCAST_MAC_ADDRESS);
+        put(packet, ARP_HEADER_OFFSET, ARP_IPV4_REPLY_HEADER);
+        put(packet, ARP_TARGET_IP_ADDRESS_OFFSET, tip);
+        return packet.array();
+    }
+
+    private static byte[] arpReplyUnicast(byte[] tip) {
+        ByteBuffer packet = ByteBuffer.wrap(new byte[100]);
+        packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_ARP);
+        put(packet, ARP_HEADER_OFFSET, ARP_IPV4_REPLY_HEADER);
+        put(packet, ARP_TARGET_IP_ADDRESS_OFFSET, tip);
+        return packet.array();
+    }
+
+    private static byte[] garpReply() {
+        ByteBuffer packet = ByteBuffer.wrap(new byte[100]);
+        packet.putShort(ETH_ETHERTYPE_OFFSET, (short)ETH_P_ARP);
+        put(packet, ETH_DEST_ADDR_OFFSET, ETH_BROADCAST_MAC_ADDRESS);
+        put(packet, ARP_HEADER_OFFSET, ARP_IPV4_REPLY_HEADER);
+        put(packet, ARP_TARGET_IP_ADDRESS_OFFSET, IPV4_ANY_HOST_ADDR);
+        return packet.array();
+    }
+
     // Verify that the last program pushed to the IpManager.Callback properly filters the
     // given packet for the given lifetime.
     private void verifyRaLifetime(MockIpManagerCallback ipManagerCallback, ByteBuffer packet,
@@ -837,22 +963,22 @@
         byte[] program = ipManagerCallback.getApfProgram();
 
         // Verify new program should drop RA for 1/6th its lifetime
-        assertDrop(program, packet.array(), 0);
+        assertDrop(program, packet.array());
         assertDrop(program, packet.array(), lifetime/6);
         assertPass(program, packet.array(), lifetime/6 + 1);
         assertPass(program, packet.array(), lifetime);
 
         // Verify RA checksum is ignored
         packet.putShort(ICMP6_RA_CHECKSUM_OFFSET, (short)12345);
-        assertDrop(program, packet.array(), 0);
+        assertDrop(program, packet.array());
         packet.putShort(ICMP6_RA_CHECKSUM_OFFSET, (short)-12345);
-        assertDrop(program, packet.array(), 0);
+        assertDrop(program, packet.array());
 
         // Verify other changes to RA make it not match filter
         packet.put(0, (byte)-1);
-        assertPass(program, packet.array(), 0);
+        assertPass(program, packet.array());
         packet.put(0, (byte)0);
-        assertDrop(program, packet.array(), 0);
+        assertDrop(program, packet.array());
     }
 
     // Test that when ApfFilter is shown the given packet, it generates a program to filter it
@@ -867,6 +993,35 @@
         verifyRaLifetime(ipManagerCallback, packet, lifetime);
     }
 
+    private void verifyRaEvent(RaEvent expected) {
+        ArgumentCaptor<Parcelable> captor = ArgumentCaptor.forClass(Parcelable.class);
+        verify(mLog, atLeastOnce()).log(captor.capture());
+        RaEvent got = lastRaEvent(captor.getAllValues());
+        if (!raEventEquals(expected, got)) {
+            assertEquals(expected, got);  // fail for printing an assertion error message.
+        }
+    }
+
+    private RaEvent lastRaEvent(List<Parcelable> events) {
+        RaEvent got = null;
+        for (Parcelable ev : events) {
+            if (ev instanceof RaEvent) {
+                got = (RaEvent) ev;
+            }
+        }
+        return got;
+    }
+
+    private boolean raEventEquals(RaEvent ev1, RaEvent ev2) {
+        return (ev1 != null) && (ev2 != null)
+                && (ev1.routerLifetime == ev2.routerLifetime)
+                && (ev1.prefixValidLifetime == ev2.prefixValidLifetime)
+                && (ev1.prefixPreferredLifetime == ev2.prefixPreferredLifetime)
+                && (ev1.routeInfoLifetime == ev2.routeInfoLifetime)
+                && (ev1.rdnssLifetime == ev2.rdnssLifetime)
+                && (ev1.dnsslLifetime == ev2.dnsslLifetime);
+    }
+
     private void assertInvalidRa(TestApfFilter apfFilter, MockIpManagerCallback ipManagerCallback,
             ByteBuffer packet) throws IOException, ErrnoException {
         ipManagerCallback.resetApfProgramWait();
@@ -877,7 +1032,7 @@
     @LargeTest
     public void testApfFilterRa() throws Exception {
         MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
-        TestApfFilter apfFilter = new TestApfFilter(ipManagerCallback, true /* multicastFilter */);
+        TestApfFilter apfFilter = new TestApfFilter(ipManagerCallback, DROP_MULTICAST, mLog);
         byte[] program = ipManagerCallback.getApfProgram();
 
         // Verify RA is passed the first time
@@ -888,9 +1043,10 @@
         basePacket.putShort(ICMP6_RA_ROUTER_LIFETIME_OFFSET, (short)1000);
         basePacket.position(IPV6_DEST_ADDR_OFFSET);
         basePacket.put(IPV6_ALL_NODES_ADDRESS);
-        assertPass(program, basePacket.array(), 0);
+        assertPass(program, basePacket.array());
 
         testRaLifetime(apfFilter, ipManagerCallback, basePacket, 1000);
+        verifyRaEvent(new RaEvent(1000, -1, -1, -1, -1, -1));
 
         // Ensure zero-length options cause the packet to be silently skipped.
         // Do this before we test other packets. http://b/29586253
@@ -916,6 +1072,7 @@
         prefixOptionPacket.putInt(
                 ICMP6_RA_OPTION_OFFSET + ICMP6_PREFIX_OPTION_VALID_LIFETIME_OFFSET, 200);
         testRaLifetime(apfFilter, ipManagerCallback, prefixOptionPacket, 100);
+        verifyRaEvent(new RaEvent(1000, 200, 100, -1, -1, -1));
 
         ByteBuffer rdnssOptionPacket = ByteBuffer.wrap(
                 new byte[ICMP6_RA_OPTION_OFFSET + ICMP6_4_BYTE_OPTION_LEN]);
@@ -926,6 +1083,7 @@
         rdnssOptionPacket.putInt(
                 ICMP6_RA_OPTION_OFFSET + ICMP6_4_BYTE_LIFETIME_OFFSET, 300);
         testRaLifetime(apfFilter, ipManagerCallback, rdnssOptionPacket, 300);
+        verifyRaEvent(new RaEvent(1000, -1, -1, -1, 300, -1));
 
         ByteBuffer routeInfoOptionPacket = ByteBuffer.wrap(
                 new byte[ICMP6_RA_OPTION_OFFSET + ICMP6_4_BYTE_OPTION_LEN]);
@@ -936,6 +1094,7 @@
         routeInfoOptionPacket.putInt(
                 ICMP6_RA_OPTION_OFFSET + ICMP6_4_BYTE_LIFETIME_OFFSET, 400);
         testRaLifetime(apfFilter, ipManagerCallback, routeInfoOptionPacket, 400);
+        verifyRaEvent(new RaEvent(1000, -1, -1, 400, -1, -1));
 
         ByteBuffer dnsslOptionPacket = ByteBuffer.wrap(
                 new byte[ICMP6_RA_OPTION_OFFSET + ICMP6_4_BYTE_OPTION_LEN]);
@@ -948,6 +1107,7 @@
         // Note that lifetime of 2000 will be ignored in favor of shorter
         // route lifetime of 1000.
         testRaLifetime(apfFilter, ipManagerCallback, dnsslOptionPacket, 1000);
+        verifyRaEvent(new RaEvent(1000, -1, -1, -1, -1, 2000));
 
         // Verify that current program filters all five RAs:
         verifyRaLifetime(ipManagerCallback, basePacket, 1000);
@@ -979,6 +1139,13 @@
         return file.getAbsolutePath();
     }
 
+    private static void put(ByteBuffer buffer, int position, byte[] bytes) {
+        final int original = buffer.position();
+        buffer.position(position);
+        buffer.put(bytes);
+        buffer.position(original);
+    }
+
     /**
      * Call the APF interpreter the run {@code program} on {@code packet} pretending the
      * filter was installed {@code filter_age} seconds ago.
@@ -999,4 +1166,30 @@
      */
     private native static boolean compareBpfApf(String filter, String pcap_filename,
             byte[] apf_program);
+
+    public void testBytesToInt() {
+        assertEquals(0x00000000, ApfFilter.bytesToInt(IPV4_ANY_HOST_ADDR));
+        assertEquals(0xffffffff, ApfFilter.bytesToInt(IPV4_BROADCAST_ADDRESS));
+        assertEquals(0x0a000001, ApfFilter.bytesToInt(MOCK_IPV4_ADDR));
+        assertEquals(0x0a000002, ApfFilter.bytesToInt(ANOTHER_IPV4_ADDR));
+        assertEquals(0x0a001fff, ApfFilter.bytesToInt(MOCK_BROADCAST_IPV4_ADDR));
+        assertEquals(0xe0000001, ApfFilter.bytesToInt(MOCK_MULTICAST_IPV4_ADDR));
+    }
+
+    public void testBroadcastAddress() throws Exception {
+        assertEqualsIp("255.255.255.255", ApfFilter.ipv4BroadcastAddress(IPV4_ANY_HOST_ADDR, 0));
+        assertEqualsIp("0.0.0.0", ApfFilter.ipv4BroadcastAddress(IPV4_ANY_HOST_ADDR, 32));
+        assertEqualsIp("0.0.3.255", ApfFilter.ipv4BroadcastAddress(IPV4_ANY_HOST_ADDR, 22));
+        assertEqualsIp("0.255.255.255", ApfFilter.ipv4BroadcastAddress(IPV4_ANY_HOST_ADDR, 8));
+
+        assertEqualsIp("255.255.255.255", ApfFilter.ipv4BroadcastAddress(MOCK_IPV4_ADDR, 0));
+        assertEqualsIp("10.0.0.1", ApfFilter.ipv4BroadcastAddress(MOCK_IPV4_ADDR, 32));
+        assertEqualsIp("10.0.0.255", ApfFilter.ipv4BroadcastAddress(MOCK_IPV4_ADDR, 24));
+        assertEqualsIp("10.0.255.255", ApfFilter.ipv4BroadcastAddress(MOCK_IPV4_ADDR, 16));
+    }
+
+    public void assertEqualsIp(String expected, int got) throws Exception {
+        int want = ApfFilter.bytesToInt(InetAddress.getByName(expected).getAddress());
+        assertEquals(want, got);
+    }
 }
diff --git a/services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java b/services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java
index f8eaf7d..bc8baa1 100644
--- a/services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java
+++ b/services/tests/servicestests/src/android/net/dhcp/DhcpPacketTest.java
@@ -16,24 +16,22 @@
 
 package android.net.dhcp;
 
-import android.net.NetworkUtils;
 import android.net.DhcpResults;
 import android.net.LinkAddress;
+import android.net.NetworkUtils;
+import android.net.metrics.DhcpErrorEvent;
 import android.system.OsConstants;
 import android.test.suitebuilder.annotation.SmallTest;
 import com.android.internal.util.HexDump;
-
 import java.net.Inet4Address;
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
-
-import junit.framework.TestCase;
-import libcore.util.HexEncoding;
 import java.util.Arrays;
+import java.util.Random;
+import junit.framework.TestCase;
 
 import static android.net.dhcp.DhcpPacket.*;
 
-
 public class DhcpPacketTest extends TestCase {
 
     private static Inet4Address SERVER_ADDR = v4Address("192.0.2.1");
@@ -285,7 +283,7 @@
         // TODO: Turn all of these into golden files. This will probably require modifying
         // Android.mk appropriately, making this into an AndroidTestCase, and adding code to read
         // the golden files from the test APK's assets via mContext.getAssets().
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // IP header.
             "451001480000000080118849c0a89003c0a89ff7" +
             // UDP header.
@@ -304,8 +302,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options
             "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" +
-            "3a0400000e103b040000189cff00000000000000000000"
-        ).toCharArray(), false));
+            "3a0400000e103b040000189cff00000000000000000000"));
 
         DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
         assertTrue(offerPacket instanceof DhcpOfferPacket);  // Implicitly checks it's non-null.
@@ -316,7 +313,7 @@
 
     @SmallTest
     public void testOffer2() throws Exception {
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // IP header.
             "450001518d0600004011144dc0a82b01c0a82bf7" +
             // UDP header.
@@ -335,8 +332,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options
             "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
-            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"
-        ).toCharArray(), false));
+            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"));
 
         assertEquals(337, packet.limit());
         DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
@@ -347,6 +343,185 @@
         assertTrue(dhcpResults.hasMeteredHint());
     }
 
+    @SmallTest
+    public void testBadIpPacket() throws Exception {
+        final byte[] packet = HexDump.hexStringToByteArray(
+            // IP header.
+            "450001518d0600004011144dc0a82b01c0a82bf7");
+
+        try {
+            DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, packet.length, ENCAP_L3);
+        } catch (DhcpPacket.ParseException expected) {
+            assertDhcpErrorCodes(DhcpErrorEvent.L3_TOO_SHORT, expected.errorCode);
+            return;
+        }
+        fail("Dhcp packet parsing should have failed");
+    }
+
+    @SmallTest
+    public void testBadDhcpPacket() throws Exception {
+        final byte[] packet = HexDump.hexStringToByteArray(
+            // IP header.
+            "450001518d0600004011144dc0a82b01c0a82bf7" +
+            // UDP header.
+            "00430044013d9ac7" +
+            // BOOTP header.
+            "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000");
+
+        try {
+            DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, packet.length, ENCAP_L3);
+        } catch (DhcpPacket.ParseException expected) {
+            assertDhcpErrorCodes(DhcpErrorEvent.L3_TOO_SHORT, expected.errorCode);
+            return;
+        }
+        fail("Dhcp packet parsing should have failed");
+    }
+
+    @SmallTest
+    public void testBadTruncatedOffer() throws Exception {
+        final byte[] packet = HexDump.hexStringToByteArray(
+            // IP header.
+            "450001518d0600004011144dc0a82b01c0a82bf7" +
+            // UDP header.
+            "00430044013d9ac7" +
+            // BOOTP header.
+            "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
+            // MAC address.
+            "30766ff2a90c00000000000000000000" +
+            // Server name.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            // File, missing one byte
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "00000000000000000000000000000000000000000000000000000000000000");
+
+        try {
+            DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, packet.length, ENCAP_L3);
+        } catch (DhcpPacket.ParseException expected) {
+            assertDhcpErrorCodes(DhcpErrorEvent.L3_TOO_SHORT, expected.errorCode);
+            return;
+        }
+        fail("Dhcp packet parsing should have failed");
+    }
+
+    @SmallTest
+    public void testBadOfferWithoutACookie() throws Exception {
+        final byte[] packet = HexDump.hexStringToByteArray(
+            // IP header.
+            "450001518d0600004011144dc0a82b01c0a82bf7" +
+            // UDP header.
+            "00430044013d9ac7" +
+            // BOOTP header.
+            "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
+            // MAC address.
+            "30766ff2a90c00000000000000000000" +
+            // Server name.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            // File.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000"
+            // No options
+            );
+
+        try {
+            DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, packet.length, ENCAP_L3);
+        } catch (DhcpPacket.ParseException expected) {
+            assertDhcpErrorCodes(DhcpErrorEvent.DHCP_NO_COOKIE, expected.errorCode);
+            return;
+        }
+        fail("Dhcp packet parsing should have failed");
+    }
+
+    @SmallTest
+    public void testOfferWithBadCookie() throws Exception {
+        final byte[] packet = HexDump.hexStringToByteArray(
+            // IP header.
+            "450001518d0600004011144dc0a82b01c0a82bf7" +
+            // UDP header.
+            "00430044013d9ac7" +
+            // BOOTP header.
+            "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
+            // MAC address.
+            "30766ff2a90c00000000000000000000" +
+            // Server name.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            // File.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            // Bad cookie
+            "DEADBEEF3501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
+            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff");
+
+        try {
+            DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, packet.length, ENCAP_L3);
+        } catch (DhcpPacket.ParseException expected) {
+            assertDhcpErrorCodes(DhcpErrorEvent.DHCP_BAD_MAGIC_COOKIE, expected.errorCode);
+            return;
+        }
+        fail("Dhcp packet parsing should have failed");
+    }
+
+    private void assertDhcpErrorCodes(int expected, int got) {
+        assertEquals(Integer.toHexString(expected), Integer.toHexString(got));
+    }
+
+    public void testTruncatedOfferPackets() throws Exception {
+        final byte[] packet = HexDump.hexStringToByteArray(
+            // IP header.
+            "450001518d0600004011144dc0a82b01c0a82bf7" +
+            // UDP header.
+            "00430044013d9ac7" +
+            // BOOTP header.
+            "02010600dfc23d1f0002000000000000c0a82bf7c0a82b0100000000" +
+            // MAC address.
+            "30766ff2a90c00000000000000000000" +
+            // Server name.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            // File.
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            "0000000000000000000000000000000000000000000000000000000000000000" +
+            // Options
+            "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
+            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff");
+
+        for (int len = 0; len < packet.length; len++) {
+            try {
+                DhcpPacket.decodeFullPacket(packet, len, ENCAP_L3);
+            } catch (ParseException e) {
+                if (e.errorCode == DhcpErrorEvent.PARSING_ERROR) {
+                    fail(String.format("bad truncated packet of length %d", len));
+                }
+            }
+        }
+    }
+
+    public void testRandomPackets() throws Exception {
+        final int maxRandomPacketSize = 512;
+        final Random r = new Random();
+        for (int i = 0; i < 10000; i++) {
+            byte[] packet = new byte[r.nextInt(maxRandomPacketSize + 1)];
+            r.nextBytes(packet);
+            try {
+                DhcpPacket.decodeFullPacket(packet, packet.length, ENCAP_L3);
+            } catch (ParseException e) {
+                if (e.errorCode == DhcpErrorEvent.PARSING_ERROR) {
+                    fail("bad packet: " + HexDump.toHexString(packet));
+                }
+            }
+        }
+    }
+
     private byte[] mtuBytes(int mtu) {
         // 0x1a02: option 26, length 2. 0xff: no more options.
         if (mtu > Short.MAX_VALUE - Short.MIN_VALUE) {
@@ -354,7 +529,7 @@
                 String.format("Invalid MTU %d, must be 16-bit unsigned", mtu));
         }
         String hexString = String.format("1a02%04xff", mtu);
-        return HexEncoding.decode(hexString.toCharArray(), false);
+        return HexDump.hexStringToByteArray(hexString);
     }
 
     private void checkMtu(ByteBuffer packet, int expectedMtu, byte[] mtuBytes) throws Exception {
@@ -372,7 +547,7 @@
 
     @SmallTest
     public void testMtu() throws Exception {
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // IP header.
             "451001480000000080118849c0a89003c0a89ff7" +
             // UDP header.
@@ -391,8 +566,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options
             "638253633501023604c0a89003330400001c200104fffff0000304c0a89ffe06080808080808080404" +
-            "3a0400000e103b040000189cff00000000"
-        ).toCharArray(), false));
+            "3a0400000e103b040000189cff00000000"));
 
         checkMtu(packet, 0, null);
         checkMtu(packet, 0, mtuBytes(1501));
@@ -409,7 +583,7 @@
 
     @SmallTest
     public void testBadHwaddrLength() throws Exception {
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // IP header.
             "450001518d0600004011144dc0a82b01c0a82bf7" +
             // UDP header.
@@ -428,8 +602,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options
             "638253633501023604c0a82b01330400000e103a04000007083b0400000c4e0104ffffff00" +
-            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"
-        ).toCharArray(), false));
+            "1c04c0a82bff0304c0a82b010604c0a82b012b0f414e44524f49445f4d455445524544ff"));
         String expectedClientMac = "30766FF2A90C";
 
         final int hwAddrLenOffset = 20 + 8 + 2;
@@ -486,7 +659,7 @@
         //    store any information in the overloaded fields).
         //
         // For now, we just check that it parses correctly.
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // Ethernet header.
             "b4cef6000000e80462236e300800" +
             // IP header.
@@ -507,8 +680,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options
             "638253633501023604010101010104ffff000033040000a8c03401030304ac1101010604ac110101" +
-            "0000000000000000000000000000000000000000000000ff000000"
-        ).toCharArray(), false));
+            "0000000000000000000000000000000000000000000000ff000000"));
 
         DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
         assertTrue(offerPacket instanceof DhcpOfferPacket);
@@ -519,7 +691,7 @@
 
     @SmallTest
     public void testBug2111() throws Exception {
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // IP header.
             "4500014c00000000ff119beac3eaf3880a3f5d04" +
             // UDP header. TODO: fix invalid checksum (due to MAC address obfuscation).
@@ -538,8 +710,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options.
             "638253633501023604c00002fe33040000bfc60104fffff00003040a3f50010608c0000201c0000202" +
-            "0f0f646f6d61696e3132332e636f2e756b0000000000ff00000000"
-        ).toCharArray(), false));
+            "0f0f646f6d61696e3132332e636f2e756b0000000000ff00000000"));
 
         DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L3);
         assertTrue(offerPacket instanceof DhcpOfferPacket);
@@ -550,7 +721,7 @@
 
     @SmallTest
     public void testBug2136() throws Exception {
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // Ethernet header.
             "bcf5ac000000d0c7890000000800" +
             // IP header.
@@ -571,8 +742,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options.
             "6382536335010236040a20ff80330400001c200104fffff00003040a20900106089458413494584135" +
-            "0f0b6c616e63732e61632e756b000000000000000000ff00000000"
-        ).toCharArray(), false));
+            "0f0b6c616e63732e61632e756b000000000000000000ff00000000"));
 
         DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
         assertTrue(offerPacket instanceof DhcpOfferPacket);
@@ -584,7 +754,7 @@
 
     @SmallTest
     public void testUdpServerAnySourcePort() throws Exception {
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // Ethernet header.
             "9cd917000000001c2e0000000800" +
             // IP header.
@@ -606,8 +776,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options.
             "6382536335010236040a0169fc3304000151800104ffff000003040a0fc817060cd1818003d1819403" +
-            "d18180060f0777766d2e6564751c040a0fffffff000000"
-        ).toCharArray(), false));
+            "d18180060f0777766d2e6564751c040a0fffffff000000"));
 
         DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
         assertTrue(offerPacket instanceof DhcpOfferPacket);
@@ -620,7 +789,7 @@
 
     @SmallTest
     public void testUdpInvalidDstPort() throws Exception {
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // Ethernet header.
             "9cd917000000001c2e0000000800" +
             // IP header.
@@ -642,8 +811,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options.
             "6382536335010236040a0169fc3304000151800104ffff000003040a0fc817060cd1818003d1819403" +
-            "d18180060f0777766d2e6564751c040a0fffffff000000"
-        ).toCharArray(), false));
+            "d18180060f0777766d2e6564751c040a0fffffff000000"));
 
         try {
             DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
@@ -653,7 +821,7 @@
 
     @SmallTest
     public void testMultipleRouters() throws Exception {
-        final ByteBuffer packet = ByteBuffer.wrap(HexEncoding.decode((
+        final ByteBuffer packet = ByteBuffer.wrap(HexDump.hexStringToByteArray(
             // Ethernet header.
             "fc3d93000000" + "081735000000" + "0800" +
             // IP header.
@@ -674,8 +842,7 @@
             "0000000000000000000000000000000000000000000000000000000000000000" +
             // Options.
             "638253633501023604c0abbd023304000070803a04000038403b04000062700104ffffff00" +
-            "0308c0a8bd01ffffff0006080808080808080404ff000000000000"
-        ).toCharArray(), false));
+            "0308c0a8bd01ffffff0006080808080808080404ff000000000000"));
 
         DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_L2);
         assertTrue(offerPacket instanceof DhcpOfferPacket);
diff --git a/services/tests/servicestests/src/com/android/internal/util/FakeSettingsProvider.java b/services/tests/servicestests/src/com/android/internal/util/FakeSettingsProvider.java
new file mode 100644
index 0000000..808f4dd
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/internal/util/FakeSettingsProvider.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.util;
+
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.Settings;
+import android.test.mock.MockContentProvider;
+import android.util.Log;
+
+import java.util.HashMap;
+
+/**
+ * Fake for system settings.
+ *
+ * To use, ensure that the Context used by the test code returns a ContentResolver that uses this
+ * provider for the Settings authority:
+ *
+ *   class MyTestContext extends MockContext {
+ *       ...
+ *       private final MockContentResolver mContentResolver;
+ *       public MyTestContext(...) {
+ *           ...
+ *           mContentResolver = new MockContentResolver();
+ *           mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
+ *       }
+ *       ...
+ *       @Override
+ *       public ContentResolver getContentResolver() {
+ *           return mContentResolver;
+ *       }
+ *
+ * As long as the code under test is using the test Context, the actual code under test does not
+ * need to be modified, and can access Settings using the normal static methods:
+ *
+ *   Settings.Global.getInt(cr, "my_setting", 0);  // Returns 0.
+ *   Settings.Global.putInt(cr, "my_setting", 5);
+ *   Settings.Global.getInt(cr, "my_setting", 0);  // Returns 5.
+ *
+ * Note that this class cannot be used in the same process as real settings. This is because it
+ * works by passing an alternate ContentResolver to Settings operations. Unfortunately, the Settings
+ * class only fetches the content provider from the passed-in ContentResolver the first time it's
+ * used, and after that stores it in a per-process static.
+ *
+ * TODO: evaluate implementing settings change notifications. This would require:
+ *
+ * 1. Making ContentResolver#registerContentObserver non-final and overriding it in
+ *    MockContentResolver.
+ * 2. Making FakeSettingsProvider take a ContentResolver argument.
+ * 3. Calling ContentResolver#notifyChange(getUriFor(table, arg), ...) on every settings change.
+ */
+public class FakeSettingsProvider extends MockContentProvider {
+
+    private static final String TAG = FakeSettingsProvider.class.getSimpleName();
+    private static final boolean DBG = false;
+    private static final String[] TABLES = { "system", "secure", "global" };
+
+    private final HashMap<String, HashMap<String, String>> mTables = new HashMap<>();
+
+    public FakeSettingsProvider() {
+        for (int i = 0; i < TABLES.length; i++) {
+            mTables.put(TABLES[i], new HashMap<String, String>());
+        }
+    }
+
+    private Uri getUriFor(String table, String key) {
+        switch (table) {
+            case "system":
+                return Settings.System.getUriFor(key);
+            case "secure":
+                return Settings.Secure.getUriFor(key);
+            case "global":
+                return Settings.Global.getUriFor(key);
+            default:
+                throw new UnsupportedOperationException("Unknown settings table " + table);
+        }
+    }
+
+    public Bundle call(String method, String arg, Bundle extras) {
+        // Methods are "GET_system", "GET_global", "PUT_secure", etc.
+        String[] commands = method.split("_", 2);
+        String op = commands[0];
+        String table = commands[1];
+
+        Bundle out = new Bundle();
+        String value;
+        switch (op) {
+            case "GET":
+                value = mTables.get(table).get(arg);
+                if (value != null) {
+                    if (DBG) {
+                        Log.d(TAG, String.format("Returning fake setting %s.%s = %s",
+                                table, arg, value));
+                    }
+                    out.putString(Settings.NameValueTable.VALUE, value);
+                }
+                break;
+            case "PUT":
+                value = extras.getString(Settings.NameValueTable.VALUE, null);
+                if (DBG) {
+                    Log.d(TAG, String.format("Inserting fake setting %s.%s = %s",
+                            table, arg, value));
+                }
+                if (value != null) {
+                    mTables.get(table).put(arg, value);
+                } else {
+                    mTables.get(table).remove(arg);
+                }
+                break;
+            default:
+                throw new UnsupportedOperationException("Unknown command " + method);
+        }
+
+        return out;
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/internal/util/FakeSettingsProviderTest.java b/services/tests/servicestests/src/com/android/internal/util/FakeSettingsProviderTest.java
new file mode 100644
index 0000000..05de0a5
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/internal/util/FakeSettingsProviderTest.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.internal.util;
+
+import android.content.ContentResolver;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.provider.Settings;
+import android.test.AndroidTestCase;
+import android.test.mock.MockContentResolver;
+import android.test.mock.MockContext;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.test.suitebuilder.annotation.Suppress;
+import android.util.Log;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Unit tests for FakeSettingsProvider.
+ */
+public class FakeSettingsProviderTest extends AndroidTestCase {
+
+    private MockContentResolver mCr;
+
+    @Override
+    public void setUp() throws Exception {
+        mCr = new MockContentResolver();
+        mCr.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
+    }
+
+    @SmallTest
+    public void testBasicOperation() throws Exception {
+        String settingName = Settings.System.SCREEN_BRIGHTNESS;
+
+        try {
+            Settings.System.getInt(mCr, settingName);
+            fail("FakeSettingsProvider should start off empty.");
+        } catch (Settings.SettingNotFoundException expected) {}
+
+        // Check that fake settings can be written and read back.
+        Settings.System.putInt(mCr, settingName, 123);
+        assertEquals(123, Settings.System.getInt(mCr, settingName));
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java b/services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java
index 4fae4a7..4af1cf1 100644
--- a/services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/ConnectivityServiceTest.java
@@ -17,6 +17,7 @@
 package com.android.server;
 
 import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
+import static android.net.ConnectivityManager.TYPE_ETHERNET;
 import static android.net.ConnectivityManager.TYPE_MOBILE;
 import static android.net.ConnectivityManager.TYPE_WIFI;
 import static android.net.ConnectivityManager.getNetworkTypeName;
@@ -24,8 +25,10 @@
 
 import static org.mockito.Mockito.mock;
 
+import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.ContextWrapper;
 import android.content.Intent;
@@ -49,6 +52,8 @@
 import android.net.NetworkMisc;
 import android.net.NetworkRequest;
 import android.net.RouteInfo;
+import android.net.metrics.IpConnectivityLog;
+import android.net.util.AvoidBadWifiTracker;
 import android.os.ConditionVariable;
 import android.os.Handler;
 import android.os.HandlerThread;
@@ -61,12 +66,15 @@
 import android.os.MessageQueue.IdleHandler;
 import android.os.Process;
 import android.os.SystemClock;
+import android.provider.Settings;
 import android.test.AndroidTestCase;
+import android.test.mock.MockContentResolver;
 import android.test.suitebuilder.annotation.LargeTest;
 import android.test.suitebuilder.annotation.SmallTest;
 import android.util.Log;
 import android.util.LogPrinter;
 
+import com.android.internal.util.FakeSettingsProvider;
 import com.android.internal.util.WakeupMessage;
 import com.android.server.connectivity.NetworkAgentInfo;
 import com.android.server.connectivity.NetworkMonitor;
@@ -75,6 +83,8 @@
 
 import java.net.InetAddress;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Objects;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
@@ -90,12 +100,14 @@
     private static final String TAG = "ConnectivityServiceTest";
 
     private static final int TIMEOUT_MS = 500;
+    private static final int TEST_LINGER_DELAY_MS = 120;
 
     private BroadcastInterceptingContext mServiceContext;
     private WrappedConnectivityService mService;
     private WrappedConnectivityManager mCm;
     private MockNetworkAgent mWiFiNetworkAgent;
     private MockNetworkAgent mCellNetworkAgent;
+    private MockNetworkAgent mEthernetNetworkAgent;
 
     // This class exists to test bindProcessToNetwork and getBoundNetworkForProcess. These methods
     // do not go through ConnectivityService but talk to netd directly, so they don't automatically
@@ -118,27 +130,25 @@
     }
 
     private class MockContext extends BroadcastInterceptingContext {
+        private final MockContentResolver mContentResolver;
+
         MockContext(Context base) {
             super(base);
+            mContentResolver = new MockContentResolver();
+            mContentResolver.addProvider(Settings.AUTHORITY, new FakeSettingsProvider());
         }
 
         @Override
-        public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
-            // PendingIntents sent by the AlarmManager are not intercepted by
-            // BroadcastInterceptingContext so we must really register the receiver.
-            // This shouldn't effect the real NetworkMonitors as the action contains a random token.
-            if (filter.getAction(0).startsWith("android.net.netmon.lingerExpired")) {
-                return getBaseContext().registerReceiver(receiver, filter);
-            } else {
-                return super.registerReceiver(receiver, filter);
-            }
-        }
-
-        @Override
-        public Object getSystemService (String name) {
-            if (name == Context.CONNECTIVITY_SERVICE) return mCm;
+        public Object getSystemService(String name) {
+            if (Context.CONNECTIVITY_SERVICE.equals(name)) return mCm;
+            if (Context.NOTIFICATION_SERVICE.equals(name)) return mock(NotificationManager.class);
             return super.getSystemService(name);
         }
+
+        @Override
+        public ContentResolver getContentResolver() {
+            return mContentResolver;
+        }
     }
 
     /**
@@ -242,6 +252,9 @@
             mNetworkCapabilities = new NetworkCapabilities();
             mNetworkCapabilities.addTransportType(transport);
             switch (transport) {
+                case TRANSPORT_ETHERNET:
+                    mScore = 70;
+                    break;
                 case TRANSPORT_WIFI:
                     mScore = 60;
                     break;
@@ -303,6 +316,11 @@
             mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities);
         }
 
+        public void removeCapability(int capability) {
+            mNetworkCapabilities.removeCapability(capability);
+            mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities);
+        }
+
         public void setSignalStrength(int signalStrength) {
             mNetworkCapabilities.setSignalStrength(signalStrength);
             mNetworkAgent.sendNetworkCapabilities(mNetworkCapabilities);
@@ -318,7 +336,8 @@
          * @param validated Indicate if network should pretend to be validated.
          */
         public void connect(boolean validated) {
-            assertEquals(mNetworkInfo.getDetailedState(), DetailedState.IDLE);
+            assertEquals("MockNetworkAgents can only be connected once",
+                    mNetworkInfo.getDetailedState(), DetailedState.IDLE);
             assertFalse(mNetworkCapabilities.hasCapability(NET_CAPABILITY_INTERNET));
 
             NetworkCallback callback = null;
@@ -536,6 +555,11 @@
             super(context, handler, cmdName, cmd);
         }
 
+        public FakeWakeupMessage(Context context, Handler handler, String cmdName, int cmd,
+                int arg1, int arg2, Object obj) {
+            super(context, handler, cmdName, cmd, arg1, arg2, obj);
+        }
+
         @Override
         public void schedule(long when) {
             long delayMs = when - SystemClock.elapsedRealtime();
@@ -544,12 +568,13 @@
                 fail("Attempting to send msg more than " + UNREASONABLY_LONG_WAIT +
                         "ms into the future: " + delayMs);
             }
-            mHandler.sendEmptyMessageDelayed(mCmd, delayMs);
+            Message msg = mHandler.obtainMessage(mCmd, mArg1, mArg2, mObj);
+            mHandler.sendMessageDelayed(msg, delayMs);
         }
 
         @Override
         public void cancel() {
-            mHandler.removeMessages(mCmd);
+            mHandler.removeMessages(mCmd, mObj);
         }
 
         @Override
@@ -565,28 +590,39 @@
         public String gen204ProbeRedirectUrl = null;
 
         public WrappedNetworkMonitor(Context context, Handler handler,
-            NetworkAgentInfo networkAgentInfo, NetworkRequest defaultRequest) {
-            super(context, handler, networkAgentInfo, defaultRequest);
+                NetworkAgentInfo networkAgentInfo, NetworkRequest defaultRequest,
+                IpConnectivityLog log) {
+            super(context, handler, networkAgentInfo, defaultRequest, log);
         }
 
         @Override
         protected CaptivePortalProbeResult isCaptivePortal() {
-            return new CaptivePortalProbeResult(gen204ProbeResult, gen204ProbeRedirectUrl);
+            return new CaptivePortalProbeResult(gen204ProbeResult, gen204ProbeRedirectUrl, null);
+        }
+    }
+
+    private class WrappedAvoidBadWifiTracker extends AvoidBadWifiTracker {
+        public boolean configRestrictsAvoidBadWifi;
+
+        public WrappedAvoidBadWifiTracker(Context c, Handler h, Runnable r) {
+            super(c, h, r);
         }
 
         @Override
-        protected WakeupMessage makeWakeupMessage(
-                Context context, Handler handler, String cmdName, int cmd) {
-            return new FakeWakeupMessage(context, handler, cmdName, cmd);
+        public boolean configRestrictsAvoidBadWifi() {
+            return configRestrictsAvoidBadWifi;
         }
     }
 
     private class WrappedConnectivityService extends ConnectivityService {
+        public WrappedAvoidBadWifiTracker wrappedAvoidBadWifiTracker;
         private WrappedNetworkMonitor mLastCreatedNetworkMonitor;
 
         public WrappedConnectivityService(Context context, INetworkManagementService netManager,
-                INetworkStatsService statsService, INetworkPolicyManager policyManager) {
-            super(context, netManager, statsService, policyManager);
+                INetworkStatsService statsService, INetworkPolicyManager policyManager,
+                IpConnectivityLog log) {
+            super(context, netManager, statsService, policyManager, log);
+            mLingerDelayMs = TEST_LINGER_DELAY_MS;
         }
 
         @Override
@@ -624,12 +660,29 @@
         @Override
         public NetworkMonitor createNetworkMonitor(Context context, Handler handler,
                 NetworkAgentInfo nai, NetworkRequest defaultRequest) {
-            final WrappedNetworkMonitor monitor = new WrappedNetworkMonitor(context, handler, nai,
-                    defaultRequest);
+            final WrappedNetworkMonitor monitor = new WrappedNetworkMonitor(
+                    context, handler, nai, defaultRequest, mock(IpConnectivityLog.class));
             mLastCreatedNetworkMonitor = monitor;
             return monitor;
         }
 
+        @Override
+        public AvoidBadWifiTracker createAvoidBadWifiTracker(
+                Context c, Handler h, Runnable r) {
+            final WrappedAvoidBadWifiTracker tracker = new WrappedAvoidBadWifiTracker(c, h, r);
+            return tracker;
+        }
+
+        public WrappedAvoidBadWifiTracker getAvoidBadWifiTracker() {
+            return (WrappedAvoidBadWifiTracker) mAvoidBadWifiTracker;
+        }
+
+        @Override
+        public WakeupMessage makeWakeupMessage(
+                Context context, Handler handler, String cmdName, int cmd, Object obj) {
+            return new FakeWakeupMessage(context, handler, cmdName, cmd, 0, 0, obj);
+        }
+
         public WrappedNetworkMonitor getLastCreatedWrappedNetworkMonitor() {
             return mLastCreatedNetworkMonitor;
         }
@@ -641,7 +694,6 @@
         public void waitForIdle() {
             waitForIdle(TIMEOUT_MS);
         }
-
     }
 
     private interface Criteria {
@@ -675,8 +727,6 @@
     public void setUp() throws Exception {
         super.setUp();
 
-        NetworkMonitor.SetDefaultLingerTime(120);
-
         // InstrumentationTestRunner prepares a looper, but AndroidJUnitRunner does not.
         // http://b/25897652 .
         if (Looper.myLooper() == null) {
@@ -687,21 +737,32 @@
         mService = new WrappedConnectivityService(mServiceContext,
                 mock(INetworkManagementService.class),
                 mock(INetworkStatsService.class),
-                mock(INetworkPolicyManager.class));
+                mock(INetworkPolicyManager.class),
+                mock(IpConnectivityLog.class));
 
         mService.systemReady();
         mCm = new WrappedConnectivityManager(getContext(), mService);
         mCm.bindProcessToNetwork(null);
     }
 
+    public void tearDown() throws Exception {
+        setMobileDataAlwaysOn(false);
+        if (mCellNetworkAgent != null) { mCellNetworkAgent.disconnect(); }
+        if (mWiFiNetworkAgent != null) { mWiFiNetworkAgent.disconnect(); }
+        mCellNetworkAgent = mWiFiNetworkAgent = null;
+        super.tearDown();
+    }
+
     private int transportToLegacyType(int transport) {
         switch (transport) {
+            case TRANSPORT_ETHERNET:
+                return TYPE_ETHERNET;
             case TRANSPORT_WIFI:
                 return TYPE_WIFI;
             case TRANSPORT_CELLULAR:
                 return TYPE_MOBILE;
             default:
-                throw new IllegalStateException("Unknown transport" + transport);
+                throw new IllegalStateException("Unknown transport " + transport);
         }
     }
 
@@ -911,8 +972,6 @@
         mWiFiNetworkAgent.adjustScore(11);
         waitFor(cv);
         verifyActiveNetwork(TRANSPORT_WIFI);
-        mCellNetworkAgent.disconnect();
-        mWiFiNetworkAgent.disconnect();
     }
 
     @LargeTest
@@ -984,8 +1043,6 @@
         assertFalse(mCm.getNetworkCapabilities(mWiFiNetworkAgent.getNetwork()).hasCapability(
                 NET_CAPABILITY_VALIDATED));
         verifyActiveNetwork(TRANSPORT_WIFI);
-        mCellNetworkAgent.disconnect();
-        mWiFiNetworkAgent.disconnect();
     }
 
     @LargeTest
@@ -1012,13 +1069,13 @@
         assertFalse(mCm.getNetworkCapabilities(mCellNetworkAgent.getNetwork()).hasCapability(
                 NET_CAPABILITY_VALIDATED));
         verifyActiveNetwork(TRANSPORT_WIFI);
-        mCellNetworkAgent.disconnect();
-        mWiFiNetworkAgent.disconnect();
     }
 
     enum CallbackState {
         NONE,
         AVAILABLE,
+        NETWORK_CAPABILITIES,
+        LINK_PROPERTIES,
         LOSING,
         LOST
     }
@@ -1029,59 +1086,98 @@
      * received. assertNoCallback may be called at any time.
      */
     private class TestNetworkCallback extends NetworkCallback {
-        private final ConditionVariable mConditionVariable = new ConditionVariable();
-        private CallbackState mLastCallback = CallbackState.NONE;
-        private Network mLastNetwork;
+        // Chosen to be much less than the linger timeout. This ensures that we can distinguish
+        // between a LOST callback that arrives immediately and a LOST callback that arrives after
+        // the linger timeout.
+        private final static int TIMEOUT_MS = 50;
 
+        private class CallbackInfo {
+            public final CallbackState state;
+            public final Network network;
+            public Object arg;
+            public CallbackInfo(CallbackState s, Network n, Object o) {
+                state = s; network = n; arg = o;
+            }
+            public String toString() { return String.format("%s (%s)", state, network); }
+            public boolean equals(Object o) {
+                if (!(o instanceof CallbackInfo)) return false;
+                // Ignore timeMs, since it's unpredictable.
+                CallbackInfo other = (CallbackInfo) o;
+                return state == other.state && Objects.equals(network, other.network);
+            }
+        }
+        private final LinkedBlockingQueue<CallbackInfo> mCallbacks = new LinkedBlockingQueue<>();
+
+        protected void setLastCallback(CallbackState state, Network network, Object o) {
+            mCallbacks.offer(new CallbackInfo(state, network, o));
+        }
+
+        @Override
         public void onAvailable(Network network) {
-            assertEquals(CallbackState.NONE, mLastCallback);
-            mLastCallback = CallbackState.AVAILABLE;
-            mLastNetwork = network;
-            mConditionVariable.open();
+            setLastCallback(CallbackState.AVAILABLE, network, null);
         }
 
+        @Override
         public void onLosing(Network network, int maxMsToLive) {
-            assertEquals(CallbackState.NONE, mLastCallback);
-            mLastCallback = CallbackState.LOSING;
-            mLastNetwork = network;
-            mConditionVariable.open();
+            setLastCallback(CallbackState.LOSING, network, maxMsToLive /* autoboxed int */);
         }
 
+        @Override
         public void onLost(Network network) {
-            assertEquals(CallbackState.NONE, mLastCallback);
-            mLastCallback = CallbackState.LOST;
-            mLastNetwork = network;
-            mConditionVariable.open();
+            setLastCallback(CallbackState.LOST, network, null);
         }
 
-        void expectCallback(CallbackState state) {
-            expectCallback(state, null);
+        void expectCallback(CallbackState state, MockNetworkAgent mockAgent, int timeoutMs) {
+            CallbackInfo expected = new CallbackInfo(
+                    state, (mockAgent != null) ? mockAgent.getNetwork() : null, 0);
+            CallbackInfo actual;
+            try {
+                actual = mCallbacks.poll(timeoutMs, TimeUnit.MILLISECONDS);
+                assertEquals("Unexpected callback:", expected, actual);
+            } catch (InterruptedException e) {
+                fail("Did not receive expected " + expected + " after " + TIMEOUT_MS + "ms");
+                actual = null;  // Or the compiler can't tell it's never used uninitialized.
+            }
+            if (state == CallbackState.LOSING) {
+                String msg = String.format(
+                        "Invalid linger time value %d, must be between %d and %d",
+                        actual.arg, 0, TEST_LINGER_DELAY_MS);
+                int maxMsToLive = (Integer) actual.arg;
+                assertTrue(msg, 0 <= maxMsToLive && maxMsToLive <= TEST_LINGER_DELAY_MS);
+            }
         }
 
         void expectCallback(CallbackState state, MockNetworkAgent mockAgent) {
-            waitFor(mConditionVariable);
-            assertEquals(state, mLastCallback);
-            if (mockAgent != null) {
-                assertEquals(mockAgent.getNetwork(), mLastNetwork);
-            }
-            mLastCallback = CallbackState.NONE;
-            mLastNetwork = null;
-            mConditionVariable.close();
+            expectCallback(state, mockAgent, TIMEOUT_MS);
         }
 
         void assertNoCallback() {
-            assertEquals(CallbackState.NONE, mLastCallback);
+            mService.waitForIdle();
+            CallbackInfo c = mCallbacks.peek();
+            assertNull("Unexpected callback: " + c, c);
+        }
+    }
+
+    // Can't be part of TestNetworkCallback because "cannot be declared static; static methods can
+    // only be declared in a static or top level type".
+    static void assertNoCallbacks(TestNetworkCallback ... callbacks) {
+        for (TestNetworkCallback c : callbacks) {
+            c.assertNoCallback();
         }
     }
 
     @LargeTest
     public void testStateChangeNetworkCallbacks() throws Exception {
+        final TestNetworkCallback genericNetworkCallback = new TestNetworkCallback();
         final TestNetworkCallback wifiNetworkCallback = new TestNetworkCallback();
         final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
+        final NetworkRequest genericRequest = new NetworkRequest.Builder()
+                .clearCapabilities().build();
         final NetworkRequest wifiRequest = new NetworkRequest.Builder()
                 .addTransportType(TRANSPORT_WIFI).build();
         final NetworkRequest cellRequest = new NetworkRequest.Builder()
                 .addTransportType(TRANSPORT_CELLULAR).build();
+        mCm.registerNetworkCallback(genericRequest, genericNetworkCallback);
         mCm.registerNetworkCallback(wifiRequest, wifiNetworkCallback);
         mCm.registerNetworkCallback(cellRequest, cellNetworkCallback);
 
@@ -1089,65 +1185,275 @@
         ConditionVariable cv = waitForConnectivityBroadcasts(1);
         mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
         mCellNetworkAgent.connect(false);
-        cellNetworkCallback.expectCallback(CallbackState.AVAILABLE);
-        wifiNetworkCallback.assertNoCallback();
+        genericNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
         assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
         waitFor(cv);
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
 
         // This should not trigger spurious onAvailable() callbacks, b/21762680.
         mCellNetworkAgent.adjustScore(-1);
         mService.waitForIdle();
-        wifiNetworkCallback.assertNoCallback();
-        cellNetworkCallback.assertNoCallback();
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
         assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
 
         cv = waitForConnectivityBroadcasts(2);
         mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
         mWiFiNetworkAgent.connect(false);
-        wifiNetworkCallback.expectCallback(CallbackState.AVAILABLE);
-        cellNetworkCallback.assertNoCallback();
+        genericNetworkCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        wifiNetworkCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
         assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
         waitFor(cv);
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
 
         cv = waitForConnectivityBroadcasts(2);
         mWiFiNetworkAgent.disconnect();
-        wifiNetworkCallback.expectCallback(CallbackState.LOST);
+        genericNetworkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        wifiNetworkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
         cellNetworkCallback.assertNoCallback();
         waitFor(cv);
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
 
         cv = waitForConnectivityBroadcasts(1);
         mCellNetworkAgent.disconnect();
-        cellNetworkCallback.expectCallback(CallbackState.LOST);
-        wifiNetworkCallback.assertNoCallback();
+        genericNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+        cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
         waitFor(cv);
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
 
         // Test validated networks
         mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
         mCellNetworkAgent.connect(true);
-        cellNetworkCallback.expectCallback(CallbackState.AVAILABLE);
-        wifiNetworkCallback.assertNoCallback();
+        genericNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
         assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
 
         // This should not trigger spurious onAvailable() callbacks, b/21762680.
         mCellNetworkAgent.adjustScore(-1);
         mService.waitForIdle();
-        wifiNetworkCallback.assertNoCallback();
-        cellNetworkCallback.assertNoCallback();
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
         assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
 
         mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
         mWiFiNetworkAgent.connect(true);
-        wifiNetworkCallback.expectCallback(CallbackState.AVAILABLE);
-        cellNetworkCallback.expectCallback(CallbackState.LOSING);
+        genericNetworkCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        genericNetworkCallback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+        wifiNetworkCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        cellNetworkCallback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+        assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
+
+        mWiFiNetworkAgent.disconnect();
+        genericNetworkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        wifiNetworkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
+
+        mCellNetworkAgent.disconnect();
+        genericNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+        cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+        assertNoCallbacks(genericNetworkCallback, wifiNetworkCallback, cellNetworkCallback);
+    }
+
+    @SmallTest
+    public void testMultipleLingering() {
+        NetworkRequest request = new NetworkRequest.Builder()
+                .clearCapabilities().addCapability(NET_CAPABILITY_NOT_METERED)
+                .build();
+        TestNetworkCallback callback = new TestNetworkCallback();
+        mCm.registerNetworkCallback(request, callback);
+
+        TestNetworkCallback defaultCallback = new TestNetworkCallback();
+        mCm.registerDefaultNetworkCallback(defaultCallback);
+
+        mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mEthernetNetworkAgent = new MockNetworkAgent(TRANSPORT_ETHERNET);
+
+        mCellNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+        mWiFiNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+        mEthernetNetworkAgent.addCapability(NET_CAPABILITY_NOT_METERED);
+
+        mCellNetworkAgent.connect(true);
+        callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        mWiFiNetworkAgent.connect(true);
+        // We get AVAILABLE on wifi when wifi connects and satisfies our unmetered request.
+        // We then get LOSING when wifi validates and cell is outscored.
+        callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        mEthernetNetworkAgent.connect(true);
+        callback.expectCallback(CallbackState.AVAILABLE, mEthernetNetworkAgent);
+        callback.expectCallback(CallbackState.LOSING, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mEthernetNetworkAgent);
+        assertEquals(mEthernetNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        mEthernetNetworkAgent.disconnect();
+        callback.expectCallback(CallbackState.LOST, mEthernetNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.LOST, mEthernetNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+
+        for (int i = 0; i < 4; i++) {
+            MockNetworkAgent oldNetwork, newNetwork;
+            if (i % 2 == 0) {
+                mWiFiNetworkAgent.adjustScore(-15);
+                oldNetwork = mWiFiNetworkAgent;
+                newNetwork = mCellNetworkAgent;
+            } else {
+                mWiFiNetworkAgent.adjustScore(15);
+                oldNetwork = mCellNetworkAgent;
+                newNetwork = mWiFiNetworkAgent;
+
+            }
+            callback.expectCallback(CallbackState.LOSING, oldNetwork);
+            // TODO: should we send an AVAILABLE callback to newNetwork, to indicate that it is no
+            // longer lingering?
+            defaultCallback.expectCallback(CallbackState.AVAILABLE, newNetwork);
+            assertEquals(newNetwork.getNetwork(), mCm.getActiveNetwork());
+        }
+        assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        // Verify that if a network no longer satisfies a request, we send LOST and not LOSING, even
+        // if the network is still up.
+        mWiFiNetworkAgent.removeCapability(NET_CAPABILITY_NOT_METERED);
+        callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+
+        // Wifi no longer satisfies our listen, which is for an unmetered network.
+        // But because its score is 55, it's still up (and the default network).
+        defaultCallback.assertNoCallback();
+        assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        // Disconnect our test networks.
+        mWiFiNetworkAgent.disconnect();
+        defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        mCellNetworkAgent.disconnect();
+        defaultCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+
+        mCm.unregisterNetworkCallback(callback);
+        mService.waitForIdle();
+
+        // Check that a network is only lingered or torn down if it would not satisfy a request even
+        // if it validated.
+        request = new NetworkRequest.Builder().clearCapabilities().build();
+        callback = new TestNetworkCallback();
+
+        mCm.registerNetworkCallback(request, callback);
+
+        mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
+        mCellNetworkAgent.connect(false);   // Score: 10
+        callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        // Bring up wifi with a score of 20.
+        // Cell stays up because it would satisfy the default request if it validated.
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mWiFiNetworkAgent.connect(false);   // Score: 20
+        callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
         assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
 
         mWiFiNetworkAgent.disconnect();
-        wifiNetworkCallback.expectCallback(CallbackState.LOST);
-        cellNetworkCallback.assertNoCallback();
+        callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
 
+        // Bring up wifi with a score of 70.
+        // Cell is lingered because it would not satisfy any request, even if it validated.
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mWiFiNetworkAgent.adjustScore(50);
+        mWiFiNetworkAgent.connect(false);   // Score: 70
+        callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        // Tear down wifi.
+        mWiFiNetworkAgent.disconnect();
+        callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertEquals(mCellNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        // Bring up wifi, then validate it. Previous versions would immediately tear down cell, but
+        // it's arguably correct to linger it, since it was the default network before it validated.
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mWiFiNetworkAgent.connect(true);
+        callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        assertEquals(mWiFiNetworkAgent.getNetwork(), mCm.getActiveNetwork());
+
+        mWiFiNetworkAgent.disconnect();
+        callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
         mCellNetworkAgent.disconnect();
-        cellNetworkCallback.expectCallback(CallbackState.LOST);
-        wifiNetworkCallback.assertNoCallback();
+        callback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+
+        // If a network is lingering, and we add and remove a request from it, resume lingering.
+        mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
+        mCellNetworkAgent.connect(true);
+        callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mWiFiNetworkAgent.connect(true);
+        callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+
+        NetworkRequest cellRequest = new NetworkRequest.Builder()
+                .addTransportType(TRANSPORT_CELLULAR).build();
+        NetworkCallback noopCallback = new NetworkCallback();
+        mCm.requestNetwork(cellRequest, noopCallback);
+        // TODO: should this cause an AVAILABLE callback, to indicate that the network is no longer
+        // lingering?
+        mCm.unregisterNetworkCallback(noopCallback);
+        callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+
+        // Similar to the above: lingering can start even after the lingered request is removed.
+        // Disconnect wifi and switch to cell.
+        mWiFiNetworkAgent.disconnect();
+        callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+
+        // Cell is now the default network. Pin it with a cell-specific request.
+        noopCallback = new NetworkCallback();  // Can't reuse NetworkCallbacks. http://b/20701525
+        mCm.requestNetwork(cellRequest, noopCallback);
+
+        // Now connect wifi, and expect it to become the default network.
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mWiFiNetworkAgent.connect(true);
+        callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        // The default request is lingering on cell, but nothing happens to cell, and we send no
+        // callbacks for it, because it's kept up by cellRequest.
+        callback.assertNoCallback();
+        // Now unregister cellRequest and expect cell to start lingering.
+        mCm.unregisterNetworkCallback(noopCallback);
+        callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+
+        // Let linger run its course.
+        callback.assertNoCallback();
+        callback.expectCallback(CallbackState.LOST, mCellNetworkAgent,
+                TEST_LINGER_DELAY_MS /* timeoutMs */);
+
+        // Clean up.
+        mWiFiNetworkAgent.disconnect();
+        callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+
+        mCm.unregisterNetworkCallback(callback);
+        mCm.unregisterNetworkCallback(defaultCallback);
     }
 
     private void tryNetworkFactoryRequests(int capability) throws Exception {
@@ -1314,7 +1620,7 @@
         mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
         mCellNetworkAgent.addCapability(NET_CAPABILITY_MMS);
         mCellNetworkAgent.connectWithoutInternet();
-        networkCallback.expectCallback(CallbackState.AVAILABLE);
+        networkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
         verifyActiveNetwork(TRANSPORT_WIFI);
         // Test releasing NetworkRequest disconnects cellular with MMS
         cv = mCellNetworkAgent.getDisconnectedCV();
@@ -1340,7 +1646,7 @@
         MockNetworkAgent mmsNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
         mmsNetworkAgent.addCapability(NET_CAPABILITY_MMS);
         mmsNetworkAgent.connectWithoutInternet();
-        networkCallback.expectCallback(CallbackState.AVAILABLE);
+        networkCallback.expectCallback(CallbackState.AVAILABLE, mmsNetworkAgent);
         verifyActiveNetwork(TRANSPORT_CELLULAR);
         // Test releasing MMS NetworkRequest does not disconnect main cellular NetworkAgent
         cv = mmsNetworkAgent.getDisconnectedCV();
@@ -1366,36 +1672,36 @@
         mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
         String firstRedirectUrl = "http://example.com/firstPath";
         mWiFiNetworkAgent.connectWithCaptivePortal(firstRedirectUrl);
-        captivePortalCallback.expectCallback(CallbackState.AVAILABLE);
+        captivePortalCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
         assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), firstRedirectUrl);
 
         // Take down network.
         // Expect onLost callback.
         mWiFiNetworkAgent.disconnect();
-        captivePortalCallback.expectCallback(CallbackState.LOST);
+        captivePortalCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
 
         // Bring up a network with a captive portal.
         // Expect onAvailable callback of listen for NET_CAPABILITY_CAPTIVE_PORTAL.
         mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
         String secondRedirectUrl = "http://example.com/secondPath";
         mWiFiNetworkAgent.connectWithCaptivePortal(secondRedirectUrl);
-        captivePortalCallback.expectCallback(CallbackState.AVAILABLE);
+        captivePortalCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
         assertEquals(mWiFiNetworkAgent.waitForRedirectUrl(), secondRedirectUrl);
 
         // Make captive portal disappear then revalidate.
         // Expect onLost callback because network no longer provides NET_CAPABILITY_CAPTIVE_PORTAL.
         mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 204;
         mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), true);
-        captivePortalCallback.expectCallback(CallbackState.LOST);
+        captivePortalCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
 
         // Expect NET_CAPABILITY_VALIDATED onAvailable callback.
-        validatedCallback.expectCallback(CallbackState.AVAILABLE);
+        validatedCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
 
         // Break network connectivity.
         // Expect NET_CAPABILITY_VALIDATED onLost callback.
         mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 500;
         mCm.reportNetworkConnectivity(mWiFiNetworkAgent.getNetwork(), false);
-        validatedCallback.expectCallback(CallbackState.LOST);
+        validatedCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
     }
 
     @SmallTest
@@ -1410,7 +1716,7 @@
             // do nothing - should get here
         }
 
-        assertTrue("NetworkReqeuest builder with MATCH_ALL_REQUESTS_NETWORK_SPECIFIER",
+        assertTrue("NetworkRequest builder with MATCH_ALL_REQUESTS_NETWORK_SPECIFIER",
                 execptionCalled);
 
         try {
@@ -1477,6 +1783,444 @@
         defaultNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
     }
 
+    private class TestRequestUpdateCallback extends TestNetworkCallback {
+        @Override
+        public void onCapabilitiesChanged(Network network, NetworkCapabilities netCap) {
+            setLastCallback(CallbackState.NETWORK_CAPABILITIES, network, netCap);
+        }
+
+        @Override
+        public void onLinkPropertiesChanged(Network network, LinkProperties linkProp) {
+            setLastCallback(CallbackState.LINK_PROPERTIES, network, linkProp);
+        }
+    }
+
+    @LargeTest
+    public void testRequestCallbackUpdates() throws Exception {
+        // File a network request for mobile.
+        final TestNetworkCallback cellNetworkCallback = new TestRequestUpdateCallback();
+        final NetworkRequest cellRequest = new NetworkRequest.Builder()
+                .addTransportType(TRANSPORT_CELLULAR).build();
+        mCm.requestNetwork(cellRequest, cellNetworkCallback);
+
+        // Bring up the mobile network.
+        mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
+        mCellNetworkAgent.connect(true);
+
+        // We should get onAvailable().
+        cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        // We should get onCapabilitiesChanged(), when the mobile network successfully validates.
+        cellNetworkCallback.expectCallback(CallbackState.NETWORK_CAPABILITIES, mCellNetworkAgent);
+        cellNetworkCallback.assertNoCallback();
+
+        // Update LinkProperties.
+        final LinkProperties lp = new LinkProperties();
+        lp.setInterfaceName("foonet_data0");
+        mCellNetworkAgent.sendLinkProperties(lp);
+        // We should get onLinkPropertiesChanged().
+        cellNetworkCallback.expectCallback(CallbackState.LINK_PROPERTIES, mCellNetworkAgent);
+        cellNetworkCallback.assertNoCallback();
+
+        // Register a garden variety default network request.
+        final TestNetworkCallback dfltNetworkCallback = new TestRequestUpdateCallback();
+        mCm.registerDefaultNetworkCallback(dfltNetworkCallback);
+        // Only onAvailable() is called; no other information is delivered.
+        dfltNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        dfltNetworkCallback.assertNoCallback();
+
+        // Request a NetworkCapabilities update; only the requesting callback is notified.
+        mCm.requestNetworkCapabilities(dfltNetworkCallback);
+        dfltNetworkCallback.expectCallback(CallbackState.NETWORK_CAPABILITIES, mCellNetworkAgent);
+        cellNetworkCallback.assertNoCallback();
+        dfltNetworkCallback.assertNoCallback();
+
+        // Request a LinkProperties update; only the requesting callback is notified.
+        mCm.requestLinkProperties(dfltNetworkCallback);
+        dfltNetworkCallback.expectCallback(CallbackState.LINK_PROPERTIES, mCellNetworkAgent);
+        cellNetworkCallback.assertNoCallback();
+        dfltNetworkCallback.assertNoCallback();
+
+        mCm.unregisterNetworkCallback(dfltNetworkCallback);
+        mCm.unregisterNetworkCallback(cellNetworkCallback);
+    }
+
+    private void setMobileDataAlwaysOn(boolean enable) {
+        ContentResolver cr = mServiceContext.getContentResolver();
+        Settings.Global.putInt(cr, Settings.Global.MOBILE_DATA_ALWAYS_ON, enable ? 1 : 0);
+        mService.updateMobileDataAlwaysOn();
+        mService.waitForIdle();
+    }
+
+    private boolean isForegroundNetwork(MockNetworkAgent network) {
+        NetworkCapabilities nc = mCm.getNetworkCapabilities(network.getNetwork());
+        assertNotNull(nc);
+        return nc.hasCapability(NET_CAPABILITY_FOREGROUND);
+    }
+
+    @SmallTest
+    public void testBackgroundNetworks() throws Exception {
+        // Create a background request. We can't do this ourselves because ConnectivityService
+        // doesn't have an API for it. So just turn on mobile data always on.
+        setMobileDataAlwaysOn(true);
+        final NetworkRequest request = new NetworkRequest.Builder().build();
+        final NetworkRequest fgRequest = new NetworkRequest.Builder()
+                .addCapability(NET_CAPABILITY_FOREGROUND).build();
+        final TestNetworkCallback callback = new TestNetworkCallback();
+        final TestNetworkCallback fgCallback = new TestNetworkCallback();
+        mCm.registerNetworkCallback(request, callback);
+        mCm.registerNetworkCallback(fgRequest, fgCallback);
+
+        mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
+        mCellNetworkAgent.connect(true);
+        callback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        fgCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertTrue(isForegroundNetwork(mCellNetworkAgent));
+
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mWiFiNetworkAgent.connect(true);
+
+        // When wifi connects, cell lingers.
+        callback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        fgCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        callback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+        fgCallback.expectCallback(CallbackState.LOSING, mCellNetworkAgent);
+        assertTrue(isForegroundNetwork(mCellNetworkAgent));
+        assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
+
+        // When lingering is complete, cell is still there but is now in the background.
+        fgCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent, TEST_LINGER_DELAY_MS);
+        callback.assertNoCallback();
+        assertFalse(isForegroundNetwork(mCellNetworkAgent));
+        assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
+
+        // File a cell request and check that cell comes into the foreground.
+        final NetworkRequest cellRequest = new NetworkRequest.Builder()
+                .addTransportType(TRANSPORT_CELLULAR).build();
+        final TestNetworkCallback cellCallback = new TestNetworkCallback();
+        mCm.requestNetwork(cellRequest, cellCallback);
+        cellCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        fgCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        callback.assertNoCallback();  // Because the network is already up.
+        assertTrue(isForegroundNetwork(mCellNetworkAgent));
+        assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
+
+        // Release the request. The network immediately goes into the background, since it was not
+        // lingering.
+        mCm.unregisterNetworkCallback(cellCallback);
+        fgCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+        callback.assertNoCallback();
+        assertFalse(isForegroundNetwork(mCellNetworkAgent));
+        assertTrue(isForegroundNetwork(mWiFiNetworkAgent));
+
+        // Disconnect wifi and check that cell is foreground again.
+        mWiFiNetworkAgent.disconnect();
+        callback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        fgCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+        fgCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertTrue(isForegroundNetwork(mCellNetworkAgent));
+
+        mCm.unregisterNetworkCallback(callback);
+        mCm.unregisterNetworkCallback(fgCallback);
+    }
+
+    @SmallTest
+    public void testRequestBenchmark() throws Exception {
+        // Benchmarks connecting and switching performance in the presence of a large number of
+        // NetworkRequests.
+        // 1. File NUM_REQUESTS requests.
+        // 2. Have a network connect. Wait for NUM_REQUESTS onAvailable callbacks to fire.
+        // 3. Have a new network connect and outscore the previous. Wait for NUM_REQUESTS onLosing
+        //    and NUM_REQUESTS onAvailable callbacks to fire.
+        // See how long it took.
+        final int NUM_REQUESTS = 90;
+        final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
+        final NetworkCallback[] callbacks = new NetworkCallback[NUM_REQUESTS];
+        final CountDownLatch availableLatch = new CountDownLatch(NUM_REQUESTS);
+        final CountDownLatch losingLatch = new CountDownLatch(NUM_REQUESTS);
+
+        final int REGISTER_TIME_LIMIT_MS = 100;
+        long startTime = System.currentTimeMillis();
+        for (int i = 0; i < NUM_REQUESTS; i++) {
+            callbacks[i] = new NetworkCallback() {
+                @Override public void onAvailable(Network n) { availableLatch.countDown(); }
+                @Override public void onLosing(Network n, int t) { losingLatch.countDown(); }
+            };
+            mCm.registerNetworkCallback(request, callbacks[i]);
+        }
+        long timeTaken = System.currentTimeMillis() - startTime;
+        String msg = String.format("Register %d callbacks: %dms, acceptable %dms",
+                NUM_REQUESTS, timeTaken, REGISTER_TIME_LIMIT_MS);
+        Log.d(TAG, msg);
+        assertTrue(msg, timeTaken < REGISTER_TIME_LIMIT_MS);
+
+        final int CONNECT_TIME_LIMIT_MS = 30;
+        mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
+        // Don't request that the network validate, because otherwise connect() will block until
+        // the network gets NET_CAPABILITY_VALIDATED, after all the callbacks below have fired,
+        // and we won't actually measure anything.
+        mCellNetworkAgent.connect(false);
+        startTime = System.currentTimeMillis();
+        if (!availableLatch.await(CONNECT_TIME_LIMIT_MS, TimeUnit.MILLISECONDS)) {
+            fail(String.format("Only dispatched %d/%d onAvailable callbacks in %dms",
+                    NUM_REQUESTS - availableLatch.getCount(), NUM_REQUESTS,
+                    CONNECT_TIME_LIMIT_MS));
+        }
+        timeTaken = System.currentTimeMillis() - startTime;
+        Log.d(TAG, String.format("Connect, %d callbacks: %dms, acceptable %dms",
+                NUM_REQUESTS, timeTaken, CONNECT_TIME_LIMIT_MS));
+
+        final int SWITCH_TIME_LIMIT_MS = 30;
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        // Give wifi a high enough score that we'll linger cell when wifi comes up.
+        mWiFiNetworkAgent.adjustScore(40);
+        mWiFiNetworkAgent.connect(false);
+        startTime = System.currentTimeMillis();
+        if (!losingLatch.await(SWITCH_TIME_LIMIT_MS, TimeUnit.MILLISECONDS)) {
+            fail(String.format("Only dispatched %d/%d onLosing callbacks in %dms",
+                    NUM_REQUESTS - losingLatch.getCount(), NUM_REQUESTS, SWITCH_TIME_LIMIT_MS));
+        }
+        timeTaken = System.currentTimeMillis() - startTime;
+        Log.d(TAG, String.format("Linger, %d callbacks: %dms, acceptable %dms",
+                NUM_REQUESTS, timeTaken, SWITCH_TIME_LIMIT_MS));
+
+        final int UNREGISTER_TIME_LIMIT_MS = 10;
+        startTime = System.currentTimeMillis();
+        for (int i = 0; i < NUM_REQUESTS; i++) {
+            mCm.unregisterNetworkCallback(callbacks[i]);
+        }
+        timeTaken = System.currentTimeMillis() - startTime;
+        msg = String.format("Unregister %d callbacks: %dms, acceptable %dms",
+                NUM_REQUESTS, timeTaken, UNREGISTER_TIME_LIMIT_MS);
+        Log.d(TAG, msg);
+        assertTrue(msg, timeTaken < UNREGISTER_TIME_LIMIT_MS);
+    }
+
+    @SmallTest
+    public void testMobileDataAlwaysOn() throws Exception {
+        final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
+        final NetworkRequest cellRequest = new NetworkRequest.Builder()
+                .addTransportType(TRANSPORT_CELLULAR).build();
+        mCm.registerNetworkCallback(cellRequest, cellNetworkCallback);
+
+        final HandlerThread handlerThread = new HandlerThread("MobileDataAlwaysOnFactory");
+        handlerThread.start();
+        NetworkCapabilities filter = new NetworkCapabilities()
+                .addTransportType(TRANSPORT_CELLULAR)
+                .addCapability(NET_CAPABILITY_INTERNET);
+        final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(),
+                mServiceContext, "testFactory", filter);
+        testFactory.setScoreFilter(40);
+
+        // Register the factory and expect it to start looking for a network.
+        testFactory.expectAddRequests(1);
+        testFactory.register();
+        testFactory.waitForNetworkRequests(1);
+        assertTrue(testFactory.getMyStartRequested());
+
+        // Bring up wifi. The factory stops looking for a network.
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        testFactory.expectAddRequests(2);  // Because the default request changes score twice.
+        mWiFiNetworkAgent.connect(true);
+        testFactory.waitForNetworkRequests(1);
+        assertFalse(testFactory.getMyStartRequested());
+
+        ContentResolver cr = mServiceContext.getContentResolver();
+
+        // Turn on mobile data always on. The factory starts looking again.
+        testFactory.expectAddRequests(1);
+        setMobileDataAlwaysOn(true);
+        testFactory.waitForNetworkRequests(2);
+        assertTrue(testFactory.getMyStartRequested());
+
+        // Bring up cell data and check that the factory stops looking.
+        assertEquals(1, mCm.getAllNetworks().length);
+        mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
+        testFactory.expectAddRequests(2);  // Because the cell request changes score twice.
+        mCellNetworkAgent.connect(true);
+        cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        testFactory.waitForNetworkRequests(2);
+        assertFalse(testFactory.getMyStartRequested());  // Because the cell network outscores us.
+
+        // Check that cell data stays up.
+        mService.waitForIdle();
+        verifyActiveNetwork(TRANSPORT_WIFI);
+        assertEquals(2, mCm.getAllNetworks().length);
+
+        // Turn off mobile data always on and expect the request to disappear...
+        testFactory.expectRemoveRequests(1);
+        setMobileDataAlwaysOn(false);
+        testFactory.waitForNetworkRequests(1);
+
+        // ...  and cell data to be torn down.
+        cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+        assertEquals(1, mCm.getAllNetworks().length);
+
+        testFactory.unregister();
+        mCm.unregisterNetworkCallback(cellNetworkCallback);
+        handlerThread.quit();
+    }
+
+    @SmallTest
+    public void testAvoidBadWifiSetting() throws Exception {
+        final ContentResolver cr = mServiceContext.getContentResolver();
+        final WrappedAvoidBadWifiTracker tracker = mService.getAvoidBadWifiTracker();
+        final String settingName = Settings.Global.NETWORK_AVOID_BAD_WIFI;
+
+        tracker.configRestrictsAvoidBadWifi = false;
+        String[] values = new String[] {null, "0", "1"};
+        for (int i = 0; i < values.length; i++) {
+            Settings.Global.putInt(cr, settingName, 1);
+            tracker.reevaluate();
+            mService.waitForIdle();
+            String msg = String.format("config=false, setting=%s", values[i]);
+            assertTrue(msg, mService.avoidBadWifi());
+            assertFalse(msg, tracker.shouldNotifyWifiUnvalidated());
+        }
+
+        tracker.configRestrictsAvoidBadWifi = true;
+
+        Settings.Global.putInt(cr, settingName, 0);
+        tracker.reevaluate();
+        mService.waitForIdle();
+        assertFalse(mService.avoidBadWifi());
+        assertFalse(tracker.shouldNotifyWifiUnvalidated());
+
+        Settings.Global.putInt(cr, settingName, 1);
+        tracker.reevaluate();
+        mService.waitForIdle();
+        assertTrue(mService.avoidBadWifi());
+        assertFalse(tracker.shouldNotifyWifiUnvalidated());
+
+        Settings.Global.putString(cr, settingName, null);
+        tracker.reevaluate();
+        mService.waitForIdle();
+        assertFalse(mService.avoidBadWifi());
+        assertTrue(tracker.shouldNotifyWifiUnvalidated());
+    }
+
+    @SmallTest
+    public void testAvoidBadWifi() throws Exception {
+        final ContentResolver cr = mServiceContext.getContentResolver();
+        final WrappedAvoidBadWifiTracker tracker = mService.getAvoidBadWifiTracker();
+
+        // Pretend we're on a carrier that restricts switching away from bad wifi.
+        tracker.configRestrictsAvoidBadWifi = true;
+
+        // File a request for cell to ensure it doesn't go down.
+        final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
+        final NetworkRequest cellRequest = new NetworkRequest.Builder()
+                .addTransportType(TRANSPORT_CELLULAR).build();
+        mCm.requestNetwork(cellRequest, cellNetworkCallback);
+
+        TestNetworkCallback defaultCallback = new TestNetworkCallback();
+        mCm.registerDefaultNetworkCallback(defaultCallback);
+
+        NetworkRequest validatedWifiRequest = new NetworkRequest.Builder()
+                .addTransportType(TRANSPORT_WIFI)
+                .addCapability(NET_CAPABILITY_VALIDATED)
+                .build();
+        TestNetworkCallback validatedWifiCallback = new TestNetworkCallback();
+        mCm.registerNetworkCallback(validatedWifiRequest, validatedWifiCallback);
+
+        Settings.Global.putInt(cr, Settings.Global.NETWORK_AVOID_BAD_WIFI, 0);
+        tracker.reevaluate();
+
+        // Bring up validated cell.
+        mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
+        mCellNetworkAgent.connect(true);
+        cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        Network cellNetwork = mCellNetworkAgent.getNetwork();
+
+        // Bring up validated wifi.
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mWiFiNetworkAgent.connect(true);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        validatedWifiCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        Network wifiNetwork = mWiFiNetworkAgent.getNetwork();
+
+        // Fail validation on wifi.
+        mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 599;
+        mCm.reportNetworkConnectivity(wifiNetwork, false);
+        validatedWifiCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+
+        // Because avoid bad wifi is off, we don't switch to cellular.
+        defaultCallback.assertNoCallback();
+        assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(
+                NET_CAPABILITY_VALIDATED));
+        assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(
+                NET_CAPABILITY_VALIDATED));
+        assertEquals(mCm.getActiveNetwork(), wifiNetwork);
+
+        // Simulate switching to a carrier that does not restrict avoiding bad wifi, and expect
+        // that we switch back to cell.
+        tracker.configRestrictsAvoidBadWifi = false;
+        tracker.reevaluate();
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertEquals(mCm.getActiveNetwork(), cellNetwork);
+
+        // Switch back to a restrictive carrier.
+        tracker.configRestrictsAvoidBadWifi = true;
+        tracker.reevaluate();
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        assertEquals(mCm.getActiveNetwork(), wifiNetwork);
+
+        // Simulate the user selecting "switch" on the dialog, and check that we switch to cell.
+        mCm.setAvoidUnvalidated(wifiNetwork);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(
+                NET_CAPABILITY_VALIDATED));
+        assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(
+                NET_CAPABILITY_VALIDATED));
+        assertEquals(mCm.getActiveNetwork(), cellNetwork);
+
+        // Disconnect and reconnect wifi to clear the one-time switch above.
+        mWiFiNetworkAgent.disconnect();
+        mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+        mWiFiNetworkAgent.connect(true);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        validatedWifiCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        wifiNetwork = mWiFiNetworkAgent.getNetwork();
+
+        // Fail validation on wifi and expect the dialog to appear.
+        mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 599;
+        mCm.reportNetworkConnectivity(wifiNetwork, false);
+        validatedWifiCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+
+        // Simulate the user selecting "switch" and checking the don't ask again checkbox.
+        Settings.Global.putInt(cr, Settings.Global.NETWORK_AVOID_BAD_WIFI, 1);
+        tracker.reevaluate();
+
+        // We now switch to cell.
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertFalse(mCm.getNetworkCapabilities(wifiNetwork).hasCapability(
+                NET_CAPABILITY_VALIDATED));
+        assertTrue(mCm.getNetworkCapabilities(cellNetwork).hasCapability(
+                NET_CAPABILITY_VALIDATED));
+        assertEquals(mCm.getActiveNetwork(), cellNetwork);
+
+        // Simulate the user turning the cellular fallback setting off and then on.
+        // We switch to wifi and then to cell.
+        Settings.Global.putString(cr, Settings.Global.NETWORK_AVOID_BAD_WIFI, null);
+        tracker.reevaluate();
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        assertEquals(mCm.getActiveNetwork(), wifiNetwork);
+        Settings.Global.putInt(cr, Settings.Global.NETWORK_AVOID_BAD_WIFI, 1);
+        tracker.reevaluate();
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
+        assertEquals(mCm.getActiveNetwork(), cellNetwork);
+
+        // If cell goes down, we switch to wifi.
+        mCellNetworkAgent.disconnect();
+        defaultCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
+        defaultCallback.expectCallback(CallbackState.AVAILABLE, mWiFiNetworkAgent);
+        validatedWifiCallback.assertNoCallback();
+
+        mCm.unregisterNetworkCallback(cellNetworkCallback);
+        mCm.unregisterNetworkCallback(validatedWifiCallback);
+        mCm.unregisterNetworkCallback(defaultCallback);
+    }
+
     private static class TestKeepaliveCallback extends PacketKeepaliveCallback {
 
         public static enum CallbackType { ON_STARTED, ON_STOPPED, ON_ERROR };
diff --git a/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java
index 622e46e..541be3d 100644
--- a/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/NetworkPolicyManagerServiceTest.java
@@ -20,6 +20,7 @@
 import static android.content.Intent.EXTRA_UID;
 import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
 import static android.net.ConnectivityManager.TYPE_WIFI;
+import static android.net.NetworkPolicy.CYCLE_NONE;
 import static android.net.NetworkPolicy.LIMIT_DISABLED;
 import static android.net.NetworkPolicy.WARNING_DISABLED;
 import static android.net.NetworkPolicyManager.POLICY_NONE;
@@ -68,7 +69,6 @@
 import android.net.NetworkTemplate;
 import android.os.Binder;
 import android.os.INetworkManagementService;
-import android.os.IPowerManager;
 import android.os.MessageQueue.IdleHandler;
 import android.os.UserHandle;
 import android.test.AndroidTestCase;
@@ -86,7 +86,9 @@
 import org.easymock.IAnswer;
 
 import java.io.File;
+import java.util.Calendar;
 import java.util.LinkedHashSet;
+import java.util.TimeZone;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
@@ -112,7 +114,6 @@
     private File mPolicyDir;
 
     private IActivityManager mActivityManager;
-    private IPowerManager mPowerManager;
     private INetworkStatsService mStatsService;
     private INetworkManagementService mNetworkManager;
     private INetworkPolicyListener mPolicyListener;
@@ -141,8 +142,7 @@
     private static final int PID_2 = 401;
     private static final int PID_3 = 402;
 
-    @Override
-    public void setUp() throws Exception {
+    public void _setUp() throws Exception {
         super.setUp();
 
         setCurrentTimeMillis(TEST_START);
@@ -185,7 +185,6 @@
         }
 
         mActivityManager = createMock(IActivityManager.class);
-        mPowerManager = createMock(IPowerManager.class);
         mStatsService = createMock(INetworkStatsService.class);
         mNetworkManager = createMock(INetworkManagementService.class);
         mPolicyListener = createMock(INetworkPolicyListener.class);
@@ -193,7 +192,7 @@
         mConnManager = createMock(IConnectivityManager.class);
         mNotifManager = createMock(INotificationManager.class);
 
-        mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mPowerManager,
+        mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager,
                 mStatsService, mNetworkManager, mTime, mPolicyDir, true);
         mService.bindConnectivityManager(mConnManager);
         mService.bindNotificationManager(mNotifManager);
@@ -215,8 +214,6 @@
         mNetworkManager.registerObserver(capture(networkObserver));
         expectLastCall().atLeastOnce();
 
-        // expect to answer screen status during systemReady()
-        expect(mPowerManager.isInteractive()).andReturn(true).atLeastOnce();
         expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
         expectCurrentTime();
 
@@ -229,8 +226,7 @@
 
     }
 
-    @Override
-    public void tearDown() throws Exception {
+    public void _tearDown() throws Exception {
         for (File file : mPolicyDir.listFiles()) {
             file.delete();
         }
@@ -239,7 +235,6 @@
         mPolicyDir = null;
 
         mActivityManager = null;
-        mPowerManager = null;
         mStatsService = null;
         mPolicyListener = null;
         mTime = null;
@@ -263,6 +258,7 @@
         backgroundChanged.get();
     }
 
+    @Suppress
     public void testPidForegroundCombined() throws Exception {
         IdleFuture idle;
 
@@ -310,47 +306,7 @@
         assertFalse(mService.isUidForeground(UID_A));
     }
 
-    public void testScreenChangesRules() throws Exception {
-        Future<Void> future;
-
-        expectSetUidMeteredNetworkBlacklist(UID_A, false);
-        expectSetUidForeground(UID_A, true);
-        future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
-        replay();
-        mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
-        future.get();
-        verifyAndReset();
-
-        // push strict policy for foreground uid, verify ALLOW rule
-        expectSetUidMeteredNetworkBlacklist(UID_A, false);
-        expectSetUidForeground(UID_A, true);
-        future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
-        replay();
-        mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
-        future.get();
-        verifyAndReset();
-
-        // now turn screen off and verify REJECT rule
-        expect(mPowerManager.isInteractive()).andReturn(false).atLeastOnce();
-        expectSetUidMeteredNetworkBlacklist(UID_A, true);
-        expectSetUidForeground(UID_A, false);
-        future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
-        replay();
-        mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_OFF));
-        future.get();
-        verifyAndReset();
-
-        // and turn screen back on, verify ALLOW rule restored
-        expect(mPowerManager.isInteractive()).andReturn(true).atLeastOnce();
-        expectSetUidMeteredNetworkBlacklist(UID_A, false);
-        expectSetUidForeground(UID_A, true);
-        future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
-        replay();
-        mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_ON));
-        future.get();
-        verifyAndReset();
-    }
-
+    @Suppress
     public void testPolicyNone() throws Exception {
         Future<Void> future;
 
@@ -381,6 +337,7 @@
         verifyAndReset();
     }
 
+    @Suppress
     public void testPolicyReject() throws Exception {
         Future<Void> future;
 
@@ -412,6 +369,7 @@
         verifyAndReset();
     }
 
+    @Suppress
     public void testPolicyRejectAddRemove() throws Exception {
         Future<Void> future;
 
@@ -576,6 +534,17 @@
                 computeLastCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
     }
 
+    public void testLastCycleBoundaryDST() throws Exception {
+        final long currentTime = parseTime("1989-01-02T07:30:00.000");
+        final long expectedCycle = parseTime("1988-12-03T02:00:00.000Z");
+
+        final NetworkPolicy policy = new NetworkPolicy(
+                sTemplateWifi, 3, "America/Argentina/Buenos_Aires", 1024L, 1024L, false);
+        final long actualCycle = computeLastCycleBoundary(currentTime, policy);
+        assertTimeEquals(expectedCycle, actualCycle);
+    }
+
+    @Suppress
     public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
         NetworkState[] state = null;
         NetworkStats stats = null;
@@ -628,6 +597,7 @@
         verifyAndReset();
     }
 
+    @Suppress
     public void testUidRemovedPolicyCleared() throws Exception {
         Future<Void> future;
 
@@ -652,6 +622,7 @@
         verifyAndReset();
     }
 
+    @Suppress
     public void testOverWarningLimitNotification() throws Exception {
         NetworkState[] state = null;
         NetworkStats stats = null;
@@ -783,6 +754,7 @@
         }
     }
 
+    @Suppress
     public void testMeteredNetworkWithoutLimit() throws Exception {
         NetworkState[] state = null;
         NetworkStats stats = null;
@@ -1029,14 +1001,14 @@
     }
 
     private void replay() {
-        EasyMock.replay(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
-                mNetworkManager, mTime, mConnManager, mNotifManager);
+        EasyMock.replay(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
+                mConnManager, mNotifManager);
     }
 
     private void verifyAndReset() {
-        EasyMock.verify(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
-                mNetworkManager, mTime, mConnManager, mNotifManager);
-        EasyMock.reset(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
-                mNetworkManager, mTime, mConnManager, mNotifManager);
+        EasyMock.verify(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
+                mConnManager, mNotifManager);
+        EasyMock.reset(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
+                mConnManager, mNotifManager);
     }
 }
diff --git a/services/tests/servicestests/src/com/android/server/accounts/AccountManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/accounts/AccountManagerServiceTest.java
index ef9739d..a3d0afa 100644
--- a/services/tests/servicestests/src/com/android/server/accounts/AccountManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/accounts/AccountManagerServiceTest.java
@@ -21,6 +21,7 @@
 import static org.mockito.Mockito.when;
 
 import android.accounts.Account;
+import android.accounts.AccountManagerInternal;
 import android.accounts.AuthenticatorDescription;
 import android.app.AppOpsManager;
 import android.app.Notification;
@@ -44,6 +45,8 @@
 import android.test.mock.MockPackageManager;
 import android.util.Log;
 
+import com.android.server.LocalServices;
+
 import java.io.File;
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
@@ -73,6 +76,7 @@
         SQLiteDatabase.deleteDatabase(new File(mAms.getCeDatabaseName(UserHandle.USER_SYSTEM)));
         SQLiteDatabase.deleteDatabase(new File(mAms.getDeDatabaseName(UserHandle.USER_SYSTEM)));
         SQLiteDatabase.deleteDatabase(new File(mAms.getPreNDatabaseName(UserHandle.USER_SYSTEM)));
+        LocalServices.removeServiceForTest(AccountManagerInternal.class);
         super.tearDown();
     }
 
@@ -282,6 +286,7 @@
 
     private AccountManagerService createAccountManagerService(Context mockContext,
             Context realContext) {
+        LocalServices.removeServiceForTest(AccountManagerInternal.class);
         return new MyAccountManagerService(mockContext,
                 new MyMockPackageManager(), new MockAccountAuthenticatorCache(), realContext);
     }
@@ -337,6 +342,10 @@
         @Override
         public void invalidateCache(int userId) {
         }
+
+        @Override
+        public void updateServices(int userId) {
+        }
     }
 
     static public class MyMockContext extends MockContext {
@@ -416,6 +425,11 @@
         public int checkSignatures(final int uid1, final int uid2) {
             return PackageManager.SIGNATURE_MATCH;
         }
+
+        @Override
+        public void addOnPermissionsChangeListener(
+                OnPermissionsChangedListener listener) {
+        }
     }
 
     static public class MyAccountManagerService extends AccountManagerService {
diff --git a/services/tests/servicestests/src/com/android/server/am/TaskPersisterTest.java b/services/tests/servicestests/src/com/android/server/am/TaskPersisterTest.java
index e440a0d..984a484 100644
--- a/services/tests/servicestests/src/com/android/server/am/TaskPersisterTest.java
+++ b/services/tests/servicestests/src/com/android/server/am/TaskPersisterTest.java
@@ -62,7 +62,7 @@
         for (int i = 0; i < 100; i++) {
             taskIdsOnFile.put(getRandomTaskIdForUser(testUserId), true);
         }
-        mTaskPersister.maybeWritePersistedTaskIdsForUser(taskIdsOnFile, testUserId);
+        mTaskPersister.writePersistedTaskIdsForUser(taskIdsOnFile, testUserId);
         SparseBooleanArray newTaskIdsOnFile = mTaskPersister
                 .loadPersistedTaskIdsForUser(testUserId);
         assertTrue("TaskIds written differ from TaskIds read back from file",
diff --git a/services/tests/servicestests/src/com/android/server/connectivity/IpConnectivityEventBuilderTest.java b/services/tests/servicestests/src/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
new file mode 100644
index 0000000..aed3635
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/connectivity/IpConnectivityEventBuilderTest.java
@@ -0,0 +1,359 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.net.ConnectivityMetricsEvent;
+import android.net.metrics.ApfProgramEvent;
+import android.net.metrics.ApfStats;
+import android.net.metrics.DefaultNetworkEvent;
+import android.net.metrics.DhcpClientEvent;
+import android.net.metrics.DhcpErrorEvent;
+import android.net.metrics.DnsEvent;
+import android.net.metrics.IpManagerEvent;
+import android.net.metrics.IpReachabilityEvent;
+import android.net.metrics.NetworkEvent;
+import android.net.metrics.RaEvent;
+import android.net.metrics.ValidationProbeEvent;
+import com.google.protobuf.nano.MessageNano;
+import java.util.Arrays;
+import junit.framework.TestCase;
+
+import static com.android.server.connectivity.metrics.IpConnectivityLogClass.IpConnectivityLog;
+import static com.android.server.connectivity.MetricsTestUtil.aBool;
+import static com.android.server.connectivity.MetricsTestUtil.aByteArray;
+import static com.android.server.connectivity.MetricsTestUtil.aLong;
+import static com.android.server.connectivity.MetricsTestUtil.aString;
+import static com.android.server.connectivity.MetricsTestUtil.aType;
+import static com.android.server.connectivity.MetricsTestUtil.anInt;
+import static com.android.server.connectivity.MetricsTestUtil.anIntArray;
+import static com.android.server.connectivity.MetricsTestUtil.b;
+import static com.android.server.connectivity.MetricsTestUtil.describeIpEvent;
+import static com.android.server.connectivity.MetricsTestUtil.ipEv;
+
+public class IpConnectivityEventBuilderTest extends TestCase {
+
+    public void testDefaultNetworkEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(DefaultNetworkEvent.class),
+                anInt(102),
+                anIntArray(1, 2, 3),
+                anInt(101),
+                aBool(true),
+                aBool(false));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  default_network_event <",
+                "    network_id <",
+                "      network_id: 102",
+                "    >",
+                "    previous_network_id <",
+                "      network_id: 101",
+                "    >",
+                "    previous_network_ip_support: 1",
+                "    transport_types: 1",
+                "    transport_types: 2",
+                "    transport_types: 3",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testDhcpClientEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(DhcpClientEvent.class),
+                aString("wlan0"),
+                aString("SomeState"),
+                anInt(192));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  dhcp_event <",
+                "    duration_ms: 192",
+                "    error_code: 0",
+                "    if_name: \"wlan0\"",
+                "    state_transition: \"SomeState\"",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testDhcpErrorEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(DhcpErrorEvent.class),
+                aString("wlan0"),
+                anInt(DhcpErrorEvent.L4_NOT_UDP));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  dhcp_event <",
+                "    duration_ms: 0",
+                "    error_code: 50397184",
+                "    if_name: \"wlan0\"",
+                "    state_transition: \"\"",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testDnsEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(DnsEvent.class),
+                anInt(101),
+                aByteArray(b(1), b(1), b(2), b(1), b(1), b(1), b(2), b(2)),
+                aByteArray(b(0), b(0), b(22), b(3), b(1), b(0), b(200), b(178)),
+                anIntArray(3456, 267, 1230, 45, 2111, 450, 638, 1300));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  dns_lookup_batch <",
+                "    event_types: 1",
+                "    event_types: 1",
+                "    event_types: 2",
+                "    event_types: 1",
+                "    event_types: 1",
+                "    event_types: 1",
+                "    event_types: 2",
+                "    event_types: 2",
+                "    latencies_ms: 3456",
+                "    latencies_ms: 267",
+                "    latencies_ms: 1230",
+                "    latencies_ms: 45",
+                "    latencies_ms: 2111",
+                "    latencies_ms: 450",
+                "    latencies_ms: 638",
+                "    latencies_ms: 1300",
+                "    network_id <",
+                "      network_id: 101",
+                "    >",
+                "    return_codes: 0",
+                "    return_codes: 0",
+                "    return_codes: 22",
+                "    return_codes: 3",
+                "    return_codes: 1",
+                "    return_codes: 0",
+                "    return_codes: 200",
+                "    return_codes: 178",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testIpManagerEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(IpManagerEvent.class),
+                aString("wlan0"),
+                anInt(IpManagerEvent.PROVISIONING_OK),
+                aLong(5678));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  ip_provisioning_event <",
+                "    event_type: 1",
+                "    if_name: \"wlan0\"",
+                "    latency_ms: 5678",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testIpReachabilityEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(IpReachabilityEvent.class),
+                aString("wlan0"),
+                anInt(IpReachabilityEvent.NUD_FAILED));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  ip_reachability_event <",
+                "    event_type: 512",
+                "    if_name: \"wlan0\"",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testNetworkEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(NetworkEvent.class),
+                anInt(100),
+                anInt(5),
+                aLong(20410));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  network_event <",
+                "    event_type: 5",
+                "    latency_ms: 20410",
+                "    network_id <",
+                "      network_id: 100",
+                "    >",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testValidationProbeEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(ValidationProbeEvent.class),
+                anInt(120),
+                aLong(40730),
+                anInt(ValidationProbeEvent.PROBE_HTTP),
+                anInt(204));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  time_ms: 1",
+                "  validation_probe_event <",
+                "    latency_ms: 40730",
+                "    network_id <",
+                "      network_id: 120",
+                "    >",
+                "    probe_result: 204",
+                "    probe_type: 1",
+                "  >",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testApfProgramEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(ApfProgramEvent.class),
+                aLong(200),
+                anInt(7),
+                anInt(9),
+                anInt(2048),
+                anInt(3));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  apf_program_event <",
+                "    current_ras: 9",
+                "    drop_multicast: true",
+                "    filtered_ras: 7",
+                "    has_ipv4_addr: true",
+                "    lifetime: 200",
+                "    program_length: 2048",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testApfStatsSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(ApfStats.class),
+                aLong(45000),
+                anInt(10),
+                anInt(2),
+                anInt(2),
+                anInt(1),
+                anInt(2),
+                anInt(4),
+                anInt(2048));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  apf_statistics <",
+                "    dropped_ras: 2",
+                "    duration_ms: 45000",
+                "    matching_ras: 2",
+                "    max_program_size: 2048",
+                "    parse_errors: 2",
+                "    program_updates: 4",
+                "    received_ras: 10",
+                "    zero_lifetime_ras: 1",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    public void testRaEventSerialization() {
+        ConnectivityMetricsEvent ev = describeIpEvent(
+                aType(RaEvent.class),
+                aLong(2000),
+                aLong(400),
+                aLong(300),
+                aLong(-1),
+                aLong(1000),
+                aLong(-1));
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  ra_event <",
+                "    dnssl_lifetime: -1",
+                "    prefix_preferred_lifetime: 300",
+                "    prefix_valid_lifetime: 400",
+                "    rdnss_lifetime: 1000",
+                "    route_info_lifetime: -1",
+                "    router_lifetime: 2000",
+                "  >",
+                "  time_ms: 1",
+                ">");
+
+        verifySerialization(want, ev);
+    }
+
+    static void verifySerialization(String want, ConnectivityMetricsEvent... input) {
+        try {
+            byte[] got = IpConnectivityEventBuilder.serialize(0, Arrays.asList(input));
+            IpConnectivityLog log = new IpConnectivityLog();
+            MessageNano.mergeFrom(log, got);
+            assertEquals(want, log.toString());
+        } catch (Exception e) {
+            fail(e.toString());
+        }
+    }
+
+    static String joinLines(String ... elems) {
+        StringBuilder b = new StringBuilder();
+        for (String s : elems) {
+            b.append(s);
+            b.append("\n");
+        }
+        return b.toString();
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/connectivity/IpConnectivityMetricsTest.java b/services/tests/servicestests/src/com/android/server/connectivity/IpConnectivityMetricsTest.java
new file mode 100644
index 0000000..3fc89b9
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/connectivity/IpConnectivityMetricsTest.java
@@ -0,0 +1,269 @@
+/*
+ * Copyright (C) 2016, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.content.Context;
+import android.net.ConnectivityMetricsEvent;
+import android.net.IIpConnectivityMetrics;
+import android.net.metrics.ApfStats;
+import android.net.metrics.DefaultNetworkEvent;
+import android.net.metrics.DhcpClientEvent;
+import android.net.metrics.IpConnectivityLog;
+import android.net.metrics.IpManagerEvent;
+import android.net.metrics.IpReachabilityEvent;
+import android.net.metrics.RaEvent;
+import android.net.metrics.ValidationProbeEvent;
+import android.os.Parcelable;
+import android.util.Base64;
+import com.android.server.connectivity.metrics.IpConnectivityLogClass;
+import com.google.protobuf.nano.MessageNano;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import junit.framework.TestCase;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class IpConnectivityMetricsTest extends TestCase {
+    static final IpReachabilityEvent FAKE_EV =
+            new IpReachabilityEvent("wlan0", IpReachabilityEvent.NUD_FAILED);
+
+    @Mock Context mCtx;
+    @Mock IIpConnectivityMetrics mMockService;
+
+    IpConnectivityMetrics mService;
+
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mService = new IpConnectivityMetrics(mCtx);
+    }
+
+    public void testLoggingEvents() throws Exception {
+        IpConnectivityLog logger = new IpConnectivityLog(mMockService);
+
+        assertTrue(logger.log(1, FAKE_EV));
+        assertTrue(logger.log(2, FAKE_EV));
+        assertTrue(logger.log(3, FAKE_EV));
+
+        List<ConnectivityMetricsEvent> got = verifyEvents(3);
+        assertEventsEqual(expectedEvent(1), got.get(0));
+        assertEventsEqual(expectedEvent(2), got.get(1));
+        assertEventsEqual(expectedEvent(3), got.get(2));
+    }
+
+    public void testLoggingEventsWithMultipleCallers() throws Exception {
+        IpConnectivityLog logger = new IpConnectivityLog(mMockService);
+
+        final int nCallers = 10;
+        final int nEvents = 10;
+        for (int n = 0; n < nCallers; n++) {
+            final int i = n;
+            new Thread() {
+                public void run() {
+                    for (int j = 0; j < nEvents; j++) {
+                        assertTrue(logger.log(i * 100 + j, FAKE_EV));
+                    }
+                }
+            }.start();
+        }
+
+        List<ConnectivityMetricsEvent> got = verifyEvents(nCallers * nEvents, 100);
+        Collections.sort(got, EVENT_COMPARATOR);
+        Iterator<ConnectivityMetricsEvent> iter = got.iterator();
+        for (int i = 0; i < nCallers; i++) {
+            for (int j = 0; j < nEvents; j++) {
+                int expectedTimestamp = i * 100 + j;
+                assertEventsEqual(expectedEvent(expectedTimestamp), iter.next());
+            }
+        }
+    }
+
+    public void testBufferFlushing() {
+        String output1 = getdump("flush");
+        assertEquals("", output1);
+
+        new IpConnectivityLog(mService.impl).log(1, FAKE_EV);
+        String output2 = getdump("flush");
+        assertFalse("".equals(output2));
+
+        String output3 = getdump("flush");
+        assertEquals("", output3);
+    }
+
+    public void testEndToEndLogging() {
+        IpConnectivityLog logger = new IpConnectivityLog(mService.impl);
+
+        Parcelable[] events = {
+            new IpReachabilityEvent("wlan0", IpReachabilityEvent.NUD_FAILED),
+            new DhcpClientEvent("wlan0", "SomeState", 192),
+            new DefaultNetworkEvent(102, new int[]{1,2,3}, 101, true, false),
+            new IpManagerEvent("wlan0", IpManagerEvent.PROVISIONING_OK, 5678),
+            new ValidationProbeEvent(120, 40730, ValidationProbeEvent.PROBE_HTTP, 204),
+            new ApfStats(45000, 10, 2, 2, 1, 2, 4, 2048),
+            new RaEvent(2000, 400, 300, -1, 1000, -1)
+        };
+
+        for (int i = 0; i < events.length; i++) {
+            logger.log(100 * (i + 1), events[i]);
+        }
+
+        String want = joinLines(
+                "dropped_events: 0",
+                "events <",
+                "  ip_reachability_event <",
+                "    event_type: 512",
+                "    if_name: \"wlan0\"",
+                "  >",
+                "  time_ms: 100",
+                ">",
+                "events <",
+                "  dhcp_event <",
+                "    duration_ms: 192",
+                "    error_code: 0",
+                "    if_name: \"wlan0\"",
+                "    state_transition: \"SomeState\"",
+                "  >",
+                "  time_ms: 200",
+                ">",
+                "events <",
+                "  default_network_event <",
+                "    network_id <",
+                "      network_id: 102",
+                "    >",
+                "    previous_network_id <",
+                "      network_id: 101",
+                "    >",
+                "    previous_network_ip_support: 1",
+                "    transport_types: 1",
+                "    transport_types: 2",
+                "    transport_types: 3",
+                "  >",
+                "  time_ms: 300",
+                ">",
+                "events <",
+                "  ip_provisioning_event <",
+                "    event_type: 1",
+                "    if_name: \"wlan0\"",
+                "    latency_ms: 5678",
+                "  >",
+                "  time_ms: 400",
+                ">",
+                "events <",
+                "  time_ms: 500",
+                "  validation_probe_event <",
+                "    latency_ms: 40730",
+                "    network_id <",
+                "      network_id: 120",
+                "    >",
+                "    probe_result: 204",
+                "    probe_type: 1",
+                "  >",
+                ">",
+                "events <",
+                "  apf_statistics <",
+                "    dropped_ras: 2",
+                "    duration_ms: 45000",
+                "    matching_ras: 2",
+                "    max_program_size: 2048",
+                "    parse_errors: 2",
+                "    program_updates: 4",
+                "    received_ras: 10",
+                "    zero_lifetime_ras: 1",
+                "  >",
+                "  time_ms: 600",
+                ">",
+                "events <",
+                "  ra_event <",
+                "    dnssl_lifetime: -1",
+                "    prefix_preferred_lifetime: 300",
+                "    prefix_valid_lifetime: 400",
+                "    rdnss_lifetime: 1000",
+                "    route_info_lifetime: -1",
+                "    router_lifetime: 2000",
+                "  >",
+                "  time_ms: 700",
+                ">");
+
+        verifySerialization(want, getdump("flush"));
+    }
+
+    String getdump(String ... command) {
+        StringWriter buffer = new StringWriter();
+        PrintWriter writer = new PrintWriter(buffer);
+        mService.impl.dump(null, writer, command);
+        return buffer.toString();
+    }
+
+    List<ConnectivityMetricsEvent> verifyEvents(int n, int timeoutMs) throws Exception {
+        ArgumentCaptor<ConnectivityMetricsEvent> captor =
+                ArgumentCaptor.forClass(ConnectivityMetricsEvent.class);
+        verify(mMockService, timeout(timeoutMs).times(n)).logEvent(captor.capture());
+        return captor.getAllValues();
+    }
+
+    List<ConnectivityMetricsEvent> verifyEvents(int n) throws Exception {
+        return verifyEvents(n, 10);
+    }
+
+    static void verifySerialization(String want, String output) {
+        try {
+            byte[] got = Base64.decode(output, Base64.DEFAULT);
+            IpConnectivityLogClass.IpConnectivityLog log =
+                    new IpConnectivityLogClass.IpConnectivityLog();
+            MessageNano.mergeFrom(log, got);
+            assertEquals(want, log.toString());
+        } catch (Exception e) {
+            fail(e.toString());
+        }
+    }
+
+    static String joinLines(String ... elems) {
+        StringBuilder b = new StringBuilder();
+        for (String s : elems) {
+            b.append(s).append("\n");
+        }
+        return b.toString();
+    }
+
+    static ConnectivityMetricsEvent expectedEvent(int timestamp) {
+        return new ConnectivityMetricsEvent((long)timestamp, 0, 0, FAKE_EV);
+    }
+
+    /** Outer equality for ConnectivityMetricsEvent to avoid overriding equals() and hashCode(). */
+    static void assertEventsEqual(ConnectivityMetricsEvent expected, ConnectivityMetricsEvent got) {
+        assertEquals(expected.timestamp, got.timestamp);
+        assertEquals(expected.componentTag, got.componentTag);
+        assertEquals(expected.eventTag, got.eventTag);
+        assertEquals(expected.data, got.data);
+    }
+
+    static final Comparator<ConnectivityMetricsEvent> EVENT_COMPARATOR =
+        new Comparator<ConnectivityMetricsEvent>() {
+            @Override
+            public int compare(ConnectivityMetricsEvent ev1, ConnectivityMetricsEvent ev2) {
+                return (int) (ev1.timestamp - ev2.timestamp);
+            }
+        };
+}
diff --git a/services/tests/servicestests/src/com/android/server/connectivity/LingerMonitorTest.java b/services/tests/servicestests/src/com/android/server/connectivity/LingerMonitorTest.java
new file mode 100644
index 0000000..bce5787e
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/connectivity/LingerMonitorTest.java
@@ -0,0 +1,349 @@
+/*
+ * Copyright (C) 2016, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.res.Resources;
+import android.net.ConnectivityManager;
+import android.net.Network;
+import android.net.NetworkCapabilities;
+import android.net.NetworkInfo;
+import android.net.NetworkMisc;
+import android.text.format.DateUtils;
+import com.android.internal.R;
+import com.android.server.ConnectivityService;
+import com.android.server.connectivity.NetworkNotificationManager;
+import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
+import junit.framework.TestCase;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyBoolean;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.reset;
+
+public class LingerMonitorTest extends TestCase {
+    static final String CELLULAR = "CELLULAR";
+    static final String WIFI     = "WIFI";
+
+    static final long LOW_RATE_LIMIT = DateUtils.MINUTE_IN_MILLIS;
+    static final long HIGH_RATE_LIMIT = 0;
+
+    static final int LOW_DAILY_LIMIT = 2;
+    static final int HIGH_DAILY_LIMIT = 1000;
+
+    LingerMonitor mMonitor;
+
+    @Mock ConnectivityService mConnService;
+    @Mock Context mCtx;
+    @Mock NetworkMisc mMisc;
+    @Mock NetworkNotificationManager mNotifier;
+    @Mock Resources mResources;
+
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        when(mCtx.getResources()).thenReturn(mResources);
+        when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
+        when(mConnService.createNetworkMonitor(any(), any(), any(), any())).thenReturn(null);
+
+        mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, HIGH_RATE_LIMIT);
+    }
+
+    public void testTransitions() {
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        NetworkAgentInfo nai1 = wifiNai(100);
+        NetworkAgentInfo nai2 = cellNai(101);
+
+        assertTrue(mMonitor.isNotificationEnabled(nai1, nai2));
+        assertFalse(mMonitor.isNotificationEnabled(nai2, nai1));
+    }
+
+    public void testNotificationOnLinger() {
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNotification(from, to);
+    }
+
+    public void testToastOnLinger() {
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyToast(from, to);
+    }
+
+    public void testNotificationClearedAfterDisconnect() {
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNotification(from, to);
+
+        mMonitor.noteDisconnect(to);
+        verify(mNotifier, times(1)).clearNotification(100);
+    }
+
+    public void testNotificationClearedAfterSwitchingBack() {
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNotification(from, to);
+
+        mMonitor.noteLingerDefaultNetwork(to, from);
+        verify(mNotifier, times(1)).clearNotification(100);
+    }
+
+    public void testUniqueToast() {
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyToast(from, to);
+
+        mMonitor.noteLingerDefaultNetwork(to, from);
+        verify(mNotifier, times(1)).clearNotification(100);
+
+        reset(mNotifier);
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNoNotifications();
+    }
+
+    public void testMultipleNotifications() {
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
+        NetworkAgentInfo wifi1 = wifiNai(100);
+        NetworkAgentInfo wifi2 = wifiNai(101);
+        NetworkAgentInfo cell = cellNai(102);
+
+        mMonitor.noteLingerDefaultNetwork(wifi1, cell);
+        verifyNotification(wifi1, cell);
+
+        mMonitor.noteLingerDefaultNetwork(cell, wifi2);
+        verify(mNotifier, times(1)).clearNotification(100);
+
+        reset(mNotifier);
+        mMonitor.noteLingerDefaultNetwork(wifi2, cell);
+        verifyNotification(wifi2, cell);
+    }
+
+    public void testRateLimiting() throws InterruptedException {
+        mMonitor = new TestableLingerMonitor(mCtx, mNotifier, HIGH_DAILY_LIMIT, LOW_RATE_LIMIT);
+
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
+        NetworkAgentInfo wifi1 = wifiNai(100);
+        NetworkAgentInfo wifi2 = wifiNai(101);
+        NetworkAgentInfo wifi3 = wifiNai(102);
+        NetworkAgentInfo cell = cellNai(103);
+
+        mMonitor.noteLingerDefaultNetwork(wifi1, cell);
+        verifyNotification(wifi1, cell);
+        reset(mNotifier);
+
+        Thread.sleep(50);
+        mMonitor.noteLingerDefaultNetwork(cell, wifi2);
+        mMonitor.noteLingerDefaultNetwork(wifi2, cell);
+        verifyNoNotifications();
+
+        Thread.sleep(50);
+        mMonitor.noteLingerDefaultNetwork(cell, wifi3);
+        mMonitor.noteLingerDefaultNetwork(wifi3, cell);
+        verifyNoNotifications();
+    }
+
+    public void testDailyLimiting() throws InterruptedException {
+        mMonitor = new TestableLingerMonitor(mCtx, mNotifier, LOW_DAILY_LIMIT, HIGH_RATE_LIMIT);
+
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
+        NetworkAgentInfo wifi1 = wifiNai(100);
+        NetworkAgentInfo wifi2 = wifiNai(101);
+        NetworkAgentInfo wifi3 = wifiNai(102);
+        NetworkAgentInfo cell = cellNai(103);
+
+        mMonitor.noteLingerDefaultNetwork(wifi1, cell);
+        verifyNotification(wifi1, cell);
+        reset(mNotifier);
+
+        Thread.sleep(50);
+        mMonitor.noteLingerDefaultNetwork(cell, wifi2);
+        mMonitor.noteLingerDefaultNetwork(wifi2, cell);
+        verifyNotification(wifi2, cell);
+        reset(mNotifier);
+
+        Thread.sleep(50);
+        mMonitor.noteLingerDefaultNetwork(cell, wifi3);
+        mMonitor.noteLingerDefaultNetwork(wifi3, cell);
+        verifyNoNotifications();
+    }
+
+    public void testUniqueNotification() {
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNotification(from, to);
+
+        mMonitor.noteLingerDefaultNetwork(to, from);
+        verify(mNotifier, times(1)).clearNotification(100);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNotification(from, to);
+    }
+
+    public void testIgnoreNeverValidatedNetworks() {
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+        from.everValidated = false;
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNoNotifications();
+    }
+
+    public void testIgnoreCurrentlyValidatedNetworks() {
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+        from.lastValidated = true;
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNoNotifications();
+    }
+
+    public void testNoNotificationType() {
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
+        setNotificationSwitch();
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNoNotifications();
+    }
+
+    public void testNoTransitionToNotify() {
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_NONE);
+        setNotificationSwitch(transition(WIFI, CELLULAR));
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNoNotifications();
+    }
+
+    public void testDifferentTransitionToNotify() {
+        setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
+        setNotificationSwitch(transition(CELLULAR, WIFI));
+        NetworkAgentInfo from = wifiNai(100);
+        NetworkAgentInfo to = cellNai(101);
+
+        mMonitor.noteLingerDefaultNetwork(from, to);
+        verifyNoNotifications();
+    }
+
+    void setNotificationSwitch(String... transitions) {
+        when(mResources.getStringArray(R.array.config_networkNotifySwitches))
+                .thenReturn(transitions);
+    }
+
+    String transition(String from, String to) {
+        return from + "-" + to;
+    }
+
+    void setNotificationType(int type) {
+        when(mResources.getInteger(R.integer.config_networkNotifySwitchType)).thenReturn(type);
+    }
+
+    void verifyNoToast() {
+        verify(mNotifier, never()).showToast(any(), any());
+    }
+
+    void verifyNoNotification() {
+        verify(mNotifier, never())
+                .showNotification(anyInt(), any(), any(), any(), any(), anyBoolean());
+    }
+
+    void verifyNoNotifications() {
+        verifyNoToast();
+        verifyNoNotification();
+    }
+
+    void verifyToast(NetworkAgentInfo from, NetworkAgentInfo to) {
+        verifyNoNotification();
+        verify(mNotifier, times(1)).showToast(from, to);
+    }
+
+    void verifyNotification(NetworkAgentInfo from, NetworkAgentInfo to) {
+        verifyNoToast();
+        verify(mNotifier, times(1)).showNotification(eq(from.network.netId),
+                eq(NotificationType.NETWORK_SWITCH), eq(from), eq(to), any(), eq(true));
+    }
+
+    NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
+        NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
+        NetworkCapabilities caps = new NetworkCapabilities();
+        caps.addCapability(0);
+        caps.addTransportType(transport);
+        NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
+                caps, 50, mCtx, null, mMisc, null, mConnService);
+        nai.everValidated = true;
+        return nai;
+    }
+
+    NetworkAgentInfo wifiNai(int netId) {
+        return nai(netId, NetworkCapabilities.TRANSPORT_WIFI,
+                ConnectivityManager.TYPE_WIFI, WIFI);
+    }
+
+    NetworkAgentInfo cellNai(int netId) {
+        return nai(netId, NetworkCapabilities.TRANSPORT_CELLULAR,
+                ConnectivityManager.TYPE_MOBILE, CELLULAR);
+    }
+
+    public static class TestableLingerMonitor extends LingerMonitor {
+        public TestableLingerMonitor(Context c, NetworkNotificationManager n, int l, long r) {
+            super(c, n, l, r);
+        }
+        @Override protected PendingIntent createNotificationIntent() {
+            return null;
+        }
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/connectivity/MetricsLoggerServiceTest.java b/services/tests/servicestests/src/com/android/server/connectivity/MetricsLoggerServiceTest.java
new file mode 100644
index 0000000..5f84ea1
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/connectivity/MetricsLoggerServiceTest.java
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2016, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.content.Context;
+import android.net.ConnectivityMetricsEvent;
+import android.os.Bundle;
+import android.os.RemoteException;
+import static android.net.ConnectivityMetricsEvent.Reference;
+
+import junit.framework.TestCase;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
+
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.io.FileDescriptor;
+import java.io.FileOutputStream;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/*
+ * TODO:
+ *  - allow overriding MetricsLoggerService constants in tests.
+ *  - test intents are correctly sent after the notification threshold.
+ *  - test oldest events are correctly pushed out when internal deque is full.
+ *  - test throttling triggers correctly.
+ */
+public class MetricsLoggerServiceTest extends TestCase {
+
+    static final int COMPONENT_TAG = 1;
+    static final long N_EVENTS = 10L;
+    static final ConnectivityMetricsEvent EVENTS[] = new ConnectivityMetricsEvent[(int)N_EVENTS];
+    static {
+        for (int i = 0; i < N_EVENTS; i++) {
+            EVENTS[i] = new ConnectivityMetricsEvent(i, COMPONENT_TAG, i, new Bundle());
+        }
+    }
+
+    static final ConnectivityMetricsEvent NO_EVENTS[] = new ConnectivityMetricsEvent[0];
+
+    @Mock Context mContext;
+    MetricsLoggerService mService;
+
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mService = new MetricsLoggerService(mContext);
+        mService.onStart();
+    }
+
+    public void testGetNoEvents() throws Exception {
+        Reference r = new Reference(0);
+        assertArrayEquals(NO_EVENTS, mService.mBinder.getEvents(r));
+        assertEquals(0, r.getValue());
+    }
+
+    public void testLogAndGetEvents() throws Exception {
+        mService.mBinder.logEvents(EVENTS);
+
+        Reference r = new Reference(0);
+
+        assertArrayEquals(EVENTS, mService.mBinder.getEvents(r));
+        assertEquals(N_EVENTS, r.getValue());
+
+        assertArrayEquals(NO_EVENTS, mService.mBinder.getEvents(r));
+        assertEquals(N_EVENTS, r.getValue());
+    }
+
+    public void testLogOneByOne() throws Exception {
+        for (ConnectivityMetricsEvent ev : EVENTS) {
+            mService.mBinder.logEvent(ev);
+        }
+
+        Reference r = new Reference(0);
+
+        assertArrayEquals(EVENTS, mService.mBinder.getEvents(r));
+        assertEquals(N_EVENTS, r.getValue());
+
+        assertArrayEquals(NO_EVENTS, mService.mBinder.getEvents(r));
+        assertEquals(N_EVENTS, r.getValue());
+    }
+
+    public void testInterleavedLogAndGet() throws Exception {
+        mService.mBinder.logEvents(Arrays.copyOfRange(EVENTS, 0, 3));
+
+        Reference r = new Reference(0);
+
+        assertArrayEquals(Arrays.copyOfRange(EVENTS, 0, 3), mService.mBinder.getEvents(r));
+        assertEquals(3, r.getValue());
+
+        mService.mBinder.logEvents(Arrays.copyOfRange(EVENTS, 3, 8));
+        mService.mBinder.logEvents(Arrays.copyOfRange(EVENTS, 8, 10));
+
+        assertArrayEquals(Arrays.copyOfRange(EVENTS, 3, 10), mService.mBinder.getEvents(r));
+        assertEquals(N_EVENTS, r.getValue());
+
+        assertArrayEquals(NO_EVENTS, mService.mBinder.getEvents(r));
+        assertEquals(N_EVENTS, r.getValue());
+    }
+
+    public void testMultipleGetAll() throws Exception {
+        mService.mBinder.logEvents(Arrays.copyOf(EVENTS, 3));
+
+        Reference r1 = new Reference(0);
+        assertArrayEquals(Arrays.copyOf(EVENTS, 3), mService.mBinder.getEvents(r1));
+        assertEquals(3, r1.getValue());
+
+        mService.mBinder.logEvents(Arrays.copyOfRange(EVENTS, 3, 10));
+
+        Reference r2 = new Reference(0);
+        assertArrayEquals(EVENTS, mService.mBinder.getEvents(r2));
+        assertEquals(N_EVENTS, r2.getValue());
+    }
+
+    public void testLogAndDumpConcurrently() throws Exception {
+        for (int i = 0; i < 50; i++) {
+            mContext = null;
+            mService = null;
+            setUp();
+            logAndDumpConcurrently();
+        }
+    }
+
+    public void logAndDumpConcurrently() throws Exception {
+        final CountDownLatch latch = new CountDownLatch((int)N_EVENTS);
+        final FileDescriptor fd = new FileOutputStream("/dev/null").getFD();
+
+        for (ConnectivityMetricsEvent ev : EVENTS) {
+            new Thread() {
+                public void run() {
+                    mService.mBinder.logEvent(ev);
+                    latch.countDown();
+                }
+            }.start();
+        }
+
+        new Thread() {
+            public void run() {
+                while (latch.getCount() > 0) {
+                    mService.mBinder.dump(fd, new String[]{"--all"});
+                }
+            }
+        }.start();
+
+        latch.await(100, TimeUnit.MILLISECONDS);
+
+        Reference r = new Reference(0);
+        ConnectivityMetricsEvent[] got = mService.mBinder.getEvents(r);
+        Arrays.sort(got, new EventComparator());
+        assertArrayEquals(EVENTS, got);
+        assertEquals(N_EVENTS, r.getValue());
+    }
+
+    static class EventComparator implements Comparator<ConnectivityMetricsEvent> {
+        public int compare(ConnectivityMetricsEvent ev1, ConnectivityMetricsEvent ev2) {
+            return Long.compare(ev1.timestamp, ev2.timestamp);
+        }
+        public boolean equal(Object o) {
+            return o instanceof EventComparator;
+        }
+    };
+}
diff --git a/services/tests/servicestests/src/com/android/server/connectivity/MetricsTestUtil.java b/services/tests/servicestests/src/com/android/server/connectivity/MetricsTestUtil.java
new file mode 100644
index 0000000..e201012
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/connectivity/MetricsTestUtil.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.net.ConnectivityMetricsEvent;
+import android.net.ConnectivityMetricsLogger;
+import android.os.Bundle;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+abstract public class MetricsTestUtil {
+    private MetricsTestUtil() {
+    }
+
+    static ConnectivityMetricsEvent ipEv(Parcelable p) {
+        return ev(ConnectivityMetricsLogger.COMPONENT_TAG_CONNECTIVITY, p);
+    }
+
+    static ConnectivityMetricsEvent telephonyEv() {
+        return ev(ConnectivityMetricsLogger.COMPONENT_TAG_TELEPHONY, new Bundle());
+    }
+
+    static ConnectivityMetricsEvent ev(int tag, Parcelable p) {
+        return new ConnectivityMetricsEvent(1L, tag, 0, p);
+    }
+
+    // Utiliy interface for describing the content of a Parcel. This relies on
+    // the implementation defails of Parcelable and on the fact that the fully
+    // qualified Parcelable class names are written as string in the Parcels.
+    interface ParcelField {
+        void write(Parcel p);
+    }
+
+    static ConnectivityMetricsEvent describeIpEvent(ParcelField... fs) {
+        Parcel p = Parcel.obtain();
+        for (ParcelField f : fs) {
+            f.write(p);
+        }
+        p.setDataPosition(0);
+        return ipEv(p.readParcelable(ClassLoader.getSystemClassLoader()));
+    }
+
+    static ParcelField aType(Class<?> c) {
+        return new ParcelField() {
+            public void write(Parcel p) {
+                p.writeString(c.getName());
+            }
+        };
+    }
+
+    static ParcelField aBool(boolean b) {
+        return aByte((byte) (b ? 1 : 0));
+    }
+
+    static ParcelField aByte(byte b) {
+        return new ParcelField() {
+            public void write(Parcel p) {
+                p.writeByte(b);
+            }
+        };
+    }
+
+    static ParcelField anInt(int i) {
+        return new ParcelField() {
+            public void write(Parcel p) {
+                p.writeInt(i);
+            }
+        };
+    }
+
+    static ParcelField aLong(long l) {
+        return new ParcelField() {
+            public void write(Parcel p) {
+                p.writeLong(l);
+            }
+        };
+    }
+
+    static ParcelField aString(String s) {
+        return new ParcelField() {
+            public void write(Parcel p) {
+                p.writeString(s);
+            }
+        };
+    }
+
+    static ParcelField aByteArray(byte... ary) {
+        return new ParcelField() {
+            public void write(Parcel p) {
+                p.writeByteArray(ary);
+            }
+        };
+    }
+
+    static ParcelField anIntArray(int... ary) {
+        return new ParcelField() {
+            public void write(Parcel p) {
+                p.writeIntArray(ary);
+            }
+        };
+    }
+
+    static byte b(int i) {
+        return (byte) i;
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/connectivity/NetdEventListenerServiceTest.java b/services/tests/servicestests/src/com/android/server/connectivity/NetdEventListenerServiceTest.java
new file mode 100644
index 0000000..63d5d9fb
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/connectivity/NetdEventListenerServiceTest.java
@@ -0,0 +1,197 @@
+/*
+ * Copyright (C) 2016, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.connectivity;
+
+import android.net.ConnectivityManager.NetworkCallback;
+import android.net.ConnectivityManager;
+import android.net.Network;
+import android.net.metrics.DnsEvent;
+import android.net.metrics.INetdEventListener;
+import android.net.metrics.IpConnectivityLog;
+
+import junit.framework.TestCase;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.anyInt;
+import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.io.FileOutputStream;
+import java.io.PrintWriter;
+import java.util.Arrays;
+import java.util.List;
+import java.util.OptionalInt;
+import java.util.stream.IntStream;
+
+public class NetdEventListenerServiceTest extends TestCase {
+
+    // TODO: read from NetdEventListenerService after this constant is read from system property
+    static final int BATCH_SIZE = 100;
+    static final int EVENT_TYPE = INetdEventListener.EVENT_GETADDRINFO;
+    // TODO: read from INetdEventListener
+    static final int RETURN_CODE = 1;
+
+    static final byte[] EVENT_TYPES  = new byte[BATCH_SIZE];
+    static final byte[] RETURN_CODES = new byte[BATCH_SIZE];
+    static final int[] LATENCIES     = new int[BATCH_SIZE];
+    static {
+        for (int i = 0; i < BATCH_SIZE; i++) {
+            EVENT_TYPES[i] = EVENT_TYPE;
+            RETURN_CODES[i] = RETURN_CODE;
+            LATENCIES[i] = i;
+        }
+    }
+
+    NetdEventListenerService mNetdEventListenerService;
+
+    @Mock ConnectivityManager mCm;
+    @Mock IpConnectivityLog mLog;
+    ArgumentCaptor<NetworkCallback> mCallbackCaptor;
+    ArgumentCaptor<DnsEvent> mEvCaptor;
+
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mCallbackCaptor = ArgumentCaptor.forClass(NetworkCallback.class);
+        mEvCaptor = ArgumentCaptor.forClass(DnsEvent.class);
+        mNetdEventListenerService = new NetdEventListenerService(mCm, mLog);
+
+        verify(mCm, times(1)).registerNetworkCallback(any(), mCallbackCaptor.capture());
+    }
+
+    public void testOneBatch() throws Exception {
+        log(105, LATENCIES);
+        log(106, Arrays.copyOf(LATENCIES, BATCH_SIZE - 1)); // one lookup short of a batch event
+
+        verifyLoggedEvents(new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES));
+
+        log(106, Arrays.copyOfRange(LATENCIES, BATCH_SIZE - 1, BATCH_SIZE));
+
+        mEvCaptor = ArgumentCaptor.forClass(DnsEvent.class); // reset argument captor
+        verifyLoggedEvents(
+            new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
+            new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES));
+    }
+
+    public void testSeveralBatches() throws Exception {
+        log(105, LATENCIES);
+        log(106, LATENCIES);
+        log(105, LATENCIES);
+        log(107, LATENCIES);
+
+        verifyLoggedEvents(
+            new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
+            new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES),
+            new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
+            new DnsEvent(107, EVENT_TYPES, RETURN_CODES, LATENCIES));
+    }
+
+    public void testBatchAndNetworkLost() throws Exception {
+        byte[] eventTypes = Arrays.copyOf(EVENT_TYPES, 20);
+        byte[] returnCodes = Arrays.copyOf(RETURN_CODES, 20);
+        int[] latencies = Arrays.copyOf(LATENCIES, 20);
+
+        log(105, LATENCIES);
+        log(105, latencies);
+        mCallbackCaptor.getValue().onLost(new Network(105));
+        log(105, LATENCIES);
+
+        verifyLoggedEvents(
+            new DnsEvent(105, eventTypes, returnCodes, latencies),
+            new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
+            new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES));
+    }
+
+    public void testConcurrentBatchesAndDumps() throws Exception {
+        final long stop = System.currentTimeMillis() + 100;
+        final PrintWriter pw = new PrintWriter(new FileOutputStream("/dev/null"));
+        new Thread() {
+            public void run() {
+                while (System.currentTimeMillis() < stop) {
+                    mNetdEventListenerService.dump(pw);
+                }
+            }
+        }.start();
+
+        logAsync(105, LATENCIES);
+        logAsync(106, LATENCIES);
+        logAsync(107, LATENCIES);
+
+        verifyLoggedEvents(500,
+            new DnsEvent(105, EVENT_TYPES, RETURN_CODES, LATENCIES),
+            new DnsEvent(106, EVENT_TYPES, RETURN_CODES, LATENCIES),
+            new DnsEvent(107, EVENT_TYPES, RETURN_CODES, LATENCIES));
+    }
+
+    public void testConcurrentBatchesAndNetworkLoss() throws Exception {
+        logAsync(105, LATENCIES);
+        Thread.sleep(10L);
+        // call onLost() asynchronously to logAsync's onDnsEvent() calls.
+        mCallbackCaptor.getValue().onLost(new Network(105));
+
+        // do not verify unpredictable batch
+        verify(mLog, timeout(500).times(1)).log(any());
+    }
+
+    void log(int netId, int[] latencies) {
+        for (int l : latencies) {
+            mNetdEventListenerService.onDnsEvent(netId, EVENT_TYPE, RETURN_CODE, l);
+        }
+    }
+
+    void logAsync(int netId, int[] latencies) {
+        new Thread() {
+            public void run() {
+                log(netId, latencies);
+            }
+        }.start();
+    }
+
+    void verifyLoggedEvents(DnsEvent... expected) {
+        verifyLoggedEvents(0, expected);
+    }
+
+    void verifyLoggedEvents(int wait, DnsEvent... expectedEvents) {
+        verify(mLog, timeout(wait).times(expectedEvents.length)).log(mEvCaptor.capture());
+        for (DnsEvent got : mEvCaptor.getAllValues()) {
+            OptionalInt index = IntStream.range(0, expectedEvents.length)
+                    .filter(i -> eventsEqual(expectedEvents[i], got))
+                    .findFirst();
+            // Don't match same expected event more than once.
+            index.ifPresent(i -> expectedEvents[i] = null);
+            assertTrue(index.isPresent());
+        }
+    }
+
+    /** equality function for DnsEvent to avoid overriding equals() and hashCode(). */
+    static boolean eventsEqual(DnsEvent expected, DnsEvent got) {
+        return (expected == got) || ((expected != null) && (got != null)
+                && (expected.netId == got.netId)
+                && Arrays.equals(expected.eventTypes, got.eventTypes)
+                && Arrays.equals(expected.returnCodes, got.returnCodes)
+                && Arrays.equals(expected.latenciesMs, got.latenciesMs));
+    }
+}
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 3a2e946..2d96bff 100644
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DevicePolicyManagerTest.java
@@ -27,7 +27,6 @@
 import android.content.pm.PackageManager;
 import android.net.wifi.WifiInfo;
 import android.os.Build.VERSION_CODES;
-import android.os.Build;
 import android.os.Bundle;
 import android.os.Process;
 import android.os.UserHandle;
@@ -42,7 +41,6 @@
 import com.android.server.LocalServices;
 import com.android.server.SystemService;
 
-import org.mockito.ArgumentCaptor;
 import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
@@ -62,7 +60,6 @@
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.validateMockitoUsage;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
@@ -454,8 +451,7 @@
                 .thenReturn(true);
 
         dpm.removeActiveAdmin(admin1);
-
-        assertTrue(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
+        assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE));
     }
 
     /**
@@ -479,8 +475,7 @@
         mContext.binder.callingUid = 1234567;
 
         dpms.removeActiveAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE);
-
-        assertTrue(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
+        assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE));
 
         // TODO DO Still can't be removed in this case.
     }
@@ -510,28 +505,18 @@
         mContext.callerPermissions.clear();
         dpm.removeActiveAdmin(admin1);
 
-        final ArgumentCaptor<BroadcastReceiver> brCap =
-                ArgumentCaptor.forClass(BroadcastReceiver.class);
-
-        // Is removing now, but not removed yet.
-        assertTrue(dpm.isAdminActive(admin1));
-        assertTrue(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
-
         verify(mContext.spiedContext).sendOrderedBroadcastAsUser(
                 MockUtils.checkIntentAction(
                         DeviceAdminReceiver.ACTION_DEVICE_ADMIN_DISABLED),
                 MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE),
                 isNull(String.class),
-                brCap.capture(),
+                any(BroadcastReceiver.class),
                 eq(dpms.mHandler),
                 eq(Activity.RESULT_OK),
                 isNull(String.class),
                 isNull(Bundle.class));
 
-        brCap.getValue().onReceive(mContext, null);
-
-        assertFalse(dpm.isAdminActive(admin1));
-        assertFalse(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
+        assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE));
 
         // Again broadcast from saveSettingsLocked().
         verify(mContext.spiedContext, times(2)).sendBroadcastAsUser(
@@ -853,9 +838,7 @@
                 MockUtils.checkUserRestrictions()
         );
 
-        assertTrue(dpm.isAdminActive(admin1));
-        assertTrue(dpm.isRemovingAdmin(admin1, UserHandle.USER_SYSTEM));
-
+        assertFalse(dpm.isAdminActiveAsUser(admin1, UserHandle.USER_SYSTEM));
         // TODO Check other calls.
     }
 
@@ -945,7 +928,7 @@
 
         // Check
         assertFalse(dpm.isProfileOwnerApp(admin1.getPackageName()));
-        assertTrue(dpm.isRemovingAdmin(admin1, DpmMockContext.CALLER_USER_HANDLE));
+        assertFalse(dpm.isAdminActiveAsUser(admin1, DpmMockContext.CALLER_USER_HANDLE));
     }
 
     public void testSetProfileOwner_failures() throws Exception {
@@ -1477,7 +1460,7 @@
 
         // Remove PO.
         dpm.clearProfileOwner(admin1);
-
+        dpm.setActiveAdmin(admin1, false);
         // Test 4, Caller is DO now.
         assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM));
 
@@ -1526,6 +1509,7 @@
 
         // Remove PO and add DO.
         dpm.clearProfileOwner(admin1);
+        dpm.setActiveAdmin(admin1, false);
         assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM));
 
         // admin1 is DO.
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java
index f6e35f5..0783afc 100644
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmMockContext.java
@@ -527,6 +527,7 @@
             int initialCode, String initialData, Bundle initialExtras) {
         spiedContext.sendOrderedBroadcastAsUser(intent, user, receiverPermission, resultReceiver,
                 scheduler, initialCode, initialData, initialExtras);
+        resultReceiver.onReceive(spiedContext, intent);
     }
 
     @Override
diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/DpmTestBase.java b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmTestBase.java
index c80ca6c..b4b74b3 100644
--- a/services/tests/servicestests/src/com/android/server/devicepolicy/DpmTestBase.java
+++ b/services/tests/servicestests/src/com/android/server/devicepolicy/DpmTestBase.java
@@ -131,7 +131,7 @@
 
         doReturn(ai).when(mMockContext.ipackageManager).getApplicationInfo(
                 eq(admin.getPackageName()),
-                eq(PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS),
+                anyInt(),
                 eq(UserHandle.getUserId(packageUid)));
 
         // Set up queryBroadcastReceivers().
diff --git a/services/tests/servicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java b/services/tests/servicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java
index 83a59fd..d51f2d8 100644
--- a/services/tests/servicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java
+++ b/services/tests/servicestests/src/com/android/server/notification/BuzzBeepBlinkTest.java
@@ -76,6 +76,7 @@
         mService.setVibrator(mVibrator);
         mService.setSystemReady(true);
         mService.setHandler(mHandler);
+        mService.setSystemNotificationSound("beep!");
     }
 
     //
diff --git a/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java
new file mode 100644
index 0000000..792f300
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/pm/BaseShortcutManagerTest.java
@@ -0,0 +1,1952 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.cloneShortcutList;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.annotation.NonNull;
+import android.annotation.UserIdInt;
+import android.app.Activity;
+import android.app.ActivityManager;
+import android.app.ActivityManagerInternal;
+import android.app.IUidObserver;
+import android.app.usage.UsageStatsManagerInternal;
+import android.content.ActivityNotFoundException;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.ILauncherApps;
+import android.content.pm.LauncherApps;
+import android.content.pm.LauncherApps.ShortcutQuery;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManagerInternal;
+import android.content.pm.ResolveInfo;
+import android.content.pm.ShortcutInfo;
+import android.content.pm.ShortcutManager;
+import android.content.pm.ShortcutServiceInternal;
+import android.content.pm.Signature;
+import android.content.pm.UserInfo;
+import android.content.res.Resources;
+import android.content.res.XmlResourceParser;
+import android.graphics.drawable.Icon;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.FileUtils;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.PersistableBundle;
+import android.os.Process;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.test.InstrumentationTestCase;
+import android.test.mock.MockContext;
+import android.util.Log;
+import android.util.Pair;
+
+import com.android.internal.util.Preconditions;
+import com.android.server.LocalServices;
+import com.android.server.SystemService;
+import com.android.server.pm.LauncherAppsService.LauncherAppsImpl;
+import com.android.server.pm.ShortcutUser.PackageWithUser;
+
+import org.junit.Assert;
+import org.mockito.ArgumentCaptor;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.BiFunction;
+import java.util.function.BiPredicate;
+import java.util.function.Consumer;
+import java.util.function.Function;
+
+public abstract class BaseShortcutManagerTest extends InstrumentationTestCase {
+    protected static final String TAG = "ShortcutManagerTest";
+
+    protected static final boolean DUMP_IN_TEARDOWN = false; // DO NOT SUBMIT WITH true
+
+    /**
+     * Whether to enable dump or not.  Should be only true when debugging to avoid bugs where
+     * dump affecting the behavior.
+     */
+    protected static final boolean ENABLE_DUMP = false // DO NOT SUBMIT WITH true
+            || DUMP_IN_TEARDOWN || ShortcutService.DEBUG;
+
+    protected static final String[] EMPTY_STRINGS = new String[0]; // Just for readability.
+
+    protected static final String MAIN_ACTIVITY_CLASS = "MainActivity";
+
+    // public for mockito
+    public class BaseContext extends MockContext {
+        @Override
+        public Object getSystemService(String name) {
+            switch (name) {
+                case Context.USER_SERVICE:
+                    return mMockUserManager;
+            }
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public String getSystemServiceName(Class<?> serviceClass) {
+            return getTestContext().getSystemServiceName(serviceClass);
+        }
+
+        @Override
+        public PackageManager getPackageManager() {
+            return mMockPackageManager;
+        }
+
+        @Override
+        public Resources getResources() {
+            return getTestContext().getResources();
+        }
+
+        @Override
+        public Intent registerReceiverAsUser(BroadcastReceiver receiver, UserHandle user,
+                IntentFilter filter, String broadcastPermission, Handler scheduler) {
+            // ignore.
+            return null;
+        }
+
+        @Override
+        public void unregisterReceiver(BroadcastReceiver receiver) {
+            // ignore.
+        }
+    }
+
+    /** Context used in the client side */
+    public class ClientContext extends BaseContext {
+        @Override
+        public String getPackageName() {
+            return mInjectedClientPackage;
+        }
+
+        @Override
+        public int getUserId() {
+            return getCallingUserId();
+        }
+    }
+
+    /** Context used in the service side */
+    public class ServiceContext extends BaseContext {
+        long injectClearCallingIdentity() {
+            final int prevCallingUid = mInjectedCallingUid;
+            mInjectedCallingUid = Process.SYSTEM_UID;
+            return prevCallingUid;
+        }
+
+        void injectRestoreCallingIdentity(long token) {
+            mInjectedCallingUid = (int) token;
+        }
+
+        @Override
+        public int getUserId() {
+            return UserHandle.USER_SYSTEM;
+        }
+
+        public PackageInfo injectGetActivitiesWithMetadata(
+                String packageName, @UserIdInt int userId) {
+            return BaseShortcutManagerTest.this.injectGetActivitiesWithMetadata(packageName, userId);
+        }
+
+        public XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
+            return BaseShortcutManagerTest.this.injectXmlMetaData(activityInfo, key);
+        }
+    }
+
+    /** ShortcutService with injection override methods. */
+    protected final class ShortcutServiceTestable extends ShortcutService {
+        final ServiceContext mContext;
+        IUidObserver mUidObserver;
+
+        public ShortcutServiceTestable(ServiceContext context, Looper looper) {
+            super(context, looper, /* onyForPackageManagerApis */ false);
+            mContext = context;
+        }
+
+        @Override
+        public String injectGetLocaleTagsForUser(@UserIdInt int userId) {
+            return mInjectedLocale.toLanguageTag();
+        }
+
+        @Override
+        boolean injectShouldPerformVerification() {
+            return true; // Always verify during unit tests.
+        }
+
+        @Override
+        String injectShortcutManagerConstants() {
+            return ConfigConstants.KEY_RESET_INTERVAL_SEC + "=" + (INTERVAL / 1000) + ","
+                    + ConfigConstants.KEY_MAX_SHORTCUTS + "=" + MAX_SHORTCUTS + ","
+                    + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "="
+                    + MAX_UPDATES_PER_INTERVAL + ","
+                    + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=" + MAX_ICON_DIMENSION + ","
+                    + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "="
+                    + MAX_ICON_DIMENSION_LOWRAM + ","
+                    + ConfigConstants.KEY_ICON_FORMAT + "=PNG,"
+                    + ConfigConstants.KEY_ICON_QUALITY + "=100";
+        }
+
+        @Override
+        long injectClearCallingIdentity() {
+            return mContext.injectClearCallingIdentity();
+        }
+
+        @Override
+        void injectRestoreCallingIdentity(long token) {
+            mContext.injectRestoreCallingIdentity(token);
+        }
+
+        @Override
+        int injectDipToPixel(int dip) {
+            return dip;
+        }
+
+        @Override
+        long injectCurrentTimeMillis() {
+            return mInjectedCurrentTimeMillis;
+        }
+
+        @Override
+        long injectElapsedRealtime() {
+            // TODO This should be kept separately from mInjectedCurrentTimeMillis, since
+            // this should increase even if we rewind mInjectedCurrentTimeMillis in some tests.
+            return mInjectedCurrentTimeMillis - START_TIME;
+        }
+
+        @Override
+        int injectBinderCallingUid() {
+            return mInjectedCallingUid;
+        }
+
+        @Override
+        int injectGetPackageUid(String packageName, int userId) {
+            return getInjectedPackageInfo(packageName, userId, false).applicationInfo.uid;
+        }
+
+        @Override
+        File injectSystemDataPath() {
+            return new File(mInjectedFilePathRoot, "system");
+        }
+
+        @Override
+        File injectUserDataPath(@UserIdInt int userId) {
+            return new File(mInjectedFilePathRoot, "user-" + userId);
+        }
+
+        @Override
+        void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) {
+            // Can't check
+        }
+
+        @Override
+        boolean injectIsLowRamDevice() {
+            return mInjectedIsLowRamDevice;
+        }
+
+        @Override
+        void injectRegisterUidObserver(IUidObserver observer, int which) {
+            mUidObserver = observer;
+        }
+
+        @Override
+        boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId) {
+            return mDefaultLauncherChecker.test(callingPackage, userId);
+        }
+
+        @Override
+        PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId,
+                boolean getSignatures) {
+            return getInjectedPackageInfo(packageName, userId, getSignatures);
+        }
+
+        @Override
+        ApplicationInfo injectApplicationInfoWithUninstalled(
+                String packageName, @UserIdInt int userId) {
+            PackageInfo pi = injectPackageInfoWithUninstalled(
+                    packageName, userId, /* getSignatures= */ false);
+            return pi != null ? pi.applicationInfo : null;
+        }
+
+        @Override
+        List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) {
+            return BaseShortcutManagerTest.this.getInstalledPackagesWithUninstalled(userId);
+        }
+
+        @Override
+        ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled(ComponentName activity,
+                @UserIdInt int userId) {
+            final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
+                    activity.getPackageName(), userId);
+            if (pi == null || pi.activities == null) {
+                return null;
+            }
+            for (ActivityInfo ai : pi.activities) {
+                if (!mEnabledActivityChecker.test(ai.getComponentName(), userId)) {
+                    continue;
+                }
+                if (activity.equals(ai.getComponentName())) {
+                    return ai;
+                }
+            }
+            return null;
+        }
+
+        @Override
+        boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) {
+            if (!mEnabledActivityChecker.test(activity, userId)) {
+                return false;
+            }
+            return mMainActivityChecker.test(activity, userId);
+        }
+
+        @Override
+        List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) {
+            final PackageInfo pi = mContext.injectGetActivitiesWithMetadata(
+                    packageName, userId);
+            if (pi == null || pi.activities == null) {
+                return null;
+            }
+            final ArrayList<ResolveInfo> ret = new ArrayList<>(pi.activities.length);
+            for (int i = 0; i < pi.activities.length; i++) {
+                if (!mEnabledActivityChecker.test(pi.activities[i].getComponentName(), userId)) {
+                    continue;
+                }
+                final ResolveInfo ri = new ResolveInfo();
+                ri.activityInfo = pi.activities[i];
+                ret.add(ri);
+            }
+
+            return ret;
+        }
+
+        @Override
+        ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) {
+            return mMainActivityFetcher.apply(packageName, userId);
+        }
+
+        @Override
+        boolean injectIsActivityEnabledAndExported(ComponentName activity, @UserIdInt int userId) {
+            return mEnabledActivityChecker.test(activity, userId);
+        }
+
+        @Override
+        XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
+            return mContext.injectXmlMetaData(activityInfo, key);
+        }
+
+        @Override
+        void injectPostToHandler(Runnable r) {
+            runOnHandler(r);
+        }
+
+        @Override
+        void injectRunOnNewThread(Runnable r) {
+            runOnHandler(r);
+        }
+
+        @Override
+        void injectEnforceCallingPermission(String permission, String message) {
+            if (!mCallerPermissions.contains(permission)) {
+                throw new SecurityException("Missing permission: " + permission);
+            }
+        }
+
+        @Override
+        boolean injectIsSafeModeEnabled() {
+            return mSafeMode;
+        }
+
+        @Override
+        String injectBuildFingerprint() {
+            return mInjectedBuildFingerprint;
+        }
+
+        @Override
+        void wtf(String message, Throwable th) {
+            // During tests, WTF is fatal.
+            fail(message + "  exception: " + th + "\n" + Log.getStackTraceString(th));
+        }
+    }
+
+    /** ShortcutManager with injection override methods. */
+    protected class ShortcutManagerTestable extends ShortcutManager {
+        public ShortcutManagerTestable(Context context, ShortcutServiceTestable service) {
+            super(context, service);
+        }
+
+        @Override
+        protected int injectMyUserId() {
+            return UserHandle.getUserId(mInjectedCallingUid);
+        }
+
+        @Override
+        public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
+            // Note to simulate the binder RPC, we need to clone the incoming arguments.
+            // Otherwise bad things will happen because they're mutable.
+            return super.setDynamicShortcuts(cloneShortcutList(shortcutInfoList));
+        }
+
+        @Override
+        public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
+            // Note to simulate the binder RPC, we need to clone the incoming arguments.
+            return super.addDynamicShortcuts(cloneShortcutList(shortcutInfoList));
+        }
+
+        @Override
+        public boolean updateShortcuts(List<ShortcutInfo> shortcutInfoList) {
+            // Note to simulate the binder RPC, we need to clone the incoming arguments.
+            return super.updateShortcuts(cloneShortcutList(shortcutInfoList));
+        }
+    }
+
+    protected class LauncherAppImplTestable extends LauncherAppsImpl {
+        final ServiceContext mContext;
+
+        public LauncherAppImplTestable(ServiceContext context) {
+            super(context);
+            mContext = context;
+        }
+
+        @Override
+        public void verifyCallingPackage(String callingPackage) {
+            // SKIP
+        }
+
+        @Override
+        void postToPackageMonitorHandler(Runnable r) {
+            runOnHandler(r);
+        }
+
+        @Override
+        int injectBinderCallingUid() {
+            return mInjectedCallingUid;
+        }
+
+        @Override
+        long injectClearCallingIdentity() {
+            final int prevCallingUid = mInjectedCallingUid;
+            mInjectedCallingUid = Process.SYSTEM_UID;
+            return prevCallingUid;
+        }
+
+        @Override
+        void injectRestoreCallingIdentity(long token) {
+            mInjectedCallingUid = (int) token;
+        }
+    }
+
+    protected class LauncherAppsTestable extends LauncherApps {
+        public LauncherAppsTestable(Context context, ILauncherApps service) {
+            super(context, service);
+        }
+    }
+
+    public static class ShortcutActivity extends Activity {
+    }
+
+    public static class ShortcutActivity2 extends Activity {
+    }
+
+    public static class ShortcutActivity3 extends Activity {
+    }
+
+    protected Looper mLooper;
+    protected Handler mHandler;
+
+    protected ServiceContext mServiceContext;
+    protected ClientContext mClientContext;
+
+    protected ShortcutServiceTestable mService;
+    protected ShortcutManagerTestable mManager;
+    protected ShortcutServiceInternal mInternal;
+
+    protected LauncherAppImplTestable mLauncherAppImpl;
+
+    // LauncherApps has per-instace state, so we need a differnt instance for each launcher.
+    protected final Map<Pair<Integer, String>, LauncherAppsTestable>
+            mLauncherAppsMap = new HashMap<>();
+    protected LauncherAppsTestable mLauncherApps; // Current one
+
+    protected File mInjectedFilePathRoot;
+
+    protected boolean mSafeMode;
+
+    protected long mInjectedCurrentTimeMillis;
+
+    protected boolean mInjectedIsLowRamDevice;
+
+    protected Locale mInjectedLocale = Locale.ENGLISH;
+
+    protected int mInjectedCallingUid;
+    protected String mInjectedClientPackage;
+
+    protected Map<String, PackageInfo> mInjectedPackages;
+
+    protected Set<PackageWithUser> mUninstalledPackages;
+    protected Set<String> mSystemPackages;
+
+    protected PackageManager mMockPackageManager;
+    protected PackageManagerInternal mMockPackageManagerInternal;
+    protected UserManager mMockUserManager;
+    protected UsageStatsManagerInternal mMockUsageStatsManagerInternal;
+    protected ActivityManagerInternal mMockActivityManagerInternal;
+
+    protected static final String CALLING_PACKAGE_1 = "com.android.test.1";
+    protected static final int CALLING_UID_1 = 10001;
+
+    protected static final String CALLING_PACKAGE_2 = "com.android.test.2";
+    protected static final int CALLING_UID_2 = 10002;
+
+    protected static final String CALLING_PACKAGE_3 = "com.android.test.3";
+    protected static final int CALLING_UID_3 = 10003;
+
+    protected static final String CALLING_PACKAGE_4 = "com.android.test.4";
+    protected static final int CALLING_UID_4 = 10004;
+
+    protected static final String LAUNCHER_1 = "com.android.launcher.1";
+    protected static final int LAUNCHER_UID_1 = 10011;
+
+    protected static final String LAUNCHER_2 = "com.android.launcher.2";
+    protected static final int LAUNCHER_UID_2 = 10012;
+
+    protected static final String LAUNCHER_3 = "com.android.launcher.3";
+    protected static final int LAUNCHER_UID_3 = 10013;
+
+    protected static final String LAUNCHER_4 = "com.android.launcher.4";
+    protected static final int LAUNCHER_UID_4 = 10014;
+
+    protected static final int USER_0 = UserHandle.USER_SYSTEM;
+    protected static final int USER_10 = 10;
+    protected static final int USER_11 = 11;
+    protected static final int USER_P0 = 20; // profile of user 0
+
+    protected static final UserHandle HANDLE_USER_0 = UserHandle.of(USER_0);
+    protected static final UserHandle HANDLE_USER_10 = UserHandle.of(USER_10);
+    protected static final UserHandle HANDLE_USER_11 = UserHandle.of(USER_11);
+    protected static final UserHandle HANDLE_USER_P0 = UserHandle.of(USER_P0);
+
+    protected static final UserInfo USER_INFO_0 = withProfileGroupId(
+            new UserInfo(USER_0, "user0",
+                    UserInfo.FLAG_ADMIN | UserInfo.FLAG_PRIMARY | UserInfo.FLAG_INITIALIZED), 10);
+
+    protected static final UserInfo USER_INFO_10 =
+            new UserInfo(USER_10, "user10", UserInfo.FLAG_INITIALIZED);
+
+    protected static final UserInfo USER_INFO_11 =
+            new UserInfo(USER_11, "user11", UserInfo.FLAG_INITIALIZED);
+
+    protected static final UserInfo USER_INFO_P0 = withProfileGroupId(
+            new UserInfo(USER_P0, "userP0",
+                    UserInfo.FLAG_MANAGED_PROFILE), 10);
+
+    protected BiPredicate<String, Integer> mDefaultLauncherChecker =
+            (callingPackage, userId) ->
+            LAUNCHER_1.equals(callingPackage) || LAUNCHER_2.equals(callingPackage)
+            || LAUNCHER_3.equals(callingPackage) || LAUNCHER_4.equals(callingPackage);
+
+    protected BiPredicate<ComponentName, Integer> mMainActivityChecker =
+            (activity, userId) -> true;
+
+    protected BiFunction<String, Integer, ComponentName> mMainActivityFetcher =
+            (packageName, userId) -> new ComponentName(packageName, MAIN_ACTIVITY_CLASS);
+
+    protected BiPredicate<ComponentName, Integer> mEnabledActivityChecker
+            = (activity, userId) -> true; // all activities are enabled.
+
+    protected static final long START_TIME = 1440000000101L;
+
+    protected static final long INTERVAL = 10000;
+
+    protected static final int MAX_SHORTCUTS = 10;
+
+    protected static final int MAX_UPDATES_PER_INTERVAL = 3;
+
+    protected static final int MAX_ICON_DIMENSION = 128;
+
+    protected static final int MAX_ICON_DIMENSION_LOWRAM = 32;
+
+    protected static final ShortcutQuery QUERY_ALL = new ShortcutQuery();
+
+    protected final ArrayList<String> mCallerPermissions = new ArrayList<>();
+
+    protected final HashMap<String, LinkedHashMap<ComponentName, Integer>> mActivityMetadataResId
+            = new HashMap<>();
+
+    protected final Map<Integer, UserInfo> mUserInfos = new HashMap<>();
+    protected final Map<Integer, Boolean> mRunningUsers = new HashMap<>();
+    protected final Map<Integer, Boolean> mUnlockedUsers = new HashMap<>();
+
+    protected static final String PACKAGE_SYSTEM_LAUNCHER = "com.android.systemlauncher";
+    protected static final String PACKAGE_SYSTEM_LAUNCHER_NAME = "systemlauncher_name";
+    protected static final int PACKAGE_SYSTEM_LAUNCHER_PRIORITY = 0;
+
+    protected static final String PACKAGE_FALLBACK_LAUNCHER = "com.android.settings";
+    protected static final String PACKAGE_FALLBACK_LAUNCHER_NAME = "fallback";
+    protected static final int PACKAGE_FALLBACK_LAUNCHER_PRIORITY = -999;
+
+    protected String mInjectedBuildFingerprint = "build1";
+
+    static {
+        QUERY_ALL.setQueryFlags(
+                ShortcutQuery.FLAG_GET_ALL_KINDS);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        mLooper = Looper.getMainLooper();
+        mHandler = new Handler(mLooper);
+
+        mServiceContext = spy(new ServiceContext());
+        mClientContext = new ClientContext();
+
+        mMockPackageManager = mock(PackageManager.class);
+        mMockPackageManagerInternal = mock(PackageManagerInternal.class);
+        mMockUserManager = mock(UserManager.class);
+        mMockUsageStatsManagerInternal = mock(UsageStatsManagerInternal.class);
+        mMockActivityManagerInternal = mock(ActivityManagerInternal.class);
+
+        LocalServices.removeServiceForTest(PackageManagerInternal.class);
+        LocalServices.addService(PackageManagerInternal.class, mMockPackageManagerInternal);
+        LocalServices.removeServiceForTest(UsageStatsManagerInternal.class);
+        LocalServices.addService(UsageStatsManagerInternal.class, mMockUsageStatsManagerInternal);
+        LocalServices.removeServiceForTest(ActivityManagerInternal.class);
+        LocalServices.addService(ActivityManagerInternal.class, mMockActivityManagerInternal);
+
+        // Prepare injection values.
+
+        mInjectedCurrentTimeMillis = START_TIME;
+
+        mInjectedPackages = new HashMap<>();
+        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1);
+        addPackage(CALLING_PACKAGE_2, CALLING_UID_2, 2);
+        addPackage(CALLING_PACKAGE_3, CALLING_UID_3, 3);
+        addPackage(CALLING_PACKAGE_4, CALLING_UID_4, 10);
+        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4);
+        addPackage(LAUNCHER_2, LAUNCHER_UID_2, 5);
+        addPackage(LAUNCHER_3, LAUNCHER_UID_3, 6);
+        addPackage(LAUNCHER_4, LAUNCHER_UID_4, 10);
+
+        // CALLING_PACKAGE_3 / LAUNCHER_3 are not backup target.
+        updatePackageInfo(CALLING_PACKAGE_3,
+                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
+        updatePackageInfo(LAUNCHER_3,
+                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
+
+        mUninstalledPackages = new HashSet<>();
+        mSystemPackages = new HashSet<>();
+
+        mInjectedFilePathRoot = new File(getTestContext().getCacheDir(), "test-files");
+
+        deleteAllSavedFiles();
+
+        // Set up users.
+        when(mMockUserManager.getUserInfo(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
+                inv -> mUserInfos.get((Integer) inv.getArguments()[0])));
+
+        mUserInfos.put(USER_0, USER_INFO_0);
+        mUserInfos.put(USER_10, USER_INFO_10);
+        mUserInfos.put(USER_11, USER_INFO_11);
+        mUserInfos.put(USER_P0, USER_INFO_P0);
+
+        // Set up isUserRunning and isUserUnlocked.
+        when(mMockUserManager.isUserRunning(anyInt())).thenAnswer(new AnswerWithSystemCheck<>(
+                        inv -> b(mRunningUsers.get((Integer) inv.getArguments()[0]))));
+
+        when(mMockUserManager.isUserUnlocked(anyInt()))
+                .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
+                    final int userId = (Integer) inv.getArguments()[0];
+                    return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
+                }));
+        // isUserUnlockingOrUnlocked() return the same value as isUserUnlocked().
+        when(mMockUserManager.isUserUnlockingOrUnlocked(anyInt()))
+                .thenAnswer(new AnswerWithSystemCheck<>(inv -> {
+                    final int userId = (Integer) inv.getArguments()[0];
+                    return b(mRunningUsers.get(userId)) && b(mUnlockedUsers.get(userId));
+                }));
+
+        when(mMockActivityManagerInternal.getUidProcessState(anyInt())).thenReturn(
+                ActivityManager.PROCESS_STATE_CACHED_EMPTY);
+
+        // User 0 and P0 are always running
+        mRunningUsers.put(USER_0, true);
+        mRunningUsers.put(USER_10, false);
+        mRunningUsers.put(USER_11, false);
+        mRunningUsers.put(USER_P0, true);
+
+        // Unlock all users by default.
+        mUnlockedUsers.put(USER_0, true);
+        mUnlockedUsers.put(USER_10, true);
+        mUnlockedUsers.put(USER_11, true);
+        mUnlockedUsers.put(USER_P0, true);
+
+        // Set up resources
+        setUpAppResources();
+
+        // Start the service.
+        initService();
+        setCaller(CALLING_PACKAGE_1);
+    }
+
+    private static boolean b(Boolean value) {
+        return (value != null && value);
+    }
+
+    /**
+     * Returns a boolean but also checks if the current UID is SYSTEM_UID.
+     */
+    protected class AnswerWithSystemCheck<T> implements Answer<T> {
+        private final Function<InvocationOnMock, T> mChecker;
+
+        public AnswerWithSystemCheck(Function<InvocationOnMock, T> checker) {
+            mChecker = checker;
+        }
+
+        @Override
+        public T answer(InvocationOnMock invocation) throws Throwable {
+            assertEquals("Must be called on SYSTEM UID.",
+                    Process.SYSTEM_UID, mInjectedCallingUid);
+            return mChecker.apply(invocation);
+        }
+    }
+
+    protected void setUpAppResources() throws Exception {
+        setUpAppResources(/* offset = */ 0);
+    }
+
+    protected void setUpAppResources(int ressIdOffset) throws Exception {
+        // ressIdOffset is used to adjust resource IDs to emulate the case where an updated app
+        // has resource IDs changed.
+
+        doAnswer(pmInvocation -> {
+            assertEquals(Process.SYSTEM_UID, mInjectedCallingUid);
+
+            final String packageName = (String) pmInvocation.getArguments()[0];
+            final int userId = (Integer) pmInvocation.getArguments()[1];
+
+            final Resources res = mock(Resources.class);
+
+            doAnswer(resInvocation -> {
+                final int argResId = (Integer) resInvocation.getArguments()[0];
+
+                return "string-" + packageName + "-user:" + userId + "-res:" + argResId
+                        + "/" + mInjectedLocale;
+            }).when(res).getString(anyInt());
+
+            doAnswer(resInvocation -> {
+                final int resId = (Integer) resInvocation.getArguments()[0];
+
+                // Always use the "string" resource type.  The type doesn't matter during the test.
+                return packageName + ":string/r" + resId;
+            }).when(res).getResourceName(anyInt());
+
+            doAnswer(resInvocation -> {
+                final String argResName = (String) resInvocation.getArguments()[0];
+                final String argType = (String) resInvocation.getArguments()[1];
+                final String argPackageName = (String) resInvocation.getArguments()[2];
+
+                // See the above code.  getResourceName() will just use "r" + res ID as the entry
+                // name.
+                String entryName = argResName;
+                if (entryName.contains("/")) {
+                    entryName = ShortcutInfo.getResourceEntryName(entryName);
+                }
+                return Integer.parseInt(entryName.substring(1)) + ressIdOffset;
+            }).when(res).getIdentifier(anyString(), anyString(), anyString());
+            return res;
+        }).when(mMockPackageManager).getResourcesForApplicationAsUser(anyString(), anyInt());
+    }
+
+    protected static UserInfo withProfileGroupId(UserInfo in, int groupId) {
+        in.profileGroupId = groupId;
+        return in;
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (DUMP_IN_TEARDOWN) dumpsysOnLogcat("Teardown");
+
+        shutdownServices();
+
+        super.tearDown();
+    }
+
+    protected Context getTestContext() {
+        return getInstrumentation().getContext();
+    }
+
+    protected ShortcutManager getManager() {
+        return mManager;
+    }
+
+    protected void deleteAllSavedFiles() {
+        // Empty the data directory.
+        if (mInjectedFilePathRoot.exists()) {
+            Assert.assertTrue("failed to delete dir",
+                    FileUtils.deleteContents(mInjectedFilePathRoot));
+        }
+        mInjectedFilePathRoot.mkdirs();
+    }
+
+    /** (Re-) init the manager and the service. */
+    protected void initService() {
+        shutdownServices();
+
+        LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
+
+        // Instantiate targets.
+        mService = new ShortcutServiceTestable(mServiceContext, mLooper);
+        mManager = new ShortcutManagerTestable(mClientContext, mService);
+
+        mInternal = LocalServices.getService(ShortcutServiceInternal.class);
+
+        mLauncherAppImpl = new LauncherAppImplTestable(mServiceContext);
+        mLauncherApps = null;
+        mLauncherAppsMap.clear();
+
+        // Send boot sequence events.
+        mService.onBootPhase(SystemService.PHASE_LOCK_SETTINGS_READY);
+
+        mService.onBootPhase(SystemService.PHASE_BOOT_COMPLETED);
+    }
+
+    protected void shutdownServices() {
+        if (mService != null) {
+            // Flush all the unsaved data from the previous instance.
+            mService.saveDirtyInfo();
+
+            // Make sure everything is consistent.
+            mService.verifyStates();
+        }
+        LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
+
+        mService = null;
+        mManager = null;
+        mInternal = null;
+        mLauncherAppImpl = null;
+        mLauncherApps = null;
+        mLauncherAppsMap.clear();
+    }
+
+    protected void runOnHandler(Runnable r) {
+        final long token = mServiceContext.injectClearCallingIdentity();
+        try {
+            r.run();
+        } finally {
+            mServiceContext.injectRestoreCallingIdentity(token);
+        }
+    }
+
+    protected void addPackage(String packageName, int uid, int version) {
+        addPackage(packageName, uid, version, packageName);
+    }
+
+    protected Signature[] genSignatures(String... signatures) {
+        final Signature[] sigs = new Signature[signatures.length];
+        for (int i = 0; i < signatures.length; i++){
+            sigs[i] = new Signature(signatures[i].getBytes());
+        }
+        return sigs;
+    }
+
+    protected PackageInfo genPackage(String packageName, int uid, int version, String... signatures) {
+        final PackageInfo pi = new PackageInfo();
+        pi.packageName = packageName;
+        pi.applicationInfo = new ApplicationInfo();
+        pi.applicationInfo.uid = uid;
+        pi.applicationInfo.flags = ApplicationInfo.FLAG_INSTALLED
+                | ApplicationInfo.FLAG_ALLOW_BACKUP;
+        pi.versionCode = version;
+        pi.applicationInfo.versionCode = version;
+        pi.signatures = genSignatures(signatures);
+
+        return pi;
+    }
+
+    protected void addPackage(String packageName, int uid, int version, String... signatures) {
+        mInjectedPackages.put(packageName, genPackage(packageName, uid, version, signatures));
+    }
+
+    protected void updatePackageInfo(String packageName, Consumer<PackageInfo> c) {
+        c.accept(mInjectedPackages.get(packageName));
+    }
+
+    protected void updatePackageVersion(String packageName, int increment) {
+        updatePackageInfo(packageName, pi -> {
+            pi.versionCode += increment;
+            pi.applicationInfo.versionCode += increment;
+        });
+    }
+
+    protected void updatePackageLastUpdateTime(String packageName, long increment) {
+        updatePackageInfo(packageName, pi -> {
+            pi.lastUpdateTime += increment;
+        });
+    }
+
+    protected void setPackageLastUpdateTime(String packageName, long value) {
+        updatePackageInfo(packageName, pi -> {
+            pi.lastUpdateTime = value;
+        });
+    }
+
+    protected void uninstallPackage(int userId, String packageName) {
+        if (ENABLE_DUMP) {
+            Log.v(TAG, "Unnstall package " + packageName + " / " + userId);
+        }
+        mUninstalledPackages.add(PackageWithUser.of(userId, packageName));
+    }
+
+    protected void installPackage(int userId, String packageName) {
+        if (ENABLE_DUMP) {
+            Log.v(TAG, "Install package " + packageName + " / " + userId);
+        }
+        mUninstalledPackages.remove(PackageWithUser.of(userId, packageName));
+    }
+
+    PackageInfo getInjectedPackageInfo(String packageName, @UserIdInt int userId,
+            boolean getSignatures) {
+        final PackageInfo pi = mInjectedPackages.get(packageName);
+        if (pi == null) return null;
+
+        final PackageInfo ret = new PackageInfo();
+        ret.packageName = pi.packageName;
+        ret.versionCode = pi.versionCode;
+        ret.lastUpdateTime = pi.lastUpdateTime;
+
+        ret.applicationInfo = new ApplicationInfo(pi.applicationInfo);
+        ret.applicationInfo.uid = UserHandle.getUid(userId, pi.applicationInfo.uid);
+        ret.applicationInfo.packageName = pi.packageName;
+
+        if (mUninstalledPackages.contains(PackageWithUser.of(userId, packageName))) {
+            ret.applicationInfo.flags &= ~ApplicationInfo.FLAG_INSTALLED;
+        }
+        if (mSystemPackages.contains(packageName)) {
+            ret.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
+        }
+
+        if (getSignatures) {
+            ret.signatures = pi.signatures;
+        }
+
+        return ret;
+    }
+
+    protected void addApplicationInfo(PackageInfo pi, List<ApplicationInfo> list) {
+        if (pi != null && pi.applicationInfo != null) {
+            list.add(pi.applicationInfo);
+        }
+    }
+
+    protected List<ApplicationInfo> getInstalledApplications(int userId) {
+        final ArrayList<ApplicationInfo> ret = new ArrayList<>();
+
+        addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
+        addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
+        addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
+        addApplicationInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
+        addApplicationInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
+        addApplicationInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
+        addApplicationInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
+        addApplicationInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
+
+        return ret;
+    }
+
+    private void addPackageInfo(PackageInfo pi, List<PackageInfo> list) {
+        if (pi != null) {
+            list.add(pi);
+        }
+    }
+
+    private List<PackageInfo> getInstalledPackagesWithUninstalled(int userId) {
+        final ArrayList<PackageInfo> ret = new ArrayList<>();
+
+        addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_1, userId, false), ret);
+        addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_2, userId, false), ret);
+        addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_3, userId, false), ret);
+        addPackageInfo(getInjectedPackageInfo(CALLING_PACKAGE_4, userId, false), ret);
+        addPackageInfo(getInjectedPackageInfo(LAUNCHER_1, userId, false), ret);
+        addPackageInfo(getInjectedPackageInfo(LAUNCHER_2, userId, false), ret);
+        addPackageInfo(getInjectedPackageInfo(LAUNCHER_3, userId, false), ret);
+        addPackageInfo(getInjectedPackageInfo(LAUNCHER_4, userId, false), ret);
+
+        return ret;
+    }
+
+    protected void addManifestShortcutResource(ComponentName activity, int resId) {
+        final String packageName = activity.getPackageName();
+        LinkedHashMap<ComponentName, Integer> map = mActivityMetadataResId.get(packageName);
+        if (map == null) {
+            map = new LinkedHashMap<>();
+            mActivityMetadataResId.put(packageName, map);
+        }
+        map.put(activity, resId);
+    }
+
+    protected PackageInfo injectGetActivitiesWithMetadata(String packageName, @UserIdInt int userId) {
+        final PackageInfo ret = getInjectedPackageInfo(packageName, userId,
+                /* getSignatures=*/ false);
+
+        final HashMap<ComponentName, Integer> activities = mActivityMetadataResId.get(packageName);
+        if (activities != null) {
+            final ArrayList<ActivityInfo> list = new ArrayList<>();
+
+            for (ComponentName cn : activities.keySet()) {
+                ActivityInfo ai = new ActivityInfo();
+                ai.packageName = cn.getPackageName();
+                ai.name = cn.getClassName();
+                ai.metaData = new Bundle();
+                ai.metaData.putInt(ShortcutParser.METADATA_KEY, activities.get(cn));
+                ai.applicationInfo = ret.applicationInfo;
+                list.add(ai);
+            }
+            ret.activities = list.toArray(new ActivityInfo[list.size()]);
+        }
+        return ret;
+    }
+
+    protected XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) {
+        if (!ShortcutParser.METADATA_KEY.equals(key) || activityInfo.metaData == null) {
+            return null;
+        }
+        final int resId = activityInfo.metaData.getInt(key);
+        return getTestContext().getResources().getXml(resId);
+    }
+
+    /** Replace the current calling package */
+    protected void setCaller(String packageName, int userId) {
+        mInjectedClientPackage = packageName;
+        mInjectedCallingUid =
+                Preconditions.checkNotNull(getInjectedPackageInfo(packageName, userId, false),
+                        "Unknown package").applicationInfo.uid;
+
+        // Set up LauncherApps for this caller.
+        final Pair<Integer, String> key = Pair.create(userId, packageName);
+        if (!mLauncherAppsMap.containsKey(key)) {
+            mLauncherAppsMap.put(key, new LauncherAppsTestable(mClientContext, mLauncherAppImpl));
+        }
+        mLauncherApps = mLauncherAppsMap.get(key);
+    }
+
+    protected void setCaller(String packageName) {
+        setCaller(packageName, UserHandle.USER_SYSTEM);
+    }
+
+    protected String getCallingPackage() {
+        return mInjectedClientPackage;
+    }
+
+    protected void setDefaultLauncherChecker(BiPredicate<String, Integer> p) {
+        mDefaultLauncherChecker = p;
+    }
+
+    protected void runWithCaller(String packageName, int userId, Runnable r) {
+        final String previousPackage = mInjectedClientPackage;
+        final int previousUserId = UserHandle.getUserId(mInjectedCallingUid);
+
+        setCaller(packageName, userId);
+
+        r.run();
+
+        setCaller(previousPackage, previousUserId);
+    }
+
+    protected void runWithSystemUid(Runnable r) {
+        final int origUid = mInjectedCallingUid;
+        mInjectedCallingUid = Process.SYSTEM_UID;
+        r.run();
+        mInjectedCallingUid = origUid;
+    }
+
+    protected void lookupAndFillInResourceNames(ShortcutInfo si) {
+        runWithSystemUid(() -> si.lookupAndFillInResourceNames(
+                mService.injectGetResourcesForApplicationAsUser(si.getPackage(), si.getUserId())));
+    }
+
+    protected int getCallingUserId() {
+        return UserHandle.getUserId(mInjectedCallingUid);
+    }
+
+    protected UserHandle getCallingUser() {
+        return UserHandle.of(getCallingUserId());
+    }
+
+    /** For debugging */
+    protected void dumpsysOnLogcat() {
+        dumpsysOnLogcat("");
+    }
+
+    protected void dumpsysOnLogcat(String message) {
+        dumpsysOnLogcat(message, false);
+    }
+
+    protected void dumpsysOnLogcat(String message, boolean force) {
+        if (force || !ENABLE_DUMP) return;
+
+        Log.v(TAG, "Dumping ShortcutService: " + message);
+        for (String line : dumpsys(null).split("\n")) {
+            Log.v(TAG, line);
+        }
+    }
+
+    protected String dumpCheckin() {
+        return dumpsys(new String[]{"--checkin"});
+    }
+
+    private String dumpsys(String[] args) {
+        final ArrayList<String> origPermissions = new ArrayList<>(mCallerPermissions);
+        mCallerPermissions.add(android.Manifest.permission.DUMP);
+        try {
+            final ByteArrayOutputStream out = new ByteArrayOutputStream();
+            final PrintWriter pw = new PrintWriter(out);
+            mService.dump(/* fd */ null, pw, args);
+            pw.close();
+
+            return out.toString();
+        } finally {
+            mCallerPermissions.clear();
+            mCallerPermissions.addAll(origPermissions);
+        }
+    }
+
+    /**
+     * For debugging, dump arbitrary file on logcat.
+     */
+    protected void dumpFileOnLogcat(String path) {
+        dumpFileOnLogcat(path, "");
+    }
+
+    protected void dumpFileOnLogcat(String path, String message) {
+        if (!ENABLE_DUMP) return;
+
+        Log.v(TAG, "Dumping file: " + path + " " + message);
+        final StringBuilder sb = new StringBuilder();
+        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
+            String line;
+            while ((line = br.readLine()) != null) {
+                Log.v(TAG, line);
+            }
+        } catch (Exception e) {
+            Log.e(TAG, "Couldn't read file", e);
+            fail("Exception " + e);
+        }
+    }
+
+    /**
+     * For debugging, dump the main state file on logcat.
+     */
+    protected void dumpBaseStateFile() {
+        mService.saveDirtyInfo();
+        dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
+                + "/system/" + ShortcutService.FILENAME_BASE_STATE);
+    }
+
+    /**
+     * For debugging, dump per-user state file on logcat.
+     */
+    protected void dumpUserFile(int userId) {
+        dumpUserFile(userId, "");
+    }
+
+    protected void dumpUserFile(int userId, String message) {
+        mService.saveDirtyInfo();
+        dumpFileOnLogcat(mInjectedFilePathRoot.getAbsolutePath()
+                + "/user-" + userId
+                + "/" + ShortcutService.FILENAME_USER_PACKAGES, message);
+    }
+
+    /**
+     * Make a shortcut with an ID.
+     */
+    protected ShortcutInfo makeShortcut(String id) {
+        return makeShortcut(
+                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
+    }
+
+    protected ShortcutInfo makeShortcutWithTitle(String id, String title) {
+        return makeShortcut(
+                id, title, /* activity =*/ null, /* icon =*/ null,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
+    }
+
+    /**
+     * Make a shortcut with an ID and timestamp.
+     */
+    protected ShortcutInfo makeShortcutWithTimestamp(String id, long timestamp) {
+        final ShortcutInfo s = makeShortcut(
+                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
+        s.setTimestamp(timestamp);
+        return s;
+    }
+
+    /**
+     * Make a shortcut with an ID, a timestamp and an activity component
+     */
+    protected ShortcutInfo makeShortcutWithTimestampWithActivity(String id, long timestamp,
+            ComponentName activity) {
+        final ShortcutInfo s = makeShortcut(
+                id, "Title-" + id, activity, /* icon =*/ null,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
+        s.setTimestamp(timestamp);
+        return s;
+    }
+
+    /**
+     * Make a shortcut with an ID and icon.
+     */
+    protected ShortcutInfo makeShortcutWithIcon(String id, Icon icon) {
+        return makeShortcut(
+                id, "Title-" + id, /* activity =*/ null, icon,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
+    }
+
+    protected ShortcutInfo makePackageShortcut(String packageName, String id) {
+        String origCaller = getCallingPackage();
+
+        setCaller(packageName);
+        ShortcutInfo s = makeShortcut(
+                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
+        setCaller(origCaller); // restore the caller
+
+        return s;
+    }
+
+    /**
+     * Make multiple shortcuts with IDs.
+     */
+    protected List<ShortcutInfo> makeShortcuts(String... ids) {
+        final ArrayList<ShortcutInfo> ret = new ArrayList();
+        for (String id : ids) {
+            ret.add(makeShortcut(id));
+        }
+        return ret;
+    }
+
+    protected ShortcutInfo.Builder makeShortcutBuilder() {
+        return new ShortcutInfo.Builder(mClientContext);
+    }
+
+    protected ShortcutInfo makeShortcutWithActivity(String id, ComponentName activity) {
+        return makeShortcut(
+                id, "Title-" + id, activity, /* icon =*/ null,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
+    }
+
+    protected ShortcutInfo makeShortcutWithIntent(String id, Intent intent) {
+        return makeShortcut(
+                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
+                intent, /* rank =*/ 0);
+    }
+
+    protected ShortcutInfo makeShortcutWithActivityAndTitle(String id, ComponentName activity,
+            String title) {
+        return makeShortcut(
+                id, title, activity, /* icon =*/ null,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), /* rank =*/ 0);
+    }
+
+    protected ShortcutInfo makeShortcutWithActivityAndRank(String id, ComponentName activity,
+            int rank) {
+        return makeShortcut(
+                id, "Title-" + id, activity, /* icon =*/ null,
+                makeIntent(Intent.ACTION_VIEW, ShortcutActivity.class), rank);
+    }
+
+    /**
+     * Make a shortcut with details.
+     */
+    protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
+            Icon icon, Intent intent, int rank) {
+        final ShortcutInfo.Builder  b = new ShortcutInfo.Builder(mClientContext, id)
+                .setActivity(new ComponentName(mClientContext.getPackageName(), "dummy"))
+                .setShortLabel(title)
+                .setRank(rank)
+                .setIntent(intent);
+        if (icon != null) {
+            b.setIcon(icon);
+        }
+        if (activity != null) {
+            b.setActivity(activity);
+        }
+        final ShortcutInfo s = b.build();
+
+        s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
+
+        return s;
+    }
+
+    protected ShortcutInfo makeShortcutWithIntents(String id, Intent... intents) {
+        return makeShortcut(
+                id, "Title-" + id, /* activity =*/ null, /* icon =*/ null,
+                intents, /* rank =*/ 0);
+    }
+
+    /**
+     * Make a shortcut with details.
+     */
+    protected ShortcutInfo makeShortcut(String id, String title, ComponentName activity,
+            Icon icon, Intent[] intents, int rank) {
+        final ShortcutInfo.Builder  b = new ShortcutInfo.Builder(mClientContext, id)
+                .setActivity(new ComponentName(mClientContext.getPackageName(), "dummy"))
+                .setShortLabel(title)
+                .setRank(rank)
+                .setIntents(intents);
+        if (icon != null) {
+            b.setIcon(icon);
+        }
+        if (activity != null) {
+            b.setActivity(activity);
+        }
+        final ShortcutInfo s = b.build();
+
+        s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
+
+        return s;
+    }
+
+    /**
+     * Make a shortcut with details.
+     */
+    protected ShortcutInfo makeShortcutWithExtras(String id, Intent intent,
+            PersistableBundle extras) {
+        final ShortcutInfo.Builder  b = new ShortcutInfo.Builder(mClientContext, id)
+                .setActivity(new ComponentName(mClientContext.getPackageName(), "dummy"))
+                .setShortLabel("title-" + id)
+                .setExtras(extras)
+                .setIntent(intent);
+        final ShortcutInfo s = b.build();
+
+        s.setTimestamp(mInjectedCurrentTimeMillis); // HACK
+
+        return s;
+    }
+
+    /**
+     * Make an intent.
+     */
+    protected Intent makeIntent(String action, Class<?> clazz, Object... bundleKeysAndValues) {
+        final Intent intent = new Intent(action);
+        intent.setComponent(makeComponent(clazz));
+        intent.replaceExtras(makeBundle(bundleKeysAndValues));
+        return intent;
+    }
+
+    /**
+     * Make an component name, with the client context.
+     */
+    @NonNull
+    protected ComponentName makeComponent(Class<?> clazz) {
+        return new ComponentName(mClientContext, clazz);
+    }
+
+    @NonNull
+    protected ShortcutInfo findById(List<ShortcutInfo> list, String id) {
+        for (ShortcutInfo s : list) {
+            if (s.getId().equals(id)) {
+                return s;
+            }
+        }
+        fail("Shortcut with id " + id + " not found");
+        return null;
+    }
+
+    protected void assertSystem() {
+        assertEquals("Caller must be system", Process.SYSTEM_UID, mInjectedCallingUid);
+    }
+
+    protected void assertResetTimes(long expectedLastResetTime, long expectedNextResetTime) {
+        assertEquals(expectedLastResetTime, mService.getLastResetTimeLocked());
+        assertEquals(expectedNextResetTime, mService.getNextResetTimeLocked());
+    }
+
+    public static List<ShortcutInfo> assertAllNotHaveIcon(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertNull("ID " + s.getId(), s.getIcon());
+        }
+        return actualShortcuts;
+    }
+
+    @NonNull
+    protected List<ShortcutInfo> assertAllHaveFlags(@NonNull List<ShortcutInfo> actualShortcuts,
+            int shortcutFlags) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId() + " doesn't have flags " + shortcutFlags,
+                    s.hasFlags(shortcutFlags));
+        }
+        return actualShortcuts;
+    }
+
+    protected ShortcutInfo getPackageShortcut(String packageName, String shortcutId, int userId) {
+        return mService.getPackageShortcutForTest(packageName, shortcutId, userId);
+    }
+
+    protected void assertShortcutExists(String packageName, String shortcutId, int userId) {
+        assertTrue(getPackageShortcut(packageName, shortcutId, userId) != null);
+    }
+
+    protected void assertShortcutNotExists(String packageName, String shortcutId, int userId) {
+        assertTrue(getPackageShortcut(packageName, shortcutId, userId) == null);
+    }
+
+    protected Intent[] launchShortcutAndGetIntentsInner(Runnable shortcutStarter,
+            @NonNull String packageName, @NonNull String shortcutId, int userId) {
+        reset(mMockActivityManagerInternal);
+        shortcutStarter.run();
+
+        final ArgumentCaptor<Intent[]> intentsCaptor = ArgumentCaptor.forClass(Intent[].class);
+        verify(mMockActivityManagerInternal).startActivitiesAsPackage(
+                eq(packageName),
+                eq(userId),
+                intentsCaptor.capture(),
+                any(Bundle.class));
+        return intentsCaptor.getValue();
+    }
+
+    protected Intent[] launchShortcutAndGetIntents(
+            @NonNull String packageName, @NonNull String shortcutId, int userId) {
+        return launchShortcutAndGetIntentsInner(
+                () -> {
+                    mLauncherApps.startShortcut(packageName, shortcutId, null, null,
+                            UserHandle.of(userId));
+                }, packageName, shortcutId, userId
+        );
+    }
+
+    protected Intent launchShortcutAndGetIntent(
+            @NonNull String packageName, @NonNull String shortcutId, int userId) {
+        final Intent[] intents = launchShortcutAndGetIntents(packageName, shortcutId, userId);
+        assertEquals(1, intents.length);
+        return intents[0];
+    }
+
+    protected Intent[] launchShortcutAndGetIntents_withShortcutInfo(
+            @NonNull String packageName, @NonNull String shortcutId, int userId) {
+        return launchShortcutAndGetIntentsInner(
+                () -> {
+                    mLauncherApps.startShortcut(
+                            getShortcutInfoAsLauncher(packageName, shortcutId, userId), null, null);
+                }, packageName, shortcutId, userId
+        );
+    }
+
+    protected Intent launchShortcutAndGetIntent_withShortcutInfo(
+            @NonNull String packageName, @NonNull String shortcutId, int userId) {
+        final Intent[] intents = launchShortcutAndGetIntents_withShortcutInfo(
+                packageName, shortcutId, userId);
+        assertEquals(1, intents.length);
+        return intents[0];
+    }
+
+    protected void assertShortcutLaunchable(@NonNull String packageName, @NonNull String shortcutId,
+            int userId) {
+        assertNotNull(launchShortcutAndGetIntent(packageName, shortcutId, userId));
+        assertNotNull(launchShortcutAndGetIntent_withShortcutInfo(packageName, shortcutId, userId));
+    }
+
+    protected void assertShortcutNotLaunched(@NonNull String packageName,
+            @NonNull String shortcutId, int userId) {
+        reset(mMockActivityManagerInternal);
+        try {
+            mLauncherApps.startShortcut(packageName, shortcutId, null, null,
+                    UserHandle.of(userId));
+            fail("ActivityNotFoundException was not thrown");
+        } catch (ActivityNotFoundException expected) {
+        }
+        // This shouldn't have been called.
+        verify(mMockActivityManagerInternal, times(0)).startActivitiesAsPackage(
+                anyString(),
+                anyInt(),
+                any(Intent[].class),
+                any(Bundle.class));
+    }
+
+    protected void assertStartShortcutThrowsException(@NonNull String packageName,
+            @NonNull String shortcutId, int userId, Class<?> expectedException) {
+        Exception thrown = null;
+        try {
+            mLauncherApps.startShortcut(packageName, shortcutId, null, null,
+                    UserHandle.of(userId));
+        } catch (Exception e) {
+            thrown = e;
+        }
+        assertNotNull("Exception was not thrown", thrown);
+        assertEquals("Exception type different", expectedException, thrown.getClass());
+    }
+
+    protected void assertBitmapDirectories(int userId, String... expectedDirectories) {
+        final Set<String> expected = hashSet(set(expectedDirectories));
+
+        final Set<String> actual = new HashSet<>();
+
+        final File[] files = mService.getUserBitmapFilePath(userId).listFiles();
+        if (files != null) {
+            for (File child : files) {
+                if (child.isDirectory()) {
+                    actual.add(child.getName());
+                }
+            }
+        }
+
+        assertEquals(expected, actual);
+    }
+
+    protected void assertBitmapFiles(int userId, String packageName, String... expectedFiles) {
+        final Set<String> expected = hashSet(set(expectedFiles));
+
+        final Set<String> actual = new HashSet<>();
+
+        final File[] files = new File(mService.getUserBitmapFilePath(userId), packageName)
+                .listFiles();
+        if (files != null) {
+            for (File child : files) {
+                if (child.isFile()) {
+                    actual.add(child.getName());
+                }
+            }
+        }
+
+        assertEquals(expected, actual);
+    }
+
+    protected String getBitmapFilename(int userId, String packageName, String shortcutId) {
+        final ShortcutInfo si = mService.getPackageShortcutForTest(packageName, shortcutId, userId);
+        if (si == null) {
+            return null;
+        }
+        return new File(si.getBitmapPath()).getName();
+    }
+
+    /**
+     * @return all shortcuts stored internally for the caller.  This reflects the *internal* view
+     * of shortcuts, which may be different from what {@link #getCallerVisibleShortcuts} would
+     * return, because getCallerVisibleShortcuts() will get shortcuts from the proper "front door"
+     * which performs some extra checks, like {@link ShortcutPackage#onRestored}.
+     */
+    protected List<ShortcutInfo> getCallerShortcuts() {
+        final ShortcutPackage p = mService.getPackageShortcutForTest(
+                getCallingPackage(), getCallingUserId());
+        return p == null ? null : p.getAllShortcutsForTest();
+    }
+
+    /**
+     * @return all shortcuts owned by caller that are actually visible via ShortcutManager.
+     * See also {@link #getCallerShortcuts}.
+     */
+    protected List<ShortcutInfo> getCallerVisibleShortcuts() {
+        final ArrayList<ShortcutInfo> ret = new ArrayList<>();
+        ret.addAll(mManager.getDynamicShortcuts());
+        ret.addAll(mManager.getPinnedShortcuts());
+        ret.addAll(mManager.getManifestShortcuts());
+        return ret;
+    }
+
+    protected ShortcutInfo getCallerShortcut(String shortcutId) {
+        return getPackageShortcut(getCallingPackage(), shortcutId, getCallingUserId());
+    }
+
+    protected List<ShortcutInfo> getLauncherShortcuts(String launcher, int userId, int queryFlags) {
+        final List<ShortcutInfo>[] ret = new List[1];
+        runWithCaller(launcher, userId, () -> {
+            final ShortcutQuery q = new ShortcutQuery();
+            q.setQueryFlags(queryFlags);
+            ret[0] = mLauncherApps.getShortcuts(q, UserHandle.of(userId));
+        });
+        return ret[0];
+    }
+
+    protected List<ShortcutInfo> getLauncherPinnedShortcuts(String launcher, int userId) {
+        return getLauncherShortcuts(launcher, userId, ShortcutQuery.FLAG_GET_PINNED);
+    }
+
+    protected ShortcutInfo getShortcutInfoAsLauncher(String packageName, String shortcutId,
+            int userId) {
+        final List<ShortcutInfo> infoList =
+                mLauncherApps.getShortcutInfo(packageName, list(shortcutId),
+                        UserHandle.of(userId));
+        assertEquals("No shortcutInfo found (or too many of them)", 1, infoList.size());
+        return infoList.get(0);
+    }
+
+    protected Intent genPackageAddIntent(String packageName, int userId) {
+        installPackage(userId, packageName);
+
+        Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
+        i.setData(Uri.parse("package:" + packageName));
+        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
+        return i;
+    }
+
+    protected Intent genPackageDeleteIntent(String pakcageName, int userId) {
+        uninstallPackage(userId, pakcageName);
+
+        Intent i = new Intent(Intent.ACTION_PACKAGE_REMOVED);
+        i.setData(Uri.parse("package:" + pakcageName));
+        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
+        return i;
+    }
+
+    protected Intent genPackageUpdateIntent(String pakcageName, int userId) {
+        installPackage(userId, pakcageName);
+
+        Intent i = new Intent(Intent.ACTION_PACKAGE_ADDED);
+        i.setData(Uri.parse("package:" + pakcageName));
+        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
+        i.putExtra(Intent.EXTRA_REPLACING, true);
+        return i;
+    }
+
+    protected Intent genPackageChangedIntent(String pakcageName, int userId) {
+        Intent i = new Intent(Intent.ACTION_PACKAGE_CHANGED);
+        i.setData(Uri.parse("package:" + pakcageName));
+        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
+        return i;
+    }
+
+    protected Intent genPackageDataClear(String packageName, int userId) {
+        Intent i = new Intent(Intent.ACTION_PACKAGE_DATA_CLEARED);
+        i.setData(Uri.parse("package:" + packageName));
+        i.putExtra(Intent.EXTRA_USER_HANDLE, userId);
+        return i;
+    }
+
+    protected void assertExistsAndShadow(ShortcutPackageItem spi) {
+        assertNotNull(spi);
+        assertTrue(spi.getPackageInfo().isShadow());
+    }
+
+    protected File makeFile(File baseDirectory, String... paths) {
+        File ret = baseDirectory;
+
+        for (String path : paths) {
+            ret = new File(ret, path);
+        }
+
+        return ret;
+    }
+
+    protected boolean bitmapDirectoryExists(String packageName, int userId) {
+        final File path = new File(mService.getUserBitmapFilePath(userId), packageName);
+        return path.isDirectory();
+    }
+    protected static ShortcutQuery buildQuery(long changedSince,
+            String packageName, ComponentName componentName,
+            /* @ShortcutQuery.QueryFlags */ int flags) {
+        return buildQuery(changedSince, packageName, null, componentName, flags);
+    }
+
+    protected static ShortcutQuery buildQuery(long changedSince,
+            String packageName, List<String> shortcutIds, ComponentName componentName,
+            /* @ShortcutQuery.QueryFlags */ int flags) {
+        final ShortcutQuery q = new ShortcutQuery();
+        q.setChangedSince(changedSince);
+        q.setPackage(packageName);
+        q.setShortcutIds(shortcutIds);
+        q.setActivity(componentName);
+        q.setQueryFlags(flags);
+        return q;
+    }
+
+    protected static ShortcutQuery buildAllQuery(String packageName) {
+        final ShortcutQuery q = new ShortcutQuery();
+        q.setPackage(packageName);
+        q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS);
+        return q;
+    }
+
+    protected static ShortcutQuery buildPinnedQuery(String packageName) {
+        final ShortcutQuery q = new ShortcutQuery();
+        q.setPackage(packageName);
+        q.setQueryFlags(ShortcutQuery.FLAG_GET_PINNED);
+        return q;
+    }
+
+    protected static ShortcutQuery buildQueryWithFlags(int queryFlags) {
+        final ShortcutQuery q = new ShortcutQuery();
+        q.setQueryFlags(queryFlags);
+        return q;
+    }
+
+    protected void backupAndRestore() {
+        int prevUid = mInjectedCallingUid;
+
+        mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
+
+        dumpsysOnLogcat("Before backup");
+
+        final byte[] payload =  mService.getBackupPayload(USER_0);
+        if (ENABLE_DUMP) {
+            final String xml = new String(payload);
+            Log.v(TAG, "Backup payload:");
+            for (String line : xml.split("\n")) {
+                Log.v(TAG, line);
+            }
+        }
+
+        // Before doing anything else, uninstall all packages.
+        for (int userId : list(USER_0, USER_P0)) {
+            for (String pkg : list(CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
+                    LAUNCHER_1, LAUNCHER_2, LAUNCHER_3)) {
+                uninstallPackage(userId, pkg);
+            }
+        }
+
+        shutdownServices();
+
+        deleteAllSavedFiles();
+
+        initService();
+        mService.applyRestore(payload, USER_0);
+
+        // handleUnlockUser will perform the gone package check, but it shouldn't remove
+        // shadow information.
+        mService.handleUnlockUser(USER_0);
+
+        dumpsysOnLogcat("After restore");
+
+        mInjectedCallingUid = prevUid;
+    }
+
+    protected void prepareCrossProfileDataSet() {
+        mRunningUsers.put(USER_10, true); // this test needs user 10.
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
+                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
+                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
+                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list()));
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
+                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"),
+                    makeShortcut("x4"), makeShortcut("x5"), makeShortcut("x6"))));
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s1", "s2"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s1", "s2", "s3"), HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s4"), HANDLE_USER_P0);
+        });
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s2", "s3"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s2", "s3", "s4"), HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s5"), HANDLE_USER_P0);
+        });
+
+        // Note LAUNCHER_3 has allowBackup=false.
+        runWithCaller(LAUNCHER_3, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5"), HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s6"), HANDLE_USER_P0);
+        });
+        runWithCaller(LAUNCHER_4, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list(), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_4, list(), HANDLE_USER_0);
+        });
+
+        // Launcher on a managed profile is referring ot user 0!
+        runWithCaller(LAUNCHER_1, USER_P0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s3", "s4"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s3", "s4", "s5"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("s3", "s4", "s5", "s6"),
+                    HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s4", "s1"), HANDLE_USER_P0);
+        });
+        runWithCaller(LAUNCHER_1, USER_10, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("x4", "x5"), HANDLE_USER_10);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("x4", "x5", "x6"), HANDLE_USER_10);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("x4", "x5", "x6", "x1"),
+                    HANDLE_USER_10);
+        });
+
+        // Then remove some dynamic shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list()));
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("x1"), makeShortcut("x2"), makeShortcut("x3"))));
+        });
+    }
+
+    public static List<ShortcutInfo> assertAllHaveIconResId(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId() + " not have icon res ID", s.hasIconResource());
+            assertFalse("ID " + s.getId() + " shouldn't have icon FD", s.hasIconFile());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllHaveIconFile(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertFalse("ID " + s.getId() + " shouldn't have icon res ID", s.hasIconResource());
+            assertTrue("ID " + s.getId() + " not have icon FD", s.hasIconFile());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllHaveIcon(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId() + " has no icon ", s.hasIconFile() || s.hasIconResource());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllStringsResolved(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), s.hasStringResourcesResolved());
+        }
+        return actualShortcuts;
+    }
+
+    public String readTestAsset(String assetPath) throws IOException {
+        final StringBuilder sb = new StringBuilder();
+        try (BufferedReader br = new BufferedReader(
+                new InputStreamReader(
+                        getTestContext().getResources().getAssets().open(assetPath)))) {
+            String line;
+            while ((line = br.readLine()) != null) {
+                sb.append(line);
+                sb.append(System.lineSeparator());
+            }
+        }
+        return sb.toString();
+    }
+
+    protected void prepareGetHomeActivitiesAsUser(ComponentName preferred,
+            List<ResolveInfo> candidates, int userId) {
+        doAnswer(inv -> {
+            ((List) inv.getArguments()[0]).addAll(candidates);
+            return preferred;
+        }).when(mMockPackageManagerInternal).getHomeActivitiesAsUser(any(List.class), eq(userId));
+    }
+
+    protected static ComponentName cn(String packageName, String name) {
+        return new ComponentName(packageName, name);
+    }
+
+    protected static ResolveInfo ri(String packageName, String name, boolean isSystem, int priority) {
+        final ResolveInfo ri = new ResolveInfo();
+        ri.activityInfo = new ActivityInfo();
+        ri.activityInfo.applicationInfo = new ApplicationInfo();
+
+        ri.activityInfo.packageName = packageName;
+        ri.activityInfo.name = name;
+        if (isSystem) {
+            ri.activityInfo.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
+        }
+        ri.priority = priority;
+        return ri;
+    }
+
+    protected static ResolveInfo getSystemLauncher() {
+        return ri(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME, true,
+                PACKAGE_SYSTEM_LAUNCHER_PRIORITY);
+    }
+
+    protected static ResolveInfo getFallbackLauncher() {
+        return ri(PACKAGE_FALLBACK_LAUNCHER, PACKAGE_FALLBACK_LAUNCHER_NAME, true,
+                PACKAGE_FALLBACK_LAUNCHER_PRIORITY);
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java
new file mode 100644
index 0000000..3cfdc32
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java
@@ -0,0 +1,7340 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDisabled;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamic;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllDynamicOrPinned;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllEnabled;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveIntents;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllHaveTitle;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllImmutable;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllKeyFieldsOnly;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllManifest;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveIntents;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotHaveTitle;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotKeyFieldsOnly;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllNotManifest;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllPinned;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertAllUnique;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBitmapSize;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBundleEmpty;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackNotReceived;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCallbackReceived;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertCannotUpdateImmutable;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicAndPinned;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicOnly;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertDynamicShortcutCountExceeded;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertEmpty;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertForLauncherCallback;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertShortcutIds;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.filterByActivity;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.findShortcut;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.hashSet;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.pfdToBitmap;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resetAll;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.waitOnMainThread;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.Manifest.permission;
+import android.app.ActivityManager;
+import android.content.ActivityNotFoundException;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.LauncherApps;
+import android.content.pm.LauncherApps.ShortcutQuery;
+import android.content.pm.ShortcutInfo;
+import android.graphics.Bitmap;
+import android.graphics.Bitmap.CompressFormat;
+import android.graphics.BitmapFactory;
+import android.graphics.drawable.Icon;
+import android.net.Uri;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Process;
+import android.os.UserHandle;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.util.Log;
+import android.util.SparseArray;
+
+import com.android.frameworks.servicestests.R;
+import com.android.server.pm.ShortcutService.ConfigConstants;
+import com.android.server.pm.ShortcutService.FileOutputStreamWithPath;
+import com.android.server.pm.ShortcutUser.PackageWithUser;
+
+import org.mockito.ArgumentCaptor;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * Tests for ShortcutService and ShortcutManager.
+ *
+ m FrameworksServicesTests &&
+ adb install \
+ -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
+ adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest1 \
+ -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
+ */
+@SmallTest
+public class ShortcutManagerTest1 extends BaseShortcutManagerTest {
+
+    /**
+     * Test for the first launch path, no settings file available.
+     */
+    public void testFirstInitialize() {
+        assertResetTimes(START_TIME, START_TIME + INTERVAL);
+    }
+
+    /**
+     * Test for {@link ShortcutService#getLastResetTimeLocked()} and
+     * {@link ShortcutService#getNextResetTimeLocked()}.
+     */
+    public void testUpdateAndGetNextResetTimeLocked() {
+        assertResetTimes(START_TIME, START_TIME + INTERVAL);
+
+        // Advance clock.
+        mInjectedCurrentTimeMillis += 100;
+
+        // Shouldn't have changed.
+        assertResetTimes(START_TIME, START_TIME + INTERVAL);
+
+        // Advance clock, almost the reset time.
+        mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
+
+        // Shouldn't have changed.
+        assertResetTimes(START_TIME, START_TIME + INTERVAL);
+
+        // Advance clock.
+        mInjectedCurrentTimeMillis += 1;
+
+        assertResetTimes(START_TIME + INTERVAL, START_TIME + 2 * INTERVAL);
+
+        // Advance further; 4 hours since start.
+        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
+
+        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
+    }
+
+    /**
+     * Test for the restoration from saved file.
+     */
+    public void testInitializeFromSavedFile() {
+
+        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
+        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
+
+        mService.saveBaseStateLocked();
+
+        dumpBaseStateFile();
+
+        mService.saveDirtyInfo();
+
+        // Restore.
+        initService();
+
+        assertResetTimes(START_TIME + 4 * INTERVAL, START_TIME + 5 * INTERVAL);
+    }
+
+    /**
+     * Test for the restoration from restored file.
+     */
+    public void testLoadFromBrokenFile() {
+        // TODO Add various broken cases.
+    }
+
+    public void testLoadConfig() {
+        mService.updateConfigurationLocked(
+                ConfigConstants.KEY_RESET_INTERVAL_SEC + "=123,"
+                        + ConfigConstants.KEY_MAX_SHORTCUTS + "=4,"
+                        + ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=5,"
+                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
+                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
+                        + ConfigConstants.KEY_ICON_FORMAT + "=WEBP,"
+                        + ConfigConstants.KEY_ICON_QUALITY + "=75");
+        assertEquals(123000, mService.getResetIntervalForTest());
+        assertEquals(4, mService.getMaxShortcutsForTest());
+        assertEquals(5, mService.getMaxUpdatesPerIntervalForTest());
+        assertEquals(100, mService.getMaxIconDimensionForTest());
+        assertEquals(CompressFormat.WEBP, mService.getIconPersistFormatForTest());
+        assertEquals(75, mService.getIconPersistQualityForTest());
+
+        mInjectedIsLowRamDevice = true;
+        mService.updateConfigurationLocked(
+                ConfigConstants.KEY_MAX_ICON_DIMENSION_DP + "=100,"
+                        + ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM + "=50,"
+                        + ConfigConstants.KEY_ICON_FORMAT + "=JPEG");
+        assertEquals(ShortcutService.DEFAULT_RESET_INTERVAL_SEC * 1000,
+                mService.getResetIntervalForTest());
+
+        assertEquals(ShortcutService.DEFAULT_MAX_SHORTCUTS_PER_APP,
+                mService.getMaxShortcutsForTest());
+
+        assertEquals(ShortcutService.DEFAULT_MAX_UPDATES_PER_INTERVAL,
+                mService.getMaxUpdatesPerIntervalForTest());
+
+        assertEquals(50, mService.getMaxIconDimensionForTest());
+
+        assertEquals(CompressFormat.JPEG, mService.getIconPersistFormatForTest());
+
+        assertEquals(ShortcutService.DEFAULT_ICON_PERSIST_QUALITY,
+                mService.getIconPersistQualityForTest());
+    }
+
+    // === Test for app side APIs ===
+
+    /** Test for {@link android.content.pm.ShortcutManager#getMaxShortcutCountForActivity()} */
+    public void testGetMaxDynamicShortcutCount() {
+        assertEquals(MAX_SHORTCUTS, mManager.getMaxShortcutCountForActivity());
+    }
+
+    /** Test for {@link android.content.pm.ShortcutManager#getRemainingCallCount()} */
+    public void testGetRemainingCallCount() {
+        assertEquals(MAX_UPDATES_PER_INTERVAL, mManager.getRemainingCallCount());
+    }
+
+    public void testGetIconMaxDimensions() {
+        assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxWidth());
+        assertEquals(MAX_ICON_DIMENSION, mManager.getIconMaxHeight());
+    }
+
+    /** Test for {@link android.content.pm.ShortcutManager#getRateLimitResetTime()} */
+    public void testGetRateLimitResetTime() {
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL + 50;
+
+        assertEquals(START_TIME + 5 * INTERVAL, mManager.getRateLimitResetTime());
+    }
+
+    public void testSetDynamicShortcuts() {
+        setCaller(CALLING_PACKAGE_1, USER_0);
+
+        final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.icon1);
+        final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.icon2));
+
+        final ShortcutInfo si1 = makeShortcut(
+                "shortcut1",
+                "Title 1",
+                makeComponent(ShortcutActivity.class),
+                icon1,
+                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
+                        "key1", "val1", "nest", makeBundle("key", 123)),
+                /* weight */ 10);
+
+        final ShortcutInfo si2 = makeShortcut(
+                "shortcut2",
+                "Title 2",
+                /* activity */ null,
+                icon2,
+                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
+                /* weight */ 12);
+        final ShortcutInfo si3 = makeShortcut("shortcut3");
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut1", "shortcut2");
+        assertEquals(2, mManager.getRemainingCallCount());
+
+        // TODO: Check fields
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut1");
+        assertEquals(1, mManager.getRemainingCallCount());
+
+        assertTrue(mManager.setDynamicShortcuts(list()));
+        assertEquals(0, mManager.getDynamicShortcuts().size());
+        assertEquals(0, mManager.getRemainingCallCount());
+
+        dumpsysOnLogcat();
+
+        mInjectedCurrentTimeMillis++; // Need to advance the clock for reset to work.
+        mService.resetThrottlingInner(UserHandle.USER_SYSTEM);
+
+        dumpsysOnLogcat();
+
+        assertTrue(mManager.setDynamicShortcuts(list(si2, si3)));
+        assertEquals(2, mManager.getDynamicShortcuts().size());
+
+        // TODO Check max number
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(makeShortcut("s1"))));
+        });
+    }
+
+    public void testAddDynamicShortcuts() {
+        setCaller(CALLING_PACKAGE_1, USER_0);
+
+        final ShortcutInfo si1 = makeShortcut("shortcut1");
+        final ShortcutInfo si2 = makeShortcut("shortcut2");
+        final ShortcutInfo si3 = makeShortcut("shortcut3");
+
+        assertEquals(3, mManager.getRemainingCallCount());
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut1");
+
+        assertTrue(mManager.addDynamicShortcuts(list(si2, si3)));
+        assertEquals(1, mManager.getRemainingCallCount());
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut1", "shortcut2", "shortcut3");
+
+        // This should not crash.  It'll still consume the quota.
+        assertTrue(mManager.addDynamicShortcuts(list()));
+        assertEquals(0, mManager.getRemainingCallCount());
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut1", "shortcut2", "shortcut3");
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset
+
+        // Add with the same ID
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("shortcut1"))));
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut1", "shortcut2", "shortcut3");
+
+        // TODO Check max number
+
+        // TODO Check fields.
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
+        });
+    }
+
+    public void testPublishWithNoActivity() {
+        // If activity is not explicitly set, use the default one.
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            // s1 and s3 has no activities.
+            final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
+                    .setShortLabel("label1")
+                    .setIntent(new Intent("action1"))
+                    .build();
+            final ShortcutInfo si2 = new ShortcutInfo.Builder(mClientContext, "si2")
+                    .setShortLabel("label2")
+                    .setActivity(new ComponentName(getCallingPackage(), "abc"))
+                    .setIntent(new Intent("action2"))
+                    .build();
+            final ShortcutInfo si3 = new ShortcutInfo.Builder(mClientContext, "si3")
+                    .setShortLabel("label3")
+                    .setIntent(new Intent("action3"))
+                    .build();
+
+            // Set test 1
+            assertTrue(mManager.setDynamicShortcuts(list(si1)));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("si1")
+                    .forShortcutWithId("si1", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                MAIN_ACTIVITY_CLASS), si.getActivity());
+                    });
+
+            // Set test 2
+            assertTrue(mManager.setDynamicShortcuts(list(si2, si1)));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("si1", "si2")
+                    .forShortcutWithId("si1", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                MAIN_ACTIVITY_CLASS), si.getActivity());
+                    })
+                    .forShortcutWithId("si2", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                "abc"), si.getActivity());
+                    });
+
+
+            // Set test 3
+            assertTrue(mManager.setDynamicShortcuts(list(si3, si1)));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("si1", "si3")
+                    .forShortcutWithId("si1", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                MAIN_ACTIVITY_CLASS), si.getActivity());
+                    })
+                    .forShortcutWithId("si3", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                MAIN_ACTIVITY_CLASS), si.getActivity());
+                    });
+
+            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
+
+            // Add test 1
+            mManager.removeAllDynamicShortcuts();
+            assertTrue(mManager.addDynamicShortcuts(list(si1)));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("si1")
+                    .forShortcutWithId("si1", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                MAIN_ACTIVITY_CLASS), si.getActivity());
+                    });
+
+            // Add test 2
+            mManager.removeAllDynamicShortcuts();
+            assertTrue(mManager.addDynamicShortcuts(list(si2, si1)));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("si1", "si2")
+                    .forShortcutWithId("si1", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                MAIN_ACTIVITY_CLASS), si.getActivity());
+                    })
+                    .forShortcutWithId("si2", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                "abc"), si.getActivity());
+                    });
+
+
+            // Add test 3
+            mManager.removeAllDynamicShortcuts();
+            assertTrue(mManager.addDynamicShortcuts(list(si3, si1)));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("si1", "si3")
+                    .forShortcutWithId("si1", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                MAIN_ACTIVITY_CLASS), si.getActivity());
+                    })
+                    .forShortcutWithId("si3", si -> {
+                        assertEquals(new ComponentName(getCallingPackage(),
+                                MAIN_ACTIVITY_CLASS), si.getActivity());
+                    });
+        });
+    }
+
+    public void testPublishWithNoActivity_noMainActivityInPackage() {
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            final ShortcutInfo si1 = new ShortcutInfo.Builder(mClientContext, "si1")
+                    .setShortLabel("label1")
+                    .setIntent(new Intent("action1"))
+                    .build();
+
+            // Returning null means there's no main activity in this package.
+            mMainActivityFetcher = (packageName, userId) -> null;
+
+            assertExpectException(
+                    RuntimeException.class, "Launcher activity not found for", () -> {
+                        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+                    });
+        });
+    }
+
+    public void testDeleteDynamicShortcuts() {
+        final ShortcutInfo si1 = makeShortcut("shortcut1");
+        final ShortcutInfo si2 = makeShortcut("shortcut2");
+        final ShortcutInfo si3 = makeShortcut("shortcut3");
+        final ShortcutInfo si4 = makeShortcut("shortcut4");
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3, si4)));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut1", "shortcut2", "shortcut3", "shortcut4");
+
+        assertEquals(2, mManager.getRemainingCallCount());
+
+        mManager.removeDynamicShortcuts(list("shortcut1"));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut2", "shortcut3", "shortcut4");
+
+        mManager.removeDynamicShortcuts(list("shortcut1"));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut2", "shortcut3", "shortcut4");
+
+        mManager.removeDynamicShortcuts(list("shortcutXXX"));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut2", "shortcut3", "shortcut4");
+
+        mManager.removeDynamicShortcuts(list("shortcut2", "shortcut4"));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut3");
+
+        mManager.removeDynamicShortcuts(list("shortcut3"));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()));
+
+        // Still 2 calls left.
+        assertEquals(2, mManager.getRemainingCallCount());
+    }
+
+    public void testDeleteAllDynamicShortcuts() {
+        final ShortcutInfo si1 = makeShortcut("shortcut1");
+        final ShortcutInfo si2 = makeShortcut("shortcut2");
+        final ShortcutInfo si3 = makeShortcut("shortcut3");
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mManager.getDynamicShortcuts()),
+                "shortcut1", "shortcut2", "shortcut3");
+
+        assertEquals(2, mManager.getRemainingCallCount());
+
+        mManager.removeAllDynamicShortcuts();
+        assertEquals(0, mManager.getDynamicShortcuts().size());
+        assertEquals(2, mManager.getRemainingCallCount());
+
+        // Note delete shouldn't affect throttling, so...
+        assertEquals(0, mManager.getDynamicShortcuts().size());
+        assertEquals(0, mManager.getDynamicShortcuts().size());
+        assertEquals(0, mManager.getDynamicShortcuts().size());
+
+        // This should still work.
+        assertTrue(mManager.setDynamicShortcuts(list(si1, si2, si3)));
+        assertEquals(3, mManager.getDynamicShortcuts().size());
+
+        // Still 1 call left
+        assertEquals(1, mManager.getRemainingCallCount());
+    }
+
+    public void testIcons() throws IOException {
+        final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
+        final Icon res64x64 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
+        final Icon res512x512 = Icon.createWithResource(getTestContext(), R.drawable.black_512x512);
+
+        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_32x32));
+        final Icon bmp64x64 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_64x64));
+        final Icon bmp512x512 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_512x512));
+
+        // Set from package 1
+        setCaller(CALLING_PACKAGE_1);
+        assertTrue(mManager.setDynamicShortcuts(list(
+                makeShortcutWithIcon("res32x32", res32x32),
+                makeShortcutWithIcon("res64x64", res64x64),
+                makeShortcutWithIcon("bmp32x32", bmp32x32),
+                makeShortcutWithIcon("bmp64x64", bmp64x64),
+                makeShortcutWithIcon("bmp512x512", bmp512x512),
+                makeShortcut("none")
+        )));
+
+        // getDynamicShortcuts() shouldn't return icons, thus assertAllNotHaveIcon().
+        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
+                "res32x32",
+                "res64x64",
+                "bmp32x32",
+                "bmp64x64",
+                "bmp512x512",
+                "none");
+
+        // Call from another caller with the same ID, just to make sure storage is per-package.
+        setCaller(CALLING_PACKAGE_2);
+        assertTrue(mManager.setDynamicShortcuts(list(
+                makeShortcutWithIcon("res32x32", res512x512),
+                makeShortcutWithIcon("res64x64", res512x512),
+                makeShortcutWithIcon("none", res512x512)
+        )));
+        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
+                "res32x32",
+                "res64x64",
+                "none");
+
+        // Different profile.  Note the names and the contents don't match.
+        setCaller(CALLING_PACKAGE_1, USER_P0);
+        assertTrue(mManager.setDynamicShortcuts(list(
+                makeShortcutWithIcon("res32x32", res512x512),
+                makeShortcutWithIcon("bmp32x32", bmp512x512)
+        )));
+        assertShortcutIds(assertAllNotHaveIcon(mManager.getDynamicShortcuts()),
+                "res32x32",
+                "bmp32x32");
+
+        // Re-initialize and load from the files.
+        mService.saveDirtyInfo();
+        initService();
+
+        // Load from launcher.
+        Bitmap bmp;
+
+        setCaller(LAUNCHER_1);
+        // Check hasIconResource()/hasIconFile().
+        assertShortcutIds(assertAllHaveIconResId(
+                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0))),
+                "res32x32");
+
+        assertShortcutIds(assertAllHaveIconResId(
+                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0))),
+                "res64x64");
+
+        assertShortcutIds(assertAllHaveIconFile(
+                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0))),
+                "bmp32x32");
+
+        assertShortcutIds(assertAllHaveIconFile(
+                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0))),
+                "bmp64x64");
+
+        assertShortcutIds(assertAllHaveIconFile(
+                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0))),
+                "bmp512x512");
+
+        assertShortcutIds(assertAllHaveIconResId(
+                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0))),
+                "res32x32");
+        assertShortcutIds(assertAllHaveIconFile(
+                list(getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0))),
+                "bmp32x32");
+
+        // Check
+        assertEquals(
+                R.drawable.black_32x32,
+                mLauncherApps.getShortcutIconResId(
+                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_0)));
+
+        assertEquals(
+                R.drawable.black_64x64,
+                mLauncherApps.getShortcutIconResId(
+                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res64x64", USER_0)));
+
+        assertEquals(
+                0, // because it's not a resource
+                mLauncherApps.getShortcutIconResId(
+                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
+        assertEquals(
+                0, // because it's not a resource
+                mLauncherApps.getShortcutIconResId(
+                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
+        assertEquals(
+                0, // because it's not a resource
+                mLauncherApps.getShortcutIconResId(
+                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
+
+        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
+                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_0)));
+        assertBitmapSize(32, 32, bmp);
+
+        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
+                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp64x64", USER_0)));
+        assertBitmapSize(64, 64, bmp);
+
+        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
+                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp512x512", USER_0)));
+        assertBitmapSize(128, 128, bmp);
+
+        assertEquals(
+                R.drawable.black_512x512,
+                mLauncherApps.getShortcutIconResId(
+                        getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "res32x32", USER_P0)));
+        // Should be 512x512, so shrunk.
+        bmp = pfdToBitmap(mLauncherApps.getShortcutIconFd(
+                getShortcutInfoAsLauncher(CALLING_PACKAGE_1, "bmp32x32", USER_P0)));
+        assertBitmapSize(128, 128, bmp);
+
+        // Also check the overload APIs too.
+        assertEquals(
+                R.drawable.black_32x32,
+                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_0));
+        assertEquals(
+                R.drawable.black_64x64,
+                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res64x64", HANDLE_USER_0));
+        assertEquals(
+                R.drawable.black_512x512,
+                mLauncherApps.getShortcutIconResId(CALLING_PACKAGE_1, "res32x32", HANDLE_USER_P0));
+        bmp = pfdToBitmap(
+                mLauncherApps.getShortcutIconFd(CALLING_PACKAGE_1, "bmp32x32", HANDLE_USER_P0));
+        assertBitmapSize(128, 128, bmp);
+    }
+
+    public void testCleanupDanglingBitmaps() throws Exception {
+        assertBitmapDirectories(USER_0, EMPTY_STRINGS);
+        assertBitmapDirectories(USER_10, EMPTY_STRINGS);
+
+        // Make some shortcuts with bitmap icons.
+        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_32x32));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.setDynamicShortcuts(list(
+                    makeShortcutWithIcon("s1", bmp32x32),
+                    makeShortcutWithIcon("s2", bmp32x32),
+                    makeShortcutWithIcon("s3", bmp32x32)
+            ));
+        });
+
+        // Increment the time (which actually we don't have to), which is used for filenames.
+        mInjectedCurrentTimeMillis++;
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            mManager.setDynamicShortcuts(list(
+                    makeShortcutWithIcon("s4", bmp32x32),
+                    makeShortcutWithIcon("s5", bmp32x32),
+                    makeShortcutWithIcon("s6", bmp32x32)
+            ));
+        });
+
+        // Increment the time, which is used for filenames.
+        mInjectedCurrentTimeMillis++;
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            mManager.setDynamicShortcuts(list(
+            ));
+        });
+
+        // For USER-10, let's try without updating the times.
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            mManager.setDynamicShortcuts(list(
+                    makeShortcutWithIcon("10s1", bmp32x32),
+                    makeShortcutWithIcon("10s2", bmp32x32),
+                    makeShortcutWithIcon("10s3", bmp32x32)
+            ));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            mManager.setDynamicShortcuts(list(
+                    makeShortcutWithIcon("10s4", bmp32x32),
+                    makeShortcutWithIcon("10s5", bmp32x32),
+                    makeShortcutWithIcon("10s6", bmp32x32)
+            ));
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
+            mManager.setDynamicShortcuts(list(
+            ));
+        });
+
+        dumpsysOnLogcat();
+
+        // Check files and directories.
+        // Package 3 has no bitmaps, so we don't create a directory.
+        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
+        assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
+
+        assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
+                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
+                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
+                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
+        );
+        assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
+                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
+                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
+                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
+        );
+        assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
+                EMPTY_STRINGS
+        );
+        assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
+                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
+                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
+                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
+        );
+        assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
+                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
+                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
+                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
+        );
+        assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
+                EMPTY_STRINGS
+        );
+
+        // Then create random directories and files.
+        makeFile(mService.getUserBitmapFilePath(USER_0), "a.b.c").mkdir();
+        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f").mkdir();
+        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "123").createNewFile();
+        makeFile(mService.getUserBitmapFilePath(USER_0), "d.e.f", "456").createNewFile();
+
+        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_3).mkdir();
+
+        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "1").createNewFile();
+        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "2").createNewFile();
+        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "3").createNewFile();
+        makeFile(mService.getUserBitmapFilePath(USER_0), CALLING_PACKAGE_1, "4").createNewFile();
+
+        makeFile(mService.getUserBitmapFilePath(USER_10), "10a.b.c").mkdir();
+        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f").mkdir();
+        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "123").createNewFile();
+        makeFile(mService.getUserBitmapFilePath(USER_10), "10d.e.f", "456").createNewFile();
+
+        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "1").createNewFile();
+        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "2").createNewFile();
+        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "3").createNewFile();
+        makeFile(mService.getUserBitmapFilePath(USER_10), CALLING_PACKAGE_2, "4").createNewFile();
+
+        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3,
+                "a.b.c", "d.e.f");
+
+        // Save and load.  When a user is loaded, we do the cleanup.
+        mService.saveDirtyInfo();
+        initService();
+
+        mService.handleUnlockUser(USER_0);
+        mService.handleUnlockUser(USER_10);
+        mService.handleUnlockUser(20); // Make sure the logic will still work for nonexistent user.
+
+        // The below check is the same as above, except this time USER_0 use the CALLING_PACKAGE_3
+        // directory.
+
+        assertBitmapDirectories(USER_0, CALLING_PACKAGE_1, CALLING_PACKAGE_2, CALLING_PACKAGE_3);
+        assertBitmapDirectories(USER_10, CALLING_PACKAGE_1, CALLING_PACKAGE_2);
+
+        assertBitmapFiles(USER_0, CALLING_PACKAGE_1,
+                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s1"),
+                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s2"),
+                getBitmapFilename(USER_0, CALLING_PACKAGE_1, "s3")
+        );
+        assertBitmapFiles(USER_0, CALLING_PACKAGE_2,
+                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s4"),
+                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s5"),
+                getBitmapFilename(USER_0, CALLING_PACKAGE_2, "s6")
+        );
+        assertBitmapFiles(USER_0, CALLING_PACKAGE_3,
+                EMPTY_STRINGS
+        );
+        assertBitmapFiles(USER_10, CALLING_PACKAGE_1,
+                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s1"),
+                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s2"),
+                getBitmapFilename(USER_10, CALLING_PACKAGE_1, "10s3")
+        );
+        assertBitmapFiles(USER_10, CALLING_PACKAGE_2,
+                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s4"),
+                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s5"),
+                getBitmapFilename(USER_10, CALLING_PACKAGE_2, "10s6")
+        );
+        assertBitmapFiles(USER_10, CALLING_PACKAGE_3,
+                EMPTY_STRINGS
+        );
+    }
+
+    protected void checkShrinkBitmap(int expectedWidth, int expectedHeight, int resId, int maxSize) {
+        assertBitmapSize(expectedWidth, expectedHeight,
+                ShortcutService.shrinkBitmap(BitmapFactory.decodeResource(
+                        getTestContext().getResources(), resId),
+                        maxSize));
+    }
+
+    public void testShrinkBitmap() {
+        checkShrinkBitmap(32, 32, R.drawable.black_512x512, 32);
+        checkShrinkBitmap(511, 511, R.drawable.black_512x512, 511);
+        checkShrinkBitmap(512, 512, R.drawable.black_512x512, 512);
+
+        checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4096);
+        checkShrinkBitmap(1024, 4096, R.drawable.black_1024x4096, 4100);
+        checkShrinkBitmap(512, 2048, R.drawable.black_1024x4096, 2048);
+
+        checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4096);
+        checkShrinkBitmap(4096, 1024, R.drawable.black_4096x1024, 4100);
+        checkShrinkBitmap(2048, 512, R.drawable.black_4096x1024, 2048);
+    }
+
+    protected File openIconFileForWriteAndGetPath(int userId, String packageName)
+            throws IOException {
+        // Shortcut IDs aren't used in the path, so just pass the same ID.
+        final FileOutputStreamWithPath out =
+                mService.openIconFileForWrite(userId, makePackageShortcut(packageName, "id"));
+        out.close();
+        return out.getFile();
+    }
+
+    public void testOpenIconFileForWrite() throws IOException {
+        mInjectedCurrentTimeMillis = 1000;
+
+        final File p10_1_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
+        final File p10_1_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
+
+        final File p10_2_1 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
+        final File p10_2_2 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
+
+        final File p11_1_1 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
+        final File p11_1_2 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
+
+        mInjectedCurrentTimeMillis++;
+
+        final File p10_1_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
+        final File p10_1_4 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
+        final File p10_1_5 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_1);
+
+        final File p10_2_3 = openIconFileForWriteAndGetPath(10, CALLING_PACKAGE_2);
+        final File p11_1_3 = openIconFileForWriteAndGetPath(11, CALLING_PACKAGE_1);
+
+        // Make sure their paths are all unique
+        assertAllUnique(list(
+                p10_1_1,
+                p10_1_2,
+                p10_1_3,
+                p10_1_4,
+                p10_1_5,
+
+                p10_2_1,
+                p10_2_2,
+                p10_2_3,
+
+                p11_1_1,
+                p11_1_2,
+                p11_1_3
+        ));
+
+        // Check each set has the same parent.
+        assertEquals(p10_1_1.getParent(), p10_1_2.getParent());
+        assertEquals(p10_1_1.getParent(), p10_1_3.getParent());
+        assertEquals(p10_1_1.getParent(), p10_1_4.getParent());
+        assertEquals(p10_1_1.getParent(), p10_1_5.getParent());
+
+        assertEquals(p10_2_1.getParent(), p10_2_2.getParent());
+        assertEquals(p10_2_1.getParent(), p10_2_3.getParent());
+
+        assertEquals(p11_1_1.getParent(), p11_1_2.getParent());
+        assertEquals(p11_1_1.getParent(), p11_1_3.getParent());
+
+        // Check the parents are still unique.
+        assertAllUnique(list(
+                p10_1_1.getParent(),
+                p10_2_1.getParent(),
+                p11_1_1.getParent()
+        ));
+
+        // All files created at the same time for the same package/user, expcet for the first ones,
+        // will have "_" in the path.
+        assertFalse(p10_1_1.getName().contains("_"));
+        assertTrue(p10_1_2.getName().contains("_"));
+        assertFalse(p10_1_3.getName().contains("_"));
+        assertTrue(p10_1_4.getName().contains("_"));
+        assertTrue(p10_1_5.getName().contains("_"));
+
+        assertFalse(p10_2_1.getName().contains("_"));
+        assertTrue(p10_2_2.getName().contains("_"));
+        assertFalse(p10_2_3.getName().contains("_"));
+
+        assertFalse(p11_1_1.getName().contains("_"));
+        assertTrue(p11_1_2.getName().contains("_"));
+        assertFalse(p11_1_3.getName().contains("_"));
+    }
+
+    public void testUpdateShortcuts() {
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"),
+                    makeShortcut("s2"),
+                    makeShortcut("s3"),
+                    makeShortcut("s4"),
+                    makeShortcut("s5")
+            )));
+        });
+        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"),
+                    makeShortcut("s2"),
+                    makeShortcut("s3"),
+                    makeShortcut("s4"),
+                    makeShortcut("s5")
+            )));
+        });
+        runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s2", "s3"),
+                    getCallingUser());
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s4", "s5"),
+                    getCallingUser());
+        });
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            mManager.removeDynamicShortcuts(list("s1"));
+            mManager.removeDynamicShortcuts(list("s2"));
+        });
+        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
+            mManager.removeDynamicShortcuts(list("s1"));
+            mManager.removeDynamicShortcuts(list("s3"));
+            mManager.removeDynamicShortcuts(list("s5"));
+        });
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            assertShortcutIds(assertAllDynamic(
+                    mManager.getDynamicShortcuts()),
+                    "s3", "s4", "s5");
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s2", "s3");
+        });
+        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
+            assertShortcutIds(assertAllDynamic(
+                    mManager.getDynamicShortcuts()),
+                    "s2", "s4");
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s4", "s5");
+        });
+
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            ShortcutInfo s2 = makeShortcutBuilder()
+                    .setId("s2")
+                    .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
+                    .build();
+
+            ShortcutInfo s4 = makeShortcutBuilder()
+                    .setId("s4")
+                    .setTitle("new title")
+                    .build();
+
+            mManager.updateShortcuts(list(s2, s4));
+        });
+        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
+            ShortcutInfo s2 = makeShortcutBuilder()
+                    .setId("s2")
+                    .setIntent(makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
+                            "key1", "val1"))
+                    .build();
+
+            ShortcutInfo s4 = makeShortcutBuilder()
+                    .setId("s4")
+                    .setIntent(new Intent(Intent.ACTION_ALL_APPS))
+                    .build();
+
+            mManager.updateShortcuts(list(s2, s4));
+        });
+
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            assertShortcutIds(assertAllDynamic(
+                    mManager.getDynamicShortcuts()),
+                    "s3", "s4", "s5");
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s2", "s3");
+
+            ShortcutInfo s = getCallerShortcut("s2");
+            assertTrue(s.hasIconResource());
+            assertEquals(R.drawable.black_32x32, s.getIconResourceId());
+            assertEquals("string/r" + R.drawable.black_32x32, s.getIconResName());
+            assertEquals("Title-s2", s.getTitle());
+
+            s = getCallerShortcut("s4");
+            assertFalse(s.hasIconResource());
+            assertEquals(0, s.getIconResourceId());
+            assertEquals("new title", s.getTitle());
+        });
+        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
+            assertShortcutIds(assertAllDynamic(
+                    mManager.getDynamicShortcuts()),
+                    "s2", "s4");
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s4", "s5");
+
+            ShortcutInfo s = getCallerShortcut("s2");
+            assertFalse(s.hasIconResource());
+            assertEquals(0, s.getIconResourceId());
+            assertEquals("Title-s2", s.getTitle());
+            assertEquals(Intent.ACTION_ANSWER, s.getIntent().getAction());
+            assertEquals(1, s.getIntent().getExtras().size());
+
+            s = getCallerShortcut("s4");
+            assertFalse(s.hasIconResource());
+            assertEquals(0, s.getIconResourceId());
+            assertEquals("Title-s4", s.getTitle());
+            assertEquals(Intent.ACTION_ALL_APPS, s.getIntent().getAction());
+            assertBundleEmpty(s.getIntent().getExtras());
+        });
+        // TODO Check with other fields too.
+
+        // TODO Check bitmap removal too.
+
+        mRunningUsers.put(USER_11, true);
+
+        runWithCaller(CALLING_PACKAGE_2, USER_11, () -> {
+            mManager.updateShortcuts(list());
+        });
+    }
+
+    public void testUpdateShortcuts_icons() {
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1")
+            )));
+
+            // Set resource icon
+            assertTrue(mManager.updateShortcuts(list(
+                    new ShortcutInfo.Builder(mClientContext, "s1")
+                    .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
+                    .build()
+            )));
+
+            assertWith(getCallerShortcuts())
+                    .forShortcutWithId("s1", si -> {
+                        assertTrue(si.hasIconResource());
+                        assertEquals(R.drawable.black_32x32, si.getIconResourceId());
+                    });
+
+            // Set bitmap icon
+            assertTrue(mManager.updateShortcuts(list(
+                    new ShortcutInfo.Builder(mClientContext, "s1")
+                    .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
+                            getTestContext().getResources(), R.drawable.black_64x64)))
+                    .build()
+            )));
+
+            assertWith(getCallerShortcuts())
+                    .forShortcutWithId("s1", si -> {
+                        assertTrue(si.hasIconFile());
+                    });
+
+            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
+
+            // Do it again, with the reverse order (bitmap -> icon)
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1")
+            )));
+
+            // Set bitmap icon
+            assertTrue(mManager.updateShortcuts(list(
+                    new ShortcutInfo.Builder(mClientContext, "s1")
+                            .setIcon(Icon.createWithBitmap(BitmapFactory.decodeResource(
+                                    getTestContext().getResources(), R.drawable.black_64x64)))
+                            .build()
+            )));
+
+            assertWith(getCallerShortcuts())
+                    .forShortcutWithId("s1", si -> {
+                        assertTrue(si.hasIconFile());
+                    });
+
+            // Set resource icon
+            assertTrue(mManager.updateShortcuts(list(
+                    new ShortcutInfo.Builder(mClientContext, "s1")
+                            .setIcon(Icon.createWithResource(getTestContext(), R.drawable.black_32x32))
+                            .build()
+            )));
+
+            assertWith(getCallerShortcuts())
+                    .forShortcutWithId("s1", si -> {
+                        assertTrue(si.hasIconResource());
+                        assertEquals(R.drawable.black_32x32, si.getIconResourceId());
+                    });
+        });
+    }
+
+    // === Test for launcher side APIs ===
+
+    public void testGetShortcuts() {
+
+        // Set up shortcuts.
+
+        setCaller(CALLING_PACKAGE_1);
+        final ShortcutInfo s1_1 = makeShortcut("s1");
+        final ShortcutInfo s1_2 = makeShortcut("s2");
+
+        assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
+
+        // Because setDynamicShortcuts will update the timestamps when ranks are changing,
+        // we explicitly set timestamps here.
+        getCallerShortcut("s1").setTimestamp(5000);
+        getCallerShortcut("s2").setTimestamp(1000);
+
+        setCaller(CALLING_PACKAGE_2);
+        final ShortcutInfo s2_2 = makeShortcut("s2");
+        final ShortcutInfo s2_3 = makeShortcutWithActivity("s3",
+                makeComponent(ShortcutActivity2.class));
+        final ShortcutInfo s2_4 = makeShortcutWithActivity("s4",
+                makeComponent(ShortcutActivity.class));
+        assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
+
+        getCallerShortcut("s2").setTimestamp(1500);
+        getCallerShortcut("s3").setTimestamp(3000);
+        getCallerShortcut("s4").setTimestamp(500);
+
+        setCaller(CALLING_PACKAGE_3);
+        final ShortcutInfo s3_2 = makeShortcut("s3");
+        assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
+
+        getCallerShortcut("s3").setTimestamp(START_TIME + 5000);
+
+        setCaller(LAUNCHER_1);
+
+        // Get dynamic
+        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertAllStringsResolved(
+                assertShortcutIds(
+                        assertAllNotKeyFieldsOnly(
+                                mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                        /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                        "s1", "s2")))));
+
+        // Get pinned
+        assertShortcutIds(
+                mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_PINNED), getCallingUser())
+                /* none */);
+
+        // Get both, with timestamp
+        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
+                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 1000, CALLING_PACKAGE_2,
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
+                        getCallingUser())),
+                "s2", "s3"))));
+
+        // FLAG_GET_KEY_FIELDS_ONLY
+        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
+                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 1000, CALLING_PACKAGE_2,
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
+                        getCallingUser())),
+                "s2", "s3"))));
+
+        // Filter by activity
+        assertAllDynamic(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
+                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 0, CALLING_PACKAGE_2,
+                        new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                        ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC),
+                        getCallingUser())),
+                "s4"))));
+
+        // With ID.
+        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
+                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3"),
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
+                        getCallingUser())),
+                "s3"))));
+        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
+                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3", "s2", "ss"),
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
+                        getCallingUser())),
+                "s2", "s3"))));
+        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
+                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 1000, CALLING_PACKAGE_2, list("s3x", "s2x"),
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
+                        getCallingUser()))
+                /* empty */))));
+        assertAllDynamic(assertAllNotHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
+                assertAllKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 1000, CALLING_PACKAGE_2, list(),
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY),
+                        getCallingUser()))
+                /* empty */))));
+
+        // Pin some shortcuts.
+        mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                list("s3", "s4"), getCallingUser());
+
+        // Pinned ones only
+        assertAllPinned(assertAllHaveTitle(assertAllNotHaveIntents(assertShortcutIds(
+                assertAllNotKeyFieldsOnly(mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 1000, CALLING_PACKAGE_2,
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_PINNED),
+                        getCallingUser())),
+                "s3"))));
+
+        // All packages.
+        assertShortcutIds(assertAllNotKeyFieldsOnly(
+                mLauncherApps.getShortcuts(buildQuery(
+                        /* time =*/ 5000, /* package= */ null,
+                        /* activity =*/ null,
+                        ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED),
+                        getCallingUser())),
+                "s1", "s3");
+
+        assertExpectException(
+                IllegalArgumentException.class, "package name must also be set", () -> {
+                    mLauncherApps.getShortcuts(buildQuery(
+                    /* time =*/ 0, /* package= */ null, list("id"),
+                    /* activity =*/ null, /* flags */ 0), getCallingUser());
+                });
+
+        // TODO More tests: pinned but dynamic.
+    }
+
+    public void testGetShortcuts_shortcutKinds() throws Exception {
+        // Create 3 manifest and 3 dynamic shortcuts
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_3);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+
+        // Pin 2 and 3
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "ms3", "s2", "s3"),
+                    HANDLE_USER_0);
+        });
+
+        // Remove ms3 and s3
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"))));
+        });
+
+        // Check their status.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3")
+
+                    .selectByIds("ms1", "ms2")
+                    .areAllManifest()
+                    .areAllImmutable()
+                    .areAllNotDynamic()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms3")
+                    .areAllNotManifest()
+                    .areAllImmutable()
+                    .areAllDisabled()
+                    .areAllNotDynamic()
+
+                    .revertToOriginalList()
+                    .selectByIds("s1", "s2")
+                    .areAllNotManifest()
+                    .areAllMutable()
+                    .areAllDynamic()
+
+                    .revertToOriginalList()
+                    .selectByIds("s3")
+                    .areAllNotManifest()
+                    .areAllMutable()
+                    .areAllEnabled()
+                    .areAllNotDynamic()
+
+                    .revertToOriginalList()
+                    .selectByIds("s1", "ms1")
+                    .areAllNotPinned()
+
+                    .revertToOriginalList()
+                    .selectByIds("s2", "s3", "ms2", "ms3")
+                    .areAllPinned()
+            ;
+        });
+
+        // Finally, actual tests.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertWith(mLauncherApps.getShortcuts(
+                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0))
+                    .haveIds("s1", "s2");
+            assertWith(mLauncherApps.getShortcuts(
+                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_MANIFEST), HANDLE_USER_0))
+                    .haveIds("ms1", "ms2");
+            assertWith(mLauncherApps.getShortcuts(
+                    buildQueryWithFlags(ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0))
+                    .haveIds("s2", "s3", "ms2", "ms3");
+
+            assertWith(mLauncherApps.getShortcuts(
+                    buildQueryWithFlags(
+                            ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_PINNED
+                    ), HANDLE_USER_0))
+                    .haveIds("s1", "s2", "s3", "ms2", "ms3");
+
+            assertWith(mLauncherApps.getShortcuts(
+                    buildQueryWithFlags(
+                            ShortcutQuery.FLAG_GET_MANIFEST | ShortcutQuery.FLAG_GET_PINNED
+                    ), HANDLE_USER_0))
+                    .haveIds("ms1", "s2", "s3", "ms2", "ms3");
+
+            assertWith(mLauncherApps.getShortcuts(
+                    buildQueryWithFlags(
+                            ShortcutQuery.FLAG_GET_DYNAMIC | ShortcutQuery.FLAG_GET_MANIFEST
+                    ), HANDLE_USER_0))
+                    .haveIds("ms1", "ms2", "s1", "s2");
+
+            assertWith(mLauncherApps.getShortcuts(
+                    buildQueryWithFlags(
+                            ShortcutQuery.FLAG_GET_ALL_KINDS
+                    ), HANDLE_USER_0))
+                    .haveIds("ms1", "ms2", "ms3", "s1", "s2", "s3");
+        });
+    }
+
+    public void testGetShortcuts_resolveStrings() throws Exception {
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
+                    .setId("id")
+                    .setActivity(new ComponentName(mClientContext, "dummy"))
+                    .setTitleResId(10)
+                    .setTextResId(11)
+                    .setDisabledMessageResId(12)
+                    .setIntent(makeIntent("action", ShortcutActivity.class))
+                    .build();
+            mManager.setDynamicShortcuts(list(si));
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            ShortcutInfo si = new ShortcutInfo.Builder(mClientContext)
+                    .setId("id")
+                    .setActivity(new ComponentName(mClientContext, "dummy"))
+                    .setTitleResId(10)
+                    .setTextResId(11)
+                    .setDisabledMessageResId(12)
+                    .setIntent(makeIntent("action", ShortcutActivity.class))
+                    .build();
+            mManager.setDynamicShortcuts(list(si));
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            final ShortcutQuery q = new ShortcutQuery();
+            q.setQueryFlags(ShortcutQuery.FLAG_GET_DYNAMIC);
+
+            // USER 0
+            List<ShortcutInfo> ret = assertShortcutIds(
+                    assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_0)),
+                    "id");
+            assertEquals("string-com.android.test.1-user:0-res:10/en", ret.get(0).getTitle());
+            assertEquals("string-com.android.test.1-user:0-res:11/en", ret.get(0).getText());
+            assertEquals("string-com.android.test.1-user:0-res:12/en",
+                    ret.get(0).getDisabledMessage());
+
+            // USER P0
+            ret = assertShortcutIds(
+                    assertAllStringsResolved(mLauncherApps.getShortcuts(q, HANDLE_USER_P0)),
+                    "id");
+            assertEquals("string-com.android.test.1-user:20-res:10/en", ret.get(0).getTitle());
+            assertEquals("string-com.android.test.1-user:20-res:11/en", ret.get(0).getText());
+            assertEquals("string-com.android.test.1-user:20-res:12/en",
+                    ret.get(0).getDisabledMessage());
+        });
+    }
+
+    // TODO resource
+    public void testGetShortcutInfo() {
+        // Create shortcuts.
+        setCaller(CALLING_PACKAGE_1);
+        final ShortcutInfo s1_1 = makeShortcut(
+                "s1",
+                "Title 1",
+                makeComponent(ShortcutActivity.class),
+                /* icon =*/ null,
+                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
+                        "key1", "val1", "nest", makeBundle("key", 123)),
+                /* weight */ 10);
+
+        final ShortcutInfo s1_2 = makeShortcut(
+                "s2",
+                "Title 2",
+                /* activity */ null,
+                /* icon =*/ null,
+                makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
+                /* weight */ 12);
+
+        assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
+        dumpsysOnLogcat();
+
+        setCaller(CALLING_PACKAGE_2);
+        final ShortcutInfo s2_1 = makeShortcut(
+                "s1",
+                "ABC",
+                makeComponent(ShortcutActivity2.class),
+                /* icon =*/ null,
+                makeIntent(Intent.ACTION_ANSWER, ShortcutActivity2.class,
+                        "key1", "val1", "nest", makeBundle("key", 123)),
+                /* weight */ 10);
+        assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
+        dumpsysOnLogcat();
+
+        // Pin some.
+        setCaller(LAUNCHER_1);
+
+        mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                list("s2"), getCallingUser());
+
+        dumpsysOnLogcat();
+
+        // Delete some.
+        setCaller(CALLING_PACKAGE_1);
+        assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
+        mManager.removeDynamicShortcuts(list("s2"));
+        assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
+
+        dumpsysOnLogcat();
+
+        setCaller(LAUNCHER_1);
+        List<ShortcutInfo> list;
+
+        // Note we don't guarantee the orders.
+        list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
+                assertAllNotKeyFieldsOnly(
+                        mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
+                                list("s2", "s1", "s3", null), getCallingUser())))),
+                "s1", "s2");
+        assertEquals("Title 1", findById(list, "s1").getTitle());
+        assertEquals("Title 2", findById(list, "s2").getTitle());
+
+        assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
+                mLauncherApps.getShortcutInfo(CALLING_PACKAGE_1,
+                        list("s3"), getCallingUser())))
+                /* none */);
+
+        list = assertShortcutIds(assertAllHaveTitle(assertAllNotHaveIntents(
+                mLauncherApps.getShortcutInfo(CALLING_PACKAGE_2,
+                        list("s1", "s2", "s3"), getCallingUser()))),
+                "s1");
+        assertEquals("ABC", findById(list, "s1").getTitle());
+    }
+
+    public void testPinShortcutAndGetPinnedShortcuts() {
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
+            final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
+
+            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
+            final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
+            final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
+            assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
+            assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
+        });
+
+        // Pin some.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s2", "s3"), getCallingUser());
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s3", "s4", "s5"), getCallingUser());
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
+                    list("s3"), getCallingUser());  // Note ID doesn't exist
+        });
+
+        // Delete some.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
+            mManager.removeDynamicShortcuts(list("s2"));
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
+
+            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
+            mManager.removeDynamicShortcuts(list("s3"));
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
+
+            assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
+            mManager.removeDynamicShortcuts(list("s2"));
+            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
+
+            assertEmpty(mManager.getDynamicShortcuts());
+        });
+
+        // Get pinned shortcuts from launcher
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
+                    "s2");
+
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
+                    "s3", "s4");
+
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
+                    /* none */);
+        });
+    }
+
+    /**
+     * This is similar to the above test, except it used "disable" instead of "remove".  It also
+     * does "enable".
+     */
+    public void testDisableAndEnableShortcuts() {
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ShortcutInfo s1_1 = makeShortcutWithTimestamp("s1", 1000);
+            final ShortcutInfo s1_2 = makeShortcutWithTimestamp("s2", 2000);
+
+            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2)));
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            final ShortcutInfo s2_2 = makeShortcutWithTimestamp("s2", 1500);
+            final ShortcutInfo s2_3 = makeShortcutWithTimestamp("s3", 3000);
+            final ShortcutInfo s2_4 = makeShortcutWithTimestamp("s4", 500);
+            assertTrue(mManager.setDynamicShortcuts(list(s2_2, s2_3, s2_4)));
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            final ShortcutInfo s3_2 = makeShortcutWithTimestamp("s2", 1000);
+            assertTrue(mManager.setDynamicShortcuts(list(s3_2)));
+        });
+
+        // Pin some.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s2", "s3"), getCallingUser());
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s3", "s4", "s5"), getCallingUser());
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3,
+                    list("s3"), getCallingUser());  // Note ID doesn't exist
+        });
+
+        // Disable some.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
+
+            mManager.disableShortcuts(list("s2"));
+
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
+            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
+
+            // disable should work even if a shortcut is not dynamic, so try calling "remove" first
+            // here.
+            mManager.removeDynamicShortcuts(list("s3"));
+            mManager.disableShortcuts(list("s3"));
+
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s3", "s4");
+            assertShortcutIds(mManager.getDynamicShortcuts(), "s2", "s4");
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
+
+            mManager.disableShortcuts(list("s2"));
+
+            assertShortcutIds(mManager.getPinnedShortcuts() /* none */);
+
+            assertEmpty(mManager.getDynamicShortcuts());
+            assertEmpty(getCallerShortcuts());
+        });
+
+        // Get pinned shortcuts from launcher
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists, and disabled.
+            assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
+                    .haveIds("s2")
+                    .areAllPinned()
+                    .areAllNotWithKeyFieldsOnly()
+                    .areAllDisabled();
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
+                    ActivityNotFoundException.class);
+
+            // Here, s4 is still enabled and launchable, but s3 is disabled.
+            assertWith(mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))
+                    .haveIds("s3", "s4")
+                    .areAllPinned()
+                    .areAllNotWithKeyFieldsOnly()
+
+                    .selectByIds("s3")
+                    .areAllDisabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("s4")
+                    .areAllEnabled();
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
+                    ActivityNotFoundException.class);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s4", USER_0);
+
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_3,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))))
+                    /* none */);
+        });
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.enableShortcuts(list("s2"));
+
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s2");
+            assertShortcutIds(mManager.getDynamicShortcuts(), "s1");
+        });
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            // CALLING_PACKAGE_1 deleted s2, but it's pinned, so it still exists.
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(assertAllEnabled(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))),
+                    "s2");
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
+        });
+    }
+
+    public void testDisableShortcuts_thenRepublish() {
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+
+            runWithCaller(LAUNCHER_1, USER_0, () -> {
+                mLauncherApps.pinShortcuts(
+                        CALLING_PACKAGE_1, list("s1", "s2", "s3"), HANDLE_USER_0);
+            });
+
+            mManager.disableShortcuts(list("s1", "s2", "s3"));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1", "s2", "s3")
+                    .areAllNotDynamic()
+                    .areAllPinned()
+                    .areAllDisabled();
+
+            // Make sure updateShortcuts() will not re-enable them.
+            assertTrue(mManager.updateShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1", "s2", "s3")
+                    .areAllNotDynamic()
+                    .areAllPinned()
+                    .areAllDisabled();
+
+            // Re-publish s1 with setDynamicShortcuts.
+            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
+
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"))));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1", "s2", "s3")
+
+                    .selectByIds("s1")
+                    .areAllDynamic()
+                    .areAllPinned()
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("s2", "s3")
+                    .areAllNotDynamic()
+                    .areAllPinned()
+                    .areAllDisabled();
+
+            // Re-publish s2 with addDynamicShortcuts.
+            mInjectedCurrentTimeMillis += INTERVAL; // reset throttling
+
+            assertTrue(mManager.addDynamicShortcuts(list(
+                    makeShortcut("s2"))));
+
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1", "s2", "s3")
+
+                    .selectByIds("s1", "s2")
+                    .areAllDynamic()
+                    .areAllPinned()
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("s3")
+                    .areAllNotDynamic()
+                    .areAllPinned()
+                    .areAllDisabled();
+        });
+    }
+
+    public void testPinShortcutAndGetPinnedShortcuts_multi() {
+        // Create some shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+
+        dumpsysOnLogcat();
+
+        // Pin some.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s3", "s4"), getCallingUser());
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s1", "s2", "s4"), getCallingUser());
+        });
+
+        dumpsysOnLogcat();
+
+        // Delete some.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
+            mManager.removeDynamicShortcuts(list("s3"));
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s3");
+        });
+
+        dumpsysOnLogcat();
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
+            mManager.removeDynamicShortcuts(list("s1"));
+            mManager.removeDynamicShortcuts(list("s3"));
+            assertShortcutIds(mManager.getPinnedShortcuts(), "s1", "s2");
+        });
+
+        dumpsysOnLogcat();
+
+        // Get pinned shortcuts from launcher
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s3");
+
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s1", "s2");
+
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
+                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                    "s1", "s2", "s3");
+
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
+                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                    "s1", "s2");
+        });
+
+        dumpsysOnLogcat();
+
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            // Launcher2 still has no pinned ones.
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
+                    /* none */);
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser())))
+                    /* none */);
+
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
+                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                    "s1", "s2");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
+                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                    "s2");
+
+            // Now pin some.
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s1", "s2"), getCallingUser());
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s1", "s2"), getCallingUser());
+
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
+                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                    "s1", "s2");
+
+            // S1 was not visible to it, so shouldn't be pinned.
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
+                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                    "s2");
+        });
+
+        // Re-initialize and load from the files.
+        mService.saveDirtyInfo();
+        initService();
+
+        // Load from file.
+        mService.handleUnlockUser(USER_0);
+
+        // Make sure package info is restored too.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s3");
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s1", "s2");
+        });
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
+                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                    "s1", "s2");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED
+                                    | ShortcutQuery.FLAG_GET_DYNAMIC), getCallingUser())),
+                    "s2");
+        });
+
+        // Delete all dynamic.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.removeAllDynamicShortcuts();
+
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            mManager.removeAllDynamicShortcuts();
+
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2", "s1");
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s3");
+
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s1", "s2");
+
+            // from all packages.
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, null,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s1", "s2", "s3");
+
+            // Update pined.  Note s2 and s3 are actually available, but not visible to this
+            // launcher, so still can't be pinned.
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
+                    getCallingUser());
+
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s3");
+        });
+        // Re-publish s1.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s1"))));
+
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2", "s3");
+        });
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s3");
+
+            // Now "s1" is visible, so can be pinned.
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s1", "s2", "s3", "s4"),
+                    getCallingUser());
+
+            assertShortcutIds(assertAllPinned(assertAllNotKeyFieldsOnly(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()))),
+                    "s1", "s3");
+        });
+
+        // Now clear pinned shortcuts.  First, from launcher 1.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
+
+            assertEquals(0,
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
+            assertEquals(0,
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s1", "s2");
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()), "s2");
+        });
+
+        // Clear all pins from launcher 2.
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list(), getCallingUser());
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list(), getCallingUser());
+
+            assertEquals(0,
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
+            assertEquals(0,
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), getCallingUser()).size());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()), "s1");
+            assertEquals(0, mManager.getPinnedShortcuts().size());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertEquals(0, mManager.getPinnedShortcuts().size());
+        });
+    }
+
+    public void testPinShortcutAndGetPinnedShortcuts_crossProfile_plusLaunch() {
+        // Create some shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
+                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
+        });
+
+        // Pin some shortcuts and see the result.
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s1"), HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s1", "s2", "s3"), HANDLE_USER_0);
+        });
+
+        runWithCaller(LAUNCHER_1, USER_P0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s2"), HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s2", "s3"), HANDLE_USER_0);
+        });
+
+        runWithCaller(LAUNCHER_2, USER_P0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s3"), HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s3"), HANDLE_USER_0);
+        });
+
+        runWithCaller(LAUNCHER_2, USER_10, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s1", "s2", "s3"), HANDLE_USER_10);
+        });
+
+        // Cross profile pinning.
+        final int PIN_AND_DYNAMIC = ShortcutQuery.FLAG_GET_PINNED | ShortcutQuery.FLAG_GET_DYNAMIC;
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+        runWithCaller(LAUNCHER_1, USER_P0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s2");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+        runWithCaller(LAUNCHER_2, USER_P0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+        runWithCaller(LAUNCHER_2, USER_10, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
+                    "s1", "s2", "s3", "s4", "s5", "s6");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
+                    "s1", "s2", "s3", "s4", "s5", "s6");
+        });
+
+        // Remove some dynamic shortcuts.
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"))));
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"))));
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
+                    ActivityNotFoundException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
+                    ActivityNotFoundException.class);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+        runWithCaller(LAUNCHER_1, USER_P0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s2");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
+                    ActivityNotFoundException.class);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+        runWithCaller(LAUNCHER_2, USER_P0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s3");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
+                    ActivityNotFoundException.class);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
+                    ActivityNotFoundException.class);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+        runWithCaller(LAUNCHER_2, USER_10, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_10)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_10)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_10)),
+                    "s1", "s2", "s3");
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
+                    SecurityException.class);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s1", USER_0,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s3", USER_0,
+                    SecurityException.class);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_10);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_10);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_10);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    ActivityNotFoundException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    ActivityNotFoundException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    ActivityNotFoundException.class);
+        });
+
+        // Save & load and make sure we still have the same information.
+        mService.saveDirtyInfo();
+        initService();
+        mService.handleUnlockUser(USER_0);
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
+                    ActivityNotFoundException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
+                    ActivityNotFoundException.class);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+        runWithCaller(LAUNCHER_1, USER_P0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s2");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s2", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s2", USER_0);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_0,
+                    ActivityNotFoundException.class);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s2", USER_0);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+        runWithCaller(LAUNCHER_2, USER_P0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_1,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s3");
+
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_PINNED), HANDLE_USER_0)),
+                    "s3");
+            assertShortcutIds(assertAllDynamic(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, ShortcutQuery.FLAG_GET_DYNAMIC), HANDLE_USER_0)),
+                    "s1");
+            assertShortcutIds(assertAllDynamicOrPinned(
+                    mLauncherApps.getShortcuts(buildQuery(/* time =*/ 0, CALLING_PACKAGE_2,
+                    /* activity =*/ null, PIN_AND_DYNAMIC), HANDLE_USER_0)),
+                    "s1", "s3");
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s1", USER_0);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_0,
+                    ActivityNotFoundException.class);
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
+
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s1", USER_0);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_2, "s2", USER_0,
+                    ActivityNotFoundException.class);
+            assertShortcutLaunchable(CALLING_PACKAGE_2, "s3", USER_0);
+
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s2", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s3", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s4", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s5", USER_10,
+                    SecurityException.class);
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s6", USER_10,
+                    SecurityException.class);
+        });
+    }
+
+    public void testStartShortcut() {
+        // Create some shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ShortcutInfo s1_1 = makeShortcut(
+                    "s1",
+                    "Title 1",
+                    makeComponent(ShortcutActivity.class),
+                    /* icon =*/ null,
+                    new Intent[] {makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
+                            "key1", "val1", "nest", makeBundle("key", 123))
+                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
+                    new Intent("act2").setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)},
+                    /* rank */ 10);
+
+            final ShortcutInfo s1_2 = makeShortcut(
+                    "s2",
+                    "Title 2",
+            /* activity */ null,
+            /* icon =*/ null,
+                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
+            /* rank */ 12);
+
+            final ShortcutInfo s1_3 = makeShortcut("s3");
+
+            assertTrue(mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3)));
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            final ShortcutInfo s2_1 = makeShortcut(
+                    "s1",
+                    "ABC",
+                    makeComponent(ShortcutActivity.class),
+                    /* icon =*/ null,
+                    makeIntent(Intent.ACTION_ANSWER, ShortcutActivity.class,
+                            "key1", "val1", "nest", makeBundle("key", 123)),
+                    /* weight */ 10);
+            assertTrue(mManager.setDynamicShortcuts(list(s2_1)));
+        });
+
+        // Pin some.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s1", "s2"), getCallingUser());
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s1"), getCallingUser());
+        });
+
+        // Just to make it complicated, delete some.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.removeDynamicShortcuts(list("s2"));
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
+            assertEquals(ShortcutActivity2.class.getName(),
+                    intents[0].getComponent().getClassName());
+            assertEquals(Intent.ACTION_ASSIST,
+                    intents[0].getAction());
+            assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
+                    intents[0].getFlags());
+
+            assertEquals("act2",
+                    intents[1].getAction());
+            assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
+                    intents[1].getFlags());
+
+            assertEquals(
+                    ShortcutActivity3.class.getName(),
+                    launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
+                            .getComponent().getClassName());
+            assertEquals(
+                    ShortcutActivity.class.getName(),
+                    launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
+                            .getComponent().getClassName());
+
+            assertShortcutLaunchable(CALLING_PACKAGE_1, "s3", USER_0);
+
+            assertShortcutNotLaunched("no-such-package", "s2", USER_0);
+            assertShortcutNotLaunched(CALLING_PACKAGE_1, "xxxx", USER_0);
+        });
+
+        // LAUNCHER_1 is no longer the default launcher
+        setDefaultLauncherChecker((pkg, userId) -> false);
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            // Not the default launcher, but pinned shortcuts are still lauchable.
+            final Intent[] intents = launchShortcutAndGetIntents(CALLING_PACKAGE_1, "s1", USER_0);
+            assertEquals(ShortcutActivity2.class.getName(),
+                    intents[0].getComponent().getClassName());
+            assertEquals(Intent.ACTION_ASSIST,
+                    intents[0].getAction());
+            assertEquals(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK,
+                    intents[0].getFlags());
+
+            assertEquals("act2",
+                    intents[1].getAction());
+            assertEquals(Intent.FLAG_ACTIVITY_NO_ANIMATION,
+                    intents[1].getFlags());
+            assertEquals(
+                    ShortcutActivity3.class.getName(),
+                    launchShortcutAndGetIntent(CALLING_PACKAGE_1, "s2", USER_0)
+                            .getComponent().getClassName());
+            assertEquals(
+                    ShortcutActivity.class.getName(),
+                    launchShortcutAndGetIntent(CALLING_PACKAGE_2, "s1", USER_0)
+                            .getComponent().getClassName());
+
+            // Not pinned, so not lauchable.
+        });
+
+        // Test inner errors.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            // Not launchable.
+            doReturn(ActivityManager.START_CLASS_NOT_FOUND)
+                    .when(mMockActivityManagerInternal).startActivitiesAsPackage(
+                            anyString(), anyInt(), any(Intent[].class), any(Bundle.class));
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
+                    ActivityNotFoundException.class);
+
+            // Still not launchable.
+            doReturn(ActivityManager.START_CLASS_NOT_FOUND)
+                    .when(mMockActivityManagerInternal)
+                    .startActivitiesAsPackage(
+                            anyString(), anyInt(), any(Intent[].class), any(Bundle.class));
+            assertStartShortcutThrowsException(CALLING_PACKAGE_1, "s1", USER_0,
+                    ActivityNotFoundException.class);
+        });
+
+
+        // TODO Check extra, etc
+    }
+
+    public void testLauncherCallback() throws Throwable {
+        // Disable throttling for this test.
+        mService.updateConfigurationLocked(
+                ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999,"
+                        + ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999"
+        );
+
+        setCaller(LAUNCHER_1, USER_0);
+
+        assertForLauncherCallback(mLauncherApps, () -> {
+            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+                assertTrue(mManager.setDynamicShortcuts(list(
+                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+            });
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                .haveIds("s1", "s2", "s3")
+                .areAllWithKeyFieldsOnly()
+                .areAllDynamic();
+
+        // From different package.
+        assertForLauncherCallback(mLauncherApps, () -> {
+            runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+                assertTrue(mManager.setDynamicShortcuts(list(
+                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+            });
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
+                .haveIds("s1", "s2", "s3")
+                .areAllWithKeyFieldsOnly()
+                .areAllDynamic();
+
+        mRunningUsers.put(USER_10, true);
+
+        // Different user, callback shouldn't be called.
+        assertForLauncherCallback(mLauncherApps, () -> {
+            runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+                assertTrue(mManager.setDynamicShortcuts(list(
+                        makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+            });
+        }).assertNoCallbackCalled();
+
+
+        // Test for addDynamicShortcuts.
+        assertForLauncherCallback(mLauncherApps, () -> {
+            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+                assertTrue(mManager.addDynamicShortcuts(list(makeShortcut("s4"))));
+            });
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                .haveIds("s1", "s2", "s3", "s4")
+                .areAllWithKeyFieldsOnly()
+                .areAllDynamic();
+
+        // Test for remove
+        assertForLauncherCallback(mLauncherApps, () -> {
+            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+                mManager.removeDynamicShortcuts(list("s1"));
+            });
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                .haveIds("s2", "s3", "s4")
+                .areAllWithKeyFieldsOnly()
+                .areAllDynamic();
+
+        // Test for update
+        assertForLauncherCallback(mLauncherApps, () -> {
+            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+                assertTrue(mManager.updateShortcuts(list(
+                        makeShortcut("s1"), makeShortcut("s2"))));
+            });
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                // All remaining shortcuts will be passed regardless of what's been updated.
+                .haveIds("s2", "s3", "s4")
+                .areAllWithKeyFieldsOnly()
+                .areAllDynamic();
+
+        // Test for deleteAll
+        assertForLauncherCallback(mLauncherApps, () -> {
+            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+                mManager.removeAllDynamicShortcuts();
+            });
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                .isEmpty();
+
+        // Update package1 with manifest shortcuts
+        assertForLauncherCallback(mLauncherApps, () -> {
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    R.xml.shortcut_2);
+            updatePackageVersion(CALLING_PACKAGE_1, 1);
+            mService.mPackageMonitor.onReceive(getTestContext(),
+                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                .areAllManifest()
+                .areAllWithKeyFieldsOnly()
+                .haveIds("ms1", "ms2");
+
+        // Make sure pinned shortcuts are passed too.
+        // 1. Add dynamic shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"))));
+        });
+
+        // 2. Pin some.
+        runWithCaller(LAUNCHER_1, UserHandle.USER_SYSTEM, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_0);
+        });
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "s1", "s2")
+                    .areAllEnabled()
+
+                    .selectByIds("ms1", "ms2")
+                    .areAllManifest()
+
+                    .revertToOriginalList()
+                    .selectByIds("s1", "s2")
+                    .areAllDynamic()
+                    ;
+        });
+
+        // 3 Update the app with no manifest shortcuts.  (Pinned one will survive.)
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_0);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        assertForLauncherCallback(mLauncherApps, () -> {
+            runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+                mManager.removeDynamicShortcuts(list("s2"));
+
+                assertWith(getCallerShortcuts())
+                        .haveIds("ms2", "s1", "s2")
+
+                        .selectByIds("ms2")
+                        .areAllNotManifest()
+                        .areAllPinned()
+                        .areAllImmutable()
+                        .areAllDisabled()
+
+                        .revertToOriginalList()
+                        .selectByIds("s1")
+                        .areAllDynamic()
+                        .areAllNotPinned()
+                        .areAllEnabled()
+
+                        .revertToOriginalList()
+                        .selectByIds("s2")
+                        .areAllNotDynamic()
+                        .areAllPinned()
+                        .areAllEnabled()
+                ;
+            });
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                .haveIds("ms2", "s1", "s2")
+                .areAllWithKeyFieldsOnly();
+
+        // Remove CALLING_PACKAGE_2
+        assertForLauncherCallback(mLauncherApps, () -> {
+            uninstallPackage(USER_0, CALLING_PACKAGE_2);
+            mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_0, USER_0,
+                    /* appStillExists = */ false);
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_2, HANDLE_USER_0)
+                .isEmpty();
+    }
+
+    public void testLauncherCallback_crossProfile() throws Throwable {
+        prepareCrossProfileDataSet();
+
+        final Handler h = new Handler(Looper.getMainLooper());
+
+        final LauncherApps.Callback c0_1 = mock(LauncherApps.Callback.class);
+        final LauncherApps.Callback c0_2 = mock(LauncherApps.Callback.class);
+        final LauncherApps.Callback c0_3 = mock(LauncherApps.Callback.class);
+        final LauncherApps.Callback c0_4 = mock(LauncherApps.Callback.class);
+
+        final LauncherApps.Callback cP0_1 = mock(LauncherApps.Callback.class);
+        final LauncherApps.Callback c10_1 = mock(LauncherApps.Callback.class);
+        final LauncherApps.Callback c10_2 = mock(LauncherApps.Callback.class);
+        final LauncherApps.Callback c11_1 = mock(LauncherApps.Callback.class);
+
+        final List<LauncherApps.Callback> all =
+                list(c0_1, c0_2, c0_3, c0_4, cP0_1, c10_1, c11_1);
+
+        setDefaultLauncherChecker((pkg, userId) -> {
+            switch (userId) {
+                case USER_0:
+                    return LAUNCHER_2.equals(pkg);
+                case USER_P0:
+                    return LAUNCHER_1.equals(pkg);
+                case USER_10:
+                    return LAUNCHER_1.equals(pkg);
+                case USER_11:
+                    return LAUNCHER_1.equals(pkg);
+                default:
+                    return false;
+            }
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> mLauncherApps.registerCallback(c0_1, h));
+        runWithCaller(LAUNCHER_2, USER_0, () -> mLauncherApps.registerCallback(c0_2, h));
+        runWithCaller(LAUNCHER_3, USER_0, () -> mLauncherApps.registerCallback(c0_3, h));
+        runWithCaller(LAUNCHER_4, USER_0, () -> mLauncherApps.registerCallback(c0_4, h));
+        runWithCaller(LAUNCHER_1, USER_P0, () -> mLauncherApps.registerCallback(cP0_1, h));
+        runWithCaller(LAUNCHER_1, USER_10, () -> mLauncherApps.registerCallback(c10_1, h));
+        runWithCaller(LAUNCHER_2, USER_10, () -> mLauncherApps.registerCallback(c10_2, h));
+        runWithCaller(LAUNCHER_1, USER_11, () -> mLauncherApps.registerCallback(c11_1, h));
+
+        // User 0.
+
+        resetAll(all);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.removeDynamicShortcuts(list());
+        });
+        waitOnMainThread();
+
+        assertCallbackNotReceived(c0_1);
+        assertCallbackNotReceived(c0_3);
+        assertCallbackNotReceived(c0_4);
+        assertCallbackNotReceived(c10_1);
+        assertCallbackNotReceived(c10_2);
+        assertCallbackNotReceived(c11_1);
+        assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3");
+        assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
+
+        // User 0, different package.
+
+        resetAll(all);
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            mManager.removeDynamicShortcuts(list());
+        });
+        waitOnMainThread();
+
+        assertCallbackNotReceived(c0_1);
+        assertCallbackNotReceived(c0_3);
+        assertCallbackNotReceived(c0_4);
+        assertCallbackNotReceived(c10_1);
+        assertCallbackNotReceived(c10_2);
+        assertCallbackNotReceived(c11_1);
+        assertCallbackReceived(c0_2, HANDLE_USER_0, CALLING_PACKAGE_3, "s1", "s2", "s3", "s4");
+        assertCallbackReceived(cP0_1, HANDLE_USER_0, CALLING_PACKAGE_3,
+                "s1", "s2", "s3", "s4", "s5", "s6");
+
+        // Work profile.
+        resetAll(all);
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            mManager.removeDynamicShortcuts(list());
+        });
+        waitOnMainThread();
+
+        assertCallbackNotReceived(c0_1);
+        assertCallbackNotReceived(c0_3);
+        assertCallbackNotReceived(c0_4);
+        assertCallbackNotReceived(c10_1);
+        assertCallbackNotReceived(c10_2);
+        assertCallbackNotReceived(c11_1);
+        assertCallbackReceived(c0_2, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s5");
+        assertCallbackReceived(cP0_1, HANDLE_USER_P0, CALLING_PACKAGE_1, "s1", "s2", "s3", "s4");
+
+        // Normal secondary user.
+        mRunningUsers.put(USER_10, true);
+
+        resetAll(all);
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            mManager.removeDynamicShortcuts(list());
+        });
+        waitOnMainThread();
+
+        assertCallbackNotReceived(c0_1);
+        assertCallbackNotReceived(c0_2);
+        assertCallbackNotReceived(c0_3);
+        assertCallbackNotReceived(c0_4);
+        assertCallbackNotReceived(cP0_1);
+        assertCallbackNotReceived(c10_2);
+        assertCallbackNotReceived(c11_1);
+        assertCallbackReceived(c10_1, HANDLE_USER_10, CALLING_PACKAGE_1,
+                "x1", "x2", "x3", "x4", "x5");
+    }
+
+    // === Test for persisting ===
+
+    public void testSaveAndLoadUser_empty() {
+        assertTrue(mManager.setDynamicShortcuts(list()));
+
+        Log.i(TAG, "Saved state");
+        dumpsysOnLogcat();
+        dumpUserFile(0);
+
+        // Restore.
+        mService.saveDirtyInfo();
+        initService();
+
+        assertEquals(0, mManager.getDynamicShortcuts().size());
+    }
+
+    /**
+     * Try save and load, also stop/start the user.
+     */
+    public void testSaveAndLoadUser() {
+        // First, create some shortcuts and save.
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x16);
+            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                    getTestContext().getResources(), R.drawable.icon2));
+
+            final ShortcutInfo si1 = makeShortcut(
+                    "s1",
+                    "title1-1",
+                    makeComponent(ShortcutActivity.class),
+                    icon1,
+                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
+                            "key1", "val1", "nest", makeBundle("key", 123)),
+                        /* weight */ 10);
+
+            final ShortcutInfo si2 = makeShortcut(
+                    "s2",
+                    "title1-2",
+                        /* activity */ null,
+                    icon2,
+                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
+                        /* weight */ 12);
+
+            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
+
+            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+            assertEquals(2, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
+            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_16x64);
+            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                    getTestContext().getResources(), R.drawable.icon2));
+
+            final ShortcutInfo si1 = makeShortcut(
+                    "s1",
+                    "title2-1",
+                    makeComponent(ShortcutActivity.class),
+                    icon1,
+                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
+                            "key1", "val1", "nest", makeBundle("key", 123)),
+                        /* weight */ 10);
+
+            final ShortcutInfo si2 = makeShortcut(
+                    "s2",
+                    "title2-2",
+                        /* activity */ null,
+                    icon2,
+                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
+                        /* weight */ 12);
+
+            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
+
+            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+            assertEquals(2, mManager.getRemainingCallCount());
+        });
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            final Icon icon1 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
+            final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                    getTestContext().getResources(), R.drawable.icon2));
+
+            final ShortcutInfo si1 = makeShortcut(
+                    "s1",
+                    "title10-1-1",
+                    makeComponent(ShortcutActivity.class),
+                    icon1,
+                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity2.class,
+                            "key1", "val1", "nest", makeBundle("key", 123)),
+                        /* weight */ 10);
+
+            final ShortcutInfo si2 = makeShortcut(
+                    "s2",
+                    "title10-1-2",
+                        /* activity */ null,
+                    icon2,
+                    makeIntent(Intent.ACTION_ASSIST, ShortcutActivity3.class),
+                        /* weight */ 12);
+
+            assertTrue(mManager.setDynamicShortcuts(list(si1, si2)));
+
+            assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+            assertEquals(2, mManager.getRemainingCallCount());
+        });
+
+        mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM).setLauncher(
+                new ComponentName("pkg1", "class"));
+
+        // Restore.
+        mService.saveDirtyInfo();
+        initService();
+
+        // Before the load, the map should be empty.
+        assertEquals(0, mService.getShortcutsForTest().size());
+
+        // this will pre-load the per-user info.
+        mService.handleUnlockUser(UserHandle.USER_SYSTEM);
+
+        // Now it's loaded.
+        assertEquals(1, mService.getShortcutsForTest().size());
+
+        runWithCaller(CALLING_PACKAGE_1, UserHandle.USER_SYSTEM, () -> {
+            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
+                    mManager.getDynamicShortcuts()))), "s1", "s2");
+            assertEquals(2, mManager.getRemainingCallCount());
+
+            assertEquals("title1-1", getCallerShortcut("s1").getTitle());
+            assertEquals("title1-2", getCallerShortcut("s2").getTitle());
+        });
+        runWithCaller(CALLING_PACKAGE_2, UserHandle.USER_SYSTEM, () -> {
+            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
+                    mManager.getDynamicShortcuts()))), "s1", "s2");
+            assertEquals(2, mManager.getRemainingCallCount());
+
+            assertEquals("title2-1", getCallerShortcut("s1").getTitle());
+            assertEquals("title2-2", getCallerShortcut("s2").getTitle());
+        });
+
+        assertEquals("pkg1", mService.getShortcutsForTest().get(UserHandle.USER_SYSTEM)
+                .getLastKnownLauncher().getPackageName());
+
+        // Start another user
+        mService.handleUnlockUser(USER_10);
+
+        // Now the size is 2.
+        assertEquals(2, mService.getShortcutsForTest().size());
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertShortcutIds(assertAllDynamic(assertAllHaveIntents(assertAllHaveIcon(
+                    mManager.getDynamicShortcuts()))), "s1", "s2");
+            assertEquals(2, mManager.getRemainingCallCount());
+
+            assertEquals("title10-1-1", getCallerShortcut("s1").getTitle());
+            assertEquals("title10-1-2", getCallerShortcut("s2").getTitle());
+        });
+        assertNull(mService.getShortcutsForTest().get(USER_10).getLastKnownLauncher());
+
+        // Try stopping the user
+        mService.handleCleanupUser(USER_10);
+
+        // Now it's unloaded.
+        assertEquals(1, mService.getShortcutsForTest().size());
+
+        // TODO Check all other fields
+    }
+
+    public void testCleanupPackage() {
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s0_1"))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s0_2"))));
+        });
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
+                    HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
+                    HANDLE_USER_0);
+        });
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s0_1"),
+                    HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s0_2"),
+                    HANDLE_USER_0);
+        });
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s10_1"))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s10_2"))));
+        });
+        runWithCaller(LAUNCHER_1, USER_10, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
+                    HANDLE_USER_10);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
+                    HANDLE_USER_10);
+        });
+        runWithCaller(LAUNCHER_2, USER_10, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s10_1"),
+                    HANDLE_USER_10);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("s10_2"),
+                    HANDLE_USER_10);
+        });
+
+        // Remove all dynamic shortcuts; now all shortcuts are just pinned.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.removeAllDynamicShortcuts();
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            mManager.removeAllDynamicShortcuts();
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            mManager.removeAllDynamicShortcuts();
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            mManager.removeAllDynamicShortcuts();
+        });
+
+
+        final SparseArray<ShortcutUser> users =  mService.getShortcutsForTest();
+        assertEquals(2, users.size());
+        assertEquals(USER_0, users.keyAt(0));
+        assertEquals(USER_10, users.keyAt(1));
+
+        final ShortcutUser user0 =  users.get(USER_0);
+        final ShortcutUser user10 =  users.get(USER_10);
+
+
+        // Check the registered packages.
+        dumpsysOnLogcat();
+        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
+                hashSet(user0.getAllPackagesForTest().keySet()));
+        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
+                hashSet(user10.getAllPackagesForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_0, LAUNCHER_1),
+                        PackageWithUser.of(USER_0, LAUNCHER_2)),
+                hashSet(user0.getAllLaunchersForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_10, LAUNCHER_1),
+                        PackageWithUser.of(USER_10, LAUNCHER_2)),
+                hashSet(user10.getAllLaunchersForTest().keySet()));
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
+                "s0_1", "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
+                "s0_1", "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
+                "s10_1", "s10_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
+                "s10_1", "s10_2");
+        assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
+        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
+
+        mService.saveDirtyInfo();
+
+        // Nonexistent package.
+        uninstallPackage(USER_0, "abc");
+        mService.cleanUpPackageLocked("abc", USER_0, USER_0, /* appStillExists = */ false);
+
+        // No changes.
+        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
+                hashSet(user0.getAllPackagesForTest().keySet()));
+        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
+                hashSet(user10.getAllPackagesForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_0, LAUNCHER_1),
+                        PackageWithUser.of(USER_0, LAUNCHER_2)),
+                hashSet(user0.getAllLaunchersForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_10, LAUNCHER_1),
+                        PackageWithUser.of(USER_10, LAUNCHER_2)),
+                hashSet(user10.getAllLaunchersForTest().keySet()));
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
+                "s0_1", "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
+                "s0_1", "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
+                "s10_1", "s10_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
+                "s10_1", "s10_2");
+        assertShortcutExists(CALLING_PACKAGE_1, "s0_1", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
+        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
+
+        mService.saveDirtyInfo();
+
+        // Remove a package.
+        uninstallPackage(USER_0, CALLING_PACKAGE_1);
+        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
+                /* appStillExists = */ false);
+
+        assertEquals(set(CALLING_PACKAGE_2),
+                hashSet(user0.getAllPackagesForTest().keySet()));
+        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
+                hashSet(user10.getAllPackagesForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_0, LAUNCHER_1),
+                        PackageWithUser.of(USER_0, LAUNCHER_2)),
+                hashSet(user0.getAllLaunchersForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_10, LAUNCHER_1),
+                        PackageWithUser.of(USER_10, LAUNCHER_2)),
+                hashSet(user10.getAllLaunchersForTest().keySet()));
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
+                "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
+                "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_10),
+                "s10_1", "s10_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
+                "s10_1", "s10_2");
+        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
+        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
+
+        mService.saveDirtyInfo();
+
+        // Remove a launcher.
+        uninstallPackage(USER_10, LAUNCHER_1);
+        mService.cleanUpPackageLocked(LAUNCHER_1, USER_10, USER_10, /* appStillExists = */ false);
+
+        assertEquals(set(CALLING_PACKAGE_2),
+                hashSet(user0.getAllPackagesForTest().keySet()));
+        assertEquals(set(CALLING_PACKAGE_1, CALLING_PACKAGE_2),
+                hashSet(user10.getAllPackagesForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_0, LAUNCHER_1),
+                        PackageWithUser.of(USER_0, LAUNCHER_2)),
+                hashSet(user0.getAllLaunchersForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_10, LAUNCHER_2)),
+                hashSet(user10.getAllLaunchersForTest().keySet()));
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
+                "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
+                "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
+                "s10_1", "s10_2");
+        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
+        assertShortcutExists(CALLING_PACKAGE_2, "s10_2", USER_10);
+
+        mService.saveDirtyInfo();
+
+        // Remove a package.
+        uninstallPackage(USER_10, CALLING_PACKAGE_2);
+        mService.cleanUpPackageLocked(CALLING_PACKAGE_2, USER_10, USER_10,
+                /* appStillExists = */ false);
+
+        assertEquals(set(CALLING_PACKAGE_2),
+                hashSet(user0.getAllPackagesForTest().keySet()));
+        assertEquals(set(CALLING_PACKAGE_1),
+                hashSet(user10.getAllPackagesForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_0, LAUNCHER_1),
+                        PackageWithUser.of(USER_0, LAUNCHER_2)),
+                hashSet(user0.getAllLaunchersForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_10, LAUNCHER_2)),
+                hashSet(user10.getAllLaunchersForTest().keySet()));
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
+                "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
+                "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_10),
+                "s10_1");
+        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_1, "s10_1", USER_10);
+        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
+
+        mService.saveDirtyInfo();
+
+        // Remove the other launcher from user 10 too.
+        uninstallPackage(USER_10, LAUNCHER_2);
+        mService.cleanUpPackageLocked(LAUNCHER_2, USER_10, USER_10,
+                /* appStillExists = */ false);
+
+        assertEquals(set(CALLING_PACKAGE_2),
+                hashSet(user0.getAllPackagesForTest().keySet()));
+        assertEquals(set(CALLING_PACKAGE_1),
+                hashSet(user10.getAllPackagesForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_0, LAUNCHER_1),
+                        PackageWithUser.of(USER_0, LAUNCHER_2)),
+                hashSet(user0.getAllLaunchersForTest().keySet()));
+        assertEquals(
+                set(),
+                hashSet(user10.getAllLaunchersForTest().keySet()));
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
+                "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
+                "s0_2");
+
+        // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
+        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
+        assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
+        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
+
+        mService.saveDirtyInfo();
+
+        // More remove.
+        uninstallPackage(USER_10, CALLING_PACKAGE_1);
+        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_10, USER_10,
+                /* appStillExists = */ false);
+
+        assertEquals(set(CALLING_PACKAGE_2),
+                hashSet(user0.getAllPackagesForTest().keySet()));
+        assertEquals(set(),
+                hashSet(user10.getAllPackagesForTest().keySet()));
+        assertEquals(
+                set(PackageWithUser.of(USER_0, LAUNCHER_1),
+                        PackageWithUser.of(USER_0, LAUNCHER_2)),
+                hashSet(user0.getAllLaunchersForTest().keySet()));
+        assertEquals(set(),
+                hashSet(user10.getAllLaunchersForTest().keySet()));
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_1, USER_0),
+                "s0_2");
+        assertShortcutIds(getLauncherPinnedShortcuts(LAUNCHER_2, USER_0),
+                "s0_2");
+
+        // Note the pinned shortcuts on user-10 no longer referred, so they should both be removed.
+        assertShortcutNotExists(CALLING_PACKAGE_1, "s0_1", USER_0);
+        assertShortcutExists(CALLING_PACKAGE_2, "s0_2", USER_0);
+        assertShortcutNotExists(CALLING_PACKAGE_1, "s10_1", USER_10);
+        assertShortcutNotExists(CALLING_PACKAGE_2, "s10_2", USER_10);
+
+        mService.saveDirtyInfo();
+    }
+
+    public void testCleanupPackage_republishManifests() {
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s2", "s3", "ms1", "ms2"), HANDLE_USER_0);
+        });
+
+        // Remove ms2 from manifest.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"))));
+
+            // Make sure the shortcuts are in the intended state.
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "s1", "s2", "s3")
+
+                    .selectByIds("ms1")
+                    .areAllManifest()
+                    .areAllPinned()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms2")
+                    .areAllNotManifest()
+                    .areAllPinned()
+
+                    .revertToOriginalList()
+                    .selectByIds("s1")
+                    .areAllDynamic()
+                    .areAllNotPinned()
+
+                    .revertToOriginalList()
+                    .selectByIds("s2")
+                    .areAllDynamic()
+                    .areAllPinned()
+
+                    .revertToOriginalList()
+                    .selectByIds("s3")
+                    .areAllNotDynamic()
+                    .areAllPinned();
+        });
+
+        // Clean up + re-publish manifests.
+        mService.cleanUpPackageLocked(CALLING_PACKAGE_1, USER_0, USER_0,
+                /* appStillExists = */ true);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1")
+                    .areAllManifest();
+        });
+    }
+
+    public void testHandleGonePackage_crossProfile() {
+        // Create some shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        // Pin some.
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s1"), HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s2"), UserHandle.of(USER_P0));
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s3"), HANDLE_USER_0);
+        });
+
+        runWithCaller(LAUNCHER_1, USER_P0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s2"), HANDLE_USER_0);
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s3"), UserHandle.of(USER_P0));
+
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2,
+                    list("s1"), HANDLE_USER_0);
+        });
+
+        runWithCaller(LAUNCHER_1, USER_10, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s3"), HANDLE_USER_10);
+        });
+
+        // Check the state.
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        // Make sure all the information is persisted.
+        mService.saveDirtyInfo();
+        initService();
+        mService.handleUnlockUser(USER_0);
+        mService.handleUnlockUser(USER_P0);
+        mService.handleUnlockUser(USER_10);
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        // Start uninstalling.
+        uninstallPackage(USER_10, LAUNCHER_1);
+        mService.checkPackageChanges(USER_10);
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        // Uninstall.
+        uninstallPackage(USER_10, CALLING_PACKAGE_1);
+        mService.checkPackageChanges(USER_10);
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        uninstallPackage(USER_P0, LAUNCHER_1);
+        mService.checkPackageChanges(USER_0);
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        mService.checkPackageChanges(USER_P0);
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        uninstallPackage(USER_P0, CALLING_PACKAGE_1);
+
+        mService.saveDirtyInfo();
+        initService();
+        mService.handleUnlockUser(USER_0);
+        mService.handleUnlockUser(USER_P0);
+        mService.handleUnlockUser(USER_10);
+
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicAndPinned(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        // Uninstall
+        uninstallPackage(USER_0, LAUNCHER_1);
+
+        mService.saveDirtyInfo();
+        initService();
+        mService.handleUnlockUser(USER_0);
+        mService.handleUnlockUser(USER_P0);
+        mService.handleUnlockUser(USER_10);
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+
+        uninstallPackage(USER_0, CALLING_PACKAGE_2);
+
+        mService.saveDirtyInfo();
+        initService();
+        mService.handleUnlockUser(USER_0);
+        mService.handleUnlockUser(USER_P0);
+        mService.handleUnlockUser(USER_10);
+
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_0));
+        assertDynamicOnly(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_P0));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_P0));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_P0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s2", USER_0));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_2, "s3", USER_0));
+
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s2", USER_10));
+        assertNull(getPackageShortcut(CALLING_PACKAGE_1, "s3", USER_10));
+    }
+
+    protected void checkCanRestoreTo(boolean expected, ShortcutPackageInfo spi,
+            int version, String... signatures) {
+        assertEquals(expected, spi.canRestoreTo(mService, genPackage(
+                "dummy", /* uid */ 0, version, signatures)));
+    }
+
+    public void testCanRestoreTo() {
+        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sig1");
+        addPackage(CALLING_PACKAGE_2, CALLING_UID_1, 10, "sig1", "sig2");
+
+        final ShortcutPackageInfo spi1 = ShortcutPackageInfo.generateForInstalledPackageForTest(
+                mService, CALLING_PACKAGE_1, USER_0);
+        final ShortcutPackageInfo spi2 = ShortcutPackageInfo.generateForInstalledPackageForTest(
+                mService, CALLING_PACKAGE_2, USER_0);
+
+        checkCanRestoreTo(true, spi1, 10, "sig1");
+        checkCanRestoreTo(true, spi1, 10, "x", "sig1");
+        checkCanRestoreTo(true, spi1, 10, "sig1", "y");
+        checkCanRestoreTo(true, spi1, 10, "x", "sig1", "y");
+        checkCanRestoreTo(true, spi1, 11, "sig1");
+
+        checkCanRestoreTo(false, spi1, 10 /* empty */);
+        checkCanRestoreTo(false, spi1, 10, "x");
+        checkCanRestoreTo(false, spi1, 10, "x", "y");
+        checkCanRestoreTo(false, spi1, 10, "x");
+        checkCanRestoreTo(false, spi1, 9, "sig1");
+
+        checkCanRestoreTo(true, spi2, 10, "sig1", "sig2");
+        checkCanRestoreTo(true, spi2, 10, "sig2", "sig1");
+        checkCanRestoreTo(true, spi2, 10, "x", "sig1", "sig2");
+        checkCanRestoreTo(true, spi2, 10, "x", "sig2", "sig1");
+        checkCanRestoreTo(true, spi2, 10, "sig1", "sig2", "y");
+        checkCanRestoreTo(true, spi2, 10, "sig2", "sig1", "y");
+        checkCanRestoreTo(true, spi2, 10, "x", "sig1", "sig2", "y");
+        checkCanRestoreTo(true, spi2, 10, "x", "sig2", "sig1", "y");
+        checkCanRestoreTo(true, spi2, 11, "x", "sig2", "sig1", "y");
+
+        checkCanRestoreTo(false, spi2, 10, "sig1", "sig2x");
+        checkCanRestoreTo(false, spi2, 10, "sig2", "sig1x");
+        checkCanRestoreTo(false, spi2, 10, "x", "sig1x", "sig2");
+        checkCanRestoreTo(false, spi2, 10, "x", "sig2x", "sig1");
+        checkCanRestoreTo(false, spi2, 10, "sig1", "sig2x", "y");
+        checkCanRestoreTo(false, spi2, 10, "sig2", "sig1x", "y");
+        checkCanRestoreTo(false, spi2, 10, "x", "sig1x", "sig2", "y");
+        checkCanRestoreTo(false, spi2, 10, "x", "sig2x", "sig1", "y");
+        checkCanRestoreTo(false, spi2, 11, "x", "sig2x", "sig1", "y");
+    }
+
+    public void testHandlePackageDelete() {
+        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_32x32));
+        setCaller(CALLING_PACKAGE_1, USER_0);
+        assertTrue(mManager.addDynamicShortcuts(list(
+                makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
+        )));
+        // Also add a manifest shortcut, which should be removed too.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1", "s2", "ms1")
+
+                    .selectManifest()
+                    .haveIds("ms1");
+        });
+
+        setCaller(CALLING_PACKAGE_2, USER_0);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        setCaller(CALLING_PACKAGE_3, USER_0);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        mRunningUsers.put(USER_10, true);
+
+        setCaller(CALLING_PACKAGE_1, USER_10);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        setCaller(CALLING_PACKAGE_2, USER_10);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        setCaller(CALLING_PACKAGE_3, USER_10);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
+
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
+
+        uninstallPackage(USER_0, CALLING_PACKAGE_1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageDeleteIntent(CALLING_PACKAGE_1, USER_0));
+
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
+
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
+
+        mRunningUsers.put(USER_10, true);
+
+        uninstallPackage(USER_10, CALLING_PACKAGE_2);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageDeleteIntent(CALLING_PACKAGE_2, USER_10));
+
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
+
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
+
+        mInjectedPackages.remove(CALLING_PACKAGE_1);
+        mInjectedPackages.remove(CALLING_PACKAGE_3);
+
+        mService.checkPackageChanges(USER_0);
+
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));  // ---------------
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
+
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
+
+        mService.checkPackageChanges(USER_10);
+
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
+
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
+    }
+
+    /** Almost ame as testHandlePackageDelete, except it doesn't uninstall packages. */
+    public void testHandlePackageClearData() {
+        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_32x32));
+        setCaller(CALLING_PACKAGE_1, USER_0);
+        assertTrue(mManager.addDynamicShortcuts(list(
+                makeShortcutWithIcon("s1", bmp32x32), makeShortcutWithIcon("s2", bmp32x32)
+        )));
+
+        setCaller(CALLING_PACKAGE_2, USER_0);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        setCaller(CALLING_PACKAGE_3, USER_0);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        mRunningUsers.put(USER_10, true);
+
+        setCaller(CALLING_PACKAGE_1, USER_10);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        setCaller(CALLING_PACKAGE_2, USER_10);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        setCaller(CALLING_PACKAGE_3, USER_10);
+        assertTrue(mManager.addDynamicShortcuts(list(makeShortcutWithIcon("s1", bmp32x32))));
+
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
+
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
+
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageDataClear(CALLING_PACKAGE_1, USER_0));
+
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
+
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
+
+        mRunningUsers.put(USER_10, true);
+
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageDataClear(CALLING_PACKAGE_2, USER_10));
+
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_0));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "s1", USER_10));
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_2, "s1", USER_10));
+        assertNotNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_3, "s1", USER_10));
+
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_0));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_1, USER_10));
+        assertFalse(bitmapDirectoryExists(CALLING_PACKAGE_2, USER_10));
+        assertTrue(bitmapDirectoryExists(CALLING_PACKAGE_3, USER_10));
+    }
+
+    public void testHandlePackageClearData_manifestRepublished() {
+
+        mRunningUsers.put(USER_10, true);
+
+        // Add two manifests and two dynamics.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.addDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"))));
+        });
+        runWithCaller(LAUNCHER_1, USER_10, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10);
+        });
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "s1", "s2")
+                    .areAllEnabled()
+
+                    .selectPinned()
+                    .haveIds("ms2", "s2");
+        });
+
+        // Clear data
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageDataClear(CALLING_PACKAGE_1, USER_10));
+
+        // Only manifest shortcuts will remain, and are no longer pinned.
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2")
+                    .areAllEnabled()
+                    .areAllNotPinned();
+        });
+    }
+
+    public void testHandlePackageUpdate() throws Throwable {
+        // Set up shortcuts and launchers.
+
+        final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
+        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_32x32));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"),
+                    makeShortcutWithIcon("s2", res32x32),
+                    makeShortcutWithIcon("s3", res32x32),
+                    makeShortcutWithIcon("s4", bmp32x32))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"),
+                    makeShortcutWithIcon("s2", bmp32x32))));
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcutWithIcon("s1", res32x32))));
+        });
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcutWithIcon("s1", res32x32),
+                    makeShortcutWithIcon("s2", res32x32))));
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcutWithIcon("s1", bmp32x32),
+                    makeShortcutWithIcon("s2", bmp32x32))));
+        });
+
+        LauncherApps.Callback c0 = mock(LauncherApps.Callback.class);
+        LauncherApps.Callback c10 = mock(LauncherApps.Callback.class);
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.registerCallback(c0, new Handler(Looper.getMainLooper()));
+        });
+        runWithCaller(LAUNCHER_1, USER_10, () -> {
+            mLauncherApps.registerCallback(c10, new Handler(Looper.getMainLooper()));
+        });
+
+        mInjectedCurrentTimeMillis = START_TIME + 100;
+
+        ArgumentCaptor<List> shortcuts;
+
+        // Update the version info for package 1.
+        reset(c0);
+        reset(c10);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+
+        // Then send the broadcast, to only user-0.
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
+
+        waitOnMainThread();
+
+        // User-0 should get the notification.
+        shortcuts = ArgumentCaptor.forClass(List.class);
+        verify(c0).onShortcutsChanged(
+                eq(CALLING_PACKAGE_1),
+                shortcuts.capture(),
+                eq(HANDLE_USER_0));
+
+        // User-10 shouldn't yet get the notification.
+        verify(c10, times(0)).onShortcutsChanged(
+                eq(CALLING_PACKAGE_1),
+                any(List.class),
+                any(UserHandle.class));
+        assertShortcutIds(shortcuts.getValue(), "s1", "s2", "s3", "s4");
+        assertEquals(START_TIME,
+                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
+        assertEquals(START_TIME + 100,
+                findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
+        assertEquals(START_TIME + 100,
+                findShortcut(shortcuts.getValue(), "s3").getLastChangedTimestamp());
+        assertEquals(START_TIME,
+                findShortcut(shortcuts.getValue(), "s4").getLastChangedTimestamp());
+
+        // Next, send unlock even on user-10.  Now we scan packages on this user and send a
+        // notification to the launcher.
+        mInjectedCurrentTimeMillis = START_TIME + 200;
+
+        mRunningUsers.put(USER_10, true);
+        mUnlockedUsers.put(USER_10, true);
+
+        reset(c0);
+        reset(c10);
+        setPackageLastUpdateTime(CALLING_PACKAGE_1, mInjectedCurrentTimeMillis);
+        mService.handleUnlockUser(USER_10);
+        mService.checkPackageChanges(USER_10);
+
+        waitOnMainThread();
+
+        shortcuts = ArgumentCaptor.forClass(List.class);
+        verify(c0, times(0)).onShortcutsChanged(
+                eq(CALLING_PACKAGE_1),
+                any(List.class),
+                any(UserHandle.class));
+
+        verify(c10).onShortcutsChanged(
+                eq(CALLING_PACKAGE_1),
+                shortcuts.capture(),
+                eq(HANDLE_USER_10));
+
+        assertShortcutIds(shortcuts.getValue(), "s1", "s2");
+        assertEquals(START_TIME + 200,
+                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
+        assertEquals(START_TIME + 200,
+                findShortcut(shortcuts.getValue(), "s2").getLastChangedTimestamp());
+
+
+        // Do the same thing for package 2, which doesn't have resource icons.
+        mInjectedCurrentTimeMillis = START_TIME + 300;
+
+        reset(c0);
+        reset(c10);
+        updatePackageVersion(CALLING_PACKAGE_2, 10);
+
+        // Then send the broadcast, to only user-0.
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageUpdateIntent(CALLING_PACKAGE_2, USER_0));
+        mService.checkPackageChanges(USER_10);
+
+        waitOnMainThread();
+
+        verify(c0, times(0)).onShortcutsChanged(
+                eq(CALLING_PACKAGE_1),
+                any(List.class),
+                any(UserHandle.class));
+
+        verify(c10, times(0)).onShortcutsChanged(
+                eq(CALLING_PACKAGE_1),
+                any(List.class),
+                any(UserHandle.class));
+
+        // Do the same thing for package 3
+        mInjectedCurrentTimeMillis = START_TIME + 400;
+
+        reset(c0);
+        reset(c10);
+        updatePackageVersion(CALLING_PACKAGE_3, 100);
+
+        // Then send the broadcast, to only user-0.
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageUpdateIntent(CALLING_PACKAGE_3, USER_0));
+        mService.checkPackageChanges(USER_10);
+
+        waitOnMainThread();
+
+        shortcuts = ArgumentCaptor.forClass(List.class);
+        verify(c0).onShortcutsChanged(
+                eq(CALLING_PACKAGE_3),
+                shortcuts.capture(),
+                eq(HANDLE_USER_0));
+
+        // User 10 doesn't have package 3, so no callback.
+        verify(c10, times(0)).onShortcutsChanged(
+                eq(CALLING_PACKAGE_3),
+                any(List.class),
+                any(UserHandle.class));
+
+        assertShortcutIds(shortcuts.getValue(), "s1");
+        assertEquals(START_TIME + 400,
+                findShortcut(shortcuts.getValue(), "s1").getLastChangedTimestamp());
+    }
+
+    /**
+     * Test the case where an updated app has resource IDs changed.
+     */
+    public void testHandlePackageUpdate_resIdChanged() throws Exception {
+        final Icon icon1 = Icon.createWithResource(getTestContext(), /* res ID */ 1000);
+        final Icon icon2 = Icon.createWithResource(getTestContext(), /* res ID */ 1001);
+
+        // Set up shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            // Note resource strings are not officially supported (they're hidden), but
+            // should work.
+
+            final ShortcutInfo s1 = new ShortcutInfo.Builder(mClientContext)
+                    .setId("s1")
+                    .setActivity(makeComponent(ShortcutActivity.class))
+                    .setIntent(new Intent(Intent.ACTION_VIEW))
+                    .setIcon(icon1)
+                    .setTitleResId(10000)
+                    .setTextResId(10001)
+                    .setDisabledMessageResId(10002)
+                    .build();
+
+            final ShortcutInfo s2 = new ShortcutInfo.Builder(mClientContext)
+                    .setId("s2")
+                    .setActivity(makeComponent(ShortcutActivity.class))
+                    .setIntent(new Intent(Intent.ACTION_VIEW))
+                    .setIcon(icon2)
+                    .setTitleResId(20000)
+                    .build();
+
+            assertTrue(mManager.setDynamicShortcuts(list(s1, s2)));
+        });
+
+        // Verify.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ShortcutInfo s1 = getCallerShortcut("s1");
+            final ShortcutInfo s2 = getCallerShortcut("s2");
+
+            assertEquals(1000, s1.getIconResourceId());
+            assertEquals(10000, s1.getTitleResId());
+            assertEquals(10001, s1.getTextResId());
+            assertEquals(10002, s1.getDisabledMessageResourceId());
+
+            assertEquals(1001, s2.getIconResourceId());
+            assertEquals(20000, s2.getTitleResId());
+            assertEquals(0, s2.getTextResId());
+            assertEquals(0, s2.getDisabledMessageResourceId());
+        });
+
+        mService.saveDirtyInfo();
+        initService();
+
+        // Set up the mock resources again, with an "adjustment".
+        // When the package is updated, the service will fetch the updated res-IDs with res-names,
+        // and the new IDs will have this offset.
+        setUpAppResources(10);
+
+        // Update the package.
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ShortcutInfo s1 = getCallerShortcut("s1");
+            final ShortcutInfo s2 = getCallerShortcut("s2");
+
+            assertEquals(1010, s1.getIconResourceId());
+            assertEquals(10010, s1.getTitleResId());
+            assertEquals(10011, s1.getTextResId());
+            assertEquals(10012, s1.getDisabledMessageResourceId());
+
+            assertEquals(1011, s2.getIconResourceId());
+            assertEquals(20010, s2.getTitleResId());
+            assertEquals(0, s2.getTextResId());
+            assertEquals(0, s2.getDisabledMessageResourceId());
+        });
+    }
+
+    public void testHandlePackageUpdate_systemAppUpdate() {
+
+        // Package1 is a system app.  Package 2 is not a system app, so it's not scanned
+        // in this test at all.
+        mSystemPackages.add(CALLING_PACKAGE_1);
+
+        // Initial state: no shortcuts.
+        mService.checkPackageChanges(USER_0);
+
+        assertEquals(mInjectedCurrentTimeMillis,
+                mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
+        assertEquals(mInjectedBuildFingerprint,
+                mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
+
+        // They have no shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .isEmpty();
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .isEmpty();
+        });
+
+        // Next.
+        // Update the packages -- now they have 1 manifest shortcut.
+        // But checkPackageChanges() don't notice it, since their version code / timestamp haven't
+        // changed.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        mInjectedCurrentTimeMillis += 1000;
+        mService.checkPackageChanges(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .isEmpty();
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .isEmpty();
+        });
+
+        // Next.
+        // Update the build finger print.  All system apps will be scanned now.
+        mInjectedBuildFingerprint = "update1";
+        mInjectedCurrentTimeMillis += 1000;
+        mService.checkPackageChanges(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1");
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .isEmpty();
+        });
+
+        // Next.
+        // Update manifest shortcuts.
+        mInjectedBuildFingerprint = "update2";
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        mInjectedCurrentTimeMillis += 1000;
+        mService.checkPackageChanges(USER_0);
+
+        // Fingerprint hasn't changed, so CALLING_PACKAGE_1 wasn't scanned.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1");
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .isEmpty();
+        });
+
+        // Update the fingerprint, but CALLING_PACKAGE_1's version code hasn't changed, so
+        // still not scanned.
+        mInjectedBuildFingerprint = "update2";
+        mInjectedCurrentTimeMillis += 1000;
+        mService.checkPackageChanges(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1");
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .isEmpty();
+        });
+
+        // Now update the version code, so CALLING_PACKAGE_1 is scanned again.
+        mInjectedBuildFingerprint = "update3";
+        mInjectedCurrentTimeMillis += 1000;
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.checkPackageChanges(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2");
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .isEmpty();
+        });
+
+        // Make sure getLastAppScanTime / getLastAppScanOsFingerprint are persisted.
+        initService();
+        assertEquals(mInjectedCurrentTimeMillis,
+                mService.getUserShortcutsLocked(USER_0).getLastAppScanTime());
+        assertEquals(mInjectedBuildFingerprint,
+                mService.getUserShortcutsLocked(USER_0).getLastAppScanOsFingerprint());
+    }
+
+    public void testHandlePackageChanged() {
+        final ComponentName ACTIVITY1 = new ComponentName(CALLING_PACKAGE_1, "act1");
+        final ComponentName ACTIVITY2 = new ComponentName(CALLING_PACKAGE_1, "act2");
+
+        addManifestShortcutResource(ACTIVITY1, R.xml.shortcut_1);
+        addManifestShortcutResource(ACTIVITY2, R.xml.shortcut_1_alt);
+
+        mRunningUsers.put(USER_10, true);
+
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.addDynamicShortcuts(list(
+                    makeShortcutWithActivity("s1", ACTIVITY1),
+                    makeShortcutWithActivity("s2", ACTIVITY2)
+            )));
+        });
+        runWithCaller(LAUNCHER_1, USER_10, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1-alt", "s2"), HANDLE_USER_10);
+        });
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms1-alt", "s1", "s2")
+                    .areAllEnabled()
+
+                    .selectPinned()
+                    .haveIds("ms1-alt", "s2")
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1", "s1")
+                    .areAllWithActivity(ACTIVITY1)
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1-alt", "s2")
+                    .areAllWithActivity(ACTIVITY2)
+                    ;
+        });
+
+        // First, no changes.
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms1-alt", "s1", "s2")
+                    .areAllEnabled()
+
+                    .selectPinned()
+                    .haveIds("ms1-alt", "s2")
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1", "s1")
+                    .areAllWithActivity(ACTIVITY1)
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1-alt", "s2")
+                    .areAllWithActivity(ACTIVITY2)
+            ;
+        });
+
+        // Disable activity 1
+        mEnabledActivityChecker = (activity, userId) -> !ACTIVITY1.equals(activity);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1-alt", "s2")
+                    .areAllEnabled()
+
+                    .selectPinned()
+                    .haveIds("ms1-alt", "s2")
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1-alt", "s2")
+                    .areAllWithActivity(ACTIVITY2)
+            ;
+        });
+
+        // Re-enable activity 1.
+        // Manifest shortcuts will be re-published, but dynamic ones are not.
+        mEnabledActivityChecker = (activity, userId) -> true;
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms1-alt", "s2")
+                    .areAllEnabled()
+
+                    .selectPinned()
+                    .haveIds("ms1-alt", "s2")
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1")
+                    .areAllWithActivity(ACTIVITY1)
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1-alt", "s2")
+                    .areAllWithActivity(ACTIVITY2)
+                    ;
+        });
+
+        // Disable activity 2
+        // Because "ms1-alt" and "s2" are both pinned, they will remain, but disabled.
+        mEnabledActivityChecker = (activity, userId) -> !ACTIVITY2.equals(activity);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageChangedIntent(CALLING_PACKAGE_1, USER_10));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms1-alt", "s2")
+
+                    .selectDynamic().isEmpty().revertToOriginalList() // no dynamics.
+
+                    .selectPinned()
+                    .haveIds("ms1-alt", "s2")
+                    .areAllDisabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1")
+                    .areAllWithActivity(ACTIVITY1)
+                    .areAllEnabled()
+            ;
+        });
+    }
+
+    public void testHandlePackageUpdate_activityNoLongerMain() throws Throwable {
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcutWithActivity("s1a",
+                            new ComponentName(getCallingPackage(), "act1")),
+                    makeShortcutWithActivity("s1b",
+                            new ComponentName(getCallingPackage(), "act1")),
+                    makeShortcutWithActivity("s2a",
+                            new ComponentName(getCallingPackage(), "act2")),
+                    makeShortcutWithActivity("s2b",
+                            new ComponentName(getCallingPackage(), "act2")),
+                    makeShortcutWithActivity("s3a",
+                            new ComponentName(getCallingPackage(), "act3")),
+                    makeShortcutWithActivity("s3b",
+                            new ComponentName(getCallingPackage(), "act3"))
+            )));
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
+                    .areAllDynamic();
+        });
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("s1b", "s2b", "s3b"),
+                    HANDLE_USER_0);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1a", "s1b", "s2a", "s2b", "s3a", "s3b")
+                    .areAllDynamic()
+
+                    .selectByIds("s1b", "s2b", "s3b")
+                    .areAllPinned();
+        });
+
+        // Update the app and act2 and act3 are no longer main.
+        mMainActivityChecker = (activity, userId) -> {
+            return activity.getClassName().equals("act1");
+        };
+
+        setCaller(LAUNCHER_1, USER_0);
+        assertForLauncherCallback(mLauncherApps, () -> {
+            updatePackageVersion(CALLING_PACKAGE_1, 1);
+                    mService.mPackageMonitor.onReceive(getTestContext(),
+                    genPackageUpdateIntent(CALLING_PACKAGE_1, USER_0));
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                // Make sure the launcher gets callbacks.
+                .haveIds("s1a", "s1b", "s2b", "s3b")
+                .areAllWithKeyFieldsOnly();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            // s2a and s3a are gone, but s2b and s3b will remain because they're pinned, and
+            // disabled.
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1a", "s1b", "s2b", "s3b")
+
+                    .selectByIds("s1a", "s1b")
+                    .areAllDynamic()
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("s2b", "s3b")
+                    .areAllNotDynamic()
+                    .areAllDisabled()
+                    .areAllPinned()
+                    ;
+        });
+    }
+
+    protected void prepareForBackupTest() {
+
+        prepareCrossProfileDataSet();
+
+        backupAndRestore();
+    }
+
+    /**
+     * Make sure the backup data doesn't have the following information:
+     * - Launchers on other users.
+     * - Non-backup app information.
+     *
+     * But restores all other infomation.
+     *
+     * It also omits the following pieces of information, but that's tested in
+     * {@link ShortcutManagerTest2#testShortcutInfoSaveAndLoad_forBackup}.
+     * - Unpinned dynamic shortcuts
+     * - Bitmaps
+     */
+    public void testBackupAndRestore() {
+        prepareForBackupTest();
+
+        checkBackupAndRestore_success();
+    }
+
+    public void testBackupAndRestore_backupRestoreTwice() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        dumpsysOnLogcat("Before second backup");
+
+        backupAndRestore();
+
+        dumpsysOnLogcat("After second backup");
+
+        checkBackupAndRestore_success();
+    }
+
+    public void testBackupAndRestore_backupRestoreMultiple() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        // This also shouldn't affect the result.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"),
+                    makeShortcut("s4"), makeShortcut("s5"), makeShortcut("s6"))));
+        });
+
+        backupAndRestore();
+
+        checkBackupAndRestore_success();
+    }
+
+    public void testBackupAndRestore_restoreToNewVersion() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 2);
+        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 5);
+
+        checkBackupAndRestore_success();
+    }
+
+    public void testBackupAndRestore_restoreToSuperSetSignatures() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        // Change package signatures.
+        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 1, "sigx", CALLING_PACKAGE_1);
+        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 4, LAUNCHER_1, "sigy");
+
+        checkBackupAndRestore_success();
+    }
+
+    protected void checkBackupAndRestore_success() {
+        // Make sure non-system user is not restored.
+        final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
+        assertEquals(0, userP0.getAllPackagesForTest().size());
+        assertEquals(0, userP0.getAllLaunchersForTest().size());
+
+        // Make sure only "allowBackup" apps are restored, and are shadow.
+        final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
+        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
+        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
+        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
+                PackageWithUser.of(USER_0, LAUNCHER_1)));
+        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
+                PackageWithUser.of(USER_0, LAUNCHER_2)));
+
+        assertNull(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
+        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
+        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
+
+        installPackage(USER_0, CALLING_PACKAGE_1);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .selectDynamic()
+                    .isEmpty()
+
+                    .revertToOriginalList()
+                    .selectPinned()
+                    .haveIds("s1", "s2");
+        });
+
+        installPackage(USER_0, LAUNCHER_1);
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    .areAllPinned()
+                    .haveIds("s1");
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    .isEmpty();
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    .isEmpty();
+
+            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
+                    .isEmpty();
+        });
+
+        installPackage(USER_0, CALLING_PACKAGE_2);
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .selectDynamic()
+                    .isEmpty()
+
+                    .revertToOriginalList()
+                    .selectPinned()
+                    .haveIds("s1", "s2", "s3");
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    .areAllPinned()
+                    .haveIds("s1");
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    .areAllPinned()
+                    .haveIds("s1", "s2");
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    .isEmpty();
+
+            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
+                    .isEmpty();
+        });
+
+        // 3 shouldn't be backed up, so no pinned shortcuts.
+        installPackage(USER_0, CALLING_PACKAGE_3);
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .isEmpty();
+        });
+
+        // Launcher on a different profile shouldn't be restored.
+        runWithCaller(LAUNCHER_1, USER_P0, () -> {
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    .isEmpty();
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    .isEmpty();
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    .isEmpty();
+        });
+
+        // Package on a different profile, no restore.
+        installPackage(USER_P0, CALLING_PACKAGE_1);
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .isEmpty();
+        });
+
+        // Restore launcher 2 on user 0.
+        installPackage(USER_0, LAUNCHER_2);
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    .areAllPinned()
+                    .haveIds("s2");
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    .areAllPinned()
+                    .haveIds("s2", "s3");
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    .isEmpty();
+
+            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
+                    .isEmpty();
+        });
+
+
+        // Restoration of launcher2 shouldn't affect other packages; so do the same checks and
+        // make sure they still have the same result.
+        installPackage(USER_0, CALLING_PACKAGE_1);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .areAllPinned()
+                    .haveIds("s1", "s2");
+        });
+
+        installPackage(USER_0, LAUNCHER_1);
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    .areAllPinned()
+                    .haveIds("s1");
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    .areAllPinned()
+                    .haveIds("s1", "s2");
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    .isEmpty();
+
+            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
+                    .isEmpty();
+        });
+
+        installPackage(USER_0, CALLING_PACKAGE_2);
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .areAllPinned()
+                    .haveIds("s1", "s2", "s3");
+        });
+    }
+
+    public void testBackupAndRestore_publisherLowerVersion() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 0); // Lower version
+
+        checkBackupAndRestore_publisherNotRestored();
+    }
+
+    public void testBackupAndRestore_publisherWrongSignature() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        addPackage(CALLING_PACKAGE_1, CALLING_UID_1, 10, "sigx"); // different signature
+
+        checkBackupAndRestore_publisherNotRestored();
+    }
+
+    public void testBackupAndRestore_publisherNoLongerBackupTarget() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        updatePackageInfo(CALLING_PACKAGE_1,
+                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
+
+        checkBackupAndRestore_publisherNotRestored();
+    }
+
+    protected void checkBackupAndRestore_publisherNotRestored() {
+        installPackage(USER_0, CALLING_PACKAGE_1);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertEquals(0, mManager.getPinnedShortcuts().size());
+        });
+
+        installPackage(USER_0, CALLING_PACKAGE_2);
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s1", "s2", "s3");
+        });
+
+        installPackage(USER_0, LAUNCHER_1);
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
+                    "s1", "s2");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+        installPackage(USER_0, LAUNCHER_2);
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+
+        installPackage(USER_0, CALLING_PACKAGE_3);
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertEquals(0, mManager.getPinnedShortcuts().size());
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
+                    "s1", "s2");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+    }
+
+    public void testBackupAndRestore_launcherLowerVersion() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 0); // Lower version
+
+        checkBackupAndRestore_launcherNotRestored();
+    }
+
+    public void testBackupAndRestore_launcherWrongSignature() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        addPackage(LAUNCHER_1, LAUNCHER_UID_1, 10, "sigx"); // different signature
+
+        checkBackupAndRestore_launcherNotRestored();
+    }
+
+    public void testBackupAndRestore_launcherNoLongerBackupTarget() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        updatePackageInfo(LAUNCHER_1,
+                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
+
+        checkBackupAndRestore_launcherNotRestored();
+    }
+
+    protected void checkBackupAndRestore_launcherNotRestored() {
+        installPackage(USER_0, CALLING_PACKAGE_1);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+
+            // s1 was pinned by launcher 1, which is not restored, yet, so we still see "s1" here.
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s1", "s2");
+        });
+
+        installPackage(USER_0, CALLING_PACKAGE_2);
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s1", "s2", "s3");
+        });
+
+        // Now we try to restore launcher 1.  Then we realize it's not restorable, so L1 has no pinned
+        // shortcuts.
+        installPackage(USER_0, LAUNCHER_1);
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+
+            // Now CALLING_PACKAGE_1 realizes "s1" is no longer pinned.
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s2");
+        });
+
+        installPackage(USER_0, LAUNCHER_2);
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
+                    "s2");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+
+        installPackage(USER_0, CALLING_PACKAGE_3);
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertEquals(0, mManager.getPinnedShortcuts().size());
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0)),
+                    "s2");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+    }
+
+    public void testBackupAndRestore_launcherAndPackageNoLongerBackupTarget() {
+        prepareForBackupTest();
+
+        // Note doing a backup & restore again here shouldn't affect the result.
+        backupAndRestore();
+
+        updatePackageInfo(CALLING_PACKAGE_1,
+                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
+
+        updatePackageInfo(LAUNCHER_1,
+                pi -> pi.applicationInfo.flags &= ~ApplicationInfo.FLAG_ALLOW_BACKUP);
+
+        checkBackupAndRestore_publisherAndLauncherNotRestored();
+    }
+
+    protected void checkBackupAndRestore_publisherAndLauncherNotRestored() {
+        installPackage(USER_0, CALLING_PACKAGE_1);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertEquals(0, mManager.getPinnedShortcuts().size());
+        });
+
+        installPackage(USER_0, CALLING_PACKAGE_2);
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s1", "s2", "s3");
+        });
+
+        installPackage(USER_0, LAUNCHER_1);
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+        installPackage(USER_0, LAUNCHER_2);
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+
+        // Because launcher 1 wasn't restored, "s1" is no longer pinned.
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertShortcutIds(assertAllPinned(
+                    mManager.getPinnedShortcuts()),
+                    "s2", "s3");
+        });
+
+        installPackage(USER_0, CALLING_PACKAGE_3);
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertEquals(0, mManager.getDynamicShortcuts().size());
+            assertEquals(0, mManager.getPinnedShortcuts().size());
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0)),
+                    "s2", "s3");
+            assertShortcutIds(assertAllPinned(
+                    mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    /* empty */);
+        });
+    }
+
+    public void testBackupAndRestore_disabled() {
+        prepareCrossProfileDataSet();
+
+        // Before doing backup & restore, disable s1.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.disableShortcuts(list("s1"));
+        });
+
+        backupAndRestore();
+
+        // Below is copied from checkBackupAndRestore_success.
+
+        // Make sure non-system user is not restored.
+        final ShortcutUser userP0 = mService.getUserShortcutsLocked(USER_P0);
+        assertEquals(0, userP0.getAllPackagesForTest().size());
+        assertEquals(0, userP0.getAllLaunchersForTest().size());
+
+        // Make sure only "allowBackup" apps are restored, and are shadow.
+        final ShortcutUser user0 = mService.getUserShortcutsLocked(USER_0);
+        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_1));
+        assertExistsAndShadow(user0.getAllPackagesForTest().get(CALLING_PACKAGE_2));
+        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
+                PackageWithUser.of(USER_0, LAUNCHER_1)));
+        assertExistsAndShadow(user0.getAllLaunchersForTest().get(
+                PackageWithUser.of(USER_0, LAUNCHER_2)));
+
+        assertNull(user0.getAllPackagesForTest().get(CALLING_PACKAGE_3));
+        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_0, LAUNCHER_3)));
+        assertNull(user0.getAllLaunchersForTest().get(PackageWithUser.of(USER_P0, LAUNCHER_1)));
+
+        installPackage(USER_0, CALLING_PACKAGE_1);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .areAllEnabled() // disabled shortcuts shouldn't be restored.
+
+                    .selectDynamic()
+                    .isEmpty()
+
+                    .revertToOriginalList()
+                    .selectPinned()
+                    // s1 is not restored.
+                    .haveIds("s2");
+        });
+
+        installPackage(USER_0, LAUNCHER_1);
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            // Note, s1 was pinned by launcher 1, but was disabled, so isn't restored.
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0))
+                    .isEmpty();
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_2), HANDLE_USER_0))
+                    .isEmpty();
+
+            assertWith(mLauncherApps.getShortcuts(buildAllQuery(CALLING_PACKAGE_3), HANDLE_USER_0))
+                    .isEmpty();
+
+            assertWith(mLauncherApps.getShortcuts(QUERY_ALL, HANDLE_USER_P0))
+                    .isEmpty();
+        });
+    }
+
+
+    public void testBackupAndRestore_manifestRePublished() {
+        // Publish two manifest shortcuts.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+
+        // Pin from launcher 1.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
+        });
+
+        // Update and now ms2 is gone -> disabled.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Make sure the manifest shortcuts have been published.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .selectManifest()
+                    .haveIds("ms1")
+
+                    .revertToOriginalList()
+                    .selectDynamic()
+                    .haveIds("s1", "s2", "s3")
+
+                    .revertToOriginalList()
+                    .selectPinned()
+                    .haveIds("ms1", "ms2", "s1", "s2")
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1")
+                    .areAllManifest()
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms2")
+                    .areAllNotManifest()
+                    .areAllDisabled();
+        });
+
+        backupAndRestore();
+
+        // When re-installing the app, the manifest shortcut should be re-published.
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                genPackageAddIntent(LAUNCHER_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .selectPinned()
+                    // ms2 was disabled, so not restored.
+                    .haveIds("ms1", "s1", "s2")
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1")
+                    .areAllManifest()
+
+                    .revertToOriginalList()
+                    .selectByIds("s1", "s2")
+                    .areAllNotDynamic()
+                    ;
+        });
+    }
+
+    /**
+     * It's the case with preintalled apps -- when applyRestore() is called, the system
+     * apps are already installed, so manifest shortcuts need to be re-published.
+     *
+     * Also, when a restore target app is already installed, and
+     * - if it has allowBackup=true, we'll restore normally, so all existing shortcuts will be
+     * replaced. (but manifest shortcuts will be re-published anyway.)  We log a warning on
+     * logcat.
+     * - if it has allowBackup=false, we don't touch any of the existing shortcuts.
+     */
+    public void testBackupAndRestore_appAlreadyInstalledWhenRestored() {
+        // Pre-backup.  Same as testBackupAndRestore_manifestRePublished().
+
+        // Publish two manifest shortcuts.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"), makeShortcut("s3"))));
+        });
+
+        // Pin from launcher 1.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("ms1", "ms2", "s1", "s2"), HANDLE_USER_0);
+        });
+
+        // Update and now ms2 is gone -> disabled.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Set up shortcuts for package 3, which won't be backed up / restored.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_3, 1);
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                genPackageAddIntent(CALLING_PACKAGE_3, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertTrue(getManager().setDynamicShortcuts(list(
+                    makeShortcut("s1"))));
+        });
+
+        // Make sure the manifest shortcuts have been published.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .selectManifest()
+                    .haveIds("ms1")
+
+                    .revertToOriginalList()
+                    .selectDynamic()
+                    .haveIds("s1", "s2", "s3")
+
+                    .revertToOriginalList()
+                    .selectPinned()
+                    .haveIds("ms1", "ms2", "s1", "s2")
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1")
+                    .areAllManifest()
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms2")
+                    .areAllNotManifest()
+                    .areAllDisabled();
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1", "ms1");
+        });
+
+        // Backup and *without restarting the service, just call applyRestore()*.
+        {
+            int prevUid = mInjectedCallingUid;
+            mInjectedCallingUid = Process.SYSTEM_UID; // Only system can call it.
+
+            dumpsysOnLogcat("Before backup");
+
+            final byte[] payload = mService.getBackupPayload(USER_0);
+            if (ENABLE_DUMP) {
+                final String xml = new String(payload);
+                Log.v(TAG, "Backup payload:");
+                for (String line : xml.split("\n")) {
+                    Log.v(TAG, line);
+                }
+            }
+            mService.applyRestore(payload, USER_0);
+
+            dumpsysOnLogcat("After restore");
+
+            mInjectedCallingUid = prevUid;
+        }
+
+        // The check is also the same as testBackupAndRestore_manifestRePublished().
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerVisibleShortcuts())
+                    .selectPinned()
+                    // ms2 was disabled, so not restored.
+                    .haveIds("ms1", "s1", "s2")
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms1")
+                    .areAllManifest()
+
+                    .revertToOriginalList()
+                    .selectByIds("s1", "s2")
+                    .areAllNotDynamic()
+            ;
+        });
+
+        // Package 3 still has the same shortcuts.
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1", "ms1");
+        });
+    }
+
+    public void testSaveAndLoad_crossProfile() {
+        prepareCrossProfileDataSet();
+
+        dumpsysOnLogcat("Before save & load");
+
+        mService.saveDirtyInfo();
+        initService();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
+                    "s1", "s2", "s3", "s4");
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
+                    "s1", "s2", "s3", "s4", "s5");
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
+                    "s1", "s2", "s3", "s4", "s5", "s6");
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
+                    /* empty */);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
+                    "s1", "s2", "s3");
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
+                    "s1", "s2", "s3", "s4", "s5", "s6");
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_P0, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts())
+                    /* empty */);
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts())
+                    /* empty */);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertShortcutIds(assertAllDynamic(mManager.getDynamicShortcuts()),
+                    "x1", "x2", "x3");
+            assertShortcutIds(assertAllPinned(mManager.getPinnedShortcuts()),
+                    "x4", "x5");
+        });
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
+                    "s1");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
+                    "s1", "s2");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
+                    "s1", "s2", "s3");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
+                    "s1", "s4");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
+                    /* empty */);
+            assertExpectException(
+                    SecurityException.class, "", () -> {
+                        mLauncherApps.getShortcuts(
+                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
+                    });
+        });
+        runWithCaller(LAUNCHER_2, USER_0, () -> {
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
+                    "s2");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
+                    "s2", "s3");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
+                    "s2", "s3", "s4");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
+                    "s2", "s5");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
+                    /* empty */);
+        });
+        runWithCaller(LAUNCHER_3, USER_0, () -> {
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
+                    "s3");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
+                    "s3", "s4");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
+                    "s3", "s4", "s5");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
+                    "s3", "s6");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
+                    /* empty */);
+        });
+        runWithCaller(LAUNCHER_4, USER_0, () -> {
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_4), HANDLE_USER_0)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_P0)
+                    /* empty */);
+        });
+        runWithCaller(LAUNCHER_1, USER_P0, () -> {
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_0),
+                    "s3", "s4");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_0),
+                    "s3", "s4", "s5");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_0),
+                    "s3", "s4", "s5", "s6");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_P0),
+                    "s1", "s4");
+            assertExpectException(
+                    SecurityException.class, "unrelated profile", () -> {
+                        mLauncherApps.getShortcuts(
+                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_10);
+                    });
+        });
+        runWithCaller(LAUNCHER_1, USER_10, () -> {
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_1), HANDLE_USER_10),
+                    "x4", "x5");
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_2), HANDLE_USER_10)
+                    /* empty */);
+            assertShortcutIds(
+                    mLauncherApps.getShortcuts(buildPinnedQuery(CALLING_PACKAGE_3), HANDLE_USER_10)
+                    /* empty */);
+            assertExpectException(
+                    SecurityException.class, "unrelated profile", () -> {
+                        mLauncherApps.getShortcuts(
+                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_0);
+                    });
+            assertExpectException(
+                    SecurityException.class, "unrelated profile", () -> {
+                        mLauncherApps.getShortcuts(
+                                buildAllQuery(CALLING_PACKAGE_1), HANDLE_USER_P0);
+                    });
+        });
+        // Check the user-IDs.
+        assertEquals(USER_0,
+                mService.getUserShortcutsLocked(USER_0).getPackageShortcuts(CALLING_PACKAGE_1)
+                        .getOwnerUserId());
+        assertEquals(USER_0,
+                mService.getUserShortcutsLocked(USER_0).getPackageShortcuts(CALLING_PACKAGE_1)
+                        .getPackageUserId());
+        assertEquals(USER_P0,
+                mService.getUserShortcutsLocked(USER_P0).getPackageShortcuts(CALLING_PACKAGE_1)
+                        .getOwnerUserId());
+        assertEquals(USER_P0,
+                mService.getUserShortcutsLocked(USER_P0).getPackageShortcuts(CALLING_PACKAGE_1)
+                        .getPackageUserId());
+
+        assertEquals(USER_0,
+                mService.getUserShortcutsLocked(USER_0).getLauncherShortcuts(LAUNCHER_1, USER_0)
+                        .getOwnerUserId());
+        assertEquals(USER_0,
+                mService.getUserShortcutsLocked(USER_0).getLauncherShortcuts(LAUNCHER_1, USER_0)
+                        .getPackageUserId());
+        assertEquals(USER_P0,
+                mService.getUserShortcutsLocked(USER_P0).getLauncherShortcuts(LAUNCHER_1, USER_0)
+                        .getOwnerUserId());
+        assertEquals(USER_0,
+                mService.getUserShortcutsLocked(USER_P0).getLauncherShortcuts(LAUNCHER_1, USER_0)
+                        .getPackageUserId());
+    }
+
+    public void testOnApplicationActive_permission() {
+        assertExpectException(SecurityException.class, "Missing permission", () ->
+                mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0));
+
+        // Has permission, now it should pass.
+        mCallerPermissions.add(permission.RESET_SHORTCUT_MANAGER_THROTTLING);
+        mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0);
+    }
+
+    public void testDumpsys_crossProfile() {
+        prepareCrossProfileDataSet();
+        dumpsysOnLogcat("test1", /* force= */ true);
+    }
+
+    public void testDumpsys_withIcons() throws IOException {
+        testIcons();
+        // Dump after having some icons.
+        dumpsysOnLogcat("test1", /* force= */ true);
+    }
+
+    public void testManifestShortcut_publishOnUnlockUser() {
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
+                R.xml.shortcut_5);
+
+        // Unlock user-0.
+        mService.handleUnlockUser(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2", "ms3", "ms4", "ms5");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        // Try on another user, with some packages uninstalled.
+        mRunningUsers.put(USER_10, true);
+
+        uninstallPackage(USER_10, CALLING_PACKAGE_1);
+        uninstallPackage(USER_10, CALLING_PACKAGE_3);
+
+        mService.handleUnlockUser(USER_10);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_10, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        // Now change the resources for package 1, and unlock again.
+        // But we still see *old* shortcuts, because the package version and install time
+        // hasn't changed.
+        shutdownServices();
+
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_5);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+
+        initService();
+        mService.handleUnlockUser(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2", "ms3", "ms4", "ms5");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        // Do it again, but this time we change the app version, so we do detect the changes.
+        shutdownServices();
+
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        updatePackageLastUpdateTime(CALLING_PACKAGE_3, 1);
+
+        initService();
+        mService.handleUnlockUser(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2", "ms3", "ms4", "ms5");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        // Next, try removing all shortcuts, with some of them pinned.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms3"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2"), HANDLE_USER_0);
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_3, list("ms1"), HANDLE_USER_0);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2", "ms3", "ms4", "ms5");
+            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
+                    assertAllEnabled(mManager.getPinnedShortcuts())))),
+                    "ms3");
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
+                    assertAllEnabled(mManager.getPinnedShortcuts())))),
+                    "ms2");
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllManifest(
+                    assertAllEnabled(mManager.getPinnedShortcuts())))),
+                    "ms1");
+        });
+
+        shutdownServices();
+
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_0);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_3, ShortcutActivity.class.getName()),
+                R.xml.shortcut_0);
+
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        updatePackageVersion(CALLING_PACKAGE_2, 1);
+        updatePackageVersion(CALLING_PACKAGE_3, 1);
+
+        initService();
+        mService.handleUnlockUser(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
+                    assertAllDisabled(mManager.getPinnedShortcuts())))),
+                    "ms3");
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
+                    assertAllDisabled(mManager.getPinnedShortcuts())))),
+                    "ms2");
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllNotManifest(
+                    assertAllDisabled(mManager.getPinnedShortcuts())))),
+                    "ms1");
+        });
+
+        // Make sure we don't have ShortcutPackage for packages that don't have shortcuts.
+        assertNull(mService.getPackageShortcutForTest(CALLING_PACKAGE_4, USER_0));
+        assertNull(mService.getPackageShortcutForTest(LAUNCHER_1, USER_0));
+    }
+
+    public void testManifestShortcut_publishOnBroadcast() {
+        // First, no packages are installed.
+        uninstallPackage(USER_0, CALLING_PACKAGE_1);
+        uninstallPackage(USER_0, CALLING_PACKAGE_2);
+        uninstallPackage(USER_0, CALLING_PACKAGE_3);
+        uninstallPackage(USER_0, CALLING_PACKAGE_4);
+        uninstallPackage(USER_10, CALLING_PACKAGE_1);
+        uninstallPackage(USER_10, CALLING_PACKAGE_2);
+        uninstallPackage(USER_10, CALLING_PACKAGE_3);
+        uninstallPackage(USER_10, CALLING_PACKAGE_4);
+
+        mService.handleUnlockUser(USER_0);
+
+        mRunningUsers.put(USER_10, true);
+        mService.handleUnlockUser(USER_10);
+
+        // Originally no manifest shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        // Package 1 updated, with manifest shortcuts.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        // Package 2 updated, with manifest shortcuts.
+
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                R.xml.shortcut_5);
+        updatePackageVersion(CALLING_PACKAGE_2, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2", "ms3", "ms4", "ms5");
+            assertWith(getCallerShortcuts()).selectManifest()
+                    .selectByActivity(
+                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
+                    .haveRanksInOrder("ms1", "ms2", "ms3", "ms4", "ms5");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        // Package 2 updated, with less manifest shortcuts.
+        // This time we use updatePackageLastUpdateTime() instead of updatePackageVersion().
+
+        dumpsysOnLogcat("Before pinning");
+
+        // Also pin some.
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_2, list("ms2", "ms3"), HANDLE_USER_0);
+        });
+
+        dumpsysOnLogcat("After pinning");
+
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+            assertWith(getCallerShortcuts()).selectManifest()
+                    .selectByActivity(
+                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
+                    .haveRanksInOrder("ms1", "ms2");
+            assertShortcutIds(assertAllImmutable(assertAllPinned(
+                    mManager.getPinnedShortcuts())),
+                    "ms2", "ms3");
+            // ms3 is no longer in manifest, so should be disabled.
+            // but ms1 and ms2 should be enabled.
+            assertAllEnabled(list(getCallerShortcut("ms1")));
+            assertAllEnabled(list(getCallerShortcut("ms2")));
+            assertAllDisabled(list(getCallerShortcut("ms3")));
+        });
+
+        // Package 2 on user 10 has no shortcuts yet.
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+        // Send add broadcast, but the user is not running, so should be ignored.
+        mService.handleCleanupUser(USER_10);
+        mRunningUsers.put(USER_10, false);
+        mUnlockedUsers.put(USER_10, false);
+
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
+            // update check.
+            // So look the internal data directly using getCallerShortcuts().
+            assertEmpty(getCallerShortcuts());
+        });
+
+        // Try again, but the user is locked, so still ignored.
+        mRunningUsers.put(USER_10, true);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            // Don't use the mManager APIs to get shortcuts, because they'll trigger the package
+            // update check.
+            // So look the internal data directly using getCallerShortcuts().
+            assertEmpty(getCallerShortcuts());
+        });
+
+        // Unlock the user, now it should work.
+        mUnlockedUsers.put(USER_10, true);
+
+        // Send PACKAGE_ADD broadcast to have Package 2 on user-10 publish manifest shortcuts.
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_2, USER_10));
+
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+            assertWith(getCallerShortcuts()).selectManifest()
+                    .selectByActivity(
+                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
+                    .haveRanksInOrder("ms1", "ms2");
+            assertEmpty(mManager.getPinnedShortcuts());
+        });
+
+        // But it shouldn't affect user-0.
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+            assertWith(getCallerShortcuts()).selectManifest()
+                    .selectByActivity(
+                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
+                    .haveRanksInOrder("ms1", "ms2");
+            assertShortcutIds(assertAllImmutable(assertAllPinned(
+                    mManager.getPinnedShortcuts())),
+                    "ms2", "ms3");
+            assertAllEnabled(list(getCallerShortcut("ms1")));
+            assertAllEnabled(list(getCallerShortcut("ms2")));
+            assertAllDisabled(list(getCallerShortcut("ms3")));
+        });
+
+        // Multiple activities.
+        // Add shortcuts on activity 2 for package 2.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                R.xml.shortcut_5_alt);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
+                R.xml.shortcut_5_reverse);
+
+        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2", "ms3", "ms4", "ms5",
+                    "ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
+
+            // Make sure they have the correct ranks, regardless of their ID's alphabetical order.
+            assertWith(getCallerShortcuts()).selectManifest()
+                    .selectByActivity(
+                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()))
+                    .haveRanksInOrder("ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
+            assertWith(getCallerShortcuts()).selectManifest()
+                    .selectByActivity(
+                            new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()))
+                    .haveRanksInOrder("ms5", "ms4", "ms3", "ms2", "ms1");
+        });
+
+        // Package 2 now has no manifest shortcuts.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity.class.getName()),
+                R.xml.shortcut_0);
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_2, ShortcutActivity2.class.getName()),
+                R.xml.shortcut_0);
+        updatePackageLastUpdateTime(CALLING_PACKAGE_2, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_2, USER_0));
+
+        // No manifest shortcuts, and pinned ones are disabled.
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertShortcutIds(assertAllImmutable(assertAllPinned(assertAllDisabled(
+                    mManager.getPinnedShortcuts()))),
+                    "ms2", "ms3");
+        });
+    }
+
+    public void testManifestShortcuts_missingMandatoryFields() {
+        // Start with no apps installed.
+        uninstallPackage(USER_0, CALLING_PACKAGE_1);
+        uninstallPackage(USER_0, CALLING_PACKAGE_2);
+        uninstallPackage(USER_0, CALLING_PACKAGE_3);
+        uninstallPackage(USER_0, CALLING_PACKAGE_4);
+
+        mService.handleUnlockUser(USER_0);
+
+        // Make sure no manifest shortcuts.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+        });
+
+        // Package 1 updated, which has one valid manifest shortcut and one invalid.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_error_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Only the valid one is published.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .areAllManifest()
+                    .areAllImmutable()
+                    .areAllEnabled()
+                    .haveIds("x1");
+        });
+
+        // Package 1 updated, which has one valid manifest shortcut and one invalid.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_error_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Only the valid one is published.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .areAllManifest()
+                    .areAllImmutable()
+                    .areAllEnabled()
+                    .haveIds("x2");
+        });
+
+        // Package 1 updated, which has one valid manifest shortcut and one invalid.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_error_3);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Only the valid one is published.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .areAllManifest()
+                    .areAllImmutable()
+                    .areAllEnabled()
+                    .haveIds("x3")
+                    .forShortcutWithId("x3", si -> {
+                        assertEquals(set("cat2"), si.getCategories());
+                     });
+        });
+    }
+
+    public void testManifestShortcuts_intentDefinitions() {
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_error_4);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            // Make sure invalid ones are not published.
+            // Note that at this point disabled ones don't show up because they weren't pinned.
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2")
+                    .areAllManifest()
+                    .areAllNotDynamic()
+                    .areAllNotPinned()
+                    .areAllImmutable()
+                    .areAllEnabled()
+                    .forShortcutWithId("ms1", si -> {
+                        assertTrue(si.isEnabled());
+                        assertEquals(1, si.getIntents().length);
+
+                        assertEquals("action1", si.getIntent().getAction());
+                        assertEquals("value1", si.getIntent().getStringExtra("key1"));
+                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
+                                Intent.FLAG_ACTIVITY_CLEAR_TASK |
+                                Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntent().getFlags());
+
+                        assertEquals("action1", si.getIntents()[0].getAction());
+                        assertEquals("value1", si.getIntents()[0].getStringExtra("key1"));
+                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
+                                Intent.FLAG_ACTIVITY_CLEAR_TASK |
+                                Intent.FLAG_ACTIVITY_TASK_ON_HOME, si.getIntents()[0].getFlags());
+                    })
+                    .forShortcutWithId("ms2", si -> {
+                        assertTrue(si.isEnabled());
+                        assertEquals(2, si.getIntents().length);
+
+                        // getIntent will return the last one.
+                        assertEquals("action2_2", si.getIntent().getAction());
+                        assertEquals("value2", si.getIntent().getStringExtra("key2"));
+                        assertEquals(0, si.getIntent().getFlags());
+
+                        final Intent i1 = si.getIntents()[0];
+                        final Intent i2 = si.getIntents()[1];
+
+                        assertEquals("action2_1", i1.getAction());
+                        assertEquals("value1", i1.getStringExtra("key1"));
+                        assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK |
+                                        Intent.FLAG_ACTIVITY_CLEAR_TASK |
+                                        Intent.FLAG_ACTIVITY_TASK_ON_HOME, i1.getFlags());
+
+                        assertEquals("action2_2", i2.getAction());
+                        assertEquals("value2", i2.getStringExtra("key2"));
+                        assertEquals(0, i2.getFlags());
+                    });
+        });
+
+        // Publish 5 enabled to pin some, so we can later test disabled manfiest shortcuts..
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_5);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            // Make sure 5 manifest shortcuts are published.
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
+                    .areAllManifest()
+                    .areAllNotDynamic()
+                    .areAllNotPinned()
+                    .areAllImmutable()
+                    .areAllEnabled();
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1,
+                    list("ms3", "ms4", "ms5"), HANDLE_USER_0);
+        });
+
+        // Make sure they're pinned.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
+                    .selectByIds("ms1", "ms2")
+                    .areAllNotPinned()
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms3", "ms4", "ms5")
+                    .areAllPinned()
+                    .areAllEnabled();
+        });
+
+        // Update the app.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_error_4);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Make sure 3, 4 and 5 still exist but disabled.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
+                    .areAllNotDynamic()
+                    .areAllImmutable()
+
+                    .selectByIds("ms1", "ms2")
+                    .areAllManifest()
+                    .areAllNotPinned()
+                    .areAllEnabled()
+
+                    .revertToOriginalList()
+                    .selectByIds("ms3", "ms4", "ms5")
+                    .areAllNotManifest()
+                    .areAllPinned()
+                    .areAllDisabled()
+
+                    .revertToOriginalList()
+                    .forShortcutWithId("ms1", si -> {
+                        assertEquals(si.getId(), "action1", si.getIntent().getAction());
+                    })
+                    .forShortcutWithId("ms2", si -> {
+                        // getIntent returns the last one.
+                        assertEquals(si.getId(), "action2_2", si.getIntent().getAction());
+                    })
+                    .forShortcutWithId("ms3", si -> {
+                        assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
+                    })
+                    .forShortcutWithId("ms4", si -> {
+                        assertEquals(si.getId(), Intent.ACTION_VIEW, si.getIntent().getAction());
+                    })
+                    .forShortcutWithId("ms5", si -> {
+                        assertEquals(si.getId(), "action", si.getIntent().getAction());
+                    });
+        });
+    }
+
+    public void testManifestShortcuts_checkAllFields() {
+        mService.handleUnlockUser(USER_0);
+
+        // Package 1 updated, which has one valid manifest shortcut and one invalid.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_5);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Only the valid one is published.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "ms3", "ms4", "ms5")
+                    .areAllManifest()
+                    .areAllImmutable()
+                    .areAllEnabled()
+                    .areAllNotPinned()
+                    .areAllNotDynamic()
+
+                    .forShortcutWithId("ms1", si -> {
+                        assertEquals(R.drawable.icon1, si.getIconResourceId());
+                        assertEquals(new ComponentName(CALLING_PACKAGE_1,
+                                ShortcutActivity.class.getName()),
+                                si.getActivity());
+
+                        assertEquals(R.string.shortcut_title1, si.getTitleResId());
+                        assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
+                        assertEquals(R.string.shortcut_text1, si.getTextResId());
+                        assertEquals("r" + R.string.shortcut_text1, si.getTextResName());
+                        assertEquals(R.string.shortcut_disabled_message1,
+                                si.getDisabledMessageResourceId());
+                        assertEquals("r" + R.string.shortcut_disabled_message1,
+                                si.getDisabledMessageResName());
+
+                        assertEquals(set("android.shortcut.conversation", "android.shortcut.media"),
+                                si.getCategories());
+                        assertEquals("action1", si.getIntent().getAction());
+                        assertEquals(Uri.parse("http://a.b.c/1"), si.getIntent().getData());
+                    })
+
+                    .forShortcutWithId("ms2", si -> {
+                        assertEquals("ms2", si.getId());
+                        assertEquals(R.drawable.icon2, si.getIconResourceId());
+
+                        assertEquals(R.string.shortcut_title2, si.getTitleResId());
+                        assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
+                        assertEquals(R.string.shortcut_text2, si.getTextResId());
+                        assertEquals("r" + R.string.shortcut_text2, si.getTextResName());
+                        assertEquals(R.string.shortcut_disabled_message2,
+                                si.getDisabledMessageResourceId());
+                        assertEquals("r" + R.string.shortcut_disabled_message2,
+                                si.getDisabledMessageResName());
+
+                        assertEquals(set("android.shortcut.conversation"), si.getCategories());
+                        assertEquals("action2", si.getIntent().getAction());
+                        assertEquals(null, si.getIntent().getData());
+                    })
+
+                    .forShortcutWithId("ms3", si -> {
+                        assertEquals(0, si.getIconResourceId());
+                        assertEquals(R.string.shortcut_title1, si.getTitleResId());
+                        assertEquals("r" + R.string.shortcut_title1, si.getTitleResName());
+
+                        assertEquals(0, si.getTextResId());
+                        assertEquals(null, si.getTextResName());
+                        assertEquals(0, si.getDisabledMessageResourceId());
+                        assertEquals(null, si.getDisabledMessageResName());
+
+                        assertEmpty(si.getCategories());
+                        assertEquals("android.intent.action.VIEW", si.getIntent().getAction());
+                        assertEquals(null, si.getIntent().getData());
+                    })
+
+                    .forShortcutWithId("ms4", si -> {
+                        assertEquals(0, si.getIconResourceId());
+                        assertEquals(R.string.shortcut_title2, si.getTitleResId());
+                        assertEquals("r" + R.string.shortcut_title2, si.getTitleResName());
+
+                        assertEquals(0, si.getTextResId());
+                        assertEquals(null, si.getTextResName());
+                        assertEquals(0, si.getDisabledMessageResourceId());
+                        assertEquals(null, si.getDisabledMessageResName());
+
+                        assertEquals(set("cat"), si.getCategories());
+                        assertEquals("android.intent.action.VIEW2", si.getIntent().getAction());
+                        assertEquals(null, si.getIntent().getData());
+                    })
+
+                    .forShortcutWithId("ms5", si -> {
+                        si = getCallerShortcut("ms5");
+                        assertEquals("action", si.getIntent().getAction());
+                        assertEquals("http://www/", si.getIntent().getData().toString());
+                        assertEquals("foo/bar", si.getIntent().getType());
+                        assertEquals(
+                                new ComponentName("abc", ".xyz"), si.getIntent().getComponent());
+
+                        assertEquals(set("cat1", "cat2"), si.getIntent().getCategories());
+                        assertEquals("value1", si.getIntent().getStringExtra("key1"));
+                        assertEquals("value2", si.getIntent().getStringExtra("key2"));
+                    });
+        });
+    }
+
+    public void testManifestShortcuts_localeChange() throws InterruptedException {
+        mService.handleUnlockUser(USER_0);
+
+        // Package 1 updated, which has one valid manifest shortcut and one invalid.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.setDynamicShortcuts(list(makeShortcutWithTitle("s1", "title")));
+
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2");
+
+            // check first shortcut.
+            ShortcutInfo si = getCallerShortcut("ms1");
+
+            assertEquals("ms1", si.getId());
+            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/en",
+                    si.getTitle());
+            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/en",
+                    si.getText());
+            assertEquals("string-com.android.test.1-user:0-res:"
+                            + R.string.shortcut_disabled_message1 + "/en",
+                    si.getDisabledMessage());
+            assertEquals(START_TIME, si.getLastChangedTimestamp());
+
+            // check another
+            si = getCallerShortcut("ms2");
+
+            assertEquals("ms2", si.getId());
+            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/en",
+                    si.getTitle());
+            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/en",
+                    si.getText());
+            assertEquals("string-com.android.test.1-user:0-res:"
+                            + R.string.shortcut_disabled_message2 + "/en",
+                    si.getDisabledMessage());
+            assertEquals(START_TIME, si.getLastChangedTimestamp());
+
+            // Check the dynamic one.
+            si = getCallerShortcut("s1");
+
+            assertEquals("s1", si.getId());
+            assertEquals("title", si.getTitle());
+            assertEquals(null, si.getText());
+            assertEquals(null, si.getDisabledMessage());
+            assertEquals(START_TIME, si.getLastChangedTimestamp());
+        });
+
+        mInjectedCurrentTimeMillis++;
+
+        // Change the locale and send the broadcast, make sure the launcher gets a callback too.
+        mInjectedLocale = Locale.JAPANESE;
+
+        setCaller(LAUNCHER_1, USER_0);
+
+        assertForLauncherCallback(mLauncherApps, () -> {
+            mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
+        }).assertCallbackCalledForPackageAndUser(CALLING_PACKAGE_1, HANDLE_USER_0)
+                .haveIds("ms1", "ms2", "s1");
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            // check first shortcut.
+            ShortcutInfo si = getCallerShortcut("ms1");
+
+            assertEquals("ms1", si.getId());
+            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title1 + "/ja",
+                    si.getTitle());
+            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text1 + "/ja",
+                    si.getText());
+            assertEquals("string-com.android.test.1-user:0-res:"
+                            + R.string.shortcut_disabled_message1 + "/ja",
+                    si.getDisabledMessage());
+            assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
+
+            // check another
+            si = getCallerShortcut("ms2");
+
+            assertEquals("ms2", si.getId());
+            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_title2 + "/ja",
+                    si.getTitle());
+            assertEquals("string-com.android.test.1-user:0-res:" + R.string.shortcut_text2 + "/ja",
+                    si.getText());
+            assertEquals("string-com.android.test.1-user:0-res:"
+                            + R.string.shortcut_disabled_message2 + "/ja",
+                    si.getDisabledMessage());
+            assertEquals(START_TIME + 1, si.getLastChangedTimestamp());
+
+            // Check the dynamic one.  (locale change shouldn't affect.)
+            si = getCallerShortcut("s1");
+
+            assertEquals("s1", si.getId());
+            assertEquals("title", si.getTitle());
+            assertEquals(null, si.getText());
+            assertEquals(null, si.getDisabledMessage());
+            assertEquals(START_TIME, si.getLastChangedTimestamp()); // Not changed.
+        });
+    }
+
+    public void testManifestShortcuts_updateAndDisabled_notPinned() {
+        mService.handleUnlockUser(USER_0);
+
+        // First, just publish a manifest shortcut.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Only the valid one is published.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertEmpty(mManager.getPinnedShortcuts());
+
+            // Make sure there's no other dangling shortcuts.
+            assertShortcutIds(getCallerShortcuts(), "ms1");
+        });
+
+        // Now version up, the manifest shortcut is disabled now.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1_disable);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Because shortcut 1 wasn't pinned, it'll just go away.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertEmpty(mManager.getPinnedShortcuts());
+
+            // Make sure there's no other dangling shortcuts.
+            assertEmpty(getCallerShortcuts());
+        });
+    }
+
+    public void testManifestShortcuts_updateAndDisabled_pinned() {
+        mService.handleUnlockUser(USER_0);
+
+        // First, just publish a manifest shortcut.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Only the valid one is published.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+            assertEmpty(mManager.getPinnedShortcuts());
+
+            // Make sure there's no other dangling shortcuts.
+            assertShortcutIds(getCallerShortcuts(), "ms1");
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms1"), HANDLE_USER_0);
+        });
+
+        // Now upgrade, the manifest shortcut is disabled now.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1_disable);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        // Because shortcut 1 was pinned, it'll still exist as pinned, but disabled.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEmpty(mManager.getManifestShortcuts());
+            assertShortcutIds(assertAllNotManifest(assertAllImmutable(assertAllDisabled(
+                    mManager.getPinnedShortcuts()))),
+                    "ms1");
+
+            // Make sure the fields are updated.
+            ShortcutInfo si = getCallerShortcut("ms1");
+
+            assertEquals("ms1", si.getId());
+            assertEquals(R.drawable.icon2, si.getIconResourceId());
+            assertEquals(R.string.shortcut_title2, si.getTitleResId());
+            assertEquals(R.string.shortcut_text2, si.getTextResId());
+            assertEquals(R.string.shortcut_disabled_message2, si.getDisabledMessageResourceId());
+            assertEquals(Intent.ACTION_VIEW, si.getIntent().getAction());
+
+            // Make sure there's no other dangling shortcuts.
+            assertShortcutIds(getCallerShortcuts(), "ms1");
+        });
+    }
+
+    public void testManifestShortcuts_duplicateInSingleActivity() {
+        mService.handleUnlockUser(USER_0);
+
+        // The XML has two shortcuts with the same ID.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2_duplicate);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1");
+
+            // Make sure the first one has survived.  (the second one has a different title.)
+            ShortcutInfo si = getCallerShortcut("ms1");
+            assertEquals(R.string.shortcut_title1, si.getTitleResId());
+
+            // Make sure there's no other dangling shortcuts.
+            assertShortcutIds(getCallerShortcuts(), "ms1");
+        });
+    }
+
+    public void testManifestShortcuts_duplicateInTwoActivities() {
+        mService.handleUnlockUser(USER_0);
+
+        // ShortcutActivity has shortcut ms1
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+
+        // ShortcutActivity2 has two shortcuts, ms1 and ms2.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
+                R.xml.shortcut_5);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllManifest(assertAllImmutable(assertAllEnabled(
+                    mManager.getManifestShortcuts()))),
+                    "ms1", "ms2", "ms3", "ms4", "ms5");
+
+            // ms1 should belong to ShortcutActivity.
+            ShortcutInfo si = getCallerShortcut("ms1");
+            assertEquals(R.string.shortcut_title1, si.getTitleResId());
+            assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    si.getActivity());
+            assertEquals(0, si.getRank());
+
+            // ms2 should belong to ShortcutActivity*2*.
+            si = getCallerShortcut("ms2");
+            assertEquals(R.string.shortcut_title2, si.getTitleResId());
+            assertEquals(new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
+                    si.getActivity());
+
+            // Also check the ranks
+            assertWith(getCallerShortcuts()).selectManifest()
+                    .selectByActivity(
+                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()))
+                    .haveRanksInOrder("ms1");
+            assertWith(getCallerShortcuts()).selectManifest()
+                    .selectByActivity(
+                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()))
+                    .haveRanksInOrder("ms2", "ms3", "ms4", "ms5");
+
+            // Make sure there's no other dangling shortcuts.
+            assertShortcutIds(getCallerShortcuts(), "ms1", "ms2", "ms3", "ms4", "ms5");
+        });
+    }
+
+    /**
+     * Manifest shortcuts cannot override shortcuts that were published via the APIs.
+     */
+    public void testManifestShortcuts_cannotOverrideNonManifest() {
+        mService.handleUnlockUser(USER_0);
+
+        // Create a non-pinned dynamic shortcut and a non-dynamic pinned shortcut.
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.setDynamicShortcuts(list(
+                    makeShortcut("ms1", "title1",
+                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    /* icon */ null, new Intent("action1"), /* rank */ 0),
+                    makeShortcut("ms2", "title2",
+                            new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    /* icon */ null, new Intent("action1"), /* rank */ 0)));
+        });
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
+        });
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.removeDynamicShortcuts(list("ms2"));
+
+            assertShortcutIds(mManager.getDynamicShortcuts(), "ms1");
+            assertShortcutIds(mManager.getPinnedShortcuts(), "ms2");
+            assertEmpty(mManager.getManifestShortcuts());
+        });
+
+        // Then update the app with 5 manifest shortcuts.
+        // Make sure "ms1" and "ms2" won't be replaced.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_5);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllNotManifest(mManager.getDynamicShortcuts()), "ms1");
+            assertShortcutIds(assertAllNotManifest(mManager.getPinnedShortcuts()), "ms2");
+            assertShortcutIds(assertAllManifest(mManager.getManifestShortcuts()),
+                    "ms3", "ms4", "ms5");
+
+            // ms1 and ms2 shouold keep the original title.
+            ShortcutInfo si = getCallerShortcut("ms1");
+            assertEquals("title1", si.getTitle());
+
+            si = getCallerShortcut("ms2");
+            assertEquals("title2", si.getTitle());
+        });
+    }
+
+    protected void checkManifestShortcuts_immutable_verify() {
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertShortcutIds(assertAllNotManifest(assertAllEnabled(
+                    mManager.getDynamicShortcuts())),
+                    "s1");
+            assertShortcutIds(assertAllManifest(assertAllEnabled(
+                    mManager.getManifestShortcuts())),
+                    "ms1");
+            assertShortcutIds(assertAllNotManifest(assertAllDisabled(
+                    mManager.getPinnedShortcuts())),
+                    "ms2");
+
+            assertEquals("t1", getCallerShortcut("s1").getTitle());
+
+            // Make sure there are no other shortcuts.
+            assertShortcutIds(getCallerShortcuts(), "s1", "ms1", "ms2");
+        });
+    }
+
+    /**
+     * Make sure the APIs won't work on manifest shortcuts.
+     */
+    public void testManifestShortcuts_immutable() {
+        mService.handleUnlockUser(USER_0);
+
+        // Create a non-pinned manifest shortcut, a pinned shortcut that was originally
+        // a manifest shortcut, as well as a dynamic shortcut.
+
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2"), HANDLE_USER_0);
+        });
+
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_1);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.addDynamicShortcuts(list(makeShortcutWithTitle("s1", "t1")));
+        });
+
+        checkManifestShortcuts_immutable_verify();
+
+        // Note that even though the first argument is not immutable and only the second one
+        // is immutable, the first argument should not be executed either.
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertCannotUpdateImmutable(() -> {
+                mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
+            });
+            assertCannotUpdateImmutable(() -> {
+                mManager.setDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
+            });
+        });
+        checkManifestShortcuts_immutable_verify();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertCannotUpdateImmutable(() -> {
+                mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms1")));
+            });
+            assertCannotUpdateImmutable(() -> {
+                mManager.addDynamicShortcuts(list(makeShortcut("xx"), makeShortcut("ms2")));
+            });
+        });
+        checkManifestShortcuts_immutable_verify();
+
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertCannotUpdateImmutable(() -> {
+                mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms1")));
+            });
+            assertCannotUpdateImmutable(() -> {
+                mManager.updateShortcuts(list(makeShortcut("s1"), makeShortcut("ms2")));
+            });
+        });
+        checkManifestShortcuts_immutable_verify();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertCannotUpdateImmutable(() -> {
+                mManager.removeDynamicShortcuts(list("s1", "ms1"));
+            });
+            assertCannotUpdateImmutable(() -> {
+                mManager.removeDynamicShortcuts(list("s2", "ms2"));
+            });
+        });
+        checkManifestShortcuts_immutable_verify();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertCannotUpdateImmutable(() -> {
+                mManager.disableShortcuts(list("s1", "ms1"));
+            });
+        });
+        checkManifestShortcuts_immutable_verify();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertCannotUpdateImmutable(() -> {
+                mManager.enableShortcuts(list("s1", "ms2"));
+            });
+        });
+        checkManifestShortcuts_immutable_verify();
+    }
+
+
+    /**
+     * Make sure the APIs won't work on manifest shortcuts.
+     */
+    public void testManifestShortcuts_tooMany() {
+        // Change the max number of shortcuts.
+        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
+
+        mService.handleUnlockUser(USER_0);
+
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_5);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+                mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            // Only the first 3 should be published.
+            assertShortcutIds(mManager.getManifestShortcuts(), "ms1", "ms2", "ms3");
+        });
+    }
+
+    public void testMaxShortcutCount_set() {
+        // Change the max number of shortcuts.
+        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
+            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
+            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
+            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
+            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
+            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
+            final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
+            final ShortcutInfo s1_6 = makeShortcutWithActivity("s16", a1);
+            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
+            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
+            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
+            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
+
+            // 3 shortcuts for 2 activities -> okay
+            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13", "s21", "s22", "s23");
+
+            mManager.removeAllDynamicShortcuts();
+
+            // 4 shortcut for activity 1 -> too many.
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
+            });
+            assertEmpty(mManager.getDynamicShortcuts());
+
+            // 4 shortcut for activity 2 -> too many.
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
+            });
+            assertEmpty(mManager.getDynamicShortcuts());
+
+            // First, set 3.  Then set 4, which should be ignored.
+            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13");
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.setDynamicShortcuts(list(s2_1, s2_2, s2_3, s2_4));
+            });
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13");
+
+            // Set will remove the old dynamic set, unlike add, so the following should pass.
+            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13");
+            mManager.setDynamicShortcuts(list(s1_4, s1_5, s1_6));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s14", "s15", "s16");
+
+            // Now, test with 2 manifest shortcuts.
+            mManager.removeAllDynamicShortcuts();
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    R.xml.shortcut_2);
+            updatePackageVersion(CALLING_PACKAGE_1, 1);
+                    mService.mPackageMonitor.onReceive(getTestContext(),
+                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+            assertEquals(2, mManager.getManifestShortcuts().size());
+
+            // Setting 1 to activity 1 will work.
+            mManager.setDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s21", "s22", "s23");
+            assertEquals(2, mManager.getManifestShortcuts().size());
+
+            // But setting 2 will not.
+            mManager.removeAllDynamicShortcuts();
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.setDynamicShortcuts(list(s1_1, s1_2, s2_1, s2_2, s2_3));
+            });
+            assertEmpty(mManager.getDynamicShortcuts());
+            assertEquals(2, mManager.getManifestShortcuts().size());
+        });
+    }
+
+    public void testMaxShortcutCount_add() {
+        // Change the max number of shortcuts.
+        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
+            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
+            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
+            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
+            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
+            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
+            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
+            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
+            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
+            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
+
+            // 3 shortcuts for 2 activities -> okay
+            mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13", "s21", "s22", "s23");
+
+            mManager.removeAllDynamicShortcuts();
+
+            // 4 shortcut for activity 1 -> too many.
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s1_4, s2_1, s2_2, s2_3));
+            });
+            assertEmpty(mManager.getDynamicShortcuts());
+
+            // 4 shortcut for activity 2 -> too many.
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.addDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3, s2_4));
+            });
+            assertEmpty(mManager.getDynamicShortcuts());
+
+            // First, set 3.  Then add 1 more, which should be ignored.
+            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13");
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.addDynamicShortcuts(list(s1_4, s2_1));
+            });
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13");
+
+            // Update existing one, which should work.
+            mManager.addDynamicShortcuts(list(makeShortcutWithActivityAndTitle(
+                    "s11", a1, "xxx"), s2_1));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13", "s21");
+            assertEquals("xxx", getCallerShortcut("s11").getTitle());
+
+            // Make sure pinned shortcuts won't affect.
+            // - Pin s11 - s13, and remove all dynamic.
+            runWithCaller(LAUNCHER_1, USER_0, () -> {
+                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
+                        HANDLE_USER_0);
+            });
+            mManager.removeAllDynamicShortcuts();
+
+            assertEmpty(mManager.getDynamicShortcuts());
+            assertShortcutIds(mManager.getPinnedShortcuts(),
+                    "s11", "s12", "s13");
+
+            // Then add dynamic.
+            mManager.addDynamicShortcuts(list(s1_4, s2_1, s2_2, s2_3));
+
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s14", "s21", "s22", "s23");
+            assertShortcutIds(mManager.getPinnedShortcuts(),
+                    "s11", "s12", "s13");
+
+            // Adding "s11" and "s12" back, should work
+            mManager.addDynamicShortcuts(list(s1_1, s1_2));
+
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s14", "s11", "s12", "s21", "s22", "s23");
+            assertShortcutIds(mManager.getPinnedShortcuts(),
+                    "s11", "s12", "s13");
+
+            // Adding back s13 doesn't work.
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.addDynamicShortcuts(list(s1_3));
+            });
+
+            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
+                    "s11", "s12", "s14");
+            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
+                    "s21", "s22", "s23");
+
+            // Now swap the activities.
+            mManager.updateShortcuts(list(
+                    makeShortcutWithActivity("s11", a2),
+                    makeShortcutWithActivity("s21", a1)));
+
+            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
+                    "s21", "s12", "s14");
+            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
+                    "s11", "s22", "s23");
+
+            // Now, test with 2 manifest shortcuts.
+            mManager.removeAllDynamicShortcuts();
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    R.xml.shortcut_2);
+            updatePackageVersion(CALLING_PACKAGE_1, 1);
+                    mService.mPackageMonitor.onReceive(getTestContext(),
+                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+
+            assertEquals(2, mManager.getManifestShortcuts().size());
+
+            // Adding one shortcut to activity 1 works fine.
+            mManager.addDynamicShortcuts(list(s1_1, s2_1, s2_2, s2_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s21", "s22", "s23");
+            assertEquals(2, mManager.getManifestShortcuts().size());
+
+            // But adding one more doesn't.
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.addDynamicShortcuts(list(s1_4, s2_1));
+            });
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s21", "s22", "s23");
+            assertEquals(2, mManager.getManifestShortcuts().size());
+        });
+    }
+
+    public void testMaxShortcutCount_update() {
+        // Change the max number of shortcuts.
+        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
+            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
+            final ShortcutInfo s1_1 = makeShortcutWithActivity("s11", a1);
+            final ShortcutInfo s1_2 = makeShortcutWithActivity("s12", a1);
+            final ShortcutInfo s1_3 = makeShortcutWithActivity("s13", a1);
+            final ShortcutInfo s1_4 = makeShortcutWithActivity("s14", a1);
+            final ShortcutInfo s1_5 = makeShortcutWithActivity("s15", a1);
+            final ShortcutInfo s2_1 = makeShortcutWithActivity("s21", a2);
+            final ShortcutInfo s2_2 = makeShortcutWithActivity("s22", a2);
+            final ShortcutInfo s2_3 = makeShortcutWithActivity("s23", a2);
+            final ShortcutInfo s2_4 = makeShortcutWithActivity("s24", a2);
+
+            // 3 shortcuts for 2 activities -> okay
+            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13", "s21", "s22", "s23");
+
+            // Trying to move s11 from a1 to a2 should fail.
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.updateShortcuts(list(makeShortcutWithActivity("s11", a2)));
+            });
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13", "s21", "s22", "s23");
+
+            // Trying to move s21 from a2 to a1 should also fail.
+            assertDynamicShortcutCountExceeded(() -> {
+                mManager.updateShortcuts(list(makeShortcutWithActivity("s21", a1)));
+            });
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13", "s21", "s22", "s23");
+
+            // But, if we do these two at the same time, it should work.
+            mManager.updateShortcuts(list(
+                    makeShortcutWithActivity("s11", a2),
+                    makeShortcutWithActivity("s21", a1)));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13", "s21", "s22", "s23");
+            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
+                    "s21", "s12", "s13");
+            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
+                    "s11", "s22", "s23");
+
+            // Then reset.
+            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s11", "s12", "s13", "s21", "s22", "s23");
+
+            // Pin some to have more shortcuts for a1.
+            runWithCaller(LAUNCHER_1, USER_0, () -> {
+                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s13"),
+                        HANDLE_USER_0);
+            });
+            mManager.setDynamicShortcuts(list(s1_4, s1_5, s2_1, s2_2, s2_3));
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s14", "s15", "s21", "s22", "s23");
+            assertShortcutIds(mManager.getPinnedShortcuts(),
+                    "s11", "s12", "s13");
+
+            // a1 already has 2 dynamic shortcuts (and 3 pinned shortcuts that used to belong on it)
+            // But that doesn't matter for update -- the following should still work.
+            mManager.updateShortcuts(list(
+                    makeShortcutWithActivityAndTitle("s11", a1, "xxx1"),
+                    makeShortcutWithActivityAndTitle("s12", a1, "xxx2"),
+                    makeShortcutWithActivityAndTitle("s13", a1, "xxx3"),
+                    makeShortcutWithActivityAndTitle("s14", a1, "xxx4"),
+                    makeShortcutWithActivityAndTitle("s15", a1, "xxx5")));
+            // All the shortcuts should still exist they all belong on same activities,
+            // with the updated titles.
+            assertShortcutIds(mManager.getDynamicShortcuts(),
+                    "s14", "s15", "s21", "s22", "s23");
+            assertShortcutIds(mManager.getPinnedShortcuts(),
+                    "s11", "s12", "s13");
+
+            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a1),
+                    "s14", "s15");
+            assertShortcutIds(filterByActivity(mManager.getDynamicShortcuts(), a2),
+                    "s21", "s22", "s23");
+
+            assertEquals("xxx1", getCallerShortcut("s11").getTitle());
+            assertEquals("xxx2", getCallerShortcut("s12").getTitle());
+            assertEquals("xxx3", getCallerShortcut("s13").getTitle());
+            assertEquals("xxx4", getCallerShortcut("s14").getTitle());
+            assertEquals("xxx5", getCallerShortcut("s15").getTitle());
+        });
+    }
+
+    public void testShortcutsPushedOutByManifest() {
+        // Change the max number of shortcuts.
+        mService.updateConfigurationLocked(ConfigConstants.KEY_MAX_SHORTCUTS + "=3");
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            final ComponentName a1 = new ComponentName(mClientContext, ShortcutActivity.class);
+            final ComponentName a2 = new ComponentName(mClientContext, ShortcutActivity2.class);
+            final ShortcutInfo s1_1 = makeShortcutWithActivityAndRank("s11", a1, 4);
+            final ShortcutInfo s1_2 = makeShortcutWithActivityAndRank("s12", a1, 3);
+            final ShortcutInfo s1_3 = makeShortcutWithActivityAndRank("s13", a1, 2);
+            final ShortcutInfo s1_4 = makeShortcutWithActivityAndRank("s14", a1, 1);
+            final ShortcutInfo s1_5 = makeShortcutWithActivityAndRank("s15", a1, 0);
+            final ShortcutInfo s2_1 = makeShortcutWithActivityAndRank("s21", a2, 0);
+            final ShortcutInfo s2_2 = makeShortcutWithActivityAndRank("s22", a2, 1);
+            final ShortcutInfo s2_3 = makeShortcutWithActivityAndRank("s23", a2, 2);
+            final ShortcutInfo s2_4 = makeShortcutWithActivityAndRank("s24", a2, 3);
+            final ShortcutInfo s2_5 = makeShortcutWithActivityAndRank("s25", a2, 4);
+
+            // Initial state.
+            mManager.setDynamicShortcuts(list(s1_1, s1_2, s1_3, s2_1, s2_2, s2_3));
+            runWithCaller(LAUNCHER_1, USER_0, () -> {
+                mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("s11", "s12", "s21", "s22"),
+                        HANDLE_USER_0);
+            });
+            mManager.setDynamicShortcuts(list(s1_2, s1_3, s1_4, s2_2, s2_3, s2_4));
+            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
+                    "s12", "s13", "s14",
+                    "s22", "s23", "s24");
+            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
+                    "s11", "s12",
+                    "s21", "s22");
+
+            // Add 1 manifest shortcut to a1.
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    R.xml.shortcut_1);
+            updatePackageVersion(CALLING_PACKAGE_1, 1);
+                    mService.mPackageMonitor.onReceive(getTestContext(),
+                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+            assertEquals(1, mManager.getManifestShortcuts().size());
+
+            // s12 removed.
+            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
+                    "s13", "s14",
+                    "s22", "s23", "s24");
+            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
+                    "s11", "s12",
+                    "s21", "s22");
+
+            // Add more manifest shortcuts.
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    R.xml.shortcut_2);
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
+                    R.xml.shortcut_1_alt);
+            updatePackageVersion(CALLING_PACKAGE_1, 1);
+                    mService.mPackageMonitor.onReceive(getTestContext(),
+                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+            assertEquals(3, mManager.getManifestShortcuts().size());
+
+            // Note the ones with the highest rank values (== least important) will be removed.
+            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
+                    "s14",
+                    "s22", "s23");
+            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
+                    "s11", "s12",
+                    "s21", "s22");
+
+            // Add more manifest shortcuts.
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    R.xml.shortcut_2);
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
+                    R.xml.shortcut_5_alt); // manifest has 5, but max is 3, so a2 will have 3.
+            updatePackageVersion(CALLING_PACKAGE_1, 1);
+                    mService.mPackageMonitor.onReceive(getTestContext(),
+                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+            assertEquals(5, mManager.getManifestShortcuts().size());
+
+            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
+                    "s14" // a1 has 1 dynamic
+            ); // a2 has no dynamic
+            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
+                    "s11", "s12",
+                    "s21", "s22");
+
+            // Update, no manifest shortucts.  This doesn't affect anything.
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                    R.xml.shortcut_0);
+            addManifestShortcutResource(
+                    new ComponentName(CALLING_PACKAGE_1, ShortcutActivity2.class.getName()),
+                    R.xml.shortcut_0);
+            updatePackageVersion(CALLING_PACKAGE_1, 1);
+                    mService.mPackageMonitor.onReceive(getTestContext(),
+                    genPackageAddIntent(CALLING_PACKAGE_1, USER_0));
+            assertEquals(0, mManager.getManifestShortcuts().size());
+
+            assertShortcutIds(assertAllEnabled(mManager.getDynamicShortcuts()),
+                    "s14");
+            assertShortcutIds(assertAllEnabled(mManager.getPinnedShortcuts()),
+                    "s11", "s12",
+                    "s21", "s22");
+        });
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
new file mode 100644
index 0000000..d25923c
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
@@ -0,0 +1,2040 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBundlesEqual;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.parceled;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
+
+import static org.mockito.Matchers.anyInt;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.Manifest.permission;
+import android.app.ActivityManager;
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.pm.ShortcutInfo;
+import android.content.res.Resources;
+import android.graphics.BitmapFactory;
+import android.graphics.drawable.Icon;
+import android.net.Uri;
+import android.os.PersistableBundle;
+import android.os.UserHandle;
+import android.test.MoreAsserts;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.frameworks.servicestests.R;
+import com.android.server.pm.ShortcutService.ConfigConstants;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.Locale;
+
+/**
+ * Tests for ShortcutService and ShortcutManager.
+ *
+ m FrameworksServicesTests &&
+ adb install \
+ -r -g ${ANDROID_PRODUCT_OUT}/data/app/FrameworksServicesTests/FrameworksServicesTests.apk &&
+ adb shell am instrument -e class com.android.server.pm.ShortcutManagerTest2 \
+ -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
+ */
+@SmallTest
+public class ShortcutManagerTest2 extends BaseShortcutManagerTest {
+    // ShortcutInfo tests
+
+    public void testShortcutInfoMissingMandatoryFields() {
+        // Disable throttling.
+        mService.updateConfigurationLocked(
+                ShortcutService.ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999,"
+                + ShortcutService.ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999"
+        );
+
+        assertExpectException(
+                IllegalArgumentException.class,
+                "ID must be provided",
+                () -> new ShortcutInfo.Builder(getTestContext()).build());
+
+        assertExpectException(
+                RuntimeException.class,
+                "id cannot be empty",
+                () -> new ShortcutInfo.Builder(getTestContext(), null));
+
+        assertExpectException(
+                RuntimeException.class,
+                "id cannot be empty",
+                () -> new ShortcutInfo.Builder(getTestContext(), ""));
+
+        assertExpectException(
+                RuntimeException.class,
+                "intents cannot contain null",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setIntent(null));
+
+        assertExpectException(
+                RuntimeException.class,
+                "action must be set",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setIntent(new Intent()));
+
+        assertExpectException(
+                RuntimeException.class,
+                "action must be set",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id")
+                        .setIntents(new Intent[]{new Intent("action"), new Intent()}));
+
+        assertExpectException(
+                RuntimeException.class,
+                "activity cannot be null",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setActivity(null));
+
+        assertExpectException(
+                RuntimeException.class,
+                "shortLabel cannot be empty",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setShortLabel(null));
+
+        assertExpectException(
+                RuntimeException.class,
+                "shortLabel cannot be empty",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setShortLabel(""));
+
+        assertExpectException(
+                RuntimeException.class,
+                "longLabel cannot be empty",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setLongLabel(null));
+
+        assertExpectException(
+                RuntimeException.class,
+                "longLabel cannot be empty",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setLongLabel(""));
+
+        assertExpectException(
+                RuntimeException.class,
+                "disabledMessage cannot be empty",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setDisabledMessage(null));
+
+        assertExpectException(
+                RuntimeException.class,
+                "disabledMessage cannot be empty",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setDisabledMessage(""));
+
+        assertExpectException(NullPointerException.class, "action must be set",
+                () -> new ShortcutInfo.Builder(getTestContext(), "id").setIntent(new Intent()));
+
+        assertExpectException(
+                IllegalArgumentException.class, "Short label must be provided", () -> {
+            ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                    .setActivity(new ComponentName(getTestContext().getPackageName(), "s"))
+                    .build();
+            assertTrue(getManager().setDynamicShortcuts(list(si)));
+        });
+
+        // same for add.
+        assertExpectException(
+                IllegalArgumentException.class, "Short label must be provided", () -> {
+            ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                    .setActivity(new ComponentName(getTestContext().getPackageName(), "s"))
+                    .build();
+            assertTrue(getManager().addDynamicShortcuts(list(si)));
+        });
+
+        assertExpectException(NullPointerException.class, "Intent must be provided", () -> {
+            ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                    .setActivity(new ComponentName(getTestContext().getPackageName(), "s"))
+                    .setShortLabel("x")
+                    .build();
+            assertTrue(getManager().setDynamicShortcuts(list(si)));
+        });
+
+        // same for add.
+        assertExpectException(NullPointerException.class, "Intent must be provided", () -> {
+            ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                    .setActivity(new ComponentName(getTestContext().getPackageName(), "s"))
+                    .setShortLabel("x")
+                    .build();
+            assertTrue(getManager().addDynamicShortcuts(list(si)));
+        });
+
+        assertExpectException(
+                IllegalStateException.class, "does not belong to package", () -> {
+            ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                    .setActivity(new ComponentName("xxx", "s"))
+                    .build();
+            assertTrue(getManager().setDynamicShortcuts(list(si)));
+        });
+
+        // same for add.
+        assertExpectException(
+                IllegalStateException.class, "does not belong to package", () -> {
+            ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                    .setActivity(new ComponentName("xxx", "s"))
+                    .build();
+            assertTrue(getManager().addDynamicShortcuts(list(si)));
+        });
+
+        // Now all activities are not main.
+        mMainActivityChecker = (component, userId) -> false;
+
+        assertExpectException(
+                IllegalStateException.class, "is not main", () -> {
+                    ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                            .setActivity(new ComponentName(getTestContext(), "s"))
+                            .build();
+                    assertTrue(getManager().setDynamicShortcuts(list(si)));
+                });
+        // For add
+        assertExpectException(
+                IllegalStateException.class, "is not main", () -> {
+                    ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                            .setActivity(new ComponentName(getTestContext(), "s"))
+                            .build();
+                    assertTrue(getManager().addDynamicShortcuts(list(si)));
+                });
+        // For update
+        assertExpectException(
+                IllegalStateException.class, "is not main", () -> {
+                    ShortcutInfo si = new ShortcutInfo.Builder(getTestContext(), "id")
+                            .setActivity(new ComponentName(getTestContext(), "s"))
+                            .build();
+                    assertTrue(getManager().updateShortcuts(list(si)));
+                });
+    }
+
+    public void testShortcutInfoParcel() {
+        setCaller(CALLING_PACKAGE_1, USER_10);
+        ShortcutInfo si = parceled(new ShortcutInfo.Builder(mClientContext)
+                .setId("id")
+                .setTitle("title")
+                .setIntent(makeIntent("action", ShortcutActivity.class))
+                .build());
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals(USER_10, si.getUserId());
+        assertEquals(HANDLE_USER_10, si.getUserHandle());
+        assertEquals("id", si.getId());
+        assertEquals("title", si.getTitle());
+        assertEquals("action", si.getIntent().getAction());
+
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+
+        si = new ShortcutInfo.Builder(getTestContext())
+                .setId("id")
+                .setActivity(new ComponentName("a", "b"))
+                .setIcon(Icon.createWithResource(mClientContext, 123))
+                .setTitle("title")
+                .setText("text")
+                .setDisabledMessage("dismes")
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+        si.addFlags(ShortcutInfo.FLAG_PINNED);
+        si.setBitmapPath("abc");
+        si.setIconResourceId(456);
+
+        si = parceled(si);
+
+        assertEquals(getTestContext().getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(123, si.getIcon().getResId());
+        assertEquals("title", si.getTitle());
+        assertEquals("text", si.getText());
+        assertEquals("dismes", si.getDisabledMessage());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(123, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags());
+        assertEquals("abc", si.getBitmapPath());
+        assertEquals(456, si.getIconResourceId());
+
+        assertEquals(0, si.getTitleResId());
+        assertEquals(null, si.getTitleResName());
+        assertEquals(0, si.getTextResId());
+        assertEquals(null, si.getTextResName());
+        assertEquals(0, si.getDisabledMessageResourceId());
+        assertEquals(null, si.getDisabledMessageResName());
+    }
+
+    public void testShortcutInfoParcel_resId() {
+        setCaller(CALLING_PACKAGE_1, USER_10);
+        ShortcutInfo si;
+
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+
+        si = new ShortcutInfo.Builder(getTestContext())
+                .setId("id")
+                .setActivity(new ComponentName("a", "b"))
+                .setIcon(Icon.createWithResource(mClientContext, 123))
+                .setTitleResId(10)
+                .setTextResId(11)
+                .setDisabledMessageResId(12)
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+        si.addFlags(ShortcutInfo.FLAG_PINNED);
+        si.setBitmapPath("abc");
+        si.setIconResourceId(456);
+
+        lookupAndFillInResourceNames(si);
+
+        si = parceled(si);
+
+        assertEquals(getTestContext().getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(123, si.getIcon().getResId());
+        assertEquals(10, si.getTitleResId());
+        assertEquals("r10", si.getTitleResName());
+        assertEquals(11, si.getTextResId());
+        assertEquals("r11", si.getTextResName());
+        assertEquals(12, si.getDisabledMessageResourceId());
+        assertEquals("r12", si.getDisabledMessageResName());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(123, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags());
+        assertEquals("abc", si.getBitmapPath());
+        assertEquals(456, si.getIconResourceId());
+        assertEquals("string/r456", si.getIconResName());
+    }
+
+    public void testShortcutInfoClone() {
+        setCaller(CALLING_PACKAGE_1, USER_11);
+
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(mClientContext)
+                .setId("id")
+                .setActivity(new ComponentName("a", "b"))
+                .setIcon(Icon.createWithResource(mClientContext, 123))
+                .setTitle("title")
+                .setText("text")
+                .setDisabledMessage("dismes")
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+        sorig.addFlags(ShortcutInfo.FLAG_PINNED);
+        sorig.setBitmapPath("abc");
+        sorig.setIconResourceId(456);
+
+        lookupAndFillInResourceNames(sorig);
+
+        ShortcutInfo si = sorig.clone(/* clone flags*/ 0);
+
+        assertEquals(USER_11, si.getUserId());
+        assertEquals(HANDLE_USER_11, si.getUserHandle());
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(123, si.getIcon().getResId());
+        assertEquals("title", si.getTitle());
+        assertEquals("text", si.getText());
+        assertEquals("dismes", si.getDisabledMessage());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(123, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags());
+        assertEquals("abc", si.getBitmapPath());
+        assertEquals(456, si.getIconResourceId());
+        assertEquals("string/r456", si.getIconResName());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_CREATOR);
+
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(null, si.getIcon());
+        assertEquals("title", si.getTitle());
+        assertEquals("text", si.getText());
+        assertEquals("dismes", si.getDisabledMessage());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(123, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags());
+        assertEquals(null, si.getBitmapPath());
+
+        assertEquals(456, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER);
+
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(null, si.getIcon());
+        assertEquals("title", si.getTitle());
+        assertEquals("text", si.getText());
+        assertEquals("dismes", si.getDisabledMessage());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals(null, si.getIntent());
+        assertEquals(123, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags());
+        assertEquals(null, si.getBitmapPath());
+
+        assertEquals(456, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO);
+
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(null, si.getIcon());
+        assertEquals(null, si.getTitle());
+        assertEquals(null, si.getText());
+        assertEquals(null, si.getDisabledMessage());
+        assertEquals(null, si.getCategories());
+        assertEquals(null, si.getIntent());
+        assertEquals(0, si.getRank());
+        assertEquals(null, si.getExtras());
+
+        assertEquals(ShortcutInfo.FLAG_PINNED | ShortcutInfo.FLAG_KEY_FIELDS_ONLY, si.getFlags());
+        assertEquals(null, si.getBitmapPath());
+
+        assertEquals(456, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+    }
+
+    public void testShortcutInfoClone_resId() {
+        setCaller(CALLING_PACKAGE_1, USER_11);
+
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(mClientContext)
+                .setId("id")
+                .setActivity(new ComponentName("a", "b"))
+                .setIcon(Icon.createWithResource(mClientContext, 123))
+                .setTitleResId(10)
+                .setTextResId(11)
+                .setDisabledMessageResId(12)
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+        sorig.addFlags(ShortcutInfo.FLAG_PINNED);
+        sorig.setBitmapPath("abc");
+        sorig.setIconResourceId(456);
+
+        lookupAndFillInResourceNames(sorig);
+
+        ShortcutInfo si = sorig.clone(/* clone flags*/ 0);
+
+        assertEquals(USER_11, si.getUserId());
+        assertEquals(HANDLE_USER_11, si.getUserHandle());
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(123, si.getIcon().getResId());
+        assertEquals(10, si.getTitleResId());
+        assertEquals("r10", si.getTitleResName());
+        assertEquals(11, si.getTextResId());
+        assertEquals("r11", si.getTextResName());
+        assertEquals(12, si.getDisabledMessageResourceId());
+        assertEquals("r12", si.getDisabledMessageResName());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(123, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags());
+        assertEquals("abc", si.getBitmapPath());
+        assertEquals(456, si.getIconResourceId());
+        assertEquals("string/r456", si.getIconResName());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_CREATOR);
+
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(null, si.getIcon());
+        assertEquals(10, si.getTitleResId());
+        assertEquals(null, si.getTitleResName());
+        assertEquals(11, si.getTextResId());
+        assertEquals(null, si.getTextResName());
+        assertEquals(12, si.getDisabledMessageResourceId());
+        assertEquals(null, si.getDisabledMessageResName());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(123, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags());
+        assertEquals(null, si.getBitmapPath());
+
+        assertEquals(456, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER);
+
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(null, si.getIcon());
+        assertEquals(10, si.getTitleResId());
+        assertEquals(null, si.getTitleResName());
+        assertEquals(11, si.getTextResId());
+        assertEquals(null, si.getTextResName());
+        assertEquals(12, si.getDisabledMessageResourceId());
+        assertEquals(null, si.getDisabledMessageResName());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals(null, si.getIntent());
+        assertEquals(123, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED, si.getFlags());
+        assertEquals(null, si.getBitmapPath());
+
+        assertEquals(456, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO);
+
+        assertEquals(mClientContext.getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(new ComponentName("a", "b"), si.getActivity());
+        assertEquals(null, si.getIcon());
+        assertEquals(0, si.getTitleResId());
+        assertEquals(null, si.getTitleResName());
+        assertEquals(0, si.getTextResId());
+        assertEquals(null, si.getTextResName());
+        assertEquals(0, si.getDisabledMessageResourceId());
+        assertEquals(null, si.getDisabledMessageResName());
+        assertEquals(null, si.getCategories());
+        assertEquals(null, si.getIntent());
+        assertEquals(0, si.getRank());
+        assertEquals(null, si.getExtras());
+
+        assertEquals(ShortcutInfo.FLAG_PINNED | ShortcutInfo.FLAG_KEY_FIELDS_ONLY, si.getFlags());
+        assertEquals(null, si.getBitmapPath());
+
+        assertEquals(456, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+    }
+
+    public void testShortcutInfoClone_minimum() {
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(getTestContext())
+                .setId("id")
+                .setTitle("title")
+                .setIntent(makeIntent("action", ShortcutActivity.class))
+                .build();
+        ShortcutInfo si = sorig.clone(/* clone flags*/ 0);
+
+        assertEquals(getTestContext().getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals("title", si.getTitle());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals(null, si.getCategories());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_CREATOR);
+
+        assertEquals(getTestContext().getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals("title", si.getTitle());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals(null, si.getCategories());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER);
+
+        assertEquals(getTestContext().getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals("title", si.getTitle());
+        assertEquals(null, si.getIntent());
+        assertEquals(null, si.getCategories());
+
+        si = sorig.clone(ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO);
+
+        assertEquals(getTestContext().getPackageName(), si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(null, si.getTitle());
+        assertEquals(null, si.getIntent());
+        assertEquals(null, si.getCategories());
+    }
+
+    public void testShortcutInfoCopyNonNullFieldsFrom() throws InterruptedException {
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(getTestContext())
+                .setId("id")
+                .setActivity(new ComponentName("a", "b"))
+                .setIcon(Icon.createWithResource(mClientContext, 123))
+                .setTitle("title")
+                .setText("text")
+                .setDisabledMessage("dismes")
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+        sorig.addFlags(ShortcutInfo.FLAG_PINNED);
+        sorig.setBitmapPath("abc");
+        sorig.setIconResourceId(456);
+
+        lookupAndFillInResourceNames(sorig);
+
+        ShortcutInfo si;
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setActivity(new ComponentName("x", "y")).build());
+        assertEquals("text", si.getText());
+        assertEquals(123, si.getRank());
+        assertEquals(new ComponentName("x", "y"), si.getActivity());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setIcon(Icon.createWithResource(mClientContext, 456)).build());
+        assertEquals("text", si.getText());
+        assertEquals(456, si.getIcon().getResId());
+        assertEquals(0, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+        assertEquals(null, si.getBitmapPath());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setTitle("xyz").build());
+        assertEquals("text", si.getText());
+        assertEquals("xyz", si.getTitle());
+        assertEquals(0, si.getTitleResId());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setTitleResId(123).build());
+        assertEquals("text", si.getText());
+        assertEquals(null, si.getTitle());
+        assertEquals(123, si.getTitleResId());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setText("xxx").build());
+        assertEquals(123, si.getRank());
+        assertEquals("xxx", si.getText());
+        assertEquals(0, si.getTextResId());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setTextResId(1111).build());
+        assertEquals(123, si.getRank());
+        assertEquals(null, si.getText());
+        assertEquals(1111, si.getTextResId());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setDisabledMessage("xxx").build());
+        assertEquals(123, si.getRank());
+        assertEquals("xxx", si.getDisabledMessage());
+        assertEquals(0, si.getDisabledMessageResourceId());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setDisabledMessageResId(11111).build());
+        assertEquals(123, si.getRank());
+        assertEquals(null, si.getDisabledMessage());
+        assertEquals(11111, si.getDisabledMessageResourceId());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setCategories(set()).build());
+        assertEquals("text", si.getText());
+        assertEquals(set(), si.getCategories());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setCategories(set("x")).build());
+        assertEquals("text", si.getText());
+        assertEquals(set("x"), si.getCategories());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setIntent(makeIntent("action2", ShortcutActivity.class)).build());
+        assertEquals("text", si.getText());
+        assertEquals("action2", si.getIntent().getAction());
+        assertEquals(null, si.getIntent().getStringExtra("key"));
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setIntent(makeIntent("action3", ShortcutActivity.class, "key", "x")).build());
+        assertEquals("text", si.getText());
+        assertEquals("action3", si.getIntent().getAction());
+        assertEquals("x", si.getIntent().getStringExtra("key"));
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setRank(999).build());
+        assertEquals("text", si.getText());
+        assertEquals(999, si.getRank());
+
+
+        PersistableBundle pb2 = new PersistableBundle();
+        pb2.putInt("x", 99);
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setExtras(pb2).build());
+        assertEquals("text", si.getText());
+        assertEquals(99, si.getExtras().getInt("x"));
+    }
+
+    public void testShortcutInfoCopyNonNullFieldsFrom_resId() throws InterruptedException {
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(getTestContext())
+                .setId("id")
+                .setActivity(new ComponentName("a", "b"))
+                .setIcon(Icon.createWithResource(mClientContext, 123))
+                .setTitleResId(10)
+                .setTextResId(11)
+                .setDisabledMessageResId(12)
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+        sorig.addFlags(ShortcutInfo.FLAG_PINNED);
+        sorig.setBitmapPath("abc");
+        sorig.setIconResourceId(456);
+
+        ShortcutInfo si;
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setActivity(new ComponentName("x", "y")).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals(new ComponentName("x", "y"), si.getActivity());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setIcon(Icon.createWithResource(mClientContext, 456)).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals(456, si.getIcon().getResId());
+        assertEquals(0, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+        assertEquals(null, si.getBitmapPath());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setTitle("xyz").build());
+        assertEquals(11, si.getTextResId());
+        assertEquals("xyz", si.getTitle());
+        assertEquals(0, si.getTitleResId());
+        assertEquals(null, si.getTitleResName());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setTitleResId(123).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals(null, si.getTitle());
+        assertEquals(123, si.getTitleResId());
+        assertEquals(null, si.getTitleResName());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setText("xxx").build());
+        assertEquals(123, si.getRank());
+        assertEquals("xxx", si.getText());
+        assertEquals(0, si.getTextResId());
+        assertEquals(null, si.getTextResName());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setTextResId(1111).build());
+        assertEquals(123, si.getRank());
+        assertEquals(null, si.getText());
+        assertEquals(1111, si.getTextResId());
+        assertEquals(null, si.getTextResName());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setDisabledMessage("xxx").build());
+        assertEquals(123, si.getRank());
+        assertEquals("xxx", si.getDisabledMessage());
+        assertEquals(0, si.getDisabledMessageResourceId());
+        assertEquals(null, si.getDisabledMessageResName());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setDisabledMessageResId(11111).build());
+        assertEquals(123, si.getRank());
+        assertEquals(null, si.getDisabledMessage());
+        assertEquals(11111, si.getDisabledMessageResourceId());
+        assertEquals(null, si.getDisabledMessageResName());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setCategories(set()).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals(set(), si.getCategories());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setCategories(set("x")).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals(set("x"), si.getCategories());
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setIntent(makeIntent("action2", ShortcutActivity.class)).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals("action2", si.getIntent().getAction());
+        assertEquals(null, si.getIntent().getStringExtra("key"));
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setIntent(makeIntent("action3", ShortcutActivity.class, "key", "x")).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals("action3", si.getIntent().getAction());
+        assertEquals("x", si.getIntent().getStringExtra("key"));
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setRank(999).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals(999, si.getRank());
+
+
+        PersistableBundle pb2 = new PersistableBundle();
+        pb2.putInt("x", 99);
+
+        si = sorig.clone(/* flags=*/ 0);
+        si.copyNonNullFieldsFrom(new ShortcutInfo.Builder(getTestContext()).setId("id")
+                .setExtras(pb2).build());
+        assertEquals(11, si.getTextResId());
+        assertEquals(99, si.getExtras().getInt("x"));
+    }
+
+    public void testShortcutInfoSaveAndLoad() throws InterruptedException {
+        mRunningUsers.put(USER_10, true);
+
+        setCaller(CALLING_PACKAGE_1, USER_10);
+
+        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_32x32));
+
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(mClientContext)
+                .setId("id")
+                .setActivity(new ComponentName(mClientContext, ShortcutActivity2.class))
+                .setIcon(bmp32x32)
+                .setTitle("title")
+                .setText("text")
+                .setDisabledMessage("dismes")
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+        sorig.setTimestamp(mInjectedCurrentTimeMillis);
+
+        ShortcutInfo sorig2 = new ShortcutInfo.Builder(mClientContext)
+                .setId("id2")
+                .setTitle("x")
+                .setActivity(new ComponentName(mClientContext, ShortcutActivity2.class))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(456)
+                .build();
+        sorig2.setTimestamp(mInjectedCurrentTimeMillis);
+
+        mManager.addDynamicShortcuts(list(sorig, sorig2));
+
+        mInjectedCurrentTimeMillis += 1;
+        final long now = mInjectedCurrentTimeMillis;
+        mInjectedCurrentTimeMillis += 1;
+
+        dumpsysOnLogcat("before save");
+
+        // Save and load.
+        mService.saveDirtyInfo();
+        initService();
+        mService.handleUnlockUser(USER_10);
+
+        dumpUserFile(USER_10);
+        dumpsysOnLogcat("after load");
+
+        ShortcutInfo si;
+        si = mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "id", USER_10);
+
+        assertEquals(USER_10, si.getUserId());
+        assertEquals(HANDLE_USER_10, si.getUserHandle());
+        assertEquals(CALLING_PACKAGE_1, si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(ShortcutActivity2.class.getName(), si.getActivity().getClassName());
+        assertEquals(null, si.getIcon());
+        assertEquals("title", si.getTitle());
+        assertEquals("text", si.getText());
+        assertEquals("dismes", si.getDisabledMessage());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(0, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_HAS_ICON_FILE
+                | ShortcutInfo.FLAG_STRINGS_RESOLVED, si.getFlags());
+        assertNotNull(si.getBitmapPath()); // Something should be set.
+        assertEquals(0, si.getIconResourceId());
+        assertTrue(si.getLastChangedTimestamp() < now);
+
+        // Make sure ranks are saved too.  Because of the auto-adjusting, we need two shortcuts
+        // to test it.
+        si = mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "id2", USER_10);
+        assertEquals(1, si.getRank());
+
+        dumpUserFile(USER_10);
+    }
+
+    public void testShortcutInfoSaveAndLoad_resId() throws InterruptedException {
+        mRunningUsers.put(USER_10, true);
+
+        setCaller(CALLING_PACKAGE_1, USER_10);
+
+        final Icon res32x32 = Icon.createWithResource(mClientContext, R.drawable.black_32x32);
+
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(mClientContext)
+                .setId("id")
+                .setActivity(new ComponentName(mClientContext, ShortcutActivity2.class))
+                .setIcon(res32x32)
+                .setTitleResId(10)
+                .setTextResId(11)
+                .setDisabledMessageResId(12)
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+        sorig.setTimestamp(mInjectedCurrentTimeMillis);
+
+        ShortcutInfo sorig2 = new ShortcutInfo.Builder(mClientContext)
+                .setId("id2")
+                .setTitle("x")
+                .setActivity(new ComponentName(mClientContext, ShortcutActivity2.class))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(456)
+                .build();
+        sorig2.setTimestamp(mInjectedCurrentTimeMillis);
+
+        mManager.addDynamicShortcuts(list(sorig, sorig2));
+
+        mInjectedCurrentTimeMillis += 1;
+        final long now = mInjectedCurrentTimeMillis;
+        mInjectedCurrentTimeMillis += 1;
+
+        // Save and load.
+        mService.saveDirtyInfo();
+        initService();
+        mService.handleUnlockUser(USER_10);
+
+        ShortcutInfo si;
+        si = mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "id", USER_10);
+
+        assertEquals(USER_10, si.getUserId());
+        assertEquals(HANDLE_USER_10, si.getUserHandle());
+        assertEquals(CALLING_PACKAGE_1, si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(ShortcutActivity2.class.getName(), si.getActivity().getClassName());
+        assertEquals(null, si.getIcon());
+        assertEquals(10, si.getTitleResId());
+        assertEquals("r10", si.getTitleResName());
+        assertEquals(11, si.getTextResId());
+        assertEquals("r11", si.getTextResName());
+        assertEquals(12, si.getDisabledMessageResourceId());
+        assertEquals("r12", si.getDisabledMessageResName());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(0, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_DYNAMIC | ShortcutInfo.FLAG_HAS_ICON_RES
+                | ShortcutInfo.FLAG_STRINGS_RESOLVED, si.getFlags());
+        assertNull(si.getBitmapPath());
+        assertEquals(R.drawable.black_32x32, si.getIconResourceId());
+        assertTrue(si.getLastChangedTimestamp() < now);
+
+        // Make sure ranks are saved too.  Because of the auto-adjusting, we need two shortcuts
+        // to test it.
+        si = mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "id2", USER_10);
+        assertEquals(1, si.getRank());
+    }
+
+    public void testShortcutInfoSaveAndLoad_forBackup() {
+        setCaller(CALLING_PACKAGE_1, USER_0);
+
+        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_32x32));
+
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(mClientContext)
+                .setId("id")
+                .setActivity(new ComponentName(mClientContext, ShortcutActivity2.class))
+                .setIcon(bmp32x32)
+                .setTitle("title")
+                .setText("text")
+                .setDisabledMessage("dismes")
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+
+        ShortcutInfo sorig2 = new ShortcutInfo.Builder(mClientContext)
+                .setId("id2")
+                .setTitle("x")
+                .setActivity(new ComponentName(mClientContext, ShortcutActivity2.class))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(456)
+                .build();
+
+        mManager.addDynamicShortcuts(list(sorig, sorig2));
+
+        // Dynamic shortcuts won't be backed up, so we need to pin it.
+        setCaller(LAUNCHER_1, USER_0);
+        mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("id", "id2"), HANDLE_USER_0);
+
+        // Do backup & restore.
+        backupAndRestore();
+
+        mService.handleUnlockUser(USER_0); // Load user-0.
+
+        ShortcutInfo si;
+        si = mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "id", USER_0);
+
+        assertEquals(CALLING_PACKAGE_1, si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(ShortcutActivity2.class.getName(), si.getActivity().getClassName());
+        assertEquals(null, si.getIcon());
+        assertEquals("title", si.getTitle());
+        assertEquals("text", si.getText());
+        assertEquals("dismes", si.getDisabledMessage());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(0, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED | ShortcutInfo.FLAG_STRINGS_RESOLVED, si.getFlags());
+        assertNull(si.getBitmapPath()); // No icon.
+        assertEquals(0, si.getIconResourceId());
+
+        // Note when restored from backup, it's no longer dynamic, so shouldn't have a rank.
+        si = mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "id2", USER_0);
+        assertEquals(0, si.getRank());
+    }
+
+    public void testShortcutInfoSaveAndLoad_forBackup_resId() {
+        setCaller(CALLING_PACKAGE_1, USER_0);
+
+        final Icon res32x32 = Icon.createWithResource(mClientContext, R.drawable.black_32x32);
+
+        PersistableBundle pb = new PersistableBundle();
+        pb.putInt("k", 1);
+        ShortcutInfo sorig = new ShortcutInfo.Builder(mClientContext)
+                .setId("id")
+                .setActivity(new ComponentName(mClientContext, ShortcutActivity2.class))
+                .setIcon(res32x32)
+                .setTitleResId(10)
+                .setTextResId(11)
+                .setDisabledMessageResId(12)
+                .setCategories(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(123)
+                .setExtras(pb)
+                .build();
+
+        ShortcutInfo sorig2 = new ShortcutInfo.Builder(mClientContext)
+                .setId("id2")
+                .setTitle("x")
+                .setActivity(new ComponentName(mClientContext, ShortcutActivity2.class))
+                .setIntent(makeIntent("action", ShortcutActivity.class, "key", "val"))
+                .setRank(456)
+                .build();
+
+        mManager.addDynamicShortcuts(list(sorig, sorig2));
+
+        // Dynamic shortcuts won't be backed up, so we need to pin it.
+        setCaller(LAUNCHER_1, USER_0);
+        mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("id", "id2"), HANDLE_USER_0);
+
+        // Do backup & restore.
+        backupAndRestore();
+
+        mService.handleUnlockUser(USER_0); // Load user-0.
+
+        ShortcutInfo si;
+        si = mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "id", USER_0);
+
+        assertEquals(CALLING_PACKAGE_1, si.getPackage());
+        assertEquals("id", si.getId());
+        assertEquals(ShortcutActivity2.class.getName(), si.getActivity().getClassName());
+        assertEquals(null, si.getIcon());
+        assertEquals(10, si.getTitleResId());
+        assertEquals("r10", si.getTitleResName());
+        assertEquals(11, si.getTextResId());
+        assertEquals("r11", si.getTextResName());
+        assertEquals(12, si.getDisabledMessageResourceId());
+        assertEquals("r12", si.getDisabledMessageResName());
+        assertEquals(set(ShortcutInfo.SHORTCUT_CATEGORY_CONVERSATION, "xyz"), si.getCategories());
+        assertEquals("action", si.getIntent().getAction());
+        assertEquals("val", si.getIntent().getStringExtra("key"));
+        assertEquals(0, si.getRank());
+        assertEquals(1, si.getExtras().getInt("k"));
+
+        assertEquals(ShortcutInfo.FLAG_PINNED | ShortcutInfo.FLAG_STRINGS_RESOLVED, si.getFlags());
+        assertNull(si.getBitmapPath()); // No icon.
+        assertEquals(0, si.getIconResourceId());
+        assertEquals(null, si.getIconResName());
+
+        // Note when restored from backup, it's no longer dynamic, so shouldn't have a rank.
+        si = mService.getPackageShortcutForTest(CALLING_PACKAGE_1, "id2", USER_0);
+        assertEquals(0, si.getRank());
+    }
+
+    private void checkShortcutInfoSaveAndLoad_intents(Intent intent) {
+        assertTrue(mManager.setDynamicShortcuts(list(
+                makeShortcutWithIntent("s1", intent))));
+        initService();
+        mService.handleUnlockUser(USER_0);
+
+        assertWith(getCallerShortcuts())
+                .haveIds("s1")
+                .forShortcutWithId("s1", si -> {
+                    assertEquals(intent.getAction(), si.getIntent().getAction());
+                    assertEquals(intent.getData(), si.getIntent().getData());
+                    assertEquals(intent.getComponent(), si.getIntent().getComponent());
+                    assertBundlesEqual(intent.getExtras(), si.getIntent().getExtras());
+                });
+    }
+
+    private void checkShortcutInfoSaveAndLoad_intents(Intent... intents) {
+        assertTrue(mManager.setDynamicShortcuts(list(
+                makeShortcutWithIntents("s1", intents))));
+        initService();
+        mService.handleUnlockUser(USER_0);
+
+        assertWith(getCallerShortcuts())
+                .haveIds("s1")
+                .forShortcutWithId("s1", si -> {
+
+                    final Intent[] actual = si.getIntents();
+                    assertEquals(intents.length, actual.length);
+
+                    for (int i = 0; i < intents.length; i++) {
+                        assertEquals(intents[i].getAction(), actual[i].getAction());
+                        assertEquals(intents[i].getData(), actual[i].getData());
+                        assertEquals(intents[i].getComponent(), actual[i].getComponent());
+                        assertEquals(intents[i].getFlags(), actual[i].getFlags());
+                        assertBundlesEqual(intents[i].getExtras(), actual[i].getExtras());
+                    }
+                });
+    }
+
+    public void testShortcutInfoSaveAndLoad_intents() {
+        checkShortcutInfoSaveAndLoad_intents(new Intent(Intent.ACTION_VIEW));
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset throttling.
+
+        checkShortcutInfoSaveAndLoad_intents(new Intent(Intent.ACTION_MAIN));
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset throttling.
+
+        checkShortcutInfoSaveAndLoad_intents(new Intent(Intent.ACTION_VIEW,
+                Uri.parse("http://www.example.com/")));
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset throttling.
+
+        checkShortcutInfoSaveAndLoad_intents(new Intent(Intent.ACTION_MAIN,
+                Uri.parse("http://www.example.com/")));
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset throttling.
+
+        checkShortcutInfoSaveAndLoad_intents(new Intent(Intent.ACTION_VIEW)
+                .setComponent(new ComponentName("a", "b")));
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset throttling.
+
+        checkShortcutInfoSaveAndLoad_intents(new Intent(Intent.ACTION_MAIN)
+                .setComponent(new ComponentName("a", "b")));
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset throttling.
+
+        checkShortcutInfoSaveAndLoad_intents(new Intent(Intent.ACTION_VIEW)
+                .putExtras(makeBundle("a", "b")));
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset throttling.
+
+
+        checkShortcutInfoSaveAndLoad_intents(new Intent(Intent.ACTION_MAIN)
+                .putExtras(makeBundle("a", "b")));
+
+        mInjectedCurrentTimeMillis += INTERVAL; // reset throttling.
+
+        // Multi-intents
+        checkShortcutInfoSaveAndLoad_intents(
+                new Intent(Intent.ACTION_MAIN).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK),
+                new Intent(Intent.ACTION_VIEW).setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT)
+        );
+
+        checkShortcutInfoSaveAndLoad_intents(
+                new Intent(Intent.ACTION_MAIN).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
+                        .setComponent(new ComponentName("a", "b")),
+                new Intent(Intent.ACTION_VIEW)
+                        .setComponent(new ComponentName("a", "b"))
+                );
+
+        checkShortcutInfoSaveAndLoad_intents(
+                new Intent(Intent.ACTION_MAIN)
+                        .setComponent(new ComponentName("a", "b")),
+                new Intent(Intent.ACTION_VIEW).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
+                        .setComponent(new ComponentName("a", "b")),
+                new Intent("xyz").setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
+                        | Intent.FILL_IN_COMPONENT)
+                        .setComponent(new ComponentName("a", "b")).putExtras(
+                        makeBundle("xx", "yy"))
+                );
+    }
+
+    public void testThrottling() {
+        final ShortcutInfo si1 = makeShortcut("shortcut1");
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(1, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(0, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        // Reached the max
+
+        mInjectedCurrentTimeMillis++;
+        assertFalse(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(0, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        // Still throttled
+        mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
+        assertFalse(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(0, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        // Now it should work.
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1))); // fail
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL * 2, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(1, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL * 2, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(0, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL * 2, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis++;
+        assertFalse(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(0, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL * 2, mManager.getRateLimitResetTime());
+
+        // 4 hours later...
+        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL * 5, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(1, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL * 5, mManager.getRateLimitResetTime());
+
+        // Make sure getRemainingCallCount() itself gets reset without calling setDynamicShortcuts().
+        mInjectedCurrentTimeMillis = START_TIME + 8 * INTERVAL;
+        assertEquals(3, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL * 9, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL * 9, mManager.getRateLimitResetTime());
+    }
+
+    public void testThrottling_rewind() {
+        final ShortcutInfo si1 = makeShortcut("shortcut1");
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        mInjectedCurrentTimeMillis = 12345; // Clock reset!
+
+        // Since the clock looks invalid, the counter shouldn't have reset.
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        // Forward again.  Still haven't reset yet.
+        mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
+        assertEquals(2, mManager.getRemainingCallCount());
+        assertEquals(START_TIME + INTERVAL, mManager.getRateLimitResetTime());
+
+        // Now rewind -- this will reset the counters.
+        mInjectedCurrentTimeMillis = START_TIME - 100000;
+        assertEquals(3, mManager.getRemainingCallCount());
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(2, mManager.getRemainingCallCount());
+
+        // Forward again, should be reset now.
+        mInjectedCurrentTimeMillis += INTERVAL;
+        assertEquals(3, mManager.getRemainingCallCount());
+    }
+
+    public void testThrottling_perPackage() {
+        final ShortcutInfo si1 = makeShortcut("shortcut1");
+
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(2, mManager.getRemainingCallCount());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(1, mManager.getRemainingCallCount());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(0, mManager.getRemainingCallCount());
+
+        // Reached the max
+
+        mInjectedCurrentTimeMillis++;
+        assertFalse(mManager.setDynamicShortcuts(list(si1)));
+
+        // Try from a different caller.
+        mInjectedClientPackage = CALLING_PACKAGE_2;
+        mInjectedCallingUid = CALLING_UID_2;
+
+        // Need to create a new one wit the updated package name.
+        final ShortcutInfo si2 = makeShortcut("shortcut1");
+
+        assertEquals(3, mManager.getRemainingCallCount());
+
+        assertTrue(mManager.setDynamicShortcuts(list(si2)));
+        assertEquals(2, mManager.getRemainingCallCount());
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si2)));
+        assertEquals(1, mManager.getRemainingCallCount());
+
+        // Back to the original caller, still throttled.
+        mInjectedClientPackage = CALLING_PACKAGE_1;
+        mInjectedCallingUid = CALLING_UID_1;
+
+        mInjectedCurrentTimeMillis = START_TIME + INTERVAL - 1;
+        assertEquals(0, mManager.getRemainingCallCount());
+        assertFalse(mManager.setDynamicShortcuts(list(si1)));
+        assertEquals(0, mManager.getRemainingCallCount());
+
+        // Now it should work.
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+
+        mInjectedCurrentTimeMillis++;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+
+        mInjectedCurrentTimeMillis++;
+        assertFalse(mManager.setDynamicShortcuts(list(si1)));
+
+        mInjectedCurrentTimeMillis = START_TIME + 4 * INTERVAL;
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertTrue(mManager.setDynamicShortcuts(list(si1)));
+        assertFalse(mManager.setDynamicShortcuts(list(si1)));
+
+        mInjectedClientPackage = CALLING_PACKAGE_2;
+        mInjectedCallingUid = CALLING_UID_2;
+
+        assertEquals(3, mManager.getRemainingCallCount());
+
+        assertTrue(mManager.setDynamicShortcuts(list(si2)));
+        assertTrue(mManager.setDynamicShortcuts(list(si2)));
+        assertTrue(mManager.setDynamicShortcuts(list(si2)));
+        assertFalse(mManager.setDynamicShortcuts(list(si2)));
+    }
+
+    public void testThrottling_localeChanges() {
+        prepareCrossProfileDataSet();
+
+        dumpsysOnLogcat("Before save & load");
+
+        mService.saveDirtyInfo();
+        initService();
+
+        mInjectedLocale = Locale.CHINA;
+        mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
+
+        // Note at this point only user-0 is loaded, and the counters are reset for this user,
+        // but it will work for other users too because we check the locale change at any
+        // API entry point.
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+
+        // Make sure even if we receive ACTION_LOCALE_CHANGED, if the locale hasn't actually
+        // changed, we don't reset throttling.
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            mManager.updateShortcuts(list());
+            assertEquals(2, mManager.getRemainingCallCount());
+        });
+
+        mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(2, mManager.getRemainingCallCount()); // Still 2.
+        });
+
+        mService.saveDirtyInfo();
+        initService();
+
+        // The locale should be persisted, so it still shouldn't reset throttling.
+        mService.mReceiver.onReceive(mServiceContext, new Intent(Intent.ACTION_LOCALE_CHANGED));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(2, mManager.getRemainingCallCount()); // Still 2.
+        });
+    }
+
+    public void testThrottling_foreground() throws Exception {
+        prepareCrossProfileDataSet();
+
+        dumpsysOnLogcat("Before save & load");
+
+        mService.saveDirtyInfo();
+        initService();
+
+        // We need to update the current time from time to time, since some of the internal checks
+        // rely on the time being correctly incremented.
+        mInjectedCurrentTimeMillis++;
+
+        // First, all packages have less than 3 (== initial value) remaining calls.
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+
+        mInjectedCurrentTimeMillis++;
+
+        // State changed, but not foreground, so no resetting.
+        mService.mUidObserver.onUidStateChanged(
+                CALLING_UID_1, ActivityManager.PROCESS_STATE_TOP_SLEEPING);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+
+        mInjectedCurrentTimeMillis++;
+
+        // State changed, package1 foreground, reset.
+        mService.mUidObserver.onUidStateChanged(
+                CALLING_UID_1, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        mService.mUidObserver.onUidStateChanged(
+                CALLING_UID_1, ActivityManager.PROCESS_STATE_TOP_SLEEPING);
+
+        mInjectedCurrentTimeMillis++;
+
+        // Different app comes to foreground briefly, and goes back to background.
+        // Now, make sure package 2's counter is reset, even in this case.
+        mService.mUidObserver.onUidStateChanged(
+                CALLING_UID_2, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE);
+        mService.mUidObserver.onUidStateChanged(
+                CALLING_UID_2, ActivityManager.PROCESS_STATE_TOP_SLEEPING);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+
+        mInjectedCurrentTimeMillis++;
+
+        // Do the same thing one more time.  This would catch the bug with mixuing up
+        // the current time and the elapsed time.
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            mManager.updateShortcuts(list(makeShortcut("s")));
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+
+        mService.mUidObserver.onUidStateChanged(
+                CALLING_UID_2, ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE);
+        mService.mUidObserver.onUidStateChanged(
+                CALLING_UID_2, ActivityManager.PROCESS_STATE_TOP_SLEEPING);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+
+        mInjectedCurrentTimeMillis++;
+
+        // Package 1 on user-10 comes to foreground.
+        // Now, also try calling some APIs and make sure foreground apps don't get throttled.
+        mService.mUidObserver.onUidStateChanged(
+                UserHandle.getUid(USER_10, CALLING_UID_1),
+                ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE);
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+            assertFalse(mManager.isRateLimitingActive());
+
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+
+            assertEquals(2, mManager.getRemainingCallCount());
+            assertFalse(mManager.isRateLimitingActive());
+
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+
+            assertEquals(1, mManager.getRemainingCallCount());
+            assertFalse(mManager.isRateLimitingActive());
+
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+
+            assertEquals(0, mManager.getRemainingCallCount());
+            assertTrue(mManager.isRateLimitingActive());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+
+            assertEquals(0, mManager.getRemainingCallCount());
+            assertTrue(mManager.isRateLimitingActive());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+
+            assertEquals(0, mManager.getRemainingCallCount());
+            assertTrue(mManager.isRateLimitingActive());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+
+            assertEquals(0, mManager.getRemainingCallCount());
+            assertTrue(mManager.isRateLimitingActive());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+
+            assertEquals(0, mManager.getRemainingCallCount());
+            assertTrue(mManager.isRateLimitingActive());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+            mManager.setDynamicShortcuts(list(makeShortcut("s")));
+
+            assertEquals(3, mManager.getRemainingCallCount()); // Still 3!
+            assertFalse(mManager.isRateLimitingActive());
+        });
+    }
+
+
+    public void testThrottling_resetByInternalCall() throws Exception {
+        prepareCrossProfileDataSet();
+
+        dumpsysOnLogcat("Before save & load");
+
+        mService.saveDirtyInfo();
+        initService();
+
+        // First, all packages have less than 3 (== initial value) remaining calls.
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+
+        // Simulate a call from sys UI.
+        mCallerPermissions.add(permission.RESET_SHORTCUT_MANAGER_THROTTLING);
+        mManager.onApplicationActive(CALLING_PACKAGE_1, USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+
+        mManager.onApplicationActive(CALLING_PACKAGE_3, USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+
+        mManager.onApplicationActive(CALLING_PACKAGE_1, USER_10);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_3, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_4, USER_0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_P0, () -> {
+            MoreAsserts.assertNotEqual(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+    }
+
+    public void testReportShortcutUsed() {
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            reset(mMockUsageStatsManagerInternal);
+
+            // Report with an nonexistent shortcut.
+            mManager.reportShortcutUsed("s1");
+            verify(mMockUsageStatsManagerInternal, times(0)).reportShortcutUsage(
+                    anyString(), anyString(), anyInt());
+
+            // Publish s2, but s1 still doesn't exist.
+            mManager.setDynamicShortcuts(list(makeShortcut("s2")));
+            mManager.reportShortcutUsed("s1");
+            verify(mMockUsageStatsManagerInternal, times(0)).reportShortcutUsage(
+                    anyString(), anyString(), anyInt());
+
+            mManager.reportShortcutUsed("s2");
+            verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
+                    eq(CALLING_PACKAGE_1), eq("s2"), eq(USER_10));
+
+        });
+        runWithCaller(CALLING_PACKAGE_2, USER_10, () -> {
+            // Try with a different package.
+            reset(mMockUsageStatsManagerInternal);
+
+            // Report with an nonexistent shortcut.
+            mManager.reportShortcutUsed("s2");
+            verify(mMockUsageStatsManagerInternal, times(0)).reportShortcutUsage(
+                    anyString(), anyString(), anyInt());
+
+            // Publish s2, but s1 still doesn't exist.
+            mManager.setDynamicShortcuts(list(makeShortcut("s3")));
+            mManager.reportShortcutUsed("s2");
+            verify(mMockUsageStatsManagerInternal, times(0)).reportShortcutUsage(
+                    anyString(), anyString(), anyInt());
+
+            mManager.reportShortcutUsed("s3");
+            verify(mMockUsageStatsManagerInternal, times(1)).reportShortcutUsage(
+                    eq(CALLING_PACKAGE_2), eq("s3"), eq(USER_10));
+
+        });
+    }
+
+    // Test for a ShortcutInfo method.
+    public void testGetResourcePackageName() {
+        assertEquals(null, ShortcutInfo.getResourcePackageName(""));
+        assertEquals(null, ShortcutInfo.getResourcePackageName("abc"));
+        assertEquals("p", ShortcutInfo.getResourcePackageName("p:"));
+        assertEquals("p", ShortcutInfo.getResourcePackageName("p:xx"));
+        assertEquals("pac", ShortcutInfo.getResourcePackageName("pac:"));
+    }
+
+    // Test for a ShortcutInfo method.
+    public void testGetResourceTypeName() {
+        assertEquals(null, ShortcutInfo.getResourceTypeName(""));
+        assertEquals(null, ShortcutInfo.getResourceTypeName(":"));
+        assertEquals(null, ShortcutInfo.getResourceTypeName("/"));
+        assertEquals(null, ShortcutInfo.getResourceTypeName("/:"));
+        assertEquals("a", ShortcutInfo.getResourceTypeName(":a/"));
+        assertEquals("type", ShortcutInfo.getResourceTypeName("xxx:type/yyy"));
+    }
+
+    // Test for a ShortcutInfo method.
+    public void testGetResourceTypeAndEntryName() {
+        assertEquals(null, ShortcutInfo.getResourceTypeAndEntryName(""));
+        assertEquals(null, ShortcutInfo.getResourceTypeAndEntryName("abc"));
+        assertEquals("", ShortcutInfo.getResourceTypeAndEntryName("p:"));
+        assertEquals("x", ShortcutInfo.getResourceTypeAndEntryName(":x"));
+        assertEquals("x", ShortcutInfo.getResourceTypeAndEntryName("p:x"));
+        assertEquals("xyz", ShortcutInfo.getResourceTypeAndEntryName("pac:xyz"));
+    }
+
+    // Test for a ShortcutInfo method.
+    public void testGetResourceEntryName() {
+        assertEquals(null, ShortcutInfo.getResourceEntryName(""));
+        assertEquals(null, ShortcutInfo.getResourceEntryName("ab:"));
+        assertEquals("", ShortcutInfo.getResourceEntryName("/"));
+        assertEquals("abc", ShortcutInfo.getResourceEntryName("/abc"));
+        assertEquals("abc", ShortcutInfo.getResourceEntryName("xyz/abc"));
+    }
+
+    // Test for a ShortcutInfo method.
+    public void testLookUpResourceName_systemResources() {
+        // For android system resources, lookUpResourceName will simply return the value as a
+        // string, regardless of "withType".
+        final Resources res = getTestContext().getResources();
+
+        assertEquals("" + android.R.string.cancel, ShortcutInfo.lookUpResourceName(res,
+                android.R.string.cancel, true, getTestContext().getPackageName()));
+        assertEquals("" + android.R.drawable.alert_dark_frame, ShortcutInfo.lookUpResourceName(res,
+                android.R.drawable.alert_dark_frame, true, getTestContext().getPackageName()));
+        assertEquals("" + android.R.string.cancel, ShortcutInfo.lookUpResourceName(res,
+                android.R.string.cancel, false, getTestContext().getPackageName()));
+    }
+
+    public void testLookUpResourceName_appResources() {
+        final Resources res = getTestContext().getResources();
+
+        assertEquals("shortcut_text1", ShortcutInfo.lookUpResourceName(res,
+                R.string.shortcut_text1, false, getTestContext().getPackageName()));
+        assertEquals("string/shortcut_text1", ShortcutInfo.lookUpResourceName(res,
+                R.string.shortcut_text1, true, getTestContext().getPackageName()));
+
+        assertEquals("black_16x64", ShortcutInfo.lookUpResourceName(res,
+                R.drawable.black_16x64, false, getTestContext().getPackageName()));
+        assertEquals("drawable/black_16x64", ShortcutInfo.lookUpResourceName(res,
+                R.drawable.black_16x64, true, getTestContext().getPackageName()));
+    }
+
+    // Test for a ShortcutInfo method.
+    public void testLookUpResourceId_systemResources() {
+        final Resources res = getTestContext().getResources();
+
+        assertEquals(android.R.string.cancel, ShortcutInfo.lookUpResourceId(res,
+                "" + android.R.string.cancel, null,
+                getTestContext().getPackageName()));
+        assertEquals(android.R.drawable.alert_dark_frame, ShortcutInfo.lookUpResourceId(res,
+                "" + android.R.drawable.alert_dark_frame, null,
+                getTestContext().getPackageName()));
+    }
+
+    // Test for a ShortcutInfo method.
+    public void testLookUpResourceId_appResources() {
+        final Resources res = getTestContext().getResources();
+
+        assertEquals(R.string.shortcut_text1,
+                ShortcutInfo.lookUpResourceId(res, "shortcut_text1", "string",
+                        getTestContext().getPackageName()));
+
+        assertEquals(R.string.shortcut_text1,
+                ShortcutInfo.lookUpResourceId(res, "string/shortcut_text1", null,
+                        getTestContext().getPackageName()));
+
+        assertEquals(R.drawable.black_16x64,
+                ShortcutInfo.lookUpResourceId(res, "black_16x64", "drawable",
+                        getTestContext().getPackageName()));
+
+        assertEquals(R.drawable.black_16x64,
+                ShortcutInfo.lookUpResourceId(res, "drawable/black_16x64", null,
+                        getTestContext().getPackageName()));
+    }
+
+    public void testDumpCheckin() throws IOException {
+        prepareCrossProfileDataSet();
+
+        // prepareCrossProfileDataSet() doesn't set any icons, so do set here.
+        final Icon res32x32 = Icon.createWithResource(getTestContext(), R.drawable.black_32x32);
+        final Icon res64x64 = Icon.createWithResource(getTestContext(), R.drawable.black_64x64);
+        final Icon bmp32x32 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_32x32));
+        final Icon bmp64x64 = Icon.createWithBitmap(BitmapFactory.decodeResource(
+                getTestContext().getResources(), R.drawable.black_64x64));
+
+        runWithCaller(CALLING_PACKAGE_2, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcutWithIcon("res32x32", res32x32),
+                    makeShortcutWithIcon("res64x64", res64x64),
+                    makeShortcutWithIcon("bmp32x32", bmp32x32),
+                    makeShortcutWithIcon("bmp64x64", bmp64x64))));
+        });
+        // We can't predict the compressed bitmap sizes, so get the real sizes here.
+        final long bitmapTotal =
+                new File(getPackageShortcut(CALLING_PACKAGE_2, "bmp32x32", USER_0)
+                        .getBitmapPath()).length() +
+                new File(getPackageShortcut(CALLING_PACKAGE_2, "bmp64x64", USER_0)
+                        .getBitmapPath()).length();
+
+        // Read the expected output and inject the bitmap size.
+        final String expected = readTestAsset("shortcut/dumpsys_expected.txt")
+                .replace("***BITMAP_SIZE***", String.valueOf(bitmapTotal));
+
+        assertEquals(expected, dumpCheckin());
+    }
+
+    public void testDumpsysNoPermission() {
+        assertExpectException(SecurityException.class, "android.permission.DUMP",
+                () -> mService.dump(null, new PrintWriter(new StringWriter()), null));
+
+        // System can call it without the permission.
+        runWithSystemUid(() -> {
+            mService.dump(null, new PrintWriter(new StringWriter()), null);
+        });
+    }
+
+    /**
+     * Make sure the legacy file format that only supported a single intent per shortcut
+     * can still be read.
+     */
+    public void testLoadLegacySavedFile() throws Exception {
+        final File path = mService.getUserFile(USER_0);
+        path.getParentFile().mkdirs();
+        try (Writer w = new FileWriter(path)) {
+            w.write(readTestAsset("shortcut/shortcut_legacy_file.xml"));
+        };
+        initService();
+        mService.handleUnlockUser(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("manifest-shortcut-storage")
+                    .forShortcutWithId("manifest-shortcut-storage", si -> {
+                        assertEquals("android.settings.INTERNAL_STORAGE_SETTINGS",
+                                si.getIntent().getAction());
+                        assertEquals(12345, si.getIntent().getIntExtra("key", 0));
+                    });
+        });
+    }
+
+    public void testIsUserUnlocked() {
+        mRunningUsers.clear();
+        mUnlockedUsers.clear();
+
+        assertFalse(mService.isUserUnlockedL(USER_0));
+        assertFalse(mService.isUserUnlockedL(USER_10));
+
+        // Start user 0, still locked.
+        mRunningUsers.put(USER_0, true);
+        assertFalse(mService.isUserUnlockedL(USER_0));
+        assertFalse(mService.isUserUnlockedL(USER_10));
+
+        // Unlock user.
+        mUnlockedUsers.put(USER_0, true);
+        assertTrue(mService.isUserUnlockedL(USER_0));
+        assertFalse(mService.isUserUnlockedL(USER_10));
+
+        // Clear again.
+        mRunningUsers.clear();
+        mUnlockedUsers.clear();
+
+        // Directly call the lifecycle event.  Now also locked.
+        mService.handleUnlockUser(USER_0);
+        assertTrue(mService.isUserUnlockedL(USER_0));
+        assertFalse(mService.isUserUnlockedL(USER_10));
+
+        // Directly call the stop lifecycle event.  Goes back to the initial state.
+        mService.handleCleanupUser(USER_0);
+        assertFalse(mService.isUserUnlockedL(USER_0));
+        assertFalse(mService.isUserUnlockedL(USER_10));
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest3.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest3.java
new file mode 100644
index 0000000..eb4db7a
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest3.java
@@ -0,0 +1,505 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+
+import android.content.ComponentName;
+import android.content.pm.ShortcutInfo;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.frameworks.servicestests.R;
+import com.android.server.pm.ShortcutService.ConfigConstants;
+
+/**
+ * Tests related to shortcut rank auto-adjustment.
+ */
+@SmallTest
+public class ShortcutManagerTest3 extends BaseShortcutManagerTest {
+
+    private static final String CALLING_PACKAGE = CALLING_PACKAGE_1;
+
+    private static final ComponentName A1 = new ComponentName(CALLING_PACKAGE,
+            ShortcutActivity.class.getName());
+
+    private static final ComponentName A2 = new ComponentName(CALLING_PACKAGE,
+            ShortcutActivity2.class.getName());
+
+    private static final ComponentName A3 = new ComponentName(CALLING_PACKAGE,
+            ShortcutActivity3.class.getName());
+
+    private ShortcutInfo shortcut(String id, ComponentName activity, int rank) {
+        return makeShortcutWithActivityAndRank(id, activity, rank);
+    }
+
+    private ShortcutInfo shortcut(String id, ComponentName activity) {
+        return makeShortcutWithActivityAndRank(id, activity, ShortcutInfo.RANK_NOT_SET);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // We don't need throttling during this test class, and also relax the max cap.
+        mService.updateConfigurationLocked(
+                ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=99999999,"
+                + ConfigConstants.KEY_MAX_SHORTCUTS + "=99999999"
+        );
+
+        setCaller(CALLING_PACKAGE, USER_0);
+    }
+
+    private void publishManifestShortcuts(ComponentName activity, int resId) {
+        addManifestShortcutResource(activity, resId);
+        updatePackageVersion(CALLING_PACKAGE, 1);
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE, USER_0));
+    }
+
+    public void testSetDynamicShortcuts_noManifestShortcuts() {
+        mManager.setDynamicShortcuts(list(
+                shortcut("s1", A1)
+        ));
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s1");
+
+        assertTrue(mManager.setDynamicShortcuts(list(
+                shortcut("s5", A1),
+                shortcut("s4", A1),
+                shortcut("s3", A1)
+        )));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s4", "s3");
+
+        // RANK_NOT_SET is always the last.
+        assertTrue(mManager.setDynamicShortcuts(list(
+                shortcut("s5", A1),
+                shortcut("s4", A1, 5),
+                shortcut("s3", A1, 3),
+                shortcut("s2", A1)
+        )));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s3", "s4", "s5", "s2");
+
+        // Same rank, preserve the argument order.
+        assertTrue(mManager.setDynamicShortcuts(list(
+                shortcut("s5", A1, 5),
+                shortcut("s4", A1, 0),
+                shortcut("s3", A1, 5)
+        )));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s4", "s5", "s3");
+
+        // Multiple activities.
+        assertTrue(mManager.setDynamicShortcuts(list(
+                shortcut("s5", A1),
+                shortcut("s4", A2),
+                shortcut("s3", A3)
+        )));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5");
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("s4");
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A3)
+                .haveRanksInOrder("s3");
+
+        assertTrue(mManager.setDynamicShortcuts(list(
+                shortcut("s5", A1, 5),
+                shortcut("s4", A1),
+                shortcut("s3", A1, 5),
+                shortcut("x5", A2, 5),
+                shortcut("x4", A2),
+                shortcut("x3", A2, 1)
+        )));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s4");
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x5", "x4");
+
+        // Clear.  Make sure it wouldn't lead to invalid internals state.
+        // (ShortcutService.verifyStates() will do so internally.)
+        assertTrue(mManager.setDynamicShortcuts(list()));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1).isEmpty();
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2).isEmpty();
+    }
+
+    private void runTestWithManifestShortcuts(Runnable r) {
+        publishManifestShortcuts(A1, R.xml.shortcut_5_alt);
+        publishManifestShortcuts(A2, R.xml.shortcut_1);
+
+        assertWith(getCallerShortcuts()).selectManifest().selectByActivity(A1)
+                .haveRanksInOrder("ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
+
+        assertWith(getCallerShortcuts()).selectManifest().selectByActivity(A2)
+                .haveRanksInOrder("ms1");
+
+        // Existence of manifest shortcuts shouldn't affect dynamic shortcut ranks,
+        // so running another test here should pass.
+        r.run();
+
+        // And dynamic shortcut tests shouldn't affect manifest shortcuts, so repeat the
+        // same check.
+        assertWith(getCallerShortcuts()).selectManifest().selectByActivity(A1)
+                .haveRanksInOrder("ms1_alt", "ms2_alt", "ms3_alt", "ms4_alt", "ms5_alt");
+
+        assertWith(getCallerShortcuts()).selectManifest().selectByActivity(A2)
+                .haveRanksInOrder("ms1");
+    }
+
+    public void testSetDynamicShortcuts_withManifestShortcuts() {
+        runTestWithManifestShortcuts(() -> testSetDynamicShortcuts_noManifestShortcuts());
+    }
+
+    public void testAddDynamicShortcuts_noManifestShortcuts() {
+        mManager.addDynamicShortcuts(list(
+                shortcut("s1", A1)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s1");
+
+        //------------------------------------------------------
+        long lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.addDynamicShortcuts(list(
+                shortcut("s5", A1, 0),
+                shortcut("s4", A1),
+                shortcut("s2", A1, 3),
+                shortcut("x1", A2),
+                shortcut("x3", A2, 2),
+                shortcut("x2", A2, 2),
+                shortcut("s3", A1, 0)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s1", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s5", "s3", "s1", "s2", "s4", "x3", "x2", "x1");
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.addDynamicShortcuts(list(
+                shortcut("s1", A1, 1)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s1", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s1", "s3");
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.addDynamicShortcuts(list(
+                shortcut("s1", A1, 1),
+
+                // This is add, not update, so the following means s5 will have NO_RANK,
+                // which puts it at the end.
+                shortcut("s5", A1),
+                shortcut("s3", A1, 0),
+
+                // s10 also has NO_RANK, so it'll be put at the end, even after "s5" as we preserve
+                // the argument order.
+                shortcut("s10", A1),
+
+                // Note we're changing the activity for x2.
+                shortcut("x2", A1, 0),
+                shortcut("x10", A2)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s3", "x2", "s1", "s2", "s4", "s5", "s10");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x1", "x10");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s3", "x2", "s1", "s5", "s10", "x1", "x10");
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        // Change the activities again.
+        mManager.addDynamicShortcuts(list(
+                shortcut("s1", A2),
+                shortcut("s2", A2, 999)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s3", "x2", "s4", "s5", "s10");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x1", "x10", "s2", "s1");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s1", "s2", "s4", "s5", "s10");
+    }
+
+    public void testAddDynamicShortcuts_withManifestShortcuts() {
+        runTestWithManifestShortcuts(() -> testAddDynamicShortcuts_noManifestShortcuts());
+    }
+
+    public void testUpdateShortcuts_noManifestShortcuts() {
+        mManager.addDynamicShortcuts(list(
+                shortcut("s5", A1, 0),
+                shortcut("s4", A1),
+                shortcut("s2", A1, 3),
+                shortcut("x1", A2),
+                shortcut("x3", A2, 2),
+                shortcut("x2", A2, 2),
+                shortcut("s3", A1, 0)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        //------------------------------------------------------
+        long lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.updateShortcuts(list());
+        // Same order.
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .isEmpty();
+
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE, list("s2", "s4", "x2"), HANDLE_USER_0);
+        });
+        // Still same order.
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.updateShortcuts(list(
+                shortcut("s4", A1, 1),
+
+                // Rank not changing, should keep the same positions.
+                // c.f. in case of addDynamicShortcuts, this means "put them at the end".
+                shortcut("s3", A1),
+                shortcut("x2", A2)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s4", "s3", "s2");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s4", "s3", "s2", "x2");
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.updateShortcuts(list(
+                shortcut("s4", A1, 0),
+
+                // Change the activity without specifying a rank -> keep the same rank.
+                shortcut("s5", A2),
+
+                // Change the activity without specifying a rank -> assign a new rank.
+                shortcut("x2", A1, 2),
+
+                // "xx" doesn't exist, so it'll be ignored.
+                shortcut("xx", A1, 0)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s4", "x2", "s3", "s2");
+
+        // Interesting case: both x3 and s5 originally had rank=0, and in this case s5 has moved
+        // to A2 without changing the rank.  So they're tie for the new rank, as well as
+        // the "rank changed" bit.  Also in this case, "s5" won't have an implicit order, since
+        // its rank isn't changing.  So we sort them by ID, thus s5 comes before x3.
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("s5", "x3", "x1");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s4", "x2", "s5", "x3");
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.updateShortcuts(list(
+                shortcut("s3", A3)));
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s4", "x2", "s2");
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("s5", "x3", "x1");
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A3)
+                .haveRanksInOrder("s3");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s3", "s2");
+    }
+
+    public void testUpdateShortcuts_withManifestShortcuts() {
+        runTestWithManifestShortcuts(() -> testUpdateShortcuts_noManifestShortcuts());
+    }
+
+    public void testDeleteDynamicShortcuts_noManifestShortcuts() {
+        mManager.addDynamicShortcuts(list(
+                shortcut("s5", A1, 0),
+                shortcut("s4", A1),
+                shortcut("s2", A1, 3),
+                shortcut("x1", A2),
+                shortcut("x3", A2, 2),
+                shortcut("x2", A2, 2),
+                shortcut("s3", A1, 0)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        //------------------------------------------------------
+        long lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.removeDynamicShortcuts(list());
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .isEmpty();
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(
+                    CALLING_PACKAGE, list("s2", "s4", "x1", "x2"), HANDLE_USER_0);
+        });
+        // Still same order.
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.removeDynamicShortcuts(list("s3", "x1", "xxxx"));
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s2", "s4");
+    }
+
+    public void testDeleteDynamicShortcuts_withManifestShortcuts() {
+        runTestWithManifestShortcuts(() -> testDeleteDynamicShortcuts_noManifestShortcuts());
+    }
+
+    public void testDisableShortcuts_noManifestShortcuts() {
+        mManager.addDynamicShortcuts(list(
+                shortcut("s5", A1, 0),
+                shortcut("s4", A1),
+                shortcut("s2", A1, 3),
+                shortcut("x1", A2),
+                shortcut("x3", A2, 2),
+                shortcut("x2", A2, 2),
+                shortcut("s3", A1, 0)
+        ));
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        //------------------------------------------------------
+        long lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.disableShortcuts(list());
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s3", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2", "x1");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .isEmpty();
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.disableShortcuts(list("s3", "x1", "xxxx"));
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s2", "s4");
+
+        runWithCaller(LAUNCHER_1, USER_0, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE, list("s2", "s4", "x2"), HANDLE_USER_0);
+        });
+        // Still same order.
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s2", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x3", "x2");
+
+        //------------------------------------------------------
+        lastApiTime = ++mInjectedCurrentTimeMillis;
+
+        mManager.disableShortcuts(list("s2", "x3"));
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A1)
+                .haveRanksInOrder("s5", "s4");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByActivity(A2)
+                .haveRanksInOrder("x2");
+
+        assertWith(getCallerShortcuts()).selectDynamic().selectByChangedSince(lastApiTime)
+                .haveIds("s4", "x2");
+    }
+
+    public void testDisableShortcuts_withManifestShortcuts() {
+        runTestWithManifestShortcuts(() -> testDisableShortcuts_noManifestShortcuts());
+    }
+
+}
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest4.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest4.java
new file mode 100644
index 0000000..583c3d4
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest4.java
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertBundlesEqual;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makeBundle;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.makePersistableBundle;
+
+import android.content.Intent;
+import android.os.Bundle;
+import android.os.PersistableBundle;
+import android.test.suitebuilder.annotation.SmallTest;
+
+@SmallTest
+public class ShortcutManagerTest4 extends BaseShortcutManagerTest {
+
+    private static Bundle sIntentExtras = makeBundle(
+            "key{\u0000}", "value{\u0000}",
+            "key{\u0001}", "value{\u0001}",
+            "key{\u001f}", "value{\u001f}",
+            "key{\u007f}", "value{\u007f}",
+
+            "key{\ud800\udc00}", "value{\ud800\udc00}",
+            "key{\ud801\udc01}", "value{\ud801\udc01}",
+            "key{\udbff\udfff}", "value{\udbff\udfff}",
+
+            "key{\ud801}x", 1, // broken surrogate pair
+            "key{\uDC01}\"x", 2, // broken surrogate pair
+
+            "x1", "value{\ud801}x", // broken surrogate pair
+            "x2", "value{\uDC01}\"x" // broken surrogate pair
+    );
+
+    // Same as above, except broken surrogate pairs are replaced with '?'s.
+    private static Bundle sIntentExtrasDecoded = makeBundle(
+            "key{\u0000}", "value{\u0000}",
+            "key{\u0001}", "value{\u0001}",
+            "key{\u001f}", "value{\u001f}",
+            "key{\u007f}", "value{\u007f}",
+
+            "key{\ud800\udc00}", "value{\ud800\udc00}",
+            "key{\ud801\udc01}", "value{\ud801\udc01}",
+            "key{\udbff\udfff}", "value{\udbff\udfff}",
+
+            "key{?}x", 1,
+            "key{?}\"x", 2,
+
+            "x1", "value{?}x",
+            "x2", "value{?}\"x"
+    );
+
+    private static PersistableBundle sShortcutExtras = makePersistableBundle(
+            "key{\u0000}", "value{\u0000}",
+            "key{\u0001}", "value{\u0001}",
+            "key{\u001f}", "value{\u001f}",
+            "key{\u007f}", "value{\u007f}",
+
+            "key{\ud800\udc00}", "value{\ud800\udc00}",
+            "key{\ud801\udc01}", "value{\ud801\udc01}",
+            "key{\udbff\udfff}", "value{\udbff\udfff}",
+
+            "key{\ud801}", 1, // broken surrogate pair
+            "key{\uDC01}", 2, // broken surrogate pair
+
+            "x1", "value{\ud801}", // broken surrogate pair
+            "x2", "value{\uDC01}" // broken surrogate pair
+    );
+
+    // Same as above, except broken surrogate pairs are replaced with '?'s.
+    private static PersistableBundle sShortcutExtrasDecoded = makePersistableBundle(
+            "key{\u0000}", "value{\u0000}",
+            "key{\u0001}", "value{\u0001}",
+            "key{\u001f}", "value{\u001f}",
+            "key{\u007f}", "value{\u007f}",
+
+            "key{\ud800\udc00}", "value{\ud800\udc00}",
+            "key{\ud801\udc01}", "value{\ud801\udc01}",
+            "key{\udbff\udfff}", "value{\udbff\udfff}",
+
+            "key{?}", 1,
+            "key{?}", 2,
+
+            "x1", "value{?}",
+            "x2", "value{?}"
+    );
+
+    public void testPersistingWeirdCharacters() {
+        final Intent intent = new Intent(Intent.ACTION_MAIN)
+                .putExtras(sIntentExtras);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.setDynamicShortcuts(list(
+                    makeShortcutWithExtras("s1", intent, sShortcutExtras),
+                    makeShortcut("s{\u0000}{\u0001}{\uD800\uDC00}x[\uD801][\uDC01]")
+            )));
+        });
+
+        // Make sure save & load works fine. (i.e. shouldn't crash even with invalid characters.)
+        initService();
+        mService.handleUnlockUser(USER_0);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("s1", "s{\u0000}{\u0001}{\uD800\uDC00}x[?][?]")
+                    .forShortcutWithId("s1", si -> {
+                        assertBundlesEqual(si.getIntent().getExtras(), sIntentExtrasDecoded);
+                        assertBundlesEqual(si.getExtras(), sShortcutExtrasDecoded);
+                    });
+        });
+    }
+}
\ No newline at end of file
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest5.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest5.java
new file mode 100644
index 0000000..29c98dc
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest5.java
@@ -0,0 +1,202 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.set;
+
+import android.app.Activity;
+import android.content.ComponentName;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.ShortcutServiceInternal;
+import android.content.res.XmlResourceParser;
+import android.os.Looper;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.server.LocalServices;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Unit tests for all the IPackageManager related methods in {@link ShortcutService}.
+ *
+ * All the tests here actually talks to the real IPackageManager, so we can't test complicated
+ * cases.  Instead we just make sure they all work reasonably without at least crashing.
+ */
+@SmallTest
+public class ShortcutManagerTest5 extends BaseShortcutManagerTest {
+    private ShortcutService mShortcutService;
+
+    private String mMyPackage;
+    private int mMyUserId;
+
+    public static class ShortcutEnabled extends Activity {
+    }
+
+    public static class ShortcutDisabled extends Activity {
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        LocalServices.removeServiceForTest(ShortcutServiceInternal.class);
+        mShortcutService = new ShortcutService(getTestContext(), Looper.getMainLooper(),
+                /* onyForPackageManagerApis */ true);
+
+        mMyPackage = getTestContext().getPackageName();
+        mMyUserId = android.os.Process.myUserHandle().getIdentifier();
+    }
+
+    public void testGetPackageUid() {
+        assertTrue(mShortcutService.injectGetPackageUid(
+                mMyPackage, mMyUserId) != 0);
+
+        assertEquals(-1, mShortcutService.injectGetPackageUid(
+                "no.such.package", mMyUserId));
+    }
+
+    public void testGetPackageInfo() {
+        PackageInfo pi = mShortcutService.getPackageInfo(
+                mMyPackage, mMyUserId, /*signature*/ false);
+        assertEquals(mMyPackage, pi.packageName);
+        assertNull(pi.signatures);
+
+        pi = mShortcutService.getPackageInfo(
+                mMyPackage, mMyUserId, /*signature*/ true);
+        assertEquals(mMyPackage, pi.packageName);
+        assertNotNull(pi.signatures);
+
+        pi = mShortcutService.getPackageInfo(
+                "no.such.package", mMyUserId, /*signature*/ true);
+        assertNull(pi);
+    }
+
+    public void testGetApplicationInfo() {
+        ApplicationInfo ai = mShortcutService.getApplicationInfo(
+                mMyPackage, mMyUserId);
+        assertEquals(mMyPackage, ai.packageName);
+
+        ai = mShortcutService.getApplicationInfo(
+                "no.such.package", mMyUserId);
+        assertNull(ai);
+    }
+
+    public void testGetActivityInfoWithMetadata() {
+        // Disabled activity
+        ActivityInfo ai = mShortcutService.getActivityInfoWithMetadata(
+                new ComponentName(mMyPackage, "ShortcutDisabled"), mMyUserId);
+        assertNull(ai);
+
+        // Nonexistent
+        ai = mShortcutService.getActivityInfoWithMetadata(
+                new ComponentName("no.such.package", "ShortcutDisabled"), mMyUserId);
+        assertNull(ai);
+
+        // Existent, with no metadata.
+        ai = mShortcutService.getActivityInfoWithMetadata(
+                new ComponentName(mMyPackage, "a.ShortcutEnabled"), mMyUserId);
+        assertEquals(mMyPackage, ai.packageName);
+        assertEquals("a.ShortcutEnabled", ai.name);
+        assertNull(ai.loadXmlMetaData(getTestContext().getPackageManager(),
+                "android.app.shortcuts"));
+
+        // Existent, with a shortcut metadata.
+        ai = mShortcutService.getActivityInfoWithMetadata(
+                new ComponentName(mMyPackage, "a.Shortcut1"), mMyUserId);
+        assertEquals(mMyPackage, ai.packageName);
+        assertEquals("a.Shortcut1", ai.name);
+        XmlResourceParser meta = ai.loadXmlMetaData(getTestContext().getPackageManager(),
+                "android.app.shortcuts");
+        assertNotNull(meta);
+        meta.close();
+    }
+
+    public void testGetInstalledPackages() {
+        List<PackageInfo> apks = mShortcutService.getInstalledPackages(mMyUserId);
+
+        Set<String> expectedPackages = set("com.android.settings", mMyPackage);
+        for (PackageInfo pi : apks) {
+            expectedPackages.remove(pi.packageName);
+        }
+        assertEquals(set(), expectedPackages);
+    }
+
+    public void testGetDefaultMainActivity() {
+        ComponentName cn = mShortcutService.injectGetDefaultMainActivity(
+                "com.android.settings", mMyUserId);
+
+        assertEquals(
+                ComponentName.unflattenFromString("com.android.settings/.Settings"),
+                cn);
+
+        // This package has no main activity.
+        assertNull(mShortcutService.injectGetDefaultMainActivity(
+                mMyPackage, mMyUserId));
+
+        // Nonexistent.
+        assertNull(mShortcutService.injectGetDefaultMainActivity(
+                "no.such.package", mMyUserId));
+    }
+
+    public void testIsMainActivity() {
+        assertTrue(mShortcutService.injectIsMainActivity(
+                ComponentName.unflattenFromString("com.android.settings/.Settings"), mMyUserId));
+        assertFalse(mShortcutService.injectIsMainActivity(
+                ComponentName.unflattenFromString("com.android.settings/.xxx"), mMyUserId));
+        assertFalse(mShortcutService.injectIsMainActivity(
+                ComponentName.unflattenFromString("no.such.package/.xxx"), mMyUserId));
+
+        assertFalse(mShortcutService.injectIsMainActivity(
+                new ComponentName(mMyPackage, "a.DisabledMain"), mMyUserId));
+        assertFalse(mShortcutService.injectIsMainActivity(
+                new ComponentName(mMyPackage, "a.UnexportedMain"), mMyUserId));
+
+    }
+
+    public void testGetMainActivities() {
+        assertEquals(1, mShortcutService.injectGetMainActivities(
+                "com.android.settings", mMyUserId).size());
+
+        // This APK has no main activities.
+        assertEquals(0, mShortcutService.injectGetMainActivities(
+                mMyPackage, mMyUserId).size());
+    }
+
+    public void testIsActivityEnabledAndExported() {
+        assertTrue(mShortcutService.injectIsActivityEnabledAndExported(
+                ComponentName.unflattenFromString("com.android.settings/.Settings"), mMyUserId));
+        assertFalse(mShortcutService.injectIsActivityEnabledAndExported(
+                ComponentName.unflattenFromString("com.android.settings/.xxx"), mMyUserId));
+        assertFalse(mShortcutService.injectIsActivityEnabledAndExported(
+                ComponentName.unflattenFromString("no.such.package/.xxx"), mMyUserId));
+
+        assertTrue(mShortcutService.injectIsActivityEnabledAndExported(
+                new ComponentName(mMyPackage, "com.android.server.pm.ShortcutTestActivity"),
+                mMyUserId));
+
+        assertTrue(mShortcutService.injectIsActivityEnabledAndExported(
+                new ComponentName(mMyPackage, "a.ShortcutEnabled"), mMyUserId));
+
+        assertFalse(mShortcutService.injectIsActivityEnabledAndExported(
+                new ComponentName(mMyPackage, "a.ShortcutDisabled"), mMyUserId));
+        assertFalse(mShortcutService.injectIsActivityEnabledAndExported(
+                new ComponentName(mMyPackage, "a.ShortcutUnexported"), mMyUserId));
+
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest6.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest6.java
new file mode 100644
index 0000000..ba4dbc1
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest6.java
@@ -0,0 +1,281 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
+
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.ResolveInfo;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import java.util.List;
+
+/**
+ * Tests for {@link ShortcutService#hasShortcutHostPermissionInner}.
+ */
+@SmallTest
+public class ShortcutManagerTest6 extends BaseShortcutManagerTest {
+    public void testHasShortcutHostPermissionInner_systemLauncherOnly() {
+        // Preferred isn't set, use the system launcher.
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ null,
+                list(getSystemLauncher(), getFallbackLauncher()),
+                USER_0);
+        assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0));
+
+        // Should be cached.
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+        // Also make sure the last known is saved, but the cached is not.
+
+        initService();
+
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(null,
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+    }
+
+    public void testHasShortcutHostPermissionInner_with3pLauncher() {
+        // Preferred isn't set, still use the system launcher.
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ null,
+                list(getSystemLauncher(), getFallbackLauncher(),
+                        ri(CALLING_PACKAGE_1, "name", false, 0),
+                        ri(CALLING_PACKAGE_2, "name", false, 0)
+                ),
+                USER_0);
+        assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0));
+
+        // Should be cached.
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+    }
+
+    public void testHasShortcutHostPermissionInner_with3pLauncher_complicated() {
+        // Preferred is set.  That's the default launcher.
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ cn(CALLING_PACKAGE_2, "name"),
+                list(getSystemLauncher(), getFallbackLauncher(),
+                        ri(CALLING_PACKAGE_1, "name", false, 0),
+                        ri(CALLING_PACKAGE_2, "name", false, 0)
+                ),
+                USER_0);
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+        assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0));
+
+        // Should be cached.
+        assertEquals(cn(CALLING_PACKAGE_2, "name"),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(cn(CALLING_PACKAGE_2, "name"),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+
+        // Once set, even after the preferred launcher is cleared, SM still allows it to access
+        // shortcuts.
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ null,
+                list(getSystemLauncher(), getFallbackLauncher(),
+                        ri(CALLING_PACKAGE_1, "name", false, 0),
+                        ri(CALLING_PACKAGE_2, "name", false, 0)
+                ),
+                USER_0);
+
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+        assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0));
+
+        // Should be cached.
+        assertEquals(cn(CALLING_PACKAGE_2, "name"),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(cn(CALLING_PACKAGE_2, "name"),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+        // However, if the component has been disabled, then we'll recalculate it.
+        mEnabledActivityChecker = (comp, user) -> false;
+
+        assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0));
+
+        mEnabledActivityChecker = (comp, user) -> true;
+
+        // Now the preferred changed.
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ cn(CALLING_PACKAGE_1, "xyz"),
+                list(getSystemLauncher(), getFallbackLauncher(),
+                        ri(CALLING_PACKAGE_1, "name", false, 0),
+                        ri(CALLING_PACKAGE_2, "name", false, 0)
+                ),
+                USER_0);
+
+        assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+
+        // Should be cached.
+        assertEquals(cn(CALLING_PACKAGE_1, "xyz"),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(cn(CALLING_PACKAGE_1, "xyz"),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+
+        // As long as there's the cached launcher set, even if getHomeActivitiesAsUser()
+        // returns different values, the cached one is still the default.
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ getSystemLauncher().activityInfo.getComponentName(),
+                list(getSystemLauncher(), getFallbackLauncher()),
+                USER_0);
+
+        assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+
+        // Cached ones haven't changed.
+        assertEquals(cn(CALLING_PACKAGE_1, "xyz"),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(cn(CALLING_PACKAGE_1, "xyz"),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+        // However, now the "real" default launcher is the system one.  So if the system
+        // launcher asks for shortcuts, we'll allow it.
+        assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+
+        // Since the cache is updated, CALLING_PACKAGE_1 no longer has the permission.
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+
+        // Cached ones haven't changed.
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+    }
+
+    public void testHasShortcutHostPermissionInner_multiUser() {
+        mRunningUsers.put(USER_10, true);
+
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ null,
+                list(getSystemLauncher(), getFallbackLauncher()),
+                USER_0);
+
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ cn(CALLING_PACKAGE_2, "name"),
+                list(getSystemLauncher(), getFallbackLauncher(),
+                        ri(CALLING_PACKAGE_1, "name", false, 0),
+                        ri(CALLING_PACKAGE_2, "name", false, 0)
+                ),
+                USER_10);
+
+        assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_0));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_0));
+
+        // Check the cache.
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getLastKnownLauncher());
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_10));
+        assertFalse(mService.hasShortcutHostPermissionInner(PACKAGE_FALLBACK_LAUNCHER, USER_10));
+        assertFalse(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_1, USER_10));
+        assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_10));
+
+        // Check the cache.
+        assertEquals(cn(CALLING_PACKAGE_2, "name"),
+                mService.getUserShortcutsLocked(USER_10).getLastKnownLauncher());
+        assertEquals(cn(CALLING_PACKAGE_2, "name"),
+                mService.getUserShortcutsLocked(USER_10).getCachedLauncher());
+    }
+
+    public void testHasShortcutHostPermissionInner_clearCache() {
+        mRunningUsers.put(USER_10, true);
+
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ null,
+                list(getSystemLauncher(), getFallbackLauncher()),
+                USER_0);
+
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ cn(CALLING_PACKAGE_2, "name"),
+                list(getSystemLauncher(), getFallbackLauncher(),
+                        ri(CALLING_PACKAGE_1, "name", false, 0),
+                        ri(CALLING_PACKAGE_2, "name", false, 0)
+                ),
+                USER_10);
+
+        assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+        assertTrue(mService.hasShortcutHostPermissionInner(CALLING_PACKAGE_2, USER_10));
+
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+        assertEquals(cn(CALLING_PACKAGE_2, "name"),
+                mService.getUserShortcutsLocked(USER_10).getCachedLauncher());
+
+        // Test it on a non-running user.
+        // Send ACTION_PREFERRED_ACTIVITY_CHANGED on user 10.
+        // But the user is not running, so will be ignored.
+        mRunningUsers.put(USER_10, false);
+
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                new Intent(Intent.ACTION_PREFERRED_ACTIVITY_CHANGED).putExtra(
+                        Intent.EXTRA_USER_HANDLE, USER_10));
+
+        // Need to run the user again to access the internal status.
+        mRunningUsers.put(USER_10, true);
+
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+        assertEquals(cn(CALLING_PACKAGE_2, "name"),
+                mService.getUserShortcutsLocked(USER_10).getCachedLauncher());
+
+         // Send it again after starting the user.
+        mRunningUsers.put(USER_10, true);
+        mService.mPackageMonitor.onReceive(mServiceContext,
+                new Intent(Intent.ACTION_PREFERRED_ACTIVITY_CHANGED).putExtra(
+                        Intent.EXTRA_USER_HANDLE, USER_10));
+
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+        // Only user-10's cache is cleared.
+        assertEquals(null,
+                mService.getUserShortcutsLocked(USER_10).getCachedLauncher());
+
+    }
+}
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest7.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest7.java
new file mode 100644
index 0000000..3c99174
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest7.java
@@ -0,0 +1,339 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertContains;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertExpectException;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertSuccess;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.assertWith;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.readAll;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resultContains;
+
+import android.content.ComponentName;
+import android.os.Bundle;
+import android.os.ParcelFileDescriptor;
+import android.os.Process;
+import android.os.RemoteException;
+import android.os.ResultReceiver;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.frameworks.servicestests.R;
+import com.android.server.pm.ShortcutService.ConfigConstants;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Unit test for "cmd shortcut"
+ *
+ * Launcher related commands are tested in
+ */
+@SmallTest
+public class ShortcutManagerTest7 extends BaseShortcutManagerTest {
+    private List<String> callShellCommand(String... args) throws IOException, RemoteException {
+
+        // For reset to work, the current time needs to be incrementing.
+        mInjectedCurrentTimeMillis++;
+
+        final AtomicInteger resultCode = new AtomicInteger(Integer.MIN_VALUE);
+
+        final ResultReceiver rr = new ResultReceiver(mHandler) {
+            @Override
+            public void send(int resultCode_, Bundle resultData) {
+                resultCode.set(resultCode_);
+            }
+        };
+        final File out = File.createTempFile("shellout-", ".tmp",
+                getTestContext().getCacheDir());
+        try {
+            try (final ParcelFileDescriptor fd = ParcelFileDescriptor.open(out,
+                    ParcelFileDescriptor.MODE_READ_WRITE)) {
+                mService.onShellCommand(
+                    /* fdin*/ null,
+                    /* fdout*/ fd.getFileDescriptor(),
+                    /* fderr*/ fd.getFileDescriptor(),
+                        args, rr);
+            }
+            return readAll(out);
+        } finally {
+            out.delete();
+        }
+    }
+
+    public void testNonShell() throws Exception {
+        mService.mMaxUpdatesPerInterval = 99;
+
+        mInjectedCallingUid = 12345;
+        assertExpectException(SecurityException.class, "must be shell",
+                () -> callShellCommand("reset-config"));
+
+        mInjectedCallingUid = Process.SYSTEM_UID;
+        assertExpectException(SecurityException.class, "must be shell",
+                () -> callShellCommand("reset-config"));
+
+        assertEquals(99, mService.mMaxUpdatesPerInterval);
+    }
+
+    public void testRoot() throws Exception {
+        mService.mMaxUpdatesPerInterval = 99;
+
+        mInjectedCallingUid = Process.ROOT_UID;
+        assertSuccess(callShellCommand("reset-config"));
+
+        assertEquals(3, mService.mMaxUpdatesPerInterval);
+    }
+
+    public void testRestConfig() throws Exception {
+        mService.mMaxUpdatesPerInterval = 99;
+
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertSuccess(callShellCommand("reset-config"));
+
+        assertEquals(3, mService.mMaxUpdatesPerInterval);
+    }
+
+    public void testOverrideConfig() throws Exception {
+        mService.mMaxUpdatesPerInterval = 99;
+
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertSuccess(callShellCommand("override-config",
+                ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL + "=1"));
+
+        assertEquals(1, mService.mMaxUpdatesPerInterval);
+    }
+
+    public void testResetThrottling() throws Exception {
+        prepareCrossProfileDataSet();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertSuccess(callShellCommand("reset-throttling"));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+    }
+
+    public void testResetThrottling_user_not_running() throws Exception {
+        prepareCrossProfileDataSet();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+
+        mInjectedCallingUid = Process.SHELL_UID;
+
+        mRunningUsers.put(USER_10, false);
+
+        assertTrue(resultContains(
+                callShellCommand("reset-throttling", "--user", "10"),
+                "User 10 is not running or locked"));
+
+        mRunningUsers.put(USER_10, true);
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+    }
+
+    public void testResetThrottling_user_running() throws Exception {
+        prepareCrossProfileDataSet();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+
+        mRunningUsers.put(USER_10, true);
+        mUnlockedUsers.put(USER_10, true);
+
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertSuccess(callShellCommand("reset-throttling", "--user", "10"));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+    }
+
+    public void testResetAllThrottling() throws Exception {
+        prepareCrossProfileDataSet();
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.getRemainingCallCount() < 3);
+        });
+
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertSuccess(callShellCommand("reset-all-throttling"));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_0, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertEquals(3, mManager.getRemainingCallCount());
+        });
+    }
+
+    public void testLauncherCommands() throws Exception {
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ null,
+                list(getSystemLauncher(), getFallbackLauncher()),
+                USER_0);
+
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ cn(CALLING_PACKAGE_2, "name"),
+                list(getSystemLauncher(), getFallbackLauncher(),
+                        ri(CALLING_PACKAGE_1, "name", false, 0),
+                        ri(CALLING_PACKAGE_2, "name", false, 0)
+                ),
+                USER_10);
+
+        assertTrue(mService.hasShortcutHostPermissionInner(PACKAGE_SYSTEM_LAUNCHER, USER_0));
+
+        // First, test "get".
+
+        mRunningUsers.put(USER_10, true);
+        mUnlockedUsers.put(USER_10, true);
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertContains(
+                assertSuccess(callShellCommand("get-default-launcher")),
+                "Launcher: ComponentInfo{com.android.systemlauncher/systemlauncher_name}");
+
+        assertContains(
+                assertSuccess(callShellCommand("get-default-launcher", "--user", "10")),
+                "Launcher: ComponentInfo{com.android.test.2/name}");
+
+        // Next, test "clear".
+        assertSuccess(callShellCommand("clear-default-launcher", "--user", "10"));
+
+        // User-10's launcher should be cleared.
+        assertEquals(null, mService.getUserShortcutsLocked(USER_10).getLastKnownLauncher());
+        assertEquals(null, mService.getUserShortcutsLocked(USER_10).getCachedLauncher());
+
+        // but user'0's shouldn't.
+        assertEquals(cn(PACKAGE_SYSTEM_LAUNCHER, PACKAGE_SYSTEM_LAUNCHER_NAME),
+                mService.getUserShortcutsLocked(USER_0).getCachedLauncher());
+
+        // Change user-0's launcher.
+        prepareGetHomeActivitiesAsUser(
+                /* preferred */ cn(CALLING_PACKAGE_1, "name"),
+                list(
+                        ri(CALLING_PACKAGE_1, "name", false, 0)
+                ),
+                USER_0);
+        assertContains(
+                assertSuccess(callShellCommand("get-default-launcher")),
+                "Launcher: ComponentInfo{com.android.test.1/name}");
+    }
+
+    public void testUnloadUser() throws Exception {
+        prepareCrossProfileDataSet();
+
+        assertNotNull(mService.getShortcutsForTest().get(USER_10));
+
+        mRunningUsers.put(USER_10, true);
+        mUnlockedUsers.put(USER_10, true);
+
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertSuccess(callShellCommand("unload-user", "--user", "10"));
+
+        assertNull(mService.getShortcutsForTest().get(USER_10));
+    }
+
+    public void testClearShortcuts() throws Exception {
+
+        mRunningUsers.put(USER_10, true);
+
+        // Add two manifests and two dynamics.
+        addManifestShortcutResource(
+                new ComponentName(CALLING_PACKAGE_1, ShortcutActivity.class.getName()),
+                R.xml.shortcut_2);
+        updatePackageVersion(CALLING_PACKAGE_1, 1);
+        mService.mPackageMonitor.onReceive(getTestContext(),
+                genPackageAddIntent(CALLING_PACKAGE_1, USER_10));
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertTrue(mManager.addDynamicShortcuts(list(
+                    makeShortcut("s1"), makeShortcut("s2"))));
+        });
+        runWithCaller(LAUNCHER_1, USER_10, () -> {
+            mLauncherApps.pinShortcuts(CALLING_PACKAGE_1, list("ms2", "s2"), HANDLE_USER_10);
+        });
+
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "s1", "s2")
+                    .areAllEnabled()
+
+                    .selectPinned()
+                    .haveIds("ms2", "s2");
+        });
+
+        // First, call for a different package.
+
+        mRunningUsers.put(USER_10, true);
+        mUnlockedUsers.put(USER_10, true);
+
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertSuccess(callShellCommand("clear-shortcuts", "--user", "10", CALLING_PACKAGE_2));
+
+        // Shouldn't be cleared yet.
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2", "s1", "s2")
+                    .areAllEnabled()
+
+                    .selectPinned()
+                    .haveIds("ms2", "s2");
+        });
+
+        mInjectedCallingUid = Process.SHELL_UID;
+        assertSuccess(callShellCommand("clear-shortcuts", "--user", "10", CALLING_PACKAGE_1));
+
+        // Only manifest shortcuts will remain, and are no longer pinned.
+        runWithCaller(CALLING_PACKAGE_1, USER_10, () -> {
+            assertWith(getCallerShortcuts())
+                    .haveIds("ms1", "ms2")
+                    .areAllEnabled()
+                    .areAllNotPinned();
+        });
+    }
+}
diff --git a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl b/services/tests/servicestests/src/com/android/server/pm/ShortcutTestActivity.java
similarity index 69%
copy from telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
copy to services/tests/servicestests/src/com/android/server/pm/ShortcutTestActivity.java
index b7e78d1..d82b0d5 100644
--- a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutTestActivity.java
@@ -1,11 +1,11 @@
 /*
- * Copyright 2016, The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ *      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,
@@ -13,10 +13,9 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package com.android.server.pm;
 
-package android.telecom;
+import android.app.Activity;
 
-/**
- * {@hide}
- */
-parcelable ParcelableCallAnalytics;
+public class ShortcutTestActivity extends Activity {
+}
diff --git a/services/tests/servicestests/src/com/android/server/pm/UserManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/pm/UserManagerServiceTest.java
index 48a11c5..9f77297 100644
--- a/services/tests/servicestests/src/com/android/server/pm/UserManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/UserManagerServiceTest.java
@@ -23,12 +23,14 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
 import android.util.AtomicFile;
 
 import java.io.File;
 import java.io.IOException;
 import java.util.Arrays;
 
+@SmallTest
 public class UserManagerServiceTest extends AndroidTestCase {
     private static String[] STRING_ARRAY = new String[] {"<tag", "<![CDATA["};
     private File restrictionsFile;
diff --git a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
index 6c2fcd5..ced4980 100644
--- a/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/UserManagerTest.java
@@ -25,6 +25,7 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.MediumTest;
 
 import com.android.internal.util.ArrayUtils;
 
@@ -33,6 +34,7 @@
 import java.util.List;
 
 /** Test {@link UserManager} functionality. */
+@MediumTest
 public class UserManagerTest extends AndroidTestCase {
     private static final int REMOVE_CHECK_INTERVAL = 500;
     private static final int REMOVE_TIMEOUT = 60 * 1000;
@@ -96,7 +98,7 @@
                     && !user.isPrimary()) {
                 found = true;
                 Bundle restrictions = mUserManager.getUserRestrictions(user.getUserHandle());
-                assertFalse("New user should have DISALLOW_CONFIG_WIFI =false by default",
+                assertTrue("Guest user should have DISALLOW_CONFIG_WIFI=true by default",
                         restrictions.getBoolean(UserManager.DISALLOW_CONFIG_WIFI));
             }
         }
diff --git a/services/tests/servicestests/src/com/android/server/pm/UserRestrictionsUtilsTest.java b/services/tests/servicestests/src/com/android/server/pm/UserRestrictionsUtilsTest.java
index 5bdf6f7..11f9ebb 100644
--- a/services/tests/servicestests/src/com/android/server/pm/UserRestrictionsUtilsTest.java
+++ b/services/tests/servicestests/src/com/android/server/pm/UserRestrictionsUtilsTest.java
@@ -16,13 +16,13 @@
 
 package com.android.server.pm;
 
-import android.os.UserHandle;
-import com.android.server.devicepolicy.DpmTestUtils;
-
 import android.os.Bundle;
+import android.os.UserHandle;
 import android.os.UserManager;
 import android.test.AndroidTestCase;
-import android.test.MoreAsserts;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.server.devicepolicy.DpmTestUtils;
 
 /**
  * Tests for {@link com.android.server.pm.UserRestrictionsUtils}.
@@ -35,6 +35,7 @@
      -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
  * </pre>
  */
+@SmallTest
 public class UserRestrictionsUtilsTest extends AndroidTestCase {
     public void testNonNull() {
         Bundle out = UserRestrictionsUtils.nonNull(null);
diff --git a/services/tests/servicestests/src/com/android/server/usage/AppIdleHistoryTests.java b/services/tests/servicestests/src/com/android/server/usage/AppIdleHistoryTests.java
index 9ccb1a6..0bd014c 100644
--- a/services/tests/servicestests/src/com/android/server/usage/AppIdleHistoryTests.java
+++ b/services/tests/servicestests/src/com/android/server/usage/AppIdleHistoryTests.java
@@ -67,7 +67,7 @@
         // Screen on time should not keep progressing with screen is off
         assertEquals(aih.getScreenOnTimeLocked(7000), 3000);
         assertEquals(aih.getScreenOnTimeLocked(8000), 3000);
-        aih.writeElapsedTimeLocked();
+        aih.writeAppIdleDurationsLocked();
 
         // Check if the screen on time is persisted across instantiations
         AppIdleHistory aih2 = new AppIdleHistory(mStorageDir, 0);
diff --git a/services/tests/shortcutmanagerutils/Android.mk b/services/tests/shortcutmanagerutils/Android.mk
new file mode 100644
index 0000000..2818457
--- /dev/null
+++ b/services/tests/shortcutmanagerutils/Android.mk
@@ -0,0 +1,31 @@
+# Copyright (C) 2016 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := \
+    $(call all-java-files-under, src)
+
+LOCAL_STATIC_JAVA_LIBRARIES := \
+    mockito-target
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE := ShortcutManagerTestUtils
+
+LOCAL_SDK_VERSION := test_current
+
+include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/services/tests/shortcutmanagerutils/src/com/android/server/pm/shortcutmanagertest/ShortcutManagerTestUtils.java b/services/tests/shortcutmanagerutils/src/com/android/server/pm/shortcutmanagertest/ShortcutManagerTestUtils.java
new file mode 100644
index 0000000..1fe5cb7
--- /dev/null
+++ b/services/tests/shortcutmanagerutils/src/com/android/server/pm/shortcutmanagertest/ShortcutManagerTestUtils.java
@@ -0,0 +1,1073 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.server.pm.shortcutmanagertest;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertFalse;
+import static junit.framework.Assert.assertNotNull;
+import static junit.framework.Assert.assertNull;
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.Assert.fail;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyList;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import android.app.Instrumentation;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.LauncherApps;
+import android.content.pm.LauncherApps.Callback;
+import android.content.pm.ShortcutInfo;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.BaseBundle;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Parcel;
+import android.os.ParcelFileDescriptor;
+import android.os.PersistableBundle;
+import android.os.UserHandle;
+import android.test.MoreAsserts;
+import android.util.Log;
+
+import junit.framework.Assert;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.concurrent.CountDownLatch;
+import java.util.function.BooleanSupplier;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Predicate;
+
+/**
+ * Common utility methods for ShortcutManager tests.  This is used by both CTS and the unit tests.
+ * Because it's used by CTS too, it can only access the public APIs.
+ */
+public class ShortcutManagerTestUtils {
+    private static final String TAG = "ShortcutManagerUtils";
+
+    private static final boolean ENABLE_DUMPSYS = true; // DO NOT SUBMIT WITH true
+
+    private static final int STANDARD_TIMEOUT_SEC = 5;
+
+    private static final String[] EMPTY_STRINGS = new String[0];
+
+    private ShortcutManagerTestUtils() {
+    }
+
+    public static List<String> readAll(File file) throws FileNotFoundException {
+        return readAll(ParcelFileDescriptor.open(
+                file.getAbsoluteFile(), ParcelFileDescriptor.MODE_READ_ONLY));
+    }
+
+    public static List<String> readAll(ParcelFileDescriptor pfd) {
+        try {
+            try {
+                final ArrayList<String> ret = new ArrayList<>();
+                try (BufferedReader r = new BufferedReader(
+                        new FileReader(pfd.getFileDescriptor()))) {
+                    String line;
+                    while ((line = r.readLine()) != null) {
+                        ret.add(line);
+                    }
+                    r.readLine();
+                }
+                return ret;
+            } finally {
+                pfd.close();
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static String concatResult(List<String> result) {
+        final StringBuilder sb = new StringBuilder();
+        for (String s : result) {
+            sb.append(s);
+            sb.append("\n");
+        }
+        return sb.toString();
+    }
+
+    public static boolean resultContains(List<String> result, String expected) {
+        for (String line : result) {
+            if (line.contains(expected)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public static List<String> assertSuccess(List<String> result) {
+        if (!resultContains(result, "Success")) {
+            fail("Command failed.  Result was:\n" + concatResult(result));
+        }
+        return result;
+    }
+
+    public static List<String> assertContains(List<String> result, String expected) {
+        if (!resultContains(result, expected)) {
+            fail("Didn't contain expected string=" + expected
+                    + "\nActual:\n" + concatResult(result));
+        }
+        return result;
+    }
+
+    public static List<String> runCommand(Instrumentation instrumentation, String command) {
+        return runCommand(instrumentation, command, null);
+    }
+    public static List<String> runCommand(Instrumentation instrumentation, String command,
+            Predicate<List<String>> resultAsserter) {
+        Log.d(TAG, "Running command: " + command);
+        final List<String> result;
+        try {
+            result = readAll(
+                    instrumentation.getUiAutomation().executeShellCommand(command));
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+        if (resultAsserter != null && !resultAsserter.test(result)) {
+            fail("Command '" + command + "' failed, output was:\n" + concatResult(result));
+        }
+        return result;
+    }
+
+    public static void runCommandForNoOutput(Instrumentation instrumentation, String command) {
+        runCommand(instrumentation, command, result -> result.size() == 0);
+    }
+
+    public static List<String> runShortcutCommand(Instrumentation instrumentation, String command,
+            Predicate<List<String>> resultAsserter) {
+        return runCommand(instrumentation, "cmd shortcut " + command, resultAsserter);
+    }
+
+    public static List<String> runShortcutCommandForSuccess(Instrumentation instrumentation,
+            String command) {
+        return runShortcutCommand(instrumentation, command, result -> result.contains("Success"));
+    }
+
+    public static String getDefaultLauncher(Instrumentation instrumentation) {
+        final String PREFIX = "Launcher: ComponentInfo{";
+        final String POSTFIX = "}";
+        final List<String> result = runShortcutCommandForSuccess(
+                instrumentation, "get-default-launcher");
+        for (String s : result) {
+            if (s.startsWith(PREFIX) && s.endsWith(POSTFIX)) {
+                return s.substring(PREFIX.length(), s.length() - POSTFIX.length());
+            }
+        }
+        fail("Default launcher not found");
+        return null;
+    }
+
+    public static void setDefaultLauncher(Instrumentation instrumentation, String component) {
+        runCommand(instrumentation, "cmd package set-home-activity --user "
+                + instrumentation.getContext().getUserId() + " " + component,
+                result -> result.contains("Success"));
+    }
+
+    public static void setDefaultLauncher(Instrumentation instrumentation, Context packageContext) {
+        setDefaultLauncher(instrumentation, packageContext.getPackageName()
+                + "/android.content.pm.cts.shortcutmanager.packages.Launcher");
+    }
+
+    public static void overrideConfig(Instrumentation instrumentation, String config) {
+        runShortcutCommandForSuccess(instrumentation, "override-config " + config);
+    }
+
+    public static void resetConfig(Instrumentation instrumentation) {
+        runShortcutCommandForSuccess(instrumentation, "reset-config");
+    }
+
+    public static void resetThrottling(Instrumentation instrumentation) {
+        runShortcutCommandForSuccess(instrumentation, "reset-throttling");
+    }
+
+    public static void resetAllThrottling(Instrumentation instrumentation) {
+        runShortcutCommandForSuccess(instrumentation, "reset-all-throttling");
+    }
+
+    public static void clearShortcuts(Instrumentation instrumentation, int userId,
+            String packageName) {
+        runShortcutCommandForSuccess(instrumentation, "clear-shortcuts "
+                + " --user " + userId + " " + packageName);
+    }
+
+    public static void anyContains(List<String> result, String expected) {
+        for (String l : result) {
+            if (l.contains(expected)) {
+                return;
+            }
+        }
+        fail("Result didn't contain '" + expected + "': was\n" + result);
+    }
+
+    public static void enableComponent(Instrumentation instrumentation, ComponentName cn,
+            boolean enable) {
+
+        final String word = (enable ? "enable" : "disable");
+        runCommand(instrumentation,
+                "pm " + word + " " + cn.flattenToString()
+                , result ->concatResult(result).contains(word));
+    }
+
+    public static void appOps(Instrumentation instrumentation, String packageName,
+            String op, String mode) {
+        runCommand(instrumentation, "appops set " + packageName + " " + op + " " + mode);
+    }
+
+    public static void dumpsysShortcut(Instrumentation instrumentation) {
+        if (!ENABLE_DUMPSYS) {
+            return;
+        }
+        Log.e(TAG, "Dumpsys shortcut");
+        for (String s : runCommand(instrumentation, "dumpsys shortcut")) {
+            Log.e(TAG, s);
+        }
+    }
+
+    public static JSONObject getCheckinDump(Instrumentation instrumentation) throws JSONException {
+        return new JSONObject(concatResult(runCommand(instrumentation, "dumpsys shortcut -c")));
+    }
+
+    public static boolean isLowRamDevice(Instrumentation instrumentation) throws JSONException {
+        return getCheckinDump(instrumentation).getBoolean("lowRam");
+    }
+
+    public static int getIconSize(Instrumentation instrumentation) throws JSONException {
+        return getCheckinDump(instrumentation).getInt("iconSize");
+    }
+
+    public static Bundle makeBundle(Object... keysAndValues) {
+        assertTrue((keysAndValues.length % 2) == 0);
+
+        if (keysAndValues.length == 0) {
+            return null;
+        }
+        final Bundle ret = new Bundle();
+
+        for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
+            final String key = keysAndValues[i].toString();
+            final Object value = keysAndValues[i + 1];
+
+            if (value == null) {
+                ret.putString(key, null);
+            } else if (value instanceof Integer) {
+                ret.putInt(key, (Integer) value);
+            } else if (value instanceof String) {
+                ret.putString(key, (String) value);
+            } else if (value instanceof Bundle) {
+                ret.putBundle(key, (Bundle) value);
+            } else {
+                fail("Type not supported yet: " + value.getClass().getName());
+            }
+        }
+        return ret;
+    }
+
+    public static PersistableBundle makePersistableBundle(Object... keysAndValues) {
+        assertTrue((keysAndValues.length % 2) == 0);
+
+        if (keysAndValues.length == 0) {
+            return null;
+        }
+        final PersistableBundle ret = new PersistableBundle();
+
+        for (int i = keysAndValues.length - 2; i >= 0; i -= 2) {
+            final String key = keysAndValues[i].toString();
+            final Object value = keysAndValues[i + 1];
+
+            if (value == null) {
+                ret.putString(key, null);
+            } else if (value instanceof Integer) {
+                ret.putInt(key, (Integer) value);
+            } else if (value instanceof String) {
+                ret.putString(key, (String) value);
+            } else if (value instanceof PersistableBundle) {
+                ret.putPersistableBundle(key, (PersistableBundle) value);
+            } else {
+                fail("Type not supported yet: " + value.getClass().getName());
+            }
+        }
+        return ret;
+    }
+
+    public static <T> List<T> list(T... array) {
+        return Arrays.asList(array);
+    }
+
+    public static <T> Set<T> hashSet(Set<T> in) {
+        return new LinkedHashSet<>(in);
+    }
+
+    public static <T> Set<T> set(T... values) {
+        return set(v -> v, values);
+    }
+
+    public static <T, V> Set<T> set(Function<V, T> converter, V... values) {
+        return set(converter, Arrays.asList(values));
+    }
+
+    public static <T, V> Set<T> set(Function<V, T> converter, List<V> values) {
+        final LinkedHashSet<T> ret = new LinkedHashSet<>();
+        for (V v : values) {
+            ret.add(converter.apply(v));
+        }
+        return ret;
+    }
+
+    public static void resetAll(Collection<?> mocks) {
+        for (Object o : mocks) {
+            reset(o);
+        }
+    }
+
+    public static <T extends Collection<?>> T assertEmpty(T collection) {
+        if (collection == null) {
+            return collection; // okay.
+        }
+        assertEquals(0, collection.size());
+        return collection;
+    }
+
+    public static List<ShortcutInfo> filter(List<ShortcutInfo> list, Predicate<ShortcutInfo> p) {
+        final ArrayList<ShortcutInfo> ret = new ArrayList<>(list);
+        ret.removeIf(si -> !p.test(si));
+        return ret;
+    }
+
+    public static List<ShortcutInfo> filterByActivity(List<ShortcutInfo> list,
+            ComponentName activity) {
+        return filter(list, si ->
+                (si.getActivity().equals(activity)
+                        && (si.isDeclaredInManifest() || si.isDynamic())));
+    }
+
+    public static List<ShortcutInfo> changedSince(List<ShortcutInfo> list, long time) {
+        return filter(list, si -> si.getLastChangedTimestamp() >= time);
+    }
+
+    @FunctionalInterface
+    public interface ExceptionRunnable {
+        void run() throws Exception;
+    }
+
+    public static void assertExpectException(Class<? extends Throwable> expectedExceptionType,
+            String expectedExceptionMessageRegex, ExceptionRunnable r) {
+        assertExpectException("", expectedExceptionType, expectedExceptionMessageRegex, r);
+    }
+
+    public static void assertCannotUpdateImmutable(Runnable r) {
+        assertExpectException(
+                IllegalArgumentException.class, "may not be manipulated via APIs", r::run);
+    }
+
+    public static void assertDynamicShortcutCountExceeded(Runnable r) {
+        assertExpectException(IllegalArgumentException.class,
+                "Max number of dynamic shortcuts exceeded", r::run);
+    }
+
+    public static void assertExpectException(String message,
+            Class<? extends Throwable> expectedExceptionType,
+            String expectedExceptionMessageRegex, ExceptionRunnable r) {
+        try {
+            r.run();
+        } catch (Throwable e) {
+            Assert.assertTrue(
+                    "Expected exception type was " + expectedExceptionType.getName()
+                            + " but caught " + e + " (message=" + message + ")",
+                    expectedExceptionType.isAssignableFrom(e.getClass()));
+            if (expectedExceptionMessageRegex != null) {
+                MoreAsserts.assertContainsRegex(expectedExceptionMessageRegex, e.getMessage());
+            }
+            return; // Pass
+        }
+        Assert.fail("Expected exception type " + expectedExceptionType.getName()
+                + " was not thrown");
+    }
+
+    public static List<ShortcutInfo> assertShortcutIds(List<ShortcutInfo> actualShortcuts,
+            String... expectedIds) {
+        final SortedSet<String> expected = new TreeSet<>(list(expectedIds));
+        final SortedSet<String> actual = new TreeSet<>();
+        for (ShortcutInfo s : actualShortcuts) {
+            actual.add(s.getId());
+        }
+
+        // Compare the sets.
+        assertEquals(expected, actual);
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertShortcutIdsOrdered(List<ShortcutInfo> actualShortcuts,
+            String... expectedIds) {
+        final ArrayList<String> expected = new ArrayList<>(list(expectedIds));
+        final ArrayList<String> actual = new ArrayList<>();
+        for (ShortcutInfo s : actualShortcuts) {
+            actual.add(s.getId());
+        }
+        assertEquals(expected, actual);
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllHaveIntents(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertNotNull("ID " + s.getId(), s.getIntent());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllNotHaveIntents(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertNull("ID " + s.getId(), s.getIntent());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllHaveTitle(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertNotNull("ID " + s.getId(), s.getShortLabel());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllNotHaveTitle(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertNull("ID " + s.getId(), s.getShortLabel());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllKeyFieldsOnly(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), s.hasKeyFieldsOnly());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllNotKeyFieldsOnly(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertFalse("ID " + s.getId(), s.hasKeyFieldsOnly());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllDynamic(List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), s.isDynamic());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllPinned(List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), s.isPinned());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllDynamicOrPinned(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), s.isDynamic() || s.isPinned());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllManifest(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), s.isDeclaredInManifest());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllNotManifest(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertFalse("ID " + s.getId(), s.isDeclaredInManifest());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllDisabled(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), !s.isEnabled());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllEnabled(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), s.isEnabled());
+        }
+        return actualShortcuts;
+    }
+
+    public static List<ShortcutInfo> assertAllImmutable(
+            List<ShortcutInfo> actualShortcuts) {
+        for (ShortcutInfo s : actualShortcuts) {
+            assertTrue("ID " + s.getId(), s.isImmutable());
+        }
+        return actualShortcuts;
+    }
+
+    public static void assertDynamicOnly(ShortcutInfo si) {
+        assertTrue(si.isDynamic());
+        assertFalse(si.isPinned());
+    }
+
+    public static void assertPinnedOnly(ShortcutInfo si) {
+        assertFalse(si.isDynamic());
+        assertFalse(si.isDeclaredInManifest());
+        assertTrue(si.isPinned());
+    }
+
+    public static void assertDynamicAndPinned(ShortcutInfo si) {
+        assertTrue(si.isDynamic());
+        assertTrue(si.isPinned());
+    }
+
+    public static void assertBitmapSize(int expectedWidth, int expectedHeight, Bitmap bitmap) {
+        assertEquals("width", expectedWidth, bitmap.getWidth());
+        assertEquals("height", expectedHeight, bitmap.getHeight());
+    }
+
+    public static <T> void assertAllUnique(Collection<T> list) {
+        final Set<Object> set = new LinkedHashSet<>();
+        for (T item : list) {
+            if (set.contains(item)) {
+                fail("Duplicate item found: " + item + " (in the list: " + list + ")");
+            }
+            set.add(item);
+        }
+    }
+
+    public static ShortcutInfo findShortcut(List<ShortcutInfo> list, String id) {
+        for (ShortcutInfo si : list) {
+            if (si.getId().equals(id)) {
+                return si;
+            }
+        }
+        fail("Shortcut " + id + " not found in the list");
+        return null;
+    }
+
+    public static Bitmap pfdToBitmap(ParcelFileDescriptor pfd) {
+        assertNotNull(pfd);
+        try {
+            try {
+                return BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor());
+            } finally {
+                pfd.close();
+            }
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static void assertBundleEmpty(BaseBundle b) {
+        assertTrue(b == null || b.size() == 0);
+    }
+
+    public static void assertCallbackNotReceived(LauncherApps.Callback mock) {
+        verify(mock, times(0)).onShortcutsChanged(anyString(), anyList(),
+                any(UserHandle.class));
+    }
+
+    public static void assertCallbackReceived(LauncherApps.Callback mock,
+            UserHandle user, String packageName, String... ids) {
+        verify(mock).onShortcutsChanged(eq(packageName), checkShortcutIds(ids),
+                eq(user));
+    }
+
+    public static boolean checkAssertSuccess(Runnable r) {
+        try {
+            r.run();
+            return true;
+        } catch (AssertionError e) {
+            return false;
+        }
+    }
+
+    public static <T> T checkArgument(Predicate<T> checker, String description,
+            List<T> matchedCaptor) {
+        final Matcher<T> m = new BaseMatcher<T>() {
+            @Override
+            public boolean matches(Object item) {
+                if (item == null) {
+                    return false;
+                }
+                final T value = (T) item;
+                if (!checker.test(value)) {
+                    return false;
+                }
+
+                if (matchedCaptor != null) {
+                    matchedCaptor.add(value);
+                }
+                return true;
+            }
+
+            @Override
+            public void describeTo(Description d) {
+                d.appendText(description);
+            }
+        };
+        return Mockito.argThat(m);
+    }
+
+    public static List<ShortcutInfo> checkShortcutIds(String... ids) {
+        return checkArgument((List<ShortcutInfo> list) -> {
+            final Set<String> actualSet = set(si -> si.getId(), list);
+            return actualSet.equals(set(ids));
+
+        }, "Shortcut IDs=[" + Arrays.toString(ids) + "]", null);
+    }
+
+    public static ShortcutInfo parceled(ShortcutInfo si) {
+        Parcel p = Parcel.obtain();
+        p.writeParcelable(si, 0);
+        p.setDataPosition(0);
+        ShortcutInfo si2 = p.readParcelable(ShortcutManagerTestUtils.class.getClassLoader());
+        p.recycle();
+        return si2;
+    }
+
+    public static List<ShortcutInfo> cloneShortcutList(List<ShortcutInfo> list) {
+        if (list == null) {
+            return null;
+        }
+        final List<ShortcutInfo> ret = new ArrayList<>(list.size());
+        for (ShortcutInfo si : list) {
+            ret.add(parceled(si));
+        }
+
+        return ret;
+    }
+
+    private static final Comparator<ShortcutInfo> sRankComparator =
+            (ShortcutInfo a, ShortcutInfo b) -> Integer.compare(a.getRank(), b.getRank());
+
+    public static List<ShortcutInfo> sortedByRank(List<ShortcutInfo> shortcuts) {
+        final ArrayList<ShortcutInfo> ret = new ArrayList<>(shortcuts);
+        Collections.sort(ret, sRankComparator);
+        return ret;
+    }
+
+    public static void waitUntil(String message, BooleanSupplier condition) {
+        waitUntil(message, condition, STANDARD_TIMEOUT_SEC);
+    }
+
+    public static void waitUntil(String message, BooleanSupplier condition, int timeoutSeconds) {
+        final long timeout = System.currentTimeMillis() + (timeoutSeconds * 1000L);
+        while (System.currentTimeMillis() < timeout) {
+            if (condition.getAsBoolean()) {
+                return;
+            }
+            try {
+                Thread.sleep(100);
+            } catch (InterruptedException e) {
+                throw new RuntimeException(e);
+            }
+        }
+        fail("Timed out for: " + message);
+    }
+
+    public static ShortcutListAsserter assertWith(List<ShortcutInfo> list) {
+        return new ShortcutListAsserter(list);
+    }
+
+    /**
+     * New style assertion that allows chained calls.
+     */
+    public static class ShortcutListAsserter {
+        private final ShortcutListAsserter mOriginal;
+        private final List<ShortcutInfo> mList;
+
+        ShortcutListAsserter(List<ShortcutInfo> list) {
+            this(null, list);
+        }
+
+        private ShortcutListAsserter(ShortcutListAsserter original, List<ShortcutInfo> list) {
+            mOriginal = (original == null) ? this : original;
+            mList = (list == null) ? new ArrayList<>(0) : new ArrayList<>(list);
+        }
+
+        public ShortcutListAsserter revertToOriginalList() {
+            return mOriginal;
+        }
+
+        public ShortcutListAsserter selectDynamic() {
+            return new ShortcutListAsserter(this,
+                    filter(mList, ShortcutInfo::isDynamic));
+        }
+
+        public ShortcutListAsserter selectManifest() {
+            return new ShortcutListAsserter(this,
+                    filter(mList, ShortcutInfo::isDeclaredInManifest));
+        }
+
+        public ShortcutListAsserter selectPinned() {
+            return new ShortcutListAsserter(this,
+                    filter(mList, ShortcutInfo::isPinned));
+        }
+
+        public ShortcutListAsserter selectByActivity(ComponentName activity) {
+            return new ShortcutListAsserter(this,
+                    ShortcutManagerTestUtils.filterByActivity(mList, activity));
+        }
+
+        public ShortcutListAsserter selectByChangedSince(long time) {
+            return new ShortcutListAsserter(this,
+                    ShortcutManagerTestUtils.changedSince(mList, time));
+        }
+
+        public ShortcutListAsserter selectByIds(String... ids) {
+            final Set<String> idSet = set(ids);
+            final ArrayList<ShortcutInfo> selected = new ArrayList<>();
+            for (ShortcutInfo si : mList) {
+                if (idSet.contains(si.getId())) {
+                    selected.add(si);
+                    idSet.remove(si.getId());
+                }
+            }
+            if (idSet.size() > 0) {
+                fail("Shortcuts not found for IDs=" + idSet);
+            }
+
+            return new ShortcutListAsserter(this, selected);
+        }
+
+        public ShortcutListAsserter toSortByRank() {
+            return new ShortcutListAsserter(this,
+                    ShortcutManagerTestUtils.sortedByRank(mList));
+        }
+
+        public ShortcutListAsserter call(Consumer<List<ShortcutInfo>> c) {
+            c.accept(mList);
+            return this;
+        }
+
+        public ShortcutListAsserter haveIds(String... expectedIds) {
+            assertShortcutIds(mList, expectedIds);
+            return this;
+        }
+
+        public ShortcutListAsserter haveIdsOrdered(String... expectedIds) {
+            assertShortcutIdsOrdered(mList, expectedIds);
+            return this;
+        }
+
+        private ShortcutListAsserter haveSequentialRanks() {
+            for (int i = 0; i < mList.size(); i++) {
+                final ShortcutInfo si = mList.get(i);
+                assertEquals("Rank not sequential: id=" + si.getId(), i, si.getRank());
+            }
+            return this;
+        }
+
+        public ShortcutListAsserter haveRanksInOrder(String... expectedIds) {
+            toSortByRank()
+                    .haveSequentialRanks()
+                    .haveIdsOrdered(expectedIds);
+            return this;
+        }
+
+        public ShortcutListAsserter isEmpty() {
+            assertEquals(0, mList.size());
+            return this;
+        }
+
+        public ShortcutListAsserter areAllDynamic() {
+            forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDynamic()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllNotDynamic() {
+            forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDynamic()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllPinned() {
+            forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isPinned()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllNotPinned() {
+            forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isPinned()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllManifest() {
+            forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isDeclaredInManifest()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllNotManifest() {
+            forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isDeclaredInManifest()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllImmutable() {
+            forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isImmutable()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllMutable() {
+            forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isImmutable()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllEnabled() {
+            forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.isEnabled()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllDisabled() {
+            forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.isEnabled()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllWithKeyFieldsOnly() {
+            forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.hasKeyFieldsOnly()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllNotWithKeyFieldsOnly() {
+            forAllShortcuts(s -> assertFalse("id=" + s.getId(), s.hasKeyFieldsOnly()));
+            return this;
+        }
+
+        public ShortcutListAsserter areAllWithActivity(ComponentName activity) {
+            forAllShortcuts(s -> assertTrue("id=" + s.getId(), s.getActivity().equals(activity)));
+            return this;
+        }
+
+        public ShortcutListAsserter forAllShortcuts(Consumer<ShortcutInfo> sa) {
+            boolean found = false;
+            for (int i = 0; i < mList.size(); i++) {
+                final ShortcutInfo si = mList.get(i);
+                found = true;
+                sa.accept(si);
+            }
+            assertTrue("No shortcuts found.", found);
+            return this;
+        }
+
+        public ShortcutListAsserter forShortcut(Predicate<ShortcutInfo> p,
+                Consumer<ShortcutInfo> sa) {
+            boolean found = false;
+            for (int i = 0; i < mList.size(); i++) {
+                final ShortcutInfo si = mList.get(i);
+                if (p.test(si)) {
+                    found = true;
+                    try {
+                        sa.accept(si);
+                    } catch (Throwable e) {
+                        throw new AssertionError("Assertion failed for shortcut " + si.getId(), e);
+                    }
+                }
+            }
+            assertTrue("Shortcut with the given condition not found.", found);
+            return this;
+        }
+
+        public ShortcutListAsserter forShortcutWithId(String id, Consumer<ShortcutInfo> sa) {
+            forShortcut(si -> si.getId().equals(id), sa);
+
+            return this;
+        }
+    }
+
+    public static void assertBundlesEqual(BaseBundle b1, BaseBundle b2) {
+        if (b1 == null && b2 == null) {
+            return; // pass
+        }
+        assertNotNull("b1 is null but b2 is not", b1);
+        assertNotNull("b2 is null but b1 is not", b2);
+
+        // HashSet makes the error message readable.
+        assertEquals(set(b1.keySet()), set(b2.keySet()));
+
+        for (String key : b1.keySet()) {
+            final Object v1 = b1.get(key);
+            final Object v2 = b2.get(key);
+            if (v1 == null) {
+                if (v2 == null) {
+                    return;
+                }
+            }
+            if (v1.equals(v2)) {
+                return;
+            }
+
+            assertTrue("Only either value is null: key=" + key
+                    + " b1=" + b1 + " b2=" + b2, v1 != null && v2 != null);
+            assertEquals("Class mismatch: key=" + key, v1.getClass(), v2.getClass());
+
+            if (v1 instanceof BaseBundle) {
+                assertBundlesEqual((BaseBundle) v1, (BaseBundle) v2);
+
+            } else if (v1 instanceof boolean[]) {
+                assertTrue(Arrays.equals((boolean[]) v1, (boolean[]) v2));
+
+            } else if (v1 instanceof int[]) {
+                MoreAsserts.assertEquals((int[]) v1, (int[]) v2);
+
+            } else if (v1 instanceof double[]) {
+                MoreAsserts.assertEquals((double[]) v1, (double[]) v2);
+
+            } else if (v1 instanceof String[]) {
+                MoreAsserts.assertEquals((String[]) v1, (String[]) v2);
+
+            } else if (v1 instanceof Double) {
+                if (((Double) v1).isNaN()) {
+                    assertTrue(((Double) v2).isNaN());
+                } else {
+                    assertEquals(v1, v2);
+                }
+
+            } else {
+                assertEquals(v1, v2);
+            }
+        }
+    }
+
+    public static void waitOnMainThread() throws InterruptedException {
+        final CountDownLatch latch = new CountDownLatch(1);
+
+        new Handler(Looper.getMainLooper()).post(() -> latch.countDown());
+
+        latch.await();
+    }
+
+    public static class LauncherCallbackAsserter {
+        private final LauncherApps.Callback mCallback = mock(LauncherApps.Callback.class);
+
+        private Callback getMockCallback() {
+            return mCallback;
+        }
+
+        public LauncherCallbackAsserter assertNoCallbackCalled() {
+            verify(mCallback, times(0)).onShortcutsChanged(
+                    anyString(),
+                    any(List.class),
+                    any(UserHandle.class));
+            return this;
+        }
+
+        public LauncherCallbackAsserter assertNoCallbackCalledForPackage(
+                String publisherPackageName) {
+            verify(mCallback, times(0)).onShortcutsChanged(
+                    eq(publisherPackageName),
+                    any(List.class),
+                    any(UserHandle.class));
+            return this;
+        }
+
+        public LauncherCallbackAsserter assertNoCallbackCalledForPackageAndUser(
+                String publisherPackageName, UserHandle publisherUserHandle) {
+            verify(mCallback, times(0)).onShortcutsChanged(
+                    eq(publisherPackageName),
+                    any(List.class),
+                    eq(publisherUserHandle));
+            return this;
+        }
+
+        public ShortcutListAsserter assertCallbackCalledForPackageAndUser(
+                String publisherPackageName, UserHandle publisherUserHandle) {
+            final ArgumentCaptor<List> shortcuts = ArgumentCaptor.forClass(List.class);
+            verify(mCallback, times(1)).onShortcutsChanged(
+                    eq(publisherPackageName),
+                    shortcuts.capture(),
+                    eq(publisherUserHandle));
+            return new ShortcutListAsserter(shortcuts.getValue());
+        }
+    }
+
+    public static LauncherCallbackAsserter assertForLauncherCallback(
+            LauncherApps launcherApps, Runnable body) throws InterruptedException {
+        final LauncherCallbackAsserter asserter = new LauncherCallbackAsserter();
+        launcherApps.registerCallback(asserter.getMockCallback(),
+                new Handler(Looper.getMainLooper()));
+
+        body.run();
+
+        waitOnMainThread();
+
+        // TODO unregister doesn't work well during unit tests.  Figure out and fix it.
+        // launcherApps.unregisterCallback(asserter.getMockCallback());
+
+        return asserter;
+    }
+
+    public static void retryUntil(BooleanSupplier checker, String message) {
+        retryUntil(checker, message, 30);
+    }
+
+    public static void retryUntil(BooleanSupplier checker, String message, long timeoutSeconds) {
+        final long timeOut = System.currentTimeMillis() + timeoutSeconds * 1000;
+        while (!checker.getAsBoolean()) {
+            if (System.currentTimeMillis() > timeOut) {
+                break;
+            }
+            try {
+                Thread.sleep(200);
+            } catch (InterruptedException ignore) {
+            }
+        }
+        assertTrue(message, checker.getAsBoolean());
+    }
+}
diff --git a/services/usage/java/com/android/server/usage/AppIdleHistory.java b/services/usage/java/com/android/server/usage/AppIdleHistory.java
index a3313c9..f69dae4 100644
--- a/services/usage/java/com/android/server/usage/AppIdleHistory.java
+++ b/services/usage/java/com/android/server/usage/AppIdleHistory.java
@@ -118,7 +118,6 @@
         } else {
             mScreenOnDuration += elapsedRealtime - mScreenOnSnapshot;
             mElapsedDuration += elapsedRealtime - mElapsedSnapshot;
-            writeScreenOnTimeLocked();
             mElapsedSnapshot = elapsedRealtime;
         }
     }
@@ -167,7 +166,7 @@
     /**
      * To be called periodically to keep track of elapsed time when app idle times are written
      */
-    public void writeElapsedTimeLocked() {
+    public void writeAppIdleDurationsLocked() {
         final long elapsedRealtime = SystemClock.elapsedRealtime();
         // Only bump up and snapshot the elapsed time. Don't change screen on duration.
         mElapsedDuration += elapsedRealtime - mElapsedSnapshot;
diff --git a/services/usage/java/com/android/server/usage/UsageStatsService.java b/services/usage/java/com/android/server/usage/UsageStatsService.java
index ecfeff9..04104b5 100644
--- a/services/usage/java/com/android/server/usage/UsageStatsService.java
+++ b/services/usage/java/com/android/server/usage/UsageStatsService.java
@@ -94,7 +94,7 @@
 
     static final String TAG = "UsageStatsService";
 
-    static final boolean DEBUG = false;
+    static final boolean DEBUG = false; // Never submit with true
     static final boolean COMPRESS_TIME = false;
 
     private static final long TEN_SECONDS = 10 * 1000;
@@ -139,8 +139,8 @@
     long mSystemTimeSnapshot;
 
     boolean mAppIdleEnabled;
-    boolean mAppIdleParoled;
-    private boolean mScreenOn;
+    boolean mAppIdleTempParoled;
+    boolean mCharging;
     private long mLastAppIdleParoledTime;
 
     private volatile boolean mPendingOneTimeCheckIdleStates;
@@ -191,7 +191,7 @@
         mAppIdleEnabled = getContext().getResources().getBoolean(
                 com.android.internal.R.bool.config_enableAutoPowerModes);
         if (mAppIdleEnabled) {
-            IntentFilter deviceStates = new IntentFilter(BatteryManager.ACTION_CHARGING);
+            IntentFilter deviceStates = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
             deviceStates.addAction(BatteryManager.ACTION_DISCHARGING);
             deviceStates.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
             getContext().registerReceiver(new DeviceStateReceiver(), deviceStates);
@@ -237,7 +237,7 @@
 
             mSystemServicesReady = true;
         } else if (phase == PHASE_BOOT_COMPLETED) {
-            setAppIdleParoled(getContext().getSystemService(BatteryManager.class).isCharging());
+            setChargingState(getContext().getSystemService(BatteryManager.class).isCharging());
         }
     }
 
@@ -284,9 +284,8 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             final String action = intent.getAction();
-            if (BatteryManager.ACTION_CHARGING.equals(action)
-                    || BatteryManager.ACTION_DISCHARGING.equals(action)) {
-                setAppIdleParoled(BatteryManager.ACTION_CHARGING.equals(action));
+            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
+                setChargingState(intent.getIntExtra("plugged", 0) != 0);
             } else if (PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(action)) {
                 onDeviceIdleModeChanged();
             }
@@ -304,9 +303,9 @@
 
         @Override public void onDisplayChanged(int displayId) {
             if (displayId == Display.DEFAULT_DISPLAY) {
+                final boolean displayOn = isDisplayOn();
                 synchronized (UsageStatsService.this.mLock) {
-                    mAppIdleHistory.updateDisplayLocked(isDisplayOn(),
-                            SystemClock.elapsedRealtime());
+                    mAppIdleHistory.updateDisplayLocked(displayOn, SystemClock.elapsedRealtime());
                 }
             }
         }
@@ -376,12 +375,21 @@
         }
     }
 
+    void setChargingState(boolean charging) {
+        synchronized (mLock) {
+            if (mCharging != charging) {
+                mCharging = charging;
+                postParoleStateChanged();
+            }
+        }
+    }
+
     /** Paroled here means temporary pardon from being inactive */
     void setAppIdleParoled(boolean paroled) {
         synchronized (mLock) {
-            if (mAppIdleParoled != paroled) {
-                mAppIdleParoled = paroled;
-                if (DEBUG) Slog.d(TAG, "Changing paroled to " + mAppIdleParoled);
+            if (mAppIdleTempParoled != paroled) {
+                mAppIdleTempParoled = paroled;
+                if (DEBUG) Slog.d(TAG, "Changing paroled to " + mAppIdleTempParoled);
                 if (paroled) {
                     postParoleEndTimeout();
                 } else {
@@ -393,6 +401,12 @@
         }
     }
 
+    boolean isParoledOrCharging() {
+        synchronized (mLock) {
+            return mAppIdleTempParoled || mCharging;
+        }
+    }
+
     private void postNextParoleTimeout() {
         if (DEBUG) Slog.d(TAG, "Posting MSG_CHECK_PAROLE_TIMEOUT");
         mHandler.removeMessages(MSG_CHECK_PAROLE_TIMEOUT);
@@ -495,7 +509,7 @@
     /** Check if it's been a while since last parole and let idle apps do some work */
     void checkParoleTimeout() {
         synchronized (mLock) {
-            if (!mAppIdleParoled) {
+            if (!mAppIdleTempParoled) {
                 final long timeSinceLastParole = checkAndGetTimeLocked() - mLastAppIdleParoledTime;
                 if (timeSinceLastParole > mAppIdleParoleIntervalMillis) {
                     if (DEBUG) Slog.d(TAG, "Crossed default parole interval");
@@ -786,7 +800,7 @@
     }
 
     boolean isAppIdleFilteredOrParoled(String packageName, int userId, long elapsedRealtime) {
-        if (mAppIdleParoled) {
+        if (isParoledOrCharging()) {
             return false;
         }
         return isAppIdleFiltered(packageName, getAppId(packageName), userId, elapsedRealtime);
@@ -838,6 +852,10 @@
                     && mAppWidgetManager.isBoundWidgetPackage(packageName, userId)) {
                 return false;
             }
+
+            if (isDeviceProvisioningPackage(packageName)) {
+                return false;
+            }
         }
 
         if (!isAppIdleUnfiltered(packageName, userId, elapsedRealtime)) {
@@ -930,6 +948,16 @@
         return dpm.packageHasActiveAdmins(packageName, userId);
     }
 
+    /**
+     * Returns {@code true} if the supplied package is the device provisioning app. Otherwise,
+     * returns {@code false}.
+     */
+    private boolean isDeviceProvisioningPackage(String packageName) {
+        String deviceProvisioningPackage = getContext().getResources().getString(
+                com.android.internal.R.string.config_deviceProvisioningPackage);
+        return deviceProvisioningPackage != null && deviceProvisioningPackage.equals(packageName);
+    }
+
     private boolean isCarrierApp(String packageName) {
         synchronized (mLock) {
             if (!mHaveCarrierPrivilegedApps) {
@@ -975,8 +1003,9 @@
     }
 
     void informParoleStateChanged() {
+        final boolean paroled = isParoledOrCharging();
         for (AppIdleStateChangeListener listener : mPackageAccessListeners) {
-            listener.onParoleStateChanged(mAppIdleParoled);
+            listener.onParoleStateChanged(paroled);
         }
     }
 
@@ -991,9 +1020,9 @@
             service.persistActiveStats();
             mAppIdleHistory.writeAppIdleTimesLocked(mUserState.keyAt(i));
         }
-        // Persist elapsed time periodically, in case screen doesn't get toggled
-        // until the next boot
-        mAppIdleHistory.writeElapsedTimeLocked();
+        // Persist elapsed and screen on time. If this fails for whatever reason, the apps will be
+        // considered not-idle, which is the safest outcome in such an event.
+        mAppIdleHistory.writeAppIdleDurationsLocked();
         mHandler.removeMessages(MSG_FLUSH_TO_DISK);
     }
 
@@ -1058,9 +1087,9 @@
 
             pw.println();
             pw.print("mAppIdleEnabled="); pw.print(mAppIdleEnabled);
-            pw.print(" mAppIdleParoled="); pw.print(mAppIdleParoled);
-            pw.print(" mScreenOn="); pw.println(mScreenOn);
-            pw.print("mLastAppIdleParoledTime=");
+            pw.print(" mAppIdleTempParoled="); pw.print(mAppIdleTempParoled);
+            pw.print(" mCharging="); pw.print(mCharging);
+            pw.print(" mLastAppIdleParoledTime=");
             TimeUtils.formatDuration(mLastAppIdleParoledTime, pw);
             pw.println();
         }
@@ -1125,7 +1154,8 @@
                     break;
 
                 case MSG_PAROLE_STATE_CHANGED:
-                    if (DEBUG) Slog.d(TAG, "Parole state changed: " + mAppIdleParoled);
+                    if (DEBUG) Slog.d(TAG, "Parole state: " + mAppIdleTempParoled
+                            + ", Charging state:" + mCharging);
                     informParoleStateChanged();
                     break;
 
@@ -1412,6 +1442,24 @@
         }
 
         @Override
+        public void reportShortcutUsage(String packageName, String shortcutId, int userId) {
+            if (packageName == null || shortcutId == null) {
+                Slog.w(TAG, "Event reported without a package name or a shortcut ID");
+                return;
+            }
+
+            UsageEvents.Event event = new UsageEvents.Event();
+            event.mPackage = packageName.intern();
+            event.mShortcutId = shortcutId.intern();
+
+            // This will later be converted to system time.
+            event.mTimeStamp = SystemClock.elapsedRealtime();
+
+            event.mEventType = Event.SHORTCUT_INVOCATION;
+            mHandler.obtainMessage(MSG_REPORT_EVENT, userId, 0, event).sendToTarget();
+        }
+
+        @Override
         public void reportContentProviderUsage(String name, String packageName, int userId) {
             SomeArgs args = SomeArgs.obtain();
             args.arg1 = name;
@@ -1434,7 +1482,7 @@
 
         @Override
         public boolean isAppIdleParoleOn() {
-            return mAppIdleParoled;
+            return isParoledOrCharging();
         }
 
         @Override
diff --git a/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java b/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java
index c95ff23..03cee9c 100644
--- a/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java
+++ b/services/usage/java/com/android/server/usage/UsageStatsXmlV1.java
@@ -51,6 +51,7 @@
     private static final String ACTIVE_ATTR = "active";
     private static final String LAST_EVENT_ATTR = "lastEvent";
     private static final String TYPE_ATTR = "type";
+    private static final String SHORTCUT_ID_ATTR = "shortcutId";
 
     // Time attributes stored as an offset of the beginTime.
     private static final String LAST_TIME_ACTIVE_ATTR = "lastTimeActive";
@@ -106,9 +107,15 @@
         event.mTimeStamp = statsOut.beginTime + XmlUtils.readLongAttribute(parser, TIME_ATTR);
 
         event.mEventType = XmlUtils.readIntAttribute(parser, TYPE_ATTR);
-        if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE) {
-            event.mConfiguration = new Configuration();
-            Configuration.readXmlAttrs(parser, event.mConfiguration);
+        switch (event.mEventType) {
+            case UsageEvents.Event.CONFIGURATION_CHANGE:
+                event.mConfiguration = new Configuration();
+                Configuration.readXmlAttrs(parser, event.mConfiguration);
+                break;
+            case UsageEvents.Event.SHORTCUT_INVOCATION:
+                final String id = XmlUtils.readStringAttribute(parser, SHORTCUT_ID_ATTR);
+                event.mShortcutId = (id != null) ? id.intern() : null;
+                break;
         }
 
         if (statsOut.events == null) {
@@ -165,9 +172,17 @@
         }
         XmlUtils.writeIntAttribute(xml, TYPE_ATTR, event.mEventType);
 
-        if (event.mEventType == UsageEvents.Event.CONFIGURATION_CHANGE
-                && event.mConfiguration != null) {
-            Configuration.writeXmlAttrs(xml, event.mConfiguration);
+        switch (event.mEventType) {
+            case UsageEvents.Event.CONFIGURATION_CHANGE:
+                if (event.mConfiguration != null) {
+                    Configuration.writeXmlAttrs(xml, event.mConfiguration);
+                }
+                break;
+            case UsageEvents.Event.SHORTCUT_INVOCATION:
+                if (event.mShortcutId != null) {
+                    XmlUtils.writeStringAttribute(xml, SHORTCUT_ID_ATTR, event.mShortcutId);
+                }
+                break;
         }
 
         xml.endTag(null, EVENT_TAG);
diff --git a/services/usage/java/com/android/server/usage/UserUsageStatsService.java b/services/usage/java/com/android/server/usage/UserUsageStatsService.java
index 7d003f3..59e4c80 100644
--- a/services/usage/java/com/android/server/usage/UserUsageStatsService.java
+++ b/services/usage/java/com/android/server/usage/UserUsageStatsService.java
@@ -549,6 +549,9 @@
             if (event.mConfiguration != null) {
                 pw.printPair("config", Configuration.resourceQualifierString(event.mConfiguration));
             }
+            if (event.mShortcutId != null) {
+                pw.printPair("shortcutId", event.mShortcutId);
+            }
             pw.println();
         }
         pw.decreaseIndent();
@@ -588,6 +591,8 @@
                 return "SYSTEM_INTERACTION";
             case UsageEvents.Event.USER_INTERACTION:
                 return "USER_INTERACTION";
+            case UsageEvents.Event.SHORTCUT_INVOCATION:
+                return "SHORTCUT_INVOCATION";
             default:
                 return "UNKNOWN";
         }
diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
index df9242d..8560651 100644
--- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java
+++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java
@@ -49,6 +49,7 @@
 import android.util.Slog;
 
 import com.android.internal.annotations.GuardedBy;
+import com.android.internal.os.SomeArgs;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.server.FgThread;
 
@@ -320,6 +321,7 @@
         private boolean mConnected;
         private boolean mHostConnected;
         private boolean mSourcePower;
+        private boolean mSinkPower;
         private boolean mConfigured;
         private boolean mUsbDataUnlocked;
         private String mCurrentFunctions;
@@ -401,7 +403,18 @@
         public void updateHostState(UsbPort port, UsbPortStatus status) {
             boolean hostConnected = status.getCurrentDataRole() == UsbPort.DATA_ROLE_HOST;
             boolean sourcePower = status.getCurrentPowerRole() == UsbPort.POWER_ROLE_SOURCE;
-            obtainMessage(MSG_UPDATE_HOST_STATE, hostConnected ? 1 :0, sourcePower ? 1 :0).sendToTarget();
+            boolean sinkPower = status.getCurrentPowerRole() == UsbPort.POWER_ROLE_SINK;
+
+            if (DEBUG) {
+                Slog.i(TAG, "updateHostState " + port + " status=" + status);
+            }
+
+            SomeArgs args = SomeArgs.obtain();
+            args.argi1 = hostConnected ? 1 :0;
+            args.argi2 = sourcePower ? 1 :0;
+            args.argi3 = sinkPower ? 1 :0;
+
+            obtainMessage(MSG_UPDATE_HOST_STATE, args).sendToTarget();
         }
 
         private boolean waitForState(String state) {
@@ -718,8 +731,11 @@
                     }
                     break;
                 case MSG_UPDATE_HOST_STATE:
-                    mHostConnected = (msg.arg1 == 1);
-                    mSourcePower = (msg.arg2 == 1);
+                    SomeArgs args = (SomeArgs) msg.obj;
+                    mHostConnected = (args.argi1 == 1);
+                    mSourcePower = (args.argi2 == 1);
+                    mSinkPower = (args.argi3 == 1);
+                    args.recycle();
                     updateUsbNotification();
                     if (mBootCompleted) {
                         updateUsbStateBroadcastIfNeeded();
@@ -809,6 +825,8 @@
                 }
             } else if (mSourcePower) {
                 id = com.android.internal.R.string.usb_supplying_notification_title;
+            } else if (mHostConnected && mSinkPower) {
+                id = com.android.internal.R.string.usb_charging_notification_title;
             }
             if (id != mUsbNotificationId) {
                 // clear notification if title needs changing
@@ -908,6 +926,9 @@
             pw.println("  mConfigured: " + mConfigured);
             pw.println("  mUsbDataUnlocked: " + mUsbDataUnlocked);
             pw.println("  mCurrentAccessory: " + mCurrentAccessory);
+            pw.println("  mHostConnected: " + mHostConnected);
+            pw.println("  mSourcePower: " + mSourcePower);
+            pw.println("  mSinkPower: " + mSinkPower);
             try {
                 pw.println("  Kernel state: "
                         + FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim());
diff --git a/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerHelper.java b/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerHelper.java
index 3779d87..e89585c 100644
--- a/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerHelper.java
+++ b/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerHelper.java
@@ -696,8 +696,13 @@
         Slog.w(TAG, "Recognition aborted");
         MetricsLogger.count(mContext, "sth_recognition_aborted", 1);
         ModelData modelData = getModelDataForLocked(event.soundModelHandle);
-        if (modelData != null) {
+        if (modelData != null && modelData.isModelStarted()) {
             modelData.setStopped();
+            try {
+                modelData.getCallback().onRecognitionPaused();
+            } catch (RemoteException e) {
+                Slog.w(TAG, "RemoteException in onRecognitionPaused", e);
+            }
         }
     }
 
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java
index 0f68cca..0dcd152 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/DatabaseHelper.java
@@ -40,11 +40,12 @@
     static final boolean DBG = false;
 
     private static final String NAME = "sound_model.db";
-    private static final int VERSION = 4;
+    private static final int VERSION = 5;
 
     public static interface SoundModelContract {
         public static final String TABLE = "sound_model";
         public static final String KEY_MODEL_UUID = "model_uuid";
+        public static final String KEY_VENDOR_UUID = "vendor_uuid";
         public static final String KEY_KEYPHRASE_ID = "keyphrase_id";
         public static final String KEY_TYPE = "type";
         public static final String KEY_DATA = "data";
@@ -58,6 +59,7 @@
     private static final String CREATE_TABLE_SOUND_MODEL = "CREATE TABLE "
             + SoundModelContract.TABLE + "("
             + SoundModelContract.KEY_MODEL_UUID + " TEXT PRIMARY KEY,"
+            + SoundModelContract.KEY_VENDOR_UUID + " TEXT, "
             + SoundModelContract.KEY_KEYPHRASE_ID + " INTEGER,"
             + SoundModelContract.KEY_TYPE + " INTEGER,"
             + SoundModelContract.KEY_DATA + " BLOB,"
@@ -78,9 +80,19 @@
 
     @Override
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-        // TODO: For now, drop older tables and recreate new ones.
-        db.execSQL("DROP TABLE IF EXISTS " + SoundModelContract.TABLE);
-        onCreate(db);
+        if (oldVersion < 4) {
+            // For old versions just drop the tables and recreate new ones.
+            db.execSQL("DROP TABLE IF EXISTS " + SoundModelContract.TABLE);
+            onCreate(db);
+        } else {
+            // In the jump to version 5, we added support for the vendor UUID.
+            if (oldVersion == 4) {
+                Slog.d(TAG, "Adding vendor UUID column");
+                db.execSQL("ALTER TABLE " + SoundModelContract.TABLE + " ADD COLUMN "
+                        + SoundModelContract.KEY_VENDOR_UUID + " TEXT");
+                oldVersion++;
+            }
+        }
     }
 
     /**
@@ -93,6 +105,9 @@
             SQLiteDatabase db = getWritableDatabase();
             ContentValues values = new ContentValues();
             values.put(SoundModelContract.KEY_MODEL_UUID, soundModel.uuid.toString());
+            if (soundModel.vendorUuid != null) {
+                values.put(SoundModelContract.KEY_VENDOR_UUID, soundModel.vendorUuid.toString());
+            }
             values.put(SoundModelContract.KEY_TYPE, SoundTrigger.SoundModel.TYPE_KEYPHRASE);
             values.put(SoundModelContract.KEY_DATA, soundModel.data);
 
@@ -176,6 +191,11 @@
                             continue;
                         }
 
+                        String vendorUuidString = null;
+                        int vendorUuidColumn = c.getColumnIndex(SoundModelContract.KEY_VENDOR_UUID);
+                        if (vendorUuidColumn != -1) {
+                            vendorUuidString = c.getString(vendorUuidColumn);
+                        }
                         byte[] data = c.getBlob(c.getColumnIndex(SoundModelContract.KEY_DATA));
                         int recognitionModes = c.getInt(
                                 c.getColumnIndex(SoundModelContract.KEY_RECOGNITION_MODES));
@@ -212,9 +232,12 @@
                         Keyphrase[] keyphrases = new Keyphrase[1];
                         keyphrases[0] = new Keyphrase(
                                 keyphraseId, recognitionModes, modelLocale, text, users);
+                        UUID vendorUuid = null;
+                        if (vendorUuidString != null) {
+                            vendorUuid = UUID.fromString(vendorUuidString);
+                        }
                         KeyphraseSoundModel model = new KeyphraseSoundModel(
-                                UUID.fromString(modelUuid),
-                                null /* FIXME use vendor UUID */, data, keyphrases);
+                                UUID.fromString(modelUuid), vendorUuid, data, keyphrases);
                         if (DBG) {
                             Slog.d(TAG, "Found SoundModel for the given keyphrase/locale/user: "
                                     + model);
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
index 23c58fe..a04034e 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
@@ -41,6 +41,7 @@
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Parcel;
+import android.os.RemoteCallbackList;
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.provider.Settings;
@@ -55,15 +56,16 @@
 import android.util.Log;
 import android.util.Slog;
 
+import com.android.internal.app.IVoiceInteractionSessionListener;
 import com.android.internal.app.IVoiceInteractionManagerService;
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
 import com.android.internal.app.IVoiceInteractor;
 import com.android.internal.content.PackageMonitor;
 import com.android.internal.os.BackgroundThread;
 import com.android.server.LocalServices;
-import com.android.server.soundtrigger.SoundTriggerInternal;
 import com.android.server.SystemService;
 import com.android.server.UiThread;
+import com.android.server.soundtrigger.SoundTriggerInternal;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
@@ -84,6 +86,9 @@
     final TreeSet<Integer> mLoadedKeyphraseIds;
     SoundTriggerInternal mSoundTriggerInternal;
 
+    private final RemoteCallbackList<IVoiceInteractionSessionListener>
+            mVoiceInteractionSessionListeners = new RemoteCallbackList<>();
+
     public VoiceInteractionManagerService(Context context) {
         super(context);
         mContext = context;
@@ -530,6 +535,18 @@
                     + " user=" + userHandle);
         }
 
+        ComponentName getCurAssistant(int userHandle) {
+            String curAssistant = Settings.Secure.getStringForUser(
+                    mContext.getContentResolver(),
+                    Settings.Secure.ASSISTANT, userHandle);
+            if (TextUtils.isEmpty(curAssistant)) {
+                return null;
+            }
+            if (DEBUG) Slog.d(TAG, "getCurAssistant curAssistant=" + curAssistant
+                    + " user=" + userHandle);
+            return ComponentName.unflattenFromString(curAssistant);
+        }
+
         void resetCurAssistant(int userHandle) {
             Settings.Secure.putStringForUser(mContext.getContentResolver(),
                     Settings.Secure.ASSISTANT, null, userHandle);
@@ -1038,6 +1055,48 @@
         }
 
         @Override
+        public void registerVoiceInteractionSessionListener(
+                IVoiceInteractionSessionListener listener) {
+            enforceCallingPermission(Manifest.permission.ACCESS_VOICE_INTERACTION_SERVICE);
+            synchronized (this) {
+                mVoiceInteractionSessionListeners.register(listener);
+            }
+        }
+
+        public void onSessionShown() {
+            synchronized (this) {
+                final int size = mVoiceInteractionSessionListeners.beginBroadcast();
+                for (int i = 0; i < size; ++i) {
+                    final IVoiceInteractionSessionListener listener =
+                            mVoiceInteractionSessionListeners.getBroadcastItem(i);
+                    try {
+                        listener.onVoiceSessionShown();
+                    } catch (RemoteException e) {
+                        Slog.e(TAG, "Error delivering voice interaction open event.", e);
+                    }
+                }
+                mVoiceInteractionSessionListeners.finishBroadcast();
+            }
+        }
+
+        public void onSessionHidden() {
+            synchronized (this) {
+                final int size = mVoiceInteractionSessionListeners.beginBroadcast();
+                for (int i = 0; i < size; ++i) {
+                    final IVoiceInteractionSessionListener listener =
+                            mVoiceInteractionSessionListeners.getBroadcastItem(i);
+                    try {
+                        listener.onVoiceSessionHidden();
+
+                    } catch (RemoteException e) {
+                        Slog.e(TAG, "Error delivering voice interaction closed event.", e);
+                    }
+                }
+                mVoiceInteractionSessionListeners.finishBroadcast();
+            }
+        }
+
+        @Override
         public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
             if (mContext.checkCallingOrSelfPermission(Manifest.permission.DUMP)
                     != PackageManager.PERMISSION_GRANTED) {
@@ -1131,6 +1190,7 @@
                 synchronized (VoiceInteractionManagerServiceStub.this) {
                     ComponentName curInteractor = getCurInteractor(userHandle);
                     ComponentName curRecognizer = getCurRecognizer(userHandle);
+                    ComponentName curAssistant = getCurAssistant(userHandle);
                     if (curRecognizer == null) {
                         // Could a new recognizer appear when we don't have one pre-installed?
                         if (anyPackagesAppearing()) {
@@ -1149,6 +1209,7 @@
                             // the default config.
                             setCurInteractor(null, userHandle);
                             setCurRecognizer(null, userHandle);
+                            resetCurAssistant(userHandle);
                             initForUser(userHandle);
                             return;
                         }
@@ -1165,6 +1226,20 @@
                         return;
                     }
 
+                    if (curAssistant != null) {
+                        int change = isPackageDisappearing(curAssistant.getPackageName());
+                        if (change == PACKAGE_PERMANENT_CHANGE) {
+                            // If the currently set assistant is being removed, then we should
+                            // reset back to the default state (which is probably that we prefer
+                            // to have the default full voice interactor enabled).
+                            setCurInteractor(null, userHandle);
+                            setCurRecognizer(null, userHandle);
+                            resetCurAssistant(userHandle);
+                            initForUser(userHandle);
+                            return;
+                        }
+                    }
+
                     // There is no interactor, so just deal with a simple recognizer.
                     int change = isPackageDisappearing(curRecognizer.getPackageName());
                     if (change == PACKAGE_PERMANENT_CHANGE
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
index 3f9da4c..a46ccee 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
@@ -41,6 +41,7 @@
 import android.util.Slog;
 import android.view.IWindowManager;
 
+import com.android.internal.app.IVoiceInteractionSessionListener;
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
 import com.android.internal.app.IVoiceInteractor;
 import com.android.server.LocalServices;
@@ -58,7 +59,7 @@
 
     final Context mContext;
     final Handler mHandler;
-    final Object mLock;
+    final VoiceInteractionManagerService.VoiceInteractionManagerServiceStub mServiceStub;
     final int mUser;
     final ComponentName mComponent;
     final IActivityManager mAm;
@@ -77,7 +78,7 @@
             if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
                 String reason = intent.getStringExtra("reason");
                 if (!CLOSE_REASON_VOICE_INTERACTION.equals(reason) && !"dream".equals(reason)) {
-                    synchronized (mLock) {
+                    synchronized (mServiceStub) {
                         if (mActiveSession != null && mActiveSession.mSession != null) {
                             try {
                                 mActiveSession.mSession.closeSystemDialogs();
@@ -93,7 +94,7 @@
     final ServiceConnection mConnection = new ServiceConnection() {
         @Override
         public void onServiceConnected(ComponentName name, IBinder service) {
-            synchronized (mLock) {
+            synchronized (mServiceStub) {
                 mService = IVoiceInteractionService.Stub.asInterface(service);
                 try {
                     mService.ready();
@@ -108,11 +109,12 @@
         }
     };
 
-    VoiceInteractionManagerServiceImpl(Context context, Handler handler, Object lock,
+    VoiceInteractionManagerServiceImpl(Context context, Handler handler,
+            VoiceInteractionManagerService.VoiceInteractionManagerServiceStub stub,
             int userHandle, ComponentName service) {
         mContext = context;
         mHandler = handler;
-        mLock = lock;
+        mServiceStub = stub;
         mUser = userHandle;
         mComponent = service;
         mAm = ActivityManagerNative.getDefault();
@@ -148,8 +150,9 @@
     public boolean showSessionLocked(Bundle args, int flags,
             IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) {
         if (mActiveSession == null) {
-            mActiveSession = new VoiceInteractionSessionConnection(mLock, mSessionComponentName,
-                    mUser, mContext, this, mInfo.getServiceInfo().applicationInfo.uid, mHandler);
+            mActiveSession = new VoiceInteractionSessionConnection(mServiceStub,
+                    mSessionComponentName, mUser, mContext, this,
+                    mInfo.getServiceInfo().applicationInfo.uid, mHandler);
         }
         List<IBinder> activityTokens = null;
         if (activityToken == null) {
@@ -355,8 +358,18 @@
 
     @Override
     public void sessionConnectionGone(VoiceInteractionSessionConnection connection) {
-        synchronized (mLock) {
+        synchronized (mServiceStub) {
             finishLocked(connection.mToken, false);
         }
     }
+
+    @Override
+    public void onSessionShown(VoiceInteractionSessionConnection connection) {
+        mServiceStub.onSessionShown();
+    }
+
+    @Override
+    public void onSessionHidden(VoiceInteractionSessionConnection connection) {
+        mServiceStub.onSessionHidden();
+    }
 }
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
index 0694911..c3075b3 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
@@ -46,6 +46,7 @@
 import android.view.IWindowManager;
 import android.view.WindowManager;
 
+import com.android.internal.app.AssistUtils;
 import com.android.internal.app.IAssistScreenshotReceiver;
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
 import com.android.internal.app.IVoiceInteractor;
@@ -130,6 +131,8 @@
 
     public interface Callback {
         public void sessionConnectionGone(VoiceInteractionSessionConnection connection);
+        public void onSessionShown(VoiceInteractionSessionConnection connection);
+        public void onSessionHidden(VoiceInteractionSessionConnection connection);
     }
 
     final ServiceConnection mFullConnection = new ServiceConnection() {
@@ -299,7 +302,7 @@
             } else {
                 mScreenshot = null;
             }
-            if (needDisclosure) {
+            if (needDisclosure && AssistUtils.shouldDisclose(mContext, mSessionComponentName)) {
                 mHandler.post(mShowAssistDisclosureRunnable);
             }
             if (mSession != null) {
@@ -313,6 +316,7 @@
             } else if (showCallback != null) {
                 mPendingShowCallbacks.add(showCallback);
             }
+            mCallback.onSessionShown(this);
             return true;
         }
         if (showCallback != null) {
@@ -468,6 +472,7 @@
                     } catch (RemoteException e) {
                     }
                 }
+                mCallback.onSessionHidden(this);
             }
             if (mFullyBound) {
                 mContext.unbindService(mFullConnection);
diff --git a/telecomm/java/android/telecom/Call.java b/telecomm/java/android/telecom/Call.java
index 7d7e1eb..62625bdf 100644
--- a/telecomm/java/android/telecom/Call.java
+++ b/telecomm/java/android/telecom/Call.java
@@ -23,6 +23,7 @@
 
 import java.lang.String;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -104,7 +105,6 @@
      * An {@link InCallService} will only see this state if it has the
      * {@link TelecomManager#METADATA_INCLUDE_EXTERNAL_CALLS} metadata set to {@code true} in its
      * manifest.
-     * @hide
      */
     public static final int STATE_PULLING_CALL = 11;
 
@@ -252,7 +252,6 @@
          * <p>
          * See {@link Connection#CAPABILITY_CAN_PULL_CALL} and
          * {@link Connection#PROPERTY_IS_EXTERNAL_CALL}.
-         * @hide
          */
         public static final int CAPABILITY_CAN_PULL_CALL = 0x00800000;
 
@@ -305,10 +304,14 @@
          * in its manifest.
          * <p>
          * See {@link Connection#PROPERTY_IS_EXTERNAL_CALL}.
-         * @hide
          */
         public static final int PROPERTY_IS_EXTERNAL_CALL = 0x00000040;
 
+        /**
+         * Indicates that the call has CDMA Enhanced Voice Privacy enabled.
+         */
+        public static final int PROPERTY_HAS_CDMA_VOICE_PRIVACY = 0x00000080;
+
         //******************************************************************************************
         // Next PROPERTY value: 0x00000100
         //******************************************************************************************
@@ -465,6 +468,9 @@
             if (hasProperty(properties, PROPERTY_IS_EXTERNAL_CALL)) {
                 builder.append(" PROPERTY_IS_EXTERNAL_CALL");
             }
+            if(hasProperty(properties, PROPERTY_HAS_CDMA_VOICE_PRIVACY)) {
+                builder.append(" PROPERTY_HAS_CDMA_VOICE_PRIVACY");
+            }
             builder.append("]");
             return builder.toString();
         }
@@ -695,6 +701,24 @@
         }
     }
 
+    /**
+     * Defines callbacks which inform the {@link InCallService} of changes to a {@link Call}.
+     * These callbacks can originate from the Telecom framework, or a {@link ConnectionService}
+     * implementation.
+     * <p>
+     * You can handle these callbacks by extending the {@link Callback} class and overriding the
+     * callbacks that your {@link InCallService} is interested in.  The callback methods include the
+     * {@link Call} for which the callback applies, allowing reuse of a single instance of your
+     * {@link Callback} implementation, if desired.
+     * <p>
+     * Use {@link Call#registerCallback(Callback)} to register your callback(s).  Ensure
+     * {@link Call#unregisterCallback(Callback)} is called when you no longer require callbacks
+     * (typically in {@link InCallService#onCallRemoved(Call)}).
+     * Note: Callbacks which occur before you call {@link Call#registerCallback(Callback)} will not
+     * reach your implementation of {@link Callback}, so it is important to register your callback
+     * as soon as your {@link InCallService} is notified of a new call via
+     * {@link InCallService#onCallAdded(Call)}.
+     */
     public static abstract class Callback {
         /**
          * Invoked when the state of this {@code Call} has changed. See {@link #getState()}.
@@ -779,14 +803,19 @@
         public void onConferenceableCallsChanged(Call call, List<Call> conferenceableCalls) {}
 
         /**
-         * Invoked when a call receives an event from its associated {@link Connection}.
+         * Invoked when a {@link Call} receives an event from its associated {@link Connection}.
+         * <p>
+         * Where possible, the Call should make an attempt to handle {@link Connection} events which
+         * are part of the {@code android.telecom.*} namespace.  The Call should ignore any events
+         * it does not wish to handle.  Unexpected events should be handled gracefully, as it is
+         * possible that a {@link ConnectionService} has defined its own Connection events which a
+         * Call is not aware of.
          * <p>
          * See {@link Connection#sendConnectionEvent(String, Bundle)}.
          *
          * @param call The {@code Call} receiving the event.
          * @param event The event.
          * @param extras Extras associated with the connection event.
-         * @hide
          */
         public void onConnectionEvent(Call call, String event, Bundle extras) {}
     }
@@ -965,7 +994,6 @@
      * An {@link InCallService} will only see calls which support this method if it has the
      * {@link TelecomManager#METADATA_INCLUDE_EXTERNAL_CALLS} metadata set to {@code true}
      * in its manifest.
-     * @hide
      */
     public void pullExternalCall() {
         // If this isn't an external call, ignore the request.
@@ -980,15 +1008,35 @@
      * Sends a {@code Call} event from this {@code Call} to the associated {@link Connection} in
      * the {@link ConnectionService}.
      * <p>
+     * Call events are used to communicate point in time information from an {@link InCallService}
+     * to a {@link ConnectionService}.  A {@link ConnectionService} implementation could define
+     * events which enable the {@link InCallService}, for example, toggle a unique feature of the
+     * {@link ConnectionService}.
+     * <p>
+     * A {@link ConnectionService} can communicate to the {@link InCallService} using
+     * {@link Connection#sendConnectionEvent(String, Bundle)}.
+     * <p>
      * Events are exposed to {@link ConnectionService} implementations via
      * {@link android.telecom.Connection#onCallEvent(String, Bundle)}.
      * <p>
      * No assumptions should be made as to how a {@link ConnectionService} will handle these events.
-     * Events should be fully qualified (e.g., com.example.event.MY_EVENT) to avoid conflicts.
+     * The {@link InCallService} must assume that the {@link ConnectionService} could chose to
+     * ignore some events altogether.
+     * <p>
+     * Events should be fully qualified (e.g., {@code com.example.event.MY_EVENT}) to avoid
+     * conflicts between {@link InCallService} implementations.  Further, {@link InCallService}
+     * implementations shall not re-purpose events in the {@code android.*} namespace, nor shall
+     * they define their own event types in this namespace.  When defining a custom event type,
+     * ensure the contents of the extras {@link Bundle} is clearly defined.  Extra keys for this
+     * bundle should be named similar to the event type (e.g. {@code com.example.extra.MY_EXTRA}).
+     * <p>
+     * When defining events and the associated extras, it is important to keep their behavior
+     * consistent when the associated {@link InCallService} is updated.  Support for deprecated
+     * events/extras should me maintained to ensure backwards compatibility with older
+     * {@link ConnectionService} implementations which were built to support the older behavior.
      *
      * @param event The connection event.
      * @param extras Bundle containing extra information associated with the event.
-     * @hide
      */
     public void sendCallEvent(String event, Bundle extras) {
         mInCallAdapter.sendCallEvent(mTelecomCallId, event, extras);
@@ -1002,7 +1050,6 @@
      * extras.  Keys should be fully qualified (e.g., com.example.MY_EXTRA) to avoid conflicts.
      *
      * @param extras The extras to add.
-     * @hide
      */
     public final void putExtras(Bundle extras) {
         if (extras == null) {
@@ -1032,7 +1079,7 @@
     }
 
     /**
-     * Adds an integer extra to this {@code Connection}.
+     * Adds an integer extra to this {@link Call}.
      *
      * @param key The extra key.
      * @param value The value.
@@ -1047,7 +1094,7 @@
     }
 
     /**
-     * Adds a string extra to this {@code Connection}.
+     * Adds a string extra to this {@link Call}.
      *
      * @param key The extra key.
      * @param value The value.
@@ -1062,10 +1109,9 @@
     }
 
     /**
-     * Removes extras from this {@code Connection}.
+     * Removes extras from this {@link Call}.
      *
      * @param keys The keys of the extras to remove.
-     * @hide
      */
     public final void removeExtras(List<String> keys) {
         if (mExtras != null) {
@@ -1080,6 +1126,15 @@
     }
 
     /**
+     * Removes extras from this {@link Call}.
+     *
+     * @param keys The keys of the extras to remove.
+     */
+    public final void removeExtras(String ... keys) {
+        removeExtras(Arrays.asList(keys));
+    }
+
+    /**
      * Obtains the parent of this {@code Call} in a conference, if any.
      *
      * @return The parent {@code Call}, or {@code null} if this {@code Call} is not a
diff --git a/telecomm/java/android/telecom/Conference.java b/telecomm/java/android/telecom/Conference.java
index 0227d27..a012082 100644
--- a/telecomm/java/android/telecom/Conference.java
+++ b/telecomm/java/android/telecom/Conference.java
@@ -24,6 +24,7 @@
 import android.util.ArraySet;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Locale;
@@ -164,7 +165,6 @@
      * {@link Connection} for valid values.
      *
      * @return A bitmask of the properties of the conference call.
-     * @hide
      */
     public final int getConnectionProperties() {
         return mConnectionProperties;
@@ -256,60 +256,63 @@
     }
 
     /**
-     * Invoked when the Conference and all it's {@link Connection}s should be disconnected.
+     * Notifies the {@link Conference} when the Conference and all it's {@link Connection}s should
+     * be disconnected.
      */
     public void onDisconnect() {}
 
     /**
-     * Invoked when the specified {@link Connection} should be separated from the conference call.
+     * Notifies the {@link Conference} when the specified {@link Connection} should be separated
+     * from the conference call.
      *
      * @param connection The connection to separate.
      */
     public void onSeparate(Connection connection) {}
 
     /**
-     * Invoked when the specified {@link Connection} should merged with the conference call.
+     * Notifies the {@link Conference} when the specified {@link Connection} should merged with the
+     * conference call.
      *
      * @param connection The {@code Connection} to merge.
      */
     public void onMerge(Connection connection) {}
 
     /**
-     * Invoked when the conference should be put on hold.
+     * Notifies the {@link Conference} when it should be put on hold.
      */
     public void onHold() {}
 
     /**
-     * Invoked when the conference should be moved from hold to active.
+     * Notifies the {@link Conference} when it should be moved from a held to active state.
      */
     public void onUnhold() {}
 
     /**
-     * Invoked when the child calls should be merged. Only invoked if the conference contains the
-     * capability {@link Connection#CAPABILITY_MERGE_CONFERENCE}.
+     * Notifies the {@link Conference} when the child calls should be merged.  Only invoked if the
+     * conference contains the capability {@link Connection#CAPABILITY_MERGE_CONFERENCE}.
      */
     public void onMerge() {}
 
     /**
-     * Invoked when the child calls should be swapped. Only invoked if the conference contains the
-     * capability {@link Connection#CAPABILITY_SWAP_CONFERENCE}.
+     * Notifies the {@link Conference} when the child calls should be swapped. Only invoked if the
+     * conference contains the capability {@link Connection#CAPABILITY_SWAP_CONFERENCE}.
      */
     public void onSwap() {}
 
     /**
-     * Notifies this conference of a request to play a DTMF tone.
+     * Notifies the {@link Conference} of a request to play a DTMF tone.
      *
      * @param c A DTMF character.
      */
     public void onPlayDtmfTone(char c) {}
 
     /**
-     * Notifies this conference of a request to stop any currently playing DTMF tones.
+     * Notifies the {@link Conference} of a request to stop any currently playing DTMF tones.
      */
     public void onStopDtmfTone() {}
 
     /**
-     * Notifies this conference that the {@link #getAudioState()} property has a new value.
+     * Notifies the {@link Conference} that the {@link #getAudioState()} property has a new value.
      *
      * @param state The new call audio state.
      * @deprecated Use {@link #onCallAudioStateChanged(CallAudioState)} instead.
@@ -320,14 +323,15 @@
     public void onAudioStateChanged(AudioState state) {}
 
     /**
-     * Notifies this conference that the {@link #getCallAudioState()} property has a new value.
+     * Notifies the {@link Conference} that the {@link #getCallAudioState()} property has a new
+     * value.
      *
      * @param state The new call audio state.
      */
     public void onCallAudioStateChanged(CallAudioState state) {}
 
     /**
-     * Notifies this conference that a connection has been added to it.
+     * Notifies the {@link Conference} that a {@link Connection} has been added to it.
      *
      * @param connection The newly added connection.
      */
@@ -396,7 +400,6 @@
      * {@link Connection} for valid values.
      *
      * @param connectionProperties A bitmask of the {@code Properties} of the conference call.
-     * @hide
      */
     public final void setConnectionProperties(int connectionProperties) {
         if (connectionProperties != mConnectionProperties) {
@@ -681,8 +684,11 @@
      * New or existing keys are replaced in the {@code Conference} extras.  Keys which are no longer
      * in the new extras, but were present the last time {@code setExtras} was called are removed.
      * <p>
+     * Alternatively you may use the {@link #putExtras(Bundle)}, and
+     * {@link #removeExtras(String...)} methods to modify the extras.
+     * <p>
      * No assumptions should be made as to how an In-Call UI or service will handle these extras.
-     * Keys should be fully qualified (e.g., com.example.MY_EXTRA) to avoid conflicts.
+     * Keys should be fully qualified (e.g., com.example.extras.MY_EXTRA) to avoid conflicts.
      *
      * @param extras The extras associated with this {@code Conference}.
      */
@@ -727,7 +733,6 @@
      * Keys should be fully qualified (e.g., com.example.MY_EXTRA) to avoid conflicts.
      *
      * @param extras The extras to add.
-     * @hide
      */
     public final void putExtras(@NonNull Bundle extras) {
         if (extras == null) {
@@ -790,10 +795,9 @@
     }
 
     /**
-     * Removes an extra from this {@link Conference}.
+     * Removes extras from this {@link Conference}.
      *
-     * @param keys The key of the extra key to remove.
-     * @hide
+     * @param keys The keys of the extras to remove.
      */
     public final void removeExtras(List<String> keys) {
         if (keys == null || keys.isEmpty()) {
@@ -815,7 +819,25 @@
     }
 
     /**
+     * Removes extras from this {@link Conference}.
+     *
+     * @param keys The keys of the extras to remove.
+     */
+    public final void removeExtras(String ... keys) {
+        removeExtras(Arrays.asList(keys));
+    }
+
+    /**
      * Returns the extras associated with this conference.
+     * <p>
+     * Extras should be updated using {@link #putExtras(Bundle)} and {@link #removeExtras(List)}.
+     * <p>
+     * Telecom or an {@link InCallService} can also update the extras via
+     * {@link android.telecom.Call#putExtras(Bundle)}, and
+     * {@link Call#removeExtras(List)}.
+     * <p>
+     * The conference is notified of changes to the extras made by Telecom or an
+     * {@link InCallService} by {@link #onExtrasChanged(Bundle)}.
      *
      * @return The extras associated with this connection.
      */
@@ -832,7 +854,6 @@
      * {@link Call#removeExtras(List)}.
      *
      * @param extras The new extras bundle.
-     * @hide
      */
     public void onExtrasChanged(Bundle extras) {}
 
diff --git a/telecomm/java/android/telecom/ConferenceParticipant.java b/telecomm/java/android/telecom/ConferenceParticipant.java
index db0f151..20b04eb 100644
--- a/telecomm/java/android/telecom/ConferenceParticipant.java
+++ b/telecomm/java/android/telecom/ConferenceParticipant.java
@@ -114,13 +114,13 @@
     public String toString() {
         StringBuilder sb = new StringBuilder();
         sb.append("[ConferenceParticipant Handle: ");
-        sb.append(mHandle);
+        sb.append(Log.pii(mHandle));
         sb.append(" DisplayName: ");
-        sb.append(mDisplayName);
+        sb.append(Log.pii(mDisplayName));
         sb.append(" Endpoint: ");
-        sb.append(mEndpoint);
+        sb.append(Log.pii(mEndpoint));
         sb.append(" State: ");
-        sb.append(mState);
+        sb.append(Connection.stateToString(mState));
         sb.append("]");
         return sb.toString();
     }
diff --git a/telecomm/java/android/telecom/Connection.java b/telecomm/java/android/telecom/Connection.java
index ff220f3..c006185 100644
--- a/telecomm/java/android/telecom/Connection.java
+++ b/telecomm/java/android/telecom/Connection.java
@@ -35,6 +35,7 @@
 import android.view.Surface;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
@@ -52,6 +53,37 @@
  * Implementations are then responsible for updating the state of the {@code Connection}, and
  * must call {@link #destroy()} to signal to the framework that the {@code Connection} is no
  * longer used and associated resources may be recovered.
+ * <p>
+ * Subclasses of {@code Connection} override the {@code on*} methods to provide the the
+ * {@link ConnectionService}'s implementation of calling functionality.  The {@code on*} methods are
+ * called by Telecom to inform an instance of a {@code Connection} of actions specific to that
+ * {@code Connection} instance.
+ * <p>
+ * Basic call support requires overriding the following methods: {@link #onAnswer()},
+ * {@link #onDisconnect()}, {@link #onReject()}, {@link #onAbort()}
+ * <p>
+ * Where a {@code Connection} has {@link #CAPABILITY_SUPPORT_HOLD}, the {@link #onHold()} and
+ * {@link #onUnhold()} methods should be overridden to provide hold support for the
+ * {@code Connection}.
+ * <p>
+ * Where a {@code Connection} supports a variation of video calling (e.g. the
+ * {@code CAPABILITY_SUPPORTS_VT_*} capability bits), {@link #onAnswer(int)} should be overridden
+ * to support answering a call as a video call.
+ * <p>
+ * Where a {@code Connection} has {@link #PROPERTY_IS_EXTERNAL_CALL} and
+ * {@link #CAPABILITY_CAN_PULL_CALL}, {@link #onPullExternalCall()} should be overridden to provide
+ * support for pulling the external call.
+ * <p>
+ * Where a {@code Connection} supports conference calling {@link #onSeparate()} should be
+ * overridden.
+ * <p>
+ * There are a number of other {@code on*} methods which a {@code Connection} can choose to
+ * implement, depending on whether it is concerned with the associated calls from Telecom.  If,
+ * for example, call events from a {@link InCallService} are handled,
+ * {@link #onCallEvent(String, Bundle)} should be overridden.  Another example is
+ * {@link #onExtrasChanged(Bundle)}, which should be overridden if the {@code Connection} wishes to
+ * make use of extra information provided via the {@link Call#putExtras(Bundle)} and
+ * {@link Call#removeExtras(String...)} methods.
  */
 public abstract class Connection extends Conferenceable {
 
@@ -100,7 +132,6 @@
      * <p>
      * A connection can only be in this state if the {@link #PROPERTY_IS_EXTERNAL_CALL} property and
      * {@link #CAPABILITY_CAN_PULL_CALL} capability bits are set on the connection.
-     * @hide
      */
     public static final int STATE_PULLING_CALL = 7;
 
@@ -284,7 +315,6 @@
      * <p>
      * Should only be set on a {@code Connection} where {@link #PROPERTY_IS_EXTERNAL_CALL}
      * is set.
-     * @hide
      */
     public static final int CAPABILITY_CAN_PULL_CALL = 0x01000000;
 
@@ -297,7 +327,7 @@
      *
      * @hide
      */
-    public static final int PROPERTY_SHOW_CALLBACK_NUMBER = 1<<0;
+    public static final int PROPERTY_EMERGENCY_CALLBACK_MODE = 1<<0;
 
     /**
      * Whether the call is a generic conference, where we do not know the precise state of
@@ -332,13 +362,22 @@
      * external connections.  Only those {@link InCallService}s which have the
      * {@link TelecomManager#METADATA_INCLUDE_EXTERNAL_CALLS} metadata set to {@code true} in its
      * manifest will see external connections.
-     * @hide
      */
     public static final int PROPERTY_IS_EXTERNAL_CALL = 1<<4;
 
+    /**
+     * Indicates that the connection has CDMA Enhanced Voice Privacy enabled.
+     */
+    public static final int PROPERTY_HAS_CDMA_VOICE_PRIVACY = 1<<5;
+
+    /**
+     * Indicates that the connection represents a downgraded IMS conference.
+     * @hide
+     */
+    public static final int PROPERTY_IS_DOWNGRADED_CONFERENCE = 1<<6;
 
     //**********************************************************************************************
-    // Next PROPERTY value: 1<<5
+    // Next PROPERTY value: 1<<7
     //**********************************************************************************************
 
     /**
@@ -365,9 +404,25 @@
     public static final String EXTRA_CALL_SUBJECT = "android.telecom.extra.CALL_SUBJECT";
 
     /**
+     * Boolean connection extra key set on a {@link Connection} in
+     * {@link Connection#STATE_RINGING} state to indicate that answering the call will cause the
+     * current active foreground call to be dropped.
+     */
+    public static final String EXTRA_ANSWERING_DROPS_FG_CALL =
+            "android.telecom.extra.ANSWERING_DROPS_FG_CALL";
+
+    /**
+     * Boolean connection extra key on a {@link Connection} which indicates that adding an
+     * additional call is disallowed.
+     * @hide
+     */
+    public static final String EXTRA_DISABLE_ADD_CALL =
+            "android.telecom.extra.DISABLE_ADD_CALL";
+
+    /**
      * Connection event used to inform Telecom that it should play the on hold tone.  This is used
      * to play a tone when the peer puts the current call on hold.  Sent to Telecom via
-     * {@link #sendConnectionEvent(String)}.
+     * {@link #sendConnectionEvent(String, Bundle)}.
      * @hide
      */
     public static final String EVENT_ON_HOLD_TONE_START =
@@ -376,7 +431,7 @@
     /**
      * Connection event used to inform Telecom that it should stop the on hold tone.  This is used
      * to stop a tone when the peer puts the current call on hold.  Sent to Telecom via
-     * {@link #sendConnectionEvent(String)}.
+     * {@link #sendConnectionEvent(String, Bundle)}.
      * @hide
      */
     public static final String EVENT_ON_HOLD_TONE_END =
@@ -391,10 +446,47 @@
      * {@link Call.Details#PROPERTY_IS_EXTERNAL_CALL} and
      * {@link Call.Details#CAPABILITY_CAN_PULL_CALL}, but the {@link ConnectionService} could not
      * pull the external call due to an error condition.
-     * @hide
+     * <p>
+     * Sent via {@link #sendConnectionEvent(String, Bundle)}.  The {@link Bundle} parameter is
+     * expected to be null when this connection event is used.
      */
     public static final String EVENT_CALL_PULL_FAILED = "android.telecom.event.CALL_PULL_FAILED";
 
+    /**
+     * Connection event used to inform {@link InCallService}s when the merging of two calls has
+     * failed. The User Interface should use this message to inform the user of the error.
+     * <p>
+     * Sent via {@link #sendConnectionEvent(String, Bundle)}.  The {@link Bundle} parameter is
+     * expected to be null when this connection event is used.
+     */
+    public static final String EVENT_CALL_MERGE_FAILED = "android.telecom.event.CALL_MERGE_FAILED";
+
+    /**
+     * Connection event used to inform {@link InCallService}s when a call has been put on hold by
+     * the remote party.
+     * <p>
+     * This is different than the {@link Connection#STATE_HOLDING} state which indicates that the
+     * call is being held locally on the device.  When a capable {@link ConnectionService} receives
+     * signalling to indicate that the remote party has put the call on hold, it can send this
+     * connection event.
+     * @hide
+     */
+    public static final String EVENT_CALL_REMOTELY_HELD =
+            "android.telecom.event.CALL_REMOTELY_HELD";
+
+    /**
+     * Connection event used to inform {@link InCallService}s when a call which was remotely held
+     * (see {@link #EVENT_CALL_REMOTELY_HELD}) has been un-held by the remote party.
+     * <p>
+     * This is different than the {@link Connection#STATE_HOLDING} state which indicates that the
+     * call is being held locally on the device.  When a capable {@link ConnectionService} receives
+     * signalling to indicate that the remote party has taken the call off hold, it can send this
+     * connection event.
+     * @hide
+     */
+    public static final String EVENT_CALL_REMOTELY_UNHELD =
+            "android.telecom.event.CALL_REMOTELY_UNHELD";
+
     // Flag controlling whether PII is emitted into the logs
     private static final boolean PII_DEBUG = Log.isLoggable(android.util.Log.DEBUG);
 
@@ -441,69 +533,94 @@
         mConnectionCapabilities |= capability;
     }
 
-
+    /**
+     * Renders a set of capability bits ({@code CAPABILITY_*}) as a human readable string.
+     *
+     * @param capabilities A capability bit field.
+     * @return A human readable string representation.
+     */
     public static String capabilitiesToString(int capabilities) {
+        return capabilitiesToStringInternal(capabilities, true /* isLong */);
+    }
+
+    /**
+     * Renders a set of capability bits ({@code CAPABILITY_*}) as a *short* human readable
+     * string.
+     *
+     * @param capabilities A capability bit field.
+     * @return A human readable string representation.
+     * @hide
+     */
+    public static String capabilitiesToStringShort(int capabilities) {
+        return capabilitiesToStringInternal(capabilities, false /* isLong */);
+    }
+
+    private static String capabilitiesToStringInternal(int capabilities, boolean isLong) {
         StringBuilder builder = new StringBuilder();
-        builder.append("[Capabilities:");
+        builder.append("[");
+        if (isLong) {
+            builder.append("Capabilities:");
+        }
+
         if (can(capabilities, CAPABILITY_HOLD)) {
-            builder.append(" CAPABILITY_HOLD");
+            builder.append(isLong ? " CAPABILITY_HOLD" : " hld");
         }
         if (can(capabilities, CAPABILITY_SUPPORT_HOLD)) {
-            builder.append(" CAPABILITY_SUPPORT_HOLD");
+            builder.append(isLong ? " CAPABILITY_SUPPORT_HOLD" : " sup_hld");
         }
         if (can(capabilities, CAPABILITY_MERGE_CONFERENCE)) {
-            builder.append(" CAPABILITY_MERGE_CONFERENCE");
+            builder.append(isLong ? " CAPABILITY_MERGE_CONFERENCE" : " mrg_cnf");
         }
         if (can(capabilities, CAPABILITY_SWAP_CONFERENCE)) {
-            builder.append(" CAPABILITY_SWAP_CONFERENCE");
+            builder.append(isLong ? " CAPABILITY_SWAP_CONFERENCE" : " swp_cnf");
         }
         if (can(capabilities, CAPABILITY_RESPOND_VIA_TEXT)) {
-            builder.append(" CAPABILITY_RESPOND_VIA_TEXT");
+            builder.append(isLong ? " CAPABILITY_RESPOND_VIA_TEXT" : " txt");
         }
         if (can(capabilities, CAPABILITY_MUTE)) {
-            builder.append(" CAPABILITY_MUTE");
+            builder.append(isLong ? " CAPABILITY_MUTE" : " mut");
         }
         if (can(capabilities, CAPABILITY_MANAGE_CONFERENCE)) {
-            builder.append(" CAPABILITY_MANAGE_CONFERENCE");
+            builder.append(isLong ? " CAPABILITY_MANAGE_CONFERENCE" : " mng_cnf");
         }
         if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_RX)) {
-            builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_RX");
+            builder.append(isLong ? " CAPABILITY_SUPPORTS_VT_LOCAL_RX" : " VTlrx");
         }
         if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_TX)) {
-            builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_TX");
+            builder.append(isLong ? " CAPABILITY_SUPPORTS_VT_LOCAL_TX" : " VTltx");
         }
         if (can(capabilities, CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL)) {
-            builder.append(" CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL");
+            builder.append(isLong ? " CAPABILITY_SUPPORTS_VT_LOCAL_BIDIRECTIONAL" : " VTlbi");
         }
         if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_RX)) {
-            builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_RX");
+            builder.append(isLong ? " CAPABILITY_SUPPORTS_VT_REMOTE_RX" : " VTrrx");
         }
         if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_TX)) {
-            builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_TX");
+            builder.append(isLong ? " CAPABILITY_SUPPORTS_VT_REMOTE_TX" : " VTrtx");
         }
         if (can(capabilities, CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL)) {
-            builder.append(" CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL");
+            builder.append(isLong ? " CAPABILITY_SUPPORTS_VT_REMOTE_BIDIRECTIONAL" : " VTrbi");
         }
         if (can(capabilities, CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO)) {
-            builder.append(" CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO");
+            builder.append(isLong ? " CAPABILITY_CANNOT_DOWNGRADE_VIDEO_TO_AUDIO" : " !v2a");
         }
         if (can(capabilities, CAPABILITY_SPEED_UP_MT_AUDIO)) {
-            builder.append(" CAPABILITY_SPEED_UP_MT_AUDIO");
+            builder.append(isLong ? " CAPABILITY_SPEED_UP_MT_AUDIO" : " spd_aud");
         }
         if (can(capabilities, CAPABILITY_CAN_UPGRADE_TO_VIDEO)) {
-            builder.append(" CAPABILITY_CAN_UPGRADE_TO_VIDEO");
+            builder.append(isLong ? " CAPABILITY_CAN_UPGRADE_TO_VIDEO" : " a2v");
         }
         if (can(capabilities, CAPABILITY_CAN_PAUSE_VIDEO)) {
-            builder.append(" CAPABILITY_CAN_PAUSE_VIDEO");
+            builder.append(isLong ? " CAPABILITY_CAN_PAUSE_VIDEO" : " paus_VT");
         }
         if (can(capabilities, CAPABILITY_CONFERENCE_HAS_NO_CHILDREN)) {
-            builder.append(" CAPABILITY_SINGLE_PARTY_CONFERENCE");
+            builder.append(isLong ? " CAPABILITY_SINGLE_PARTY_CONFERENCE" : " 1p_cnf");
         }
         if (can(capabilities, CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION)) {
-            builder.append(" CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION");
+            builder.append(isLong ? " CAPABILITY_CAN_SEND_RESPONSE_VIA_CONNECTION" : " rsp_by_con");
         }
         if (can(capabilities, CAPABILITY_CAN_PULL_CALL)) {
-            builder.append(" CAPABILITY_CAN_PULL_CALL");
+            builder.append(isLong ? " CAPABILITY_CAN_PULL_CALL" : " pull");
         }
 
         builder.append("]");
@@ -511,34 +628,55 @@
     }
 
     /**
-     * Builds a string representation of a properties bit-mask.
+     * Renders a set of property bits ({@code PROPERTY_*}) as a human readable string.
      *
-     * @param properties The properties bit-mask.
-     * @return String representation.
-     * @hide
+     * @param properties A property bit field.
+     * @return A human readable string representation.
      */
     public static String propertiesToString(int properties) {
-        StringBuilder builder = new StringBuilder();
-        builder.append("[Properties:");
+        return propertiesToStringInternal(properties, true /* isLong */);
+    }
 
-        if (can(properties, PROPERTY_SHOW_CALLBACK_NUMBER)) {
-            builder.append(" PROPERTY_SHOW_CALLBACK_NUMBER");
+    /**
+     * Renders a set of property bits ({@code PROPERTY_*}) as a *short* human readable string.
+     *
+     * @param properties A property bit field.
+     * @return A human readable string representation.
+     * @hide
+     */
+    public static String propertiesToStringShort(int properties) {
+        return propertiesToStringInternal(properties, false /* isLong */);
+    }
+
+    private static String propertiesToStringInternal(int properties, boolean isLong) {
+        StringBuilder builder = new StringBuilder();
+        builder.append("[");
+        if (isLong) {
+            builder.append("Properties:");
+        }
+
+        if (can(properties, PROPERTY_EMERGENCY_CALLBACK_MODE)) {
+            builder.append(isLong ? " PROPERTY_EMERGENCY_CALLBACK_MODE" : " ecbm");
         }
 
         if (can(properties, PROPERTY_HIGH_DEF_AUDIO)) {
-            builder.append(" PROPERTY_HIGH_DEF_AUDIO");
+            builder.append(isLong ? " PROPERTY_HIGH_DEF_AUDIO" : " HD");
         }
 
         if (can(properties, PROPERTY_WIFI)) {
-            builder.append(" PROPERTY_WIFI");
+            builder.append(isLong ? " PROPERTY_WIFI" : " wifi");
         }
 
         if (can(properties, PROPERTY_GENERIC_CONFERENCE)) {
-            builder.append(" PROPERTY_GENERIC_CONFERENCE");
+            builder.append(isLong ? " PROPERTY_GENERIC_CONFERENCE" : " gen_conf");
         }
 
         if (can(properties, PROPERTY_IS_EXTERNAL_CALL)) {
-            builder.append(" PROPERTY_IS_EXTERNAL_CALL");
+            builder.append(isLong ? " PROPERTY_IS_EXTERNAL_CALL" : " xtrnl");
+        }
+
+        if (can(properties, PROPERTY_HAS_CDMA_VOICE_PRIVACY)) {
+            builder.append(isLong ? " PROPERTY_HAS_CDMA_VOICE_PRIVACY" : " priv");
         }
 
         builder.append("]");
@@ -574,6 +712,8 @@
         public void onExtrasChanged(Connection c, Bundle extras) {}
         public void onExtrasRemoved(Connection c, List<String> keys) {}
         public void onConnectionEvent(Connection c, String event, Bundle extras) {}
+        /** @hide */
+        public void onConferenceSupportedChanged(Connection c, boolean isConferenceSupported) {}
     }
 
     /**
@@ -593,7 +733,6 @@
      * {@link android.telecom.InCallService.VideoCall}.
      */
     public static abstract class VideoProvider {
-
         /**
          * Video is not being received (no protocol pause was issued).
          * @see #handleCallSessionEvent(int)
@@ -678,6 +817,14 @@
         private static final int MSG_SET_PAUSE_IMAGE = 11;
         private static final int MSG_REMOVE_VIDEO_CALLBACK = 12;
 
+        private static final String SESSION_EVENT_RX_PAUSE_STR = "RX_PAUSE";
+        private static final String SESSION_EVENT_RX_RESUME_STR = "RX_RESUME";
+        private static final String SESSION_EVENT_TX_START_STR = "TX_START";
+        private static final String SESSION_EVENT_TX_STOP_STR = "TX_STOP";
+        private static final String SESSION_EVENT_CAMERA_FAILURE_STR = "CAMERA_FAIL";
+        private static final String SESSION_EVENT_CAMERA_READY_STR = "CAMERA_READY";
+        private static final String SESSION_EVENT_UNKNOWN_STR = "UNKNOWN";
+
         private VideoProvider.VideoProviderHandler mMessageHandler;
         private final VideoProvider.VideoProviderBinder mBinder;
 
@@ -1188,6 +1335,32 @@
                 }
             }
         }
+
+        /**
+         * Returns a string representation of a call session event.
+         *
+         * @param event A call session event passed to {@link #handleCallSessionEvent(int)}.
+         * @return String representation of the call session event.
+         * @hide
+         */
+        public static String sessionEventToString(int event) {
+            switch (event) {
+                case SESSION_EVENT_CAMERA_FAILURE:
+                    return SESSION_EVENT_CAMERA_FAILURE_STR;
+                case SESSION_EVENT_CAMERA_READY:
+                    return SESSION_EVENT_CAMERA_READY_STR;
+                case SESSION_EVENT_RX_PAUSE:
+                    return SESSION_EVENT_RX_PAUSE_STR;
+                case SESSION_EVENT_RX_RESUME:
+                    return SESSION_EVENT_RX_RESUME_STR;
+                case SESSION_EVENT_TX_START:
+                    return SESSION_EVENT_TX_START_STR;
+                case SESSION_EVENT_TX_STOP:
+                    return SESSION_EVENT_TX_STOP_STR;
+                default:
+                    return SESSION_EVENT_UNKNOWN_STR + " " + event;
+            }
+        }
     }
 
     private final Listener mConnectionDeathListener = new Listener() {
@@ -1385,6 +1558,15 @@
 
     /**
      * Returns the extras associated with this connection.
+     * <p>
+     * Extras should be updated using {@link #putExtras(Bundle)}.
+     * <p>
+     * Telecom or an {@link InCallService} can also update the extras via
+     * {@link android.telecom.Call#putExtras(Bundle)}, and
+     * {@link Call#removeExtras(List)}.
+     * <p>
+     * The connection is notified of changes to the extras made by Telecom or an
+     * {@link InCallService} by {@link #onExtrasChanged(Bundle)}.
      *
      * @return The extras associated with this connection.
      */
@@ -1472,6 +1654,8 @@
                 return "RINGING";
             case STATE_DIALING:
                 return "DIALING";
+            case STATE_PULLING_CALL:
+                return "PULLING_CALL";
             case STATE_ACTIVE:
                 return "ACTIVE";
             case STATE_HOLDING:
@@ -1493,7 +1677,6 @@
 
     /**
      * Returns the connection's properties, as a bit mask of the {@code PROPERTY_*} constants.
-     * @hide
      */
     public final int getConnectionProperties() {
         return mConnectionProperties;
@@ -1594,6 +1777,16 @@
     }
 
     /**
+     * Sets state to pulling (e.g. the connection is being pulled to the local device from another
+     * device).  Only applicable for {@link Connection}s with
+     * {@link Connection#PROPERTY_IS_EXTERNAL_CALL} and {@link Connection#CAPABILITY_CAN_PULL_CALL}.
+     */
+    public final void setPulling() {
+        checkImmutable();
+        setState(STATE_PULLING_CALL);
+    }
+
+    /**
      * Sets state to be on hold.
      */
     public final void setOnHold() {
@@ -1699,7 +1892,6 @@
      * Sets the connection's properties as a bit mask of the {@code PROPERTY_*} constants.
      *
      * @param connectionProperties The new connection properties.
-     * @hide
      */
     public final void setConnectionProperties(int connectionProperties) {
         checkImmutable();
@@ -1880,6 +2072,9 @@
      * New or existing keys are replaced in the {@code Connection} extras.  Keys which are no longer
      * in the new extras, but were present the last time {@code setExtras} was called are removed.
      * <p>
+     * Alternatively you may use the {@link #putExtras(Bundle)}, and
+     * {@link #removeExtras(String...)} methods to modify the extras.
+     * <p>
      * No assumptions should be made as to how an In-Call UI or service will handle these extras.
      * Keys should be fully qualified (e.g., com.example.MY_EXTRA) to avoid conflicts.
      *
@@ -1924,7 +2119,6 @@
      * Keys should be fully qualified (e.g., com.example.MY_EXTRA) to avoid conflicts.
      *
      * @param extras The extras to add.
-     * @hide
      */
     public final void putExtras(@NonNull Bundle extras) {
         checkImmutable();
@@ -1988,10 +2182,9 @@
     }
 
     /**
-     * Removes an extra from this {@code Connection}.
+     * Removes extras from this {@code Connection}.
      *
-     * @param keys The key of the extra key to remove.
-     * @hide
+     * @param keys The keys of the extras to remove.
      */
     public final void removeExtras(List<String> keys) {
         synchronized (mExtrasLock) {
@@ -2008,6 +2201,15 @@
     }
 
     /**
+     * Removes extras from this {@code Connection}.
+     *
+     * @param keys The keys of the extras to remove.
+     */
+    public final void removeExtras(String ... keys) {
+        removeExtras(Arrays.asList(keys));
+    }
+
+    /**
      * Notifies this Connection that the {@link #getAudioState()} property has a new value.
      *
      * @param state The new connection audio state.
@@ -2129,7 +2331,6 @@
      * capability and {@link Connection#PROPERTY_IS_EXTERNAL_CALL} property bits must be set.
      * <p>
      * For more information on external calls, see {@link Connection#PROPERTY_IS_EXTERNAL_CALL}.
-     * @hide
      */
     public void onPullExternalCall() {}
 
@@ -2138,11 +2339,16 @@
      * <p>
      * The {@link InCallService} issues a Call event via {@link Call#sendCallEvent(String, Bundle)}.
      * <p>
+     * Where possible, the Connection should make an attempt to handle {@link Call} events which
+     * are part of the {@code android.telecom.*} namespace.  The Connection should ignore any events
+     * it does not wish to handle.  Unexpected events should be handled gracefully, as it is
+     * possible that a {@link InCallService} has defined its own Call events which a Connection is
+     * not aware of.
+     * <p>
      * See also {@link Call#sendCallEvent(String, Bundle)}.
      *
      * @param event The call event.
      * @param extras Extras associated with the call event.
-     * @hide
      */
     public void onCallEvent(String event, Bundle extras) {}
 
@@ -2155,7 +2361,6 @@
      * {@link Call#removeExtras(List)}.
      *
      * @param extras The new extras bundle.
-     * @hide
      */
     public void onExtrasChanged(Bundle extras) {}
 
@@ -2330,17 +2535,54 @@
     }
 
     /**
-     * Sends an event associated with this {@code Connection}, with associated event extras.
-     *
-     * Events are exposed to {@link InCallService} implementations via the
-     * {@link Call.Callback#onConnectionEvent(Call, String, Bundle)} API.
-     *
+     * Notifies listeners when a change has occurred to the Connection which impacts its ability to
+     * be a part of a conference call.
+     * @param isConferenceSupported {@code true} if the connection supports being part of a
+     *      conference call, {@code false} otherwise.
+     * @hide
+     */
+    protected void notifyConferenceSupportedChanged(boolean isConferenceSupported) {
+        for (Listener l : mListeners) {
+            l.onConferenceSupportedChanged(this, isConferenceSupported);
+        }
+    }
+
+    /**
+     * Sends an event associated with this {@code Connection} with associated event extras to the
+     * {@link InCallService}.
+     * <p>
+     * Connection events are used to communicate point in time information from a
+     * {@link ConnectionService} to a {@link InCallService} implementations.  An example of a
+     * custom connection event includes notifying the UI when a WIFI call has been handed over to
+     * LTE, which the InCall UI might use to inform the user that billing charges may apply.  The
+     * Android Telephony framework will send the {@link #EVENT_CALL_MERGE_FAILED} connection event
+     * when a call to {@link Call#mergeConference()} has failed to complete successfully.  A
+     * connection event could also be used to trigger UI in the {@link InCallService} which prompts
+     * the user to make a choice (e.g. whether they want to incur roaming costs for making a call),
+     * which is communicated back via {@link Call#sendCallEvent(String, Bundle)}.
+     * <p>
+     * Events are exposed to {@link InCallService} implementations via
+     * {@link Call.Callback#onConnectionEvent(Call, String, Bundle)}.
+     * <p>
      * No assumptions should be made as to how an In-Call UI or service will handle these events.
-     * Events should be fully qualified (e.g., com.example.event.MY_EVENT) to avoid conflicts.
+     * The {@link ConnectionService} must assume that the In-Call UI could even chose to ignore
+     * some events altogether.
+     * <p>
+     * Events should be fully qualified (e.g. {@code com.example.event.MY_EVENT}) to avoid
+     * conflicts between {@link ConnectionService} implementations.  Further, custom
+     * {@link ConnectionService} implementations shall not re-purpose events in the
+     * {@code android.*} namespace, nor shall they define new event types in this namespace.  When
+     * defining a custom event type, ensure the contents of the extras {@link Bundle} is clearly
+     * defined.  Extra keys for this bundle should be named similar to the event type (e.g.
+     * {@code com.example.extra.MY_EXTRA}).
+     * <p>
+     *  When defining events and the associated extras, it is important to keep their behavior
+     * consistent when the associated {@link ConnectionService} is updated.  Support for deprecated
+     * events/extras should me maintained to ensure backwards compatibility with older
+     * {@link InCallService} implementations which were built to support the older behavior.
      *
      * @param event The connection event.
-     * @param extras Bundle containing extra information associated with the event.
-     * @hide
+     * @param extras Optional bundle containing extra information associated with the event.
      */
     public void sendConnectionEvent(String event, Bundle extras) {
         for (Listener l : mListeners) {
diff --git a/telecomm/java/android/telecom/ConnectionService.java b/telecomm/java/android/telecom/ConnectionService.java
index 4d7c304..7b68a4c 100644
--- a/telecomm/java/android/telecom/ConnectionService.java
+++ b/telecomm/java/android/telecom/ConnectionService.java
@@ -556,6 +556,9 @@
                 case Connection.STATE_DIALING:
                     mAdapter.setDialing(id);
                     break;
+                case Connection.STATE_PULLING_CALL:
+                    mAdapter.setPulling(id);
+                    break;
                 case Connection.STATE_DISCONNECTED:
                     // Handled in onDisconnected()
                     break;
diff --git a/telecomm/java/android/telecom/ConnectionServiceAdapter.java b/telecomm/java/android/telecom/ConnectionServiceAdapter.java
index c8cd3c0..df7d539 100644
--- a/telecomm/java/android/telecom/ConnectionServiceAdapter.java
+++ b/telecomm/java/android/telecom/ConnectionServiceAdapter.java
@@ -143,6 +143,21 @@
     }
 
     /**
+     * Sets a call's state to pulling (e.g. a call with {@link Connection#PROPERTY_IS_EXTERNAL_CALL}
+     * is being pulled to the local device.
+     *
+     * @param callId The unique ID of the call whose state is changing to dialing.
+     */
+    void setPulling(String callId) {
+        for (IConnectionServiceAdapter adapter : mAdapters) {
+            try {
+                adapter.setPulling(callId);
+            } catch (RemoteException e) {
+            }
+        }
+    }
+
+    /**
      * Sets a call's state to disconnected.
      *
      * @param callId The unique ID of the call whose state is changing to disconnected.
diff --git a/telecomm/java/android/telecom/ConnectionServiceAdapterServant.java b/telecomm/java/android/telecom/ConnectionServiceAdapterServant.java
index bf28feb..486f9d5 100644
--- a/telecomm/java/android/telecom/ConnectionServiceAdapterServant.java
+++ b/telecomm/java/android/telecom/ConnectionServiceAdapterServant.java
@@ -65,6 +65,7 @@
     private static final int MSG_REMOVE_EXTRAS = 25;
     private static final int MSG_ON_CONNECTION_EVENT = 26;
     private static final int MSG_SET_CONNECTION_PROPERTIES = 27;
+    private static final int MSG_SET_PULLING = 28;
 
     private final IConnectionServiceAdapter mDelegate;
 
@@ -101,6 +102,9 @@
                 case MSG_SET_DIALING:
                     mDelegate.setDialing((String) msg.obj);
                     break;
+                case MSG_SET_PULLING:
+                    mDelegate.setPulling((String) msg.obj);
+                    break;
                 case MSG_SET_DISCONNECTED: {
                     SomeArgs args = (SomeArgs) msg.obj;
                     try {
@@ -299,6 +303,11 @@
         }
 
         @Override
+        public void setPulling(String connectionId) {
+            mHandler.obtainMessage(MSG_SET_PULLING, connectionId).sendToTarget();
+        }
+
+        @Override
         public void setDisconnected(
                 String connectionId, DisconnectCause disconnectCause) {
             SomeArgs args = SomeArgs.obtain();
diff --git a/telecomm/java/android/telecom/DisconnectCause.java b/telecomm/java/android/telecom/DisconnectCause.java
index 65437d9..6860269 100644
--- a/telecomm/java/android/telecom/DisconnectCause.java
+++ b/telecomm/java/android/telecom/DisconnectCause.java
@@ -67,13 +67,11 @@
     /**
      * Disconnected because the user did not locally answer the incoming call, but it was answered
      * on another device where the call was ringing.
-     * @hide
      */
     public static final int ANSWERED_ELSEWHERE = 11;
 
     /**
      * Disconnected because the call was pulled from the current device to another device.
-     * @hide
      */
     public static final int CALL_PULLED = 12;
 
@@ -277,6 +275,12 @@
             case CONNECTION_MANAGER_NOT_SUPPORTED:
                 code = "CONNECTION_MANAGER_NOT_SUPPORTED";
                 break;
+            case CALL_PULLED:
+                code = "CALL_PULLED";
+                break;
+            case ANSWERED_ELSEWHERE:
+                code = "ANSWERED_ELSEWHERE";
+                break;
             default:
                 code = "invalid code: " + mDisconnectCode;
                 break;
diff --git a/telecomm/java/android/telecom/InCallService.java b/telecomm/java/android/telecom/InCallService.java
index e2399ff..69de89d 100644
--- a/telecomm/java/android/telecom/InCallService.java
+++ b/telecomm/java/android/telecom/InCallService.java
@@ -449,15 +449,14 @@
     }
 
     /**
-     * Called when a {@link Call} has received a connection event issued by the
-     * {@link ConnectionService}.
+     * Unused; to handle connection events issued by a {@link ConnectionService}, implement the
+     * {@link android.telecom.Call.Callback#onConnectionEvent(Call, String, Bundle)} callback.
      * <p>
      * See {@link Connection#sendConnectionEvent(String, Bundle)}.
      *
      * @param call The call the event is associated with.
      * @param event The event.
      * @param extras Any associated extras.
-     * @hide
      */
     public void onConnectionEvent(Call call, String event, Bundle extras) {
     }
diff --git a/telecomm/java/android/telecom/ParcelableCallAnalytics.java b/telecomm/java/android/telecom/ParcelableCallAnalytics.java
index e7c9672..383d10b 100644
--- a/telecomm/java/android/telecom/ParcelableCallAnalytics.java
+++ b/telecomm/java/android/telecom/ParcelableCallAnalytics.java
@@ -20,11 +20,231 @@
 import android.os.Parcel;
 import android.os.Parcelable;
 
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
 /**
  * @hide
  */
 @SystemApi
 public class ParcelableCallAnalytics implements Parcelable {
+    /** {@hide} */
+    public static final class VideoEvent implements Parcelable {
+        public static final int SEND_LOCAL_SESSION_MODIFY_REQUEST = 0;
+        public static final int SEND_LOCAL_SESSION_MODIFY_RESPONSE = 1;
+        public static final int RECEIVE_REMOTE_SESSION_MODIFY_REQUEST = 2;
+        public static final int RECEIVE_REMOTE_SESSION_MODIFY_RESPONSE = 3;
+
+        public static final Parcelable.Creator<VideoEvent> CREATOR =
+                new Parcelable.Creator<VideoEvent> () {
+
+                    @Override
+                    public VideoEvent createFromParcel(Parcel in) {
+                        return new VideoEvent(in);
+                    }
+
+                    @Override
+                    public VideoEvent[] newArray(int size) {
+                        return new VideoEvent[size];
+                    }
+                };
+
+        private int mEventName;
+        private long mTimeSinceLastEvent;
+        private int mVideoState;
+
+        public VideoEvent(int eventName, long timeSinceLastEvent, int videoState) {
+            mEventName = eventName;
+            mTimeSinceLastEvent = timeSinceLastEvent;
+            mVideoState = videoState;
+        }
+
+        VideoEvent(Parcel in) {
+            mEventName = in.readInt();
+            mTimeSinceLastEvent = in.readLong();
+            mVideoState = in.readInt();
+        }
+
+        public int getEventName() {
+            return mEventName;
+        }
+
+        public long getTimeSinceLastEvent() {
+            return mTimeSinceLastEvent;
+        }
+
+        public int getVideoState() {
+            return mVideoState;
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public void writeToParcel(Parcel out, int flags) {
+            out.writeInt(mEventName);
+            out.writeLong(mTimeSinceLastEvent);
+            out.writeInt(mVideoState);
+        }
+    }
+
+    public static final class AnalyticsEvent implements Parcelable {
+        public static final int SET_SELECT_PHONE_ACCOUNT = 0;
+        public static final int SET_ACTIVE = 1;
+        public static final int SET_DISCONNECTED = 2;
+        public static final int START_CONNECTION = 3;
+        public static final int SET_DIALING = 4;
+        public static final int BIND_CS = 5;
+        public static final int CS_BOUND = 6;
+        public static final int REQUEST_ACCEPT = 7;
+        public static final int REQUEST_REJECT = 8;
+
+        public static final int SCREENING_SENT = 100;
+        public static final int SCREENING_COMPLETED = 101;
+        public static final int DIRECT_TO_VM_INITIATED = 102;
+        public static final int DIRECT_TO_VM_FINISHED = 103;
+        public static final int BLOCK_CHECK_INITIATED = 104;
+        public static final int BLOCK_CHECK_FINISHED = 105;
+        public static final int FILTERING_INITIATED = 106;
+        public static final int FILTERING_COMPLETED = 107;
+        public static final int FILTERING_TIMED_OUT = 108;
+
+        public static final int SKIP_RINGING = 200;
+        public static final int SILENCE = 201;
+        public static final int MUTE = 202;
+        public static final int UNMUTE = 203;
+        public static final int AUDIO_ROUTE_BT = 204;
+        public static final int AUDIO_ROUTE_EARPIECE = 205;
+        public static final int AUDIO_ROUTE_HEADSET = 206;
+        public static final int AUDIO_ROUTE_SPEAKER = 207;
+
+        public static final int CONFERENCE_WITH = 300;
+        public static final int SPLIT_CONFERENCE = 301;
+        public static final int SET_PARENT = 302;
+
+        public static final int REQUEST_HOLD = 400;
+        public static final int REQUEST_UNHOLD = 401;
+        public static final int REMOTELY_HELD = 402;
+        public static final int REMOTELY_UNHELD = 403;
+        public static final int SET_HOLD = 404;
+        public static final int SWAP = 405;
+
+        public static final int REQUEST_PULL = 500;
+
+
+        public static final Parcelable.Creator<AnalyticsEvent> CREATOR =
+                new Parcelable.Creator<AnalyticsEvent> () {
+
+                    @Override
+                    public AnalyticsEvent createFromParcel(Parcel in) {
+                        return new AnalyticsEvent(in);
+                    }
+
+                    @Override
+                    public AnalyticsEvent[] newArray(int size) {
+                        return new AnalyticsEvent[size];
+                    }
+                };
+
+        private int mEventName;
+        private long mTimeSinceLastEvent;
+
+        public AnalyticsEvent(int eventName, long timestamp) {
+            mEventName = eventName;
+            mTimeSinceLastEvent = timestamp;
+        }
+
+        AnalyticsEvent(Parcel in) {
+            mEventName = in.readInt();
+            mTimeSinceLastEvent = in.readLong();
+        }
+
+        public int getEventName() {
+            return mEventName;
+        }
+
+        public long getTimeSinceLastEvent() {
+            return mTimeSinceLastEvent;
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public void writeToParcel(Parcel out, int flags) {
+            out.writeInt(mEventName);
+            out.writeLong(mTimeSinceLastEvent);
+        }
+    }
+
+    public static final class EventTiming implements Parcelable {
+        public static final int ACCEPT_TIMING = 0;
+        public static final int REJECT_TIMING = 1;
+        public static final int DISCONNECT_TIMING = 2;
+        public static final int HOLD_TIMING = 3;
+        public static final int UNHOLD_TIMING = 4;
+        public static final int OUTGOING_TIME_TO_DIALING_TIMING = 5;
+        public static final int BIND_CS_TIMING = 6;
+        public static final int SCREENING_COMPLETED_TIMING = 7;
+        public static final int DIRECT_TO_VM_FINISHED_TIMING = 8;
+        public static final int BLOCK_CHECK_FINISHED_TIMING = 9;
+        public static final int FILTERING_COMPLETED_TIMING = 10;
+        public static final int FILTERING_TIMED_OUT_TIMING = 11;
+
+        public static final int INVALID = 999999;
+
+        public static final Parcelable.Creator<EventTiming> CREATOR =
+                new Parcelable.Creator<EventTiming> () {
+
+                    @Override
+                    public EventTiming createFromParcel(Parcel in) {
+                        return new EventTiming(in);
+                    }
+
+                    @Override
+                    public EventTiming[] newArray(int size) {
+                        return new EventTiming[size];
+                    }
+                };
+
+        private int mName;
+        private long mTime;
+
+        public EventTiming(int name, long time) {
+            this.mName = name;
+            this.mTime = time;
+        }
+
+        private EventTiming(Parcel in) {
+            mName = in.readInt();
+            mTime = in.readLong();
+        }
+
+        public int getName() {
+            return mName;
+        }
+
+        public long getTime() {
+            return mTime;
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public void writeToParcel(Parcel out, int flags) {
+            out.writeInt(mName);
+            out.writeLong(mTime);
+        }
+    }
+
     public static final int CALLTYPE_UNKNOWN = 0;
     public static final int CALLTYPE_INCOMING = 1;
     public static final int CALLTYPE_OUTGOING = 2;
@@ -87,10 +307,23 @@
     // Whether the call object was created from an existing connection.
     private final boolean isCreatedFromExistingConnection;
 
+    // A list of events that are associated with this call
+    private final List<AnalyticsEvent> analyticsEvents;
+
+    // A map from event-pair names to their durations.
+    private final List<EventTiming> eventTimings;
+
+    // Whether the call has ever been a video call.
+    private boolean isVideoCall = false;
+
+    // A list of video events that have occurred.
+    private List<VideoEvent> videoEvents;
+
     public ParcelableCallAnalytics(long startTimeMillis, long callDurationMillis, int callType,
             boolean isAdditionalCall, boolean isInterrupted, int callTechnologies,
             int callTerminationCode, boolean isEmergencyCall, String connectionService,
-            boolean isCreatedFromExistingConnection) {
+            boolean isCreatedFromExistingConnection, List<AnalyticsEvent> analyticsEvents,
+            List<EventTiming> eventTimings) {
         this.startTimeMillis = startTimeMillis;
         this.callDurationMillis = callDurationMillis;
         this.callType = callType;
@@ -101,6 +334,8 @@
         this.isEmergencyCall = isEmergencyCall;
         this.connectionService = connectionService;
         this.isCreatedFromExistingConnection = isCreatedFromExistingConnection;
+        this.analyticsEvents = analyticsEvents;
+        this.eventTimings = eventTimings;
     }
 
     public ParcelableCallAnalytics(Parcel in) {
@@ -114,6 +349,13 @@
         isEmergencyCall = readByteAsBoolean(in);
         connectionService = in.readString();
         isCreatedFromExistingConnection = readByteAsBoolean(in);
+        analyticsEvents = new ArrayList<>();
+        in.readTypedList(analyticsEvents, AnalyticsEvent.CREATOR);
+        eventTimings = new ArrayList<>();
+        in.readTypedList(eventTimings, EventTiming.CREATOR);
+        isVideoCall = readByteAsBoolean(in);
+        videoEvents = new LinkedList<>();
+        in.readTypedList(videoEvents, VideoEvent.CREATOR);
     }
 
     public void writeToParcel(Parcel out, int flags) {
@@ -127,6 +369,20 @@
         writeBooleanAsByte(out, isEmergencyCall);
         out.writeString(connectionService);
         writeBooleanAsByte(out, isCreatedFromExistingConnection);
+        out.writeTypedList(analyticsEvents);
+        out.writeTypedList(eventTimings);
+        writeBooleanAsByte(out, isVideoCall);
+        out.writeTypedList(videoEvents);
+    }
+
+    /** {@hide} */
+    public void setIsVideoCall(boolean isVideoCall) {
+        this.isVideoCall = isVideoCall;
+    }
+
+    /** {@hide} */
+    public void setVideoEvents(List<VideoEvent> videoEvents) {
+        this.videoEvents = videoEvents;
     }
 
     public long getStartTimeMillis() {
@@ -169,6 +425,24 @@
         return isCreatedFromExistingConnection;
     }
 
+    public List<AnalyticsEvent> analyticsEvents() {
+        return analyticsEvents;
+    }
+
+    public List<EventTiming> getEventTimings() {
+        return eventTimings;
+    }
+
+    /** {@hide} */
+    public boolean isVideoCall() {
+        return isVideoCall;
+    }
+
+    /** {@hide} */
+    public List<VideoEvent> getVideoEvents() {
+        return videoEvents;
+    }
+
     @Override
     public int describeContents() {
         return 0;
diff --git a/telecomm/java/android/telecom/PhoneAccount.java b/telecomm/java/android/telecom/PhoneAccount.java
index dbc2b0c..473e394 100644
--- a/telecomm/java/android/telecom/PhoneAccount.java
+++ b/telecomm/java/android/telecom/PhoneAccount.java
@@ -220,6 +220,7 @@
     private final Icon mIcon;
     private final Bundle mExtras;
     private boolean mIsEnabled;
+    private String mGroupId;
 
     /**
      * Helper class for creating a {@link PhoneAccount}.
@@ -236,6 +237,7 @@
         private Icon mIcon;
         private Bundle mExtras;
         private boolean mIsEnabled = false;
+        private String mGroupId = "";
 
         /**
          * Creates a builder with the specified {@link PhoneAccountHandle} and label.
@@ -263,6 +265,7 @@
             mIcon = phoneAccount.getIcon();
             mIsEnabled = phoneAccount.isEnabled();
             mExtras = phoneAccount.getExtras();
+            mGroupId = phoneAccount.getGroupId();
         }
 
         /**
@@ -387,6 +390,27 @@
         }
 
         /**
+         * Sets the group Id of the {@link PhoneAccount}. When a new {@link PhoneAccount} is
+         * registered to Telecom, it will replace another {@link PhoneAccount} that is already
+         * registered in Telecom and take on the current user defaults and enabled status. There can
+         * only be one {@link PhoneAccount} with a non-empty group number registered to Telecom at a
+         * time. By default, there is no group Id for a {@link PhoneAccount} (an empty String). Only
+         * grouped {@link PhoneAccount}s with the same {@link ConnectionService} can be replaced.
+         * @param groupId The group Id of the {@link PhoneAccount} that will replace any other
+         * registered {@link PhoneAccount} in Telecom with the same Group Id.
+         * @return The builder
+         * @hide
+         */
+        public Builder setGroupId(String groupId) {
+            if (groupId != null) {
+                mGroupId = groupId;
+            } else {
+                mGroupId = "";
+            }
+            return this;
+        }
+
+        /**
          * Creates an instance of a {@link PhoneAccount} based on the current builder settings.
          *
          * @return The {@link PhoneAccount}.
@@ -408,7 +432,8 @@
                     mShortDescription,
                     mSupportedUriSchemes,
                     mExtras,
-                    mIsEnabled);
+                    mIsEnabled,
+                    mGroupId);
         }
     }
 
@@ -423,7 +448,8 @@
             CharSequence shortDescription,
             List<String> supportedUriSchemes,
             Bundle extras,
-            boolean isEnabled) {
+            boolean isEnabled,
+            String groupId) {
         mAccountHandle = account;
         mAddress = address;
         mSubscriptionAddress = subscriptionAddress;
@@ -435,6 +461,7 @@
         mSupportedUriSchemes = Collections.unmodifiableList(supportedUriSchemes);
         mExtras = extras;
         mIsEnabled = isEnabled;
+        mGroupId = groupId;
     }
 
     public static Builder builder(
@@ -564,6 +591,21 @@
     }
 
     /**
+     * A non-empty {@link String} representing the group that A {@link PhoneAccount} is in or an
+     * empty {@link String} if the {@link PhoneAccount} is not in a group. If this
+     * {@link PhoneAccount} is in a group, this new {@link PhoneAccount} will replace a registered
+     * {@link PhoneAccount} that is in the same group. When the {@link PhoneAccount} is replaced,
+     * its user defined defaults and enabled status will also pass to this new {@link PhoneAccount}.
+     * Only {@link PhoneAccount}s that share the same {@link ConnectionService} can be replaced.
+     *
+     * @return A non-empty String Id if this {@link PhoneAccount} belongs to a group.
+     * @hide
+     */
+    public String getGroupId() {
+        return mGroupId;
+    }
+
+    /**
      * Determines if the {@link PhoneAccount} supports calls to/from addresses with a specified URI
      * scheme.
      *
@@ -644,6 +686,7 @@
         }
         out.writeByte((byte) (mIsEnabled ? 1 : 0));
         out.writeBundle(mExtras);
+        out.writeString(mGroupId);
     }
 
     public static final Creator<PhoneAccount> CREATOR
@@ -687,6 +730,7 @@
         }
         mIsEnabled = in.readByte() == 1;
         mExtras = in.readBundle();
+        mGroupId = in.readString();
     }
 
     @Override
@@ -704,6 +748,8 @@
         }
         sb.append(" Extras: ");
         sb.append(mExtras);
+        sb.append(" GroupId: ");
+        sb.append(Log.pii(mGroupId));
         sb.append("]");
         return sb.toString();
     }
diff --git a/telecomm/java/android/telecom/RemoteConference.java b/telecomm/java/android/telecom/RemoteConference.java
index bf6038a..943da6d 100644
--- a/telecomm/java/android/telecom/RemoteConference.java
+++ b/telecomm/java/android/telecom/RemoteConference.java
@@ -97,7 +97,6 @@
          *
          * @param conference The {@code RemoteConference} invoking this method.
          * @param connectionProperties The new properties of the {@code RemoteConference}.
-         * @hide
          */
         public void onConnectionPropertiesChanged(
                 RemoteConference conference,
diff --git a/telecomm/java/android/telecom/RemoteConnection.java b/telecomm/java/android/telecom/RemoteConnection.java
index 8e06659db..f030115 100644
--- a/telecomm/java/android/telecom/RemoteConnection.java
+++ b/telecomm/java/android/telecom/RemoteConnection.java
@@ -95,7 +95,6 @@
          *
          * @param connection The {@code RemoteConnection} invoking this method.
          * @param connectionProperties The new properties of the {@code RemoteConnection}.
-         * @hide
          */
         public void onConnectionPropertiesChanged(
                 RemoteConnection connection,
@@ -230,7 +229,6 @@
          * @param connection The {@code RemoteConnection} invoking this method.
          * @param event The connection event.
          * @param extras Extras associated with the event.
-         * @hide
          */
         public void onConnectionEvent(RemoteConnection connection, String event, Bundle extras) {}
     }
@@ -640,7 +638,12 @@
         mConnectionCapabilities = connection.getConnectionCapabilities();
         mConnectionProperties = connection.getConnectionProperties();
         mVideoState = connection.getVideoState();
-        mVideoProvider = new RemoteConnection.VideoProvider(connection.getVideoProvider());
+        IVideoProvider videoProvider = connection.getVideoProvider();
+        if (videoProvider != null) {
+            mVideoProvider = new RemoteConnection.VideoProvider(videoProvider);
+        } else {
+            mVideoProvider = null;
+        }
         mIsVoipAudioMode = connection.getIsVoipAudioMode();
         mStatusHints = connection.getStatusHints();
         mAddress = connection.getHandle();
@@ -738,7 +741,6 @@
      *
      * @return A bitmask of the properties of the {@code RemoteConnection}, as defined in the
      *         {@code PROPERTY_*} constants in class {@link Connection}.
-     * @hide
      */
     public int getConnectionProperties() {
         return mConnectionProperties;
@@ -993,7 +995,6 @@
      * Instructs this {@link RemoteConnection} to pull itself to the local device.
      * <p>
      * See {@link Call#pullExternalCall()} for more information.
-     * @hide
      */
     public void pullExternalCall() {
         try {
diff --git a/telecomm/java/android/telecom/RemoteConnectionService.java b/telecomm/java/android/telecom/RemoteConnectionService.java
index 21a7706..c4739ff 100644
--- a/telecomm/java/android/telecom/RemoteConnectionService.java
+++ b/telecomm/java/android/telecom/RemoteConnectionService.java
@@ -118,6 +118,12 @@
         }
 
         @Override
+        public void setPulling(String callId) {
+            findConnectionForAction(callId, "setPulling")
+                    .setState(Connection.STATE_PULLING_CALL);
+        }
+
+        @Override
         public void setDisconnected(String callId, DisconnectCause disconnectCause) {
             if (mConnectionById.containsKey(callId)) {
                 findConnectionForAction(callId, "setDisconnected")
@@ -218,6 +224,7 @@
 
             conference.setState(parcel.getState());
             conference.setConnectionCapabilities(parcel.getConnectionCapabilities());
+            conference.setConnectionProperties(parcel.getConnectionProperties());
             mConferenceById.put(callId, conference);
             conference.registerCallback(new RemoteConference.Callback() {
                 @Override
diff --git a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl b/telecomm/java/android/telecom/TelecomAnalytics.aidl
similarity index 94%
rename from telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
rename to telecomm/java/android/telecom/TelecomAnalytics.aidl
index b7e78d1..08ad0a2 100644
--- a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
+++ b/telecomm/java/android/telecom/TelecomAnalytics.aidl
@@ -19,4 +19,4 @@
 /**
  * {@hide}
  */
-parcelable ParcelableCallAnalytics;
+parcelable TelecomAnalytics;
diff --git a/telecomm/java/android/telecom/TelecomAnalytics.java b/telecomm/java/android/telecom/TelecomAnalytics.java
new file mode 100644
index 0000000..6e0d02c
--- /dev/null
+++ b/telecomm/java/android/telecom/TelecomAnalytics.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.telecom;
+
+import android.annotation.SystemApi;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @hide
+ */
+@SystemApi
+public final class TelecomAnalytics implements Parcelable {
+    public static final Parcelable.Creator<TelecomAnalytics> CREATOR =
+            new Parcelable.Creator<TelecomAnalytics> () {
+
+                @Override
+                public TelecomAnalytics createFromParcel(Parcel in) {
+                    return new TelecomAnalytics(in);
+                }
+
+                @Override
+                public TelecomAnalytics[] newArray(int size) {
+                    return new TelecomAnalytics[size];
+                }
+            };
+
+    public static final class SessionTiming extends TimedEvent<Integer> implements Parcelable {
+        public static final Parcelable.Creator<SessionTiming> CREATOR =
+                new Parcelable.Creator<SessionTiming> () {
+
+                    @Override
+                    public SessionTiming createFromParcel(Parcel in) {
+                        return new SessionTiming(in);
+                    }
+
+                    @Override
+                    public SessionTiming[] newArray(int size) {
+                        return new SessionTiming[size];
+                    }
+                };
+
+        public static final int ICA_ANSWER_CALL = 1;
+        public static final int ICA_REJECT_CALL = 2;
+        public static final int ICA_DISCONNECT_CALL = 3;
+        public static final int ICA_HOLD_CALL = 4;
+        public static final int ICA_UNHOLD_CALL = 5;
+        public static final int ICA_MUTE = 6;
+        public static final int ICA_SET_AUDIO_ROUTE = 7;
+        public static final int ICA_CONFERENCE = 8;
+
+        public static final int CSW_HANDLE_CREATE_CONNECTION_COMPLETE = 100;
+        public static final int CSW_SET_ACTIVE = 101;
+        public static final int CSW_SET_RINGING = 102;
+        public static final int CSW_SET_DIALING = 103;
+        public static final int CSW_SET_DISCONNECTED = 104;
+        public static final int CSW_SET_ON_HOLD = 105;
+        public static final int CSW_REMOVE_CALL = 106;
+        public static final int CSW_SET_IS_CONFERENCED = 107;
+        public static final int CSW_ADD_CONFERENCE_CALL = 108;
+
+        private int mId;
+        private long mTime;
+
+        public SessionTiming(int id, long time) {
+            this.mId = id;
+            this.mTime = time;
+        }
+
+        private SessionTiming(Parcel in) {
+            mId = in.readInt();
+            mTime = in.readLong();
+        }
+
+        @Override
+        public Integer getKey() {
+            return mId;
+        }
+
+        @Override
+        public long getTime() {
+            return mTime;
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public void writeToParcel(Parcel out, int flags) {
+            out.writeInt(mId);
+            out.writeLong(mTime);
+        }
+    }
+
+    private List<SessionTiming> mSessionTimings;
+    private List<ParcelableCallAnalytics> mCallAnalytics;
+
+    public TelecomAnalytics(List<SessionTiming> sessionTimings,
+            List<ParcelableCallAnalytics> callAnalytics) {
+        this.mSessionTimings = sessionTimings;
+        this.mCallAnalytics = callAnalytics;
+    }
+
+    private TelecomAnalytics(Parcel in) {
+        mSessionTimings = new ArrayList<>();
+        in.readTypedList(mSessionTimings, SessionTiming.CREATOR);
+        mCallAnalytics = new ArrayList<>();
+        in.readTypedList(mCallAnalytics, ParcelableCallAnalytics.CREATOR);
+    }
+
+    public List<SessionTiming> getSessionTimings() {
+        return mSessionTimings;
+    }
+
+    public List<ParcelableCallAnalytics> getCallAnalytics() {
+        return mCallAnalytics;
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(Parcel out, int flags) {
+        out.writeTypedList(mSessionTimings);
+        out.writeTypedList(mCallAnalytics);
+    }
+}
diff --git a/telecomm/java/android/telecom/TelecomManager.java b/telecomm/java/android/telecom/TelecomManager.java
index da0d048..f12886a 100644
--- a/telecomm/java/android/telecom/TelecomManager.java
+++ b/telecomm/java/android/telecom/TelecomManager.java
@@ -348,7 +348,6 @@
      * informed of external calls should set this meta-data to {@code true} in the manifest
      * registration of their {@link InCallService}.  By default, the {@link InCallService} will NOT
      * be informed of external calls.
-     * @hide
      */
     public static final String METADATA_INCLUDE_EXTERNAL_CALLS =
             "android.telecom.INCLUDE_EXTERNAL_CALLS";
@@ -1444,9 +1443,9 @@
      */
     @SystemApi
     @RequiresPermission(Manifest.permission.DUMP)
-    public List<ParcelableCallAnalytics> dumpAnalytics() {
+    public TelecomAnalytics dumpAnalytics() {
         ITelecomService service = getTelecomService();
-        List<ParcelableCallAnalytics> result = null;
+        TelecomAnalytics result = null;
         if (service != null) {
             try {
                 result = service.dumpCallAnalytics();
diff --git a/telecomm/java/android/telecom/TimedEvent.java b/telecomm/java/android/telecom/TimedEvent.java
new file mode 100644
index 0000000..e484e79
--- /dev/null
+++ b/telecomm/java/android/telecom/TimedEvent.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package android.telecom;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @hide
+ */
+public abstract class TimedEvent<T> {
+    public abstract long getTime();
+    public abstract T getKey();
+
+    public static <T> Map<T, Double> averageTimings(Collection<? extends TimedEvent<T>> events) {
+        HashMap<T, Integer> counts = new HashMap<>();
+        HashMap<T, Double> result = new HashMap<>();
+
+        for (TimedEvent<T> entry : events) {
+            if (counts.containsKey(entry.getKey())) {
+                counts.put(entry.getKey(), counts.get(entry.getKey()) + 1);
+                result.put(entry.getKey(), result.get(entry.getKey()) + entry.getTime());
+            } else {
+                counts.put(entry.getKey(), 1);
+                result.put(entry.getKey(), (double) entry.getTime());
+            }
+        }
+
+        for (Map.Entry<T, Double> entry : result.entrySet()) {
+            result.put(entry.getKey(), entry.getValue() / counts.get(entry.getKey()));
+        }
+
+        return result;
+    }
+}
+
diff --git a/telecomm/java/com/android/internal/telecom/IConnectionServiceAdapter.aidl b/telecomm/java/com/android/internal/telecom/IConnectionServiceAdapter.aidl
index 9bc8ffe..3bf83ae 100644
--- a/telecomm/java/com/android/internal/telecom/IConnectionServiceAdapter.aidl
+++ b/telecomm/java/com/android/internal/telecom/IConnectionServiceAdapter.aidl
@@ -47,6 +47,8 @@
 
     void setDialing(String callId);
 
+    void setPulling(String callId);
+
     void setDisconnected(String callId, in DisconnectCause disconnectCause);
 
     void setOnHold(String callId);
diff --git a/telecomm/java/com/android/internal/telecom/ITelecomService.aidl b/telecomm/java/com/android/internal/telecom/ITelecomService.aidl
index 871565d..5c412e7 100644
--- a/telecomm/java/com/android/internal/telecom/ITelecomService.aidl
+++ b/telecomm/java/com/android/internal/telecom/ITelecomService.aidl
@@ -18,7 +18,7 @@
 
 import android.content.ComponentName;
 import android.content.Intent;
-import android.telecom.ParcelableCallAnalytics;
+import android.telecom.TelecomAnalytics;
 import android.telecom.PhoneAccountHandle;
 import android.net.Uri;
 import android.os.Bundle;
@@ -148,7 +148,7 @@
     /**
     * @see TelecomServiceImpl#dumpCallAnalytics
     */
-    List<ParcelableCallAnalytics> dumpCallAnalytics();
+    TelecomAnalytics dumpCallAnalytics();
 
     //
     // Internal system apis relating to call management.
diff --git a/telephony/java/android/telephony/CarrierConfigManager.java b/telephony/java/android/telephony/CarrierConfigManager.java
index 17f0da0..7432de9 100644
--- a/telephony/java/android/telephony/CarrierConfigManager.java
+++ b/telephony/java/android/telephony/CarrierConfigManager.java
@@ -24,6 +24,7 @@
 import android.os.RemoteException;
 import android.os.ServiceManager;
 
+import com.android.ims.ImsReasonInfo;
 import com.android.internal.telephony.ICarrierConfigLoader;
 
 /**
@@ -262,6 +263,29 @@
     public static final String KEY_CARRIER_VT_AVAILABLE_BOOL = "carrier_vt_available_bool";
 
     /**
+     * Flag specifying whether the carrier wants to notify the user when a VT call has been handed
+     * over from WIFI to LTE.
+     * <p>
+     * The handover notification is sent as a
+     * {@link TelephonyManager#EVENT_HANDOVER_VIDEO_FROM_WIFI_TO_LTE}
+     * {@link android.telecom.Connection} event, which an {@link android.telecom.InCallService}
+     * should use to trigger the display of a user-facing message.
+     * <p>
+     * The Connection event is sent to the InCallService only once, the first time it occurs.
+     * @hide
+     */
+    public static final String KEY_NOTIFY_HANDOVER_VIDEO_FROM_WIFI_TO_LTE_BOOL =
+            "notify_handover_video_from_wifi_to_lte_bool";
+
+    /**
+     * Flag specifying whether the carrier supports downgrading a video call (tx, rx or tx/rx)
+     * directly to an audio call.
+     * @hide
+     */
+    public static final String KEY_SUPPORT_DOWNGRADE_VT_TO_AUDIO_BOOL =
+            "support_downgrade_vt_to_audio_bool";
+
+    /**
      * Flag specifying whether WFC over IMS should be available for carrier: independent of
      * carrier provisioning. If false: hard disabled. If true: then depends on carrier
      * provisioning, availability etc.
@@ -290,13 +314,23 @@
             "carrier_wfc_supports_wifi_only_bool";
 
     /**
-     * Default WFC_IMS_mode 0: WIFI_ONLY
-     *                      1: CELLULAR_PREFERRED
-     *                      2: WIFI_PREFERRED
+     * Default WFC_IMS_MODE for home network   0: WIFI_ONLY
+     *                                         1: CELLULAR_PREFERRED
+     *                                         2: WIFI_PREFERRED
      * @hide
      */
     public static final String KEY_CARRIER_DEFAULT_WFC_IMS_MODE_INT =
             "carrier_default_wfc_ims_mode_int";
+
+    /**
+     * Default WFC_IMS_MODE for roaming
+     * See {@link KEY_CARRIER_DEFAULT_WFC_IMS_MODE_INT} for valid values.
+     *
+     * @hide
+     */
+    public static final String KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_MODE_INT =
+            "carrier_default_wfc_ims_roaming_mode_int";
+
     /**
      * Default WFC_IMS_enabled: true VoWiFi by default is on
      *                          false VoWiFi by default is off
@@ -313,6 +347,16 @@
     public static final String KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_ENABLED_BOOL =
             "carrier_default_wfc_ims_roaming_enabled_bool";
 
+    /**
+     * Flag indicating whether failed calls due to no service should prompt the user to enable
+     * WIFI calling.  When {@code true}, if the user attempts to establish a call when there is no
+     * service available, they are connected to WIFI, and WIFI calling is disabled, a different
+     * call failure message will be used to encourage the user to enable WIFI calling.
+     * @hide
+     */
+    public static final String KEY_CARRIER_PROMOTE_WFC_ON_CALL_FAIL_BOOL =
+            "carrier_promote_wfc_on_call_fail_bool";
+
     /** Flag specifying whether provisioning is required for VOLTE. */
     public static final String KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL
             = "carrier_volte_provisioning_required_bool";
@@ -420,18 +464,18 @@
             "always_show_emergency_alert_onoff_bool";
 
     /**
-     * The data call APN retry configuration for default type APN.
+     * The flag to disable cell broadcast severe alert when extreme alert is disabled.
      * @hide
      */
-    public static final String KEY_CARRIER_DATA_CALL_RETRY_CONFIG_DEFAULT_STRING =
-            "carrier_data_call_retry_config_default_string";
+    public static final String KEY_DISABLE_SEVERE_WHEN_EXTREME_DISABLED_BOOL =
+            "disable_severe_when_extreme_disabled_bool";
 
     /**
-     * The data call APN retry configuration for other type APNs.
+     * The data call retry configuration for different types of APN.
      * @hide
      */
-    public static final String KEY_CARRIER_DATA_CALL_RETRY_CONFIG_OTHERS_STRING =
-            "carrier_data_call_retry_config_others_string";
+    public static final String KEY_CARRIER_DATA_CALL_RETRY_CONFIG_STRINGS =
+            "carrier_data_call_retry_config_strings";
 
     /**
      * Delay between trying APN from the pool
@@ -558,6 +602,17 @@
     public static final String KEY_SUPPORT_CONFERENCE_CALL_BOOL = "support_conference_call_bool";
 
     /**
+     * Determines whether video conference calls are supported by a carrier.  When {@code true},
+     * video calls can be merged into conference calls, {@code false} otherwiwse.
+     * <p>
+     * Note: even if video conference calls are not supported, audio calls may be merged into a
+     * conference if {@link #KEY_SUPPORT_CONFERENCE_CALL_BOOL} is {@code true}.
+     * @hide
+     */
+    public static final String KEY_SUPPORT_VIDEO_CONFERENCE_CALL_BOOL =
+            "support_video_conference_call_bool";
+
+    /**
      * Determine whether user can toggle Enhanced 4G LTE Mode in Settings.
      */
     public static final String KEY_EDITABLE_ENHANCED_4G_LTE_BOOL = "editable_enhanced_4g_lte_bool";
@@ -610,6 +665,14 @@
     public static final String KEY_WFC_DATA_SPN_FORMAT_IDX_INT = "wfc_data_spn_format_idx_int";
 
     /**
+     * The Component Name of the activity that can setup the emergency addrees for WiFi Calling
+     * as per carrier requirement.
+     * @hide
+     */
+     public static final String KEY_WFC_EMERGENCY_ADDRESS_CARRIER_APP_STRING =
+            "wfc_emergency_address_carrier_app_string";
+
+    /**
      * Boolean to decide whether to use #KEY_CARRIER_NAME_STRING from CarrierConfig app.
      * @hide
      */
@@ -622,7 +685,6 @@
      */
     public static final String KEY_CARRIER_NAME_STRING = "carrier_name_string";
 
-
     /**
      * If this is true, the SIM card (through Customer Service Profile EF file) will be able to
      * prevent manual operator selection. If false, this SIM setting will be ignored and manual
@@ -637,12 +699,33 @@
     public static final String KEY_ALLOW_ADDING_APNS_BOOL = "allow_adding_apns_bool";
 
     /**
+     * APN types that user is not allowed to modify
+     * @hide
+     */
+    public static final String KEY_READ_ONLY_APN_TYPES_STRING_ARRAY =
+            "read_only_apn_types_string_array";
+
+    /**
+     * APN fields that user is not allowed to modify
+     * @hide
+     */
+    public static final String KEY_READ_ONLY_APN_FIELDS_STRING_ARRAY =
+            "read_only_apn_fields_string_array";
+
+    /**
      * Boolean indicating if intent for emergency call state changes should be broadcast
      * @hide
      */
     public static final String KEY_BROADCAST_EMERGENCY_CALL_STATE_CHANGES_BOOL =
             "broadcast_emergency_call_state_changes_bool";
 
+    /**
+     * Cell broadcast additional channels enbled by the carrier
+     * @hide
+     */
+    public static final String KEY_CARRIER_ADDITIONAL_CBS_CHANNELS_STRINGS =
+            "carrier_additional_cbs_channels_strings";
+
     // These variables are used by the MMS service and exposed through another API, {@link
     // SmsManager}. The variable names and string values are copied from there.
     public static final String KEY_MMS_ALIAS_ENABLED_BOOL = "aliasEnabled";
@@ -676,6 +759,8 @@
     public static final String KEY_MMS_UA_PROF_TAG_NAME_STRING = "uaProfTagName";
     public static final String KEY_MMS_UA_PROF_URL_STRING = "uaProfUrl";
     public static final String KEY_MMS_USER_AGENT_STRING = "userAgent";
+    /** @hide */
+    public static final String KEY_MMS_CLOSE_CONNECTION_BOOL = "mmsCloseConnection";
 
     /**
      * If carriers require differentiate un-provisioned status: cold sim or out of credit sim
@@ -686,10 +771,44 @@
      * example:
      * <item>com.google.android.carrierPackageName</item>
      * <item>com.google.android.carrierPackageName.CarrierActivityName</item>
+     * The ComponentName of the carrier activity that can setup the device and activate with the
+     * network as part of the Setup Wizard flow.
      * @hide
      */
-     public static final String KEY_SIM_PROVISIONING_STATUS_DETECTION_CARRIER_APP_STRING_ARRAY =
-            "sim_state_detection_carrier_app_string_array";
+     public static final String KEY_CARRIER_SETUP_APP_STRING = "carrier_setup_app_string";
+
+    /**
+     * A list of component name of carrier signalling receivers which are interested in intent
+     * android.intent.action.CARRIER_SIGNAL_REDIRECTED.
+     * Example:
+     * <item>com.google.android.carrierPackageName/.CarrierSignalReceiverNameA</item>
+     * <item>com.google.android.carrierPackageName/.CarrierSignalReceiverNameB</item>
+     * @hide
+     */
+    public static final String KEY_SIGNAL_REDIRECTION_RECEIVER_STRING_ARRAY =
+            "signal_redirection_receiver_string_array";
+
+    /**
+     * A list of component name of carrier signalling receivers which are interested in intent
+     * android.intent.action.CARRIER_SIGNAL_REQUEST_NETWORK_FAILED.
+     * Example:
+     * <item>com.google.android.carrierPackageName/.CarrierSignalReceiverNameA</item>
+     * <item>com.google.android.carrierPackageName/.CarrierSignalReceiverNameB</item>
+     * @hide
+     */
+    public static final String KEY_SIGNAL_DCFAILURE_RECEIVER_STRING_ARRAY =
+            "signal_dcfailure_receiver_string_array";
+
+    /**
+     * A list of component name of carrier signalling receivers which are interested in intent
+     * android.intent.action.CARRIER_SIGNAL_PCO_VALUE.
+     * Example:
+     * <item>com.google.android.carrierPackageName/.CarrierSignalReceiverNameA</item>
+     * <item>com.google.android.carrierPackageName/.CarrierSignalReceiverNameB</item>
+     * @hide
+     */
+    public static final String KEY_SIGNAL_PCO_RECEIVER_STRING_ARRAY =
+            "signal_pco_receiver_string_array";
 
     /**
      * Determines whether the carrier supports making non-emergency phone calls while the phone is
@@ -755,11 +874,154 @@
     /** @hide */
     public static final int CDMA_ROAMING_MODE_ANY = 2;
 
+    /**
+     * Report IMEI as device id even if it's a CDMA/LTE phone.
+     *
+     * @hide
+     */
+    public static final String KEY_FORCE_IMEI_BOOL = "force_imei_bool";
+
+    /**
+     * The families of Radio Access Technologies that will get clustered and ratcheted,
+     * ie, we will report transitions up within the family, but not down until we change
+     * cells.  This prevents flapping between base technologies and higher techs that are
+     * granted on demand within the cell.
+     * @hide
+     */
+    public static final String KEY_RATCHET_RAT_FAMILIES =
+            "ratchet_rat_families";
+
+    /**
+     * Flag indicating whether some telephony logic will treat a call which was formerly a video
+     * call as if it is still a video call.  When {@code true}:
+     * <p>
+     * Logic which will automatically drop a video call which takes place over WIFI when a
+     * voice call is answered (see {@link #KEY_DROP_VIDEO_CALL_WHEN_ANSWERING_AUDIO_CALL_BOOL}.
+     * <p>
+     * Logic which determines whether the user can use TTY calling.
+     */
+    public static final String KEY_TREAT_DOWNGRADED_VIDEO_CALLS_AS_VIDEO_CALLS_BOOL =
+            "treat_downgraded_video_calls_as_video_calls_bool";
+
+    /**
+     * When {@code true}, if the user is in an ongoing video call over WIFI and answers an incoming
+     * audio call, the video call will be disconnected before the audio call is answered.  This is
+     * in contrast to the usual expected behavior where a foreground video call would be put into
+     * the background and held when an incoming audio call is answered.
+     */
+    public static final String KEY_DROP_VIDEO_CALL_WHEN_ANSWERING_AUDIO_CALL_BOOL =
+            "drop_video_call_when_answering_audio_call_bool";
+
+    /**
+     * Flag indicating whether the carrier supports merging wifi calls when VoWIFI is disabled.
+     * This can happen in the case of a carrier which allows offloading video calls to WIFI
+     * separately of whether voice over wifi is enabled.  In such a scenario when two video calls
+     * are downgraded to voice, they remain over wifi.  However, if VoWIFI is disabled, these calls
+     * cannot be merged.
+     */
+    public static final String KEY_ALLOW_MERGE_WIFI_CALLS_WHEN_VOWIFI_OFF_BOOL =
+            "allow_merge_wifi_calls_when_vowifi_off_bool";
+
+    /**
+     * Flag indicating whether the carrier supports the Hold command while in an IMS call.
+     * <p>
+     * The device configuration value {@code config_device_respects_hold_carrier_config} ultimately
+     * controls whether this carrier configuration option is used.  Where
+     * {@code config_device_respects_hold_carrier_config} is false, the value of the
+     * {@link #KEY_ALLOW_HOLD_IN_IMS_CALL_BOOL} carrier configuration option is ignored.
+     * @hide
+     */
+    public static final String KEY_ALLOW_HOLD_IN_IMS_CALL_BOOL = "allow_hold_in_ims_call";
+
+    /**
+     * When true, indicates that adding a call is disabled when there is an ongoing video call
+     * or when there is an ongoing call on wifi which was downgraded from video and VoWifi is
+     * turned off.
+     */
+    public static final String KEY_ALLOW_ADD_CALL_DURING_VIDEO_CALL_BOOL =
+            "allow_add_call_during_video_call";
+
+    /**
+     * When true, indicates that the HD audio icon in the in-call screen should not be shown for
+     * VoWifi calls.
+     * @hide
+     */
+    public static final String KEY_WIFI_CALLS_CAN_BE_HD_AUDIO = "wifi_calls_can_be_hd_audio";
+
+    /**
+     * When true, indicates that the HD audio icon in the in-call screen should not be shown for
+     * video calls.
+     * @hide
+     */
+    public static final String KEY_VIDEO_CALLS_CAN_BE_HD_AUDIO = "video_calls_can_be_hd_audio";
+
+    /**
+     * Defines operator-specific {@link com.android.ims.ImsReasonInfo} mappings.
+     *
+     * Format: "ORIGINAL_CODE|MESSAGE|NEW_CODE"
+     * Where {@code ORIGINAL_CODE} corresponds to a {@link ImsReasonInfo#getCode()} code,
+     * {@code MESSAGE} corresponds to an expected {@link ImsReasonInfo#getExtraMessage()} string,
+     * and {@code NEW_CODE} is the new {@code ImsReasonInfo#CODE_*} which this combination of
+     * original code and message shall be remapped to.
+     *
+     * Example: "501|call completion elsewhere|1014"
+     * When the {@link ImsReasonInfo#getCode()} is {@link ImsReasonInfo#CODE_USER_TERMINATED} and
+     * the {@link ImsReasonInfo#getExtraMessage()} is {@code "call completion elsewhere"},
+     * {@link ImsReasonInfo#CODE_ANSWERED_ELSEWHERE} shall be used as the {@link ImsReasonInfo}
+     * code instead.
+     * @hide
+     */
+    public static final String KEY_IMS_REASONINFO_MAPPING_STRING_ARRAY =
+            "ims_reasoninfo_mapping_string_array";
+
+    /**
+     * When {@code false}, use default title for Enhanced 4G LTE Mode settings.
+     * When {@code true}, use the variant.
+     * @hide
+     */
+    public static final String KEY_ENHANCED_4G_LTE_TITLE_VARIANT_BOOL =
+            "enhanced_4g_lte_title_variant_bool";
+
+    /**
+     * Indicates whether the carrier wants to notify the user when handover of an LTE video call to
+     * WIFI fails.
+     * <p>
+     * When {@code true}, if a video call starts on LTE and the modem reports a failure to handover
+     * the call to WIFI or if no handover success is reported within 60 seconds of call initiation,
+     * the {@link android.telephony.TelephonyManager#EVENT_HANDOVER_TO_WIFI_FAILED} event is raised
+     * on the connection.
+     * @hide
+     */
+    public static final String KEY_NOTIFY_VT_HANDOVER_TO_WIFI_FAILURE_BOOL =
+            "notify_vt_handover_to_wifi_failure_bool";
+
+    /**
+     * A upper case list of CNAP names that are unhelpful to the user for distinguising calls and
+     * should be filtered out of the CNAP information. This includes CNAP names such as "WIRELESS
+     * CALLER" or "UNKNOWN NAME". By default, if there are no filtered names for this carrier, null
+     * is returned.
+     * @hide
+     */
+    public static final String FILTERED_CNAP_NAMES_STRING_ARRAY = "filtered_cnap_names_string_array";
+
+    /**
+     * Determine whether user can change Wi-Fi Calling preference in roaming.
+     * {@code false} - roaming preference {@link KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_MODE_INT} is
+     *                 the same as home preference {@link KEY_CARRIER_DEFAULT_WFC_IMS_MODE_INT}
+     *                 and cannot be changed.
+     * {@code true}  - roaming preference can be changed by user independently.
+     *
+     * @hide
+     */
+    public static final String KEY_EDITABLE_WFC_ROAMING_MODE_BOOL =
+            "editable_wfc_roaming_mode_bool";
+
     /** The default value for every variable. */
     private final static PersistableBundle sDefaults;
 
     static {
         sDefaults = new PersistableBundle();
+        sDefaults.putBoolean(KEY_ALLOW_HOLD_IN_IMS_CALL_BOOL, true);
         sDefaults.putBoolean(KEY_ADDITIONAL_CALL_SETTING_BOOL, true);
         sDefaults.putBoolean(KEY_ALLOW_EMERGENCY_NUMBERS_IN_CALL_LOG_BOOL, false);
         sDefaults.putBoolean(KEY_ALLOW_LOCAL_DTMF_TONES_BOOL, true);
@@ -768,11 +1030,15 @@
         sDefaults.putBoolean(KEY_CARRIER_SETTINGS_ENABLE_BOOL, false);
         sDefaults.putBoolean(KEY_CARRIER_VOLTE_AVAILABLE_BOOL, false);
         sDefaults.putBoolean(KEY_CARRIER_VT_AVAILABLE_BOOL, false);
+        sDefaults.putBoolean(KEY_NOTIFY_HANDOVER_VIDEO_FROM_WIFI_TO_LTE_BOOL, false);
+        sDefaults.putBoolean(KEY_SUPPORT_DOWNGRADE_VT_TO_AUDIO_BOOL, true);
         sDefaults.putBoolean(KEY_CARRIER_WFC_IMS_AVAILABLE_BOOL, false);
         sDefaults.putBoolean(KEY_CARRIER_WFC_SUPPORTS_WIFI_ONLY_BOOL, false);
         sDefaults.putBoolean(KEY_CARRIER_DEFAULT_WFC_IMS_ENABLED_BOOL, false);
         sDefaults.putBoolean(KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_ENABLED_BOOL, false);
+        sDefaults.putBoolean(KEY_CARRIER_PROMOTE_WFC_ON_CALL_FAIL_BOOL, false);
         sDefaults.putInt(KEY_CARRIER_DEFAULT_WFC_IMS_MODE_INT, 2);
+        sDefaults.putInt(KEY_CARRIER_DEFAULT_WFC_IMS_ROAMING_MODE_INT, 2);
         sDefaults.putBoolean(KEY_CARRIER_FORCE_DISABLE_ETWS_CMAS_TEST_BOOL, false);
         sDefaults.putBoolean(KEY_CARRIER_VOLTE_PROVISIONING_REQUIRED_BOOL, false);
         sDefaults.putBoolean(KEY_CARRIER_VOLTE_TTY_SUPPORTED_BOOL, true);
@@ -822,13 +1088,17 @@
         sDefaults.putString(KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING, "");
         sDefaults.putBoolean(KEY_CSP_ENABLED_BOOL, false);
         sDefaults.putBoolean(KEY_ALLOW_ADDING_APNS_BOOL, true);
+        sDefaults.putStringArray(KEY_READ_ONLY_APN_TYPES_STRING_ARRAY, null);
+        sDefaults.putStringArray(KEY_READ_ONLY_APN_FIELDS_STRING_ARRAY, null);
         sDefaults.putBoolean(KEY_BROADCAST_EMERGENCY_CALL_STATE_CHANGES_BOOL, false);
         sDefaults.putBoolean(KEY_ALWAYS_SHOW_EMERGENCY_ALERT_ONOFF_BOOL, false);
-        sDefaults.putString(KEY_CARRIER_DATA_CALL_RETRY_CONFIG_DEFAULT_STRING,
-                "default_randomization=2000,5000,10000,20000,40000,80000:5000,160000:5000,"
-                        + "320000:5000,640000:5000,1280000:5000,1800000:5000");
-        sDefaults.putString(KEY_CARRIER_DATA_CALL_RETRY_CONFIG_OTHERS_STRING,
-                "max_retries=3, 5000, 5000, 5000");
+        sDefaults.putBoolean(KEY_DISABLE_SEVERE_WHEN_EXTREME_DISABLED_BOOL, true);
+        sDefaults.putStringArray(KEY_CARRIER_DATA_CALL_RETRY_CONFIG_STRINGS, new String[]{
+                "default:default_randomization=2000,5000,10000,20000,40000,80000:5000,160000:5000,"
+                        + "320000:5000,640000:5000,1280000:5000,1800000:5000",
+                "mms:default_randomization=2000,5000,10000,20000,40000,80000:5000,160000:5000,"
+                        + "320000:5000,640000:5000,1280000:5000,1800000:5000",
+                "others:max_retries=3, 5000, 5000, 5000"});
         sDefaults.putLong(KEY_CARRIER_DATA_CALL_APN_DELAY_DEFAULT_LONG, 20000);
         sDefaults.putLong(KEY_CARRIER_DATA_CALL_APN_DELAY_FASTER_LONG, 3000);
         sDefaults.putString(KEY_CARRIER_ERI_FILE_NAME_STRING, "eri.xml");
@@ -849,6 +1119,7 @@
         sDefaults.putInt(KEY_CDMA_DTMF_TONE_DELAY_INT, 100);
         sDefaults.putInt(KEY_CDMA_3WAYCALL_FLASH_DELAY_INT , 0);
         sDefaults.putBoolean(KEY_SUPPORT_CONFERENCE_CALL_BOOL, true);
+        sDefaults.putBoolean(KEY_SUPPORT_VIDEO_CONFERENCE_CALL_BOOL, false);
         sDefaults.putBoolean(KEY_EDITABLE_ENHANCED_4G_LTE_BOOL, true);
         sDefaults.putBoolean(KEY_HIDE_IMS_APN_BOOL, false);
         sDefaults.putBoolean(KEY_HIDE_PREFERRED_NETWORK_TYPE_BOOL, false);
@@ -857,6 +1128,7 @@
         sDefaults.putStringArray(KEY_WFC_OPERATOR_ERROR_CODES_STRING_ARRAY, null);
         sDefaults.putInt(KEY_WFC_SPN_FORMAT_IDX_INT, 0);
         sDefaults.putInt(KEY_WFC_DATA_SPN_FORMAT_IDX_INT, 0);
+        sDefaults.putString(KEY_WFC_EMERGENCY_ADDRESS_CARRIER_APP_STRING, "");
         sDefaults.putBoolean(KEY_CONFIG_WIFI_DISABLE_IN_ECBM, false);
         sDefaults.putBoolean(KEY_CARRIER_NAME_OVERRIDE_BOOL, false);
         sDefaults.putString(KEY_CARRIER_NAME_STRING, "");
@@ -876,6 +1148,7 @@
         sDefaults.putBoolean(KEY_MMS_SMS_DELIVERY_REPORT_ENABLED_BOOL, true);
         sDefaults.putBoolean(KEY_MMS_SUPPORT_HTTP_CHARSET_HEADER_BOOL, false);
         sDefaults.putBoolean(KEY_MMS_SUPPORT_MMS_CONTENT_DISPOSITION_BOOL, true);
+        sDefaults.putBoolean(KEY_MMS_CLOSE_CONNECTION_BOOL, false);
         sDefaults.putInt(KEY_MMS_ALIAS_MAX_CHARS_INT, 48);
         sDefaults.putInt(KEY_MMS_ALIAS_MIN_CHARS_INT, 2);
         sDefaults.putInt(KEY_MMS_HTTP_SOCKET_TIMEOUT_INT, 60 * 1000);
@@ -895,11 +1168,32 @@
         sDefaults.putString(KEY_MMS_USER_AGENT_STRING, "");
         sDefaults.putBoolean(KEY_ALLOW_NON_EMERGENCY_CALLS_IN_ECM_BOOL, true);
         sDefaults.putBoolean(KEY_USE_RCS_PRESENCE_BOOL, false);
+        sDefaults.putBoolean(KEY_FORCE_IMEI_BOOL, false);
         sDefaults.putInt(KEY_CDMA_ROAMING_MODE_INT, CDMA_ROAMING_MODE_RADIO_DEFAULT);
 
-        // Used for Sim card State detection app
-        sDefaults.putStringArray(KEY_SIM_PROVISIONING_STATUS_DETECTION_CARRIER_APP_STRING_ARRAY,
-                null);
+        // Carrier Signalling Receivers
+        sDefaults.putStringArray(KEY_SIGNAL_REDIRECTION_RECEIVER_STRING_ARRAY, null);
+        sDefaults.putStringArray(KEY_SIGNAL_DCFAILURE_RECEIVER_STRING_ARRAY, null);
+        sDefaults.putStringArray(KEY_SIGNAL_PCO_RECEIVER_STRING_ARRAY, null);
+        sDefaults.putString(KEY_CARRIER_SETUP_APP_STRING, "");
+
+        // Rat families: {GPRS, EDGE}, {EVDO, EVDO_A, EVDO_B}, {UMTS, HSPA, HSDPA, HSUPA, HSPAP},
+        // {LTE, LTE_CA}
+        // Order is important - lowest precidence first
+        sDefaults.putStringArray(KEY_RATCHET_RAT_FAMILIES,
+                new String[]{"1,2","7,8,12","3,11,9,10,15","14,19"});
+        sDefaults.putBoolean(KEY_TREAT_DOWNGRADED_VIDEO_CALLS_AS_VIDEO_CALLS_BOOL, false);
+        sDefaults.putBoolean(KEY_DROP_VIDEO_CALL_WHEN_ANSWERING_AUDIO_CALL_BOOL, false);
+        sDefaults.putBoolean(KEY_ALLOW_MERGE_WIFI_CALLS_WHEN_VOWIFI_OFF_BOOL, true);
+        sDefaults.putBoolean(KEY_ALLOW_ADD_CALL_DURING_VIDEO_CALL_BOOL, true);
+        sDefaults.putBoolean(KEY_WIFI_CALLS_CAN_BE_HD_AUDIO, true);
+        sDefaults.putBoolean(KEY_VIDEO_CALLS_CAN_BE_HD_AUDIO, true);
+
+        sDefaults.putStringArray(KEY_IMS_REASONINFO_MAPPING_STRING_ARRAY, null);
+        sDefaults.putBoolean(KEY_ENHANCED_4G_LTE_TITLE_VARIANT_BOOL, false);
+        sDefaults.putBoolean(KEY_NOTIFY_VT_HANDOVER_TO_WIFI_FAILURE_BOOL, false);
+        sDefaults.putStringArray(FILTERED_CNAP_NAMES_STRING_ARRAY, null);
+        sDefaults.putBoolean(KEY_EDITABLE_WFC_ROAMING_MODE_BOOL, false);
     }
 
     /**
diff --git a/telephony/java/android/telephony/DisconnectCause.java b/telephony/java/android/telephony/DisconnectCause.java
index 9eb1304..0334254 100644
--- a/telephony/java/android/telephony/DisconnectCause.java
+++ b/telephony/java/android/telephony/DisconnectCause.java
@@ -194,6 +194,45 @@
      */
     public static final int VIDEO_CALL_NOT_ALLOWED_WHILE_TTY_ENABLED = 50;
 
+    /**
+     * The call was terminated because it was pulled to another device.
+     * {@hide}
+     */
+    public static final int CALL_PULLED = 51;
+
+    /**
+     * The call was terminated because it was answered on another device.
+     * {@hide}
+     */
+    public static final int ANSWERED_ELSEWHERE = 52;
+
+    /**
+     * The call was terminated because the maximum allowable number of calls has been reached.
+     * {@hide}
+     */
+    public static final int MAXIMUM_NUMBER_OF_CALLS_REACHED = 53;
+
+    /**
+     * The call was terminated because cellular data has been disabled.
+     * Used when in a video call and the user disables cellular data via the settings.
+     * {@hide}
+     */
+    public static final int DATA_DISABLED = 54;
+
+    /**
+     * The call was terminated because the data policy has disabled cellular data.
+     * Used when in a video call and the user has exceeded the device data limit.
+     * {@hide}
+     */
+    public static final int DATA_LIMIT_REACHED = 55;
+
+    /**
+     * The emergency call was terminated because it was dialed on the wrong SIM slot.
+     * The call needs to be redialed the other slot.
+     * {@hide}
+     */
+    public static final int DIALED_ON_WRONG_SLOT = 56;
+
     //*********************************************************************************************
     // When adding a disconnect type:
     // 1) Please assign the new type the next id value below.
@@ -202,14 +241,14 @@
     // 4) Update toString() with the newly added disconnect type.
     // 5) Update android.telecom.DisconnectCauseUtil with any mappings to a telecom.DisconnectCause.
     //
-    // NextId: 50
+    // NextId: 57
     //*********************************************************************************************
 
     /** Smallest valid value for call disconnect codes. */
     public static final int MINIMUM_VALID_VALUE = NOT_DISCONNECTED;
 
     /** Largest valid value for call disconnect codes. */
-    public static final int MAXIMUM_VALID_VALUE = VIDEO_CALL_NOT_ALLOWED_WHILE_TTY_ENABLED;
+    public static final int MAXIMUM_VALID_VALUE = DIALED_ON_WRONG_SLOT;
 
     /** Private constructor to avoid class instantiation. */
     private DisconnectCause() {
@@ -318,7 +357,19 @@
         case CDMA_ALREADY_ACTIVATED:
             return "CDMA_ALREADY_ACTIVATED";
         case VIDEO_CALL_NOT_ALLOWED_WHILE_TTY_ENABLED:
-                return "VIDEO_CALL_NOT_ALLOWED_WHILE_TTY_ENABLED";
+            return "VIDEO_CALL_NOT_ALLOWED_WHILE_TTY_ENABLED";
+        case CALL_PULLED:
+            return "CALL_PULLED";
+        case ANSWERED_ELSEWHERE:
+            return "ANSWERED_ELSEWHERE";
+        case MAXIMUM_NUMBER_OF_CALLS_REACHED:
+            return "MAXIMUM_NUMER_OF_CALLS_REACHED";
+        case DATA_DISABLED:
+            return "DATA_DISABLED";
+        case DATA_LIMIT_REACHED:
+            return "DATA_LIMIT_REACHED";
+        case DIALED_ON_WRONG_SLOT:
+            return "DIALED_ON_WRONG_SLOT";
         default:
             return "INVALID: " + cause;
         }
diff --git a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl b/telephony/java/android/telephony/PcoData.aidl
similarity index 82%
copy from core/java/com/android/internal/app/EphemeralResolveInfo.aidl
copy to telephony/java/android/telephony/PcoData.aidl
index 529527b..a93b8d3 100644
--- a/core/java/com/android/internal/app/EphemeralResolveInfo.aidl
+++ b/telephony/java/android/telephony/PcoData.aidl
@@ -1,5 +1,6 @@
 /*
-** Copyright 2015, The Android Open Source Project
+**
+** Copyright (C) 2016 The Android Open Source Project
 **
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
@@ -14,6 +15,7 @@
 ** limitations under the License.
 */
 
-package com.android.internal.app;
+package android.telephony;
 
-parcelable EphemeralResolveInfo;
+parcelable PcoData;
+
diff --git a/telephony/java/android/telephony/PcoData.java b/telephony/java/android/telephony/PcoData.java
new file mode 100644
index 0000000..3e735e7
--- /dev/null
+++ b/telephony/java/android/telephony/PcoData.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.telephony;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+
+/**
+ * Contains Carrier-specific (and opaque) Protocol configuration Option
+ * Data.  In general this is only passed on to carrier-specific applications
+ * for interpretation.
+ *
+ * @hide
+ */
+public class PcoData implements Parcelable {
+
+    public final int cid;
+    public final String bearerProto;
+    public final int pcoId;
+    public final byte[] contents;
+
+    public PcoData(int cid, String bearerProto, int pcoId, byte[]contents) {
+        this.cid = cid;
+        this.bearerProto = bearerProto;
+        this.pcoId = pcoId;
+        this.contents = contents;
+    }
+
+    public PcoData(Parcel in) {
+        cid = in.readInt();
+        bearerProto = in.readString();
+        pcoId = in.readInt();
+        contents = in.createByteArray();
+    }
+
+    /**
+     * {@link Parcelable#writeToParcel}
+     */
+    public void writeToParcel(Parcel out, int flags) {
+        out.writeInt(cid);
+        out.writeString(bearerProto);
+        out.writeInt(pcoId);
+        out.writeByteArray(contents);
+    }
+
+    /**
+     * {@link Parcelable#describeContents}
+     */
+    public int describeContents() {
+        return 0;
+    }
+
+    /**
+     * {@link Parcelable.Creator}
+     *
+     * @hide
+     */
+    public static final Parcelable.Creator<PcoData> CREATOR = new Parcelable.Creator() {
+        public PcoData createFromParcel(Parcel in) {
+            return new PcoData(in);
+        }
+
+        public PcoData[] newArray(int size) {
+            return new PcoData[size];
+        }
+    };
+
+    @Override
+    public String toString() {
+        return "PcoData(" + cid + ", " + bearerProto + ", " + pcoId + ", contents[" +
+                contents.length + "])";
+    }
+}
diff --git a/telephony/java/android/telephony/PhoneNumberUtils.java b/telephony/java/android/telephony/PhoneNumberUtils.java
index 3c30251..152b868 100644
--- a/telephony/java/android/telephony/PhoneNumberUtils.java
+++ b/telephony/java/android/telephony/PhoneNumberUtils.java
@@ -24,12 +24,14 @@
 
 import android.content.Context;
 import android.content.Intent;
+import android.content.res.Resources;
 import android.database.Cursor;
 import android.location.CountryDetector;
 import android.net.Uri;
 import android.os.SystemProperties;
 import android.provider.Contacts;
 import android.provider.ContactsContract;
+import android.telecom.PhoneAccount;
 import android.text.Editable;
 import android.text.Spannable;
 import android.text.SpannableStringBuilder;
@@ -2599,6 +2601,48 @@
     }
 
     /**
+     * Given a {@link Uri} with a {@code sip} scheme, attempts to build an equivalent {@code tel}
+     * scheme {@link Uri}.  If the source {@link Uri} does not contain a valid number, or is not
+     * using the {@code sip} scheme, the original {@link Uri} is returned.
+     *
+     * @param source The {@link Uri} to convert.
+     * @return The equivalent {@code tel} scheme {@link Uri}.
+     *
+     * @hide
+     */
+    public static Uri convertSipUriToTelUri(Uri source) {
+        // A valid SIP uri has the format: sip:user:password@host:port;uri-parameters?headers
+        // Per RFC3261, the "user" can be a telephone number.
+        // For example: sip:1650555121;phone-context=blah.com@host.com
+        // In this case, the phone number is in the user field of the URI, and the parameters can be
+        // ignored.
+        //
+        // A SIP URI can also specify a phone number in a format similar to:
+        // sip:+1-212-555-1212@something.com;user=phone
+        // In this case, the phone number is again in user field and the parameters can be ignored.
+        // We can get the user field in these instances by splitting the string on the @, ;, or :
+        // and looking at the first found item.
+
+        String scheme = source.getScheme();
+
+        if (!PhoneAccount.SCHEME_SIP.equals(scheme)) {
+            // Not a sip URI, bail.
+            return source;
+        }
+
+        String number = source.getSchemeSpecificPart();
+        String numberParts[] = number.split("[@;:]");
+
+        if (numberParts.length == 0) {
+            // Number not found, bail.
+            return source;
+        }
+        number = numberParts[0];
+
+        return Uri.fromParts(PhoneAccount.SCHEME_TEL, number, null);
+    }
+
+    /**
      * This function handles the plus code conversion
      * If the number format is
      * 1)+1NANP,remove +,
@@ -2978,4 +3022,79 @@
         return SubscriptionManager.getDefaultVoiceSubscriptionId();
     }
     //==== End of utility methods used only in compareStrictly() =====
+
+
+    /*
+     * The config held calling number conversion map, expected to convert to emergency number.
+     */
+    private static final String[] CONVERT_TO_EMERGENCY_MAP = Resources.getSystem().getStringArray(
+            com.android.internal.R.array.config_convert_to_emergency_number_map);
+    /**
+     * Check whether conversion to emergency number is enabled
+     *
+     * @return {@code true} when conversion to emergency numbers is enabled,
+     *         {@code false} otherwise
+     *
+     * @hide
+     */
+    public static boolean isConvertToEmergencyNumberEnabled() {
+        return CONVERT_TO_EMERGENCY_MAP != null && CONVERT_TO_EMERGENCY_MAP.length > 0;
+    }
+
+    /**
+     * Converts to emergency number based on the conversion map.
+     * The conversion map is declared as config_convert_to_emergency_number_map.
+     *
+     * Make sure {@link #isConvertToEmergencyNumberEnabled} is true before calling
+     * this function.
+     *
+     * @return The converted emergency number if the number matches conversion map,
+     * otherwise original number.
+     *
+     * @hide
+     */
+    public static String convertToEmergencyNumber(String number) {
+        if (TextUtils.isEmpty(number)) {
+            return number;
+        }
+
+        String normalizedNumber = normalizeNumber(number);
+
+        // The number is already emergency number. Skip conversion.
+        if (isEmergencyNumber(normalizedNumber)) {
+            return number;
+        }
+
+        for (String convertMap : CONVERT_TO_EMERGENCY_MAP) {
+            if (DBG) log("convertToEmergencyNumber: " + convertMap);
+            String[] entry = null;
+            String[] filterNumbers = null;
+            String convertedNumber = null;
+            if (!TextUtils.isEmpty(convertMap)) {
+                entry = convertMap.split(":");
+            }
+            if (entry != null && entry.length == 2) {
+                convertedNumber = entry[1];
+                if (!TextUtils.isEmpty(entry[0])) {
+                    filterNumbers = entry[0].split(",");
+                }
+            }
+            // Skip if the format of entry is invalid
+            if (TextUtils.isEmpty(convertedNumber) || filterNumbers == null
+                    || filterNumbers.length == 0) {
+                continue;
+            }
+
+            for (String filterNumber : filterNumbers) {
+                if (DBG) log("convertToEmergencyNumber: filterNumber = " + filterNumber
+                        + ", convertedNumber = " + convertedNumber);
+                if (!TextUtils.isEmpty(filterNumber) && filterNumber.equals(normalizedNumber)) {
+                    if (DBG) log("convertToEmergencyNumber: Matched. Successfully converted to: "
+                            + convertedNumber);
+                    return convertedNumber;
+                }
+            }
+        }
+        return number;
+    }
 }
diff --git a/telephony/java/android/telephony/RadioAccessFamily.java b/telephony/java/android/telephony/RadioAccessFamily.java
index 2bfaf1b..d657bae 100644
--- a/telephony/java/android/telephony/RadioAccessFamily.java
+++ b/telephony/java/android/telephony/RadioAccessFamily.java
@@ -29,31 +29,39 @@
 public class RadioAccessFamily implements Parcelable {
 
     // Radio Access Family
+    // 2G
     public static final int RAF_UNKNOWN = (1 <<  ServiceState.RIL_RADIO_TECHNOLOGY_UNKNOWN);
+    public static final int RAF_GSM = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_GSM);
     public static final int RAF_GPRS = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_GPRS);
     public static final int RAF_EDGE = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EDGE);
-    public static final int RAF_UMTS = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_UMTS);
     public static final int RAF_IS95A = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_IS95A);
     public static final int RAF_IS95B = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_IS95B);
     public static final int RAF_1xRTT = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT);
+    // 3G
     public static final int RAF_EVDO_0 = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_0);
     public static final int RAF_EVDO_A = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_A);
-    public static final int RAF_HSDPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSDPA);
-    public static final int RAF_HSUPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSUPA);
-    public static final int RAF_HSPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSPA);
     public static final int RAF_EVDO_B = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_B);
     public static final int RAF_EHRPD = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD);
-    public static final int RAF_LTE = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
+    public static final int RAF_HSUPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSUPA);
+    public static final int RAF_HSDPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSDPA);
+    public static final int RAF_HSPA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSPA);
     public static final int RAF_HSPAP = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_HSPAP);
-    public static final int RAF_GSM = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_GSM);
+    public static final int RAF_UMTS = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_UMTS);
     public static final int RAF_TD_SCDMA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_TD_SCDMA);
+    // 4G
+    public static final int RAF_LTE = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
+    public static final int RAF_LTE_CA = (1 << ServiceState.RIL_RADIO_TECHNOLOGY_LTE_CA);
 
     // Grouping of RAFs
+    // 2G
     private static final int GSM = RAF_GSM | RAF_GPRS | RAF_EDGE;
-    private static final int HS = RAF_HSUPA | RAF_HSDPA | RAF_HSPA | RAF_HSPAP;
     private static final int CDMA = RAF_IS95A | RAF_IS95B | RAF_1xRTT;
+    // 3G
     private static final int EVDO = RAF_EVDO_0 | RAF_EVDO_A | RAF_EVDO_B | RAF_EHRPD;
+    private static final int HS = RAF_HSUPA | RAF_HSDPA | RAF_HSPA | RAF_HSPAP;
     private static final int WCDMA = HS | RAF_UMTS;
+    // 4G
+    private static final int LTE = RAF_LTE | RAF_LTE_CA;
 
     /* Phone ID of phone */
     private int mPhoneId;
@@ -162,19 +170,19 @@
                 raf = CDMA | EVDO;
                 break;
             case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO:
-                raf = RAF_LTE | CDMA | EVDO;
+                raf = LTE | CDMA | EVDO;
                 break;
             case RILConstants.NETWORK_MODE_LTE_GSM_WCDMA:
-                raf = RAF_LTE | GSM | WCDMA;
+                raf = LTE | GSM | WCDMA;
                 break;
             case RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA:
-                raf = RAF_LTE | CDMA | EVDO | GSM | WCDMA;
+                raf = LTE | CDMA | EVDO | GSM | WCDMA;
                 break;
             case RILConstants.NETWORK_MODE_LTE_ONLY:
-                raf = RAF_LTE;
+                raf = LTE;
                 break;
             case RILConstants.NETWORK_MODE_LTE_WCDMA:
-                raf = RAF_LTE | WCDMA;
+                raf = LTE | WCDMA;
                 break;
             case RILConstants.NETWORK_MODE_CDMA_NO_EVDO:
                 raf = CDMA;
@@ -192,28 +200,28 @@
                 raf = RAF_TD_SCDMA | WCDMA;
                 break;
             case RILConstants.NETWORK_MODE_LTE_TDSCDMA:
-                raf = RAF_LTE | RAF_TD_SCDMA;
+                raf = LTE | RAF_TD_SCDMA;
                 break;
             case RILConstants.NETWORK_MODE_TDSCDMA_GSM:
                 raf = RAF_TD_SCDMA | GSM;
                 break;
             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM:
-                raf = RAF_LTE | RAF_TD_SCDMA | GSM;
+                raf = LTE | RAF_TD_SCDMA | GSM;
                 break;
             case RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA:
                 raf = RAF_TD_SCDMA | GSM | WCDMA;
                 break;
             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA:
-                raf = RAF_LTE | RAF_TD_SCDMA | WCDMA;
+                raf = LTE | RAF_TD_SCDMA | WCDMA;
                 break;
             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA:
-                raf = RAF_LTE | RAF_TD_SCDMA | GSM | WCDMA;
+                raf = LTE | RAF_TD_SCDMA | GSM | WCDMA;
                 break;
             case RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
                 raf = RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA;
                 break;
             case RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA:
-                raf = RAF_LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA;
+                raf = LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA;
                 break;
             default:
                 raf = RAF_UNKNOWN;
@@ -232,10 +240,29 @@
         raf = ((WCDMA & raf) > 0) ? (WCDMA | raf) : raf;
         raf = ((CDMA & raf) > 0) ? (CDMA | raf) : raf;
         raf = ((EVDO & raf) > 0) ? (EVDO | raf) : raf;
+        raf = ((LTE & raf) > 0) ? (LTE | raf) : raf;
 
         return raf;
     }
 
+    /**
+     * Returns the highest capability of the RadioAccessFamily (4G > 3G > 2G).
+     * @param raf The RadioAccessFamily that we wish to filter
+     * @return The highest radio capability
+     */
+    public static int getHighestRafCapability(int raf) {
+        if ((LTE & raf) > 0) {
+            return TelephonyManager.NETWORK_CLASS_4_G;
+        }
+        if ((EVDO|HS|WCDMA & raf) > 0) {
+            return TelephonyManager.NETWORK_CLASS_3_G;
+        }
+        if((GSM|CDMA & raf) > 0) {
+            return TelephonyManager.NETWORK_CLASS_2_G;
+        }
+        return TelephonyManager.NETWORK_CLASS_UNKNOWN;
+    }
+
     public static int getNetworkTypeFromRaf(int raf) {
         int type;
 
@@ -254,19 +281,19 @@
             case (CDMA | EVDO):
                 type = RILConstants.NETWORK_MODE_CDMA;
                 break;
-            case (RAF_LTE | CDMA | EVDO):
+            case (LTE | CDMA | EVDO):
                 type = RILConstants.NETWORK_MODE_LTE_CDMA_EVDO;
                 break;
-            case (RAF_LTE | GSM | WCDMA):
+            case (LTE | GSM | WCDMA):
                 type = RILConstants.NETWORK_MODE_LTE_GSM_WCDMA;
                 break;
-            case (RAF_LTE | CDMA | EVDO | GSM | WCDMA):
+            case (LTE | CDMA | EVDO | GSM | WCDMA):
                 type = RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA;
                 break;
-            case RAF_LTE:
+            case LTE:
                 type = RILConstants.NETWORK_MODE_LTE_ONLY;
                 break;
-            case (RAF_LTE | WCDMA):
+            case (LTE | WCDMA):
                 type = RILConstants.NETWORK_MODE_LTE_WCDMA;
                 break;
             case CDMA:
@@ -284,28 +311,28 @@
             case (RAF_TD_SCDMA | WCDMA):
                 type = RILConstants.NETWORK_MODE_TDSCDMA_WCDMA;
                 break;
-            case (RAF_LTE | RAF_TD_SCDMA):
+            case (LTE | RAF_TD_SCDMA):
                 type = RILConstants.NETWORK_MODE_LTE_TDSCDMA;
                 break;
             case (RAF_TD_SCDMA | GSM):
                 type = RILConstants.NETWORK_MODE_TDSCDMA_GSM;
                 break;
-            case (RAF_LTE | RAF_TD_SCDMA | GSM):
+            case (LTE | RAF_TD_SCDMA | GSM):
                 type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM;
                 break;
             case (RAF_TD_SCDMA | GSM | WCDMA):
                 type = RILConstants.NETWORK_MODE_TDSCDMA_GSM_WCDMA;
                 break;
-            case (RAF_LTE | RAF_TD_SCDMA | WCDMA):
+            case (LTE | RAF_TD_SCDMA | WCDMA):
                 type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_WCDMA;
                 break;
-            case (RAF_LTE | RAF_TD_SCDMA | GSM | WCDMA):
+            case (LTE | RAF_TD_SCDMA | GSM | WCDMA):
                 type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_GSM_WCDMA;
                 break;
             case (RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
                 type = RILConstants.NETWORK_MODE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
                 break;
-            case (RAF_LTE | RAF_TD_SCDMA | RAF_LTE | CDMA | EVDO | GSM | WCDMA):
+            case (LTE | RAF_TD_SCDMA | CDMA | EVDO | GSM | WCDMA):
                 type = RILConstants.NETWORK_MODE_LTE_TDSCDMA_CDMA_EVDO_GSM_WCDMA;
                 break;
             default:
@@ -339,6 +366,7 @@
             case "CDMA":    return CDMA;
             case "EVDO":    return EVDO;
             case "WCDMA":   return WCDMA;
+            case "LTE_CA":  return RAF_LTE_CA;
             default:        return RAF_UNKNOWN;
         }
     }
diff --git a/telephony/java/android/telephony/Rlog.java b/telephony/java/android/telephony/Rlog.java
index 2a7f7af..cd0a012 100644
--- a/telephony/java/android/telephony/Rlog.java
+++ b/telephony/java/android/telephony/Rlog.java
@@ -16,8 +16,15 @@
 
 package android.telephony;
 
+import android.text.TextUtils;
 import android.util.Log;
 
+import android.util.Base64;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+
 /**
  * A class to log strings to the RADIO LOG.
  *
@@ -85,5 +92,54 @@
         return Log.isLoggable(tag, level);
     }
 
+    /**
+     * Redact personally identifiable information for production users.
+     * @param tag used to identify the source of a log message
+     * @param pii the personally identifiable information we want to apply secure hash on.
+     * @return If tag is loggable in verbose mode or pii is null, return the original input.
+     * otherwise return a secure Hash of input pii
+     */
+    public static String pii(String tag, Object pii) {
+        String val = String.valueOf(pii);
+        if (pii == null || TextUtils.isEmpty(val) || isLoggable(tag, Log.VERBOSE)) {
+            return val;
+        }
+        return "[" + secureHash(val.getBytes()) + "]";
+    }
+
+    /**
+     * Redact personally identifiable information for production users.
+     * @param enablePiiLogging set when caller explicitly want to enable sensitive logging.
+     * @param pii the personally identifiable information we want to apply secure hash on.
+     * @return If enablePiiLogging is set to true or pii is null, return the original input.
+     * otherwise return a secure Hash of input pii
+     */
+    public static String pii(boolean enablePiiLogging, Object pii) {
+        String val = String.valueOf(pii);
+        if (pii == null || TextUtils.isEmpty(val) || enablePiiLogging) {
+            return val;
+        }
+        return "[" + secureHash(val.getBytes()) + "]";
+    }
+
+    /**
+     * Returns a secure hash (using the SHA1 algorithm) of the provided input.
+     *
+     * @return the hash
+     * @param input the bytes for which the secure hash should be computed.
+     */
+    private static String secureHash(byte[] input) {
+        MessageDigest messageDigest;
+
+        try {
+            messageDigest = MessageDigest.getInstance("SHA-1");
+        } catch (NoSuchAlgorithmException e) {
+            return "####";
+        }
+
+        byte[] result = messageDigest.digest(input);
+        return Base64.encodeToString(
+                result, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP);
+    }
 }
 
diff --git a/telephony/java/android/telephony/ServiceState.java b/telephony/java/android/telephony/ServiceState.java
index ea87fa3..11d84c1 100644
--- a/telephony/java/android/telephony/ServiceState.java
+++ b/telephony/java/android/telephony/ServiceState.java
@@ -155,6 +155,12 @@
      */
     public static final int RIL_RADIO_TECHNOLOGY_IWLAN = 18;
 
+    /**
+     * LTE_CA
+     * @hide
+     */
+    public static final int RIL_RADIO_TECHNOLOGY_LTE_CA = 19;
+
     /** @hide */
     public static final int RIL_RADIO_CDMA_TECHNOLOGY_BITMASK =
             (1 << (RIL_RADIO_TECHNOLOGY_IS95A - 1))
@@ -234,6 +240,8 @@
 
     private boolean mIsDataRoamingFromRegistration;
 
+    private boolean mIsUsingCarrierAggregation;
+
     /**
      * get String description of roaming type
      * @hide
@@ -312,6 +320,7 @@
         mCdmaEriIconMode = s.mCdmaEriIconMode;
         mIsEmergencyOnly = s.mIsEmergencyOnly;
         mIsDataRoamingFromRegistration = s.mIsDataRoamingFromRegistration;
+        mIsUsingCarrierAggregation = s.mIsUsingCarrierAggregation;
     }
 
     /**
@@ -340,6 +349,7 @@
         mCdmaEriIconMode = in.readInt();
         mIsEmergencyOnly = in.readInt() != 0;
         mIsDataRoamingFromRegistration = in.readInt() != 0;
+        mIsUsingCarrierAggregation = in.readInt() != 0;
     }
 
     public void writeToParcel(Parcel out, int flags) {
@@ -365,6 +375,7 @@
         out.writeInt(mCdmaEriIconMode);
         out.writeInt(mIsEmergencyOnly ? 1 : 0);
         out.writeInt(mIsDataRoamingFromRegistration ? 1 : 0);
+        out.writeInt(mIsUsingCarrierAggregation ? 1 : 0);
     }
 
     public int describeContents() {
@@ -674,7 +685,8 @@
                 && equalsHandlesNulls(mCdmaDefaultRoamingIndicator,
                         s.mCdmaDefaultRoamingIndicator)
                 && mIsEmergencyOnly == s.mIsEmergencyOnly
-                && mIsDataRoamingFromRegistration == s.mIsDataRoamingFromRegistration);
+                && mIsDataRoamingFromRegistration == s.mIsDataRoamingFromRegistration
+                && mIsUsingCarrierAggregation == s.mIsUsingCarrierAggregation);
     }
 
     /**
@@ -746,6 +758,9 @@
             case RIL_RADIO_TECHNOLOGY_TD_SCDMA:
                 rtString = "TD-SCDMA";
                 break;
+            case RIL_RADIO_TECHNOLOGY_LTE_CA:
+                rtString = "LTE_CA";
+                break;
             default:
                 rtString = "Unexpected";
                 Rlog.w(LOG_TAG, "Unexpected radioTechnology=" + rt);
@@ -779,7 +794,8 @@
                 + " RoamInd=" + mCdmaRoamingIndicator
                 + " DefRoamInd=" + mCdmaDefaultRoamingIndicator
                 + " EmergOnly=" + mIsEmergencyOnly
-                + " IsDataRoamingFromRegistration=" + mIsDataRoamingFromRegistration);
+                + " IsDataRoamingFromRegistration=" + mIsDataRoamingFromRegistration
+                + " IsUsingCarrierAggregation=" + mIsUsingCarrierAggregation);
     }
 
     private void setNullState(int state) {
@@ -806,6 +822,7 @@
         mCdmaEriIconMode = -1;
         mIsEmergencyOnly = false;
         mIsDataRoamingFromRegistration = false;
+        mIsUsingCarrierAggregation = false;
     }
 
     public void setStateOutOfService() {
@@ -979,6 +996,7 @@
         mCdmaDefaultRoamingIndicator = m.getInt("cdmaDefaultRoamingIndicator");
         mIsEmergencyOnly = m.getBoolean("emergencyOnly");
         mIsDataRoamingFromRegistration = m.getBoolean("isDataRoamingFromRegistration");
+        mIsUsingCarrierAggregation = m.getBoolean("isUsingCarrierAggregation");
     }
 
     /**
@@ -1008,21 +1026,42 @@
         m.putInt("cdmaDefaultRoamingIndicator", mCdmaDefaultRoamingIndicator);
         m.putBoolean("emergencyOnly", mIsEmergencyOnly);
         m.putBoolean("isDataRoamingFromRegistration", mIsDataRoamingFromRegistration);
+        m.putBoolean("isUsingCarrierAggregation", mIsUsingCarrierAggregation);
     }
 
     /** @hide */
     public void setRilVoiceRadioTechnology(int rt) {
+        if (rt == RIL_RADIO_TECHNOLOGY_LTE_CA) {
+            rt = RIL_RADIO_TECHNOLOGY_LTE;
+        }
+
         this.mRilVoiceRadioTechnology = rt;
     }
 
     /** @hide */
     public void setRilDataRadioTechnology(int rt) {
+        if (rt == RIL_RADIO_TECHNOLOGY_LTE_CA) {
+            rt = RIL_RADIO_TECHNOLOGY_LTE;
+            this.mIsUsingCarrierAggregation = true;
+        } else {
+            this.mIsUsingCarrierAggregation = false;
+        }
         this.mRilDataRadioTechnology = rt;
         if (VDBG) Rlog.d(LOG_TAG, "[ServiceState] setRilDataRadioTechnology=" +
                 mRilDataRadioTechnology);
     }
 
     /** @hide */
+    public boolean isUsingCarrierAggregation() {
+        return mIsUsingCarrierAggregation;
+    }
+
+    /** @hide */
+    public void setIsUsingCarrierAggregation(boolean ca) {
+        mIsUsingCarrierAggregation = ca;
+    }
+
+    /** @hide */
     public void setCssIndicator(int css) {
         this.mCssIndicator = (css != 0);
     }
@@ -1088,6 +1127,8 @@
             return TelephonyManager.NETWORK_TYPE_TD_SCDMA;
         case ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN:
             return TelephonyManager.NETWORK_TYPE_IWLAN;
+        case ServiceState.RIL_RADIO_TECHNOLOGY_LTE_CA:
+            return TelephonyManager.NETWORK_TYPE_LTE_CA;
         default:
             return TelephonyManager.NETWORK_TYPE_UNKNOWN;
         }
@@ -1139,7 +1180,9 @@
                 || radioTechnology == RIL_RADIO_TECHNOLOGY_HSPAP
                 || radioTechnology == RIL_RADIO_TECHNOLOGY_GSM
                 || radioTechnology == RIL_RADIO_TECHNOLOGY_TD_SCDMA
-                || radioTechnology == RIL_RADIO_TECHNOLOGY_IWLAN;
+                || radioTechnology == RIL_RADIO_TECHNOLOGY_IWLAN
+                || radioTechnology == RIL_RADIO_TECHNOLOGY_LTE_CA;
+
     }
 
     /** @hide */
@@ -1154,6 +1197,12 @@
     }
 
     /** @hide */
+    public static boolean isLte(int radioTechnology) {
+        return radioTechnology == RIL_RADIO_TECHNOLOGY_LTE ||
+                radioTechnology == RIL_RADIO_TECHNOLOGY_LTE_CA;
+    }
+
+    /** @hide */
     public static boolean bearerBitmapHasCdma(int radioTechnologyBitmap) {
         return (RIL_RADIO_CDMA_TECHNOLOGY_BITMASK & radioTechnologyBitmap) != 0;
     }
diff --git a/telephony/java/android/telephony/SubscriptionInfo.java b/telephony/java/android/telephony/SubscriptionInfo.java
index b5cf212..cf2d27e 100644
--- a/telephony/java/android/telephony/SubscriptionInfo.java
+++ b/telephony/java/android/telephony/SubscriptionInfo.java
@@ -90,14 +90,6 @@
     private int mDataRoaming;
 
     /**
-     * Sim Provisioning Status:
-     * {@See SubscriptionManager#SIM_PROVISIONED}
-     * {@See SubscriptionManager#SIM_UNPROVISIONED_COLD}
-     * {@See SubscriptionManager#SIM_UNPROVISIONED_OUT_OF_CREDIT}
-     */
-    private int mSimProvisioningStatus;
-
-    /**
      * SIM Icon bitmap
      */
     private Bitmap mIconBitmap;
@@ -122,7 +114,7 @@
      */
     public SubscriptionInfo(int id, String iccId, int simSlotIndex, CharSequence displayName,
             CharSequence carrierName, int nameSource, int iconTint, String number, int roaming,
-            Bitmap icon, int mcc, int mnc, String countryIso, int simProvisioningStatus) {
+            Bitmap icon, int mcc, int mnc, String countryIso) {
         this.mId = id;
         this.mIccId = iccId;
         this.mSimSlotIndex = simSlotIndex;
@@ -136,7 +128,6 @@
         this.mMcc = mcc;
         this.mMnc = mnc;
         this.mCountryIso = countryIso;
-        this.mSimProvisioningStatus = simProvisioningStatus;
     }
 
     /**
@@ -273,17 +264,6 @@
     }
 
     /**
-     * @return Sim Provisioning Status
-     * {@See SubscriptionManager#SIM_PROVISIONED}
-     * {@See SubscriptionManager#SIM_UNPROVISIONED_COLD}
-     * {@See SubscriptionManager#SIM_UNPROVISIONED_OUT_OF_CREDIT}
-     * @hide
-     */
-    public int getSimProvisioningStatus() {
-        return this.mSimProvisioningStatus;
-    }
-
-    /**
      * @return the MCC.
      */
     public int getMcc() {
@@ -319,12 +299,10 @@
             int mcc = source.readInt();
             int mnc = source.readInt();
             String countryIso = source.readString();
-            int simProvisioningStatus = source.readInt();
             Bitmap iconBitmap = Bitmap.CREATOR.createFromParcel(source);
 
             return new SubscriptionInfo(id, iccId, simSlotIndex, displayName, carrierName,
-                    nameSource, iconTint, number, dataRoaming, iconBitmap, mcc, mnc, countryIso,
-                    simProvisioningStatus);
+                    nameSource, iconTint, number, dataRoaming, iconBitmap, mcc, mnc, countryIso);
         }
 
         @Override
@@ -347,7 +325,6 @@
         dest.writeInt(mMcc);
         dest.writeInt(mMnc);
         dest.writeString(mCountryIso);
-        dest.writeInt(mSimProvisioningStatus);
         mIconBitmap.writeToParcel(dest, flags);
     }
 
@@ -363,7 +340,7 @@
         String iccIdToPrint = null;
         if (iccId != null) {
             if (iccId.length() > 9 && !Build.IS_DEBUGGABLE) {
-                iccIdToPrint = iccId.substring(0, 9) + "XXXXXXXXXXX";
+                iccIdToPrint = iccId.substring(0, 9) + Rlog.pii(false, iccId.substring(9));
             } else {
                 iccIdToPrint = iccId;
             }
@@ -378,6 +355,6 @@
                 + " displayName=" + mDisplayName + " carrierName=" + mCarrierName
                 + " nameSource=" + mNameSource + " iconTint=" + mIconTint
                 + " dataRoaming=" + mDataRoaming + " iconBitmap=" + mIconBitmap + " mcc " + mMcc
-                + " mnc " + mMnc + " SimProvisioningStatus " + mSimProvisioningStatus +"}";
+                + " mnc " + mMnc + "}";
     }
 }
diff --git a/telephony/java/android/telephony/SubscriptionManager.java b/telephony/java/android/telephony/SubscriptionManager.java
index c49966a..dd6f9cb 100644
--- a/telephony/java/android/telephony/SubscriptionManager.java
+++ b/telephony/java/android/telephony/SubscriptionManager.java
@@ -232,25 +232,12 @@
     /** Indicates that data roaming is disabled for a subscription */
     public static final int DATA_ROAMING_DISABLE = 0;
 
-    /** Sim provisioning status: provisioned */
-    /** @hide */
-    public static final int SIM_PROVISIONED = 0;
-
-    /** Sim provisioning status: un-provisioned due to cold sim */
-    /** @hide */
-    public static final int SIM_UNPROVISIONED_COLD = 1;
-
-    /** Sim provisioning status: un-provisioned due to out of credit */
-    /** @hide */
-    public static final int SIM_UNPROVISIONED_OUT_OF_CREDIT = 2;
-
-    /** Maximum possible sim provisioning status */
-    /** @hide */
-    public static final int MAX_SIM_PROVISIONING_STATUS = SIM_UNPROVISIONED_OUT_OF_CREDIT;
-
     /** @hide */
     public static final int DATA_ROAMING_DEFAULT = DATA_ROAMING_DISABLE;
 
+    /** @hide */
+    public static final int SIM_PROVISIONED = 0;
+
     /**
      * TelephonyProvider column name for the MCC associated with a SIM.
      * <P>Type: INTEGER (int)</P>
@@ -843,40 +830,6 @@
     }
 
     /**
-     * Set Sim Provisioning Status by subscription ID
-     * @param simProvisioningStatus with the subscription
-     * {@See SubscriptionManager#SIM_PROVISIONED}
-     * {@See SubscriptionManager#SIM_UNPROVISIONED_COLD}
-     * {@See SubscriptionManager#SIM_UNPROVISIONED_OUT_OF_CREDIT}
-     * @param subId the unique SubInfoRecord index in database
-     * @return the number of records updated
-     * Permissions android.Manifest.permission.MODIFY_PHONE_STATE is required
-     * @hide
-     */
-    public int setSimProvisioningStatus(int simProvisioningStatus, int subId) {
-        if (VDBG) {
-            logd("[setSimProvisioningStatus]+ status:" + simProvisioningStatus + " subId:" + subId);
-        }
-        if (simProvisioningStatus < 0 || simProvisioningStatus > MAX_SIM_PROVISIONING_STATUS ||
-                !isValidSubscriptionId(subId)) {
-            logd("[setSimProvisioningStatus]- fail");
-            return -1;
-        }
-
-        int result = 0;
-
-        try {
-            ISub iSub = ISub.Stub.asInterface(ServiceManager.getService("isub"));
-            if (iSub != null) {
-                result = iSub.setSimProvisioningStatus(simProvisioningStatus, subId);
-            }
-        } catch (RemoteException ex) {
-            // ignore it
-        }
-        return result;
-    }
-
-    /**
      * Get slotId associated with the subscription.
      * @return slotId as a positive integer or a negative value if an error either
      * SIM_NOT_INSERTED or < 0 if an invalid slot index
diff --git a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl b/telephony/java/android/telephony/TelephonyHistogram.aidl
similarity index 74%
copy from telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
copy to telephony/java/android/telephony/TelephonyHistogram.aidl
index b7e78d1..8de81cf 100644
--- a/telecomm/java/android/telecom/ParcelableCallAnalytics.aidl
+++ b/telephony/java/android/telephony/TelephonyHistogram.aidl
@@ -1,11 +1,11 @@
 /*
- * Copyright 2016, The Android Open Source Project
+ * Copyright (C) 2016 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *     http://www.apache.org/licenses/LICENSE-2.0
+ *      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,
@@ -13,10 +13,8 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
-package android.telecom;
-
+package android.telephony;
 /**
- * {@hide}
+ * @hide
  */
-parcelable ParcelableCallAnalytics;
+parcelable TelephonyHistogram;
diff --git a/telephony/java/android/telephony/TelephonyHistogram.java b/telephony/java/android/telephony/TelephonyHistogram.java
new file mode 100644
index 0000000..e1c3d7b
--- /dev/null
+++ b/telephony/java/android/telephony/TelephonyHistogram.java
@@ -0,0 +1,313 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.telephony;
+
+import android.annotation.SystemApi;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Parcelable class to store Telephony histogram.
+ * @hide
+ */
+@SystemApi
+public final class TelephonyHistogram implements Parcelable {
+    // Type of Telephony histogram Eg: RIL histogram will have all timing data associated with
+    // RIL calls. Similarly we can have any other Telephony histogram.
+    private final int mCategory;
+
+    // Unique Id identifying a sample within particular category of histogram
+    private final int mId;
+
+    // Min time taken in ms
+    private int mMinTimeMs;
+
+    // Max time taken in ms
+    private int mMaxTimeMs;
+
+    // Average time taken in ms
+    private int mAverageTimeMs;
+
+    // Total count of samples
+    private int mSampleCount;
+
+    // Array storing time taken for first #RANGE_CALCULATION_COUNT samples of histogram.
+    private int[] mInitialTimings;
+
+    // Total number of time ranges expected (must be greater than 1)
+    private final int mBucketCount;
+
+    // Array storing endpoints of range buckets. Calculated based on values of minTime & maxTime
+    // after totalTimeCount is #RANGE_CALCULATION_COUNT.
+    private final int[] mBucketEndPoints;
+
+    // Array storing counts for each time range starting from smallest value range
+    private final int[] mBucketCounters;
+
+    /**
+     * Constant for Telephony category
+     */
+    public static final int TELEPHONY_CATEGORY_RIL = 1;
+
+    // Count of Histogram samples after which time buckets are created.
+    private static final int RANGE_CALCULATION_COUNT = 10;
+
+
+    // Constant used to indicate #initialTimings is null while parceling
+    private static final int ABSENT = 0;
+
+    // Constant used to indicate #initialTimings is not null while parceling
+    private static final int PRESENT = 1;
+
+    // Throws exception if #totalBuckets is not greater than one.
+    public TelephonyHistogram (int category, int id, int bucketCount) {
+        if (bucketCount <= 1) {
+            throw new IllegalArgumentException("Invalid number of buckets");
+        }
+        mCategory = category;
+        mId = id;
+        mMinTimeMs = Integer.MAX_VALUE;
+        mMaxTimeMs = 0;
+        mAverageTimeMs = 0;
+        mSampleCount = 0;
+        mInitialTimings = new int[RANGE_CALCULATION_COUNT];
+        mBucketCount = bucketCount;
+        mBucketEndPoints = new int[bucketCount - 1];
+        mBucketCounters = new int[bucketCount];
+    }
+
+    public TelephonyHistogram(TelephonyHistogram th) {
+        mCategory = th.getCategory();
+        mId = th.getId();
+        mMinTimeMs = th.getMinTime();
+        mMaxTimeMs = th.getMaxTime();
+        mAverageTimeMs = th.getAverageTime();
+        mSampleCount = th.getSampleCount();
+        mInitialTimings = th.getInitialTimings();
+        mBucketCount = th.getBucketCount();
+        mBucketEndPoints = th.getBucketEndPoints();
+        mBucketCounters = th.getBucketCounters();
+    }
+
+    public int getCategory() {
+        return mCategory;
+    }
+
+    public int getId() {
+        return mId;
+    }
+
+    public int getMinTime() {
+        return mMinTimeMs;
+    }
+
+    public int getMaxTime() {
+        return mMaxTimeMs;
+    }
+
+    public int getAverageTime() {
+        return mAverageTimeMs;
+    }
+
+    public int getSampleCount () {
+        return mSampleCount;
+    }
+
+    private int[] getInitialTimings() {
+        return mInitialTimings;
+    }
+
+    public int getBucketCount() {
+        return mBucketCount;
+    }
+
+    public int[] getBucketEndPoints() {
+        if (mSampleCount > 1 && mSampleCount < 10) {
+            int[] tempEndPoints = new int[mBucketCount - 1];
+            calculateBucketEndPoints(tempEndPoints);
+            return tempEndPoints;
+        } else {
+            return getDeepCopyOfArray(mBucketEndPoints);
+        }
+    }
+
+    public int[] getBucketCounters() {
+        if (mSampleCount > 1 && mSampleCount < 10) {
+            int[] tempEndPoints = new int[mBucketCount - 1];
+            int[] tempBucketCounters = new int[mBucketCount];
+            calculateBucketEndPoints(tempEndPoints);
+            for (int j = 0; j < mSampleCount; j++) {
+                addToBucketCounter(tempEndPoints, tempBucketCounters, mInitialTimings[j]);
+            }
+            return tempBucketCounters;
+        } else {
+            return getDeepCopyOfArray(mBucketCounters);
+        }
+    }
+
+    private int[] getDeepCopyOfArray(int[] array) {
+        int[] clone = new int[array.length];
+        System.arraycopy(array, 0, clone, 0, array.length);
+        return clone;
+    }
+
+    private void addToBucketCounter(int[] bucketEndPoints, int[] bucketCounters, int time) {
+        int i;
+        for (i = 0; i < bucketEndPoints.length; i++) {
+            if (time <= bucketEndPoints[i]) {
+                bucketCounters[i]++;
+                return;
+            }
+        }
+        bucketCounters[i]++;
+    }
+
+    private void calculateBucketEndPoints(int[] bucketEndPoints) {
+        for (int i = 1; i < mBucketCount; i++) {
+            int endPt = mMinTimeMs + (i * (mMaxTimeMs - mMinTimeMs)) / mBucketCount;
+            bucketEndPoints[i - 1] = endPt;
+        }
+    }
+
+    // Add new value of time taken
+    // This function updates minTime, maxTime, averageTime & totalTimeCount every time it is
+    // called. initialTimings[] is updated if totalTimeCount <= #RANGE_CALCULATION_COUNT. When
+    // totalTimeCount = RANGE_CALCULATION_COUNT, based on the min, max time & the number of buckets
+    // expected, bucketEndPoints[] would be calculated. Then bucketCounters[] would be filled up
+    // using values stored in initialTimings[]. Thereafter bucketCounters[] will always be updated.
+    public void addTimeTaken(int time) {
+        // Initialize all fields if its first entry or if integer overflow is going to occur while
+        // trying to calculate averageTime
+        if (mSampleCount == 0 || (mSampleCount == Integer.MAX_VALUE)) {
+            if (mSampleCount == 0) {
+                mMinTimeMs = time;
+                mMaxTimeMs = time;
+                mAverageTimeMs = time;
+            } else {
+                mInitialTimings = new int[RANGE_CALCULATION_COUNT];
+            }
+            mSampleCount = 1;
+            Arrays.fill(mInitialTimings, 0);
+            mInitialTimings[0] = time;
+            Arrays.fill(mBucketEndPoints, 0);
+            Arrays.fill(mBucketCounters, 0);
+        } else {
+            if (time < mMinTimeMs) {
+                mMinTimeMs = time;
+            }
+            if (time > mMaxTimeMs) {
+                mMaxTimeMs = time;
+            }
+            long totalTime = ((long)mAverageTimeMs) * mSampleCount + time;
+            mAverageTimeMs = (int)(totalTime/++mSampleCount);
+
+            if (mSampleCount < RANGE_CALCULATION_COUNT) {
+                mInitialTimings[mSampleCount - 1] = time;
+            } else if (mSampleCount == RANGE_CALCULATION_COUNT) {
+                mInitialTimings[mSampleCount - 1] = time;
+
+                // Calculate bucket endpoints based on bucketCount expected
+                calculateBucketEndPoints(mBucketEndPoints);
+
+                // Use values stored in initialTimings[] to update bucketCounters
+                for (int j = 0; j < RANGE_CALCULATION_COUNT; j++) {
+                    addToBucketCounter(mBucketEndPoints, mBucketCounters, mInitialTimings[j]);
+                }
+                mInitialTimings = null;
+            } else {
+                addToBucketCounter(mBucketEndPoints, mBucketCounters, time);
+            }
+
+        }
+    }
+
+    public String toString() {
+        String basic = " Histogram id = " + mId + " Time(ms): min = " + mMinTimeMs + " max = "
+                + mMaxTimeMs + " avg = " + mAverageTimeMs + " Count = " + mSampleCount;
+        if (mSampleCount < RANGE_CALCULATION_COUNT) {
+            return basic;
+        } else {
+            StringBuffer intervals = new StringBuffer(" Interval Endpoints:");
+            for (int i = 0; i < mBucketEndPoints.length; i++) {
+                intervals.append(" " + mBucketEndPoints[i]);
+            }
+            intervals.append(" Interval counters:");
+            for (int i = 0; i < mBucketCounters.length; i++) {
+                intervals.append(" " + mBucketCounters[i]);
+            }
+            return basic + intervals;
+        }
+    }
+
+    public static final Parcelable.Creator<TelephonyHistogram> CREATOR =
+            new Parcelable.Creator<TelephonyHistogram> () {
+
+                @Override
+                public TelephonyHistogram createFromParcel(Parcel in) {
+                    return new TelephonyHistogram(in);
+                }
+
+                @Override
+                public TelephonyHistogram[] newArray(int size) {
+                    return new TelephonyHistogram[size];
+                }
+            };
+
+    public TelephonyHistogram(Parcel in) {
+        mCategory = in.readInt();
+        mId = in.readInt();
+        mMinTimeMs = in.readInt();
+        mMaxTimeMs = in.readInt();
+        mAverageTimeMs = in.readInt();
+        mSampleCount = in.readInt();
+        if (in.readInt() == PRESENT) {
+            mInitialTimings = new int[RANGE_CALCULATION_COUNT];
+            in.readIntArray(mInitialTimings);
+        }
+        mBucketCount = in.readInt();
+        mBucketEndPoints = new int[mBucketCount - 1];
+        in.readIntArray(mBucketEndPoints);
+        mBucketCounters = new int[mBucketCount];
+        in.readIntArray(mBucketCounters);
+    }
+
+    public void writeToParcel(Parcel out, int flags) {
+        out.writeInt(mCategory);
+        out.writeInt(mId);
+        out.writeInt(mMinTimeMs);
+        out.writeInt(mMaxTimeMs);
+        out.writeInt(mAverageTimeMs);
+        out.writeInt(mSampleCount);
+        if (mInitialTimings == null) {
+            out.writeInt(ABSENT);
+        } else {
+            out.writeInt(PRESENT);
+            out.writeIntArray(mInitialTimings);
+        }
+        out.writeInt(mBucketCount);
+        out.writeIntArray(mBucketEndPoints);
+        out.writeIntArray(mBucketCounters);
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+}
diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java
index 0875ff9..093ddb6 100644
--- a/telephony/java/android/telephony/TelephonyManager.java
+++ b/telephony/java/android/telephony/TelephonyManager.java
@@ -34,8 +34,10 @@
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.SystemProperties;
+import android.service.carrier.CarrierIdentifier;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
+import android.telephony.TelephonyHistogram;
 import android.util.Log;
 
 import com.android.internal.telecom.ITelecomService;
@@ -50,6 +52,7 @@
 
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.regex.Matcher;
@@ -702,6 +705,50 @@
             "android.telephony.extra.LAUNCH_VOICEMAIL_SETTINGS_INTENT";
 
     /**
+     * {@link android.telecom.Connection} event used to indicate that an IMS call has be
+     * successfully handed over from WIFI to LTE.
+     * <p>
+     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
+     * The {@link Bundle} parameter is expected to be null when this connection event is used.
+     * @hide
+     */
+    public static final String EVENT_HANDOVER_VIDEO_FROM_WIFI_TO_LTE =
+            "android.telephony.event.EVENT_HANDOVER_VIDEO_FROM_WIFI_TO_LTE";
+
+    /**
+     * {@link android.telecom.Connection} event used to indicate that an IMS call failed to be
+     * handed over from LTE to WIFI.
+     * <p>
+     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
+     * The {@link Bundle} parameter is expected to be null when this connection event is used.
+     * @hide
+     */
+    public static final String EVENT_HANDOVER_TO_WIFI_FAILED =
+            "android.telephony.event.EVENT_HANDOVER_TO_WIFI_FAILED";
+
+    /**
+     * {@link android.telecom.Connection} event used to indicate that a video call was downgraded to
+     * audio because the data limit was reached.
+     * <p>
+     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
+     * The {@link Bundle} parameter is expected to be null when this connection event is used.
+     * @hide
+     */
+    public static final String EVENT_DOWNGRADE_DATA_LIMIT_REACHED =
+            "android.telephony.event.EVENT_DOWNGRADE_DATA_LIMIT_REACHED";
+
+    /**
+     * {@link android.telecom.Connection} event used to indicate that a video call was downgraded to
+     * audio because the data was disabled.
+     * <p>
+     * Sent via {@link android.telecom.Connection#sendConnectionEvent(String, Bundle)}.
+     * The {@link Bundle} parameter is expected to be null when this connection event is used.
+     * @hide
+     */
+    public static final String EVENT_DOWNGRADE_DATA_DISABLED =
+            "android.telephony.event.EVENT_DOWNGRADE_DATA_DISABLED";
+
+    /**
      * Response codes for sim activation. Activation completed successfully.
      * @hide
      */
@@ -1442,13 +1489,14 @@
     public static final int NETWORK_TYPE_EHRPD = 14;
     /** Current network is HSPA+ */
     public static final int NETWORK_TYPE_HSPAP = 15;
-    /** Current network is GSM {@hide} */
+    /** Current network is GSM */
     public static final int NETWORK_TYPE_GSM = 16;
-     /** Current network is TD_SCDMA {@hide} */
+    /** Current network is TD_SCDMA */
     public static final int NETWORK_TYPE_TD_SCDMA = 17;
-   /** Current network is IWLAN {@hide} */
+    /** Current network is IWLAN */
     public static final int NETWORK_TYPE_IWLAN = 18;
-
+    /** Current network is LTE_CA {@hide} */
+    public static final int NETWORK_TYPE_LTE_CA = 19;
     /**
      * @return the NETWORK_TYPE_xxxx for current data connection.
      */
@@ -1614,6 +1662,12 @@
         }
     }
 
+    /**
+     * Network Class Definitions.
+     * Do not change this order, it is used for sorting during emergency calling in
+     * {@link TelephonyConnectionService#getFirstPhoneForEmergencyCall()}. Any newer technologies
+     * should be added after the current definitions.
+     */
     /** Unknown network class. {@hide} */
     public static final int NETWORK_CLASS_UNKNOWN = 0;
     /** Class of broadly defined "2G" networks. {@hide} */
@@ -1651,6 +1705,7 @@
                 return NETWORK_CLASS_3_G;
             case NETWORK_TYPE_LTE:
             case NETWORK_TYPE_IWLAN:
+            case NETWORK_TYPE_LTE_CA:
                 return NETWORK_CLASS_4_G;
             default:
                 return NETWORK_CLASS_UNKNOWN;
@@ -1714,6 +1769,8 @@
                 return "TD_SCDMA";
             case NETWORK_TYPE_IWLAN:
                 return "IWLAN";
+            case NETWORK_TYPE_LTE_CA:
+                return "LTE_CA";
             default:
                 return "UNKNOWN";
         }
@@ -1756,6 +1813,11 @@
      *@hide
      */
     public static final int SIM_STATE_CARD_IO_ERROR = 8;
+    /** SIM card state: SIM Card restricted, present but not usable due to
+     * carrier restrictions.
+     *@hide
+     */
+    public static final int SIM_STATE_CARD_RESTRICTED = 9;
 
     /**
      * @return true if a ICC card is present
@@ -1909,7 +1971,7 @@
         return getSimOperatorNumericForPhone(phoneId);
     }
 
-   /**
+    /**
      * Returns the MCC+MNC (mobile country code + mobile network code) of the
      * provider of the SIM for a particular subscription. 5 or 6 decimal digits.
      * <p>
@@ -2461,6 +2523,152 @@
     }
 
     /**
+     * Enables or disables the visual voicemail client for a phone account.
+     *
+     * <p>Requires that the calling app is the default dialer, or has carrier privileges, or
+     * has permission {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}.
+     * @see #hasCarrierPrivileges
+     *
+     * @param phoneAccountHandle the phone account to change the client state
+     * @param enabled the new state of the client
+     * @hide
+     */
+    @SystemApi
+    public void setVisualVoicemailEnabled(PhoneAccountHandle phoneAccountHandle, boolean enabled){
+        try {
+            ITelephony telephony = getITelephony();
+            if (telephony != null) {
+                telephony.setVisualVoicemailEnabled(mContext.getOpPackageName(), phoneAccountHandle,
+                    enabled);
+            }
+        } catch (RemoteException ex) {
+        } catch (NullPointerException ex) {
+            // This could happen before phone restarts due to crashing
+        }
+    }
+
+    /**
+     * Returns whether the visual voicemail client is enabled.
+     *
+     * <p>Requires Permission:
+     *   {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
+     *
+     * @param phoneAccountHandle the phone account to check for.
+     * @return {@code true} when the visual voicemail client is enabled for this client
+     * @hide
+     */
+    @SystemApi
+    public boolean isVisualVoicemailEnabled(PhoneAccountHandle phoneAccountHandle){
+        try {
+            ITelephony telephony = getITelephony();
+            if (telephony != null) {
+                return telephony.isVisualVoicemailEnabled(
+                    mContext.getOpPackageName(), phoneAccountHandle);
+            }
+        } catch (RemoteException ex) {
+        } catch (NullPointerException ex) {
+            // This could happen before phone restarts due to crashing
+        }
+        return false;
+    }
+
+    /**
+     * Enables the visual voicemail SMS filter for a phone account. When the filter is
+     * enabled, Incoming SMS messages matching the OMTP VVM SMS interface will be redirected to the
+     * visual voicemail client with
+     * {@link android.provider.VoicemailContract.ACTION_VOICEMAIL_SMS_RECEIVED}.
+     *
+     * <p>This takes effect only when the caller is the default dialer. The enabled status and
+     * settings persist through default dialer changes, but the filter will only honor the setting
+     * set by the current default dialer.
+     *
+     *
+     * @param subId The subscription id of the phone account.
+     * @param settings The settings for the filter.
+     */
+    /** @hide */
+    public void enableVisualVoicemailSmsFilter(int subId,
+            VisualVoicemailSmsFilterSettings settings) {
+        if(settings == null){
+            throw new IllegalArgumentException("Settings cannot be null");
+        }
+        try {
+            ITelephony telephony = getITelephony();
+            if (telephony != null) {
+                telephony.enableVisualVoicemailSmsFilter(mContext.getOpPackageName(), subId,
+                        settings);
+            }
+        } catch (RemoteException ex) {
+        } catch (NullPointerException ex) {
+        }
+    }
+
+    /**
+     * Disables the visual voicemail SMS filter for a phone account.
+     *
+     * <p>This takes effect only when the caller is the default dialer. The enabled status and
+     * settings persist through default dialer changes, but the filter will only honor the setting
+     * set by the current default dialer.
+     */
+    /** @hide */
+    public void disableVisualVoicemailSmsFilter(int subId) {
+        try {
+            ITelephony telephony = getITelephony();
+            if (telephony != null) {
+                telephony.disableVisualVoicemailSmsFilter(mContext.getOpPackageName(), subId);
+            }
+        } catch (RemoteException ex) {
+        } catch (NullPointerException ex) {
+        }
+    }
+
+    /**
+     * @returns the settings of the visual voicemail SMS filter for a phone account, or {@code null}
+     * if the filter is disabled.
+     *
+     * <p>This takes effect only when the caller is the default dialer. The enabled status and
+     * settings persist through default dialer changes, but the filter will only honor the setting
+     * set by the current default dialer.
+     */
+    /** @hide */
+    @Nullable
+    public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(int subId) {
+        try {
+            ITelephony telephony = getITelephony();
+            if (telephony != null) {
+                return telephony
+                        .getVisualVoicemailSmsFilterSettings(mContext.getOpPackageName(), subId);
+            }
+        } catch (RemoteException ex) {
+        } catch (NullPointerException ex) {
+        }
+
+        return null;
+    }
+
+    /**
+     * @returns the settings of the visual voicemail SMS filter for a phone account set by the
+     * package, or {@code null} if the filter is disabled.
+     *
+     * <p>Requires the calling app to have READ_PRIVILEGED_PHONE_STATE permission.
+     */
+    /** @hide */
+    @Nullable
+    public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(String packageName,
+            int subId) {
+        try {
+            ITelephony telephony = getITelephony();
+            if (telephony != null) {
+                return telephony.getSystemVisualVoicemailSmsFilterSettings(packageName, subId);
+            }
+        } catch (RemoteException ex) {
+        } catch (NullPointerException ex) {
+        }
+
+        return null;
+    }
+
+    /**
      * Returns the voice mail count. Return 0 if unavailable, -1 if there are unread voice messages
      * but the count is unknown.
      * <p>
@@ -5246,4 +5454,239 @@
         }
         return false;
     }
+
+    /**
+     * Return the application ID for the app type like {@link APPTYPE_CSIM}.
+     *
+     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
+     *
+     * @param appType the uicc app type like {@link APPTYPE_CSIM}
+     * @return Application ID for specificied app type or null if no uicc or error.
+     * @hide
+     */
+    public String getAidForAppType(int appType) {
+        return getAidForAppType(getDefaultSubscription(), appType);
+    }
+
+    /**
+     * Return the application ID for the app type like {@link APPTYPE_CSIM}.
+     *
+     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
+     *
+     * @param subId the subscription ID that this request applies to.
+     * @param appType the uicc app type, like {@link APPTYPE_CSIM}
+     * @return Application ID for specificied app type or null if no uicc or error.
+     * @hide
+     */
+    public String getAidForAppType(int subId, int appType) {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                return service.getAidForAppType(subId, appType);
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#getAidForAppType", e);
+        }
+        return null;
+    }
+
+    /**
+     * Return the Electronic Serial Number.
+     *
+     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
+     *
+     * @return ESN or null if error.
+     * @hide
+     */
+    public String getEsn() {
+        return getEsn(getDefaultSubscription());
+    }
+
+    /**
+     * Return the Electronic Serial Number.
+     *
+     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
+     *
+     * @param subId the subscription ID that this request applies to.
+     * @return ESN or null if error.
+     * @hide
+     */
+    public String getEsn(int subId) {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                return service.getEsn(subId);
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#getEsn", e);
+        }
+        return null;
+    }
+
+    /**
+     * Return the Preferred Roaming List Version
+     *
+     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
+     *
+     * @return PRLVersion or null if error.
+     * @hide
+     */
+    public String getCdmaPrlVersion() {
+        return getCdmaPrlVersion(getDefaultSubscription());
+    }
+
+    /**
+     * Return the Preferred Roaming List Version
+     *
+     * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
+     *
+     * @param subId the subscription ID that this request applies to.
+     * @return PRLVersion or null if error.
+     * @hide
+     */
+    public String getCdmaPrlVersion(int subId) {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                return service.getCdmaPrlVersion(subId);
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#getCdmaPrlVersion", e);
+        }
+        return null;
+    }
+
+    /**
+     * Get snapshot of Telephony histograms
+     * @return List of Telephony histograms
+     * Requires Permission:
+     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
+     * Or the calling app has carrier privileges.
+     * @hide
+     */
+    @SystemApi
+    public List<TelephonyHistogram> getTelephonyHistograms() {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                return service.getTelephonyHistograms();
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#getTelephonyHistograms", e);
+        }
+        return null;
+    }
+
+    /**
+     * Set the allowed carrier list for slotId
+     * Require system privileges. In the future we may add this to carrier APIs.
+     *
+     * @return The number of carriers set successfully. Should be length of
+     * carrierList on success; -1 on error.
+     * @hide
+     */
+    public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                return service.setAllowedCarriers(slotId, carriers);
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#setAllowedCarriers", e);
+        }
+        return -1;
+    }
+
+    /**
+     * Get the allowed carrier list for slotId.
+     * Require system privileges. In the future we may add this to carrier APIs.
+     *
+     * @return List of {@link android.telephony.CarrierIdentifier}; empty list
+     * means all carriers are allowed.
+     * @hide
+     */
+    public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                return service.getAllowedCarriers(slotId);
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#getAllowedCarriers", e);
+        }
+        return new ArrayList<CarrierIdentifier>(0);
+    }
+
+    /**
+     * Action set from carrier signalling broadcast receivers to enable/disable metered apns
+     * Permissions android.Manifest.permission.MODIFY_PHONE_STATE is required
+     * @param subId the subscription ID that this action applies to.
+     * @param enabled control enable or disable metered apns.
+     * @hide
+     */
+    public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                service.carrierActionSetMeteredApnsEnabled(subId, enabled);
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#carrierActionSetMeteredApnsEnabled", e);
+        }
+    }
+
+    /**
+     * Action set from carrier signalling broadcast receivers to enable/disable radio
+     * Permissions android.Manifest.permission.MODIFY_PHONE_STATE is required
+     * @param subId the subscription ID that this action applies to.
+     * @param enabled control enable or disable radio.
+     * @hide
+     */
+    public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                service.carrierActionSetRadioEnabled(subId, enabled);
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#carrierActionSetRadioEnabled", e);
+        }
+    }
+
+    /**
+     * Get aggregated video call data usage since boot.
+     * Permissions android.Manifest.permission.READ_NETWORK_USAGE_HISTORY is required.
+     * @return total data usage in bytes
+     * @hide
+     */
+    public long getVtDataUsage() {
+
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                return service.getVtDataUsage();
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling getVtDataUsage", e);
+        }
+        return 0;
+    }
+
+    /**
+     * Policy control of data connection. Usually used when data limit is passed.
+     * @param enabled True if enabling the data, otherwise disabling.
+     * @param subId sub id
+     * @hide
+     */
+    public void setPolicyDataEnabled(boolean enabled, int subId) {
+        try {
+            ITelephony service = getITelephony();
+            if (service != null) {
+                service.setPolicyDataEnabled(enabled, subId);
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "Error calling ITelephony#setPolicyDataEnabled", e);
+        }
+    }
 }
+
diff --git a/telephony/java/android/telephony/VisualVoicemailSmsFilterSettings.aidl b/telephony/java/android/telephony/VisualVoicemailSmsFilterSettings.aidl
new file mode 100644
index 0000000..4b0539d
--- /dev/null
+++ b/telephony/java/android/telephony/VisualVoicemailSmsFilterSettings.aidl
@@ -0,0 +1,19 @@
+/*
+* Copyright (C) 2016 The Android Open Source Project
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*      http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+package android.telephony;
+
+parcelable VisualVoicemailSmsFilterSettings;
diff --git a/telephony/java/android/telephony/VisualVoicemailSmsFilterSettings.java b/telephony/java/android/telephony/VisualVoicemailSmsFilterSettings.java
new file mode 100644
index 0000000..5b81027
--- /dev/null
+++ b/telephony/java/android/telephony/VisualVoicemailSmsFilterSettings.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.telephony;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Class to represent various settings for the visual voicemail SMS filter. When the filter is
+ * enabled, incoming SMS matching the generalized OMTP format:
+ *
+ * <p>[clientPrefix]:[prefix]:([key]=[value];)*
+ *
+ * <p>will be regarded as a visual voicemail SMS, and removed before reaching the SMS provider. The
+ * intent {@link android.provider.VoicemailContract#ACTION_VOICEMAIL_SMS_RECEIVED} will then be sent
+ * to the default dialer with the information extracted from the SMS.
+ *
+ * <p>Use {@link android.telephony.VisualVoicemailSmsFilterSettings.Builder} to construct this
+ * class.
+ *
+ * @see android.telephony.TelephonyManager#enableVisualVoicemailSmsFilter
+ *
+ * @hide
+ */
+public class VisualVoicemailSmsFilterSettings implements Parcelable {
+
+
+    /**
+     * The visual voicemail SMS message does not have to be a data SMS, and can be directed to any
+     * port.
+     *
+     * @hide
+     */
+    public static final int DESTINATION_PORT_ANY = -1;
+
+    /**
+     * The visual voicemail SMS message can be directed to any port, but must be a data SMS.
+     *
+     * @hide
+     */
+    public static final int DESTINATION_PORT_DATA_SMS = -2;
+
+    public static final String DEFAULT_CLIENT_PREFIX = "//VVM";
+    public static final List<String> DEFAULT_ORIGINATING_NUMBERS = Collections.emptyList();
+    public static final int DEFAULT_DESTINATION_PORT = DESTINATION_PORT_ANY;
+
+    /**
+     * Builder class for {@link VisualVoicemailSmsFilterSettings} objects.
+     *
+     * @hide
+     */
+    public static class Builder {
+
+        private String mClientPrefix = DEFAULT_CLIENT_PREFIX;
+        private List<String> mOriginatingNumbers = DEFAULT_ORIGINATING_NUMBERS;
+        private int mDestinationPort = DEFAULT_DESTINATION_PORT;
+
+        public VisualVoicemailSmsFilterSettings build() {
+            return new VisualVoicemailSmsFilterSettings(this);
+        }
+
+        /**
+         * Sets the client prefix for the visual voicemail SMS filter. The client prefix will appear
+         * at the start of a visual voicemail SMS message, followed by a colon(:).
+         */
+        public Builder setClientPrefix(String clientPrefix) {
+            if (clientPrefix == null) {
+                throw new IllegalArgumentException("Client prefix cannot be null");
+            }
+            mClientPrefix = clientPrefix;
+            return this;
+        }
+
+        /**
+         * Sets the originating number whitelist for the visual voicemail SMS filter. If the list is
+         * not null only the SMS messages from a number in the list can be considered as a visual
+         * voicemail SMS. Otherwise, messages from any address will be considered.
+         */
+        public Builder setOriginatingNumbers(List<String> originatingNumbers) {
+            if (originatingNumbers == null) {
+                throw new IllegalArgumentException("Originating numbers cannot be null");
+            }
+            mOriginatingNumbers = originatingNumbers;
+            return this;
+        }
+
+        /**
+         * Sets the destination port for the visual voicemail SMS filter.
+         *
+         * @param destinationPort The destination port, or {@link #DESTINATION_PORT_ANY}, or {@link
+         * #DESTINATION_PORT_DATA_SMS}
+         */
+        public Builder setDestinationPort(int destinationPort) {
+            mDestinationPort = destinationPort;
+            return this;
+        }
+
+    }
+
+    /**
+     * The client prefix for the visual voicemail SMS filter. The client prefix will appear at the
+     * start of a visual voicemail SMS message, followed by a colon(:).
+     */
+    public final String clientPrefix;
+
+    /**
+     * The originating number whitelist for the visual voicemail SMS filter of a phone account. If
+     * the list is not null only the SMS messages from a number in the list can be considered as a
+     * visual voicemail SMS. Otherwise, messages from any address will be considered.
+     */
+    public final List<String> originatingNumbers;
+
+    /**
+     * The destination port for the visual voicemail SMS filter, or {@link #DESTINATION_PORT_ANY},
+     * or {@link #DESTINATION_PORT_DATA_SMS}
+     */
+    public final int destinationPort;
+
+    /**
+     * Use {@link Builder} to construct
+     */
+    private VisualVoicemailSmsFilterSettings(Builder builder) {
+        clientPrefix = builder.mClientPrefix;
+        originatingNumbers = builder.mOriginatingNumbers;
+        destinationPort = builder.mDestinationPort;
+    }
+
+    public static final Creator<VisualVoicemailSmsFilterSettings> CREATOR =
+            new Creator<VisualVoicemailSmsFilterSettings>() {
+                @Override
+                public VisualVoicemailSmsFilterSettings createFromParcel(Parcel in) {
+                    Builder builder = new Builder();
+                    builder.setClientPrefix(in.readString());
+                    builder.setOriginatingNumbers(in.createStringArrayList());
+                    builder.setDestinationPort(in.readInt());
+
+                    return builder.build();
+                }
+
+                @Override
+                public VisualVoicemailSmsFilterSettings[] newArray(int size) {
+                    return new VisualVoicemailSmsFilterSettings[size];
+                }
+            };
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeString(clientPrefix);
+        dest.writeStringList(originatingNumbers);
+        dest.writeInt(destinationPort);
+    }
+
+}
diff --git a/telephony/java/com/android/ims/ImsCallProfile.java b/telephony/java/com/android/ims/ImsCallProfile.java
index 92b70e8..36abfc9 100644
--- a/telephony/java/com/android/ims/ImsCallProfile.java
+++ b/telephony/java/com/android/ims/ImsCallProfile.java
@@ -205,6 +205,14 @@
      */
     public static final String EXTRA_CALL_RAT_TYPE = "CallRadioTech";
 
+    /**
+     * Similar to {@link #EXTRA_CALL_RAT_TYPE}, except with a lowercase 'c'.  Used to ensure
+     * compatibility with modems that are non-compliant with the {@link #EXTRA_CALL_RAT_TYPE}
+     * extra key.  Should be removed when the non-compliant modems are fixed.
+     * @hide
+     */
+    public static final String EXTRA_CALL_RAT_TYPE_ALT = "callRadioTech";
+
     public int mServiceType;
     public int mCallType;
     public int mRestrictCause = CALL_RESTRICT_CAUSE_NONE;
@@ -448,6 +456,15 @@
     }
 
     /**
+     * Determines if the {@link ImsCallProfile} represents a video call.
+     *
+     * @return {@code true} if the profile is for a video call, {@code false} otherwise.
+     */
+    public boolean isVideoCall() {
+        return VideoProfile.isVideo(getVideoStateFromCallType(mCallType));
+    }
+
+    /**
      * Determines if a video state is set in a video state bit-mask.
      *
      * @param videoState The video state bit mask.
diff --git a/telephony/java/com/android/ims/ImsExternalCallState.java b/telephony/java/com/android/ims/ImsExternalCallState.java
index 71c1837..da26073 100644
--- a/telephony/java/com/android/ims/ImsExternalCallState.java
+++ b/telephony/java/com/android/ims/ImsExternalCallState.java
@@ -19,6 +19,7 @@
 import android.net.Uri;
 import android.os.Parcel;
 import android.os.Parcelable;
+import android.telecom.Log;
 import android.telephony.Rlog;
 
 /*
@@ -130,7 +131,7 @@
     @Override
     public String toString() {
         return "ImsExternalCallState { mCallId = " + mCallId +
-                ", mAddress = " + mAddress +
+                ", mAddress = " + Log.pii(mAddress) +
                 ", mIsPullable = " + mIsPullable +
                 ", mCallState = " + mCallState +
                 ", mCallType = " + mCallType +
diff --git a/telephony/java/com/android/ims/ImsReasonInfo.java b/telephony/java/com/android/ims/ImsReasonInfo.java
index 9369b20..56b8822 100644
--- a/telephony/java/com/android/ims/ImsReasonInfo.java
+++ b/telephony/java/com/android/ims/ImsReasonInfo.java
@@ -25,6 +25,7 @@
  * @hide
  */
 public class ImsReasonInfo implements Parcelable {
+
     /**
      * Specific code of each types
      */
@@ -284,6 +285,29 @@
     public static final int CODE_EPDG_TUNNEL_LOST_CONNECTION = 1402;
 
     /**
+     * The maximum number of calls allowed has been reached.  Used in a multi-endpoint scenario
+     * where the number of calls across all connected devices has reached the maximum.
+     */
+    public static final int CODE_MAXIMUM_NUMBER_OF_CALLS_REACHED = 1403;
+
+    /**
+     * Similar to {@link #CODE_LOCAL_CALL_DECLINE}, except indicates that a remote device has
+     * declined the call.  Used in a multi-endpoint scenario where a remote device declined an
+     * incoming call.
+     */
+    public static final int CODE_REMOTE_CALL_DECLINE = 1404;
+
+    /**
+     * Indicates the call was disconnected due to the user reaching their data limit.
+     */
+    public static final int CODE_DATA_LIMIT_REACHED = 1405;
+
+    /**
+     * Indicates the call was disconnected due to the user disabling cellular data.
+     */
+    public static final int CODE_DATA_DISABLED = 1406;
+
+    /**
      * Network string error messages.
      * mExtraMessage may have these values.
      */
diff --git a/telephony/java/com/android/ims/internal/IImsService.aidl b/telephony/java/com/android/ims/internal/IImsService.aidl
index a9614a6..406d22d 100644
--- a/telephony/java/com/android/ims/internal/IImsService.aidl
+++ b/telephony/java/com/android/ims/internal/IImsService.aidl
@@ -38,8 +38,19 @@
     void close(int serviceId);
     boolean isConnected(int serviceId, int serviceType, int callType);
     boolean isOpened(int serviceId);
+
+    /**
+     * Replace existing registration listener
+     *
+     */
     void setRegistrationListener(int serviceId, in IImsRegistrationListener listener);
 
+    /**
+     * Add new registration listener
+     */
+    void addRegistrationListener(int phoneId, int serviceClass,
+            in IImsRegistrationListener listener);
+
     ImsCallProfile createCallProfile(int serviceId, int serviceType, int callType);
 
     IImsCallSession createCallSession(int serviceId, in ImsCallProfile profile,
diff --git a/telephony/java/com/android/internal/telephony/CallerInfoAsyncQuery.java b/telephony/java/com/android/internal/telephony/CallerInfoAsyncQuery.java
index 05cb31e..d4104bd 100644
--- a/telephony/java/com/android/internal/telephony/CallerInfoAsyncQuery.java
+++ b/telephony/java/com/android/internal/telephony/CallerInfoAsyncQuery.java
@@ -176,13 +176,13 @@
                     // However, if there is any code that this Handler calls (such as in
                     // super.handleMessage) that DOES place unexpected messages on the
                     // queue, then we need pass these messages on.
-                    if (DBG) Rlog.d(LOG_TAG, "Unexpected command (CookieWrapper is null): " + msg.what +
+                    Rlog.i(LOG_TAG, "Unexpected command (CookieWrapper is null): " + msg.what +
                             " ignored by CallerInfoWorkerHandler, passing onto parent.");
 
                     super.handleMessage(msg);
                 } else {
 
-                    if (DBG) Rlog.d(LOG_TAG, "Processing event: " + cw.event + " token (arg1): " + msg.arg1 +
+                    Rlog.d(LOG_TAG, "Processing event: " + cw.event + " token (arg1): " + msg.arg1 +
                         " command: " + msg.what + " query URI: " + sanitizeUriToString(args.uri));
 
                     switch (cw.event) {
@@ -239,7 +239,7 @@
          */
         @Override
         protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
-            if (DBG) Rlog.d(LOG_TAG, "##### onQueryComplete() #####   query complete for token: " + token);
+            Rlog.d(LOG_TAG, "##### onQueryComplete() #####   query complete for token: " + token);
 
             //get the cookie and notify the listener.
             CookieWrapper cw = (CookieWrapper) cookie;
@@ -248,7 +248,7 @@
                 // from within this code.
                 // However, if there is any code that calls this method, we should
                 // check the parameters to make sure they're viable.
-                if (DBG) Rlog.d(LOG_TAG, "Cookie is null, ignoring onQueryComplete() request.");
+                Rlog.i(LOG_TAG, "Cookie is null, ignoring onQueryComplete() request.");
                 if (cursor != null) {
                     cursor.close();
                 }
@@ -333,9 +333,11 @@
 
             //notify the listener that the query is complete.
             if (cw.listener != null) {
-                if (DBG) Rlog.d(LOG_TAG, "notifying listener: " + cw.listener.getClass().toString() +
+                Rlog.d(LOG_TAG, "notifying listener: " + cw.listener.getClass().toString() +
                              " for token: " + token + mCallerInfo);
                 cw.listener.onQueryComplete(token, cw.cookie, mCallerInfo);
+            } else {
+                Rlog.w(LOG_TAG, "There is no listener to notify for this query.");
             }
 
             if (cursor != null) {
diff --git a/telephony/java/com/android/internal/telephony/CarrierAppUtils.java b/telephony/java/com/android/internal/telephony/CarrierAppUtils.java
index ca7354f..8b81b0d 100644
--- a/telephony/java/com/android/internal/telephony/CarrierAppUtils.java
+++ b/telephony/java/com/android/internal/telephony/CarrierAppUtils.java
@@ -17,18 +17,23 @@
 package com.android.internal.telephony;
 
 import android.annotation.Nullable;
+import android.content.ContentResolver;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.IPackageManager;
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
 import android.os.RemoteException;
+import android.provider.Settings;
 import android.telephony.TelephonyManager;
+import android.util.ArrayMap;
 import android.util.Slog;
 
 import com.android.internal.annotations.VisibleForTesting;
+import com.android.server.SystemConfig;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 /**
  * Utilities for handling carrier applications.
@@ -53,6 +58,11 @@
      * in the default state (e.g. not explicitly DISABLED/DISABLED_BY_USER/ENABLED), or we enable if
      * the app is carrier privileged and in either the default state or DISABLED_UNTIL_USED.
      *
+     * In addition, there is a list of carrier-associated applications in
+     * {@link SystemConfig#getDisabledUntilUsedPreinstalledCarrierAssociatedApps}. Each app in this
+     * list is associated with a carrier app. When the given carrier app is enabled/disabled per the
+     * above, the associated applications are enabled/disabled to match.
+     *
      * When enabling a carrier app we also grant it default permissions.
      *
      * This method is idempotent and is safe to be called at any time; it should be called once at
@@ -60,19 +70,24 @@
      * privileged apps may have changed.
      */
     public synchronized static void disableCarrierAppsUntilPrivileged(String callingPackage,
-            IPackageManager packageManager, TelephonyManager telephonyManager, int userId) {
+            IPackageManager packageManager, TelephonyManager telephonyManager,
+            ContentResolver contentResolver, int userId) {
         if (DEBUG) {
             Slog.d(TAG, "disableCarrierAppsUntilPrivileged");
         }
+        SystemConfig config = SystemConfig.getInstance();
         String[] systemCarrierAppsDisabledUntilUsed = Resources.getSystem().getStringArray(
                 com.android.internal.R.array.config_disabledUntilUsedPreinstalledCarrierApps);
-        disableCarrierAppsUntilPrivileged(callingPackage, packageManager, telephonyManager, userId,
-                systemCarrierAppsDisabledUntilUsed);
+        ArrayMap<String, List<String>> systemCarrierAssociatedAppsDisabledUntilUsed =
+                config.getDisabledUntilUsedPreinstalledCarrierAssociatedApps();
+        disableCarrierAppsUntilPrivileged(callingPackage, packageManager, telephonyManager,
+                contentResolver, userId, systemCarrierAppsDisabledUntilUsed,
+                systemCarrierAssociatedAppsDisabledUntilUsed);
     }
 
     /**
      * Like {@link #disableCarrierAppsUntilPrivileged(String, IPackageManager, TelephonyManager,
-     * int)}, but assumes that no carrier apps have carrier privileges.
+     * ContentResolver, int)}, but assumes that no carrier apps have carrier privileges.
      *
      * This prevents a potential race condition on first boot - since the app's default state is
      * enabled, we will initially disable it when the telephony stack is first initialized as it has
@@ -82,29 +97,43 @@
      * Manager can kill it, and this can lead to crashes as the app is in an unexpected state.
      */
     public synchronized static void disableCarrierAppsUntilPrivileged(String callingPackage,
-            IPackageManager packageManager, int userId) {
+            IPackageManager packageManager, ContentResolver contentResolver, int userId) {
         if (DEBUG) {
             Slog.d(TAG, "disableCarrierAppsUntilPrivileged");
         }
+        SystemConfig config = SystemConfig.getInstance();
         String[] systemCarrierAppsDisabledUntilUsed = Resources.getSystem().getStringArray(
                 com.android.internal.R.array.config_disabledUntilUsedPreinstalledCarrierApps);
+        ArrayMap<String, List<String>> systemCarrierAssociatedAppsDisabledUntilUsed =
+                config.getDisabledUntilUsedPreinstalledCarrierAssociatedApps();
         disableCarrierAppsUntilPrivileged(callingPackage, packageManager,
-                null /* telephonyManager */, userId, systemCarrierAppsDisabledUntilUsed);
+                null /* telephonyManager */, contentResolver, userId,
+                systemCarrierAppsDisabledUntilUsed, systemCarrierAssociatedAppsDisabledUntilUsed);
     }
 
     // Must be public b/c framework unit tests can't access package-private methods.
     @VisibleForTesting
     public static void disableCarrierAppsUntilPrivileged(String callingPackage,
-            IPackageManager packageManager, @Nullable TelephonyManager telephonyManager, int userId,
-            String[] systemCarrierAppsDisabledUntilUsed) {
+            IPackageManager packageManager, @Nullable TelephonyManager telephonyManager,
+            ContentResolver contentResolver, int userId,
+            String[] systemCarrierAppsDisabledUntilUsed,
+            ArrayMap<String, List<String>> systemCarrierAssociatedAppsDisabledUntilUsed) {
         List<ApplicationInfo> candidates = getDefaultCarrierAppCandidatesHelper(packageManager,
                 userId, systemCarrierAppsDisabledUntilUsed);
         if (candidates == null || candidates.isEmpty()) {
             return;
         }
 
+        Map<String, List<ApplicationInfo>> associatedApps = getDefaultCarrierAssociatedAppsHelper(
+                packageManager,
+                userId,
+                systemCarrierAssociatedAppsDisabledUntilUsed);
+
         List<String> enabledCarrierPackages = new ArrayList<>();
 
+        boolean hasRunOnce = Settings.Secure.getIntForUser(
+                contentResolver, Settings.Secure.CARRIER_APPS_HANDLED, 0, userId) == 1;
+
         try {
             for (ApplicationInfo ai : candidates) {
                 String packageName = ai.packageName;
@@ -112,33 +141,92 @@
                         telephonyManager.checkCarrierPrivilegesForPackageAnyPhone(packageName) ==
                                 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS;
 
-                // Only update enabled state for the app on /system. Once it has been updated we
-                // shouldn't touch it.
-                if (!ai.isUpdatedSystemApp()) {
-                    if (hasPrivileges
-                            && (ai.enabledSetting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
+                if (hasPrivileges) {
+                    // Only update enabled state for the app on /system. Once it has been
+                    // updated we shouldn't touch it.
+                    if (!ai.isUpdatedSystemApp()
+                            && (ai.enabledSetting ==
+                            PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
                             || ai.enabledSetting ==
                             PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED)) {
                         Slog.i(TAG, "Update state(" + packageName + "): ENABLED for user "
                                 + userId);
-                        packageManager.setApplicationEnabledSetting(packageName,
+                        packageManager.setApplicationEnabledSetting(
+                                packageName,
                                 PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
-                                PackageManager.DONT_KILL_APP, userId, callingPackage);
-                    } else if (!hasPrivileges
+                                PackageManager.DONT_KILL_APP,
+                                userId,
+                                callingPackage);
+                    }
+
+                    // Also enable any associated apps for this carrier app.
+                    List<ApplicationInfo> associatedAppList = associatedApps.get(packageName);
+                    if (associatedAppList != null) {
+                        for (ApplicationInfo associatedApp : associatedAppList) {
+                            if (associatedApp.enabledSetting ==
+                                    PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
+                                    || associatedApp.enabledSetting ==
+                                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED) {
+                                Slog.i(TAG, "Update associated state(" + associatedApp.packageName
+                                        + "): ENABLED for user " + userId);
+                                packageManager.setApplicationEnabledSetting(
+                                        associatedApp.packageName,
+                                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
+                                        PackageManager.DONT_KILL_APP,
+                                        userId,
+                                        callingPackage);
+                            }
+                        }
+                    }
+
+                    // Always re-grant default permissions to carrier apps w/ privileges.
+                    enabledCarrierPackages.add(ai.packageName);
+                } else {  // No carrier privileges
+                    // Only update enabled state for the app on /system. Once it has been
+                    // updated we shouldn't touch it.
+                    if (!ai.isUpdatedSystemApp()
                             && ai.enabledSetting ==
                             PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
                         Slog.i(TAG, "Update state(" + packageName
                                 + "): DISABLED_UNTIL_USED for user " + userId);
-                        packageManager.setApplicationEnabledSetting(packageName,
-                                PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, 0,
-                                userId, callingPackage);
+                        packageManager.setApplicationEnabledSetting(
+                                packageName,
+                                PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED,
+                                0,
+                                userId,
+                                callingPackage);
+                    }
+
+                    // Also disable any associated apps for this carrier app if this is the first
+                    // run. We avoid doing this a second time because it is brittle to rely on the
+                    // distinction between "default" and "enabled".
+                    if (!hasRunOnce) {
+                        List<ApplicationInfo> associatedAppList = associatedApps.get(packageName);
+                        if (associatedAppList != null) {
+                            for (ApplicationInfo associatedApp : associatedAppList) {
+                                if (associatedApp.enabledSetting
+                                        == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
+                                    Slog.i(TAG,
+                                            "Update associated state(" + associatedApp.packageName
+                                                    + "): DISABLED_UNTIL_USED for user " + userId);
+                                    packageManager.setApplicationEnabledSetting(
+                                            associatedApp.packageName,
+                                            PackageManager
+                                                    .COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED,
+                                            0,
+                                            userId,
+                                            callingPackage);
+                                }
+                            }
+                        }
                     }
                 }
+            }
 
-                // Always re-grant default permissions to carrier apps w/ privileges.
-                if (hasPrivileges) {
-                    enabledCarrierPackages.add(ai.packageName);
-                }
+            // Mark the execution so we do not disable apps again.
+            if (!hasRunOnce) {
+                Settings.Secure.putIntForUser(
+                        contentResolver, Settings.Secure.CARRIER_APPS_HANDLED, 1, userId);
             }
 
             if (!enabledCarrierPackages.isEmpty()) {
@@ -190,8 +278,8 @@
      *
      * These are the apps subject to the hiding/showing logic in
      * {@link CarrierAppUtils#disableCarrierAppsUntilPrivileged(String, IPackageManager,
-     * TelephonyManager, int)}, as well as the apps which should have default permissions granted,
-     * when a matching SIM is inserted.
+     * TelephonyManager, ContentResolver, int)}, as well as the apps which should have default
+     * permissions granted, when a matching SIM is inserted.
      *
      * Whether or not the app is actually considered a default app depends on whether the app has
      * carrier privileges as determined by the SIMs in the device.
@@ -205,30 +293,68 @@
     }
 
     private static List<ApplicationInfo> getDefaultCarrierAppCandidatesHelper(
-            IPackageManager packageManager, int userId,
+            IPackageManager packageManager,
+            int userId,
             String[] systemCarrierAppsDisabledUntilUsed) {
         if (systemCarrierAppsDisabledUntilUsed == null
                 || systemCarrierAppsDisabledUntilUsed.length == 0) {
             return null;
         }
-        List<ApplicationInfo> apps = null;
-        try {
-            apps = new ArrayList<>(systemCarrierAppsDisabledUntilUsed.length);
-            for (String packageName : systemCarrierAppsDisabledUntilUsed) {
-                ApplicationInfo ai = packageManager.getApplicationInfo(packageName,
-                        PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS, userId);
-                if (ai == null) {
-                    // No app found for packageName
-                    continue;
-                }
-                if (!ai.isSystemApp()) {
-                    continue;
-                }
+        List<ApplicationInfo> apps = new ArrayList<>(systemCarrierAppsDisabledUntilUsed.length);
+        for (int i = 0; i < systemCarrierAppsDisabledUntilUsed.length; i++) {
+            String packageName = systemCarrierAppsDisabledUntilUsed[i];
+            ApplicationInfo ai =
+                    getApplicationInfoIfSystemApp(packageManager, userId, packageName);
+            if (ai != null) {
                 apps.add(ai);
             }
+        }
+        return apps;
+    }
+
+    private static Map<String, List<ApplicationInfo>> getDefaultCarrierAssociatedAppsHelper(
+            IPackageManager packageManager,
+            int userId,
+            ArrayMap<String, List<String>> systemCarrierAssociatedAppsDisabledUntilUsed) {
+        int size = systemCarrierAssociatedAppsDisabledUntilUsed.size();
+        Map<String, List<ApplicationInfo>> associatedApps = new ArrayMap<>(size);
+        for (int i = 0; i < size; i++) {
+            String carrierAppPackage = systemCarrierAssociatedAppsDisabledUntilUsed.keyAt(i);
+            List<String> associatedAppPackages =
+                    systemCarrierAssociatedAppsDisabledUntilUsed.valueAt(i);
+            for (int j = 0; j < associatedAppPackages.size(); j++) {
+                ApplicationInfo ai =
+                        getApplicationInfoIfSystemApp(
+                                packageManager, userId, associatedAppPackages.get(j));
+                // Only update enabled state for the app on /system. Once it has been updated we
+                // shouldn't touch it.
+                if (ai != null && !ai.isUpdatedSystemApp()) {
+                    List<ApplicationInfo> appList = associatedApps.get(carrierAppPackage);
+                    if (appList == null) {
+                        appList = new ArrayList<>();
+                        associatedApps.put(carrierAppPackage, appList);
+                    }
+                    appList.add(ai);
+                }
+            }
+        }
+        return associatedApps;
+    }
+
+    @Nullable
+    private static ApplicationInfo getApplicationInfoIfSystemApp(
+            IPackageManager packageManager,
+            int userId,
+            String packageName) {
+        try {
+            ApplicationInfo ai = packageManager.getApplicationInfo(packageName,
+                    PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, userId);
+            if (ai != null && ai.isSystemApp()) {
+                return ai;
+            }
         } catch (RemoteException e) {
             Slog.w(TAG, "Could not reach PackageManager", e);
         }
-        return apps;
+        return null;
     }
 }
diff --git a/telephony/java/com/android/internal/telephony/DctConstants.java b/telephony/java/com/android/internal/telephony/DctConstants.java
index e4981ce..f01e4c0 100644
--- a/telephony/java/com/android/internal/telephony/DctConstants.java
+++ b/telephony/java/com/android/internal/telephony/DctConstants.java
@@ -104,6 +104,8 @@
     public static final int CMD_CLEAR_PROVISIONING_SPINNER = BASE + 42;
     public static final int EVENT_DEVICE_PROVISIONED_CHANGE = BASE + 43;
     public static final int EVENT_REDIRECTION_DETECTED = BASE + 44;
+    public static final int EVENT_PCO_DATA_RECEIVED = BASE + 45;
+    public static final int EVENT_SET_CARRIER_DATA_ENABLED = BASE + 46;
 
     /***** Constants *****/
 
diff --git a/telephony/java/com/android/internal/telephony/ISms.aidl b/telephony/java/com/android/internal/telephony/ISms.aidl
index 70a8653..6115656 100644
--- a/telephony/java/com/android/internal/telephony/ISms.aidl
+++ b/telephony/java/com/android/internal/telephony/ISms.aidl
@@ -184,7 +184,7 @@
      */
     void sendTextForSubscriberWithSelfPermissions(in int subId, String callingPkg,
             in String destAddr, in String scAddr, in String text, in PendingIntent sentIntent,
-            in PendingIntent deliveryIntent);
+            in PendingIntent deliveryIntent, in boolean persistMessage);
 
     /**
      * Inject an SMS PDU into the android platform.
diff --git a/telephony/java/com/android/internal/telephony/ISub.aidl b/telephony/java/com/android/internal/telephony/ISub.aidl
index c61ed2a..f6aef08 100755
--- a/telephony/java/com/android/internal/telephony/ISub.aidl
+++ b/telephony/java/com/android/internal/telephony/ISub.aidl
@@ -118,17 +118,6 @@
     int setDisplayName(String displayName, int subId);
 
     /**
-     * Set Sim Provisioning Status by subscription ID
-     * @param simProvisionStatus with the subscription:
-     * {@See SubscriptionManager#SIM_PROVISIONED}
-     * {@See SubscriptionManager#SIM_UNPROVISIONED_COLD}
-     * {@See SubscriptionManager#SIM_UNPROVISIONED_OUT_OF_CREDIT}
-     * @param subId the unique SubInfoRecord index in database
-     * @return the number of records updated
-     */
-    int setSimProvisioningStatus(int simProvisioningStatus, int subId);
-
-    /**
      * Set display name by simInfo index with name source
      * @param displayName the display name of SIM card
      * @param subId the unique SubscriptionInfo index in database
diff --git a/telephony/java/com/android/internal/telephony/ITelephony.aidl b/telephony/java/com/android/internal/telephony/ITelephony.aidl
index bb8aaad5..a8eaf36 100644
--- a/telephony/java/com/android/internal/telephony/ITelephony.aidl
+++ b/telephony/java/com/android/internal/telephony/ITelephony.aidl
@@ -20,6 +20,7 @@
 import android.os.Bundle;
 import android.os.ResultReceiver;
 import android.net.Uri;
+import android.service.carrier.CarrierIdentifier;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
 import android.telephony.CellInfo;
@@ -28,8 +29,11 @@
 import android.telephony.NeighboringCellInfo;
 import android.telephony.RadioAccessFamily;
 import android.telephony.ServiceState;
+import android.telephony.TelephonyHistogram;
+import android.telephony.VisualVoicemailSmsFilterSettings;
 import com.android.internal.telephony.CellNetworkScanResult;
 import com.android.internal.telephony.OperatorInfo;
+
 import java.util.List;
 
 
@@ -450,6 +454,26 @@
      */
     int getVoiceMessageCountForSubscriber(int subId);
 
+    oneway void setVisualVoicemailEnabled(String callingPackage,
+            in PhoneAccountHandle accountHandle, boolean enabled);
+
+    boolean isVisualVoicemailEnabled(String callingPackage,
+            in PhoneAccountHandle accountHandle);
+
+    // Not oneway, caller needs to make sure the vaule is set before receiving a SMS
+    void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
+            in VisualVoicemailSmsFilterSettings settings);
+
+    oneway void disableVisualVoicemailSmsFilter(String callingPackage, int subId);
+
+    // Get settings set by the calling package
+    VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(String callingPackage,
+            int subId);
+
+    // Get settings set by the package, requires READ_PRIVILEGED_PHONE_STATE permission
+    VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(String packageName,
+            int subId);
+
     /**
      * Returns the network type for data transmission
      * Legacy call, permission-free
@@ -1067,4 +1091,95 @@
      * Returns a list of packages that have carrier privileges.
      */
     List<String> getPackagesWithCarrierPrivileges();
+
+    /**
+     * Return the application ID for the app type.
+     *
+     * @param subId the subscription ID that this request applies to.
+     * @param appType the uicc app type,
+     * @return Application ID for specificied app type or null if no uicc or error.
+     */
+    String getAidForAppType(int subId, int appType);
+
+    /**
+    * Return the Electronic Serial Number.
+    *
+    * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
+    *
+    * @param subId the subscription ID that this request applies to.
+    * @return ESN or null if error.
+    * @hide
+    */
+    String getEsn(int subId);
+
+    /**
+    * Return the Preferred Roaming List Version
+    *
+    * Requires that the calling app has READ_PRIVILEGED_PHONE_STATE permission
+    * @param subId the subscription ID that this request applies to.
+    * @return PRLVersion or null if error.
+    * @hide
+    */
+    String getCdmaPrlVersion(int subId);
+
+    /**
+     * Get snapshot of Telephony histograms
+     * @return List of Telephony histograms
+     * Requires Permission:
+     *   {@link android.Manifest.permission#MODIFY_PHONE_STATE MODIFY_PHONE_STATE}
+     * Or the calling app has carrier privileges.
+     */
+    List<TelephonyHistogram> getTelephonyHistograms();
+
+    /**
+     * Set the allowed carrier list for slotId
+     * Require system privileges. In the future we may add this to carrier APIs.
+     *
+     * @return The number of carriers set successfully. Should match length of
+     * carriers on success.
+     */
+    int setAllowedCarriers(int slotId, in List<CarrierIdentifier> carriers);
+
+    /**
+     * Get the allowed carrier list for slotId.
+     * Require system privileges. In the future we may add this to carrier APIs.
+     *
+     * @return List of {@link android.service.carrier.CarrierIdentifier}; empty list
+     * means all carriers are allowed.
+     */
+    List<CarrierIdentifier> getAllowedCarriers(int slotId);
+
+    /**
+     * Action set from carrier signalling broadcast receivers to enable/disable metered apns
+     * Permissions android.Manifest.permission.MODIFY_PHONE_STATE is required
+     * @param subId the subscription ID that this action applies to.
+     * @param enabled control enable or disable metered apns.
+     * @hide
+     */
+    void carrierActionSetMeteredApnsEnabled(int subId, boolean visible);
+
+    /**
+     * Action set from carrier signalling broadcast receivers to enable/disable radio
+     * Permissions android.Manifest.permission.MODIFY_PHONE_STATE is required
+     * @param subId the subscription ID that this action applies to.
+     * @param enabled control enable or disable radio.
+     * @hide
+     */
+    void carrierActionSetRadioEnabled(int subId, boolean enabled);
+
+    /**
+     * Get aggregated video call data usage since boot.
+     * Permissions android.Manifest.permission.READ_NETWORK_USAGE_HISTORY is required.
+     * @return total data usage in bytes
+     * @hide
+     */
+    long getVtDataUsage();
+
+    /**
+     * Policy control of data connection. Usually used when data limit is passed.
+     * @param enabled True if enabling the data, otherwise disabling.
+     * @param subId Subscription index
+     * @hide
+     */
+    void setPolicyDataEnabled(boolean enabled, int subId);
 }
diff --git a/telephony/java/com/android/internal/telephony/IccCardConstants.java b/telephony/java/com/android/internal/telephony/IccCardConstants.java
index c1e2518..f3d9335 100644
--- a/telephony/java/com/android/internal/telephony/IccCardConstants.java
+++ b/telephony/java/com/android/internal/telephony/IccCardConstants.java
@@ -32,6 +32,8 @@
     public static final String INTENT_VALUE_ICC_ABSENT = "ABSENT";
     /* CARD_IO_ERROR means for three consecutive times there was SIM IO error */
     static public final String INTENT_VALUE_ICC_CARD_IO_ERROR = "CARD_IO_ERROR";
+    /* CARD_RESTRICTED means card is present but not usable due to carrier restrictions */
+    static public final String INTENT_VALUE_ICC_CARD_RESTRICTED = "CARD_RESTRICTED";
     /* LOCKED means ICC is locked by pin or by network */
     public static final String INTENT_VALUE_ICC_LOCKED = "LOCKED";
     //TODO: we can remove this state in the future if Bug 18489776 analysis
@@ -74,7 +76,8 @@
         READY,          /** ordinal(5) == {@See TelephonyManager#SIM_STATE_READY} */
         NOT_READY,      /** ordinal(6) == {@See TelephonyManager#SIM_STATE_NOT_READY} */
         PERM_DISABLED,  /** ordinal(7) == {@See TelephonyManager#SIM_STATE_PERM_DISABLED} */
-        CARD_IO_ERROR;  /** ordinal(8) == {@See TelephonyManager#SIM_STATE_CARD_IO_ERROR} */
+        CARD_IO_ERROR,  /** ordinal(8) == {@See TelephonyManager#SIM_STATE_CARD_IO_ERROR} */
+        CARD_RESTRICTED;/** ordinal(9) == {@See TelephonyManager#SIM_STATE_CARD_RESTRICTED} */
 
         public boolean isPinLocked() {
             return ((this == PIN_REQUIRED) || (this == PUK_REQUIRED));
@@ -83,7 +86,8 @@
         public boolean iccCardExist() {
             return ((this == PIN_REQUIRED) || (this == PUK_REQUIRED)
                     || (this == NETWORK_LOCKED) || (this == READY)
-                    || (this == PERM_DISABLED) || (this == CARD_IO_ERROR));
+                    || (this == PERM_DISABLED) || (this == CARD_IO_ERROR)
+                    || (this == CARD_RESTRICTED));
         }
 
         public static State intToState(int state) throws IllegalArgumentException {
@@ -97,6 +101,7 @@
                 case 6: return NOT_READY;
                 case 7: return PERM_DISABLED;
                 case 8: return CARD_IO_ERROR;
+                case 9: return CARD_RESTRICTED;
                 default:
                     throw new IllegalArgumentException();
             }
diff --git a/telephony/java/com/android/internal/telephony/PhoneConstants.java b/telephony/java/com/android/internal/telephony/PhoneConstants.java
index 1680fe3..fdc68b9 100644
--- a/telephony/java/com/android/internal/telephony/PhoneConstants.java
+++ b/telephony/java/com/android/internal/telephony/PhoneConstants.java
@@ -140,6 +140,18 @@
     /** APN type for Emergency PDN. This is not an IA apn, but is used
      * for access to carrier services in an emergency call situation. */
     public static final String APN_TYPE_EMERGENCY = "emergency";
+    /** Array of all APN types */
+    public static final String[] APN_TYPES = {APN_TYPE_DEFAULT,
+            APN_TYPE_MMS,
+            APN_TYPE_SUPL,
+            APN_TYPE_DUN,
+            APN_TYPE_HIPRI,
+            APN_TYPE_FOTA,
+            APN_TYPE_IMS,
+            APN_TYPE_CBS,
+            APN_TYPE_IA,
+            APN_TYPE_EMERGENCY
+    };
 
     public static final int RIL_CARD_MAX_APPS    = 8;
 
@@ -208,4 +220,14 @@
     public static final int AUTH_CONTEXT_EAP_SIM = 128;
     public static final int AUTH_CONTEXT_EAP_AKA = 129;
     public static final int AUTH_CONTEXT_UNDEFINED = -1;
+
+    /**
+     * Value for the global property CELL_ON
+     *  0: Cell radio is off
+     *  1: Cell radio is on
+     *  2: Cell radio is off because airplane mode is enabled
+     */
+    public static final int CELL_OFF_FLAG = 0;
+    public static final int CELL_ON_FLAG = 1;
+    public static final int CELL_OFF_DUE_TO_AIRPLANE_MODE_FLAG = 2;
 }
diff --git a/telephony/java/com/android/internal/telephony/RILConstants.java b/telephony/java/com/android/internal/telephony/RILConstants.java
index cfc5305..a91e9be 100644
--- a/telephony/java/com/android/internal/telephony/RILConstants.java
+++ b/telephony/java/com/android/internal/telephony/RILConstants.java
@@ -409,6 +409,8 @@
     int RIL_REQUEST_STOP_LCE = 133;
     int RIL_REQUEST_PULL_LCEDATA = 134;
     int RIL_REQUEST_GET_ACTIVITY_INFO = 135;
+    int RIL_REQUEST_SET_ALLOWED_CARRIERS = 136;
+    int RIL_REQUEST_GET_ALLOWED_CARRIERS = 137;
 
     int RIL_RESPONSE_ACKNOWLEDGEMENT = 800;
 
@@ -459,4 +461,5 @@
     int RIL_UNSOL_ON_SS = 1043;
     int RIL_UNSOL_STK_CC_ALPHA_NOTIFY = 1044;
     int RIL_UNSOL_LCEDATA_RECV = 1045;
+    int RIL_UNSOL_PCO_DATA = 1046;
 }
diff --git a/telephony/java/com/android/internal/telephony/TelephonyIntents.java b/telephony/java/com/android/internal/telephony/TelephonyIntents.java
index 46b0fbd..0168874 100644
--- a/telephony/java/com/android/internal/telephony/TelephonyIntents.java
+++ b/telephony/java/com/android/internal/telephony/TelephonyIntents.java
@@ -402,34 +402,59 @@
     /**
      * <p>Broadcast Action: when data connections get redirected with validation failure.
      * intended for sim/account status checks and only sent to the specified carrier app
-     * feedback is via carrier/system APIs to report cold-sim, out-of-credit-sim, etc
      * The intent will have the following extra values:</p>
      * <ul>
-     *   <li>redirectUrl</li><dd>A string with the redirection url info.</dd>
-     *   <li>subId</li><dd>Sub Id which associated the data redirection.</dd>
+     *   <li>apnType</li><dd>A string with the apn type.</dd>
+     *   <li>redirectionUrl</li><dd>redirection url string</dd>
+     *   <li>subId</dt><li>Sub Id which associated the data connection failure.</dd>
      * </ul>
      * <p class="note">This is a protected intent that can only be sent by the system.</p>
      */
-    public static final String ACTION_DATA_CONNECTION_REDIRECTED =
-            "android.intent.action.REDIRECTION_DETECTED";
+    public static final String ACTION_CARRIER_SIGNAL_REDIRECTED =
+            "android.intent.action.CARRIER_SIGNAL_REDIRECTED";
     /**
      * <p>Broadcast Action: when data connections setup fails.
      * intended for sim/account status checks and only sent to the specified carrier app
-     * feedback is via carrier/system APIs to report cold-sim, out-of-credit-sim, etc
      * The intent will have the following extra values:</p>
      * <ul>
      *   <li>apnType</li><dd>A string with the apn type.</dd>
      *   <li>errorCode</li><dd>A integer with dataFailCause.</dd>
-     *   <li>subId</dt><li>Sub Id which associated the data redirection.</dd>
+     *   <li>subId</dt><li>Sub Id which associated the data connection failure.</dd>
      * </ul>
      * <p class="note">This is a protected intent that can only be sent by the system. </p>
      */
-    public static final String ACTION_REQUEST_NETWORK_FAILED =
-            "android.intent.action.REQUEST_NETWORK_FAILED";
+    public static final String ACTION_CARRIER_SIGNAL_REQUEST_NETWORK_FAILED =
+            "android.intent.action.CARRIER_SIGNAL_REQUEST_NETWORK_FAILED";
 
     /**
-     * Broadcast action to trigger CI OMA-DM Session.
+     * <p>Broadcast Action: when pco value is available.
+     * intended for sim/account status checks and only sent to the specified carrier app
+     * The intent will have the following extra values:</p>
+     * <ul>
+     *   <li>apnType</li><dd>A string with the apn type.</dd>
+     *   <li>apnProto</li><dd>A string with the protocol of the apn connection (IP,IPV6,
+     *                        IPV4V6)</dd>
+     *   <li>pcoId</li><dd>An integer indicating the pco id for the data.</dd>
+     *   <li>pcoValue</li><dd>A byte array of pco data read from modem.</dd>
+     *   <li>subId</dt><li>Sub Id which associated the data connection.</dd>
+     * </ul>
+     * <p class="note">This is a protected intent that can only be sent by the system. </p>
      */
+    public static final String ACTION_CARRIER_SIGNAL_PCO_VALUE =
+            "android.intent.action.CARRIER_SIGNAL_PCO_VALUE";
+
+    // CARRIER_SIGNAL_ACTION extra keys
+    public static final String EXTRA_REDIRECTION_URL_KEY = "redirectionUrl";
+    public static final String EXTRA_ERROR_CODE_KEY = "errorCode";
+    public static final String EXTRA_APN_TYPE_KEY = "apnType";
+    public static final String EXTRA_APN_PROTO_KEY = "apnProto";
+    public static final String EXTRA_PCO_ID_KEY = "pcoId";
+    public static final String EXTRA_PCO_VALUE_KEY = "pcoValue";
+
+
+   /**
+     * Broadcast action to trigger CI OMA-DM Session.
+    */
     public static final String ACTION_REQUEST_OMADM_CONFIGURATION_UPDATE =
             "com.android.omadm.service.CONFIGURATION_UPDATE";
 }
diff --git a/tests/AppLaunch/src/com/android/tests/applaunch/AppLaunch.java b/tests/AppLaunch/src/com/android/tests/applaunch/AppLaunch.java
index 2346f85..16a0def 100644
--- a/tests/AppLaunch/src/com/android/tests/applaunch/AppLaunch.java
+++ b/tests/AppLaunch/src/com/android/tests/applaunch/AppLaunch.java
@@ -574,6 +574,7 @@
             mLaunchIntent = intent;
             mForceStopBeforeLaunch = forceStopBeforeLaunch;
             mLaunchReason = launchReason;
+            mResult = -1L;
         }
 
         public Long getResult() {
diff --git a/tests/Assist/res/drawable/assistant.xml b/tests/Assist/res/drawable/assistant.xml
index 2a89dda..56fe2de 100644
--- a/tests/Assist/res/drawable/assistant.xml
+++ b/tests/Assist/res/drawable/assistant.xml
@@ -19,9 +19,6 @@
         android:viewportWidth="48.0"
         android:viewportHeight="48.0">
     <path
-        android:pathData="M0 0h48v48H0z"
-        android:fillColor="#00000000"/>
-    <path
         android:fillColor="#FF000000"
         android:pathData="M38.0,4.0L10.0,4.0C7.79,4.0 6.0,5.79 6.0,8.0l0.0,28.0c0.0,2.21 1.79,4.0 4.0,4.0l8.0,0.0l6.0,6.0 6.0,-6.0l8.0,0.0c2.21,0.0 4.0,-1.79 4.0,-4.0L36.0,8.0c0.0,-2.21 -1.79,-4.0 -4.0,-4.0zM27.75,25.75L24.0,34.0l-3.75,-8.25L12.0,22.0l8.25,-3.75L24.0,10.0l3.75,8.25L36.0,22.0l-8.25,3.75z"/>
 </vector>
diff --git a/tests/Assist/src/com/android/test/assist/AssistInteractionSession.java b/tests/Assist/src/com/android/test/assist/AssistInteractionSession.java
index 851bda9..0718628 100644
--- a/tests/Assist/src/com/android/test/assist/AssistInteractionSession.java
+++ b/tests/Assist/src/com/android/test/assist/AssistInteractionSession.java
@@ -77,6 +77,9 @@
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
+
+        getWindow().getWindow().getDecorView().setSystemUiVisibility(
+                View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
     }
 
     @Override
diff --git a/tests/SoundTriggerTestApp/AndroidManifest.xml b/tests/SoundTriggerTestApp/AndroidManifest.xml
index 71d6001..87f3e92 100644
--- a/tests/SoundTriggerTestApp/AndroidManifest.xml
+++ b/tests/SoundTriggerTestApp/AndroidManifest.xml
@@ -1,25 +1,28 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.android.test.soundtrigger">
-
+    <uses-permission android:name="android.permission.CAPTURE_AUDIO_HOTWORD" />
     <uses-permission android:name="android.permission.MANAGE_SOUND_TRIGGER" />
+    <uses-permission android:name="android.permission.RECORD_AUDIO" />
     <uses-permission android:name="android.permission.WAKE_LOCK" />
     <application>
         <activity
-            android:name="TestSoundTriggerActivity"
+            android:name=".SoundTriggerTestActivity"
             android:label="SoundTrigger Test Application"
             android:screenOrientation="portrait"
             android:theme="@android:style/Theme.Material">
-            <!--
-            <intent-filter>
-                <action android:name="com.android.intent.action.MANAGE_SOUND_TRIGGER" />
-                <category android:name="android.intent.category.DEFAULT" />
-            </intent-filter>
-            -->
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.DEFAULT" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+        <service
+            android:name=".SoundTriggerTestService"
+            android:stopWithTask="false"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="com.android.intent.action.MANAGE_SOUND_TRIGGER" />
+            </intent-filter>
+        </service>
     </application>
 </manifest>
diff --git a/tests/SoundTriggerTestApp/res/layout/main.xml b/tests/SoundTriggerTestApp/res/layout/main.xml
index 06949a0..0fd8b12 100644
--- a/tests/SoundTriggerTestApp/res/layout/main.xml
+++ b/tests/SoundTriggerTestApp/res/layout/main.xml
@@ -18,81 +18,107 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
-    android:orientation="vertical"
-    >
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="wrap_content"
-    >
+    android:orientation="vertical">
 
-    <Button
-        android:layout_width="wrap_content"
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/load"
+            android:onClick="onLoadButtonClicked"
+            android:padding="20dp" />
+
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/start_recog"
+            android:onClick="onStartRecognitionButtonClicked"
+            android:padding="20dp" />
+
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/stop_recog"
+            android:onClick="onStopRecognitionButtonClicked"
+            android:padding="20dp" />
+
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/unload"
+            android:onClick="onUnloadButtonClicked"
+            android:padding="20dp" />
+
+    </LinearLayout>
+
+    <LinearLayout
+        android:orientation="horizontal"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content">
+
+        <Button
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/reload"
+            android:onClick="onReloadButtonClicked"
+            android:padding="20dp" />
+
+        <Button
+            android:id="@+id/play_trigger_id"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/play_trigger"
+            android:onClick="onPlayTriggerButtonClicked"
+            android:padding="20dp" />
+    </LinearLayout>
+
+    <LinearLayout
+        android:orientation="horizontal"
+        android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:text="@string/enroll"
-        android:onClick="onEnrollButtonClicked"
-        android:padding="20dp" />
+        android:layout_gravity="right">
 
-    <Button
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/reenroll"
-        android:onClick="onReEnrollButtonClicked"
-        android:padding="20dp" />
+        <CheckBox
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/capture"
+            android:id="@+id/caputre_check_box"
+            android:layout_gravity="center_horizontal"
+            android:padding="20dp" />
 
-    <Button
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/start_recog"
-        android:onClick="onStartRecognitionButtonClicked"
-        android:padding="20dp" />
+        <Button
+            android:id="@+id/play_captured_id"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:text="@string/play_capture"
+            android:padding="20dp"
+            android:enabled="false" />
+    </LinearLayout>
 
-    <Button
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/stop_recog"
-        android:onClick="onStopRecognitionButtonClicked"
-        android:padding="20dp" />
-
-    <Button
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/unenroll"
-        android:onClick="onUnEnrollButtonClicked"
-        android:padding="20dp" />
-
-    <Button
-        android:id="@+id/play_trigger_id"
-        android:layout_width="wrap_content"
-        android:layout_height="wrap_content"
-        android:text="@string/play_trigger"
-        android:onClick="onPlayTriggerButtonClicked"
-        android:padding="20dp" />
-
-</LinearLayout>
-
-<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
+    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/model_group_id"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:padding="20dp"
-        android:orientation="vertical">
-</RadioGroup>
+        android:orientation="vertical" />
 
-<ScrollView
+    <ScrollView
        android:id="@+id/scroller_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:fillViewport="true">
 
-      <TextView
-        android:id="@+id/console"
-        android:paddingTop="20pt"
-        android:layout_height="fill_parent"
-        android:layout_width="fill_parent"
-        android:textSize="14dp"
-        android:layout_weight="1.0"
-        android:text="@string/none">
-     </TextView>
-</ScrollView>
+        <TextView
+            android:id="@+id/console"
+            android:paddingTop="20pt"
+            android:layout_height="fill_parent"
+            android:layout_width="fill_parent"
+            android:textSize="14dp"
+            android:layout_weight="1.0"
+            android:text="@string/none" />
+    </ScrollView>
 </LinearLayout>
diff --git a/tests/SoundTriggerTestApp/res/values/strings.xml b/tests/SoundTriggerTestApp/res/values/strings.xml
index 7c1f649..c48b648 100644
--- a/tests/SoundTriggerTestApp/res/values/strings.xml
+++ b/tests/SoundTriggerTestApp/res/values/strings.xml
@@ -16,11 +16,14 @@
 -->
 <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
 
-    <string name="enroll">Load</string>
-    <string name="reenroll">Re-load</string>
-    <string name="unenroll">Un-load</string>
+    <string name="load">Load</string>
+    <string name="reload">Reload</string>
+    <string name="unload">Unload</string>
     <string name="start_recog">Start</string>
     <string name="stop_recog">Stop</string>
     <string name="play_trigger">Play Trigger Audio</string>
+    <string name="capture">Capture Audio</string>
+    <string name="stop_capture">Stop Capturing Audio</string>
+    <string name="play_capture">Play Captured Audio</string>
     <string name="none">Debug messages appear here:\n</string>
 </resources>
diff --git a/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerTestActivity.java b/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerTestActivity.java
new file mode 100644
index 0000000..4841bc5
--- /dev/null
+++ b/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerTestActivity.java
@@ -0,0 +1,329 @@
+/*
+ * Copyright (C) 2014 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.soundtrigger;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import android.Manifest;
+import android.app.Activity;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.ServiceConnection;
+import android.content.pm.PackageManager;
+import android.media.AudioManager;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.IBinder;
+import android.os.PowerManager;
+import android.text.Editable;
+import android.text.method.ScrollingMovementMethod;
+import android.util.Log;
+import android.view.View;
+import android.view.WindowManager;
+import android.widget.Button;
+import android.widget.CheckBox;
+import android.widget.RadioButton;
+import android.widget.RadioGroup;
+import android.widget.ScrollView;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.android.test.soundtrigger.SoundTriggerTestService.SoundTriggerTestBinder;
+
+public class SoundTriggerTestActivity extends Activity implements SoundTriggerTestService.UserActivity {
+    private static final String TAG = "SoundTriggerTest";
+    private static final int AUDIO_PERMISSIONS_REQUEST = 1;
+
+    private SoundTriggerTestService mService = null;
+
+    private static UUID mSelectedModelUuid = null;
+
+    private Map<RadioButton, UUID> mButtonModelUuidMap;
+    private Map<UUID, RadioButton> mModelButtons;
+    private Map<UUID, String> mModelNames;
+    private List<RadioButton> mModelRadioButtons;
+
+    private TextView mDebugView = null;
+    private ScrollView mScrollView = null;
+    private Button mPlayTriggerButton = null;
+    private PowerManager.WakeLock mScreenWakelock;
+    private Handler mHandler;
+    private RadioGroup mRadioGroup;
+    private CheckBox mCaptureAudioCheckBox;
+    private Button mPlayCapturedAudioButton = null;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        // Make sure that this activity can punch through the lockscreen if needed.
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
+                             WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
+
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.main);
+        mDebugView = (TextView) findViewById(R.id.console);
+        mScrollView = (ScrollView) findViewById(R.id.scroller_id);
+        mRadioGroup = (RadioGroup) findViewById(R.id.model_group_id);
+        mPlayTriggerButton = (Button) findViewById(R.id.play_trigger_id);
+        mDebugView.setText(mDebugView.getText(), TextView.BufferType.EDITABLE);
+        mDebugView.setMovementMethod(new ScrollingMovementMethod());
+        mCaptureAudioCheckBox = (CheckBox) findViewById(R.id.caputre_check_box);
+        mPlayCapturedAudioButton = (Button) findViewById(R.id.play_captured_id);
+        mHandler = new Handler();
+        mButtonModelUuidMap = new HashMap();
+        mModelButtons = new HashMap();
+        mModelNames = new HashMap();
+        mModelRadioButtons = new LinkedList();
+
+        setVolumeControlStream(AudioManager.STREAM_MUSIC);
+
+        // Make sure that the service is started, so even if our activity goes down, we'll still
+        // have a request for it to run.
+        startService(new Intent(getBaseContext(), SoundTriggerTestService.class));
+
+        // Bind to SoundTriggerTestService.
+        Intent intent = new Intent(this, SoundTriggerTestService.class);
+        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
+    }
+
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+
+        // Unbind from the service.
+        if (mService != null) {
+            mService.setUserActivity(null);
+            unbindService(mConnection);
+        }
+    }
+
+    @Override
+    public void addModel(UUID modelUuid, String name) {
+        // Create a new widget for this model, and insert everything we'd need into the map.
+        RadioButton button = new RadioButton(this);
+        mModelRadioButtons.add(button);
+        button.setText(name);
+        button.setOnClickListener(new View.OnClickListener() {
+            public void onClick(View v) {
+                onRadioButtonClicked(v);
+            }
+        });
+        mButtonModelUuidMap.put(button, modelUuid);
+        mModelButtons.put(modelUuid, button);
+        mModelNames.put(modelUuid, name);
+
+        // Sort all the radio buttons by name, then push them into the group in order.
+        Collections.sort(mModelRadioButtons, new Comparator<RadioButton>(){
+            @Override
+            public int compare(RadioButton button0, RadioButton button1) {
+                return button0.getText().toString().compareTo(button1.getText().toString());
+            }
+        });
+        mRadioGroup.removeAllViews();
+        for (View v : mModelRadioButtons) {
+            mRadioGroup.addView(v);
+        }
+
+        // If we don't have something selected, select this first thing.
+        if (mSelectedModelUuid == null || mSelectedModelUuid.equals(modelUuid)) {
+            button.setChecked(true);
+            onRadioButtonClicked(button);
+        }
+    }
+
+    @Override
+    public void setModelState(UUID modelUuid, String state) {
+        runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                String newButtonText = mModelNames.get(modelUuid);
+                if (state != null) {
+                    newButtonText += ": " + state;
+                }
+                mModelButtons.get(modelUuid).setText(newButtonText);
+                updateSelectModelSpecificUiElements();
+            }
+        });
+    }
+
+    @Override
+    public void showMessage(String msg, boolean showToast) {
+        // Append the message to the text field, then show the toast if requested.
+        this.runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                ((Editable) mDebugView.getText()).append(msg + "\n");
+                mScrollView.post(new Runnable() {
+                    public void run() {
+                        mScrollView.smoothScrollTo(0, mDebugView.getBottom());
+                    }
+                });
+                if (showToast) {
+                    Toast.makeText(SoundTriggerTestActivity.this, msg, Toast.LENGTH_SHORT).show();
+                }
+            }
+        });
+    }
+
+    @Override
+    public void handleDetection(UUID modelUuid) {
+        screenWakeup();
+        mHandler.postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                screenRelease();
+            }
+        }, 1000L);
+    }
+
+    private void screenWakeup() {
+        if (mScreenWakelock == null) {
+            PowerManager pm = ((PowerManager)getSystemService(POWER_SERVICE));
+            mScreenWakelock =  pm.newWakeLock(
+                    PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);
+        }
+        mScreenWakelock.acquire();
+    }
+
+    private void screenRelease() {
+        mScreenWakelock.release();
+    }
+
+    public void onLoadButtonClicked(View v) {
+        if (mService == null) {
+            Log.e(TAG, "Could not load sound model: not bound to SoundTriggerTestService");
+        } else {
+            mService.loadModel(mSelectedModelUuid);
+        }
+    }
+
+    public void onUnloadButtonClicked(View v) {
+        if (mService == null) {
+           Log.e(TAG, "Can't unload model: not bound to SoundTriggerTestService");
+        } else {
+            mService.unloadModel(mSelectedModelUuid);
+        }
+    }
+
+    public void onReloadButtonClicked(View v) {
+        if (mService == null) {
+            Log.e(TAG, "Can't reload model: not bound to SoundTriggerTestService");
+        } else {
+            mService.reloadModel(mSelectedModelUuid);
+        }
+    }
+
+    public void onStartRecognitionButtonClicked(View v) {
+        if (mService == null) {
+            Log.e(TAG, "Can't start recognition: not bound to SoundTriggerTestService");
+        } else {
+            mService.startRecognition(mSelectedModelUuid);
+        }
+    }
+
+    public void onStopRecognitionButtonClicked(View v) {
+        if (mService == null) {
+            Log.e(TAG, "Can't stop recognition: not bound to SoundTriggerTestService");
+        } else {
+            mService.stopRecognition(mSelectedModelUuid);
+        }
+    }
+
+    public synchronized void onPlayTriggerButtonClicked(View v) {
+        if (mService == null) {
+            Log.e(TAG, "Can't play trigger audio: not bound to SoundTriggerTestService");
+        } else {
+            mService.playTriggerAudio(mSelectedModelUuid);
+        }
+    }
+
+    public synchronized void onCaptureAudioCheckboxClicked(View v) {
+        // See if we have the right permissions
+        if (!mService.hasMicrophonePermission()) {
+            requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO},
+                    AUDIO_PERMISSIONS_REQUEST);
+            return;
+        } else {
+            mService.setCaptureAudio(mSelectedModelUuid, mCaptureAudioCheckBox.isChecked());
+        }
+    }
+
+    @Override
+    public synchronized void onRequestPermissionsResult(int requestCode, String permissions[],
+                                                        int[] grantResults) {
+        if (requestCode == AUDIO_PERMISSIONS_REQUEST) {
+            if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
+                // Make sure that the check box is set to false.
+                mCaptureAudioCheckBox.setChecked(false);
+            }
+            mService.setCaptureAudio(mSelectedModelUuid, mCaptureAudioCheckBox.isChecked());
+        }
+    }
+
+    public synchronized void onPlayCapturedAudioButtonClicked(View v) {
+        if (mService == null) {
+            Log.e(TAG, "Can't play captured audio: not bound to SoundTriggerTestService");
+        } else {
+            mService.playCapturedAudio(mSelectedModelUuid);
+        }
+    }
+
+    public synchronized void onRadioButtonClicked(View view) {
+        // Is the button now checked?
+        boolean checked = ((RadioButton) view).isChecked();
+        if (checked) {
+            mSelectedModelUuid = mButtonModelUuidMap.get(view);
+            showMessage("Selected " + mModelNames.get(mSelectedModelUuid), false);
+            updateSelectModelSpecificUiElements();
+        }
+    }
+
+    private synchronized void updateSelectModelSpecificUiElements() {
+        // Set the play trigger button to be enabled only if we actually have some audio.
+        mPlayTriggerButton.setEnabled(mService.modelHasTriggerAudio((mSelectedModelUuid)));
+        // Similar logic for the captured audio.
+        mCaptureAudioCheckBox.setChecked(
+                mService.modelWillCaptureTriggerAudio(mSelectedModelUuid));
+        mPlayCapturedAudioButton.setEnabled(mService.modelHasCapturedAudio((mSelectedModelUuid)));
+    }
+
+    private ServiceConnection mConnection = new ServiceConnection() {
+        @Override
+        public void onServiceConnected(ComponentName className, IBinder service) {
+            synchronized (SoundTriggerTestActivity.this) {
+                // We've bound to LocalService, cast the IBinder and get LocalService instance
+                SoundTriggerTestBinder binder = (SoundTriggerTestBinder) service;
+                mService = binder.getService();
+                mService.setUserActivity(SoundTriggerTestActivity.this);
+            }
+        }
+
+        @Override
+        public void onServiceDisconnected(ComponentName arg0) {
+            synchronized (SoundTriggerTestActivity.this) {
+                mService.setUserActivity(null);
+                mService = null;
+            }
+        }
+    };
+}
diff --git a/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerTestService.java b/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerTestService.java
new file mode 100644
index 0000000..a2385d6
--- /dev/null
+++ b/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerTestService.java
@@ -0,0 +1,720 @@
+/*
+ * Copyright (C) 2014 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.soundtrigger;
+
+import android.Manifest;
+import android.app.Service;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
+import android.media.AudioAttributes;
+import android.media.AudioFormat;
+import android.media.AudioManager;
+import android.media.AudioRecord;
+import android.media.AudioTrack;
+import android.media.MediaPlayer;
+import android.media.soundtrigger.SoundTriggerDetector;
+import android.net.Uri;
+import android.os.Binder;
+import android.os.IBinder;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Random;
+import java.util.UUID;
+
+public class SoundTriggerTestService extends Service {
+    private static final String TAG = "SoundTriggerTestSrv";
+    private static final String INTENT_ACTION = "com.android.intent.action.MANAGE_SOUND_TRIGGER";
+
+    // Binder given to clients.
+    private final IBinder mBinder;
+    private final Map<UUID, ModelInfo> mModelInfoMap;
+    private SoundTriggerUtil mSoundTriggerUtil;
+    private Random mRandom;
+    private UserActivity mUserActivity;
+
+    public interface UserActivity {
+        void addModel(UUID modelUuid, String state);
+        void setModelState(UUID modelUuid, String state);
+        void showMessage(String msg, boolean showToast);
+        void handleDetection(UUID modelUuid);
+    }
+
+    public SoundTriggerTestService() {
+        super();
+        mRandom = new Random();
+        mModelInfoMap = new HashMap();
+        mBinder = new SoundTriggerTestBinder();
+    }
+
+    @Override
+    public synchronized int onStartCommand(Intent intent, int flags, int startId) {
+        if (mModelInfoMap.isEmpty()) {
+            mSoundTriggerUtil = new SoundTriggerUtil(this);
+            loadModelsInDataDir();
+        }
+
+        // If we get killed, after returning from here, restart
+        return START_STICKY;
+    }
+
+    @Override
+    public void onCreate() {
+        super.onCreate();
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(INTENT_ACTION);
+        registerReceiver(mBroadcastReceiver, filter);
+
+        // Make sure the data directory exists, and we're the owner of it.
+        try {
+            getFilesDir().mkdir();
+        } catch (Exception e) {
+            // Don't care - we either made it, or it already exists.
+        }
+    }
+
+    @Override
+    public void onDestroy() {
+        super.onDestroy();
+        stopAllRecognitions();
+        unregisterReceiver(mBroadcastReceiver);
+    }
+
+    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            if (intent != null && INTENT_ACTION.equals(intent.getAction())) {
+                String command = intent.getStringExtra("command");
+                if (command == null) {
+                    Log.e(TAG, "No 'command' specified in " + INTENT_ACTION);
+                } else {
+                    try {
+                        if (command.equals("load")) {
+                            loadModel(getModelUuidFromIntent(intent));
+                        } else if (command.equals("unload")) {
+                            unloadModel(getModelUuidFromIntent(intent));
+                        } else if (command.equals("start")) {
+                            startRecognition(getModelUuidFromIntent(intent));
+                        } else if (command.equals("stop")) {
+                            stopRecognition(getModelUuidFromIntent(intent));
+                        } else if (command.equals("play_trigger")) {
+                            playTriggerAudio(getModelUuidFromIntent(intent));
+                        } else if (command.equals("play_captured")) {
+                            playCapturedAudio(getModelUuidFromIntent(intent));
+                        } else if (command.equals("set_capture")) {
+                            setCaptureAudio(getModelUuidFromIntent(intent),
+                                    intent.getBooleanExtra("enabled", true));
+                        } else if (command.equals("set_capture_timeout")) {
+                            setCaptureAudioTimeout(getModelUuidFromIntent(intent),
+                                    intent.getIntExtra("timeout", 5000));
+                        } else {
+                            Log.e(TAG, "Unknown command '" + command + "'");
+                        }
+                    } catch (Exception e) {
+                        Log.e(TAG, "Failed to process " + command, e);
+                    }
+                }
+            }
+        }
+    };
+
+    private UUID getModelUuidFromIntent(Intent intent) {
+        // First, see if the specified the UUID straight up.
+        String value = intent.getStringExtra("modelUuid");
+        if (value != null) {
+            return UUID.fromString(value);
+        }
+
+        // If they specified a name, use that to iterate through the map of models and find it.
+        value = intent.getStringExtra("name");
+        if (value != null) {
+            for (ModelInfo modelInfo : mModelInfoMap.values()) {
+                if (value.equals(modelInfo.name)) {
+                    return modelInfo.modelUuid;
+                }
+            }
+            Log.e(TAG, "Failed to find a matching model with name '" + value + "'");
+        }
+
+        // We couldn't figure out what they were asking for.
+        throw new RuntimeException("Failed to get model from intent - specify either " +
+                "'modelUuid' or 'name'");
+    }
+
+    /**
+     * Will be called when the service is killed (through swipe aways, not if we're force killed).
+     */
+    @Override
+    public void onTaskRemoved(Intent rootIntent) {
+        super.onTaskRemoved(rootIntent);
+        stopAllRecognitions();
+        stopSelf();
+    }
+
+    @Override
+    public synchronized IBinder onBind(Intent intent) {
+        return mBinder;
+    }
+
+    public class SoundTriggerTestBinder extends Binder {
+        SoundTriggerTestService getService() {
+            // Return instance of our parent so clients can call public methods.
+            return SoundTriggerTestService.this;
+        }
+    }
+
+    public synchronized void setUserActivity(UserActivity activity) {
+        mUserActivity = activity;
+        if (mUserActivity != null) {
+            for (Map.Entry<UUID, ModelInfo> entry : mModelInfoMap.entrySet()) {
+                mUserActivity.addModel(entry.getKey(), entry.getValue().name);
+                mUserActivity.setModelState(entry.getKey(), entry.getValue().state);
+            }
+        }
+    }
+
+    private synchronized void stopAllRecognitions() {
+        for (ModelInfo modelInfo : mModelInfoMap.values()) {
+            if (modelInfo.detector != null) {
+                Log.i(TAG, "Stopping recognition for " + modelInfo.name);
+                try {
+                    modelInfo.detector.stopRecognition();
+                } catch (Exception e) {
+                    Log.e(TAG, "Failed to stop recognition", e);
+                }
+            }
+        }
+    }
+
+    // Helper struct for holding information about a model.
+    public static class ModelInfo {
+        public String name;
+        public String state;
+        public UUID modelUuid;
+        public UUID vendorUuid;
+        public MediaPlayer triggerAudioPlayer;
+        public SoundTriggerDetector detector;
+        public byte modelData[];
+        public boolean captureAudio;
+        public int captureAudioMs;
+        public AudioTrack captureAudioTrack;
+    }
+
+    private GenericSoundModel createNewSoundModel(ModelInfo modelInfo) {
+        return new GenericSoundModel(modelInfo.modelUuid, modelInfo.vendorUuid,
+                modelInfo.modelData);
+    }
+
+    public synchronized void loadModel(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+
+        postMessage("Loading model: " + modelInfo.name);
+
+        GenericSoundModel soundModel = createNewSoundModel(modelInfo);
+
+        boolean status = mSoundTriggerUtil.addOrUpdateSoundModel(soundModel);
+        if (status) {
+            postToast("Successfully loaded " + modelInfo.name + ", UUID=" + soundModel.uuid);
+            setModelState(modelInfo, "Loaded");
+        } else {
+            postErrorToast("Failed to load " + modelInfo.name + ", UUID=" + soundModel.uuid + "!");
+            setModelState(modelInfo, "Failed to load");
+        }
+    }
+
+    public synchronized void unloadModel(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+
+        postMessage("Unloading model: " + modelInfo.name);
+
+        GenericSoundModel soundModel = mSoundTriggerUtil.getSoundModel(modelUuid);
+        if (soundModel == null) {
+            postErrorToast("Sound model not found for " + modelInfo.name + "!");
+            return;
+        }
+        modelInfo.detector = null;
+        boolean status = mSoundTriggerUtil.deleteSoundModel(modelUuid);
+        if (status) {
+            postToast("Successfully unloaded " + modelInfo.name + ", UUID=" + soundModel.uuid);
+            setModelState(modelInfo, "Unloaded");
+        } else {
+            postErrorToast("Failed to unload " +
+                    modelInfo.name + ", UUID=" + soundModel.uuid + "!");
+            setModelState(modelInfo, "Failed to unload");
+        }
+    }
+
+    public synchronized void reloadModel(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+        postMessage("Reloading model: " + modelInfo.name);
+        GenericSoundModel soundModel = mSoundTriggerUtil.getSoundModel(modelUuid);
+        if (soundModel == null) {
+            postErrorToast("Sound model not found for " + modelInfo.name + "!");
+            return;
+        }
+        GenericSoundModel updated = createNewSoundModel(modelInfo);
+        boolean status = mSoundTriggerUtil.addOrUpdateSoundModel(updated);
+        if (status) {
+            postToast("Successfully reloaded " + modelInfo.name + ", UUID=" + modelInfo.modelUuid);
+            setModelState(modelInfo, "Reloaded");
+        } else {
+            postErrorToast("Failed to reload "
+                    + modelInfo.name + ", UUID=" + modelInfo.modelUuid + "!");
+            setModelState(modelInfo, "Failed to reload");
+        }
+    }
+
+    public synchronized void startRecognition(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+
+        if (modelInfo.detector == null) {
+            postMessage("Creating SoundTriggerDetector for " + modelInfo.name);
+            modelInfo.detector = mSoundTriggerUtil.createSoundTriggerDetector(
+                    modelUuid, new DetectorCallback(modelInfo));
+        }
+
+        postMessage("Starting recognition for " + modelInfo.name + ", UUID=" + modelInfo.modelUuid);
+        if (modelInfo.detector.startRecognition(modelInfo.captureAudio ?
+                SoundTriggerDetector.RECOGNITION_FLAG_CAPTURE_TRIGGER_AUDIO :
+                SoundTriggerDetector.RECOGNITION_FLAG_ALLOW_MULTIPLE_TRIGGERS)) {
+            setModelState(modelInfo, "Started");
+        } else {
+            postErrorToast("Fast failure attempting to start recognition for " +
+                    modelInfo.name + ", UUID=" + modelInfo.modelUuid);
+            setModelState(modelInfo, "Failed to start");
+        }
+    }
+
+    public synchronized void stopRecognition(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+
+        if (modelInfo.detector == null) {
+            postErrorToast("Stop called on null detector for " +
+                    modelInfo.name + ", UUID=" + modelInfo.modelUuid);
+            return;
+        }
+        postMessage("Triggering stop recognition for " +
+                modelInfo.name + ", UUID=" + modelInfo.modelUuid);
+        if (modelInfo.detector.stopRecognition()) {
+            setModelState(modelInfo, "Stopped");
+        } else {
+            postErrorToast("Fast failure attempting to stop recognition for " +
+                    modelInfo.name + ", UUID=" + modelInfo.modelUuid);
+            setModelState(modelInfo, "Failed to stop");
+        }
+    }
+
+    public synchronized void playTriggerAudio(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+        if (modelInfo.triggerAudioPlayer != null) {
+            postMessage("Playing trigger audio for " + modelInfo.name);
+            modelInfo.triggerAudioPlayer.start();
+        } else {
+            postMessage("No trigger audio for " + modelInfo.name);
+        }
+    }
+
+    public synchronized void playCapturedAudio(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+        if (modelInfo.captureAudioTrack != null) {
+            postMessage("Playing captured audio for " + modelInfo.name);
+            modelInfo.captureAudioTrack.stop();
+            modelInfo.captureAudioTrack.reloadStaticData();
+            modelInfo.captureAudioTrack.play();
+        } else {
+            postMessage("No captured audio for " + modelInfo.name);
+        }
+    }
+
+    public synchronized void setCaptureAudioTimeout(UUID modelUuid, int captureTimeoutMs) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+        modelInfo.captureAudioMs = captureTimeoutMs;
+        Log.i(TAG, "Set " + modelInfo.name + " capture audio timeout to " +
+                captureTimeoutMs + "ms");
+    }
+
+    public synchronized void setCaptureAudio(UUID modelUuid, boolean captureAudio) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        if (modelInfo == null) {
+            postError("Could not find model for: " + modelUuid.toString());
+            return;
+        }
+        modelInfo.captureAudio = captureAudio;
+        Log.i(TAG, "Set " + modelInfo.name + " capture audio to " + captureAudio);
+    }
+
+    public synchronized boolean hasMicrophonePermission() {
+        return getBaseContext().checkSelfPermission(Manifest.permission.RECORD_AUDIO)
+                == PackageManager.PERMISSION_GRANTED;
+    }
+
+    public synchronized boolean modelHasTriggerAudio(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        return modelInfo != null && modelInfo.triggerAudioPlayer != null;
+    }
+
+    public synchronized boolean modelWillCaptureTriggerAudio(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        return modelInfo != null && modelInfo.captureAudio;
+    }
+
+    public synchronized boolean modelHasCapturedAudio(UUID modelUuid) {
+        ModelInfo modelInfo = mModelInfoMap.get(modelUuid);
+        return modelInfo != null && modelInfo.captureAudioTrack != null;
+    }
+
+    private void loadModelsInDataDir() {
+        // Load all the models in the data dir.
+        boolean loadedModel = false;
+        for (File file : getFilesDir().listFiles()) {
+            // Find meta-data in .properties files, ignore everything else.
+            if (!file.getName().endsWith(".properties")) {
+                continue;
+            }
+            try {
+                Properties properties = new Properties();
+                properties.load(new FileInputStream(file));
+                createModelInfo(properties);
+                loadedModel = true;
+            } catch (Exception e) {
+                Log.e(TAG, "Failed to load properties file " + file.getName());
+            }
+        }
+
+        // Create a few dummy models if we didn't load anything.
+        if (!loadedModel) {
+            Properties dummyModelProperties = new Properties();
+            for (String name : new String[]{"1", "2", "3"}) {
+                dummyModelProperties.setProperty("name", "Model " + name);
+                createModelInfo(dummyModelProperties);
+            }
+        }
+    }
+
+    /** Parses a Properties collection to generate a sound model.
+     *
+     * Missing keys are filled in with default/random values.
+     * @param properties Has the required 'name' property, but the remaining 'modelUuid',
+     *                   'vendorUuid', 'triggerAudio', and 'dataFile' optional properties.
+     *
+     */
+    private synchronized void createModelInfo(Properties properties) {
+        try {
+            ModelInfo modelInfo = new ModelInfo();
+
+            if (!properties.containsKey("name")) {
+                throw new RuntimeException("must have a 'name' property");
+            }
+            modelInfo.name = properties.getProperty("name");
+
+            if (properties.containsKey("modelUuid")) {
+                modelInfo.modelUuid = UUID.fromString(properties.getProperty("modelUuid"));
+            } else {
+                modelInfo.modelUuid = UUID.randomUUID();
+            }
+
+            if (properties.containsKey("vendorUuid")) {
+                modelInfo.vendorUuid = UUID.fromString(properties.getProperty("vendorUuid"));
+            } else {
+                modelInfo.vendorUuid = UUID.randomUUID();
+            }
+
+            if (properties.containsKey("triggerAudio")) {
+                modelInfo.triggerAudioPlayer = MediaPlayer.create(this, Uri.parse(
+                        getFilesDir().getPath() + "/" + properties.getProperty("triggerAudio")));
+                if (modelInfo.triggerAudioPlayer.getDuration() == 0) {
+                    modelInfo.triggerAudioPlayer.release();
+                    modelInfo.triggerAudioPlayer = null;
+                }
+            }
+
+            if (properties.containsKey("dataFile")) {
+                File modelDataFile = new File(
+                        getFilesDir().getPath() + "/" + properties.getProperty("dataFile"));
+                modelInfo.modelData = new byte[(int) modelDataFile.length()];
+                FileInputStream input = new FileInputStream(modelDataFile);
+                input.read(modelInfo.modelData, 0, modelInfo.modelData.length);
+            } else {
+                modelInfo.modelData = new byte[1024];
+                mRandom.nextBytes(modelInfo.modelData);
+            }
+
+            modelInfo.captureAudioMs = Integer.parseInt((String) properties.getOrDefault(
+                    "captureAudioDurationMs", "5000"));
+
+            // TODO: Add property support for keyphrase models when they're exposed by the
+            // service.
+
+            // Update our maps containing the button -> id and id -> modelInfo.
+            mModelInfoMap.put(modelInfo.modelUuid, modelInfo);
+            if (mUserActivity != null) {
+                mUserActivity.addModel(modelInfo.modelUuid, modelInfo.name);
+                mUserActivity.setModelState(modelInfo.modelUuid, modelInfo.state);
+            }
+        } catch (IOException e) {
+            Log.e(TAG, "Error parsing properties for " + properties.getProperty("name"), e);
+        }
+    }
+
+    private class CaptureAudioRecorder implements Runnable {
+        private final ModelInfo mModelInfo;
+        private final SoundTriggerDetector.EventPayload mEvent;
+
+        public CaptureAudioRecorder(ModelInfo modelInfo, SoundTriggerDetector.EventPayload event) {
+            mModelInfo = modelInfo;
+            mEvent = event;
+        }
+
+        @Override
+        public void run() {
+            AudioFormat format = mEvent.getCaptureAudioFormat();
+            if (format == null) {
+                postErrorToast("No audio format in recognition event.");
+                return;
+            }
+
+            AudioRecord audioRecord = null;
+            AudioTrack playbackTrack = null;
+            try {
+                // Inform the audio flinger that we really do want the stream from the soundtrigger.
+                AudioAttributes.Builder attributesBuilder = new AudioAttributes.Builder();
+                attributesBuilder.setInternalCapturePreset(1999);
+                AudioAttributes attributes = attributesBuilder.build();
+
+                // Make sure we understand this kind of playback so we know how many bytes to read.
+                String encoding;
+                int bytesPerSample;
+                switch (format.getEncoding()) {
+                    case AudioFormat.ENCODING_PCM_8BIT:
+                        encoding = "8bit";
+                        bytesPerSample = 1;
+                        break;
+                    case AudioFormat.ENCODING_PCM_16BIT:
+                        encoding = "16bit";
+                        bytesPerSample = 2;
+                        break;
+                    case AudioFormat.ENCODING_PCM_FLOAT:
+                        encoding = "float";
+                        bytesPerSample = 4;
+                        break;
+                    default:
+                        throw new RuntimeException("Unhandled audio format in event");
+                }
+
+                int bytesRequired = format.getSampleRate() * format.getChannelCount() *
+                        bytesPerSample * mModelInfo.captureAudioMs / 1000;
+                int minBufferSize = AudioRecord.getMinBufferSize(
+                        format.getSampleRate(), format.getChannelMask(), format.getEncoding());
+                if (minBufferSize > bytesRequired) {
+                    bytesRequired = minBufferSize;
+                }
+
+                // Make an AudioTrack so we can play the data back out after it's finished
+                // recording.
+                try {
+                    int channelConfig = AudioFormat.CHANNEL_OUT_MONO;
+                    if (format.getChannelCount() == 2) {
+                        channelConfig = AudioFormat.CHANNEL_OUT_STEREO;
+                    } else if (format.getChannelCount() >= 3) {
+                        throw new RuntimeException(
+                                "Too many channels in captured audio for playback");
+                    }
+
+                    playbackTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
+                            format.getSampleRate(), channelConfig, format.getEncoding(),
+                            bytesRequired, AudioTrack.MODE_STATIC);
+                } catch (Exception e) {
+                    Log.e(TAG, "Exception creating playback track", e);
+                    postErrorToast("Failed to create playback track: " + e.getMessage());
+                }
+
+                audioRecord = new AudioRecord(attributes, format, bytesRequired,
+                        mEvent.getCaptureSession());
+
+                byte[] buffer = new byte[bytesRequired];
+
+                // Create a file so we can save the output data there for analysis later.
+                FileOutputStream fos  = null;
+                try {
+                    fos = new FileOutputStream( new File(
+                            getFilesDir() + File.separator + mModelInfo.name.replace(' ', '_') +
+                                    "_capture_" + format.getChannelCount() + "ch_" +
+                                    format.getSampleRate() + "hz_" + encoding + ".pcm"));
+                } catch (IOException e) {
+                    Log.e(TAG, "Failed to open output for saving PCM data", e);
+                    postErrorToast("Failed to open output for saving PCM data: " + e.getMessage());
+                }
+
+                // Inform the user we're recording.
+                setModelState(mModelInfo, "Recording");
+                audioRecord.startRecording();
+                while (bytesRequired > 0) {
+                    int bytesRead = audioRecord.read(buffer, 0, buffer.length);
+                    if (bytesRead == -1) {
+                        break;
+                    }
+                    if (fos != null) {
+                        fos.write(buffer, 0, bytesRead);
+                    }
+                    if (playbackTrack != null) {
+                        playbackTrack.write(buffer, 0, bytesRead);
+                    }
+                    bytesRequired -= bytesRead;
+                }
+                audioRecord.stop();
+            } catch (Exception e) {
+                Log.e(TAG, "Error recording trigger audio", e);
+                postErrorToast("Error recording trigger audio: " + e.getMessage());
+            } finally {
+                if (audioRecord != null) {
+                    audioRecord.release();
+                }
+                synchronized (SoundTriggerTestService.this) {
+                    if (mModelInfo.captureAudioTrack != null) {
+                        mModelInfo.captureAudioTrack.release();
+                    }
+                    mModelInfo.captureAudioTrack = playbackTrack;
+                }
+                setModelState(mModelInfo, "Recording finished");
+            }
+        }
+    }
+
+    // Implementation of SoundTriggerDetector.Callback.
+    private class DetectorCallback extends SoundTriggerDetector.Callback {
+        private final ModelInfo mModelInfo;
+
+        public DetectorCallback(ModelInfo modelInfo) {
+            mModelInfo = modelInfo;
+        }
+
+        public void onAvailabilityChanged(int status) {
+            postMessage(mModelInfo.name + " availability changed to: " + status);
+        }
+
+        public void onDetected(SoundTriggerDetector.EventPayload event) {
+            postMessage(mModelInfo.name + " onDetected(): " + eventPayloadToString(event));
+            synchronized (SoundTriggerTestService.this) {
+                if (mUserActivity != null) {
+                    mUserActivity.handleDetection(mModelInfo.modelUuid);
+                }
+                if (mModelInfo.captureAudio) {
+                    new Thread(new CaptureAudioRecorder(mModelInfo, event)).start();
+                }
+            }
+        }
+
+        public void onError() {
+            postMessage(mModelInfo.name + " onError()");
+            setModelState(mModelInfo, "Error");
+        }
+
+        public void onRecognitionPaused() {
+            postMessage(mModelInfo.name + " onRecognitionPaused()");
+            setModelState(mModelInfo, "Paused");
+        }
+
+        public void onRecognitionResumed() {
+            postMessage(mModelInfo.name + " onRecognitionResumed()");
+            setModelState(mModelInfo, "Resumed");
+        }
+    }
+
+    private String eventPayloadToString(SoundTriggerDetector.EventPayload event) {
+        String result = "EventPayload(";
+        AudioFormat format =  event.getCaptureAudioFormat();
+        result = result + "AudioFormat: " + ((format == null) ? "null" : format.toString());
+        byte[] triggerAudio = event.getTriggerAudio();
+        result = result + "TriggerAudio: " + (triggerAudio == null ? "null" : triggerAudio.length);
+        result = result + "CaptureSession: " + event.getCaptureSession();
+        result += " )";
+        return result;
+    }
+
+    private void postMessage(String msg) {
+        showMessage(msg, Log.INFO, false);
+    }
+
+    private void postError(String msg) {
+        showMessage(msg, Log.ERROR, false);
+    }
+
+    private void postToast(String msg) {
+        showMessage(msg, Log.INFO, true);
+    }
+
+    private void postErrorToast(String msg) {
+        showMessage(msg, Log.ERROR, true);
+    }
+
+    /** Logs the message at the specified level, then forwards it to the activity if present. */
+    private synchronized void showMessage(String msg, int logLevel, boolean showToast) {
+        Log.println(logLevel, TAG, msg);
+        if (mUserActivity != null) {
+            mUserActivity.showMessage(msg, showToast);
+        }
+    }
+
+    private synchronized void setModelState(ModelInfo modelInfo, String state) {
+        modelInfo.state = state;
+        if (mUserActivity != null) {
+            mUserActivity.setModelState(modelInfo.modelUuid, modelInfo.state);
+        }
+    }
+}
diff --git a/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerUtil.java b/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerUtil.java
index 1c95c25..8e5ed32 100644
--- a/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerUtil.java
+++ b/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/SoundTriggerUtil.java
@@ -18,7 +18,6 @@
 
 import android.annotation.Nullable;
 import android.content.Context;
-import android.hardware.soundtrigger.SoundTrigger;
 import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
 import android.media.soundtrigger.SoundTriggerDetector;
 import android.media.soundtrigger.SoundTriggerManager;
@@ -36,7 +35,7 @@
  * Utility class for the managing sound trigger sound models.
  */
 public class SoundTriggerUtil {
-    private static final String TAG = "TestSoundTriggerUtil:Hotsound";
+    private static final String TAG = "SoundTriggerTestUtil";
 
     private final ISoundTriggerService mSoundTriggerService;
     private final SoundTriggerManager mSoundTriggerManager;
@@ -68,10 +67,6 @@
         return true;
     }
 
-    public void addOrUpdateSoundModel(SoundTriggerManager.Model soundModel) {
-        mSoundTriggerManager.updateModel(soundModel);
-    }
-
     /**
      * Gets the sound model for the given keyphrase, null if none exists.
      * If a sound model for a given keyphrase exists, and it needs to be updated,
@@ -91,7 +86,7 @@
         }
 
         if (model == null) {
-            Log.w(TAG, "No models present for the gien keyphrase ID");
+            Log.w(TAG, "No models present for the given keyphrase ID");
             return null;
         } else {
             return model;
@@ -109,18 +104,14 @@
         try {
             mSoundTriggerService.deleteSoundModel(new ParcelUuid(modelId));
         } catch (RemoteException e) {
-            Log.e(TAG, "RemoteException in updateSoundModel");
+            Log.e(TAG, "RemoteException in deleteSoundModel");
+            return false;
         }
         return true;
     }
 
-    public void deleteSoundModelUsingManager(UUID modelId) {
-            mSoundTriggerManager.deleteModel(modelId);
-    }
-
     public SoundTriggerDetector createSoundTriggerDetector(UUID modelId,
             SoundTriggerDetector.Callback callback) {
         return mSoundTriggerManager.createSoundTriggerDetector(modelId, callback, null);
     }
-
 }
diff --git a/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/TestSoundTriggerActivity.java b/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/TestSoundTriggerActivity.java
deleted file mode 100644
index 5fd38e9..0000000
--- a/tests/SoundTriggerTestApp/src/com/android/test/soundtrigger/TestSoundTriggerActivity.java
+++ /dev/null
@@ -1,400 +0,0 @@
-/*
- * Copyright (C) 2014 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.soundtrigger;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Random;
-import java.util.UUID;
-
-import android.app.Activity;
-import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
-import android.hardware.soundtrigger.SoundTrigger;
-import android.media.AudioFormat;
-import android.media.AudioManager;
-import android.media.MediaPlayer;
-import android.media.soundtrigger.SoundTriggerDetector;
-import android.media.soundtrigger.SoundTriggerManager;
-import android.net.Uri;
-import android.os.Bundle;
-import android.os.Handler;
-import android.os.PowerManager;
-import android.os.UserManager;
-import android.text.Editable;
-import android.text.method.ScrollingMovementMethod;
-import android.util.Log;
-import android.view.View;
-import android.widget.Button;
-import android.widget.RadioButton;
-import android.widget.RadioButton;
-import android.widget.RadioGroup;
-import android.widget.ScrollView;
-import android.widget.TextView;
-import android.widget.Toast;
-
-public class TestSoundTriggerActivity extends Activity {
-    private static final String TAG = "TestSoundTriggerActivity";
-    private static final boolean DBG = false;
-
-    private SoundTriggerUtil mSoundTriggerUtil;
-    private Random mRandom;
-
-    private Map<Integer, ModelInfo> mModelInfoMap;
-    private Map<View, Integer> mModelIdMap;
-
-    private TextView mDebugView = null;
-    private int mSelectedModelId = -1;
-    private ScrollView mScrollView = null;
-    private Button mPlayTriggerButton = null;
-    private PowerManager.WakeLock mScreenWakelock;
-    private Handler mHandler;
-    private RadioGroup mRadioGroup;
-
-    @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        if (DBG) Log.d(TAG, "onCreate");
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.main);
-        mDebugView = (TextView) findViewById(R.id.console);
-        mScrollView = (ScrollView) findViewById(R.id.scroller_id);
-        mRadioGroup = (RadioGroup) findViewById(R.id.model_group_id);
-        mPlayTriggerButton = (Button) findViewById(R.id.play_trigger_id);
-        mDebugView.setText(mDebugView.getText(), TextView.BufferType.EDITABLE);
-        mDebugView.setMovementMethod(new ScrollingMovementMethod());
-        mSoundTriggerUtil = new SoundTriggerUtil(this);
-        mRandom = new Random();
-        mHandler = new Handler();
-
-        mModelInfoMap = new HashMap();
-        mModelIdMap = new HashMap();
-
-        setVolumeControlStream(AudioManager.STREAM_MUSIC);
-
-        // Load all the models in the data dir.
-        for (File file : getFilesDir().listFiles()) {
-            // Find meta-data in .properties files, ignore everything else.
-            if (!file.getName().endsWith(".properties")) {
-                continue;
-            }
-            try {
-                Properties properties = new Properties();
-                properties.load(new FileInputStream(file));
-                createModelInfoAndWidget(properties);
-            } catch (Exception e) {
-                Log.e(TAG, "Failed to load properties file " + file.getName());
-            }
-        }
-
-        // Create a few dummy models if we didn't load anything.
-        if (mModelIdMap.isEmpty()) {
-            Properties dummyModelProperties = new Properties();
-            for (String name : new String[]{"One", "Two", "Three"}) {
-                dummyModelProperties.setProperty("name", "Model " + name);
-                createModelInfoAndWidget(dummyModelProperties);
-            }
-        }
-    }
-
-    private void createModelInfoAndWidget(Properties properties) {
-        try {
-            ModelInfo modelInfo = new ModelInfo();
-
-            if (!properties.containsKey("name")) {
-                throw new RuntimeException("must have a 'name' property");
-            }
-            modelInfo.name = properties.getProperty("name");
-
-            if (properties.containsKey("modelUuid")) {
-                modelInfo.modelUuid = UUID.fromString(properties.getProperty("modelUuid"));
-            } else {
-                modelInfo.modelUuid = UUID.randomUUID();
-            }
-
-            if (properties.containsKey("vendorUuid")) {
-                modelInfo.vendorUuid = UUID.fromString(properties.getProperty("vendorUuid"));
-            } else {
-                modelInfo.vendorUuid = UUID.randomUUID();
-            }
-
-            if (properties.containsKey("triggerAudio")) {
-                modelInfo.triggerAudioPlayer = MediaPlayer.create(this, Uri.parse(
-                        getFilesDir().getPath() + "/" + properties.getProperty("triggerAudio")));
-            }
-
-            if (properties.containsKey("dataFile")) {
-                File modelDataFile = new File(
-                        getFilesDir().getPath() + "/" + properties.getProperty("dataFile"));
-                modelInfo.modelData = new byte[(int) modelDataFile.length()];
-                FileInputStream input = new FileInputStream(modelDataFile);
-                input.read(modelInfo.modelData, 0, modelInfo.modelData.length);
-            } else {
-                modelInfo.modelData = new byte[1024];
-                mRandom.nextBytes(modelInfo.modelData);
-            }
-
-            // TODO: Add property support for keyphrase models when they're exposed by the
-            // service. Also things like how much audio they should record with the capture session
-            // provided in the callback.
-
-            // Add a widget into the radio group.
-            RadioButton button = new RadioButton(this);
-            mRadioGroup.addView(button);
-            button.setText(modelInfo.name);
-            button.setOnClickListener(new View.OnClickListener() {
-                public void onClick(View v) {
-                    onRadioButtonClicked(v);
-                }
-            });
-
-            // Update our maps containing the button -> id and id -> modelInfo.
-            int newModelId = mModelIdMap.size() + 1;
-            mModelIdMap.put(button, newModelId);
-            mModelInfoMap.put(newModelId, modelInfo);
-
-            // If we don't have something selected, select this first thing.
-            if (mSelectedModelId < 0) {
-                button.setChecked(true);
-                onRadioButtonClicked(button);
-            }
-        } catch (IOException e) {
-            Log.e(TAG, "Error parsing properties for " + properties.getProperty("name"), e);
-        }
-    }
-
-    private void postMessage(String msg) {
-        Log.i(TAG, "Posted: " + msg);
-        ((Editable) mDebugView.getText()).append(msg + "\n");
-        if ((mDebugView.getMeasuredHeight() - mScrollView.getScrollY()) <=
-                (mScrollView.getHeight() + mDebugView.getLineHeight())) {
-            scrollToBottom();
-        }
-    }
-
-    private void scrollToBottom() {
-        mScrollView.post(new Runnable() {
-            public void run() {
-                mScrollView.smoothScrollTo(0, mDebugView.getBottom());
-            }
-        });
-    }
-
-    private synchronized UUID getSelectedUuid() {
-        return mModelInfoMap.get(mSelectedModelId).modelUuid;
-    }
-
-    private synchronized void setDetector(SoundTriggerDetector detector) {
-        mModelInfoMap.get(mSelectedModelId).detector = detector;
-    }
-
-    private synchronized SoundTriggerDetector getDetector() {
-        return mModelInfoMap.get(mSelectedModelId).detector;
-    }
-
-    private void screenWakeup() {
-        PowerManager pm = ((PowerManager)getSystemService(POWER_SERVICE));
-        if (mScreenWakelock == null) {
-            mScreenWakelock =  pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "TAG");
-        }
-        mScreenWakelock.acquire();
-    }
-
-    private void screenRelease() {
-        PowerManager pm = ((PowerManager)getSystemService(POWER_SERVICE));
-        mScreenWakelock.release();
-    }
-
-    /** TODO: Should return the abstract sound model that can be then sent to the service. */
-    private GenericSoundModel createNewSoundModel() {
-        ModelInfo modelInfo = mModelInfoMap.get(mSelectedModelId);
-        return new GenericSoundModel(modelInfo.modelUuid, modelInfo.vendorUuid,
-                modelInfo.modelData);
-    }
-
-    /**
-     * Called when the user clicks the enroll button.
-     * Performs a fresh enrollment.
-     */
-    public void onEnrollButtonClicked(View v) {
-        postMessage("Loading model: " + mSelectedModelId);
-
-        GenericSoundModel model = createNewSoundModel();
-
-        boolean status = mSoundTriggerUtil.addOrUpdateSoundModel(model);
-        if (status) {
-            Toast.makeText(
-                    this, "Successfully created sound trigger model UUID=" + model.uuid,
-                    Toast.LENGTH_SHORT).show();
-        } else {
-            Toast.makeText(this, "Failed to enroll!!!" + model.uuid, Toast.LENGTH_SHORT).show();
-        }
-
-        // Test the SoundManager API.
-    }
-
-    /**
-     * Called when the user clicks the un-enroll button.
-     * Clears the enrollment information for the user.
-     */
-    public void onUnEnrollButtonClicked(View v) {
-        postMessage("Unloading model: " + mSelectedModelId);
-        UUID modelUuid = getSelectedUuid();
-        GenericSoundModel soundModel = mSoundTriggerUtil.getSoundModel(modelUuid);
-        if (soundModel == null) {
-            Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show();
-            return;
-        }
-        boolean status = mSoundTriggerUtil.deleteSoundModel(modelUuid);
-        if (status) {
-            Toast.makeText(this, "Successfully deleted model UUID=" + soundModel.uuid,
-                    Toast.LENGTH_SHORT)
-                    .show();
-        } else {
-            Toast.makeText(this, "Failed to delete sound model!!!", Toast.LENGTH_SHORT).show();
-        }
-    }
-
-    /**
-     * Called when the user clicks the re-enroll button.
-     * Uses the previously enrolled sound model and makes changes to it before pushing it back.
-     */
-    public void onReEnrollButtonClicked(View v) {
-        postMessage("Re-loading model: " + mSelectedModelId);
-        UUID modelUuid = getSelectedUuid();
-        GenericSoundModel soundModel = mSoundTriggerUtil.getSoundModel(modelUuid);
-        if (soundModel == null) {
-            Toast.makeText(this, "Sound model not found!!!", Toast.LENGTH_SHORT).show();
-            return;
-        }
-        GenericSoundModel updated = createNewSoundModel();
-        boolean status = mSoundTriggerUtil.addOrUpdateSoundModel(updated);
-        if (status) {
-            Toast.makeText(this, "Successfully re-enrolled, model UUID=" + updated.uuid,
-                    Toast.LENGTH_SHORT)
-                    .show();
-        } else {
-            Toast.makeText(this, "Failed to re-enroll!!!", Toast.LENGTH_SHORT).show();
-        }
-    }
-
-    public void onStartRecognitionButtonClicked(View v) {
-        UUID modelUuid = getSelectedUuid();
-        SoundTriggerDetector detector = getDetector();
-        if (detector == null) {
-            Log.i(TAG, "Created an instance of the SoundTriggerDetector for model #" +
-                    mSelectedModelId);
-            postMessage("Created an instance of the SoundTriggerDetector for model #" +
-                    mSelectedModelId);
-            detector = mSoundTriggerUtil.createSoundTriggerDetector(modelUuid,
-                    new DetectorCallback());
-            setDetector(detector);
-        }
-        postMessage("Triggering start recognition for model: " + mSelectedModelId);
-        if (!detector.startRecognition(
-                SoundTriggerDetector.RECOGNITION_FLAG_ALLOW_MULTIPLE_TRIGGERS)) {
-            Log.e(TAG, "Fast failure attempting to start recognition.");
-            postMessage("Fast failure attempting to start recognition:" + mSelectedModelId);
-        }
-    }
-
-    public void onStopRecognitionButtonClicked(View v) {
-        SoundTriggerDetector detector = getDetector();
-        if (detector == null) {
-            Log.e(TAG, "Stop called on null detector.");
-            postMessage("Error: Stop called on null detector.");
-            return;
-        }
-        postMessage("Triggering stop recognition for model: " + mSelectedModelId);
-        if (!detector.stopRecognition()) {
-            Log.e(TAG, "Fast failure attempting to stop recognition.");
-            postMessage("Fast failure attempting to stop recognition: " + mSelectedModelId);
-        }
-    }
-
-    public synchronized void onRadioButtonClicked(View view) {
-        // Is the button now checked?
-        boolean checked = ((RadioButton) view).isChecked();
-        if (checked) {
-            mSelectedModelId = mModelIdMap.get(view);
-            ModelInfo modelInfo = mModelInfoMap.get(mSelectedModelId);
-            postMessage("Selected " + modelInfo.name);
-
-            // Set the play trigger button to be enabled only if we actually have some audio.
-            mPlayTriggerButton.setEnabled(modelInfo.triggerAudioPlayer != null);
-        }
-    }
-
-    public synchronized void onPlayTriggerButtonClicked(View v) {
-        ModelInfo modelInfo = mModelInfoMap.get(mSelectedModelId);
-        modelInfo.triggerAudioPlayer.start();
-        postMessage("Playing trigger audio for " + modelInfo.name);
-    }
-
-    // Helper struct for holding information about a model.
-    private static class ModelInfo {
-      public String name;
-      public UUID modelUuid;
-      public UUID vendorUuid;
-      public MediaPlayer triggerAudioPlayer;
-      public SoundTriggerDetector detector;
-      public byte modelData[];
-    };
-
-    // Implementation of SoundTriggerDetector.Callback.
-    public class DetectorCallback extends SoundTriggerDetector.Callback {
-        public void onAvailabilityChanged(int status) {
-            postMessage("Availability changed to: " + status);
-        }
-
-        public void onDetected(SoundTriggerDetector.EventPayload event) {
-            postMessage("onDetected(): " + eventPayloadToString(event));
-            screenWakeup();
-            mHandler.postDelayed(new Runnable() {
-                @Override
-                public void run() {
-                   screenRelease();
-                }
-            }, 1000L);
-        }
-
-        public void onError() {
-            postMessage("onError()");
-        }
-
-        public void onRecognitionPaused() {
-            postMessage("onRecognitionPaused()");
-        }
-
-        public void onRecognitionResumed() {
-            postMessage("onRecognitionResumed()");
-        }
-    }
-
-    private String eventPayloadToString(SoundTriggerDetector.EventPayload event) {
-        String result = "EventPayload(";
-        AudioFormat format =  event.getCaptureAudioFormat();
-        result = result + "AudioFormat: " + ((format == null) ? "null" : format.toString());
-        byte[] triggerAudio = event.getTriggerAudio();
-        result = result + "TriggerAudio: " + (triggerAudio == null ? "null" : triggerAudio.length);
-        result = result + "CaptureSession: " + event.getCaptureSession();
-        result += " )";
-        return result;
-    }
-}
diff --git a/tests/UiBench/AndroidManifest.xml b/tests/UiBench/AndroidManifest.xml
index 95bbb21..3d3247c 100644
--- a/tests/UiBench/AndroidManifest.xml
+++ b/tests/UiBench/AndroidManifest.xml
@@ -85,13 +85,21 @@
         </activity>
         <activity
             android:name=".TrivialRecyclerViewActivity"
-            android:label="General/Trivial Recycler ListView" >
+            android:label="General/Trivial RecyclerView" >
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="com.android.test.uibench.TEST" />
             </intent-filter>
         </activity>
         <activity
+            android:name=".SlowBindRecyclerViewActivity"
+            android:label="General/Slow Bind RecyclerView" >
+        <intent-filter>
+            <action android:name="android.intent.action.MAIN" />
+            <category android:name="com.android.test.uibench.TEST" />
+        </intent-filter>
+        </activity>
+        <activity
             android:name=".ActivityTransition"
             android:label="Transitions/Activity Transition" >
             <intent-filter>
diff --git a/tests/UiBench/src/com/android/test/uibench/MainActivity.java b/tests/UiBench/src/com/android/test/uibench/MainActivity.java
index 2111274..79837b6 100644
--- a/tests/UiBench/src/com/android/test/uibench/MainActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/MainActivity.java
@@ -38,6 +38,23 @@
     private static final String EXTRA_PATH = "activity_path";
     private static final String CATEGORY_HWUI_TEST = "com.android.test.uibench.TEST";
 
+    public static class TestListFragment extends ListFragment {
+        @Override
+        @SuppressWarnings("unchecked")
+        public void onListItemClick(ListView l, View v, int position, long id) {
+            Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
+
+            Intent intent = (Intent) map.get("intent");
+            startActivity(intent);
+        }
+
+        @Override
+        public void onViewCreated(View view, Bundle savedInstanceState) {
+            super.onViewCreated(view, savedInstanceState);
+            getListView().setTextFilterEnabled(true);
+        }
+    }
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
@@ -54,22 +71,7 @@
 
         FragmentManager fm = getSupportFragmentManager();
         if (fm.findFragmentById(android.R.id.content) == null) {
-            ListFragment listFragment = new ListFragment() {
-                @Override
-                @SuppressWarnings("unchecked")
-                public void onListItemClick(ListView l, View v, int position, long id) {
-                    Map<String, Object> map = (Map<String, Object>)l.getItemAtPosition(position);
-
-                    Intent intent = (Intent) map.get("intent");
-                    startActivity(intent);
-                }
-
-                @Override
-                public void onViewCreated(View view, Bundle savedInstanceState) {
-                    super.onViewCreated(view, savedInstanceState);
-                    getListView().setTextFilterEnabled(true);
-                }
-            };
+            ListFragment listFragment = new TestListFragment();
             listFragment.setListAdapter(new SimpleAdapter(this, getData(path),
                     android.R.layout.simple_list_item_1, new String[] { "title" },
                     new int[] { android.R.id.text1 }));
diff --git a/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java b/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java
index d32f071..88847ee 100644
--- a/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/ShadowGridActivity.java
@@ -23,19 +23,22 @@
 import android.widget.ArrayAdapter;
 
 public class ShadowGridActivity extends AppCompatActivity {
+    public static class NoDividerListFragment extends ListFragment {
+        @Override
+        public void onViewCreated(View view, Bundle savedInstanceState) {
+            super.onViewCreated(view, savedInstanceState);
+            getListView().setDivider(null);
+        }
+    };
+
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
         FragmentManager fm = getSupportFragmentManager();
         if (fm.findFragmentById(android.R.id.content) == null) {
-            ListFragment listFragment = new ListFragment() {
-                @Override
-                public void onViewCreated(View view, Bundle savedInstanceState) {
-                    super.onViewCreated(view, savedInstanceState);
-                    getListView().setDivider(null);
-                }
-            };
+            ListFragment listFragment = new NoDividerListFragment();
 
             listFragment.setListAdapter(new ArrayAdapter<>(this,
                     R.layout.card_row, R.id.card_text, TextUtils.buildSimpleStringList()));
diff --git a/tests/UiBench/src/com/android/test/uibench/SlowBindRecyclerViewActivity.java b/tests/UiBench/src/com/android/test/uibench/SlowBindRecyclerViewActivity.java
new file mode 100644
index 0000000..e32862f
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/SlowBindRecyclerViewActivity.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.test.uibench;
+
+import android.content.Context;
+import android.os.Trace;
+import android.support.v7.widget.GridLayoutManager;
+import android.support.v7.widget.RecyclerView;
+import com.android.test.uibench.recyclerview.RvBoxAdapter;
+import com.android.test.uibench.recyclerview.RvCompatListActivity;
+
+import java.util.concurrent.TimeUnit;
+
+public class SlowBindRecyclerViewActivity extends RvCompatListActivity {
+    /**
+     * Spin wait. Used instead of sleeping so a core is used up for the duration, and so
+     * traces/sampled profiling show the sections as expensive, and not just a scheduling mistake.
+     */
+    private static void spinWaitMs(long ms) {
+        long start = System.nanoTime();
+        while (System.nanoTime() - start < TimeUnit.MILLISECONDS.toNanos(ms));
+    }
+
+    @Override
+    protected RecyclerView.LayoutManager createLayoutManager(Context context) {
+        return new GridLayoutManager(context, 3);
+    }
+
+    @Override
+    protected RecyclerView.Adapter createAdapter() {
+        return new RvBoxAdapter(this, TextUtils.buildSimpleStringList()) {
+            @Override
+            public void onBindViewHolder(ViewHolder holder, int position) {
+                Trace.beginSection("bind item " + position);
+
+                spinWaitMs(3);
+                super.onBindViewHolder(holder, position);
+                Trace.endSection();
+            }
+        };
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/recyclerview/RvBoxAdapter.java b/tests/UiBench/src/com/android/test/uibench/recyclerview/RvBoxAdapter.java
new file mode 100644
index 0000000..3440f19
--- /dev/null
+++ b/tests/UiBench/src/com/android/test/uibench/recyclerview/RvBoxAdapter.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.test.uibench.recyclerview;
+
+import android.content.Context;
+import android.graphics.Color;
+import android.support.v7.widget.RecyclerView;
+import android.util.TypedValue;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class RvBoxAdapter extends RecyclerView.Adapter<RvBoxAdapter.ViewHolder> {
+
+    private int mBackground;
+
+    private List<String> mValues;
+
+    public static class ViewHolder extends RecyclerView.ViewHolder {
+        public TextView mTextView;
+
+        public ViewHolder(TextView v) {
+            super(v);
+            mTextView = v;
+        }
+
+        @Override
+        public String toString() {
+            return super.toString() + " '" + mTextView.getText();
+        }
+    }
+
+    public RvBoxAdapter(Context context, String[] strings) {
+        TypedValue val = new TypedValue();
+        if (context.getTheme() != null) {
+            context.getTheme().resolveAttribute(
+                    android.R.attr.selectableItemBackground, val, true);
+        }
+        mBackground = val.resourceId;
+        mValues = new ArrayList<>();
+        Collections.addAll(mValues, strings);
+    }
+
+    @Override
+    public RvBoxAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+        final ViewHolder h = new ViewHolder(new TextView(parent.getContext()));
+        h.mTextView.setMinimumHeight(128);
+        h.mTextView.setPadding(20, 0, 20, 0);
+        h.mTextView.setFocusable(true);
+        h.mTextView.setBackgroundResource(mBackground);
+        RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(
+                ViewGroup.LayoutParams.WRAP_CONTENT,
+                ViewGroup.LayoutParams.WRAP_CONTENT);
+        lp.leftMargin = 10;
+        lp.rightMargin = 5;
+        lp.topMargin = 20;
+        lp.bottomMargin = 15;
+        h.mTextView.setLayoutParams(lp);
+        return h;
+    }
+
+    @Override
+    public void onBindViewHolder(ViewHolder holder, int position) {
+        holder.mTextView.setText(position + ":" + mValues.get(position));
+        holder.mTextView.setMinHeight((200 + mValues.get(position).length() * 10));
+        holder.mTextView.setBackgroundColor(getBackgroundColor(position));
+    }
+
+    private int getBackgroundColor(int position) {
+        switch (position % 4) {
+            case 0: return Color.LTGRAY;
+            case 1: return Color.RED;
+            case 2: return Color.DKGRAY;
+            case 3: return Color.BLUE;
+        }
+        return Color.TRANSPARENT;
+    }
+
+    @Override
+    public int getItemCount() {
+        return mValues.size();
+    }
+}
diff --git a/tests/UiBench/src/com/android/test/uibench/recyclerview/RvCompatListActivity.java b/tests/UiBench/src/com/android/test/uibench/recyclerview/RvCompatListActivity.java
index e08dbc6..939b661 100644
--- a/tests/UiBench/src/com/android/test/uibench/recyclerview/RvCompatListActivity.java
+++ b/tests/UiBench/src/com/android/test/uibench/recyclerview/RvCompatListActivity.java
@@ -26,7 +26,6 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ArrayAdapter;
 
 import com.android.test.uibench.R;
 
diff --git a/tests/WallpaperTest/res/values/strings.xml b/tests/WallpaperTest/res/values/strings.xml
index fd21259..80a3d49 100644
--- a/tests/WallpaperTest/res/values/strings.xml
+++ b/tests/WallpaperTest/res/values/strings.xml
@@ -24,6 +24,9 @@
         Test wallpaper for use with the wallpaper test app.
     </string>
 
+    <string name="test_wallpaper_context_uri">https://www.google.com/maps/@37.8092876,-122.408986,1391m/data=!3m1!1e3</string>
+    <string name="test_wallpaper_context_description">Explore</string>
+
     <string name="dimens">Dimens: </string>
     <string name="width">Width: </string>
     <string name="height">Height: </string>
diff --git a/tests/WallpaperTest/res/xml/test_wallpaper.xml b/tests/WallpaperTest/res/xml/test_wallpaper.xml
index 9f7d714..ba22478 100644
--- a/tests/WallpaperTest/res/xml/test_wallpaper.xml
+++ b/tests/WallpaperTest/res/xml/test_wallpaper.xml
@@ -23,4 +23,7 @@
 <wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
     android:author="@string/test_wallpaper_author"
     android:description="@string/test_wallpaper_desc"
-    android:thumbnail="@drawable/test_wallpaper_thumb" />
+    android:thumbnail="@drawable/test_wallpaper_thumb"
+    android:showMetadataInPreview="true"
+    android:contextUri="@string/test_wallpaper_context_uri"
+    android:contextDescription="@string/test_wallpaper_context_description"/>
diff --git a/tests/WallpaperTest/src/com/example/wallpapertest/TestWallpaper.java b/tests/WallpaperTest/src/com/example/wallpapertest/TestWallpaper.java
index 95db6d1..ab36c22 100644
--- a/tests/WallpaperTest/src/com/example/wallpapertest/TestWallpaper.java
+++ b/tests/WallpaperTest/src/com/example/wallpapertest/TestWallpaper.java
@@ -144,6 +144,14 @@
         @Override
         public void onSurfaceCreated(SurfaceHolder holder) {
             super.onSurfaceCreated(holder);
+
+            // Simulate some slowness, so we can test the loading process in the live wallpaper
+            // picker.
+            try {
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
         }
 
         @Override
diff --git a/tests/permission/src/com/android/framework/permission/tests/WindowManagerPermissionTests.java b/tests/permission/src/com/android/framework/permission/tests/WindowManagerPermissionTests.java
index a726a15..063dd86 100644
--- a/tests/permission/src/com/android/framework/permission/tests/WindowManagerPermissionTests.java
+++ b/tests/permission/src/com/android/framework/permission/tests/WindowManagerPermissionTests.java
@@ -93,7 +93,7 @@
 
         try {
             mWm.addAppToken(0, null, 0, 0, 0, false, false, 0, 0, false, false, null,
-                    Configuration.EMPTY, 0, false, false, 0);
+                    Configuration.EMPTY, 0, false, false, 0, -1);
             fail("IWindowManager.addAppToken did not throw SecurityException as"
                     + " expected");
         } catch (SecurityException e) {
diff --git a/tests/utils/testutils/Android.mk b/tests/utils/testutils/Android.mk
index 4bfd234..d53167f 100644
--- a/tests/utils/testutils/Android.mk
+++ b/tests/utils/testutils/Android.mk
@@ -25,6 +25,6 @@
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
     android-support-test \
-    mockito-target-minus-junit4
+    mockito-target
 
 include $(BUILD_STATIC_JAVA_LIBRARY)
diff --git a/tools/aapt/Command.cpp b/tools/aapt/Command.cpp
index 78720b3..1dd43f7 100644
--- a/tools/aapt/Command.cpp
+++ b/tools/aapt/Command.cpp
@@ -529,8 +529,18 @@
     int openGLESVersion;
 };
 
+static bool hasFeature(const char* name, const FeatureGroup& grp,
+                       const KeyedVector<String8, ImpliedFeature>& implied) {
+    String8 name8(name);
+    ssize_t idx = grp.features.indexOfKey(name8);
+    if (idx < 0) {
+        idx = implied.indexOfKey(name8);
+    }
+    return idx >= 0;
+}
+
 static void addImpliedFeature(KeyedVector<String8, ImpliedFeature>* impliedFeatures,
-                              const char* name, const char* reason, bool sdk23) {
+                              const char* name, const String8& reason, bool sdk23) {
     String8 name8(name);
     ssize_t idx = impliedFeatures->indexOfKey(name8);
     if (idx < 0) {
@@ -543,7 +553,7 @@
     if (feature->impliedBySdk23 && !sdk23) {
         feature->impliedBySdk23 = false;
     }
-    feature->reasons.add(String8(reason));
+    feature->reasons.add(reason);
 }
 
 static void printFeatureGroupImpl(const FeatureGroup& grp,
@@ -616,9 +626,16 @@
     } else if (name == "android.hardware.location.gps" ||
             name == "android.hardware.location.network") {
         grp->features.add(String8("android.hardware.location"), Feature(true));
+    } else if (name == "android.hardware.faketouch.multitouch") {
+        grp->features.add(String8("android.hardware.faketouch"), Feature(true));
+    } else if (name == "android.hardware.faketouch.multitouch.distinct" ||
+            name == "android.hardware.faketouch.multitouch.jazzhands") {
+        grp->features.add(String8("android.hardware.faketouch.multitouch"), Feature(true));
+        grp->features.add(String8("android.hardware.faketouch"), Feature(true));
     } else if (name == "android.hardware.touchscreen.multitouch") {
         grp->features.add(String8("android.hardware.touchscreen"), Feature(true));
-    } else if (name == "android.hardware.touchscreen.multitouch.distinct") {
+    } else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
+            name == "android.hardware.touchscreen.multitouch.jazzhands") {
         grp->features.add(String8("android.hardware.touchscreen.multitouch"), Feature(true));
         grp->features.add(String8("android.hardware.touchscreen"), Feature(true));
     } else if (name == "android.hardware.opengles.aep") {
@@ -634,50 +651,58 @@
                                             bool impliedBySdk23Permission) {
     if (name == "android.permission.CAMERA") {
         addImpliedFeature(impliedFeatures, "android.hardware.camera",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
+                          String8::format("requested %s permission", name.string()),
+                          impliedBySdk23Permission);
     } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
-        addImpliedFeature(impliedFeatures, "android.hardware.location.gps",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
+        if (targetSdk < SDK_LOLLIPOP) {
+            addImpliedFeature(impliedFeatures, "android.hardware.location.gps",
+                              String8::format("requested %s permission", name.string()),
+                              impliedBySdk23Permission);
+            addImpliedFeature(impliedFeatures, "android.hardware.location.gps",
+                              String8::format("targetSdkVersion < %d", SDK_LOLLIPOP),
+                              impliedBySdk23Permission);
+        }
         addImpliedFeature(impliedFeatures, "android.hardware.location",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
-    } else if (name == "android.permission.ACCESS_MOCK_LOCATION") {
-        addImpliedFeature(impliedFeatures, "android.hardware.location",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
+                String8::format("requested %s permission", name.string()),
+                impliedBySdk23Permission);
     } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
-        addImpliedFeature(impliedFeatures, "android.hardware.location.network",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
+        if (targetSdk < SDK_LOLLIPOP) {
+            addImpliedFeature(impliedFeatures, "android.hardware.location.network",
+                              String8::format("requested %s permission", name.string()),
+                              impliedBySdk23Permission);
+            addImpliedFeature(impliedFeatures, "android.hardware.location.network",
+                              String8::format("targetSdkVersion < %d", SDK_LOLLIPOP),
+                              impliedBySdk23Permission);
+        }
         addImpliedFeature(impliedFeatures, "android.hardware.location",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
-    } else if (name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
+                          String8::format("requested %s permission", name.string()),
+                          impliedBySdk23Permission);
+    } else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
+               name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
                name == "android.permission.INSTALL_LOCATION_PROVIDER") {
         addImpliedFeature(impliedFeatures, "android.hardware.location",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
+                          String8::format("requested %s permission", name.string()),
+                          impliedBySdk23Permission);
     } else if (name == "android.permission.BLUETOOTH" ||
                name == "android.permission.BLUETOOTH_ADMIN") {
-        if (targetSdk > 4) {
+        if (targetSdk > SDK_DONUT) {
             addImpliedFeature(impliedFeatures, "android.hardware.bluetooth",
-                    String8::format("requested %s permission", name.string())
-                    .string(), impliedBySdk23Permission);
+                              String8::format("requested %s permission", name.string()),
+                              impliedBySdk23Permission);
             addImpliedFeature(impliedFeatures, "android.hardware.bluetooth",
-                    "targetSdkVersion > 4", impliedBySdk23Permission);
+                              String8::format("targetSdkVersion > %d", SDK_DONUT),
+                              impliedBySdk23Permission);
         }
     } else if (name == "android.permission.RECORD_AUDIO") {
         addImpliedFeature(impliedFeatures, "android.hardware.microphone",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
+                          String8::format("requested %s permission", name.string()),
+                          impliedBySdk23Permission);
     } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
                name == "android.permission.CHANGE_WIFI_STATE" ||
                name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
         addImpliedFeature(impliedFeatures, "android.hardware.wifi",
-                String8::format("requested %s permission", name.string())
-                .string(), impliedBySdk23Permission);
+                          String8::format("requested %s permission", name.string()),
+                          impliedBySdk23Permission);
     } else if (name == "android.permission.CALL_PHONE" ||
                name == "android.permission.CALL_PRIVILEGED" ||
                name == "android.permission.MODIFY_PHONE_STATE" ||
@@ -690,8 +715,8 @@
                name == "android.permission.WRITE_APN_SETTINGS" ||
                name == "android.permission.WRITE_SMS") {
         addImpliedFeature(impliedFeatures, "android.hardware.telephony",
-                String8("requested a telephony permission").string(),
-                impliedBySdk23Permission);
+                          String8("requested a telephony permission"),
+                          impliedBySdk23Permission);
     }
 }
 
@@ -1642,18 +1667,18 @@
                             if (error == "") {
                                 if (orien == 0 || orien == 6 || orien == 8) {
                                     // Requests landscape, sensorLandscape, or reverseLandscape.
-                                    addImpliedFeature(&impliedFeatures,
-                                                      "android.hardware.screen.landscape",
-                                                      "one or more activities have specified a "
-                                                      "landscape orientation",
-                                                      false);
+                                    addImpliedFeature(
+                                            &impliedFeatures, "android.hardware.screen.landscape",
+                                            String8("one or more activities have specified a "
+                                                    "landscape orientation"),
+                                            false);
                                 } else if (orien == 1 || orien == 7 || orien == 9) {
                                     // Requests portrait, sensorPortrait, or reversePortrait.
-                                    addImpliedFeature(&impliedFeatures,
-                                                      "android.hardware.screen.portrait",
-                                                      "one or more activities have specified a "
-                                                      "portrait orientation",
-                                                      false);
+                                    addImpliedFeature(
+                                            &impliedFeatures, "android.hardware.screen.portrait",
+                                            String8("one or more activities have specified a "
+                                                    "portrait orientation"),
+                                            false);
                                 }
                             }
                         } else if (tag == "uses-library") {
@@ -2005,8 +2030,12 @@
                 }
             }
 
-            addImpliedFeature(&impliedFeatures, "android.hardware.touchscreen",
-                    "default feature for all apps", false);
+            // If the app hasn't declared the touchscreen as a feature requirement (either
+            // directly or implied, required or not), then the faketouch feature is implied.
+            if (!hasFeature("android.hardware.touchscreen", commonFeatures, impliedFeatures)) {
+                addImpliedFeature(&impliedFeatures, "android.hardware.faketouch",
+                                  String8("default feature for all apps"), false);
+            }
 
             const size_t numFeatureGroups = featureGroups.size();
             if (numFeatureGroups == 0) {
diff --git a/tools/aapt/Images.cpp b/tools/aapt/Images.cpp
index 9939c18..aea16c7 100644
--- a/tools/aapt/Images.cpp
+++ b/tools/aapt/Images.cpp
@@ -808,13 +808,13 @@
     assert(outPatch->paddingTop == inPatch->paddingTop);
     assert(outPatch->paddingBottom == inPatch->paddingBottom);
     for (int i = 0; i < outPatch->numXDivs; i++) {
-        assert(outPatch->xDivs[i] == inPatch->xDivs[i]);
+        assert(outPatch->getXDivs()[i] == inPatch->getXDivs()[i]);
     }
     for (int i = 0; i < outPatch->numYDivs; i++) {
-        assert(outPatch->yDivs[i] == inPatch->yDivs[i]);
+        assert(outPatch->getYDivs()[i] == inPatch->getYDivs()[i]);
     }
     for (int i = 0; i < outPatch->numColors; i++) {
-        assert(outPatch->colors[i] == inPatch->colors[i]);
+        assert(outPatch->getColors()[i] == inPatch->getColors()[i]);
     }
     free(newData);
 }
diff --git a/tools/aapt/Resource.cpp b/tools/aapt/Resource.cpp
index cf7f0e8..3e8abb1 100644
--- a/tools/aapt/Resource.cpp
+++ b/tools/aapt/Resource.cpp
@@ -322,13 +322,18 @@
         }
         String8 resPath = it.getPath();
         resPath.convertToResPath();
-        table->addEntry(SourcePos(it.getPath(), 0), String16(assets->getPackage()),
+        status_t result = table->addEntry(SourcePos(it.getPath(), 0),
+                        String16(assets->getPackage()),
                         type16,
                         baseName,
                         String16(resPath),
                         NULL,
                         &it.getParams());
-        assets->addResource(it.getLeafName(), resPath, it.getFile(), type8);
+        if (result != NO_ERROR) {
+            hasErrors = true;
+        } else {
+            assets->addResource(it.getLeafName(), resPath, it.getFile(), type8);
+        }
     }
 
     return hasErrors ? STATUST(UNKNOWN_ERROR) : NO_ERROR;
@@ -1024,7 +1029,6 @@
         return NO_ERROR;
     }
 
-    ResXMLTree tree;
     Asset* asset = assets.openNonAsset(cookie, "AndroidManifest.xml", Asset::ACCESS_STREAMING);
     if (asset == NULL) {
         fprintf(stderr, "ERROR: Platform AndroidManifest.xml not found\n");
@@ -1032,11 +1036,17 @@
     }
 
     ssize_t result = NO_ERROR;
-    if (tree.setTo(asset->getBuffer(true), asset->getLength()) != NO_ERROR) {
-        fprintf(stderr, "ERROR: Platform AndroidManifest.xml is corrupt\n");
-        result = UNKNOWN_ERROR;
-    } else {
-        result = extractPlatformBuildVersion(tree, bundle);
+
+    // Create a new scope so that ResXMLTree is destroyed before we delete the memory over
+    // which it iterates (asset).
+    {
+        ResXMLTree tree;
+        if (tree.setTo(asset->getBuffer(true), asset->getLength()) != NO_ERROR) {
+            fprintf(stderr, "ERROR: Platform AndroidManifest.xml is corrupt\n");
+            result = UNKNOWN_ERROR;
+        } else {
+            result = extractPlatformBuildVersion(tree, bundle);
+        }
     }
 
     delete asset;
@@ -1366,6 +1376,10 @@
         }
     }
 
+    if (hasErrors) {
+        return UNKNOWN_ERROR;
+    }
+
     // --------------------------------------------------------------------
     // Assignment of resource IDs and initial generation of resource table.
     // --------------------------------------------------------------------
diff --git a/tools/aapt/ResourceFilter.cpp b/tools/aapt/ResourceFilter.cpp
index 8693999..ed06f60 100644
--- a/tools/aapt/ResourceFilter.cpp
+++ b/tools/aapt/ResourceFilter.cpp
@@ -54,6 +54,46 @@
     return NO_ERROR;
 }
 
+// Returns true if the locale script of the config should be considered matching
+// the locale script of entry.
+//
+// If both the scripts are empty, the scripts are considered matching for
+// backward compatibility reasons.
+//
+// If only one script is empty, we try to compute it based on the provided
+// language and country. If we could not compute it, we assume it's either a
+// new language we don't know about, or a private use language. We return true
+// since we don't know any better and they might as well be a match.
+//
+// Finally, when we have two scripts (one of which could be computed), we return
+// true if and only if they are an exact match.
+inline bool
+scriptsMatch(const ResTable_config& config, const ResTable_config& entry) {
+    const char* configScript = config.localeScript;
+    const char* entryScript = entry.localeScript;
+    if (configScript[0] == '\0' && entryScript[0] == '\0') {
+        return true;  // both scripts are empty. We match for backward compatibility reasons.
+    }
+
+    char scriptBuffer[sizeof(config.localeScript)];
+    if (configScript[0] == '\0') {
+        localeDataComputeScript(scriptBuffer, config.language, config.country);
+        if (scriptBuffer[0] == '\0') {  // We can't compute the script, so we match.
+            return true;
+        }
+        configScript = scriptBuffer;
+    } else if (entryScript[0] == '\0') {
+        localeDataComputeScript(
+                scriptBuffer, entry.language, entry.country);
+        if (scriptBuffer[0] == '\0') {  // We can't compute the script, so we match.
+            return true;
+        }
+        entryScript = scriptBuffer;
+    }
+    return (memcmp(configScript, entryScript, sizeof(config.localeScript)) == 0);
+}
+
+
 bool
 WeakResourceFilter::match(const ResTable_config& config) const
 {
@@ -75,11 +115,19 @@
             // If the locales differ, but the languages are the same and
             // the locale we are matching only has a language specified,
             // we match.
-            if (config.language[0] &&
-                    memcmp(config.language, entry.first.language, sizeof(config.language)) == 0) {
-                if (config.country[0] == 0) {
-                    matchedAxis |= ResTable_config::CONFIG_LOCALE;
-                }
+            //
+            // Exception: we won't match if a script is specified for at least
+            // one of the locales and it's different from the other locale's
+            // script. (We will compute the other script if at least one of the
+            // scripts were explicitly set. In cases we can't compute an script,
+            // we match.)
+            if (config.language[0] != '\0' &&
+                    config.country[0] == '\0' &&
+                    config.localeVariant[0] == '\0' &&
+                    config.language[0] == entry.first.language[0] &&
+                    config.language[1] == entry.first.language[1] &&
+                    scriptsMatch(config, entry.first)) {
+                matchedAxis |= ResTable_config::CONFIG_LOCALE;
             }
         } else if ((diff & entry.second) == ResTable_config::CONFIG_SMALLEST_SCREEN_SIZE) {
             // Special case if the smallest screen width doesn't match. We check that the
diff --git a/tools/aapt/ResourceTable.cpp b/tools/aapt/ResourceTable.cpp
index 8b07db6d..661409e 100644
--- a/tools/aapt/ResourceTable.cpp
+++ b/tools/aapt/ResourceTable.cpp
@@ -2623,6 +2623,14 @@
         const SourcePos unknown(String8("????"), 0);
         sp<Type> attr = p->getType(String16("attr"), unknown);
 
+        // Force creation of ID if we are building feature splits.
+        // Auto-generated ID resources won't apply the type ID offset correctly unless
+        // the offset is applied here first.
+        // b/30607637
+        if (mPackageType == AppFeature && p->getName() == mAssetsPackage) {
+            sp<Type> id = p->getType(String16("id"), unknown);
+        }
+
         // Assign indices...
         const size_t typeCount = p->getOrderedTypes().size();
         for (size_t ti = 0; ti < typeCount; ti++) {
@@ -4521,6 +4529,7 @@
         const ConfigDescription& sourceConfig,
         const int sdkVersionToGenerate) {
     assert(sdkVersionToGenerate > sourceConfig.sdkVersion);
+    assert(configList != NULL);
     const DefaultKeyedVector<ConfigDescription, sp<ResourceTable::Entry>>& entries
             = configList->getEntries();
     ssize_t idx = entries.indexOfKey(sourceConfig);
@@ -4851,24 +4860,39 @@
         const Vector<sp<Type> >& types = mOrderedPackages[p]->getOrderedTypes();
         const size_t typeCount = types.size();
         for (size_t t = 0; t < typeCount; t++) {
-            const Vector<sp<ConfigList> >& configs = types[t]->getOrderedConfigs();
+            const sp<Type>& type = types[t];
+            if (type == NULL) {
+                continue;
+            }
+
+            const Vector<sp<ConfigList> >& configs = type->getOrderedConfigs();
             const size_t configCount = configs.size();
             for (size_t c = 0; c < configCount; c++) {
+                const sp<ConfigList>& configList = configs[c];
+                if (configList == NULL) {
+                    continue;
+                }
+
                 const DefaultKeyedVector<ConfigDescription, sp<Entry> >& configEntries
-                        = configs[c]->getEntries();
+                        = configList->getEntries();
                 const size_t configEntryCount = configEntries.size();
                 for (size_t ce = 0; ce < configEntryCount; ce++) {
+                    const sp<Entry>& entry = configEntries.valueAt(ce);
+                    if (entry == NULL) {
+                        continue;
+                    }
+
                     const ConfigDescription& config = configEntries.keyAt(ce);
                     if (AaptConfig::isDensityOnly(config)) {
                         // This configuration only varies with regards to density.
                         const Symbol symbol(
                                 mOrderedPackages[p]->getName(),
-                                types[t]->getName(),
-                                configs[c]->getName(),
+                                type->getName(),
+                                configList->getName(),
                                 getResId(mOrderedPackages[p], types[t],
-                                         configs[c]->getEntryIndex()));
+                                         configList->getEntryIndex()));
 
-                        const sp<Entry>& entry = configEntries.valueAt(ce);
+
                         AaptUtil::appendValue(resources, symbol,
                                               SymbolDefinition(symbol, config, entry->getPos()));
                     }
diff --git a/tools/aapt2/Android.mk b/tools/aapt2/Android.mk
index d86de1e..53225da 100644
--- a/tools/aapt2/Android.mk
+++ b/tools/aapt2/Android.mk
@@ -115,6 +115,7 @@
 
 toolSources := \
 	compile/Compile.cpp \
+	diff/Diff.cpp \
 	dump/Dump.cpp \
 	link/Link.cpp
 
diff --git a/tools/aapt2/Main.cpp b/tools/aapt2/Main.cpp
index a2fadd9..00d8aae 100644
--- a/tools/aapt2/Main.cpp
+++ b/tools/aapt2/Main.cpp
@@ -24,6 +24,7 @@
 extern int compile(const std::vector<StringPiece>& args);
 extern int link(const std::vector<StringPiece>& args);
 extern int dump(const std::vector<StringPiece>& args);
+extern int diff(const std::vector<StringPiece>& args);
 
 } // namespace aapt
 
@@ -44,12 +45,14 @@
             return aapt::link(args);
         } else if (command == "dump" || command == "d") {
             return aapt::dump(args);
+        } else if (command == "diff") {
+            return aapt::diff(args);
         }
         std::cerr << "unknown command '" << command << "'\n";
     } else {
         std::cerr << "no command specified\n";
     }
 
-    std::cerr << "\nusage: aapt2 [compile|link|dump] ..." << std::endl;
+    std::cerr << "\nusage: aapt2 [compile|link|dump|diff] ..." << std::endl;
     return 1;
 }
diff --git a/tools/aapt2/ResourceParser.cpp b/tools/aapt2/ResourceParser.cpp
index 9704d970..a84c306 100644
--- a/tools/aapt2/ResourceParser.cpp
+++ b/tools/aapt2/ResourceParser.cpp
@@ -152,7 +152,7 @@
                 break;
             }
 
-            spanStack.back().lastChar = builder.str().size();
+            spanStack.back().lastChar = builder.str().size() - 1;
             outStyleString->spans.push_back(spanStack.back());
             spanStack.pop_back();
 
@@ -1058,6 +1058,16 @@
 
     std::unique_ptr<Array> array = util::make_unique<Array>();
 
+    bool translateable = mOptions.translatable;
+    if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) {
+        if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) {
+            mDiag->error(DiagMessage(outResource->source)
+                         << "invalid value for 'translatable'. Must be a boolean");
+            return false;
+        }
+    }
+    array->setTranslateable(translateable);
+
     bool error = false;
     const size_t depth = parser->getDepth();
     while (xml::XmlPullParser::nextChildNode(parser, depth)) {
diff --git a/tools/aapt2/ResourceTable.cpp b/tools/aapt2/ResourceTable.cpp
index 9d6476a..f9707e4 100644
--- a/tools/aapt2/ResourceTable.cpp
+++ b/tools/aapt2/ResourceTable.cpp
@@ -189,6 +189,17 @@
     return results;
 }
 
+std::vector<ResourceConfigValue*> ResourceEntry::findValuesIf(
+        const std::function<bool(ResourceConfigValue*)>& f) {
+    std::vector<ResourceConfigValue*> results;
+    for (auto& configValue : values) {
+        if (f(configValue.get())) {
+            results.push_back(configValue.get());
+        }
+    }
+    return results;
+}
+
 /**
  * The default handler for collisions. A return value of -1 means keep the
  * existing value, 0 means fail, and +1 means take the incoming value.
diff --git a/tools/aapt2/ResourceTable.h b/tools/aapt2/ResourceTable.h
index 155bd1c..cf9e7b8 100644
--- a/tools/aapt2/ResourceTable.h
+++ b/tools/aapt2/ResourceTable.h
@@ -26,6 +26,7 @@
 #include "io/File.h"
 
 #include <android-base/macros.h>
+#include <functional>
 #include <map>
 #include <memory>
 #include <string>
@@ -109,6 +110,9 @@
     ResourceConfigValue* findOrCreateValue(const ConfigDescription& config,
                                            const StringPiece& product);
     std::vector<ResourceConfigValue*> findAllValues(const ConfigDescription& config);
+    std::vector<ResourceConfigValue*> findValuesIf(
+            const std::function<bool(ResourceConfigValue*)>& f);
+
 
 private:
     DISALLOW_COPY_AND_ASSIGN(ResourceEntry);
diff --git a/tools/aapt2/ResourceUtils.cpp b/tools/aapt2/ResourceUtils.cpp
index 74c48b0..a0a7efc 100644
--- a/tools/aapt2/ResourceUtils.cpp
+++ b/tools/aapt2/ResourceUtils.cpp
@@ -289,7 +289,7 @@
 std::unique_ptr<BinaryPrimitive> tryParseFlagSymbol(const Attribute* flagAttr,
                                                     const StringPiece16& str) {
     android::Res_value flags = { };
-    flags.dataType = android::Res_value::TYPE_INT_DEC;
+    flags.dataType = android::Res_value::TYPE_INT_HEX;
     flags.data = 0u;
 
     if (util::trimWhitespace(str).empty()) {
diff --git a/tools/aapt2/ResourceValues.cpp b/tools/aapt2/ResourceValues.cpp
index dd7ff01..c10b134 100644
--- a/tools/aapt2/ResourceValues.cpp
+++ b/tools/aapt2/ResourceValues.cpp
@@ -39,6 +39,14 @@
 RawString::RawString(const StringPool::Ref& ref) : value(ref) {
 }
 
+bool RawString::equals(const Value* value) const {
+    const RawString* other = valueCast<RawString>(value);
+    if (!other) {
+        return false;
+    }
+    return *this->value == *other->value;
+}
+
 RawString* RawString::clone(StringPool* newPool) const {
     RawString* rs = new RawString(newPool->makeRef(*value));
     rs->mComment = mComment;
@@ -66,6 +74,15 @@
 Reference::Reference(const ResourceId& i, Type type) : id(i), referenceType(type) {
 }
 
+bool Reference::equals(const Value* value) const {
+    const Reference* other = valueCast<Reference>(value);
+    if (!other) {
+        return false;
+    }
+    return referenceType == other->referenceType && privateReference == other->privateReference &&
+            id == other->id && name == other->name;
+}
+
 bool Reference::flatten(android::Res_value* outValue) const {
     outValue->dataType = (referenceType == Reference::Type::kResource) ?
             android::Res_value::TYPE_REFERENCE : android::Res_value::TYPE_ATTRIBUTE;
@@ -97,6 +114,10 @@
     }
 }
 
+bool Id::equals(const Value* value) const {
+    return valueCast<Id>(value) != nullptr;
+}
+
 bool Id::flatten(android::Res_value* out) const {
     out->dataType = android::Res_value::TYPE_INT_BOOLEAN;
     out->data = util::hostToDevice32(0);
@@ -111,15 +132,15 @@
     *out << "(id)";
 }
 
-String::String(const StringPool::Ref& ref) : value(ref), mTranslateable(true) {
+String::String(const StringPool::Ref& ref) : value(ref) {
 }
 
-void String::setTranslateable(bool val) {
-    mTranslateable = val;
-}
-
-bool String::isTranslateable() const {
-    return mTranslateable;
+bool String::equals(const Value* value) const {
+    const String* other = valueCast<String>(value);
+    if (!other) {
+        return false;
+    }
+    return *this->value == *other->value;
 }
 
 bool String::flatten(android::Res_value* outValue) const {
@@ -144,15 +165,24 @@
     *out << "(string) \"" << *value << "\"";
 }
 
-StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref), mTranslateable(true) {
+StyledString::StyledString(const StringPool::StyleRef& ref) : value(ref) {
 }
 
-void StyledString::setTranslateable(bool val) {
-    mTranslateable = val;
-}
+bool StyledString::equals(const Value* value) const {
+    const StyledString* other = valueCast<StyledString>(value);
+    if (!other) {
+        return false;
+    }
 
-bool StyledString::isTranslateable() const {
-    return mTranslateable;
+    if (*this->value->str == *other->value->str) {
+        const std::vector<StringPool::Span>& spansA = this->value->spans;
+        const std::vector<StringPool::Span>& spansB = other->value->spans;
+        return std::equal(spansA.begin(), spansA.end(), spansB.begin(),
+                          [](const StringPool::Span& a, const StringPool::Span& b) -> bool {
+            return *a.name == *b.name && a.firstChar == b.firstChar && a.lastChar == b.lastChar;
+        });
+    }
+    return false;
 }
 
 bool StyledString::flatten(android::Res_value* outValue) const {
@@ -174,11 +204,22 @@
 
 void StyledString::print(std::ostream* out) const {
     *out << "(styled string) \"" << *value->str << "\"";
+    for (const StringPool::Span& span : value->spans) {
+        *out << " "<< *span.name << ":" << span.firstChar << "," << span.lastChar;
+    }
 }
 
 FileReference::FileReference(const StringPool::Ref& _path) : path(_path) {
 }
 
+bool FileReference::equals(const Value* value) const {
+    const FileReference* other = valueCast<FileReference>(value);
+    if (!other) {
+        return false;
+    }
+    return *path == *other->path;
+}
+
 bool FileReference::flatten(android::Res_value* outValue) const {
     if (path.getIndex() > std::numeric_limits<uint32_t>::max()) {
         return false;
@@ -209,6 +250,14 @@
     value.data = data;
 }
 
+bool BinaryPrimitive::equals(const Value* value) const {
+    const BinaryPrimitive* other = valueCast<BinaryPrimitive>(value);
+    if (!other) {
+        return false;
+    }
+    return this->value.dataType == other->value.dataType && this->value.data == other->value.data;
+}
+
 bool BinaryPrimitive::flatten(android::Res_value* outValue) const {
     outValue->dataType = value.dataType;
     outValue->data = util::hostToDevice32(value.data);
@@ -228,7 +277,7 @@
             *out << "(integer) " << static_cast<int32_t>(value.data);
             break;
         case android::Res_value::TYPE_INT_HEX:
-            *out << "(integer) " << std::hex << value.data << std::dec;
+            *out << "(integer) 0x" << std::hex << value.data << std::dec;
             break;
         case android::Res_value::TYPE_INT_BOOLEAN:
             *out << "(boolean) " << (value.data != 0 ? "true" : "false");
@@ -253,6 +302,21 @@
     mWeak = w;
 }
 
+bool Attribute::equals(const Value* value) const {
+    const Attribute* other = valueCast<Attribute>(value);
+    if (!other) {
+        return false;
+    }
+
+    return this->typeMask == other->typeMask && this->minInt == other->minInt &&
+            this->maxInt == other->maxInt &&
+            std::equal(this->symbols.begin(), this->symbols.end(),
+                       other->symbols.begin(),
+                       [](const Symbol& a, const Symbol& b) -> bool {
+        return a.symbol.equals(&b.symbol) && a.value == b.value;
+    });
+}
+
 Attribute* Attribute::clone(StringPool* /*newPool*/) const {
     return new Attribute(*this);
 }
@@ -365,6 +429,14 @@
             << "]";
     }
 
+    if (minInt != std::numeric_limits<int32_t>::min()) {
+        *out << " min=" << minInt;
+    }
+
+    if (maxInt != std::numeric_limits<int32_t>::max()) {
+        *out << " max=" << maxInt;
+    }
+
     if (isWeak()) {
         *out << " [weak]";
     }
@@ -445,6 +517,21 @@
     return true;
 }
 
+bool Style::equals(const Value* value) const {
+    const Style* other = valueCast<Style>(value);
+    if (!other) {
+        return false;
+    }
+    if (bool(parent) != bool(other->parent) ||
+            (parent && other->parent && !parent.value().equals(&other->parent.value()))) {
+        return false;
+    }
+    return std::equal(entries.begin(), entries.end(), other->entries.begin(),
+                      [](const Entry& a, const Entry& b) -> bool {
+        return a.key.equals(&b.key) && a.value->equals(b.value.get());
+    });
+}
+
 Style* Style::clone(StringPool* newPool) const {
     Style* style = new Style();
     style->parent = parent;
@@ -484,6 +571,18 @@
     return out;
 }
 
+bool Array::equals(const Value* value) const {
+    const Array* other = valueCast<Array>(value);
+    if (!other) {
+        return false;
+    }
+
+    return std::equal(items.begin(), items.end(), other->items.begin(),
+                      [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
+        return a->equals(b.get());
+    });
+}
+
 Array* Array::clone(StringPool* newPool) const {
     Array* array = new Array();
     array->mComment = mComment;
@@ -500,6 +599,21 @@
         << "]";
 }
 
+bool Plural::equals(const Value* value) const {
+    const Plural* other = valueCast<Plural>(value);
+    if (!other) {
+        return false;
+    }
+
+    return std::equal(values.begin(), values.end(), other->values.begin(),
+                      [](const std::unique_ptr<Item>& a, const std::unique_ptr<Item>& b) -> bool {
+        if (bool(a) != bool(b)) {
+            return false;
+        }
+        return bool(a) == bool(b) || a->equals(b.get());
+    });
+}
+
 Plural* Plural::clone(StringPool* newPool) const {
     Plural* p = new Plural();
     p->mComment = mComment;
@@ -515,12 +629,42 @@
 
 void Plural::print(std::ostream* out) const {
     *out << "(plural)";
+    if (values[Zero]) {
+        *out << " zero=" << *values[Zero];
+    }
+
+    if (values[One]) {
+        *out << " one=" << *values[One];
+    }
+
+    if (values[Two]) {
+        *out << " two=" << *values[Two];
+    }
+
+    if (values[Few]) {
+        *out << " few=" << *values[Few];
+    }
+
+    if (values[Many]) {
+        *out << " many=" << *values[Many];
+    }
 }
 
 static ::std::ostream& operator<<(::std::ostream& out, const std::unique_ptr<Item>& item) {
     return out << *item;
 }
 
+bool Styleable::equals(const Value* value) const {
+    const Styleable* other = valueCast<Styleable>(value);
+    if (!other) {
+        return false;
+    }
+    return std::equal(entries.begin(), entries.end(), other->entries.begin(),
+                      [](const Reference& a, const Reference& b) -> bool {
+        return a.equals(&b);
+    });
+}
+
 Styleable* Styleable::clone(StringPool* /*newPool*/) const {
     return new Styleable(*this);
 }
diff --git a/tools/aapt2/ResourceValues.h b/tools/aapt2/ResourceValues.h
index 449ab29..9a6f1a1 100644
--- a/tools/aapt2/ResourceValues.h
+++ b/tools/aapt2/ResourceValues.h
@@ -54,6 +54,18 @@
         mWeak = val;
     }
 
+    // Whether the value is marked as translateable.
+    // This does not persist when flattened.
+    // It is only used during compilation phase.
+    void setTranslateable(bool val) {
+        mTranslateable = val;
+    }
+
+    // Default true.
+    bool isTranslateable() const {
+        return mTranslateable;
+    }
+
     /**
      * Returns the source where this value was defined.
      */
@@ -84,6 +96,8 @@
         mComment = std::move(str);
     }
 
+    virtual bool equals(const Value* value) const = 0;
+
     /**
      * Calls the appropriate overload of ValueVisitor.
      */
@@ -103,6 +117,7 @@
     Source mSource;
     std::u16string mComment;
     bool mWeak = false;
+    bool mTranslateable = true;
 };
 
 /**
@@ -158,6 +173,7 @@
     explicit Reference(const ResourceNameRef& n, Type type = Type::kResource);
     explicit Reference(const ResourceId& i, Type type = Type::kResource);
 
+    bool equals(const Value* value) const override;
     bool flatten(android::Res_value* outValue) const override;
     Reference* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
@@ -168,6 +184,7 @@
  */
 struct Id : public BaseItem<Id> {
     Id() { mWeak = true; }
+    bool equals(const Value* value) const override;
     bool flatten(android::Res_value* out) const override;
     Id* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
@@ -183,6 +200,7 @@
 
     explicit RawString(const StringPool::Ref& ref);
 
+    bool equals(const Value* value) const override;
     bool flatten(android::Res_value* outValue) const override;
     RawString* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
@@ -193,17 +211,10 @@
 
     explicit String(const StringPool::Ref& ref);
 
-    // Whether the string is marked as translateable. This does not persist when flattened.
-    // It is only used during compilation phase.
-    void setTranslateable(bool val);
-    bool isTranslateable() const;
-
+    bool equals(const Value* value) const override;
     bool flatten(android::Res_value* outValue) const override;
     String* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
-
-private:
-    bool mTranslateable;
 };
 
 struct StyledString : public BaseItem<StyledString> {
@@ -211,17 +222,10 @@
 
     explicit StyledString(const StringPool::StyleRef& ref);
 
-    // Whether the string is marked as translateable. This does not persist when flattened.
-    // It is only used during compilation phase.
-    void setTranslateable(bool val);
-    bool isTranslateable() const;
-
+    bool equals(const Value* value) const override;
     bool flatten(android::Res_value* outValue) const override;
     StyledString* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
-
-private:
-    bool mTranslateable;
 };
 
 struct FileReference : public BaseItem<FileReference> {
@@ -235,6 +239,7 @@
     FileReference() = default;
     explicit FileReference(const StringPool::Ref& path);
 
+    bool equals(const Value* value) const override;
     bool flatten(android::Res_value* outValue) const override;
     FileReference* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
@@ -250,6 +255,7 @@
     explicit BinaryPrimitive(const android::Res_value& val);
     BinaryPrimitive(uint8_t dataType, uint32_t data);
 
+    bool equals(const Value* value) const override;
     bool flatten(android::Res_value* outValue) const override;
     BinaryPrimitive* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
@@ -268,6 +274,7 @@
 
     explicit Attribute(bool w, uint32_t t = 0u);
 
+    bool equals(const Value* value) const override;
     Attribute* clone(StringPool* newPool) const override;
     void printMask(std::ostream* out) const;
     void print(std::ostream* out) const override;
@@ -290,6 +297,7 @@
 
     std::vector<Entry> entries;
 
+    bool equals(const Value* value) const override;
     Style* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
 };
@@ -297,6 +305,7 @@
 struct Array : public BaseValue<Array> {
     std::vector<std::unique_ptr<Item>> items;
 
+    bool equals(const Value* value) const override;
     Array* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
 };
@@ -314,6 +323,7 @@
 
     std::array<std::unique_ptr<Item>, Count> values;
 
+    bool equals(const Value* value) const override;
     Plural* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
 };
@@ -321,6 +331,7 @@
 struct Styleable : public BaseValue<Styleable> {
     std::vector<Reference> entries;
 
+    bool equals(const Value* value) const override;
     Styleable* clone(StringPool* newPool) const override;
     void print(std::ostream* out) const override;
 };
diff --git a/tools/aapt2/ValueVisitor.h b/tools/aapt2/ValueVisitor.h
index ea2aa55..b8bc5db 100644
--- a/tools/aapt2/ValueVisitor.h
+++ b/tools/aapt2/ValueVisitor.h
@@ -127,6 +127,11 @@
     }
 };
 
+template <typename T>
+const T* valueCast(const Value* value) {
+    return valueCast<T>(const_cast<Value*>(value));
+}
+
 /**
  * Returns a valid pointer to T if the Value is of subtype T.
  * Otherwise, returns nullptr.
@@ -141,7 +146,6 @@
     return visitor.value;
 }
 
-
 inline void visitAllValuesInPackage(ResourceTablePackage* pkg, RawValueVisitor* visitor) {
     for (auto& type : pkg->types) {
         for (auto& entry : type->entries) {
diff --git a/tools/aapt2/compile/PseudolocaleGenerator.cpp b/tools/aapt2/compile/PseudolocaleGenerator.cpp
index 99c2077..d080e16 100644
--- a/tools/aapt2/compile/PseudolocaleGenerator.cpp
+++ b/tools/aapt2/compile/PseudolocaleGenerator.cpp
@@ -128,23 +128,6 @@
             mPool(pool), mMethod(method), mLocalizer(method) {
     }
 
-    void visit(Array* array) override {
-        std::unique_ptr<Array> localized = util::make_unique<Array>();
-        localized->items.resize(array->items.size());
-        for (size_t i = 0; i < array->items.size(); i++) {
-            Visitor subVisitor(mPool, mMethod);
-            array->items[i]->accept(&subVisitor);
-            if (subVisitor.mItem) {
-                localized->items[i] = std::move(subVisitor.mItem);
-            } else {
-                localized->items[i] = std::unique_ptr<Item>(array->items[i]->clone(mPool));
-            }
-        }
-        localized->setSource(array->getSource());
-        localized->setWeak(true);
-        mValue = std::move(localized);
-    }
-
     void visit(Plural* plural) override {
         std::unique_ptr<Plural> localized = util::make_unique<Plural>();
         for (size_t i = 0; i < plural->values.size(); i++) {
@@ -164,10 +147,6 @@
     }
 
     void visit(String* string) override {
-        if (!string->isTranslateable()) {
-            return;
-        }
-
         std::u16string result = mLocalizer.start() + mLocalizer.text(*string->value) +
                 mLocalizer.end();
         std::unique_ptr<String> localized = util::make_unique<String>(mPool->makeRef(result));
@@ -177,10 +156,6 @@
     }
 
     void visit(StyledString* string) override {
-        if (!string->isTranslateable()) {
-            return;
-        }
-
         mItem = pseudolocalizeStyledString(string, mMethod, mPool);
         mItem->setWeak(true);
     }
@@ -238,14 +213,26 @@
     }
 }
 
+/**
+ * A value is pseudolocalizable if it does not define a locale (or is the default locale)
+ * and is translateable.
+ */
+static bool isPseudolocalizable(ResourceConfigValue* configValue) {
+    const int diff = configValue->config.diff(ConfigDescription::defaultConfig());
+    if (diff & ConfigDescription::CONFIG_LOCALE) {
+        return false;
+    }
+    return configValue->value->isTranslateable();
+}
+
 } // namespace
 
 bool PseudolocaleGenerator::consume(IAaptContext* context, ResourceTable* table) {
     for (auto& package : table->packages) {
         for (auto& type : package->types) {
             for (auto& entry : type->entries) {
-                std::vector<ResourceConfigValue*> values = entry->findAllValues(
-                        ConfigDescription::defaultConfig());
+                std::vector<ResourceConfigValue*> values = entry->findValuesIf(isPseudolocalizable);
+
                 for (ResourceConfigValue* value : values) {
                     pseudolocalizeIfNeeded(Pseudolocalizer::Method::kAccent, value,
                                            &table->stringPool, entry.get());
diff --git a/tools/aapt2/diff/Diff.cpp b/tools/aapt2/diff/Diff.cpp
new file mode 100644
index 0000000..20b7b59
--- /dev/null
+++ b/tools/aapt2/diff/Diff.cpp
@@ -0,0 +1,411 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Flags.h"
+#include "ResourceTable.h"
+#include "io/ZipArchive.h"
+#include "process/IResourceTableConsumer.h"
+#include "process/SymbolTable.h"
+#include "unflatten/BinaryResourceParser.h"
+
+#include <android-base/macros.h>
+
+namespace aapt {
+
+class DiffContext : public IAaptContext {
+public:
+    const std::u16string& getCompilationPackage() override {
+        return mEmpty;
+    }
+
+    uint8_t getPackageId() override {
+        return 0x0;
+    }
+
+    IDiagnostics* getDiagnostics() override {
+        return &mDiagnostics;
+    }
+
+    NameMangler* getNameMangler() override {
+        return &mNameMangler;
+    }
+
+    SymbolTable* getExternalSymbols() override {
+        return &mSymbolTable;
+    }
+
+    bool verbose() override {
+        return false;
+    }
+
+private:
+    std::u16string mEmpty;
+    StdErrDiagnostics mDiagnostics;
+    NameMangler mNameMangler = NameMangler(NameManglerPolicy{});
+    SymbolTable mSymbolTable;
+};
+
+class LoadedApk {
+public:
+    LoadedApk(const Source& source, std::unique_ptr<io::IFileCollection> apk,
+              std::unique_ptr<ResourceTable> table) :
+            mSource(source), mApk(std::move(apk)), mTable(std::move(table)) {
+    }
+
+    io::IFileCollection* getFileCollection() {
+        return mApk.get();
+    }
+
+    ResourceTable* getResourceTable() {
+        return mTable.get();
+    }
+
+    const Source& getSource() {
+        return mSource;
+    }
+
+private:
+    Source mSource;
+    std::unique_ptr<io::IFileCollection> mApk;
+    std::unique_ptr<ResourceTable> mTable;
+
+    DISALLOW_COPY_AND_ASSIGN(LoadedApk);
+};
+
+static std::unique_ptr<LoadedApk> loadApkFromPath(IAaptContext* context, const StringPiece& path) {
+    Source source(path);
+    std::string error;
+    std::unique_ptr<io::ZipFileCollection> apk = io::ZipFileCollection::create(path, &error);
+    if (!apk) {
+        context->getDiagnostics()->error(DiagMessage(source) << error);
+        return {};
+    }
+
+    io::IFile* file = apk->findFile("resources.arsc");
+    if (!file) {
+        context->getDiagnostics()->error(DiagMessage(source) << "no resources.arsc found");
+        return {};
+    }
+
+    std::unique_ptr<io::IData> data = file->openAsData();
+    if (!data) {
+        context->getDiagnostics()->error(DiagMessage(source) << "could not open resources.arsc");
+        return {};
+    }
+
+    std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>();
+    BinaryResourceParser parser(context, table.get(), source, data->data(), data->size());
+    if (!parser.parse()) {
+        return {};
+    }
+
+    return util::make_unique<LoadedApk>(source, std::move(apk), std::move(table));
+}
+
+static void emitDiffLine(const Source& source, const StringPiece& message) {
+    std::cerr << source << ": " << message << "\n";
+}
+
+static bool isSymbolVisibilityDifferent(const Symbol& symbolA, const Symbol& symbolB) {
+    return symbolA.state != symbolB.state;
+}
+
+template <typename Id>
+static bool isIdDiff(const Symbol& symbolA, const Maybe<Id>& idA,
+                     const Symbol& symbolB, const Maybe<Id>& idB) {
+    if (symbolA.state == SymbolState::kPublic || symbolB.state == SymbolState::kPublic) {
+        return idA != idB;
+    }
+    return false;
+}
+
+static bool emitResourceConfigValueDiff(IAaptContext* context,
+                                        LoadedApk* apkA,
+                                        ResourceTablePackage* pkgA,
+                                        ResourceTableType* typeA,
+                                        ResourceEntry* entryA,
+                                        ResourceConfigValue* configValueA,
+                                        LoadedApk* apkB,
+                                        ResourceTablePackage* pkgB,
+                                        ResourceTableType* typeB,
+                                        ResourceEntry* entryB,
+                                        ResourceConfigValue* configValueB) {
+    Value* valueA = configValueA->value.get();
+    Value* valueB = configValueB->value.get();
+    if (!valueA->equals(valueB)) {
+        std::stringstream strStream;
+        strStream << "value " << pkgA->name << ":" << typeA->type << "/" << entryA->name
+                << " config=" << configValueA->config << " does not match:\n";
+        valueA->print(&strStream);
+        strStream << "\n vs \n";
+        valueB->print(&strStream);
+        emitDiffLine(apkB->getSource(), strStream.str());
+        return true;
+    }
+    return false;
+}
+
+static bool emitResourceEntryDiff(IAaptContext* context,
+                                  LoadedApk* apkA,
+                                  ResourceTablePackage* pkgA,
+                                  ResourceTableType* typeA,
+                                  ResourceEntry* entryA,
+                                  LoadedApk* apkB,
+                                  ResourceTablePackage* pkgB,
+                                  ResourceTableType* typeB,
+                                  ResourceEntry* entryB) {
+    bool diff = false;
+    for (std::unique_ptr<ResourceConfigValue>& configValueA : entryA->values) {
+        ResourceConfigValue* configValueB = entryB->findValue(configValueA->config);
+        if (!configValueB) {
+            std::stringstream strStream;
+            strStream << "missing " << pkgA->name << ":" << typeA->type << "/" << entryA->name
+                    << " config=" << configValueA->config;
+            emitDiffLine(apkB->getSource(), strStream.str());
+            diff = true;
+        } else {
+            diff |= emitResourceConfigValueDiff(context, apkA, pkgA, typeA, entryA,
+                                                configValueA.get(), apkB, pkgB, typeB, entryB,
+                                                configValueB);
+        }
+    }
+
+    // Check for any newly added config values.
+    for (std::unique_ptr<ResourceConfigValue>& configValueB : entryB->values) {
+        ResourceConfigValue* configValueA = entryA->findValue(configValueB->config);
+        if (!configValueA) {
+            std::stringstream strStream;
+            strStream << "new config " << pkgB->name << ":" << typeB->type << "/" << entryB->name
+                    << " config=" << configValueB->config;
+            emitDiffLine(apkB->getSource(), strStream.str());
+            diff = true;
+        }
+    }
+    return false;
+}
+
+static bool emitResourceTypeDiff(IAaptContext* context,
+                                 LoadedApk* apkA,
+                                 ResourceTablePackage* pkgA,
+                                 ResourceTableType* typeA,
+                                 LoadedApk* apkB,
+                                 ResourceTablePackage* pkgB,
+                                 ResourceTableType* typeB) {
+    bool diff = false;
+    for (std::unique_ptr<ResourceEntry>& entryA : typeA->entries) {
+        ResourceEntry* entryB = typeB->findEntry(entryA->name);
+        if (!entryB) {
+            std::stringstream strStream;
+            strStream << "missing " << pkgA->name << ":" << typeA->type << "/" << entryA->name;
+            emitDiffLine(apkB->getSource(), strStream.str());
+            diff = true;
+        } else {
+            if (isSymbolVisibilityDifferent(entryA->symbolStatus, entryB->symbolStatus)) {
+                std::stringstream strStream;
+                strStream << pkgA->name << ":" << typeA->type << "/" << entryA->name
+                        << " has different visibility (";
+                if (entryB->symbolStatus.state == SymbolState::kPublic) {
+                    strStream << "PUBLIC";
+                } else {
+                    strStream << "PRIVATE";
+                }
+                strStream << " vs ";
+                if (entryA->symbolStatus.state == SymbolState::kPublic) {
+                    strStream << "PUBLIC";
+                } else {
+                    strStream << "PRIVATE";
+                }
+                strStream << ")";
+                emitDiffLine(apkB->getSource(), strStream.str());
+                diff = true;
+            } else if (isIdDiff(entryA->symbolStatus, entryA->id,
+                                entryB->symbolStatus, entryB->id)) {
+                std::stringstream strStream;
+                strStream << pkgA->name << ":" << typeA->type << "/" << entryA->name
+                        << " has different public ID (";
+                if (entryB->id) {
+                    strStream << "0x" << std::hex << entryB->id.value();
+                } else {
+                    strStream << "none";
+                }
+                strStream << " vs ";
+                if (entryA->id) {
+                    strStream << "0x " << std::hex << entryA->id.value();
+                } else {
+                    strStream << "none";
+                }
+                strStream << ")";
+                emitDiffLine(apkB->getSource(), strStream.str());
+                diff = true;
+            }
+            diff |= emitResourceEntryDiff(context, apkA, pkgA, typeA, entryA.get(),
+                                          apkB, pkgB, typeB, entryB);
+        }
+    }
+
+    // Check for any newly added entries.
+    for (std::unique_ptr<ResourceEntry>& entryB : typeB->entries) {
+        ResourceEntry* entryA = typeA->findEntry(entryB->name);
+        if (!entryA) {
+            std::stringstream strStream;
+            strStream << "new entry " << pkgB->name << ":" << typeB->type << "/" << entryB->name;
+            emitDiffLine(apkB->getSource(), strStream.str());
+            diff = true;
+        }
+    }
+    return diff;
+}
+
+static bool emitResourcePackageDiff(IAaptContext* context, LoadedApk* apkA,
+                                    ResourceTablePackage* pkgA,
+                                    LoadedApk* apkB, ResourceTablePackage* pkgB) {
+    bool diff = false;
+    for (std::unique_ptr<ResourceTableType>& typeA : pkgA->types) {
+        ResourceTableType* typeB = pkgB->findType(typeA->type);
+        if (!typeB) {
+            std::stringstream strStream;
+            strStream << "missing " << pkgA->name << ":" << typeA->type;
+            emitDiffLine(apkA->getSource(), strStream.str());
+            diff = true;
+        } else {
+            if (isSymbolVisibilityDifferent(typeA->symbolStatus, typeB->symbolStatus)) {
+                std::stringstream strStream;
+                strStream << pkgA->name << ":" << typeA->type << " has different visibility (";
+                if (typeB->symbolStatus.state == SymbolState::kPublic) {
+                    strStream << "PUBLIC";
+                } else {
+                    strStream << "PRIVATE";
+                }
+                strStream << " vs ";
+                if (typeA->symbolStatus.state == SymbolState::kPublic) {
+                    strStream << "PUBLIC";
+                } else {
+                    strStream << "PRIVATE";
+                }
+                strStream << ")";
+                emitDiffLine(apkB->getSource(), strStream.str());
+                diff = true;
+            } else if (isIdDiff(typeA->symbolStatus, typeA->id, typeB->symbolStatus, typeB->id)) {
+                std::stringstream strStream;
+                strStream << pkgA->name << ":" << typeA->type << " has different public ID (";
+                if (typeB->id) {
+                    strStream << "0x" << std::hex << typeB->id.value();
+                } else {
+                    strStream << "none";
+                }
+                strStream << " vs ";
+                if (typeA->id) {
+                    strStream << "0x " << std::hex << typeA->id.value();
+                } else {
+                    strStream << "none";
+                }
+                strStream << ")";
+                emitDiffLine(apkB->getSource(), strStream.str());
+                diff = true;
+            }
+            diff |= emitResourceTypeDiff(context, apkA, pkgA, typeA.get(), apkB, pkgB, typeB);
+        }
+    }
+
+    // Check for any newly added types.
+    for (std::unique_ptr<ResourceTableType>& typeB : pkgB->types) {
+        ResourceTableType* typeA = pkgA->findType(typeB->type);
+        if (!typeA) {
+            std::stringstream strStream;
+            strStream << "new type " << pkgB->name << ":" << typeB->type;
+            emitDiffLine(apkB->getSource(), strStream.str());
+            diff = true;
+        }
+    }
+    return diff;
+}
+
+static bool emitResourceTableDiff(IAaptContext* context, LoadedApk* apkA, LoadedApk* apkB) {
+    ResourceTable* tableA = apkA->getResourceTable();
+    ResourceTable* tableB = apkB->getResourceTable();
+
+    bool diff = false;
+    for (std::unique_ptr<ResourceTablePackage>& pkgA : tableA->packages) {
+        ResourceTablePackage* pkgB = tableB->findPackage(pkgA->name);
+        if (!pkgB) {
+            std::stringstream strStream;
+            strStream << "missing package " << pkgA->name;
+            emitDiffLine(apkB->getSource(), strStream.str());
+            diff = true;
+        } else {
+            if (pkgA->id != pkgB->id) {
+                std::stringstream strStream;
+                strStream << "package '" << pkgA->name << "' has different id (";
+                if (pkgB->id) {
+                    strStream << "0x" << std::hex << pkgB->id.value();
+                } else {
+                    strStream << "none";
+                }
+                strStream << " vs ";
+                if (pkgA->id) {
+                    strStream << "0x" << std::hex << pkgA->id.value();
+                } else {
+                    strStream << "none";
+                }
+                strStream << ")";
+                emitDiffLine(apkB->getSource(), strStream.str());
+                diff = true;
+            }
+            diff |= emitResourcePackageDiff(context, apkA, pkgA.get(), apkB, pkgB);
+        }
+    }
+
+    // Check for any newly added packages.
+    for (std::unique_ptr<ResourceTablePackage>& pkgB : tableB->packages) {
+        ResourceTablePackage* pkgA = tableA->findPackage(pkgB->name);
+        if (!pkgA) {
+            std::stringstream strStream;
+            strStream << "new package " << pkgB->name;
+            emitDiffLine(apkB->getSource(), strStream.str());
+            diff = true;
+        }
+    }
+    return diff;
+}
+
+int diff(const std::vector<StringPiece>& args) {
+    DiffContext context;
+
+    Flags flags;
+    if (!flags.parse("aapt2 diff", args, &std::cerr)) {
+        return 1;
+    }
+
+    if (flags.getArgs().size() != 2u) {
+        std::cerr << "must have two apks as arguments.\n\n";
+        flags.usage("aapt2 diff", &std::cerr);
+        return 1;
+    }
+
+    std::unique_ptr<LoadedApk> apkA = loadApkFromPath(&context, flags.getArgs()[0]);
+    std::unique_ptr<LoadedApk> apkB = loadApkFromPath(&context, flags.getArgs()[1]);
+    if (!apkA || !apkB) {
+        return 1;
+    }
+
+    if (emitResourceTableDiff(&context, apkA.get(), apkB.get())) {
+        // We emitted a diff, so return 1 (failure).
+        return 1;
+    }
+    return 0;
+}
+
+} // namespace aapt
diff --git a/tools/aapt2/link/ManifestFixer.cpp b/tools/aapt2/link/ManifestFixer.cpp
index 953e87e..77a949f 100644
--- a/tools/aapt2/link/ManifestFixer.cpp
+++ b/tools/aapt2/link/ManifestFixer.cpp
@@ -180,7 +180,6 @@
 
     manifestAction[u"uses-configuration"];
     manifestAction[u"uses-feature"];
-    manifestAction[u"uses-library"];
     manifestAction[u"supports-screens"];
     manifestAction[u"compatible-screens"];
     manifestAction[u"supports-gl-texture"];
@@ -189,6 +188,9 @@
     xml::XmlNodeAction& applicationAction = (*executor)[u"manifest"][u"application"];
     applicationAction.action(optionalNameIsJavaClassName);
 
+    // Uses library actions.
+    applicationAction[u"uses-library"];
+
     // Activity actions.
     applicationAction[u"activity"].action(requiredNameIsJavaClassName);
     applicationAction[u"activity"][u"intent-filter"] = intentFilterAction;
diff --git a/tools/aapt2/proto/TableProtoDeserializer.cpp b/tools/aapt2/proto/TableProtoDeserializer.cpp
index 82e4fb0..1ec48f0 100644
--- a/tools/aapt2/proto/TableProtoDeserializer.cpp
+++ b/tools/aapt2/proto/TableProtoDeserializer.cpp
@@ -234,6 +234,8 @@
                 const pb::Attribute& pbAttr = pbCompoundValue.attr();
                 std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(isWeak);
                 attr->typeMask = pbAttr.format_flags();
+                attr->minInt = pbAttr.min_int();
+                attr->maxInt = pbAttr.max_int();
                 for (const pb::Attribute_Symbol& pbSymbol : pbAttr.symbols()) {
                     Attribute::Symbol symbol;
                     deserializeItemCommon(pbSymbol, &symbol.symbol);
diff --git a/tools/fonts/fontchain_lint.py b/tools/fonts/fontchain_lint.py
index ea36e2c..7ec46a3e 100755
--- a/tools/fonts/fontchain_lint.py
+++ b/tools/fonts/fontchain_lint.py
@@ -256,13 +256,20 @@
 
 
 def check_emoji_coverage(all_emoji, equivalent_emoji):
+    emoji_font = get_emoji_font()
+    check_emoji_font_coverage(emoji_font, all_emoji, equivalent_emoji)
+
+
+def get_emoji_font():
     emoji_fonts = [
         record.font for record in _fallback_chain
         if 'Zsye' in record.scripts]
     assert len(emoji_fonts) == 1, 'There are %d emoji fonts.' % len(emoji_fonts)
-    emoji_font = emoji_fonts[0]
-    coverage = get_emoji_map(emoji_font)
+    return emoji_fonts[0]
 
+
+def check_emoji_font_coverage(emoji_font, all_emoji, equivalent_emoji):
+    coverage = get_emoji_map(emoji_font)
     for sequence in all_emoji:
         assert sequence in coverage, (
             '%s is not supported in the emoji font.' % printable(sequence))
@@ -322,15 +329,9 @@
     # Noto does not have monochrome glyphs for Unicode 7.0 wingdings and
     # webdings yet.
     missing_text_chars -= _chars_by_age['7.0']
-    # TODO: Remove these after b/26113320 is fixed
-    missing_text_chars -= {
-        0x263A, # WHITE SMILING FACE
-        0x270C, # VICTORY HAND
-        0x2744, # SNOWFLAKE
-        0x2764, # HEAVY BLACK HEART
-    }
     assert missing_text_chars == set(), (
-        'Text style version of some emoji characters are missing: ' + repr(missing_text_chars))
+        'Text style version of some emoji characters are missing: ' +
+            repr(missing_text_chars))
 
 
 # Setting reverse to true returns a dictionary that maps the values to sets of
@@ -350,7 +351,7 @@
             if not line:
                 continue
 
-            chars, prop = line.split(';')
+            chars, prop = line.split(';')[:2]
             chars = chars.strip()
             prop = prop.strip()
 
@@ -411,21 +412,6 @@
     _emoji_zwj_sequences = parse_unicode_datafile(
         path.join(ucd_path, 'emoji-zwj-sequences.txt'))
 
-    # filter modern pentathlon, as it seems likely to be removed from final spec
-    # also filter rifle
-    def is_excluded(n):
-        return n in [0x1f93b, 0x1f946]
-
-    def contains_excluded(t):
-        if type(t) == int:
-            return is_excluded(t)
-        return any(is_excluded(cp) for cp in t)
-
-    # filter modern pentathlon, as it seems likely to be removed from final spec
-    _emoji_properties['Emoji'] = set(
-        t for t in _emoji_properties['Emoji'] if not contains_excluded(t))
-    _emoji_sequences = dict(
-        (t, v) for (t, v) in _emoji_sequences.items() if not contains_excluded(t))
 
 def flag_sequence(territory_code):
     return tuple(0x1F1E6 + ord(ch) - ord('A') for ch in territory_code)
@@ -437,7 +423,8 @@
     flag_sequence('GF'), flag_sequence('GP'), flag_sequence('GS'),
     flag_sequence('MF'), flag_sequence('MQ'), flag_sequence('NC'),
     flag_sequence('PM'), flag_sequence('RE'), flag_sequence('TF'),
-    flag_sequence('WF'), flag_sequence('XK'), flag_sequence('YT'),
+    flag_sequence('UN'), flag_sequence('WF'), flag_sequence('XK'),
+    flag_sequence('YT'),
 })
 
 EQUIVALENT_FLAGS = {
@@ -450,6 +437,22 @@
 
 COMBINING_KEYCAP = 0x20E3
 
+# Characters that Android defaults to emoji style, different from the recommendations in UTR #51
+ANDROID_DEFAULT_EMOJI = frozenset({
+    0x2600, # BLACK SUN WITH RAYS
+    0x2601, # CLOUD
+    0x260E, # BLACK TELEPHONE
+    0x261D, # WHITE UP POINTING INDEX
+    0x263A, # WHITE SMILING FACE
+    0x2660, # BLACK SPADE SUIT
+    0x2663, # BLACK CLUB SUIT
+    0x2665, # BLACK HEART SUIT
+    0x2666, # BLACK DIAMOND SUIT
+    0x270C, # VICTORY HAND
+    0x2744, # SNOWFLAKE
+    0x2764, # HEAVY BLACK HEART
+})
+
 LEGACY_ANDROID_EMOJI = {
     0xFE4E5: flag_sequence('JP'),
     0xFE4E6: flag_sequence('US'),
@@ -483,24 +486,73 @@
     (0x1F468, 0x200D, 0x1F469, 0x200D, 0x1F466): 0x1F46A,
 }
 
+
+def is_fitzpatrick_modifier(cp):
+    return 0x1F3FB <= cp <= 0x1F3FF
+
+
+def reverse_emoji(seq):
+    rev = list(reversed(seq))
+    # if there are fitzpatrick modifiers in the sequence, keep them after
+    # the emoji they modify
+    for i in xrange(1, len(rev)):
+        if is_fitzpatrick_modifier(rev[i-1]):
+            rev[i], rev[i-1] = rev[i-1], rev[i]
+    return tuple(rev)
+
+
 def compute_expected_emoji():
     equivalent_emoji = {}
     sequence_pieces = set()
     all_sequences = set()
     all_sequences.update(_emoji_variation_sequences)
 
+    # add zwj sequences not in the current emoji-zwj-sequences.txt
+    adjusted_emoji_zwj_sequences = dict(_emoji_zwj_sequences)
+    adjusted_emoji_zwj_sequences.update(_emoji_zwj_sequences)
+    # single parent families
+    additional_emoji_zwj = (
+        (0x1F468, 0x200D, 0x1F466),
+        (0x1F468, 0x200D, 0x1F467),
+        (0x1F468, 0x200D, 0x1F466, 0x200D, 0x1F466),
+        (0x1F468, 0x200D, 0x1F467, 0x200D, 0x1F466),
+        (0x1F468, 0x200D, 0x1F467, 0x200D, 0x1F467),
+        (0x1F469, 0x200D, 0x1F466),
+        (0x1F469, 0x200D, 0x1F467),
+        (0x1F469, 0x200D, 0x1F466, 0x200D, 0x1F466),
+        (0x1F469, 0x200D, 0x1F467, 0x200D, 0x1F466),
+        (0x1F469, 0x200D, 0x1F467, 0x200D, 0x1F467),
+    )
+    # sequences formed from man and woman and optional fitzpatrick modifier
+    modified_extensions = (
+        0x2696,
+        0x2708,
+        0x1F3A8,
+        0x1F680,
+        0x1F692,
+    )
+    for seq in additional_emoji_zwj:
+        adjusted_emoji_zwj_sequences[seq] = 'Emoji_ZWJ_Sequence'
+    for ext in modified_extensions:
+        for base in (0x1F468, 0x1F469):
+            seq = (base, 0x200D, ext)
+            adjusted_emoji_zwj_sequences[seq] = 'Emoji_ZWJ_Sequence'
+            for modifier in range(0x1F3FB, 0x1F400):
+                seq = (base, modifier, 0x200D, ext)
+                adjusted_emoji_zwj_sequences[seq] = 'Emoji_ZWJ_Sequence'
+
     for sequence in _emoji_sequences.keys():
         sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
         all_sequences.add(sequence)
         sequence_pieces.update(sequence)
 
-    for sequence in _emoji_zwj_sequences.keys():
+    for sequence in adjusted_emoji_zwj_sequences.keys():
         sequence = tuple(ch for ch in sequence if ch != EMOJI_VS)
         all_sequences.add(sequence)
         sequence_pieces.update(sequence)
         # Add reverse of all emoji ZWJ sequences, which are added to the fonts
         # as a workaround to get the sequences work in RTL text.
-        reversed_seq = tuple(reversed(sequence))
+        reversed_seq = reverse_emoji(sequence)
         all_sequences.add(reversed_seq)
         equivalent_emoji[reversed_seq] = sequence
 
@@ -519,6 +571,7 @@
         set(LEGACY_ANDROID_EMOJI.keys()))
     default_emoji = (
         _emoji_properties['Emoji_Presentation'] |
+        ANDROID_DEFAULT_EMOJI |
         all_sequences |
         set(LEGACY_ANDROID_EMOJI.keys()))
 
@@ -536,8 +589,8 @@
 
 
 def main():
-    target_out = sys.argv[1]
     global _fonts_dir
+    target_out = sys.argv[1]
     _fonts_dir = path.join(target_out, 'fonts')
 
     fonts_xml_path = path.join(target_out, 'etc', 'fonts.xml')
diff --git a/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java b/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
index 04a59bc..0c3231b 100644
--- a/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
+++ b/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
@@ -78,7 +78,8 @@
     @Override
     public void addAppToken(int arg0, IApplicationToken arg1, int arg2, int arg3, int arg4,
             boolean arg5, boolean arg6, int arg7, int arg8, boolean arg9, boolean arg10,
-            Rect arg11, Configuration arg12, int arg13, boolean arg14, boolean arg15, int arg16)
+            Rect arg11, Configuration arg12, int arg13, boolean arg14, boolean arg15, int arg16,
+            int arg17)
             throws RemoteException {
         // TODO Auto-generated method stub
     }
@@ -95,7 +96,7 @@
     }
 
     @Override
-    public void clearForcedDisplayDensity(int displayId) throws RemoteException {
+    public void clearForcedDisplayDensityForUser(int displayId, int userId) throws RemoteException {
         // TODO Auto-generated method stub
     }
 
@@ -350,7 +351,13 @@
     }
 
     @Override
-    public void notifyAppStopped(IBinder token, boolean stopped) throws RemoteException {
+    public void notifyAppResumed(IBinder token, boolean wasStopped, boolean allowSavedSurface)
+            throws RemoteException {
+        // TODO Auto-generated method stub
+    }
+
+    @Override
+    public void notifyAppStopped(IBinder token) throws RemoteException {
         // TODO Auto-generated method stub
     }
 
@@ -390,7 +397,8 @@
     }
 
     @Override
-    public void setForcedDisplayDensity(int displayId, int density) throws RemoteException {
+    public void setForcedDisplayDensityForUser(int displayId, int density, int userId)
+            throws RemoteException {
         // TODO Auto-generated method stub
     }
 
@@ -452,6 +460,16 @@
     }
 
     @Override
+    public void setRecentsVisibility(boolean visible) {
+        // TODO Auto-generated method stub
+    }
+
+    @Override
+    public void setTvPipVisibility(boolean visible) {
+        // TODO Auto-generated method stub
+    }
+
+    @Override
     public void stopAppFreezingScreen(IBinder arg0, boolean arg1) throws RemoteException {
         // TODO Auto-generated method stub
     }
@@ -589,4 +607,9 @@
 
     @Override
     public void removeWallpaperInputConsumer() throws RemoteException {}
+
+    @Override
+    public Bitmap screenshotWallpaper() throws RemoteException {
+        return null;
+    }
 }
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeIInputMethodManager.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeIInputMethodManager.java
index 3f276c9..ab73a8b 100644
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeIInputMethodManager.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeIInputMethodManager.java
@@ -16,11 +16,13 @@
 
 package com.android.layoutlib.bridge.android;
 
+import com.android.internal.inputmethod.IInputContentUriToken;
 import com.android.internal.view.IInputContext;
 import com.android.internal.view.IInputMethodClient;
 import com.android.internal.view.IInputMethodManager;
 import com.android.internal.view.InputBindResult;
 
+import android.net.Uri;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.ResultReceiver;
@@ -239,4 +241,11 @@
         // TODO Auto-generated method stub
         return null;
     }
+
+    @Override
+    public IInputContentUriToken createInputContentUriToken(IBinder token, Uri contentUri,
+            String packageName) {
+        // TODO Auto-generated method stub
+        return null;
+    }
 }
diff --git a/wifi/java/android/net/wifi/WifiManager.java b/wifi/java/android/net/wifi/WifiManager.java
index d04a60e..433d2e0 100644
--- a/wifi/java/android/net/wifi/WifiManager.java
+++ b/wifi/java/android/net/wifi/WifiManager.java
@@ -49,6 +49,8 @@
  * This class provides the primary API for managing all aspects of Wi-Fi
  * connectivity. Get an instance of this class by calling
  * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.WIFI_SERVICE)}.
+ * On releases before NYC, it should only be obtained from an application context, and not from
+ * any other derived context to avoid memory leaks within the calling process.
 
  * It deals with several categories of items:
  * <ul>